claude-code-mux 0.6.2

High-performance, intelligent Claude Code router built in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# Google AI Integration Guide

## Overview

This document covers two separate providers for Google's AI models:

1. **Google Gemini** (`provider_type: "gemini"`)
   - For Google AI Studio (AI.google.dev)
   - Supports OAuth 2.0 (Code Assist API) and API Key
   - **Only Gemini models**

2. **Vertex AI** (`provider_type: "vertex-ai"`) ✅
   - For Google Cloud Platform
   - Uses Application Default Credentials (ADC)
   - **Supports Gemini, Claude, Llama, and other models**

## ⚠️ IMPORTANT: OAuth Uses Code Assist API

The OAuth client_id `681255809395-...` from gemini-cli is registered **ONLY** for Google's Code Assist API (`cloudcode-pa.googleapis.com`), **NOT** for the public Gemini API (`generativelanguage.googleapis.com`).

Therefore:
- **OAuth** → Code Assist API (`cloudcode-pa.googleapis.com/v1internal:generateContent`)
- **API Key** → Public Gemini API (`generativelanguage.googleapis.com/v1beta/models/{model}:generateContent`)

---

## Google Gemini Provider (`provider_type: "gemini"`)

### Authentication Methods

#### 1. OAuth 2.0 (Google AI Pro/Ultra) - Code Assist API

**OAuth Configuration:**
```
client_id: 681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com
client_secret: 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
auth_url: https://accounts.google.com/o/oauth2/v2/auth
token_url: https://oauth2.googleapis.com/token
redirect_uri: http://localhost:13456/api/oauth/callback
scopes:
  - https://www.googleapis.com/auth/cloud-platform
  - https://www.googleapis.com/auth/userinfo.email
  - https://www.googleapis.com/auth/userinfo.profile
```

**OAuth Flow:**
1. Generate authorization URL with PKCE
2. User authorizes in browser → redirected to callback URL
3. Exchange authorization code for access token + refresh token
4. **Call loadCodeAssist API to get/validate project ID**
5. Store tokens with project_id for automatic refresh

**Project ID Handling:**

| Account Type | Project ID Source | Action Required |
|--------------|-------------------|-----------------|
| **Individual Account** (free-tier) | Auto-generated by API | ✅ None - works automatically |
| **Workspace Account** (standard-tier) | User-defined | ⚠️ Set `GOOGLE_CLOUD_PROJECT` env var |
| **Licensed Users** | User-defined | ⚠️ Set `GOOGLE_CLOUD_PROJECT` env var |

**For Workspace/Licensed Users Only:**
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project or select an existing one
3. Enable "Cloud AI Companion API" for the project
4. Copy the Project ID (NOT the project name)
5. Set environment variable before starting server:
   ```bash
   export GOOGLE_CLOUD_PROJECT=your-project-id
   # OR
   export GOOGLE_CLOUD_PROJECT_ID=your-project-id
   ```

**LoadCodeAssist API:**
```
POST https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist
Authorization: Bearer {access_token}
Content-Type: application/json

{
  "cloudaicompanionProject": null,
  "metadata": {
    "ideType": "IDE_UNSPECIFIED",
    "platform": "PLATFORM_UNSPECIFIED",
    "pluginType": "GEMINI"
  }
}

Response:
{
  "cloudaicompanionProject": "projects/123456789",
  "currentTier": { ... }
}
```

**API Endpoint (OAuth - Code Assist API):**
```
https://cloudcode-pa.googleapis.com/v1internal:generateContent
```

**Request Format (Code Assist API):**
```json
{
  "model": "gemini-2.0-flash-exp",
  "user_prompt_id": "gemini-1234567890",
  "request": {
    "contents": [...],
    "systemInstruction": {...},
    "generationConfig": {...}
  }
}
```

**Response Format (Code Assist API):**
```json
{
  "response": {
    "candidates": [...],
    "usageMetadata": {...}
  },
  "traceId": "..."
}
```

**Headers:**
```
Authorization: Bearer {access_token}
Content-Type: application/json
```

#### 2. API Key (Google AI Studio)

**API Key Configuration:**
- Get API key from: https://aistudio.google.com/app/apikey

**API Endpoint:**
```
https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}
```

**Headers:**
```
Content-Type: application/json
```

### Configuration Example

**OAuth-based Gemini provider:**
```toml
[[providers]]
name = "gemini-oauth"
provider_type = "gemini"
auth_type = "oauth"
oauth_provider = "google"
models = ["gemini-2.0-flash-exp", "gemini-2.5-pro"]
enabled = true
```

**API Key-based Gemini provider:**
```toml
[[providers]]
name = "gemini-studio"
provider_type = "gemini"
auth_type = "apikey"
api_key = "your-api-key-from-ai-studio"
models = ["gemini-1.5-pro", "gemini-1.5-flash"]
enabled = true
```

