UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

196 lines (195 loc) • 7.11 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { l as resolveAgentIdFromSessionKey } from "./session-key-CqAyZeVk.mjs"; import { f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs"; import { r as createLazyRuntimeModule } from "./lazy-runtime-zGjszCtt.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs"; import { t as isDeliverableMessageChannel } from "./message-channel-normalize-CFHSufeo.mjs"; import "./message-channel-DTYVLJT1.mjs"; import { d as normalizeMimeType, n as detectMime, t as FILE_TYPE_SNIFF_MAX_BYTES } from "./mime-D2wVBzpa.mjs"; import { t as extractDeliveryInfo } from "./delivery-info-DGIoojvH.mjs"; import { n as resolveWorkspaceRoot } from "./workspace-dir-VdDz3_sC.mjs"; import { a as resolvePathFromInput } from "./path-policy-CyYRyWre.mjs"; import * as fsPromises from "node:fs/promises"; import { lstat } from "node:fs/promises"; //#region src/plugins/host-hook-attachments.ts const DEFAULT_ATTACHMENT_MAX_BYTES = 26214400; const MAX_ATTACHMENT_FILES = 10; const loadSendMessage = createLazyRuntimeModule(() => import("./message-CWaiGqpG.mjs").then((e) => e.sendMessage)); const loadGetChannelPlugin = createLazyRuntimeModule(() => import("./plugins-DrH1L0wJ.mjs").then((e) => e.getChannelPlugin)); function escapeHtmlText(e) { return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"); } function normalizeOptionalThreadId(e) { if (typeof e === "number" && Number.isFinite(e)) return e; return normalizeOptionalString(e); } /** Resolves portable attachment delivery options while honoring shipped channel-specific hints. */ 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 } : {} }; } /** Sends a bundled-plugin session attachment through the session's active delivery route. */ 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 }; } //#endregion export { sendPluginSessionAttachment };