UNPKG

eve

Version:

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

1 lines 2.46 kB
const VALUE_TYPE_KEY=`__eveDevelopmentWorldType`,DATE_MARKER=`date`,OBJECT_MARKER=`object`,UINT8_ARRAY_MARKER=`uint8-array`,UNDEFINED_MARKER=`undefined`;function encodeDevelopmentWorldValue(e){return JSON.stringify({value:encodeValue(e)})}function decodeDevelopmentWorldValue(e){let t=JSON.parse(e);return isRecord(t)?decodeTransportValue(t.value):void 0}function decodeDevelopmentWorldJson(e){return decodeWorkflowJsonValue(JSON.parse(e))}function serializeDevelopmentWorldError(e){return e instanceof Error?{details:Object.fromEntries(Object.entries(e)),message:e.message,name:e.name,stack:e.stack}:{details:{},message:String(e),name:`Error`}}function deserializeDevelopmentWorldError(e){if(!isRecord(e)||!isRecord(e.details)||typeof e.message!=`string`||typeof e.name!=`string`)return;let t=Error(e.message);return t.name=e.name,typeof e.stack==`string`&&(t.stack=e.stack),Object.assign(t,e.details),t}function encodeValue(n){return n===void 0?{[VALUE_TYPE_KEY]:UNDEFINED_MARKER}:n instanceof Date?{[VALUE_TYPE_KEY]:DATE_MARKER,data:n.toISOString()}:n instanceof Uint8Array?{[VALUE_TYPE_KEY]:UINT8_ARRAY_MARKER,data:Buffer.from(n).toString(`base64`)}:Array.isArray(n)?n.map(e=>encodeValue(e)):isRecord(n)?{[VALUE_TYPE_KEY]:OBJECT_MARKER,data:Object.fromEntries(Object.entries(n).map(([e,t])=>[e,encodeValue(t)]))}:n}function decodeTransportValue(n){if(Array.isArray(n))return n.map(e=>decodeTransportValue(e));if(!isRecord(n))return n;if(n[VALUE_TYPE_KEY]!==UNDEFINED_MARKER){if(n[VALUE_TYPE_KEY]===DATE_MARKER&&typeof n.data==`string`)return new Date(n.data);if(n[VALUE_TYPE_KEY]===UINT8_ARRAY_MARKER&&typeof n.data==`string`)return Uint8Array.from(Buffer.from(n.data,`base64`));if(n[VALUE_TYPE_KEY]===OBJECT_MARKER&&isRecord(n.data))return Object.fromEntries(Object.entries(n.data).map(([e,t])=>[e,decodeTransportValue(t)]));throw Error(`Development Workflow World transport contained an invalid encoded value.`)}}function decodeWorkflowJsonValue(e){return Array.isArray(e)?e.map(e=>decodeWorkflowJsonValue(e)):isRecord(e)?Object.keys(e).length===2&&e.__type===`Uint8Array`&&typeof e.data==`string`?Uint8Array.from(Buffer.from(e.data,`base64`)):Object.fromEntries(Object.entries(e).map(([e,t])=>[e,decodeWorkflowJsonValue(t)])):e}function isRecord(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}export{decodeDevelopmentWorldJson,decodeDevelopmentWorldValue,deserializeDevelopmentWorldError,encodeDevelopmentWorldValue,serializeDevelopmentWorldError};