mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-18 07:58:27 +00:00
fix(translator): pair id-less tool results and keep orphan Responses tool outputs (#16539)
Tool results that arrive without an id pair to the oldest unanswered call. Orphan Responses tool outputs stay in the prompt as labelled user context. The existing thinking-binding argument is kept, and the pending tool-use id list is passed beside it.
Boarded on release/v3.8.52 at efea1806d7. Focused tests: 187 passed in the combined run. Two vscode catalog tests returned 503 under that parallel load and passed on an isolated rerun. File-size kept only the inherited executeTargetAttempt.ts overage (1288 > 1284). This pull request does not touch that file.
⚠️ base-red inherited: #15306
This commit is contained in:
committed by
GitHub
parent
6a36e69718
commit
ab1abecf26
1
changelog.d/fixes/16539-tool-result-pairing.md
Normal file
1
changelog.d/fixes/16539-tool-result-pairing.md
Normal file
@@ -0,0 +1 @@
|
||||
- **fix(translator):** id-less tool results are paired with their pending call instead of dropped (Chat → Claude and Responses → Chat), orphan outputs from stateless Responses continuations are kept as labelled context, and an empty `tool_calls: []` no longer bypasses OpenAI-format normalization ([#16539](https://github.com/diegosouzapw/OmniRoute/pull/16539)) — thanks @ntdatt812, @claytontavaresdan and @vianhanif
|
||||
@@ -61,6 +61,10 @@ export interface FilterToOpenAIFormatOptions {
|
||||
preserveReasoningContent?: boolean;
|
||||
}
|
||||
|
||||
function hasToolCalls(msg: { tool_calls?: unknown }): boolean {
|
||||
return Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0;
|
||||
}
|
||||
|
||||
export function filterToOpenAIFormat(body, opts: FilterToOpenAIFormatOptions = {}) {
|
||||
// #2069 — when the routed provider honors OpenAI-format cache_control
|
||||
// breakpoints (DashScope/alibaba, Xiaomi MiMo, etc.) and preservation was
|
||||
@@ -87,11 +91,19 @@ export function filterToOpenAIFormat(body, opts: FilterToOpenAIFormatOptions = {
|
||||
// Keep tool messages as-is (OpenAI format)
|
||||
if (msg.role === "tool") return msg;
|
||||
|
||||
// An empty `tool_calls: []` carries no tool calls: drop the key (several providers
|
||||
// 400 on an empty array) and let the turn go through the normal content
|
||||
// normalization below instead of being passed through untouched.
|
||||
if (msg.role === "assistant" && Array.isArray(msg.tool_calls) && !hasToolCalls(msg)) {
|
||||
const { tool_calls, ...withoutToolCalls } = msg;
|
||||
msg = withoutToolCalls;
|
||||
}
|
||||
|
||||
// Keep assistant messages with tool_calls, but strip reasoning_content —
|
||||
// reasoning blobs inflate context on every subsequent agentic turn (O(n^2)).
|
||||
// Exception: reasoning-replay providers must keep client-provided
|
||||
// reasoning_content (they 400 without it), so preserve it when requested.
|
||||
if (msg.role === "assistant" && msg.tool_calls) {
|
||||
if (msg.role === "assistant" && hasToolCalls(msg)) {
|
||||
if (!preserveReasoningContent && msg.reasoning_content !== undefined) {
|
||||
const { reasoning_content, ...cleanMsg } = msg;
|
||||
return cleanMsg;
|
||||
@@ -210,7 +222,7 @@ export function filterToOpenAIFormat(body, opts: FilterToOpenAIFormatOptions = {
|
||||
// Always keep tool messages
|
||||
if (msg.role === "tool") return true;
|
||||
// Always keep assistant messages with tool_calls
|
||||
if (msg.role === "assistant" && msg.tool_calls) return true;
|
||||
if (msg.role === "assistant" && hasToolCalls(msg)) return true;
|
||||
// Moonshot partial assistant messages are output prefixes, and an empty
|
||||
// prefix is valid when `name` supplies the constrained value.
|
||||
if (msg.role === "assistant" && msg.partial === true) return true;
|
||||
|
||||
@@ -265,6 +265,17 @@ export function openaiResponsesToOpenAIRequest(
|
||||
let currentAssistantMsg: JsonRecord | null = null;
|
||||
let pendingToolResults: JsonRecord[] = [];
|
||||
let pendingReasoningContent = "";
|
||||
// Call ids still waiting for their output, oldest first. Some clients send a
|
||||
// `*_output` item without `call_id`; pair it with the oldest pending call so the real
|
||||
// tool output reaches the model instead of an empty synthetic result.
|
||||
const pendingCallIds: string[] = [];
|
||||
const claimToolCallId = (rawCallId: unknown): string => {
|
||||
const callId = toString(rawCallId);
|
||||
if (!callId.trim()) return pendingCallIds.shift() ?? callId;
|
||||
const index = pendingCallIds.indexOf(callId);
|
||||
if (index !== -1) pendingCallIds.splice(index, 1);
|
||||
return callId;
|
||||
};
|
||||
|
||||
// Upstream providers reject messages:[] with "400: at least one message is required".
|
||||
// When the client sends input:[] (empty), inject a placeholder user message — mirrors
|
||||
@@ -309,7 +320,7 @@ export function openaiResponsesToOpenAIRequest(
|
||||
if (toString(item.role) === "tool") {
|
||||
messages.push({
|
||||
role: "tool",
|
||||
tool_call_id: toString(item.tool_call_id),
|
||||
tool_call_id: claimToolCallId(item.tool_call_id),
|
||||
content: toolOutputContentToString(item.content),
|
||||
});
|
||||
const roleToolImages = toolOutputImagesToChatParts(item.content);
|
||||
@@ -360,6 +371,7 @@ export function openaiResponsesToOpenAIRequest(
|
||||
if (currentAssistantMsg) {
|
||||
messages.push(currentAssistantMsg);
|
||||
}
|
||||
for (const toolCall of roleBasedToolCalls) pendingCallIds.push(toolCall.id);
|
||||
currentAssistantMsg = {
|
||||
role,
|
||||
content,
|
||||
@@ -432,6 +444,7 @@ export function openaiResponsesToOpenAIRequest(
|
||||
},
|
||||
});
|
||||
currentAssistantMsg.tool_calls = toolCalls;
|
||||
pendingCallIds.push(toString(item.call_id));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -453,7 +466,7 @@ export function openaiResponsesToOpenAIRequest(
|
||||
// Add tool result immediately
|
||||
messages.push({
|
||||
role: "tool",
|
||||
tool_call_id: toString(item.call_id),
|
||||
tool_call_id: claimToolCallId(item.call_id),
|
||||
content: toolOutputContentToString(item.output),
|
||||
});
|
||||
// #14111: Chat Completions `tool` content is text-only, so a following
|
||||
@@ -498,6 +511,7 @@ export function openaiResponsesToOpenAIRequest(
|
||||
},
|
||||
});
|
||||
currentAssistantMsg.tool_calls = toolCalls;
|
||||
if (toString(item.call_id).trim()) pendingCallIds.push(toString(item.call_id));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -526,7 +540,7 @@ export function openaiResponsesToOpenAIRequest(
|
||||
}
|
||||
messages.push({
|
||||
role: "tool",
|
||||
tool_call_id: toString(item.call_id),
|
||||
tool_call_id: claimToolCallId(item.call_id),
|
||||
content: toolContent,
|
||||
});
|
||||
const customToolImages = toolOutputImagesToChatParts(item.output);
|
||||
@@ -783,15 +797,21 @@ export function openaiResponsesToOpenAIRequest(
|
||||
}
|
||||
}
|
||||
}
|
||||
result.messages = messages.filter((m) => {
|
||||
result.messages = messages.flatMap((m) => {
|
||||
const rec = toRecord(m);
|
||||
// #2893: drop ANY tool result whose tool_call_id has no matching tool_call —
|
||||
// including empty/missing ids (the previous `&& rec.tool_call_id` guard let
|
||||
// empty-id orphans slip through and triggered an upstream 400).
|
||||
if (rec.role === "tool") {
|
||||
return allToolCallIds.has(String(rec.tool_call_id ?? ""));
|
||||
}
|
||||
return true;
|
||||
if (rec.role !== "tool") return [m];
|
||||
const toolCallId = String(rec.tool_call_id ?? "");
|
||||
if (allToolCallIds.has(toolCallId)) return [m];
|
||||
// #2893: a tool result without a matching tool_call is never sent as `role:"tool"`
|
||||
// (strict upstreams 400). An empty/missing id can never be attributed — drop it.
|
||||
if (!toolCallId.trim()) return [];
|
||||
// Stateless continuations (`previous_response_id` + only `*_output` items) carry
|
||||
// the output of a call that lives in the previous server-side response. Keep it as
|
||||
// labelled user context (same label the Claude reconciliation uses) instead of
|
||||
// dropping it silently, which made the model re-run the tool.
|
||||
return [
|
||||
{ role: "user", content: `[Unpaired tool result ${toolCallId}]\n${toString(rec.content)}` },
|
||||
];
|
||||
});
|
||||
|
||||
// Translate tool_choice object format: Responses {type,name} → Chat {type,function:{name}}
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
} from "../../../src/shared/constants/modelSpecs.ts";
|
||||
import { fitThinkingToMaxTokens } from "./openai-to-claude/thinkingBudget.ts";
|
||||
import { enforceToolResultAdjacency } from "./openai-to-claude/toolResultAdjacency.ts";
|
||||
import { sanitizeToolResultId } from "./openai-to-claude/sanitizeToolResultId.ts";
|
||||
import { resolveToolResultId } from "./openai-to-claude/sanitizeToolResultId.ts";
|
||||
import {
|
||||
openAiImagePartToClaudeBlock,
|
||||
normalizeToolResultImages,
|
||||
@@ -341,6 +341,10 @@ export function openaiToClaudeRequest(model, body, stream, credentials = null) {
|
||||
}
|
||||
};
|
||||
|
||||
// tool_use ids of the latest assistant turn still waiting for a result; an id-less
|
||||
// tool result is paired with the oldest of them instead of being dropped.
|
||||
let pendingToolUseIds: string[] = [];
|
||||
|
||||
for (const msg of nonSystemMessages) {
|
||||
const newRole = msg.role === "user" || msg.role === "tool" ? "user" : "assistant";
|
||||
const blocks = getContentBlocksFromMessage(
|
||||
@@ -353,8 +357,12 @@ export function openaiToClaudeRequest(model, body, stream, credentials = null) {
|
||||
provider: routedProvider,
|
||||
model,
|
||||
disabled: credentials?._videoTranscriptSensitive === true,
|
||||
}
|
||||
},
|
||||
pendingToolUseIds
|
||||
);
|
||||
if (newRole === "assistant") {
|
||||
pendingToolUseIds = blocks.filter((b) => b.type === "tool_use").map((b) => String(b.id));
|
||||
}
|
||||
const hasToolUse = blocks.some((b) => b.type === "tool_use");
|
||||
const hasToolResult = blocks.some((b) => b.type === "tool_result");
|
||||
|
||||
@@ -584,12 +592,13 @@ function getContentBlocksFromMessage(
|
||||
disableToolPrefix = false,
|
||||
thinkingEnabledForRequest = false,
|
||||
isKimiCoding = false,
|
||||
thinkingBinding: { provider?: unknown; model?: unknown; disabled?: boolean } = {}
|
||||
thinkingBinding: { provider?: unknown; model?: unknown; disabled?: boolean } = {},
|
||||
pendingToolUseIds: string[] = []
|
||||
) {
|
||||
const blocks = [];
|
||||
|
||||
if (msg.role === "tool") {
|
||||
const sanitizedToolUseId = sanitizeToolResultId(msg.tool_call_id); // #7705
|
||||
const sanitizedToolUseId = resolveToolResultId(msg.tool_call_id, pendingToolUseIds); // #7705
|
||||
if (!sanitizedToolUseId) return blocks;
|
||||
// T02: Strip empty text blocks from nested tool_result content to avoid Anthropic 400
|
||||
// #9692: rewrite OpenAI image_url parts to Claude image blocks (same as user turns)
|
||||
@@ -615,8 +624,10 @@ function getContentBlocksFromMessage(
|
||||
if (part.type === "text" && part.text) {
|
||||
blocks.push({ type: "text", text: part.text });
|
||||
} else if (part.type === "tool_result") {
|
||||
// Skip tool_result with no tool_use_id (would be useless and may cause errors)
|
||||
if (!part.tool_use_id) continue;
|
||||
// Pair an id-less tool_result with the oldest pending tool_use; skip it only
|
||||
// when nothing is pending (it would be useless and may cause errors).
|
||||
const toolUseId = resolveToolResultId(part.tool_use_id, pendingToolUseIds);
|
||||
if (!toolUseId) continue;
|
||||
// T02: strip empty text blocks from nested content before passing to Anthropic
|
||||
// #9692: convert OpenAI image_url nested in tool_result the same way
|
||||
const resultContent = normalizeToolResultImages(
|
||||
@@ -624,7 +635,7 @@ function getContentBlocksFromMessage(
|
||||
);
|
||||
blocks.push({
|
||||
type: "tool_result",
|
||||
tool_use_id: sanitizeToolId(part.tool_use_id), // #7705
|
||||
tool_use_id: toolUseId, // #7705
|
||||
content: resultContent,
|
||||
// Always emit a boolean: Zed's strict Anthropic parser rejects
|
||||
// tool_result blocks with a missing `is_error` field.
|
||||
|
||||
@@ -11,3 +11,18 @@ export function sanitizeToolResultId(rawId: unknown): string | null {
|
||||
// and throw. Coerce instead so a numeric id (some clients send one) sanitizes normally.
|
||||
return sanitizeToolId(typeof rawId === "string" ? rawId : String(rawId));
|
||||
}
|
||||
|
||||
// Resolve the tool_use_id for a tool result against the preceding assistant turn's
|
||||
// still-unanswered tool_use ids (already sanitized, in emission order; mutated in place).
|
||||
// A present id is sanitized as above and marked answered. A missing id is paired with the
|
||||
// oldest unanswered call (FIFO) so the real tool output is kept instead of dropped; null
|
||||
// only when nothing is pending, which keeps the "skip orphan tool_result" guard.
|
||||
export function resolveToolResultId(rawId: unknown, pendingToolUseIds: string[]): string | null {
|
||||
const sanitized = sanitizeToolResultId(rawId);
|
||||
if (sanitized) {
|
||||
const index = pendingToolUseIds.indexOf(sanitized);
|
||||
if (index !== -1) pendingToolUseIds.splice(index, 1);
|
||||
return sanitized;
|
||||
}
|
||||
return pendingToolUseIds.shift() ?? null;
|
||||
}
|
||||
|
||||
83
tests/unit/openai-helper-empty-tool-calls.test.ts
Normal file
83
tests/unit/openai-helper-empty-tool-calls.test.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
// An assistant turn carrying `tool_calls: []` has NO tool calls. Treating the empty
|
||||
// array as truthy skipped the content normalization (Claude `thinking` blocks leaked
|
||||
// raw to OpenAI-compatible upstreams) and kept blank assistant turns alive.
|
||||
|
||||
const { filterToOpenAIFormat } = await import("../../open-sse/translator/helpers/openaiHelper.ts");
|
||||
|
||||
type Msg = Record<string, unknown>;
|
||||
|
||||
test("filterToOpenAIFormat: tool_calls:[] does not bypass content normalization", () => {
|
||||
const body = {
|
||||
messages: [
|
||||
{ role: "user", content: "hi" },
|
||||
{
|
||||
role: "assistant",
|
||||
tool_calls: [],
|
||||
content: [
|
||||
{ type: "thinking", thinking: "x" },
|
||||
{ type: "text", text: "y" },
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
const result = filterToOpenAIFormat(body);
|
||||
const assistant = (result.messages as Msg[])[1];
|
||||
const content = assistant.content as Msg[];
|
||||
assert.ok(
|
||||
content.every((block) => block.type !== "thinking"),
|
||||
"thinking block must not leak to an OpenAI-format body"
|
||||
);
|
||||
assert.deepEqual(
|
||||
content.map((block) => block.text),
|
||||
["y"]
|
||||
);
|
||||
assert.equal(assistant.reasoning_content, "x");
|
||||
assert.equal("tool_calls" in assistant, false, "empty tool_calls key is removed");
|
||||
});
|
||||
|
||||
test("filterToOpenAIFormat: blank assistant turn with tool_calls:[] is dropped", () => {
|
||||
const body = {
|
||||
messages: [
|
||||
{ role: "user", content: "hi" },
|
||||
{ role: "assistant", tool_calls: [], content: " " },
|
||||
{ role: "user", content: "again" },
|
||||
],
|
||||
};
|
||||
const result = filterToOpenAIFormat(body);
|
||||
assert.deepEqual(
|
||||
(result.messages as Msg[]).map((m) => m.role),
|
||||
["user", "user"]
|
||||
);
|
||||
});
|
||||
|
||||
test("filterToOpenAIFormat: empty tool_calls key removed from a text assistant turn", () => {
|
||||
const body = {
|
||||
messages: [
|
||||
{ role: "user", content: "hi" },
|
||||
{ role: "assistant", tool_calls: [], content: "answer" },
|
||||
],
|
||||
};
|
||||
const result = filterToOpenAIFormat(body);
|
||||
const assistant = (result.messages as Msg[])[1];
|
||||
assert.equal(assistant.content, "answer");
|
||||
assert.equal("tool_calls" in assistant, false);
|
||||
});
|
||||
|
||||
test("filterToOpenAIFormat: a real tool_calls turn is kept unchanged", () => {
|
||||
const toolCalls = [{ id: "call_1", type: "function", function: { name: "f", arguments: "{}" } }];
|
||||
const body = {
|
||||
messages: [
|
||||
{ role: "user", content: "hi" },
|
||||
{ role: "assistant", content: "", tool_calls: toolCalls },
|
||||
{ role: "tool", tool_call_id: "call_1", content: "ok" },
|
||||
],
|
||||
};
|
||||
const result = filterToOpenAIFormat(body);
|
||||
const messages = result.messages as Msg[];
|
||||
assert.equal(messages.length, 3);
|
||||
assert.deepEqual(messages[1].tool_calls, toolCalls);
|
||||
assert.equal(messages[1].content, "");
|
||||
});
|
||||
134
tests/unit/openai-to-claude-idless-tool-result.test.ts
Normal file
134
tests/unit/openai-to-claude-idless-tool-result.test.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
// Some clients send a tool result without its id (`role:"tool"` without
|
||||
// `tool_call_id`, or a Claude-shaped `tool_result` part without `tool_use_id`).
|
||||
// Dropping it loses the real tool output and leaves the call unanswered; pair it
|
||||
// with the oldest still-unanswered tool_use of the preceding assistant turn instead.
|
||||
|
||||
const { openaiToClaudeRequest } =
|
||||
await import("../../open-sse/translator/request/openai-to-claude.ts");
|
||||
|
||||
type Block = Record<string, unknown>;
|
||||
|
||||
function toolResults(result: { messages: Array<{ content: unknown }> }): Block[] {
|
||||
const blocks: Block[] = [];
|
||||
for (const msg of result.messages) {
|
||||
if (!Array.isArray(msg.content)) continue;
|
||||
for (const block of msg.content as Block[]) {
|
||||
if (block.type === "tool_result") blocks.push(block);
|
||||
}
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
function assistantWithCalls(ids: string[]) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: null,
|
||||
tool_calls: ids.map((id) => ({
|
||||
id,
|
||||
type: "function",
|
||||
function: { name: `fn_${id}`, arguments: "{}" },
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
test("openaiToClaudeRequest: id-less tool messages pair with pending calls in order", () => {
|
||||
const body = {
|
||||
_disableToolPrefix: true,
|
||||
messages: [
|
||||
{ role: "user", content: "run both" },
|
||||
assistantWithCalls(["call_a", "call_b"]),
|
||||
{ role: "tool", content: "result A" },
|
||||
{ role: "tool", content: "result B" },
|
||||
],
|
||||
};
|
||||
const results = toolResults(openaiToClaudeRequest("claude-sonnet-4", body, false));
|
||||
assert.deepEqual(
|
||||
results.map((block) => [block.tool_use_id, block.content]),
|
||||
[
|
||||
["call_a", "result A"],
|
||||
["call_b", "result B"],
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
test("openaiToClaudeRequest: id-less result takes the call not yet answered", () => {
|
||||
const body = {
|
||||
_disableToolPrefix: true,
|
||||
messages: [
|
||||
{ role: "user", content: "run both" },
|
||||
assistantWithCalls(["call_a", "call_b"]),
|
||||
{ role: "tool", tool_call_id: "call_b", content: "result B" },
|
||||
{ role: "tool", content: "result A" },
|
||||
],
|
||||
};
|
||||
const results = toolResults(openaiToClaudeRequest("claude-sonnet-4", body, false));
|
||||
// Adjacency enforcement re-orders results to the tool_use order.
|
||||
assert.deepEqual(
|
||||
results.map((block) => [block.tool_use_id, block.content]),
|
||||
[
|
||||
["call_a", "result A"],
|
||||
["call_b", "result B"],
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
test("openaiToClaudeRequest: Claude-shaped tool_result part without tool_use_id is paired", () => {
|
||||
const body = {
|
||||
_disableToolPrefix: true,
|
||||
messages: [
|
||||
{ role: "user", content: "run it" },
|
||||
assistantWithCalls(["call_x"]),
|
||||
{ role: "user", content: [{ type: "tool_result", content: "x output" }] },
|
||||
],
|
||||
};
|
||||
const results = toolResults(openaiToClaudeRequest("claude-sonnet-4", body, false));
|
||||
assert.equal(results.length, 1);
|
||||
assert.equal(results[0].tool_use_id, "call_x");
|
||||
assert.equal(results[0].content, "x output");
|
||||
});
|
||||
|
||||
test("openaiToClaudeRequest: id-less result with nothing pending is still dropped", () => {
|
||||
const body = {
|
||||
_disableToolPrefix: true,
|
||||
messages: [
|
||||
{ role: "user", content: "run it" },
|
||||
assistantWithCalls(["call_a"]),
|
||||
{ role: "tool", tool_call_id: "call_a", content: "result A" },
|
||||
{ role: "tool", content: "stray" },
|
||||
],
|
||||
};
|
||||
const results = toolResults(openaiToClaudeRequest("claude-sonnet-4", body, false));
|
||||
assert.deepEqual(
|
||||
results.map((block) => block.tool_use_id),
|
||||
["call_a"]
|
||||
);
|
||||
});
|
||||
|
||||
test("openaiToClaudeRequest: pairing uses the sanitized tool_use id (#7705 guard)", () => {
|
||||
const body = {
|
||||
_disableToolPrefix: true,
|
||||
messages: [
|
||||
{ role: "user", content: "run both" },
|
||||
assistantWithCalls(["call.with:bad/chars", "call.two:x"]),
|
||||
{ role: "tool", content: "paired" },
|
||||
{ role: "tool", tool_call_id: "call.two:x", content: "explicit" },
|
||||
],
|
||||
};
|
||||
const result = openaiToClaudeRequest("claude-sonnet-4", body, false);
|
||||
const toolUseIds = (result.messages as Array<{ content: unknown }>)
|
||||
.flatMap((msg) => (Array.isArray(msg.content) ? (msg.content as Block[]) : []))
|
||||
.filter((block) => block.type === "tool_use")
|
||||
.map((block) => block.id);
|
||||
assert.deepEqual(toolUseIds, ["call_with_bad_chars", "call_two_x"]);
|
||||
assert.deepEqual(
|
||||
toolResults(result).map((block) => [block.tool_use_id, block.content]),
|
||||
[
|
||||
["call_with_bad_chars", "paired"],
|
||||
// A present-but-invalid id keeps the existing sanitization path.
|
||||
["call_two_x", "explicit"],
|
||||
]
|
||||
);
|
||||
});
|
||||
162
tests/unit/responses-tool-output-pairing.test.ts
Normal file
162
tests/unit/responses-tool-output-pairing.test.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
// Responses → Chat translation of tool outputs that cannot be matched by id:
|
||||
// 1. `function_call_output` / `custom_tool_call_output` WITHOUT `call_id` that follows a
|
||||
// pending call → paired with that call (oldest first) instead of being replaced by an
|
||||
// empty synthetic result.
|
||||
// 2. Stateless continuations (`previous_response_id` + only `*_output` items) → the
|
||||
// originating call lives server-side, so the output is kept as labelled user context
|
||||
// instead of being dropped silently. An output with an EMPTY id stays dropped (#2893).
|
||||
|
||||
const { openaiResponsesToOpenAIRequest } =
|
||||
await import("../../open-sse/translator/request/openai-responses.ts");
|
||||
const { translateRequest } = await import("../../open-sse/translator/index.ts");
|
||||
|
||||
type Msg = Record<string, unknown>;
|
||||
|
||||
function translate(input: Msg[], extra: Msg = {}): Msg[] {
|
||||
const result = openaiResponsesToOpenAIRequest(
|
||||
"gpt-4o",
|
||||
{ model: "gpt-4o", input, ...extra },
|
||||
false,
|
||||
null
|
||||
) as { messages: Msg[] };
|
||||
return result.messages;
|
||||
}
|
||||
|
||||
const user = { type: "message", role: "user", content: [{ type: "input_text", text: "go" }] };
|
||||
|
||||
function call(id: string, name = "lookup") {
|
||||
return { type: "function_call", call_id: id, name, arguments: "{}" };
|
||||
}
|
||||
|
||||
function toolMessages(messages: Msg[]): Array<[unknown, unknown]> {
|
||||
return messages.filter((m) => m.role === "tool").map((m) => [m.tool_call_id, m.content]);
|
||||
}
|
||||
|
||||
function allText(messages: Msg[]): string {
|
||||
return JSON.stringify(messages);
|
||||
}
|
||||
|
||||
test("id-less function_call_output pairs with the pending call", () => {
|
||||
const messages = translate([
|
||||
user,
|
||||
call("call_abc"),
|
||||
{ type: "function_call_output", output: "ok" },
|
||||
]);
|
||||
assert.deepEqual(toolMessages(messages), [["call_abc", "ok"]]);
|
||||
});
|
||||
|
||||
test("two parallel calls + two id-less outputs pair in order", () => {
|
||||
const messages = translate([
|
||||
user,
|
||||
call("call_1"),
|
||||
call("call_2"),
|
||||
{ type: "function_call_output", output: "first" },
|
||||
{ type: "function_call_output", output: "second" },
|
||||
]);
|
||||
assert.deepEqual(toolMessages(messages), [
|
||||
["call_1", "first"],
|
||||
["call_2", "second"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("explicit call_id wins and id-less output takes the remaining call", () => {
|
||||
const messages = translate([
|
||||
user,
|
||||
call("call_1"),
|
||||
call("call_2"),
|
||||
{ type: "function_call_output", call_id: "call_2", output: "second" },
|
||||
{ type: "function_call_output", output: "first" },
|
||||
]);
|
||||
assert.deepEqual(toolMessages(messages), [
|
||||
["call_2", "second"],
|
||||
["call_1", "first"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("id-less custom_tool_call_output pairs with the pending custom call", () => {
|
||||
const messages = translate([
|
||||
user,
|
||||
{ type: "custom_tool_call", call_id: "call_patch", name: "apply_patch", input: "*** Begin" },
|
||||
{ type: "custom_tool_call_output", output: "patched" },
|
||||
]);
|
||||
assert.deepEqual(toolMessages(messages), [["call_patch", "patched"]]);
|
||||
});
|
||||
|
||||
test("id-less output with no pending call is still dropped", () => {
|
||||
const messages = translate([
|
||||
user,
|
||||
call("call_1"),
|
||||
{ type: "function_call_output", call_id: "call_1", output: "done" },
|
||||
{ type: "function_call_output", output: "stray" },
|
||||
]);
|
||||
assert.deepEqual(toolMessages(messages), [["call_1", "done"]]);
|
||||
assert.ok(!allText(messages).includes("stray"), "no stray user turn for an id-less orphan");
|
||||
});
|
||||
|
||||
test("stateless continuation keeps the orphan output as labelled user context", () => {
|
||||
const messages = translate(
|
||||
[{ type: "function_call_output", call_id: "call_a", output: '{"ok":true}' }],
|
||||
{ previous_response_id: "resp_prev" }
|
||||
);
|
||||
assert.deepEqual(toolMessages(messages), []);
|
||||
const carriers = messages.filter(
|
||||
(m) => m.role === "user" && String(m.content).includes('{"ok":true}')
|
||||
);
|
||||
assert.equal(carriers.length, 1);
|
||||
assert.equal(carriers[0].content, '[Unpaired tool result call_a]\n{"ok":true}');
|
||||
});
|
||||
|
||||
test("two orphan outputs are both preserved, in order", () => {
|
||||
const messages = translate(
|
||||
[
|
||||
{ type: "function_call_output", call_id: "call_a", output: "A" },
|
||||
{ type: "function_call_output", call_id: "call_b", output: "B" },
|
||||
],
|
||||
{ previous_response_id: "resp_prev" }
|
||||
);
|
||||
assert.deepEqual(
|
||||
messages.map((m) => [m.role, m.content]),
|
||||
[
|
||||
["user", "[Unpaired tool result call_a]\nA"],
|
||||
["user", "[Unpaired tool result call_b]\nB"],
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
test("paired call + output is unchanged", () => {
|
||||
const messages = translate([
|
||||
user,
|
||||
call("call_1"),
|
||||
{ type: "function_call_output", call_id: "call_1", output: "ok" },
|
||||
]);
|
||||
assert.deepEqual(
|
||||
messages.map((m) => m.role),
|
||||
["user", "assistant", "tool"]
|
||||
);
|
||||
assert.deepEqual(toolMessages(messages), [["call_1", "ok"]]);
|
||||
});
|
||||
|
||||
for (const target of ["openai", "gemini", "claude"]) {
|
||||
test(`translateRequest(openai-responses → ${target}): orphan output reaches the model`, () => {
|
||||
const body = {
|
||||
model: "m",
|
||||
previous_response_id: "resp_prev",
|
||||
input: [{ type: "function_call_output", call_id: "call_a", output: "weather: sunny" }],
|
||||
};
|
||||
const result = translateRequest("openai-responses", target, "m", body, false) as Msg;
|
||||
assert.ok(allText([result]).includes("weather: sunny"), "tool output preserved");
|
||||
if (Array.isArray(result.messages)) {
|
||||
const callIds = new Set(
|
||||
(result.messages as Msg[]).flatMap((m) =>
|
||||
Array.isArray(m.tool_calls) ? (m.tool_calls as Msg[]).map((tc) => tc.id) : []
|
||||
)
|
||||
);
|
||||
for (const m of result.messages as Msg[]) {
|
||||
if (m.role === "tool") assert.ok(callIds.has(m.tool_call_id), "no orphan role:tool");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -249,10 +249,16 @@ test("Responses -> Chat filters orphan tool outputs and supports role-based mess
|
||||
null
|
||||
);
|
||||
|
||||
assert.equal((result as any).messages.length, 3);
|
||||
// An orphan output is never sent as `role:"tool"`; its content is kept as labelled
|
||||
// user context (same label the Claude reconciliation uses) instead of being dropped.
|
||||
assert.equal((result as any).messages.length, 4);
|
||||
assert.equal((result as any).messages[0].role, "user");
|
||||
assert.equal((result as any).messages[1].tool_calls[0].id, "call_2");
|
||||
(assert as any).deepEqual((result as any).messages[2], {
|
||||
assert.deepEqual((result as { messages: unknown[] }).messages[1], {
|
||||
role: "user",
|
||||
content: "[Unpaired tool result orphan]\nskip",
|
||||
});
|
||||
assert.equal((result as any).messages[2].tool_calls[0].id, "call_2");
|
||||
(assert as any).deepEqual((result as any).messages[3], {
|
||||
role: "tool",
|
||||
tool_call_id: "call_2",
|
||||
content: "found",
|
||||
|
||||
Reference in New Issue
Block a user