|
| 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 | +export interface SessionResponseLogChunk { |
| 7 | + choices: Array<{ |
| 8 | + finish_reason: string; |
| 9 | + delta: { |
| 10 | + content?: string; |
| 11 | + role: string; |
| 12 | + tool_calls?: Array<{ |
| 13 | + function: { |
| 14 | + arguments: string; |
| 15 | + name: string; |
| 16 | + }; |
| 17 | + id: string; |
| 18 | + type: string; |
| 19 | + index: number; |
| 20 | + }>; |
| 21 | + }; |
| 22 | + }>; |
| 23 | + created: number; |
| 24 | + id: string; |
| 25 | + usage: { |
| 26 | + completion_tokens: number; |
| 27 | + prompt_tokens: number; |
| 28 | + prompt_tokens_details: { |
| 29 | + cached_tokens: number; |
| 30 | + }; |
| 31 | + total_tokens: number; |
| 32 | + }; |
| 33 | + model: string; |
| 34 | + object: string; |
| 35 | +} |
| 36 | + |
| 37 | +export interface ParsedToolCall { |
| 38 | + type: 'str_replace_editor' | 'think' | 'bash' | 'report_progress' | 'unknown'; |
| 39 | + name: string; |
| 40 | + args: any; |
| 41 | + content: string; |
| 42 | + command?: string; // For str_replace_editor |
| 43 | +} |
| 44 | + |
| 45 | +export interface ParsedChoice { |
| 46 | + type: 'assistant_content' | 'tool_call' | 'pr_title'; |
| 47 | + content?: string; |
| 48 | + toolCall?: ParsedToolCall; |
| 49 | + finishReason?: string; |
| 50 | +} |
| 51 | + |
| 52 | +export interface ParsedToolCallDetails { |
| 53 | + toolName: string; |
| 54 | + invocationMessage: string; |
| 55 | + pastTenseMessage?: string; |
| 56 | + originMessage?: string; |
| 57 | + toolSpecificData?: any; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Parse tool call arguments and return normalized tool details |
| 62 | + */ |
| 63 | +export function parseToolCallDetails( |
| 64 | + toolCall: { |
| 65 | + function: { name: string; arguments: string }; |
| 66 | + id: string; |
| 67 | + type: string; |
| 68 | + index: number; |
| 69 | + }, |
| 70 | + content: string |
| 71 | +): ParsedToolCallDetails { |
| 72 | + let args: any = {}; |
| 73 | + try { |
| 74 | + args = toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : {}; |
| 75 | + } catch { |
| 76 | + // fallback to empty args |
| 77 | + } |
| 78 | + |
| 79 | + const name = toolCall.function.name; |
| 80 | + |
| 81 | + if (name === 'str_replace_editor') { |
| 82 | + if (args.command === 'view') { |
| 83 | + return { |
| 84 | + toolName: args.path ? `View ${args.path}` : 'View repository', |
| 85 | + invocationMessage: `View ${args.path}`, |
| 86 | + pastTenseMessage: `View ${args.path}` |
| 87 | + }; |
| 88 | + } else { |
| 89 | + return { |
| 90 | + toolName: 'Edit', |
| 91 | + invocationMessage: `Edit: ${args.path}`, |
| 92 | + pastTenseMessage: `Edit: ${args.path}` |
| 93 | + }; |
| 94 | + } |
| 95 | + } else if (name === 'think') { |
| 96 | + return { |
| 97 | + toolName: 'Thought', |
| 98 | + invocationMessage: content |
| 99 | + }; |
| 100 | + } else if (name === 'report_progress') { |
| 101 | + const details: ParsedToolCallDetails = { |
| 102 | + toolName: 'Progress Update', |
| 103 | + invocationMessage: args.prDescription || content |
| 104 | + }; |
| 105 | + if (args.commitMessage) { |
| 106 | + details.originMessage = `Commit: ${args.commitMessage}`; |
| 107 | + } |
| 108 | + return details; |
| 109 | + } else if (name === 'bash') { |
| 110 | + const command = args.command ? `$ ${args.command}` : undefined; |
| 111 | + const bashContent = [command, content].filter(Boolean).join('\n'); |
| 112 | + const details: ParsedToolCallDetails = { |
| 113 | + toolName: 'Run Bash command', |
| 114 | + invocationMessage: bashContent |
| 115 | + }; |
| 116 | + |
| 117 | + // Use the terminal-specific data for bash commands |
| 118 | + if (args.command) { |
| 119 | + details.toolSpecificData = { |
| 120 | + commandLine: { |
| 121 | + original: args.command, |
| 122 | + }, |
| 123 | + language: 'bash' |
| 124 | + }; |
| 125 | + } |
| 126 | + return details; |
| 127 | + } else { |
| 128 | + // Unknown tool type |
| 129 | + return { |
| 130 | + toolName: name || 'unknown', |
| 131 | + invocationMessage: content |
| 132 | + }; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Parse raw session logs text into structured log chunks |
| 138 | + */ |
| 139 | +export function parseSessionLogs(rawText: string): SessionResponseLogChunk[] { |
| 140 | + const parts = rawText |
| 141 | + .split(/\r?\n/) |
| 142 | + .filter(part => part.startsWith('data: ')) |
| 143 | + .map(part => part.slice('data: '.length).trim()) |
| 144 | + .map(part => JSON.parse(part)); |
| 145 | + |
| 146 | + return parts as SessionResponseLogChunk[]; |
| 147 | +} |
0 commit comments