From bd65304a668bc02c69a9af4c7bcb6f7eb3393e07 Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Wed, 4 Nov 2026 18:15:12 +0200 Subject: [PATCH] feat: AI chat: deny an approval with what should be different (from the card or the composer), status marks for chats that run or need the user, and Stop / Esc for a running reply --- .../server/controller/AiChatController.java | 9 ++ .../server/service/ai/chat/AiChatService.java | 2 + .../service/ai/chat/DefaultAiChatService.java | 6 ++ .../ai/chat/DefaultAiChatServiceTest.java | 16 ++++ ui-ngx/src/app/core/http/ai-chat.service.ts | 4 + .../src/app/core/services/ai-chats.service.ts | 45 ++++++++-- .../ai/ai-approval-card.component.html | 7 ++ .../ai/ai-approval-card.component.scss | 24 ++++++ .../ai/ai-approval-card.component.ts | 33 ++++++- .../ai/ai-chat-messages.component.html | 6 ++ .../ai/ai-chat-messages.component.ts | 5 ++ .../home/components/ai/ai-chat.component.html | 12 ++- .../home/components/ai/ai-chat.component.scss | 24 +++++- .../home/components/ai/ai-chat.component.ts | 86 +++++++++++++++++-- .../ai/ai-prompt-input.component.html | 40 ++++++--- .../ai/ai-prompt-input.component.scss | 14 +++ .../ai/ai-prompt-input.component.ts | 21 +++++ .../ai-assistant/ai-assistant.component.html | 4 +- .../ai-assistant/ai-assistant.component.ts | 16 +++- .../src/app/shared/models/ai-chat.models.ts | 14 ++- .../assets/locale/locale.constant-en_US.json | 8 ++ ui-ngx/src/theme/ai-theme.scss | 15 ++++ 22 files changed, 380 insertions(+), 31 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/AiChatController.java b/application/src/main/java/org/thingsboard/server/controller/AiChatController.java index bbd9366776..40e7cfabcb 100644 --- a/application/src/main/java/org/thingsboard/server/controller/AiChatController.java +++ b/application/src/main/java/org/thingsboard/server/controller/AiChatController.java @@ -65,6 +65,15 @@ class AiChatController extends BaseController { aiChatService.updateChat(chatId, request, user); } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @PostMapping("/{chatId}/stop") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void stopChat(@PathVariable UUID chatId) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + accessControlService.checkPermission(user, Resource.AI, Operation.WRITE); + aiChatService.stopChat(chatId, user); + } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") @GetMapping public JsonNode listChats() throws ThingsboardException { diff --git a/application/src/main/java/org/thingsboard/server/service/ai/chat/AiChatService.java b/application/src/main/java/org/thingsboard/server/service/ai/chat/AiChatService.java index 2eae20227a..b5d698af60 100644 --- a/application/src/main/java/org/thingsboard/server/service/ai/chat/AiChatService.java +++ b/application/src/main/java/org/thingsboard/server/service/ai/chat/AiChatService.java @@ -15,6 +15,8 @@ public interface AiChatService { void updateChat(UUID chatId, JsonNode request, SecurityUser user); + void stopChat(UUID chatId, SecurityUser user); + JsonNode listChats(SecurityUser user); JsonNode getChatMessages(UUID chatId, SecurityUser user); diff --git a/application/src/main/java/org/thingsboard/server/service/ai/chat/DefaultAiChatService.java b/application/src/main/java/org/thingsboard/server/service/ai/chat/DefaultAiChatService.java index 1b15fc8045..52bf9de418 100644 --- a/application/src/main/java/org/thingsboard/server/service/ai/chat/DefaultAiChatService.java +++ b/application/src/main/java/org/thingsboard/server/service/ai/chat/DefaultAiChatService.java @@ -56,6 +56,12 @@ class DefaultAiChatService implements AiChatService { new TbAiOperation(ChannelProtocol.CHAT_MESSAGES, new ChatOperationRequest(chatId, TOOL_CARDS)), user, tokenProvider), user, false); } + @Override + public void stopChat(UUID chatId, SecurityUser user) { + tbAiService.process(tokenProvider -> operations.execute( + new TbAiOperation(ChannelProtocol.CHAT_STOP, new ChatOperationRequest(chatId, null)), user, tokenProvider), user, false); + } + @Override public void deleteChat(UUID chatId, SecurityUser user) { tbAiService.process(tokenProvider -> operations.execute( diff --git a/application/src/test/java/org/thingsboard/server/service/ai/chat/DefaultAiChatServiceTest.java b/application/src/test/java/org/thingsboard/server/service/ai/chat/DefaultAiChatServiceTest.java index a5606a95bf..f97b8c79bb 100644 --- a/application/src/test/java/org/thingsboard/server/service/ai/chat/DefaultAiChatServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/ai/chat/DefaultAiChatServiceTest.java @@ -155,6 +155,22 @@ class DefaultAiChatServiceTest { then(tbAiService).shouldHaveNoMoreInteractions(); } + @Test + void shouldProcessWithoutCreditCheckAndDelegateToClient_whenStopChatCalled() { + // GIVEN + UUID chatId = UUID.randomUUID(); + given(tbAiService.process(any(), same(user), eq(false))).willReturn(null); + + // WHEN + service.stopChat(chatId, user); + + // THEN + assertThat(captureProcessCall().apply(tokenProvider)).isSameAs(tbAiResponse); + then(operations).should().execute(argThat(operation -> ChannelProtocol.CHAT_STOP.equals(operation.type()) + && Objects.equals(new ChatOperationRequest(chatId, null), operation.payload())), same(user), same(tokenProvider)); + then(tbAiService).shouldHaveNoMoreInteractions(); + } + @Test void shouldProcessStreamAndDelegateToTransportWithTurnContext_whenSendChatMessageWithClientContextCalled() { // GIVEN diff --git a/ui-ngx/src/app/core/http/ai-chat.service.ts b/ui-ngx/src/app/core/http/ai-chat.service.ts index 3044f6a0e8..bd70582ce2 100644 --- a/ui-ngx/src/app/core/http/ai-chat.service.ts +++ b/ui-ngx/src/app/core/http/ai-chat.service.ts @@ -62,6 +62,10 @@ export class AiChatService { * Follows the reply still running in the chat: a 'turnResumed' event, every event of the reply so far, then the * live ones. Completes with no events when nothing is running. */ + public stopChat(chatId: string, config?: RequestConfig): Observable { + return this.http.post(`/api/ai/chats/${chatId}/stop`, null, defaultHttpOptionsFromConfig(config)); + } + public resumeChat(chatId: string, config?: RequestConfig): Observable { return this.streamEvents(`/api/ai/chats/${chatId}/resume`, null, config); } diff --git a/ui-ngx/src/app/core/services/ai-chats.service.ts b/ui-ngx/src/app/core/services/ai-chats.service.ts index 192dfab1aa..7d9a060f5c 100644 --- a/ui-ngx/src/app/core/services/ai-chats.service.ts +++ b/ui-ngx/src/app/core/services/ai-chats.service.ts @@ -1,13 +1,13 @@ // SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. // SPDX-License-Identifier: BUSL-1.1 -import { inject, Injectable, signal } from '@angular/core'; +import { inject, Injectable, signal, untracked, WritableSignal } from '@angular/core'; import { Observable, of } from 'rxjs'; import { map, switchMap, tap } from 'rxjs/operators'; import { TranslateService } from '@ngx-translate/core'; import { AiChatService } from '@core/http/ai-chat.service'; import { AiAssistantPanelService } from '@core/services/ai-assistant-panel.service'; import { DialogService } from '@core/services/dialog.service'; -import { ChatInfo } from '@shared/models/ai-chat.models'; +import { ChatInfo, ChatLevel } from '@shared/models/ai-chat.models'; @Injectable({ providedIn: 'root' }) export class AiChatsService { @@ -26,6 +26,37 @@ export class AiChatsService { private _replying = signal>(new Set()); readonly replying = this._replying.asReadonly(); + private _waiting = signal>(new Set()); + + private _open = signal>(new Set()); + + levelOf(chat: ChatInfo): ChatLevel | null { + if (this._waiting().has(chat.id)) { + return 'needs'; + } + if (this._replying().has(chat.id)) { + return 'running'; + } + if (this._open().has(chat.id)) { + return null; + } + return chat.status === 'NEEDS_YOU' ? 'needs' : chat.status === 'RUNNING' ? 'running' : null; + } + + setWaiting(chatId: string | undefined, waiting: boolean): void { + this.toggle(this._waiting, chatId, waiting); + } + + setOpen(chatId: string | undefined, open: boolean): void { + this.toggle(this._open, chatId, open); + } + + refresh(): Observable { + return this.aiChat.listChats({ignoreLoading: true, ignoreErrors: true}).pipe( + tap(chats => this._chats.set(chats)) + ); + } + load(): Observable { return this.aiChat.listChats({ignoreLoading: true, ignoreErrors: true}).pipe( tap({ @@ -93,12 +124,16 @@ export class AiChatsService { } setReplying(chatId: string | undefined, replying: boolean): void { - if (!chatId) { + this.toggle(this._replying, chatId, replying); + } + + private toggle(target: WritableSignal>, chatId: string | undefined, on: boolean): void { + if (!chatId || untracked(target).has(chatId) === on) { return; } - this._replying.update(set => { + target.update(set => { const next = new Set(set); - if (replying) { + if (on) { next.add(chatId); } else { next.delete(chatId); diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.html b/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.html index ce8881931c..e781c64d4f 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.html @@ -18,6 +18,13 @@ {{ data.approveLabel ?? ('ai-assistant.approve' | translate) }} + @if (denying) { + + } + @if (canStop) { + + } @else { + + }
@@ -89,6 +100,13 @@
+ +
+ {{ 'ai-assistant.stop' | translate }} + Esc +
+
+
{{ 'ai-assistant.send' | translate }} diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.scss b/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.scss index da26d7dccd..2729daa8f2 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.scss +++ b/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.scss @@ -200,6 +200,20 @@ $chevron: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox= display: inline-flex; } + .composer-stop { + &:disabled { + opacity: .6; + cursor: default; + } + + .stop-glyph { + width: 10px; + height: 10px; + border-radius: 2px; + background: var(--ai-on-action); + } + } + .composer-send { position: relative; display: inline-flex; diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.ts index 884a95088d..57d2c852a5 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-prompt-input.component.ts @@ -83,6 +83,22 @@ export class AiPromptInputComponent implements OnChanges, AfterViewInit, OnDestr @Output() autoApproveChange = new EventEmitter(); + @Input() stoppable = false; + + @Input() stopping = false; + + @Output() stopped = new EventEmitter(); + + get canStop(): boolean { + return this.loading && this.stoppable; + } + + stop(): void { + if (this.canStop && !this.stopping) { + this.stopped.emit(); + } + } + @ViewChild('textarea') textarea!: ElementRef; @ViewChild('fileInput') fileInput!: ElementRef; @@ -158,6 +174,11 @@ export class AiPromptInputComponent implements OnChanges, AfterViewInit, OnDestr } onKeydown(event: KeyboardEvent): void { + if (event.key === 'Escape' && this.canStop) { + event.preventDefault(); + this.stop(); + return; + } if (event.key === 'Enter' && (!event.shiftKey || event.metaKey || event.ctrlKey)) { event.preventDefault(); this.send(); diff --git a/ui-ngx/src/app/modules/home/pages/ai-assistant/ai-assistant.component.html b/ui-ngx/src/app/modules/home/pages/ai-assistant/ai-assistant.component.html index eab5fd617f..57e7d0249f 100644 --- a/ui-ngx/src/app/modules/home/pages/ai-assistant/ai-assistant.component.html +++ b/ui-ngx/src/app/modules/home/pages/ai-assistant/ai-assistant.component.html @@ -58,8 +58,8 @@ {{ chat.title }} - @if (aiChats.replying().has(chat.id)) { - + @if (aiChats.levelOf(chat); as level) { + }