openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.9 kB
JavaScript
import{a as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{l as t}from"./transcript-string-coerce-DTk-8HSr.mjs";function isCanonicalSessionEntryType(e){switch(e){case`message`:case`thinking_level_change`:case`model_change`:case`compaction`:case`reset`:case`branch_summary`:case`custom`:case`custom_message`:case`label`:case`session_info`:return!0;default:return!1}}function isCanonicalSessionTranscriptEntry(t){return e(t)&&isCanonicalSessionEntryType(t.type)}function isSessionTranscriptSideAppendEntry(e){return isCanonicalSessionTranscriptEntry(e)&&e.appendMode===`side`}function isSessionTranscriptLeafControl(t){return e(t)&&t.type===`leaf`&&parseSessionTranscriptTreeEntry(t)!==void 0}function parseSessionTranscriptTreeEntry(n){if(!e(n)||n.type===`session`||!Object.hasOwn(n,`parentId`))return;let r=t(n.id),i=n.parentId===null?null:t(n.parentId)??void 0;if(r&&i!==void 0){if(n.type===`leaf`){let e=n.targetId===null?null:t(n.targetId)??void 0,i=n.appendParentId===void 0?e:n.appendParentId===null?null:t(n.appendParentId)??void 0,a=n.appendMode===void 0?void 0:n.appendMode===`side`?`side`:null;return e===void 0||i===void 0||a===null?void 0:{id:r,parentId:e,leafId:e,appendParentId:i,...a?{appendMode:a}:{}}}return{id:r,parentId:i,leafId:isCanonicalSessionTranscriptEntry(n)&&n.appendMode!==`side`?r:void 0,appendParentId:r,...n.appendMode===`side`?{appendMode:n.appendMode}:{}}}}function parseParentlessCanonicalEntry(e,n){if(!isCanonicalSessionTranscriptEntry(e)||Object.hasOwn(e,`parentId`))return;let r=t(e.id);return r?{id:r,parentId:n,leafId:e.appendMode===`side`?void 0:r,appendParentId:r,...e.appendMode===`side`?{appendMode:e.appendMode}:{}}:void 0}function resolveCanonicalParentId(e,t){let n,r=e;for(;r!==null;){if(n?.has(r))return r;let e=t.get(r);if(!e||!isSessionTranscriptLeafControl(e.entry))return r;(n??=/* @__PURE__ */ new Set).add(r),r=e.parentId}return null}function scanSessionTranscriptTree(e){let t=[],n=/* @__PURE__ */ new Map;return{nodes:t,byId:n,...scanSessionTranscriptNavigation(e,{byId:n,addNode:e=>t.push(e),resetDescendantIds:/* @__PURE__ */ new Set,invalidLeafControlIds:/* @__PURE__ */ new Set})}}function resolveSessionTranscriptActiveLeafEntryId(e){return scanSessionTranscriptTree(e).leafId??void 0}function scanSessionTranscriptNavigation(t,n){let{byId:r,resetDescendantIds:i,invalidLeafControlIds:a}=n,o=null,s=null,c=!1,l=!1,u=!1,d=!1,f,p=0;for(let m of t){let t=p++,h=parseSessionTranscriptTreeEntry(m),g=h!==void 0&&e(m)&&m.type===`leaf`;f&&o!==null&&h?.leafId!==void 0&&g&&(h.leafId===null||!i.has(h.leafId))&&(h={...h,parentId:o,leafId:o,appendParentId:o});let isKnownLeafReference=e=>e===null||r.has(e)&&!a.has(e);if(h?.leafId!==void 0&&g&&(!isKnownLeafReference(h.leafId)||!isKnownLeafReference(h.appendParentId))&&h){d=!0,a.add(h.id);let e=m.parentId,i={...h,parentId:e,leafId:void 0,appendParentId:s,entry:m,index:t};n.addNode(i),r.set(i.id,i);continue}let _=h??parseParentlessCanonicalEntry(m,o);if(_&&isCanonicalSessionTranscriptEntry(m)){let e=h&&_.parentId!==null&&!r.has(_.parentId)&&o!==null,t=resolveCanonicalParentId(f!==void 0&&_.appendMode!==`side`&&(_.parentId===null||!i.has(_.parentId))||_.appendMode!==`side`&&e||h&&_.appendMode!==`side`&&_.parentId===s&&o!==s?o:_.parentId,r);t!==_.parentId&&(_={..._,parentId:t})}if(!_)continue;let v={..._,entry:m,index:t};n.addNode(v),r.set(v.id,v),e(m)&&m.type===`reset`?(f=v.id,i.clear(),i.add(v.id)):f!==void 0&&v.parentId!==null&&i.has(v.parentId)&&i.add(v.id),s=v.appendParentId,v.leafId!==void 0&&(o=v.leafId,l=!0,h&&(u=!0)),g&&(c=!0)}return{leafId:o,appendParentId:s,hasLeafControl:c,hasLeafUpdate:l,hasExplicitLeafUpdate:u,hasInvalidLeafControl:d}}function selectSessionTranscriptActiveEntries(e){let t=e.entries.map(e.recordOf),n=e.tree??scanSessionTranscriptTree(t);if(e.failClosedOnInvalidLeafControl===!0&&n.hasInvalidLeafControl)return[];if(!n.hasExplicitLeafUpdate)return[...e.entries];let r=selectSessionTranscriptTreePathNodes(n,n.leafId);return[...selectSessionTranscriptActiveEntryIndexes({tree:n,entryCount:e.entries.length,recordAt:e=>t[e],readPath:e=>e===n.leafId?r:selectSessionTranscriptTreePathNodes(n,e)})].flatMap(t=>{let n=e.entries[t];return n===void 0?[]:[n]})}function*selectSessionTranscriptActiveEntryIndexes(n){if(!n.tree.hasExplicitLeafUpdate){for(let e=0;e<n.entryCount;e+=1)yield e;return}let r=n.readPath(n.tree.leafId),i;for(let e of r){i=e;break}for(let r=(i?.index??0)-1;r>=0;--r){let i=n.recordAt(r);if(!(!e(i)||i.type!==`compaction`&&i.type!==`reset`)){if(i.type===`reset`){let e=t(i.id),r=t(i.firstKeptEntryId);if(e&&r){let t=!1;for(let i of n.readPath(e))t||=i.id===r,t&&(yield i.index);if(t)break}}yield r;break}}for(let e of n.readPath(n.tree.leafId))yield e.index}function selectSessionTranscriptTreeTipNodes(e){let t=new Set(e.nodes.flatMap(e=>isSessionTranscriptLeafControl(e.entry)||e.parentId===null?[]:[e.parentId]));return e.nodes.filter(n=>!isSessionTranscriptLeafControl(n.entry)&&(n.id===e.leafId||!t.has(n.id)))}function selectSessionTranscriptTreePathNodes(e,t){let n=[];return visitSessionTranscriptTreePathNodes(e.byId,t,/* @__PURE__ */ new Set,e=>{n.push(e)})?n.toReversed():[]}function visitSessionTranscriptTreePathNodes(e,t,n,r){let i=t;for(;i;){if(n.has(i))return!1;n.add(i);let t=e.get(i);if(!t)break;isSessionTranscriptLeafControl(t.entry)||r(t),i=t.parentId}return!0}function mergeSessionTranscriptTreePaths(e){let t=/* @__PURE__ */ new Map;for(let n of e){let e=null;for(let r of n)t.set(r.id,{...r,selectedParentId:e}),e=r.id}return[...t.values()].toSorted((e,t)=>e.index-t.index)}function mergeSessionTranscriptVisiblePathWithOpaqueAppendPath(e){let t=mergeSessionTranscriptTreePaths([e.visiblePath]),n=new Set(t.map(e=>e.id)),r=e.appendPath.length;for(;r>0;--r){let t=e.appendPath[r-1];if(!t||n.has(t.id)||isCanonicalSessionTranscriptEntry(t.entry))break}let i=t.at(-1)?.id??null;for(let a=r;a<e.appendPath.length;a+=1){let r=e.appendPath[a];t.push({...r,selectedParentId:i}),n.add(r.id),i=r.id}return{nodes:t,appendParentId:e.appendParentId===null?null:n.has(e.appendParentId)?e.appendParentId:t.at(-1)?.id??null}}function selectSessionTranscriptLeafControlledPath(n){let r=scanSessionTranscriptTree(n);if(r.hasLeafControl)return selectSessionTranscriptActiveEntries({entries:n,recordOf:e=>e,tree:r}).map(n=>{let i=e(n)?r.byId.get(t(n.id)??``):void 0;return!i||!e(n)||n.parentId===i.parentId?n:Object.assign({},n,{parentId:i.parentId})})}export{parseSessionTranscriptTreeEntry as a,selectSessionTranscriptActiveEntries as c,selectSessionTranscriptTreeTipNodes as d,mergeSessionTranscriptVisiblePathWithOpaqueAppendPath as i,selectSessionTranscriptLeafControlledPath as l,isSessionTranscriptLeafControl as n,resolveSessionTranscriptActiveLeafEntryId as o,isSessionTranscriptSideAppendEntry as r,scanSessionTranscriptTree as s,isCanonicalSessionTranscriptEntry as t,selectSessionTranscriptTreePathNodes as u};