import{select}from"../ask.js";const POPULAR_PROVIDERS=[`anthropic`,`openai`,`google`],DEFAULT_MODEL_ID=`anthropic/claude-sonnet-4.6`;function modelOption(e,t,n){return{id:e,label:t,value:e,hint:n,featured:!0}}const FEATURED_MODEL_IDS=[DEFAULT_MODEL_ID,`anthropic/claude-opus-4.8`,`openai/gpt-5.5`,`google/gemini-3.5`],FALLBACK_MODELS=[modelOption(DEFAULT_MODEL_ID,`Claude Sonnet 4.6`,`Anthropic`),modelOption(`anthropic/claude-opus-4.8`,`Claude Opus 4.8`,`Anthropic`),modelOption(`openai/gpt-5.5`,`GPT-5.5`,`OpenAI`),modelOption(`google/gemini-3.5`,`Gemini 3.5`,`Google`)];function providerLabel(e){return e.length===0?``:e.charAt(0).toUpperCase()+e.slice(1)}function providerPriority(e){let n=POPULAR_PROVIDERS.indexOf(e);return n===-1?POPULAR_PROVIDERS.length:n}async function fetchGatewayCatalog(){let e=new AbortController,t=setTimeout(()=>e.abort(),5e3);try{return(await(await fetch(`https://ai-gateway.vercel.sh/v1/models`,{signal:e.signal})).json()).data}finally{clearTimeout(t)}}function featuredPriority(e){let t=FEATURED_MODEL_IDS.indexOf(e);return t===-1?FEATURED_MODEL_IDS.length:t}async function buildModelOptions(e){try{let t=(await e()).filter(e=>e.type===`language`&&(e.tags??[]).includes(`web-search`)).map(e=>{let t=e.id.split(`/`)[0]??``;return{value:e.id,label:e.name,hint:providerLabel(t),provider:t}}).sort((e,t)=>{let n=featuredPriority(e.value)-featuredPriority(t.value);if(n!==0)return n;let r=providerPriority(e.provider)-providerPriority(t.provider);if(r!==0)return r;let i=e.provider.localeCompare(t.provider);return i===0?e.label.localeCompare(t.label):i});return t.length===0?FALLBACK_MODELS:t.map(({value:e,label:t,hint:n})=>({id:e,label:t,value:e,hint:n,featured:FEATURED_MODEL_IDS.includes(e)||void 0}))}catch{return FALLBACK_MODELS}}function selectModel(t){let r=t.deps??{fetchModels:fetchGatewayCatalog};return{id:`select-model`,async gather(){let i=t.presetModel;if(i!==void 0&&i.length>0)return i;let a=await buildModelOptions(r.fetchModels),o=t.defaultModel!==void 0&&a.some(e=>e.value===t.defaultModel)?t.defaultModel:a.some(e=>e.value===`anthropic/claude-sonnet-4.6`)?DEFAULT_MODEL_ID:a[0]?.value;return t.asker.ask(select({key:`model`,message:`Which model should your agent use?`,options:a,recommended:o,required:!0,search:!0,placeholder:`type to filter (e.g. 'claude', 'gpt', 'gemini')`}))},async perform({input:e}){return e},apply(e,t){return{...e,modelId:t}}}}export{DEFAULT_MODEL_ID,selectModel};