UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

203 lines (202 loc) • 7 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, t as hasNonEmptyString, u as normalizeOptionalStringifiedId } from "./string-coerce-CCIiB4H7.mjs"; import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs"; import { r as isInternalNonDeliveryChannel } from "./message-channel-constants-CkbqRgGl.mjs"; import { n as getBootstrapChannelPlugin } from "./channel-configured-3z7rRlJy.mjs"; import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs"; import { t as isDeliverableMessageChannel } from "./message-channel-normalize-CFHSufeo.mjs"; import "./message-channel-DTYVLJT1.mjs"; import { n as isMessageToolSendActionName, r as resolveChannelMessageActionFacts } from "./message-action-names-BPsScVN2.mjs"; import { i as missingMessageActionTargetError } from "./target-errors-DefLZSM6.mjs"; import "./embedded-agent-messaging-CBeq8Mz9.mjs"; //#region src/infra/outbound/message-action-param-keys.ts const STANDARD_MESSAGE_ACTION_PARAM_KEYS = /* @__PURE__ */ new Set([ "accountId", "action", "asDocument", "attachments", "base64", "bestEffort", "buffer", "caption", "channel", "channelId", "clawhub", "contentType", "delivery", "dryRun", "filePath", "fileUrl", "filename", "forceDocument", "gifPlayback", "gatewayToken", "gatewayUrl", "image", "idempotencyKey", "interactive", "json", "media", "mediaUrl", "mediaUrls", "media_urls", "message", "mimeType", "path", "pollAnonymous", "pollDurationHours", "pollMulti", "pollOption", "pollPublic", "pollQuestion", "pin", "presentation", "replyTo", "silent", "senderIsOwner", "target", "targets", "text", "threadId", "timeoutMs", "topLevel", "to" ]); /** * Detects non-standard message action params that may need plugin-owned handling. */ function hasPotentialPluginActionParam(e) { return Object.entries(e).some(([e, t]) => { if (STANDARD_MESSAGE_ACTION_PARAM_KEYS.has(e)) return false; return typeof t === "string" ? Boolean(normalizeOptionalString(t)) : typeof t === "number" ? Number.isFinite(t) : t !== void 0; }); } //#endregion //#region src/infra/outbound/message-action-spec.ts function resolvePluginActionTargetAliasSpec(e, t, n) { const r = normalizeOptionalLowercaseString(n?.channel); if (!r || !hasPotentialPluginActionParam(t)) return; const i = n?.aliasSpec; return i !== void 0 ? i : getBootstrapChannelPlugin(r)?.actions?.messageActionTargetAliases?.[e]; } const ACTION_TARGET_ALIASES = { unsend: ["messageId"], edit: ["messageId"], react: [ "chatGuid", "chatIdentifier", "chatId" ], renameGroup: [ "chatGuid", "chatIdentifier", "chatId" ], setGroupIcon: [ "chatGuid", "chatIdentifier", "chatId" ], addParticipant: [ "chatGuid", "chatIdentifier", "chatId" ], removeParticipant: [ "chatGuid", "chatIdentifier", "chatId" ], leaveGroup: [ "chatGuid", "chatIdentifier", "chatId" ] }; /** Resolves a plugin-declared delivery alias into the shared target contract. */ function resolveActionDeliveryTargetAlias(e, t, n) { const r = resolvePluginActionTargetAliasSpec(e, t, n); if (!r) return; const i = r.resolveDeliveryTarget?.({ args: t }); if (i !== void 0) return normalizeOptionalString(i); const a = (r.deliveryTargetAliases ?? []).map((e) => normalizeOptionalStringifiedId(t[e])).filter((e) => Boolean(e)); if (new Set(a).size > 1) throw new Error(`Action ${e} received conflicting delivery target aliases.`); return a[0]; } /** Reports whether a plugin alias identifies an existing resource rather than a conversation. */ function actionHasResourceReference(e, t, n) { const r = resolvePluginActionTargetAliasSpec(e, t, n); if (!r?.deliveryTargetAliases) return false; const i = new Set(r.deliveryTargetAliases); return r.aliases.some((e) => !i.has(e) && normalizeOptionalStringifiedId(t[e]) !== void 0); } function actionRequiresTarget(e) { return resolveChannelMessageActionFacts(e)?.targetMode !== "none"; } function actionHasTarget(e, t, n) { if (hasNonEmptyString(t.to) || hasNonEmptyString(t.channelId)) return true; const r = resolvePluginActionTargetAliasSpec(e, t, n)?.aliases; return [...ACTION_TARGET_ALIASES[e] ?? [], ...r ?? []].some((e) => normalizeOptionalStringifiedId(t[e]) !== void 0); } //#endregion //#region src/infra/outbound/message-action-normalization.ts function resolveImplicitMessageActionTarget(e, t) { const n = isMessageToolSendActionName(t) || t === "poll" ? [e?.currentMessagingTarget, e?.currentChannelId] : [e?.currentChannelId, e?.currentMessagingTarget]; for (const e of n) { const t = normalizeOptionalString(e); if (!t || isInternalNonDeliveryChannel(t)) continue; if (parseAgentSessionKey(t.replace(/^channel:/i, ""))) continue; return t; } } function normalizeMessageActionInput(e) { const t = { ...e.args }; const { action: n, toolContext: r } = e; const i = normalizeOptionalString(t.channel) ?? ""; const a = i || normalizeMessageChannel(r?.currentChannelProvider) || ""; const o = normalizeOptionalString(t.target) ?? ""; const s = Object.hasOwn(t, "targets"); const c = typeof t.to === "string" || typeof t.channelId === "string"; const l = normalizeOptionalString(t.to) ?? normalizeOptionalString(t.channelId) ?? ""; const u = { channel: a, aliasSpec: e.targetAliasSpec }; const d = resolveActionDeliveryTargetAlias(n, t, u); const f = actionHasResourceReference(n, t, u); if (d && (o && d !== o || l && d !== l)) throw new Error(`Action ${n} received conflicting target and delivery alias values.`); if (o && c) { delete t.to; delete t.channelId; } if (!o && !l && d) t.target = d; if (!o && actionRequiresTarget(n)) { if (l) { t.target = l; delete t.to; delete t.channelId; } else if (!s && !d && (f || !actionHasTarget(n, t, u))) { const e = resolveImplicitMessageActionTarget(r, n); if (e) t.target = e; } } if (!i && a && isDeliverableMessageChannel(a)) t.channel = a; const p = normalizeOptionalString(t.target) ?? ""; const m = hasNonEmptyString(t.to); const h = hasNonEmptyString(t.channelId); const g = resolveChannelMessageActionFacts(n)?.targetMode ?? "none"; if (g !== "none" && (m || h)) throw new Error("Use `target` instead of `to`/`channelId`."); else if (g === "none" && m) throw new Error("Use `target` for actions that accept a destination."); if (p) { if (g === "none") throw new Error(`Action ${n} does not accept a target.`); t[g] = p; } const _ = [ t.target, t.to, t.channelId ].some((e) => Boolean(normalizeOptionalString(e))); if (actionRequiresTarget(n) && (!actionHasTarget(n, t, u) || f && !_ && !e.allowResourceOnly)) throw missingMessageActionTargetError(n, { hasCurrentSourceConversation: Boolean(resolveImplicitMessageActionTarget(r, "send")) }); return t; } //#endregion export { resolveActionDeliveryTargetAlias as a, actionRequiresTarget as i, resolveImplicitMessageActionTarget as n, hasPotentialPluginActionParam as o, actionHasTarget as r, normalizeMessageActionInput as t };