UNPKG

eve

Version:

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

5 lines 3.52 kB
import{resolvePackageDependencyPath,resolvePackageSourceFilePath}from"#internal/application/package.js";import{stringifyEsmImportSpecifier}from"#internal/application/import-specifier.js";import{getAllFrameworkChannelNames,getFrameworkChannelDefinitions}from"#runtime/framework-channels/index.js";const EVE_CHANNEL_VIRTUAL_ID_PREFIX=`#nitro/virtual/eve-channel/`;function computeChannelRouteRegistrations(e){let t=e.compileResult.manifest.channels,n=new Set,a=[],o=new Set,s=getAllFrameworkChannelNames();for(let e of t){if(e.kind===`disabled`){if(!s.has(e.name))throw Error(`agent/channels/${e.name}.ts exports disableRoute() but "${e.name}" is not a framework channel. Rename the file to one of: ${[...s].sort().join(`, `)}.`);o.add(e.name);continue}n.add(e.name),a.push({method:e.method,route:e.urlPath,cors:e.cors})}let c=getFrameworkChannelDefinitions().filter(e=>!n.has(e.name)&&!o.has(e.name)).map(e=>({method:e.method,route:e.urlPath,cors:e.cors})),l=new Set,u=[];for(let e of[...c,...a]){let t=createChannelRouteKey(e);l.has(t)||(l.add(t),u.push(e))}return u}function registerChannelVirtualHandlers(e,t){let n=new Set;for(let r of t.registrations)addChannelVirtualHandler(e,{artifactsConfig:t.artifactsConfig,cors:r.cors,method:r.method,preflightRoutes:n,route:r.route})}function createChannelRouteKey(e){return`${e.method.toUpperCase()} ${e.route}`}function addChannelVirtualHandler(r,i){let o=createChannelRouteKey(i),s=`${EVE_CHANNEL_VIRTUAL_ID_PREFIX}${o}`,c=stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/nitro/routes/channel-dispatch.ts`)),l=stringifyEsmImportSpecifier(resolvePackageDependencyPath(`nitro`)),u=stringifyEsmImportSpecifier(resolvePackageDependencyPath(`nitro/h3`));if(i.method===`WEBSOCKET`){r.options.handlers.push({handler:s,route:i.route}),r.options.virtual[s]=[`import { defineWebSocketHandler } from ${l};`,`import { dispatchChannelWebSocketRequest } from ${c};`,`const config = ${JSON.stringify(i.artifactsConfig)};`,`export default defineWebSocketHandler((event) => dispatchChannelWebSocketRequest(event, ${JSON.stringify(o)}, config));`].join(` `);return}r.options.handlers.push({handler:s,method:i.method,route:i.route}),i.cors!==void 0&&addChannelCorsPreflightHandler(r,{cors:i.cors,nitroH3ModulePath:u,preflightRoutes:i.preflightRoutes,route:i.route}),r.options.virtual[s]=[...i.cors===void 0?[]:[`import { handleCors } from ${u};`,`const cors = ${JSON.stringify(i.cors)};`],`import { dispatchChannelRequest } from ${c};`,`const config = ${JSON.stringify(i.artifactsConfig)};`,i.cors===void 0?`export default (event) => dispatchChannelRequest(event, ${JSON.stringify(o)}, config);`:[`export default (event) => {`,` const corsResponse = handleCors(event, cors);`,` if (corsResponse !== false) return corsResponse;`,` return dispatchChannelRequest(event, ${JSON.stringify(o)}, config);`,`};`].join(` `)].join(` `)}function addChannelCorsPreflightHandler(e,t){if(t.preflightRoutes.has(t.route))return;t.preflightRoutes.add(t.route);let n=`${EVE_CHANNEL_VIRTUAL_ID_PREFIX}${`OPTIONS ${t.route}`}`;e.options.handlers.push({handler:n,method:`OPTIONS`,route:t.route}),e.options.virtual[n]=[`import { handleCors } from ${t.nitroH3ModulePath};`,`const cors = ${JSON.stringify(t.cors)};`,`export default (event) => {`,` const corsResponse = handleCors(event, cors);`,` if (corsResponse !== false) return corsResponse;`,` return new Response(null, { status: 204 });`,`};`].join(` `)}export{computeChannelRouteRegistrations,registerChannelVirtualHandlers};