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