eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 4.65 kB
JavaScript
const MAX_ACTIVITY_EVENTS_PER_BATCH=100;function parseActivityWorkIdentityV1(e){if(!isRecord(e)||!hasOnlyKeys(e,[`callId`,`id`,`kind`,`name`,`parentId`,`rootSessionId`,`rootTurnId`,`sessionId`,`turnId`]))return;let t=e.kind;if(isOneOf(t,[`root-turn`,`subagent`,`remote-agent`,`task`])&&isIdentity(e.id)&&isIdentity(e.rootSessionId)&&isIdentity(e.rootTurnId)&&isOptionalIdentity(e.callId)&&isOptionalBoundedString(e.name)&&isOptionalIdentity(e.parentId)&&isOptionalIdentity(e.sessionId)&&isOptionalIdentity(e.turnId))return{callId:e.callId,id:e.id,kind:t,name:e.name,parentId:e.parentId,rootSessionId:e.rootSessionId,rootTurnId:e.rootTurnId,sessionId:e.sessionId,turnId:e.turnId}}function parseActivityBatchV1(e){if(!isRecord(e)||!hasOnlyKeys(e,[`events`,`version`])||e.version!==1||!Array.isArray(e.events)||e.events.length>100)return;let t=[];for(let n of e.events){if(!isRecord(n)||!isBoundedString(n.kind))return;let e=parseKnownEvent(n);if(e!==null){if(e===void 0)return;t.push(e)}}return{events:t,version:1}}function parseKnownEvent(e){switch(e.kind){case`work.started`:{if(!hasOnlyKeys(e,[`eventId`,`kind`,`startedAt`,`work`]))return;let t=parseActivityWorkIdentityV1(e.work);return!isIdentity(e.eventId)||!isBoundedString(e.startedAt)||t===void 0?void 0:{eventId:e.eventId,kind:`work.started`,startedAt:e.startedAt,work:t}}case`work.settled`:return!hasOnlyKeys(e,[`eventId`,`kind`,`outcome`,`settledAt`,`workId`])||!isIdentity(e.eventId)||!isOneOf(e.outcome,[`completed`,`failed`,`cancelled`])||!isBoundedString(e.settledAt)||!isIdentity(e.workId)?void 0:{eventId:e.eventId,kind:`work.settled`,outcome:e.outcome,settledAt:e.settledAt,workId:e.workId};case`action.started`:{if(!hasOnlyKeys(e,[`action`,`eventId`,`kind`,`startedAt`]))return;let t=parseActionIdentity(e.action);return!isIdentity(e.eventId)||!isBoundedString(e.startedAt)||t===void 0?void 0:{action:t,eventId:e.eventId,kind:`action.started`,startedAt:e.startedAt}}case`action.settled`:return!hasOnlyKeys(e,[`actionId`,`eventId`,`kind`,`outcome`,`settledAt`])||!isIdentity(e.actionId)||!isIdentity(e.eventId)||!isOneOf(e.outcome,[`completed`,`failed`,`rejected`,`cancelled`])||!isBoundedString(e.settledAt)?void 0:{actionId:e.actionId,eventId:e.eventId,kind:`action.settled`,outcome:e.outcome,settledAt:e.settledAt};case`action.label.updated`:return!hasOnlyKeys(e,[`actionId`,`eventId`,`kind`,`label`])||!isIdentity(e.actionId)||!isIdentity(e.eventId)||!isBoundedString(e.label)?void 0:{actionId:e.actionId,eventId:e.eventId,kind:`action.label.updated`,label:e.label};case`blocker.started`:{if(!hasOnlyKeys(e,[`blocker`,`eventId`,`kind`,`startedAt`]))return;let t=parseBlockerIdentity(e.blocker);return!isIdentity(e.eventId)||!isBoundedString(e.startedAt)||t===void 0?void 0:{blocker:t,eventId:e.eventId,kind:`blocker.started`,startedAt:e.startedAt}}case`blocker.settled`:return!hasOnlyKeys(e,[`blockerId`,`eventId`,`kind`,`outcome`,`settledAt`])||!isIdentity(e.blockerId)||!isIdentity(e.eventId)||!isOneOf(e.outcome,[`completed`,`cancelled`,`failed`])||!isBoundedString(e.settledAt)?void 0:{blockerId:e.blockerId,eventId:e.eventId,kind:`blocker.settled`,outcome:e.outcome,settledAt:e.settledAt};default:return null}}function parseActionIdentity(e){if(isRecord(e)&&hasOnlyKeys(e,[`id`,`kind`,`name`,`parentWorkId`,`rootTurnId`,`stepIndex`])&&!(!isIdentity(e.id)||!isOneOf(e.kind,[`tool`,`skill`])||!isBoundedString(e.name)||!isIdentity(e.parentWorkId)||!isIdentity(e.rootTurnId)||!Number.isInteger(e.stepIndex)||e.stepIndex<0))return{id:e.id,kind:e.kind,name:e.name,parentWorkId:e.parentWorkId,rootTurnId:e.rootTurnId,stepIndex:e.stepIndex}}function parseBlockerIdentity(e){if(isRecord(e)&&hasOnlyKeys(e,[`id`,`kind`,`label`,`parentActionId`,`parentWorkId`,`rootTurnId`])&&isIdentity(e.id)&&isOneOf(e.kind,[`approval`,`authorization`,`input`])&&isOptionalBoundedString(e.label)&&isOptionalIdentity(e.parentActionId)&&isIdentity(e.parentWorkId)&&isIdentity(e.rootTurnId))return{id:e.id,kind:e.kind,label:e.label,parentActionId:e.parentActionId,parentWorkId:e.parentWorkId,rootTurnId:e.rootTurnId}}function isRecord(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function hasOnlyKeys(e,t){return Object.keys(e).every(e=>t.includes(e))}function isBoundedString(e){return typeof e==`string`&&e.length>0&&e.length<=500}function isIdentity(e){return typeof e==`string`&&e.length>0&&e.length<=1e3}function isOptionalIdentity(e){return e===void 0||isIdentity(e)}function isOptionalBoundedString(e){return e===void 0||isBoundedString(e)}function isOneOf(e,t){return typeof e==`string`&&t.includes(e)}export{MAX_ACTIVITY_EVENTS_PER_BATCH,parseActivityBatchV1,parseActivityWorkIdentityV1};