openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 2.84 kB
JavaScript
const e=1e3,t=1e6,n=1e4,r=67108864,i=/* @__PURE__ */ new Set([`transcript`,`reset-window`]);function normalizeVisibleMessageLimit(e,t,n,r){let i=e??t;if(!Number.isInteger(i)||i<1||i>n)throw RangeError(`${r} must be an integer between 1 and ${String(n)}`);return i}function normalizeVisibleDeltaLimits(a){if(a.start!==void 0&&!i.has(a.start))throw RangeError(`start must be "transcript" or "reset-window"`);return{start:a.start??`transcript`,maxMessages:normalizeVisibleMessageLimit(a.maxMessages,e,n,`maxMessages`),maxBytes:normalizeVisibleMessageLimit(a.maxBytes,t,r,`maxBytes`)}}function encodeVisibleMessageCursor(e){let t={agentId:e.agentId,generation:e.generation,sessionId:e.sessionId,lastEventSeq:e.lastEventSeq,lastMessagePosition:e.lastMessagePosition,...e.resetBoundarySeq===void 0?{}:{resetBoundarySeq:e.resetBoundarySeq},...e.resetBoundaryId===void 0?{}:{resetBoundaryId:e.resetBoundaryId},version:e.version};return Buffer.from(JSON.stringify(t),`utf8`).toString(`base64url`)}function createVisibleMessageCursor(e){return{agentId:e.agentId,generation:e.generation,sessionId:e.sessionId,lastEventSeq:-1,lastMessagePosition:-1,version:1}}function withVisibleResetBoundary(e,t){return{...e,resetBoundarySeq:t?.seq??-1,...t?.event_id===void 0?{}:{resetBoundaryId:t.event_id}}}function hasVisibleResetBoundaryChanged(e,t,n){return n?(t?.seq??-1)!==e.resetBoundarySeq:t?.event_id!==e.resetBoundaryId}function parseVisibleMessageCursor(e){if(!(e.length>4096))try{let t=Buffer.from(e,`base64url`);if(t.toString(`base64url`)!==e)return;let n=JSON.parse(t.toString(`utf8`));if(n.version!==1||typeof n.agentId!=`string`||typeof n.sessionId!=`string`||typeof n.generation!=`string`||typeof n.lastEventSeq!=`number`||!Number.isSafeInteger(n.lastEventSeq)||n.lastEventSeq<-1||typeof n.lastMessagePosition!=`number`||!Number.isSafeInteger(n.lastMessagePosition)||n.lastMessagePosition<-1||n.lastEventSeq===-1!=(n.lastMessagePosition===-1)||n.resetBoundarySeq!==void 0&&(typeof n.resetBoundarySeq!=`number`||!Number.isSafeInteger(n.resetBoundarySeq)||n.resetBoundarySeq<-1)||n.resetBoundaryId!==void 0&&(typeof n.resetBoundaryId!=`string`||n.resetBoundaryId.length===0||n.resetBoundarySeq===void 0||n.resetBoundarySeq<0))return;let r={agentId:n.agentId,generation:n.generation,sessionId:n.sessionId,lastEventSeq:n.lastEventSeq,lastMessagePosition:n.lastMessagePosition,...n.resetBoundarySeq===void 0?{}:{resetBoundarySeq:n.resetBoundarySeq},...n.resetBoundaryId===void 0?{}:{resetBoundaryId:n.resetBoundaryId},version:n.version};return encodeVisibleMessageCursor(r)===e?r:void 0}catch{return}}export{createVisibleMessageCursor as a,normalizeVisibleDeltaLimits as c,withVisibleResetBoundary as d,n as i,normalizeVisibleMessageLimit as l,e as n,encodeVisibleMessageCursor as o,r,hasVisibleResetBoundaryChanged as s,t,parseVisibleMessageCursor as u};