Skip to content

Commit 3a35583

Browse files
committed
fix(vscode-lm): order text parts before tool calls in assistant messages
The VS Code LM API requires that tool call parts within an assistant message come at the END, so they are properly followed by the next user message containing matching LanguageModelToolResultPart objects. Previously, tool calls were placed BEFORE text parts, causing the error: "Invalid request: Tool call part must be followed by a User message with a LanguageModelToolResultPart with a matching callId." Fixes ROO-427
1 parent 2ff08b5 commit 3a35583

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/api/transform/__tests__/vscode-lm-format.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ describe("convertToVsCodeLmMessages", () => {
143143
expect(result).toHaveLength(1)
144144
expect(result[0].role).toBe("assistant")
145145
expect(result[0].content).toHaveLength(2)
146-
const [toolCall, textContent] = result[0].content as [MockLanguageModelToolCallPart, MockLanguageModelTextPart]
147-
expect(toolCall.type).toBe("tool_call")
146+
// Text must come before tool calls so that tool calls are at the end,
147+
// properly followed by user message with tool results
148+
const [textContent, toolCall] = result[0].content as [MockLanguageModelTextPart, MockLanguageModelToolCallPart]
148149
expect(textContent.type).toBe("text")
150+
expect(toolCall.type).toBe("tool_call")
149151
})
150152

151153
it("should handle image blocks with appropriate placeholders", () => {

src/api/transform/vscode-lm-format.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,18 @@ export function convertToVsCodeLmMessages(
114114
{ nonToolMessages: [], toolMessages: [] },
115115
)
116116

117-
// Process tool messages first then non-tool messages
117+
// Process non-tool messages first, then tool messages
118+
// Tool calls must come at the end so they are properly followed by user message with tool results
118119
const contentParts = [
119-
// Convert tool messages to ToolCallParts first
120+
// Convert non-tool messages to TextParts first
121+
...nonToolMessages.map((part) => {
122+
if (part.type === "image") {
123+
return new vscode.LanguageModelTextPart("[Image generation not supported by VSCode LM API]")
124+
}
125+
return new vscode.LanguageModelTextPart(part.text)
126+
}),
127+
128+
// Convert tool messages to ToolCallParts after text
120129
...toolMessages.map(
121130
(toolMessage) =>
122131
new vscode.LanguageModelToolCallPart(
@@ -125,14 +134,6 @@ export function convertToVsCodeLmMessages(
125134
asObjectSafe(toolMessage.input),
126135
),
127136
),
128-
129-
// Convert non-tool messages to TextParts after tool messages
130-
...nonToolMessages.map((part) => {
131-
if (part.type === "image") {
132-
return new vscode.LanguageModelTextPart("[Image generation not supported by VSCode LM API]")
133-
}
134-
return new vscode.LanguageModelTextPart(part.text)
135-
}),
136137
]
137138

138139
// Add the assistant message to the list of messages

0 commit comments

Comments
 (0)