Elffuss BraidHub

kikocis/continue public

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