Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: show Gemini's thinking in chat (#13107)

Nilaycc9128134156
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.py
index ff77691174c..6321e590389 100644
--- a/studio/backend/core/inference/external_provider.py
+++ b/studio/backend/core/inference/external_provider.py
@@ -4242,16 +4242,69 @@ class ExternalProviderClient:
}
}
)
- # Gemini 3 strict function-calling requires text-part thoughtSignatures to be replayed on history; the
- # frontend stows the latest one as extra_content.google.thought_signature on the assistant message and we
- # pin it onto the last text part here.
- if role == "assistant" and parts:
+ # Gemini history must keep every native thought/answer boundary intact so a signature is never moved
+ # onto adjacent unsigned text. The frontend carries those exact parts because OpenAI reasoning_content
+ # is not otherwise replayed to native Gemini.
+ if role == "assistant":
_msg_extra = msg.get("extra_content") if isinstance(msg, dict) else None
if isinstance(_msg_extra, dict):
_msg_g = _msg_extra.get("google") or {}
if isinstance(_msg_g, dict):
+ _replayed_thoughts: list[dict[str, Any]] = []
+ _thought_parts = _msg_g.get("thought_parts")
+ if isinstance(_thought_parts, list):
+ for _thought_part in _thought_parts:
+ if not isinstance(_thought_part, dict):
+ continue
+ _thought_text = _thought_part.get("text")
+ _thought_sig = _thought_part.get(
+ "thought_signature"
+ ) or _thought_part.get("thoughtSignature")
+ if not isinstance(_thought_text, str):
+ continue
+ if _thought_sig is not None and (
+ not isinstance(_thought_sig, str) or not _thought_sig
+ ):
+ continue
+ _replayed_thought: dict[str, Any] = {
+ "text": _thought_text,
+ "thought": True,
+ }
+ if isinstance(_thought_sig, str):
+ _replayed_thought["thoughtSignature"] = _thought_sig
+ _replayed_thoughts.append(_replayed_thought)
+ _signed_answers = _msg_g.get("answer_parts")
+ _replayed_answers: list[dict[str, Any]] = []
+ if isinstance(_signed_answers, list):
+ for _signed_answer in _signed_answers:
+ if not isinstance(_signed_answer, dict):
+ continue
+ _answer_text = _signed_answer.get("text")
+ _answer_sig = _signed_answer.get(
+ "thought_signature"
+ ) or _signed_answer.get("thoughtSignature")
+ if not isinstance(_answer_text, str):
+ continue
+ if _answer_sig is not None and (
+ not isinstance(_answer_sig, str) or not _answer_sig
+ ):
+ continue
+ _answer_part: dict[str, Any] = {"text": _answer_text}
+ if isinstance(_answer_sig, str):
+ _answer_part["thoughtSignature"] = _answer_sig
+ _replayed_answers.append(_answer_part)
+ if _replayed_answers:
+ # `content` is the UI's merged rendering of these exact native parts. Replace its text
+ # rather than appending a second copy, and keep empty signature-only parts intact.
+ parts = [
+ *_replayed_thoughts,
+ *_replayed_answers,
+ *(part for part in parts if "text" not in part),
+ ]
+ else:
+ parts[:0] = _replayed_thoughts
_msg_sig = _msg_g.get("thought_signature") or _msg_g.get("thoughtSignature")
- if isinstance(_msg_sig, str) and _msg_sig:
+ if not _replayed_answers and isinstance(_msg_sig, str) and _msg_sig:
for _idx in range(len(parts) - 1, -1, -1):
if "text" in parts[_idx]:
parts[_idx] = {
@@ -4590,8 +4643,7 @@ class ExternalProviderClient:
}
thinking_budget: Optional[int] = None
if effort_lc == "none" or enable_thinking is False:
- # Pro-tier 2.5 rejects budget=0 (400 "only works in thinking mode"), so coerce to a small positive
- # value.
+ # Gemini 2.5 Pro rejects zero, so use a small positive budget.
thinking_budget = 128 if _is_pro_thinking_only else 0
elif effort_lc in _EFFORT_TO_BUDGET:
thinking_budget = _EFFORT_TO_BUDGET[effort_lc]
@@ -4601,12 +4653,17 @@ class ExternalProviderClient:
gen_config["thinkingConfig"] = {
"thinkingBudget": thinking_budget,
}
+ if (
+ "thinkingConfig" in gen_config
+ and effort_lc not in ("none", "off")
+ and enable_thinking is not False
+ ):
+ gen_config["thinkingConfig"]["includeThoughts"] = True
if gen_config:
body["generationConfig"] = gen_config
- # Hosted tools: googleSearch (grounding) and codeExecution. Image-mode rejects codeExecution; only Gemini 3
- # image models accept googleSearch.
+ # Image models reject codeExecution; only Gemini 3 image models accept googleSearch.
def _gemini_image_model_allows_google_search(_m: str) -> bool:
return (
_m.startswith("gemini-3-pro-image")
@@ -4876,8 +4933,12 @@ class ExternalProviderClient:
}
return f"data: {_json.dumps(chunk)}"
- def _text_chunk(text: str, extra_content: Optional[dict[str, Any]] = None) -> str:
- delta: dict[str, Any] = {"content": text}
+ def _text_chunk(
+ text: str,
+ extra_content: Optional[dict[str, Any]] = None,
+ field: str = "content",
+ ) -> str:
+ delta: dict[str, Any] = {field: text}
if extra_content:
delta["extra_content"] = extra_content
chunk = {
@@ -4894,13 +4955,41 @@ class ExternalProviderClient:
return f"data: {_json.dumps(chunk)}"
def _gemini_part_extra(part: dict[str, Any]) -> Optional[dict[str, Any]]:
- """Return ``{"google": {"thought_signature": ...}}`` when the Gemini stream part carries
- a `thoughtSignature` we must replay on a follow-up turn (Gemini 3 image editing and
- tool contexts both require an exact signature echo)."""
+ """Return replay metadata for one Gemini native stream part.
+
+ Thought and answer parts keep their exact boundaries, including unsigned text beside signed
+ text and signature-only empty parts. Gemini validates a signature against the part it originally
+ signed, so collapsing these to one scalar signature corrupts follow-up history.
+ """
sig = part.get("thoughtSignature") or part.get("thought_signature")
- if isinstance(sig, str) and sig:
- return {"google": {"thought_signature": sig}}
- return None
+ valid_sig = sig if isinstance(sig, str) and sig else None
+ google: dict[str, Any] = {}
+ if valid_sig is not None:
+ google["thought_signature"] = valid_sig
+ if part.get("thought") is True:
+ thought_part: dict[str, Any] = {"text": part.get("text", "")}
+ if valid_sig is not None:
+ thought_part["thought_signature"] = valid_sig
+ google["thought_part"] = thought_part
+ if valid_sig is not None:
+ google["thought"] = True
+ elif ("text" in part and isinstance(part.get("text"), str)) or (
+ valid_sig is not None
+ and not any(
+ key in part
+ for key in (
+ "functionCall",
+ "executableCode",
+ "codeExecutionResult",
+ "inlineData",
+ )
+ )
+ ):
+ answer_part: dict[str, Any] = {"text": part.get("text", "")}
+ if valid_sig is not None:
+ answer_part["thought_signature"] = valid_sig
+ google["answer_part"] = answer_part
+ return {"google": google} if google else None
# Gemini finish reasons -> OpenAI vocabulary.
_finish_reason_map: dict[str, Optional[str]] = {
@@ -5099,14 +5188,18 @@ class ExternalProviderClient:
for part in parts:
if not isinstance(part, dict):
continue
- # Text delta. Stow part-level `thoughtSignature` on the delta so Gemini 3 turns
- # needing an exact signature echo round-trip cleanly.
+ # Preserve thoughtSignature for Gemini 3's exact follow-up echo.
text = part.get("text")
_part_extra = _gemini_part_extra(part)
if isinstance(text, str) and text:
yield _text_chunk(
text,
extra_content = _part_extra,
+ field = (
+ "reasoning_content"
+ if part.get("thought")
+ else "content"
+ ),
)
elif _part_extra is not None and not any(
k in part
@@ -5117,8 +5210,7 @@ class ExternalProviderClient:
"inlineData",
)
):
- # Empty-content part carrying a thoughtSignature: emit an empty delta to
- # preserve the signature.
+ # Emit an empty delta when a signature has no payload.
yield _text_chunk(
"",
extra_content = _part_extra,
diff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.py
index 9b9d7c720d3..67ce6fa4dc8 100644
--- a/studio/backend/core/inference/studio_tool_loop.py
+++ b/studio/backend/core/inference/studio_tool_loop.py
@@ -565,6 +565,58 @@ class _Turn:
hosted_results: dict[str, dict[str, Any]] = field(default_factory = dict)
provider_compaction: dict[str, Any] | None = None
+ def note_reasoning_extra(self, extra: Any) -> None:
+ """Accumulate Gemini's per-part replay envelopes across one provider turn."""
+ if not isinstance(extra, dict):
+ return
+ previous = self.reasoning_extra if isinstance(self.reasoning_extra, dict) else {}
+ merged = {**previous, **extra}
+ previous_google = previous.get("google")
+ incoming_google = extra.get("google")
+ if isinstance(previous_google, dict) or isinstance(incoming_google, dict):
+ old_google = previous_google if isinstance(previous_google, dict) else {}
+ new_google = incoming_google if isinstance(incoming_google, dict) else {}
+ google = {**old_google, **new_google}
+ for singular, plural in (
+ ("thought_part", "thought_parts"),
+ ("answer_part", "answer_parts"),
+ ):
+ accumulated: list[dict[str, Any]] = []
+ for source in (old_google, new_google):
+ many = source.get(plural)
+ if isinstance(many, list):
+ accumulated.extend(dict(part) for part in many if isinstance(part, dict))
+ one = source.get(singular)
+ if isinstance(one, dict):
+ accumulated.append(dict(one))
+ if accumulated:
+ compacted: list[dict[str, Any]] = []
+ for part in accumulated:
+ signature = part.get("thought_signature") or part.get("thoughtSignature")
+ if (
+ compacted
+ and not signature
+ and not (
+ compacted[-1].get("thought_signature")
+ or compacted[-1].get("thoughtSignature")
+ )
+ and isinstance(part.get("text"), str)
+ and isinstance(compacted[-1].get("text"), str)
+ ):
+ compacted[-1]["text"] += part["text"]
+ else:
+ compacted.append(part)
+ google[plural] = compacted
+ google.pop(singular, None)
+ if google.get("thought_parts") or google.get("answer_parts"):
+ # Native ledgers already pin every signature to its original part. Retaining the legacy scalar
+ # would let the translator move an earlier thought signature onto later unsigned text.
+ google.pop("thought_signature", None)
+ google.pop("thoughtSignature", None)
+ google.pop("thought", None)
+ merged["google"] = google
+ self.reasoning_extra = merged
+
def compaction_replay_message(self) -> dict[str, Any] | None:
"""Build one replay turn and remove Anthropic's native block from later metadata."""
replay = self.provider_compaction
@@ -1545,8 +1597,7 @@ async def stream_with_studio_tools(
content = delta.get("content")
raw_calls = delta.get("tool_calls")
extra = delta.get("extra_content")
- if isinstance(extra, dict):
- turn.reasoning_extra = extra
+ turn.note_reasoning_extra(extra)
turn.note_hosted_tool_event(payload.get("_toolEvent"))
compaction = _openai_compaction_item(payload.get("_toolEvent"))
if compaction:
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 921376ca191..2f2f9d198ca 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -27521,6 +27521,9 @@ def _build_external_messages(
if provider_type == "llama_cpp" and msg.role == "assistant" and msg.reasoning_content
else {}
)
+ has_message_extra_content = bool(
+ emit_message_extra_content and msg.role == "assistant" and msg.extra_content
+ )
if anthropic and msg.role == "assistant" and isinstance(msg.extra_content, dict):
native = msg.extra_content.get("anthropic")
if (
@@ -27538,7 +27541,9 @@ def _build_external_messages(
and msg.tool_call_id in dropped_server_builtin_tool_call_ids
):
continue
- if isinstance(msg.content, str) or (msg.content is None and replay):
+ if isinstance(msg.content, str) or (
+ msg.content is None and (replay or has_message_extra_content)
+ ):
# Drop bare assistant messages with no content AND no tool_calls
# (some providers reject empty assistant turns). Preserve assistant
# turns whose only payload is tool_calls so multi-turn
@@ -27548,6 +27553,7 @@ def _build_external_messages(
and not (msg.content or "").strip()
and not msg.tool_calls
and not replay
+ and not has_message_extra_content
):
continue
out: dict[str, Any] = {"role": msg.role, "content": msg.content or "", **replay}
@@ -27555,7 +27561,9 @@ def _build_external_messages(
_tcs = _filter_tool_calls(msg.tool_calls)
if _tcs:
out["tool_calls"] = _tcs
- elif not (msg.content or "").strip() and not replay:
+ elif (
+ not (msg.content or "").strip() and not replay and not has_message_extra_content
+ ):
# Every tool_call was a dropped synthetic provider card;
# the turn would be an empty
# `{"role":"assistant","content":""}` that some providers
@@ -27638,12 +27646,17 @@ def _build_external_messages(
_tcs = _filter_tool_calls(msg.tool_calls)
if _tcs:
entry["tool_calls"] = _tcs
- elif not parts and not replay:
+ elif not parts and not replay and not has_message_extra_content:
# All tool_calls were synthetic and dropped, and no
# content parts survived. Skip rather than forward an
# empty assistant turn that downstream providers reject.
continue
- elif msg.role == "assistant" and not parts and not replay:
+ elif (
+ msg.role == "assistant"
+ and not parts
+ and not replay
+ and not has_message_extra_content
+ ):
continue
if msg.role == "tool":
if msg.tool_call_id:
@@ -27673,7 +27686,12 @@ def _build_external_messages(
if p.content and p.encrypted_content:
compaction["encrypted_content"] = p.encrypted_content
preserved.append(compaction)
- if msg.role == "assistant" and not preserved and not replay:
+ if (
+ msg.role == "assistant"
+ and not preserved
+ and not replay
+ and not has_message_extra_content
+ ):
continue
if len(preserved) == 1 and preserved[0]["type"] == "text":
# Single text part collapses to a string for providers that
@@ -27692,7 +27710,7 @@ def _build_external_messages(
_has_text = (
isinstance(_entry_content, str) and _entry_content.strip()
) or (isinstance(_entry_content, list) and len(_entry_content) > 0)
- if not _has_text and not replay:
+ if not _has_text and not replay and not has_message_extra_content:
continue
if msg.role == "tool":
if msg.tool_call_id:
diff --git a/studio/backend/tests/test_gemini_provider.py b/studio/backend/tests/test_gemini_provider.py
index c86e455f53d..548c9c898ef 100644
--- a/studio/backend/tests/test_gemini_provider.py
+++ b/studio/backend/tests/test_gemini_provider.py
@@ -410,11 +410,10 @@ def test_gemini25_flash_effort_levels_map_to_budgets(monkeypatch):
reasoning_effort = effort,
)
tc = captured["body"]["generationConfig"].get("thinkingConfig")
- assert tc == {"thinkingBudget": expected}, (effort, tc)
+ assert tc == {"thinkingBudget": expected, "includeThoughts": True}, (effort, tc)
def test_gemini3_flash_effort_levels_map_to_thinking_level(monkeypatch):
- """Gemini 3 Flash thinkingLevel ladder: minimal/low/medium/high."""
cases = {
"minimal": "minimal",
"low": "low",
@@ -429,13 +428,11 @@ def test_gemini3_flash_effort_levels_map_to_thinking_level(monkeypatch):
reasoning_effort = effort,
)
tc = captured["body"]["generationConfig"].get("thinkingConfig")
- assert tc == {"thinkingLevel": expected}, (effort, tc)
+ assert tc == {"thinkingLevel": expected, "includeThoughts": True}, (effort, tc)
def test_gemini3_pro_passes_medium_through(monkeypatch):
- """Gemini 3.1+ Pro accepts thinkingLevel="medium" per
- https://docs.cloud.google.com/vertex-ai/generative-ai/docs/models/gemini/3-1-pro;
- forward as-is (medium is the documented mid-tier on Gemini 3.1)."""
+ """https://docs.cloud.google.com/vertex-ai/generative-ai/docs/models/gemini/3-1-pro"""
for model in (
"gemini-3.1-pro-preview",
"gemini-pro-latest",
@@ -446,22 +443,21 @@ def test_gemini3_pro_passes_medium_through(monkeypatch):
reasoning_effort = "medium",
)
tc = captured["body"]["generationConfig"].get("thinkingConfig")
- assert tc == {"thinkingLevel": "medium"}, (model, tc)
+ assert tc == {"thinkingLevel": "medium", "includeThoughts": True}, (model, tc)
def test_gemini3_pro_minimal_effort_coerces_to_low(monkeypatch):
- """Gemini 3 Pro rejects thinkingLevel="minimal"; coerce to "low"."""
+ """Gemini 3 Pro rejects thinkingLevel="minimal", so use "low"."""
captured = _capture_body(
monkeypatch,
model = "gemini-3.1-pro-preview",
reasoning_effort = "minimal",
)
tc = captured["body"]["generationConfig"].get("thinkingConfig")
- assert tc == {"thinkingLevel": "low"}, tc
+ assert tc == {"thinkingLevel": "low", "includeThoughts": True}, tc
def test_gemini3_flash_effort_none_maps_to_minimal(monkeypatch):
- """reasoning_effort='none' on Gemini 3 Flash -> thinkingLevel=minimal."""
captured = _capture_body(
monkeypatch,
model = "gemini-3.5-flash",
@@ -472,17 +468,83 @@ def test_gemini3_flash_effort_none_maps_to_minimal(monkeypatch):
def test_thinking_default_omits_thinking_config(monkeypatch):
- """When neither knob is supplied, thinkingConfig is omitted (Google's
- server-side default applies)."""
+ """Google's server-side default applies when neither thinking knob is supplied."""
captured = _capture_body(monkeypatch, model = "gemini-3.5-flash")
gc = captured["body"]["generationConfig"]
assert "thinkingConfig" not in gc, gc
+ ("model", "kwargs"),
+ (
+ ("gemini-3.6-flash", {"reasoning_effort": "high"}),
+ ("gemini-3.1-pro-preview", {"reasoning_effort": "low"}),
+ ("gemini-2.5-flash", {"reasoning_effort": "medium"}),
+ ("gemma-4-31b-it", {"enable_thinking": True}),
+ ),
+)
+def test_thinking_on_asks_gemini_for_its_thoughts(monkeypatch, model, kwargs):
+ tc = _capture_body(monkeypatch, model = model, **kwargs)["body"]["generationConfig"][
+ "thinkingConfig"
+ ]
+ assert tc.get("includeThoughts") is True, (model, tc)
+
+
+ ("model", "kwargs"),
+ (
+ ("gemini-3.6-flash", {"reasoning_effort": "none"}),
+ ("gemini-2.5-flash", {"enable_thinking": False}),
+ ("gemini-2.5-pro", {"enable_thinking": False}),
+ ("gemma-4-31b-it", {"enable_thinking": False}),
+ ),
+)
+def test_thinking_off_does_not_ask_for_thoughts(monkeypatch, model, kwargs):
+ tc = _capture_body(monkeypatch, model = model, **kwargs)["body"]["generationConfig"][
+ "thinkingConfig"
+ ]
+ assert "includeThoughts" not in tc, (model, tc)
+
+
+def test_thought_parts_stream_as_reasoning_not_answer(monkeypatch):
+ sse = [
+ _event(
+ [{"text": "Unsigned preface. ", "thought": True}],
+ finish_reason = None,
+ ),
+ _event(
+ [
+ {
+ "text": "**Reading the riddle**",
+ "thought": True,
+ "thoughtSignature": "SIG-THOUGHT",
+ }
+ ],
+ finish_reason = None,
+ ),
+ _event([{"text": "The man is your son."}], finish_reason = None),
+ _event([{"text": "", "thoughtSignature": "SIG"}]),
+ ]
+ chunks = _parse_chunks(
+ _collect(monkeypatch, sse, model = "gemini-3.6-flash", reasoning_effort = "high")
+ )
+ deltas = [c["choices"][0]["delta"] for c in chunks if c.get("choices")]
+ reasoning = "".join(d.get("reasoning_content", "") for d in deltas)
+ answer = "".join(d.get("content") or "" for d in deltas)
+ assert reasoning == "Unsigned preface. **Reading the riddle**", deltas
+ assert answer == "The man is your son.", deltas
+ thought_extras = [
+ delta["extra_content"]["google"]["thought_part"]
+ for delta in deltas
+ if delta.get("reasoning_content")
+ ]
+ assert thought_extras == [
+ {"text": "Unsigned preface. "},
+ {"text": "**Reading the riddle**", "thought_signature": "SIG-THOUGHT"},
+ ]
+
+
def test_nano_banana_alias_routes_through_image_modalities(monkeypatch):
- """`nano-banana-pro-preview` aliases the Pro image model; must set
- responseModalities=[TEXT,IMAGE] when the Images pill is on
- (enabled_tools includes "image_generation")."""
captured = _capture_body(
monkeypatch,
model = "nano-banana-pro-preview",
@@ -493,11 +555,7 @@ def test_nano_banana_alias_routes_through_image_modalities(monkeypatch):
def test_image_capable_model_without_image_pill_stays_text_only(monkeypatch):
- """When the Images pill is off (no image_generation in enabled_tools), an
- image-capable model id (gemini-2.5-flash-image) must force
- responseModalities=["TEXT"]. Google's image models default to text+image
- when responseModalities is omitted, so omitting it would silently bill
- image output the UI says is disabled."""
+ """Forcing TEXT avoids billed images because Google image models default to text and image."""
captured = _capture_body(
monkeypatch,
model = "gemini-2.5-flash-image",
@@ -1346,11 +1404,44 @@ def test_empty_text_part_with_thought_signature_emits_extra_content(monkeypatch)
for c in chunks
if c.get("choices")
and c["choices"][0]["delta"].get("extra_content")
- == {"google": {"thought_signature": "SIG-FINAL"}}
+ == {
+ "google": {
+ "thought_signature": "SIG-FINAL",
+ "answer_part": {"text": "", "thought_signature": "SIG-FINAL"},
+ }
+ }
]
assert extra_carriers, chunks
+def test_answer_part_boundaries_surface_even_when_unsigned(monkeypatch):
+ sse = [
+ _event(
+ [
+ {"text": "first"},
+ {"text": "second", "thoughtSignature": "SIG-SECOND"},
+ {"thoughtSignature": "SIG-EMPTY"},
+ ],
+ usage = {"promptTokenCount": 2, "candidatesTokenCount": 2},
+ ),
+ ]
+ deltas = [
+ chunk["choices"][0]["delta"]
+ for chunk in _parse_chunks(_collect(monkeypatch, sse))
+ if chunk.get("choices") and chunk["choices"][0].get("delta")
+ ]
+ answer_parts = [
+ delta["extra_content"]["google"]["answer_part"]
+ for delta in deltas
+ if (delta.get("extra_content") or {}).get("google", {}).get("answer_part") is not None
+ ]
+ assert answer_parts == [
+ {"text": "first"},
+ {"text": "second", "thought_signature": "SIG-SECOND"},
+ {"text": "", "thought_signature": "SIG-EMPTY"},
+ ]
+
+
def test_enable_prompt_caching_false_string_coerces_to_bool():
"""Pre-PR the field was Optional[bool]; widening to Union[bool,str] must
preserve historical coercion so callers sending `"false"` still opt out of
@@ -1787,7 +1878,12 @@ def test_text_chunk_carries_thought_signature(monkeypatch):
]
assert text_chunks, chunks
extra = text_chunks[0]["choices"][0]["delta"].get("extra_content")
- assert extra == {"google": {"thought_signature": "SIG-TEXT"}}, text_chunks
+ assert extra == {
+ "google": {
+ "thought_signature": "SIG-TEXT",
+ "answer_part": {"text": "hello", "thought_signature": "SIG-TEXT"},
+ }
+ }, text_chunks
def test_openai_tools_translated_into_function_declarations(monkeypatch):
@@ -2023,6 +2119,76 @@ def test_assistant_text_thought_signature_replays_on_outbound_text_part(monkeypa
assert text_parts[-1].get("thoughtSignature") == "SIG-TEXT", text_parts
+def test_assistant_answer_parts_replay_exact_signed_boundaries(monkeypatch):
+ captured = _capture_body(
+ monkeypatch,
+ messages = [
+ {"role": "user", "content": "hi"},
+ {
+ "role": "assistant",
+ "content": "firstsecond",
+ "extra_content": {
+ "google": {
+ "answer_parts": [
+ {"text": "first"},
+ {"text": "second", "thought_signature": "SIG-SECOND"},
+ {"text": "", "thought_signature": "SIG-EMPTY"},
+ ],
+ },
+ },
+ },
+ {"role": "user", "content": "again"},
+ ],
+ )
+ assert captured["body"]["contents"][1]["parts"] == [
+ {"text": "first"},
+ {"text": "second", "thoughtSignature": "SIG-SECOND"},
+ {"text": "", "thoughtSignature": "SIG-EMPTY"},
+ ]
+
+
+def test_assistant_thought_parts_replay_exact_boundaries_before_answer(monkeypatch):
+ captured = _capture_body(
+ monkeypatch,
+ messages = [
+ {"role": "user", "content": "hi"},
+ {
+ "role": "assistant",
+ "content": "the answer",
+ "extra_content": {
+ "google": {
+ "thought_parts": [
+ {"text": "unsigned preface"},
+ {
+ "text": "consider the clues",
+ "thought_signature": "SIG-THOUGHT-1",
+ },
+ {
+ "text": "check the conclusion",
+ "thought_signature": "SIG-THOUGHT-2",
+ },
+ ]
+ }
+ },
+ },
+ {"role": "user", "content": "again"},
+ ],
+ )
+ parts = captured["body"]["contents"][1]["parts"]
+ assert parts[0] == {"text": "unsigned preface", "thought": True}
+ assert parts[1] == {
+ "text": "consider the clues",
+ "thought": True,
+ "thoughtSignature": "SIG-THOUGHT-1",
+ }
+ assert parts[2] == {
+ "text": "check the conclusion",
+ "thought": True,
+ "thoughtSignature": "SIG-THOUGHT-2",
+ }
+ assert parts[3] == {"text": "the answer"}
+
+
def test_function_declarations_strip_openai_only_schema_keys(monkeypatch):
"""OpenAI strict tools commonly include `additionalProperties`, `$schema`,
`$defs`, `strict`, etc. Gemini's Schema rejects those with
@@ -2470,6 +2636,59 @@ def test_chat_message_extra_content_round_trips_through_validation():
assert "extra_content" not in built_custom[1], built_custom[1]
+def test_metadata_only_gemini_assistant_turn_survives_external_message_build():
+ """Gemini can finish with a signed thought summary and no visible answer.
+ Preserve that metadata-only assistant turn for the next native Gemini request."""
+ from models.inference import ChatCompletionRequest
+ from routes.inference import _build_external_messages
+
+ req = ChatCompletionRequest.model_validate(
+ {
+ "model": "gemini-2.5-flash",
+ "messages": [
+ {"role": "user", "content": "think silently"},
+ {
+ "role": "assistant",
+ "content": "",
+ "extra_content": {
+ "google": {
+ "thought_parts": [
+ {"text": "private summary", "thought_signature": "SIG-ONLY"}
+ ]
+ }
+ },
+ },
+ {"role": "user", "content": "continue"},
+ ],
+ "max_tokens": 64,
+ "stream": True,
+ }
+ )
+
+ built = _build_external_messages(
+ req.messages,
+ supports_vision = True,
+ provider_type = "gemini",
+ base_url = "https://generativelanguage.googleapis.com/v1beta",
+ )
+ assert built[1] == {
+ "role": "assistant",
+ "content": "",
+ "extra_content": {
+ "google": {
+ "thought_parts": [{"text": "private summary", "thought_signature": "SIG-ONLY"}]
+ }
+ },
+ }
+
+ built_openai = _build_external_messages(
+ req.messages,
+ supports_vision = True,
+ provider_type = "openai",
+ )
+ assert [message["role"] for message in built_openai] == ["user", "user"]
+
+
def test_parallel_tool_results_group_into_one_user_block(monkeypatch):
"""Round 14: Gemini docs group parallel functionResponses in a single
subsequent user content with multiple functionResponse parts. Consecutive
@@ -2876,34 +3095,32 @@ def test_function_schema_anyof_null_variant_flattens_to_nullable(monkeypatch):
def test_legacy_gemini3_pro_medium_coerced_to_high(monkeypatch):
- """Round 17: legacy `gemini-3-pro*` (incl. `-preview`, shut down
- 2026-03-09) only accepted low/high. 3.1+ Pro added medium. The backend
- must coerce medium → high for the legacy model so stale UI state doesn't
- 400 the request."""
+ """Legacy Gemini 3 Pro accepted only low/high, so stale medium state must map to high."""
captured = _capture_body(
monkeypatch,
model = "gemini-3-pro-preview",
reasoning_effort = "medium",
)
- assert captured["body"]["generationConfig"]["thinkingConfig"] == {"thinkingLevel": "high"}
+ assert captured["body"]["generationConfig"]["thinkingConfig"] == {
+ "thinkingLevel": "high",
+ "includeThoughts": True,
+ }
def test_gemini_3_1_pro_medium_passes_through(monkeypatch):
- """Round 17 regression: 3.1+ Pro accepts medium; coercion must NOT apply
- when the model id is gemini-3.1-pro*."""
captured = _capture_body(
monkeypatch,
model = "gemini-3.1-pro-preview",
reasoning_effort = "medium",
)
- assert captured["body"]["generationConfig"]["thinkingConfig"] == {"thinkingLevel": "medium"}
+ assert captured["body"]["generationConfig"]["thinkingConfig"] == {
+ "thinkingLevel": "medium",
+ "includeThoughts": True,
+ }
def test_tool_calls_extra_content_stripped_for_non_native_gemini():
- """Round 17: per-tool-call `extra_content` (Gemini thoughtSignature
- carrier) must not leak through `_build_external_messages` to
- non-native-Gemini providers; OpenAI / Anthropic / custom Gemini OAI-compat
- gateways would 400 on the unknown key."""
+ """Strip Gemini thought signatures because non-native providers reject `extra_content`."""
from models.inference import ChatCompletionRequest
from routes.inference import _build_external_messages
diff --git a/studio/backend/tests/test_provider_reasoning_wire.py b/studio/backend/tests/test_provider_reasoning_wire.py
index c632f58c556..616b604551a 100644
--- a/studio/backend/tests/test_provider_reasoning_wire.py
+++ b/studio/backend/tests/test_provider_reasoning_wire.py
@@ -198,7 +198,10 @@ def test_gemini_families_past_three_use_thinking_level():
top_p = 0.95,
max_tokens = 64,
)
- assert body["generationConfig"]["thinkingConfig"] == {"thinkingLevel": "medium"}
+ assert body["generationConfig"]["thinkingConfig"] == {
+ "thinkingLevel": "medium",
+ "includeThoughts": True,
+ }
body = _body(
"gemini",
"gemini-4-pro",
@@ -207,7 +210,10 @@ def test_gemini_families_past_three_use_thinking_level():
top_p = 0.95,
max_tokens = 64,
)
- assert body["generationConfig"]["thinkingConfig"] == {"thinkingLevel": "low"}
+ assert body["generationConfig"]["thinkingConfig"] == {
+ "thinkingLevel": "low",
+ "includeThoughts": True,
+ }
body = _body(
"gemini",
"gemini-2.5-flash",
@@ -244,15 +250,17 @@ def test_anthropic_models_outside_the_spec_take_the_adaptive_shape():
def test_gemma_on_gemini_toggles_with_thinking_level_not_budget():
sampling = {"temperature": 0.7, "top_p": 0.95, "max_tokens": 64}
body = _body("gemini", "gemma-4-31b-it", enable_thinking = True, **sampling)
- assert body["generationConfig"]["thinkingConfig"] == {"thinkingLevel": "high"}
+ assert body["generationConfig"]["thinkingConfig"] == {
+ "thinkingLevel": "high",
+ "includeThoughts": True,
+ }
body = _body("gemini", "gemma-4-26b-a4b-it", enable_thinking = False, **sampling)
assert body["generationConfig"]["thinkingConfig"] == {"thinkingLevel": "minimal"}
body = _body("gemini", "gemma-4-31b-it", **sampling)
assert "thinkingConfig" not in body.get("generationConfig", {})
-# Wire proof for older Claude models, run against a mocked Anthropic stream rather than the live API. Anthropic documents
-# adaptive thinking as a 400 on Claude 4.5 and earlier, and budget_tokens as the only thinking mode there:
+# Claude 4.5 and earlier reject adaptive thinking and require budget_tokens.
# https://platform.claude.com/docs/en/build-with-claude/extended-thinking
@pytest.mark.parametrize(
"model,effort,budget",
diff --git a/studio/backend/tests/test_studio_tool_loop.py b/studio/backend/tests/test_studio_tool_loop.py
index 0536d61bd11..8fce1cf8d6b 100644
--- a/studio/backend/tests/test_studio_tool_loop.py
+++ b/studio/backend/tests/test_studio_tool_loop.py
@@ -1143,6 +1143,82 @@ def test_gemini_thought_signature_is_replayed_on_the_assistant_turn(executed):
assert json.loads(call["function"]["arguments"]) == {"query": "u"}
+def test_gemini_part_replay_ledgers_accumulate_across_a_tool_turn(executed):
+ transport = FakeTransport(
+ [
+ [
+ _sse(
+ {
+ "reasoning_content": "signed thought",
+ "extra_content": {
+ "google": {
+ "thought": True,
+ "thought_signature": "SIG-T",
+ "thought_part": {
+ "text": "signed thought",
+ "thought_signature": "SIG-T",
+ },
+ }
+ },
+ }
+ ),
+ _sse(
+ {
+ "reasoning_content": " unsigned thought",
+ "extra_content": {
+ "google": {"thought_part": {"text": " unsigned thought"}}
+ },
+ }
+ ),
+ _sse(
+ {
+ "content": "answer ",
+ "extra_content": {"google": {"answer_part": {"text": "answer "}}},
+ }
+ ),
+ _sse(
+ {
+ "content": "tail",
+ "extra_content": {"google": {"answer_part": {"text": "tail"}}},
+ }
+ ),
+ _sse(
+ {
+ "tool_calls": [
+ {
+ "index": 0,
+ "id": "call_a",
+ "function": {
+ "name": "web_search",
+ "arguments": '{"query":"u"}',
+ },
+ }
+ ]
+ }
+ ),
+ _sse(finish = "tool_calls"),
+ _DONE,
+ ],
+ [_sse({"content": "ok"}), _sse(finish = "stop"), _DONE],
+ ],
+ heals = False,
+ )
+ transport.preserves_reasoning = True
+ _run(transport)
+
+ assistant = [
+ message
+ for message in transport.requests[1]["messages"]
+ if message.get("role") == "assistant"
+ ][-1]
+ assert assistant["extra_content"]["google"]["thought_parts"] == [
+ {"text": "signed thought", "thought_signature": "SIG-T"},
+ {"text": " unsigned thought"},
+ ]
+ assert assistant["extra_content"]["google"]["answer_parts"] == [{"text": "answer tail"}]
+ assert "thought_signature" not in assistant["extra_content"]["google"]
+
+
def _call_delta(index, call_id, name, arguments):
return {
"index": index,
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 8ca7ec308e2..b53fd44bd64 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -179,6 +179,11 @@ import {
import { toolStatusKind } from "@/features/chat/utils/tool-status";
import { replySourceMarkdown } from "@/features/chat/utils/reply-source-markdown";
import { toolResultModelText } from "@/features/chat/api/chat-adapter";
+import {
+ collectGeminiAnswerReplayParts,
+ collectGeminiThoughtReplayParts,
+ continuationGeminiReplayTurns,
+} from "@/features/chat/gemini-thought-replay";
import {
CONTINUATION_RUN_CONFIG_KEY,
type ContinuationRequest,
@@ -410,6 +415,7 @@ import {
useEffect,
useId,
useLayoutEffect,
+ useMemo,
useRef,
useState,
useSyncExternalStore,
@@ -7478,16 +7484,41 @@ function useContinuation() {
const reasoning = useAuiState(
({ message }) => readContinuationSource(message.content).reasoning,
);
- // A tool-calling turn cannot be resumed: the continuation runs as a sibling, so the
- // call and its result would be missing from the outbound history.
- const continuable = useAuiState(({ message }) =>
- isContinuableContent(message.content, { thought: thoughtResumable }),
- );
- // Gemini signs its text parts, and the resumed turn is replayed from this branch,
- // so the signature travels with the partial.
+ // Gemini signs answer and thought parts. A continuation runs from a sibling branch,
+ // so both kinds of replay metadata travel with the partial.
const thoughtSignature = useAuiState(({ message }) =>
readTextThoughtSignature(message.content),
);
+ const messageContent = useAuiState(({ message }) => message.content);
+ const thoughtParts = useMemo(
+ () => collectGeminiThoughtReplayParts(messageContent),
+ [messageContent],
+ );
+ const answerParts = useMemo(
+ () => collectGeminiAnswerReplayParts(messageContent),
+ [messageContent],
+ );
+ const geminiReplayTurns = useMemo(
+ () =>
+ continuationGeminiReplayTurns(metadata, {
+ text: partial,
+ ...(thoughtSignature ? { thoughtSignature } : {}),
+ ...(thoughtParts.length > 0 ? { thoughtParts } : {}),
+ ...(answerParts.length > 0 ? { answerParts } : {}),
+ }),
+ [metadata, partial, thoughtSignature, thoughtParts, answerParts],
+ );
+ // A tool-calling turn cannot be resumed: the continuation runs as a sibling, so the
+ // call and its result would be missing from the outbound history. A Gemini ledger is
+ // independently replayable even when its signed thought produced no visible text.
+ const continuable = useMemo(
+ () =>
+ isContinuableContent(messageContent, {
+ thought: thoughtResumable,
+ replay: geminiReplayTurns.length > 0,
+ }),
+ [messageContent, thoughtResumable, geminiReplayTurns],
+ );
// Audio input re-listens to the recording and answers afresh rather than resuming,
// so continuing there would append a second answer.
const fromAudioInput = useAuiState(({ thread }) =>
@@ -7520,7 +7551,11 @@ function useContinuation() {
fromAudioInput,
audioOutputModel,
}) &&
- Boolean(partial.trim() || carriedReasoning.trim());
+ Boolean(
+ partial.trim() ||
+ carriedReasoning.trim() ||
+ geminiReplayTurns.length > 0,
+ );
const reasoningDuration = readThoughtDuration(metadata);
// Hands the started run back, untyped: the only handle identified with THIS run.
@@ -7536,6 +7571,9 @@ function useContinuation() {
partial,
...(carriedReasoning ? { reasoning: carriedReasoning, reasoningDuration } : {}),
...(thoughtSignature ? { thoughtSignature } : {}),
+ ...(thoughtParts.length > 0 ? { thoughtParts } : {}),
+ ...(answerParts.length > 0 ? { answerParts } : {}),
+ ...(geminiReplayTurns.length > 0 ? { geminiReplayTurns } : {}),
...providerCompactionContinuationFields(metadata),
};
return aui.thread().startRun({
@@ -7551,6 +7589,9 @@ function useContinuation() {
carriedReasoning,
reasoningDuration,
thoughtSignature,
+ thoughtParts,
+ answerParts,
+ geminiReplayTurns,
metadata,
]);
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 80ebef152a6..a22c43abf51 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -177,6 +177,25 @@ import {
startsNewCodexToolRound,
type CodexReasoningLedger,
} from "../codex-reasoning";
+import {
+ type GeminiContinuationReplay,
+ type GeminiContinuationReplayTurn,
+ type GeminiAnswerReplayPart,
+ type GeminiThoughtReplayPart,
+ type PositionedGeminiAnswerReplayPart,
+ type PositionedGeminiThoughtReplayPart,
+ appendGeminiAnswerReplayPart,
+ appendGeminiThoughtReplayPart,
+ geminiAnswerReplayParts,
+ geminiContinuationReplayEntries,
+ geminiThoughtReplayParts,
+ pinGeminiAnswerReplayParts,
+ pinGeminiTextThoughtSignature,
+ pinGeminiThoughtReplayParts,
+ readGeminiContinuationReplay,
+ withGeminiAnswerReplayParts,
+ withGeminiThoughtReplayParts,
+} from "../gemini-thought-replay";
import {
createBoundaryScan,
@@ -338,6 +357,7 @@ import {
incompleteLabel,
incompleteReasonAfterError,
type IncompleteReason,
+ isRestart,
noteRunStartedThisSession,
readIncompleteInfo,
resolveIncompleteReason,
@@ -1374,6 +1394,87 @@ function attachAssistantThoughtSignature(
}
}
+function serializeGeminiContinuationTurns(
+ turns: GeminiContinuationReplayTurn[],
+ includeTrailingInstruction: boolean,
+): SerializedMessage[] {
+ const messages: SerializedMessage[] = [];
+ for (const entry of geminiContinuationReplayEntries(
+ turns,
+ includeTrailingInstruction,
+ )) {
+ if (entry.role === "user") {
+ messages.push({ role: "user", content: CONTINUE_INSTRUCTION });
+ continue;
+ }
+ const { turn } = entry;
+ const assistant: SerializedMessage = {
+ role: "assistant",
+ content: turn.text,
+ ...(turn.thoughtParts?.length || turn.answerParts?.length
+ ? {
+ extra_content: withGeminiAnswerReplayParts(
+ withGeminiThoughtReplayParts(undefined, turn.thoughtParts ?? []),
+ turn.answerParts ?? [],
+ ),
+ }
+ : {}),
+ };
+ attachAssistantThoughtSignature([assistant], turn.thoughtSignature);
+ messages.push(assistant);
+ }
+ return messages;
+}
+
+function stripGeminiContinuationVisiblePrefix(
+ messages: SerializedMessage[],
+ visiblePrefix: string,
+): void {
+ if (!visiblePrefix) return;
+ const assistant = messages.find((message) => message.role === "assistant");
+ if (!assistant) return;
+ if (typeof assistant.content === "string") {
+ if (assistant.content.startsWith(visiblePrefix)) {
+ assistant.content = assistant.content.slice(visiblePrefix.length);
+ }
+ return;
+ }
+ if (!Array.isArray(assistant.content)) return;
+ const content = assistant.content as Array<Record<string, unknown>>;
+ const text = content
+ .filter((part) => part.type === "text" && typeof part.text === "string")
+ .map((part) => part.text as string)
+ .join("");
+ if (!text.startsWith(visiblePrefix)) return;
+ let remaining = visiblePrefix.length;
+ assistant.content = content.flatMap((part) => {
+ if (remaining === 0 || part.type !== "text" || typeof part.text !== "string") {
+ return [part];
+ }
+ const drop = Math.min(remaining, part.text.length);
+ remaining -= drop;
+ const trimmed = part.text.slice(drop);
+ return trimmed ? [{ ...part, text: trimmed }] : [];
+ }) as OpenAIMessageContent;
+}
+
+function expandGeminiContinuationReplay(
+ message: RunMessage,
+ serialized: SerializedMessage[],
+): SerializedMessage[] {
+ const replay = readGeminiContinuationReplay(
+ (message as { metadata?: unknown }).metadata,
+ );
+ if (!replay) return serialized;
+ if (replay.stripVisiblePrefix) {
+ stripGeminiContinuationVisiblePrefix(serialized, replay.visiblePrefix);
+ }
+ return [
+ ...serializeGeminiContinuationTurns(replay.turns, true),
+ ...serialized,
+ ];
+}
+
function serializeAssistantReplayMessages(
message: RunMessage,
includeReasoningContent = false,
@@ -1396,6 +1497,8 @@ function serializeAssistantReplayMessages(
const pendingReasoningParts: string[] = [];
let pendingToolCalls: SerializedToolCall[] = [];
let pendingToolResults: SerializedToolResult[] = [];
+ let pendingGeminiThoughtParts: GeminiThoughtReplayPart[] = [];
+ let pendingGeminiAnswerParts: GeminiAnswerReplayPart[] = [];
let imagePartsPending = imageParts.length > 0;
let pendingLocalToolRoundId: number | null = null;
@@ -1407,6 +1510,8 @@ function serializeAssistantReplayMessages(
const includeImageParts = imagePartsPending ? imageParts : [];
const hasContent = textContent.length > 0 || includeImageParts.length > 0;
const hasToolCalls = pendingToolCalls.length > 0;
+ const hasGeminiThoughtParts = pendingGeminiThoughtParts.length > 0;
+ const hasGeminiAnswerParts = pendingGeminiAnswerParts.length > 0;
const reasoningContent = pendingReasoningParts.join("\n");
const incomplete =
message.status?.type === "incomplete" ||
@@ -1419,7 +1524,14 @@ function serializeAssistantReplayMessages(
incomplete,
});
- if (!force && !hasContent && !hasToolCalls && !hasReasoningContent) {
+ if (
+ !force &&
+ !hasContent &&
+ !hasToolCalls &&
+ !hasReasoningContent &&
+ !hasGeminiThoughtParts &&
+ !hasGeminiAnswerParts
+ ) {
return;
}
@@ -1439,6 +1551,18 @@ function serializeAssistantReplayMessages(
if (hasReasoningContent) {
assistantMessage.reasoning_content = reasoningContent;
}
+ if (hasGeminiThoughtParts) {
+ assistantMessage.extra_content = withGeminiThoughtReplayParts(
+ assistantMessage.extra_content,
+ pendingGeminiThoughtParts,
+ );
+ }
+ if (hasGeminiAnswerParts) {
+ assistantMessage.extra_content = withGeminiAnswerReplayParts(
+ assistantMessage.extra_content,
+ pendingGeminiAnswerParts,
+ );
+ }
if (hasToolCalls) {
setAssistantCodexReasoning(
@@ -1466,6 +1590,8 @@ function serializeAssistantReplayMessages(
pendingReasoningParts.length = 0;
pendingToolCalls = [];
pendingToolResults = [];
+ pendingGeminiThoughtParts = [];
+ pendingGeminiAnswerParts = [];
imagePartsPending = false;
pendingLocalToolRoundId = null;
@@ -1476,6 +1602,7 @@ function serializeAssistantReplayMessages(
if (pendingToolCalls.length > 0) {
flushAssistantAndToolResults();
}
+ pendingGeminiThoughtParts.push(...geminiThoughtReplayParts(part));
pendingReasoningParts.push(part.text);
continue;
}
@@ -1484,6 +1611,8 @@ function serializeAssistantReplayMessages(
if (pendingToolCalls.length > 0) {
flushAssistantAndToolResults();
}
+ pendingGeminiThoughtParts.push(...geminiThoughtReplayParts(part));
+ pendingGeminiAnswerParts.push(...geminiAnswerReplayParts(part));
pendingTextParts.push(part.text);
continue;
}
@@ -1544,6 +1673,7 @@ function serializeAssistantReplayMessages(
function toOpenAIMessages(
message: RunMessage,
includeReasoningContent = false,
+ expandGeminiContinuations = false,
): SerializedMessage[] {
message = modelVisibleMessage(message);
if (
@@ -1555,10 +1685,13 @@ function toOpenAIMessages(
}
if (message.role === "assistant") {
- return fillStoppedAssistantReplay(
+ const serialized = fillStoppedAssistantReplay(
message,
serializeAssistantReplayMessages(message, includeReasoningContent),
);
+ return expandGeminiContinuations
+ ? expandGeminiContinuationReplay(message, serialized)
+ : serialized;
}
const textContent = collectTextParts(message).join("\n");
@@ -5106,7 +5239,11 @@ export function createOpenAIStreamAdapter(
// translator rebuilds the functionCall/functionResponse parts.
let outboundMessages = renderedMessages
.flatMap((message) => {
- const serialized = toOpenAIMessages(message, replayReasoning);
+ const serialized = toOpenAIMessages(
+ message,
+ replayReasoning,
+ externalProvider?.providerType === "gemini",
+ );
return isExternalRequest
? withProviderCompaction(message, serialized, {
providerType: toExternalBackendProviderType(
@@ -5160,7 +5297,20 @@ export function createOpenAIStreamAdapter(
})
? (continuation.reasoning ?? "")
: "";
- if (continuation && !continuation.partial && !resumedThought) {
+ const hasGeminiReplayContinuation = Boolean(
+ continuation &&
+ externalProvider?.providerType === "gemini" &&
+ (continuation.geminiReplayTurns?.length ||
+ continuation.thoughtParts?.length ||
+ continuation.answerParts?.length ||
+ continuation.thoughtSignature),
+ );
+ if (
+ continuation &&
+ !continuation.partial &&
+ !resumedThought &&
+ !hasGeminiReplayContinuation
+ ) {
toast.error("This response cannot be resumed", {
description:
"It stopped mid-thought, and only GGUF and MLX models can resume a thought. Use Retry instead.",
@@ -5177,24 +5327,40 @@ export function createOpenAIStreamAdapter(
: null;
// The run's messages stop at the user turn, so the partial is appended here for the backend to resume.
if (continuation) {
- outboundMessages.push({
- role: "assistant",
- content: continuationCompaction
- ? [
- continuationCompaction,
- ...(continuation.partial
- ? [{ type: "text" as const, text: continuation.partial }]
- : []),
- ]
- : continuation.partial,
- ...(resumedThought ? { reasoning_content: resumedThought } : {}),
- });
- // The original assistant message is not in this branch, so without its signature the history
- // goes back unsigned.
- attachAssistantThoughtSignature(
- outboundMessages,
- continuation.thoughtSignature,
- );
+ const geminiReplayTurns =
+ externalProvider?.providerType === "gemini"
+ ? continuation.geminiReplayTurns
+ : undefined;
+ if (geminiReplayTurns?.length) {
+ outboundMessages.push(
+ ...serializeGeminiContinuationTurns(geminiReplayTurns, false),
+ );
+ } else {
+ outboundMessages.push({
+ role: "assistant",
+ content: continuationCompaction
+ ? [
+ continuationCompaction,
+ ...(continuation.partial
+ ? [{ type: "text" as const, text: continuation.partial }]
+ : []),
+ ]
+ : continuation.partial,
+ ...(resumedThought ? { reasoning_content: resumedThought } : {}),
+ ...(continuation.thoughtParts && continuation.thoughtParts.length > 0
+ ? {
+ extra_content: withGeminiThoughtReplayParts(
+ undefined,
+ continuation.thoughtParts,
+ ),
+ }
+ : {}),
+ });
+ attachAssistantThoughtSignature(
+ outboundMessages,
+ continuation.thoughtSignature,
+ );
+ }
// Anthropic 400s when the last message is an assistant turn, so append an instruction turn
// instead of a prefill.
if (rejectsAssistantPrefill(externalProvider?.providerType)) {
@@ -5592,9 +5758,28 @@ export function createOpenAIStreamAdapter(
const continuationPartial = continuation
? continuationSeed(continuation.partial, resumedThought)
: "";
+ const geminiContinuationReplayTurns =
+ continuation?.geminiReplayTurns?.length
+ ? continuation.geminiReplayTurns
+ : undefined;
// A seed that ends inside the thought: the next reasoning delta extends it.
const resumesInsideThought = Boolean(resumedThought) && !continuation?.partial;
let cumulativeText = continuationPartial;
+ const geminiContinuationReplay = (
+ final: boolean,
+ ): GeminiContinuationReplay | undefined =>
+ geminiContinuationReplayTurns
+ ? {
+ turns: geminiContinuationReplayTurns,
+ visiblePrefix: continuation?.partial ?? "",
+ stripVisiblePrefix:
+ !final ||
+ !isRestart(
+ continuation?.partial ?? "",
+ cumulativeText.slice(continuationPartial.length),
+ ),
+ }
+ : undefined;
// Reading `cumulativeText` costs O(reply): each `+=` builds a cons string that the first read
// flattens, so one charCodeAt per arrival is as expensive as a scan. Everything below is fed
// the delta through `appendCumulative` and the buffer is read only where the reply is
@@ -5694,6 +5879,7 @@ export function createOpenAIStreamAdapter(
providerCompactionProviderType,
providerCompactionModelId,
providerCompactionConnectionKey: providerCompactionOriginConnectionKey,
+ geminiContinuationReplay: geminiContinuationReplay(false),
// A legacy (browser-tool / attachment / incognito) run that ends because you closed the tab has no
// server-side run to resume from, so its last streamed yield is what persists. Mark it an interruption
// — partial kept + Resume — instead of a silent blank/ambiguous state. Durable runs keep "cancelled":
@@ -6000,24 +6186,21 @@ export function createOpenAIStreamAdapter(
toolCallParts[toolCallParts.length - 1]?.toolCallId ?? "",
() => `${backendToolCallId}:${crypto.randomUUID()}`,
);
- // Latest Gemini text-part thoughtSignature, pinned onto the final text MessagePart so the
- // next turn's replay carries it.
- let latestTextThoughtSignature: string | undefined;
- const pinTextThoughtSignature = <T extends { type: string }>(
- parts: T[],
- ): T[] => {
- if (!latestTextThoughtSignature || parts.length === 0) return parts;
- for (let i = parts.length - 1; i >= 0; i -= 1) {
- if (parts[i].type === "text") {
- parts[i] = {
- ...parts[i],
- _google_thought_signature: latestTextThoughtSignature,
- } as T;
- break;
- }
- }
- return parts;
- };
+ let legacyGeminiTextSignature: string | undefined;
+ const geminiThoughtParts: PositionedGeminiThoughtReplayPart[] =
+ geminiContinuationReplayTurns
+ ? []
+ : (continuation?.thoughtParts?.map((part) => ({
+ ...part,
+ afterToolCalls: 0,
+ })) ?? []);
+ const geminiAnswerParts: PositionedGeminiAnswerReplayPart[] =
+ geminiContinuationReplayTurns
+ ? []
+ : (continuation?.answerParts?.map((part) => ({
+ ...part,
+ afterToolCalls: 0,
+ })) ?? []);
const buildAssistantContent = (rawText: string) => {
const positionedTools = toolCallParts
.map((part, index) => {
@@ -6060,7 +6243,14 @@ export function createOpenAIStreamAdapter(
}
assembled.push(...runs[boundaries.length]);
- return pinTextThoughtSignature(assembled);
+ pinGeminiThoughtReplayParts(assembled, geminiThoughtParts);
+ pinGeminiAnswerReplayParts(assembled, geminiAnswerParts);
+ return pinGeminiTextThoughtSignature(
+ assembled,
+ geminiAnswerParts.length > 0
+ ? undefined
+ : legacyGeminiTextSignature,
+ );
};
const {
@@ -7591,6 +7781,9 @@ export function createOpenAIStreamAdapter(
// Replay state reaches the message only through a yield, so a Stop while the gate holds one
// persists a turn that cannot replay. Pace previews, never state.
let replayStateChanged = false;
+ let geminiReplayStateChanged = false;
+ let geminiThoughtSignature: string | undefined;
+ let capturedGeminiThoughtPart = false;
if (deltaExtraContent && typeof deltaExtraContent === "object") {
const extraRecord = deltaExtraContent as Record<
string,
@@ -7598,11 +7791,71 @@ export function createOpenAIStreamAdapter(
>;
const eGoogle = extraRecord.google;
if (eGoogle && typeof eGoogle === "object") {
- const sig = (eGoogle as Record<string, unknown>)
- .thought_signature;
+ const googleRecord = eGoogle as Record<string, unknown>;
+ const sig = googleRecord.thought_signature;
+ const thoughtPart = googleRecord.thought_part;
+ const answerPart = googleRecord.answer_part;
+ if (
+ thoughtPart &&
+ typeof thoughtPart === "object" &&
+ !Array.isArray(thoughtPart)
+ ) {
+ const thoughtRecord = thoughtPart as Record<string, unknown>;
+ const thoughtText = thoughtRecord.text;
+ const thoughtSignature = thoughtRecord.thought_signature;
+ if (
+ typeof thoughtText === "string" &&
+ (thoughtSignature === undefined ||
+ (typeof thoughtSignature === "string" && thoughtSignature))
+ ) {
+ appendGeminiThoughtReplayPart(
+ geminiThoughtParts,
+ thoughtText,
+ typeof thoughtSignature === "string"
+ ? thoughtSignature
+ : undefined,
+ toolCallParts.length,
+ );
+ capturedGeminiThoughtPart = true;
+ geminiReplayStateChanged = true;
+ }
+ }
+ let capturedAnswerPart = false;
+ if (
+ answerPart &&
+ typeof answerPart === "object" &&
+ !Array.isArray(answerPart)
+ ) {
+ const answerRecord = answerPart as Record<string, unknown>;
+ const answerText = answerRecord.text;
+ const answerSignature = answerRecord.thought_signature;
+ if (
+ typeof answerText === "string" &&
+ (answerSignature === undefined ||
+ (typeof answerSignature === "string" && answerSignature))
+ ) {
+ appendGeminiAnswerReplayPart(
+ geminiAnswerParts,
+ {
+ text: answerText,
+ ...(typeof answerSignature === "string"
+ ? { thoughtSignature: answerSignature }
+ : {}),
+ },
+ toolCallParts.length,
+ );
+ capturedAnswerPart = true;
+ geminiReplayStateChanged = true;
+ }
+ }
if (typeof sig === "string" && sig) {
- replayStateChanged ||= sig !== latestTextThoughtSignature;
- latestTextThoughtSignature = sig;
+ const belongsToThought = googleRecord.thought === true;
+ if (belongsToThought) {
+ geminiThoughtSignature = sig;
+ } else if (!capturedAnswerPart) {
+ geminiReplayStateChanged ||= sig !== legacyGeminiTextSignature;
+ legacyGeminiTextSignature = sig;
+ }
}
}
const codexReasoning = extraRecord.openai_codex_reasoning;
@@ -7659,6 +7912,23 @@ export function createOpenAIStreamAdapter(
const reasoning =
(typeof rawReasoning === "string" ? rawReasoning : "") +
reasoningFromDetails;
+ if (
+ !capturedGeminiThoughtPart &&
+ externalProvider?.providerType === "gemini" &&
+ (reasoning || geminiThoughtSignature)
+ ) {
+ appendGeminiThoughtReplayPart(
+ geminiThoughtParts,
+ reasoning,
+ geminiThoughtSignature,
+ toolCallParts.length,
+ );
+ geminiReplayStateChanged = true;
+ }
+ // Text-bearing replay metadata is published with the same paced preview as its visible delta.
+ // Metadata-only signature parts still need an immediate yield or Stop could lose them.
+ replayStateChanged ||=
+ geminiReplayStateChanged && !delta && !reasoning;
// OpenAI delta.tool_calls streams fragments by index; accumulate into one part. extra_content
// carries the Gemini 3 thoughtSignature.
const rawDeltaToolCalls = (
@@ -8467,6 +8737,7 @@ export function createOpenAIStreamAdapter(
providerCompactionModelId,
providerCompactionConnectionKey:
providerCompactionOriginConnectionKey,
+ geminiContinuationReplay: geminiContinuationReplay(true),
incomplete: finalIncompleteReason
? { reason: finalIncompleteReason }
: undefined,
@@ -8645,6 +8916,7 @@ export function createOpenAIStreamAdapter(
providerCompactionModelId,
providerCompactionConnectionKey:
providerCompactionOriginConnectionKey,
+ geminiContinuationReplay: geminiContinuationReplay(true),
// Unfinished too, so it also offers Continue -- unless the provider already
// said why the model stopped.
incomplete: {
diff --git a/studio/frontend/src/features/chat/gemini-thought-replay.ts b/studio/frontend/src/features/chat/gemini-thought-replay.ts
new file mode 100644
index 00000000000..0bc1ae5c84e
--- /dev/null
+++ b/studio/frontend/src/features/chat/gemini-thought-replay.ts
@@ -0,0 +1,442 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+export type GeminiThoughtReplayPart = {
+ text: string;
+ thoughtSignature?: string;
+};
+
+export type PositionedGeminiThoughtReplayPart = GeminiThoughtReplayPart & {
+ afterToolCalls: number;
+};
+
+export type GeminiAnswerReplayPart = {
+ text: string;
+ thoughtSignature?: string;
+};
+
+export type PositionedGeminiAnswerReplayPart = GeminiAnswerReplayPart & {
+ afterToolCalls: number;
+};
+
+export type GeminiContinuationReplayTurn = {
+ text: string;
+ thoughtSignature?: string;
+ thoughtParts?: GeminiThoughtReplayPart[];
+ answerParts?: GeminiAnswerReplayPart[];
+};
+
+export type GeminiContinuationReplay = {
+ turns: GeminiContinuationReplayTurn[];
+ visiblePrefix: string;
+ stripVisiblePrefix: boolean;
+};
+
+export type GeminiContinuationReplayEntry =
+ | { role: "assistant"; turn: GeminiContinuationReplayTurn }
+ | { role: "user" };
+
+type ReplayableMessagePart = {
+ type: string;
+ text?: unknown;
+ _google_thought_signature?: unknown;
+ _google_thought_parts?: unknown;
+ _google_answer_parts?: unknown;
+};
+
+export function appendGeminiThoughtReplayPart(
+ parts: PositionedGeminiThoughtReplayPart[],
+ text: string,
+ thoughtSignature: string | undefined,
+ afterToolCalls: number,
+): void {
+ const latestPart = parts.at(-1);
+ if (
+ !thoughtSignature &&
+ !latestPart?.thoughtSignature &&
+ latestPart?.afterToolCalls === afterToolCalls
+ ) {
+ latestPart.text += text;
+ return;
+ }
+ parts.push({
+ text,
+ ...(thoughtSignature ? { thoughtSignature } : {}),
+ afterToolCalls,
+ });
+}
+
+export function pinGeminiTextThoughtSignature<T extends { type: string }>(
+ parts: T[],
+ thoughtSignature: string | undefined,
+): T[] {
+ if (!thoughtSignature || parts.length === 0) {
+ return parts;
+ }
+ for (let index = parts.length - 1; index >= 0; index -= 1) {
+ if (parts[index].type !== "text") {
+ continue;
+ }
+ parts[index] = {
+ ...parts[index],
+ _google_thought_signature: thoughtSignature,
+ } as T;
+ break;
+ }
+ return parts;
+}
+
+export function appendGeminiAnswerReplayPart(
+ parts: PositionedGeminiAnswerReplayPart[],
+ part: GeminiAnswerReplayPart,
+ afterToolCalls: number,
+): void {
+ const latestPart = parts.at(-1);
+ if (
+ !part.thoughtSignature &&
+ !latestPart?.thoughtSignature &&
+ latestPart?.afterToolCalls === afterToolCalls
+ ) {
+ latestPart.text += part.text;
+ return;
+ }
+ parts.push({ ...part, afterToolCalls });
+}
+
+function replayPlacement<T extends { type: string }>(
+ parts: T[],
+ afterToolCalls: number,
+): { targetIndex: number; insertIndex: number } {
+ let toolCallsSeen = 0;
+ let reasoningIndex = -1;
+ let textIndex = -1;
+ let insertIndex = parts.length;
+ for (let index = 0; index < parts.length; index += 1) {
+ const type = parts[index].type;
+ if (type === "tool-call") {
+ if (toolCallsSeen === afterToolCalls) {
+ insertIndex = index;
+ }
+ toolCallsSeen += 1;
+ } else if (toolCallsSeen === afterToolCalls) {
+ if (type === "reasoning") {
+ reasoningIndex = index;
+ } else if (type === "text") {
+ textIndex = index;
+ }
+ insertIndex = index + 1;
+ }
+ }
+ return {
+ targetIndex: reasoningIndex === -1 ? textIndex : reasoningIndex,
+ insertIndex,
+ };
+}
+
+export function pinGeminiThoughtReplayParts<T extends { type: string }>(
+ parts: T[],
+ thoughtParts: PositionedGeminiThoughtReplayPart[],
+): T[] {
+ if (thoughtParts.length === 0) {
+ return parts;
+ }
+ const byToolRound = new Map<number, GeminiThoughtReplayPart[]>();
+ for (const { afterToolCalls, text, thoughtSignature } of thoughtParts) {
+ const round = byToolRound.get(afterToolCalls) ?? [];
+ round.push({ text, ...(thoughtSignature ? { thoughtSignature } : {}) });
+ byToolRound.set(afterToolCalls, round);
+ }
+ for (const [afterToolCalls, replayMetadata] of byToolRound) {
+ const { targetIndex, insertIndex } = replayPlacement(parts, afterToolCalls);
+ if (targetIndex === -1) {
+ parts.splice(insertIndex, 0, {
+ type: "reasoning",
+ text: "",
+ _google_thought_parts: replayMetadata,
+ } as unknown as T);
+ } else {
+ parts[targetIndex] = {
+ ...parts[targetIndex],
+ _google_thought_parts: replayMetadata,
+ } as T;
+ }
+ }
+ return parts;
+}
+
+export function pinGeminiAnswerReplayParts<T extends { type: string }>(
+ parts: T[],
+ answerParts: PositionedGeminiAnswerReplayPart[],
+): T[] {
+ if (answerParts.length === 0) return parts;
+ const byToolRound = new Map<number, GeminiAnswerReplayPart[]>();
+ for (const { afterToolCalls, text, thoughtSignature } of answerParts) {
+ const round = byToolRound.get(afterToolCalls) ?? [];
+ round.push({ text, ...(thoughtSignature ? { thoughtSignature } : {}) });
+ byToolRound.set(afterToolCalls, round);
+ }
+ for (const [afterToolCalls, replayMetadata] of byToolRound) {
+ let toolCallsSeen = 0;
+ let targetIndex = -1;
+ let insertIndex = parts.length;
+ for (let index = 0; index < parts.length; index += 1) {
+ const type = parts[index].type;
+ if (type === "tool-call") {
+ if (toolCallsSeen === afterToolCalls) insertIndex = index;
+ toolCallsSeen += 1;
+ } else if (toolCallsSeen === afterToolCalls) {
+ if (type === "text") targetIndex = index;
+ insertIndex = index + 1;
+ }
+ }
+ if (targetIndex === -1) {
+ parts.splice(insertIndex, 0, {
+ type: "text",
+ text: "",
+ _google_answer_parts: replayMetadata,
+ } as unknown as T);
+ } else {
+ parts[targetIndex] = {
+ ...parts[targetIndex],
+ _google_answer_parts: replayMetadata,
+ } as T;
+ }
+ }
+ return parts;
+}
+
+export function parseGeminiThoughtReplayParts(
+ value: unknown,
+): GeminiThoughtReplayPart[] {
+ if (!Array.isArray(value)) {
+ return [];
+ }
+ return value.flatMap((entry) => {
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
+ return [];
+ }
+ const record = entry as Record<string, unknown>;
+ if (typeof record.text !== "string") return [];
+ const thoughtSignature = record.thoughtSignature;
+ if (
+ thoughtSignature !== undefined &&
+ (typeof thoughtSignature !== "string" || !thoughtSignature)
+ ) {
+ return [];
+ }
+ return [{
+ text: record.text,
+ ...(typeof thoughtSignature === "string" ? { thoughtSignature } : {}),
+ }];
+ });
+}
+
+export function parseGeminiAnswerReplayParts(
+ value: unknown,
+): GeminiAnswerReplayPart[] {
+ if (!Array.isArray(value)) return [];
+ return value.flatMap((entry) => {
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) return [];
+ const record = entry as Record<string, unknown>;
+ if (typeof record.text !== "string") return [];
+ const thoughtSignature = record.thoughtSignature;
+ if (
+ thoughtSignature !== undefined &&
+ (typeof thoughtSignature !== "string" || !thoughtSignature)
+ ) {
+ return [];
+ }
+ return [{
+ text: record.text,
+ ...(typeof thoughtSignature === "string" ? { thoughtSignature } : {}),
+ }];
+ });
+}
+
+export function geminiThoughtReplayParts(
+ part: ReplayableMessagePart,
+): GeminiThoughtReplayPart[] {
+ return parseGeminiThoughtReplayParts(part._google_thought_parts);
+}
+
+export function collectGeminiThoughtReplayParts(
+ content: readonly unknown[] | undefined,
+): GeminiThoughtReplayPart[] {
+ if (!content) {
+ return [];
+ }
+ return content.flatMap((part) =>
+ part && typeof part === "object" && !Array.isArray(part)
+ ? geminiThoughtReplayParts(part as ReplayableMessagePart)
+ : [],
+ );
+}
+
+export function geminiAnswerReplayParts(
+ part: ReplayableMessagePart,
+): GeminiAnswerReplayPart[] {
+ return parseGeminiAnswerReplayParts(part._google_answer_parts);
+}
+
+export function collectGeminiAnswerReplayParts(
+ content: readonly unknown[] | undefined,
+): GeminiAnswerReplayPart[] {
+ if (!content) return [];
+ return content.flatMap((part) =>
+ part && typeof part === "object" && !Array.isArray(part)
+ ? geminiAnswerReplayParts(part as ReplayableMessagePart)
+ : [],
+ );
+}
+
+function parseGeminiContinuationReplayTurn(
+ value: unknown,
+): GeminiContinuationReplayTurn | null {
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
+ return null;
+ }
+ const record = value as Record<string, unknown>;
+ if (typeof record.text !== "string") {
+ return null;
+ }
+ const thoughtParts = parseGeminiThoughtReplayParts(record.thoughtParts);
+ const answerParts = parseGeminiAnswerReplayParts(record.answerParts);
+ const thoughtSignature = record.thoughtSignature;
+ return {
+ text: record.text,
+ ...(typeof thoughtSignature === "string" && thoughtSignature
+ ? { thoughtSignature }
+ : {}),
+ ...(thoughtParts.length > 0 ? { thoughtParts } : {}),
+ ...(answerParts.length > 0 ? { answerParts } : {}),
+ };
+}
+
+export function parseGeminiContinuationReplayTurns(
+ value: unknown,
+): GeminiContinuationReplayTurn[] {
+ if (!Array.isArray(value)) {
+ return [];
+ }
+ return value.flatMap((entry) => {
+ const turn = parseGeminiContinuationReplayTurn(entry);
+ return turn ? [turn] : [];
+ });
+}
+
+export function readGeminiContinuationReplay(
+ metadata: unknown,
+): GeminiContinuationReplay | null {
+ const custom = (metadata as { custom?: Record<string, unknown> } | undefined)
+ ?.custom;
+ const replay = custom?.geminiContinuationReplay as
+ | {
+ turns?: unknown;
+ visiblePrefix?: unknown;
+ stripVisiblePrefix?: unknown;
+ }
+ | undefined;
+ const turns = parseGeminiContinuationReplayTurns(replay?.turns);
+ return turns.length > 0 && typeof replay?.visiblePrefix === "string"
+ ? {
+ turns,
+ visiblePrefix: replay.visiblePrefix,
+ stripVisiblePrefix: replay.stripVisiblePrefix !== false,
+ }
+ : null;
+}
+
+export function continuationGeminiReplayTurns(
+ metadata: unknown,
+ current: GeminiContinuationReplayTurn,
+): GeminiContinuationReplayTurn[] {
+ const replay = readGeminiContinuationReplay(metadata);
+ const hasSignedCurrentPart = Boolean(
+ current.thoughtSignature ||
+ (current.thoughtParts?.length ?? 0) > 0 ||
+ (current.answerParts?.length ?? 0) > 0,
+ );
+ if (!replay && !hasSignedCurrentPart) {
+ return [];
+ }
+ const currentText =
+ replay?.stripVisiblePrefix && current.text.startsWith(replay.visiblePrefix)
+ ? current.text.slice(replay.visiblePrefix.length)
+ : current.text;
+ const currentTurn = { ...current, text: currentText };
+ const hasCurrentTurn = Boolean(
+ currentTurn.text ||
+ currentTurn.thoughtSignature ||
+ (currentTurn.thoughtParts?.length ?? 0) > 0 ||
+ (currentTurn.answerParts?.length ?? 0) > 0,
+ );
+ return [...(replay?.turns ?? []), ...(hasCurrentTurn ? [currentTurn] : [])];
+}
+
+export function geminiContinuationReplayEntries(
+ turns: GeminiContinuationReplayTurn[],
+ includeTrailingUser: boolean,
+): GeminiContinuationReplayEntry[] {
+ return turns.flatMap((turn, index) => [
+ { role: "assistant" as const, turn },
+ ...(includeTrailingUser || index < turns.length - 1
+ ? [{ role: "user" as const }]
+ : []),
+ ]);
+}
+
+export function withGeminiThoughtReplayParts(
+ extraContent: unknown,
+ thoughtParts: GeminiThoughtReplayPart[],
+): unknown {
+ if (thoughtParts.length === 0) {
+ return extraContent;
+ }
+ const extra =
+ extraContent &&
+ typeof extraContent === "object" &&
+ !Array.isArray(extraContent)
+ ? (extraContent as Record<string, unknown>)
+ : {};
+ const google =
+ extra.google &&
+ typeof extra.google === "object" &&
+ !Array.isArray(extra.google)
+ ? (extra.google as Record<string, unknown>)
+ : {};
+ return {
+ ...extra,
+ google: {
+ ...google,
+ thought_parts: thoughtParts.map((part) => ({
+ text: part.text,
+ ...(part.thoughtSignature ? { thought_signature: part.thoughtSignature } : {}),
+ })),
+ },
+ };
+}
+
+export function withGeminiAnswerReplayParts(
+ extraContent: unknown,
+ answerParts: GeminiAnswerReplayPart[],
+): unknown {
+ if (answerParts.length === 0) return extraContent;
+ const extra =
+ extraContent && typeof extraContent === "object" && !Array.isArray(extraContent)
+ ? (extraContent as Record<string, unknown>)
+ : {};
+ const google =
+ extra.google && typeof extra.google === "object" && !Array.isArray(extra.google)
+ ? (extra.google as Record<string, unknown>)
+ : {};
+ return {
+ ...extra,
+ google: {
+ ...google,
+ answer_parts: answerParts.map((part) => ({
+ text: part.text,
+ ...(part.thoughtSignature ? { thought_signature: part.thoughtSignature } : {}),
+ })),
+ },
+ };
+}
diff --git a/studio/frontend/src/features/chat/utils/continuation.ts b/studio/frontend/src/features/chat/utils/continuation.ts
index d34835f4f65..43697353c2a 100644
--- a/studio/frontend/src/features/chat/utils/continuation.ts
+++ b/studio/frontend/src/features/chat/utils/continuation.ts
@@ -1,6 +1,14 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+import {
+ type GeminiAnswerReplayPart,
+ type GeminiContinuationReplayTurn,
+ type GeminiThoughtReplayPart,
+ parseGeminiAnswerReplayParts,
+ parseGeminiContinuationReplayTurns,
+ parseGeminiThoughtReplayParts,
+} from "../gemini-thought-replay.ts";
import type { ProviderCompactionContentPart } from "../types/api";
import { providerCompactionPart } from "./provider-compaction.ts";
@@ -26,14 +34,17 @@ export type IncompleteInfo = {
};
/** Whether a finished turn left anything on screen, which is what separates `empty` from a
- * real answer. Non-text parts (tool calls, images, sources) always count; text has to be
- * more than whitespace. */
+ * real answer. Structured parts (tool calls, images, sources) always count; text and
+ * reasoning have to contain more than whitespace. */
export function hasRenderableContent(
content: readonly { type: string; text?: string }[],
): boolean {
- return content.some(
- (part) => part.type !== "text" || (part.text ?? "").trim().length > 0,
- );
+ return content.some((part) => {
+ if (part.type === "text" || part.type === "reasoning") {
+ return (part.text ?? "").trim().length > 0;
+ }
+ return true;
+ });
}
const INCOMPLETE_REASONS: readonly IncompleteReason[] = [
@@ -247,10 +258,13 @@ export function budgetImpliesTruncation({
/** Mirrors the backend guard: tool calls block; reasoning-only needs `thought`. */
export function isContinuableContent(
content: readonly unknown[] | undefined,
- { thought = false }: { thought?: boolean } = {},
+ {
+ thought = false,
+ replay = false,
+ }: { thought?: boolean; replay?: boolean } = {},
): boolean {
if (!content) {
- return false;
+ return replay;
}
let hasText = false;
let hasReasoning = false;
@@ -271,7 +285,7 @@ export function isContinuableContent(
}
return false;
}
- return hasText || (thought && hasReasoning);
+ return hasText || (thought && hasReasoning) || replay;
}
/** Reasoning is kept only when it all precedes the answer, as reasoning_content does. */
@@ -380,6 +394,12 @@ export type ContinuationRequest = {
/** Gemini text-part thoughtSignature from the turn being resumed: the sibling run drops the
* original assistant message, so replaying it here keeps the history signed. */
thoughtSignature?: string;
+ /** Signed Gemini thought-summary parts from the turn being resumed. */
+ thoughtParts?: GeminiThoughtReplayPart[];
+ /** Exact Gemini answer-part boundaries from the turn being resumed. */
+ answerParts?: GeminiAnswerReplayPart[];
+ /** Gemini responses hidden behind the merged continuation bubble, in provider order. */
+ geminiReplayTurns?: GeminiContinuationReplayTurn[];
providerCompaction?: ProviderCompactionContentPart;
providerCompactionAfterToolCalls?: number;
providerCompactionProviderType?: string;
@@ -454,6 +474,9 @@ export function readContinuationRequest(
reasoning?: unknown;
reasoningDuration?: unknown;
thoughtSignature?: unknown;
+ thoughtParts?: unknown;
+ answerParts?: unknown;
+ geminiReplayTurns?: unknown;
providerCompaction?: unknown;
providerCompactionAfterToolCalls?: unknown;
providerCompactionProviderType?: unknown;
@@ -466,11 +489,22 @@ export function readContinuationRequest(
typeof request?.reasoning === "string" && request.reasoning.trim()
? request.reasoning
: "";
- if (!partial && !reasoning) {
+ const signature = request?.thoughtSignature;
+ const thoughtParts = parseGeminiThoughtReplayParts(request?.thoughtParts);
+ const answerParts = parseGeminiAnswerReplayParts(request?.answerParts);
+ const geminiReplayTurns = parseGeminiContinuationReplayTurns(
+ request?.geminiReplayTurns,
+ );
+ const hasGeminiReplay = Boolean(
+ (typeof signature === "string" && signature) ||
+ thoughtParts.length > 0 ||
+ answerParts.length > 0 ||
+ geminiReplayTurns.length > 0,
+ );
+ if (!partial && !reasoning && !hasGeminiReplay) {
return null;
}
const duration = request?.reasoningDuration;
- const signature = request?.thoughtSignature;
return {
partial,
...(reasoning ? { reasoning } : {}),
@@ -483,6 +517,9 @@ export function readContinuationRequest(
...(typeof signature === "string" && signature
? { thoughtSignature: signature }
: {}),
+ ...(thoughtParts.length > 0 ? { thoughtParts } : {}),
+ ...(answerParts.length > 0 ? { answerParts } : {}),
+ ...(geminiReplayTurns.length > 0 ? { geminiReplayTurns } : {}),
...providerCompactionFields(request),
};
}
diff --git a/studio/frontend/src/features/chat/utils/update-thread-message.ts b/studio/frontend/src/features/chat/utils/update-thread-message.ts
index 8e4422a95fd..6238f3e7784 100644
--- a/studio/frontend/src/features/chat/utils/update-thread-message.ts
+++ b/studio/frontend/src/features/chat/utils/update-thread-message.ts
@@ -26,6 +26,23 @@ function withoutServerOwnership(record: MessageRecord): MessageRecord {
return Object.keys(kept).length > 0 ? { ...rest, metadata: kept } : rest;
}
+function withoutGeminiContinuationReplay<T>(metadata: T): T {
+ if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) {
+ return metadata;
+ }
+ const record = metadata as Record<string, unknown>;
+ const custom = record.custom;
+ if (!custom || typeof custom !== "object" || Array.isArray(custom)) {
+ return metadata;
+ }
+ const customRecord = custom as Record<string, unknown>;
+ if (!("geminiContinuationReplay" in customRecord)) {
+ return metadata;
+ }
+ const { geminiContinuationReplay: _stale, ...keptCustom } = customRecord;
+ return { ...record, custom: keptCustom } as T;
+}
+
type ThreadImportExport = {
export: () => ExportedMessageRepository;
import: (data: ExportedMessageRepository) => void;
@@ -217,6 +234,7 @@ export async function updateThreadMessage(args: {
message: {
...m.message,
content: finalContent,
+ metadata: withoutGeminiContinuationReplay(m.message.metadata),
},
};
}) as typeof currentExport.messages;
diff --git a/studio/frontend/tests/cancelled-turn-history-prune.test.ts b/studio/frontend/tests/cancelled-turn-history-prune.test.ts
index 41b9a1c5217..b9a1cc6a8de 100644
--- a/studio/frontend/tests/cancelled-turn-history-prune.test.ts
+++ b/studio/frontend/tests/cancelled-turn-history-prune.test.ts
@@ -63,7 +63,7 @@ test("a stopped empty assistant is filled before the prune sees it", () => {
assert.match(adapter, /function fillStoppedAssistantReplay\(/);
assert.match(
adapter,
- /return fillStoppedAssistantReplay\(\s*message,\s*serializeAssistantReplayMessages\(/,
+ /const serialized = fillStoppedAssistantReplay\(\s*message,\s*serializeAssistantReplayMessages\(/,
);
assert.match(adapter, /function stoppedAssistantReplayText\(/);
assert.match(adapter, /incompleteLabel\(info\?\.reason \?\? fromStatus\)/);
diff --git a/studio/frontend/tests/chat-continuation.test.ts b/studio/frontend/tests/chat-continuation.test.ts
index b143f923a78..559fc116d2e 100644
--- a/studio/frontend/tests/chat-continuation.test.ts
+++ b/studio/frontend/tests/chat-continuation.test.ts
@@ -206,11 +206,16 @@ test("every stop reason has a label", () => {
);
});
-test("only text has to carry weight for a turn to count as rendered", () => {
+test("text-like parts have to carry weight for a turn to count as rendered", () => {
assert.equal(hasRenderableContent([]), false);
assert.equal(hasRenderableContent([{ type: "text", text: "" }]), false);
assert.equal(hasRenderableContent([{ type: "text", text: " \n\t" }]), false);
assert.equal(hasRenderableContent([{ type: "text", text: "hi" }]), true);
+ assert.equal(hasRenderableContent([{ type: "reasoning", text: "" }]), false);
+ assert.equal(
+ hasRenderableContent([{ type: "reasoning", text: "thought" }]),
+ true,
+ );
// A turn that only called a tool or drew an image still answered.
assert.equal(hasRenderableContent([{ type: "tool-call" }]), true);
assert.equal(hasRenderableContent([{ type: "image" }]), true);
@@ -625,6 +630,26 @@ test("a continuation carries the Gemini signature of the turn it resumes", () =>
}),
{ partial: "half", thoughtSignature: "SIG" },
);
+ assert.deepEqual(
+ readContinuationRequest({
+ custom: {
+ unslothContinuation: {
+ partial: "half",
+ answerParts: [
+ { text: "half" },
+ { text: "", thoughtSignature: "SIG-END" },
+ ],
+ },
+ },
+ }),
+ {
+ partial: "half",
+ answerParts: [
+ { text: "half" },
+ { text: "", thoughtSignature: "SIG-END" },
+ ],
+ },
+ );
// An unsigned turn stays unsigned rather than gaining an empty key.
assert.deepEqual(
readContinuationRequest({
diff --git a/studio/frontend/tests/chat-thought-continuation.test.ts b/studio/frontend/tests/chat-thought-continuation.test.ts
index 60967205cac..9ac3a5a600a 100644
--- a/studio/frontend/tests/chat-thought-continuation.test.ts
+++ b/studio/frontend/tests/chat-thought-continuation.test.ts
@@ -18,6 +18,24 @@ import { createReasoningDurationTracker } from "../src/features/chat/utils/reaso
import { readSrc } from "./helpers/kit.ts";
const THOUGHT = "The user wants three primes. Small ones are";
+const COLLECT_GEMINI_THOUGHTS =
+ /collectGeminiThoughtReplayParts\(messageContent\)/;
+const CARRY_GEMINI_THOUGHTS =
+ /thoughtParts\.length > 0 \? \{ thoughtParts \} : \{\}/;
+const REPLAY_GEMINI_THOUGHTS =
+ /continuation\.thoughtParts && continuation\.thoughtParts\.length > 0/;
+const CARRY_GEMINI_TURNS =
+ /geminiReplayTurns\.length > 0 \? \{ geminiReplayTurns \} : \{\}/;
+const REPLAY_GEMINI_TURNS =
+ /serializeGeminiContinuationTurns\(geminiReplayTurns, false\)/;
+const PRESERVE_GEMINI_BOUNDARY =
+ /geminiContinuationReplay: geminiContinuationReplay\(false\),[\s\S]*incomplete:/;
+const RETAIN_GEMINI_BOUNDARY_ACROSS_PROVIDER_SWITCH =
+ /const geminiContinuationReplayTurns =\s*continuation\?\.geminiReplayTurns\?\.length/;
+const ACTIVE_PROVIDER_DOES_NOT_GATE_GEMINI_BOUNDARY =
+ /externalProvider\?\.providerType === "gemini" &&\s*continuation\?\.geminiReplayTurns/;
+const ALLOW_GEMINI_REPLAY_WITHOUT_VISIBLE_TEXT =
+ /const hasGeminiReplayContinuation = Boolean\([\s\S]{0,500}!hasGeminiReplayContinuation/;
test("the source splits a reply into the answer and the thought before it", () => {
assert.deepEqual(
@@ -59,16 +77,60 @@ test("a thought-only turn is continuable only where a thought resumes", () => {
}),
false,
);
+ assert.equal(
+ isContinuableContent([{ type: "reasoning", text: "" }], { replay: true }),
+ true,
+ );
+ assert.equal(
+ isContinuableContent([{ type: "tool-call", toolName: "web_search" }], {
+ replay: true,
+ }),
+ false,
+ );
});
test("a thought-only request is read, and its duration only travels with a thought", () => {
assert.deepEqual(
readContinuationRequest({
custom: {
- unslothContinuation: { partial: "", reasoning: THOUGHT, reasoningDuration: 7 },
+ unslothContinuation: {
+ partial: "",
+ reasoning: THOUGHT,
+ reasoningDuration: 7,
+ thoughtParts: [
+ { text: "first thought", thoughtSignature: "SIG-THOUGHT-1" },
+ { text: "second thought", thoughtSignature: "SIG-THOUGHT-2" },
+ ],
+ geminiReplayTurns: [
+ {
+ text: "2, 3",
+ thoughtSignature: "SIG-ANSWER",
+ thoughtParts: [
+ { text: "first thought", thoughtSignature: "SIG-THOUGHT-1" },
+ ],
+ },
+ ],
+ },
},
}),
- { partial: "", reasoning: THOUGHT, reasoningDuration: 7 },
+ {
+ partial: "",
+ reasoning: THOUGHT,
+ reasoningDuration: 7,
+ thoughtParts: [
+ { text: "first thought", thoughtSignature: "SIG-THOUGHT-1" },
+ { text: "second thought", thoughtSignature: "SIG-THOUGHT-2" },
+ ],
+ geminiReplayTurns: [
+ {
+ text: "2, 3",
+ thoughtSignature: "SIG-ANSWER",
+ thoughtParts: [
+ { text: "first thought", thoughtSignature: "SIG-THOUGHT-1" },
+ ],
+ },
+ ],
+ },
);
assert.deepEqual(
readContinuationRequest({
@@ -82,6 +144,46 @@ test("a thought-only request is read, and its duration only travels with a thoug
}),
null,
);
+ assert.deepEqual(
+ readContinuationRequest({
+ custom: {
+ unslothContinuation: {
+ partial: "",
+ thoughtParts: [{ text: "", thoughtSignature: "SIG-THOUGHT" }],
+ geminiReplayTurns: [
+ {
+ text: "",
+ thoughtParts: [{ text: "", thoughtSignature: "SIG-THOUGHT" }],
+ },
+ ],
+ },
+ },
+ }),
+ {
+ partial: "",
+ thoughtParts: [{ text: "", thoughtSignature: "SIG-THOUGHT" }],
+ geminiReplayTurns: [
+ {
+ text: "",
+ thoughtParts: [{ text: "", thoughtSignature: "SIG-THOUGHT" }],
+ },
+ ],
+ },
+ );
+});
+
+test("Gemini continuations retain the provider turn boundary", () => {
+ const thread = readSrc("components/assistant-ui/thread.tsx");
+ const adapter = readSrc("features/chat/api/chat-adapter.ts");
+ assert.match(thread, COLLECT_GEMINI_THOUGHTS);
+ assert.match(thread, CARRY_GEMINI_THOUGHTS);
+ assert.match(thread, CARRY_GEMINI_TURNS);
+ assert.match(adapter, REPLAY_GEMINI_THOUGHTS);
+ assert.match(adapter, REPLAY_GEMINI_TURNS);
+ assert.match(adapter, PRESERVE_GEMINI_BOUNDARY);
+ assert.match(adapter, RETAIN_GEMINI_BOUNDARY_ACROSS_PROVIDER_SWITCH);
+ assert.doesNotMatch(adapter, ACTIVE_PROVIDER_DOES_NOT_GATE_GEMINI_BOUNDARY);
+ assert.match(adapter, ALLOW_GEMINI_REPLAY_WITHOUT_VISIBLE_TEXT);
});
/** What the adapter's stream loop appends, per delta, to a seeded buffer. */
diff --git a/studio/frontend/tests/composer-keystroke-subscription-budget.test.ts b/studio/frontend/tests/composer-keystroke-subscription-budget.test.ts
index 85ba827eade..03d40be5fe2 100644
--- a/studio/frontend/tests/composer-keystroke-subscription-budget.test.ts
+++ b/studio/frontend/tests/composer-keystroke-subscription-budget.test.ts
@@ -122,7 +122,7 @@ test("the newest message still gets the whole continue bar", () => {
/useAuiState\(\(\{ message \}\) => message\.status\)/,
/useAuiState\(\(\{ message \}\) => message\.metadata\)/,
/readContinuationSource\(message\.content\)/,
- /isContinuableContent\(message\.content, \{ thought: thoughtResumable \}\)/,
+ /isContinuableContent\(messageContent, \{[\s\S]{0,160}thought: thoughtResumable,[\s\S]{0,160}replay: geminiReplayTurns\.length > 0/,
/findLatestUserAudioBase64\(thread\.messages, false\)/,
/modeAllowsContinuation\(\{/,
]) {
diff --git a/studio/frontend/tests/edit-message-keeps-metadata.test.ts b/studio/frontend/tests/edit-message-keeps-metadata.test.ts
index 8b21b704e0b..e9e395b6c6a 100644
--- a/studio/frontend/tests/edit-message-keeps-metadata.test.ts
+++ b/studio/frontend/tests/edit-message-keeps-metadata.test.ts
@@ -127,6 +127,23 @@ test("an edited reply keeps the metadata the turn was stored with", async () =>
assert.deepEqual(record.metadata, CUSTOM);
});
+test("editing clears stale Gemini continuation replay and keeps other metadata", async () => {
+ const record = await saveOwnedReply({
+ ...CUSTOM,
+ geminiContinuationReplay: {
+ turns: [{ text: "the original reply", thoughtSignature: "SIG-OLD" }],
+ visiblePrefix: "the original reply",
+ stripVisiblePrefix: true,
+ },
+ });
+
+ assert.deepEqual(record.metadata, CUSTOM);
+ assert.equal(
+ "geminiContinuationReplay" in (record.metadata as Record<string, unknown>),
+ false,
+ );
+});
+
test("the edit still rewrites the content and its identity", async () => {
const record = await save("an edited reply");
diff --git a/studio/frontend/tests/external-preserve-thinking.test.ts b/studio/frontend/tests/external-preserve-thinking.test.ts
index 9bfcc988ada..24ca6c39ffc 100644
--- a/studio/frontend/tests/external-preserve-thinking.test.ts
+++ b/studio/frontend/tests/external-preserve-thinking.test.ts
@@ -36,7 +36,7 @@ test("llama.cpp sends the selected value and replays reasoning only when enabled
assert.deepEqual(result, {replay: enabled, fields: {preserve_thinking: enabled}});
}
assert.match(source, /pruneOutboundHistory\(messages, replayReasoning\)/);
- assert.match(source, /toOpenAIMessages\(message, replayReasoning\)/);
+ assert.match(source, /toOpenAIMessages\(\s*message,\s*replayReasoning,/);
});
test("switching providers never leaks a retained enabled preference", () => {
diff --git a/studio/frontend/tests/gemini-thought-replay.test.ts b/studio/frontend/tests/gemini-thought-replay.test.ts
new file mode 100644
index 00000000000..8f47409c9a4
--- /dev/null
+++ b/studio/frontend/tests/gemini-thought-replay.test.ts
@@ -0,0 +1,296 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import {
+ type GeminiAnswerReplayPart,
+ type GeminiThoughtReplayPart,
+ type PositionedGeminiAnswerReplayPart,
+ type PositionedGeminiThoughtReplayPart,
+ appendGeminiAnswerReplayPart,
+ appendGeminiThoughtReplayPart,
+ collectGeminiAnswerReplayParts,
+ collectGeminiThoughtReplayParts,
+ continuationGeminiReplayTurns,
+ geminiContinuationReplayEntries,
+ geminiThoughtReplayParts,
+ pinGeminiAnswerReplayParts,
+ pinGeminiTextThoughtSignature,
+ pinGeminiThoughtReplayParts,
+ readGeminiContinuationReplay,
+ withGeminiAnswerReplayParts,
+ withGeminiThoughtReplayParts,
+} from "../src/features/chat/gemini-thought-replay.ts";
+import { readSrc } from "./helpers/kit.ts";
+
+const READ_REPLAY_PARTS = /geminiThoughtReplayParts\(part\)/;
+const WRITE_REPLAY_PARTS = /withGeminiThoughtReplayParts\(/;
+const PIN_THOUGHT_PARTS = /pinGeminiThoughtReplayParts\(/;
+const CAPTURE_THOUGHT_PARTS =
+ /appendGeminiThoughtReplayPart\([\s\S]{0,220}toolCallParts\.length/;
+const CAPTURE_NATIVE_THOUGHT_PART =
+ /const thoughtPart = googleRecord\.thought_part/;
+const MERGE_UNSIGNED_THOUGHT_INTO_LATER_SIGNATURE = /pendingGeminiThoughtText/;
+const PACE_TEXT_BEARING_REPLAY_METADATA =
+ /geminiReplayStateChanged && !delta && !reasoning/;
+const FLUSH_BEFORE_REPLAY_CAPTURE =
+ /if \(part\.type === "reasoning"\) \{[\s\S]{0,180}flushAssistantAndToolResults\(\);[\s\S]{0,180}pendingGeminiThoughtParts\.push/;
+const PIN_TEXT_SIGNATURE =
+ /pinGeminiTextThoughtSignature\([\s\S]*legacyGeminiTextSignature,[\s\S]*\)/;
+
+test("every native thought boundary stays separate from signed answer text", () => {
+ const parts: Array<{
+ type: string;
+ text?: string;
+ _google_thought_signature?: string;
+ _google_thought_parts?: GeminiThoughtReplayPart[];
+ }> = [
+ { type: "reasoning", text: "first thought" },
+ { type: "tool-call" },
+ { type: "reasoning", text: "second thought" },
+ { type: "text", text: "the answer" },
+ ];
+ const thoughtParts: PositionedGeminiThoughtReplayPart[] = [];
+ appendGeminiThoughtReplayPart(thoughtParts, "unsigned thought", undefined, 0);
+ appendGeminiThoughtReplayPart(thoughtParts, "signed thought", "SIG-THOUGHT-1", 0);
+ appendGeminiThoughtReplayPart(
+ thoughtParts,
+ "second thought",
+ "SIG-THOUGHT-2",
+ 1,
+ );
+
+ pinGeminiThoughtReplayParts(parts, thoughtParts);
+ pinGeminiTextThoughtSignature(parts, "SIG-ANSWER");
+
+ assert.equal(parts[3]._google_thought_signature, "SIG-ANSWER");
+ assert.deepEqual(geminiThoughtReplayParts(parts[0]), [
+ { text: "unsigned thought" },
+ { text: "signed thought", thoughtSignature: "SIG-THOUGHT-1" },
+ ]);
+ assert.deepEqual(geminiThoughtReplayParts(parts[2]), [
+ { text: "second thought", thoughtSignature: "SIG-THOUGHT-2" },
+ ]);
+ assert.deepEqual(collectGeminiThoughtReplayParts(parts), [
+ { text: "unsigned thought" },
+ { text: "signed thought", thoughtSignature: "SIG-THOUGHT-1" },
+ { text: "second thought", thoughtSignature: "SIG-THOUGHT-2" },
+ ]);
+});
+
+test("thought parts use a separate replay envelope from signed answer text", () => {
+ const extra = withGeminiThoughtReplayParts(
+ { google: { thought_signature: "SIG-ANSWER" } },
+ [
+ { text: "unsigned preface" },
+ { text: "consider the clues", thoughtSignature: "SIG-THOUGHT" },
+ ],
+ );
+
+ assert.deepEqual(extra, {
+ google: {
+ thought_signature: "SIG-ANSWER",
+ thought_parts: [
+ { text: "unsigned preface" },
+ { text: "consider the clues", thought_signature: "SIG-THOUGHT" },
+ ],
+ },
+ });
+});
+
+test("unsigned stream chunks compact without crossing signature or tool boundaries", () => {
+ const thoughtParts: PositionedGeminiThoughtReplayPart[] = [];
+ const answerParts: PositionedGeminiAnswerReplayPart[] = [];
+ for (let index = 0; index < 1_000; index += 1) {
+ appendGeminiThoughtReplayPart(thoughtParts, "t", undefined, 0);
+ appendGeminiAnswerReplayPart(answerParts, { text: "a" }, 0);
+ }
+ appendGeminiThoughtReplayPart(thoughtParts, "signed", "SIG-T", 0);
+ appendGeminiThoughtReplayPart(thoughtParts, "next", undefined, 1);
+ appendGeminiAnswerReplayPart(
+ answerParts,
+ { text: "signed", thoughtSignature: "SIG-A" },
+ 0,
+ );
+ appendGeminiAnswerReplayPart(answerParts, { text: "next" }, 1);
+
+ assert.deepEqual(thoughtParts, [
+ { text: "t".repeat(1_000), afterToolCalls: 0 },
+ { text: "signed", thoughtSignature: "SIG-T", afterToolCalls: 0 },
+ { text: "next", afterToolCalls: 1 },
+ ]);
+ assert.deepEqual(answerParts, [
+ { text: "a".repeat(1_000), afterToolCalls: 0 },
+ { text: "signed", thoughtSignature: "SIG-A", afterToolCalls: 0 },
+ { text: "next", afterToolCalls: 1 },
+ ]);
+});
+
+test("signed and unsigned Gemini answer-part boundaries replay exactly", () => {
+ const parts: Array<{
+ type: string;
+ text?: string;
+ _google_answer_parts?: GeminiAnswerReplayPart[];
+ }> = [{ type: "text", text: "firstsecond" }];
+ const answerParts: PositionedGeminiAnswerReplayPart[] = [];
+ appendGeminiAnswerReplayPart(answerParts, { text: "first" }, 0);
+ appendGeminiAnswerReplayPart(
+ answerParts,
+ { text: "second", thoughtSignature: "SIG-SECOND" },
+ 0,
+ );
+ appendGeminiAnswerReplayPart(
+ answerParts,
+ { text: "", thoughtSignature: "SIG-EMPTY" },
+ 0,
+ );
+ pinGeminiAnswerReplayParts(parts, answerParts);
+ assert.deepEqual(collectGeminiAnswerReplayParts(parts), [
+ { text: "first" },
+ { text: "second", thoughtSignature: "SIG-SECOND" },
+ { text: "", thoughtSignature: "SIG-EMPTY" },
+ ]);
+ assert.deepEqual(
+ withGeminiAnswerReplayParts(undefined, collectGeminiAnswerReplayParts(parts)),
+ {
+ google: {
+ answer_parts: [
+ { text: "first" },
+ { text: "second", thought_signature: "SIG-SECOND" },
+ { text: "", thought_signature: "SIG-EMPTY" },
+ ],
+ },
+ },
+ );
+});
+
+test("continued Gemini turns keep the hidden user boundary", () => {
+ const metadata = {
+ custom: {
+ geminiContinuationReplay: {
+ turns: [
+ {
+ text: "first answer",
+ thoughtSignature: "SIG-ANSWER-1",
+ thoughtParts: [
+ { text: "first thought", thoughtSignature: "SIG-THOUGHT-1" },
+ ],
+ answerParts: [
+ { text: "first answer" },
+ { text: "", thoughtSignature: "SIG-ANSWER-1" },
+ ],
+ },
+ ],
+ visiblePrefix: "first answer",
+ stripVisiblePrefix: true,
+ },
+ },
+ };
+
+ assert.deepEqual(readGeminiContinuationReplay(metadata), {
+ turns: [
+ {
+ text: "first answer",
+ thoughtSignature: "SIG-ANSWER-1",
+ thoughtParts: [
+ { text: "first thought", thoughtSignature: "SIG-THOUGHT-1" },
+ ],
+ answerParts: [
+ { text: "first answer" },
+ { text: "", thoughtSignature: "SIG-ANSWER-1" },
+ ],
+ },
+ ],
+ visiblePrefix: "first answer",
+ stripVisiblePrefix: true,
+ });
+ assert.deepEqual(
+ continuationGeminiReplayTurns(metadata, {
+ text: "first answer and the rest",
+ thoughtSignature: "SIG-ANSWER-2",
+ thoughtParts: [
+ { text: "second thought", thoughtSignature: "SIG-THOUGHT-2" },
+ ],
+ }),
+ [
+ {
+ text: "first answer",
+ thoughtSignature: "SIG-ANSWER-1",
+ thoughtParts: [
+ { text: "first thought", thoughtSignature: "SIG-THOUGHT-1" },
+ ],
+ answerParts: [
+ { text: "first answer" },
+ { text: "", thoughtSignature: "SIG-ANSWER-1" },
+ ],
+ },
+ {
+ text: " and the rest",
+ thoughtSignature: "SIG-ANSWER-2",
+ thoughtParts: [
+ { text: "second thought", thoughtSignature: "SIG-THOUGHT-2" },
+ ],
+ },
+ ],
+ );
+ assert.deepEqual(
+ continuationGeminiReplayTurns(
+ {
+ custom: {
+ geminiContinuationReplay: {
+ turns: [{ text: "first answer", thoughtSignature: "SIG-ANSWER-1" }],
+ visiblePrefix: "first answer",
+ stripVisiblePrefix: false,
+ },
+ },
+ },
+ {
+ text: "first answer restarted in full",
+ thoughtSignature: "SIG-ANSWER-2",
+ },
+ ),
+ [
+ { text: "first answer", thoughtSignature: "SIG-ANSWER-1" },
+ {
+ text: "first answer restarted in full",
+ thoughtSignature: "SIG-ANSWER-2",
+ },
+ ],
+ );
+ assert.deepEqual(
+ geminiContinuationReplayEntries(
+ [
+ { text: "first", thoughtSignature: "SIG-1" },
+ { text: "second", thoughtSignature: "SIG-2" },
+ ],
+ true,
+ ),
+ [
+ {
+ role: "assistant",
+ turn: { text: "first", thoughtSignature: "SIG-1" },
+ },
+ { role: "user" },
+ {
+ role: "assistant",
+ turn: { text: "second", thoughtSignature: "SIG-2" },
+ },
+ { role: "user" },
+ ],
+ );
+});
+
+test("the chat adapter retains thought and answer signatures independently", () => {
+ const adapter = readSrc("features/chat/api/chat-adapter.ts");
+ assert.match(adapter, READ_REPLAY_PARTS);
+ assert.match(adapter, WRITE_REPLAY_PARTS);
+ assert.match(adapter, PIN_THOUGHT_PARTS);
+ assert.match(adapter, CAPTURE_THOUGHT_PARTS);
+ assert.match(adapter, CAPTURE_NATIVE_THOUGHT_PART);
+ assert.doesNotMatch(adapter, MERGE_UNSIGNED_THOUGHT_INTO_LATER_SIGNATURE);
+ assert.match(adapter, PACE_TEXT_BEARING_REPLAY_METADATA);
+ assert.match(adapter, FLUSH_BEFORE_REPLAY_CAPTURE);
+ assert.match(adapter, PIN_TEXT_SIGNATURE);
+});