eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.71 kB
JavaScript
import{r as e}from"./chunk-BHKSVoKr.js";import{n as t,o as n,w as r}from"./dist-Dxrjttr2.js";import{E as i,Gt as a,It as o,Nt as s,Y as c,a as l,cn as u,en as d,fn as f,it as p,m,n as h,nt as g,q as _,r as v,rt as y,y as b,yn as x,z as S}from"./attribute-changes-Bi5DLT8S.js";var C=e({getHookByToken:()=>T,resumeHook:()=>E,resumeWebhook:()=>D});async function w(e){let t=await u(),n=await t.hooks.getByToken(e),r=await t.runs.get(n.runId),a=await t.getEncryptionKeyForRun?.(r),o=a?await f(a):void 0;return n.metadata!==void 0&&(n.metadata=await i(n.metadata,n.runId,o)),{hook:n,run:r,encryptionKey:o}}async function T(e){let{hook:t}=await w(e);return t}async function E(e,t,n){return await v(()=>c(`hook.resume`,async r=>{let i=await u();try{let c,u,d;if(typeof e==`string`){let t=await w(e);c=t.hook,u=t.run,d=n??t.encryptionKey}else if(c=e,u=await i.runs.get(c.runId),n)d=n;else{let e=await i.getEncryptionKeyForRun?.(u);d=e?await f(e):void 0}r?.setAttributes({...p(c.token),...y(c.hookId),...o(c.runId)});let g=u.executionContext?.workflowCoreVersion,v=m(typeof g==`string`?g:void 0);v.supportedFormats.has(a.ENCRYPTED)||(d=void 0);let C=(u.specVersion??0)>=5&&v.supportedFormats.has(a.GZIP),T=[],E=x(c.specVersion),D=await b(t,c.runId,d,T,globalThis,E,v.framedByteStreams,C);h(Promise.all(T),e=>{e!==void 0&&S.warn(`Background flush of hook payload ops failed`,{workflowRunId:c.runId,hookId:c.hookId,error:e instanceof Error?e.message:String(e)})}),await i.events.create(c.runId,{eventType:`hook_received`,specVersion:5,correlationId:c.hookId,eventData:{...E?{}:{token:c.token},payload:D}},{v1Compat:E}),r?.setAttributes({...s(u.workflowName)});let O=await _(u.executionContext?.traceCarrier);return O&&r?.addLink?.(O),await i.queue(l(u.workflowName),{runId:c.runId,traceCarrier:u.executionContext?.traceCarrier??void 0},{deploymentId:u.deploymentId,specVersion:u.specVersion??1}),c}catch(t){throw r?.setAttributes({...p(typeof e==`string`?e:e.token),...g(!1)}),t}}))}async function D(e,i){let{hook:a,encryptionKey:o}=await w(e);if(a.isWebhook===!1)throw new n(e);let s,c;if(a.metadata&&typeof a.metadata==`object`&&`respondWith`in a.metadata)if(a.metadata.respondWith===`manual`){let{readable:e,writable:t}=new TransformStream;c=e,i[d]=t}else if(a.metadata.respondWith instanceof Response)s=a.metadata.respondWith;else throw new r(`Invalid \`respondWith\` value: ${a.metadata.respondWith}`,{slug:t.WEBHOOK_INVALID_RESPOND_WITH_VALUE});else s=new Response(null,{status:202});if(await E(a,i,o),c){let e=c.getReader(),t=await e.read();t.value&&(s=t.value),e.cancel()}if(!s)throw new r(`Workflow run did not send a response`,{slug:t.WEBHOOK_RESPONSE_NOT_SENT});return s}export{C as i,E as n,D as r,T as t};