Skip to content

feat: Support Gemini 3 built-in and custom tool combinations (tool context circulation)#12086

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
feature/gemini3-tool-context-circulation
Draft

feat: Support Gemini 3 built-in and custom tool combinations (tool context circulation)#12086
roomote-v0[bot] wants to merge 1 commit intomainfrom
feature/gemini3-tool-context-circulation

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 bot commented Apr 8, 2026

Related GitHub Issue

Closes: #11966

Description

This PR attempts to address Issue #11966 by enabling "tool context circulation" for Gemini 3+ models. Feedback and guidance are welcome.

Key changes:

  1. src/api/providers/gemini.ts: Added isGemini3Model() helper to detect Gemini 3+ models. For these models, Google Search is included as a built-in tool alongside custom function declarations in the tools config. The streaming handler now processes executableCode and codeExecutionResult parts from server-side tool invocations, surfacing them as text to the user and storing them for history round-tripping via a new getServerSideToolParts() method.

  2. src/api/transform/gemini-format.ts: Added ServerSideToolContentBlock type and isServerSideToolContentBlock() guard. The converter now recognizes executableCode and codeExecutionResult content blocks in conversation history and converts them back to proper Gemini Part format for round-tripping.

  3. src/core/task/Task.ts: Updated addToApiConversationHistory() to retrieve server-side tool parts from the handler and persist them as content blocks in the conversation history, enabling context circulation across turns.

Design decisions:

  • Gated entirely behind isGemini3Model() check -- older Gemini models and other providers are completely unaffected.
  • Server-side tool outputs are surfaced as formatted text (code blocks) to the user rather than a new stream event type, keeping the change minimal.
  • The ServerSideToolPart type uses a generic data: Record<string, unknown> payload to preserve all API fields verbatim for future-proofing.

Test Procedure

  • Added tests in gemini.spec.ts verifying: googleSearch is included for Gemini 3 models; not included for pre-Gemini 3 models; executableCode/codeExecutionResult parts are handled in streaming and stored; parts are reset between requests.
  • Added tests in gemini-format.spec.ts verifying: executableCode and codeExecutionResult content blocks are round-tripped correctly; mixed function calls and server-side tool parts work together.
  • Updated existing gemini-handler.spec.ts test to reflect the new combined tools behavior for the default Gemini 3 model.
  • All 60 tests across 4 test files pass. TypeScript compilation and linting pass cleanly.

Run: cd src && npx vitest run api/providers/__tests__/gemini.spec.ts api/providers/__tests__/gemini-handler.spec.ts api/transform/__tests__/gemini-format.spec.ts

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes.
  • Documentation Impact: No documentation updates are required for this internal API change.
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

  • The @google/genai SDK v1.29.1 does not expose an explicit includeServerSideToolInvocations flag in its TypeScript types. Instead, Gemini 3 natively supports combining googleSearch and functionDeclarations in the same tools array without additional flags.
  • The -customtools model variant (gemini-3.1-pro-preview-customtools) is unrelated to this feature per the issue discussion -- it is for prioritizing custom tool use over basic shell commands.

Interactively review PR in Roo Code Cloud

…ntext circulation)

Enable combining server-side built-in tools (Google Search, Code Execution)
with client-side function declarations for Gemini 3+ models. This enables
"tool context circulation" where the model can invoke its built-in tools
server-side and seamlessly pass that context into Roo Code local tools.

Changes:
- Add isGemini3Model helper to detect Gemini 3+ models
- Include googleSearch as a built-in tool alongside function declarations
  for Gemini 3 models
- Handle executableCode and codeExecutionResult parts in streaming response
- Store server-side tool parts on handler for conversation history
  round-tripping via getServerSideToolParts()
- Update gemini-format.ts to convert server-side tool content blocks back
  to Gemini Part format
- Update Task.ts addToApiConversationHistory to persist server-side tool
  parts in the conversation history
- Add comprehensive tests for all new functionality

Closes #11966
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENHANCEMENT] Support Gemini 3 Built-in and Custom Tool Combinations (Tool Context Circulation)

1 participant