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:
serenity
2026-11-18 23:49:07 -03:00
committed by diegosouzapw
parent b02e282ac7
commit 73f02b9d74
4 changed files with 55 additions and 3 deletions

View File

@@ -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 = {

View File

@@ -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");

View File

@@ -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/);
});

View File

@@ -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,
});
});