fix(vscode-lm): order text parts before tool calls in assistant messages (#10573)

This commit is contained in:
Daniel 2026-01-09 10:01:26 -05:00 committed by GitHub
parent 2ff08b5e5c
commit ade10e275f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 12 deletions

View file

@ -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", () => {

View file

@ -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