Skip to content

Commit e5d4657

Browse files
author
Bertan Ari
committed
fix(vscode-lm): keep an open invoke buffered when the salvage drain boundary falls inside it
1 parent 8a5594b commit e5d4657

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,27 @@ describe("VsCodeLmHandler", () => {
603603
).toEqual([])
604604
})
605605

606+
it("keeps an incomplete invoke buffered when the cap passes a newline inside it", async () => {
607+
// The drain boundary was the first newline after the last closed block, even inside a
608+
// later open block. The filler trips the cap before that block's closing tag arrives.
609+
const padding = "p".repeat(20 * 1024)
610+
const filler = "x".repeat(240 * 1024)
611+
const openBody = `sub\n${filler}`
612+
const chunks = await collect([
613+
`<function${"_calls"}><in${"voke"} name="calculator"><parameter name="operation">${padding}</parameter></in${"voke"}> ` +
614+
`<in${"voke"} name="calculator"><parameter name="operation">sub\n`,
615+
filler,
616+
`</parameter></in${"voke"}></function${"_calls"}>`,
617+
])
618+
619+
// Prose between the blocks is part of the decided span, so it precedes the first call.
620+
expect(chunks.filter((chunk) => chunk.type !== "usage")).toMatchObject([
621+
{ type: "text", text: " " },
622+
{ type: "tool_call", name: "calculator", arguments: JSON.stringify({ operation: padding }) },
623+
{ type: "tool_call", name: "calculator", arguments: JSON.stringify({ operation: openBody }) },
624+
])
625+
})
626+
606627
it("gives each recovered call in one response a distinct id", async () => {
607628
const chunks = await collect([
608629
`<function${"_calls"}><in${"voke"} name="calculator"><parameter name="operation">add</parameter></in${"voke"}>\n` +

‎src/api/providers/vscode-lm.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ function decidedSalvageEnd(text: string): number {
139139
return 0
140140
}
141141
const lineEnd = text.indexOf("\n", blockEnd)
142-
return lineEnd === -1 ? 0 : lineEnd + 1
142+
if (lineEnd === -1) {
143+
return 0
144+
}
145+
// A newline inside a later, still-open block must not drain that block's opener as text.
146+
const nextOpen = text.slice(blockEnd, lineEnd).search(/<(?:antml:)?invoke\s+name="/i)
147+
return nextOpen === -1 ? lineEnd + 1 : blockEnd + nextOpen
143148
}
144149

145150
/**

0 commit comments

Comments
 (0)