openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
81 lines (80 loc) • 4.76 kB
JavaScript
import { g as readStringValue, t as hasNonEmptyString } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import "./tool-policy-BR4v75RA.mjs";
import { c as normalizeToolPolicyName } from "./tool-policy-shared-BHvBopvF.mjs";
import { a as readToolResultDetails, n as isToolResultError } from "./tool-result-error-B2GHe83l.mjs";
import { n as isMessageToolSendActionName } from "./message-action-names-BPsScVN2.mjs";
import { o as isPluginNativeMessagingTool, r as isMessagingToolDeliveryAction, t as isMessageToolConversationCreateActionName } from "./embedded-agent-messaging-CBeq8Mz9.mjs";
import { a as pluginEnvelopeHas, c as readEmbeddedMessageDeliveryFact, i as pluginBroadcastHasDelivery, l as normalizeMessageDeliveryStatus } from "./embedded-agent-message-delivery-ClVuBxqZ.mjs";
//#region src/agents/embedded-agent-message-tool-source-reply.ts
const EXPLICIT_MESSAGE_ROUTE_KEYS = [
"channel",
"target",
"to",
"channelId",
"provider"
];
/** Resolves whether a delivered source send ends the turn; an omitted `final` takes the run's default. */
function resolveMessageToolSourceReplyFinal(e, t = true) {
const n = (asOptionalRecord(e) ?? {}).final;
return n === void 0 ? t : n !== false;
}
function resultConfirmsCurrentSourceRoute(e) {
return asOptionalRecord(asOptionalRecord(e)?.details)?.sourceReplyRoute === "current-source";
}
function hasExplicitMessageRoute(e) {
return EXPLICIT_MESSAGE_ROUTE_KEYS.some((t) => hasNonEmptyString(e[t])) || Array.isArray(e.targets) && e.targets.some((e) => hasNonEmptyString(e));
}
function isMessageToolSourceReplyActionName(e) {
return isMessageToolSendActionName(e) || [
"reply",
"thread-reply",
"poll"
].includes(normalizeMessageDeliveryStatus(e) ?? "");
}
function readMessageToolSourceReplyText(e) {
const t = asOptionalRecord(e) ?? {};
if (!isMessageToolSourceReplyActionName(t.action)) return;
if (normalizeMessageDeliveryStatus(t.action) === "poll") return readStringValue(t.pollQuestion) ?? readStringValue(t.poll_question);
return [
"content",
"message",
"text",
"body"
].map((e) => readStringValue(t[e])).find((e) => e !== void 0);
}
function isDeliveredMessagingToolResult(e) {
const t = asOptionalRecord(e.args) ?? {};
if (t.dryRun === true) return false;
const n = normalizeToolPolicyName(e.toolName ?? "message");
if (n === "conversations_send" || n === "conversations_turn") {
const t = readToolResultDetails(e.result)?.status;
return t === "sent" || n === "conversations_turn" && (t === "replied" || t === "timeout");
}
if (!isPluginNativeMessagingTool(n)) return false;
const r = normalizeMessageDeliveryStatus(t.action);
const i = [e.result, e.hookResult];
const hasSignal = (e) => i.some((t) => pluginEnvelopeHas(t, e));
if (hasSignal("dryRun") || e.requirePluginDeliveryId && !hasSignal("deliveryId")) return false;
if (hasSignal("partial")) return true;
if (r && isMessageToolConversationCreateActionName(r) && hasSignal("conversation")) return true;
if (r === "broadcast" && i.some(pluginBroadcastHasDelivery)) return true;
if (e.isError || i.some((e) => isToolResultError(e) || pluginEnvelopeHas(e, "failure"))) return false;
const a = hasSignal("nonDelivery");
const o = hasSignal("noOp");
return !a && !o && (isMessagingToolDeliveryAction(n, t) && r !== "broadcast" && hasSignal("ok") || hasSignal("delivery"));
}
function isDeliveredMessageToolOnlySourceReplyResult(e) {
const t = readEmbeddedMessageDeliveryFact(readToolResultDetails(e.hookResult)?.messageDelivery) ?? readEmbeddedMessageDeliveryFact(readToolResultDetails(e.result)?.messageDelivery);
const n = t?.sourceReplyDelivered === true || resultConfirmsCurrentSourceRoute(e.result) || resultConfirmsCurrentSourceRoute(e.hookResult);
if (e.sourceReplyDeliveryMode !== "message_tool_only" && !n && e.allowExplicitSourceRoute !== true) return false;
if (normalizeToolPolicyName(e.toolName) !== "message") return false;
const r = asOptionalRecord(e.args) ?? {};
if (r.dryRun === true || e.isError && !t?.sourceReplyDelivered || t?.partialDelivery || [e.result, e.hookResult].some((e) => pluginEnvelopeHas(e, "partial"))) return false;
const i = (e.allowExplicitSourceRoute === true || n) && isMessageToolSourceReplyActionName(r.action);
if (t?.sourceReplyDelivered !== true && !isMessageToolSendActionName(r.action) && !i) return false;
if (hasExplicitMessageRoute(r) && e.allowExplicitSourceRoute !== true && !n) return false;
return e.deliveryConfirmed ?? (t ? t.status === "settled" : isDeliveredMessagingToolResult(e));
}
//#endregion
export { resolveMessageToolSourceReplyFinal as i, isDeliveredMessagingToolResult as n, readMessageToolSourceReplyText as r, isDeliveredMessageToolOnlySourceReplyResult as t };