fix(ruvocal): send Gemini's thought_signature back with tool calls (#5237)

After a tool ran, the follow-up completion to Gemini's OpenAI-compatible
endpoint failed with 400 "Function call is missing a thought_signature",
and the chat showed "No output was generated". Gemini attaches the
signature to each streamed tool call in extra_content and requires it on
the assistant message that carries those calls.

runMcpFlow now keeps extra_content from streamed and non-streamed tool
calls and echoes it on the assistant tool message. Providers that send no
extra_content are unaffected.

Reproduced against gemini-2.5-flash: the request shape ruvocal sent got
400; the same request with extra_content got 200 and an answer.

Co-authored-by: r <r@x>
This commit is contained in:
rUvandr authored and GitHub committed 2026-12-02 12:40:37 -05:00
1 parent 058943b053
commit 987efee9ee
2 files changed
+28 -9

No files matched your search

@@ -503,7 +503,10 @@ export async function* runMcpFlow({
logger.debug({ provider: providerHeader }, "[mcp] provider metadata emitted");
}
const toolCallState: Record<number, { id?: string; name?: string; arguments: string }> = {};
const toolCallState: Record<
number,
{ id?: string; name?: string; arguments: string; extraContent?: Record<string, unknown> }
> = {};
let firstToolDeltaLogged = false;
let sawToolCall = false;
let tokenCount = 0;
@@ -520,12 +523,14 @@ export async function* runMcpFlow({
index?: number;
id?: string;
function?: { name?: string; arguments?: string };
extra_content?: Record<string, unknown>;
};
const index = toolCall.index ?? 0;
const current = toolCallState[index] ?? { arguments: "" };
if (toolCall.id) current.id = toolCall.id;
if (toolCall.function?.name) current.name = toolCall.function.name;
if (toolCall.function?.arguments) current.arguments += toolCall.function.arguments;
if (toolCall.extra_content) current.extraContent = toolCall.extra_content;
toolCallState[index] = current;
}
if (!firstToolDeltaLogged) {
@@ -651,11 +656,15 @@ export async function* runMcpFlow({
}
);
const tc = nonStream.choices?.[0]?.message?.tool_calls ?? [];
calls = tc.map((t) => ({
id: t.id,
name: t.function?.name ?? "",
arguments: t.function?.arguments ?? "",
}));
calls = tc.map((t) => {
const extra = (t as { extra_content?: Record<string, unknown> }).extra_content;
return {
id: t.id,
name: t.function?.name ?? "",
arguments: t.function?.arguments ?? "",
...(extra ? { extraContent: extra } : {}),
};
});
} else {
calls = Object.values(toolCallState)
.map((c) => (c?.id && c?.name ? c : undefined))
@@ -664,16 +673,20 @@ export async function* runMcpFlow({
id: c?.id ?? "",
name: c?.name ?? "",
arguments: c?.arguments ?? "",
...(c?.extraContent ? { extraContent: c.extraContent } : {}),
})) as NormalizedToolCall[];
}
// Include the assistant message with tool_calls so the next round
// sees both the calls and their outputs, matching MCP branch behavior.
const toolCalls: ChatCompletionMessageToolCall[] = calls.map((call) => ({
// Echo provider extras (Gemini thought_signature) or the provider
// rejects the follow-up request.
const toolCalls = calls.map((call) => ({
id: call.id,
type: "function",
type: "function" as const,
function: { name: call.name, arguments: call.arguments },
}));
...(call.extraContent ? { extra_content: call.extraContent } : {}),
})) as ChatCompletionMessageToolCall[];
// Avoid sending <think> content back to the model alongside tool_calls
// to prevent confusing follow-up reasoning. Strip any think blocks.
@@ -27,6 +27,12 @@ export interface NormalizedToolCall {
id: string;
name: string;
arguments: string;
/**
* Provider data attached to the call that must be sent back unchanged with
* the follow-up request. Gemini puts the thought_signature here and rejects
* the next turn with a 400 when it is missing.
*/
extraContent?: Record<string, unknown>;
}
export interface ExecuteToolCallsParams {