openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.76 kB
JavaScript
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{i as mergeSessionTranscriptTreePaths,l as scanSessionTranscriptTree,p as selectSessionTranscriptTreePathNodes}from"./transcript-tree-Dwc2Zlw1.mjs";import{g as stripInternalRuntimeContext,s as hasInternalRuntimeContext}from"./internal-runtime-context-DervRrPi.mjs";import{t as isLegacyCodexProviderId}from"./legacy-codex-provider-Bh8WIf2G.mjs";
//!src/config/sessions/legacy-transcript-repair.ts
const OPENAI_PROVIDER_ID=`openai`;const LEGACY_OPENAI_CODEX_RESPONSES_API=`openai-codex-responses`;const OPENAI_CHATGPT_RESPONSES_API=`openai-chatgpt-responses`;function getEntryId(e){return typeof e.id===`string`&&e.id.trim()?e.id:null}function getParentId(e){return typeof e.parentId===`string`&&e.parentId.trim()?e.parentId:null}function getMessage(e){return isRecord(e.message)?e.message:null}function withSelectedParent(e,t){return e.parentId===t?e:{...e,parentId:t}}function normalizeLegacyOpenAICodexTranscriptMetadata(e){let t=0;for(const n of e){const e=getMessage(n);if(!e)continue;let r=false;if(isLegacyCodexProviderId(e.provider)){e.provider=OPENAI_PROVIDER_ID;r=true}if(e.api===LEGACY_OPENAI_CODEX_RESPONSES_API){e.api=OPENAI_CHATGPT_RESPONSES_API;r=true}if(r)t+=1}return t}function textFromContent(e){if(typeof e===`string`)return e;if(!Array.isArray(e))return null;return e.map(e=>isRecord(e)&&typeof e.text===`string`?e.text:``).join(``)||null}function selectActivePath(e){const t=e.filter(e=>e.type!==`session`);const n=scanSessionTranscriptTree(t);if(!n.hasExplicitLeafUpdate){const e=/* @__PURE__ */ new Map;for(const n of t){const t=getEntryId(n);if(t)e.set(t,n)}const n=[];const r=/* @__PURE__ */ new Set;let i=t.at(-1);while(i){const t=getEntryId(i);if(!t||r.has(t))return null;r.add(t);n.unshift(i);const a=getParentId(i);i=a?e.get(a):void 0}return n.length>0?{entries:n}:null}if(!n.hasLeafUpdate)return null;const r=selectSessionTranscriptTreePathNodes(n,n.leafId);return{entries:mergeSessionTranscriptTreePaths([r]).map(e=>withSelectedParent(e.entry,e.selectedParentId))}}function hasBrokenPromptRewriteBranch(e,t){const n=new Set(t.map(getEntryId).filter(e=>Boolean(e)));const r=new Set(t.map(e=>transcriptRepairUserKey(e,false)).filter(e=>e!==void 0));return e.some(e=>!n.has(getEntryId(e)??``)&&r.has(transcriptRepairUserKey(e,true)??``))}function transcriptRepairUserKey(e,t){const n=getMessage(e);if(!getEntryId(e)||n?.role!==`user`)return;const r=textFromContent(n.content);if(r===null||t&&!hasInternalRuntimeContext(r))return;const i=(t?stripInternalRuntimeContext(r):r).trim();return i?`${getParentId(e)??``}\0${i}`:void 0}export{transcriptRepairUserKey as i,normalizeLegacyOpenAICodexTranscriptMetadata as n,selectActivePath as r,hasBrokenPromptRewriteBranch as t};