mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-24 04:43:07 +00:00
fix(translator): read Responses usage names in the non-stream Gemini path and pair Cursor tool_result ids
- responseTranslator: the non-streaming OpenAI to Gemini-family converter falls back to input_tokens / output_tokens, so prompt and candidate counts are not zero while thoughtsTokenCount is set from output_tokens_details. - openai-to-cursor: in a Cursor agent session, a Claude tool_result block carries the same first-line id as the assistant tool call it answers.
This commit is contained in:
@@ -1051,8 +1051,9 @@ function convertOpenAINonStreamingToGeminiFamily(openaiResponse: JsonRecord): Js
|
||||
OPENAI_TO_GEMINI_FINISH_REASON[toString(choice.finish_reason, "stop")] ?? "STOP";
|
||||
|
||||
const usageSrc = toRecord(openaiResponse.usage);
|
||||
const promptTokens = toFiniteNumber(usageSrc.prompt_tokens, 0);
|
||||
const completionTokens = toFiniteNumber(usageSrc.completion_tokens, 0);
|
||||
// Some OpenAI-compatible upstreams report the Responses names (input/output_tokens).
|
||||
const promptTokens = toFiniteNumber(usageSrc.prompt_tokens ?? usageSrc.input_tokens, 0);
|
||||
const completionTokens = toFiniteNumber(usageSrc.completion_tokens ?? usageSrc.output_tokens, 0);
|
||||
const extraUsage = geminiExtraUsageCounts(usageSrc);
|
||||
|
||||
const geminiResponse: JsonRecord = {
|
||||
|
||||
@@ -160,7 +160,9 @@ function convertMessages(messages, preserveToolResults = false) {
|
||||
toolCallMetaMap.get(normalizeToolCallId(toolCallId));
|
||||
const toolName = toolMeta?.name || "tool";
|
||||
const toolContent = extractContent(tr.content);
|
||||
parts.push(buildToolResultBlock(toolName, toolCallId, toolContent));
|
||||
// Same id as the assistant call it answers (see withPairedId).
|
||||
const pairedId = preserveToolResults ? normalizeToolCallId(toolCallId) : toolCallId;
|
||||
parts.push(buildToolResultBlock(toolName, pairedId, toolContent));
|
||||
}
|
||||
}
|
||||
const joined = parts.filter(Boolean).join("\n");
|
||||
|
||||
@@ -89,3 +89,26 @@ test("Other Cursor-shaped consumers keep the raw assistant id and the text-block
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
test("Cursor: a Claude tool_result block carries the same id as the assistant call", () => {
|
||||
const out = translate(
|
||||
[
|
||||
{ role: "user", content: "go" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: "",
|
||||
tool_calls: [{ id: longId, type: "function", function: { name: "f", arguments: "{}" } }],
|
||||
},
|
||||
{ role: "user", content: [{ type: "tool_result", tool_use_id: longId, content: "result" }] },
|
||||
],
|
||||
"cursor"
|
||||
);
|
||||
const assistant = out.find((message) => message.role === "assistant");
|
||||
const resultText = out
|
||||
.filter((message) => message.role === "user")
|
||||
.map((message) => (typeof message.content === "string" ? message.content : ""))
|
||||
.join("\n");
|
||||
assert.equal(assistant?.tool_calls?.[0].id, "call_a");
|
||||
assert.match(resultText, /<tool_call_id>call_a<\/tool_call_id>/);
|
||||
assert.doesNotMatch(resultText, /fc_b/);
|
||||
});
|
||||
|
||||
@@ -72,3 +72,29 @@ test("the non-streaming Gemini-family path counts reasoning once too", () => {
|
||||
thoughtsTokenCount: 60,
|
||||
});
|
||||
});
|
||||
|
||||
test("the non-streaming Gemini-family path reads Responses-style usage names too", () => {
|
||||
const out = translateNonStreamingResponse(
|
||||
{
|
||||
id: "chatcmpl-2",
|
||||
model: "m",
|
||||
choices: [{ index: 0, message: { role: "assistant", content: "hi" }, finish_reason: "stop" }],
|
||||
usage: {
|
||||
input_tokens: 10,
|
||||
output_tokens: 100,
|
||||
total_tokens: 110,
|
||||
input_tokens_details: { cached_tokens: 4 },
|
||||
output_tokens_details: { reasoning_tokens: 60 },
|
||||
},
|
||||
},
|
||||
FORMATS.OPENAI,
|
||||
FORMATS.GEMINI
|
||||
) as { response: { usageMetadata: Record<string, number> } };
|
||||
assert.deepEqual(out.response.usageMetadata, {
|
||||
promptTokenCount: 10,
|
||||
candidatesTokenCount: 40,
|
||||
totalTokenCount: 110,
|
||||
thoughtsTokenCount: 60,
|
||||
cachedContentTokenCount: 4,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user