UNPKG

eve

Version:

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

17 lines 12.7 kB
import{copyFile,mkdir,writeFile}from"node:fs/promises";import{join}from"node:path";import{resolvePackageCompiledFilePath,resolvePackageSourceFilePath}from"#internal/application/package.js";import{buildPackageUserAgent}from"#internal/user-agent.js";import{stringifyEsmImportSpecifier}from"#internal/application/import-specifier.js";import{usesParentDevelopmentWorkflowWorld}from"#internal/workflow/development-world-protocol.js";import{prepareAuthoredRuntimeModules}from"#internal/authored-runtime-modules.js";import{readMaterializedAuthoredModuleIndex}from"#internal/materialized-authored-modules.js";import"#internal/instrumentation-layout.js";import{resolveWorkflowWorldImport}from"#internal/workflow/world-target.js";async function writeCompiledArtifactsFiles(e){let t=join(e.outDir,`compiled-artifacts-bootstrap.mjs`),n=join(e.outDir,`compiled-artifacts-module-map.mjs`),r=join(e.outDir,`compiled-artifacts-instrumentation.mjs`),i=join(e.outDir,`compiled-artifacts-workflow-world.mjs`),a=await prepareAuthoredRuntimeModules({manifest:e.compileResult.manifest,moduleMapPath:e.compileResult.paths.moduleMapPath});await mkdir(e.outDir,{recursive:!0}),await writeFile(n,a.moduleMapCode),await writeFile(t,createCompiledArtifactsBootstrapSource({compileResult:e.compileResult,installModulePath:resolvePackageSourceFilePath(`src/runtime/loaders/bundled-artifacts.ts`),metadata:e.compileResult.metadata,moduleMapImportPath:n})),await writeFile(i,createWorkflowWorldPluginSource({compiledArtifactsBootstrapPath:t,configuredWorld:e.compileResult.manifest.config.experimental?.workflow?.world,defaultWorld:e.defaultWorkflowWorld}));let o={authoredWorkflowModules:a.authoredWorkflowModules,bootstrapPath:t,workflowWorldPluginPath:i};if(e.compileResult.manifest.instrumentation!==void 0){let t=e.compileResult.manifest.instrumentation.sourceId;await writeFile(r,createInstrumentationPluginSource({agentName:e.compileResult.manifest.config.name,layout:{kind:`module-map`,moduleMapPath:n,sourceId:t}})),o.instrumentationPluginPath=r,o.instrumentationLayout={kind:`file`},o.instrumentationSourcePaths=[n]}else if(a.instrumentation!==void 0){let t=await writePreparedInstrumentation({instrumentation:a.instrumentation,outDir:e.outDir});await writeFile(r,createInstrumentationPluginSource({agentName:e.compileResult.manifest.config.name,layout:t})),o.instrumentationPluginPath=r,o.instrumentationLayout=generatedInstrumentationLayout(t),o.instrumentationSourcePaths=instrumentationSourcePathsOf(t)}return o}async function writePreparedInstrumentation(e){let writeOne=async(t,n)=>{let r=join(e.outDir,`compiled-artifacts-instrumentation-${t}.mjs`);return await writeFile(r,n),r},t={};for(let[n,r]of Object.entries(e.instrumentation.moduleCodeBySlot))t[n]=await writeOne(n,r);return{kind:`directory`,modulePathsBySlot:t}}function instrumentationSourcePathsOf(e){return e.kind===`file`?[e.modulePath]:Object.values(e.modulePathsBySlot)}async function writeDevelopmentCompiledArtifactsFiles(e){let t=join(e.outDir,`compiled-artifacts-bootstrap.mjs`),n=join(e.outDir,`compiled-artifacts-instrumentation.mjs`),r=join(e.outDir,`compiled-artifacts-workflow-world.mjs`),i=await readMaterializedAuthoredModuleIndex(e.runtimeAppRoot);if(i===void 0)throw Error(`Development generation at "${e.runtimeAppRoot}" is not materialized.`);await mkdir(e.outDir,{recursive:!0}),await writeFile(t,createDevelopmentCompiledArtifactsBootstrapSource(e.compileResult.manifest.config.name)),await writeFile(r,createDevelopmentWorkflowWorldPluginSource({compiledArtifactsBootstrapPath:t,configuredWorld:e.compileResult.manifest.config.experimental?.workflow?.world}));let a={authoredWorkflowModules:e.authoredWorkflowModules,bootstrapPath:t,workflowWorldPluginPath:r};if(e.compileResult.manifest.instrumentation!==void 0){let t=join(e.runtimeAppRoot,`.eve`,`compile`),r=join(e.outDir,`compiled-artifacts-instrumentation-source.mjs`);await copyFile(join(t,i.moduleMap),r),await writeFile(n,createInstrumentationPluginSource({agentName:e.compileResult.manifest.config.name,layout:{kind:`module-map`,moduleMapPath:r,sourceId:e.compileResult.manifest.instrumentation.sourceId}})),a.instrumentationPluginPath=n,a.instrumentationLayout={kind:`file`},a.instrumentationSourcePaths=[r]}else if(i.instrumentation!==void 0){let t=await copyMaterializedInstrumentation({instrumentation:i.instrumentation,outDir:e.outDir,runtimeAppRoot:e.runtimeAppRoot});await writeFile(n,createInstrumentationPluginSource({agentName:e.compileResult.manifest.config.name,layout:t})),a.instrumentationPluginPath=n,a.instrumentationLayout=generatedInstrumentationLayout(t),a.instrumentationSourcePaths=instrumentationSourcePathsOf(t)}return a}function generatedInstrumentationLayout(e){return e.kind===`file`?{kind:`file`}:{kind:`directory`,slots:Object.keys(e.modulePathsBySlot)}}async function copyMaterializedInstrumentation(e){let t=join(e.runtimeAppRoot,`.eve`,`compile`),copyOne=async(n,r)=>{let i=join(e.outDir,`compiled-artifacts-instrumentation-${n}.mjs`);return await copyFile(join(t,r),i),i};if(e.instrumentation.kind===`file`)return{kind:`file`,modulePath:await copyOne(`source`,e.instrumentation.modulePath)};let n={};for(let[t,r]of Object.entries(e.instrumentation.modulePathsBySlot))n[t]=await copyOne(t,r);return{kind:`directory`,modulePathsBySlot:n}}function createDevelopmentCompiledArtifactsBootstrapSource(e){return[`// Generated by eve. Do not edit by hand.`,`import { installEveWorkflowQueueNamespace } from ${stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/workflow/queue-namespace.ts`))};`,``,`installEveWorkflowQueueNamespace(${JSON.stringify(e)});`,``,`export default function installDevelopmentCompiledArtifactsPlugin() {}`,``].join(` `)}function createCompiledArtifactsBootstrapSource(e){let t=e.compileResult.manifest.config.name;return[`// Generated by eve. Do not edit by hand.`,`import moduleMap from ${stringifyEsmImportSpecifier(e.moduleMapImportPath)};`,`import { installBundledCompiledArtifacts } from ${stringifyEsmImportSpecifier(e.installModulePath)};`,`import { installEveWorkflowQueueNamespace } from ${stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/workflow/queue-namespace.ts`))};`,``,`installEveWorkflowQueueNamespace(${JSON.stringify(t)});`,``,`const metadata = ${JSON.stringify(e.metadata,null,2)};`,``,`const manifest = ${JSON.stringify(e.compileResult.manifest,null,2)};`,``,`export { moduleMap };`,``,`export function installCompiledArtifactsBootstrap() {`,` installBundledCompiledArtifacts({`,` manifest,`,` metadata,`,` moduleMap,`,` });`,`}`,``,`installCompiledArtifactsBootstrap();`,``,`// Default export satisfies the Nitro plugin contract so this file`,`// can be used directly as a Nitro plugin without a separate wrapper.`,`export default function installCompiledArtifactsPlugin() {`,` // Already installed on import above.`,`}`,``].join(` `)}function resolveWorkflowWorldWiring(e){if(e===`@workflow/world-local`){let t=stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/workflow/local-world-data-directory.ts`)),n=stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/workflow/local-world-delivery-timeouts.ts`));return{moduleImportSpecifier:resolvePackageCompiledFilePath(`src/compiled/${e}/index.js`),extraImportLines:` import { applyLocalWorkflowWorldDeliveryTimeoutDefaults } from ${n}; import { resolveLocalWorkflowWorldDataDirectory } from ${t};`.trimStart().split(` `),runtimeImports:`getWorld, setWorld`,createWorldSource:`applyLocalWorkflowWorldDeliveryTimeoutDefaults(); const workflowWorld = await workflowWorldModule.createWorld({ dataDir: resolveLocalWorkflowWorldDataDirectory(process.cwd()), });`}}return e===`@workflow/world-vercel`?{moduleImportSpecifier:resolvePackageCompiledFilePath(`src/compiled/${e}/index.js`),extraImportLines:[],runtimeImports:`getWorld, setWorld`,createWorldSource:` const workflowWorld = await workflowWorldModule.createWorld({ headers: { "User-Agent": ${JSON.stringify(buildPackageUserAgent())} }, });`.trimStart()}:{moduleImportSpecifier:e,extraImportLines:[],runtimeImports:`createWorldFromModule, getWorld, setWorld`,createWorldSource:`const workflowWorld = await createWorldFromModule(workflowWorldModule);`}}function createWorkflowWorldPluginSource(e){let t=e.configuredWorld??e.defaultWorld,n=resolveWorkflowWorldWiring(resolveWorkflowWorldImport(t)),r=resolvePackageCompiledFilePath(`src/compiled/@workflow/core/runtime.js`),i=resolvePackageSourceFilePath(`src/internal/workflow/validate-world.ts`);return[`// Generated by eve. Do not edit by hand.`,`import ${stringifyEsmImportSpecifier(e.compiledArtifactsBootstrapPath)};`,`import * as workflowWorldModule from ${stringifyEsmImportSpecifier(n.moduleImportSpecifier)};`,...n.extraImportLines,`import { ${n.runtimeImports} } from ${stringifyEsmImportSpecifier(r)};`,`import { validateWorkflowWorld } from ${stringifyEsmImportSpecifier(i)};`,``,n.createWorldSource,`validateWorkflowWorld({ packageName: ${JSON.stringify(e.configuredWorld)}, world: workflowWorld });`,`setWorld(workflowWorld);`,`await getWorld();`,`await workflowWorld.start?.();`,``,`export default function installWorkflowWorldPlugin() {}`,``].join(` `)}function createDevelopmentWorkflowWorldPluginSource(e){if(!usesParentDevelopmentWorkflowWorld(e.configuredWorld))return createWorkflowWorldPluginSource({...e,defaultWorld:`local`});let t=resolvePackageCompiledFilePath(`src/compiled/@workflow/core/runtime.js`),n=resolvePackageSourceFilePath(`src/internal/workflow/development-world-client.ts`);return[`// Generated by eve. Do not edit by hand.`,`import ${stringifyEsmImportSpecifier(e.compiledArtifactsBootstrapPath)};`,`import { getWorld, setWorld } from ${stringifyEsmImportSpecifier(t)};`,`import { createDevelopmentWorkflowWorld } from ${stringifyEsmImportSpecifier(n)};`,``,`setWorld(createDevelopmentWorkflowWorld());`,`await getWorld();`,``,`export default function installDevelopmentWorkflowWorldPlugin() {}`,``].join(` `)}function createInstrumentationPluginSource(e){let t=JSON.stringify(e.agentName);if(e.layout.kind===`module-map`){let n=resolvePackageSourceFilePath(`src/instrumentation/config.ts`);return[`// Generated by eve. Do not edit by hand.`,`import { moduleMap } from ${stringifyEsmImportSpecifier(e.layout.moduleMapPath)};`,`import { registerInstrumentationConfig } from ${stringifyEsmImportSpecifier(n)};`,``,`const instrumentationModule = moduleMap.nodes["__root__"].modules[${JSON.stringify(e.layout.sourceId)}];`,`if (instrumentationModule.default != null) {`,` await registerInstrumentationConfig(instrumentationModule.default, { agentName: ${t} });`,`}`,``,`export default function installInstrumentationPlugin() {}`,``].join(` `)}if(e.layout.kind===`file`){let n=resolvePackageSourceFilePath(`src/instrumentation/config.ts`);return[`// Generated by eve. Do not edit by hand.`,`import * as instrumentationModule from ${stringifyEsmImportSpecifier(e.layout.modulePath)};`,`import { registerInstrumentationConfig } from ${stringifyEsmImportSpecifier(n)};`,``,`if (instrumentationModule.default != null) {`,` await registerInstrumentationConfig(instrumentationModule.default, { agentName: ${t} });`,`}`,``,`// Default export satisfies the Nitro plugin contract so this file`,`// can be used directly as a Nitro plugin without a separate wrapper.`,`export default function installInstrumentationPlugin() {}`,``].join(` `)}let n=resolvePackageSourceFilePath(`src/instrumentation/providers.ts`),r=Object.entries(e.layout.modulePathsBySlot);return[`// Generated by eve. Do not edit by hand.`,...r.map(([e,t],n)=>`import * as provider${n} from ${stringifyEsmImportSpecifier(t)}; // ${e}`),`import { finalizeInstrumentationProviders, registerInstrumentationProvider, seedInstrumentationProviders, shutdownInstrumentationProviders } from ${stringifyEsmImportSpecifier(n)};`,``,`seedInstrumentationProviders();`,``,...r.flatMap(([e],n)=>[`await registerInstrumentationProvider({`,` agentName: ${t},`,` slot: ${JSON.stringify(e)},`,` value: provider${n}.default,`,`});`]),``,`finalizeInstrumentationProviders({ serviceName: ${t} });`,``,`// Default export satisfies the Nitro plugin contract so this file`,`// can be used directly as a Nitro plugin without a separate wrapper.`,`export default function installInstrumentationPlugin(nitroApp) {`,` // The last point a buffered exporter can still reach the network.`,` nitroApp?.hooks?.hook('close', async () => {`,` await shutdownInstrumentationProviders();`,` });`,`}`,``].join(` `)}export{createCompiledArtifactsBootstrapSource,createDevelopmentWorkflowWorldPluginSource,createWorkflowWorldPluginSource,writeCompiledArtifactsFiles,writeDevelopmentCompiledArtifactsFiles};