UNPKG

eve

Version:

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

1 lines 4.55 kB
import{AGENT_HANDLES_STATE_KEY}from"./state-key.js";import{z}from"#compiled/zod/index.js";const EMPTY_AGENT_HANDLE_STORE={handles:[]},nonEmptyString=z.string().min(1),identitySchema=z.looseObject({id:nonEmptyString,name:nonEmptyString,nodeId:nonEmptyString}),startOperationSchema=z.looseObject({callId:nonEmptyString,id:nonEmptyString,kind:z.literal(`start`),parentTurnId:nonEmptyString}),continueOperationSchema=z.looseObject({callId:nonEmptyString,id:nonEmptyString,kind:z.literal(`continue`),parentTurnId:nonEmptyString,previousStatus:z.string().max(120)}),startTargetSchema=z.discriminatedUnion(`kind`,[z.looseObject({continuationToken:nonEmptyString,kind:z.literal(`agent/local`)}),z.looseObject({continuationToken:nonEmptyString,kind:z.literal(`agent/self`)}),z.looseObject({callbackBaseUrl:z.url(),credentialResolver:z.looseObject({resolverId:nonEmptyString.optional()}).optional(),kind:z.literal(`agent/remote`),url:z.url()})]),addressSchema=z.discriminatedUnion(`kind`,[z.looseObject({continuationToken:nonEmptyString,kind:z.literal(`agent/local`),sessionId:nonEmptyString}),z.looseObject({continuationToken:nonEmptyString,kind:z.literal(`agent/self`),sessionId:nonEmptyString}),z.looseObject({callbackBaseUrl:z.url(),credentialResolver:z.looseObject({resolverId:nonEmptyString.optional()}).optional(),kind:z.literal(`agent/remote`),sessionId:nonEmptyString,url:z.url()})]),agentHandleStoreCommandSchema=z.discriminatedUnion(`kind`,[z.strictObject({kind:z.literal(`read`)}),z.strictObject({identity:identitySchema,callId:nonEmptyString.optional(),kind:z.literal(`reserve`),operationId:nonEmptyString,ownerId:nonEmptyString}),z.strictObject({address:addressSchema,kind:z.literal(`confirm`),operationId:nonEmptyString,ownerId:nonEmptyString}),z.strictObject({agentId:nonEmptyString,callId:nonEmptyString.optional(),expectedTarget:z.enum([`local`,`remote`]),invokedName:nonEmptyString,kind:z.literal(`claim`),operationId:nonEmptyString,ownerId:nonEmptyString}),z.strictObject({agentId:nonEmptyString,kind:z.literal(`remove`),ownerId:nonEmptyString}),z.strictObject({kind:z.literal(`release-owner`),ownerId:nonEmptyString})]),turnOwnedAgentHandleSchema=z.discriminatedUnion(`phase`,[z.looseObject({identity:identitySchema,operation:startOperationSchema,phase:z.literal(`starting`),target:startTargetSchema}),z.looseObject({address:addressSchema,identity:identitySchema,operation:z.discriminatedUnion(`kind`,[startOperationSchema,continueOperationSchema]),phase:z.literal(`running`)}),z.looseObject({address:addressSchema,identity:identitySchema,lastStatus:z.string().max(120),phase:z.literal(`parked`)})]),taskOwnedAgentHandleSchema=z.discriminatedUnion(`phase`,[z.looseObject({callId:nonEmptyString.optional(),identity:identitySchema,operationId:nonEmptyString,phase:z.literal(`reserved`),ownerId:nonEmptyString}),z.looseObject({address:addressSchema,callId:nonEmptyString.optional(),identity:identitySchema,operationId:nonEmptyString,phase:z.literal(`claimed`),ownerId:nonEmptyString}),z.looseObject({address:addressSchema,identity:identitySchema,phase:z.literal(`available`)})]),agentHandleSchema=z.union([turnOwnedAgentHandleSchema,taskOwnedAgentHandleSchema]),agentHandleStoreSchema=z.looseObject({handles:z.array(agentHandleSchema)}).refine(e=>new Set(e.handles.map(e=>e.identity.id)).size===e.handles.length,{message:`Agent handle ids must be unique.`});function deriveAgentId(e,t){return`ag_${e}:${t.slice(0,12)}`}function formatAgentStatus(e){return(typeof e==`string`?e:JSON.stringify(e)??``).replaceAll(/\s+/g,` `).trim().slice(0,120)}function assertPersistableAgentHandleStore(e){let t=agentHandleStoreSchema.safeParse(e);if(!t.success)throw Error(`Refusing to persist a corrupt agent handle store: ${t.error.message}`);return t.data}function parseAgentHandleStoreCommand(e){let t=agentHandleStoreCommandSchema.safeParse(e);return t.success?t.data:void 0}function getAgentHandleStore(e){let t=e?.[AGENT_HANDLES_STATE_KEY];if(t===void 0)return;let n=agentHandleStoreSchema.safeParse(t);if(!n.success)throw Error(`Corrupt agent handle store under session state key "${AGENT_HANDLES_STATE_KEY}": ${n.error.message}`);return n.data}function setAgentHandleStore(e,t){return{...e,[AGENT_HANDLES_STATE_KEY]:assertPersistableAgentHandleStore(t)}}function writeHandles(e,t){return{...e,state:setAgentHandleStore(e.state,{handles:t})}}export{AGENT_HANDLES_STATE_KEY,EMPTY_AGENT_HANDLE_STORE,assertPersistableAgentHandleStore,deriveAgentId,formatAgentStatus,getAgentHandleStore,parseAgentHandleStoreCommand,setAgentHandleStore,writeHandles};