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 3bc73f3428..c24a3b7b71 100644 --- a/application/src/main/java/org/thingsboard/server/controller/AiSolutionController.java +++ b/application/src/main/java/org/thingsboard/server/controller/AiSolutionController.java @@ -110,6 +110,22 @@ public class AiSolutionController extends BaseController { aiSolutionService.clearStep(solutionId, step, user); } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @PostMapping(value = "/{solutionId}/update", consumes = MediaType.APPLICATION_JSON_VALUE) + public void updateSolution(@PathVariable UUID solutionId, @RequestBody JsonNode request) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + accessControlService.checkPermission(user, Resource.AI, Operation.WRITE); + aiSolutionService.updateSolution(solutionId, request, user); + } + + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @GetMapping("/{solutionId}/progress") + public JsonNode getInstallProgress(@PathVariable UUID solutionId) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + accessControlService.checkPermission(user, Resource.AI, Operation.READ); + return aiSolutionService.getInstallProgress(solutionId, user); + } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") @PostMapping("/{solutionId}/install") public JsonNode installSolution(@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 b0f7763131..83f557ebdd 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 @@ -31,4 +31,8 @@ public interface AiSolutionService { void deleteSolution(UUID solutionId, SecurityUser user); + void updateSolution(UUID solutionId, JsonNode request, SecurityUser user); + + JsonNode getInstallProgress(UUID solutionId, 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 c110699cbd..d044d42bdb 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 @@ -16,6 +16,7 @@ import org.thingsboard.ai.common.channel.SolutionChatRequest; import org.thingsboard.ai.common.channel.SolutionDataRequest; import org.thingsboard.ai.common.channel.SolutionOperationRequest; import org.thingsboard.ai.common.channel.SolutionStepRequest; +import org.thingsboard.ai.common.channel.SolutionUpdateRequest; import org.thingsboard.server.service.ai.TbAiService; import org.thingsboard.server.service.ai.transport.TbAiOperation; import org.thingsboard.server.service.ai.transport.TbAiOperations; @@ -100,4 +101,16 @@ public class DefaultAiSolutionService implements AiSolutionService { new TbAiOperation(ChannelProtocol.SOLUTION_DELETE, new SolutionOperationRequest(solutionId)), user, tokenProvider), user, false); } + @Override + public void updateSolution(UUID solutionId, JsonNode request, SecurityUser user) { + tbAiService.process(tokenProvider -> operations.execute( + new TbAiOperation(ChannelProtocol.SOLUTION_UPDATE, new SolutionUpdateRequest(solutionId, request)), user, tokenProvider), user, false); + } + + @Override + public JsonNode getInstallProgress(UUID solutionId, SecurityUser user) { + return tbAiService.process(tokenProvider -> operations.execute( + new TbAiOperation(ChannelProtocol.SOLUTION_PROGRESS, new SolutionOperationRequest(solutionId)), user, tokenProvider), user, false); + } + } diff --git a/ui-ngx/package.json b/ui-ngx/package.json index 5cc9d8d727..12e20acecc 100644 --- a/ui-ngx/package.json +++ b/ui-ngx/package.json @@ -26,6 +26,8 @@ "@auth0/angular-jwt": "^5.2.0", "@flowjs/flow.js": "^2.14.1", "@flowjs/ngx-flow": "20.0.2", + "@fontsource-variable/roboto": "^5.3.0", + "@fontsource-variable/roboto-mono": "^5.3.0", "@fullcalendar/angular": "^6.1.20", "@fullcalendar/core": "^6.1.20", "@fullcalendar/daygrid": "^6.1.20", @@ -103,8 +105,6 @@ "tinycolor2": "^1.6.0", "tooltipster": "^4.2.8", "tslib": "^2.8.1", - "typeface-roboto": "^1.1.13", - "typeface-roboto-mono": "^1.1.13", "yaml": "^2.6.1", "zone.js": "~0.15.1" }, 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 2cfc4b2bb2..b98b717664 100644 --- a/ui-ngx/src/app/core/http/solutions-creator.service.ts +++ b/ui-ngx/src/app/core/http/solutions-creator.service.ts @@ -9,8 +9,10 @@ import { SolutionDataKey, SolutionDataValues, SolutionInfo, + SolutionInstallProgress, SolutionInstallResult, - SolutionStep + SolutionStep, + SolutionUpdateRequest } from '@shared/models/solution-creator.models'; @Injectable({ @@ -64,4 +66,12 @@ export class SolutionsCreatorService { public deleteSolution(solutionId: string, config?: RequestConfig): Observable { return this.http.delete(`/api/ai/solution/${solutionId}`, defaultHttpOptionsFromConfig(config)); } + + public updateSolution(solutionId: string, request: SolutionUpdateRequest, config?: RequestConfig): Observable { + return this.http.post(`/api/ai/solution/${solutionId}/update`, request, defaultHttpOptionsFromConfig(config)); + } + + public getInstallProgress(solutionId: string, config?: RequestConfig): Observable { + return this.http.get(`/api/ai/solution/${solutionId}/progress`, defaultHttpOptionsFromConfig(config)); + } } 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 a85ad83d0f..7bd80d8590 100644 --- a/ui-ngx/src/app/core/services/ai-solutions.service.ts +++ b/ui-ngx/src/app/core/services/ai-solutions.service.ts @@ -4,9 +4,16 @@ import { inject, Injectable, signal } from '@angular/core'; import { Observable, of } from 'rxjs'; import { map, switchMap, 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'; import { DialogService } from '@core/services/dialog.service'; -import { SolutionCreatorInfo, SolutionInfo } from '@shared/models/solution-creator.models'; +import { + SolutionCreatorInfo, + SolutionInfo, + SolutionInstallProgress, + SolutionStep, + SolutionUpdateRequest +} from '@shared/models/solution-creator.models'; @Injectable({providedIn: 'root'}) export class AiSolutionsService { @@ -19,6 +26,7 @@ export class AiSolutionsService { private _running = signal>(new Set()); readonly running = this._running.asReadonly(); + private _failed = signal>(new Map()); private api = inject(SolutionsCreatorService); private dialog = inject(DialogService); @@ -40,6 +48,9 @@ export class AiSolutionsService { if (!solutionId || this._running().has(solutionId) === running) { return; } + if (running) { + this.update(solutionId, {status: undefined, statusReason: undefined}); + } this._running.update(set => { const next = new Set(set); if (running) { @@ -55,6 +66,43 @@ export class AiSolutionsService { return this._running().has(solutionId); } + setFailed(solutionId: string | undefined, reason: string | null): void { + if (!solutionId || this._failed().get(solutionId) === (reason ?? undefined)) { + return; + } + this._failed.update(map => { + const next = new Map(map); + if (reason) { + next.set(solutionId, reason); + } else { + next.delete(solutionId); + } + return next; + }); + } + + levelOf(solution: SolutionInfo, open = false): ChatLevel | null { + if (this._running().has(solution.id)) { + return 'running'; + } + if (this._failed().has(solution.id)) { + return 'error'; + } + if (open) { + return solution.status === 'FAILED' ? 'error' : null; + } + return solution.status === 'NEEDS_YOU' ? 'needs' : solution.status === 'RUNNING' ? 'running' : solution.status === 'FAILED' ? 'error' : null; + } + + tipOf(solution: SolutionInfo, open = false): string { + const level = this.levelOf(solution, open); + if (!level) { + return ''; + } + const reason = level === 'error' ? this._failed().get(solution.id) ?? solution.statusReason : undefined; + return reason || this.translate.instant('ai-assistant.status-tip-' + level); + } + find(solutionId: string | undefined): SolutionInfo | undefined { return solutionId ? this._solutions().find(solution => solution.id === solutionId) : undefined; } @@ -86,6 +134,20 @@ export class AiSolutionsService { ); } + setPinned(solution: SolutionInfo, pinned: boolean): Observable { + return this.api.updateSolution(solution.id, {pinned}, {ignoreLoading: true, ignoreErrors: true}).pipe( + tap(() => this.update(solution.id, {pinned})) + ); + } + + sendFeedback(solutionId: string, step: SolutionStep, feedback: SolutionUpdateRequest['feedback']): Observable { + return this.api.updateSolution(solutionId, {step, feedback}, {ignoreLoading: true}); + } + + installProgress(solutionId: string): Observable { + return this.api.getInstallProgress(solutionId, {ignoreLoading: true, ignoreErrors: true}); + } + confirmAndUninstall(solution: SolutionInfo): Observable { return this.dialog.confirm( this.translate.instant('solution-creator.uninstall-solution-dialog-title', {name: this.titleOf(solution)}), diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html index e9513e0c23..908ef1d7dd 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.html @@ -8,7 +8,9 @@ @if (dayLines[$index]; as day) { } - @if (msg.from === 'USER') { + @if (msg.from === 'USER' && msg.answers?.length) { + + } @else if (msg.from === 'USER') { @if (msg.startedFrom) { } @@ -79,14 +81,23 @@ } } @else { -
+
+ @if (msg.done) { + + }
- + @if (msg.lead) { +

{{ msg.lead }}

+ } + @if (summaries[$index]; as entities) { }
+ @if (msg.note) { +

{{ msg.note }}

+ }
@if (msg.time) { diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.scss b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.scss index 84c3681008..e403ef684e 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.scss +++ b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.scss @@ -220,6 +220,43 @@ .agent-body { min-width: 0; } + + &.done { + display: flex; + align-items: flex-start; + gap: 8px; + + > .done-mark { + flex: none; + margin-top: 2px; + font-size: 20px; + width: 20px; + height: 20px; + line-height: 20px; + color: var(--tb-ok-text); + } + + > .agent-body { + flex: 1; + } + } + + .lead { + margin: 0 0 14px; + font-size: var(--chat-text); + line-height: var(--chat-text-lh); + letter-spacing: var(--chat-text-ls); + font-weight: 500; + color: var(--tb-ink); + } + } + + .agent-note { + margin: 14px 0 4px; + letter-spacing: .25px; + font-size: var(--chat-line); + line-height: var(--chat-line-lh); + color: var(--k-grey); } .incomplete { @@ -311,6 +348,14 @@ margin: 0 0 0 6px; } + &::ng-deep .ai .agent-body:has(> .lead) .tb-markdown-view [class*="tb-markdown-view-"] ol { + padding-left: 20px; + + li + li { + margin-top: 6px; + } + } + &::ng-deep .ai .agent-body .tb-markdown-view { font-family: var(--tb-font); font-size: var(--chat-text); diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.ts index 1ef842308a..55b944e365 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-chat-messages.component.ts @@ -113,6 +113,10 @@ export class AiChatMessagesComponent implements OnChanges, OnDestroy { this.unfolded.add(index); } + withoutDoneMark(content: string): string { + return (content ?? '').replace(/^\s*(✅|🛠️|🛠|🤔|💡|🛑)\s*/u, ''); + } + copyAnswer(answer: HTMLElement, index: number): void { const text = (answer.querySelector('tb-markdown') ?? answer).innerText.trim(); if (this.clipboard.copyFromContent(text)) { diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-question.component.html b/ui-ngx/src/app/modules/home/components/ai/ai-question.component.html new file mode 100644 index 0000000000..3f66bb620c --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/ai/ai-question.component.html @@ -0,0 +1,76 @@ + +@if (answered(); as answers) { +
+
+ {{ 'ai-assistant.question.you-said' | translate }} +
+ @if (time()) { + + } + @for (item of answers; track item.question.id) { +
+ {{ item.question.question }} + +
+ } +
+} @else if (current(); as question) { +
+
+ {{ 'ai-assistant.question.needs-answer' | translate }} + @if (questions().length > 1) { + {{ 'ai-assistant.question.count' | translate: {at: at() + 1, of: questions().length} }} + } +
+ @for (step of [question]; track step.id) { +
+
+ {{ step.question }} + @if (step.why) { + {{ step.why }} + } +
+
+
+ @for (option of step.options; track option.label) { + + } + +
+
+
+ } +
+ @if (at() > 0) { + + } + + + +
+
+} diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-question.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-question.component.ts new file mode 100644 index 0000000000..043491bafa --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/ai/ai-question.component.ts @@ -0,0 +1,108 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +import { Component, computed, EventEmitter, input, Output, signal } from '@angular/core'; +import { AiQuestion, AiQuestionAnswer } from '@shared/models/solution-creator.models'; + +@Component({ + selector: 'tb-ai-question', + templateUrl: './ai-question.component.html', + standalone: false +}) +export class AiQuestionComponent { + + questions = input.required(); + answered = input(null); + disabled = input(false); + time = input(undefined); + + @Output() done = new EventEmitter(); + @Output() skip = new EventEmitter(); + + at = signal(0); + move = signal<'next' | 'back' | null>(null); + private picked = signal>({}); + private typed = signal>({}); + + current = computed(() => this.questions()[Math.min(this.at(), this.questions().length - 1)]); + last = computed(() => this.at() >= this.questions().length - 1); + + valueOf(question: AiQuestion): string { + const picked = this.picked()[question.id]; + if (picked !== undefined) { + return picked; + } + return question.options.some(option => option.label === question.recommended) ? question.recommended : question.options[0]?.label ?? ''; + } + + isOther(question: AiQuestion): boolean { + return this.picked()[question.id] === null; + } + + otherText(question: AiQuestion): string { + return this.typed()[question.id] ?? ''; + } + + ready(question: AiQuestion): boolean { + return this.isOther(question) ? !!this.otherText(question).trim() : !!this.valueOf(question); + } + + pick(question: AiQuestion, label: string): void { + this.picked.update(values => ({...values, [question.id]: label})); + } + + pickOther(question: AiQuestion): void { + if (!this.isOther(question)) { + this.picked.update(values => ({...values, [question.id]: null})); + } + } + + type(question: AiQuestion, area: HTMLTextAreaElement): void { + this.pickOther(question); + this.typed.update(values => ({...values, [question.id]: area.value})); + area.style.height = 'auto'; + area.style.height = area.scrollHeight + 'px'; + } + + next(): void { + const question = this.current(); + if (!question || !this.ready(question) || this.disabled()) { + return; + } + if (this.last()) { + this.done.emit(this.answers()); + } else { + this.move.set('next'); + this.at.update(index => index + 1); + } + } + + back(): void { + this.move.set('back'); + this.at.update(index => Math.max(0, index - 1)); + } + + skipAll(): void { + if (!this.disabled()) { + this.skip.emit(this.questions().map(question => ({question, answer: this.defaultOf(question)}))); + } + } + + onEnter(event: Event): void { + const keyboard = event as KeyboardEvent; + if (!keyboard.shiftKey && !keyboard.isComposing) { + event.preventDefault(); + this.next(); + } + } + + private answers(): AiQuestionAnswer[] { + return this.questions().map(question => ({ + question, + answer: this.isOther(question) ? this.otherText(question).trim() : this.valueOf(question) + })); + } + + private defaultOf(question: AiQuestion): string { + return question.options.some(option => option.label === question.recommended) ? question.recommended : question.options[0]?.label ?? ''; + } +} diff --git a/ui-ngx/src/app/modules/home/components/ai/ai.module.ts b/ui-ngx/src/app/modules/home/components/ai/ai.module.ts index 376819b752..8dd9f5c513 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai.module.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai.module.ts @@ -23,6 +23,7 @@ import { AiRunSummaryComponent } from '@home/components/ai/ai-run-summary.compon import { AiStartedFromComponent } from '@home/components/ai/ai-started-from.component'; import { AiAssistantButtonComponent } from '@home/components/ai/ai-assistant-button.component'; import { AiRecordComponent } from '@home/components/ai/ai-record.component'; +import { AiQuestionComponent } from '@home/components/ai/ai-question.component'; import { AiSparkComponent } from '@home/components/ai/ai-spark.component'; @NgModule({ @@ -47,7 +48,8 @@ import { AiSparkComponent } from '@home/components/ai/ai-spark.component'; AiStartedFromComponent, AiAssistantButtonComponent, AiRecordComponent, - AiSparkComponent + AiSparkComponent, + AiQuestionComponent ], imports: [ CommonModule, @@ -67,7 +69,8 @@ import { AiSparkComponent } from '@home/components/ai/ai-spark.component'; AiCreditsComponent, AiLoadingModalComponent, AiNoTelemetryModalComponent, - AiAttachmentsComponent + AiAttachmentsComponent, + AiQuestionComponent ] }) export class AiModule {} diff --git a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.html b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.html index 45d1200b9a..eb53260529 100644 --- a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.html +++ b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.html @@ -4,7 +4,8 @@ SPDX-License-Identifier: BUSL-1.1 --> -@if (installed() && !bannerClosed()) { +@if (creating()) { +} @else if (installed() && !bannerClosed()) {
@@ -24,7 +25,7 @@ } @else if (!installed()) {

{{ 'solution-creator.pane.not-installed' | translate }}

} -
+
@for (section of sections(); track section.type) {

{{ section.title | translate }}

diff --git a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.scss b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.scss index a4bcb75625..b3848e17b9 100644 --- a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.scss +++ b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.scss @@ -155,3 +155,23 @@ outline-offset: 1px; } } + +.sol-infos.creating tbody tr { + animation: sol-row-in .2s cubic-bezier(.2, 0, 0, 1); +} + +@keyframes sol-row-in { + from { + opacity: 0; + } + + to { + opacity: 1; + } +} + +@media (prefers-reduced-motion: reduce) { + .sol-infos.creating tbody tr { + animation: none; + } +} diff --git a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.ts b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.ts index 3e443654cf..871d2b45dc 100644 --- a/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/solution/pane/solution-installed.component.ts @@ -50,6 +50,7 @@ const SECTIONS: EntityType[] = [ export class SolutionInstalledComponent { solution = input.required(); + creating = input(false); @Output() openMain = new EventEmitter(); 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 0c49096e0c..3c751f1cca 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 @@ -85,13 +85,21 @@