diff --git a/application/src/main/java/org/thingsboard/server/controller/AiSolutionController.java b/application/src/main/java/org/thingsboard/server/controller/AiSolutionController.java index c24a3b7b71..14fc76ee3a 100644 --- a/application/src/main/java/org/thingsboard/server/controller/AiSolutionController.java +++ b/application/src/main/java/org/thingsboard/server/controller/AiSolutionController.java @@ -92,6 +92,14 @@ public class AiSolutionController extends BaseController { return aiSolutionService.createSolution(solutionId, user); } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @PostMapping("/{solutionId}/stop") + public JsonNode stopSolution(@PathVariable UUID solutionId) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + accessControlService.checkPermission(user, Resource.AI, Operation.WRITE); + return aiSolutionService.stopSolution(solutionId, user); + } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") @PutMapping("/{solutionId}/{dataKey}") public JsonNode updateData(@PathVariable UUID solutionId, diff --git a/application/src/main/java/org/thingsboard/server/service/ai/solution/AiSolutionService.java b/application/src/main/java/org/thingsboard/server/service/ai/solution/AiSolutionService.java index 83f557ebdd..5e5582df98 100644 --- a/application/src/main/java/org/thingsboard/server/service/ai/solution/AiSolutionService.java +++ b/application/src/main/java/org/thingsboard/server/service/ai/solution/AiSolutionService.java @@ -25,6 +25,8 @@ public interface AiSolutionService { void clearStep(UUID solutionId, SolutionStep step, SecurityUser user); + JsonNode stopSolution(UUID solutionId, SecurityUser user); + JsonNode installSolution(UUID solutionId, String tbAccessToken, String clientOrigin, SecurityUser user); JsonNode uninstallSolution(UUID solutionId, String tbAccessToken, String clientOrigin, SecurityUser user); diff --git a/application/src/main/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionService.java b/application/src/main/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionService.java index d044d42bdb..667bcfc630 100644 --- a/application/src/main/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionService.java +++ b/application/src/main/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionService.java @@ -81,6 +81,12 @@ public class DefaultAiSolutionService implements AiSolutionService { new TbAiOperation(ChannelProtocol.SOLUTION_STEP_CLEAR, new SolutionStepRequest(solutionId, step)), user, tokenProvider), user, false); } + @Override + public JsonNode stopSolution(UUID solutionId, SecurityUser user) { + return tbAiService.process(tokenProvider -> operations.execute( + new TbAiOperation(ChannelProtocol.SOLUTION_STOP, new SolutionOperationRequest(solutionId)), user, tokenProvider), user, false); + } + @Override public JsonNode installSolution(UUID solutionId, String tbAccessToken, String clientOrigin, SecurityUser user) { return tbAiService.process(tokenProvider -> operations.execute( diff --git a/application/src/test/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionServiceTest.java b/application/src/test/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionServiceTest.java index 723dc0131e..73cab7c1dd 100644 --- a/application/src/test/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionServiceTest.java @@ -214,6 +214,22 @@ class DefaultAiSolutionServiceTest { then(tbAiService).shouldHaveNoMoreInteractions(); } + @Test + void shouldProcessWithoutCreditCheckAndDelegateToClient_whenStopSolutionCalled() { + // GIVEN + UUID solutionId = UUID.randomUUID(); + given(tbAiService.process(any(), same(user), eq(false))).willReturn(null); + + // WHEN + service.stopSolution(solutionId, user); + + // THEN + assertThat(captureProcessCallWithoutCreditCheck().apply(tokenProvider)).isSameAs(tbAiResponse); + then(operations).should().execute(argThat(operation -> ChannelProtocol.SOLUTION_STOP.equals(operation.type()) + && Objects.equals(new SolutionOperationRequest(solutionId), operation.payload())), same(user), same(tokenProvider)); + then(tbAiService).shouldHaveNoMoreInteractions(); + } + @Test void shouldProcessWithoutCreditCheckAndDelegateToClient_whenInstallSolutionCalled() { // GIVEN diff --git a/ui-ngx/src/app/core/http/solutions-creator.service.ts b/ui-ngx/src/app/core/http/solutions-creator.service.ts index b98b717664..4cd06c68e3 100644 --- a/ui-ngx/src/app/core/http/solutions-creator.service.ts +++ b/ui-ngx/src/app/core/http/solutions-creator.service.ts @@ -37,6 +37,10 @@ export class SolutionsCreatorService { return this.http.delete(`/api/ai/solution/${solutionId}/${step}/clear`, defaultHttpOptionsFromConfig(config)); } + public stopSolution(solutionId: string, config?: RequestConfig): Observable { + return this.http.post(`/api/ai/solution/${solutionId}/stop`, null, defaultHttpOptionsFromConfig(config)); + } + public createSolution(solutionId: string, config?: RequestConfig): Observable { return this.http.post(`/api/ai/solution/${solutionId}/create`, '', defaultHttpOptionsFromConfig(config)); } diff --git a/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.html b/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.html index 5c5f849932..23280027c7 100644 --- a/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.html @@ -86,6 +86,7 @@ (rate)="onRate($event)" (report)="onReport($event)" (chip)="onChip($event)" + (resume)="resume()" (resourceError)="showErrorMessage($event)"> @if (pendingQuestions(); as questions) { @@ -170,7 +171,10 @@ } 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 788275534c..cb2c915712 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 @@ -208,6 +208,10 @@ export class SolutionChatComponent extends PageComponent implements OnInit, OnDe private installOnly = signal(false); private messageSteps: SolutionStep[] = []; + stopping = signal(false); + + private stoppedAction: (() => void) | null = null; + progressInfo = computed(() => { const progress = this.installProgress(); const info = this.info(); @@ -769,6 +773,40 @@ export class SolutionChatComponent extends PageComponent implements OnInit, OnDe this.aboutOpen.set(false); } + stop(): void { + if (!this.solutionId || this.stopping()) { + return; + } + this.stopping.set(true); + this.solutionCreator.stopSolution(this.solutionId, {ignoreLoading: true, ignoreErrors: true}).subscribe({ + next: stopped => { + if (!stopped) { + this.stopping.set(false); + } + }, + error: () => this.stopping.set(false) + }); + } + + resume(): void { + const action = this.stoppedAction; + this.stoppedAction = null; + this.messages.update(list => list.filter(message => (message as GeneralChatMessage).status !== 'STOPPED')); + action?.(); + } + + private wasStopped(err: any, again: () => void): boolean { + if (!this.stopping() || err?.status !== 409) { + return false; + } + this.stopping.set(false); + this.stoppedAction = again; + this.messageSteps = [...this.messageSteps, this.solutionStep()]; + this.messages.update(list => [...list, {from: 'AI', content: this.translate.instant('solution-creator.stopped'), status: 'STOPPED', time: Date.now()}]); + setTimeout(() => this.scrollLogToEnd(true)); + return true; + } + private reportFailure(err: any, at?: 'install' | 'step'): void { this.failedAt.set(at ?? (this.creatorStep() === 'result' ? 'install' : 'step')); const responseType = isNotEmptyStr(err?.error) ? 'text' : undefined; @@ -871,7 +909,9 @@ export class SolutionChatComponent extends PageComponent implements OnInit, OnDe this.showInstalled(); }, error: (err) => { - this.reportFailure(err); + if (!this.wasStopped(err, () => this.nextStep())) { + this.reportFailure(err); + } } }); } @@ -907,7 +947,9 @@ export class SolutionChatComponent extends PageComponent implements OnInit, OnDe this.showInstalled(); }, error: (err) => { - this.reportFailure(err, 'install'); + if (!this.wasStopped(err, () => this.installSolution())) { + this.reportFailure(err, 'install'); + } } }); } @@ -1201,7 +1243,9 @@ export class SolutionChatComponent extends PageComponent implements OnInit, OnDe } }), catchError(err => { - this.showErrorMessage(err); + if (!this.wasStopped(err, () => this.sendMessage(msg, resources).subscribe(() => {}))) { + this.showErrorMessage(err); + } this.rememberSolution(); return EMPTY; }), diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index f4b7af16e9..04a678874f 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -11829,6 +11829,7 @@ "select-recaptcha-enterprise-service-account-file": "Drag and drop your account credentials file" }, "solution-creator": { + "stopped": "You stopped this step. Nothing was changed.", "pane": { "alarms": "Alarms", "attribute": "attribute",