UNPKG

eve

Version:

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

1 lines • 7.18 kB
import{contextStorage,loadContext}from"#context/container.js";import{parseJsonValue}from"#shared/json.js";import{ContextKey}from"#context/key.js";import{SERIALIZED_INSTRUMENTATION_STATE_KEYS,preserveSerializedInstrumentationState}from"#shared/serialized-observability-state.js";const InstrumentationStateKey=new ContextKey(SERIALIZED_INSTRUMENTATION_STATE_KEYS.providerState,{codec:{deserialize:deserializeState,serialize:e=>e}}),InstrumentationActionStateKey=new ContextKey(SERIALIZED_INSTRUMENTATION_STATE_KEYS.actionScopes,{codec:{deserialize:deserializeActionStates,serialize:e=>e}}),InstrumentationInputScopeKey=new ContextKey(SERIALIZED_INSTRUMENTATION_STATE_KEYS.inputScopes,{codec:{deserialize:deserializeScopes,serialize:e=>e}});function instrumentationStateSlot(e,t,n={}){let r=stateKey(e,t),i=!0;return{get:()=>{if(!i)return;let e=contextStorage.getStore()?.get(InstrumentationStateKey)?.[r]?.value;return e===void 0?void 0:cloneAndFreezeJson(e)},revoke:()=>{i=!1},set:e=>{if(!i)return;let t=e===void 0?void 0:cloneAndFreezeJson(parseJsonValue(e));writeInstrumentationState(e=>{if(t===void 0)return writeSlot(e,r,void 0);let i=e[r],a={value:t};return i?.abandoned===!0&&(a.abandoned=!0),assignOwner(a,n),writeSlot(e,r,a)})}}}function abandonInstrumentationState(e,t,n={}){let r=stateKey(e,t);writeInstrumentationState(e=>{let t=e[r],i={attemptId:n.attemptId??t?.attemptId,sessionId:n.sessionId??t?.sessionId,turnId:n.turnId??t?.turnId},a={abandoned:!0};return assignOwner(a,i),t?.value!==void 0&&(a.value=t.value),writeSlot(e,r,a)})}function isInstrumentationStateAbandoned(e,t){return contextStorage.getStore()?.get(InstrumentationStateKey)?.[stateKey(e,t)]?.abandoned===!0}function releaseAllInstrumentationState(e){let t=`\0${e}`;releaseMatchingInstrumentationState(e=>e.endsWith(t))}function releaseAllInstrumentationAttemptState(e){releaseMatchingInstrumentationState((t,n)=>n.attemptId===e)}function releaseAllInstrumentationTurnState(e,t){let n=t===void 0?new Set:backgroundActionKeys(e,t);releaseMatchingInstrumentationState((r,i)=>i.sessionId===e&&(t===void 0||i.turnId===t)&&!n.has(instrumentationStateOperationId(r)))}function releaseMatchingInstrumentationState(e){let t=contextStorage.getStore()?.get(InstrumentationStateKey);t!==void 0&&Object.entries(t).some(([t,n])=>e(t,n))&&writeInstrumentationState(t=>{let n={...t};for(let[r,i]of Object.entries(t))e(r,i)&&delete n[r];return n})}function rememberInstrumentationActionScope(e,t){writeContextKey(InstrumentationActionStateKey,n=>{let r=n[e]?.taskId;return{...n,[e]:r===void 0?{scope:t}:{scope:t,taskId:r}}})}function rememberInstrumentationInputScope(e,t){writeContextKey(InstrumentationInputScopeKey,n=>({...n,[e]:t}))}function takeInstrumentationInputScope(e){let t=contextStorage.getStore()?.get(InstrumentationInputScopeKey)?.[e];if(t!==void 0)return writeContextKey(InstrumentationInputScopeKey,t=>{let n={...t};return delete n[e],n}),t}function rememberInstrumentationBackgroundTask(e,t){writeContextKey(InstrumentationActionStateKey,n=>({...n,[t.idempotencyKey]:{scope:n[t.idempotencyKey]?.scope??t.scope,taskId:e}}))}function rememberInstrumentationBackgroundTaskForCall(e,t,n){let r=findInstrumentationActionScopeForCall(e,t);r!==void 0&&rememberInstrumentationBackgroundTask(n,r)}function findInstrumentationActionScopeForCall(e,t){let n=contextStorage.getStore()?.get(InstrumentationActionStateKey);if(n!==void 0)for(let r of Object.values(n)){let i=`action:${e}:${r.scope.turnId}:${t}`,a=n[i];if(a!==void 0)return{idempotencyKey:i,scope:a.scope}}}function takeInstrumentationActionScopeForCall(e,t){let n=findInstrumentationActionScopeForCall(e,t);if(n!==void 0)return releaseInstrumentationActionCorrelation(n.idempotencyKey),n}function takeInstrumentationActionScopeForTask(e){let t=contextStorage.getStore()?.get(InstrumentationActionStateKey);if(t!==void 0){for(let[n,r]of Object.entries(t))if(r.taskId===e)return releaseInstrumentationActionCorrelation(n),{idempotencyKey:n,scope:r.scope}}}function takeInstrumentationActionScopes(e,t){let n=contextStorage.getStore()?.get(InstrumentationActionStateKey);if(n===void 0)return[];let r=Object.entries(n).filter(([,n])=>n.scope.sessionId===e&&(t===void 0||n.scope.turnId===t)&&(t===void 0||n.taskId===void 0)).map(([e,t])=>({idempotencyKey:e,scope:t.scope}));if(r.length===0)return[];let i=new Set(r.map(e=>e.idempotencyKey));return writeContextKey(InstrumentationActionStateKey,e=>{let t={...e};for(let e of i)delete t[e];return t}),r}function releaseInstrumentationActionCorrelation(e){writeContextKey(InstrumentationActionStateKey,t=>{let n={...t};return delete n[e],n})}function backgroundActionKeys(e,t){let n=contextStorage.getStore()?.get(InstrumentationActionStateKey);return n===void 0?new Set:new Set(Object.entries(n).flatMap(([n,r])=>r.taskId!==void 0&&r.scope.sessionId===e&&r.scope.turnId===t?[n]:[]))}function writeSlot(e,t,n){if(n===void 0){let n={...e};return delete n[t],n}return{...e,[t]:n}}function writeInstrumentationState(e){writeContextKey(InstrumentationStateKey,e)}function assignOwner(e,t){t.attemptId!==void 0&&(e.attemptId=t.attemptId),t.sessionId!==void 0&&(e.sessionId=t.sessionId),t.turnId!==void 0&&(e.turnId=t.turnId)}function writeContextKey(e,t){contextStorage.getStore()!==void 0&&loadContext().set(e,e=>t(e??{}))}function stateKey(e,t){return`${e}\0${t}`}function instrumentationStateOperationId(e){return e.slice(e.indexOf(`\0`)+1)}function deserializeState(e){if(typeof e!=`object`||!e||Array.isArray(e))return{};let t={};for(let[n,r]of Object.entries(e)){if(typeof r!=`object`||!r||Array.isArray(r))continue;let e=r,i=typeof e.attemptId==`string`?e.attemptId:void 0,a=typeof e.sessionId==`string`?e.sessionId:void 0,o=typeof e.turnId==`string`?e.turnId:void 0,s={};e.abandoned===!0&&(s.abandoned=!0),i!==void 0&&(s.attemptId=i),a!==void 0&&(s.sessionId=a),o!==void 0&&(s.turnId=o),e.value!==void 0&&(s.value=cloneAndFreezeJson(e.value)),t[n]=s}return t}function cloneAndFreezeJson(e){if(Array.isArray(e))return Object.freeze(e.map(e=>cloneAndFreezeJson(e)));if(typeof e!=`object`||!e)return e;let t={};for(let[n,r]of Object.entries(e))t[n]=cloneAndFreezeJson(r);return Object.freeze(t)}function deserializeScopes(e){return typeof e!=`object`||!e||Array.isArray(e)?{}:e}function deserializeActionStates(e){if(typeof e!=`object`||!e||Array.isArray(e))return{};let t={};for(let[n,r]of Object.entries(e)){if(typeof r!=`object`||!r||Array.isArray(r))continue;let e=r,i=typeof e.scope==`object`&&e.scope!==null&&!Array.isArray(e.scope)?e.scope:r,a=typeof e.taskId==`string`?e.taskId:void 0;t[n]=a===void 0?{scope:i}:{scope:i,taskId:a}}return t}export{abandonInstrumentationState,findInstrumentationActionScopeForCall,instrumentationStateSlot,isInstrumentationStateAbandoned,preserveSerializedInstrumentationState,releaseAllInstrumentationAttemptState,releaseAllInstrumentationState,releaseAllInstrumentationTurnState,rememberInstrumentationActionScope,rememberInstrumentationBackgroundTask,rememberInstrumentationBackgroundTaskForCall,rememberInstrumentationInputScope,takeInstrumentationActionScopeForCall,takeInstrumentationActionScopeForTask,takeInstrumentationActionScopes,takeInstrumentationInputScope};