Continue stored Gemini interactions with previous_interaction_id

Merged
MU
7d ago
+239-3

On a multi-step tool run, the Gemini gateway sends the whole conversation, attachments included, again on every step. The xAI and OpenAI gateways send only the tool results and point at the previous response. Gemini already runs on the Interactions API, so the pieces were almost there: buildStepBody() ignored the StepContext, and neither the text nor the streaming parser set a continuationToken.

This PR follows the xAI pattern, but only when the caller opts in with the store provider option, so the default stays stateless and Google keeps nothing:

  • parseTextResponse() and the streaming path set continuationToken to the interaction id when the request was sent with store: true.
  • buildStepBody() sends previous_interaction_id together with only the new function_result steps when the step context carries a token. Tools, system instruction, response format and generation config are sent again, since Gemini scopes them to each interaction.
  • A previous_interaction_id passed through provider options still applies to the first request. Later steps continue from the interaction the loop just stored.

TextGenerationLoop::stepContextFor() already drops the token when the messages, model or instructions change, so failover and edited history keep working. A stream that never reports an interaction id, and any request sent without store, replays the full history exactly as before.

Non-breaking. The request body without store is unchanged and no stored data changes shape, so no UPGRADE.md entry is needed.

Tests (all fail without the change, except the two guarding the default):

  • a stored interaction is continued with previous_interaction_id and only the function results
  • each step of a stored tool loop continues from the interaction before it
  • a caller's own previous_interaction_id is used only on the first step
  • a continuation carries the same schema and generation config as the first request
  • a stored stream is continued with previous_interaction_id
  • a stored stream without an interaction id, and an unstored request or stream, replay the full history

Fixes #1175