diff --git a/src/api/transform/__tests__/vscode-lm-format.spec.ts b/src/api/transform/__tests__/vscode-lm-format.spec.ts index 1f53cc5751..e60860b549 100644 --- a/src/api/transform/__tests__/vscode-lm-format.spec.ts +++ b/src/api/transform/__tests__/vscode-lm-format.spec.ts @@ -143,9 +143,11 @@ describe("convertToVsCodeLmMessages", () => { expect(result).toHaveLength(1) expect(result[0].role).toBe("assistant") expect(result[0].content).toHaveLength(2) - const [toolCall, textContent] = result[0].content as [MockLanguageModelToolCallPart, MockLanguageModelTextPart] - expect(toolCall.type).toBe("tool_call") + // Text must come before tool calls so that tool calls are at the end, + // properly followed by user message with tool results + const [textContent, toolCall] = result[0].content as [MockLanguageModelTextPart, MockLanguageModelToolCallPart] expect(textContent.type).toBe("text") + expect(toolCall.type).toBe("tool_call") }) it("should handle image blocks with appropriate placeholders", () => { diff --git a/src/api/transform/vscode-lm-format.ts b/src/api/transform/vscode-lm-format.ts index 58b85f19a9..388197c2c2 100644 --- a/src/api/transform/vscode-lm-format.ts +++ b/src/api/transform/vscode-lm-format.ts @@ -114,9 +114,18 @@ export function convertToVsCodeLmMessages( { nonToolMessages: [], toolMessages: [] }, ) - // Process tool messages first then non-tool messages + // Process non-tool messages first, then tool messages + // Tool calls must come at the end so they are properly followed by user message with tool results const contentParts = [ - // Convert tool messages to ToolCallParts first + // Convert non-tool messages to TextParts first + ...nonToolMessages.map((part) => { + if (part.type === "image") { + return new vscode.LanguageModelTextPart("[Image generation not supported by VSCode LM API]") + } + return new vscode.LanguageModelTextPart(part.text) + }), + + // Convert tool messages to ToolCallParts after text ...toolMessages.map( (toolMessage) => new vscode.LanguageModelToolCallPart( @@ -125,14 +134,6 @@ export function convertToVsCodeLmMessages( asObjectSafe(toolMessage.input), ), ), - - // Convert non-tool messages to TextParts after tool messages - ...nonToolMessages.map((part) => { - if (part.type === "image") { - return new vscode.LanguageModelTextPart("[Image generation not supported by VSCode LM API]") - } - return new vscode.LanguageModelTextPart(part.text) - }), ] // Add the assistant message to the list of messages