---

## Vertex AI Provider (`provider_type: "vertex-ai"`)

Vertex AI is Google Cloud's unified AI platform that provides access to multiple model families:
- **Gemini models** (2.5 Flash, 2.0 Flash, 1.5 Pro, etc.)
- **Claude models** (3.7 Sonnet, 3.5 Haiku, etc.)
- **Llama models** (Llama 4, Llama 3.x)
- **Other models** via Model Garden

### Authentication

Vertex AI uses **Application Default Credentials (ADC)** for authentication.

**Setup Steps:**
1. Install Google Cloud SDK: https://cloud.google.com/sdk/docs/install
2. Authenticate with ADC:
   ```bash
   gcloud auth application-default login
   ```
3. This creates credentials that the server will use automatically

**Alternative Authentication Methods:**
- Service Account JSON key
- Google Cloud API key
- Workload Identity (for GKE deployments)

### Configuration

**Required Fields:**
- `project_id`: Your Google Cloud Project ID (not project name)
- `location`: GCP region (e.g., us-central1, europe-west1)

**Available Locations:**
```
us-central1, us-east1, us-west1, us-west4
europe-west1, europe-west2, europe-west4
asia-east1, asia-northeast1, asia-southeast1
australia-southeast1
```

**TOML Configuration Example:**
```toml
[[providers]]
name = "vertex-gemini"
provider_type = "vertex-ai"
project_id = "my-gcp-project-id"
location = "us-central1"
models = [
    "publishers/google/models/gemini-2.0-flash-exp",
    "publishers/google/models/gemini-1.5-pro",
]
enabled = true
```

### Admin UI Setup

1. In Admin UI, select **"Vertex AI"** as provider type
2. Enter your **Project ID** (from Google Cloud Console)
3. Select **Location** from dropdown (11 regions)
4. Add model names with `publishers/google/models/` prefix
5. Save configuration
6. **Before starting the server**, authenticate with ADC (see above)

**Admin UI Features:**
- ✅ Select from 11 pre-configured regions
- ✅ Validates Project ID and Location before saving
- ✅ Shows Vertex AI badge on provider cards
- ✅ Edit/Update Vertex AI credentials anytime

### API Endpoint

```
https://{location}-aiplatform.googleapis.com/v1/projects/{project}/locations/{location}/publishers/google/models/{model}:generateContent
```

**Example:**
```
https://us-central1-aiplatform.googleapis.com/v1/projects/my-project/locations/us-central1/publishers/google/models/gemini-2.0-flash-exp:generateContent
```

**Headers:**
```
Authorization: Bearer {access_token}  // From ADC
Content-Type: application/json
```

### Supported Models

Vertex AI supports models from multiple providers. Model names must include the `publishers/{publisher}/models/` prefix.

**Gemini Models:**
```
publishers/google/models/gemini-2.0-flash-exp
publishers/google/models/gemini-1.5-pro
publishers/google/models/gemini-1.5-flash
```

**Claude Models:**
```
publishers/anthropic/models/claude-3-7-sonnet
publishers/anthropic/models/claude-3-5-haiku
```

**Llama Models:**
```
publishers/meta/models/llama-4-maverick
publishers/meta/models/llama-3-2-90b
```

---

## API Format

### Request Format (Gemini generateContent API)

```json
{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "Hello, how are you?"
        }
      ]
    }
  ],
  "generationConfig": {
    "temperature": 1.0,
    "topP": 0.95,
    "topK": 40,
    "maxOutputTokens": 8192,
    "stopSequences": []
  },
  "systemInstruction": {
    "parts": [
      {
        "text": "You are a helpful assistant."
      }
    ]
  }
}
```

### Response Format (Non-streaming)

```json
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "I'm doing well, thank you for asking!"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [...]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 10,
    "candidatesTokenCount": 15,
    "totalTokenCount": 25
  }
}
```

### Streaming Response Format

```
data: {"candidates": [{"content": {"parts": [{"text": "Hello"}],"role": "model"}}]}

data: {"candidates": [{"content": {"parts": [{"text": " there"}],"role": "model"}}]}

data: {"candidates": [{"content": {"parts": [{"text": "!"}],"role": "model"}],"finishReason": "STOP","usageMetadata": {...}}]}
```

## Anthropic → Gemini Conversion

### Role Mapping
```
Anthropic → Gemini
user      → user
assistant → model
```

### Content Block Mapping

**Text:**
```rust
// Anthropic
ContentBlock::Text { text: "Hello" }

// Gemini
{ "text": "Hello" }
```

**Image:**
```rust
// Anthropic
ContentBlock::Image {
  source: ImageSource {
    type: "base64",
    media_type: "image/png",
    data: "iVBORw0KG..."
  }
}

// Gemini
{
  "inline_data": {
    "mime_type": "image/png",
    "data": "iVBORw0KG..."
  }
}
```

