import{z}from"#compiled/zod/index.js";import{join}from"node:path";import{mkdir,readFile,writeFile}from"node:fs/promises";import{AI_GATEWAY_MODELS_CATALOG_URL,vercelGatewayFetch}from"#internal/gateway.js";const COMPILED_RUNTIME_MODEL_CATALOG_CACHE_KIND=`eve-model-catalog-cache`,catalogModelProviderSchema=z.object({provider:z.string().min(1),providerModelId:z.string().min(1),contextWindowTokens:z.number().int().nonnegative().optional(),maxOutputTokens:z.number().int().nonnegative().optional()}).passthrough(),catalogModelSchema=z.object({slug:z.string().min(1),providers:z.array(catalogModelProviderSchema).min(1)}).passthrough(),modelCatalogResponseSchema=z.object({models:z.array(catalogModelSchema),providerAliases:z.record(z.string(),z.string())}).passthrough();z.object({contextWindowTokens:z.number().int().positive(),maxOutputTokens:z.number().int().positive().optional()}).strict();const compiledRuntimeModelCatalogCacheSchema=z.object({fetchedAt:z.string(),kind:z.literal(COMPILED_RUNTIME_MODEL_CATALOG_CACHE_KIND),models:z.array(catalogModelSchema),providerAliases:z.record(z.string(),z.string()),version:z.literal(2)}).strict(),builtInCompiledRuntimeModelLimitsById=new Map([[`anthropic/claude-opus-4.7`,{contextWindowTokens:2e5,maxOutputTokens:32e3}],[`openai/gpt-5.4`,{contextWindowTokens:4e5,maxOutputTokens:128e3}],[`openai/gpt-5.4-mini`,{contextWindowTokens:4e5,maxOutputTokens:128e3}]]);function resolveCompiledRuntimeModelCatalogCachePath(e){return join(e,`.eve`,`cache`,`model-catalog.json`)}function createCompiledRuntimeModelCatalogLoader(e){let t=null,n=null,r=null,getCachedCatalog=async()=>(t??=readModelCatalogCache(e),await t),getFetchedCatalog=async()=>{if(n!==null)throw n;if(r!==null)return await r;r=fetchAndPersistModelCatalog(e).then(e=>(t=Promise.resolve(e),e));try{return await r}catch(e){throw n=e,e}},resolveModelsFromCacheOrFetch=async()=>{let e=await getCachedCatalog();if(e!==null&&isCacheFresh(e))return e;try{return await getFetchedCatalog()}catch{return e===null?null:e}};return{async getModelLimits(e){let t=normalizeModelId(e),n=await resolveModelsFromCacheOrFetch();if(n!==null){let e=findBySlug(n.models,t);if(e)for(let t of e.providers){let e=limitsFromProvider(t);if(e!==null)return e}}return builtInCompiledRuntimeModelLimitsById.get(t)??null},async getByProviderModelId(e,t){let n=await resolveModelsFromCacheOrFetch();if(n===null)return null;let r=e.split(`.`)[0],i=n.providerAliases[r]??r,a=normalizeModelId(t);for(let e of n.models)for(let t of e.providers)if(t.provider===i&&normalizeModelId(t.providerModelId)===a){let n=limitsFromProvider(t);if(n!==null)return{slug:e.slug,limits:n}}return null}}}async function fetchAndPersistModelCatalog(e){let r=await vercelGatewayFetch(AI_GATEWAY_MODELS_CATALOG_URL);if(!r.ok)throw Error(`AI Gateway model catalog request failed with HTTP ${r.status} ${r.statusText}.`);let o=modelCatalogResponseSchema.safeParse(await r.json());if(!o.success)throw Error(`AI Gateway model catalog response did not match the expected schema.`);let s={fetchedAt:new Date().toISOString(),kind:COMPILED_RUNTIME_MODEL_CATALOG_CACHE_KIND,models:o.data.models,providerAliases:o.data.providerAliases,version:2};try{let r=resolveCompiledRuntimeModelCatalogCachePath(e);await mkdir(join(e,`.eve`,`cache`),{recursive:!0}),await writeFile(r,`${JSON.stringify(s,null,2)}\n`,`utf8`)}catch{}return s}async function readModelCatalogCache(e){try{let t=await readFile(resolveCompiledRuntimeModelCatalogCachePath(e),`utf8`),n=compiledRuntimeModelCatalogCacheSchema.safeParse(JSON.parse(t));return n.success?n.data:null}catch(e){return e instanceof Error&&`code`in e&&typeof e.code==`string`&&e.code,null}}function findBySlug(e,t){return e.find(e=>e.slug===t)}function limitsFromProvider(e){return e.contextWindowTokens===void 0||e.contextWindowTokens<=0?null:{contextWindowTokens:e.contextWindowTokens,...e.maxOutputTokens!==void 0&&e.maxOutputTokens>0&&{maxOutputTokens:e.maxOutputTokens}}}function isCacheFresh(e){let t=Date.parse(e.fetchedAt);return Number.isFinite(t)?Date.now()-t<=864e5:!1}function normalizeModelId(e){return e.endsWith(`-thinking`)?e.slice(0,-9):e}export{catalogModelProviderSchema,catalogModelSchema,createCompiledRuntimeModelCatalogLoader,modelCatalogResponseSchema,resolveCompiledRuntimeModelCatalogCachePath};