UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 6.4 kB
import{a as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{i as t}from"./transcript-global-singleton-DIJgobpU.mjs";import"./transcript-sqlite-database-admission-IPmtadyc.mjs";import"./transcript-sqlite-schema-facts-C1_zV4Yg.mjs";import"./transcript-session-sqlite-target-paths-C_ECqUX6.mjs";import"./transcript-session-row-changes-DGiwEF1b.mjs";import{c as n,f as r,g as i,h as a,i as o}from"./transcript-schemas-C_W5KYqZ.mjs";import{i as s}from"./transcript-session-entry-codec-C1BgT9Uk.mjs";import"./transcript-session-transcript-authority-DYcFN1Ei.mjs";import"./transcript-transcript-events-BDUVmbAW.mjs";import{f as c}from"./transcript-tool-result-pairing-DrpmDHYa.mjs";import{s as l,u}from"./transcript-transcript-tree-CEw6GxlK.mjs";import{a as d,i as f}from"./transcript-session-transcript-projection-append-BX6beDU2.mjs";import"./transcript-session-transcript-read-fence-Fb_G9Rx1.mjs";import"./transcript-activity-summary-3AtfjidQ.mjs";import"./transcript-transcript-entry-anchor-nHWvyQR0.mjs";import{n as p,t as m}from"./transcript-session-bounded-context-selection-CJBr1Xe7.mjs";import"node:path";const h=i([r({key:a(),mode:n(`soft`)}),r({key:a(),mode:n(`hard`),placeholder:a()})]),g=r({key:a(),sourceHash:a(),texts:o(a()).optional()}),_=r({prunedToolResults:o(h),ambiguousToolResultBaseKeys:o(a()).default([]),frozenToolResults:o(g).default([])});function changesSchema(e){return r({set:o(e),remove:o(a())})}const v=r({cacheTtlDelta:r({prunedToolResults:changesSchema(h),ambiguousToolResultBaseKeys:changesSchema(a()),frozenToolResults:changesSchema(g)})}),y=[...Object.keys(_.shape),...Object.keys(v.shape)];function isCacheTtlTouch(t){return e(t)&&typeof t.timestamp==`number`&&Number.isFinite(t.timestamp)&&!y.some(e=>e in t)}function applyChanges(e,t,n){for(let n of t.remove)e.delete(n);for(let r of t.set)e.set(n(r),r)}function readCacheTtlCheckpoint(t){let n=[],r=!1;for(let i=t.length-1;i>=0;i--){let a=t[i];if(a?.type===`reset`)return;if(a?.type!==`custom`||a.customType!==`openclaw.cache-ttl`)continue;let o=a.data,s=e(o)&&`cacheTtlDelta`in o,c=s?v.safeParse(o):_.safeParse(o);if(!c.success){(s||!isCacheTtlTouch(o))&&(n.length=0,r=!0);continue}if(`cacheTtlDelta`in c.data){n.push(c.data);continue}if(!n.length)return{snapshot:c.data,deltas:r?31:0,bytes:0};let keyed=e=>e.key,l=new Map(c.data.prunedToolResults.map(e=>[e.key,e])),u=new Map(c.data.ambiguousToolResultBaseKeys.map(e=>[e,e])),d=new Map(c.data.frozenToolResults.map(e=>[e.key,e])),f=0;for(let e of n.toReversed())applyChanges(l,e.cacheTtlDelta.prunedToolResults,keyed),applyChanges(u,e.cacheTtlDelta.ambiguousToolResultBaseKeys,e=>e),applyChanges(d,e.cacheTtlDelta.frozenToolResults,keyed),f+=Buffer.byteLength(JSON.stringify(e));return{snapshot:{prunedToolResults:[...l.values()],ambiguousToolResultBaseKeys:[...u.values()],frozenToolResults:[...d.values()]},deltas:r?31:n.length,bytes:f}}}function collectCacheTtlProjectionPrefix(t,n){if(s(t.entry)&&(t.entry.type===`reset`||readCacheTtlCheckpoint([t.entry])))return;let r=[];for(let t of n)if(s(t)&&(t.type===`reset`||t.type===`custom`&&t.customType===`openclaw.cache-ttl`&&!isCacheTtlTouch(t.data))&&(e(t)&&r.push(t),t.type===`reset`||readCacheTtlCheckpoint([t])))break;return r.length?{anchorIds:[t.id],entries:r.toReversed()}:void 0}function readSessionActorBoundedHistory(t,n){let r={...t.hot.transcript.version},i=m(n),a=l(t.events.map(e=>e.event)),o=u(a,a.leafId).filter(e=>f(e.entry)).map((e,n)=>{let r=t.events[e.index];return{rawSeq:r.rawSeq,event:r.event,node:e,activePosition:n,serialized_bytes:r.serializedBytes}}),h=t.events.find(t=>e(t.event)&&t.event.type===`session`),g=h?h.serializedBytes:0,_=o.filter(({event:t})=>e(t)&&(t.type===`reset`||t.type===`compaction`)),v=_.at(-1),y=!i&&v?.event,b=e(y)?o.find(e=>e.node.id===y.firstKeptEntryId):void 0,x=b&&v&&b.activePosition<v.activePosition?b.activePosition:v?.activePosition??0,S=v&&e(y)&&y.type===`reset`?new Set(c(o.slice(x,v.activePosition).flatMap(({event:e})=>s(e)?[e]:[]))):void 0,C=new Set(i&&t.hot.transcript.modelContext.kind===`resident`?t.hot.transcript.modelContext.entries.map(e=>e.rawSeq):[]),w=o.filter(e=>!i&&v&&e.activePosition<v.activePosition?S?S.has(e.event):e.activePosition>=x&&d(e.event)===1:d(e.event)===1||C.has(e.rawSeq)),T=p(w.toReversed(),g,i),{selectedRows:E}=T,{serializedBytes:D,truncated:O}=T,k=E.toSorted((e,t)=>e.rawSeq-t.rawSeq),A;v&&!E.includes(v)&&(!i||D+v.serialized_bytes<=i.maxBytes?(A=v,k.unshift(v),D+=v.serialized_bytes):O=!0);let j=new Map(k.map(e=>[e.node.id,o[e.activePosition-1]?.node.id??null])),M=/* @__PURE__ */ new Map,N;for(let t of k){let n=t.event;t===A?N=t.node.id:e(n)&&`parentId`in n&&(N!==void 0&&typeof n.parentId==`string`&&n.parentId!==N&&M.set(n.parentId,N),N=t.node.id)}a.leafId&&a.leafId!==N&&M.set(a.leafId,N??null);let P=+!!h,F=/* @__PURE__ */ new Map;for(let[t,n]of k.entries()){let r=n.event;if(!e(r)||r.type!==`reset`&&r.type!==`compaction`||typeof r.firstKeptEntryId!=`string`)continue;let i=o.find(e=>e.node.id===r.firstKeptEntryId);if(!i)continue;let a=t>0&&k[0].rawSeq>=i.rawSeq?0:Math.min(1,t);for(;a<t&&k[a].rawSeq<i.rawSeq;)a++;F.set(n.node.id,{startIndex:a+P,endIndex:t+P})}let I=[...A&&e(A.event)&&A.event.type===`compaction`?[A]:[],...E.at(-1)?[E.at(-1)]:[]].flatMap(e=>{let t=collectCacheTtlProjectionPrefix({id:e.node.id,entry:e.event},o.slice(0,e.activePosition).toReversed().map(e=>e.event));return t?[t]:[]});return{kind:`bounded`,snapshot:{version:r,activeLeafEntryId:a.leafId,opaqueParents:M,parents:j,firstKeptRanges:F,persistedSuffixStartSeq:k[0]?.rawSeq??(h?h.rawSeq+1:0),boundaryCount:_.length,events:structuredClone([...h?[h.event]:[],...k.map(e=>e.event)]),cacheTtlProjectionPrefixes:structuredClone(I),serializedBytes:D,totalEvents:o.length,transcriptMutationAt:r.updatedAt,truncated:O,...!O&&E.length===o.length?{completeActivePath:!0}:{}}}}const b=t(Symbol.for(`openclaw.sessionActorReplicas`),()=>({cells:/* @__PURE__ */ new Map,cellsBySessionKey:/* @__PURE__ */ new Map,snapshots:0,bytes:0}),e=>{e.unsubscribe?.(),e.unsubscribe=void 0;for(let t of e.cells.values())discard(t);e.cells.clear(),e.cellsBySessionKey.clear(),e.snapshots=0,e.bytes=0});function discard(e){(e.snapshot||e.entry||e.transcript)&&--b.snapshots,b.bytes-=e.bytes,e.transcript=void 0,e.transcriptBytes=0,e.snapshot=void 0,e.entry=void 0,e.bytes=0}export{collectCacheTtlProjectionPrefix as n,readSessionActorBoundedHistory as t};