import{__toESM}from"../../_virtual/_rolldown/runtime.js";import{require_picocolors}from"../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js";import{appendEnv}from"../append-env.js";import{AI_GATEWAY_API_KEY_ENV_FILE,AI_GATEWAY_API_KEY_ENV_VAR,writeAiGatewayApiKey}from"../ai-gateway-api-key.js";import{WizardCancelledError}from"../step.js";import{withSpinner}from"../with-spinner.js";import{getVercelAuthStatus,vercelAuthBlockerReason}from"../vercel-project.js";import{runLinkFlow}from"./link.js";import{validateGatewayApiKey}from"../validate-gateway-key.js";var import_picocolors=__toESM(require_picocolors(),1);const PROVIDER_QUESTION=`Which model provider do you want to use?`,EXTERNAL_PROVIDER_INSTRUCTIONS_TITLE=`Using another model provider`,EXTERNAL_PROVIDER_INSTRUCTIONS=[`Set your provider's API key in ${AI_GATEWAY_API_KEY_ENV_FILE} — e.g. ANTHROPIC_API_KEY or OPENAI_API_KEY.`,'In agent/agent.ts, set `model` to a provider-authored model — e.g. `anthropic("claude-opus-4.8")` from `@ai-sdk/anthropic`.',`See https://eve.dev/docs/agent-config for details.`,"A running `eve dev` reloads env files automatically — no restart needed."];function projectConnectionOption(e){let t={value:`project`,label:`AI Gateway via Project`,hint:`Authenticates with AI Gateway automatically
in a new or existing project. No keys to manage.`},n=e===void 0?void 0:vercelAuthBlockerReason(e);return n===void 0?t:{...t,disabled:!0,disabledReason:n,disabledReasonTone:`warning`}}function providerOptions(e,t){let n=projectConnectionOption(e);if(t?.kind===`gateway-project`){let e=t.teamName===void 0?import_picocolors.default.bold(t.projectName):`${import_picocolors.default.bold(t.projectName)} in team ${import_picocolors.default.bold(t.teamName)}`;n={...n,checked:!0,hint:`Linked to ${e}`}}let r={value:`own-key`,label:`AI Gateway via ${AI_GATEWAY_API_KEY_ENV_VAR}`,hint:`⎿ type your key`};if(t?.kind===`gateway-key`){let e=t.source.kind===`shell`?`your shell`:t.source.path;r={...r,checked:!0,hint:`${t.envKey} set in ${e}`}}return[n,r,{value:`external`,label:`Other providers`,hint:`Connect directly to a model provider
via OPENAI_API_KEY or ANTHROPIC_API_KEY.`}]}async function selectProvider(e){let t={message:PROVIDER_QUESTION,options:e.options,initialValue:e.initialValue,validateInlineKey:e.validateInlineKey};if(e.picker===void 0)throw Error(`The provider flow requires the Dev TUI provider picker.`);let n=await e.picker(t);if(n===void 0)throw new WizardCancelledError;return n}async function runProviderFlow(e){let{appRoot:t,prompter:r,signal:i}=e,o={getVercelAuthStatus,runLinkFlow,appendEnv,validateGatewayApiKey,...e.deps},s,c=e.currentProvider?.kind===`gateway-key`?`own-key`:`project`,l;try{for(;;){let n=await selectProvider({picker:e.picker,options:providerOptions(s,e.currentProvider),initialValue:c,validateInlineKey:(e,t)=>o.validateGatewayApiKey(e,i===void 0?t:AbortSignal.any([i,t]))});if(n.kind===`external`)return r.acknowledge?await r.acknowledge({message:EXTERNAL_PROVIDER_INSTRUCTIONS_TITLE,lines:EXTERNAL_PROVIDER_INSTRUCTIONS}):r.note(EXTERNAL_PROVIDER_INSTRUCTIONS.join(`
`),EXTERNAL_PROVIDER_INSTRUCTIONS_TITLE),{kind:`external-provider`};if(n.kind===`inline-key`){l=n;break}let a=await withSpinner(r,`Checking your Vercel login…`,()=>o.getVercelAuthStatus(t,{signal:i}));if(i?.throwIfAborted(),s=a,vercelAuthBlockerReason(s)!==void 0){c=`own-key`;continue}return await o.runLinkFlow({appRoot:t,prompter:r,signal:i,projectSelection:`create-or-link`})}}catch(e){if(e instanceof WizardCancelledError)return{kind:`cancelled`};throw e}let u=l.key.trim(),d=l.validation;i?.throwIfAborted(),d.kind===`inconclusive`&&r.log.warning(`Couldn't reach the gateway to validate (${d.message}). Saving the key anyway.`);let f=await writeAiGatewayApiKey({projectRoot:t,apiKey:u,appendEnv:o.appendEnv});return r.log.success(`${f.envKey} set.`),{kind:`done`,resolution:{credential:`api-key`,source:{kind:`env-file`,path:f.envFile}}}}export{EXTERNAL_PROVIDER_INSTRUCTIONS,EXTERNAL_PROVIDER_INSTRUCTIONS_TITLE,PROVIDER_QUESTION,runProviderFlow};