openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
99 lines (98 loc) • 4 kB
JavaScript
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { t as basenameFromAnyPath } from "./file-name-PRG-HeBL.mjs";
import { o as isAudioFileName } from "./mime-D2wVBzpa.mjs";
import { d as normalizeMediaFacts } from "./media-facts-B-0Dpo5L.mjs";
import { t as getMediaDir } from "./store-path-D4uVfy73.mjs";
import "./store-zsTAVy24.mjs";
import path from "node:path";
//#region src/auto-reply/media-note.ts
function stripDarwinPrivatePrefix(e) {
return e.startsWith("/private/var/") ? e.slice(8) : e;
}
function normalizeManagedInboundMediaRef(e) {
if (!path.isAbsolute(e)) return e;
const t = stripDarwinPrivatePrefix(path.resolve(getMediaDir()));
const n = stripDarwinPrivatePrefix(path.resolve(e));
const r = path.join(t, "inbound");
const i = path.relative(r, n);
if (!i || i.startsWith("..") || path.isAbsolute(i)) return e;
return `media://inbound/${path.basename(n)}`;
}
function sanitizeInlineMediaNoteValue(e) {
const t = e?.trim();
if (!t) return "";
return normalizeManagedInboundMediaRef(t).replace(/[\p{Cc}\]]+/gu, " ").replace(/\s+/g, " ").trim();
}
function formatMediaAttachedLine(e, t, n) {
const r = n > 1 ? `[media attached ${t}/${n}: ` : "[media attached: ";
const i = sanitizeInlineMediaNoteValue(e.path);
const a = sanitizeInlineMediaNoteValue(e.contentType ?? e.kind);
const o = a ? ` (${a})` : "";
const s = sanitizeInlineMediaNoteValue(e.url);
const c = s && s !== i ? ` | ${s}` : "";
const l = truncateUtf16Safe(sanitizeInlineMediaNoteValue(basenameFromAnyPath(e.fileName ?? "")), 256);
return `${r}${i}${o}${c}${l ? ` ${JSON.stringify(l)}` : ""}]`;
}
const AUDIO_EXTENSIONS_WITHOUT_CANONICAL_MIME = [
".webm",
".wma",
".alac"
];
function isAudioPath(e) {
if (!e) return false;
if (isAudioFileName(e)) return true;
const t = normalizeLowercaseStringOrEmpty(e);
return AUDIO_EXTENSIONS_WITHOUT_CANONICAL_MIME.some((e) => t.endsWith(e));
}
function isValidAttachmentIndex(e, t) {
return Number.isSafeInteger(e) && e >= 0 && e < t;
}
function collectTranscribedAudioAttachmentIndices(e, t) {
const n = /* @__PURE__ */ new Set();
for (const r of e.MediaUnderstanding ?? []) if (r.kind === "audio.transcription" && isValidAttachmentIndex(r.attachmentIndex, t)) n.add(r.attachmentIndex);
for (const r of e.MediaUnderstandingDecisions ?? []) {
if (r.capability !== "audio" || r.outcome !== "success") continue;
for (const e of r.attachments) if (e.chosen?.outcome === "success" && isValidAttachmentIndex(e.attachmentIndex, t)) n.add(e.attachmentIndex);
}
return n;
}
/** Formats prompt-visible attachment text and retains facts that still need native hydration. */
function buildInboundMediaNoteProjection(e) {
const t = normalizeMediaFacts(e.media);
const n = t.flatMap((e, t) => e.path || e.url ? [{
fact: e,
index: t
}] : []);
if (n.length === 0) return {
media: [],
mediaIndexes: []
};
const r = collectTranscribedAudioAttachmentIndices(e, t.length);
const i = Boolean(e.Transcript?.trim()) && t.length === 1;
const a = n.filter((e) => {
const t = normalizeLowercaseStringOrEmpty(e.fact.contentType ?? e.fact.kind);
const n = t === "audio" || t.startsWith("audio/");
return !(e.fact.kind === "audio" || isAudioPath(e.fact.path) || n) || !(e.fact.transcribed === true || r.has(e.index) || i && e.index === 0);
});
if (a.length === 0) return {
media: [],
mediaIndexes: []
};
const o = new Set(e.MediaUnderstanding?.flatMap((e) => e.kind === "image.description" ? [e.attachmentIndex] : []) ?? []);
const s = a.map((e) => o.has(e.index) ? {
...e.fact,
hydrationSuppressed: true
} : e.fact);
const c = a.map((e) => e.index);
const l = a.length;
const u = a.map((e, t) => formatMediaAttachedLine(e.fact, t + 1, l));
if (l > 1) u.unshift(`[media attached: ${l} files]`);
return {
text: u.join("\n"),
media: s,
mediaIndexes: c
};
}
//#endregion
export { buildInboundMediaNoteProjection as t };