UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

318 lines (317 loc) • 9.92 kB
import { h as readNonEmptyStringPreservingWhitespace } from "./string-coerce-CCIiB4H7.mjs"; import { i as asOptionalObjectRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import { n as readParsedField } from "./string-readers-BtMVaphq.mjs"; import { i as normalizeChatChannelId } from "./ids-BRSWrLzE.mjs"; import { t as findChatChannelMeta } from "./chat-meta-CsbrDHrF.mjs"; import "./registry-BB86xKJa.mjs"; import { n as getLoadedChannelPlugin, t as getChannelPlugin } from "./registry-ByqVE5S3.mjs"; import "./plugins-BoMiRtiY.mjs"; import { n as isRich, r as theme } from "./theme-BOI0xyRZ.mjs"; import { t as formatTargetDisplay } from "./target-resolver-DZ4okT3e.mjs"; import { r as renderTerminalSafeTable, t as getTerminalTableWidth } from "./table-D2a-pD4_.mjs"; import { i as resolveMessageActionOutcome, r as resolveMessageActionMessageId } from "./message-action-contracts-Al2l9Lkb.mjs"; import { n as shortenText } from "./text-format-NQBZN0eq.mjs"; //#region src/commands/message-format.ts const resolveChannelLabel = (e) => getLoadedChannelPlugin(e)?.meta.label ?? e; function firstNonemptyString(e, ...t) { return (e && readParsedField(e, t, readNonEmptyStringPreservingWhitespace)) ?? ""; } function renderSummaryValue(e) { if (e == null) return "null"; if (Array.isArray(e)) return `${e.length} items`; switch (typeof e) { case "string": case "number": case "boolean": case "bigint": case "symbol": return String(e); default: return typeof e; } } function renderObjectSummary(e, t) { if (!e || typeof e !== "object") return sanitizeTerminalText(String(e)); const n = e; const r = Object.keys(n); if (r.length === 0) return theme.muted("(empty)"); const i = r.slice(0, 20).map((e) => { const t = renderSummaryValue(n[e]); return { Key: e, Value: shortenText(t, 96) }; }); return renderTerminalSafeTable({ width: t.width, columns: [{ key: "Key", header: "Key", minWidth: 16 }, { key: "Value", header: "Value", flex: true, minWidth: 24 }], rows: i }).trimEnd(); } function renderMessageList(e, t, n, r = false) { const i = t.displayLimit ?? 25; const a = e.slice(0, i).map((e) => { const t = r && isRecord(e) && isRecord(e.message) ? e.message : e; const n = firstNonemptyString(t, "id", "ts", "messageId"); const i = t.author; let a = firstNonemptyString(t, "authorTag") || firstNonemptyString(i, "username") || firstNonemptyString(t, "user"); if (!a) { const e = asOptionalObjectRecord(t.from); a = firstNonemptyString(asOptionalObjectRecord(e?.user ?? e?.application), "displayName", "id"); } const o = firstNonemptyString(t, "timestamp", "ts", "createdAt"); const s = firstNonemptyString(t, "content", "text"); return { Time: shortenText(o, 28), Author: shortenText(a, 22), Text: shortenText(s.replace(/\s+/g, " ").trim(), 90), Id: shortenText(n, 22) }; }); if (a.length === 0) return theme.muted(n); return renderTerminalSafeTable({ width: t.width, columns: [ { key: "Time", header: "Time", minWidth: 14 }, { key: "Author", header: "Author", minWidth: 10 }, { key: "Text", header: "Text", flex: true, minWidth: 24 }, { key: "Id", header: "Id", minWidth: 10 } ], rows: a }).trimEnd(); } function renderPaginationHint(e, t) { if (!e || typeof e !== "object") return null; const n = e; if (n.hasMore === true || typeof n.nextBatch === "string" && n.nextBatch || typeof n["@odata.nextLink"] === "string" && n["@odata.nextLink"]) return t("More results available. Use --limit to fetch more, or --json for the raw cursor."); return null; } function formatMessageCliText(e, t) { const n = isRich(); const ok = (e) => { const t = sanitizeTerminalText(e); return n ? theme.success(t) : t; }; const fail = (e) => { const t = sanitizeTerminalText(e); return n ? theme.error(t) : t; }; const muted = (e) => n ? theme.muted(e) : e; const heading = (e) => n ? theme.heading(e) : e; const r = getTerminalTableWidth(); const i = t?.displayLimit; const a = { width: r, displayLimit: i }; if (e.dryRun) return [muted(`[dry-run] would run ${e.action} via ${e.channel}`)]; const o = resolveMessageActionOutcome(e); if (e.kind === "broadcast") { const t = e.payload.results ?? []; const n = t.map((e) => ({ Channel: resolveChannelLabel(e.channel), Target: shortenText(formatTargetDisplay({ channel: e.channel, target: e.to }), 36), Status: e.ok ? "ok" : e.attempted === false ? "not attempted" : "error", Error: e.ok ? "" : shortenText(e.error ?? "unknown error", 48) })); const r = t.filter((e) => e.ok).length; const i = t.filter((e) => e.attempted === false).length; const s = t.length - r - i; const c = t.length; const l = o.ok; return [(l ? ok : fail)(`${l ? "✅ Broadcast complete" : i ? "❌ Broadcast incomplete" : "❌ Broadcast failed"} (${r}/${c} succeeded, ${s} failed${i ? `, ${i} not attempted` : ""})`), renderTerminalSafeTable({ width: a.width, columns: [ { key: "Channel", header: "Channel", minWidth: 10 }, { key: "Target", header: "Target", minWidth: 12, flex: true }, { key: "Status", header: "Status", minWidth: 6 }, { key: "Error", header: "Error", minWidth: 20, flex: true } ], rows: n }).trimEnd()]; } if (!o.ok) { const t = e.kind === "send" ? e.sendResult?.result?.messageId : void 0; return [fail(`❌ ${o.error}${t ? ` Message ID: ${t}` : ""}`)]; } if (e.kind === "send" || e.kind === "poll") { const t = e.kind === "send" ? e.sendResult : void 0; const n = e.kind === "poll" ? e.pollResult : void 0; const r = t ?? n; const i = e.kind === "poll" ? "Poll sent" : "Sent"; if (e.handledBy === "core" && r) { const e = n?.result?.pollId; const a = n?.result ? { ...n.result, channel: n.channel } : t?.result; let o; if (r.via === "direct") { const e = a ? a.channel : r.channel; let t = getChannelPlugin(e)?.meta.label; if (!t) { const n = normalizeChatChannelId(e); t = n ? findChatChannelMeta(n)?.label ?? e : e; } const n = `✅ ${i} via ${t}. Message ID: ${a ? a.messageId : "unknown"}`; o = a?.target ? `${n} (${a.target.kind} ${a.target.id})` : n; } else o = `✅ ${i} via gateway${r.channel ? ` (${r.channel})` : ""}. Message ID: ${r.result?.messageId ?? "unknown"}`; const s = [ok(o)]; if (e) s.push(ok(`Poll id: ${e}`)); return s; } const a = resolveChannelLabel(e.channel); const o = resolveMessageActionMessageId(e.payload); return [ok(`✅ ${i} via ${a}.${o ? ` Message ID: ${o}` : ""}`)]; } const s = e.payload; if (e.action === "react") { const e = s.added; const t = s.removed; if (typeof e === "string" && e.trim()) return [ok(`✅ Reaction added: ${e.trim()}`)]; if (typeof t === "string" && t.trim()) return [ok(`✅ Reaction removed: ${t.trim()}`)]; if (Array.isArray(t)) { const e = normalizeStringEntries(t).join(", "); return [ok(`✅ Reactions removed${e ? `: ${e}` : ""}`)]; } return [ok("✅ Reaction updated.")]; } if (e.action === "reactions" && s && typeof s === "object") { const e = s.reactions; if (Array.isArray(e)) { const t = e.slice(0, i ?? 50).map((e) => { const t = e; const n = t.emoji; const r = firstNonemptyString(n, "raw") || firstNonemptyString(t, "name", "emoji", "key"); const i = typeof t.count === "number" ? String(t.count) : ""; const a = Array.isArray(t.users) ? t.users.slice(0, 8).map((e) => { if (typeof e === "string") return e; if (!e || typeof e !== "object") return ""; return firstNonemptyString(e, "tag", "username", "id"); }).filter(Boolean) : []; return { Emoji: r, Count: i, Users: shortenText(a.join(", "), 72) }; }); const n = t.length === 0 ? theme.muted("No reactions.") : renderTerminalSafeTable({ width: r, columns: [ { key: "Emoji", header: "Emoji", minWidth: 8 }, { key: "Count", header: "Count", align: "right", minWidth: 6 }, { key: "Users", header: "Users", flex: true, minWidth: 20 } ], rows: t }).trimEnd(); return [heading("Reactions"), n]; } } if (e.action === "read" || e.action === "list-pins") { const t = e.action === "read"; let n = s && typeof s === "object" ? s[t ? "messages" : "pins"] : void 0; if (t && !Array.isArray(n)) { const e = asOptionalObjectRecord(s)?.message; if (e && typeof e === "object") n = [e]; } if (Array.isArray(n)) { const r = renderMessageList(n, a, t ? "No messages." : "No pins.", !t && e.channel === "slack"); const i = [heading(t ? "Messages" : "Pinned messages"), r]; const o = renderPaginationHint(s, muted); if (o) i.push(o); return i; } } if (e.action === "search") { const e = s.results; let t = null; if (e && typeof e === "object") { const n = e.messages; if (Array.isArray(n)) { t = []; for (const e of n) if (Array.isArray(e) && e.length > 0) t.push(e[0]); else if (e && typeof e === "object") t.push(e); } } if (t === null) { const e = asOptionalObjectRecord(s)?.messages; if (Array.isArray(e)) t = e; } if (t) { const n = [heading("Search results"), renderMessageList(t, a, "No results.")]; const r = renderPaginationHint(s, muted) ?? renderPaginationHint(e, muted); if (r) n.push(r); return n; } } return [ ok(`✅ ${e.action} via ${resolveChannelLabel(e.channel)}.`), "", renderObjectSummary(s, a), "", muted("Tip: use --json for full output.") ]; } //#endregion export { formatMessageCliText };