Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(openai-chat): send Gemini's thought signature back with its tool calls

AdityaUmale83e355afabe4
diff --git a/server/drivers/openai-chat-protocol.test.ts b/server/drivers/openai-chat-protocol.test.ts
index 19f0147faa..0263147d5f 100644
--- a/server/drivers/openai-chat-protocol.test.ts
+++ b/server/drivers/openai-chat-protocol.test.ts
@@ -68,6 +68,19 @@ describe("complete chat tool calls", () => {
});
describe("streamed tool calls without an index (Gemini's OpenAI-compatible endpoint)", () => {
+ it("keeps each call's thought signature", () => {
+ const calls = new ChatToolCalls();
+ const extra_content = { google: { thought_signature: "c2ln" } };
+ calls.add([{ ...completeCall, extra_content }, { ...completeCall, id: "call_two" }], true);
+ expect(calls.finish("stop", false)).toEqual([{ ...completeCall, extra_content }, { ...completeCall, id: "call_two" }]);
+ });
+
+ it("ignores extra content that is not an object", () => {
+ const calls = new ChatToolCalls();
+ calls.add([{ ...completeCall, extra_content: "sig" }], true);
+ expect(calls.finish("stop", false)).toEqual([completeCall]);
+ });
+
const whole = (id: string, name: string, args = "{}") => ({ id, type: "function", function: { name, arguments: args } });
it("keeps two whole calls sent in one chunk", () => {
diff --git a/server/drivers/openai-chat-protocol.ts b/server/drivers/openai-chat-protocol.ts
index 6e8b0d77cd..beb2eb058a 100644
--- a/server/drivers/openai-chat-protocol.ts
+++ b/server/drivers/openai-chat-protocol.ts
@@ -3,6 +3,9 @@ export interface ChatToolCall {
id: string;
type: "function";
function: { name: string; arguments: string };
+ /** Gemini's thought signature (`{ google: { thought_signature } }`): sent back with the call, or Gemini rejects the
+ * next round with "Function call is missing a thought_signature". */
+ extra_content?: Record<string, unknown>;
}
/** Calls one model reply may carry (its index bound). */
@@ -66,6 +69,11 @@ export class ChatToolCalls {
if (field === "arguments") this.argumentChars += fn[field].length;
}
}
+ const extra = object(delta.extra_content);
+ if (extra) {
+ call.extra_content = extra;
+ this.argumentChars += JSON.stringify(extra).length;
+ }
if (call.function.name.length > 64 || this.argumentChars > MAX_ARGUMENT_CHARS) {
throw new ChatProtocolError("provider tool calls exceeded the size limit");
}
diff --git a/server/drivers/openai-chat-tools.test.ts b/server/drivers/openai-chat-tools.test.ts
index 588fedd53e..f543ff5a73 100644
--- a/server/drivers/openai-chat-tools.test.ts
+++ b/server/drivers/openai-chat-tools.test.ts
@@ -1413,6 +1413,23 @@ describe("a turn with the person's Windows computer, the built-in browser and th
});
describe("a provider that streams whole tool calls without an index", () => {
+ it("sends Gemini's thought signature back with its call on the next round", async () => {
+ const signed = { google: { thought_signature: "c2lnbmF0dXJl" } };
+ const f = await fixture((_body, response, round) => {
+ if (round === 1) sse(response, [chunk({ tool_calls: [
+ { id: "function-call-1", type: "function", function: { name: "audit_write", arguments: '{"name":"first","value":"done"}' }, extra_content: signed },
+ ] }, "stop")]);
+ else answer(response);
+ });
+ await f.start();
+ await f.decide();
+ expect(await f.completed()).toMatchObject({ ok: true });
+ const replayed = f.requests[1].messages.find((message) => message.role === "assistant");
+ expect(replayed?.tool_calls).toEqual([
+ { id: "function-call-1", type: "function", function: { name: "audit_write", arguments: '{"name":"first","value":"done"}' }, extra_content: signed },
+ ]);
+ });
+
it("runs both of Gemini's calls from one chunk and answers each by its id", async () => {
const f = await fixture((_body, response, round) => {
if (round === 1) sse(response, [chunk({ tool_calls: [