Browse Source

AI Assistant menu badge and Home Waiting for you count the solutions that need you, not only chats; Home's two pills are the loudest and then the most recent of chats and solutions, and a solution's pill opens the solution

pull/16882/head
Igor Kulikov 1 day ago
parent
commit
5e5e6924b6
  1. 9
      ui-ngx/src/app/core/services/ai-solutions.service.ts
  2. 6
      ui-ngx/src/app/modules/home/menu/menu-link.component.ts
  3. 63
      ui-ngx/src/app/modules/home/pages/home-page/components/waiting-ai-chats-home.component.ts

9
ui-ngx/src/app/core/services/ai-solutions.service.ts

@ -1,6 +1,6 @@
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: BUSL-1.1
import { DestroyRef, inject, Injectable, signal } from '@angular/core';
import { computed, DestroyRef, inject, Injectable, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
@ -182,6 +182,13 @@ export class AiSolutionsService {
return solution.status === 'NEEDS_YOU' ? 'needs' : solution.status === 'RUNNING' ? 'running' : solution.status === 'FAILED' ? 'error' : null;
}
needsAttention(solution: SolutionInfo): boolean {
const level = this.levelOf(solution);
return level === 'needs' || level === 'error';
}
readonly attention = computed(() => this._solutions().filter(solution => this.needsAttention(solution)));
tipOf(solution: SolutionInfo, open = false): string {
const level = this.levelOf(solution, open);
if (!level) {

6
ui-ngx/src/app/modules/home/menu/menu-link.component.ts

@ -4,6 +4,7 @@
import { ChangeDetectionStrategy, Component, computed, inject, Input, OnInit, signal } from '@angular/core';
import { MenuId, MenuSection } from '@core/services/menu.models';
import { AiChatsService } from '@core/services/ai-chats.service';
import { AiSolutionsService } from '@core/services/ai-solutions.service';
import { coerceBoolean } from '@shared/decorators/coercion';
@Component({
@ -27,13 +28,16 @@ export class MenuLinkComponent implements OnInit {
private countsChats = signal(false);
private solutions = inject(AiSolutionsService);
readonly needYou = computed(() => this.countsChats()
? this.chats.statusChats().filter(chat => this.chats.needsAttention(chat)).length : 0);
? this.chats.statusChats().filter(chat => this.chats.needsAttention(chat)).length + this.solutions.attention().length : 0);
ngOnInit(): void {
if (this.section?.id === MenuId.ai_assistant) {
this.countsChats.set(true);
this.chats.watchStatus();
this.solutions.ensureLoaded();
}
}

63
ui-ngx/src/app/modules/home/pages/home-page/components/waiting-ai-chats-home.component.ts

@ -4,13 +4,24 @@ import { ChangeDetectionStrategy, Component, computed, effect, inject, signal }
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { TranslateService } from '@ngx-translate/core';
import { interval } from 'rxjs';
import { ChatInfo } from '@shared/models/ai-chat.models';
import { ChatLevel } from '@shared/models/ai-chat.models';
import { homeAgo, homeElapsed } from '@home/pages/home-page/home-list.models';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
import { readStored, writeStored } from '@core/local-storage/stored';
import { AiChatsService } from '@core/services/ai-chats.service';
import { AiSolutionsService } from '@core/services/ai-solutions.service';
interface WaitingItem {
id: string;
title: string;
level: ChatLevel;
tip: string;
updatedTime?: number;
recency: number;
queryParams: {chatId?: string; solutionId?: string};
}
const SHOWN = 2;
@ -19,7 +30,7 @@ const TICK_MS = 60_000;
@Component({
selector: 'tb-waiting-ai-chats-home',
template: `
@if (!chats.statusResolved()) {
@if (!resolved()) {
@if (reserved) {
<div class="tb-ai-attn-block reserve" aria-hidden="true">
<div class="tb-ai-attn-h">{{ 'home.ai.waiting' | translate }}</div>
@ -34,13 +45,13 @@ const TICK_MS = 60_000;
<div class="tb-ai-attn-block" [class.appear]="!reserved">
<div class="tb-ai-attn-h" id="tb-ai-attn-h">{{ 'home.ai.waiting' | translate }}</div>
<div class="tb-ai-attn" role="group" aria-labelledby="tb-ai-attn-h">
@for (chat of shown(); track chat.id) {
@let elapsed = elapsedOf(chat);
<a class="tb-chat-pill" routerLink="/ai-assistant" [queryParams]="{chatId: chat.id}"
[matTooltip]="elapsed ? ('home.ai.waiting-tip' | translate: {tip: chats.tipOf(chat), ago: agoOf(chat)}) : chats.tipOf(chat)"
@for (item of shown(); track item.id) {
@let elapsed = elapsedOf(item);
<a class="tb-chat-pill" routerLink="/ai-assistant" [queryParams]="item.queryParams"
[matTooltip]="elapsed ? ('home.ai.waiting-tip' | translate: {tip: item.tip, ago: agoOf(item)}) : item.tip"
matTooltipPosition="below">
<span class="tb-ai-status-mark" [class]="chats.levelOf(chat)" aria-hidden="true"></span>
<span class="tb-chat-pill-t">{{ chat.title || ('ai-assistant.new-chat' | translate) }}</span>
<span class="tb-ai-status-mark" [class]="item.level" aria-hidden="true"></span>
<span class="tb-chat-pill-t">{{ item.title }}</span>
@if (elapsed) {
<span class="tb-chat-pill-m">{{ elapsed }}</span>
}
@ -62,8 +73,29 @@ export class WaitingAIChatsHomeComponent {
private readonly now = signal(Date.now());
readonly waiting = computed(() => this.chats.statusChats().filter(chat => this.chats.needsAttention(chat))
.sort((a, b) => (this.chats.levelOf(a) === 'error' ? 0 : 1) - (this.chats.levelOf(b) === 'error' ? 0 : 1)));
private readonly solutions = inject(AiSolutionsService);
readonly resolved = computed(() => this.chats.statusResolved() && this.solutions.loaded());
readonly waiting = computed<WaitingItem[]>(() => [
...this.chats.statusChats().filter(chat => this.chats.needsAttention(chat)).map(chat => ({
id: chat.id,
title: chat.title || this.translate.instant('ai-assistant.new-chat'),
level: this.chats.levelOf(chat),
tip: this.chats.tipOf(chat),
updatedTime: chat.updatedTime,
recency: chat.updatedTime ?? chat.createdTime ?? 0,
queryParams: {chatId: chat.id}
})),
...this.solutions.attention().map(solution => ({
id: solution.id,
title: this.solutions.titleOf(solution),
level: this.solutions.levelOf(solution),
tip: this.solutions.tipOf(solution),
recency: solution.createdTime ?? 0,
queryParams: {solutionId: solution.id}
}))
].sort((a, b) => (a.level === 'error' ? 0 : 1) - (b.level === 'error' ? 0 : 1) || b.recency - a.recency));
readonly shown = computed(() => this.waiting().slice(0, SHOWN));
@ -75,20 +107,21 @@ export class WaitingAIChatsHomeComponent {
constructor() {
this.chats.watchStatus();
this.solutions.ensureLoaded();
interval(TICK_MS).pipe(takeUntilDestroyed()).subscribe(() => this.now.set(Date.now()));
effect(() => {
if (this.chats.statusResolved()) {
if (this.resolved()) {
this.writeShown(this.shown().length);
}
});
}
elapsedOf(chat: ChatInfo): string | null {
return chat.updatedTime ? homeElapsed(this.translate, Math.max(0, this.now() - chat.updatedTime)) : null;
elapsedOf(item: WaitingItem): string | null {
return item.updatedTime ? homeElapsed(this.translate, Math.max(0, this.now() - item.updatedTime)) : null;
}
agoOf(chat: ChatInfo): string {
return homeAgo(this.translate, Math.max(0, this.now() - chat.updatedTime));
agoOf(item: WaitingItem): string {
return homeAgo(this.translate, Math.max(0, this.now() - item.updatedTime));
}
private readShown(): number {

Loading…
Cancel
Save