Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/chatDebug/chatDebugMessageContentRenderer.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as DOM from '../../../../../base/browser/dom.js';
7
import { DisposableStore } from '../../../../../base/common/lifecycle.js';
8
import { localize } from '../../../../../nls.js';
9
import { IClipboardService } from '../../../../../platform/clipboard/common/clipboardService.js';
10
import { ILanguageService } from '../../../../../editor/common/languages/language.js';
11
import { IChatDebugUserMessageEvent, IChatDebugAgentResponseEvent, IChatDebugEventMessageContent } from '../../common/chatDebugService.js';
12
import { renderSection, tokenizeContent } from './chatDebugToolCallContentRenderer.js';
13
14
const $ = DOM.$;
15
16
/**
17
* Render a user message event with collapsible prompt sections.
18
* JSON content in sections is syntax-highlighted.
19
*/
20
export async function renderUserMessageContent(event: IChatDebugUserMessageEvent, languageService: ILanguageService, clipboardService?: IClipboardService, scrollable?: { scanDomNode(): void }): Promise<{ element: HTMLElement; disposables: DisposableStore }> {
21
const disposables = new DisposableStore();
22
const container = $('div.chat-debug-message-content');
23
container.tabIndex = 0;
24
25
DOM.append(container, $('div.chat-debug-message-content-title', undefined, localize('chatDebug.userMessage', "User Message")));
26
DOM.append(container, $('div.chat-debug-message-content-summary', undefined, event.message));
27
28
if (event.sections.length > 0) {
29
const sectionsContainer = DOM.append(container, $('div.chat-debug-message-sections'));
30
DOM.append(sectionsContainer, $('div.chat-debug-message-sections-label', undefined,
31
localize('chatDebug.promptSections', "Prompt Sections ({0})", event.sections.length)));
32
33
for (const section of event.sections) {
34
const { plainText, tokenizedHtml } = await tokenizeContent(section.content, languageService);
35
renderSection(sectionsContainer, section.name, plainText, tokenizedHtml, disposables, false, clipboardService, scrollable);
36
}
37
}
38
39
return { element: container, disposables };
40
}
41
42
/**
43
* Render an agent response event with collapsible response sections.
44
* JSON content in sections is syntax-highlighted.
45
*/
46
export async function renderAgentResponseContent(event: IChatDebugAgentResponseEvent, languageService: ILanguageService, clipboardService?: IClipboardService, scrollable?: { scanDomNode(): void }): Promise<{ element: HTMLElement; disposables: DisposableStore }> {
47
const disposables = new DisposableStore();
48
const container = $('div.chat-debug-message-content');
49
container.tabIndex = 0;
50
51
DOM.append(container, $('div.chat-debug-message-content-title', undefined, localize('chatDebug.agentResponse', "Agent Response")));
52
DOM.append(container, $('div.chat-debug-message-content-summary', undefined, event.message));
53
54
if (event.sections.length > 0) {
55
const sectionsContainer = DOM.append(container, $('div.chat-debug-message-sections'));
56
DOM.append(sectionsContainer, $('div.chat-debug-message-sections-label', undefined,
57
localize('chatDebug.responseSections', "Response Sections ({0})", event.sections.length)));
58
59
for (const section of event.sections) {
60
const { plainText, tokenizedHtml } = await tokenizeContent(section.content, languageService);
61
renderSection(sectionsContainer, section.name, plainText, tokenizedHtml, disposables, false, clipboardService, scrollable);
62
}
63
}
64
65
return { element: container, disposables };
66
}
67
68
/**
69
* Convert a user message or agent response event to plain text for clipboard / editor output.
70
*/
71
export function messageEventToPlainText(event: IChatDebugUserMessageEvent | IChatDebugAgentResponseEvent): string {
72
const lines: string[] = [];
73
const label = event.kind === 'userMessage' ? localize('chatDebug.userMessage', "User Message") : localize('chatDebug.agentResponse', "Agent Response");
74
lines.push(`${label}: ${event.message}`);
75
lines.push('');
76
77
for (const section of event.sections) {
78
lines.push(`--- ${section.name} ---`);
79
lines.push(section.content);
80
lines.push('');
81
}
82
83
return lines.join('\n');
84
}
85
86
/**
87
* Render a resolved message content (from resolveChatDebugLogEvent) with collapsible sections.
88
* JSON content in sections is syntax-highlighted.
89
*/
90
export async function renderResolvedMessageContent(content: IChatDebugEventMessageContent, languageService: ILanguageService, clipboardService?: IClipboardService, scrollable?: { scanDomNode(): void }): Promise<{ element: HTMLElement; disposables: DisposableStore }> {
91
const disposables = new DisposableStore();
92
const container = $('div.chat-debug-message-content');
93
container.tabIndex = 0;
94
95
const title = content.type === 'user'
96
? localize('chatDebug.userMessage', "User Message")
97
: localize('chatDebug.agentResponse', "Agent Response");
98
DOM.append(container, $('div.chat-debug-message-content-title', undefined, title));
99
DOM.append(container, $('div.chat-debug-message-content-summary', undefined, content.message));
100
101
if (content.sections.length > 0) {
102
const sectionsContainer = DOM.append(container, $('div.chat-debug-message-sections'));
103
const label = content.type === 'user'
104
? localize('chatDebug.promptSections', "Prompt Sections ({0})", content.sections.length)
105
: localize('chatDebug.responseSections', "Response Sections ({0})", content.sections.length);
106
DOM.append(sectionsContainer, $('div.chat-debug-message-sections-label', undefined, label));
107
108
for (const section of content.sections) {
109
const { plainText, tokenizedHtml } = await tokenizeContent(section.content, languageService);
110
renderSection(sectionsContainer, section.name, plainText, tokenizedHtml, disposables, false, clipboardService, scrollable);
111
}
112
}
113
114
return { element: container, disposables };
115
}
116
117
/**
118
* Convert a resolved message content to plain text.
119
*/
120
export function resolvedMessageToPlainText(content: IChatDebugEventMessageContent): string {
121
const lines: string[] = [];
122
const label = content.type === 'user'
123
? localize('chatDebug.userMessage', "User Message")
124
: localize('chatDebug.agentResponse', "Agent Response");
125
lines.push(`${label}: ${content.message}`);
126
lines.push('');
127
128
for (const section of content.sections) {
129
lines.push(`--- ${section.name} ---`);
130
lines.push(section.content);
131
lines.push('');
132
}
133
134
return lines.join('\n');
135
}
136
137