UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

189 lines (188 loc) • 8.24 kB
import { g as readStringValue, l as normalizeOptionalString, t as hasNonEmptyString } from "./string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord, i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs"; import { i as isSilentReplyText } from "./tokens-CcE6ESvH.mjs"; import { t as stripMixedSilentReplyTokens } from "./mixed-silent-reply-tokens-5vt94DeG.mjs"; //#region src/agents/delivery-evidence-values.ts function hasAnyNonEmptyString(e) { return Array.isArray(e) && e.some(hasNonEmptyString); } async function normalizeSentMediaUrlsForDelivery(e) { const t = /* @__PURE__ */ new Set(); for (const n of e.sentMediaUrls) { const r = n.trim(); if (!r) continue; t.add(r); if (!e.normalizeMediaPaths) continue; try { const n = await e.normalizeMediaPaths({ mediaUrl: r, mediaUrls: [r] }); for (const e of [n.mediaUrl, ...n.mediaUrls ?? []]) { const n = e?.trim(); if (n) t.add(n); } } catch {} } return [...t]; } //#endregion //#region src/sessions/user-turn-media.ts const MEDIA_ONLY_USER_TEXT = "[User sent media without caption]"; function hasPersistedMedia(e) { if (!e || typeof e !== "object" || Array.isArray(e)) return false; const t = asOptionalRecord(asOptionalRecord(e)?.["__openclaw"])?.media; return Array.isArray(t) && t.some((e) => { const t = asOptionalRecord(e); return Boolean(t && (normalizeOptionalString(t.path) || normalizeOptionalString(t.url) || normalizeOptionalString(t.contentType) || t.kind && t.kind !== "unknown")); }); } //#endregion //#region src/shared/chat-message-content.ts /** Returns inline string content or the first array text block without scanning later blocks. */ function extractFirstTextBlock(e) { const t = asOptionalObjectRecord(e)?.content; const n = readStringValue(t); if (n !== void 0) return n; return Array.isArray(t) ? readStringValue(asOptionalObjectRecord(t[0])?.text) : void 0; } const assistantTextSignatureCache = /* @__PURE__ */ new WeakMap(); function isAssistantTextContentBlockType(e) { return e === "text" || e === "input_text" || e === "output_text"; } /** Narrows unknown phase metadata to assistant text phases that affect visibility. */ function normalizeAssistantPhase(e) { return e === "commentary" || e === "final_answer" ? e : void 0; } /** Parses assistant text block signatures, preserving legacy raw ids when not JSON encoded. */ function parseAssistantTextSignature(e) { const t = e.textSignature; const n = assistantTextSignatureCache.get(e); if (n && n.text === t) return n.result; let r; if (typeof t !== "string" || t.trim().length === 0) r = null; else if (!t.startsWith("{")) r = { id: t }; else try { const e = JSON.parse(t); const n = normalizeAssistantPhase(e.phase); r = e.v === 1 ? { ...typeof e.id === "string" ? { id: e.id } : {}, ...n ? { phase: n } : {} } : null; } catch { r = null; } assistantTextSignatureCache.set(e, { text: t, result: r }); return r; } /** Resolves a message phase only when the top-level phase or all explicit blocks agree. */ function resolveAssistantMessagePhase(e) { const t = asOptionalObjectRecord(e); if (!t) return; const n = normalizeAssistantPhase(t.phase); if (n) return n; if (!Array.isArray(t.content)) return; let r; for (const e of t.content) { const t = asOptionalObjectRecord(e); if (!t || !isAssistantTextContentBlockType(t.type)) continue; const n = parseAssistantTextSignature(t)?.phase; if (n) { if (r && r !== n) return; r = n; } } return r; } /** Finds assistant phase metadata on event payloads that may wrap message-like records. */ function resolveAssistantEventPhase(e) { const t = asOptionalObjectRecord(e); if (!t) return; return normalizeAssistantPhase(t.phase) ?? resolveAssistantMessagePhase(t.message) ?? resolveAssistantMessagePhase(t.partial) ?? resolveAssistantMessagePhase(t.item) ?? resolveAssistantMessagePhase(t); } /** Selects original text blocks with the same explicit-phase precedence used for delivery. */ function readAssistantTextBlocksForPhase(e, t) { const n = asOptionalRecord(e); if (!Array.isArray(n?.content)) return []; const r = n.content.some((e) => { const t = asOptionalRecord(e); return Boolean(t && isAssistantTextContentBlockType(t.type) && parseAssistantTextSignature(t)?.phase); }); if (!t && r) return []; const i = r ? void 0 : normalizeAssistantPhase(n.phase); return n.content.filter((e) => { const n = asOptionalRecord(e); return Boolean(n && isAssistantTextContentBlockType(n.type) && typeof n.text === "string" && (parseAssistantTextSignature(n)?.phase ?? i) === t); }); } /** Extracts assistant text for a requested phase without mixing legacy and explicitly phased text. */ function extractAssistantTextForPhase(e, t) { const n = asOptionalObjectRecord(e); if (!n) return; const r = normalizeAssistantPhase(n.phase); const i = t?.phase; const a = typeof n.text === "string" ? n.text : n.content; if (typeof a === "string") { const e = r === i ? a : void 0; return e?.trim() ? e : void 0; } const o = []; for (const t of readAssistantTextBlocksForPhase(e, i)) if (t.text.trim()) o.push(t.text); return o.length ? o.join("\n") : void 0; } /** Returns user-visible assistant text, preferring final answers over legacy unphased text. */ function extractAssistantPhaseText(e) { return extractAssistantTextForPhase(e, { phase: "final_answer" }) ?? extractAssistantTextForPhase(e); } /** Captures authored display sources without making commentary a final reply. */ function extractAssistantTranscriptSourceText(e) { const t = extractAssistantTextForPhase(e, { phase: "commentary" }); const n = extractAssistantPhaseText(e); return t && n ? `${t}\n${n}` : t ?? n; } function readTerminalSourceReplyDeliveryMirror(e) { const t = asOptionalObjectRecord(asOptionalObjectRecord(e)?.openclawDeliveryMirror); if (!t) return; const n = typeof t.sourceTurnId === "string" ? t.sourceTurnId.trim() : ""; if (t.kind !== "message-tool-source-reply" || t.final !== true || !n) return; const r = typeof t.toolCallId === "string" ? t.toolCallId.trim() : ""; return { sourceTurnId: n, ...r ? { toolCallId: r } : {} }; } /** Classifies raw assistant rows after their persisted visibility facts. */ function isSilentAssistantTranscriptMessage(e) { const t = asOptionalObjectRecord(e); if (t?.role !== "assistant") return false; if (t.display === false) return true; if (t.display === true || readTerminalSourceReplyDeliveryMirror(e) || hasPersistedMedia(e) || hasAnyNonEmptyString(asOptionalRecord(t.openclawDelivery)?.mediaUrls)) return false; const n = typeof t.text === "string" ? t.text : t.content; if (typeof n === "string") return isSilentReplyText(n); if (!Array.isArray(t.content)) return false; const r = []; for (const e of t.content) { const t = asOptionalRecord(e); if (t?.type === "thinking" || t?.type === "reasoning" || t?.type === "redacted_thinking") continue; if (![ "text", "input_text", "output_text" ].includes(String(t?.type)) || typeof t?.text !== "string") return false; r.push(t.text); } const i = readAssistantTextBlocksForPhase(e, "final_answer"); return isSilentReplyText(i.length ? i.map((e) => e.text).join("") : r.join("")); } /** Applies raw stored-row visibility before preparing one leading silence line. */ function prepareAssistantTranscriptText(e, t) { const n = asOptionalObjectRecord(e); if (n?.role !== "assistant") return t; if (isSilentAssistantTranscriptMessage(e)) return; return n.display !== true && !readTerminalSourceReplyDeliveryMirror(e) && t !== void 0 ? stripMixedSilentReplyTokens(t) ?? t : t; } //#endregion export { normalizeSentMediaUrlsForDelivery as _, isAssistantTextContentBlockType as a, parseAssistantTextSignature as c, readTerminalSourceReplyDeliveryMirror as d, resolveAssistantEventPhase as f, hasAnyNonEmptyString as g, hasPersistedMedia as h, extractFirstTextBlock as i, prepareAssistantTranscriptText as l, MEDIA_ONLY_USER_TEXT as m, extractAssistantTextForPhase as n, isSilentAssistantTranscriptMessage as o, resolveAssistantMessagePhase as p, extractAssistantTranscriptSourceText as r, normalizeAssistantPhase as s, extractAssistantPhaseText as t, readAssistantTextBlocksForPhase as u };