Skip to content

feat(agents): add Google Gemini provider with API key and OAuth support#33

Open
penso wants to merge 29 commits intomainfrom
claude/add-gemini-provider-HP53H
Open

feat(agents): add Google Gemini provider with API key and OAuth support#33
penso wants to merge 29 commits intomainfrom
claude/add-gemini-provider-HP53H

Conversation

@penso
Copy link
Copy Markdown
Collaborator

@penso penso commented Feb 5, 2026

Summary

  • Add native Google Gemini provider with full tool/function calling support
  • Add OAuth-based Gemini provider using Authorization Code + PKCE flow
  • Support streaming via Server-Sent Events for both providers
  • Add comprehensive documentation at docs/gemini.md

Features

API Key Provider (gemini)

  • Direct Google Generative Language API integration (no wrapper crate)
  • Full tool/function calling support (functionDeclarations, functionCall, functionResponse)
  • JSON Schema type conversion (lowercase to uppercase for Gemini compatibility)
  • System instruction support
  • Streaming SSE support

OAuth Provider (gemini-oauth)

  • Authorization Code + PKCE flow (browser-based, no client secret needed)
  • Built-in OAuth client ID (users don't need to create their own Google Cloud project)
  • API usage billed to user's Google account (not the app developer)
  • Automatic token refresh with 5-minute expiry buffer
  • Secure token storage via moltis-oauth TokenStore

Supported Models

  • gemini-2.5-pro-preview-06-05
  • gemini-2.5-flash-preview-05-20
  • gemini-2.0-flash
  • gemini-2.0-flash-lite
  • gemini-1.5-pro
  • gemini-1.5-flash

All models support 1M context window.

What You Need To Do

Before merging, replace the placeholder client ID in crates/oauth/src/defaults.rs:

client_id: "MOLTIS_GEMINI_CLIENT_ID".into(),

With your actual client ID from Google Cloud Console. See docs/gemini.md for setup instructions.

Test plan

  • Run cargo test --all-features - all tests pass
  • Create OAuth client in Google Cloud Console
  • Replace placeholder client ID
  • Test OAuth provider browser-based flow
  • Verify tool calling works with both providers
  • Verify streaming responses work correctly

🤖 Generated with Claude Code

claude and others added 23 commits February 5, 2026 03:43
Implement a native Gemini provider that directly calls the Google
Generative Language API, bypassing the genai crate wrapper for better
control and full feature support.

Features:
- Full tool/function calling support (functionDeclarations, functionCall,
  functionResponse)
- Streaming support via Server-Sent Events
- System instruction support
- Message format conversion (user, assistant, tool messages)
- JSON Schema type conversion (lowercase to uppercase for Gemini)
- Comprehensive test coverage

Supported models:
- gemini-2.5-pro-preview-06-05
- gemini-2.5-flash-preview-05-20
- gemini-2.0-flash
- gemini-2.0-flash-lite
- gemini-1.5-pro
- gemini-1.5-flash

All models support 1M context window.

https://claude.ai/code/session_01YRnqNPmxNtRyrtWwmhnJxz
Implement OAuth-based authentication for Gemini API using Google's
device authorization flow, similar to GitHub Copilot and Kimi Code
providers.

Features:
- Device flow OAuth authentication (poll for token after user enters code)
- Token refresh with automatic expiry detection (5 min buffer)
- Full tool/function calling support (same as API key version)
- Streaming support via Server-Sent Events
- Secure token storage via moltis-oauth TokenStore

Configuration:
- Requires GOOGLE_CLIENT_ID environment variable (from Google Cloud project)
- Optional GOOGLE_CLIENT_SECRET for confidential clients
- Provider name: "gemini-oauth"
- Feature flag: provider-gemini-oauth

Users need to:
1. Create a Google Cloud project
2. Enable the Generative Language API
3. Create OAuth 2.0 credentials (Desktop app type)
4. Set GOOGLE_CLIENT_ID environment variable
5. Run the device flow to authenticate

https://claude.ai/code/session_01YRnqNPmxNtRyrtWwmhnJxz
Comprehensive documentation covering:
- API key provider (gemini.rs) implementation details
- OAuth provider (gemini_oauth.rs) with device flow
- Message format conversion (Gemini's parts/content structure)
- Tool schema conversion (JSON Schema type uppercasing)
- Token management and refresh logic
- Provider registration flow
- Supported models and context windows
- Security considerations

https://claude.ai/code/session_01YRnqNPmxNtRyrtWwmhnJxz
Replace device-flow authentication with Authorization Code + PKCE flow.
This change allows Moltis to ship with a built-in Google OAuth client ID,
so users can authenticate with their Google account without needing to
create their own OAuth credentials.

Key changes:
- Use OAuthFlow and CallbackServer from moltis-oauth crate
- Add gemini-oauth config to defaults.rs with Google OAuth endpoints
- Remove GOOGLE_CLIENT_ID environment variable requirement
- API usage is billed to the user's Google account (not the app developer)

Also replaces the implementation documentation with user-facing docs at
docs/gemini.md.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move Gemini provider documentation to the mdbook structure and add
it as a sub-page under LLM Providers in the summary.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add async functions to list available Gemini models from the API:
- API key provider: list_models() and list_models_with_base_url()
- OAuth provider: list_models_oauth() and list_models_with_token()

Models are fetched from /v1beta/models endpoint, filtered to those
supporting text generation, and sorted by name. Pagination is handled
automatically. Both providers also get list_available_models() methods.

The GeminiModelInfo struct includes model_id() helper to extract the
model ID from the full resource name, and supports_generation() to
filter generation-capable models.

Includes comprehensive tests for:
- Model info struct methods
- ListModelsResponse deserialization with camelCase fields
- Token refresh threshold logic
- Missing field handling

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Update both Gemini providers (API key and OAuth) to use the new typed
ChatMessage enum instead of serde_json::Value for the LlmProvider trait
methods (complete, stream).

This aligns with the recent trait change that prevents metadata leakage
into provider API requests by using a typed message format.

Changes:
- Update extract_system_instruction() to work with &[ChatMessage]
- Update to_gemini_messages() to work with &[&ChatMessage]
- Add multimodal/vision support for User messages with images
- Update complete() and stream() method signatures
- Update tests to use ChatMessage constructors

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq bot commented Feb 9, 2026

Merging this PR will not alter performance

✅ 39 untouched benchmarks
⏩ 5 skipped benchmarks1


Comparing claude/add-gemini-provider-HP53H (049df7e) with main (edae809)2

Open in CodSpeed

Footnotes

  1. 5 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (89a72b2) during the generation of this report, so edae809 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

penso and others added 2 commits February 9, 2026 00:36
- Add cache_read_tokens and cache_write_tokens fields (via ..Default::default())
- Update test to expect namespaced model ID format (gemini::model-name)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 2, 2026

Codecov Report

❌ Patch coverage is 55.22581% with 347 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/providers/src/gemini_oauth.rs 52.41% 325 Missing ⚠️
crates/providers/src/lib.rs 51.11% 22 Missing ⚠️

📢 Thoughts on this report? Let us know!

penso added 3 commits March 1, 2026 18:26
Wire gemini-oauth into provider feature/registry setup, expose it in provider setup and config validation, and update provider docs/changelog. Remove the branch-local native gemini implementation that duplicates mainline behavior.
@deimosfr
Copy link
Copy Markdown

Hi, any update on this one? Having Gemini would be super useful! Thanks

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.

3 participants