From 55e1e5c074c5781258fe9514299500e01ced0204 Mon Sep 17 00:00:00 2001 From: Lars Baunwall Date: Mon, 29 Sep 2025 19:42:37 +0200 Subject: [PATCH] fix: Make sure tool call messages work --- src/messages.ts | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/messages.ts b/src/messages.ts index ac9d2c8..71a6a56 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -57,7 +57,11 @@ export const normalizeMessagesLM = ( ): (vscode.LanguageModelChatMessage | { role: 'user' | 'assistant'; content: string })[] => { const systemMessages = messages.filter((m) => m.role === 'system'); const systemMessage = systemMessages[systemMessages.length - 1]; - const conversationMessages = messages.filter((m) => m.role === 'user' || m.role === 'assistant').slice(-histWindow * 2); + + // Include user, assistant, and tool messages in conversation + const conversationMessages = messages.filter((m) => + m.role === 'user' || m.role === 'assistant' || m.role === 'tool' + ).slice(-histWindow * 3); // Increased window to account for tool messages const lmMsg = (vscode as unknown as { LanguageModelChatMessage?: typeof vscode.LanguageModelChatMessage }).LanguageModelChatMessage; const UserFactory = lmMsg?.User; @@ -74,9 +78,37 @@ export const normalizeMessagesLM = ( firstUserSeen = true; } result.push(UserFactory ? UserFactory(text) : { role: 'user', content: text }); - } else { - const text = toText(m.content); + } else if (m.role === 'assistant') { + // For assistant messages, we need to handle both content and tool calls + let text = ''; + + if (m.content) { + text = toText(m.content); + } + + // If the assistant message has tool calls, format them appropriately + if (m.tool_calls && m.tool_calls.length > 0) { + const toolCallsText = m.tool_calls.map(tc => + `[TOOL_CALL:${tc.id}] ${tc.function.name}(${tc.function.arguments})` + ).join('\n'); + + if (text) { + text += '\n' + toolCallsText; + } else { + text = toolCallsText; + } + } + + // Handle deprecated function_call format + if (!text && m.function_call) { + text = `[FUNCTION_CALL] ${m.function_call.name}(${m.function_call.arguments})`; + } + result.push(AssistantFactory ? AssistantFactory(text) : { role: 'assistant', content: text }); + } else if (m.role === 'tool') { + // Tool messages should be converted to user messages with tool result context + const toolResult = `[TOOL_RESULT:${m.tool_call_id}] ${toText(m.content)}`; + result.push(UserFactory ? UserFactory(toolResult) : { role: 'user', content: toolResult }); } }