openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 3.13 kB
JavaScript
import{B as e}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{p as t}from"./transcript-number-coercion-D2B2TTt-.mjs";import{o as n}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{l as r,r as i,s as a,t as o,u as s}from"./transcript-session-accessor.sqlite-reset-window-dONVogQj.mjs";import{p as c}from"./transcript-transcript-payload-F5t-l1WZ.mjs";import{i as l}from"./transcript-session-accessor.sqlite-visible-cursor-CP4ccuQd.mjs";import{a as u,o as d,r as f}from"./transcript-session-accessor.sqlite-projection-read-B2CcfDOt.mjs";function readRecentSessionTranscriptMessageEventsFromProjection(e,n){let r=s(e),o=t(n.maxMessages,0,{min:0,max:l}),c=t(n.maxLines,0,{min:0});if(o===0||c===0)return{activeLeafEntryId:e.state.leafEventId,events:[],totalMessages:r.total};let u=t(n.maxBytes,8388608,{min:1024}),d=i(e,Math.max(0,r.total-Math.min(c,o)),r.total,`desc`),f=r.total,p=0;for(let e of d){if(f<r.total&&p+e.serialized_bytes>u)break;f=e.logicalPosition,p+=e.serialized_bytes}return{activeLeafEntryId:e.state.leafEventId,events:a(e,f,r.total),totalMessages:r.total}}function withRecentSessionTranscriptActiveEventsInSnapshot(r,i,a){let o=t(i,0,{min:0}),s=d(r.database).select([`event.event_json`,`event.event_zstd`,`event.event_utf8_bytes`]).where(`active.session_id`,`=`,r.resolved.sessionId).where(`active.context_eligible`,`=`,1).orderBy(`active.active_position`,`desc`).limit(o),l=!0;try{return a(t=>{if(!l)throw Error(`Transcript visitor used outside its read snapshot`);if(o===0)return;let i;for(let a of n(r.database.db,s)){let n=c(a),r;try{r=JSON.parse(n)}catch(t){i=e(t,`Transcript event JSON parsing failed`);continue}t(r)}if(i!==void 0)throw i})}finally{l=!1}}function readSessionTranscriptBoundedMessageTailPageFromProjection(e,n){let a=s(e),c={boundarySeq:r(e)?.boundarySeq,generation:e.generation,indexedSeq:e.state.indexedSeq},d=a.total,p=t(n.offset,0,{min:0,max:d}),m=t(n.maxMessages,0,{min:0,max:l}),h=t(n.maxBytes,0,{min:0}),g=Math.max(0,d-p),_=Math.max(0,g-m),v=g-_,y=v>0&&h>0?Array.from(i(e,_,g)):[],b=n.oversizedMessageCheck,x=b?{hasOversizedMessages:(b.includeEarlier||h===0||y.some(e=>e.serialized_bytes>h))&&o(e,b.includeEarlier?0:_,g,h,b.roles)}:{};if(v===0||h===0)return{...x,activeLeafEntryId:e.state.leafEventId,events:[],newestContiguousEventCount:0,scannedMessages:v,serializedBytes:0,snapshot:c,totalMessages:d};if(y.length!==v)throw Error(`Active transcript bounded message page is incomplete`);let S=[],C,w=0;for(let e=y.length-1;e>=0;--e){let t=y[e];w+t.serialized_bytes>h?C??=S.length:(S.push(t),w+=t.serialized_bytes)}let T=new Map(S.length===0?[]:u(e,S.map(e=>e.event_seq)).map(e=>[e.seq,e])),E=S.sort((e,t)=>e.message_position-t.message_position).flatMap(e=>{let t=T.get(e.event_seq);return t===void 0?[]:[f({...e,...t})]});return{...x,activeLeafEntryId:e.state.leafEventId,events:E,newestContiguousEventCount:C??S.length,scannedMessages:v,serializedBytes:w,snapshot:c,totalMessages:d}}export{readSessionTranscriptBoundedMessageTailPageFromProjection as n,withRecentSessionTranscriptActiveEventsInSnapshot as r,readRecentSessionTranscriptMessageEventsFromProjection as t};