UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

194 lines (193 loc) • 8.22 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { g as normalizeUniqueStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { r as resolveAgentConfig } from "./agent-scope-config-DmXgFGKP.mjs"; import { t as normalizeAnyChannelId } from "./registry-normalize-guW5_4yo.mjs"; import { t as getChannelPlugin } from "./registry-ByqVE5S3.mjs"; import "./plugins-BoMiRtiY.mjs"; import { n as isMessageToolSendActionName, r as resolveChannelMessageActionFacts } from "./message-action-names-BPsScVN2.mjs"; import { t as MessageActionDeniedError } from "./message-action-denial-Csh3qR94.mjs"; import { r as normalizeTargetForProvider } from "./target-normalization-qHbFjVWS.mjs"; import { n as lookupDirectoryDisplay, t as formatTargetDisplay } from "./target-resolver-DZ4okT3e.mjs"; //#region src/infra/outbound/outbound-policy.ts function normalizeTarget(e, t) { return normalizeTargetForProvider(e, t) ?? t.trim(); } function isCrossContextTarget(e) { if (e.toolContext && getChannelPlugin(e.channel)?.threading?.matchesToolContextTarget?.({ target: e.target, toolContext: e.toolContext })) return false; const t = [e.toolContext?.currentMessagingTarget?.trim(), e.toolContext?.currentChannelId?.trim()].filter((e) => Boolean(e)); if (t.length === 0) return false; const n = normalizeTarget(e.channel, e.target); if (!n) return false; return !t.some((t) => normalizeTarget(e.channel, t) === n); } function mergeMessagePolicy(e, t) { return e || t ? { ...e, ...t } : void 0; } function resolveEffectiveMessageToolsConfig(e) { const { cfg: t, agentId: n } = e; const r = n?.trim(); const i = t.tools?.message; if (!r) return i; const a = resolveAgentConfig(t, r)?.tools?.message; if (!a) return i; const o = mergeMessagePolicy(i?.crossContext, a.crossContext); return { ...i, ...a, crossContext: o ? { ...o, marker: mergeMessagePolicy(i?.crossContext?.marker, a.crossContext?.marker) } : void 0, broadcast: mergeMessagePolicy(i?.broadcast, a.broadcast), actions: mergeMessagePolicy(i?.actions, a.actions) }; } function resolveAllowedMessageActions(e) { const t = resolveEffectiveMessageToolsConfig(e)?.actions?.allow; if (!t) return; const n = normalizeUniqueStringEntries(t); return n.length > 0 ? n : void 0; } /** * Rejects disabled message actions before channel-specific send handling runs. */ function enforceMessageActionAllowlist(e) { const t = resolveAllowedMessageActions(e); if (!t || t.includes(e.action)) return; throw new MessageActionDeniedError(`Message action "${e.action}" is disabled for this agent.`, "message_action_disabled", "message-actions:allowlist"); } /** * Enforces source-provider policy independently of channel/thread target availability. */ function enforceCrossContextPolicy(e) { if (!resolveChannelMessageActionFacts(e.action)?.crossContext) return; const t = resolveEffectiveMessageToolsConfig(e); const n = e.toolContext?.currentChannelProvider; const r = t?.crossContext?.allowWithinProvider !== false; const i = t?.crossContext?.allowAcrossProviders !== false; if (n && n !== e.channel) { if (!i) throw new MessageActionDeniedError(`Cross-context messaging denied: action=${e.action} target provider "${e.channel}" while bound to "${n}".`, "message_cross_context_denied", "message-cross-context:provider"); return; } if (r) return; const a = e.toolContext?.currentChannelId?.trim() ?? e.toolContext?.currentMessagingTarget?.trim(); if (!a) return; const o = e.action === "thread-reply" || e.action === "thread-create" ? ["channelId", "to"] : ["to", "channelId"]; let s; for (const t of o) if (typeof e.args[t] === "string") { s = e.args[t]; break; } if (!s || !isCrossContextTarget({ channel: e.channel, target: s, toolContext: e.toolContext })) return; throw new MessageActionDeniedError(`Cross-context messaging denied: action=${e.action} target="${s}" while bound to "${a}" (channel=${e.channel}).`, "message_cross_context_denied", "message-cross-context:target"); } async function buildCrossContextDecoration(e) { const t = e.toolContext?.currentChannelId ?? e.toolContext?.currentMessagingTarget; if (!t || e.toolContext?.skipCrossContextDecoration || !isCrossContextTarget(e)) return null; const n = resolveEffectiveMessageToolsConfig(e)?.crossContext?.marker; if (n?.enabled === false) return null; const r = await lookupDirectoryDisplay({ cfg: e.cfg, channel: e.channel, targetId: t, accountId: e.accountId ?? void 0 }) ?? t; const i = formatTargetDisplay({ channel: e.channel, target: t, display: r }); const a = n?.prefix ?? "[from {channel}] "; const o = n?.suffix ?? ""; const s = a.replaceAll("{channel}", i); const c = o.replaceAll("{channel}", i); const l = getChannelPlugin(e.channel)?.messaging?.buildCrossContextPresentation; return { prefix: s, suffix: c, presentationBuilder: l ? (t) => l({ originLabel: i, message: t, cfg: e.cfg, accountId: e.accountId ?? void 0 }) : void 0 }; } function shouldApplyCrossContextMarker(e) { return resolveChannelMessageActionFacts(e)?.crossContext === "marker"; } function applyCrossContextDecoration(e) { const t = e.decoration.presentationBuilder; if (e.preferPresentation && t) return { message: e.message, presentation: t(e.message) }; return { message: `${e.decoration.prefix}${e.message}${e.decoration.suffix}` }; } //#endregion //#region src/agents/embedded-agent-messaging.ts const CORE_MESSAGING_TOOLS = /* @__PURE__ */ new Set([ "sessions_send", "conversations_send", "conversations_turn", "message" ]); const MESSAGE_TOOL_CONVERSATION_CREATE_ACTIONS = /* @__PURE__ */ new Set([ "thread-create", "topic-create", "threadcreate", "createforumtopic" ]); /** Return true when a message action creates a visible destination conversation. */ function isMessageToolConversationCreateActionName(e) { const t = normalizeOptionalString(e)?.toLowerCase() ?? ""; return MESSAGE_TOOL_CONVERSATION_CREATE_ACTIONS.has(t); } /** Return true for core or channel-plugin messaging tool names. */ function isMessagingTool(e) { return CORE_MESSAGING_TOOLS.has(e) || isPluginNativeMessagingTool(e); } function isPluginNativeMessagingTool(e) { const t = normalizeAnyChannelId(e); return e === "message" || Boolean(t && getChannelPlugin(t)?.actions); } /** Return true when the specific tool invocation is an outbound send. */ function isMessagingToolSendAction(e, t) { const n = normalizeOptionalString(t.action) ?? ""; if (CORE_MESSAGING_TOOLS.has(e)) return e !== "message" || isMessageToolSendActionName(n); const r = normalizeAnyChannelId(e); return Boolean(r && getChannelPlugin(r)?.actions?.extractToolSend?.({ args: t })?.to); } /** Return true when a visible delivery has one target worth recording as evidence. */ function isMessagingToolTargetEvidenceAction(e, t) { if (e === "conversations_send" || e === "conversations_turn") return true; if (e === "message") { const e = normalizeOptionalString(t.action) ?? ""; return shouldApplyCrossContextMarker(e) || isMessageToolConversationCreateActionName(e); } return isMessagingToolSendAction(e, t); } /** Return true when a messaging invocation can create visible outbound delivery. */ function isMessagingToolDeliveryAction(e, t) { if (e === "conversations_send" || e === "conversations_turn") return true; if (e === "message") { const e = normalizeOptionalString(t.action) ?? ""; const n = resolveChannelMessageActionFacts(e); return n !== void 0 && !n.readOnly || isMessageToolConversationCreateActionName(e); } const n = normalizeAnyChannelId(e); if (n && getChannelPlugin(n)?.actions?.isToolDeliveryAction?.({ args: t })) return true; return isMessagingToolSendAction(e, t); } //#endregion export { isMessagingToolTargetEvidenceAction as a, buildCrossContextDecoration as c, resolveAllowedMessageActions as d, resolveEffectiveMessageToolsConfig as f, isMessagingToolSendAction as i, enforceCrossContextPolicy as l, isMessagingTool as n, isPluginNativeMessagingTool as o, shouldApplyCrossContextMarker as p, isMessagingToolDeliveryAction as r, applyCrossContextDecoration as s, isMessageToolConversationCreateActionName as t, enforceMessageActionAllowlist as u };