Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | 1x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 59x 40x 5x 40x 4x 40x 63x 63x 49x 4x 4x 45x 14x 1x 1x 1x 1x 1x 1x 1x 13x 84x 84x 84x 84x 84x 84x 64x 20x 20x 20x 20x 2x 20x 20x 20x 137x 41x 41x 72x 52x 4x 4x 32x 32x 32x 32x 32x 32x 12x 20x 28x 28x 28x 4x 4x 4x 4x 4x 4x 40x | /**
* Copyright (c) Siemens 2016 - 2025
* SPDX-License-Identifier: MIT
*/
import { NgTemplateOutlet } from '@angular/common';
import {
Component,
computed,
input,
output,
booleanAttribute,
inject,
contentChild,
Signal,
isSignal,
effect
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { BackgroundColorVariant } from '@siemens/element-ng/common';
import { SiEmptyStateComponent } from '@siemens/element-ng/empty-state';
import { SiInlineNotificationComponent } from '@siemens/element-ng/inline-notification';
import { getMarkdownRenderer } from '@siemens/element-ng/markdown-renderer';
import { MenuItem } from '@siemens/element-ng/menu';
import { SiResponsiveContainerDirective } from '@siemens/element-ng/resize-observer';
import { TranslatableString, t } from '@siemens/element-translate-ng/translate';
import {
AiChatMessage,
ChatMessage,
MessageAction,
TemplateChatMessage
} from './chat-message.model';
import { SiAiMessageComponent } from './si-ai-message.component';
import { SiChatContainerInputDirective } from './si-chat-container-input.directive';
import { SiChatContainerComponent } from './si-chat-container.component';
import { ChatInputAttachment, SiChatInputComponent } from './si-chat-input.component';
import { SiToolMessageComponent } from './si-tool-message.component';
import { SiUserMessageComponent } from './si-user-message.component';
/** @experimental */
@Component({
selector: 'si-ai-chat-container',
imports: [
NgTemplateOutlet,
SiEmptyStateComponent,
SiInlineNotificationComponent,
SiAiMessageComponent,
SiToolMessageComponent,
SiUserMessageComponent,
SiChatContainerComponent,
SiChatContainerInputDirective
],
templateUrl: './si-ai-chat-container.component.html',
host: {
class: 'd-flex si-layout-inner flex-grow-1 flex-column h-100 w-100'
},
hostDirectives: [SiResponsiveContainerDirective]
})
export class SiAiChatContainerComponent {
private readonly chatInput = contentChild<SiChatInputComponent>(SiChatInputComponent);
private sanitizer = inject(DomSanitizer);
protected markdownRenderer = getMarkdownRenderer(this.sanitizer);
constructor() {
effect(() => {
const inputComponent = this.chatInput();
Iif (inputComponent) {
inputComponent.registerStates(this.inputSending, this.inputInterruptible);
}
});
}
private messageActionsCache = new WeakMap<
ChatMessage,
{ primary: MessageAction[]; secondary: MenuItem[] }
>();
/**
* List of chat messages to display. Messages can contain either content (string)
* or a template (TemplateRef) for custom rendering. When both content and template
* are provided, the template takes precedence.
* Leave undefined to manage messages via ng-content instead.
*/
readonly messages = input<ChatMessage[] | undefined>();
/**
* Whether a message is currently being sent, disables input and shows a loading state in the input area.
* If you want to show a loading state for the latest AI message, set {@link loading} to true instead.
* @defaultValue false
*/
readonly sending = input(false, { transform: booleanAttribute });
/**
* Whether a pending loading AI message should be shown (e.g. while waiting for response).
* If you also want to prevent the user from sending new messages, set {@link sending} to true as well.
* @defaultValue false
*/
readonly loading = input(false, { transform: booleanAttribute });
/**
* Whether to disable the interrupt functionality
* @defaultValue false
*/
readonly disableInterrupt = input(false, { transform: booleanAttribute });
/**
* Whether the system is currently interrupting an operation. When true,
* forces interruptible mode and shows sending state on the input.
* @defaultValue false
*/
readonly interrupting = input(false, { transform: booleanAttribute });
/**
* Disable auto-scroll to bottom when new messages are added
* @defaultValue false
*/
readonly noAutoScroll = input(false, { transform: booleanAttribute });
/**
* Custom AI icon name, used for empty state
* @defaultValue 'element-ai'
*/
readonly aiIcon = input('element-ai');
/**
* Color to use for component background.
* @defaultValue 'base-0'
*/
readonly colorVariant = input<BackgroundColorVariant>('base-0');
/**
* Title for empty state
* @defaultValue
* ```
* t(() => $localize`:@@SI_CHAT_CONTAINER.EMPTY_STATE_TITLE:Start a conversation`)
* ```
*/
readonly emptyStateTitle = input<TranslatableString>(
t(() => $localize`:@@SI_CHAT_CONTAINER.EMPTY_STATE_TITLE:Start a conversation`)
);
/**
* Description for empty state
* @defaultValue
* ```
* t(() => $localize`:@@SI_CHAT_CONTAINER.EMPTY_STATE_DESCRIPTION:Send a message to begin chatting`)
* ```
*/
readonly emptyStateDescription = input<TranslatableString>(
t(
() => $localize`:@@SI_CHAT_CONTAINER.EMPTY_STATE_DESCRIPTION:Send a message to begin chatting`
)
);
/**
* More actions button aria label
*
* @defaultValue
* ```
* t(() => $localize`:@@SI_CHAT_CONTAINER.SECONDARY_ACTIONS:More actions`)
* ```
*/
readonly secondaryActionsLabel = input<TranslatableString>(
t(() => $localize`:@@SI_CHAT_CONTAINER.SECONDARY_ACTIONS:More actions`)
);
/**
* Status notification severity
*/
readonly statusSeverity = input<
'info' | 'success' | 'caution' | 'warning' | 'danger' | 'critical'
>();
/**
* Status notification heading
*/
readonly statusHeading = input<string>();
/**
* Status notification message
*/
readonly statusMessage = input<string>();
/**
* Status notification action link
*/
readonly statusAction = input<{ title: string; href: string; target?: string }>();
/**
* Label for tool message input arguments section
*
* @defaultValue
* ```
* t(() => $localize`:@@SI_TOOL_MESSAGE.INPUT_ARGUMENTS:Input Arguments`)
* ```
*/
readonly toolInputArgumentsLabel = input<TranslatableString>(
t(() => $localize`:@@SI_TOOL_MESSAGE.INPUT_ARGUMENTS:Input Arguments`)
);
/**
* Label for tool message output section
*
* @defaultValue
* ```
* t(() => $localize`:@@SI_TOOL_MESSAGE.OUTPUT:Output`)
* ```
*/
readonly toolOutputLabel = input<TranslatableString>(
t(() => $localize`:@@SI_TOOL_MESSAGE.OUTPUT:Output`)
);
/**
* Emitted when a new message is sent
*/
readonly messageSent = output<{
content: string;
attachments: ChatInputAttachment[];
}>();
protected readonly isEmpty = computed(() => this.messages()?.length === 0 && !this.loading());
protected readonly inputInterruptible = computed(() => {
return this.interrupting() || (this.loading() && !this.disableInterrupt() && !this.sending());
});
protected readonly inputSending = computed(() => {
return this.sending() || this.interrupting();
});
protected readonly displayMessages = computed(() => {
const messages = this.messages();
if (!messages?.length) {
if (this.loading()) {
// If loading but no messages, show a single loading AI message
const loadingMessage: AiChatMessage = {
type: 'ai',
content: '',
loading: true
};
return [loadingMessage];
}
return [];
}
// If global loading is true, check if we need to add an AI message
if (this.loading() && messages.length > 0) {
const latestMessage = messages[messages.length - 1];
// Add empty AI message if:
// 1. Latest message is tool call and global loading is on, OR
// 2. Latest message has content and not loading, but global loading is on
const shouldAddAiMessage =
this.isTemplateMessage(latestMessage) ||
latestMessage.type === 'tool' ||
(latestMessage.type === 'ai' &&
this.getContentValue(latestMessage.content) &&
!this.getLoadingState(latestMessage.loading, latestMessage.content, false) &&
!this.isStreamingContent(latestMessage.content)) ||
latestMessage.type === 'user';
if (shouldAddAiMessage) {
const newMessages = [...messages];
const loadingMessage: AiChatMessage = {
type: 'ai',
content: '',
loading: true
};
newMessages.push(loadingMessage);
return newMessages;
}
}
return messages;
});
private getMessageActions(message: ChatMessage): {
primary: MessageAction[];
secondary: MenuItem[];
} {
Iif (this.isTemplateMessage(message) || message.type === 'tool') {
return { primary: [], secondary: [] };
}
const actions = message.actions ?? [];
let primary: MessageAction[] = [];
let secondary: MenuItem[] = [];
const cached = this.messageActionsCache.get(message);
if (cached) {
return cached;
}
const primaryActions = actions.slice(0, 3);
const secondaryActions = actions.slice(3);
primary = primaryActions;
secondary = secondaryActions.map(
action =>
({
...action,
action: action.action as unknown as (actionParam: any, source: MenuItem) => void,
type: 'action'
}) as MenuItem
);
const result = { primary, secondary };
this.messageActionsCache.set(message, result);
return result;
}
protected isTemplateMessage(message: ChatMessage): message is TemplateChatMessage {
return 'template' in message && message.template !== undefined;
}
protected getMessagePrimaryActions(message: ChatMessage): MessageAction[] {
return this.getMessageActions(message).primary;
}
protected getMessageSecondaryActions(message: ChatMessage): MenuItem[] {
return this.getMessageActions(message).secondary;
}
public focus(): void {
const inputComponent = this.chatInput();
Iif (inputComponent) {
inputComponent.focus();
}
}
protected getContentValue<T extends string | object>(content: T | Signal<T> | undefined): T {
if (!content) return '' as T;
return isSignal(content) ? (content as Signal<T>)() : content;
}
protected getOutputValue(
outputValue: string | object | Signal<string | object> | undefined
): string | object | undefined {
Iif (outputValue === undefined || outputValue === null) return undefined;
return isSignal(outputValue) ? (outputValue as Signal<string | object>)() : outputValue;
}
private isEmptyContent(content: string | object | Signal<string | object> | undefined): boolean {
const contentValue = this.getContentValue(content);
return !contentValue;
}
private getLoadingValue(loading: boolean | Signal<boolean> | undefined): boolean {
return loading !== undefined ? (isSignal(loading) ? loading() : loading) : false;
}
private isStreamingContent(
content: string | object | Signal<string | object> | undefined
): boolean {
return isSignal(content) && !this.isEmptyContent(content);
}
/**
* Helper method to get loading state from boolean or signal
* If content (or signal content) is not empty but loading is true, assume streaming (no loading state shown)
* If global loading is true and content is empty, show loading state
*/
protected getLoadingState(
messageLoading: boolean | Signal<boolean> | undefined,
content: string | object | Signal<string | object> | undefined,
isLatest: boolean,
globalLoading: boolean = false,
allowEmptyContent: boolean = false
): boolean {
const messageLoadingValue = this.getLoadingValue(messageLoading);
const isEmptyContent = this.isEmptyContent(content);
// If the content is empty, always show loading
if (isEmptyContent && !allowEmptyContent) {
return true;
}
return messageLoadingValue || (isLatest && globalLoading && isEmptyContent);
}
protected isLatestMessage(message: ChatMessage): boolean {
const messages = this.displayMessages();
Iif (!messages || messages.length === 0) return false;
return messages[messages.length - 1] === message;
}
private isLatestToolMessage(message: ChatMessage): boolean {
const messages = this.displayMessages();
Iif (!messages || messages.length === 0) return false;
// Find the index of the message
const messageIndex = messages.findIndex(m => m === message);
Iif (messageIndex === -1) return false;
// If it's the last message, it's the latest
Iif (messageIndex === messages.length - 1) return true;
// If it's not second to last, it's not the latest
Iif (messageIndex < messages.length - 2) return false;
// Check if the next (actually last) message is a loading message
const nextMessage = messages[messageIndex + 1];
// If next message is a single AI (or loading) message that just started, count this as latest
Iif (!this.isTemplateMessage(nextMessage) && nextMessage.type === 'ai') return true;
return false;
}
protected shouldAutoExpandInputArguments(message: ChatMessage): boolean {
Iif (this.isTemplateMessage(message) || message.type !== 'tool') return false;
if (!message.autoExpandInputArguments) {
return false;
}
return this.isLatestToolMessage(message);
}
protected shouldAutoExpandOutput(message: ChatMessage): boolean {
Iif (this.isTemplateMessage(message) || message.type !== 'tool') return false;
if (!message.autoExpandOutput) {
return false;
}
return this.isLatestToolMessage(message);
}
protected readonly isSignal = isSignal;
}
|