UNPKG

eve

Version:

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

1 lines 7.01 kB
import{readFile}from"node:fs/promises";import{dirname,isAbsolute,join,relative,resolve,sep}from"node:path";import{existsSync,readFileSync,realpathSync}from"node:fs";import{EVE_PACKAGE_NAME}from"#internal/package-name.js";import{STABLE_WORKFLOW_NAMES}from"#execution/stable-workflow-names.js";import{prepareAuthoredWorkflowDirectives}from"#internal/workflow-bundle/authored-workflow-directives.js";import{SUBAGENT_TOOL_EXECUTE_WORKFLOW_NAME}from"#runtime/subagents/workflow-reference.js";import{findWorkflowDirectiveFunctions,stripJavaScriptExtension,transformWorkflowDirectives}from"#internal/workflow-bundle/workflow-transformer.js";const packageJsonCache=new Map,projectDepsCache=new Map;async function applyWorkflowTransform(e,t,n,r,i,a=new Set([...STABLE_WORKFLOW_NAMES,SUBAGENT_TOOL_EXECUTE_WORKFLOW_NAME])){let o=i??process.cwd(),s=r===void 0?isAbsolute(e)?e:join(o,e):r,{moduleSpecifier:c,stableModuleSpecifier:l}=resolveModuleSpecifier(s,o,n===`metadata`);if(r!==void 0&&isAuthoredApplicationModule(r,o)){let e=n===!1?void 0:await prepareAuthoredWorkflowDirectives({filePath:r,source:t});return transformWorkflowDirectives({authored:!0,authorizeSteps:!0,filename:authoredRelativePath(r,o),mode:e?.hasDirectives===!0&&n,moduleSpecifier:authoredModuleIdBase(r,o),source:e?.source??t,stableModuleSpecifier:void 0,stableWorkflowNames:a})}return transformWorkflowDirectives({authorizeSteps:isPackageTestFixtureModule(s),filename:e,mode:n,moduleSpecifier:c,source:t,stableModuleSpecifier:l,stableWorkflowNames:a})}function isPackageTestFixtureModule(e){return e.replace(/\\/g,`/`).includes(`/src/internal/testing/`)}function isAuthoredApplicationModule(e,t){let n=toRealPath(t).replace(/\\/g,`/`).replace(/\/$/,``),r=toRealPath(e).replace(/\\/g,`/`);return!r.startsWith(`${n}/`)||isInNodeModules(r)?!1:findPackageJson(r)?.name!==EVE_PACKAGE_NAME}function authoredRelativePath(e,t){return toRelativeImportPath(toRealPath(e),toRealPath(t)).replace(/^\.\//,``)}function authoredModuleIdBase(e,t){return`./${stripJavaScriptExtension(authoredRelativePath(e,t))}`}function toRealPath(e){try{return realpathSync(e)}catch{return resolve(e)}}function isAuthoredApplicationRoot(e){let t=join(e,`package.json`);if(!existsSync(t))return!1;try{return JSON.parse(readFileSync(t,`utf8`)).name!==EVE_PACKAGE_NAME}catch{return!1}}async function findWorkflowPatterns(e,t){let n=await findWorkflowDirectiveFunctions(e,t);return{hasSerde:t.includes(`workflow.serde`)||t.includes(`@serde`)||t.includes(`workflowSerde`)||t.includes(`__workflow_serde`),hasUseStep:n.some(e=>e.directive===`use step`),hasUseWorkflow:n.some(e=>e.directive===`use workflow`)}}function getImportPath(e,t){let n=isInNodeModules(e),r=!n&&isWorkspacePackage(e,t);if(n||r){let n=findPackageJson(e);if(n!==null){let i=getProjectDependencies(t).has(n.name);if(!(r||i))return{importPath:toRelativeImportPath(e,t),isPackage:!1};let a=resolveExportSubpath(e,n);return a?{importPath:`${n.name}${a}`,isPackage:!0}:isRootEntrypointFile(e,n)?{importPath:n.name,isPackage:!0}:{importPath:toRelativeImportPath(e,t),isPackage:!1}}}return{importPath:toRelativeImportPath(e,t),isPackage:!1}}function resolveModuleSpecifier(e,t,n=!1){let r=isInNodeModules(e),i=!r&&isWorkspacePackage(e,t),a=findPackageJson(e);if(n&&a!==null)return{moduleSpecifier:`${a.name}@${a.version}`,stableModuleSpecifier:a.name};if(!r&&!i)return{moduleSpecifier:void 0,stableModuleSpecifier:a?.name===EVE_PACKAGE_NAME?EVE_PACKAGE_NAME:void 0};if(a===null)return{moduleSpecifier:void 0,stableModuleSpecifier:void 0};let o=resolveExportSubpath(e,a),s=o?`${a.name}${o}`:a.name;return{moduleSpecifier:`${s}@${a.version}`,stableModuleSpecifier:s}}function findPackageJson(e){let t=dirname(e),n=[];for(;t!==dirname(t);){let e=packageJsonCache.get(t);if(e!==void 0){for(let t of n)packageJsonCache.set(t,e);return e}n.push(t);let r=join(t,`package.json`);if(existsSync(r))try{let e=JSON.parse(readFileSync(r,`utf8`));if(typeof e.name==`string`&&typeof e.version==`string`){let r={dir:t,exports:e.exports,main:e.main,module:e.module,name:e.name,version:e.version};packageJsonCache.set(t,r);for(let e of n)packageJsonCache.set(e,r);return r}}catch{}t=dirname(t)}for(let e of n)packageJsonCache.set(e,null);return null}function resolveExportSubpath(e,t){if(t.exports===null||typeof t.exports!=`object`||Array.isArray(t.exports))return``;let n=e.replace(/\\/g,`/`),r=t.dir.replace(/\\/g,`/`),i=n.startsWith(`${r}/`)?`./${n.substring(r.length+1)}`:null;if(i===null)return``;for(let[e,n]of Object.entries(t.exports)){let t=resolveExportTarget(n);if(t!==null&&normalizeExportPath(t)===i)return e===`.`?``:e.substring(1)}return``}function resolveExportTarget(e){if(typeof e==`string`)return e;if(Array.isArray(e)){for(let t of e){let e=resolveExportTarget(t);if(e!==null)return e}return null}if(typeof e==`object`&&e)for(let t of[`workflow`,`default`,`require`,`import`,`node`]){let n=e[t],r=resolveExportTarget(n);if(r!==null)return r}return null}function normalizeExportPath(e){return e.startsWith(`./`)?e:`./${e}`}function isInNodeModules(e){return e.split(sep).join(`/`).includes(`/node_modules/`)}function getProjectDependencies(e){let t=projectDepsCache.get(e);if(t!==void 0)return t;let n=new Set,r=join(e,`package.json`);if(existsSync(r))try{let e=JSON.parse(readFileSync(r,`utf8`));for(let t of[`dependencies`,`devDependencies`,`peerDependencies`,`optionalDependencies`]){let r=e[t];if(typeof r==`object`&&r&&!Array.isArray(r))for(let e of Object.keys(r))n.add(e)}}catch{}return projectDepsCache.set(e,n),n}function isWorkspacePackage(e,t){if(isInNodeModules(e))return!1;let n=findPackageJson(e);return n===null||resolve(n.dir)===resolve(t)?!1:getProjectDependencies(t).has(n.name)}function toRelativeImportPath(e,t){let n=t.replace(/\\/g,`/`),r=e.replace(/\\/g,`/`),i=r.startsWith(`${n}/`)?r.substring(n.length+1):relative(t,e).replace(/\\/g,`/`);return i.startsWith(`.`)||(i=`./${i}`),i}function hasRootExport(e){if(typeof e==`string`||Array.isArray(e))return!0;if(typeof e!=`object`||!e)return!1;let t=Object.keys(e);return t.length>0&&t.every(e=>!e.startsWith(`.`))?!0:`.`in e}function normalizePackageTargetPath(e){let t=e.replace(/\\/g,`/`);return t.startsWith(`./`)?t.substring(2):t.startsWith(`/`)?t.substring(1):t}function isRootEntrypointFile(e,t){let n=e.replace(/\\/g,`/`),r=t.dir.replace(/\\/g,`/`);if(!n.startsWith(`${r}/`))return!1;let i=n.substring(r.length+1);if(t.exports!==void 0){let e;if(t.exports!==null&&typeof t.exports==`object`&&`.`in t.exports)e=t.exports[`.`];else if(hasRootExport(t.exports))e=t.exports;else return!1;let n=resolveExportTarget(e);return n!==null&&normalizePackageTargetPath(n)===i}return[t.module,t.main,`index.js`,`index.mjs`,`index.cjs`,`index.ts`,`index.mts`,`index.cts`].flatMap(e=>typeof e==`string`?[normalizePackageTargetPath(e)]:[]).includes(i)}async function readSourceFile(e){return await readFile(e,`utf8`)}export{applyWorkflowTransform,findWorkflowPatterns,getImportPath,isAuthoredApplicationModule,isAuthoredApplicationRoot,readSourceFile};