@@ -74,8 +74,12 @@ const ANTIGRAVITY_DISCOVERY_EFFORTS = ["low", "medium", "high"] as const;
7474
7575function pickerModelIdForDiscoveredWireId (
7676 wireId : string ,
77+ info : Record < string , unknown > ,
7778 available : ReadonlyMap < string , Record < string , unknown > > ,
7879) : string {
80+ const displayModelId = antigravityDisplayModelId ( info . displayName , wireId ) ;
81+ if ( displayModelId ) return displayModelId ;
82+
7983 const explicitPickerId = Object . hasOwn ( ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID , wireId )
8084 ? ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID [ wireId ]
8185 : undefined ;
@@ -240,6 +244,8 @@ export const ANTIGRAVITY_MODEL_INPUT_MODALITIES: Record<string, string[]> = {
240244
241245export interface AntigravityAvailableModel {
242246 id : string ;
247+ /** CCA model id used by the agent envelope when `id` comes from display metadata. */
248+ wireModelId : string ;
243249 contextWindow ?: number ;
244250 inputModalities ?: string [ ] ;
245251}
@@ -254,6 +260,69 @@ function antigravityPositiveInteger(value: unknown): number | undefined {
254260 return typeof value === "number" && Number . isSafeInteger ( value ) && value > 0 ? value : undefined ;
255261}
256262
263+ const discoveredWireModelsByBaseUrl = new Map < string , ReadonlyMap < string , string > > ( ) ;
264+
265+ function antigravityBaseUrlKey ( baseUrl : string | undefined ) : string | undefined {
266+ if ( typeof baseUrl !== "string" || ! baseUrl . trim ( ) ) return undefined ;
267+ const trimmed = baseUrl . trim ( ) . replace ( / \/ + $ / , "" ) ;
268+ try {
269+ const url = new URL ( trimmed ) ;
270+ url . hash = "" ;
271+ url . search = "" ;
272+ return url . toString ( ) . replace ( / \/ + $ / , "" ) . toLowerCase ( ) ;
273+ } catch {
274+ return trimmed . toLowerCase ( ) ;
275+ }
276+ }
277+
278+ /** Remember the wire ids returned by one live CCA discovery for request routing. */
279+ export function registerAntigravityDiscoveredWireModels (
280+ baseUrl : string | undefined ,
281+ models : readonly AntigravityAvailableModel [ ] ,
282+ ) : void {
283+ const key = antigravityBaseUrlKey ( baseUrl ) ;
284+ if ( ! key ) return ;
285+ const wireModels = new Map < string , string > ( ) ;
286+ for ( const model of models ) wireModels . set ( model . id , model . wireModelId ) ;
287+ discoveredWireModelsByBaseUrl . set ( key , wireModels ) ;
288+ }
289+
290+ function discoveredAntigravityWireModelId (
291+ modelId : string ,
292+ baseUrl : string | undefined ,
293+ ) : string | undefined {
294+ const key = antigravityBaseUrlKey ( baseUrl ) ;
295+ return key ? discoveredWireModelsByBaseUrl . get ( key ) ?. get ( modelId ) : undefined ;
296+ }
297+
298+ /**
299+ * Convert the CCA display label used by `agy` into its public model selector.
300+ *
301+ * The wire id is authoritative for requests, while the label is authoritative for the
302+ * user-facing selector when Google has renamed or re-tiered a model. Keep both instead
303+ * of maintaining a provider-specific list of known model names.
304+ */
305+ function antigravityDisplayModelId ( displayName : unknown , wireId : string ) : string | undefined {
306+ if ( typeof displayName !== "string" ) return undefined ;
307+ const label = displayName . trim ( ) ;
308+ if ( ! label || label . length > 512 ) return undefined ;
309+ const slug = ( replaceDots : boolean ) : string => label
310+ . normalize ( "NFKC" )
311+ . toLowerCase ( )
312+ . replace ( replaceDots ? / \. / g : / \s + / g, replaceDots ? "-" : " " )
313+ . replace ( / [ ^ a - z 0 - 9 . - ] + / g, "-" )
314+ . replace ( / - + / g, "-" )
315+ . replace ( / ^ - | - $ / g, "" ) ;
316+ const preserved = slug ( false ) ;
317+ const compact = slug ( true ) ;
318+ if ( ! isValidModelDiscoveryModelId ( preserved ) && ! isValidModelDiscoveryModelId ( compact ) ) return undefined ;
319+ if ( preserved === wireId || compact === wireId
320+ || preserved === `${ wireId } -thinking` || compact === `${ wireId } -thinking` ) {
321+ return wireId ;
322+ }
323+ return isValidModelDiscoveryModelId ( preserved ) ? preserved : compact ;
324+ }
325+
257326/**
258327 * Extract the CCA models that are valid for agent requests. The endpoint also returns tab,
259328 * command, commit-message, transcription, and standalone image-generation models; those are not
@@ -288,13 +357,6 @@ export function parseAntigravityAvailableModels(
288357 }
289358 }
290359 }
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- }
298360 // Newer CCA responses identify tiered Flash models through this index instead of
299361 // adding their synthetic wire ids to agentModelSorts.
300362 const tieredModelIds = antigravityRecord ( body . tieredModelIds ) ;
@@ -305,6 +367,8 @@ export function parseAntigravityAvailableModels(
305367 || ! Object . hasOwn ( models , id )
306368 || ! antigravityRecord ( models [ id ] )
307369 || ids . length >= limit ) return null ;
370+ const baseId = id . endsWith ( "-tiered" ) ? id . slice ( 0 , - "-tiered" . length ) : id ;
371+ if ( ids . some ( agentId => agentId === baseId || agentId . startsWith ( `${ baseId } -` ) ) ) continue ;
308372 ids . push ( id ) ;
309373 }
310374 }
@@ -313,23 +377,18 @@ export function parseAntigravityAvailableModels(
313377 for ( const wireId of ids ) {
314378 const info = antigravityRecord ( models [ wireId ] ) ;
315379 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 ;
322380 available . set ( wireId , info ) ;
323381 }
324382
325383 const out : AntigravityAvailableModel [ ] = [ ] ;
326384 const seen = new Set < string > ( ) ;
327385 for ( const [ wireId , info ] of available ) {
328- const id = pickerModelIdForDiscoveredWireId ( wireId , available ) ;
386+ const id = pickerModelIdForDiscoveredWireId ( wireId , info , available ) ;
329387 if ( seen . has ( id ) ) continue ;
330388 seen . add ( id ) ;
331389 out . push ( {
332390 id,
391+ wireModelId : wireId ,
333392 ...( antigravityPositiveInteger ( info . maxTokens ) ? { contextWindow : antigravityPositiveInteger ( info . maxTokens ) } : { } ) ,
334393 // Tri-state, deliberately not a ternary: `true` asserts image support,
335394 // `false` asserts against it, and ABSENT is unknown. Collapsing absent into
@@ -347,7 +406,9 @@ export function parseAntigravityAvailableModels(
347406 return out ;
348407}
349408
350- export function resolveAntigravityWireModelId ( modelId : string ) : string {
409+ export function resolveAntigravityWireModelId ( modelId : string , baseUrl ?: string ) : string {
410+ const discovered = discoveredAntigravityWireModelId ( modelId , baseUrl ) ;
411+ if ( discovered ) return discovered ;
351412 return Object . hasOwn ( ANTIGRAVITY_MODEL_ALIASES , modelId )
352413 ? ANTIGRAVITY_MODEL_ALIASES [ modelId ]
353414 : modelId ;
@@ -380,7 +441,13 @@ export function retiredAntigravityFlashTier(modelId: string): string | undefined
380441export function resolveAntigravityEffortWireModel (
381442 modelId : string ,
382443 effort ?: string ,
444+ baseUrl ?: string ,
383445) : { wireModelId : string ; thinkingLevel ?: string } {
446+ const discoveredWireModelId = discoveredAntigravityWireModelId ( modelId , baseUrl ) ;
447+ if ( discoveredWireModelId && ( discoveredWireModelId !== modelId || isAntigravitySuffixModelId ( modelId ) ) ) {
448+ return { wireModelId : discoveredWireModelId } ;
449+ }
450+
384451 // Rule 0: retired Flash id — Google has taken the wire id offline, so route to the
385452 // current generation and carry the tier the retired id encoded. This runs BEFORE the
386453 // suffix check because those ids are aliases, and rule 1 would drop the tier.
@@ -394,7 +461,7 @@ export function resolveAntigravityEffortWireModel(
394461
395462 // Rule 1: suffix/compat alias — suffix IS the effort.
396463 if ( isAntigravitySuffixModelId ( modelId ) ) {
397- return { wireModelId : resolveAntigravityWireModelId ( modelId ) } ;
464+ return { wireModelId : resolveAntigravityWireModelId ( modelId , baseUrl ) } ;
398465 }
399466
400467 // Rule 1b: single-wire-id Gemini model whose tiers ride on thinkingLevel. Without
@@ -424,7 +491,7 @@ export function resolveAntigravityEffortWireModel(
424491 }
425492
426493 // Rule 5: everything else.
427- return { wireModelId : resolveAntigravityWireModelId ( modelId ) } ;
494+ return { wireModelId : resolveAntigravityWireModelId ( modelId , baseUrl ) } ;
428495}
429496
430497
0 commit comments