openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.86 kB
JavaScript
import{p as e}from"./transcript-number-coercion-D2B2TTt-.mjs";import{t}from"./transcript-session-transcript-read-fence-error-DfU0swK6.mjs";import{i as n,p as r,r as i}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{t as a}from"./transcript-json-values-C_3KtpDU.mjs";import{a as o,l as s,s as c,u as l}from"./transcript-session-accessor.sqlite-reset-window-dONVogQj.mjs";import{n as u}from"./transcript-session-transcript-read-fence-Fb_G9Rx1.mjs";import{a as d,c as f,d as p,o as m,s as h,u as g}from"./transcript-session-accessor.sqlite-visible-cursor-CP4ccuQd.mjs";import{c as _,l as v,n as y,r as b,t as x,u as S}from"./transcript-session-accessor.sqlite-projection-read-B2CcfDOt.mjs";import{t as C}from"./transcript-session-accessor.sqlite-active-projection-BdMyLOaK.mjs";import{n as w,r as T}from"./transcript-session-accessor.sqlite-active-events-read-CHG4jzRS.mjs";import"./transcript-session-accessor.sqlite-transcript-anchor-PzoHSaVL.mjs";function readLatestSessionTranscriptMessageEvent(e,t){return C(e,e=>{let t=u({database:e.database,...e.resolved}),n=y(e.database).latest({sessionId:e.resolved.sessionId,start:0,endExclusive:t?.beforeActiveMessagePosition??e.state.activeMessageCount});return n?b(n):void 0},t)}function readRecentSessionTranscriptActiveEvents(e,t,n){return C(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:s}=f(n);return C(e,e=>{let r=u({database:e.database,...e.resolved}),c=e.generation;if(!c)return{kind:`missing`};let l,readLatestReset=()=>(l===void 0&&(l=o(e,r?.beforeRawSeq)??null),l),f=n.cursor===void 0?void 0:g(n.cursor),freshCursor=()=>{let t=d({agentId:e.resolved.agentId,generation:c,sessionId:e.resolved.sessionId});return s!==`reset-window`&&f?.resetBoundarySeq===void 0?t:p(t,readLatestReset())},reset=e=>({kind:`reset`,cursor:m(freshCursor()),reason:e}),_=n.cursor===void 0?freshCursor():f;if(!_)return reset(`invalid_cursor`);if(_.agentId!==e.resolved.agentId||_.sessionId!==e.resolved.sessionId)return reset(`scope_mismatch`);let v=_.generation===c,y=_.resetBoundarySeq!==void 0&&h(_,readLatestReset(),v);if(!v)return reset(y?`session_reset`:`generation_mismatch`);if(r!==void 0&&(_.lastMessagePosition>=r.beforeActiveMessagePosition||(_.resetBoundarySeq??-1)>=r.beforeRawSeq))throw new t(`Transcript read cursor has crossed the current-turn admission fence`);if(y)return reset(`session_reset`);let b=resolveVisibleDeltaRange(e,_,{beforeRawSeq:r?.beforeRawSeq,readLatestReset});return`reason`in b?reset(b.reason):readVisibleDeltaPage(e,_,b,{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,x(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 c=s(e,`reset`,r.beforeRawSeq);return c?{kept:c.keptMessagePositions.filter(e=>e>=a),start:Math.max(a,c.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,u=c?.message_position??t.lastMessagePosition,d=s.filter(e=>e.message_position<n.start),f=o===0?[]:readVisibleDeltaPayload(e,d.length===0?{start:n.start,endExclusive:u+1}:{positions:s.map(e=>e.message_position)}),p=o===0&&i[0]?i[0].serialized_bytes:void 0;return{kind:`page`,cursor:m({...t,lastEventSeq:l,lastMessagePosition:u}),events:f,hasMore:o<i.length,...p===void 0?{}:{requiredBytes:p},serializedBytes:a}}function readVisibleDeltaMetadata(e,t,n){let o=n.maxMessages+1,s=t.kept.slice(0,o),c=S(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,_(l).limit(o)).rows}function readVisibleDeltaPayload(e,t){return i(e.database.db,v(S(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(e=>{let{event:t,eventSeq:n,seq:r}=b(e);return{event:t,eventSeq:n,parentId:e.parent_id,seq:r}})}function readSessionTranscriptMessageEventPage(t,n){return C(t,t=>{let r=l(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:c(t,s,o),totalMessages:r}},n)}function readSessionTranscriptBoundedMessageTailPage(e,t){return C(e,e=>w(e,t),t)}export{readSessionTranscriptVisibleMessageDeltaCore as a,readSessionTranscriptMessageEventPage as i,readRecentSessionTranscriptActiveEvents as n,readSessionTranscriptBoundedMessageTailPage as r,readLatestSessionTranscriptMessageEvent as t};