UNPKG

eve

Version:

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

1 lines • 8.56 kB
import{S as e,_ as t,it as n,u as r,ut as i,w as a}from"./dist-DjECvUyK.js";import{B as o,G as s,Gn as c,H as l,Kn as u,L as d,On as f,R as p,V as m,Vn as h,Xt as g,Yt as _,an as ee,at as v,d as y,fn as b,n as x,ot as S,st as C,y as w,z as T}from"./logger-DFNCa8Nl.js";import{A as E,c as D,j as O}from"./dist-xTushGCp.js";const k=`5.0.0-beta.55`,te=/^[a-zA-Z0-9_\-./@()[\]]+$/;function ne(t,n){if(!te.test(t))throw Error(`Invalid workflow name "${t}": must only contain alphanumeric characters, underscores, hyphens, dots, forward slashes, at signs, parentheses, or square brackets`);return`${e(`workflow`,a(n))}${t}`}const re=i();function A(e){return`__health_check__${e}`}function ie(e){let n=t.safeParse(e);if(n.success)return n.data}function j(e){return`wrun_hc_${e}`}async function M(e,t){let n=await u(),r=A(e.correlationId),i;if(e.runId)try{let t=await n.getEncryptionKeyForRun?.(e.runId);t&&(i=h((await c(t)).publicKey))}catch(t){x.warn(`Health check could not derive a run public key; the caller will fall back to a key lookup`,{correlationId:e.correlationId,error:t instanceof Error?t.message:String(t)})}let a=JSON.stringify({healthy:!0,correlationId:e.correlationId,specVersion:t??7,workflowCoreVersion:k,hookResumeInputVersion:1,...i?{encryptionPublicKey:i}:{},timestamp:Date.now()}),o=j(e.correlationId);await n.streams.write(o,r,a),await n.streams.close(o,r)}function N(e,t){let n;return Promise.race([e,new Promise((e,r)=>{n=setTimeout(()=>r(Error(`Operation timed out after ${t}ms`)),t)})]).finally(()=>clearTimeout(n))}async function P(e,t){let n=[],r=!1,i=!1;for(;!r&&!i;){let a=e.read(),o=new Promise(e=>setTimeout(()=>{i=!0,e({done:!0,value:void 0})},t)),s=await Promise.race([a,o]);r=s.done,s.value&&n.push(s.value)}return{chunks:n,timedOut:i}}function F(e){if(e.length===0)return null;let t=e.reduce((e,t)=>e+t.length,0),n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.length;let i=new TextDecoder().decode(n),a;try{a=JSON.parse(i)}catch{return i.length>0?{healthy:!0}:null}if(typeof a!=`object`||!a||!(`healthy`in a)||typeof a.healthy!=`boolean`)return null;let o=a,s={healthy:o.healthy};return typeof o.specVersion==`number`&&(s.specVersion=o.specVersion),typeof o.workflowCoreVersion==`string`&&(s.workflowCoreVersion=o.workflowCoreVersion),typeof o.encryptionPublicKey==`string`&&(s.encryptionPublicKey=o.encryptionPublicKey),typeof o.hookResumeInputVersion==`number`&&(s.hookResumeInputVersion=o.hookResumeInputVersion),s}async function I(t,n){let r=n?.timeout??3e4,i=t.createRunId?.()??re(),o=A(i),s=`${e(`workflow`,a(n?.namespace))}health_check`,c=Date.now();try{for(await t.queue(s,{__healthCheck:!0,correlationId:i,...n?.runId?{runId:n.runId}:{}},{specVersion:1,deploymentId:n?.deploymentId});Date.now()-c<r;)try{let e=r-(Date.now()-c),n=(await N(t.streams.get(j(i),o),e)).getReader(),{chunks:a,timedOut:s}=await P(n,500);if(s){try{n.cancel()}catch{}await new Promise(e=>setTimeout(e,100));continue}let l=F(a);if(l)return{...l,latencyMs:Date.now()-c};await new Promise(e=>setTimeout(e,100))}catch{await new Promise(e=>setTimeout(e,100))}return{healthy:!1,error:`Health check timed out after ${r}ms`}}catch(e){return{healthy:!1,error:e instanceof Error?e.message:String(e)}}}function L(e,t){return new E(`Event pagination ${t} for workflow run "${e}".`,{code:O.WORLD_CONTRACT_ERROR})}function R(e,t,n){if(t){if(n.has(t))throw L(e,`did not advance`);n.add(t)}}function z(e,t,n){if(t.length===0)return;let r=n??new Set(e.map(e=>e.eventId));for(let n of t)r.has(n.eventId)||(r.add(n.eventId),e.push(n))}function B(e,t){let n=e.length;for(;n>0;){let r=e[n-1];if(r.eventId===t.eventId)return;if(r.eventId<t.eventId)break;n--}e.splice(n,0,t)}function V(e,t,n,r){if(t){if(n===null)throw L(e,`returned more pages without a cursor`);if(r.has(n))throw L(e,`repeated a cursor`)}}function H(e,t,n){return t!==null&&!n&&E.is(e)&&e.status===400}async function U(e,t){let n=t!==void 0;return w(`workflow.replay.load`,async r=>{r?.setAttributes({...b(e),...ee(n?`events_list_incremental`:`events_list`)});let i=[],a=new Set,o=new Set,s=t??null,c=!0,l=0,d=!1,f=await u(),p=Date.now();for(;c;){let t=Date.now(),r=s;R(e,r,o);let u;try{u=await f.events.list({runId:e,pagination:{sortOrder:`asc`,cursor:r??void 0}})}catch(t){if(H(t,r,d)){x.warn(`Event cursor was rejected; retrying with a full event reload.`,{workflowRunId:e}),i.length=0,a.clear(),o.clear(),s=null,d=!0;continue}throw t}z(i,u.data,a),c=u.hasMore,V(e,c,u.cursor,o),s=u.cursor??s,l++,x.debug(`Loaded event page`,{workflowRunId:e,incremental:n,page:l,pageEvents:u.data.length,totalEvents:i.length,hasMore:c,pageMs:Date.now()-t})}return x.debug(`Event load complete`,{workflowRunId:e,incremental:n,totalEvents:i.length,pagesLoaded:l,totalMs:Date.now()-p}),r?.setAttributes({..._(i.length),...g(l)}),{events:i,cursor:s}})}function W(e,t){z(e.events,t.events),e.cursor=t.cursor??e.cursor}function G(){return process.env.WORKFLOW_SLOT_GAP_CHECK!==`0`}function K(e,t){let n=e.length;z(e,t);let r=e.length-n;return r>0&&e.sort((e,t)=>e.eventId<t.eventId?-1:+(e.eventId>t.eventId)),r}function ae(e,t){let n=t.events?.length??0;return n===0?{added:0,offered:0,truncated:!1}:t.hasMore===!0?{added:0,offered:n,truncated:!0}:{added:K(e,t.events??[]),offered:n,truncated:!1}}function q(e){let t;for(let n of e){let e=r(n.eventId);(t===void 0||e>t)&&(t=e)}return t}function J(e){let t=new Set,n=0;for(let i of e){let e=r(i.eventId);t.add(e),e>n&&(n=e)}if(n===0)return;let i=t.has(1)?1:2;if(t.size===n-i+1)return;let a,o=0;for(let e=i;e<=n;e++)t.has(e)||(a??=e,o++);if(a!==void 0)return{firstMissingSlot:a,missingCount:o,maxSlot:n}}const oe=3;async function se(e,t){let n=t,r=J(n.events);for(let t=0;r!==void 0&&t<3;t++)await new Promise(e=>setTimeout(e,25*2**t)),n=await U(e),r=J(n.events);return{log:n,gap:r}}function ce(e){let t=q(e);return t===void 0?{}:{eventCount:t}}function le(e,t){if(!D.is(e))return null;let n=e.details;if(typeof n!=`object`||!n)return null;let{events:r,cursor:i}=n;if(!Array.isArray(r)||r.length===0)return null;for(let e of r)if(typeof e!=`object`||!e||typeof e.eventId!=`string`||e.runId!==t)return null;return{events:r,cursor:typeof i==`string`?i:null}}const Y={"Access-Control-Allow-Origin":`*`,"Access-Control-Allow-Methods":`POST, OPTIONS, GET, HEAD`,"Access-Control-Allow-Headers":`Content-Type`};function X(e,t){return async n=>{let r=new URL(n.url);return r.searchParams.has(`__health`)?n.method===`OPTIONS`?new Response(null,{status:204,headers:Y}):new Response(JSON.stringify({healthy:!0,endpoint:r.pathname,specVersion:t??7,workflowCoreVersion:k}),{status:200,headers:{"Content-Type":`application/json`,...Y}}):await e(n)}}function Z(e,t){return e?.[n]??t}function ue(e){return{deploymentId:e.deploymentId,specVersion:e.specVersion??0,...e.startedAt?{startedAt:+e.startedAt}:{},rootRunId:Z(e.attributes,e.runId)}}function de(e){let t=2166136261;for(let n=0;n<e.length;n++)t^=e.charCodeAt(n),t=Math.imul(t,16777619);return(t>>>0).toString(16).padStart(8,`0`)}function fe(e,t){return`${e}:${de(t)}`}async function Q(e,...t){let n=t[0];await w(`queue.publish`,{attributes:{...m(`vercel-queue`),...p(n),...o(`publish`),...l(`vercel-queue`),...C(`vercel-queue`),...S(`vqs`),...v(`publish`)},kind:await y(`PRODUCER`)},async n=>{let{messageId:r}=await e.queue(...t);r&&n?.setAttributes(T(r))})}async function pe(e,t,n){if(n.length===0)return;let r=e.queueBatch?.bind(e);if(!r){await Promise.all(n.map(n=>Q(e,t,n.message,n.opts)));return}await w(`queue.publish`,{attributes:{...m(`vercel-queue`),...p(t),...o(`publish`),...d(n.length),...l(`vercel-queue`),...C(`vercel-queue`),...S(`vqs`),...v(`publishBatch`)},kind:await y(`PRODUCER`)},async()=>{let e=await r(t,n);if(e.length!==n.length)throw Object.assign(Error(`Queue batch for ${t} returned ${e.length} result(s) for ${n.length} message(s)`),{retryable:!0});let i=e.filter(e=>e.error!==void 0);if(i.length===0)return;let a=i.some(e=>e.error!==void 0&&e.retryable),o=Error(`Failed to publish ${i.length} of ${n.length} queue message(s) to ${t}: ${i[0]?.error}`);throw Object.assign(o,{retryable:a}),o})}function me(e){if(e.requestedAt)try{return s(Date.now()-e.requestedAt.getTime())}catch{return}}async function $(e,t,n){let r=typeof t==`string`?await e.getEncryptionKeyForRun?.(t,n):await e.getEncryptionKeyForRun?.(t);return r?await f(r):void 0}function he(e,t){let n;return()=>(n||=$(e,t),n)}export{se as C,k as D,X as E,ue as S,fe as T,le as _,J as a,$ as b,M as c,G as d,U as f,ie as g,K as h,z as i,I as l,he as m,ae as n,me as o,q as p,W as r,ne as s,oe as t,B as u,Q as v,ce as w,Z as x,pe as y};