fix: Make sure tool call messages work

This commit is contained in:
Lars Baunwall 2025-09-29 19:42:37 +02:00
parent 6b20e60f5b
commit 55e1e5c074
No known key found for this signature in database

View file

@ -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 });
}
}