| 1 | import { ApplyState, ToolCallState } from "core"; |
| 2 | import { IIdeMessenger } from "../context/IdeMessenger"; |
| 3 | import { store } from "../redux/store"; |
| 4 | |
| 5 | /** |
| 6 | * Extract model information from tool call state |
| 7 | */ |
| 8 | function extractModelInfo(toolCallState: ToolCallState): { |
| 9 | modelProvider: string; |
| 10 | modelTitle: string; |
| 11 | } { |
| 12 | // Get the conversation history to find the model info |
| 13 | const history = store.getState().session.history; |
| 14 | |
| 15 | // Find the assistant message that contains this tool call |
| 16 | const assistantMessage = history.find( |
| 17 | (item) => |
| 18 | item.message.role === "assistant" && |
| 19 | item.message.toolCalls?.some((tc) => tc.id === toolCallState.toolCallId), |
| 20 | ); |
| 21 | |
| 22 | if ( |
| 23 | assistantMessage?.message.role === "assistant" && |
| 24 | "model" in assistantMessage.message && |
| 25 | assistantMessage.message.model |
| 26 | ) { |
| 27 | // Extract provider from model string (e.g., "anthropic::claude-3-5-sonnet" -> "anthropic") |
| 28 | const modelParts = String(assistantMessage.message.model).split("::"); |
| 29 | return { |
| 30 | modelProvider: modelParts[0] || "unknown", |
| 31 | modelTitle: modelParts[1] || String(assistantMessage.message.model), |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | // Fallback to config if not found in message |
| 36 | const config = store.getState().config.config; |
| 37 | const chatModel = config?.selectedModelByRole?.chat; |
| 38 | |
| 39 | return { |
| 40 | modelProvider: chatModel?.provider || "unknown", |
| 41 | modelTitle: chatModel?.model || "unknown", |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Extract prompt and completion from tool call state |
| 47 | */ |
| 48 | function extractPromptAndCompletion(toolCallState: ToolCallState): { |
| 49 | prompt: string; |
| 50 | completion: string; |
| 51 | } { |
| 52 | const history = store.getState().session.history; |
| 53 | |
| 54 | // Find the assistant message with this tool call |
| 55 | const assistantMessageIndex = history.findIndex( |
| 56 | (item) => |
| 57 | item.message.role === "assistant" && |
| 58 | item.message.toolCalls?.some((tc) => tc.id === toolCallState.toolCallId), |
| 59 | ); |
| 60 | |
| 61 | if (assistantMessageIndex >= 0) { |
| 62 | const assistantMessage = history[assistantMessageIndex]; |
| 63 | |
| 64 | // Look for the most recent user message before this assistant message |
| 65 | let userMessage = null; |
| 66 | for (let i = assistantMessageIndex - 1; i >= 0; i--) { |
| 67 | if (history[i].message.role === "user") { |
| 68 | userMessage = history[i]; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const promptContent = userMessage |
| 74 | ? Array.isArray(userMessage.message.content) |
| 75 | ? userMessage.message.content |
| 76 | .map((part) => |
| 77 | typeof part === "string" |
| 78 | ? part |
| 79 | : part.type === "text" |
| 80 | ? part.text || "" |
| 81 | : "", |
| 82 | ) |
| 83 | .join("") |
| 84 | : userMessage.message.content || "" |
| 85 | : ""; |
| 86 | |
| 87 | const completionContent = Array.isArray(assistantMessage.message.content) |
| 88 | ? assistantMessage.message.content |
| 89 | .map((part) => |
| 90 | typeof part === "string" |
| 91 | ? part |
| 92 | : part.type === "text" |
| 93 | ? part.text || "" |
| 94 | : "", |
| 95 | ) |
| 96 | .join("") |
| 97 | : assistantMessage.message.content || ""; |
| 98 | |
| 99 | return { |
| 100 | prompt: promptContent, |
| 101 | completion: completionContent, |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | return { |
| 106 | prompt: "Unknown prompt", |
| 107 | completion: "Unknown completion", |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Extract code changes from apply state |
| 113 | */ |
| 114 | function extractCodeChanges(applyState: ApplyState): { |
| 115 | previousCode: string; |
| 116 | newCode: string; |
| 117 | previousCodeLines: number; |
| 118 | newCodeLines: number; |
| 119 | lineChange: number; |
| 120 | } { |
| 121 | // Use the original file content if provided, otherwise empty string |
| 122 | const previousCode = applyState.originalFileContent || ""; |
| 123 | const newCode = applyState.fileContent || ""; |
| 124 | |
| 125 | // Calculate line counts properly - empty string should be 0 lines, not 1 |
| 126 | const previousCodeLines = |
| 127 | previousCode === "" ? 0 : previousCode.split("\n").length; |
| 128 | const newCodeLines = newCode === "" ? 0 : newCode.split("\n").length; |
| 129 | const lineChange = newCodeLines - previousCodeLines; |
| 130 | |
| 131 | return { |
| 132 | previousCode, |
| 133 | newCode, |
| 134 | previousCodeLines, |
| 135 | newCodeLines, |
| 136 | lineChange, |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Assemble complete edit outcome data from tool call and apply state |
| 142 | */ |
| 143 | export function assembleEditOutcomeData( |
| 144 | toolCallState: ToolCallState, |
| 145 | applyState: ApplyState, |
| 146 | accepted: boolean, |
| 147 | ) { |
| 148 | const modelInfo = extractModelInfo(toolCallState); |
| 149 | const promptAndCompletion = extractPromptAndCompletion(toolCallState); |
| 150 | const codeChanges = extractCodeChanges(applyState); |
| 151 | |
| 152 | return { |
| 153 | streamId: applyState.streamId, |
| 154 | timestamp: new Date().toISOString(), |
| 155 | modelProvider: modelInfo.modelProvider, |
| 156 | modelTitle: modelInfo.modelTitle, |
| 157 | prompt: promptAndCompletion.prompt, |
| 158 | completion: promptAndCompletion.completion, |
| 159 | previousCode: codeChanges.previousCode, |
| 160 | newCode: codeChanges.newCode, |
| 161 | filepath: applyState.filepath || "", |
| 162 | previousCodeLines: codeChanges.previousCodeLines, |
| 163 | newCodeLines: codeChanges.newCodeLines, |
| 164 | lineChange: codeChanges.lineChange, |
| 165 | accepted, |
| 166 | }; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Log Agent Mode edit outcome to editOutcome.jsonl |
| 171 | */ |
| 172 | export async function logAgentModeEditOutcome( |
| 173 | toolCallState: ToolCallState, |
| 174 | applyState: ApplyState, |
| 175 | accepted: boolean, |
| 176 | ideMessenger: IIdeMessenger, |
| 177 | ): Promise<void> { |
| 178 | // Use the original file content stored in applyState, captured before edits were applied |
| 179 | |
| 180 | const editOutcomeData = assembleEditOutcomeData( |
| 181 | toolCallState, |
| 182 | applyState, |
| 183 | accepted, |
| 184 | ); |
| 185 | |
| 186 | ideMessenger.post("devdata/log", { |
| 187 | name: "editOutcome", |
| 188 | data: editOutcomeData, |
| 189 | }); |
| 190 | } |