forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel-resolver.ts
More file actions
411 lines (356 loc) · 13.4 KB
/
Copy pathmodel-resolver.ts
File metadata and controls
411 lines (356 loc) · 13.4 KB
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
/**
* Model Resolution with Thinking Tier Support
*
* Resolves model names with tier suffixes (e.g., gemini-3-pro-high, claude-opus-4-6-thinking-low)
* to their actual API model names and corresponding thinking configurations.
*/
import type { ResolvedModel, ThinkingTier, GoogleSearchConfig } from "./types";
export interface ModelResolverOptions {
cli_first?: boolean;
}
/**
* Thinking tier budgets by model family.
* Claude and Gemini 2.5 Pro use numeric budgets.
*/
export const THINKING_TIER_BUDGETS = {
claude: { low: 8192, medium: 16384, high: 32768 },
"gemini-2.5-pro": { low: 8192, medium: 16384, high: 32768 },
"gemini-2.5-flash": { low: 6144, medium: 12288, high: 24576 },
default: { low: 4096, medium: 8192, high: 16384 },
} as const;
/**
* Gemini 3 uses thinkingLevel strings instead of numeric budgets.
* Flash supports: minimal, low, medium, high
* Pro supports: low, high (no minimal/medium)
*/
export const GEMINI_3_THINKING_LEVELS = ["minimal", "low", "medium", "high"] as const;
/**
* Model aliases - maps user-friendly names to API model names.
*
* Format:
* - Gemini 3 Pro variants: gemini-3-pro-{low,medium,high}
* - Claude thinking variants: claude-{model}-thinking-{low,medium,high}
* - Claude non-thinking: claude-{model} (no -thinking suffix)
*/
export const MODEL_ALIASES: Record<string, string> = {
// Gemini 3 variants - for Gemini CLI only (tier stripped, thinkingLevel used)
// For Antigravity, these are bypassed and full model name is kept
"gemini-3-pro-low": "gemini-3-pro",
"gemini-3-pro-high": "gemini-3-pro",
"gemini-3.1-pro-low": "gemini-3.1-pro",
"gemini-3.1-pro-high": "gemini-3.1-pro",
"gemini-3-flash-low": "gemini-3-flash",
"gemini-3-flash-medium": "gemini-3-flash",
"gemini-3-flash-high": "gemini-3-flash",
// Claude proxy names (gemini- prefix for compatibility)
"gemini-claude-opus-4-6-thinking-low": "claude-opus-4-6-thinking",
"gemini-claude-opus-4-6-thinking-medium": "claude-opus-4-6-thinking",
"gemini-claude-opus-4-6-thinking-high": "claude-opus-4-6-thinking",
"gemini-claude-sonnet-4-6": "claude-sonnet-4-6",
// Image generation models - only gemini-3-pro-image is available via Antigravity API
// Note: gemini-2.5-flash-image (Nano Banana) is NOT supported by Antigravity - only Google AI API
// Reference: Antigravity-Manager/src-tauri/src/proxy/common/model_mapping.rs
};
const TIER_REGEX = /-(minimal|low|medium|high)$/;
const QUOTA_PREFIX_REGEX = /^antigravity-/i;
const GEMINI_3_PRO_REGEX = /^gemini-3(?:\.\d+)?-pro/i;
const GEMINI_3_FLASH_REGEX = /^gemini-3(?:\.\d+)?-flash/i;
// ANTIGRAVITY_ONLY_MODELS removed - all models now default to antigravity
/**
* Image generation models - always route to Antigravity.
* These models don't support thinking and require imageConfig.
*/
const IMAGE_GENERATION_MODELS = /image|imagen/i;
// Legacy LEGACY_ANTIGRAVITY_GEMINI3 regex removed - all Gemini models now default to antigravity
/**
* Models that support thinking tier suffixes.
* Only these models should have -low/-medium/-high stripped as thinking tiers.
* GPT models like gpt-oss-120b-medium should NOT have -medium stripped.
*/
function supportsThinkingTiers(model: string): boolean {
const lower = model.toLowerCase();
return (
lower.includes("gemini-3") ||
lower.includes("gemini-2.5") ||
(lower.includes("claude") && lower.includes("thinking"))
);
}
/**
* Extracts thinking tier from model name suffix.
* Only extracts tier for models that support thinking tiers.
*/
function extractThinkingTierFromModel(model: string): ThinkingTier | undefined {
// Only extract tier for models that support thinking tiers
if (!supportsThinkingTiers(model)) {
return undefined;
}
const tierMatch = model.match(TIER_REGEX);
return tierMatch?.[1] as ThinkingTier | undefined;
}
/**
* Determines the budget family for a model.
*/
function getBudgetFamily(model: string): keyof typeof THINKING_TIER_BUDGETS {
if (model.includes("claude")) {
return "claude";
}
if (model.includes("gemini-2.5-pro")) {
return "gemini-2.5-pro";
}
if (model.includes("gemini-2.5-flash")) {
return "gemini-2.5-flash";
}
return "default";
}
/**
* Checks if a model is a thinking-capable model.
*/
function isThinkingCapableModel(model: string): boolean {
const lower = model.toLowerCase();
return (
lower.includes("thinking") ||
lower.includes("gemini-3") ||
lower.includes("gemini-2.5")
);
}
function isGemini3ProModel(model: string): boolean {
return GEMINI_3_PRO_REGEX.test(model);
}
function isGemini3FlashModel(model: string): boolean {
return GEMINI_3_FLASH_REGEX.test(model);
}
/**
* Resolves a model name with optional tier suffix and quota prefix to its actual API model name
* and corresponding thinking configuration.
*
* Quota routing:
* - Default to Antigravity quota unless cli_first is enabled for Gemini models
* - Fallback to Gemini CLI happens at account rotation level when Antigravity is exhausted
* - "antigravity-" prefix marks explicit quota (no fallback allowed)
* - Claude and image models always use Antigravity
*
* Examples:
* - "gemini-2.5-flash" → { quotaPreference: "antigravity" }
* - "gemini-3-pro-preview" → { quotaPreference: "antigravity" }
* - "antigravity-gemini-3-pro-high" → { quotaPreference: "antigravity", explicitQuota: true }
* - "claude-opus-4-6-thinking-medium" → { quotaPreference: "antigravity" }
*
* @param requestedModel - The model name from the request
* @param options - Optional configuration including cli_first preference
* @returns Resolved model with thinking configuration
*/
export function resolveModelWithTier(requestedModel: string, options: ModelResolverOptions = {}): ResolvedModel {
const isAntigravity = QUOTA_PREFIX_REGEX.test(requestedModel);
const modelWithoutQuota = requestedModel.replace(QUOTA_PREFIX_REGEX, "");
const tier = extractThinkingTierFromModel(modelWithoutQuota);
const baseName = tier ? modelWithoutQuota.replace(TIER_REGEX, "") : modelWithoutQuota;
const isImageModel = IMAGE_GENERATION_MODELS.test(modelWithoutQuota);
const isClaudeModel = modelWithoutQuota.toLowerCase().includes("claude");
// All models default to Antigravity quota unless cli_first is enabled
// Fallback to gemini-cli happens at the account rotation level when Antigravity is exhausted
const preferGeminiCli = options.cli_first === true && !isAntigravity && !isImageModel && !isClaudeModel;
const quotaPreference = preferGeminiCli ? "gemini-cli" as const : "antigravity" as const;
const explicitQuota = isAntigravity || isImageModel;
const isGemini3 = modelWithoutQuota.toLowerCase().startsWith("gemini-3");
const skipAlias = isAntigravity && isGemini3;
// For Antigravity Gemini 3 Pro models without explicit tier, append default tier
// Antigravity API: gemini-3-pro requires tier suffix (gemini-3-pro-low/high)
// gemini-3-flash uses bare name + thinkingLevel param
// Pro defaults to -low unless an explicit tier is provided
const isGemini3Pro = isGemini3ProModel(modelWithoutQuota);
const isGemini3Flash = isGemini3FlashModel(modelWithoutQuota);
let antigravityModel = modelWithoutQuota;
if (skipAlias) {
if (isGemini3Pro && !tier && !isImageModel) {
antigravityModel = `${modelWithoutQuota}-low`;
} else if (isGemini3Flash && tier) {
antigravityModel = baseName;
}
}
const actualModel = skipAlias
? antigravityModel
: MODEL_ALIASES[modelWithoutQuota] || MODEL_ALIASES[baseName] || baseName;
const resolvedModel = actualModel;
const isThinking = isThinkingCapableModel(resolvedModel);
// Image generation models don't support thinking - return early without thinking config
if (isImageModel) {
return {
actualModel: resolvedModel,
isThinkingModel: false,
isImageModel: true,
quotaPreference,
explicitQuota,
};
}
// Check if this is a Gemini 3 model (works for both aliased and skipAlias paths)
const isEffectiveGemini3 = resolvedModel.toLowerCase().includes("gemini-3");
const isClaudeThinking = resolvedModel.toLowerCase().includes("claude") && resolvedModel.toLowerCase().includes("thinking");
if (!tier) {
// Gemini 3 models without explicit tier get a default thinkingLevel
if (isEffectiveGemini3) {
return {
actualModel: resolvedModel,
thinkingLevel: "low",
isThinkingModel: true,
quotaPreference,
explicitQuota,
};
}
// Claude thinking models without explicit tier get max budget (32768)
// Per Anthropic docs, budget_tokens is required when enabling extended thinking
if (isClaudeThinking) {
return {
actualModel: resolvedModel,
thinkingBudget: THINKING_TIER_BUDGETS.claude.high,
isThinkingModel: true,
quotaPreference,
explicitQuota,
};
}
return { actualModel: resolvedModel, isThinkingModel: isThinking, quotaPreference, explicitQuota };
}
// Gemini 3 models with tier always get thinkingLevel set
if (isEffectiveGemini3) {
return {
actualModel: resolvedModel,
thinkingLevel: tier,
tier,
isThinkingModel: true,
quotaPreference,
explicitQuota,
};
}
const budgetFamily = getBudgetFamily(resolvedModel);
const budgets = THINKING_TIER_BUDGETS[budgetFamily];
const thinkingBudget = budgets[tier];
return {
actualModel: resolvedModel,
thinkingBudget,
tier,
isThinkingModel: isThinking,
quotaPreference,
explicitQuota,
};
}
/**
* Gets the model family for routing decisions.
*/
export function getModelFamily(model: string): "claude" | "gemini-flash" | "gemini-pro" {
const lower = model.toLowerCase();
if (lower.includes("claude")) {
return "claude";
}
if (lower.includes("flash")) {
return "gemini-flash";
}
return "gemini-pro";
}
/**
* Variant config from OpenCode's providerOptions.
*/
export interface VariantConfig {
thinkingBudget?: number;
googleSearch?: GoogleSearchConfig;
}
/**
* Maps a thinking budget to Gemini 3 thinking level.
* ≤8192 → low, ≤16384 → medium, >16384 → high
*/
function budgetToGemini3Level(budget: number): "low" | "medium" | "high" {
if (budget <= 8192) return "low";
if (budget <= 16384) return "medium";
return "high";
}
/**
* Resolves model name for a specific headerStyle (quota fallback support).
* Transforms model names when switching between gemini-cli and antigravity quotas.
*
* Issue #103: When quota fallback occurs, model names need to be transformed:
* - gemini-3-flash-preview (gemini-cli) → gemini-3-flash (antigravity)
* - gemini-3-pro-preview (gemini-cli) → gemini-3-pro-low (antigravity)
* - gemini-3-flash (antigravity) → gemini-3-flash-preview (gemini-cli)
*/
export function resolveModelForHeaderStyle(
requestedModel: string,
headerStyle: "antigravity" | "gemini-cli"
): ResolvedModel {
const lower = requestedModel.toLowerCase();
const isGemini3 = lower.includes("gemini-3");
if (!isGemini3) {
return resolveModelWithTier(requestedModel);
}
if (headerStyle === "antigravity") {
let transformedModel = requestedModel
.replace(/-preview-customtools$/i, "")
.replace(/-preview$/i, "")
.replace(/^antigravity-/i, "");
const isGemini3Pro = isGemini3ProModel(transformedModel);
const hasTierSuffix = /-(low|medium|high)$/i.test(transformedModel);
const isImageModel = IMAGE_GENERATION_MODELS.test(transformedModel);
// Don't add tier suffix to image models - they don't support thinking
if (isGemini3Pro && !hasTierSuffix && !isImageModel) {
transformedModel = `${transformedModel}-low`;
}
const prefixedModel = `antigravity-${transformedModel}`;
return resolveModelWithTier(prefixedModel);
}
if (headerStyle === "gemini-cli") {
let transformedModel = requestedModel
.replace(/^antigravity-/i, "")
.replace(/-(low|medium|high)$/i, "");
const hasPreviewSuffix = /-preview($|-)/i.test(transformedModel);
if (!hasPreviewSuffix) {
transformedModel = `${transformedModel}-preview`;
}
return {
...resolveModelWithTier(transformedModel),
quotaPreference: "gemini-cli",
};
}
return resolveModelWithTier(requestedModel);
}
/**
* Resolves model with variant config from providerOptions.
* Variant config takes priority over tier suffix in model name.
*/
export function resolveModelWithVariant(
requestedModel: string,
variantConfig?: VariantConfig
): ResolvedModel {
const base = resolveModelWithTier(requestedModel);
if (!variantConfig) {
return base;
}
// Apply Google Search config if present
if (variantConfig.googleSearch) {
base.googleSearch = variantConfig.googleSearch;
base.configSource = "variant";
}
if (!variantConfig.thinkingBudget) {
return base;
}
const budget = variantConfig.thinkingBudget;
const isGemini3 = base.actualModel.toLowerCase().includes("gemini-3");
if (isGemini3) {
const level = budgetToGemini3Level(budget);
const isAntigravityGemini3Pro = base.quotaPreference === "antigravity" &&
isGemini3ProModel(base.actualModel);
let actualModel = base.actualModel;
if (isAntigravityGemini3Pro) {
const baseModel = base.actualModel.replace(/-(low|medium|high)$/, "");
actualModel = `${baseModel}-${level}`;
}
return {
...base,
actualModel,
thinkingLevel: level,
thinkingBudget: undefined,
configSource: "variant",
};
}
return {
...base,
thinkingBudget: budget,
configSource: "variant",
};
}