UNPKG

eve

Version:

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

1 lines 3.2 kB
import{isObject}from"#shared/guards.js";import{SESSION_INBOX_WIRE_VERSION,SessionInboxWireError,SessionInboxWireError as SessionInboxWireError$1}from"#execution/wire/session-inbox-contract.js";import{sessionInboxWireV0Migration}from"#execution/wire/session-inbox-wire.v0.js";import{runMigrationChain}from"#execution/durable-session-migrations/chain.js";import{normalizeSessionInboxWireV2}from"#execution/wire/session-inbox-wire.v2-migration.js";import{sessionInboxWireV1Migration}from"#execution/wire/session-inbox-wire.v2.migration.js";import{sessionInboxWireV2Migration}from"#execution/wire/session-inbox-wire.v3.migration.js";import{sessionInboxWireV3Migration}from"#execution/wire/session-inbox-wire.v4.migration.js";import{sessionInboxWireV4Migration}from"#execution/wire/session-inbox-wire.v5.migration.js";const WIRE_LABEL=`session inbox payload`,sessionInboxMigrations=[sessionInboxWireV0Migration,sessionInboxWireV1Migration,sessionInboxWireV2Migration,sessionInboxWireV3Migration,sessionInboxWireV4Migration,{from:5,migrate(e){if(!isObject(e))throw Error(`session inbox wire v5 value is not an object.`);return{...e,version:6}},to:6}];function decode(e){let t=typeof e==`object`&&e&&`version`in e?e.version:void 0;if(typeof e==`object`&&e&&`version`in e&&typeof t!=`number`)throw new SessionInboxWireError$1(`${WIRE_LABEL}: value has no numeric "version" field.`);let n=normalizeSessionInboxWireV2(e);if((t===1||t===2||t===3)&&containsCurrentTaskMessages(n))throw new SessionInboxWireError$1(`${WIRE_LABEL} does not match wire version ${t}.`);let r;try{r=runMigrationChain({initialVersion:0,label:WIRE_LABEL,migrations:sessionInboxMigrations,targetVersion:SESSION_INBOX_WIRE_VERSION,value:n})}catch(e){throw new SessionInboxWireError$1(e instanceof Error?e.message:String(e))}let i=normalizeSessionInboxWireV2(r);if(i.version!==SESSION_INBOX_WIRE_VERSION)throw new SessionInboxWireError$1(`${WIRE_LABEL} declares version ${JSON.stringify(i.version)}, expected ${SESSION_INBOX_WIRE_VERSION}.`);if(typeof t==`number`&&t>=2&&i.kind===`deliver`&&!(`payload`in i))throw new SessionInboxWireError$1(`${WIRE_LABEL} does not match wire version ${t}.`);return normalizeWire(i)}const sessionInboxWire={decode};function containsCurrentTaskMessages(e){return!isObject(e)||e.kind!==`deliver`?!1:(Array.isArray(e.payloads)?e.payloads:[]).some(e=>{if(!isObject(e)||!isObject(e.task))return!1;if(Object.hasOwn(e.task,`agentRequests`))return!0;let t=e.task.inputRequests;return Array.isArray(t)&&t.some(e=>isObject(e)&&(`request`in e||`requests`in e))})}function normalizeWire(e){switch(e.kind){case`deliver`:return{auth:e.auth,caller:e.caller,deliveryMetadata:e.deliveryMetadata,kind:`deliver`,payloads:e.payloads,requestId:e.requestId,taskDeliveryId:e.taskDeliveryId,turnPolicy:e.turnPolicy};case`session-timeout`:return{kind:`session-timeout`};case`clear`:return{kind:`clear`};case`compact`:return{kind:`compact`};case`reset`:return{kind:`reset`,reason:e.reason};case`cancel`:return{kind:`cancel`,taskId:e.taskId,tasks:e.tasks,turnId:e.turnId};default:throw new SessionInboxWireError$1(`${WIRE_LABEL} has an unrecognized kind ${JSON.stringify(e.kind)}.`)}}export{SessionInboxWireError,sessionInboxWire};