UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

3 lines (2 loc) 4.11 kB
import{interactiveAsker}from"../ask.js";import{detectDeployment,isProjectResolved,projectResolutionFromDeployment}from"../project-resolution.js";import{snapshotSetupState}from"../state.js";import{WizardCancelledError}from"../step.js";import{withSpinner}from"../with-spinner.js";import{getVercelAuthStatus,vercelAuthBlockerReason}from"../vercel-project.js";import{selectConnections}from"../boxes/select-connections.js";import{addConnections}from"../boxes/add-connections.js";import{runInteractive}from"../runner.js";import{inProjectSetupState,prompterSink}from"./in-project.js";import{runLinkFlow}from"./link.js";import{CONNECTION_CATALOG,ensureConnectionDependencies,listAuthoredConnections}from"#setup/scaffold/index.js";import{detectPackageManager}from"#setup/package-manager.js";import{toErrorMessage}from"#shared/errors.js";import{createPromptCommandOutput,withPhase}from"#setup/cli/index.js";import{runPackageManagerInstall}from"#setup/primitives/pm/run.js";const CONNECTIONS_PROMPT_MESSAGE=`Select an MCP server to add to your agent through Vercel Connect`,CONNECT_CONNECTIONS=CONNECTION_CATALOG.filter(e=>e.auth.kind===`connect`);function connectionRows(e,t,n){let r=vercelAuthBlockerReason(t),i=CONNECT_CONNECTIONS.map(t=>{let i={value:t.slug,label:t.label};if(e.has(t.slug))return{...i,completed:!0,focusHint:`Already added`};let a=n[t.slug];return a===void 0?r===void 0?{...i,hint:t.hint}:{...i,disabled:!0,disabledReason:r,disabledReasonTone:`warning`}:{...i,disabled:!0,disabledReason:a,disabledReasonTone:`warning`}});return i.push({value:`done`,label:`Done`,trailingAction:!0}),i}async function pickConnection(e,t,n,r){let i=connectionRows(t,n,r),o={message:CONNECTIONS_PROMPT_MESSAGE,options:i,hintLayout:`inline`,search:!0,placeholder:`type to search MCP servers`};i.some(e=>e.value!==`done`&&e.disabled!==!0&&e.completed!==!0)||(o.initialValue=`done`);try{return await e.select(o)}catch(e){if(e instanceof WizardCancelledError)return;throw e}}async function runConnectionsFlow(s){let{appRoot:c,prompter:l,signal:u}=s,d={detectDeployment,detectPackageManager,ensureConnectionDependencies,getVercelAuthStatus,listAuthoredConnections,runLinkFlow,runPackageManagerInstall,...s.deps},[f,p,m]=await withSpinner(l,`Checking the project…`,()=>Promise.all([d.detectDeployment(c,{signal:u}),d.listAuthoredConnections(c),d.getVercelAuthStatus(c,{signal:u})]));u?.throwIfAborted();let h=inProjectSetupState(c,projectResolutionFromDeployment(f)),g=new Set(p),_=await pickConnection(l,g,m,s.disabledConnectionReasons??{});if(_===void 0)return{kind:`cancelled`};if(_===`done`||g.has(_))return{kind:`done`,addedConnections:[]};try{if(!isProjectResolved(h.project)){if((await d.runLinkFlow({appRoot:c,prompter:l,signal:u,projectSelection:`create-or-link`,teamSelectMessage:()=>`You need to link to a project to use Vercel Connect. Select your team`})).kind===`cancelled`)return{kind:`cancelled`};let e=projectResolutionFromDeployment(await withSpinner(l,`Checking the project…`,()=>d.detectDeployment(c,{signal:u})));if(!isProjectResolved(e))throw Error(`Project link was not found after linking.`);h={...h,project:e}}let t=await d.detectPackageManager(c);if(await d.ensureConnectionDependencies({projectRoot:c}),!await withPhase(l.log,`Installing connection dependencies (${t.kind} install)...`,()=>d.runPackageManagerInstall(t.kind,c,{onOutput:createPromptCommandOutput(l.log),signal:u})))throw Error(`Dependency installation failed. Run \`${t.kind} install\`.`);if((await runInteractive([selectConnections({asker:interactiveAsker(l),presetConnections:[_]}),addConnections({prompter:l,signal:u,deps:d.addConnections})],h,prompterSink(l),{snapshot:snapshotSetupState,signal:u})).kind!==`done`)return{kind:`cancelled`};if(!new Set(await d.listAuthoredConnections(c)).has(_))throw Error(`Connection "${_}" was not added.`);return{kind:`done`,addedConnections:[_]}}catch(e){return e instanceof WizardCancelledError?{kind:`cancelled`}:{kind:`failed`,addedConnections:new Set(await d.listAuthoredConnections(c)).has(_)?[_]:[],message:toErrorMessage(e)}}}export{CONNECTIONS_PROMPT_MESSAGE,runConnectionsFlow};