Browse Source

feat(ai): Stop in the solution chat (step chat, create, install), the stopped line with Resume repeating the step; solution stop endpoint proxied to the AI service

pull/16833/head
Igor Kulikov 3 weeks ago
parent
commit
56b8e0fc12
  1. 8
      application/src/main/java/org/thingsboard/server/controller/AiSolutionController.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/ai/solution/AiSolutionService.java
  3. 6
      application/src/main/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionService.java
  4. 16
      application/src/test/java/org/thingsboard/server/service/ai/solution/DefaultAiSolutionServiceTest.java
  5. 4
      ui-ngx/src/app/core/http/solutions-creator.service.ts
  6. 6
      ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.html
  7. 50
      ui-ngx/src/app/modules/home/components/ai/solution/solution-chat.component.ts
  8. 1
      ui-ngx/src/assets/locale/locale.constant-en_US.json

8
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,

2
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);

6
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(

16
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

4
ui-ngx/src/app/core/http/solutions-creator.service.ts

@ -37,6 +37,10 @@ export class SolutionsCreatorService {
return this.http.delete<void>(`/api/ai/solution/${solutionId}/${step}/clear`, defaultHttpOptionsFromConfig(config));
}
public stopSolution(solutionId: string, config?: RequestConfig): Observable<boolean> {
return this.http.post<boolean>(`/api/ai/solution/${solutionId}/stop`, null, defaultHttpOptionsFromConfig(config));
}
public createSolution(solutionId: string, config?: RequestConfig): Observable<SolutionCreatorInfo> {
return this.http.post<SolutionCreatorInfo>(`/api/ai/solution/${solutionId}/create`, '', defaultHttpOptionsFromConfig(config));
}

6
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)">
</tb-ai-chat-messages>
@if (pendingQuestions(); as questions) {
@ -170,7 +171,10 @@
}
<tb-ai-prompt-input
[placeholder]="activePlaceholder() | translate"
[loading]="loading() || isGenerating()"
[loading]="loading() || isGenerating() || making() === 'solution'"
[stoppable]="!!info()?.id"
[stopping]="stopping()"
(stopped)="stop()"
[pinned]="true"
[dropZone]="chatArea"
(messageSent)="sendMsgFromUser($event)">

50
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<SolutionCreatorInfo | null>(() => {
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;
}),

1
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",

Loading…
Cancel
Save