openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
101 lines (100 loc) • 3.59 kB
JavaScript
import { l as kindFromMime, u as mimeTypeFromFilePath } from "./mime-D2wVBzpa.mjs";
import { d as normalizeMediaFacts, f as projectMediaFacts } from "./media-facts-B-0Dpo5L.mjs";
import { t as probeMediaFilesWithinBudget } from "./media-probe-_SAoK4Iu.mjs";
import { t as resolveLocalMediaPath } from "./local-media-path-R9G3PyAB.mjs";
//#region src/channels/inbound-event/media.ts
const MAX_INBOUND_MEDIA_PROBES = 8;
const INBOUND_MEDIA_PROBE_CONCURRENCY = 2;
const INBOUND_MEDIA_PROBE_BUDGET_MS = 3e3;
function resolveMediaPlaceholderKind(e) {
if (e.kind && e.kind !== "unknown") return e.kind;
const t = kindFromMime(e.contentType) ?? kindFromMime(mimeTypeFromFilePath(e.url)) ?? kindFromMime(mimeTypeFromFilePath(e.path));
return t && t !== "unknown" ? t : "attachment";
}
const PLURAL_MEDIA_PLACEHOLDER_LABELS = {
image: "images",
video: "videos",
audio: "audio attachments",
document: "files",
sticker: "stickers",
attachment: "attachments"
};
/** Renders structured media facts for channel surfaces that can carry text only. */
function formatMediaPlaceholderText(e) {
if (e.length === 0) return "";
const t = e.map(resolveMediaPlaceholderKind);
const n = t[0] ?? "attachment";
const r = t.every((e) => e === n) ? n : t.includes("attachment") ? "attachment" : "document";
const i = `<media:${r}>`;
return e.length === 1 ? i : `${i} (${e.length} ${PLURAL_MEDIA_PLACEHOLDER_LABELS[r]})`;
}
/** Appends an unavailable-media notice to real caption text, or returns the notice alone. */
function formatInboundMediaUnavailableText(e) {
const t = e.body?.trim() ?? "";
const n = e.notice.trim();
if (!t) return n;
return `${t}\n\n${n}`;
}
/** Normalizes plugin-provided attachments into ordered runtime facts. */
function toInboundMediaFacts(e, t = {}) {
return normalizeMediaFacts(e, t);
}
function resolveProbeKind(e) {
const t = e.kind ?? kindFromMime(e.contentType) ?? kindFromMime(mimeTypeFromFilePath(e.path));
return t === "audio" || t === "video" ? t : void 0;
}
/** Adds best-effort audio/video metadata without probing URL-only media. */
async function toInboundMediaFactsWithMetadata(e, t = {}) {
const n = toInboundMediaFacts(e, t);
const r = [];
for (const [e, t] of n.entries()) {
const n = resolveProbeKind(t);
const i = t.path ? resolveLocalMediaPath(t.path) : void 0;
if (n && i) r.push({
fact: t,
index: e,
kind: n,
localPath: i
});
}
const i = await probeMediaFilesWithinBudget(r.map((e) => ({
filePath: e.localPath,
kind: e.kind
})), {
budgetMs: INBOUND_MEDIA_PROBE_BUDGET_MS,
concurrency: INBOUND_MEDIA_PROBE_CONCURRENCY,
maxProbes: MAX_INBOUND_MEDIA_PROBES
});
for (const [e, t] of r.entries()) n[t.index] = {
...t.fact,
...i[e]
};
return n;
}
/** Projects facts into history without transient turn-only fields. */
function toHistoryMediaEntries(e, t = {}) {
return toInboundMediaFacts(e, t).map((e) => {
const t = {
path: e.path,
url: e.url,
contentType: e.contentType,
kind: e.kind,
messageId: e.messageId
};
for (const n of [
"durationMs",
"width",
"height"
]) if (e[n]) t[n] = e[n];
return t;
});
}
/**
* Builds the legacy singular/plural environment projection.
* @deprecated Pass ordered facts as `media`; use `toInboundMediaFacts` to normalize inputs.
*/
function buildChannelInboundMediaPayload(e) {
return projectMediaFacts(e);
}
//#endregion
export { toInboundMediaFacts as a, toHistoryMediaEntries as i, formatInboundMediaUnavailableText as n, toInboundMediaFactsWithMetadata as o, formatMediaPlaceholderText as r, buildChannelInboundMediaPayload as t };