This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathrequestLogTree.ts
More file actions
917 lines (781 loc) · 31.4 KB
/
Copy pathrequestLogTree.ts
File metadata and controls
917 lines (781 loc) · 31.4 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IHTMLRouter } from '@vscode/prompt-tsx';
import { createServer } from 'http';
import { AddressInfo } from 'net';
import * as os from 'os';
import * as path from 'path';
import * as tar from 'tar';
import * as vscode from 'vscode';
import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';
import { OutputChannelName } from '../../../platform/log/vscode/outputChannelLogTarget';
import { ChatRequestScheme, ILoggedElementInfo, ILoggedRequestInfo, ILoggedToolCall, IRequestLogger, LoggedInfo, LoggedInfoKind, LoggedRequestKind } from '../../../platform/requestLogger/node/requestLogger';
import { assertNever } from '../../../util/vs/base/common/assert';
import { RunOnceScheduler } from '../../../util/vs/base/common/async';
import { Disposable, toDisposable } from '../../../util/vs/base/common/lifecycle';
import { LRUCache } from '../../../util/vs/base/common/map';
import { isDefined } from '../../../util/vs/base/common/types';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { ChatRequest } from '../../../vscodeTypes';
import { IExtensionContribution } from '../../common/contributions';
const showHtmlCommand = 'vscode.copilot.chat.showRequestHtmlItem';
const exportLogItemCommand = 'github.copilot.chat.debug.exportLogItem';
const exportPromptArchiveCommand = 'github.copilot.chat.debug.exportPromptArchive';
const exportPromptLogsAsJsonCommand = 'github.copilot.chat.debug.exportPromptLogsAsJson';
const exportAllPromptLogsAsJsonCommand = 'github.copilot.chat.debug.exportAllPromptLogsAsJson';
const saveCurrentMarkdownCommand = 'github.copilot.chat.debug.saveCurrentMarkdown';
const showRawRequestBodyCommand = 'github.copilot.chat.debug.showRawRequestBody';
export class RequestLogTree extends Disposable implements IExtensionContribution {
readonly id = 'requestLogTree';
private readonly chatRequestProvider: ChatRequestProvider;
private readonly treeView: vscode.TreeView<TreeItem>;
constructor(
@IInstantiationService instantiationService: IInstantiationService,
@IRequestLogger requestLogger: IRequestLogger,
) {
super();
this.chatRequestProvider = this._register(instantiationService.createInstance(ChatRequestProvider));
this.treeView = this._register(vscode.window.createTreeView('copilot-chat', { treeDataProvider: this.chatRequestProvider }));
this.chatRequestProvider.setVisible(this.treeView.visible);
this._register(this.treeView.onDidChangeVisibility(e => this.chatRequestProvider.setVisible(e.visible)));
let server: RequestServer | undefined;
const getExportableLogEntries = (treeItem: ChatPromptItem): LoggedInfo[] => {
if (!treeItem || !treeItem.children) {
return [];
}
const logEntries = treeItem.children.map(child => {
if (child instanceof ChatRequestItem || child instanceof ToolCallItem || child instanceof ChatElementItem) {
return child.info;
}
return undefined; // Skip non-loggable items
}).filter(isDefined);
return logEntries;
};
// Helper method to process log entries for a single prompt
const preparePromptLogsAsJson = async (treeItem: ChatPromptItem): Promise<any> => {
const logEntries = getExportableLogEntries(treeItem);
if (logEntries.length === 0) {
return;
}
const promptLogs: any[] = [];
for (const logEntry of logEntries) {
try {
promptLogs.push(await logEntry.toJSON());
} catch (error) {
// If we can't get content for this entry, add an error object
promptLogs.push({
id: logEntry.id,
kind: 'error',
error: error?.toString() || 'Unknown error',
timestamp: new Date().toISOString()
});
}
}
return {
prompt: treeItem.request.prompt,
promptId: treeItem.id,
hasSeen: treeItem.hasSeen,
logCount: promptLogs.length,
logs: promptLogs
};
};
this._register(vscode.commands.registerCommand(showHtmlCommand, async (elementId: string) => {
if (!server) {
server = this._register(new RequestServer());
}
const req = requestLogger.getRequests().find(r => r.kind === LoggedInfoKind.Element && r.id === elementId);
if (!req) {
return;
}
const address = await server.addRouter(req as ILoggedElementInfo);
await vscode.commands.executeCommand('simpleBrowser.show', address);
}));
this._register(vscode.commands.registerCommand(exportLogItemCommand, async (treeItem: TreeItem) => {
if (!treeItem || !treeItem.id) {
return;
}
let logEntry: LoggedInfo;
if (treeItem instanceof ChatPromptItem) {
// ChatPromptItem doesn't represent a single log entry
vscode.window.showWarningMessage('Cannot export chat prompt item. Please select a specific request, tool call, or element.');
return;
} else if (treeItem instanceof ChatRequestItem || treeItem instanceof ToolCallItem || treeItem instanceof ChatElementItem) {
logEntry = treeItem.info;
} else {
vscode.window.showErrorMessage('Unable to determine log entry ID for this item.');
return;
}
// Check if this entry type supports markdown export
if (logEntry.kind === LoggedInfoKind.Element) {
vscode.window.showWarningMessage('Element entries cannot be exported as markdown. They contain HTML content that can be viewed in the browser.');
return;
}
// Generate a default filename based on the entry type and id
let defaultFilename: string;
switch (logEntry.kind) {
case LoggedInfoKind.Request: {
const requestEntry = logEntry as ILoggedRequestInfo;
const debugName = requestEntry.entry.debugName.replace(/\W/g, '_');
defaultFilename = `${debugName}_${logEntry.id}.copilotmd`;
break;
}
case LoggedInfoKind.ToolCall: {
const toolEntry = logEntry as ILoggedToolCall;
const toolName = toolEntry.name.replace(/\W/g, '_');
defaultFilename = `tool_${toolName}_${logEntry.id}.copilotmd`;
break;
}
}
if (!defaultFilename) {
return;
}
// Show save dialog
const saveUri = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(path.join(os.homedir(), defaultFilename)),
filters: {
'Copilot Markdown': ['copilotmd'],
'Markdown': ['md'],
'All Files': ['*']
},
title: 'Export Log Entry'
});
if (!saveUri) {
return; // User cancelled
}
try {
// Get the content using the virtual document URI
const virtualUri = vscode.Uri.parse(ChatRequestScheme.buildUri({ kind: 'request', id: logEntry.id }));
const document = await vscode.workspace.openTextDocument(virtualUri);
const content = document.getText();
// Write to the selected file
await vscode.workspace.fs.writeFile(saveUri, Buffer.from(content, 'utf8'));
// Show success message with option to open the file
const openAction = 'Open File';
const result = await vscode.window.showInformationMessage(
`Successfully exported to ${saveUri.fsPath}`,
openAction
);
if (result === openAction) {
await vscode.commands.executeCommand('vscode.open', saveUri);
}
} catch (error) {
vscode.window.showErrorMessage(`Failed to export log entry: ${error}`);
}
}));
// Save the currently opened chat log (ccreq:*.copilotmd) to a file
this._register(vscode.commands.registerCommand(saveCurrentMarkdownCommand, async (...args: any[]) => {
// Accept resource from menu invocation (editor/title passes the resource)
let resource: vscode.Uri | undefined;
const first = args?.[0];
if (first instanceof vscode.Uri) {
resource = first;
} else if (first && typeof first === 'object') {
// Some menu invocations pass { resource: Uri }
const candidate = (first as { resource?: vscode.Uri }).resource;
if (candidate instanceof vscode.Uri) {
resource = candidate;
}
}
// Fallback to the active editor's document
resource ??= vscode.window.activeTextEditor?.document.uri;
if (!resource) {
vscode.window.showWarningMessage('No document is active to save.');
return;
}
if (resource.scheme !== ChatRequestScheme.chatRequestScheme) {
vscode.window.showWarningMessage('This command only works for Copilot request documents.');
return;
}
// Determine a default filename from the virtual URI
const parseResult = ChatRequestScheme.parseUri(resource.toString());
const defaultBase = parseResult && parseResult.data.kind === 'request' ? parseResult.data.id : 'latestrequest';
const defaultFilename = `${defaultBase}.md`;
const saveUri = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(path.join(os.homedir(), defaultFilename)),
filters: {
'Markdown': ['md'],
'Copilot Markdown': ['copilotmd'],
'All Files': ['*']
},
title: 'Save Markdown As'
});
if (!saveUri) {
return; // User cancelled
}
try {
// Read the text from the virtual document URI explicitly
const doc = await vscode.workspace.openTextDocument(resource);
await vscode.workspace.fs.writeFile(saveUri, Buffer.from(doc.getText(), 'utf8'));
const openAction = 'Open File';
const result = await vscode.window.showInformationMessage(
`Successfully saved to ${saveUri.fsPath}`,
openAction
);
if (result === openAction) {
await vscode.commands.executeCommand('vscode.open', saveUri);
}
} catch (error) {
vscode.window.showErrorMessage(`Failed to save markdown: ${error}`);
}
}));
this._register(vscode.commands.registerCommand(exportPromptArchiveCommand, async (treeItem: ChatPromptItem) => {
const logEntries = getExportableLogEntries(treeItem);
if (logEntries.length === 0) {
vscode.window.showInformationMessage('No exportable entries found in this prompt.');
return;
}
// Generate a default filename based on the prompt
const promptText = treeItem.request.prompt.replace(/\W/g, '_').substring(0, 50);
const defaultFilename = `${promptText}_exports.tar.gz`;
// Show save dialog
const saveUri = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(path.join(os.homedir(), defaultFilename)),
filters: {
'Tar Archive': ['tar.gz', 'tgz'],
'All Files': ['*']
},
title: 'Export Prompt Archive'
});
if (!saveUri) {
return; // User cancelled
}
try {
// Create temporary directory for files
const tempDir = path.join(os.tmpdir(), `vscode-copilot-export-${Date.now()}-${Math.random().toString(36).substring(2, 10)}`);
await vscode.workspace.fs.createDirectory(vscode.Uri.file(tempDir));
const filesToArchive: string[] = [];
// Export each child to a temporary file
for (const logEntry of logEntries) {
// Generate filename for this entry
let filename: string;
switch (logEntry.kind) {
case LoggedInfoKind.Request: {
const requestEntry = logEntry as ILoggedRequestInfo;
const debugName = requestEntry.entry.debugName.replace(/\W/g, '_');
filename = `${debugName}_${logEntry.id}.copilotmd`;
break;
}
case LoggedInfoKind.ToolCall: {
const toolEntry = logEntry as ILoggedToolCall;
const toolName = toolEntry.name.replace(/\W/g, '_');
filename = `tool_${toolName}_${logEntry.id}.copilotmd`;
break;
}
default:
continue;
}
// Get the content and write to temporary file
const virtualUri = vscode.Uri.parse(ChatRequestScheme.buildUri({ kind: 'request', id: logEntry.id }));
const document = await vscode.workspace.openTextDocument(virtualUri);
const content = document.getText();
const tempFilePath = path.join(tempDir, filename);
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFilePath), Buffer.from(content, 'utf8'));
filesToArchive.push(tempFilePath);
}
if (filesToArchive.length > 0) {
// Create tar.gz archive
await tar.create(
{
gzip: true,
file: saveUri.fsPath,
cwd: tempDir
},
filesToArchive.map(f => path.basename(f))
);
// Clean up temporary files
for (const filePath of filesToArchive) {
await vscode.workspace.fs.delete(vscode.Uri.file(filePath));
}
await vscode.workspace.fs.delete(vscode.Uri.file(tempDir));
// Show success message with option to reveal the file
const revealAction = 'Reveal in Explorer';
const result = await vscode.window.showInformationMessage(
`Successfully exported ${filesToArchive.length} entries to ${saveUri.fsPath}`,
revealAction
);
if (result === revealAction) {
await vscode.commands.executeCommand('revealFileInOS', saveUri);
}
} else {
vscode.window.showWarningMessage('No valid entries could be exported.');
}
} catch (error) {
vscode.window.showErrorMessage(`Failed to export prompt archive: ${error}`);
}
}));
this._register(vscode.commands.registerCommand(exportPromptLogsAsJsonCommand, async (treeItem: ChatPromptItem) => {
const promptObject = await preparePromptLogsAsJson(treeItem);
if (!promptObject) {
vscode.window.showWarningMessage('No exportable entries found for this prompt.');
return;
}
// Generate a default filename based on the prompt
const promptText = treeItem.request.prompt.replace(/\W/g, '_').substring(0, 50);
const defaultFilename = `${promptText}_logs.chatreplay.json`;
// Show save dialog
const saveUri = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(path.join(os.homedir(), defaultFilename)),
filters: {
'JSON': ['json'],
'All Files': ['*']
},
title: 'Export Prompt Logs as JSON'
});
if (!saveUri) {
return; // User cancelled
}
try {
// Convert to JSON
const finalContent = JSON.stringify(promptObject, null, 2);
// Write to the selected file
await vscode.workspace.fs.writeFile(saveUri, Buffer.from(finalContent, 'utf8'));
// Show success message with option to reveal the file
const revealAction = 'Reveal in Explorer';
const openAction = 'Open File';
const result = await vscode.window.showInformationMessage(
`Successfully exported prompt with ${promptObject.logCount} log entries to ${saveUri.fsPath}`,
revealAction,
openAction
);
if (result === revealAction) {
await vscode.commands.executeCommand('revealFileInOS', saveUri);
} else if (result === openAction) {
await vscode.commands.executeCommand('vscode.open', saveUri);
}
} catch (error) {
vscode.window.showErrorMessage(`Failed to export prompt logs as JSON: ${error}`);
}
}));
this._register(vscode.commands.registerCommand(exportAllPromptLogsAsJsonCommand, async (savePath?: string) => {
// Build the tree structure to get all chat prompt items
const allTreeItems = await this.chatRequestProvider.getChildren();
if (!allTreeItems || allTreeItems.length === 0) {
vscode.window.showInformationMessage('No chat prompts found to export.');
return;
}
// Filter to get only ChatPromptItem instances
const chatPromptItems = allTreeItems.filter((item): item is ChatPromptItem => item instanceof ChatPromptItem);
if (chatPromptItems.length === 0) {
vscode.window.showInformationMessage('No chat prompts found to export.');
return;
}
let saveUri: vscode.Uri;
if (savePath && typeof savePath === 'string') {
// Use provided path
saveUri = vscode.Uri.file(savePath);
} else {
// Generate a default filename based on current timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').substring(0, 19);
const defaultFilename = `copilot_all_prompts_${timestamp}.chatreplay.json`;
// Show save dialog
const dialogResult = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(path.join(os.homedir(), defaultFilename)),
filters: {
'JSON': ['json'],
'All Files': ['*']
},
title: 'Export All Prompt Logs as JSON'
});
if (!dialogResult) {
return; // User cancelled
}
saveUri = dialogResult;
}
try {
const allPromptsContent: any[] = [];
let totalLogEntries = 0;
// Process each chat prompt item using the shared function
for (const chatPromptItem of chatPromptItems) {
// Use the shared processing function
const promptObject = await preparePromptLogsAsJson(chatPromptItem);
if (promptObject) {
allPromptsContent.push(promptObject);
totalLogEntries += promptObject.logCount;
}
}
// Combine all content as JSON
const finalContent = JSON.stringify({
exportedAt: new Date().toISOString(),
totalPrompts: allPromptsContent.length,
totalLogEntries: totalLogEntries,
prompts: allPromptsContent
}, null, 2);
// Write to the selected file
await vscode.workspace.fs.writeFile(saveUri, Buffer.from(finalContent, 'utf8'));
// Show success message with option to reveal the file
const revealAction = 'Reveal in Explorer';
const openAction = 'Open File';
const result = await vscode.window.showInformationMessage(
`Successfully exported ${allPromptsContent.length} prompts with ${totalLogEntries} log entries to ${saveUri.fsPath}`,
revealAction,
openAction
);
if (result === revealAction) {
await vscode.commands.executeCommand('revealFileInOS', saveUri);
} else if (result === openAction) {
await vscode.commands.executeCommand('vscode.open', saveUri);
}
} catch (error) {
vscode.window.showErrorMessage(`Failed to export all prompt logs as JSON: ${error}`);
}
}));
this._register(vscode.commands.registerCommand(showRawRequestBodyCommand, async (arg?: ChatPromptItem) => {
const requestId = arg?.id;
if (!requestId) {
return;
}
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(ChatRequestScheme.buildUri({ kind: 'request', id: requestId }, 'rawrequest')));
}));
this._register(vscode.commands.registerCommand('github.copilot.debug.showOutputChannel', async () => {
// Yes this is the correct auto-generated command for our output channel
await vscode.commands.executeCommand(`workbench.action.output.show.GitHub.copilot-chat.${OutputChannelName}`);
}));
}
}
/**
* Servers that shows logged request html for the simple browser. Doing this
* is annoying, but the markdown renderer is limited and doesn't show full HTML,
* and the simple browser extension can't render internal or `file://` URIs.
*
* Note that we don't need secret tokens or anything at this point because the
* server is read-only and does not advertise any CORS headers.
*/
class RequestServer extends Disposable {
public port: Promise<number>;
private routers = new LRUCache<string, IHTMLRouter>(10);
constructor() {
super();
const server = createServer((req, res) => {
for (const [key, router] of this.routers) {
if (router.route(req, res)) {
this.routers.get(key); // LRU touch
return;
}
}
res.statusCode = 404;
res.end('Not Found');
});
this.port = new Promise<number>((resolve, reject) => {
server.listen(0, '127.0.0.1', () => resolve((server.address() as AddressInfo).port)).on('error', reject);
});
this._register(toDisposable(() => server.close()));
}
async addRouter(info: ILoggedElementInfo) {
const prev = this.routers.get(info.id);
if (prev) {
return prev.address;
}
const port = await this.port;
const router = info.trace.serveRouter(`http://127.0.0.1:${port}`);
this.routers.set(info.id, router);
return router.address;
}
}
type TreeItem = ChatPromptItem | ChatRequestItem | ChatElementItem | ToolCallItem;
class ChatRequestProvider extends Disposable implements vscode.TreeDataProvider<TreeItem> {
private readonly filters: LogTreeFilters;
private rootItems: (ChatPromptItem | TreeChildItem)[] = [];
private seenChatRequests = new WeakSet<ChatRequest>();
private processedCount = 0;
private currentPrompt: ChatPromptItem | undefined;
private readonly refreshScheduler: RunOnceScheduler;
private pendingRootRefresh = false;
private readonly pendingPromptRefresh = new Set<ChatPromptItem>();
private readonly refreshDelay = 50;
private isVisible = false;
private needsRefreshWhenVisible = false;
constructor(
@IRequestLogger private readonly requestLogger: IRequestLogger,
@IInstantiationService instantiationService: IInstantiationService,
) {
super();
this.filters = this._register(instantiationService.createInstance(LogTreeFilters));
this._register(new LogTreeFilterCommands(this.filters));
this._register(this.requestLogger.onDidChangeRequests(() => this.handleRequestChange()));
this._register(this.filters.onDidChangeFilters(() => this.handleFilterChange()));
this.refreshScheduler = this._register(new RunOnceScheduler(() => this.flushRefreshQueue(), 0));
this.rebuildFromScratch();
}
private readonly _onDidChangeTreeData = new vscode.EventEmitter<TreeItem | undefined | void>();
onDidChangeTreeData = this._onDidChangeTreeData.event;
getTreeItem(element: TreeItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element;
}
getChildren(element?: TreeItem | undefined): vscode.ProviderResult<TreeItem[]> {
if (element instanceof ChatPromptItem) {
return element.children.filter(child => this.filters.itemIncluded(child));
}
if (element) {
return [];
}
return this.rootItems.filter(item => this.filters.itemIncluded(item));
}
private rebuildFromScratch(): void {
this.rootItems = [];
this.seenChatRequests = new WeakSet<ChatRequest>();
this.processedCount = 0;
this.currentPrompt = undefined;
this.appendNewEntries();
}
private handleRequestChange(): void {
this.appendNewEntries();
}
private handleFilterChange(): void {
if (this.isVisible) {
this._onDidChangeTreeData.fire(undefined);
} else {
this.pendingRootRefresh = true;
this.needsRefreshWhenVisible = true;
}
}
setVisible(visible: boolean): void {
this.isVisible = visible;
if (visible) {
if (this.pendingRootRefresh || this.pendingPromptRefresh.size || this.needsRefreshWhenVisible) {
this.refreshScheduler.schedule(0);
}
}
}
private appendNewEntries(): void {
const requests = this.requestLogger.getRequests();
let rootChanged = false;
const promptsToRefresh = new Set<ChatPromptItem>();
const newPrompts = new Set<ChatPromptItem>();
if (requests.length < this.processedCount) {
this.rootItems = [];
this.seenChatRequests = new WeakSet<ChatRequest>();
this.processedCount = 0;
this.currentPrompt = undefined;
rootChanged = true;
}
for (let i = this.processedCount; i < requests.length; i++) {
const info = requests[i];
const child = this.logToTreeItem(info);
const request = info.chatRequest;
if (request) {
const hasSeen = this.seenChatRequests.has(request);
if (!this.currentPrompt || this.currentPrompt.request !== request) {
this.currentPrompt = ChatPromptItem.create(info, request, hasSeen);
this.currentPrompt.children.length = 0;
this.rootItems.push(this.currentPrompt);
this.seenChatRequests.add(request);
rootChanged = true;
newPrompts.add(this.currentPrompt);
}
if (!this.currentPrompt.children.some(c => c.id === child.id)) {
this.currentPrompt.children.push(child);
promptsToRefresh.add(this.currentPrompt);
}
} else {
this.currentPrompt = undefined;
this.rootItems.push(child);
rootChanged = true;
}
}
this.processedCount = requests.length;
if (rootChanged) {
this.pendingRootRefresh = true;
}
for (const prompt of promptsToRefresh) {
if (rootChanged && newPrompts.has(prompt)) {
continue;
}
this.pendingPromptRefresh.add(prompt);
}
if (this.isVisible) {
this.refreshScheduler.schedule(this.refreshDelay);
} else {
this.needsRefreshWhenVisible = true;
}
}
private logToTreeItem(r: LoggedInfo): TreeChildItem {
switch (r.kind) {
case LoggedInfoKind.Request:
return new ChatRequestItem(r);
case LoggedInfoKind.Element:
return new ChatElementItem(r);
case LoggedInfoKind.ToolCall:
return new ToolCallItem(r);
default:
assertNever(r);
}
}
private flushRefreshQueue(): void {
if (!this.isVisible) {
return;
}
if (this.pendingRootRefresh) {
this._onDidChangeTreeData.fire(undefined);
} else {
for (const prompt of this.pendingPromptRefresh) {
this._onDidChangeTreeData.fire(prompt);
}
}
this.pendingRootRefresh = false;
this.pendingPromptRefresh.clear();
this.needsRefreshWhenVisible = false;
}
}
type TreeChildItem = ChatRequestItem | ChatElementItem | ToolCallItem;
class ChatPromptItem extends vscode.TreeItem {
private static readonly ids = new WeakMap<LoggedInfo, ChatPromptItem>();
override readonly contextValue = 'chatprompt';
public children: TreeChildItem[] = [];
public override id: string | undefined;
public static create(info: LoggedInfo, request: ChatRequest, hasSeen: boolean) {
const existing = ChatPromptItem.ids.get(info);
if (existing) {
return existing;
}
const item = new ChatPromptItem(request, hasSeen);
item.id = info.id + '-prompt';
ChatPromptItem.ids.set(info, item);
return item;
}
protected constructor(public readonly request: ChatRequest, public readonly hasSeen: boolean) {
super(request.prompt, vscode.TreeItemCollapsibleState.Expanded);
this.iconPath = new vscode.ThemeIcon('comment');
if (hasSeen) {
this.description = '(Continued...)';
}
}
public withFilteredChildren(filter: (child: TreeChildItem) => boolean): ChatPromptItem {
const item = new ChatPromptItem(this.request, this.hasSeen);
item.children = this.children.filter(filter);
return item;
}
}
class ToolCallItem extends vscode.TreeItem {
public override id: string;
override readonly contextValue = 'toolcall';
constructor(
readonly info: ILoggedToolCall
) {
// todo@connor4312: we should have flags from the renderer whether it dropped any messages and indicate that here
super(info.name, vscode.TreeItemCollapsibleState.None);
this.id = `${info.id}_${info.time}`;
this.description = info.args === undefined ? '' : typeof info.args === 'string' ? info.args : JSON.stringify(info.args);
this.command = {
command: 'vscode.open',
title: '',
arguments: [vscode.Uri.parse(ChatRequestScheme.buildUri({ kind: 'request', id: info.id }))]
};
this.iconPath = new vscode.ThemeIcon('tools');
}
}
class ChatElementItem extends vscode.TreeItem {
public override readonly id?: string;
constructor(
readonly info: ILoggedElementInfo
) {
// todo@connor4312: we should have flags from the renderer whether it dropped any messages and indicate that here
super(`<${info.name}/>`, vscode.TreeItemCollapsibleState.None);
this.id = info.id;
this.description = `${info.tokens} tokens`;
this.command = { command: showHtmlCommand, title: '', arguments: [info.id] };
this.iconPath = new vscode.ThemeIcon('code');
}
}
class ChatRequestItem extends vscode.TreeItem {
public override id: string;
override readonly contextValue = 'request';
constructor(
readonly info: ILoggedRequestInfo
) {
super(info.entry.debugName, vscode.TreeItemCollapsibleState.None);
this.id = info.id;
if (info.entry.type === LoggedRequestKind.MarkdownContentRequest) {
this.iconPath = info.entry.icon === undefined ? undefined : new vscode.ThemeIcon(info.entry.icon.id);
const startTimeStr = new Date(info.entry.startTimeMs).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
this.description = startTimeStr;
} else {
const durationMs = info.entry.endTime.getTime() - info.entry.startTime.getTime();
const timeStr = `${durationMs.toLocaleString('en-US')}ms`;
const startTimeStr = info.entry.startTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const tokensStr = info.entry.type === LoggedRequestKind.ChatMLSuccess && info.entry.usage ? `${info.entry.usage.prompt_tokens.toLocaleString('en-US')}tks` : '';
const tokensStrPart = tokensStr ? `[${tokensStr}] ` : '';
this.description = `${tokensStrPart}[${timeStr}] [${startTimeStr}]`;
this.iconPath = info.entry.type === LoggedRequestKind.ChatMLSuccess ? undefined : new vscode.ThemeIcon('error');
this.tooltip = `${info.entry.type === LoggedRequestKind.ChatMLCancelation ? 'cancelled' : info.entry.result.type}
${info.entry.chatEndpoint.model}
${timeStr}
${startTimeStr}`;
if (tokensStr) {
this.tooltip += `\n\t${tokensStr}`;
}
}
this.command = {
command: 'vscode.open',
title: '',
arguments: [vscode.Uri.parse(ChatRequestScheme.buildUri({ kind: 'request', id: info.id }))]
};
this.iconPath ??= new vscode.ThemeIcon('copilot');
}
}
class LogTreeFilters extends Disposable {
private _elementsShown = true;
private _toolsShown = true;
private _nesRequestsShown = true;
private readonly _onDidChangeFilters = new vscode.EventEmitter<void>();
readonly onDidChangeFilters = this._onDidChangeFilters.event;
constructor(
@IVSCodeExtensionContext private readonly vscodeExtensionContext: IVSCodeExtensionContext,
) {
super();
this.setElementsShown(!vscodeExtensionContext.workspaceState.get(this.getStorageKey('elements')));
this.setToolsShown(!vscodeExtensionContext.workspaceState.get(this.getStorageKey('tools')));
this.setNesRequestsShown(!vscodeExtensionContext.workspaceState.get(this.getStorageKey('nesRequests')));
}
private getStorageKey(name: string): string {
return `github.copilot.chat.debug.${name}Hidden`;
}
setElementsShown(value: boolean) {
this._elementsShown = value;
this.setShown('elements', this._elementsShown);
}
setToolsShown(value: boolean) {
this._toolsShown = value;
this.setShown('tools', this._toolsShown);
}
setNesRequestsShown(value: boolean) {
this._nesRequestsShown = value;
this.setShown('nesRequests', this._nesRequestsShown);
}
itemIncluded(item: TreeItem): boolean {
if (item instanceof ChatPromptItem) {
return true; // Always show chat prompt items
} else if (item instanceof ChatElementItem) {
return this._elementsShown;
} else if (item instanceof ToolCallItem) {
return this._toolsShown;
} else if (item instanceof ChatRequestItem) {
// Check if this is a NES request
if (this.isNesRequest(item)) {
return this._nesRequestsShown;
}
}
return true;
}
private isNesRequest(item: ChatRequestItem): boolean {
const debugName = item.info.entry.debugName.toLowerCase();
return debugName.startsWith('nes |') || debugName === 'xtabprovider' || debugName.startsWith('nes.');
}
private setShown(name: string, value: boolean): void {
vscode.commands.executeCommand('setContext', `github.copilot.chat.debug.${name}Hidden`, !value);
this.vscodeExtensionContext.workspaceState.update(this.getStorageKey(name), !value);
this._onDidChangeFilters.fire();
}
}
class LogTreeFilterCommands extends Disposable {
constructor(filters: LogTreeFilters) {
super();
this._register(vscode.commands.registerCommand('github.copilot.chat.debug.showElements', () => filters.setElementsShown(true)));
this._register(vscode.commands.registerCommand('github.copilot.chat.debug.hideElements', () => filters.setElementsShown(false)));
this._register(vscode.commands.registerCommand('github.copilot.chat.debug.showTools', () => filters.setToolsShown(true)));
this._register(vscode.commands.registerCommand('github.copilot.chat.debug.hideTools', () => filters.setToolsShown(false)));
this._register(vscode.commands.registerCommand('github.copilot.chat.debug.showNesRequests', () => filters.setNesRequestsShown(true)));
this._register(vscode.commands.registerCommand('github.copilot.chat.debug.hideNesRequests', () => filters.setNesRequestsShown(false)));
}
}