openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.63 kB
JavaScript
import{a as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{i as t,o as n,s as r}from"./transcript-session-entry-codec-C1BgT9Uk.mjs";function resolveSessionCanonicalParentId(e,t,n){let r,i=e;for(;i&&!t.has(i);){if(r?.has(i))return null;(r??=/* @__PURE__ */ new Set).add(i),i=n.get(i)??null}return i}function resolveOpaqueSessionFirstKeptEntryId(e){let{firstKeptEntryId:t,byId:n,opaqueParentsById:r}=e,i=resolveSessionCanonicalParentId(t,n,r);if(i!==null)return i;let a=/* @__PURE__ */ new Set,o=e.parentId,s;for(;o&&!a.has(o);){if(o===t){if(s)return s;break}a.add(o);let e=n.get(o);e?(s=e.id,o=e.parentId):o=r.get(o)??null}for(let n of e.entries()){let e=/* @__PURE__ */ new Set,i=n.parentId;for(;i&&r.has(i)&&!e.has(i);){if(i===t)return n.id;e.add(i),i=r.get(i)??null}}return e.fallbackParentId??void 0}function normalizeSessionContextEntryBoundaries(n,r){if(!n.some(t=>e(t)&&(t.type===`compaction`||t.type===`reset`)&&typeof t.firstKeptEntryId==`string`))return n.slice();let i=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;for(let e of r)t(e.entry)?i.set(e.id,{id:e.id,parentId:e.parentId}):a.set(e.id,e.parentId);return n.map(e=>{if(!t(e)||e.type!==`compaction`&&e.type!==`reset`||e.firstKeptEntryId===void 0||i.has(e.firstKeptEntryId)||!a.has(e.firstKeptEntryId))return e;let n=resolveSessionCanonicalParentId(e.parentId,i,a),r=resolveOpaqueSessionFirstKeptEntryId({firstKeptEntryId:e.firstKeptEntryId,parentId:n,fallbackParentId:n,byId:i,opaqueParentsById:a,entries:()=>i.values()});return r?{...e,firstKeptEntryId:r}:e})}var SessionEntryNavigation=class{constructor(){this.byId=/* @__PURE__ */ new Map,this.opaqueParentsById=/* @__PURE__ */ new Map,this.logicalParentsById=/* @__PURE__ */ new Map,this.invalidLeafControlIds=/* @__PURE__ */ new Set,this.labelsById=/* @__PURE__ */ new Map,this.labelTimestampsById=/* @__PURE__ */ new Map,this.leafId=null,this.appendParentId=null,this.resetDescendantIds=/* @__PURE__ */ new Set}clearNavigation(){this.byId.clear(),this.opaqueParentsById.clear(),this.logicalParentsById.clear(),this.invalidLeafControlIds.clear(),this.labelsById.clear(),this.labelTimestampsById.clear(),this.leafId=null,this.appendParentId=null,this.appendMode=void 0,this.latestResetId=void 0,this.resetDescendantIds.clear()}finishNavigation(){this.latestResetId=void 0,this.resetDescendantIds.clear()}recordResetDescendant(e,t){t!==null&&this.resetDescendantIds.has(t)&&this.resetDescendantIds.add(e)}resolveOpaqueLeafTargetId(e){return this.resolveCanonicalParentId(e)}resolveOpaqueAppendParentId(e){return e===null||this.byId.has(e)||this.opaqueParentsById.has(e)?e:this.resolveCanonicalParentId(e)}resolveOpaqueLeafControl(e){if(!e)return;let isKnownReference=e=>e===null||this.byId.has(e)||this.opaqueParentsById.has(e)&&!this.invalidLeafControlIds.has(e);if(!isKnownReference(e.targetId)||e.appendParentId!==void 0&&!isKnownReference(e.appendParentId))return;let t=this.resolveOpaqueLeafTargetId(e.targetId);return{leafId:t,appendParentId:e.appendParentId===void 0?t:this.resolveOpaqueAppendParentId(e.appendParentId),...e.appendMode?{appendMode:e.appendMode}:{}}}appendOpaqueNavigationRecord(e){let t=n(e);if(t){let e=this.resolveOpaqueLeafControl(t);if(!e){this.invalidLeafControlIds.add(t.id),this.opaqueParentsById.set(t.id,this.resolveOpaqueAppendParentId(t.parentId));return}let n=this.latestResetId!==void 0&&(e.leafId===null||!this.resetDescendantIds.has(e.leafId))?{leafId:this.leafId,appendParentId:this.leafId}:e;this.opaqueParentsById.set(t.id,n.leafId),this.recordResetDescendant(t.id,n.leafId),this.leafId=n.leafId,this.appendParentId=n.appendParentId,this.appendMode=n.appendMode;return}let i=r(e);i&&(this.opaqueParentsById.set(i.id,i.parentId),this.recordResetDescendant(i.id,i.parentId),this.appendParentId=i.id)}appendCanonicalNavigationEntry(e,t=Object.hasOwn(e,`parentId`)){if(e.type===`label`&&!this.byId.has(e.targetId)){this.opaqueParentsById.set(e.id,e.parentId);return}let n=e.appendMode===`side`;if((this.latestResetId!==void 0&&!n&&(e.parentId===null||!this.resetDescendantIds.has(e.parentId))||!t||!n&&e.parentId===this.appendParentId&&this.leafId!==this.appendParentId)&&this.logicalParentsById.set(e.id,this.leafId),this.byId.set(e.id,e),e.type===`reset`)this.latestResetId=e.id,this.resetDescendantIds.clear(),this.resetDescendantIds.add(e.id);else{let t=this.logicalParentsById.has(e.id)?this.logicalParentsById.get(e.id)??null:e.parentId;this.recordResetDescendant(e.id,t)}this.appendParentId=e.id,n?this.appendMode=`side`:(this.leafId=e.id,this.appendMode=void 0),e.type===`label`&&(e.label?(this.labelsById.set(e.targetId,e.label),this.labelTimestampsById.set(e.targetId,e.timestamp)):(this.labelsById.delete(e.targetId),this.labelTimestampsById.delete(e.targetId)))}resolveCanonicalParentId(e){return resolveSessionCanonicalParentId(e,this.byId,this.opaqueParentsById)}resolveEntryParentId(e){return this.logicalParentsById.has(e.id)?this.logicalParentsById.get(e.id)??null:this.resolveCanonicalParentId(e.parentId)}normalizeEntryParent(e){let t=this.resolveEntryParentId(e),n=t===e.id?null:t;return n===e.parentId?e:{...e,parentId:n}}getBranch(e){return readSessionEntryBranch(e??this.leafId,e=>{let t=this.byId.get(e),n=t?this.normalizeEntryParent(t):void 0;return{entry:n,parentId:n?n.parentId:this.opaqueParentsById.get(e)??null}})}};function readSessionEntryBranch(e,t){let n=[],r=/* @__PURE__ */ new Set,i=e;for(;i&&!r.has(i);){r.add(i);let{entry:e,parentId:a}=t(i);e&&n.push(e),i=a}return n.toReversed()}export{resolveSessionCanonicalParentId as i,normalizeSessionContextEntryBoundaries as n,readSessionEntryBranch as r,SessionEntryNavigation as t};