UNPKG

eve

Version:

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

3 lines (2 loc) 2.88 kB
import{remoteHost}from"./target.js";import{createTuiPrompter}from"./tui-prompter.js";import{describeRemoteAuthCompletedMutations}from"./remote-auth-result.js";import{runRemoteAuthFlow}from"./remote-auth.js";import{WizardCancelledError}from"#setup/step.js";function shouldConfigureTrustedSources(e){switch(e.state){case`auth-required`:case`auth-failed`:return e.challenge.kind===`vercel-deployment-protection`;case`unavailable`:return e.failure.code===`TRUSTED_SOURCES_ENVIRONMENT_MISMATCH`;case`checking`:case`authenticating`:case`ready`:return!1}}function unavailableAfterAuthentication(e,t){let[n=t,...r]=t.split(/\n\s*\n/u);return[`Authentication was refreshed, but ${e} is unavailable: ${/[.!?]$/u.test(n.trim())?n.trim():`${n.trim()}.`}`,...r.map(e=>e.trim()).filter(e=>e.length>0)].join(` `)}function mutedRenderer(e,t){return{readSelect:n=>t()?Promise.resolve(void 0):e.readSelect(n),readEditableSelect:n=>t()?Promise.resolve(void 0):e.readEditableSelect(n),readText:n=>t()?Promise.resolve(void 0):e.readText(n),readAcknowledge:n=>t()?Promise.resolve():e.readAcknowledge(n),readChoice:n=>t()?{choice:Promise.resolve(void 0),close:()=>{}}:e.readChoice(n),setStatus:n=>{t()||e.setStatus(n)},renderLine:(n,r)=>{(!t()||r===`warning`||r===`error`)&&e.renderLine(n,r)},renderOutput:n=>{t()||e.renderOutput(n)},withInheritedStdio:t=>e.withInheritedStdio(t),withExclusiveTerminal:t=>e.withExclusiveTerminal?.(t)??e.withInheritedStdio(t)}}async function runRemoteAuthCommand(e){e.renderer.begin(`Authenticate via Vercel OIDC`,`pulse`);let t=!0,n=!1,r=new AbortController,i=e.renderer.waitForInterrupt(),a=e.connection.current().target;try{let o=e.flow??runRemoteAuthFlow,s=shouldConfigureTrustedSources(e.connection.current().connection),c=createTuiPrompter(mutedRenderer(e.renderer,()=>n)),l=e.connection.authenticate(async e=>{let n=await o({workspaceRoot:a.workspaceRoot,serverUrl:a.serverUrl,configureTrustedSources:s,prompter:c,signal:e});return t=n.kind===`failed`,n},r.signal),u=await Promise.race([l.then(e=>({kind:`completed`,outcome:e})),i.promise.then(()=>({kind:`interrupted`}))]);u.kind===`interrupted`&&(n=!0,t=!0,r.abort(new WizardCancelledError));let d=u.kind===`completed`?u.outcome:await l;switch(d.kind){case`authenticated`:return`Authenticated ${remoteHost(a)} via Vercel OIDC.`;case`cancelled`:if(n){let e=describeRemoteAuthCompletedMutations(d.completedMutations);return e.length===0?`/vc:login interrupted.`:`/vc:login interrupted. Completed before interruption: ${e.join(`, `)}.`}return d.completedMutations.some(e=>e.kind===`vercel-login`)?`/vc:login dismissed after logging in to Vercel.`:`/vc:login dismissed.`;case`failed`:return d.message;case`unavailable`:return unavailableAfterAuthentication(remoteHost(a),d.failure.message)}}finally{i.dispose(),e.renderer.setStatus(void 0),e.renderer.end({preserveDiagnostics:t})}}export{runRemoteAuthCommand};