openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.8 kB
JavaScript
import{v as e}from"./transcript-redact-BJEzMNLa.mjs";import{L as t,T as n}from"./transcript-sqlite-database-admission-IPmtadyc.mjs";import{d as r,t as i}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{u as a}from"./transcript-kysely-sync-cache-state-DHCXK7mb.mjs";import{d as o}from"./transcript-sqlite-schema-facts-C1_zV4Yg.mjs";import{a as s,i as c,r as l}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{t as u}from"./transcript-sqlite-number-5mfxKD9j.mjs";import{n as d}from"./transcript-text-bytes-DrHX89-B.mjs";import{randomUUID as f}from"node:crypto";const p=10485760,m=/* @__PURE__ */ new WeakMap,h=/* @__PURE__ */ new WeakMap,g=/* @__PURE__ */ new WeakSet;function trajectoryRuntimeRetentionState(e){let t=m.get(e);return t||(t={},m.set(e,t)),t}function trajectoryRuntimeRetentionDue(e,t){return!e.pending&&(e.sweptAt===void 0||t<e.sweptAt||t-e.sweptAt>=36e5)}function compareRuns(e,t){return e.newest-t.newest||e.sessionId.localeCompare(t.sessionId)||(e.runId??``).localeCompare(t.runId??``)||(e.order<t.order?-1:+(e.order>t.order))}function readRuns(e,t,n){let r=s(e).selectFrom(`trajectory_runtime_events`).select([`session_id as sessionId`,`run_id as runId`]).select(t=>[t.fn.max(`created_at`).as(`newest`),t.fn.sum(t(t.fn.coalesce(`event_utf8_bytes`,t.fn(`octet_length`,[`event_json`])),`+`,1)).as(`bytes`),t.fn.countAll().as(`events`),d(e,t.ref(`session_id`)).as(`sessionOrder`),d(e,t.ref(`run_id`)).as(`runOrder`)]).groupBy([`session_id`,`run_id`]);return t!==void 0&&(r=r.where(`session_id`,`=`,t)),n!==void 0&&(r=n===null?r.where(`run_id`,`is`,null):r.where(`run_id`,`=`,n)),l(e,r).rows.map(e=>({sessionId:e.sessionId,runId:e.runId,newest:u(e.newest),bytes:u(e.bytes),events:u(e.events),order:`${e.sessionOrder}/${e.runId===null?`0`:`1${e.runOrder}`}`}))}function prepareTrajectoryRuntimeRetention(e,t,n){return{sessionId:t.sessionId,cutoff:n-12096e5,maxBytes:Math.max(1,Math.floor(t.maxGlobalRuntimeBytes??536870912)),runs:readRuns(e)}}function changes(e,t){return t?o(e):c(e,s(e).selectNoFrom(e=>e.fn(`total_changes`,[]).as(`changes`))).changes}function beginTrajectoryRuntimeRetention(e,t){g.has(e)||(g.add(e),a(r(e),()=>{let t=h.get(e);t&&(Atomics.store(t.lease,0,0),h.delete(e))}));let i=o(e)!==void 0,s={id:f(),lease:t,version:n(e),nativeChanges:i,changes:changes(e,i),receipts:/* @__PURE__ */ new Set,sessions:/* @__PURE__ */ new Map,queue:[],cursor:0,additions:[],totalBytes:0,expiredRuns:0,unprotectedRuns:0};return h.set(e,s),s.id}function currentSweep(e){let t=h.get(e);if(t&&(Atomics.load(t.lease,0)!==1||!e.isOpen||t.version===void 0||n(e)!==t.version||changes(e,t.nativeChanges)!==t.changes))h.delete(e);else return t}function stageRetentionMutation(e,t,n){let r=changes(e,t.nativeChanges);i(e,()=>{h.get(e)===t&&Atomics.load(t.lease,0)===1&&(n!==void 0&&t.receipts.add(n),t.changes=r)})}function captureTrajectoryRuntimeRetentionMutation(e){let t=currentSweep(e);if(t)return n=>{stageRetentionMutation(e,t,n)}}function captureTrajectoryRuntimeRetentionMetadataMutation(e){let t=currentSweep(e);return t?()=>stageRetentionMutation(e,t):void 0}function account(e,t,n){e.totalBytes+=n*t.bytes,t.sessionId!==e.plan.sessionId&&(e.unprotectedRuns+=n,e.expiredRuns+=n*Number(t.newest<e.plan.cutoff))}function replaceSession(e,t,n){let r=e.sessions.get(t),i=/* @__PURE__ */ new Map;for(let t of n){let n=r?.get(t.runId),a=n&&n.newest===t.newest&&n.bytes===t.bytes&&n.events===t.events?n:t;i.set(a.runId,a),a!==n&&(account(e,a,1),a.sessionId!==e.plan.sessionId&&e.additions.push(a))}for(let t of r?.values()??[])i.get(t.runId)!==t&&account(e,t,-1);e.sessions.set(t,i)}function selectTrajectoryRuntimeRetentionBatch(t,n){let r=currentSweep(t),i=[];if(!r||r.id!==n.sweepId)return{sweepId:n.sweepId,runs:i,refresh:!0};let a=new Map([...r.receipts].map(e=>[e,readRuns(t,e)]));if(n.snapshot&&!r.plan){r.plan={...n.snapshot,runs:[]};let t=e(n.snapshot.runs,e=>e.sessionId);for(let[e,n]of t)replaceSession(r,e,a.get(e)??n);for(let[e,n]of a)t.has(e)||replaceSession(r,e,n);r.queue=r.additions.toSorted(compareRuns),r.additions=[]}else if(r.plan)for(let[e,t]of a)replaceSession(r,e,t);r.receipts.clear(),r.additions.sort(compareRuns);let o=0;for(;i.length<16;){let e=r.queue[r.cursor],t=r.additions[0],n=t&&(!e||compareRuns(t,e)<0),a=n?t:e;if(!a)break;if(r.sessions.get(a.sessionId)?.get(a.runId)!==a)n?r.additions.shift():r.cursor++;else{if(i.length&&o+a.bytes>10485760)break;n?r.additions.shift():r.cursor++,i.push(a),o+=a.bytes}}return{sweepId:n.sweepId,runs:i}}function deleteTrajectoryRuntimeRetention(e,n){let r=n.refresh?void 0:currentSweep(e.db);if(!r?.plan||r.id!==n.sweepId)return{complete:!1,refresh:!0,deleted:0,invalidated:0,totalBytes:0};let a=s(e.db),o=r.totalBytes,c=r.expiredRuns,u=[],d=0;for(let i of n.runs){if(i.sessionId===r.plan.sessionId||i.newest>=r.plan.cutoff&&o<=r.plan.maxBytes)continue;let n=readRuns(e.db,i.sessionId,i.runId)[0];if(n?.newest!==i.newest||n.bytes!==i.bytes||n.events!==i.events){d++;break}t(e.db,[],()=>l(e.db,a.deleteFrom(`trajectory_runtime_events`).where(`session_id`,`=`,i.sessionId).where(`run_id`,i.runId===null?`is`:`=`,i.runId))),o-=i.bytes,c-=Number(i.newest<r.plan.cutoff),u.push(i)}let f=changes(e.db,r.nativeChanges);return i(e.db,()=>{for(let e of u)r.sessions.get(e.sessionId)?.delete(e.runId),account(r,e,-1);r.changes=f,d&&h.delete(e.db)}),{complete:c===0&&(o<=r.plan.maxBytes||u.length===r.unprotectedRuns),refresh:d>0,deleted:u.length,invalidated:d,totalBytes:o}}export{prepareTrajectoryRuntimeRetention as a,trajectoryRuntimeRetentionState as c,deleteTrajectoryRuntimeRetention as i,p as l,captureTrajectoryRuntimeRetentionMetadataMutation as n,selectTrajectoryRuntimeRetentionBatch as o,captureTrajectoryRuntimeRetentionMutation as r,trajectoryRuntimeRetentionDue as s,beginTrajectoryRuntimeRetention as t};