UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

307 lines (306 loc) • 12.4 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { c as asFiniteNumberInRange, p as asPositiveSafeInteger } from "./number-coercion-D6u-4YD8.mjs"; import { t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs"; import { d as normalizeMimeType, i as getFileExtension, l as kindFromMime, u as mimeTypeFromFilePath } from "./mime-D2wVBzpa.mjs"; //#region src/media/media-facts.ts const RUNTIME_PROMPT_MEDIA_FACTS = Symbol.for("openclaw.runtimePromptMediaFacts"); /** Attaches facts to a runtime prompt message without changing serialized/model-visible bytes. */ function attachRuntimePromptMediaFacts(e, t, n) { const r = normalizeMediaFacts(t); if (n?.length) Object.defineProperty(r, "imageOrder", { value: [...n] }); Object.defineProperty(e, RUNTIME_PROMPT_MEDIA_FACTS, { configurable: true, enumerable: true, writable: true, value: r }); return e; } function readRuntimePromptMediaFacts(e) { const t = e[RUNTIME_PROMPT_MEDIA_FACTS]; return Array.isArray(t) ? t : void 0; } /** * Pair run-only skips by source and occurrence; recorded facts remain retryable. * Staged copies retain media:// URLs. URL-less remote restaging can miss a match * and retry this turn; a failed read stays visible through the existing notice. */ function applyRunHydrationSuppression(e, t) { const n = /* @__PURE__ */ new Map(); for (const e of t ?? []) { const t = e.url ?? e.path; if (t) { const r = n.get(t) ?? []; r.push(e.unresolvedThisTurn === true); n.set(t, r); } } return e.map((e) => { const t = e.url ?? e.path; return (t ? n.get(t)?.shift() : void 0) ? { ...e, hydrationSuppressed: true } : e; }); } /** Reads the canonical persisted media envelope without consulting legacy top-level fields. */ function readPersistedMediaFacts(e) { return readPersistedMediaFactInputs(e)?.map((e, t) => normalizeMediaFact(e, t)); } function readPersistedMediaFactInputs(e) { const t = asNonArrayRecord(asNonArrayRecord(e)["__openclaw"]); return Array.isArray(t.media) ? t.media : void 0; } const LEGACY_MEDIA_CONTEXT_KEYS = [ "MediaPath", "MediaPaths", "MediaUrl", "MediaUrls", "MediaType", "MediaTypes", "MediaDir", "MediaTranscribedIndexes", "MediaStaged", "MediaWorkspaceDir" ]; const PERSISTED_LEGACY_MEDIA_KEYS = [ "MediaPath", "MediaPaths", "MediaUrl", "MediaUrls", "MediaType", "MediaTypes", "MediaTranscribedIndexes", "MediaStaged", "MediaWorkspaceDir" ]; /** Returns whether a top-level retired carrier contains an attachment fact worth migrating. */ function hasMeaningfulRetiredMediaCarrier(e) { const t = e; const n = {}; if (Array.isArray(t.media)) n.media = t.media; for (const e of PERSISTED_LEGACY_MEDIA_KEYS) if (Object.hasOwn(t, e)) n[e] = t[e]; if (resolveMediaFacts(n).some(isMeaningfulMediaFact)) return true; const r = readPersistedMediaFacts(e) ?? []; if (r.length === 0) return false; return (Array.isArray(t.MediaTranscribedIndexes) ? t.MediaTranscribedIndexes : []).some((e) => Number.isInteger(e) && e >= 0 && e < r.length) || Boolean(normalizeOptionalString(t.MediaWorkspaceDir)) || t.MediaStaged === true; } const MEDIA_KINDS = [ "image", "audio", "video", "document", "sticker", "unknown" ]; function isMediaKind(e) { return MEDIA_KINDS.some((t) => t === e); } /** Canonicalizes persisted user-message media; ambiguous sparse legacy arrays are rejected. */ function canonicalizePersistedUserMessageMedia(e) { const t = e; const n = PERSISTED_LEGACY_MEDIA_KEYS.some((e) => Object.hasOwn(t, e)); const r = Object.hasOwn(t, "media"); const i = readPersistedMediaFactInputs(e); if (!n && !r && i === void 0) return { changed: false, message: e }; const a = Array.isArray(t.media) ? t.media : void 0; let o = { ...t, media: i ?? a }; const s = Array.isArray(o.MediaPaths) ? o.MediaPaths : []; const c = Array.isArray(o.MediaUrls) ? o.MediaUrls : []; const l = Array.isArray(o.MediaTypes) ? o.MediaTypes : []; const u = Math.max(s.length, c.length); if (l.length > 0 && l.length < u) { const e = normalizeMediaFacts(o.media); let t = false; for (let n = 0; n < u; n += 1) { const r = normalizeOptionalString(s[n]) ?? normalizeOptionalString(c[n]); const i = e[n]; if (r && (!(i?.path ?? i?.url) || !(i?.contentType ?? i?.kind))) { t = true; break; } } if (n && t) throw new Error("legacy media arrays have ambiguous sparse positional alignment"); if (!t) o = { ...o, MediaType: void 0, MediaTypes: [] }; } const d = resolveMediaFacts(o); const f = o.MediaStaged === true ? resolveStagedMediaFacts(o) : void 0; const p = Array.isArray(o.MediaTypes) ? o.MediaTypes : []; const m = Array.isArray(o.media) ? o.media : []; const h = []; for (const [e, t] of d.entries()) { const n = normalizeOptionalString(p[e] ?? (e === 0 ? o.MediaType : void 0)); const r = m[e]; const i = n && isMediaKind(n) && !normalizeOptionalString(r?.contentType) && r?.kind === void 0; const a = r?.kind ?? (i ? n : void 0); h.push({ ...t.path ? { path: t.path } : {}, ...t.url ? { url: t.url } : {}, ...t.contentType && !i ? { contentType: t.contentType } : {}, ...a ? { kind: a } : {}, ...t.fileName ? { fileName: t.fileName } : {}, ...t.origin ? { origin: t.origin } : {}, ...t.sizeBytes !== void 0 ? { sizeBytes: t.sizeBytes } : {}, ...t.durationMs ? { durationMs: t.durationMs } : {}, ...t.width ? { width: t.width } : {}, ...t.height ? { height: t.height } : {}, ...t.transcribed ? { transcribed: true } : {}, ...t.messageId ? { messageId: t.messageId } : {}, ...t.workspaceDir ? { workspaceDir: t.workspaceDir } : {}, ...t.staged || f?.[e]?.staged ? { staged: true } : {}, ...t.hydrationSuppressed ? { hydrationSuppressed: true } : {} }); } const g = { ...t }; delete g.media; for (const e of PERSISTED_LEGACY_MEDIA_KEYS) delete g[e]; const _ = { ...asNonArrayRecord(t["__openclaw"]) }; if (h.length > 0 || i !== void 0 || a !== void 0) _.media = h; if (Object.keys(_).length > 0) g["__openclaw"] = _; else delete g["__openclaw"]; return { changed: JSON.stringify(g) !== JSON.stringify(t), message: g }; } function stripLegacyMediaContextFields(e) { for (const t of LEGACY_MEDIA_CONTEXT_KEYS) delete e[t]; } /** Returns whether a declared MIME only describes otherwise unclassified binary bytes. */ function isGenericBinaryMediaContentType(e) { const t = normalizeMimeType(e); return t === "application/octet-stream" || t === "binary/octet-stream"; } /** Resolves attachment kind from authoritative facts before source or filename hints. */ function resolveMediaFactKind(e) { if (e.kind && e.kind !== "unknown") return e.kind; const t = normalizeMimeType(e.contentType); if (t && !isGenericBinaryMediaContentType(t)) { const e = kindFromMime(t); if (e) return e; return isMediaKind(t) ? t : void 0; } const n = normalizeOptionalString(e.path) ?? normalizeOptionalString(e.url); if (!n) return; for (const t of [ e.path, e.url, e.fileName, n ]) { const e = mimeTypeFromFilePath(t); if (e !== void 0) return e === "image/svg+xml" ? void 0 : kindFromMime(e); const n = getFileExtension(t); if (n === ".tif" || n === ".tiff") return "image"; } } /** Returns whether a fact can produce native image input. */ function isImageMediaFact(e) { const t = resolveMediaFactKind(e); return t === "image" || t === "sticker"; } /** Returns whether a fact can produce native video input. */ function isVideoMediaFact(e) { return resolveMediaFactKind(e) === "video"; } function normalizeMediaFact(e, t, n = {}) { const r = asNonArrayRecord(e); const i = normalizeOptionalString(r.workspaceDir) ?? n.workspaceDir; const a = normalizeOptionalString(r.contentType); const o = asPositiveSafeInteger(r.durationMs); const s = asPositiveSafeInteger(r.width); const c = asPositiveSafeInteger(r.height); return { path: normalizeOptionalString(r.path), url: normalizeOptionalString(r.url), contentType: a, kind: (typeof r.kind === "string" ? r.kind : void 0) ?? n.kind ?? (isGenericBinaryMediaContentType(a) ? void 0 : kindFromMime(a)), fileName: normalizeOptionalString(r.fileName), ...r.origin === "paste" || r.origin === "file" ? { origin: r.origin } : {}, sizeBytes: asFiniteNumberInRange(r.sizeBytes, { min: 0 }), ...o ? { durationMs: o } : {}, ...s ? { width: s } : {}, ...c ? { height: c } : {}, transcribed: r.transcribed === true || n.transcribed?.(r, t) === true, messageId: normalizeOptionalString(r.messageId) ?? n.messageId, ...i ? { workspaceDir: i } : {}, ...r.staged === true ? { staged: true } : {}, ...r.hydrationSuppressed === true ? { hydrationSuppressed: true } : {} }; } /** True when every path-bearing canonical fact has explicit staging proof. */ function hasStagedMediaFacts(e) { const t = normalizeMediaFacts(e).filter((e) => Boolean(e.path)); return t.length > 0 && t.every((e) => Boolean(e.workspaceDir) || e.staged === true); } function normalizeMediaFacts(e, t = {}) { return Array.isArray(e) ? e.map((e, n) => normalizeMediaFact(e, n, t)) : []; } function isMeaningfulMediaFact(e) { return Boolean(e.path?.trim() || e.url?.trim() || e.contentType || e.kind && e.kind !== "unknown"); } function resolveMediaFactsWithPrecedence(e, t) { const n = normalizeMediaFacts(e.media); const r = Array.isArray(e.MediaPaths) ? e.MediaPaths : []; const i = Array.isArray(e.MediaUrls) ? e.MediaUrls : []; const a = Array.isArray(e.MediaTypes) ? e.MediaTypes : []; const o = Math.max(n.length, r.length, i.length, a.length, e.MediaPath || e.MediaUrl || e.MediaType ? 1 : 0); const s = new Set(e.MediaTranscribedIndexes ?? []); const c = Boolean(normalizeOptionalString(e.MediaPath)) || r.some((e) => Boolean(normalizeOptionalString(e))); return Array.from({ length: o }, (o, l) => { const u = n[l]; const d = r[l] ?? (l === 0 ? e.MediaPath : void 0); const f = i[l] ?? (l === 0 ? e.MediaUrl : void 0); const p = normalizeOptionalString(a[l]) ?? (l === 0 ? e.MediaType : void 0); return normalizeMediaFact({ ...u, path: t ? normalizeOptionalString(d) ?? u?.path : u?.path ?? d, url: t ? normalizeOptionalString(f) ?? u?.url : u?.url ?? f, contentType: t ? p ?? u?.contentType : u?.contentType ?? p, transcribed: t ? u ? u.transcribed === true : s.has(l) : u?.transcribed === true || s.has(l), workspaceDir: u?.workspaceDir ?? normalizeOptionalString(e.MediaWorkspaceDir), staged: u?.staged === true || t && e.MediaStaged === true && (!c || Boolean(normalizeOptionalString(d))) }, l); }); } /** Normalizes canonical facts or, for compatibility callers, legacy parallel fields. */ function resolveMediaFacts(e) { return resolveMediaFactsWithPrecedence(e, false); } /** Adopts staged legacy paths positionally while retaining canonical fact metadata and count. */ function resolveStagedMediaFacts(e) { return resolveMediaFactsWithPrecedence(e, true); } function projectStrings(e, t, n) { const r = t ? e.filter((e) => Boolean(e)) : e.map((e) => e ?? ""); if (r.length === 0 || !n && !r.some(Boolean)) return; return r; } function projectMediaFacts(e, t = "channel") { const n = Array.isArray(e) ? e : []; const r = t !== "channel"; const mediaUrl = (e) => (t === "channel" ? e.url ?? e.path : e.path) ?? void 0; const mediaType = (e) => e.contentType ?? (t === "channel" ? e.kind : void 0) ?? void 0; const i = n.flatMap((e, t) => e.transcribed ? [t] : []); return { MediaPath: n[0]?.path ?? void 0, MediaUrl: n[0] ? mediaUrl(n[0]) : void 0, MediaType: n[0] ? mediaType(n[0]) : void 0, MediaPaths: projectStrings(n.map((e) => e.path), false, r), MediaUrls: projectStrings(n.map(mediaUrl), false, r), MediaTypes: projectStrings(n.map(mediaType), t === "compact", r), ...t !== "channel" ? {} : { MediaTranscribedIndexes: i.length > 0 ? i : void 0 } }; } //#endregion export { resolveStagedMediaFacts as _, hasMeaningfulRetiredMediaCarrier as a, isMeaningfulMediaFact as c, normalizeMediaFacts as d, projectMediaFacts as f, resolveMediaFacts as g, resolveMediaFactKind as h, canonicalizePersistedUserMessageMedia as i, isMediaKind as l, readRuntimePromptMediaFacts as m, applyRunHydrationSuppression as n, hasStagedMediaFacts as o, readPersistedMediaFacts as p, attachRuntimePromptMediaFacts as r, isImageMediaFact as s, PERSISTED_LEGACY_MEDIA_KEYS as t, isVideoMediaFact as u, stripLegacyMediaContextFields as v };