openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.83 kB
JavaScript
import{h as e}from"./transcript-number-coercion-Dr06QpMa.mjs";import{t}from"./transcript-session-transcript-read-fence-error-DfU0swK6.mjs";import"./transcript-session-transcript-projection-error-BeJK3y26.mjs";import{i as n,m as r,r as i}from"./transcript-kysely-sync-DQ_Yn8jW.mjs";import{t as a}from"./transcript-json-values-CmANsVU4.mjs";import{n as o}from"./transcript-session-transcript-read-fence-CgDWkLBX.mjs";import{a as s,c,d as l,o as u,s as d,u as f}from"./transcript-session-accessor.sqlite-visible-cursor-CP4ccuQd.mjs";import{d as p,f as m,n as h,r as g,t as _,u as v}from"./transcript-session-accessor.sqlite-projection-read-HIelFCp6.mjs";import{t as y}from"./transcript-session-accessor.sqlite-active-projection-DqkcLtDM.mjs";import{a as b,l as x,s as S,u as C}from"./transcript-session-accessor.sqlite-reset-window-CJLYEFaR.mjs";import{n as w,r as T}from"./transcript-session-accessor.sqlite-active-events-read-33K9npQ5.mjs";function readLatestSessionTranscriptMessageEvent(e,t){return y(e,e=>{let t=o({database:e.database,...e.resolved}),n=h(e.database).latest({sessionId:e.resolved.sessionId,start:0,endExclusive:t?.beforeActiveMessagePosition??e.state.activeMessageCount});return n?g(n,e):void 0},t)}function readRecentSessionTranscriptActiveEvents(e,t,n){return y(e,e=>T(e,t,e=>{let t=[];return e(e=>t.push(e)),t.reverse()}),n)}function readSessionTranscriptVisibleMessageDeltaCore(e,n={},r){let{maxMessages:i,maxBytes:a,start:p}=c(n);return y(e,e=>{let r=o({database:e.database,...e.resolved}),c=e.generation;if(!c)return{kind:`missing`};let m,readLatestReset=()=>(m===void 0&&(m=b(e,r?.beforeRawSeq)??null),m),h=n.cursor===void 0?void 0:f(n.cursor),freshCursor=()=>{let t=s({agentId:e.resolved.agentId,generation:c,sessionId:e.resolved.sessionId});return p!==`reset-window`&&h?.resetBoundarySeq===void 0?t:l(t,readLatestReset())},reset=e=>({kind:`reset`,cursor:u(freshCursor()),reason:e}),g=n.cursor===void 0?freshCursor():h;if(!g)return reset(`invalid_cursor`);if(g.agentId!==e.resolved.agentId||g.sessionId!==e.resolved.sessionId)return reset(`scope_mismatch`);let _=g.generation===c,v=g.resetBoundarySeq!==void 0&&d(g,readLatestReset(),_);if(!_)return reset(v?`session_reset`:`generation_mismatch`);if(r!==void 0&&(g.lastMessagePosition>=r.beforeActiveMessagePosition||(g.resetBoundarySeq??-1)>=r.beforeRawSeq))throw new t(`Transcript read cursor has crossed the current-turn admission fence`);if(v)return reset(`session_reset`);let y=resolveVisibleDeltaRange(e,g,{beforeRawSeq:r?.beforeRawSeq,readLatestReset});return`reason`in y?reset(y.reason):readVisibleDeltaPage(e,g,y,{endExclusive:r?.beforeActiveMessagePosition??e.state.activeMessageCount,maxBytes:a,maxMessages:i})},r)}function resolveVisibleDeltaRange(e,t,r){let i=-1;if(t.lastEventSeq>=0){let r=n(e.database.db,_(e.database).selectFrom(`session_transcript_active_events`).select([`active_position`,`message_position`]).where(`session_id`,`=`,e.resolved.sessionId).where(`event_seq`,`=`,t.lastEventSeq).where(`message_position`,`is not`,null));if(r?.message_position==null)return{reason:`anchor_missing`};if(r.message_position!==t.lastMessagePosition)return{reason:`anchor_moved`};i=r.active_position}let a=t.lastMessagePosition+1;if(t.resetBoundarySeq===void 0)return{kept:[],start:a};let o=r.readLatestReset();if(!o||i>o.active_position)return{kept:[],start:a};let s=x(e,`reset`,r.beforeRawSeq);return s?{kept:s.keptMessagePositions.filter(e=>e>=a),start:Math.max(a,s.postBoundaryMessagePosition)}:{kept:[],start:a}}function readVisibleDeltaPage(e,t,n,r){let i=readVisibleDeltaMetadata(e,n,r),a=0,o=0;for(let e of i){if(o>=r.maxMessages||a+e.serialized_bytes>r.maxBytes)break;a+=e.serialized_bytes,o+=1}let s=i.slice(0,o),c=s.at(-1),l=c?.event_seq??t.lastEventSeq,d=c?.message_position??t.lastMessagePosition,f=s.filter(e=>e.message_position<n.start),p=o===0?[]:readVisibleDeltaPayload(e,f.length===0?{start:n.start,endExclusive:d+1}:{positions:s.map(e=>e.message_position)}),m=o===0&&i[0]?i[0].serialized_bytes:void 0;return{kind:`page`,cursor:u({...t,lastEventSeq:l,lastMessagePosition:d}),events:p,hasMore:o<i.length,...m===void 0?{}:{requiredBytes:m},serializedBytes:a}}function readVisibleDeltaMetadata(e,t,n){let o=n.maxMessages+1,s=t.kept.slice(0,o),c=m(e.database,e.resolved.sessionId,{start:s[0]??t.start,endExclusive:n.endExclusive}),l=s.length===0?c:c.where(n=>n.or([n(`active.message_position`,`>=`,t.start),n(`active.message_position`,`in`,r`(SELECT CAST(value AS BIGINT) FROM ${a(e.database.db,r.val(JSON.stringify(s)))})`)]));return i(e.database.db,v(l).limit(o)).rows}function readVisibleDeltaPayload(e,t){return i(e.database.db,p(m(e.database,e.resolved.sessionId,t)).leftJoin(`session_transcript_active_events as parent_active`,e=>e.onRef(`parent_active.session_id`,`=`,`active.session_id`).on(e=>e(`parent_active.active_position`,`=`,e(`active.active_position`,`-`,1)))).leftJoin(`transcript_event_identities as parent_identity`,e=>e.onRef(`parent_identity.session_id`,`=`,`parent_active.session_id`).onRef(`parent_identity.seq`,`=`,`parent_active.event_seq`)).select(`parent_identity.event_id as parent_id`)).rows.map(t=>{let{event:n,eventSeq:r,seq:i}=g(t,e);return{event:n,eventSeq:r,parentId:t.parent_id,seq:i}})}function readSessionTranscriptMessageEventPage(t,n){return y(t,t=>{let r=C(t).total,i=e(n.offset,0,{min:0,max:r}),a=e(n.maxMessages,0,{min:0}),o=n.offsetFrom===`start`?Math.min(r,i+a):r-i,s=n.offsetFrom===`start`?i:Math.max(0,o-a);return{activeLeafEntryId:t.state.leafEventId,events:S(t,s,o),totalMessages:r}},n)}function readSessionTranscriptBoundedMessageTailPage(e,t){return y(e,e=>w(e,t),t)}export{readLatestSessionTranscriptMessageEvent,readRecentSessionTranscriptActiveEvents,readSessionTranscriptBoundedMessageTailPage,readSessionTranscriptMessageEventPage,readSessionTranscriptVisibleMessageDeltaCore};