UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 3.09 kB
import{B as e}from"./transcript-plugin-instance-invocation-7pP9ZY8b.mjs";import{h as t}from"./transcript-number-coercion-Dr06QpMa.mjs";import{o as n}from"./transcript-kysely-sync-DQ_Yn8jW.mjs";import{i as r}from"./transcript-session-accessor.sqlite-visible-cursor-CP4ccuQd.mjs";import{a as i,o as a,r as o,s}from"./transcript-session-accessor.sqlite-projection-read-HIelFCp6.mjs";import{l as c,r as l,s as u,t as d,u as f}from"./transcript-session-accessor.sqlite-reset-window-CJLYEFaR.mjs";function readRecentSessionTranscriptMessageEventsFromProjection(e,n){let i=f(e),a=t(n.maxMessages,0,{min:0,max:r}),o=t(n.maxLines,0,{min:0});if(a===0||o===0)return{activeLeafEntryId:e.state.leafEventId,events:[],totalMessages:i.total};let s=t(n.maxBytes,8388608,{min:1024}),c=l(e,Math.max(0,i.total-Math.min(o,a)),i.total,`desc`),d=i.total,p=0;for(let e of c){if(d<i.total&&p+e.serialized_bytes>s)break;d=e.logicalPosition,p+=e.serialized_bytes}return{activeLeafEntryId:e.state.leafEventId,events:u(e,d,i.total),totalMessages:i.total}}function withRecentSessionTranscriptActiveEventsInSnapshot(r,a,o){let c=t(a,0,{min:0}),l=s(r.database).select([`event.seq`,`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(c),u=!0;try{return o(t=>{if(!u)throw Error(`Transcript visitor used outside its read snapshot`);if(c===0)return;let a;for(let o of n(r.database.db,l)){let n=i(r,o,o.seq),s;try{s=JSON.parse(n)}catch(t){a=e(t,`Transcript event JSON parsing failed`);continue}t(s)}if(a!==void 0)throw a})}finally{u=!1}}function readSessionTranscriptBoundedMessageTailPageFromProjection(e,n){let i=f(e),s={boundarySeq:c(e)?.boundarySeq,generation:e.generation,indexedSeq:e.state.indexedSeq},u=i.total,p=t(n.offset,0,{min:0,max:u}),m=t(n.maxMessages,0,{min:0,max:r}),h=t(n.maxBytes,0,{min:0}),g=Math.max(0,u-p),_=Math.max(0,g-m),v=g-_,y=v>0&&h>0?Array.from(l(e,_,g)):[],b=n.oversizedMessageCheck,x=b?{hasOversizedMessages:(b.includeEarlier||h===0||y.some(e=>e.serialized_bytes>h))&&d(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:s,totalMessages:u};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?[]:a(e,S.map(e=>e.event_seq)).map(e=>[e.seq,e])),E=S.sort((e,t)=>e.message_position-t.message_position).flatMap(t=>{let n=T.get(t.event_seq);return n===void 0?[]:[o({...t,...n},e)]});return{...x,activeLeafEntryId:e.state.leafEventId,events:E,newestContiguousEventCount:C??S.length,scannedMessages:v,serializedBytes:w,snapshot:s,totalMessages:u}}export{readSessionTranscriptBoundedMessageTailPageFromProjection as n,withRecentSessionTranscriptActiveEventsInSnapshot as r,readRecentSessionTranscriptMessageEventsFromProjection as t};