eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 8.58 kB
JavaScript
import{d as e}from"./dist-FLIfyJ4Y.js";import{C as t,S as n,T as r,b as i,m as a,r as o,w as s,x as c}from"./dist-Dxrjttr2.js";import{A as l,C as u,Dt as d,Gt as f,It as p,J as m,L as h,Lt as g,Nt as _,Pt as v,Q as y,T as b,Y as x,_n as S,a as C,b as w,cn as T,fn as E,k as D,m as O,n as k,on as A,p as j,r as M,s as N,sn as P,t as F,yn as I,z as L}from"./attribute-changes-Bi5DLT8S.js";const R=S();let z=!1;async function B(t,n,r){"use step";return await M(()=>{let i=t?.workflowId;if(!i)throw new s(`'start' received an invalid workflow function. Ensure the Workflow SDK is configured correctly and the function includes a 'use workflow' directive.`,{slug:`start-invalid-workflow-function`});return x(`workflow.start ${e(i)}`,async e=>{e?.setAttributes({..._(i),...v(`start`)});let t=[],a=r??{};Array.isArray(n)?t=n:typeof n==`object`&&(a=n),e?.setAttributes({...d(t.length)});let c=a?.world??await T(),l=await c.getDeploymentId(),u=a.deploymentId??l;u===`latest`&&(c.resolveLatestDeploymentId?u=await c.resolveLatestDeploymentId():(z||(z=!0,L.warn(`deploymentId: 'latest' has no effect in this world and was ignored. It is only supported by worlds with atomic deployments, such as Vercel. The run will target the current deployment.`,{currentDeploymentId:l})),u=l));let h,b;if(u===l)h=!0,b=!0;else if(typeof c.streams?.get!=`function`)h=!1,b=!1;else{let e=O((await N(c,`workflow`,{deploymentId:u,timeout:2e3}).catch(()=>void 0))?.workflowCoreVersion);h=e.framedByteStreams,b=e.supportedFormats.has(f.GZIP)}let x=[],S=`wrun_${R()}`,D=await m(),A=a.specVersion??c.specVersion??2,M=I(A),P=a.allowReservedAttributes===!0,B;if(a.attributes&&Object.keys(a.attributes).length>0){if(A<4)throw new s(`Initial workflow attributes require a World that supports spec version 4 or later.`);for(let[e,t]of Object.entries(a.attributes))if(typeof t!=`string`)throw new s(`Initial workflow attribute ${JSON.stringify(e)} must be a string value.`);let e=F(a.attributes,{allowReservedAttributes:P});B=Object.fromEntries(e.map(({key:e,value:t})=>[e,t]))}let H=B?{attributes:B,...P?{allowReservedAttributes:!0}:{}}:{},U=await c.getEncryptionKeyForRun?.(S,{...a,deploymentId:u}),W=U?await E(U):void 0,G=await w(t,S,W,x,globalThis,M,h,b&&A>=5),K={traceCarrier:D,workflowCoreVersion:j,features:{encryption:!!W}},[q,J]=await Promise.allSettled([c.events.create(S,{eventType:`run_created`,specVersion:A,eventData:{deploymentId:u,workflowName:i,input:G,executionContext:K,...H}},{v1Compat:M}),c.queue(C(i),{runId:S,traceCarrier:D,...A>=3?{runInput:{input:G,deploymentId:u,workflowName:i,specVersion:A,executionContext:K,...H}}:{}},{deploymentId:u,specVersion:A})]);if(J.status===`rejected`)throw J.reason;let X=!1;if(q.status===`rejected`){let e=q.reason;if(!o.is(e))if(V(e))X=!0,L.warn(`Run creation event failed, but the run was accepted via the queue. The run_created event will be re-tried async by the runtime.`,{workflowRunId:S,error:e.message});else throw e}else{let e=q.value;if(!e.run)throw new s(`Missing 'run' in server response for 'run_created' event`);if(!M&&e.run.runId!==S)throw new s(`Server returned different runId than requested: expected ${S}, got ${e.run.runId}`)}return k(Promise.all(x),e=>{L.warn(`Background flush of workflow argument streams failed`,{workflowRunId:S,error:e instanceof Error?e.message:String(e)})}),e?.setAttributes({...p(S),...y(u),...q.status===`fulfilled`&&q.value.run?g(q.value.run.status):{}}),new Y(S,{resilientStart:X})})})}function V(e){return!!(a.is(e)||r.is(e)&&e.status&&e.status>=500)}const H=e=>Array.isArray(e)?e:[e];async function U(e,t,n={}){try{let r=await e.runs.get(t,{resolveData:`all`}),i=await e.getEncryptionKeyForRun?.(r),a=i?await E(i):void 0,o=H(await D(r.input,t,a,globalThis)),s=n.specVersion??r.specVersion??1,c=n.deploymentId??r.deploymentId;return(await B({workflowId:r.workflowName},o,{deploymentId:c,world:e,specVersion:s})).runId}catch(e){throw Error(`Failed to recreate run from ${t}: ${e instanceof Error?e.message:String(e)}`,{cause:e})}}async function W(e,t){try{let n=(await e.runs.get(t,{resolveData:`none`})).specVersion??1,r=I(n),i={eventType:`run_cancelled`,specVersion:n};await e.events.create(t,i,{v1Compat:r})}catch(e){throw Error(`Failed to cancel run ${t}: ${e instanceof Error?e.message:String(e)}`,{cause:e})}}async function G(e,t){try{let n=await e.runs.get(t,{resolveData:`none`});await e.queue(C(n.workflowName),{runId:t},{deploymentId:n.deploymentId,specVersion:n.specVersion??1})}catch(e){throw Error(`Failed to re-enqueue run ${t}: ${e instanceof Error?e.message:String(e)}`,{cause:e})}}async function K(e,t,n){try{let r=await e.runs.get(t,{resolveData:`none`}),i=I(r.specVersion),a=[],s=null;do{let n=await e.events.list({runId:t,pagination:{limit:1e3,...s?{cursor:s}:{}},resolveData:`none`});a.push(...n.data),s=n.hasMore?n.cursor:null}while(s);let c=a.filter(e=>e.eventType===`wait_created`),l=new Set(a.filter(e=>e.eventType===`wait_completed`).map(e=>e.correlationId)),u=c.filter(e=>!l.has(e.correlationId));if(n?.correlationIds&&n.correlationIds.length>0){let e=new Set(n.correlationIds);u=u.filter(t=>t.correlationId&&e.has(t.correlationId))}let d=[],f=0;for(let n of u){if(!n.correlationId)continue;let a=i?{eventType:`wait_completed`,correlationId:n.correlationId}:{eventType:`wait_completed`,correlationId:n.correlationId,specVersion:r.specVersion,eventData:{resumeAt:n.eventData.resumeAt}};try{await e.events.create(t,a,{v1Compat:i}),f++}catch(e){o.is(e)?f++:d.push(e instanceof Error?e:Error(String(e)))}}if(f>0&&await e.queue(C(r.workflowName),{runId:t},{deploymentId:r.deploymentId,specVersion:r.specVersion??1}),d.length>0)throw AggregateError(d,`Failed to complete ${d.length}/${u.length} pending wait(s) for run ${t}`);return{stoppedCount:f}}catch(e){throw e instanceof AggregateError?e:Error(`Failed to wake up run ${t}: ${e instanceof Error?e.message:String(e)}`,{cause:e})}}async function q(e,t,n,r){try{return await e.streams.get(t,n,r?.startIndex)}catch(e){throw Error(`Failed to read stream ${n}: ${e instanceof Error?e.message:String(e)}`,{cause:e})}}async function J(e,t){try{return await e.streams.list(t)}catch(e){throw Error(`Failed to list streams for run ${t}: ${e instanceof Error?e.message:String(e)}`,{cause:e})}}var Y=class e{static[P](e){return{runId:e.runId,resilientStart:e.#r}}static[A](t){return new e(t.runId,{resilientStart:t.resilientStart})}runId;#e;get#t(){return this.#e||=T(),this.#e}#n=null;#r=!1;constructor(e,t){this.runId=e,this.#r=t?.resilientStart??!1}#i(){return this.#n||=(async()=>{let e=await this.#t,t=await e.runs.get(this.runId),n=await e.getEncryptionKeyForRun?.(t);return n?await E(n):void 0})(),this.#n}#a(){return()=>this.#i()}async wakeUp(e){"use step";return K(await this.#t,this.runId,e)}async cancel(){"use step";await(await this.#t).events.create(this.runId,{eventType:`run_cancelled`,specVersion:5})}get exists(){"use step";return this.#t.then(e=>e.runs.get(this.runId,{resolveData:`none`}).then(()=>!0).catch(e=>{if(t.is(e))return!1;throw e}))}get status(){"use step";return this.#t.then(e=>e.runs.get(this.runId).then(e=>e.status))}get returnValue(){"use step";return this.#o()}get workflowName(){"use step";return this.#t.then(e=>e.runs.get(this.runId).then(e=>e.workflowName))}get createdAt(){"use step";return this.#t.then(e=>e.runs.get(this.runId).then(e=>e.createdAt))}get startedAt(){"use step";return this.#t.then(e=>e.runs.get(this.runId).then(e=>e.startedAt))}get completedAt(){"use step";return this.#t.then(e=>e.runs.get(this.runId).then(e=>e.completedAt))}get readable(){return this.getReadable()}getReadable(e={}){"use step";let{ops:t=[],global:n=globalThis,startIndex:r,namespace:i}=e,a=h(this.runId,i),o=this.#a(),s=u(n,t,this.runId,o).ReadableStream({name:a,startIndex:r}),c=this.#t,l=this.runId;return Object.assign(s,{getTailIndex:async()=>(await(await c).streams.getInfo(l,a)).tailIndex})}async#o(){let e=await this.#t,r=0,a=this.#r?3:0,o=[1e3,3e3,6e3];for(;;)try{let t=await e.runs.get(this.runId);if(t.status===`completed`){let e=await this.#i();return await l(t.output,this.runId,e)}if(t.status===`cancelled`)throw new i(this.runId);if(t.status===`failed`){let e=await this.#i(),n;try{n=await b(t.error,this.runId,e)}catch{n=Error(`Failed to hydrate workflow run error`)}throw new c(this.runId,n,{errorCode:t.errorCode})}throw new n(this.runId,t.status)}catch(e){if(n.is(e)){await new Promise(e=>setTimeout(e,1e3));continue}if(t.is(e)&&r<a){let e=o[r];r++,await new Promise(t=>setTimeout(t,e));continue}throw e}}};function X(e){return new Y(e)}export{q as a,K as c,J as i,B as l,X as n,U as o,W as r,G as s,Y as t};