From 3e5ae00df341aabbfd988f9cedabfca578cbbfd7 Mon Sep 17 00:00:00 2001 From: Mutugi <48474421+Mutugiii@users.noreply.github.com> Date: Fri, 27 Sep 2024 22:51:06 +0300 Subject: [PATCH] courses: enabling context chat (fixes #7578) (#7579) Co-authored-by: dogi --- package.json | 6 ++-- src/app/chat/chat.component.ts | 22 ++++-------- .../courses-step-view.component.html | 2 +- .../courses-step-view.component.ts | 16 ++++++--- .../view-courses/courses-view.component.html | 3 -- src/app/shared/chat.service.ts | 36 ++++++++++++------- 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 3d2661d8fe..fc66a19dbb 100755 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "planet", "license": "AGPL-3.0", - "version": "0.14.79", + "version": "0.14.80", "myplanet": { - "latest": "v0.19.55", - "min": "v0.18.55" + "latest": "v0.19.65", + "min": "v0.18.65" }, "scripts": { "ng": "ng", diff --git a/src/app/chat/chat.component.ts b/src/app/chat/chat.component.ts index f55d64ed6b..2d5a4ef03f 100644 --- a/src/app/chat/chat.component.ts +++ b/src/app/chat/chat.component.ts @@ -2,7 +2,6 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { ChatService } from '../shared/chat.service'; -import { AIServices, ProviderName } from './chat.model'; @Component({ selector: 'planet-chat', @@ -11,7 +10,7 @@ import { AIServices, ProviderName } from './chat.model'; }) export class ChatComponent implements OnInit { activeService: string; - aiServices: { name: ProviderName, value: ProviderName }[] = []; + aiServices: { name: string, value: string }[] = []; displayToggle: boolean; constructor( @@ -21,20 +20,11 @@ export class ChatComponent implements OnInit { ) {} ngOnInit() { - this.chatService.listAIProviders().subscribe((services: AIServices | null) => { - if (services) { - for (const [ key, value ] of Object.entries(services)) { - if (value === true) { - this.aiServices.push({ - name: key as ProviderName, - value: key as ProviderName - }); - } - } - this.activeService = this.aiServices[0].value; - this.displayToggle = this.aiServices.length > 0; - this.chatService.toggleAIServiceSignal(this.activeService); - } + this.chatService.listAIProviders().subscribe((providers) => { + this.aiServices = providers; + this.activeService = this.aiServices[0]?.value; + this.displayToggle = this.aiServices.length > 0; + this.chatService.toggleAIServiceSignal(this.activeService); }); } diff --git a/src/app/courses/step-view-courses/courses-step-view.component.html b/src/app/courses/step-view-courses/courses-step-view.component.html index 3fc30348ea..1a9813c531 100644 --- a/src/app/courses/step-view-courses/courses-step-view.component.html +++ b/src/app/courses/step-view-courses/courses-step-view.component.html @@ -5,7 +5,7 @@

Step {{stepNum}}: {{stepDetail.stepTitle}}

- diff --git a/src/app/courses/step-view-courses/courses-step-view.component.ts b/src/app/courses/step-view-courses/courses-step-view.component.ts index 6cf460c048..36a02ddbc8 100644 --- a/src/app/courses/step-view-courses/courses-step-view.component.ts +++ b/src/app/courses/step-view-courses/courses-step-view.component.ts @@ -10,6 +10,7 @@ import { SubmissionsService } from '../../submissions/submissions.service'; import { ResourcesService } from '../../resources/resources.service'; import { DialogsSubmissionsComponent } from '../../shared/dialogs/dialogs-submissions.component'; import { StateService } from '../../shared/state.service'; +import { ChatService } from '../../shared/chat.service'; @Component({ templateUrl: './courses-step-view.component.html', @@ -36,17 +37,19 @@ export class CoursesStepViewComponent implements OnInit, OnDestroy { countActivity = true; isGridView = true; showChat = false; + isOpenai = false; @ViewChild(MatMenuTrigger) previewButton: MatMenuTrigger; constructor( + private chatService: ChatService, + private coursesService: CoursesService, + private dialog: MatDialog, + private resourcesService: ResourcesService, private router: Router, private route: ActivatedRoute, - private dialog: MatDialog, - private coursesService: CoursesService, - private userService: UserService, + private stateService: StateService, private submissionsService: SubmissionsService, - private resourcesService: ResourcesService, - private stateService: StateService + private userService: UserService, ) {} ngOnInit() { @@ -74,6 +77,9 @@ export class CoursesStepViewComponent implements OnInit, OnDestroy { this.coursesService.requestCourse({ courseId: this.courseId, parent: this.parent }); }); this.resourcesService.requestResourcesUpdate(this.parent); + this.chatService.listAIProviders().subscribe((providers) => { + this.isOpenai = providers.some(provider => provider.value === 'openai'); + }); } getSubmission() { diff --git a/src/app/courses/view-courses/courses-view.component.html b/src/app/courses/view-courses/courses-view.component.html index 535155e673..3e7011a7bb 100644 --- a/src/app/courses/view-courses/courses-view.component.html +++ b/src/app/courses/view-courses/courses-view.component.html @@ -37,9 +37,6 @@

{{courseDetail.courseTitle}}

- diff --git a/src/app/shared/chat.service.ts b/src/app/shared/chat.service.ts index e8385ca02b..06b8c6ea58 100644 --- a/src/app/shared/chat.service.ts +++ b/src/app/shared/chat.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { BehaviorSubject, Observable, Subject, of } from 'rxjs'; -import { catchError } from 'rxjs/operators'; +import { catchError, map, shareReplay } from 'rxjs/operators'; import { environment } from '../../environments/environment'; import { findDocuments, inSelector } from '../shared/mangoQueries'; @@ -22,12 +22,12 @@ import { AIServices } from '../chat/chat.model'; private newChatSelected: Subject = new Subject(); private toggleAIService = new Subject(); private selectedConversationIdSubject = new BehaviorSubject(null); - private aiProvidersSubject = new BehaviorSubject(null); - aiProviders$ = this.aiProvidersSubject.asObservable(); + private aiProvidersSubject = new BehaviorSubject>([]); newChatAdded$ = this.newChatAdded.asObservable(); newChatSelected$ = this.newChatSelected.asObservable(); toggleAIService$ = this.toggleAIService.asObservable(); + aiProviders$ = this.aiProvidersSubject.asObservable(); selectedConversationId$: Observable = this.selectedConversationIdSubject.asObservable(); @@ -51,17 +51,29 @@ import { AIServices } from '../chat/chat.model'; } private fetchAIProviders() { - this.httpClient.get(`${this.baseUrl}/checkproviders`).pipe( - catchError(err => { - console.error(err); - return of({ openai: false, perplexity: false, gemini: false }); - }) - ).subscribe((services: AIServices) => { - this.aiProvidersSubject.next(services); - }); + this.httpClient + .get(`${this.baseUrl}/checkproviders`) + .pipe( + catchError((err) => { + console.error(err); + return of({ openai: false, perplexity: false, gemini: false }); + }), + map((services: AIServices) => { + if (services) { + return Object.entries(services) + .filter(([ _, value ]) => value === true) + .map(([ key ]) => ({ name: key, value: key })); + } else { + return []; + } + }) + ) + .subscribe((providers) => { + this.aiProvidersSubject.next(providers); + }); } - listAIProviders() { + listAIProviders(): Observable> { return this.aiProviders$; }