fix(ai): classify Google thoughtSignature as thinking

Google streaming may emit thoughtSignature without thought=true (including empty-text signature-only parts). Treat non-empty thoughtSignature as thinking to avoid leaking reasoning into normal text and retain signature across streaming deltas. Add unit test coverage.
This commit is contained in:
Ahmed Kamal 2026-01-06 20:47:19 +02:00
parent e80a924292
commit e42e9e6305
5 changed files with 108 additions and 9 deletions

View file

@ -19,7 +19,14 @@ import type {
} from "../types.js";
import { AssistantMessageEventStream } from "../utils/event-stream.js";
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
import { convertMessages, convertTools, mapStopReasonString, mapToolChoice } from "./google-shared.js";
import {
convertMessages,
convertTools,
isThinkingPart,
mapStopReasonString,
mapToolChoice,
retainThoughtSignature,
} from "./google-shared.js";
/**
* Thinking level for Gemini 3 models.
@ -360,7 +367,7 @@ export const streamGoogleGeminiCli: StreamFunction<"google-gemini-cli"> = (
if (candidate?.content?.parts) {
for (const part of candidate.content.parts) {
if (part.text !== undefined) {
const isThinking = part.thought === true;
const isThinking = isThinkingPart(part);
if (
!currentBlock ||
(isThinking && currentBlock.type !== "thinking") ||
@ -395,7 +402,10 @@ export const streamGoogleGeminiCli: StreamFunction<"google-gemini-cli"> = (
}
if (currentBlock.type === "thinking") {
currentBlock.thinking += part.text;
currentBlock.thinkingSignature = part.thoughtSignature;
currentBlock.thinkingSignature = retainThoughtSignature(
currentBlock.thinkingSignature,
part.thoughtSignature,
);
stream.push({
type: "thinking_delta",
contentIndex: blockIndex(),