From 6044477fcf6c5b348e705ddaba7405528a50f3f4 Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Fri, 27 Nov 2026 12:04:28 +0200 Subject: [PATCH] feat: AI chat connectivity fixes from live testing: the nothing-heard row offers the case's own paste chip that fills the composer and reads 'It is on now, keep waiting', its threshold capped at five minutes; a file the assistant wrote sits inside the hand-over step that uses it; a wait that started before its card shows after it; a reloaded turn keeps each tool's start time; record cards waiting for data re-check until the device is active; a gateway's arrival reads that it connected and JSON values stay out of readings; expanding a disclosure, a folded code block, unchanged review fields or a done card scrolls it clear of the composer; every message sends the browser's operating system in the client context; long answers wrap in the answered-questions card --- .../services/ai-assistant-panel.service.ts | 6 +-- .../components/ai/ai-action-line.component.ts | 3 ++ .../home/components/ai/ai-action.models.ts | 23 +++++++++-- .../ai/ai-approval-card.component.html | 2 +- .../ai/ai-approval-card.component.ts | 8 ++++ .../ai/ai-assistant-context.service.ts | 4 +- .../ai/ai-chat-messages.component.html | 11 +++++- .../ai/ai-chat-messages.component.ts | 35 ++++++++++++++++- .../home/components/ai/ai-chat.component.ts | 12 +++--- .../home/components/ai/ai-code-block.ts | 4 ++ .../components/ai/ai-disclosure.component.ts | 39 ++++++++++++++++++- .../components/ai/ai-hand-over.component.html | 22 +++++++++-- .../components/ai/ai-hand-over.component.ts | 21 +++++++++- .../components/ai/ai-records.component.ts | 15 +++++-- .../src/app/shared/models/ai-chat.models.ts | 9 +++++ .../assets/locale/locale.constant-en_US.json | 3 +- ui-ngx/src/theme/ai-theme.scss | 10 +++++ 17 files changed, 198 insertions(+), 29 deletions(-) diff --git a/ui-ngx/src/app/core/services/ai-assistant-panel.service.ts b/ui-ngx/src/app/core/services/ai-assistant-panel.service.ts index 69f29d3248..e45637b55a 100644 --- a/ui-ngx/src/app/core/services/ai-assistant-panel.service.ts +++ b/ui-ngx/src/app/core/services/ai-assistant-panel.service.ts @@ -5,7 +5,7 @@ import { defer, Observable, Subject } from 'rxjs'; import { distinctUntilChanged, filter, finalize, map } from 'rxjs/operators'; import { NavigationEnd, NavigationStart, Router } from '@angular/router'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; -import { AiAssistantPanelConfig, AiAssistantViewType, AiChatClientContext, AiChatView, ChatInfo, AiPromptMessage } from '@shared/models/ai-chat.models'; +import { AiAssistantPanelConfig, AiAssistantViewType, AiChatClientContext, aiClientOs, AiChatView, ChatInfo, AiPromptMessage } from '@shared/models/ai-chat.models'; import { EntityId } from '@shared/models/id/entity-id'; import { getDefaultTimezone } from '@shared/models/time/time.models'; import { LocalStorageService } from '@core/local-storage/local-storage.service'; @@ -397,7 +397,7 @@ export class AiAssistantPanelService implements OnDestroy { } setClientContextForView(view: AiChatView): void { - this.setClientContext({view, timeZone: getDefaultTimezone()}); + this.setClientContext({view, timeZone: getDefaultTimezone(), os: aiClientOs()}); } focusWhile(view: AiChatView, name: string | null | undefined, until: Observable): Observable { @@ -406,7 +406,7 @@ export class AiAssistantPanelService implements OnDestroy { if (!previous || !view.entityId?.id) { return until; } - const focus = {context: {view, timeZone: getDefaultTimezone()}, previous, previousName: this._viewName()}; + const focus = {context: {view, timeZone: getDefaultTimezone(), os: aiClientOs()}, previous, previousName: this._viewName()}; this._focus = focus; this._clientContext.set(focus.context); this._viewName.set(name?.trim() || null); diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-action-line.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-action-line.component.ts index b805eae38a..7b165268f1 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-action-line.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-action-line.component.ts @@ -126,6 +126,9 @@ export class AiActionLineComponent { return null; } const time = new Date(action.startedAt + (action.durationMs ?? 0)).toLocaleTimeString([], {hour: '2-digit', minute: '2-digit', hourCycle: 'h23'}); + if (check.gateway) { + return {text: this.translate.instant('ai-assistant.hand-over.gateway-connected', {name: check.name, time}), readings: []}; + } return { text: this.translate.instant('ai-assistant.hand-over.' + (!check.keyCount ? 'arrived-message' : check.keyCount === 1 ? 'arrived-key' : 'arrived-keys') + (check.name ? '-from' : ''), {name: check.name, time, count: check.keyCount}), diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-action.models.ts b/ui-ngx/src/app/modules/home/components/ai/ai-action.models.ts index 4f04ee7647..ae6d8ebffd 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-action.models.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-action.models.ts @@ -314,6 +314,7 @@ export interface AiHandOverStep { detail?: string; markdown?: string; values: AiHandOverValue[]; + file?: string; done: boolean; } @@ -334,6 +335,7 @@ export interface AiHandOver { intervalSeconds?: number; quiet?: string; test?: AiHandOverTest; + paste?: {label: string; icon: string; intro: string}; } export interface AiHandOverTest { @@ -395,7 +397,13 @@ export function aiHandOverOf(args: unknown): AiHandOver | null { const interval = Number(values.intervalSeconds); return {title, text: text(values.text), choice, steps, waitFor, intervalSeconds: Number.isFinite(interval) && interval > 0 ? interval : undefined, quiet: text(values.quiet), - test: handOverTestOf(values.test)}; + test: handOverTestOf(values.test), paste: handOverPasteOf(values.paste)}; +} + +function handOverPasteOf(value: any): AiHandOver['paste'] { + const label = text(value?.label); + const intro = text(value?.intro); + return label && intro ? {label, intro, icon: iconOf(value.icon, 'terminal')} : undefined; } function handOverStepsOf(list: unknown): AiHandOverStep[] { @@ -406,6 +414,7 @@ function handOverStepsOf(list: unknown): AiHandOverStep[] { title: text(step.title), detail: text(step.detail), markdown: text(step.markdown), + file: text(step.file), done: step.done === true, values: (Array.isArray(step.values) ? step.values : []) .filter(value => text(value?.label) && typeof value?.value === 'string' && value.value) @@ -422,6 +431,7 @@ export interface AiConnectionCheck { note?: string; readings: {key: string; value: string}[]; keyCount: number; + gateway: boolean; } export function aiConnectionCheckOf(result: unknown): AiConnectionCheck | null { @@ -438,11 +448,16 @@ export function aiConnectionCheckOf(result: unknown): AiConnectionCheck | null { } const readings: {key: string; value: string}[] = []; const add = (telemetry: unknown) => Object.entries(telemetry && typeof telemetry === 'object' ? telemetry : {}) - .forEach(([key, item]) => readings.push({key, value: String((item as any)?.value ?? '')})); - add(values.telemetry); + .map(([key, item]) => ({key, value: String((item as any)?.value ?? '')})) + .filter(reading => !/^\s*[{[]/.test(reading.value)) + .forEach(reading => readings.push(reading)); + const gateway = values.gateway === true; + if (!gateway) { + add(values.telemetry); + } (Array.isArray(values.devices) ? values.devices : []).forEach(device => add(device?.telemetry)); return {dataArriving: values.steps.dataArriving === true, failedStep: text(values.failedStep), name: text(values.name), - note: text(values.note), readings: readings.slice(0, 6), keyCount: new Set(readings.map(reading => reading.key)).size}; + note: text(values.note), readings: readings.slice(0, 6), keyCount: new Set(readings.map(reading => reading.key)).size, gateway}; } export const isKitCardTool = (tool: string): boolean => KIT_CARD_TOOLS.has(tool); 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 f59b3e2b60..0bdcc75eda 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 @@ -79,7 +79,7 @@ } @if (unchanged.length) { - diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.ts index 82c5597d31..53936ba4fa 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-approval-card.component.ts @@ -13,6 +13,7 @@ import { SimpleChanges, ViewChild } from '@angular/core'; +import { aiRevealOpened } from '@home/components/ai/ai-disclosure.component'; import { TranslateService } from '@ngx-translate/core'; import { ClipboardService } from 'ngx-clipboard'; import { IntegrationType, integrationTypeInfoMap } from '@shared/models/integration.models'; @@ -368,6 +369,13 @@ export class AiApprovalCardComponent implements OnChanges, AfterViewChecked { return this.state === 'pending' || this.state === 'denied' || this.state === 'expired'; } + toggleUnchanged(): void { + this.showUnchanged = !this.showUnchanged; + if (this.showUnchanged) { + aiRevealOpened(this.host.nativeElement); + } + } + approve(): void { this.state = this.autoApproved ? 'auto' : 'approved'; this.denying = false; diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-assistant-context.service.ts b/ui-ngx/src/app/modules/home/components/ai/ai-assistant-context.service.ts index f17294b9d0..cd8cdc97b8 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-assistant-context.service.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-assistant-context.service.ts @@ -5,7 +5,7 @@ import { toSignal } from '@angular/core/rxjs-interop'; import { NavigationEnd, Router } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { filter, map, startWith } from 'rxjs/operators'; -import { aiPageView, AiChatClientContext } from '@shared/models/ai-chat.models'; +import { aiClientOs, aiPageView, AiChatClientContext } from '@shared/models/ai-chat.models'; import { getDefaultTimezone } from '@shared/models/time/time.models'; import { BreadCrumb } from '@shared/components/breadcrumb'; import { BreadcrumbService } from '@core/services/breadcrumb.service'; @@ -41,7 +41,7 @@ export class AiAssistantContextService { const title = this.pageTitle(); const path = this.pagePath(); const type = aiPageView(path); - return title || path !== '/' ? {view: {type, title, path}, timeZone: getDefaultTimezone()} : undefined; + return title || path !== '/' ? {view: {type, title, path}, timeZone: getDefaultTimezone(), os: aiClientOs()} : undefined; }); readonly welcome = computed(() => aiWelcome(this.clientContext()?.view, this.panelService.viewName(), this.panelService.config(), 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 289e1aafa0..525a62f5b7 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 @@ -54,6 +54,7 @@ } + } @else if (msg.from === 'ACTION' && files[$index] && inCard.has(files[$index].name)) { } @else if (msg.from === 'ACTION' && files[$index]; as file) {

{{ arrivedLine() }}

} @else { @@ -60,6 +60,14 @@ {{ step.detail }} } + @if (step.file && files().get(step.file); as file) { + + } @if (step.values.length || step.markdown) {
@for (value of step.values; track $index; let v = $index) { @@ -132,9 +140,15 @@ - + @if (handOver().paste; as paste) { + + } @else { + + }
} @else if (waiting()) { diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-hand-over.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-hand-over.component.ts index 334a93ba87..3b7c7c7767 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-hand-over.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-hand-over.component.ts @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. // SPDX-License-Identifier: BUSL-1.1 import { AfterViewChecked, Component, computed, DestroyRef, effect, ElementRef, EventEmitter, inject, input, Output, signal } from '@angular/core'; +import { AiFile, aiFileMark, aiFileName } from '@home/components/ai/ai-file.models'; +import { aiRevealOpened } from '@home/components/ai/ai-disclosure.component'; import { Clipboard } from '@angular/cdk/clipboard'; import { TranslateService } from '@ngx-translate/core'; import { AiConnectionCheck, AiHandOver } from '@home/components/ai/ai-action.models'; @@ -15,6 +17,7 @@ export interface AiHandOverWait { } const MIN_SILENCE_MS = 120000; +const MAX_SILENCE_MS = 300000; const TEST_MARKER = 'aiTestValue'; const OS_LABELS: Record = {macos: 'macOS', windows: 'Windows', linux: 'Linux'}; @@ -34,10 +37,16 @@ export class AiHandOverComponent implements AfterViewChecked { handOver = input.required(); wait = input(null); time = input(0); + files = input>(new Map()); @Output() prompt = new EventEmitter(); @Output() stop = new EventEmitter(); + @Output() compose = new EventEmitter(); + @Output() openFile = new EventEmitter(); + + readonly fileMark = aiFileMark; + readonly fileName = aiFileName; private host = inject(ElementRef); private translate = inject(TranslateService); @@ -86,7 +95,8 @@ export class AiHandOverComponent implements AfterViewChecked { const seconds = Math.floor(this.elapsed() / 1000); return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`; }); - readonly silenceMs = computed(() => Math.max(MIN_SILENCE_MS, 2 * 1000 * (this.handOver().waitFor === 'gateway' ? 0 : this.handOver().intervalSeconds ?? 0))); + readonly silenceMs = computed(() => Math.min(MAX_SILENCE_MS, + Math.max(MIN_SILENCE_MS, 2 * 1000 * (this.handOver().waitFor === 'gateway' ? 0 : this.handOver().intervalSeconds ?? 0)))); readonly quiet = computed(() => this.waiting() && this.elapsed() >= this.silenceMs()); readonly quietMinutes = computed(() => Math.max(2, Math.floor(this.elapsed() / 60000))); readonly stoppedQuiet = computed(() => { @@ -206,6 +216,15 @@ export class AiHandOverComponent implements AfterViewChecked { } } + unfold(): void { + this.open.set(true); + aiRevealOpened(this.host.nativeElement); + } + + startPaste(intro: string): void { + this.compose.emit(intro + '\n'); + } + say(key: string): void { this.prompt.emit(this.translate.instant(key)); } diff --git a/ui-ngx/src/app/modules/home/components/ai/ai-records.component.ts b/ui-ngx/src/app/modules/home/components/ai/ai-records.component.ts index 4107ed8a6a..212630cc6b 100644 --- a/ui-ngx/src/app/modules/home/components/ai/ai-records.component.ts +++ b/ui-ngx/src/app/modules/home/components/ai/ai-records.component.ts @@ -3,7 +3,8 @@ import { ChangeDetectionStrategy, Component, computed, DestroyRef, effect, inject, input, output, signal, untracked } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { TranslateService } from '@ngx-translate/core'; -import { Subscription } from 'rxjs'; +import { Subscription, timer } from 'rxjs'; +import { concatMap, take, takeWhile } from 'rxjs/operators'; import { aiEntityKind } from '@home/components/ai/ai-action.models'; import { aiRecordKey, AiRecordView, AiShownRecords } from '@home/components/ai/ai-record.models'; import { AiRecordLoaderService } from '@home/components/ai/ai-record-loader.service'; @@ -16,6 +17,9 @@ const LIST_SHOWN = 8; const TARGETED_HUB_TYPES = new Set([ItemType.CALCULATED_FIELD, ItemType.ALARM_RULE]); +const WAIT_RECHECK_MS = 10000; +const WAIT_RECHECKS = 90; + @Component({ selector: 'tb-ai-records', template: ` @@ -125,9 +129,12 @@ export class AiRecordsComponent { this.key(); const shown = untracked(() => this.shown()); this.loading?.unsubscribe(); - this.loading = this.loader.loadAll(shown.refs, {image: shown.refs.length === 1}) - .pipe(takeUntilDestroyed(this.destroyRef)) - .subscribe(views => this.views.set(views)); + this.loading = timer(0, WAIT_RECHECK_MS).pipe( + take(WAIT_RECHECKS), + concatMap(() => this.loader.loadAll(shown.refs, {image: shown.refs.length === 1})), + takeWhile(views => views.some(view => view.state?.tone === 'wait'), true), + takeUntilDestroyed(this.destroyRef) + ).subscribe(views => this.views.set(views)); }); } diff --git a/ui-ngx/src/app/shared/models/ai-chat.models.ts b/ui-ngx/src/app/shared/models/ai-chat.models.ts index 984b1b0af8..dcbad75537 100644 --- a/ui-ngx/src/app/shared/models/ai-chat.models.ts +++ b/ui-ngx/src/app/shared/models/ai-chat.models.ts @@ -187,6 +187,7 @@ export interface TurnStoppedEvent extends BaseChatEvent { export interface ToolStarted extends BaseChatToolEventData { tool: string; arguments?: unknown; + startedAt?: number; } export interface ToolStartedEvent extends BaseChatEvent { @@ -461,6 +462,14 @@ export interface AiChatView { export interface AiChatClientContext { view: AiChatView; timeZone?: string; + os?: string; +} + +export function aiClientOs(): string | undefined { + const nav = navigator as Navigator & {userAgentData?: {platform?: string}}; + const agent = `${nav.userAgentData?.platform ?? ''} ${navigator.userAgent}`; + return /Android/i.test(agent) ? 'android' : /iPhone|iPad|iPod/i.test(agent) ? 'ios' : /CrOS|Chrome OS/i.test(agent) ? 'chromeos' + : /Win/i.test(agent) ? 'windows' : /Mac/i.test(agent) ? 'macos' : /Linux|X11/i.test(agent) ? 'linux' : undefined; } export interface SendChatMessageRequest { 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 0f109ff7b2..b67b7ee1e4 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -2191,6 +2191,7 @@ "needs-you": "Needs you", "fold": "Fold", "unfold": "Show the steps", + "gateway-connected": "{{name}} connected at {{time}}.", "arrived-message": "The first message arrived at {{time}}.", "arrived-message-from": "The first message from {{name}} arrived at {{time}}.", "arrived-uplink": "The first uplink arrived at {{time}}.", @@ -2208,7 +2209,7 @@ "wait-uplink": "listening for the first uplink", "wait-gateway": "listening for the gateway", "nothing-heard": "nothing heard for {{minutes}} min", - "keep-waiting": "It is on, keep waiting", + "keep-waiting": "It is on now, keep waiting", "keep-waiting-prompt": "It is on. Keep waiting for the data.", "what-wrong": "What could be wrong?", "what-wrong-prompt": "Nothing arrived. What could be wrong?" diff --git a/ui-ngx/src/theme/ai-theme.scss b/ui-ngx/src/theme/ai-theme.scss index 58fab99af4..916b8a9123 100644 --- a/ui-ngx/src/theme/ai-theme.scss +++ b/ui-ngx/src/theme/ai-theme.scss @@ -2282,9 +2282,12 @@ code.k-ent[data-pal="solution"] .k-ent-ic { color: var(--entity-solution-fg); } display: flex; flex-wrap: wrap; gap: 8px; + min-width: 0; } .ob-chip[aria-pressed="true"] { + max-width: 100%; + overflow-wrap: anywhere; height: auto; min-height: 0; padding: 0; @@ -6832,6 +6835,13 @@ tb-ai-action-detail.k-detail .tb-codeblock pre.tb-code { text-decoration-color: var(--tb-ink-38); } + .k-step-file { + display: flex; + width: auto; + max-width: calc(100% - 28px); + margin: 8px 0 2px 28px; + } + .k-step-x { margin: 8px 0 2px 28px; min-width: 0;