Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/api/transform/__tests__/vscode-lm-format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
21 changes: 11 additions & 10 deletions src/api/transform/vscode-lm-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
Loading