openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.54 kB
JavaScript
import{c as e,o as t}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{o as n}from"./transcript-sqlite-transaction-CBQXI8WC.mjs";import{a as r}from"./transcript-openclaw-agent-db-identity-BPgUuROx.mjs";import{a as i}from"./transcript-openclaw-agent-db-readonly-scope-9KdRnbDH.mjs";import{t as a}from"./transcript-session-accessor.sqlite-scope-helpers-LCM13IZK.mjs";import{l as o}from"./transcript-session-accessor.sqlite-entry-read-CL38_Mxj.mjs";import{a as s,m as c}from"./transcript-transcript-payload-F5t-l1WZ.mjs";import{d as l,s as u,t as d}from"./transcript-transcript-tree-CEw6GxlK.mjs";import{n as f,r as p}from"./transcript-session-message-cut-content-CLvnZ8qy.mjs";import{t as m}from"./transcript-session-accessor.sqlite-transcript-watermark-read-D-6f5IoC.mjs";function readSessionBranchSummaries(n,r,i){let o=a(n.db),m=o.selectFrom(`transcript_events`).select([`seq`,s(n.db).as(`event_json`)]).where(`session_id`,`=`,r).orderBy(`seq`,`asc`),h=e(n.db,e=>o.selectFrom(`transcript_events`).select(c(n.db).as(`event_json`)).where(`session_id`,`=`,r).where(`seq`,`=`,e(e=>e)).limit(1)),readHeadline=e=>{let t=h(e).rows[0];if(!t)throw Error(`Branch headline row is missing from the transcript snapshot`);return f(JSON.parse(t.event_json))},g=i?.branches.find(e=>e.active);if(i?.appendSafe&&i.maxSeq!==null&&g){let e={...g},a,o=!0;for(let s of t(n.db,m.where(`seq`,`>`,i.maxSeq).select(e=>e.selectFrom(`transcript_event_identities`).select(`event_id`).where(`session_id`,`=`,r).whereRef(`seq`,`=`,`transcript_events.seq`).limit(1).as(`identity_id`)))){let t=p(JSON.parse(s.event_json),s.seq);if(!d(t)||t.appendMode===`side`||t.id!==s.identity_id||typeof t.id!=`string`||t.id.trim()!==t.id||t.parentId!==e.leafEntryId){o=!1;break}t.headlineCandidate&&(a={seq:s.seq,previous:a}),e={leafEntryId:t.id,headline:e.headline,messageCount:e.messageCount+Number(t.type===`message`),...typeof t.timestamp==`string`?{updatedAt:t.timestamp}:{},active:!0}}if(o)return e.headline=resolveBranchHeadline(a,/* @__PURE__ */ new Map,readHeadline)||e.headline,{branches:[e,...i.branches.filter(e=>!e.active)],appendSafe:o}}let _=!0,v=0,y=0;function*navigationEntries(){let e=m.select(e=>e.selectFrom(`transcript_event_identities`).select(e=>e.fn.countAll().as(`count`)).where(`session_id`,`=`,r).as(`identity_count`));for(let r of t(n.db,e)){v++,y=r.identity_count;let e=p(JSON.parse(r.event_json),r.seq);_&&=typeof e?.id==`string`&&e.id.length>0&&e.id.trim()===e.id,yield e}}let b=u(navigationEntries());_&&=y===v&&!b.hasInvalidLeafControl&&b.nodes.length===b.byId.size&&b.nodes.every(e=>e.parentId===null||(b.byId.get(e.parentId)?.index??1/0)<e.index);let x=/* @__PURE__ */ new Map,S=/* @__PURE__ */ new Map,C=[];for(let e of l(b).toSorted((e,t)=>Number(t.id===b.leafId)-Number(e.id===b.leafId)||t.index-e.index)){let t=b.byId.get(e.id),n=summarizeBranchPath(b,t,x),r=t.entry?.timestamp;C.push({leafEntryId:t.id,headline:resolveBranchHeadline(n?.candidate,S,readHeadline),messageCount:n?.messageCount??0,...typeof r==`string`&&r.trim()?{updatedAt:r}:{},active:b.leafId===t.id})}return{branches:C,appendSafe:_}}function summarizeBranchPath(e,t,n){let r=[],i=/* @__PURE__ */ new Set,a=t;for(;!n.has(a.id);){if(i.has(a.id)){r.length=0;break}i.add(a.id),r.push(a);let t=a.parentId===null?void 0:e.byId.get(a.parentId);if(!t)break;a=t}let o=n.get(a.id);for(let e of r.toReversed()){let t=e.entry;o={messageCount:(o?.messageCount??0)+ +(t?.type===`message`),candidate:t?.headlineCandidate?{seq:t.seq,previous:o?.candidate}:o?.candidate},n.set(e.id,o)}return o}function resolveBranchHeadline(e,t,n){let r=[],i,a=e;for(;a;){if(t.has(a)){i=t.get(a);break}if(r.push(a),i=n(a.seq),i!==void 0)break;a=a.previous}for(let e of r)t.set(e,i);return i??``}function readSessionBranchSnapshot(e,t){return n(e.db,()=>{let n=r(e).identity,i=o(e,t.sessionKey);if(!i?.sessionId)return{status:`missing-session`};let a=m(e,i.sessionId,{requireHot:!0}),s=t.previous,c=s?.source?.sessionId===i.sessionId&&s.source.databaseIdentity===n&&s.generation===a.generation&&s.maxSeq!==null&&a.maxSeq!==null&&s.maxSeq<=a.maxSeq?s:void 0,l=c?.maxSeq===a.maxSeq?c:readSessionBranchSummaries(e,i.sessionId,c);return{status:`ok`,source:{sessionId:i.sessionId,databaseIdentity:n},...a,appendSafe:l.appendSafe,branches:l.branches}},{operationLabel:`session branch summaries read`})}function readSessionBranchSummariesInWorker(e){let t=i(t=>readSessionBranchSnapshot(t,e),e.database);return t.found?t.value:{status:`missing-session`}}export{readSessionBranchSnapshot,readSessionBranchSummariesInWorker};