openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.84 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{r as createLazyRuntimeModule}from"./lazy-runtime-Ds6M7vp4.mjs";import{l as resolveAgentIdFromSessionKey}from"./session-key-BPgKfU2m.mjs";import{f as resolveAgentWorkspaceDir}from"./agent-scope-config-CG9CFpGX.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{n as normalizeMessageChannel}from"./message-channel-core-D_hPOadj.mjs";import{t as isDeliverableMessageChannel}from"./message-channel-normalize-hDAO6_JQ.mjs";import"./message-channel-514k57tV.mjs";import{d as normalizeMimeType,n as detectMime,t as FILE_TYPE_SNIFF_MAX_BYTES}from"./mime-ZmYZsEjx.mjs";import{t as extractDeliveryInfo}from"./delivery-info-BTs-RtuV.mjs";import{n as resolveWorkspaceRoot}from"./workspace-dir-wgle8m4u.mjs";import{a as resolvePathFromInput}from"./path-policy-mjUsNXiY.mjs";import*as fsPromises from"node:fs/promises";import{lstat}from"node:fs/promises";
//!src/plugins/host-hook-attachments.ts
const DEFAULT_ATTACHMENT_MAX_BYTES=26214400;const MAX_ATTACHMENT_FILES=10;const loadSendMessage=createLazyRuntimeModule(()=>import(`./message-fi1F9hTq.mjs`).then(e=>e.sendMessage));const loadGetChannelPlugin=createLazyRuntimeModule(()=>import(`./plugins-DkIK7GPL.mjs`).then(e=>e.getChannelPlugin));function escapeHtmlText(e){return e.replace(/&/g,`&`).replace(/</g,`<`).replace(/>/g,`>`)}function normalizeOptionalThreadId(e){if(typeof e===`number`&&Number.isFinite(e))return e;return normalizeOptionalString(e)}function resolveAttachmentDelivery(e){const t=e.captionFormat===`html`?`HTML`:void 0;const n=e.channel.trim().toLowerCase();const r=e.channelHints;const i=n===`telegram`?r?.telegram:void 0;const a=n===`slack`?r?.slack:void 0;const o=r?.parseMode??i?.parseMode??(n===`telegram`&&e.captionFormat===`plain`?`HTML`:t);const s=e.captionFormat===`plain`&&o===`HTML`;const c=r?.silent??i?.disableNotification;const l=normalizeMimeType(r?.forceDocumentMime??i?.forceDocumentMime);const u=normalizeOptionalThreadId(r?.threadId)??normalizeOptionalString(a?.threadTs);return{...o?{parseMode:o}:{},...s?{escapePlainHtmlCaption:true}:{},...c!==void 0?{silent:c}:{},...l?{forceDocumentMime:l}:{},...u!==void 0?{threadId:u}:{}}}async function sendPluginSessionAttachment(e){if(e.origin!==`bundled`)return{ok:false,error:`session attachments are restricted to bundled plugins`};const t=normalizeOptionalString(e.sessionKey);if(!t)return{ok:false,error:`sessionKey is required`};if(!Array.isArray(e.files)||e.files.length===0)return{ok:false,error:`at least one attachment file is required`};const n=typeof e.maxBytes===`number`&&Number.isFinite(e.maxBytes)?Math.min(DEFAULT_ATTACHMENT_MAX_BYTES,Math.max(1,Math.floor(e.maxBytes))):DEFAULT_ATTACHMENT_MAX_BYTES;const{deliveryContext:r,threadId:i}=await extractDeliveryInfo(t,{cfg:e.config});if(!r?.channel||!r.to)return{ok:false,error:`session has no active delivery route: ${t}`};const a=normalizeMessageChannel(r.channel);try{if((a&&isDeliverableMessageChannel(a)?(await loadGetChannelPlugin())(a):void 0)?.outbound?.deliveryMode===`gateway`)return{ok:false,error:`session attachments require direct outbound delivery for channel ${r.channel}; channel uses gateway delivery`}}catch(e){return{ok:false,error:`attachment delivery setup failed: ${formatErrorMessage(e)}`}}const o=normalizeOptionalString(e.text)??``;const s=resolveAttachmentDelivery({channel:r.channel,captionFormat:e.captionFormat,channelHints:e.channelHints});const c=e.files;const l=s.forceDocumentMime;const u=e.config;if(c.length>MAX_ATTACHMENT_FILES)return{ok:false,error:`at most ${MAX_ATTACHMENT_FILES} attachment files are allowed`};const d=[];let f=0;for(const e of c){if(!e||typeof e!==`object`||Array.isArray(e))return{ok:false,error:`attachment file entry must be an object`};const r=normalizeOptionalString(e.path);if(!r)return{ok:false,error:`attachment file path is required`};const i=t&&u?resolveAgentWorkspaceDir(u,resolveAgentIdFromSessionKey(t)):void 0;const a=resolvePathFromInput(r,resolveWorkspaceRoot(i));const o=await lstat(a).catch(()=>void 0);if(o?.isSymbolicLink())return{ok:false,error:`attachment file symlinks are not allowed: ${a}`};if(!o?.isFile())return{ok:false,error:`attachment file not found: ${a}`};if(o.size>n)return{ok:false,error:`attachment file exceeds ${n} bytes: ${a}`};if(l){const e=o.size;let t;let n;try{n=await fsPromises.open(a,`r`);const r=Math.min(Math.max(0,e),FILE_TYPE_SNIFF_MAX_BYTES);const i=Buffer.alloc(r);const{bytesRead:o}=await n.read(i,0,r,0);t=i.subarray(0,o)}catch(e){return{ok:false,error:`attachment file MIME read failed for ${a}: ${formatErrorMessage(e)}`}}finally{await n?.close().catch(()=>void 0)}let i;try{i=normalizeMimeType(await detectMime({buffer:t}))}catch(e){return{ok:false,error:`attachment file MIME detection failed for ${r}: `+formatErrorMessage(e)}}if(i!==l)return{ok:false,error:`attachment file MIME mismatch for ${a}: expected ${l}, got ${i??`unknown`}`}}f+=o.size;if(f>n)return{ok:false,error:`attachment files exceed ${n} bytes total`};d.push(a)}const p=s.threadId??normalizeOptionalThreadId(e.threadId)??normalizeOptionalThreadId(i)??normalizeOptionalThreadId(r.threadId);let m;try{m=await(await loadSendMessage())({to:r.to,content:s.escapePlainHtmlCaption?escapeHtmlText(o):o,channel:r.channel,accountId:r.accountId,threadId:p,requesterSessionKey:t,mediaUrls:d,forceDocument:s.forceDocumentMime?true:e.forceDocument,bestEffort:false,cfg:e.config,...s.parseMode?{parseMode:s.parseMode}:{},...s.silent!==void 0?{silent:s.silent}:{}})}catch(e){return{ok:false,error:`attachment delivery failed: ${formatErrorMessage(e)}`}}if(!m.result)return{ok:false,error:`attachment delivery failed: no delivery result returned`};return{ok:true,channel:m.channel,deliveredTo:r.to,count:d.length}}export{sendPluginSessionAttachment};