eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 7.41 kB
JavaScript
import{__toESM}from"../../_virtual/_rolldown/runtime.js";import{require_picocolors}from"../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js";import{AI_GATEWAY_API_KEY_ENV_VAR}from"../ai-gateway-api-key.js";import{detectProjectIdentity}from"../project-resolution.js";import{WizardCancelledError}from"../step.js";import{withSpinner}from"../with-spinner.js";import{findEnvFileWithKey}from"../boxes/detect-ai-gateway.js";import{gatewayModelCapabilities}from"../boxes/model-capabilities.js";import{fetchGatewayCatalog,modelOptionsFromCatalog}from"../boxes/select-model.js";import{changeAgentModelSettings,formatApplyModelSettingsOutcome}from"./model-source-change.js";import{runProviderFlow}from"./provider.js";import{inspectApplication}from"#services/inspect-application.js";import{hasEnvValue,resolveGatewayCredential}from"#internal/resolve-model-endpoint-status.js";import{formatModelSummary}from"#shared/model-summary.js";import{readGatewayServiceTier}from"#shared/gateway-service-tier.js";var import_picocolors=__toESM(require_picocolors(),1);const MODEL_MENU_MESSAGE=``;function providerStatusHint(e,t=e=>e){if(e.kind===`gateway-project`)return`AI Gateway (Linked to ${e.teamName===void 0?t(e.projectName):`${t(e.projectName)} in ${t(e.teamName)}`})`;let n=e.source.kind===`shell`?`your shell`:e.source.path;return`AI Gateway (${e.envKey} in ${n})`}function modelListRows(e){return modelOptionsFromCatalog(e).map(e=>{let t={value:e.value,label:e.value};return e.featured===!0&&(t.featured=!0),t})}function formatModelDraftHint(e,t,n){let r={model:e};return t!==null&&(r.reasoning=t),n.kind===`priority`&&(r.fastGlyph=`↯`),formatModelSummary(r)}function modelMenuRows(e,t,n,r,i,a,o){let s;a||o?(s={value:`model`,label:`Change model`,description:a?`The model, its reasoning effort, and the Gateway service tier`:`Reasoning and service tier; the model itself is an SDK model call in agent.ts`},e!==null&&(s.hint=formatModelDraftHint(e,t,n))):s={value:`model`,label:`Change model`,disabled:!0,description:`Set via an SDK model call in agent.ts; edit the source to change it`};let c;return c=i?.kind===`external`?{disabled:!0,value:`provider`,label:`Change provider`,description:`Disabled in external endpoint mode`}:r.kind===`unset`?{value:`provider`,label:import_picocolors.default.bold(`Configure model access`),hint:import_picocolors.default.yellow(`Not configured`),description:`How your agent reaches the model provider`,accent:`warning`}:{value:`provider`,label:`Change provider`,hint:providerStatusHint(r,import_picocolors.default.bold),description:`How your agent reaches the model provider`},[s,c,{value:`done`,label:`Done`}]}async function detectModelProviderStatus(e,t={},i=process.env){let[a,s,c]=await Promise.all([detectProjectIdentity(e,t),findEnvFileWithKey(e,AI_GATEWAY_API_KEY_ENV_VAR),findEnvFileWithKey(e,`VERCEL_OIDC_TOKEN`)]);if(a!==void 0){let e={kind:`gateway-project`,projectName:a.projectName};return a.teamName!==void 0&&(e.teamName=a.teamName),e}let l={apiKeyInEnv:hasEnvValue(i[AI_GATEWAY_API_KEY_ENV_VAR])};s!==void 0&&(l.apiKeyFile=s),c!==void 0&&(l.oidcFile=c);let u=resolveGatewayCredential(l);return u===void 0?{kind:`unset`}:u.credential===`api-key`?{kind:`gateway-key`,envKey:AI_GATEWAY_API_KEY_ENV_VAR,source:u.source}:{kind:`gateway-key`,envKey:`VERCEL_OIDC_TOKEN`,source:{kind:`env-file`,path:u.file}}}async function runModelFlow(e){let{appRoot:t,prompter:n,signal:r}=e,o={readCurrentModel:readCurrentAgentModel,applySettings:changeAgentModelSettings,detectProviderStatus:detectModelProviderStatus,runProviderFlow,...e.deps},detectProvider=(e=!0)=>o.detectProviderStatus(t,e&&r!==void 0?{signal:r}:{}),l=o.selectModel?.fetchModels??fetchGatewayCatalog,[d,f,p]=await withSpinner(n,`Checking the project…`,()=>Promise.all([o.readCurrentModel(t),detectProvider(),l(r).catch(()=>void 0)]));r?.throwIfAborted();let{id:m,routing:h,serviceTier:g,editable:_,settingsEditable:v}=d,y=d.reasoning===`provider-default`?null:d.reasoning,b=f,x={model:{kind:`keep`},reasoning:{kind:`keep`},gatewayServiceTier:{kind:`keep`}},S,C,w=!1,T=h?.kind===`external`?{tone:`warning`,text:"`agent.ts` specifies the model provider directly. Model, provider, and service-tier changes stay source-owned; reasoning remains configurable here."}:void 0,E=b.kind===`unset`&&h?.kind!==`external`?`provider`:_||v?`model`:`provider`,D=h?.kind!==`external`&&(e.initialStep===`provider`||b.kind===`unset`);for(;;){let e;if(D)D=!1,e=`provider`;else try{e=await n.select({message:``,options:modelMenuRows(m,y,g,b,h,_,v),hintLayout:`stacked`,initialValue:E,notices:T===void 0?[]:[T]})}catch(e){if(!(e instanceof WizardCancelledError))throw e;if(hasModelSettingsChanges(x))return{kind:`cancelled`,discardedDraft:!0};break}if(e===`done`){w=!0;break}if(e===`model`){let e=o.pickModelSettings;if(e===void 0)throw Error(`runModelFlow requires a pickModelSettings dep to open the model screen.`);let t=await e({model:_?{kind:`pick`,options:modelListRows(p),current:m}:{kind:`fixed`,current:m,reason:`Set via an SDK model call in agent.ts; edit the source to change it`},reasoning:y,serviceTier:g,settingsEditable:v,externalRouting:h?.kind===`external`,capabilitiesFor:e=>gatewayModelCapabilities(p,e)});if(r?.throwIfAborted(),t===void 0){E=`model`;continue}t.model!==void 0&&(m=t.model,h={kind:`gateway`,target:t.model.split(`/`)[0]??``},x.model={kind:`set`,value:t.model}),t.reasoning!==void 0&&(y=t.reasoning==="default"?null:t.reasoning,x.reasoning=y===null?{kind:`remove`}:{kind:`set`,value:y}),t.serviceTier!==void 0&&(g=t.serviceTier===`priority`?{kind:`priority`}:{kind:`standard`},x.gatewayServiceTier=t.serviceTier===`priority`?{kind:`set`,value:`priority`}:{kind:`remove`}),E=`done`;continue}let c=await o.runProviderFlow({appRoot:t,prompter:n,signal:r,currentProvider:b});if(c.kind===`cancelled`){if(r?.aborted)return{kind:`cancelled`};E=`provider`;continue}if(c.kind===`external-provider`){if(r?.aborted)return{kind:`cancelled`};E=`done`;continue}b=await withSpinner(n,`Checking the project…`,()=>detectProvider(!1)),C={status:b},c.kind===`done`&&c.resolution!==void 0&&(C.resolution=c.resolution),w=!0;break}if(w&&hasModelSettingsChanges(x)&&(S=await o.applySettings({appRoot:t,patch:x}),r?.throwIfAborted()),S===void 0&&C===void 0)return{kind:`cancelled`};let O={kind:`done`};return S!==void 0&&(O.modelMessage=formatApplyModelSettingsOutcome(S)),C!==void 0&&(O.providerOutcome=C),O}function hasModelSettingsChanges(e){return e.model.kind!==`keep`||e.reasoning.kind!==`keep`||e.gatewayServiceTier.kind!==`keep`}async function readCurrentAgentModel(e){try{let{compiledState:t}=await inspectApplication(e),n=t?.manifest.config,r=n?.model;return{id:r?.id??null,routing:r?.routing??null,reasoning:n?.reasoning??null,serviceTier:readGatewayServiceTier(r?.providerOptions),editable:r!==void 0&&r.source===void 0,settingsEditable:n?.source!==void 0}}catch{return{id:null,routing:null,reasoning:null,serviceTier:{kind:`standard`},editable:!1,settingsEditable:!1}}}async function modelChangeRefusalForUneditableModel(e){let{editable:t,routing:n}=await readCurrentAgentModel(e);return t?null:`Model is set via ${n?.kind===`external`?`the external provider \`${n.provider}\``:`an SDK model call`} in agent.ts, not a string literal; /model can't rewrite it. Edit \`model\` in agent.ts.`}export{MODEL_MENU_MESSAGE,detectModelProviderStatus,modelChangeRefusalForUneditableModel,runModelFlow};