openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 7.51 kB
JavaScript
import{i as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{i as t}from"./transcript-global-singleton-DIJgobpU.mjs";import"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{n}from"./transcript-state-dir-DEo0wD2Q.mjs";import{t as r}from"./transcript-session-transcript-cold-error-B2vVZQFT.mjs";import{r as i}from"./transcript-session-key-MDDhuEG9.mjs";import{r as a}from"./transcript-session-sqlite-target-paths-C_ECqUX6.mjs";import{a as o,n as s}from"./transcript-paths-CuGliRzI.mjs";import"./transcript-session-accessor.sqlite-scope-helpers-LCM13IZK.mjs";import{D as c}from"./transcript-chat-display-projection.canvas-BVwGQyPV.mjs";import{n as l}from"./transcript-session-display-projection-D53CYaHn.mjs";import{n as u}from"./transcript-transcript-target-binding-5Rp9WohZ.mjs";import"./transcript-session-sqlite-target-BPLWhZV1.mjs";import{t as d}from"./transcript-session-transcript-read-fence-Fb_G9Rx1.mjs";import{t as f}from"./transcript-session-actor-storage-binding-BeZZBFjA.mjs";import{r as p}from"./transcript-runtime-snapshot-3kkiHEmb.mjs";import{i as m,r as h}from"./transcript-session-accessor.sqlite-active-events-CUtk_Mv6.mjs";import{r as g}from"./transcript-session-accessor.sqlite-exact-read-Dlxpy9zQ.mjs";import{t as _}from"./transcript-session-accessor.sqlite-transcript-watermark-DcQxmi_B.mjs";import{i as v}from"./transcript-session-transcript-entry-message-aTWZE4hU.mjs";import{t as y}from"./transcript-session-title-source-C3h9-lAQ.mjs";function prepareSessionTranscriptReadTargetCore(e,t){let n=e.sessionKey?.trim(),r=e.agentId??i(n);if(!r)throw Error(`Cannot resolve transcript scope without an agent id: ${n}`);let o={...e,agentId:r,sessionKey:n,storePath:a(e.storePath)},c=s(o)??t?.(o);if(!c)throw Error(`Transcript reads require a concrete session store path`);return{agentId:r,sessionKey:n,storePath:c,entryValidationScope:n&&e.sessionEntry?.sessionId!==e.sessionId?{agentId:r,...e.env?{env:e.env}:{},sessionKey:n,storePath:c}:void 0}}function resolveSessionTranscriptReadTargetCore(e,t){let{agentId:n,sessionKey:r,storePath:i,entryValidationScope:a}=prepareSessionTranscriptReadTargetCore(e,t),o=(a?g(a):void 0)?.normalizedKey??r;return{agentId:n,sessionId:e.sessionId,storePath:i,...o?{sessionKey:o}:{}}}function captureSessionActorTranscriptRead(e,t){let n=f(e,{assertCurrent:()=>t?.throwIfAborted(),authorize:()=>t?.throwIfAborted()});if(!n)return;let r=n.binding,i=r&&n.agentId===r.agentId&&n.path===r.path,a=i?r.actor.storage.readCurrent({type:`session.entry.readById`,input:{sessionId:e.sessionId}},r.authority):n.owner?.readSessionById(e.sessionId,n.authority),o=a?.sessionKey??e.sessionKey??``,s=i&&o===r.actor.target.sessionKey,c=u({agentId:n.agentId,sessionKey:o,sessionId:e.sessionId,storePath:i?r.path:n.path,env:e.env}),l=d(c),p=l&&structuredClone(l),m=n.owner?.captureSessionReadGuard(o),h={...n.authority,assertCurrent:()=>{t?.throwIfAborted(),m?.(),r?.actor.assertReadable(),n.authority.assertCurrent()}};return{target:c,missing:!a,currentEntry(){return h.assertCurrent(),i?r.actor.storage.readCurrent({type:`session.entry.read`,input:{sessionKey:o}},r.authority):n.owner?.readSession(o,n.authority)?.entry},assertCurrent:h.assertCurrent,async read(e,t){if(h.assertCurrent(),!a)throw Error(`Session transcript window is unavailable`);let l;try{if(l=s?r.actor:i?await r.actor.storage.acquire(o):await n.owner?.acquireExisting(o,{assertCurrent:h.assertCurrent,assertReadable:h.assertCurrent}),!l)throw Error(`Session transcript window is unavailable`);return await l.storage.read({type:e,input:{admission:p,...t,sessionId:c.sessionId}},h)}finally{l&&!s&&await l.release()}}}}t(Symbol.for(`openclaw.heartbeatEvents.state`),()=>({lastHeartbeat:null,notifications:{onEvent:()=>{}}})).notifications;const b=t(Symbol.for(`openclaw.systemEventStores`),()=>({owners:/* @__PURE__ */ new Map}),()=>{b.resolve=void 0,b.prepare=void 0},`close-only`);function resolveSessionStorePathForScope(e,t){let n=s(e);if(n)return n;let r=e.agentId??i(e.sessionKey);return o((t??p()).session?.store,{agentId:r,env:e.env})}function toTranscriptReadScope(e){return{...e.agentId?{agentId:e.agentId}:{},sessionId:e.sessionId,...e.sessionKey?{sessionKey:e.sessionKey}:{},...e.storePath?{storePath:e.storePath}:{}}}const x={firstUserMessage:null,lastMessagePreview:null},S=new n(8192);function readSqliteTitleProbeRange(e,t,n,r){let i=Math.min(t,r),a=Math.min(Math.max(0,n),i);return a===i?[]:m(e,{maxMessages:i-a,offset:a,offsetFrom:`start`,readOnly:!0}).events}function findFirstTitleUserText(t,n,r){for(let i of t){let t=v(i),a=l(t);if(a?.role===`user`&&(n||!c(e(t)))){let e=y(a.text,r);if(e)return e}}return null}function findLastMessageText(e){let t=null;return e.findLast(e=>{let n=v(e);return t=l(n,{flattenMarkdown:!0})?.text??null,t!==null}),t}function readSqliteTitleTailProbe(e,t,n){let r=h(e,{maxMessages:t,maxBytes:65536,offset:n,readOnly:!0}),i=findLastMessageText(r.newestContiguousEventCount?r.events.slice(-r.newestContiguousEventCount):[]);return i===null&&r.newestContiguousEventCount<r.scannedMessages&&(i=findLastMessageText(m(e,{maxMessages:t,offset:n,readOnly:!0}).events)),{text:i,totalMessages:r.totalMessages,generation:r.snapshot.generation??null,maxSeq:r.snapshot.indexedSeq,boundarySeq:r.snapshot.boundarySeq}}function copySessionTitleText(e){return e===null?null:Buffer.from(e,`utf16le`).toString(`utf16le`)}function readSessionTitleFieldsFromTranscript(e,t){let n=captureSessionActorTranscriptRead(e),i=n&&f(e,{assertCurrent:n.assertCurrent,authorize:n.assertCurrent});if(i){let n=e.sessionKey??i.owner?.readSessionById(e.sessionId,i.authority)?.sessionKey,r={type:`session.history.title`,input:{sessionId:e.sessionId,includeInterSession:t?.includeInterSession,skillCommands:t?.skillCommands}},a=i.binding;return(a&&a.actor.target.sessionKey===n?a.actor.storage.readCurrent(r,i.authority):n?i.owner?.readStorage(n,r,i.authority):void 0)?.fields??x}let a=n?.target??resolveSessionTranscriptReadTargetCore(e,resolveSessionStorePathForScope);try{let n={...toTranscriptReadScope(a),...e.env?{env:e.env}:{}},r=`${a.agentId??``}\0${a.sessionId}\0${a.storePath??``}`,i=_(n);if(i.maxSeq===null)return x;let o=t?.includeInterSession===!0?`includeInterSession`:`default`,s=t?.skillCommands?void 0:S.peek(r),c=s?.generation===i.generation?s:void 0,l=c?.maxSeq===i.maxSeq,u=c?.firstUserMessages[o];if(c&&l&&u&&(u.text!==null||u.scannedMessages>=Math.min(c.totalMessages,100)))return S.set(r,c),{firstUserMessage:u.text,lastMessagePreview:c.lastMessagePreview};let d=l?{text:c.lastMessagePreview,totalMessages:c.totalMessages,generation:c.generation,maxSeq:c.maxSeq,boundarySeq:c.boundarySeq}:readSqliteTitleTailProbe(n,20,0),f=d.text;!l&&!f&&d.totalMessages>20&&(f=readSqliteTitleTailProbe(n,80,20).text);let p=c?.generation===d.generation&&c.boundarySeq===d.boundarySeq?c.firstUserMessages:{},m=p[o],h=m?.text??null,g=m?.scannedMessages??0;for(let e of[20,100]){let r=Math.min(d.totalMessages,e);!h&&g<r&&(h=findFirstTitleUserText(readSqliteTitleProbeRange(n,d.totalMessages,g,r),t?.includeInterSession===!0,t?.skillCommands),g=r)}let v={firstUserMessage:copySessionTitleText(h),lastMessagePreview:copySessionTitleText(f)};return p[o]={text:v.firstUserMessage,scannedMessages:g},t?.skillCommands||S.set(r,{generation:d.generation,maxSeq:d.maxSeq,boundarySeq:d.boundarySeq,totalMessages:d.totalMessages,firstUserMessages:p,lastMessagePreview:v.lastMessagePreview}),v}catch(e){if(!(e instanceof r))throw e;return x}}export{readSessionTitleFieldsFromTranscript};