UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

114 lines (113 loc) • 4.88 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { t as normalizeChatType } from "./chat-type-CL5nBDaO.mjs"; import { a as resolveUserTimezone } from "./date-time-ChxDjIEM.mjs"; import { a as resolveTimezone, n as formatUtcTimestamp, r as formatZonedTimestamp } from "./format-datetime-B-weOzaQ.mjs"; import { n as formatTimeAgo } from "./format-relative-JG-kbkDF.mjs"; //#region src/channels/sender-label.ts const OPAQUE_UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu; function resolveSenderLabel(e) { const t = normalizeOptionalString(e.name) ?? normalizeOptionalString(e.username) ?? normalizeOptionalString(e.tag) ?? ""; const n = normalizeOptionalString(e.e164) ?? normalizeOptionalString(e.id) ?? ""; if (t && n && t !== n && !OPAQUE_UUID_RE.test(n)) return `${t} (${n})`; return t || n || null; } //#endregion //#region src/auto-reply/envelope.ts function sanitizeEnvelopeHeaderPart(e) { return e.replaceAll("[", "(").replaceAll("]", ")").replace(/\s+/g, " ").trim(); } /** Resolves envelope formatting defaults from agent config. */ function resolveEnvelopeFormatOptions(e) { const t = e?.agents?.defaults; const n = normalizeOptionalString(t?.userTimezone); return { timezone: n ? resolveTimezone(n) ?? "local" : void 0, includeTimestamp: true, includeElapsed: true, userTimezone: t?.userTimezone }; } function resolveEnvelopeTimezone(e) { const t = normalizeOptionalString(e?.timezone); if (!t) return { mode: "local" }; const n = t.toLowerCase(); if (n === "utc" || n === "gmt") return { mode: "utc" }; if (n === "local" || n === "host") return { mode: "local" }; if (n === "user") return { mode: "iana", timeZone: resolveUserTimezone(e?.userTimezone) }; const r = resolveTimezone(t); return r ? { mode: "iana", timeZone: r } : { mode: "utc" }; } let utcWeekdayFormatter; /** Formats an envelope timestamp using local, UTC, user, or explicit IANA timezone rules. */ function formatAgentEnvelopeTimestamp(e, t) { if (e === void 0 || t?.includeTimestamp === false) return; const n = e instanceof Date ? e : new Date(e); if (Number.isNaN(n.getTime())) return; const r = resolveEnvelopeTimezone(t); if (r.mode !== "utc") return formatZonedTimestamp(n, { timeZone: r.mode === "iana" ? r.timeZone : void 0, displaySeconds: true, displayWeekday: true }); const i = formatUtcTimestamp(n, { displaySeconds: true }); utcWeekdayFormatter ??= new Intl.DateTimeFormat("en-US", { timeZone: "UTC", weekday: "short" }); return `${utcWeekdayFormatter.format(n)} ${i}`; } /** Formats the generic bracketed envelope prepended to agent-visible messages. */ function formatAgentEnvelope(e) { const t = [sanitizeEnvelopeHeaderPart(normalizeOptionalString(e.channel) || "Channel")]; let n; if (e.envelope?.includeElapsed !== false && e.timestamp && e.previousTimestamp) { const t = (e.timestamp instanceof Date ? e.timestamp.getTime() : e.timestamp) - (e.previousTimestamp instanceof Date ? e.previousTimestamp.getTime() : e.previousTimestamp); n = Number.isFinite(t) && t >= 0 ? formatTimeAgo(t, { suffix: false }) : void 0; } const r = normalizeOptionalString(e.from); if (r) { const e = sanitizeEnvelopeHeaderPart(r); t.push(n ? `${e} +${n}` : e); } else if (n) t.push(`+${n}`); for (const n of [e.host, e.ip]) { const e = normalizeOptionalString(n); if (e) t.push(sanitizeEnvelopeHeaderPart(e)); } const i = formatAgentEnvelopeTimestamp(e.timestamp, e.envelope); if (i) t.push(i); return `${`[${t.join(" ")}]`} ${e.body}`; } /** Formats an inbound message body with sender attribution appropriate for direct/group chats. */ function formatInboundEnvelope(e) { const t = normalizeChatType(e.chatType); const n = !t || t === "direct"; const r = normalizeOptionalString(e.senderLabel) || resolveSenderLabel(e.sender ?? {}); const i = r ? sanitizeEnvelopeHeaderPart(r) : ""; const a = sanitizeEnvelopeHeaderPart(normalizeOptionalString(e.from) || ""); const o = a.search(/\s+id:/i); const s = o > 0 ? a.slice(0, o).trim() : a; const c = s.includes(":") ? "(sender)" : s; const l = n ? e.fromMe ? "(self)" : c : i; return formatAgentEnvelope({ channel: e.channel, from: e.from, timestamp: e.timestamp, previousTimestamp: e.previousTimestamp, envelope: e.envelope, body: l ? `${l}: ${e.body}` : e.body }); } /** Builds the compact `from` label used in inbound envelope headers. */ function formatInboundFromLabel(e) { const t = e.isGroup ? normalizeOptionalString(e.groupLabel) || e.groupFallback || "Group" : e.directLabel.trim(); const n = (e.isGroup ? e.groupId : e.directId)?.trim(); return n && (e.isGroup || n !== t) ? `${t} id:${n}` : t; } //#endregion export { resolveEnvelopeFormatOptions as a, formatInboundFromLabel as i, formatAgentEnvelopeTimestamp as n, formatInboundEnvelope as r, formatAgentEnvelope as t };