[PR #407] [CLOSED] feat: implement gemini cli oauth and cloudcode provider api #9605

Closed
opened 2026-02-16 18:13:50 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/anomalyco/opencode/pull/407
Author: @aryasaatvik
Created: 6/25/2025
Status: Closed

Base: devHead: feat/gemini-cli-auth


📝 Commits (6)

  • b088b47 implement google gemini cli oauth authentication flow in auth module
  • bc57c08 refactor google auth to use google-auth-library
  • 531ce8f refactor provider to use Cloud Code API and improve request handling
  • 0127363 enhance google auth module with Cloud Code Assist API integration and user onboarding logic
  • 6b95cf8 fix duplicate messages
  • 735db31 switch to vertex from generative ai api to use cloud code

📊 Changes

5 files changed (+658 additions, -6 deletions)

View changed files

📝 bun.lock (+42 -3)
📝 packages/opencode/package.json (+4 -2)
packages/opencode/src/auth/google-cloud-code.ts (+428 -0)
📝 packages/opencode/src/cli/cmd/auth.ts (+57 -1)
📝 packages/opencode/src/provider/provider.ts (+127 -0)

📄 Description

FREE GEMINI!!!

This PR adds support for Google OAuth authentication to enable free usage of Gemini models through the Cloud Code Assist API, mirroring the authentication flow used by the official Gemini CLI.

See Gemini CLI Authentication Docs for reference.

resolves #402

Key Architecture Decision: Using google-vertex Provider

Google has three different APIs for Gemini models:

  • Google AI API (generativelanguage.googleapis.com) - Direct API with API keys
  • Vertex AI API - Enterprise ML platform
  • Cloud Code API (cloudcode-pa.googleapis.com) - Internal API used by Gemini CLI for free
    OAuth access

We chose to use the google-vertex provider because:

  1. Cloud Code API's request/response structure is much closer to Vertex AI than Google AI
  2. Gemini CLI uses Google Gen AI SDK in Vertex mode, so following their pattern makes sense
  3. Less transformation needed - Vertex AI already wraps requests with project/model info similar
    to Cloud Code

This means users select "google-vertex" in the auth menu for free Gemini access via OAuth.

Key Changes

1. OAuth Implementation (packages/opencode/src/auth/google.ts)

  • Implements Google OAuth 2.0 flow with automatic token refresh
  • Uses the same OAuth credentials as Gemini CLI:
    • Client ID: 681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com
    • Client Secret: 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
  • Implements Code Assist API integration for project setup
  • Includes OAuth callback server for token exchange

2. Provider Integration (packages/opencode/src/provider/provider.ts)

  • Custom loader for Google provider that uses OAuth tokens instead of API keys
  • Transforms requests to Cloud Code API format
  • Handles SSE streaming responses with proper unwrapping

3. CLI Authentication (packages/opencode/src/cli/cmd/auth.ts)

  • Adds Google OAuth login option alongside API key method
  • Integrates with the OAuth flow from auth/google.ts

Implementation Details

The implementation follows Gemini CLI's approach:

  1. OAuth Flow: Uses google-auth-library with offline access for refresh tokens
  2. Code Assist API: Implements loadCodeAssist and onboardUser endpoints
  3. Request Interception: The google-vertex custom loader intercepts SDK requests:
    // Vertex AI SDK makes request to:
    // https://us-central1-aiplatform.googleapis.com/v1/projects/.../models/gemini-2.0-flash:generate
    

Content

 // We intercept and redirect to:
 // https://cloudcode-pa.googleapis.com/v1internal:generateContent
 // With body: { model: "gemini-2.0-flash", project: "...", request: {...} }

Reference: Gemini CLI Implementation

Key files from Gemini CLI that guided this implementation:

Testing

  1. Run opencode auth login
  2. Complete OAuth flow in browser
  3. Use any Gemini model
  4. Verify free usage (no API key required)

Benefits

  • Enables free usage of Gemini models for authenticated users
  • Maintains compatibility with existing Google provider configuration
  • Seamless integration with OpenCode's existing auth system

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/anomalyco/opencode/pull/407 **Author:** [@aryasaatvik](https://github.com/aryasaatvik) **Created:** 6/25/2025 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `feat/gemini-cli-auth` --- ### 📝 Commits (6) - [`b088b47`](https://github.com/anomalyco/opencode/commit/b088b4702d3a530d646ae8446b99dc875993cc39) implement google gemini cli oauth authentication flow in auth module - [`bc57c08`](https://github.com/anomalyco/opencode/commit/bc57c08a0e1887afd021c5ac38ca476989b82ff4) refactor google auth to use google-auth-library - [`531ce8f`](https://github.com/anomalyco/opencode/commit/531ce8fddca9bca71cc6818c8746a0e7584248eb) refactor provider to use Cloud Code API and improve request handling - [`0127363`](https://github.com/anomalyco/opencode/commit/01273638bd30dbeb6b04cf17153fb582755f404a) enhance google auth module with Cloud Code Assist API integration and user onboarding logic - [`6b95cf8`](https://github.com/anomalyco/opencode/commit/6b95cf87b5bf41e92e378ec6a897b4ae1c5aeee5) fix duplicate messages - [`735db31`](https://github.com/anomalyco/opencode/commit/735db31fc6eee1bdae3c44826bd00e48709f5523) switch to vertex from generative ai api to use cloud code ### 📊 Changes **5 files changed** (+658 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `bun.lock` (+42 -3) 📝 `packages/opencode/package.json` (+4 -2) ➕ `packages/opencode/src/auth/google-cloud-code.ts` (+428 -0) 📝 `packages/opencode/src/cli/cmd/auth.ts` (+57 -1) 📝 `packages/opencode/src/provider/provider.ts` (+127 -0) </details> ### 📄 Description FREE GEMINI!!! This PR adds support for Google OAuth authentication to enable free usage of Gemini models through the Cloud Code Assist API, mirroring the authentication flow used by the official [Gemini CLI](https://github.com/google-gemini/gemini-cli/). See [Gemini CLI Authentication Docs](https://github.com/google-gemini/gemini-cli/blob/main/docs/cli/authentication.md) for reference. resolves #402 ## Key Architecture Decision: Using google-vertex Provider Google has three different APIs for Gemini models: - **Google AI API** (`generativelanguage.googleapis.com`) - Direct API with API keys - **Vertex AI API** - Enterprise ML platform - **Cloud Code API** (`cloudcode-pa.googleapis.com`) - Internal API used by Gemini CLI for free OAuth access We chose to use the `google-vertex` provider because: 1. **Cloud Code API's request/response structure is much closer to Vertex AI than Google AI** 2. **Gemini CLI uses Google Gen AI SDK in Vertex mode**, so following their pattern makes sense 3. **Less transformation needed** - Vertex AI already wraps requests with project/model info similar to Cloud Code This means users select "google-vertex" in the auth menu for free Gemini access via OAuth. ## Key Changes ### 1. OAuth Implementation (`packages/opencode/src/auth/google.ts`) - Implements Google OAuth 2.0 flow with automatic token refresh - Uses the same OAuth credentials as Gemini CLI: - Client ID: `681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com` - Client Secret: `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` - Implements Code Assist API integration for project setup - Includes OAuth callback server for token exchange ### 2. Provider Integration (`packages/opencode/src/provider/provider.ts`) - Custom loader for Google provider that uses OAuth tokens instead of API keys - Transforms requests to Cloud Code API format - Handles SSE streaming responses with proper unwrapping ### 3. CLI Authentication (`packages/opencode/src/cli/cmd/auth.ts`) - Adds Google OAuth login option alongside API key method - Integrates with the OAuth flow from `auth/google.ts` ## Implementation Details The implementation follows Gemini CLI's approach: 1. **OAuth Flow**: Uses `google-auth-library` with offline access for refresh tokens 2. **Code Assist API**: Implements `loadCodeAssist` and `onboardUser` endpoints 3. **Request Interception**: The google-vertex custom loader intercepts SDK requests: ```typescript // Vertex AI SDK makes request to: // https://us-central1-aiplatform.googleapis.com/v1/projects/.../models/gemini-2.0-flash:generate Content // We intercept and redirect to: // https://cloudcode-pa.googleapis.com/v1internal:generateContent // With body: { model: "gemini-2.0-flash", project: "...", request: {...} } ## Reference: Gemini CLI Implementation Key files from [Gemini CLI](https://github.com/google-gemini/gemini-cli/) that guided this implementation: - [`packages/core/src/code_assist/oauth2.ts`](https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/code_assist/oauth2.ts) - OAuth client setup - [`packages/core/src/code_assist/setup.ts`](https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/code_assist/setup.ts) - User setup flow - [`packages/core/src/code_assist/server.ts`](https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/code_assist/server.ts) - Code Assist API client - [`packages/core/src/code_assist/types.ts`](https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/code_assist/types.ts) - TypeScript types ## Testing 1. Run `opencode auth login` 2. Complete OAuth flow in browser 3. Use any Gemini model 4. Verify free usage (no API key required) ## Benefits - Enables free usage of Gemini models for authenticated users - Maintains compatibility with existing Google provider configuration - Seamless integration with OpenCode's existing auth system --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 18:13:50 -05:00
yindo closed this issue 2026-02-16 18:13:50 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#9605