GeezerVison is showing github.com — fix(ai): ignore late converse tool deltas (#45847) · jinhuang712/opencode@760cbf5
This repository was archived by the owner on Oct 29, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/ai/src/protocols/bedrock-converse.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -496,6 +496,7 @@ const mapUsage = (usage: BedrockUsageSchema | undefined, providerMetadataKey: st | 496 | 496 | interface ParserState { | 497 | 497 | readonly providerMetadataKey: string | 498 | 498 | readonly tools: ToolStream.State<number> | | 499 | + readonly finishedTools: ReadonlySet<number> | 499 | 500 | // Bedrock splits the finish into `messageStop` (carries `stopReason`) and | 500 | 501 | // `metadata` (carries usage). Hold the terminal event in state so `onHalt` | 501 | 502 | // can emit exactly one finish after both chunks have had a chance to arrive. | @@ -574,6 +575,7 @@ const step = (state: ParserState, event: BedrockEvent) => | 574 | 575 |
| 575 | 576 | if (event.contentBlockDelta?.delta?.toolUse) { | 576 | 577 | const index = event.contentBlockDelta.contentBlockIndex | | 578 | + if (state.finishedTools.has(index)) return [state, []] as const | 577 | 579 | const result = ToolStream.appendExisting( | 578 | 580 | ADAPTER, | 579 | 581 | state.tools, | @@ -612,6 +614,7 @@ const step = (state: ParserState, event: BedrockEvent) => | 612 | 614 | state.hasToolCalls, | 613 | 615 | lifecycle, | 614 | 616 | tools: result.tools, | | 617 | + finishedTools: resultEvents.length > 0 ? new Set([...state.finishedTools, index]) : state.finishedTools, | 615 | 618 | reasoningSignatures: Object.fromEntries( | 616 | 619 | Object.entries(state.reasoningSignatures).filter(([key]) => key !== String(index)), | 617 | 620 | ), | @@ -703,6 +706,7 @@ export const protocol = Protocol.make({ | 703 | 706 | initial: (request) => ({ | 704 | 707 | providerMetadataKey: request.model.route.providerMetadataKey ?? String(request.model.provider), | 705 | 708 | tools: ToolStream.empty<number>(), | | 709 | + finishedTools: new Set<number>(), | 706 | 710 | pendingFinish: undefined, | 707 | 711 | hasToolCalls: false, | 708 | 712 | lifecycle: Lifecycle.initial(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/ai/test/provider/bedrock-converse.test.ts +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -491,6 +491,59 @@ describe("Bedrock Converse route", () => { | 491 | 491 | }), | 492 | 492 | ) | 493 | 493 |
| | 494 | + it.effect("ignores late tool deltas after contentBlockStop", () => | | 495 | + Effect.gen(function* () { | | 496 | + const body = eventStreamBody( | | 497 | + [ | | 498 | + "contentBlockStart", | | 499 | + { | | 500 | + contentBlockIndex: 0, | | 501 | + start: { toolUse: { toolUseId: "tool_1", name: "lookup" } }, | | 502 | + }, | | 503 | + ], | | 504 | + ["contentBlockDelta", { contentBlockIndex: 0, delta: { toolUse: { input: '{"query":"weather"}' } } }], | | 505 | + ["contentBlockStop", { contentBlockIndex: 0 }], | | 506 | + ["contentBlockDelta", { contentBlockIndex: 0, delta: { toolUse: { input: '{"late":true}' } } }], | | 507 | + ["messageStop", { stopReason: "tool_use" }], | | 508 | + ) | | 509 | + const response = yield* LLMClient.generate(baseRequest).pipe(Effect.provide(fixedBytes(body))) | | 510 | + | | 511 | + expect(response.toolCalls).toEqual([ | | 512 | + { type: "tool-call", id: "tool_1", name: "lookup", input: { query: "weather" } }, | | 513 | + ]) | | 514 | + expect(response.events.filter((event) => event.type === "tool-input-delta")).toEqual([ | | 515 | + { | | 516 | + type: "tool-input-delta", | | 517 | + id: "tool_1", | | 518 | + name: "lookup", | | 519 | + text: '{"query":"weather"}', | | 520 | + input: { query: "weather" }, | | 521 | + }, | | 522 | + ]) | | 523 | + }), | | 524 | + ) | | 525 | + | | 526 | + it.effect("rejects tool deltas without contentBlockStart", () => | | 527 | + Effect.gen(function* () { | | 528 | + const error = yield* LLMClient.generate(baseRequest).pipe( | | 529 | + Effect.provide( | | 530 | + fixedBytes( | | 531 | + eventStreamBody( | | 532 | + ["contentBlockDelta", { contentBlockIndex: 0, delta: { toolUse: { input: "{}" } } }], | | 533 | + ["messageStop", { stopReason: "tool_use" }], | | 534 | + ), | | 535 | + ), | | 536 | + ), | | 537 | + Effect.flip, | | 538 | + ) | | 539 | + | | 540 | + expect(error).toMatchObject({ | | 541 | + reason: { _tag: "InvalidProviderOutput" }, | | 542 | + message: "Bedrock Converse tool delta is missing its tool call", | | 543 | + }) | | 544 | + }), | | 545 | + ) | | 546 | + | 494 | 547 | it.effect("recovers incomplete tool input at finalization", () => | 495 | 548 | Effect.gen(function* () { | 496 | 549 | const body = eventStreamBody( | |
|