-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathsessionParsing.ts
More file actions
329 lines (299 loc) · 10.9 KB
/
sessionParsing.ts
File metadata and controls
329 lines (299 loc) · 10.9 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export interface SessionResponseLogChunk {
choices: Array<{
finish_reason?: 'tool_calls' | 'null' | (string & {});
delta: {
content?: string;
role: 'assistant' | (string & {});
tool_calls?: Array<{
function: {
arguments: string;
name: string;
};
id: string;
type: string;
index: number;
}>;
};
}>;
created: number;
id: string;
usage: {
completion_tokens: number;
prompt_tokens: number;
prompt_tokens_details: {
cached_tokens: number;
};
total_tokens: number;
};
model: string;
object: string;
}
export interface ParsedToolCall {
type: 'str_replace_editor' | 'think' | 'bash' | 'report_progress' | 'unknown';
name: string;
// args: any;
content: string;
command?: string; // For str_replace_editor
}
export interface ParsedChoice {
type: 'assistant_content' | 'tool_call' | 'pr_title';
content?: string;
toolCall?: ParsedToolCall;
finishReason?: string;
}
export interface ParsedToolCallDetails {
toolName: string;
invocationMessage: string;
pastTenseMessage?: string;
originMessage?: string;
toolSpecificData?: StrReplaceEditorToolData | BashToolData;
}
export interface StrReplaceEditorToolData {
command: 'view' | 'edit' | string;
filePath?: string;
fileLabel?: string;
parsedContent?: { content: string; fileA: string | undefined; fileB: string | undefined; };
viewRange?: { start: number, end: number }
}
export namespace StrReplaceEditorToolData {
export function is(value: any): value is StrReplaceEditorToolData {
return value && (typeof value.command === 'string');
}
}
export interface BashToolData {
commandLine: {
original: string;
};
language: 'bash';
}
/**
* Parse diff content and extract file information
*/
export function parseDiff(content: string): { content: string; fileA: string | undefined; fileB: string | undefined; } | undefined {
const lines = content.split(/\r?\n/g);
let fileA: string | undefined;
let fileB: string | undefined;
let startDiffLineIndex = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('diff --git')) {
const match = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
if (match) {
fileA = match[1];
fileB = match[2];
}
} else if (line.startsWith('@@ ')) {
startDiffLineIndex = i + 1;
break;
}
}
if (startDiffLineIndex < 0) {
return undefined;
}
return {
content: lines.slice(startDiffLineIndex).join('\n'),
fileA: typeof fileA === 'string' ? '/' + fileA : undefined,
fileB: typeof fileB === 'string' ? '/' + fileB : undefined
};
}
export function parseRange(view_range: unknown): { start: number, end: number } | undefined {
if (!view_range) {
return undefined;
}
if (!Array.isArray(view_range)) {
return undefined;
}
if (view_range.length !== 2) {
return undefined;
}
const start = view_range[0];
const end = view_range[1];
if (typeof start !== 'number' || typeof end !== 'number') {
return undefined;
}
return {
start,
end
};
}
/**
* Convert absolute file path to relative file label
* File paths are absolute and look like: `/home/runner/work/repo/repo/<path>`
*/
export function toFileLabel(file: string): string {
const parts = file.split('/');
return parts.slice(6).join('/');
}
/**
* Parse tool call arguments and return normalized tool details
*/
export function parseToolCallDetails(
toolCall: {
function: { name: string; arguments: string };
id: string;
type: string;
index: number;
},
content: string
): ParsedToolCallDetails {
// Parse arguments once with graceful fallback
let args: { command?: string, path?: string, prDescription?: string, commitMessage?: string, view_range?: unknown } = {};
try { args = toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : {}; } catch { /* ignore */ }
const name = toolCall.function.name;
// Small focused helpers to remove duplication while preserving behavior
const buildReadDetails = (filePath: string | undefined, parsedRange: { start: number, end: number } | undefined, opts?: { parsedContent?: { content: string; fileA: string | undefined; fileB: string | undefined; } }): ParsedToolCallDetails => {
const fileLabel = filePath && toFileLabel(filePath);
if (fileLabel === undefined || fileLabel === '') {
return { toolName: 'Read repository', invocationMessage: 'Read repository', pastTenseMessage: 'Read repository' };
}
const rangeSuffix = parsedRange ? `, lines ${parsedRange.start} to ${parsedRange.end}` : '';
// Default helper returns bracket variant (used for generic view). Plain variant handled separately for str_replace_editor non-diff.
return {
toolName: 'Read',
invocationMessage: `Read [](${fileLabel})${rangeSuffix}`,
pastTenseMessage: `Read [](${fileLabel})${rangeSuffix}`,
toolSpecificData: {
command: 'view',
filePath: filePath,
fileLabel: fileLabel,
parsedContent: opts?.parsedContent,
viewRange: parsedRange
}
};
};
const buildEditDetails = (filePath: string | undefined, command: string, parsedRange: { start: number, end: number } | undefined, opts?: { defaultName?: string }): ParsedToolCallDetails => {
const fileLabel = filePath && toFileLabel(filePath);
const rangeSuffix = parsedRange ? `, lines ${parsedRange.start} to ${parsedRange.end}` : '';
let invocationMessage: string;
let pastTenseMessage: string;
if (fileLabel) {
invocationMessage = `Edit [](${fileLabel})${rangeSuffix}`;
pastTenseMessage = `Edit [](${fileLabel})${rangeSuffix}`;
} else {
if (opts?.defaultName === 'Create') {
invocationMessage = pastTenseMessage = `Create File ${filePath}`;
} else {
invocationMessage = pastTenseMessage = (opts?.defaultName || 'Edit');
}
invocationMessage += rangeSuffix;
pastTenseMessage += rangeSuffix;
}
return {
toolName: opts?.defaultName || 'Edit',
invocationMessage,
pastTenseMessage,
toolSpecificData: fileLabel ? {
command: command || (opts?.defaultName === 'Create' ? 'create' : (command || 'edit')),
filePath: filePath,
fileLabel: fileLabel,
viewRange: parsedRange
} : undefined
};
};
const buildStrReplaceDetails = (filePath: string | undefined): ParsedToolCallDetails => {
const fileLabel = filePath && toFileLabel(filePath);
const message = fileLabel ? `Edit [](${fileLabel})` : `Edit ${filePath}`;
return {
toolName: 'Edit',
invocationMessage: message,
pastTenseMessage: message,
toolSpecificData: fileLabel ? { command: 'str_replace', filePath, fileLabel } : undefined
};
};
const buildCreateDetails = (filePath: string | undefined): ParsedToolCallDetails => {
const fileLabel = filePath && toFileLabel(filePath);
const message = fileLabel ? `Create [](${fileLabel})` : `Create File ${filePath}`;
return {
toolName: 'Create',
invocationMessage: message,
pastTenseMessage: message,
toolSpecificData: fileLabel ? { command: 'create', filePath, fileLabel } : undefined
};
};
const buildBashDetails = (bashArgs: typeof args, contentStr: string): ParsedToolCallDetails => {
const command = bashArgs.command ? `$ ${bashArgs.command}` : undefined;
const bashContent = [command, contentStr].filter(Boolean).join('\n');
const details: ParsedToolCallDetails = { toolName: 'Run Bash command', invocationMessage: bashContent || 'Run Bash command' };
if (bashArgs.command) { details.toolSpecificData = { commandLine: { original: bashArgs.command }, language: 'bash' }; }
return details;
};
switch (name) {
case 'str_replace_editor': {
if (args.command === 'view') {
const parsedContent = parseDiff(content);
const parsedRange = parseRange(args.view_range);
if (parsedContent) {
const file = parsedContent.fileA ?? parsedContent.fileB;
const fileLabel = file && toFileLabel(file);
if (fileLabel === '') {
return { toolName: 'Read repository', invocationMessage: 'Read repository', pastTenseMessage: 'Read repository' };
} else if (fileLabel === undefined) {
return { toolName: 'Read', invocationMessage: 'Read repository', pastTenseMessage: 'Read repository' };
} else {
const rangeSuffix = parsedRange ? `, lines ${parsedRange.start} to ${parsedRange.end}` : '';
return {
toolName: 'Read',
invocationMessage: `Read [](${fileLabel})${rangeSuffix}`,
pastTenseMessage: `Read [](${fileLabel})${rangeSuffix}`,
toolSpecificData: { command: 'view', filePath: file, fileLabel, parsedContent, viewRange: parsedRange }
};
}
}
// No diff parsed: use PLAIN (non-bracket) variant for str_replace_editor views
const plainRange = parseRange(args.view_range);
const fp = args.path; const fl = fp && toFileLabel(fp);
if (fl === undefined || fl === '') {
return { toolName: 'Read repository', invocationMessage: 'Read repository', pastTenseMessage: 'Read repository' };
}
const suffix = plainRange ? `, lines ${plainRange.start} to ${plainRange.end}` : '';
return {
toolName: 'Read',
invocationMessage: `Read ${fl}${suffix}`,
pastTenseMessage: `Read ${fl}${suffix}`,
toolSpecificData: { command: 'view', filePath: fp, fileLabel: fl, viewRange: plainRange }
};
}
return buildEditDetails(args.path, args.command || 'edit', parseRange(args.view_range));
}
case 'str_replace':
return buildStrReplaceDetails(args.path);
case 'create':
return buildCreateDetails(args.path);
case 'view':
return buildReadDetails(args.path, parseRange(args.view_range)); // generic view always bracket variant
case 'think': {
const thought = (args as unknown as { thought?: string }).thought || content || 'Thought';
return { toolName: 'think', invocationMessage: thought };
}
case 'report_progress': {
const details: ParsedToolCallDetails = { toolName: 'Progress Update', invocationMessage: `${args.prDescription}` || content || 'Progress Update' };
if (args.commitMessage) { details.originMessage = `Commit: ${args.commitMessage}`; }
return details;
}
case 'bash':
return buildBashDetails(args, content);
case 'read_bash':
return { toolName: 'read_bash', invocationMessage: 'Read logs from Bash session' };
case 'stop_bash':
return { toolName: 'stop_bash', invocationMessage: 'Stop Bash session' };
default:
return { toolName: name || 'unknown', invocationMessage: content || name || 'unknown' };
}
}
/**
* Parse raw session logs text into structured log chunks
*/
export function parseSessionLogs(rawText: string): SessionResponseLogChunk[] {
const parts = rawText
.split(/\r?\n/)
.filter(part => part.startsWith('data: '))
.map(part => {
const trimmed = part.slice('data: '.length).trim();
return JSON.parse(trimmed);
});
return parts as SessionResponseLogChunk[];
}