**Thinking (Extended Thinking):**
```rust
// Anthropic
ContentBlock::Thinking {
  thinking: "Let me think...",
  signature: "..."
}

// Gemini (convert to text)
{ "text": "Let me think..." }
```

### System Prompt
```rust
// Anthropic
request.system = Some("You are a helpful assistant")

// Gemini
{
  "systemInstruction": {
    "parts": [
      { "text": "You are a helpful assistant" }
    ]
  }
}
```

### Generation Config
```rust
// Anthropic → Gemini
max_tokens       → maxOutputTokens
temperature      → temperature
top_p            → topP
stop_sequences   → stopSequences

// Gemini only
topK: 40 (default)
```

### Tools (Function Calling)

**Anthropic:**
```json
{
  "tools": [
    {
      "name": "get_weather",
      "description": "Get weather information",
      "input_schema": {
        "type": "object",
        "properties": {
          "location": { "type": "string" }
        }
      }
    }
  ]
}
```

**Gemini:**
```json
{
  "tools": [
    {
      "functionDeclarations": [
        {
          "name": "get_weather",
          "description": "Get weather information",
          "parameters": {
            "type": "object",
            "properties": {
              "location": { "type": "string" }
            }
          }
        }
      ]
    }
  ]
}
```

## Model Names Reference

### Gemini Provider Models

For `provider_type: "gemini"` (OAuth or API Key authentication), use simple model names:

**Stable Models (Recommended):**
```
gemini-2.5-pro              # Latest production model
gemini-2.0-flash-exp        # Fast experimental model
gemini-1.5-pro              # Previous stable model
gemini-1.5-flash            # Fast stable model
gemini-1.5-flash-8b         # Ultra-fast model
```

**Preview Models (Limited Access):**
```
gemini-3-pro-preview        # Requires preview access (may return 404)
gemini-exp-1206             # Experimental model
```

⚠️ **Note**: Preview models like `gemini-3-pro-preview` require special access and may not be available to all users. If you receive a 404 error, use `gemini-2.5-pro` or `gemini-2.0-flash-exp` instead.

### Vertex AI Provider Models

For `provider_type: "vertex-ai"`, use full model paths with `publishers/{publisher}/models/` prefix.

**Note:** This section was already documented above under "Vertex AI Provider → Supported Models".

## Error Handling

### Common Errors

**401 Unauthorized:**
```json
{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential.",
    "status": "UNAUTHENTICATED"
  }
}
```

**403 Forbidden:**
```json
{
  "error": {
    "code": 403,
    "message": "User does not have permission to access this resource.",
    "status": "PERMISSION_DENIED"
  }
}
```

**429 Rate Limit:**
```json
{
  "error": {
    "code": 429,
    "message": "Resource has been exhausted (e.g. check quota).",
    "status": "RESOURCE_EXHAUSTED"
  }
}
```

## Implementation Checklist

### Gemini Provider (`provider_type: "gemini"`)
- [x] GeminiProvider struct with OAuth/API Key support
- [x] OAuth configuration in auth/oauth.rs
- [x] OAuth token refresh logic
- [x] Request transformation (Anthropic → Gemini)
- [x] Response transformation (Gemini → Anthropic)
- [x] **Project ID retrieval via loadCodeAssist API** (OAuth only)
- [x] **Token storage with project_id field** (OAuth only)
- [x] **Code Assist API integration** (OAuth)
- [x] **Public Gemini API integration** (API Key)
- [x] **JSON Schema metadata cleaning for tools**
- [ ] Streaming support
- [x] Tool/Function calling support
- [x] Image support (inline_data)
- [x] Error handling and mapping
- [x] Admin UI integration
  - [x] OAuth flow UI (similar to Anthropic/OpenAI)
  - [x] API Key input
  - [x] Provider type selector showing "📱 Google Gemini"

### Vertex AI Provider (`provider_type: "vertex-ai"`)
- [x] Separate provider type in registry
- [x] Reuses GeminiProvider with ADC authentication
- [x] Project ID and Location configuration fields
- [x] ADC token acquisition logic
- [x] Vertex AI API endpoint construction
- [x] Admin UI integration
  - [x] Separate provider type selector "Vertex AI"
  - [x] Project ID input field
  - [x] Location dropdown (11 regions)
  - [x] ADC authentication instructions
  - [x] Vertex AI badge on provider cards
  - [x] Edit/Update support for Vertex AI providers
- [x] Multi-model support (Gemini, Claude, Llama)
- [x] Model name validation with `publishers/` prefix

## References

- Gemini CLI OAuth: `/tmp/gemini-cli/packages/core/src/code_assist/oauth2.ts`
- Gemini API Docs: https://ai.google.dev/gemini-api/docs
- Vertex AI Docs: https://cloud.google.com/vertex-ai/docs/generative-ai/start/quickstarts/api-quickstart