openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 13.1 kB
JavaScript
import{i as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{u as t,w as n}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{i as r,o as i,p as a,r as o}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{i as s}from"./transcript-session-entry-codec-C1BgT9Uk.mjs";import{t as c}from"./transcript-json-values-C_3KtpDU.mjs";import{f as l,l as ee,o as u}from"./transcript-session-accessor.sqlite-reset-window-dONVogQj.mjs";import{t as d}from"./transcript-session-actor-transaction-LsCQWbNw.mjs";import{t as f}from"./transcript-index-hint-B4mtmDR4.mjs";import{d as p,i as m,o as h,p as g,u as _}from"./transcript-transcript-payload-F5t-l1WZ.mjs";import{r as v}from"./transcript-session-transcript-read-bytes-DJiUSe6g.mjs";import{n as y}from"./transcript-session-transcript-read-fence-Fb_G9Rx1.mjs";import{a as b,o as x,s as S,t as C}from"./transcript-session-accessor.sqlite-projection-read-B2CcfDOt.mjs";import{t as w}from"./transcript-session-accessor.sqlite-active-projection-BdMyLOaK.mjs";import{n as T}from"./transcript-session-actor-replica-F4aUgBtp.mjs";import{n as E,t as D}from"./transcript-session-bounded-context-selection-CJBr1Xe7.mjs";function readCacheTtlProjectionPrefix(e,t){if(!t||t.activePosition===0)return;let n=a`coalesce(identity.event_type, ${_(e.database.db,`navigation_last_type`,`event`)})`,r=C(e.database),o=r.selectFrom(`transcript_event_identities`).select([`session_id`,`seq`,`event_type`]).where(`session_id`,`=`,e.resolved.sessionId),s=e.hasUnindexedPrefix?r.selectFrom(`session_transcript_active_events as active`).leftJoin(o.clearWhere().modifyEnd(f(e.database.db,`idx_agent_transcript_event_identity_sequence`)).as(`identity`),e=>e.onRef(`identity.session_id`,`=`,`active.session_id`).onRef(`identity.seq`,`=`,`active.event_seq`)):r.selectFrom(o.where(`event_type`,`in`,[`custom`,`reset`]).unionAll(o.where(`event_type`,`is`,null)).as(`identity`)).crossJoin(`session_transcript_active_events as active`).whereRef(`identity.session_id`,`=`,`active.session_id`).whereRef(`identity.seq`,`=`,`active.event_seq`),c=i(e.database.db,s.crossJoin(`transcript_events as event`).whereRef(`event.session_id`,`=`,`active.session_id`).whereRef(`event.seq`,`=`,`active.event_seq`).select(`event.seq`).select(t=>t.case().when(n,`=`,`reset`).then(m(e.database.db,`event`,a.lit(`reset`))).else(null).end().as(`reset_json`)).select(_(e.database.db,`navigation_valid`,`event`).as(`navigation_valid`)).where(`active.session_id`,`=`,e.resolved.sessionId).where(`active.active_position`,`<`,t.activePosition).$call(e=>t.beforeRawSeq===void 0?e:e.where(`active.event_seq`,`<`,t.beforeRawSeq)).where(t=>t.case().when(`identity.event_type`,`not in`,[`custom`,`reset`]).then(!1).else(t.or([t(_(e.database.db,`navigation_valid`,`event`),`=`,0),t(n,`=`,`reset`),t.and([t(n,`=`,`custom`),t(_(e.database.db,`navigation_last_custom_type`,`event`),`=`,`openclaw.cache-ttl`)])])).end()).orderBy(`active.active_position`,`desc`));return T(t,(function*(){for(let t of c){p(t.navigation_valid);let n=t.reset_json??g(b(e,[t.seq])[0]);yield JSON.parse(n)}})())}function transcriptPositionSet(e,t){return a`(SELECT CAST(value AS BIGINT) FROM ${c(e.database.db,a.val(JSON.stringify(t)))})`}function readBoundedRetentionRanges(t,n,r){let i=/* @__PURE__ */ new Map,a=n.flatMap(({event:n,seq:r},a)=>{let o=e(n);return typeof o?.id==`string`?((!t.hasUnindexedPrefix||!i.has(o.id))&&i.set(o.id,r),(o.type===`compaction`||o.type===`reset`)&&typeof o.firstKeptEntryId==`string`?[{id:o.id,firstKeptEntryId:o.firstKeptEntryId,endIndex:a,seq:r}]:[]):[]}),s=[...new Set(a.map(e=>e.firstKeptEntryId))].filter(e=>!i.has(e));if(s.length>0){let e=n.reduce((e,t)=>Math.max(e,t.seq),-1/0),r=o(t.database.db,S(t.database).select([`identity.event_id`,`identity.seq`]).where(`identity.session_id`,`=`,t.resolved.sessionId).where(`identity.event_id`,`in`,s).where(`identity.seq`,`<=`,e)).rows;for(let e of r)i.set(e.event_id,e.seq)}if(t.hasUnindexedPrefix&&a.length>0){let e=new Set(a.map(e=>e.firstKeptEntryId));for(let n of l(t,{eventIds:[...e],maxRawSeq:a.reduce((e,t)=>Math.max(e,t.seq),-1/0)-1,first:!0})){let t=typeof n.event.id==`string`?n.event.id:void 0;if(t===void 0||!e.delete(t))continue;let r=i.get(t);if((r===void 0||n.event_seq<r)&&i.set(t,n.event_seq),e.size===0)break}}let c=/* @__PURE__ */ new Map;for(let e of a){let a=i.get(e.firstKeptEntryId);if(a===void 0||t.hasUnindexedPrefix&&a>=e.seq)continue;let o=e.endIndex>0&&n[0].seq>=a?0:Math.min(1,e.endIndex),s=o===0?0:e.endIndex;for(;o<s;){let e=Math.floor((o+s)/2);n[e].seq<a?o=e+1:s=e}c.set(e.id,{startIndex:o+r,endIndex:e.endIndex+r})}return c}function readUnindexedLogicalParents(t,n,r){let i=/* @__PURE__ */ new Map;if(n.length===0)return i;let a=o(t.database.db,C(t.database).selectFrom(`session_transcript_active_events as active`).leftJoin(`session_transcript_active_events as previous`,e=>e.onRef(`previous.session_id`,`=`,`active.session_id`).on(e=>e(`previous.active_position`,`=`,e(`active.active_position`,`-`,1)))).leftJoin(`transcript_events as parent`,e=>e.onRef(`parent.session_id`,`=`,`previous.session_id`).onRef(`parent.seq`,`=`,`previous.event_seq`)).select(e=>[`active.event_seq`,`previous.event_seq as parent_seq`,e.fn.coalesce(h(t.database.db,`parent`),e.val(`null`)).as(`parent_json`)]).where(`active.session_id`,`=`,t.resolved.sessionId).where(`active.event_seq`,`in`,transcriptPositionSet(t,n))).rows;for(let t of a){let n=e(r.get(t.event_seq));if(typeof n?.id!=`string`)continue;let a=t.parent_seq===null?void 0:e(JSON.parse(t.parent_json));i.set(n.id,typeof a?.id==`string`?a.id:null)}return i}function readIndexedLogicalParents(e,t){let n=d(e.database,e.resolved);if(n?.transcript.complete){let e=/* @__PURE__ */ new Map;for(let t of n.transcript.identities.values()){let r=n.transcript.active.get(t.seq);r&&e.set(r.active_position,t.event_id)}let r=/* @__PURE__ */ new Map;for(let i of t){let t=n.transcript.active.get(i);if(!t)continue;let a=e.get(t.active_position);a!==void 0&&r.set(a,e.get(t.active_position-1)??null)}return r}return new Map((t.length===0?[]:o(e.database.db,C(e.database).selectFrom(`session_transcript_active_events as active`).innerJoin(`transcript_event_identities as entry`,e=>e.onRef(`entry.session_id`,`=`,`active.session_id`).onRef(`entry.seq`,`=`,`active.event_seq`)).leftJoin(`session_transcript_active_events as previous`,e=>e.onRef(`previous.session_id`,`=`,`active.session_id`).on(e=>e(`previous.active_position`,`=`,e(`active.active_position`,`-`,1)))).leftJoin(`transcript_event_identities as parent`,e=>e.onRef(`parent.session_id`,`=`,`previous.session_id`).onRef(`parent.seq`,`=`,`previous.event_seq`)).select([`entry.event_id`,`parent.event_id as parent_id`]).where(`active.session_id`,`=`,e.resolved.sessionId).where(`active.event_seq`,`in`,transcriptPositionSet(e,t))).rows).map(e=>[e.event_id,e.parent_id]))}function readSessionTranscriptBoundedActiveContextCore(e,t){return readActiveContext(e,t,!0)}function readSessionTranscriptCompactionPlan(e,t){let{events:n,...r}=readActiveContext(e,t,!1);return r}function readActiveContext(o,c,l){let d=D(c);return w(o,o=>{let f=c.ignoreReadFence?void 0:y({database:o.database,...o.resolved}),consume=(e,t)=>{let r=c.onRead?.(o,e,t);if(n(r))throw Promise.resolve(r).catch(()=>{}),Error(`Transcript snapshot consumers must remain synchronous`)};if(o.version.rawSeq===null){let e=f?f.admission.effectiveParentId:o.state.leafEventId;return consume([]),{version:o.version,activeLeafEntryId:e,opaqueParents:e?/* @__PURE__ */ new Map([[e,null]]):/* @__PURE__ */ new Map,parents:/* @__PURE__ */ new Map,firstKeptRanges:/* @__PURE__ */ new Map,persistedSuffixStartSeq:0,boundaryCount:0,events:[],cacheTtlProjectionPrefixes:[],serializedBytes:0,totalEvents:0,transcriptMutationAt:o.version.updatedAt,truncated:!1,...f?{}:{completeActivePath:!0}}}let m=C(o.database),h=m.selectFrom(`transcript_events`).where(`session_id`,`=`,o.resolved.sessionId),b=d?o.contextMetadata:void 0,w=b?b.header:r(o.database.db,h.select([`seq`,_(o.database.db,`navigation_valid`).as(`navigation_valid`),a`${v()} + 1`.as(`serialized_bytes`)]).where(e=>e.or([e(_(o.database.db,`navigation_type`),`=`,`session`),e(_(o.database.db,`navigation_valid`),`=`,0)])).orderBy(`seq`,`asc`).limit(1));p(w?.navigation_valid);let T=w?.serialized_bytes??0,D=b?void 0:ee(o,`context`,f?.beforeRawSeq),O=!d&&D?.boundaryType===`compaction`?[]:D?.keptMessagePositions??[],k=d?O.slice(-(d.maxEvents+1)):O,A=D?.boundaryType===`compaction`?D.firstKeptActivePosition??D.boundaryActivePosition:D?.boundaryActivePosition,j=b?b.rows.filter(e=>!f||e.event_seq<f.beforeRawSeq).slice(0,d?d.maxEvents+1:void 0):i(o.database.db,x(o.database).select([`active.event_seq`,`active.active_position`,a`${v(`event`)} + 1`.as(`serialized_bytes`)]).where(`active.session_id`,`=`,o.resolved.sessionId).$if(f!==void 0,e=>e.where(`active.event_seq`,`<`,f.beforeRawSeq)).where(e=>{let t=e.and([e(`active.context_eligible`,`=`,1),...!d&&A!==void 0?[e(`active.active_position`,`>=`,A)]:[]]);return k.length>0?e.or([t,e(`active.message_position`,`in`,transcriptPositionSet(o,k))]):t}).orderBy(`active.active_position`,`desc`).$if(d!==void 0,e=>e.limit(d.maxEvents+1))),M=E(j,T,d),{selectedRows:N}=M,{serializedBytes:P,truncated:F}=M,I=N.map(e=>e.event_seq),L=b?void 0:r(o.database.db,m.selectFrom(S(o.database).select(e=>[`active.active_position`,`identity.seq`,e.fn.count(`identity.seq`).over().as(`boundary_count`)]).where(`identity.session_id`,`=`,o.resolved.sessionId).where(`identity.event_type`,`in`,[`compaction`,`reset`]).$if(f!==void 0,e=>e.where(`identity.seq`,`<`,f.beforeRawSeq)).orderBy(`active.active_position`,`desc`).limit(1).as(`boundary`)).innerJoin(`transcript_events as event`,e=>e.on(`event.session_id`,`=`,o.resolved.sessionId).onRef(`event.seq`,`=`,`boundary.seq`)).select([`boundary.active_position`,`boundary.seq`,`boundary.boundary_count`,a`${v(`event`)} + 1`.as(`serialized_bytes`)])),R=L?.boundary_count??0;if(!b&&o.hasUnindexedPrefix)for(let e of u(o,f?.beforeRawSeq))e.event.type!==`compaction`&&e.event.type!==`reset`||f!==void 0&&e.event_seq>=f.beforeRawSeq||(R+=1,(!L||e.active_position>L.active_position)&&(L={active_position:e.active_position,seq:e.event_seq,serialized_bytes:e.serialized_bytes,boundary_count:R}));I.sort((e,t)=>e-t);let z;L&&!I.includes(L.seq)&&(!d||P+L.serialized_bytes<=d.maxBytes?(z=L.seq,I.unshift(L.seq),P+=L.serialized_bytes):F=!0);let B=w?[w.seq,...I]:I,V=new Map(N.map(e=>[e.event_seq,e])),H=/* @__PURE__ */ new Map,U,W,G,K=h.select([`seq`,`event_json`,`event_zstd`,`event_utf8_bytes`]);function*readPayloads(){if(!t(o.database.db)||l)B.length>0&&(yield*i(o.database.db,K.where(`seq`,`in`,transcriptPositionSet(o,B))));else for(let e=0;e<B.length;){let t=e,n=0;do{let e=B[t++];n+=e===w?.seq?T:e===L?.seq?L.serialized_bytes:V.get(e).serialized_bytes}while(t<B.length&&t-e<64&&n<4194304);yield*i(o.database.db,K.where(`seq`,`in`,transcriptPositionSet(o,B.slice(e,t)))),e=t}}for(let t of readPayloads()){let r=JSON.parse(g(t)),i=e(r),a=V.get(t.seq),o=c.onEntry?.(r,{rawSeq:t.seq,serializedBytes:t.seq===w?.seq?T:t.seq===L?.seq?L.serialized_bytes:a.serialized_bytes});if(n(o))throw Promise.resolve(o).catch(()=>{}),Error(`Transcript snapshot consumers must remain synchronous`);a&&typeof i?.id==`string`&&(!U||a.active_position<U.active_position)&&(U=a,W=i),t.seq===z&&(G=i),H.set(t.seq,l||!i?r:{type:i.type,...`id`in i?{id:i.id}:{},...`parentId`in i?{parentId:i.parentId}:{},firstKeptEntryId:i.firstKeptEntryId})}let te=o.hasUnindexedPrefix?readUnindexedLogicalParents(o,I,H):readIndexedLogicalParents(o,I),q=l&&w?[H.get(w.seq)]:[],J=I.map(e=>({event:H.get(e),seq:e})),Y=/* @__PURE__ */ new Map,X;for(let{event:t,seq:n}of J){let r=e(t);n===z?X=r?.id:r&&`id`in r&&`parentId`in r&&(typeof X==`string`&&typeof r.parentId==`string`&&r.parentId!==X&&Y.set(r.parentId,X),X=r.id),l&&q.push(t)}let ne=[...L&&s(G)&&G.type===`compaction`?[{activePosition:L.active_position,id:G.id,entry:G}]:[],...U&&typeof W?.id==`string`?[{activePosition:U.active_position,id:W.id,entry:W}]:[]].flatMap(e=>{let t=readCacheTtlProjectionPrefix(o,{...e,beforeRawSeq:f?.beforeRawSeq});return t?[t]:[]}),Z=f?f.admission.effectiveParentId:o.state.leafEventId;Z&&X!==Z&&Y.set(Z,typeof X==`string`?X:null);let re=readBoundedRetentionRanges(o,J,+!!w),Q=o.version,$=new Map(N.map(e=>[e.event_seq,e.serialized_bytes]));return L&&$.set(L.seq,L.serialized_bytes),consume([...w?[{rawSeq:w.seq,serializedBytes:T}]:[],...I.map(e=>({rawSeq:e,serializedBytes:$.get(e)}))],J.some(({event:t})=>e(t)?.type===`message`)?!0:void 0),{version:Q,activeLeafEntryId:Z,opaqueParents:Y,parents:te,firstKeptRanges:re,persistedSuffixStartSeq:I[0]??(w?w.seq+1:0),boundaryCount:R,events:q,cacheTtlProjectionPrefixes:ne,serializedBytes:P,totalEvents:o.state.activeEventCount,transcriptMutationAt:Q.updatedAt,truncated:F,...!F&&!f&&!o.hasUnindexedPrefix&&N.length===o.state.activeEventCount?{completeActivePath:!0}:{}}},{readOnly:c.readOnly,resolvedScope:c.resolvedScope,transaction:c.transaction})}export{readSessionTranscriptCompactionPlan as n,readSessionTranscriptBoundedActiveContextCore as t};