// ***************************************************************************** // Copyright (C) 2024 EclipseSource GmbH. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { ChatAgent, ChatAgentService, ChatModel, ChatRequestModel, ChatResponseContent, ChatResponseModel, ChatService, EditableChatRequestModel, ParsedChatRequestAgentPart, ParsedChatRequestFunctionPart, ParsedChatRequestVariablePart, type ChatRequest, type ChatHierarchyBranch, } from '@theia/ai-chat'; import { AIVariableService } from '@theia/ai-core'; import { AIActivationService } from '@theia/ai-core/lib/browser'; import { CommandRegistry, ContributionProvider, Disposable, DisposableCollection, Emitter, Event, ILogger } from '@theia/core'; import { codicon, CompositeTreeNode, ContextMenuRenderer, DISABLED_CLASS, HoverService, isActivationKey, Key, KeyCode, Message, NodeProps, OpenerService, TreeModel, TreeNode, TreeProps, TreeWidget, UnsafeWidgetUtilities, Widget, type ReactWidget } from '@theia/core/lib/browser'; import { ContextKey, ContextKeyService } from '@theia/core/lib/browser/context-key-service'; import { ChatFindHighlighter } from '../chat-find/chat-find-highlighter.js'; import { ChatFindMatch } from '../chat-find/chat-find-matcher.js'; import { ChatFindState, ChatFindWidget } from '../chat-find/chat-find-widget.js'; import { nls } from '@theia/core/lib/common/nls'; import { inject, injectable, named, postConstruct } from '@theia/core/shared/inversify'; import * as React from '@theia/core/shared/react'; import { ImageContextVariable } from '@theia/ai-chat/lib/common/image-context-variable'; import { MarkdownStringImpl } from '@theia/core/lib/common/markdown-rendering'; import { ChatNodeToolbarActionContribution } from '../chat-node-toolbar-action-contribution.js'; import { ChatResponsePartRenderer } from '../chat-response-part-renderer.js'; import { formatTokenCount } from '../chat-token-usage-indicator-util.js'; import { MarkdownRendering, useMarkdownRendering } from '../chat-response-renderer/markdown-part-renderer.js'; import { ProgressMessage } from '../chat-progress-message.js'; import { AIChatTreeInputFactory, type AIChatTreeInputWidget } from './chat-view-tree-input-widget.js'; import { PromptVariantBadge } from './prompt-variant-badge.js'; import { ModelBadge } from './model-badge.js'; // TODO Instead of directly operating on the ChatRequestModel we could use an intermediate view model export interface RequestNode extends TreeNode { request: ChatRequestModel, branch: ChatHierarchyBranch, sessionId: string } export const isRequestNode = (node: TreeNode): node is RequestNode => 'request' in node; export interface EditableRequestNode extends RequestNode { request: EditableChatRequestModel } export const isEditableRequestNode = (node: TreeNode): node is EditableRequestNode => isRequestNode(node) && EditableChatRequestModel.is(node.request); // TODO Instead of directly operating on the ChatResponseModel we could use an intermediate view model export interface ResponseNode extends TreeNode { response: ChatResponseModel, sessionId: string } export const isResponseNode = (node: TreeNode): node is ResponseNode => 'response' in node; export function isEnterKey(e: React.KeyboardEvent): boolean { return Key.ENTER.keyCode === KeyCode.createKeyCode(e.nativeEvent).key?.keyCode; } export const ChatWelcomeMessageProvider = Symbol('ChatWelcomeMessageProvider'); export interface ChatWelcomeMessageProvider { renderWelcomeMessage?(): React.ReactNode; renderDisabledMessage?(): React.ReactNode; readonly hasReadyModels?: boolean; readonly modelRequirementBypassed?: boolean; readonly defaultAgent?: string; readonly onStateChanged?: Event; /** Optional priority for rendering order. Higher values render first. Default: 0 */ readonly priority?: number; } @injectable() export class ChatViewTreeWidget extends TreeWidget { static readonly ID = 'chat-tree-widget'; static readonly CONTEXT_MENU = ['chat-tree-context-menu']; /** How long typing in the find bar must pause before a match in a collapsed message expands it. */ protected static readonly FIND_EXPAND_DELAY_MS = 300; @inject(ContributionProvider) @named(ChatResponsePartRenderer) protected readonly chatResponsePartRenderers: ContributionProvider>; @inject(ContributionProvider) @named(ChatNodeToolbarActionContribution) protected readonly chatNodeToolbarActionContributions: ContributionProvider; @inject(ChatAgentService) protected chatAgentService: ChatAgentService; @inject(AIVariableService) protected readonly variableService: AIVariableService; @inject(CommandRegistry) protected commandRegistry: CommandRegistry; @inject(OpenerService) protected readonly openerService: OpenerService; @inject(HoverService) protected hoverService: HoverService; @inject(ContributionProvider) @named(ChatWelcomeMessageProvider) protected readonly welcomeMessageProviders: ContributionProvider; @inject(AIChatTreeInputFactory) protected inputWidgetFactory: AIChatTreeInputFactory; @inject(AIActivationService) protected readonly activationService: AIActivationService; @inject(ChatService) protected readonly chatService: ChatService; @inject(ContextKeyService) protected readonly contextKeyService: ContextKeyService; @inject(ILogger) @named('ai-chat-ui:ChatViewTreeWidget') protected readonly logger: ILogger; protected chatResponseFocusKey: ContextKey; @inject(ChatFindWidget) protected readonly findWidget: ChatFindWidget; @inject(ChatFindHighlighter) protected readonly findHighlighter: ChatFindHighlighter; protected readonly onDidSubmitEditEmitter = new Emitter(); onDidSubmitEdit = this.onDidSubmitEditEmitter.event; protected readonly chatInputs: Map = new Map(); /** Ids of the request and response nodes whose message the user has collapsed. */ protected readonly collapsedNodeIds = new Set(); /** The query of the find bar with its options, to tell a reveal after typing from a step to another match. */ protected findQuery: string | undefined; protected findQueryChanged = false; /** The pending reveal of a match in a collapsed message, see {@link handleFindRevealRequest}. */ protected findRevealTimeout: ReturnType | undefined; protected _shouldScrollToEnd = true; protected isEnabled = false; protected chatModelId: string; /** Tracks if we are at the bottom for showing the scroll-to-bottom button. */ protected atBottom = true; /** * Track the visibility of the scroll button. */ protected _showScrollButton = false; onScrollLockChange?: (temporaryLocked: boolean) => void; set shouldScrollToEnd(shouldScrollToEnd: boolean) { this._shouldScrollToEnd = shouldScrollToEnd; this.shouldScrollToRow = this._shouldScrollToEnd; } get shouldScrollToEnd(): boolean { return this._shouldScrollToEnd; } constructor( @inject(TreeProps) props: TreeProps, @inject(TreeModel) model: TreeModel, @inject(ContextMenuRenderer) contextMenuRenderer: ContextMenuRenderer ) { super(props, model, contextMenuRenderer); this.id = ChatViewTreeWidget.ID; this.title.closable = false; model.root = { id: 'ChatTree', name: 'ChatRootNode', parent: undefined, visible: false, children: [], } as CompositeTreeNode; } @postConstruct() protected override init(): void { super.init(); this.id = ChatViewTreeWidget.ID + '-treeContainer'; this.addClass('treeContainer'); this.chatResponseFocusKey = this.contextKeyService.createKey('chatResponseFocus', false); this.node.setAttribute('tabindex', '0'); this.node.setAttribute('aria-label', nls.localize('theia/ai/chat-ui/chatResponses', 'Chat responses')); this.findWidget.fallbackFocusTarget = this.node; this.toDispose.pushAll([ this.findWidget, this.findHighlighter, this.findWidget.onDidChangeState(state => this.handleFindStateChange(state)), this.findWidget.onDidRequestReveal(match => this.handleFindRevealRequest(match)), Disposable.create(() => clearTimeout(this.findRevealTimeout)) ]); this.toDispose.pushAll([ this.toDisposeOnChatModelChange, this.activationService.onDidChangeCanRun(change => { this.chatInputs.forEach(widget => { widget.setEnabled(change); }); this.update(); }), this.onAtBottomStateChange(atBottom => { this.handleAtBottomStateChange(atBottom); }) ]); for (const provider of this.welcomeMessageProviders.getContributions()) { if (provider.onStateChanged) { this.toDispose.push( provider.onStateChanged(() => { this.update(); }) ); } } // Re-render node toolbars when a contribution signals its actions changed (e.g. a gating setting toggled). for (const contribution of this.chatNodeToolbarActionContributions.getContributions()) { if (contribution.onDidChange) { this.toDispose.push(contribution.onDidChange(() => this.update())); } } } public setEnabled(enabled: boolean): void { this.isEnabled = enabled; if (!enabled) { this.findWidget.dismiss(); } this.update(); } /** Opens the find bar over the response tree (or refocuses it when already open). */ showFind(): void { this.findWidget.open(); } hideFind(): void { this.findWidget.dismiss(); } get isFindVisible(): boolean { return this.findWidget.isOpen; } /** Whether the tracked session has content to search; false while the welcome/session-list screen shows. */ get canFind(): boolean { return this.findWidget.canFind; } protected handleFindStateChange(state: ChatFindState): void { // Every reveal follows a state change: with another query, it comes from typing or toggling an option. const query = state.regexp?.toString(); this.findQueryChanged = query !== this.findQuery; this.findQuery = query; if (!state.current) { // The bar was closed or the query has no match: a pending reveal no longer applies. clearTimeout(this.findRevealTimeout); } this.findHighlighter.update(this.node, state.regexp, state.matches, state.current); } /** * Reveals a match requested by the find bar. While a query is typed or an option toggled, a match in a collapsed * message is only revealed once typing pauses, as expanding the message of each intermediate match would make the * view jump. Stepping to the next or previous match reveals it right away. */ protected handleFindRevealRequest(match: ChatFindMatch): void { clearTimeout(this.findRevealTimeout); const row = this.rows.get(match.nodeId); const collapsed = row !== undefined && (isRequestNode(row.node) || isResponseNode(row.node)) && this.isCollapsed(row.node); if (!collapsed || !this.findQueryChanged) { this.revealFindMatch(match); return; } this.findRevealTimeout = setTimeout(() => { const current = this.findWidget.currentMatch; // Skip the reveal when it no longer applies: the bar was closed, or the query has no match there anymore. if (this.isFindVisible && current && ChatFindMatch.key(current) === ChatFindMatch.key(match)) { this.revealFindMatch(match); } }, ChatViewTreeWidget.FIND_EXPAND_DELAY_MS); } /** * Reveals a match: mounts the owning row via virtuoso when it is not rendered yet, then re-applies the highlights * and scrolls the matched text itself into view (rows can be taller than the viewport). */ protected revealFindMatch(match: ChatFindMatch): void { const row = this.rows.get(match.nodeId); if (row === undefined) { return; } // A collapsed message does not render its content: expand it and wait until the match is rendered, // at most about half a second at 60 Hz. const expanded = (isRequestNode(row.node) || isResponseNode(row.node)) && this.isCollapsed(row.node); if (expanded) { this.collapsedNodeIds.delete(match.nodeId); this.update(); } let framesLeft = expanded ? 30 : 0; const settle = (): void => { this.findHighlighter.refresh(); if (!this.findHighlighter.hasCurrentTarget && framesLeft-- > 0) { requestAnimationFrame(settle); return; } this.findHighlighter.scrollCurrentIntoView(this.node); }; if (this.findHighlighter.findRow(this.node, match.nodeId)) { requestAnimationFrame(settle); return; } this.view?.list?.scrollIntoView({ index: row.index, align: 'center', done: settle }); } protected override onAfterAttach(msg: Message): void { super.onAfterAttach(msg); // Registered per attach, not in `init()`: `addEventListener` disposes on detach, so listeners registered // once would be lost the first time the view is moved (e.g. to the main area), leaving `chatResponseFocus` // stuck and every keybinding scoped to it dead until the page is reloaded. this.addEventListener(this.node, 'focusin', () => this.chatResponseFocusKey.set(true)); this.addEventListener(this.node, 'focusout', () => this.chatResponseFocusKey.set(false)); // The tree node is a React root, so the find bar is attached as a sibling right before it, // the same way the core TreeWidget hosts its SearchBox. if (this.findWidget.isAttached) { Widget.detach(this.findWidget); } if (this.node.parentElement) { UnsafeWidgetUtilities.attach(this.findWidget, this.node.parentElement, this.node); } } protected override onBeforeDetach(msg: Message): void { this.chatResponseFocusKey.set(false); if (this.findWidget.isAttached) { Widget.detach(this.findWidget); } super.onBeforeDetach(msg); } /** Toggles auto-scroll and the scroll-to-bottom button based on whether the viewport includes the bottom of the list. */ protected handleAtBottomStateChange(isAtBottom: boolean): void { if (isAtBottom !== this.atBottom) { this.atBottom = isAtBottom; if (isAtBottom) { // Arrived at bottom — re-enable auto-scroll and hide the button this._showScrollButton = false; this.setTemporaryScrollLock(false); } else { // Left the bottom — lock auto-scroll and show the button this._showScrollButton = true; this.setTemporaryScrollLock(true); } this.update(); } } protected setTemporaryScrollLock(enabled: boolean): void { // Immediately apply scroll lock changes without delay this.onScrollLockChange?.(enabled); // Update cached scrollToRow so that outdated values do not cause unwanted scrolling on update() this.updateScrollToRow(); } protected override renderTree(model: TreeModel): React.ReactNode { if (!this.isEnabled) { return this.renderDisabledMessage(); } const tree = CompositeTreeNode.is(model.root) && model.root.children?.length > 0 ? super.renderTree(model) : this.renderWelcomeMessage(); return {tree} {this.renderScrollToBottomButton()} ; } /** Shows the scroll to bottom button if not at the bottom (debounced). */ protected renderScrollToBottomButton(): React.ReactNode { if (!this._showScrollButton) { return undefined; } // Down-arrow, Theia codicon, fixed overlay on widget return