UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

330 lines (329 loc) • 13.4 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { d as asPositiveFiniteNumber } from "./number-coercion-D6u-4YD8.mjs"; import { a as asOptionalRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { w as root } from "./fs-safe-BbWvmdVn.mjs"; import { n as estimateBase64DecodedBytes, t as canonicalizeBase64 } from "./base64-CnKpkbff.mjs"; import { t as basenameFromAnyPath } from "./file-name-PRG-HeBL.mjs"; import { r as extensionForMime } from "./mime-D2wVBzpa.mjs"; import { n as resolveSnakeCaseParamKey } from "./param-key-NQVXaOwt.mjs"; import { f as readStringArrayParam, m as readToolStringParam } from "./common-njQNtpnW.mjs"; import { n as normalizeMediaReferenceSource } from "./inbound-media-uri-DpK4_BLW.mjs"; import { f as resolveSandboxedMediaSource, t as assertMediaNotDataUrl } from "./sandbox-paths-CGD5HJYL.mjs"; import { t as createBoundedOutboundMediaReadFile } from "./bounded-read-file-CMMMfeGU.mjs"; import { i as resolveOutboundMediaLocalRoots, n as resolveOutboundMediaAccess, r as resolveOutboundMediaAccessForConfig, t as buildOutboundMediaLoadOptions } from "./load-options-DWp5hboI.mjs"; import { t as MEDIA_MAX_BYTES } from "./store-zsTAVy24.mjs"; import { t as classifyMediaReferenceSource } from "./media-reference-DDon-yG0.mjs"; import { n as loadWebMedia } from "./web-media-CX3Hvr6c.mjs"; import { i as resolveChannelMessageToolMediaSourceParamKeysAsync } from "./message-action-discovery-D58BOJ5F.mjs"; import { o as hasPotentialPluginActionParam } from "./message-action-normalization-BoubNx7z.mjs"; import { n as resolveChannelAccountMediaMaxMb } from "./configured-max-bytes-lKIaFMol.mjs"; import { t as resolveOutboundAttachmentFromBuffer } from "./outbound-attachment-DnDdBY4R.mjs"; import { t as readBooleanParam } from "./boolean-param-iT2wc5fb.mjs"; import { basenameFromMediaSource } from "@openclaw/fs-safe/advanced"; //#region src/infra/outbound/message-action-params.ts const BASE_ACTION_MEDIA_SOURCE_PARAM_KEYS = [ "media", "path", "filePath", "mediaUrl", "fileUrl", "image" ]; const STRUCTURED_ATTACHMENT_MEDIA_SOURCE_PARAM_KEYS = [ "media", "mediaUrl", "path", "filePath", "fileUrl", "url" ]; const STRUCTURED_ATTACHMENT_FILE_SOURCE_PARAM_KEYS = /* @__PURE__ */ new Set([ "path", "filePath", "fileUrl" ]); const SEND_BUFFER_DRY_RUN_MEDIA_URL = "buffer://message-send/attachment"; function readAttachmentContentType(e) { return readToolStringParam(e, "contentType") ?? readToolStringParam(e, "mimeType"); } function resolveMediaParamEntry(e, t) { const n = resolveSnakeCaseParamKey(e, t); if (!n) return; const r = readToolStringParam(e, t, { trim: false }); return r ? { key: n, value: r } : void 0; } function hasExplicitAttachmentPayload(e, t) { return Boolean(readToolStringParam(e, "buffer", { trim: false })) || buildActionMediaSourceParamKeys(t).some((t) => { const n = resolveMediaParamEntry(e, t); return Boolean(n && normalizeOptionalString(n.value)); }); } function hasExplicitSendMediaSource(e, t) { return buildActionMediaSourceParamKeys(t).some((t) => { const n = resolveMediaParamEntry(e, t); const r = n ? normalizeOptionalString(n.value) : void 0; return Boolean(r && r !== SEND_BUFFER_DRY_RUN_MEDIA_URL); }) || readStringArrayParam(e, "mediaUrls")?.some((e) => e !== SEND_BUFFER_DRY_RUN_MEDIA_URL) === true || collectAttachmentSources(e).length > 0; } function collectAttachmentSources(e) { const t = e.attachments; if (!Array.isArray(t)) return []; const n = []; for (const e of t) { if (!isRecord(e)) continue; for (const t of STRUCTURED_ATTACHMENT_MEDIA_SOURCE_PARAM_KEYS) { const r = resolveMediaParamEntry(e, t); if (!r || !normalizeOptionalString(r.value)) continue; n.push({ attachment: e, key: r.key, value: r.value, kind: STRUCTURED_ATTACHMENT_FILE_SOURCE_PARAM_KEYS.has(t) ? "file" : "media", contentType: readAttachmentContentType(e), filename: readToolStringParam(e, "filename") ?? readToolStringParam(e, "name") }); } } return n; } function selectStructuredAttachmentSources(e, t, n) { if (n !== "all" && hasExplicitAttachmentPayload(e, t)) return []; const r = collectAttachmentSources(e); return n === "all" ? r : r.slice(0, 1); } function buildActionMediaSourceParamKeys(e) { return [.../* @__PURE__ */ new Set([...BASE_ACTION_MEDIA_SOURCE_PARAM_KEYS, ...e ?? []])]; } async function resolveExtraActionMediaSourceParamKeysAsync(e) { if (!hasPotentialPluginActionParam(e.args)) return []; return resolveChannelMessageToolMediaSourceParamKeysAsync(e); } function collectActionMediaSourceHints(e, t, n) { const r = []; for (const n of buildActionMediaSourceParamKeys(t)) { const t = resolveMediaParamEntry(e, n); if (t && normalizeOptionalString(t.value)) r.push(t.value); } return r.concat(readStringArrayParam(e, "mediaUrls") ?? [], selectStructuredAttachmentSources(e, t, n?.structuredAttachments).map((e) => e.value)); } /** Local references need the checkout; remote and already-managed media keep their own lifetimes. */ async function waitForLocalMessageMedia(e) { const t = e.workspaceReadiness; if (!t || !e.sources.some((e) => { const t = classifyMediaReferenceSource(normalizeMediaReferenceSource(e.trim())); return !t.hasUnsupportedScheme && !t.isHttpUrl && !t.isDataUrl && !t.isMediaStoreUrl; })) return; e.assertCurrent?.(); t.assertCurrent(); await t.waitUntilReady(); e.abortSignal?.throwIfAborted(); e.assertCurrent?.(); t.assertCurrent(); } function resolveAttachmentMaxBytes(e) { const t = asPositiveFiniteNumber(resolveChannelAccountMediaMaxMb(e) ?? e.cfg.agents?.defaults?.mediaMaxMb); return t === void 0 ? void 0 : t * 1024 * 1024; } function inferAttachmentFilename(e) { const t = e.mediaHint?.trim(); if (t) { const e = basenameFromMediaSource(t); const n = e ? basenameFromAnyPath(e) : void 0; if (n) return n; } const n = e.contentType ? extensionForMime(e.contentType) : void 0; return n ? `attachment${n}` : "attachment"; } function normalizeBase64Payload(e) { const t = e.base64 ? /^data:([^;,\s]+)(;(?!base64)[^,;\s]+)*;base64,(.*)$/is.exec(e.base64.trim()) : null; if (!t) return e; const [, n, , r] = t; return { base64: r, contentType: e.contentType ?? n }; } function resolveAttachmentMediaPolicy(e) { const t = e.sandboxRoot?.trim(); if (t) return { mode: "sandbox", sandboxRoot: t, ...e.sandboxContainerWorkdir ? { containerWorkdir: e.sandboxContainerWorkdir } : {}, ...e.mediaReadFile ? { mediaReadFile: e.mediaReadFile } : {} }; const n = resolveOutboundMediaLocalRoots(e.mediaLocalRoots); return { mode: "host", mediaAccess: resolveOutboundMediaAccess({ mediaAccess: e.mediaAccess, mediaLocalRoots: n === "any" ? void 0 : n, mediaReadFile: e.mediaAccess?.readFile ? void 0 : e.mediaReadFile }), ...n !== void 0 ? { mediaLocalRoots: n } : {}, ...e.mediaAccess?.readFile ? {} : e.mediaReadFile ? { mediaReadFile: e.mediaReadFile } : {} }; } async function normalizeSandboxMediaParams(e) { const t = e.mediaPolicy.mode === "sandbox" ? { sandboxRoot: e.mediaPolicy.sandboxRoot.trim(), containerWorkdir: e.mediaPolicy.containerWorkdir } : void 0; const normalize = async (e, n) => { assertMediaNotDataUrl(n.value); if (t?.sandboxRoot) { const r = await resolveSandboxedMediaSource({ media: n.value, ...t }); if (r !== n.value) e[n.key] = r; } }; for (const t of buildActionMediaSourceParamKeys(e.extraParamKeys)) { const n = resolveMediaParamEntry(e.args, t); if (n) await normalize(e.args, n); } for (const t of selectStructuredAttachmentSources(e.args, e.extraParamKeys, e.structuredAttachments)) await normalize(t.attachment, t); } async function normalizeSandboxMediaSource(e) { const t = e.sandboxRoot?.trim(); const n = e.value.trim(); assertMediaNotDataUrl(n); return t ? await resolveSandboxedMediaSource({ media: n, sandboxRoot: t, containerWorkdir: e.sandboxContainerWorkdir }) : n; } async function hydrateAttachmentParamsForAction(e) { const t = e.action === "upload-file"; if (e.action === "send" || e.action === "thread-reply") { const { args: t, preserveSendBuffer: n } = e; if (hasExplicitSendMediaSource(t, e.extraParamKeys)) { const e = readAttachmentContentType(t); if (e) t.contentType = e; delete t.buffer; return; } const r = readToolStringParam(t, "buffer", { trim: false }); if (!r) return; const i = normalizeBase64Payload({ base64: r, contentType: readAttachmentContentType(t) }); if (!i.base64) return; const a = readToolStringParam(t, "filename") ?? inferAttachmentFilename({ contentType: i.contentType }); const o = resolveAttachmentMaxBytes(e) ?? 5242880; const s = estimateBase64DecodedBytes(i.base64); if (s > o) throw new Error(`Media too large: ${s} bytes (limit: ${o} bytes)`); const c = canonicalizeBase64(i.base64); if (!c) throw new Error("message.send buffer has invalid base64 data"); const l = e.dryRun || n ? { path: SEND_BUFFER_DRY_RUN_MEDIA_URL, contentType: i.contentType } : await resolveOutboundAttachmentFromBuffer(Buffer.from(c, "base64"), o, { contentType: i.contentType, filename: a, assertCommitAllowed: e.assertClientUploadAllowed }); t.media = l.path; t.mediaUrl = l.path; t.mediaUrls = [l.path]; if (!n) delete t.buffer; if (l.contentType && !readToolStringParam(t, "contentType")) t.contentType = l.contentType; if (!readToolStringParam(t, "filename")) t.filename = a; return; } if (e.action !== "sendAttachment" && e.action !== "setGroupIcon" && e.action !== "reply" && !t) return; const n = readBooleanParam(e.args, "forceDocument") ?? readBooleanParam(e.args, "asDocument") ?? false; const r = t && n ? false : void 0; const i = e.action === "sendAttachment" || t; const a = selectStructuredAttachmentSources(e.args, e.extraParamKeys)[0]; const o = readToolStringParam(e.args, "media", { trim: false }) ?? readToolStringParam(e.args, "mediaUrl", { trim: false }); const s = readToolStringParam(e.args, "path", { trim: false }) ?? readToolStringParam(e.args, "filePath", { trim: false }) ?? readToolStringParam(e.args, "fileUrl", { trim: false }); const c = readAttachmentContentType(e.args) ?? a?.contentType; if (a?.filename && !readToolStringParam(e.args, "filename")) e.args.filename = a.filename; if (i) { const t = readToolStringParam(e.args, "caption", { allowEmpty: true }); const n = readToolStringParam(e.args, "message", { allowEmpty: true }); if (!t && n) e.args.caption = n; } const l = o ?? (a?.kind === "media" ? a.value : void 0); const u = s ?? (a?.kind === "file" ? a.value : void 0); const d = readToolStringParam(e.args, "buffer", { trim: false }); const f = normalizeBase64Payload({ base64: d, contentType: c ?? void 0 }); if (f.base64 !== d && f.base64) e.args.buffer = f.base64; if (f.contentType && !readToolStringParam(e.args, "contentType")) e.args.contentType = f.contentType; const p = readToolStringParam(e.args, "filename"); const m = l || u; if (!e.dryRun && !d && m) { const t = resolveAttachmentMaxBytes(e); let n; if (e.mediaPolicy.mode === "sandbox") { const i = e.mediaPolicy.sandboxRoot.trim(); let a; const o = e.mediaPolicy.mediaReadFile ?? createBoundedOutboundMediaReadFile(async (e, t) => { a ??= root(i); return await (await a).readBytes(e, { maxBytes: t?.maxBytes }); }); n = { maxBytes: t, ...r !== void 0 ? { optimizeImages: r } : {}, sandboxValidated: true, readFile: o }; } else n = buildOutboundMediaLoadOptions({ maxBytes: t, mediaAccess: e.mediaPolicy.mediaAccess, mediaLocalRoots: e.mediaPolicy.mediaLocalRoots, mediaReadFile: e.mediaPolicy.mediaReadFile, optimizeImages: r }); const i = resolveOutboundMediaAccessForConfig(e.cfg)?.blockedHostnames; const a = await loadWebMedia(m, { ...n, ...i ? { ssrfPolicy: { blockedHostnames: [...i] } } : {} }); e.args.buffer = a.buffer.toString("base64"); if (!c && a.contentType) e.args.contentType = a.contentType; if (!p) e.args.filename = inferAttachmentFilename({ mediaHint: a.fileName ?? m, contentType: a.contentType ?? c ?? void 0 }); } else if (!p) e.args.filename = inferAttachmentFilename({ mediaHint: m, contentType: f.contentType }); } function parseJsonMessageParam(e, t) { const n = e[t]; if (typeof n !== "string") return; const r = n.trim(); if (!r) { delete e[t]; return; } try { e[t] = JSON.parse(r); } catch { throw new Error(`--${t} must be valid JSON`); } } /** Normalization drops a captionless table; reject it before any part is sent. */ function requirePresentationTableCaptions(e) { const t = asOptionalRecord(e.presentation)?.blocks; for (const e of Array.isArray(t) ? t : []) { const t = asOptionalRecord(e); if (t && normalizeOptionalLowercaseString(t.type) === "table") readToolStringParam(t, "caption", { required: true, label: "presentation.blocks[].caption" }); } } //#endregion export { normalizeSandboxMediaSource as a, resolveAttachmentMediaPolicy as c, normalizeSandboxMediaParams as i, resolveExtraActionMediaSourceParamKeysAsync as l, collectAttachmentSources as n, parseJsonMessageParam as o, hydrateAttachmentParamsForAction as r, requirePresentationTableCaptions as s, collectActionMediaSourceHints as t, waitForLocalMessageMedia as u };