UNPKG

eve

Version:

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

1 lines 2.65 kB
const WORKFLOW_CONTEXT_SYMBOL=Symbol.for(`WORKFLOW_CONTEXT`),WORKFLOW_CREATE_HOOK=Symbol.for(`WORKFLOW_CREATE_HOOK`),WORKFLOW_GET_STREAM_ID=Symbol.for(`WORKFLOW_GET_STREAM_ID`),WORKFLOW_SLEEP=Symbol.for(`WORKFLOW_SLEEP`),WORKFLOW_USE_STEP=Symbol.for(`WORKFLOW_USE_STEP`),STREAM_NAME_SYMBOL=Symbol.for(`WORKFLOW_STREAM_NAME`),workflowGlobal=globalThis;var RetryableError=class extends Error{constructor(e){super(e),this.name=`RetryableError`}},FatalError=class extends Error{fatal=!0;constructor(e){super(e),this.name=`FatalError`}};function createHook(e){let t=workflowGlobal[WORKFLOW_CREATE_HOOK];if(t===void 0)throw Error("`createHook()` can only be called inside a workflow function");return t(e)}function getWorkflowMetadata(){let e=workflowGlobal[WORKFLOW_CONTEXT_SYMBOL];if(e===void 0)throw Error("`getWorkflowMetadata()` can only be called inside a workflow or step function");return e}function getWritable(e={}){let t=workflowGlobal[WORKFLOW_GET_STREAM_ID];if(t===void 0)throw Error("`getWritable()` can only be called inside a workflow function");let n=t(e.namespace);return Object.create(globalThis.WritableStream.prototype,{[STREAM_NAME_SYMBOL]:{value:n,writable:!1}})}function createWebhook(e){let{respondWith:t,token:n,...r}=e??{};if(n!==void 0)throw Error("`createWebhook()` does not accept a `token` option. Use `createHook()` with `resumeHook()` for deterministic tokens.");let i=createHook({...r,metadata:t===void 0?void 0:{respondWith:t},isWebhook:!0}),a=getWorkflowMetadata();return i.url=`${typeof a.url==`string`?a.url:``}/.well-known/workflow/v1/webhook/${encodeURIComponent(i.token)}`,i}function defineHook(){return{create:createHook,resume(){throw Error("`defineHook().resume()` can only be called from external contexts.")}}}function sleep(e){let t=workflowGlobal[WORKFLOW_SLEEP];if(t===void 0)throw Error("`sleep()` can only be called inside a workflow function");return t(e)}function resumeHook(){throw Error("`resumeHook()` can only be called from outside a workflow function")}function getStepMetadata(){throw Error("`getStepMetadata()` can only be called inside a step function")}async function setAttributes(e,t={}){let n=Object.entries(e);if(n.length===0)return;let r=workflowGlobal[WORKFLOW_USE_STEP];if(r===void 0)throw Error("`setAttributes()` can only be called inside a workflow runtime context");let i=n.map(([e,t])=>({key:e,value:t===void 0?null:t})),a=t.allowReservedAttributes===!0?{allowReservedAttributes:!0}:{};await r(`__builtin_set_attributes`)(i,a)}export{FatalError,RetryableError,createHook,createWebhook,defineHook,getStepMetadata,getWorkflowMetadata,getWritable,resumeHook,setAttributes,sleep};