UNPKG

eve

Version:

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

1 lines • 3.1 kB
import{mkdir,readFile,writeFile}from"node:fs/promises";import{dirname,join}from"node:path";import{findClosestLinkedVercelDirectory,findClosestVercelOutputDirectory}from"#shared/vercel-output-directory.js";import{findConfiguredEveServiceEntry,resolveServicePrefix}from"#internal/vercel/vercel-service-config-operations.js";import{createServiceConfigRecord,hasServices,parseVercelServicesConfig}from"#internal/vercel/vercel-services-config.js";import{assembleEveVercelServices}from"#internal/vercel/assemble-eve-services.js";const VERCEL_JSON_FILE_NAME=`vercel.json`,VERCEL_OUTPUT_CONFIG_FILE_NAME=`.vercel/output/config.json`;async function resolveVercelOutputConfigLocation(e){let t=await findClosestLinkedVercelDirectory(e),n=t===void 0?e:dirname(t),r=await findClosestVercelOutputDirectory(e);return r===void 0?t===void 0?{canWriteGeneratedOutput:!!process.env.VERCEL,outputConfigPath:join(e,VERCEL_OUTPUT_CONFIG_FILE_NAME),projectRoot:n}:{canWriteGeneratedOutput:!0,outputConfigPath:join(t,`output`,`config.json`),projectRoot:n}:{canWriteGeneratedOutput:!0,outputConfigPath:join(r,`config.json`),projectRoot:n}}async function readVercelServicesConfig(e,t){try{return parseVercelServicesConfig(JSON.parse(await readFile(e,`utf8`)),t)}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return{};throw e}}function resolveConfiguredServicePrefix(e){let t=findConfiguredEveServiceEntry(e.services,e.agent)?.service;return resolveServicePrefix(t)??e.agent.servicePrefix}function assertRootServicesIncludeEve(e){let t=[];for(let n of e.agents){let r=findConfiguredEveServiceEntry(e.services,n)?.service;if(r===void 0)throw Error(`${VERCEL_JSON_FILE_NAME} already defines services, so withEve cannot add generated eve services through ${VERCEL_OUTPUT_CONFIG_FILE_NAME}. Add the eve service for ${n.name??`the default agent`} to ${VERCEL_JSON_FILE_NAME}, or remove services from ${VERCEL_JSON_FILE_NAME}.`);t.push({name:n.name,servicePrefix:resolveServicePrefix(r)??n.servicePrefix})}return t}async function ensureEveVercelOutputConfig(e){let{canWriteGeneratedOutput:t,outputConfigPath:n,projectRoot:r}=await resolveVercelOutputConfigLocation(e.nextRoot),i=(await readVercelServicesConfig(join(r,VERCEL_JSON_FILE_NAME),VERCEL_JSON_FILE_NAME)).services;if(hasServices(i))return{agents:assertRootServicesIncludeEve({agents:e.agents,services:createServiceConfigRecord(i)})};let a=await readVercelServicesConfig(n,VERCEL_OUTPUT_CONFIG_FILE_NAME),o=createServiceConfigRecord(a.services),s=e.agents.map(e=>({name:e.name,servicePrefix:resolveConfiguredServicePrefix({agent:e,services:o})}));if(!t)return{agents:s};let c=assembleEveVercelServices({agents:e.agents.map(t=>({agent:t,target:{hostOutputDirectory:dirname(n),projectRoot:e.nextRoot}})),routes:a.routes,services:o});await Promise.all(c.rootDirectories.map(e=>mkdir(e,{recursive:!0})));let{services:l,...u}=a,d={...u,routes:c.routes,services:c.services,version:3};return JSON.stringify(a)!==JSON.stringify(d)&&(await mkdir(dirname(n),{recursive:!0}),await writeFile(n,`${JSON.stringify(d,null,2)}\n`)),{agents:s}}export{ensureEveVercelOutputConfig};