openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
193 lines (192 loc) • 8.13 kB
JavaScript
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { l as prepareAssistantTranscriptText, r as extractAssistantTranscriptSourceText } from "./chat-message-content-DNUdbbJ8.mjs";
import { r as INTER_SESSION_PROMPT_PREFIX_BASE } from "./input-provenance-DupvvURj.mjs";
import { r as MAX_IMAGE_BYTES } from "./constants-ChkaB-HW.mjs";
import { a as isHeartbeatAcknowledgementText, t as HEARTBEAT_PROMPT } from "./heartbeat-DHK32_6H.mjs";
import { P as isHeartbeatUserMessage } from "./chat-display-projection.canvas-XAAIxHM8.mjs";
import { t as ASSISTANT_DISPLAY_CONTENT_FIELD } from "./assistant-display-content-CBuMU4m5.mjs";
import { c as isMeaningfulMediaFact, p as readPersistedMediaFacts, s as isImageMediaFact } from "./media-facts-B-0Dpo5L.mjs";
import { r as parseInboundMediaUri } from "./inbound-media-uri-DpK4_BLW.mjs";
import { n as splitMediaOutput } from "./parse-output-BsYrumRW.mjs";
import { u as stripInternalMetadataForDisplay } from "./agent-run-terminal-reply-DXju8G9G.mjs";
import { o as readInlineWidgetReceipt } from "./transcript-visible-record-BN4BpGj7.mjs";
import { a as readNestedToolActivity } from "./nested-tool-activity-BiEuCyrz.mjs";
import { u as sanitizeAssistantVisibleTextWithProfile } from "./assistant-visible-text-fPqh5m9O.mjs";
import { t as stripUserEnvelopeForDisplay } from "./user-envelope-display-CY_79-Ey.mjs";
//#region src/gateway/control-ui-public-session-message.ts
/** Public widgets must have a visible result, including nested tool activity. */
function readPublicWidgetReceipt(e) {
return readNestedToolActivity(e) || publicSessionMessageEntry(e) ? readInlineWidgetReceipt(e) : void 0;
}
/** Publication excludes private runtime input, including its attachments and tool activity. */
function publicSessionMessageEntry(e) {
const t = asOptionalRecord(e);
if (!t || ![
"user",
"assistant",
"toolResult",
"tool"
].includes(String(t.role)) || t.display === false || t.customType !== void 0 || t.senderSession !== void 0) return;
if ((t.role === "user" || t.role === "assistant") && (t.toolCallId !== void 0 || t.tool_call_id !== void 0)) return;
const n = asOptionalRecord(t.provenance);
if (t.provenance !== void 0 && n?.kind !== "external_user") return;
const r = rawMessageText(t);
if (r?.includes("[Inter-session message]")) return;
const i = {
role: String(t.role),
content: r ?? ""
};
if (r && (isHeartbeatUserMessage(i, HEARTBEAT_PROMPT) || t.role === "assistant" && isHeartbeatAcknowledgementText(r))) return;
return t;
}
function rawMessageText(e) {
let t;
if (e.role === "assistant") t = extractAssistantTranscriptSourceText(e);
else if (typeof e.content === "string") t = e.content;
else if (Array.isArray(e.content)) t = e.content.flatMap((e) => {
const t = asOptionalRecord(e);
return (t?.type === "text" || t?.type === "input_text") && typeof t.text === "string" ? [t.text] : [];
}).join("\n\n");
else if (typeof e.text === "string") t = e.text;
return t;
}
function publicSessionMessageText(e) {
if (e.role !== "user" && e.role !== "assistant") return "";
let t = prepareAssistantTranscriptText(e, rawMessageText(e));
if (!t) return "";
t = e.role === "user" ? stripUserEnvelopeForDisplay(t) : stripInternalMetadataForDisplay(t);
if (e.role === "assistant") t = sanitizeAssistantVisibleTextWithProfile(t, "history", true);
const n = {
role: e.role,
content: t
};
if (isHeartbeatUserMessage(n, HEARTBEAT_PROMPT) || e.role === "assistant" && isHeartbeatAcknowledgementText(t)) return "";
return splitMediaOutput(t, { extractAudioDirectives: false }).text.trim();
}
//#endregion
//#region src/gateway/control-ui-public-session-attachments.ts
function getPublicSessionEntryId(e) {
const t = asOptionalRecord(asOptionalRecord(e)?.["__openclaw"])?.id;
return typeof t === "string" && t.length > 0 && t.length <= 1024 ? t : void 0;
}
function buildPublicSessionMediaBaseUrl(e, t) {
return `${e}/share/session/media?token=${encodeURIComponent(t)}`;
}
function buildPublicSessionWidgetUrl(e, t) {
return `${e}/share/session/media?widget=${encodeURIComponent(t)}`;
}
function buildPublicSessionMediaUrl(e, t, n) {
return `${e}&entry=${encodeURIComponent(t)}&attachment=${encodeURIComponent(n)}`;
}
function attachmentSource(e) {
const t = asOptionalRecord(e.source);
const n = asOptionalRecord(e.image_url);
const r = asOptionalRecord(e.attachment);
return {
source: [
e.url,
e.openUrl,
t?.url,
e.image_url,
n?.url,
r?.url
].find((e) => typeof e === "string" && e.length > 0),
data: [e.data, t?.data].find((e) => typeof e === "string" && e.length > 0)
};
}
function collectAttachments(e) {
const t = publicSessionMessageEntry(e);
if (!t) return [];
const n = [];
const r = t[ASSISTANT_DISPLAY_CONTENT_FIELD];
const i = Array.isArray(r) ? r : t.content;
for (const [e, r] of (Array.isArray(i) ? i : []).entries()) {
const i = asOptionalRecord(r);
if (!i) continue;
const a = asOptionalRecord(i.attachment);
const o = [
"image",
"image_url",
"input_image"
].includes(String(i.type));
if (!o && ![
"attachment",
"file",
"audio",
"video"
].includes(String(i.type))) continue;
if ((t.role === "toolResult" || t.role === "tool") && !o && a?.kind !== "image") continue;
const s = [
i.fileName,
a?.name,
a?.label,
i.alt
].find((e) => typeof e === "string" && e.length > 0);
n.push({
id: `content-${e}`,
name: s ?? (o ? "Image" : "Attachment"),
image: o || a?.kind === "image",
...attachmentSource(i)
});
}
for (const [e, r] of (readPersistedMediaFacts(t) ?? []).entries()) {
if (!isMeaningfulMediaFact(r)) continue;
const i = isImageMediaFact(r);
if ((t.role === "toolResult" || t.role === "tool") && !i) continue;
n.push({
id: `media-${e}`,
name: r.fileName || (i ? "Image" : "Attachment"),
image: i,
source: r.url ?? r.path,
path: r.path
});
}
return n;
}
/** Only descriptors leave the projection; media bytes are recovered after public authorization. */
function collectPublicSessionAttachments(e) {
return collectAttachments(e).map(({ id: e, name: t, image: n }) => ({
id: e,
name: t,
image: n
}));
}
async function readPublicSessionAttachment(e, t, n) {
const r = collectAttachments(e).find((e) => e.id === t);
if (!r?.image) return null;
let i = r.data;
if (r.source?.startsWith("data:")) {
const e = /^data:image\/[a-z0-9.+-]+;base64,/iu.exec(r.source);
i = e ? r.source.slice(e[0].length) : void 0;
}
if (i) {
if (i.length > Math.ceil(6291456 / 3) * 4 || !/^[A-Za-z0-9+/]*={0,2}$/u.test(i)) return null;
const e = Buffer.from(i, "base64");
return e.byteLength <= 6291456 ? e : null;
}
if (!r.source) return null;
try {
const { buildInboundMediaUriFromPath: t } = await import("./inbound-media-path-C6OKAUay.mjs");
const i = parseInboundMediaUri(t(r.path ?? r.source) ?? r.source);
if (i) {
const { readMediaBuffer: e } = await import("./store-78XB9EFZ.mjs");
return (await e(i.id, "inbound", MAX_IMAGE_BYTES)).buffer;
}
const { parseManagedOutgoingRoute: a } = await import("./managed-image-attachments-L8xX6OIX.mjs");
const o = a(r.source);
if (!o || o.sessionKey !== n.sessionKey) return null;
const { readManagedImageRecord: s } = await import("./managed-image-record-store-Ci7sxGOs.mjs");
const c = await s(o.attachmentId);
if (!c || c.sessionKey !== n.sessionKey || c.messageId !== getPublicSessionEntryId(e) || c.agentId !== void 0 && c.agentId !== n.agentId) return null;
const { resolveManagedImageOriginalPath: l } = await import("./managed-image-attachments.custody-CvTAKgNv.mjs");
const { readLocalFileSafely: u } = await import("./fs-safe-CZVDXVig.mjs");
return (await u({
filePath: l(c),
maxBytes: MAX_IMAGE_BYTES
})).buffer;
} catch {
return null;
}
}
//#endregion
export { getPublicSessionEntryId as a, publicSessionMessageText as c, collectPublicSessionAttachments as i, readPublicWidgetReceipt as l, buildPublicSessionMediaUrl as n, readPublicSessionAttachment as o, buildPublicSessionWidgetUrl as r, publicSessionMessageEntry as s, buildPublicSessionMediaBaseUrl as t };