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
2 changes: 1 addition & 1 deletion src/adapters/openai-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
const completion = completedBlocksEnabled
? observeCompletedToolBlock(call, rawToolCall.x_opencodex_completed, indexKey, idDelta, rawFunction !== undefined)
: "ordinary";
if (completion === "invalid") return yield* terminateWithError({ type: "error", message: "invalid negotiated tool completion sequence" });
if (completion === "invalid") return yield* terminateWithError({ type: "error", message: "invalid negotiated tool completion sequence", ...(pendingUsage !== undefined ? { usage: pendingUsage } : {}) });
if (completion === "completed") continue;
if (!call) {
call = {
Expand Down
13 changes: 13 additions & 0 deletions tests/adapters/openai/openai-chat-parallel-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,19 @@ describe("negotiated block-stop tool completion", () => {
expect(assembled(out)).toHaveLength(1);
expect(out.at(-1)?.type).toBe("error");
});
test("an invalid completion sequence still reports the usage already captured", async () => {
const out = await events([
chunkOf([call(0, "call_a")]),
chunkOf([marker(0, "call_a")]),
{ choices: [], usage: { prompt_tokens: 11, completion_tokens: 7, total_tokens: 18 } },
chunkOf([{ index: 0, function: { arguments: " " } }]),
chunkOf([], "tool_calls"),
]);
const error = out.at(-1);
expect(error?.type).toBe("error");
expect(error?.message).toContain("invalid negotiated tool completion sequence");
expect(error?.usage).toEqual({ inputTokens: 11, outputTokens: 7 });
});
test("root input calls stay buffered for existing freeform reconciliation", async () => {
const out = await events([chunkOf([call(0, "call_a", '{"input":"pwd"}')]), chunkOf([marker(0, "call_a")]), tail, chunkOf([], "tool_calls")]);
expect(out.findIndex(e => e.type === "text_delta")).toBeLessThan(out.findIndex(e => e.type === "tool_call_start"));
Expand Down
Loading