@@ -3,52 +3,13 @@ import axios from "axios"
33import type { ModelInfo } from "@roo-code/types"
44
55import type { ApiHandlerOptions } from "../../../shared/api"
6- import { getCachedZooCodeToken , getZooCodeBaseUrl } from "../../../services/zoo-code-auth"
6+ import { getZooCodeBaseUrl , resolveZooGatewaySessionToken } from "../../../services/zoo-code-auth"
77
8- // Reuse the same schemas and parsing logic from vercel-ai-gateway since the API format is identical
9- import { type VercelAiGatewayModel , parseVercelAiGatewayModel } from "./vercel-ai-gateway"
10-
11- import { z } from "zod"
12-
13- /**
14- * ZooGatewayPricing (same format as Vercel AI Gateway)
15- */
16-
17- const zooGatewayPricingSchema = z . object ( {
18- input : z . string ( ) . optional ( ) ,
19- output : z . string ( ) . optional ( ) ,
20- input_cache_write : z . string ( ) . optional ( ) ,
21- input_cache_read : z . string ( ) . optional ( ) ,
22- image : z . string ( ) . optional ( ) ,
23- } )
24-
25- /**
26- * ZooGatewayModel (same format as Vercel AI Gateway)
27- */
28-
29- const zooGatewayModelSchema = z . object ( {
30- id : z . string ( ) ,
31- object : z . string ( ) ,
32- created : z . number ( ) ,
33- owned_by : z . string ( ) ,
34- name : z . string ( ) ,
35- description : z . string ( ) ,
36- context_window : z . number ( ) ,
37- max_tokens : z . number ( ) ,
38- type : z . string ( ) ,
39- pricing : zooGatewayPricingSchema ,
40- } )
41-
42- /**
43- * ZooGatewayModelsResponse
44- */
45-
46- const zooGatewayModelsResponseSchema = z . object ( {
47- object : z . string ( ) ,
48- data : z . array ( zooGatewayModelSchema ) ,
49- } )
50-
51- type ZooGatewayModelsResponse = z . infer < typeof zooGatewayModelsResponseSchema >
8+ import {
9+ type VercelAiGatewayModel ,
10+ parseVercelAiGatewayModel ,
11+ vercelAiGatewayModelsResponseSchema ,
12+ } from "./vercel-ai-gateway"
5213
5314// Bound model discovery so a network stall can't hang provider initialization paths.
5415const MODEL_DISCOVERY_TIMEOUT_MS = 15_000
@@ -63,30 +24,25 @@ export async function getZooGatewayModels(options?: ApiHandlerOptions): Promise<
6324 const models : Record < string , ModelInfo > = { }
6425 const baseURL = options ?. zooGatewayBaseUrl ?? `${ getZooCodeBaseUrl ( ) } /api/gateway/v1`
6526
66- // Build headers - Zoo Gateway requires authentication via the zoo_ext_ session token.
67- // Fall back to the secret-storage cache when the profile hasn't been seeded yet.
68- const sessionToken = options ?. zooSessionToken || getCachedZooCodeToken ( )
27+ const sessionToken = resolveZooGatewaySessionToken ( options ?. zooSessionToken )
6928 const headers : Record < string , string > = { }
7029 if ( sessionToken ) {
7130 headers [ "Authorization" ] = `Bearer ${ sessionToken } `
7231 }
7332
7433 try {
75- const response = await axios . get < ZooGatewayModelsResponse > ( `${ baseURL } /models` , {
34+ const response = await axios . get ( `${ baseURL } /models` , {
7635 headers,
7736 timeout : MODEL_DISCOVERY_TIMEOUT_MS ,
7837 } )
79- const result = zooGatewayModelsResponseSchema . safeParse ( response . data )
80-
81- // Fall back to the raw response only when it looks structurally sound; otherwise return
82- // an empty list rather than crashing on `response.data.data` being undefined.
83- const data = result . success ? result . data . data : Array . isArray ( response . data ?. data ) ? response . data . data : [ ]
38+ const result = vercelAiGatewayModelsResponseSchema . safeParse ( response . data )
8439
8540 if ( ! result . success ) {
8641 console . error ( `Zoo Gateway models response is invalid ${ JSON . stringify ( result . error . format ( ) ) } ` )
42+ return models
8743 }
8844
89- for ( const model of data ) {
45+ for ( const model of result . data . data ) {
9046 const { id } = model
9147
9248 // Only include language models for chat inference.
@@ -95,8 +51,7 @@ export async function getZooGatewayModels(options?: ApiHandlerOptions): Promise<
9551 continue
9652 }
9753
98- // Parse model using the same logic as Vercel AI Gateway since formats are identical
99- models [ id ] = parseZooGatewayModel ( { id, model : model as VercelAiGatewayModel } )
54+ models [ id ] = parseZooGatewayModel ( { id, model } )
10055 }
10156 } catch ( error ) {
10257 // Log only safe fields; never serialize the full error object because it
@@ -123,6 +78,5 @@ export async function getZooGatewayModels(options?: ApiHandlerOptions): Promise<
12378 */
12479
12580export const parseZooGatewayModel = ( { id, model } : { id : string ; model : VercelAiGatewayModel } ) : ModelInfo => {
126- // Reuse the parsing logic from vercel-ai-gateway
12781 return parseVercelAiGatewayModel ( { id, model } )
12882}
0 commit comments