UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 9.61 kB
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{Cs as t,Ho as n,It as r,Qt as i,Rt as a,Tt as o,Xt as s,et as c,fo as l,jt as u,lo as d,po as f,tt as p,yt as m}from"./control-ui-connection-Bfj9beF_.js";import{BD as h,LD as g,PD as _,Xv as v,Yv as y,kx as b,ox as x,ux as S,wx as C}from"./control-ui-boot-shared-DFTSUQQ0.js";import{Ai as w,Bs as T,Fi as E,Ii as D,Li as O,Pi as k,Ys as ee,bt as A,ji as j,vt as M,yt as N}from"./control-ui-boot-chat-DewxEbIm.js";import{n as P}from"./deferred-ui-support-D-VedBtc.js";import{c as F,f as I,l as L,p as R}from"./chat-message-media-runtime-BIOBdIgm.js";import{n as z,t as B}from"./browser-image-mime-types-BwfT2Med.js";import{n as V,t as H}from"./chat-attachment-text-reader-DYgn--mC.js";import{a as U,i as W,n as G,r as K,t as q}from"./chat-message-attachment-source-BXwcK6AD.js";async function J(e,n,r){let{client:i,connectionEpoch:a,sessionKey:o,resourceBasePath:s}=e,c=()=>!r.aborted&&e.connected&&e.client===i&&e.connectionEpoch===a&&e.sessionKey===o;if(!i||!c())return null;if(!l(i.gatewayUrl))throw Error(`File downloads require a same-origin Gateway connection.`);let u=await i.request(`sessions.files.download`,n,{signal:r});if(!c())return null;let d=t(s??``)+u.url;if(!A(d,s))throw Error(`Invalid file download URL`);return{url:d,filename:u.filename}}function Y(){return(Y=e((()=>{n(),f(),N()})))()}async function te(e,t,n,r){let i=await ie(e,n.sessionKey,n.agentId,n.requestPath,n.messageEntryId),a=n.relativeTo;if(a&&i&&(i.result.root!==(a.root??void 0)||i.result.source?.workspaceId!==a.workspaceSource?.workspaceId||i.result.source?.root!==a.workspaceSource?.root||i.result.source?.fileRoot!==a.workspaceSource?.fileRoot))throw Error(`The document's workspace changed. Reopen the document before following its links.`);return i&&{result:i.result,content:ne(e,t,i,n,r)}}function ne(e,t,n,r,i){let{sessionKey:l,agentId:u,draftScope:d,draftContext:f,gatewayUrl:p}=r,m=l===t.sessionKey,_=r.messageEntryId?{messageEntryId:r.messageEntryId}:{},{result:v,actionPath:y}=n,b=v.file;if(!b)return null;let x=b.name||h(b.path||y),S=b.workspacePath||b.path||y,C=t=>J(e,{sessionKey:v.sessionKey,agentId:u,path:y,..._},t);if(b.previewKind===`image`)return b.contentEncoding!==`base64`||typeof b.content!=`string`||!b.mimeType||!B.has(b.mimeType)?null:{kind:`image`,title:x,src:`data:${b.mimeType};base64,${b.content}`,mimeType:b.mimeType,rawText:S,file:{path:S,absolutePath:j(v.root,S),root:v.root,download:C}};if(b.previewKind===`unsupported`){let e={path:S,absolutePath:j(v.root,S),root:v.root,download:C};return b.mimeType===`application/pdf`?{kind:`attachment`,attachmentKind:`document`,title:x,mimeType:b.mimeType,sizeBytes:b.size,sourceIdentity:JSON.stringify([p,v.sessionKey,_,y,v.root,v.source]),file:e}:{...oe(b,y,C),fileLinkSessionKey:v.sessionKey,file:e}}let T=w(b);if(T===null)return null;let E=typeof b.hash==`string`&&Z(T)&&a(e,`sessions.files.set`)===!0&&o(e.hello?.auth??null)?{hash:b.hash,save:async({content:n,expectedHash:r})=>{try{let a=(await e.sessions.setFile(v.sessionKey,y,n,{agentId:u,expectedHash:r,...v.source?{expectedSource:v.source}:{}}))?.file.hash;return typeof a==`string`&&m&&D(e,t)&&i(),typeof a==`string`?{ok:!0,hash:a}:{ok:!1,code:`error`,message:`Save failed.`}}catch(n){let r=n instanceof c&&n.details&&typeof n.details==`object`&&`type`in n.details?n.details:null;return r?.type===`session_file_source_changed`&&m&&D(e,t)&&O(e,t,!0),r?.type===`session_file_conflict`?{ok:!1,code:`conflict`}:{ok:!1,code:`error`,message:s(n)}}},fetchLatest:async()=>{let t=await e.sessions.getFile(v.sessionKey,y,{agentId:u,..._});if(t?.source?.workspaceId!==v.source?.workspaceId||t?.source?.root!==v.source?.root||t?.source?.fileRoot!==v.source?.fileRoot)return null;let n=t?.file;return!n||typeof n.content!=`string`||typeof n.hash!=`string`?null:{content:n.content,hash:n.hash,editable:Z(n.content)}}}:void 0,k=x.match(/\.([a-z0-9_-]+)$/i)?.[1]?.toLowerCase()??``;return{kind:`file`,path:S,absolutePath:j(v.root,S),name:x,content:T,download:C,...v.source?{workspaceSource:v.source}:{},sessionFileSource:{sessionKey:v.sessionKey,agentId:u,path:y,..._},draftKey:[p,d??``,v.sessionKey,v.root??``,S,...v.source?[v.source.workspaceId,v.source.root??``,v.source.fileRoot??``]:[]].join(`\0`),draftContext:{sessionKey:v.sessionKey,sessionTitle:(m?f?.sessionTitle:void 0)??g(v.sessionKey),paneLabel:f?.paneLabel},root:v.root??null,mimeType:b.mimeType,language:k===`yml`?`yaml`:k,line:r.line??null,rawText:T,...E?{edit:E}:{}}}async function re(e,t,n,r){let i={sessionKey:t.sessionKey,artifactId:n,...t.agentId?{agentId:t.agentId}:{}},a=async(n,r=!1)=>{let a=k(e);if(a.sessionKey!==i.sessionKey||a.agentId!==t.agentId)return null;let o=e.client,s=e.connectionEpoch,c=()=>!n.aborted&&e.connected&&e.client===o&&e.connectionEpoch===s&&D(e,a),l=K({sessionKey:i.sessionKey,artifactId:i.artifactId,connectionEpoch:s},n);try{let a=await M(e,i,n,{readBinary:r});if(!c()||!a)return null;let o=a.url,u=a.artifact.mimeType?.split(`;`,1)[0]?.trim().toLowerCase()??``;if(!a.blob&&o&&R(o)&&B.has(u)){let r=()=>{},l=()=>ee(r),f=d(n,`abort`,l,{once:!0});try{let n=await L(o,{sessionKey:i.sessionKey,agentId:t.agentId,connectionEpoch:s,resourceBasePath:e.resourceBasePath,onMediaChange:r,resolveArtifactDownload:async()=>({url:o,expiresAt:a.expiresAt})},a.artifact.id);if(n.type.split(`;`,1)[0]?.trim().toLowerCase()!==u)throw Error(`Artifact image returned an unexpected content type`);return c()?{...a,blob:n}:null}finally{f(),l()}}if(!a.blob&&o&&R(o)&&(r||(S(u,a.artifact.title)||u===`image/svg+xml`)&&a.artifact.sizeBytes!==void 0&&a.artifact.sizeBytes<=262144)){let t=await W(Q(a,o,!1),{resourceBasePath:e.resourceBasePath},n,r?1/0:H);return c()&&t?{...a,blob:t}:null}return!r&&o&&R(o)&&!u.startsWith(`image/`)&&q(l,Q(a,o,!0),a.expiresAt),a}finally{l.release()}},o=async e=>{let t=await a(e,!0);return t?.blob?t.blob:t?.encoding!==`base64`||t.data===void 0?null:new Blob([m(t.data)],{type:t.artifact.mimeType??`application/octet-stream`})},s=await a(r);return s?.artifact?{artifact:s.artifact,content:await se(s,o,e.resourceBasePath)}:null}async function ie(e,t,n,r,i){let a={agentId:n,...i?{messageEntryId:i}:{}},o=async n=>{try{let r=await e.sessions.getFile(t,n,a);return r?{result:r,actionPath:r.file?.workspacePath?j(r.root,r.file.workspacePath):n}:null}catch(e){let r=e instanceof c?e.details:null;if(r&&typeof r==`object`&&`type`in r&&r.type===`session_file_too_large`&&`size`in r&&typeof r.size==`number`){let e={root:`root`in r?r.root:void 0,source:`source`in r?r.source:void 0};return{result:{sessionKey:t,...b(e)?e:{},file:{path:n,name:h(n),size:r.size,kind:`read`,missing:!1,previewKind:`unsupported`}},actionPath:n}}throw e}};try{return await o(r)}catch(i){let a=i instanceof c?i.details:null;if(!a||typeof a!=`object`||!(`type`in a)||a.type!==`session_file_not_found`||!/^[^\\/]+$/u.test(r))throw i;let s=await e.sessions.listFiles(t,{agentId:n,path:``,search:r}),l=s?.browser;if(!s?.root||!l||l.truncated)throw i;let u=l.entries.filter(e=>e.kind===`file`&&e.name===r),[d]=u;if(u.length!==1||!d)throw i;return await o(j(s.root,d.path))}}function X(e){let t=e.replace(/\r/g,`\\r`).replace(/\n/g,`\\n`),n=t.startsWith(` `)&&t.endsWith(` `);return P(n&&!/^ +$/.test(t)?` ${t} `:t)}function ae(e){if(typeof e!=`number`)return null;let t=new Date(e);return Number.isNaN(t.getTime())?null:t.toISOString()}function oe(e,t,n){let r=e.workspacePath||e.path||t,i=ae(e.updatedAtMs),a=[`This file is not previewable inline.`,``,`- Path: ${X(r)}`,e.mimeType?`- Type: ${X(e.mimeType)}`:null,typeof e.size==`number`?`- Size: ${e.size.toLocaleString()} bytes`:null,i?`- Updated: ${i}`:null].filter(e=>e!==null).join(` `);return{kind:`markdown`,content:a,rawText:a,fileDownload:{path:r,name:e.name||h(r),download:n}}}function Z(e){return new Set(e.match(/\r\n?|\n/g)).size<=1}async function se(e,t,n){let{data:r,encoding:i,url:a,blob:o}=e,{title:s}=e.artifact,c=e.artifact.mimeType??``,l=c.split(`;`,1)[0]?.trim().toLowerCase()===`image/svg+xml`,u={path:``,artifactId:e.artifact.id,download:async e=>{let n=await t(e);return n?{blob:n,filename:s}:null}},d,f;if(o?l||S(c,s)?f=await o.text():c.startsWith(`image/`)&&(d=await v(o,`Artifact image could not be decoded`)):i===`base64`&&r!==void 0&&(l||S(c,s)?f=new TextDecoder().decode(m(r)):c.startsWith(`image/`)&&(d=`data:${c};base64,${r}`)),d)return{kind:`image`,title:s,src:d,mimeType:c,rawText:a??null,file:u};if(f!==void 0){let t=c===`application/json`?`json`:``;return{kind:`file`,path:``,artifactId:e.artifact.id,name:s,content:f,rawText:f,mimeType:l?`image/svg+xml`:c,language:t,download:u.download}}if(a&&R(a)&&!c.startsWith(`image/`)){let t=Q(e,a,!0);return{kind:`attachment`,attachmentKind:t.kind,title:s,mimeType:c,sizeBytes:e.artifact.sizeBytes,sourceIdentity:a,resolveSource:(e,n)=>U(t,{...n,onMediaChange:e}),file:u}}if(o||i===`base64`||a&&(A(a,n)||R(a)))return{kind:`attachment`,attachmentKind:`document`,title:s,mimeType:c,download:t,file:u};let p=a?`# ${s}\n\n[Open artifact](${a})`:`# ${s}\n\nArtifact download is not previewable in the sidebar.`;return{kind:`markdown`,content:p,rawText:p}}function Q(e,t,n){let r=e.artifact.mimeType?.split(`;`,1)[0]?.trim().toLowerCase()??``;return{kind:r.startsWith(`video/`)?`video`:r.startsWith(`audio/`)?`audio`:`document`,label:e.artifact.title,mimeType:r,sizeBytes:e.artifact.sizeBytes,url:t,...n?{artifactId:e.artifact.id}:{}}}function $(){return($=e((()=>{z(),N(),p(),Y(),u(),y(),i(),r(),x(),C(),_(),V(),I(),G(),F(),T(),E()})))()}$();export{J as downloadSessionFile,re as loadSessionWorkspaceArtifactContent,te as loadSessionWorkspaceFileContent};