Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { $, clearNode } from '../../../../../../base/browser/dom.js';
import { $, clearNode, hide } from '../../../../../../base/browser/dom.js';
import { IChatThinkingPart, IChatToolInvocation, IChatToolInvocationSerialized } from '../../../common/chatService/chatService.js';
import { IChatContentPartRenderContext, IChatContentPart } from './chatContentParts.js';
import { IChatRendererContent } from '../../../common/model/chatViewModel.js';
Expand Down Expand Up @@ -94,8 +94,8 @@ export class ChatThinkingContentPart extends ChatCollapsibleContentPart implemen
private toolInvocationCount: number = 0;
private streamingCompleted: boolean = false;
private isActive: boolean = true;
private currentToolCallLabel: string | undefined;
private toolInvocations: (IChatToolInvocation | IChatToolInvocationSerialized)[] = [];
private singleToolItemInfo: { element: HTMLElement; originalParent: HTMLElement; originalNextSibling: Node | null } | undefined;

constructor(
content: IChatThinkingPart,
Expand Down Expand Up @@ -356,11 +356,9 @@ export class ChatThinkingContentPart extends ChatCollapsibleContentPart implemen
return;
}

// case where we only have one dropdown in the thinking container and no thinking parts
if (this.toolInvocationCount === 1 && this.extractedTitles.length === 1 && this.currentToolCallLabel && this.currentThinkingValue.trim() === '') {
const title = this.currentToolCallLabel;
this.currentTitle = title;
this.setTitleWithWidgets(new MarkdownString(title), this.instantiationService, this.chatMarkdownAnchorService, this.chatContentMarkdownRenderer);
// case where we only have one tool in the thinking container and no thinking parts, we want to move it back to its original position
if (this.toolInvocationCount === 1 && this.currentThinkingValue.trim() === '' && this.singleToolItemInfo) {
this.restoreSingleToolToOriginalPosition();
return;
}

Expand Down Expand Up @@ -455,6 +453,23 @@ export class ChatThinkingContentPart extends ChatCollapsibleContentPart implemen
this.setFallbackTitle();
}

private restoreSingleToolToOriginalPosition(): void {
if (!this.singleToolItemInfo) {
return;
}

const { element, originalParent, originalNextSibling } = this.singleToolItemInfo;

if (originalNextSibling && originalNextSibling.parentNode === originalParent) {
originalParent.insertBefore(element, originalNextSibling);
} else {
originalParent.appendChild(element);
}

hide(this.domNode);
this.singleToolItemInfo = undefined;
}

private setFallbackTitle(): void {
const finalLabel = this.toolInvocationCount > 0
? localize('chat.thinking.finished.withTools', 'Finished thinking and invoked {0} tool{1}', this.toolInvocationCount, this.toolInvocationCount === 1 ? '' : 's')
Expand All @@ -472,7 +487,18 @@ export class ChatThinkingContentPart extends ChatCollapsibleContentPart implemen
this.updateDropdownClickability();
}

public appendItem(content: HTMLElement, toolInvocationId?: string, toolInvocation?: IChatToolInvocation | IChatToolInvocationSerialized): void {
public appendItem(content: HTMLElement, toolInvocationId?: string, toolInvocation?: IChatToolInvocation | IChatToolInvocationSerialized, originalParent?: HTMLElement): void {
// save the first tool item info for potential restoration later
if (this.toolInvocationCount === 0 && originalParent) {
this.singleToolItemInfo = {
element: content,
originalParent,
originalNextSibling: this.domNode
};
} else {
this.singleToolItemInfo = undefined;
}

const itemWrapper = $('.chat-thinking-tool-wrapper');
const icon = toolInvocationId ? getToolInvocationIcon(toolInvocationId) : Codicon.tools;
const iconElement = createThinkingIcon(icon);
Expand All @@ -490,11 +516,6 @@ export class ChatThinkingContentPart extends ChatCollapsibleContentPart implemen
} else {
toolCallLabel = `Invoked \`${toolInvocationId}\``;
}

if (toolInvocation?.pastTenseMessage) {
this.currentToolCallLabel = typeof toolInvocation.pastTenseMessage === 'string' ? toolInvocation.pastTenseMessage : toolInvocation.pastTenseMessage.value;
}

if (toolInvocation) {
this.toolInvocations.push(toolInvocation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ export class ChatListItemRenderer extends Disposable implements ITreeRenderer<Ch
}, context, templateData);

if (thinkingPart instanceof ChatThinkingContentPart) {
thinkingPart.appendItem(part?.domNode, toolInvocation.toolId, toolInvocation);
thinkingPart.appendItem(part?.domNode, toolInvocation.toolId, toolInvocation, templateData.value);
thinkingPart.addDisposable(part);
thinkingPart.addDisposable(thinkingPart.onDidChangeHeight(() => {
this.updateItemHeight(templateData);
Expand All @@ -1567,7 +1567,7 @@ export class ChatListItemRenderer extends Disposable implements ITreeRenderer<Ch

if (this.shouldPinPart(toolInvocation, context.element)) {
if (lastThinking && part?.domNode && toolInvocation.presentation !== 'hidden') {
lastThinking.appendItem(part?.domNode, toolInvocation.toolId, toolInvocation);
lastThinking.appendItem(part?.domNode, toolInvocation.toolId, toolInvocation, templateData.value);
lastThinking.addDisposable(part);
}
} else {
Expand Down
Loading