diff --git a/ui-ngx/src/app/core/services/ai-solutions.service.ts b/ui-ngx/src/app/core/services/ai-solutions.service.ts index bbad67f1d3..91d46c1ffe 100644 --- a/ui-ngx/src/app/core/services/ai-solutions.service.ts +++ b/ui-ngx/src/app/core/services/ai-solutions.service.ts @@ -1,8 +1,8 @@ // SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. // SPDX-License-Identifier: BUSL-1.1 import { inject, Injectable, signal } from '@angular/core'; -import { Observable, of } from 'rxjs'; -import { map, switchMap, tap } from 'rxjs/operators'; +import { EMPTY, Observable, of, Subscription, timer } from 'rxjs'; +import { catchError, finalize, map, switchMap, takeWhile, tap } from 'rxjs/operators'; import { TranslateService } from '@ngx-translate/core'; import { ChatLevel } from '@shared/models/ai-chat.models'; import { SolutionsCreatorService } from '@core/http/solutions-creator.service'; @@ -15,9 +15,13 @@ import { SolutionUpdateRequest } from '@shared/models/solution-creator.models'; +const SOLUTIONS_POLL_MS = 5000; + @Injectable({providedIn: 'root'}) export class AiSolutionsService { + private following: Subscription | null = null; + private _solutions = signal([]); readonly solutions = this._solutions.asReadonly(); @@ -38,12 +42,32 @@ export class AiSolutionsService { next: solutions => { this._solutions.set(solutions); this._loaded.set(true); + this.followRunning(); }, error: () => this._loaded.set(true) }) ); } + keepRunning(solutionId: string | undefined): void { + if (!solutionId) { + return; + } + this.update(solutionId, {status: 'RUNNING', statusReason: undefined}); + this.followRunning(); + } + + followRunning(): void { + if (this.following) { + return; + } + this.following = timer(SOLUTIONS_POLL_MS, SOLUTIONS_POLL_MS).pipe( + takeWhile(() => this._solutions().some(solution => solution.status === 'RUNNING' && !this._running().has(solution.id))), + switchMap(() => this.load().pipe(catchError(() => EMPTY))), + finalize(() => this.following = null) + ).subscribe(); + } + setRunning(solutionId: string | undefined, running: boolean): void { if (!solutionId || this._running().has(solutionId) === running) { return; diff --git a/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.ts b/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.ts index e6d1662873..9514c7b0e0 100644 --- a/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.ts @@ -829,6 +829,7 @@ export class SolutionChatComponent extends PageComponent implements OnInit, OnDe } ngOnDestroy(): void { + this.keepRunningListed(); this.clearWork(); this.cancelPending$.next(); this.cancelPending$.complete(); @@ -1448,7 +1449,14 @@ export class SolutionChatComponent extends PageComponent implements OnInit, OnDe })); } + private keepRunningListed(): void { + if (this.solutionId && this.solutionId !== NEW_SOLUTION_ID && (this.loading() || this.isGenerating())) { + this.aiSolutions.keepRunning(this.solutionId); + } + } + private resetSolution(): void { + this.keepRunningListed(); this.clearWork(); this.pickedView.set(null); this.json.set(false);