UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.38 kB
import{i as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{c as t}from"./transcript-number-coercion-D2B2TTt-.mjs";import{s as n}from"./transcript-url-protocol-BKapech5.mjs";function isOrdinal(e){return typeof e==`number`&&Number.isSafeInteger(e)&&e>=0}function isIdentifier(e,t){return typeof e==`string`&&e.length>0&&(e.length<=t||Array.from(e).length<=t)}function readTranscriptDisplayPosition(t){let n=e(t);if(!n||!isIdentifier(n.source,128)||!isOrdinal(n.rawSeq))return;if(n.activity===void 0)return{source:n.source,rawSeq:n.rawSeq,...`activity`in n?{activity:void 0}:{}};let r=e(n.activity);if(!(!r||r.afterRawSeq!==null&&(!isOrdinal(r.afterRawSeq)||r.afterRawSeq>=n.rawSeq)||!isIdentifier(r.scopeId,1024)||!isOrdinal(r.startOrder)))return{source:n.source,rawSeq:n.rawSeq,activity:{afterRawSeq:r.afterRawSeq,scopeId:r.scopeId,startOrder:r.startOrder}}}function composeTranscriptDisplay(t,n=e=>e){let r=[],i=0,a=[],o=[],s,c=!0,flush=()=>{if(a.length===0)return;let e=a,t=o,n=c&&t.length>0;if(a=[],o=[],s=void 0,c=!0,!n)return;let l=r.splice(i),u=t.toSorted((t,n)=>{let r=e[t].activity.afterRawSeq,i=e[n].activity.afterRawSeq;return r===i?e[t].rawSeq-e[n].rawSeq:r===null?-1:i===null?1:r-i}),d=0,emitGap=t=>{let n;for(;d<u.length;){let r=u[d],i=e[r],a=i.activity;if(t!==void 0&&a.afterRawSeq!==null&&a.afterRawSeq>=t)break;n??=/* @__PURE__ */ new Map;let o=n.get(a.scopeId);o||(o={firstSeq:i.rawSeq,rows:[]},n.set(a.scopeId,o)),o.firstSeq=Math.min(o.firstSeq,i.rawSeq),o.rows.push(r),d+=1}if(n)for(let t of[...n.values()].toSorted((e,t)=>e.firstSeq-t.firstSeq)){let n=t.rows.toSorted((t,n)=>e[t].activity.startOrder-e[n].activity.startOrder||e[t].rawSeq-e[n].rawSeq);for(let e of n)r.push(l[e])}};for(let t=0;t<e.length;t+=1){let n=e[t];n.activity||(emitGap(n.rawSeq),r.push(l[t]))}emitGap()};for(let l of t){let t=readTranscriptDisplayPosition(e(e(n(l))?.__openclaw)?.transcriptPosition);t?(a.at(-1)?.source!==t.source&&(flush(),i=r.length),t.activity?o.push(a.length):(c&&=s===void 0||s<=t.rawSeq,s=t.rawSeq),a.push(t),r.push(l)):(flush(),r.push(l),i=r.length)}return flush(),r.every((e,n)=>e===t[n])?t:r}const r=`artifact_transcript_image_`;function imageReference(n,r){let i=e(n.__openclaw),a=readTranscriptDisplayPosition(i?.transcriptPosition),o=i?.id,s=t(i?.seq);if(!(!a||typeof o!=`string`||!o||o.length>1024||s===void 0||s>=2**53-1))return{source:a.source,rawSeq:a.rawSeq,messageId:o,messageSeq:s,contentIndex:r}}function encodeReference(e){return r+Buffer.from(JSON.stringify(e)).toString(`base64url`)}function parseTranscriptImageArtifactId(t){if(!(!t.startsWith(r)||t.length>8192))try{let n=e(JSON.parse(Buffer.from(t.slice(26),`base64url`).toString(`utf8`)));if(!n||typeof n.source!=`string`||!n.source||n.source.length>128||typeof n.messageId!=`string`||!n.messageId||n.messageId.length>1024||typeof n.messageSeq!=`number`||!Number.isSafeInteger(n.messageSeq)||n.messageSeq<1||n.messageSeq>=2**53-1||typeof n.rawSeq!=`number`||!Number.isSafeInteger(n.rawSeq)||n.rawSeq<0||typeof n.contentIndex!=`number`||!Number.isSafeInteger(n.contentIndex)||n.contentIndex<0)return;let r={source:n.source,rawSeq:n.rawSeq,messageId:n.messageId,messageSeq:n.messageSeq,contentIndex:n.contentIndex};return encodeReference(r)===t?r:void 0}catch{return}}function displayContentField(e){return e.role===`assistant`&&Array.isArray(e.openclawDisplayContent)?n:`content`}function hasInlineImagePayload(t){let n=e(t.source);return t.type===`image`&&[t.data,n?.data].some(e=>typeof e==`string`&&e.length>0)}function projectTranscriptImageArtifacts(t){let n=e(t);if(!n)return t;let r=displayContentField(n),i=n[r];if(!Array.isArray(i))return t;let a;for(let t=0;t<i.length;t++){let r=e(i[t]);if(!r||!hasInlineImagePayload(r)||typeof r.url==`string`&&r.url.trim())continue;let o=imageReference(n,t);o&&(a??=i.slice(),a[t]={...r,artifactId:encodeReference(o)})}return a?{...n,[r]:a}:t}function resolveTranscriptImageArtifactBlock(t,n){let r=parseTranscriptImageArtifactId(n),i=e(t);if(!r||!i)return;let a=imageReference(i,r.contentIndex);if(!a||encodeReference(a)!==n)return;let o=i[displayContentField(i)],s=Array.isArray(o)?e(o[r.contentIndex]):void 0;return s&&hasInlineImagePayload(s)?s:void 0}export{readTranscriptDisplayPosition as a,composeTranscriptDisplay as i,projectTranscriptImageArtifacts as n,resolveTranscriptImageArtifactBlock as r,parseTranscriptImageArtifactId as t};