openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 4.09 kB
JavaScript
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{Fn as object,Jn as string,Nt as array,or as union,xn as literal}from"./schemas-DuCQi-Qb.mjs";
//!src/agents/embedded-agent-runner/cache-ttl-checkpoint.ts
const prunedResult=union([object({key:string(),mode:literal(`soft`)}),object({key:string(),mode:literal(`hard`),placeholder:string()})]);const frozenResult=object({key:string(),sourceHash:string(),texts:array(string()).optional()});const snapshotSchema=object({prunedToolResults:array(prunedResult),ambiguousToolResultBaseKeys:array(string()).default([]),frozenToolResults:array(frozenResult).default([])});function changesSchema(e){return object({set:array(e),remove:array(string())})}const deltaSchema=object({cacheTtlDelta:object({prunedToolResults:changesSchema(prunedResult),ambiguousToolResultBaseKeys:changesSchema(string()),frozenToolResults:changesSchema(frozenResult)})});const MAX_DELTAS=31;const MAX_DELTA_BYTES=65536;const projectionFields=[...Object.keys(snapshotSchema.shape),...Object.keys(deltaSchema.shape)];function isCacheTtlTouch(e){return isRecord(e)&&typeof e.timestamp===`number`&&Number.isFinite(e.timestamp)&&!projectionFields.some(t=>t in e)}function changes(e,t,n){const r=new Map(e.map(e=>[n(e),e]));return{set:t.filter(e=>{const t=r.get(n(e));r.delete(n(e));return JSON.stringify(t)!==JSON.stringify(e)}),remove:[...r.keys()]}}function prepareCacheTtlCheckpoint(e,t){if(t){const keyed=e=>e.key;const n={cacheTtlDelta:{prunedToolResults:changes(t.snapshot.prunedToolResults,e.prunedToolResults,keyed),ambiguousToolResultBaseKeys:changes(t.snapshot.ambiguousToolResultBaseKeys,e.ambiguousToolResultBaseKeys,e=>e),frozenToolResults:changes(t.snapshot.frozenToolResults,e.frozenToolResults,keyed)}};if(Object.values(n.cacheTtlDelta).every(({set:e,remove:t})=>!e.length&&!t.length))return{checkpoint:t};const r=t.bytes+Buffer.byteLength(JSON.stringify(n));if(t.deltas<MAX_DELTAS&&r<MAX_DELTA_BYTES)return{marker:n,checkpoint:{snapshot:e,deltas:t.deltas+1,bytes:r}}}return{marker:e,checkpoint:{snapshot:e,deltas:0,bytes:0}}}function applyChanges(e,t,n){for(const n of t.remove)e.delete(n);for(const r of t.set)e.set(n(r),r)}function readCacheTtlCheckpoint(e){const t=[];let n=false;for(let r=e.length-1;r>=0;r--){const i=e[r];if(i?.type===`reset`)return;if(i?.type!==`custom`||i.customType!==`openclaw.cache-ttl`)continue;const a=i.data;const o=isRecord(a)&&`cacheTtlDelta`in a;const s=o?deltaSchema.safeParse(a):snapshotSchema.safeParse(a);if(!s.success){if(o||!isCacheTtlTouch(a)){t.length=0;n=true}continue}if(`cacheTtlDelta`in s.data){t.push(s.data);continue}if(!t.length)return{snapshot:s.data,deltas:n?MAX_DELTAS:0,bytes:0};const keyed=e=>e.key;const c=new Map(s.data.prunedToolResults.map(e=>[e.key,e]));const l=new Map(s.data.ambiguousToolResultBaseKeys.map(e=>[e,e]));const u=new Map(s.data.frozenToolResults.map(e=>[e.key,e]));let d=0;for(const e of t.toReversed()){applyChanges(c,e.cacheTtlDelta.prunedToolResults,keyed);applyChanges(l,e.cacheTtlDelta.ambiguousToolResultBaseKeys,e=>e);applyChanges(u,e.cacheTtlDelta.frozenToolResults,keyed);d+=Buffer.byteLength(JSON.stringify(e))}return{snapshot:{prunedToolResults:[...c.values()],ambiguousToolResultBaseKeys:[...l.values()],frozenToolResults:[...u.values()]},deltas:n?MAX_DELTAS:t.length,bytes:d}}}function serializeCacheTtlToolResultProjections(e){const t=new Map(e.restoredCacheTtl);for(const[n,r]of e.replacements)if(r.cacheTtl===`soft`)t.set(n,{mode:`soft`});else if(r.cacheTtl===`hard`){const e=r.content.flatMap(e=>e.type===`text`?[e.text]:[]).join(`
`);t.set(n,{mode:`hard`,placeholder:e})}return{prunedToolResults:Array.from(t,([e,t])=>Object.assign({key:e},t)),ambiguousToolResultBaseKeys:[...e.ambiguousBaseKeys],frozenToolResults:[...e.sourceHashByKey].flatMap(([t,n])=>{if(!e.frozen.has(t))return[];const r=e.replacements.get(t);return[{key:t,sourceHash:n,...!r?.cacheTtl&&r?{texts:r.content.flatMap(e=>e.type===`text`?[e.text]:[])}:{}}]})}}export{serializeCacheTtlToolResultProjections as i,prepareCacheTtlCheckpoint as n,readCacheTtlCheckpoint as r,isCacheTtlTouch as t};