19 changed files with 988 additions and 8 deletions
@ -0,0 +1,30 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
||||
|
SPDX-License-Identifier: BUSL-1.1 |
||||
|
|
||||
|
--> |
||||
|
@if (action().status === 'RUNNING') { |
||||
|
<tb-ai-work-line [words]="running()" [kind]="words().kind" [since]="action().startedAt"></tb-ai-work-line> |
||||
|
} @else { |
||||
|
<div class="out"> |
||||
|
<mat-icon class="mark" aria-hidden="true">{{ icon() }}</mat-icon> |
||||
|
<span class="say">{{ outcome() }}</span> |
||||
|
@if (duration()) { |
||||
|
<span class="dur">{{ duration() }}</span> |
||||
|
} |
||||
|
</div> |
||||
|
@if (hasDetails()) { |
||||
|
<details class="disc"> |
||||
|
<summary>{{ 'ai-assistant.action.details' | translate }}</summary> |
||||
|
@if (request()) { |
||||
|
<div class="part">{{ 'ai-assistant.action.request' | translate }}</div> |
||||
|
<pre>{{ request() }}</pre> |
||||
|
} |
||||
|
@if (result()) { |
||||
|
<div class="part">{{ 'ai-assistant.action.result' | translate }}</div> |
||||
|
<pre>{{ result() }}</pre> |
||||
|
} |
||||
|
</details> |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,124 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
||||
|
// SPDX-License-Identifier: BUSL-1.1 |
||||
|
:host { |
||||
|
display: block; |
||||
|
font-family: var(--tb-font); |
||||
|
letter-spacing: var(--chat-text-ls); |
||||
|
} |
||||
|
|
||||
|
.out { |
||||
|
display: flex; |
||||
|
align-items: flex-start; |
||||
|
gap: 8px; |
||||
|
font-size: var(--chat-line); |
||||
|
line-height: var(--chat-line-lh); |
||||
|
color: var(--tb-ink-76); |
||||
|
|
||||
|
.mark { |
||||
|
flex: none; |
||||
|
width: 16px; |
||||
|
height: 16px; |
||||
|
font-size: 16px; |
||||
|
margin-block: calc((var(--chat-line-lh) - 16px) / 2); |
||||
|
color: var(--tb-ok); |
||||
|
} |
||||
|
|
||||
|
.say { |
||||
|
min-width: 0; |
||||
|
overflow-wrap: anywhere; |
||||
|
} |
||||
|
|
||||
|
.dur { |
||||
|
margin-left: auto; |
||||
|
padding-left: 12px; |
||||
|
font-family: var(--tb-mono); |
||||
|
font-size: var(--chat-meta); |
||||
|
color: var(--k-faint); |
||||
|
white-space: nowrap; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
:host(.fail) .out .mark { |
||||
|
color: var(--tb-crit); |
||||
|
} |
||||
|
|
||||
|
:host(.stopped) .out { |
||||
|
color: var(--tb-text-secondary); |
||||
|
|
||||
|
.mark { |
||||
|
color: var(--tb-ink-38); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.disc { |
||||
|
margin: 2px 0 0 24px; |
||||
|
|
||||
|
> summary { |
||||
|
list-style: none; |
||||
|
display: flex; |
||||
|
align-items: center; |
||||
|
gap: 4px; |
||||
|
width: fit-content; |
||||
|
cursor: pointer; |
||||
|
font-size: var(--chat-meta); |
||||
|
line-height: var(--chat-meta-lh); |
||||
|
color: var(--tb-ink-60, var(--tb-text-secondary)); |
||||
|
|
||||
|
&::-webkit-details-marker { |
||||
|
display: none; |
||||
|
} |
||||
|
|
||||
|
&::before { |
||||
|
content: 'chevron_right'; |
||||
|
flex: none; |
||||
|
width: 18px; |
||||
|
height: 18px; |
||||
|
font: 18px/18px 'Material Icons'; |
||||
|
color: var(--tb-ink-38); |
||||
|
transition: transform .15s ease; |
||||
|
} |
||||
|
|
||||
|
&:hover, &:hover::before { |
||||
|
color: var(--tb-ink); |
||||
|
} |
||||
|
|
||||
|
&:focus-visible { |
||||
|
outline: 2px solid var(--ai-focus); |
||||
|
outline-offset: 2px; |
||||
|
border-radius: 4px; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
&[open] > summary::before { |
||||
|
transform: rotate(90deg); |
||||
|
} |
||||
|
|
||||
|
.part { |
||||
|
margin: 8px 0 4px; |
||||
|
font-size: var(--chat-meta); |
||||
|
line-height: var(--chat-meta-lh); |
||||
|
font-weight: 500; |
||||
|
color: var(--tb-text-secondary); |
||||
|
} |
||||
|
|
||||
|
pre { |
||||
|
margin: 0; |
||||
|
max-height: 240px; |
||||
|
overflow: auto; |
||||
|
padding: 8px 10px; |
||||
|
border-radius: 8px; |
||||
|
background: var(--tb-line-soft); |
||||
|
font-family: var(--tb-mono); |
||||
|
font-size: 12px; |
||||
|
line-height: 18px; |
||||
|
white-space: pre-wrap; |
||||
|
overflow-wrap: anywhere; |
||||
|
color: var(--tb-ink-76); |
||||
|
} |
||||
|
|
||||
|
@media (prefers-reduced-motion: reduce) { |
||||
|
> summary::before { |
||||
|
transition: none; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
||||
|
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
import { ChangeDetectionStrategy, Component, computed, inject, input } from '@angular/core'; |
||||
|
import { TranslateService } from '@ngx-translate/core'; |
||||
|
import { |
||||
|
AiAction, |
||||
|
aiActionDetail, |
||||
|
aiActionDuration, |
||||
|
aiActionWords |
||||
|
} from '@home/components/ai/ai-action.models'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-ai-action-line', |
||||
|
templateUrl: './ai-action-line.component.html', |
||||
|
styleUrls: ['./ai-action-line.component.scss'], |
||||
|
standalone: false, |
||||
|
changeDetection: ChangeDetectionStrategy.OnPush, |
||||
|
host: { |
||||
|
'[class.run]': 'action().status === "RUNNING"', |
||||
|
'[class.fail]': 'action().status === "FAILURE"', |
||||
|
'[class.stopped]': 'action().status === "STOPPED"' |
||||
|
} |
||||
|
}) |
||||
|
export class AiActionLineComponent { |
||||
|
|
||||
|
action = input.required<AiAction>(); |
||||
|
|
||||
|
private translate = inject(TranslateService); |
||||
|
|
||||
|
words = computed(() => aiActionWords(this.action().tool, this.action().arguments)); |
||||
|
|
||||
|
running = computed(() => this.translate.instant(this.words().key + '.run', this.words().params)); |
||||
|
|
||||
|
outcome = computed(() => { |
||||
|
const action = this.action(); |
||||
|
const words = this.words(); |
||||
|
switch (action.status) { |
||||
|
case 'SUCCESS': |
||||
|
return this.translate.instant(words.key + '.done', words.params); |
||||
|
case 'FAILURE': |
||||
|
return this.translate.instant('ai-assistant.action.failed', {words: this.lowerFirst(this.running())}); |
||||
|
default: |
||||
|
return this.translate.instant('ai-assistant.work.stopped', {words: this.lowerFirst(this.running())}); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
icon = computed(() => { |
||||
|
switch (this.action().status) { |
||||
|
case 'SUCCESS': |
||||
|
return 'check'; |
||||
|
case 'FAILURE': |
||||
|
return 'error_outline'; |
||||
|
default: |
||||
|
return 'block'; |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
duration = computed(() => this.action().durationMs !== undefined ? aiActionDuration(this.action().durationMs) : ''); |
||||
|
|
||||
|
request = computed(() => aiActionDetail(this.action().arguments)); |
||||
|
|
||||
|
result = computed(() => aiActionDetail(this.action().result)); |
||||
|
|
||||
|
hasDetails = computed(() => !!this.request() || !!this.result()); |
||||
|
|
||||
|
private lowerFirst(words: string): string { |
||||
|
return words ? words.charAt(0).toLocaleLowerCase() + words.slice(1) : words; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,182 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
||||
|
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
export type { AiAction, AiActionStatus } from '@shared/models/ai-chat.models'; |
||||
|
|
||||
|
export type AiSpinKind = 'think' | 'gen' | 'install' | 'listen' | 'stop'; |
||||
|
|
||||
|
export interface AiActionWords { |
||||
|
key: string; |
||||
|
params?: Record<string, string>; |
||||
|
kind: AiSpinKind; |
||||
|
} |
||||
|
|
||||
|
export type AiWorkMoment = 'reading-request' | 'going-through-results' | 'applying-change' | 'reading-what-to-change'; |
||||
|
|
||||
|
export interface AiWorkState { |
||||
|
moment: AiWorkMoment; |
||||
|
since: number; |
||||
|
} |
||||
|
|
||||
|
const HIDDEN_TOOLS = new Set(['read_current_time']); |
||||
|
|
||||
|
const TOOL_WORDS: Record<string, string> = { |
||||
|
read_entity: 'read-entity', |
||||
|
read_timeseries_keys: 'read-telemetry-keys', |
||||
|
read_attributes: 'read-attributes', |
||||
|
discover_attribute_keys: 'read-attributes', |
||||
|
save_attributes: 'save-attributes', |
||||
|
delete_attributes: 'delete-attributes', |
||||
|
read_entity_relations: 'read-relations', |
||||
|
read_device_credentials: 'read-credentials', |
||||
|
save_device: 'save-device', |
||||
|
save_calculated_field: 'save-calculated-field', |
||||
|
save_alarm_rule: 'save-alarm-rule', |
||||
|
create_dashboard: 'create-dashboard', |
||||
|
update_dashboard: 'update-dashboard', |
||||
|
add_dashboard_state: 'add-dashboard-state', |
||||
|
update_dashboard_state: 'update-dashboard-state', |
||||
|
add_dashboard_entity_alias: 'add-entity-alias', |
||||
|
update_dashboard_entity_alias: 'update-entity-alias', |
||||
|
read_dashboard_entity_alias: 'read-entity-alias', |
||||
|
add_dashboard_filter: 'add-dashboard-filter', |
||||
|
update_dashboard_filter: 'update-dashboard-filter', |
||||
|
read_dashboard_filter: 'read-dashboard-filter', |
||||
|
read_dashboard_overview: 'read-dashboard', |
||||
|
add_dashboard_widgets: 'add-widgets', |
||||
|
update_dashboard_widget: 'update-widget', |
||||
|
place_dashboard_widgets: 'place-widgets', |
||||
|
read_dashboard_widget: 'read-widget', |
||||
|
read_dashboard_html_page: 'read-dashboard-page', |
||||
|
save_notification_target: 'save-notification-target', |
||||
|
save_notification_template: 'save-notification-template', |
||||
|
save_notification_rule: 'save-notification-rule', |
||||
|
search_iot_hub_devices: 'search-iot-hub', |
||||
|
read_transport_endpoints: 'read-connection-settings', |
||||
|
read_device_connect_commands: 'read-connection-settings', |
||||
|
search_web: 'search-web' |
||||
|
}; |
||||
|
|
||||
|
const GENERATING_TOOLS: Record<string, string> = { |
||||
|
set_dashboard_html_page: 'lay-out-dashboard', |
||||
|
reprocess_calculated_field: 'recalculate-history', |
||||
|
research_device_connectivity: 'research-device' |
||||
|
}; |
||||
|
|
||||
|
const SEARCHED_TYPES: Record<string, string> = { |
||||
|
DEVICE: 'search-devices', |
||||
|
ASSET: 'search-assets', |
||||
|
DASHBOARD: 'search-dashboards', |
||||
|
CUSTOMER: 'search-customers', |
||||
|
USER: 'search-users' |
||||
|
}; |
||||
|
|
||||
|
const DELETED_TARGETS: Record<string, string> = { |
||||
|
CALCULATED_FIELD: 'delete-calculated-field', |
||||
|
ALARM_RULE: 'delete-alarm-rule', |
||||
|
NOTIFICATION_RULE: 'delete-notification-rule', |
||||
|
NOTIFICATION_TARGET: 'delete-notification-target', |
||||
|
NOTIFICATION_TEMPLATE: 'delete-notification-template', |
||||
|
DASHBOARD: 'delete-dashboard', |
||||
|
DASHBOARD_WIDGET: 'delete-widget', |
||||
|
DASHBOARD_STATE: 'delete-dashboard-state', |
||||
|
DASHBOARD_ENTITY_ALIAS: 'delete-entity-alias', |
||||
|
DASHBOARD_FILTER: 'delete-dashboard-filter', |
||||
|
DEVICE: 'delete-device' |
||||
|
}; |
||||
|
|
||||
|
const ACTION_PREFIX = 'ai-assistant.action.'; |
||||
|
|
||||
|
export const isHiddenAiAction = (tool: string): boolean => HIDDEN_TOOLS.has(tool); |
||||
|
|
||||
|
export function aiActionWords(tool: string, args: unknown): AiActionWords { |
||||
|
const values = (args && typeof args === 'object' ? args : {}) as Record<string, any>; |
||||
|
if (GENERATING_TOOLS[tool]) { |
||||
|
return {key: ACTION_PREFIX + GENERATING_TOOLS[tool], kind: 'gen'}; |
||||
|
} |
||||
|
switch (tool) { |
||||
|
case 'search_entities': |
||||
|
return {key: ACTION_PREFIX + searchedWords(values), kind: 'think'}; |
||||
|
case 'read_timeseries': |
||||
|
return {key: ACTION_PREFIX + telemetryWords(values), kind: 'think'}; |
||||
|
case 'delete': |
||||
|
return {key: ACTION_PREFIX + (DELETED_TARGETS[values.targetType] ?? 'delete-other'), kind: 'think'}; |
||||
|
case 'fetch_web_page': { |
||||
|
const site = siteOf(values.url); |
||||
|
return site ? {key: ACTION_PREFIX + 'read-site', params: {site}, kind: 'think'} : {key: ACTION_PREFIX + 'read-page', kind: 'think'}; |
||||
|
} |
||||
|
} |
||||
|
return {key: ACTION_PREFIX + (TOOL_WORDS[tool] ?? 'other'), kind: 'think'}; |
||||
|
} |
||||
|
|
||||
|
export function aiActionDuration(ms: number): string { |
||||
|
if (ms < 1000) { |
||||
|
return `${Math.max(Math.round(ms / 100), 1) / 10} s`; |
||||
|
} |
||||
|
const seconds = Math.round(ms / 1000); |
||||
|
if (seconds < 60) { |
||||
|
return `${seconds} s`; |
||||
|
} |
||||
|
const rest = seconds % 60; |
||||
|
return rest ? `${Math.floor(seconds / 60)} min ${rest} s` : `${seconds / 60} min`; |
||||
|
} |
||||
|
|
||||
|
export function aiWorkClock(ms: number): string { |
||||
|
const seconds = Math.floor(ms / 1000); |
||||
|
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`; |
||||
|
} |
||||
|
|
||||
|
export function aiActionDetail(value: unknown): string { |
||||
|
if (value === undefined || value === null || value === '') { |
||||
|
return ''; |
||||
|
} |
||||
|
if (typeof value === 'string') { |
||||
|
try { |
||||
|
const parsed = JSON.parse(value); |
||||
|
return typeof parsed === 'object' && parsed !== null ? JSON.stringify(parsed, null, 2) : value; |
||||
|
} catch { |
||||
|
return value; |
||||
|
} |
||||
|
} |
||||
|
return JSON.stringify(value, null, 2); |
||||
|
} |
||||
|
|
||||
|
function searchedWords(values: Record<string, any>): string { |
||||
|
const filter = values.entityFilter ?? values; |
||||
|
const types = new Set<string>(); |
||||
|
const add = (type: unknown) => typeof type === 'string' && types.add(type); |
||||
|
add(filter?.entityType); |
||||
|
add(filter?.singleEntity?.entityType); |
||||
|
if (filter?.deviceType || filter?.deviceTypes || filter?.filterType === 'deviceType') { |
||||
|
types.add('DEVICE'); |
||||
|
} |
||||
|
if (filter?.assetType || filter?.assetTypes || filter?.filterType === 'assetType') { |
||||
|
types.add('ASSET'); |
||||
|
} |
||||
|
(filter?.filters ?? []).forEach((relationFilter: any) => (relationFilter?.entityTypes ?? []).forEach(add)); |
||||
|
if (types.size === 1) { |
||||
|
return SEARCHED_TYPES[[...types][0]] ?? 'search-tenant'; |
||||
|
} |
||||
|
return 'search-tenant'; |
||||
|
} |
||||
|
|
||||
|
function telemetryWords(values: Record<string, any>): string { |
||||
|
switch (values.mode) { |
||||
|
case 'LATEST': |
||||
|
return 'read-latest-values'; |
||||
|
case 'KEYS': |
||||
|
return 'read-telemetry-keys'; |
||||
|
default: |
||||
|
return values.mode ? 'read-history' : 'read-latest-values'; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function siteOf(url: unknown): string | undefined { |
||||
|
if (typeof url !== 'string') { |
||||
|
return undefined; |
||||
|
} |
||||
|
try { |
||||
|
return new URL(url).hostname.replace(/^www\./, '') || undefined; |
||||
|
} catch { |
||||
|
return undefined; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
||||
|
// SPDX-License-Identifier: BUSL-1.1 |
||||
|
:host { |
||||
|
flex: none; |
||||
|
display: inline-flex; |
||||
|
align-items: center; |
||||
|
justify-content: center; |
||||
|
width: 16px; |
||||
|
height: 16px; |
||||
|
color: var(--ai-teal-500); |
||||
|
|
||||
|
&[data-kind="listen"] { |
||||
|
color: var(--tb-caution); |
||||
|
} |
||||
|
|
||||
|
&[data-kind="stop"] { |
||||
|
color: var(--tb-ink-38); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.cell { |
||||
|
display: grid; |
||||
|
grid-template-columns: repeat(2, 3px); |
||||
|
grid-template-rows: repeat(3, 3px); |
||||
|
gap: 2px 3px; |
||||
|
|
||||
|
i { |
||||
|
width: 3px; |
||||
|
height: 3px; |
||||
|
border-radius: 50%; |
||||
|
background: currentColor; |
||||
|
visibility: hidden; |
||||
|
|
||||
|
&.on { |
||||
|
visibility: visible; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,81 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
||||
|
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
import { ChangeDetectionStrategy, Component, ElementRef, inject, Input, NgZone, OnDestroy, OnInit } from '@angular/core'; |
||||
|
import { AiSpinKind } from '@home/components/ai/ai-action.models'; |
||||
|
|
||||
|
const FRAME_MS = 80; |
||||
|
const DOT_BITS = [0x01, 0x08, 0x02, 0x10, 0x04, 0x20]; |
||||
|
|
||||
|
const FRAMES: Record<AiSpinKind, number[]> = { |
||||
|
think: [...'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'].map(glyph => glyph.charCodeAt(0) - 0x2800), |
||||
|
listen: [0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x3f, 0x3e, 0x3c, 0x38, 0x30, 0x20, 0x00], |
||||
|
gen: [0x01, 0x08, 0x10, 0x20, 0x04, 0x02].map(bit => 0x3f & ~bit), |
||||
|
install: [0x09, 0x12, 0x24, 0x2d, 0x36, 0x3f, 0x3f, 0x3f, 0x00], |
||||
|
stop: [0x12] |
||||
|
}; |
||||
|
|
||||
|
const spinners = new Set<AiSpinComponent>(); |
||||
|
let tick = 0; |
||||
|
let timer: ReturnType<typeof setInterval> | undefined; |
||||
|
|
||||
|
function startTimer(zone: NgZone): void { |
||||
|
if (timer || matchMedia('(prefers-reduced-motion: reduce)').matches) { |
||||
|
return; |
||||
|
} |
||||
|
zone.runOutsideAngular(() => { |
||||
|
timer = setInterval(() => { |
||||
|
tick++; |
||||
|
spinners.forEach(spinner => spinner.draw()); |
||||
|
}, FRAME_MS); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-ai-spin', |
||||
|
template: '<span class="cell"><i></i><i></i><i></i><i></i><i></i><i></i></span>', |
||||
|
styleUrls: ['./ai-spin.component.scss'], |
||||
|
standalone: false, |
||||
|
changeDetection: ChangeDetectionStrategy.OnPush, |
||||
|
host: { |
||||
|
'aria-hidden': 'true', |
||||
|
'[attr.data-kind]': 'kind' |
||||
|
} |
||||
|
}) |
||||
|
export class AiSpinComponent implements OnInit, OnDestroy { |
||||
|
|
||||
|
private kindValue: AiSpinKind = 'think'; |
||||
|
|
||||
|
@Input() |
||||
|
set kind(kind: AiSpinKind) { |
||||
|
this.kindValue = kind ?? 'think'; |
||||
|
this.draw(); |
||||
|
} |
||||
|
|
||||
|
get kind(): AiSpinKind { |
||||
|
return this.kindValue; |
||||
|
} |
||||
|
|
||||
|
private host = inject<ElementRef<HTMLElement>>(ElementRef); |
||||
|
private zone = inject(NgZone); |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
spinners.add(this); |
||||
|
this.draw(); |
||||
|
startTimer(this.zone); |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy(): void { |
||||
|
spinners.delete(this); |
||||
|
if (!spinners.size && timer) { |
||||
|
clearInterval(timer); |
||||
|
timer = undefined; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
draw(): void { |
||||
|
const frames = FRAMES[this.kindValue]; |
||||
|
const bits = frames[tick % frames.length]; |
||||
|
const dots = this.host.nativeElement.querySelectorAll('i'); |
||||
|
dots.forEach((dot, index) => dot.classList.toggle('on', !!(bits & DOT_BITS[index]))); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
||||
|
// SPDX-License-Identifier: BUSL-1.1 |
||||
|
:host { |
||||
|
display: flex; |
||||
|
align-items: flex-start; |
||||
|
gap: 8px; |
||||
|
font-size: var(--chat-line); |
||||
|
line-height: var(--chat-line-lh); |
||||
|
color: var(--tb-text-secondary); |
||||
|
|
||||
|
tb-ai-spin { |
||||
|
margin-block: calc((var(--chat-line-lh) - 16px) / 2); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.say { |
||||
|
min-width: 0; |
||||
|
} |
||||
|
|
||||
|
.clock { |
||||
|
margin-left: auto; |
||||
|
padding-left: 12px; |
||||
|
font-family: var(--tb-mono); |
||||
|
font-size: var(--chat-meta); |
||||
|
color: var(--k-faint); |
||||
|
white-space: nowrap; |
||||
|
} |
||||
@ -0,0 +1,57 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
||||
|
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
import { ChangeDetectionStrategy, Component, computed, DestroyRef, inject, input, NgZone, OnInit, signal } from '@angular/core'; |
||||
|
import { TranslateService } from '@ngx-translate/core'; |
||||
|
import { AiSpinKind, aiWorkClock } from '@home/components/ai/ai-action.models'; |
||||
|
|
||||
|
const CLOCK_AFTER_MS = 10_000; |
||||
|
const STILL_AFTER_MS = 30_000; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-ai-work-line', |
||||
|
template: ` |
||||
|
<tb-ai-spin [kind]="kind()"></tb-ai-spin> |
||||
|
<span class="say">{{ say() }}</span> |
||||
|
@if (clock(); as time) { |
||||
|
<span class="clock">{{ time }}</span> |
||||
|
} |
||||
|
`,
|
||||
|
styleUrls: ['./ai-work-line.component.scss'], |
||||
|
standalone: false, |
||||
|
changeDetection: ChangeDetectionStrategy.OnPush, |
||||
|
host: { |
||||
|
'role': 'status', |
||||
|
'[attr.aria-label]': 'words()' |
||||
|
} |
||||
|
}) |
||||
|
export class AiWorkLineComponent implements OnInit { |
||||
|
|
||||
|
words = input.required<string>(); |
||||
|
|
||||
|
kind = input<AiSpinKind>('think'); |
||||
|
|
||||
|
since = input<number>(Date.now()); |
||||
|
|
||||
|
private now = signal(Date.now()); |
||||
|
|
||||
|
private elapsed = computed(() => Math.max(0, this.now() - this.since())); |
||||
|
|
||||
|
say = computed(() => { |
||||
|
const words = this.words(); |
||||
|
if (this.elapsed() < STILL_AFTER_MS || !words) { |
||||
|
return words; |
||||
|
} |
||||
|
return this.translate.instant('ai-assistant.work.still', {words: words.charAt(0).toLocaleLowerCase() + words.slice(1)}); |
||||
|
}); |
||||
|
|
||||
|
clock = computed(() => this.elapsed() >= CLOCK_AFTER_MS ? aiWorkClock(this.elapsed()) : undefined); |
||||
|
|
||||
|
private translate = inject(TranslateService); |
||||
|
private zone = inject(NgZone); |
||||
|
private destroyRef = inject(DestroyRef); |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
const timer = this.zone.runOutsideAngular(() => setInterval(() => this.zone.run(() => this.now.set(Date.now())), 1000)); |
||||
|
this.destroyRef.onDestroy(() => clearInterval(timer)); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue