UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 9.94 kB
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{$ as t,B as n,K as r,Pc as i,Wo as a,Z as o,ba as s,li as c,oi as l,qs as u,ta as d,v as f,y as p,ya as m}from"./_4____________________________.js";import{$b as h,AN as g,EN as ee,Ey as _,Lk as v,Pk as te,Ty as y,Zx as b,cx as x,dx as S,jk as C,pS as w,qx as T,vS as E}from"./_2_____________________________.js";import{Fr as D,Ur as O}from"./deferred-ui-support-DZEPWjAk.js";import{n as k,t as A}from"./artifact-download-url-BHTP5fwQ.js";import{n as ne,r as re}from"./artifact-download-WNJF_xpy.js";import{n as ie,t as ae}from"./markdown-runtime-CtlJb8xA.js";import{Fr as oe,Mr as j,Nr as M,Or as N,Pr as P,kr as F}from"./control-ui-boot-chat-CqvkfPXD.js";import{h as I,m as L,v as R,y as z}from"./chat-message-media-runtime-tT5AgKo0.js";import{n as B,t as V}from"./browser-image-mime-types-BwfT2Med.js";import{n as H,t as U}from"./chat-attachment-text-reader-nCp-5odC.js";import{n as W}from"./markdown-code-D3nSKiQb.js";import{a as G,i as K,n as se,r as ce,t as le}from"./chat-message-attachment-source-DGCKFWmq.js";async function q(e,t,n){let{client:r,connectionEpoch:a,sessionKey:o,resourceBasePath:s}=e,l=()=>!n.aborted&&e.connected&&e.client===r&&e.connectionEpoch===a&&e.sessionKey===o;if(!r||!l())return null;if(!m(r.gatewayUrl))throw Error(c(`chat.detailPanel.sameOrigin`));let u=await r.request(`sessions.files.download`,t,{signal:n});if(!l())return null;let d=i(s??``)+u.url;if(!k(d,s))throw Error(c(`chat.detailPanel.invalidUrl`));return{url:d,filename:u.filename}}function J(){return(J=e((()=>{u(),s(),l(),A()})))()}async function ue(e,t,n,r){let i=await fe(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:Y(e,t,i,n,r)}}function Y(e,r,i,a,o){let{sessionKey:s,agentId:c,draftScope:l,draftContext:u,gatewayUrl:d}=a,p=s===r.sessionKey,m=a.messageEntryId?{messageEntryId:a.messageEntryId}:{},{result:h,actionPath:g}=i,_=h.file;if(!_)return null;let y=_.name||v(_.path||g),b=_.workspacePath||_.path||g,x=t=>q(e,{sessionKey:h.sessionKey,agentId:c,path:g,...m},t);if(_.previewKind===`image`)return _.contentEncoding!==`base64`||typeof _.content!=`string`||!_.mimeType||!V.has(_.mimeType)?null:{kind:`image`,title:y,src:`data:${_.mimeType};base64,${_.content}`,mimeType:_.mimeType,rawText:b,file:{path:b,absolutePath:F(h.root,b),root:h.root,download:x}};if(_.previewKind===`unsupported`){let e={path:b,absolutePath:F(h.root,b),root:h.root,download:x};return _.mimeType===`application/pdf`?{kind:`attachment`,attachmentKind:`document`,title:y,mimeType:_.mimeType,sizeBytes:_.size,sourceIdentity:JSON.stringify([d,h.sessionKey,m,g,h.root,h.source]),file:e}:{...me(_,g,x),fileLinkSessionKey:h.sessionKey,file:e}}let S=N(_);if(S===null)return null;let C=typeof _.hash==`string`&&Z(S)&&t(e,`sessions.files.set`)===!0&&n(e.hello?.auth??null)?{hash:_.hash,save:async({content:t,expectedHash:n})=>{try{let i=(await e.sessions.setFile(h.sessionKey,g,t,{agentId:c,expectedHash:n,...h.source?{expectedSource:h.source}:{}}))?.file.hash;return typeof i==`string`&&p&&P(e,r)&&o(),typeof i==`string`?{ok:!0,hash:i}:{ok:!1,code:`error`,message:`Save failed.`}}catch(t){let n=t instanceof f&&t.details&&typeof t.details==`object`&&`type`in t.details?t.details:null;return n?.type===`session_file_source_changed`&&p&&P(e,r)&&oe(e,r,!0),n?.type===`session_file_conflict`?{ok:!1,code:`conflict`}:{ok:!1,code:`error`,message:ee(t)}}},fetchLatest:async()=>{let t=await e.sessions.getFile(h.sessionKey,g,{agentId:c,...m});if(t?.source?.workspaceId!==h.source?.workspaceId||t?.source?.root!==h.source?.root||t?.source?.fileRoot!==h.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,w=y.match(/\.([a-z0-9_-]+)$/i)?.[1]?.toLowerCase()??``;return{kind:`file`,path:b,absolutePath:F(h.root,b),name:y,content:S,download:x,...h.source?{workspaceSource:h.source}:{},sessionFileSource:{sessionKey:h.sessionKey,agentId:c,path:g,...m},draftKey:[d,l??``,h.sessionKey,h.root??``,b,...h.source?[h.source.workspaceId,h.source.root??``,h.source.fileRoot??``]:[]].join(`\0`),draftContext:{sessionKey:h.sessionKey,sessionTitle:(p?u?.sessionTitle:void 0)??te(h.sessionKey),paneLabel:u?.paneLabel},root:h.root??null,mimeType:_.mimeType,language:w===`yml`?`yaml`:w,line:a.line??null,rawText:S,...C?{edit:C}:{}}}async function de(e,t,n,r){let i={sessionKey:t.sessionKey,artifactId:n,...t.agentId?{agentId:t.agentId}:{}},o=async(n,r=!1)=>{let o=j(e);if(o.sessionKey!==i.sessionKey||o.agentId!==t.agentId)return null;let s=e.client,c=e.connectionEpoch,l=()=>!n.aborted&&e.connected&&e.client===s&&e.connectionEpoch===c&&P(e,o),u=ce({sessionKey:i.sessionKey,artifactId:i.artifactId,connectionEpoch:c},n);try{let o=await ne(e,i,n,{readBinary:r});if(!l()||!o)return null;let s=o.url,d=o.artifact.mimeType?.split(`;`,1)[0]?.trim().toLowerCase()??``;if(!o.blob&&s&&z(s)&&V.has(d)){let r=()=>{},u=()=>O(r),f=a(n,`abort`,u,{once:!0});try{let n=await I(s,{sessionKey:i.sessionKey,agentId:t.agentId,connectionEpoch:c,resourceBasePath:e.resourceBasePath,onMediaChange:r,resolveArtifactDownload:async()=>({url:s,expiresAt:o.expiresAt})},o.artifact.id);if(n.type.split(`;`,1)[0]?.trim().toLowerCase()!==d)throw Error(`Artifact image returned an unexpected content type`);return l()?{...o,blob:n}:null}finally{f(),u()}}if(!o.blob&&s&&z(s)&&(r||(b(d,o.artifact.title)||d===`image/svg+xml`)&&o.artifact.sizeBytes!==void 0&&o.artifact.sizeBytes<=262144)){let t=await K(Q(o,s,!1),{resourceBasePath:e.resourceBasePath},n,r?1/0:U);return l()&&t?{...o,blob:t}:null}return!r&&s&&z(s)&&!d.startsWith(`image/`)&&le(u,Q(o,s,!0),o.expiresAt),o}finally{u.release()}},s=async e=>{let t=await o(e,!0);return t?.blob?t.blob:t?.encoding!==`base64`||t.data===void 0?null:new Blob([d(t.data)],{type:t.artifact.mimeType??`application/octet-stream`})},c=await o(r);return c?.artifact?{artifact:c.artifact,content:await he(c,s,e.resourceBasePath)}:null}async function fe(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?F(r.root,r.file.workspacePath):n}:null}catch(e){let r=e instanceof f?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,...E(e)?e:{},file:{path:n,name:v(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 f?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}),c=s?.browser;if(!s?.root||!c||c.truncated)throw i;let l=c.entries.filter(e=>e.kind===`file`&&e.name===r),[u]=l;if(l.length!==1||!u)throw i;return await o(F(s.root,u.path))}}function X(e){let t=e.replace(/\r/g,`\\r`).replace(/\n/g,`\\n`);return W(t)}function pe(e){if(typeof e!=`number`)return null;let t=new Date(e);return Number.isNaN(t.getTime())?null:h(e)}function me(e,t,n){ie();let r=e.workspacePath||e.path||t,i=pe(e.updatedAtMs),a=[c(`chat.detailPanel.filePreviewUnavailable`),``,c(`chat.detailPanel.filePreviewPath`,{path:X(r)}),e.mimeType?c(`chat.detailPanel.filePreviewType`,{type:X(e.mimeType)}):null,typeof e.size==`number`?c(`chat.detailPanel.filePreviewSize`,{size:x({value:e.size,unit:`byte`,unitDisplay:`long`})}):null,i?c(`chat.detailPanel.filePreviewUpdated`,{date:i}):null].filter(e=>e!==null).join(` `);return{kind:`markdown`,content:a,rawText:a,fileDownload:{path:r,name:e.name||v(r),download:n}}}function Z(e){return new Set(e.match(/\r\n?|\n/g)).size<=1}async function he(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}},f,p;if(o?l||b(c,s)?p=await o.text():c.startsWith(`image/`)&&(f=await _(o,`Artifact image could not be decoded`)):i===`base64`&&r!==void 0&&(l||b(c,s)?p=new TextDecoder().decode(d(r)):c.startsWith(`image/`)&&(f=`data:${c};base64,${r}`)),f)return{kind:`image`,title:s,src:f,mimeType:c,rawText:a??null,file:u};if(p!==void 0){let t=c===`application/json`?`json`:``;return{kind:`file`,path:``,artifactId:e.artifact.id,name:s,content:p,rawText:p,mimeType:l?`image/svg+xml`:c,language:t,download:u.download}}if(a&&z(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)=>G(t,{...n,onMediaChange:e}),file:u}}if(o||i===`base64`||a&&(k(a,n)||z(a)))return{kind:`attachment`,attachmentKind:`document`,title:s,mimeType:c,download:t,file:u};let m=a?`# ${s}\n\n[Open artifact](${a})`:`# ${s}\n\nArtifact download is not previewable in the sidebar.`;return{kind:`markdown`,content:m,rawText:m}}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((()=>{B(),A(),re(),p(),J(),r(),l(),ae(),y(),g(),S(),o(),T(),w(),C(),H(),R(),se(),L(),D(),M()})))()}$();export{q as downloadSessionFile,de as loadSessionWorkspaceArtifactContent,ue as loadSessionWorkspaceFileContent};