Skip to content

Open source CapCut alternative.

TypeScript

fix(site): replay Gemini 3 thought signatures and pair tool calls by id

David Leae06d9b8d3f1
diff --git a/site/src/cut/lib/geminiChat.ts b/site/src/cut/lib/geminiChat.ts
index 02b6764a..45be1486 100644
--- a/site/src/cut/lib/geminiChat.ts
+++ b/site/src/cut/lib/geminiChat.ts
@@ -60,8 +60,11 @@ async function requestError(res: Response): Promise<string> {
const body = (await res.json().catch(() => null)) as {
error?: unknown;
message?: unknown;
+ details?: { message?: unknown } | null;
} | null;
- const message = [body?.message, body?.error].find(
+ // details.message carries the provider's real reason (e.g. the Vertex 400
+ // text); the top-level message is only the generic headline, so prefer it.
+ const message = [body?.details?.message, body?.message, body?.error].find(
(v): v is string => typeof v === "string" && v.length > 0
);
if (res.status === 402) {
@@ -81,16 +84,18 @@ async function execTool(name: string, args: Record<string, unknown>): Promise<un
return runAiTool(name, args);
}
-/** A tool result as a function_response content part. Screenshots leave the
- * JSON (a data URL inlined as text would blow the token budget) and ride along
- * as an image part instead, so the model actually sees the frame. */
-function functionResponsePart(name: string, output: unknown): Item {
+/** A tool result as a function_response content part. The id pairs it with its
+ * originating call so Gemini matches responses to calls across parallel calls.
+ * Screenshots leave the JSON (a data URL inlined as text would blow the token
+ * budget) and ride along as an image part instead, so the model sees the frame. */
+function functionResponsePart(name: string, output: unknown, id: string): Item {
if (output && typeof output === "object" && "image" in output) {
const { image, ...rest } = output as { image?: unknown };
const match = typeof image === "string" ? /^data:([^;,]+);base64,(.+)$/.exec(image) : null;
if (match) {
return {
type: "function_response",
+ id,
name,
response: rest,
mimeType: match[1],
@@ -102,12 +107,18 @@ function functionResponsePart(name: string, output: unknown): Item {
output && typeof output === "object" && !Array.isArray(output)
? output
: { result: output ?? null };
- return { type: "function_response", name, response };
+ return { type: "function_response", id, name, response };
}
interface ResponseBody {
output_text?: string;
- output?: { type?: string; id?: string; name?: string; arguments?: unknown }[];
+ output?: {
+ type?: string;
+ id?: string;
+ name?: string;
+ arguments?: unknown;
+ thoughtSignature?: string;
+ }[];
}
/** One chat turn: request → (tool round-trips) → reply, streamed as UI chunks. */
@@ -171,18 +182,24 @@ export function streamGeminiChat({
call.arguments && typeof call.arguments === "object" && !Array.isArray(call.arguments)
? (call.arguments as Record<string, unknown>)
: {};
- assistantParts.push({ functionCall: { name, args } });
+ const toolCallId = call.id ? String(call.id) : crypto.randomUUID().slice(0, 12);
+ // Replay the call with its id and Gemini-3 thought signature. The
+ // signature is mandatory — Gemini 3 rejects the follow-up turn unless
+ // each replayed call carries the exact signature it was issued with —
+ // and the matching id pairs each response to its call.
+ const assistantPart: Item = { functionCall: { id: toolCallId, name, args } };
+ if (call.thoughtSignature) assistantPart.thoughtSignature = call.thoughtSignature;
+ assistantParts.push(assistantPart);
if (abortSignal?.aborted) return;
- const toolCallId = String(call.id ?? crypto.randomUUID().slice(0, 12));
emit({ type: "tool-input-available", toolCallId, toolName: name, input: args });
try {
const output = await execTool(name, args);
emit({ type: "tool-output-available", toolCallId, output: output ?? null });
- responseParts.push(functionResponsePart(name, output));
+ responseParts.push(functionResponsePart(name, output, toolCallId));
} catch (err) {
const errorText = err instanceof Error ? err.message : String(err);
emit({ type: "tool-output-error", toolCallId, errorText });
- responseParts.push(functionResponsePart(name, { error: errorText }));
+ responseParts.push(functionResponsePart(name, { error: errorText }, toolCallId));
}
}
input.push({ role: "assistant", content: assistantParts });
diff --git a/site/src/lib/inference/adapters/gemini-computer-use.ts b/site/src/lib/inference/adapters/gemini-computer-use.ts
index eafcc6b2..212c31c1 100644
--- a/site/src/lib/inference/adapters/gemini-computer-use.ts
+++ b/site/src/lib/inference/adapters/gemini-computer-use.ts
@@ -454,11 +454,11 @@ function partFromValue(value: JsonValue): JsonObject {
}
if (isJsonObject(value.functionCall)) {
- return { functionCall: value.functionCall };
+ return withThoughtSignature(value, { functionCall: value.functionCall });
}
if (isJsonObject(value.function_call)) {
- return { functionCall: value.function_call };
+ return withThoughtSignature(value, { functionCall: value.function_call });
}
if (value.type === "function_response") {
@@ -486,6 +486,15 @@ function partFromValue(value: JsonValue): JsonObject {
return { text: JSON.stringify(value) };
}
+// A replayed function-call part keeps its Gemini-3 thoughtSignature, which sits
+// beside functionCall on the part (not inside it). Gemini validates the echoed
+// signature, so it must survive the round-trip through the caller's history.
+function withThoughtSignature(source: JsonObject, part: JsonObject): JsonObject {
+ const signature =
+ stringValue(source.thoughtSignature) ?? stringValue(source.thought_signature);
+ return signature ? { ...part, thoughtSignature: signature } : part;
+}
+
function imagePart(value: JsonObject): JsonObject {
const mimeType = stringValue(value.mime_type) || stringValue(value.mimeType) || "image/png";
const base64 = stringValue(value.image_base64) || stringValue(value.dataBase64);
@@ -584,6 +593,12 @@ function functionResponsePart(value: JsonObject): JsonObject {
name,
response,
};
+ // Echo the call id so Gemini pairs this response with its call — required when
+ // a single turn issued parallel calls with the same name.
+ const id = stringValue(value.id);
+ if (id) {
+ functionResponse.id = id;
+ }
if (screenshotBase64) {
functionResponse.parts = [
{
@@ -921,11 +936,21 @@ function functionCallFromPart(part: JsonObject): JsonObject | null {
return null;
}
- return {
+ const call: JsonObject = {
id: stringValue(value.id) ?? `call-${Math.random().toString(36).slice(2)}`,
name: stringValue(value.name) ?? "unknown_function",
arguments: isJsonObject(value.args) ? value.args : {},
};
+ // Gemini 3 attaches a thoughtSignature to each function call and rejects the
+ // follow-up turn unless that exact signature rides back with the replayed
+ // call. It sits on the part beside functionCall — surface it so the caller
+ // that runs the tool loop can send it back.
+ const thoughtSignature =
+ stringValue(part.thoughtSignature) ?? stringValue(part.thought_signature);
+ if (thoughtSignature) {
+ call.thoughtSignature = thoughtSignature;
+ }
+ return call;
}
function registeredToolTypes(tools: JsonValue | undefined): string[] {