Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/log/node/chatLogExport.ts
13399 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 { LoggedInfo, LoggedInfoKind } from '../../../platform/requestLogger/common/requestLogger';
7
8
/**
9
* Type for a log entry in an exported chat log file.
10
* These types correspond to LoggedInfoKind from the request logger.
11
*/
12
type ExportedLogKind = 'element' | 'request' | 'toolCall' | 'error';
13
14
/**
15
* Metadata attached to request entries.
16
*/
17
interface ExportedLogMetadata {
18
model?: string;
19
duration?: number;
20
startTime?: string;
21
endTime?: string;
22
usage?: {
23
prompt_tokens?: number;
24
completion_tokens?: number;
25
total_tokens?: number;
26
};
27
}
28
29
/**
30
* Exported log entry from a chat log export file.
31
* This is the serialized form of LoggedInfo.
32
*/
33
interface ExportedLogEntry {
34
id: string;
35
kind: ExportedLogKind;
36
/** Name of the element or request */
37
name?: string;
38
/** Tool name for tool call entries */
39
tool?: string;
40
/** Request type (e.g., 'ChatMLSuccess', 'ChatMLFailure', 'MarkdownContentRequest') */
41
type?: string;
42
/** Token count for element entries */
43
tokens?: number;
44
/** Max tokens for element entries */
45
maxTokens?: number;
46
/** Arguments for tool call entries */
47
args?: Record<string, unknown>;
48
/** Response from tool or model */
49
response?: object;
50
/** Markdown content for MarkdownContentRequest entries - displayed directly to user */
51
content?: string;
52
/** Metadata for request entries */
53
metadata?: ExportedLogMetadata;
54
/** Raw request messages */
55
requestMessages?: {
56
messages?: unknown[];
57
};
58
/** Timestamp */
59
time?: string;
60
/** Thinking content for reasoning models */
61
thinking?: {
62
id?: string;
63
text?: string;
64
};
65
/** Error message for error entries */
66
error?: string;
67
/** Timestamp for when the entry occurred */
68
timestamp?: string;
69
}
70
71
/**
72
* Structure of an exported prompt in a chat log export file.
73
* Each prompt represents a user query and its associated log entries.
74
*/
75
export interface ExportedPrompt {
76
/** The user's prompt text */
77
prompt: string;
78
/** Unique identifier for the prompt */
79
promptId?: string;
80
/** Whether this is a continuation of a previous conversation */
81
hasSeen?: boolean;
82
/** Number of log entries in this prompt */
83
logCount: number;
84
/** The log entries for this prompt */
85
logs: ExportedLogEntry[];
86
}
87
88
/**
89
* Root structure of a chat log export file.
90
*/
91
export interface ChatLogExport {
92
/** ISO timestamp of when the export was created */
93
exportedAt: string;
94
/** Total number of prompts in the export */
95
totalPrompts: number;
96
/** Total number of log entries across all prompts */
97
totalLogEntries: number;
98
/** Array of exported prompts */
99
prompts: ExportedPrompt[];
100
/** MCP server definitions active during the session */
101
mcpServers?: object[];
102
}
103
104
/**
105
* Converts a single log entry to its JSON representation.
106
* Handles async toJSON methods for tool calls.
107
*/
108
async function entryToJson(entry: LoggedInfo): Promise<object> {
109
if (entry.kind === LoggedInfoKind.ToolCall) {
110
// Tool calls have async toJSON
111
return await (entry as { toJSON(): Promise<object> }).toJSON();
112
} else {
113
// Elements and requests have sync toJSON
114
return (entry as { toJSON(): object }).toJSON();
115
}
116
}
117
118
/**
119
* Creates an exported prompt from a collection of log entries.
120
* Use this when entries are already grouped by prompt (e.g., from tree view).
121
*
122
* @param label - The prompt label
123
* @param entries - The log entries for this prompt
124
* @param options - Additional options
125
* @returns The exported prompt structure
126
*/
127
export async function createExportedPrompt(
128
label: string,
129
entries: LoggedInfo[],
130
options?: { promptId?: string; hasSeen?: boolean }
131
): Promise<ExportedPrompt> {
132
const logs: ExportedLogEntry[] = [];
133
for (const entry of entries) {
134
try {
135
logs.push(await entryToJson(entry) as ExportedLogEntry);
136
} catch (error) {
137
logs.push({
138
id: entry.id,
139
kind: 'error',
140
error: error?.toString() || 'Unknown error',
141
timestamp: new Date().toISOString()
142
});
143
}
144
}
145
146
return {
147
prompt: label,
148
promptId: options?.promptId,
149
hasSeen: options?.hasSeen,
150
logCount: logs.length,
151
logs
152
};
153
}
154
155
/**
156
* Assembles a complete ChatLogExport from exported prompts.
157
*
158
* @param prompts - Array of exported prompts
159
* @param mcpServers - Optional MCP server definitions
160
* @returns The complete export structure
161
*/
162
export function assembleChatLogExport(
163
prompts: ExportedPrompt[],
164
mcpServers?: object[]
165
): ChatLogExport {
166
const totalLogEntries = prompts.reduce((sum, p) => sum + p.logCount, 0);
167
168
return {
169
exportedAt: new Date().toISOString(),
170
totalPrompts: prompts.length,
171
totalLogEntries,
172
prompts,
173
mcpServers
174
};
175
}
176
177
/**
178
* Serializes a chat log export to a JSON string.
179
*/
180
export function serializeChatLogExport(exportData: ChatLogExport): string {
181
return JSON.stringify(exportData, null, 2);
182
}
183
184