11import { isValidModelDiscoveryModelId , MODEL_DISCOVERY_MAX_MODELS } from "./model-discovery-limits" ;
2+ import { isModelCacheGenerationCurrent } from "../codex/model-cache" ;
23
34// Google Antigravity (Cloud Code Assist) bundled model list.
45//
@@ -74,8 +75,12 @@ const ANTIGRAVITY_DISCOVERY_EFFORTS = ["low", "medium", "high"] as const;
7475
7576function pickerModelIdForDiscoveredWireId (
7677 wireId : string ,
78+ info : Record < string , unknown > ,
7779 available : ReadonlyMap < string , Record < string , unknown > > ,
7880) : string {
81+ const displayModelId = antigravityDisplayModelId ( info . displayName , wireId ) ;
82+ if ( displayModelId ) return displayModelId ;
83+
7984 const explicitPickerId = Object . hasOwn ( ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID , wireId )
8085 ? ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID [ wireId ]
8186 : undefined ;
@@ -240,6 +245,8 @@ export const ANTIGRAVITY_MODEL_INPUT_MODALITIES: Record<string, string[]> = {
240245
241246export interface AntigravityAvailableModel {
242247 id : string ;
248+ /** CCA model id used by the agent envelope when `id` comes from display metadata. */
249+ wireModelId : string ;
243250 contextWindow ?: number ;
244251 inputModalities ?: string [ ] ;
245252}
@@ -254,6 +261,86 @@ function antigravityPositiveInteger(value: unknown): number | undefined {
254261 return typeof value === "number" && Number . isSafeInteger ( value ) && value > 0 ? value : undefined ;
255262}
256263
264+ interface DiscoveredWireModelMapping {
265+ readonly models : ReadonlyMap < string , string > ;
266+ readonly generation ?: { provider : string ; cacheGeneration : string } ;
267+ }
268+
269+ const discoveredWireModelsByBaseUrl = new Map < string , DiscoveredWireModelMapping > ( ) ;
270+
271+ function antigravityBaseUrlKey ( baseUrl : string | undefined ) : string | undefined {
272+ if ( typeof baseUrl !== "string" || ! baseUrl . trim ( ) ) return undefined ;
273+ const trimmed = baseUrl . trim ( ) . replace ( / \/ + $ / , "" ) ;
274+ try {
275+ const url = new URL ( trimmed ) ;
276+ url . hash = "" ;
277+ url . search = "" ;
278+ return url . toString ( ) . replace ( / \/ + $ / , "" ) . toLowerCase ( ) ;
279+ } catch {
280+ return trimmed . toLowerCase ( ) ;
281+ }
282+ }
283+
284+ /** Remember the wire ids returned by one live CCA discovery for request routing. */
285+ export function registerAntigravityDiscoveredWireModels (
286+ baseUrl : string | undefined ,
287+ models : readonly AntigravityAvailableModel [ ] ,
288+ generation ?: { provider : string ; cacheGeneration : string } ,
289+ ) : void {
290+ const key = antigravityBaseUrlKey ( baseUrl ) ;
291+ if ( ! key ) return ;
292+ const wireModels = new Map < string , string > ( ) ;
293+ for ( const model of models ) wireModels . set ( model . id , model . wireModelId ) ;
294+ discoveredWireModelsByBaseUrl . set ( key , {
295+ models : wireModels ,
296+ ...( generation ? { generation } : { } ) ,
297+ } ) ;
298+ }
299+
300+ function discoveredAntigravityWireModelId (
301+ modelId : string ,
302+ baseUrl : string | undefined ,
303+ ) : string | undefined {
304+ const key = antigravityBaseUrlKey ( baseUrl ) ;
305+ if ( ! key ) return undefined ;
306+ const mapping = discoveredWireModelsByBaseUrl . get ( key ) ;
307+ if ( ! mapping ) return undefined ;
308+ if ( mapping . generation
309+ && ! isModelCacheGenerationCurrent ( mapping . generation . provider , mapping . generation . cacheGeneration ) ) {
310+ discoveredWireModelsByBaseUrl . delete ( key ) ;
311+ return undefined ;
312+ }
313+ return mapping . models . get ( modelId ) ;
314+ }
315+
316+ /**
317+ * Convert the CCA display label used by `agy` into its public model selector.
318+ *
319+ * The wire id is authoritative for requests, while the label is authoritative for the
320+ * user-facing selector when Google has renamed or re-tiered a model. Keep both instead
321+ * of maintaining a provider-specific list of known model names.
322+ */
323+ function antigravityDisplayModelId ( displayName : unknown , wireId : string ) : string | undefined {
324+ if ( typeof displayName !== "string" ) return undefined ;
325+ const label = displayName . trim ( ) ;
326+ if ( ! label || label . length > 512 ) return undefined ;
327+ const slug = ( replaceDots : boolean ) : string => label
328+ . normalize ( "NFKC" )
329+ . toLowerCase ( )
330+ . replace ( replaceDots ? / \. / g : / \s + / g, replaceDots ? "-" : " " )
331+ . replace ( / [ ^ a - z 0 - 9 . - ] + / g, "-" )
332+ . replace ( / - + / g, "-" )
333+ . replace ( / ^ - | - $ / g, "" ) ;
334+ const preserved = slug ( false ) ;
335+ const compact = slug ( true ) ;
336+ if ( ! isValidModelDiscoveryModelId ( preserved ) && ! isValidModelDiscoveryModelId ( compact ) ) return undefined ;
337+ if ( preserved === wireId || compact === wireId
338+ || preserved === `${ wireId } -thinking` || compact === `${ wireId } -thinking` ) {
339+ return wireId ;
340+ }
341+ return isValidModelDiscoveryModelId ( preserved ) ? preserved : compact ;
342+ }
343+
257344/**
258345 * Extract the CCA models that are valid for agent requests. The endpoint also returns tab,
259346 * command, commit-message, transcription, and standalone image-generation models; those are not
@@ -288,13 +375,6 @@ export function parseAntigravityAvailableModels(
288375 }
289376 }
290377 }
291- // This model is exposed by Antigravity's agent chat surface even though it is grouped under
292- // image generation in the discovery response.
293- if ( Array . isArray ( body . imageGenerationModelIds )
294- && body . imageGenerationModelIds . includes ( "gemini-3.1-flash-image" ) ) {
295- if ( ids . length >= limit ) return null ;
296- ids . push ( "gemini-3.1-flash-image" ) ;
297- }
298378 // Newer CCA responses identify tiered Flash models through this index instead of
299379 // adding their synthetic wire ids to agentModelSorts.
300380 const tieredModelIds = antigravityRecord ( body . tieredModelIds ) ;
@@ -305,6 +385,12 @@ export function parseAntigravityAvailableModels(
305385 || ! Object . hasOwn ( models , id )
306386 || ! antigravityRecord ( models [ id ] )
307387 || ids . length >= limit ) return null ;
388+ const baseId = id . endsWith ( "-tiered" ) ? id . slice ( 0 , - "-tiered" . length ) : id ;
389+ if ( ids . some ( agentId =>
390+ agentId === id
391+ || agentId === baseId
392+ || ANTIGRAVITY_DISCOVERY_EFFORTS . some ( effort => agentId === `${ baseId } -${ effort } ` )
393+ ) ) continue ;
308394 ids . push ( id ) ;
309395 }
310396 }
@@ -313,23 +399,18 @@ export function parseAntigravityAvailableModels(
313399 for ( const wireId of ids ) {
314400 const info = antigravityRecord ( models [ wireId ] ) ;
315401 if ( ! info || available . has ( wireId ) ) continue ;
316- // Legacy compatibility aliases are deliberately routed to newer wire ids for saved
317- // selections. They are not safe as independently discovered picker rows.
318- const alias = Object . hasOwn ( ANTIGRAVITY_MODEL_ALIASES , wireId )
319- ? ANTIGRAVITY_MODEL_ALIASES [ wireId ]
320- : undefined ;
321- if ( alias && alias !== wireId ) continue ;
322402 available . set ( wireId , info ) ;
323403 }
324404
325405 const out : AntigravityAvailableModel [ ] = [ ] ;
326406 const seen = new Set < string > ( ) ;
327407 for ( const [ wireId , info ] of available ) {
328- const id = pickerModelIdForDiscoveredWireId ( wireId , available ) ;
408+ const id = pickerModelIdForDiscoveredWireId ( wireId , info , available ) ;
329409 if ( seen . has ( id ) ) continue ;
330410 seen . add ( id ) ;
331411 out . push ( {
332412 id,
413+ wireModelId : wireId ,
333414 ...( antigravityPositiveInteger ( info . maxTokens ) ? { contextWindow : antigravityPositiveInteger ( info . maxTokens ) } : { } ) ,
334415 // Tri-state, deliberately not a ternary: `true` asserts image support,
335416 // `false` asserts against it, and ABSENT is unknown. Collapsing absent into
@@ -347,7 +428,9 @@ export function parseAntigravityAvailableModels(
347428 return out ;
348429}
349430
350- export function resolveAntigravityWireModelId ( modelId : string ) : string {
431+ export function resolveAntigravityWireModelId ( modelId : string , baseUrl ?: string ) : string {
432+ const discovered = discoveredAntigravityWireModelId ( modelId , baseUrl ) ;
433+ if ( discovered ) return discovered ;
351434 return Object . hasOwn ( ANTIGRAVITY_MODEL_ALIASES , modelId )
352435 ? ANTIGRAVITY_MODEL_ALIASES [ modelId ]
353436 : modelId ;
@@ -380,7 +463,19 @@ export function retiredAntigravityFlashTier(modelId: string): string | undefined
380463export function resolveAntigravityEffortWireModel (
381464 modelId : string ,
382465 effort ?: string ,
466+ baseUrl ?: string ,
383467) : { wireModelId : string ; thinkingLevel ?: string } {
468+ const discoveredWireModelId = discoveredAntigravityWireModelId ( modelId , baseUrl ) ;
469+ if ( discoveredWireModelId && ( discoveredWireModelId !== modelId || isAntigravitySuffixModelId ( modelId ) ) ) {
470+ const defaultLevel = ANTIGRAVITY_THINKING_LEVEL_MODELS [ modelId ] ;
471+ return {
472+ wireModelId : discoveredWireModelId ,
473+ ...( defaultLevel
474+ ? { thinkingLevel : effort ? resolveAntigravityThinkingLevel ( effort ) ?? defaultLevel : defaultLevel }
475+ : { } ) ,
476+ } ;
477+ }
478+
384479 // Rule 0: retired Flash id — Google has taken the wire id offline, so route to the
385480 // current generation and carry the tier the retired id encoded. This runs BEFORE the
386481 // suffix check because those ids are aliases, and rule 1 would drop the tier.
@@ -394,7 +489,7 @@ export function resolveAntigravityEffortWireModel(
394489
395490 // Rule 1: suffix/compat alias — suffix IS the effort.
396491 if ( isAntigravitySuffixModelId ( modelId ) ) {
397- return { wireModelId : resolveAntigravityWireModelId ( modelId ) } ;
492+ return { wireModelId : resolveAntigravityWireModelId ( modelId , baseUrl ) } ;
398493 }
399494
400495 // Rule 1b: single-wire-id Gemini model whose tiers ride on thinkingLevel. Without
@@ -424,7 +519,7 @@ export function resolveAntigravityEffortWireModel(
424519 }
425520
426521 // Rule 5: everything else.
427- return { wireModelId : resolveAntigravityWireModelId ( modelId ) } ;
522+ return { wireModelId : resolveAntigravityWireModelId ( modelId , baseUrl ) } ;
428523}
429524
430525
0 commit comments