openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
212 lines (211 loc) • 8.86 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { d as withPluginRuntimeRegistryScope, o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs";
import { _ as isPluginRegistryGatewayViewOf, d as getPluginRegistryGatewayOwner, u as getPluginRegistryGatewayChannelRegistration } from "./registry-lifecycle-CJjWnl-y.mjs";
import { t as runOutsidePluginRuntimeGenerationScope } from "./generation-scope-Cq7RLFex.mjs";
import { v as isReplyPayloadTargetSuppressed } from "./reply-payload-k5WsQWxi.mjs";
import { s as getGroupThreadDispatchContext } from "./group-thread-context-DFimpu_J.mjs";
import { r as getPluginRuntimeLoadContext } from "./load-context-CwTU1iXB.mjs";
import { r as PlatformMessageNotDispatchedError } from "./deliver-types-BnSKrpHG.mjs";
import { l as isProvenDeliveryNotSentError } from "./delivery-recovery.shared-3Nb7FEaL.mjs";
import { t as normalizeDeliverableOutboundChannel } from "./channel-resolution-DjtSMYUG.mjs";
import { t as deriveDurableFinalDeliveryRequirements } from "./capabilities-Cm5mmzL6.mjs";
import { n as sendDurableMessageBatchCore, r as sendStructuredDurableMessageBatchCore, t as durableMessageBatchMayHaveReachedRecipient } from "./send-C6MWAmk5.mjs";
import { s as resolveOutboundDurableFinalDeliverySupport } from "./deliver-prepare-D0EzlldE.mjs";
import "./deliver-dx0iSMKq.mjs";
import { d as createChannelPartialDeliveryError, l as createChannelDeliveryResultFromReceipt } from "./live-DOXDs-ke.mjs";
import { t as buildOutboundSessionContext } from "./session-context-CCVPkFMh.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/channels/turn/durable-delivery.ts
function resolveDurableInboundReplyToId(e) {
if (isReplyPayloadTargetSuppressed(e.payload)) return null;
if (e.replyToId === null || e.payload.replyToId === null) return null;
return normalizeOptionalString(e.replyToId) ?? normalizeOptionalString(e.payload.replyToId) ?? normalizeOptionalString(e.ctxPayload.ReplyToIdFull) ?? normalizeOptionalString(e.ctxPayload.ReplyToId);
}
function resolveDurableSuppression(e) {
const t = e.payloadOutcomes?.find((e) => e.status === "suppressed")?.hookEffect;
return {
reason: e.reason,
...t?.cancelReason ? { cancelReason: t.cancelReason } : {},
...t?.metadata ? { metadata: t.metadata } : {}
};
}
function isDurableInboundReplyDeliveryHandled(e) {
return e.status === "handled_visible" || e.status === "handled_no_send";
}
function throwIfDurableInboundReplyDeliveryFailed(e) {
if (e.status === "failed") throw e.error;
}
async function deliverInboundReplyWithMessageSendContextCore(e) {
return await deliverInboundReplyWithMessageSendContext(e, sendDurableMessageBatchCore);
}
/** Delivers a prepared final reply through the same durable owner without parsing its text. */
async function deliverStructuredInboundReplyWithMessageSendContextCore(e) {
const { plan: t, ...n } = e;
return await deliverInboundReplyWithMessageSendContext({
...n,
payload: t.payload
}, ({ payloads: e, ...n }) => sendStructuredDurableMessageBatchCore({
...n,
plan: [t]
}));
}
async function deliverInboundReplyWithMessageSendContext(e, t) {
if (e.info.kind !== "final") return {
status: "not_applicable",
reason: "non_final"
};
try {
const deliver = (n, r) => deliverAdmittedInboundReply({
...e,
cfg: n
}, t, r);
const n = getPluginRuntimeGatewayRequestScope()?.pluginRegistry;
const r = n && getPluginRegistryGatewayOwner(n);
if (!n || !r) return await deliver(e.cfg);
const i = r.current();
const reject = (e) => {
throw new PlatformMessageNotDispatchedError(e, {
cause: new Error(e),
retryable: false
});
};
if (!i) return reject("The Gateway that admitted this reply is closing.");
const assertCurrent = () => {
if (r.current() !== i) reject("The reply delivery runtime changed before sending.");
};
if (i === n || isPluginRegistryGatewayViewOf(n, i)) return await runOutsidePluginRuntimeGenerationScope(() => withPluginRuntimeRegistryScope(i, () => deliver(e.cfg, assertCurrent)));
const a = getPluginRuntimeLoadContext(i)?.rawConfig;
const o = i.channels.find((t) => t.plugin.id === e.channel);
const s = e.prepareRuntimeHandoff;
const c = getPluginRegistryGatewayChannelRegistration(n, e.channel);
const l = c?.config;
const u = o && c?.pluginId === o.pluginId && c.plugin === o.plugin;
if (!a || !l || !isDeepStrictEqual(a.channels?.[e.channel], l.channels?.[e.channel]) || !isDeepStrictEqual(a.channels?.defaults, l.channels?.defaults) || !o || !u || !isDeepStrictEqual(a.plugins?.entries?.[o.pluginId], l.plugins?.entries?.[o.pluginId])) return reject("The reply channel changed; delivery was not started.");
return await runOutsidePluginRuntimeGenerationScope(() => withPluginRuntimeRegistryScope(i, () => {
const e = s ? s(a) : a;
assertCurrent();
return deliver(e, assertCurrent);
}));
} catch (e) {
return {
status: "failed",
error: e
};
}
}
async function deliverAdmittedInboundReply(e, t, n) {
const r = getGroupThreadDispatchContext();
const i = r ? {
...e,
agentId: r.ctx.AgentId ?? e.agentId,
ctxPayload: r.ctx,
runId: r.runState.runId,
executionIdentityToken: r.runState.executionIdentityToken
} : e;
const a = normalizeDeliverableOutboundChannel(i.channel);
const o = normalizeOptionalString(i.to) ?? normalizeOptionalString(i.ctxPayload.OriginatingTo) ?? normalizeOptionalString(i.ctxPayload.To);
if (!a) return {
status: "unsupported",
reason: "missing_channel"
};
if (!o) return {
status: "unsupported",
reason: "missing_target"
};
const s = resolveDurableInboundReplyToId(i);
const c = "threadId" in i ? i.threadId : i.ctxPayload.MessageThreadId;
const l = i.requiredCapabilities ?? deriveDurableFinalDeliveryRequirements({
payload: i.payload,
replyToId: s,
threadId: c,
silent: i.silent
});
const u = l.reconcileUnknownSend === true ? "required" : "best_effort";
const d = await resolveOutboundDurableFinalDeliverySupport({
cfg: i.cfg,
agentId: i.agentId,
channel: a,
requirements: l
});
if (!d.ok) return {
status: "unsupported",
reason: d.reason,
...d.capability ? { capability: d.capability } : {}
};
const f = buildOutboundSessionContext({
cfg: i.cfg,
sessionKey: i.ctxPayload.SessionKey,
policySessionKey: i.ctxPayload.RuntimePolicySessionKey,
conversationType: i.ctxPayload.ChatType,
agentId: i.agentId,
requesterAccountId: i.accountId ?? i.ctxPayload.AccountId,
requesterSenderId: i.ctxPayload.SenderId ?? i.ctxPayload.From,
requesterSenderName: i.ctxPayload.SenderName,
requesterSenderUsername: i.ctxPayload.SenderUsername,
requesterSenderE164: i.ctxPayload.SenderE164
});
n?.();
const p = await t({
assertDirectAdapterHandoff: n,
cfg: i.cfg,
channel: a,
to: o,
accountId: i.accountId,
payloads: [i.payload],
retryAmbiguousFinalText: true,
...i.runId ?? i.executionIdentityToken?.runId ? { runId: i.runId ?? i.executionIdentityToken?.runId } : {},
...i.executionIdentityToken ? { executionIdentityToken: i.executionIdentityToken } : {},
threadId: c,
replyToId: s,
replyToMode: i.replyToMode,
formatting: i.formatting,
identity: i.identity,
deps: i.deps,
mediaAccess: i.mediaAccess,
silent: i.silent,
durability: u,
...l.reconcileUnknownSend === true ? { requireUnknownSendReconciliation: true } : {},
session: f,
gatewayClientScopes: i.ctxPayload.GatewayClientScopes ?? []
});
if (p.status === "failed") return {
status: "failed",
error: p.error,
...!durableMessageBatchMayHaveReachedRecipient(p) && isProvenDeliveryNotSentError(p.error) ? { sentBeforeError: false } : {}
};
const m = p.status === "partial_failed" ? p.results.map((e) => e.meta?.visibleText).filter((e) => typeof e === "string").join("") : void 0;
const h = createChannelDeliveryResultFromReceipt({
receipt: p.receipt,
threadId: c == null ? void 0 : String(c),
...s ? { replyToId: s } : {},
visibleReplySent: p.status !== "suppressed",
...m ? { content: m } : {},
...p.deliveryIntent ? { deliveryIntent: {
id: p.deliveryIntent.id,
kind: "outbound_queue",
queuePolicy: p.deliveryIntent.queuePolicy
} } : {}
});
if (p.status === "partial_failed") return {
status: "failed",
error: createChannelPartialDeliveryError(p.error, {
...h,
visibleReplySent: true
}),
sentBeforeError: true
};
if (p.status === "suppressed") return {
status: "handled_no_send",
reason: "no_visible_result",
delivery: {
...h,
suppression: resolveDurableSuppression(p)
}
};
return {
status: "handled_visible",
delivery: h
};
}
//#endregion
export { throwIfDurableInboundReplyDeliveryFailed as i, deliverStructuredInboundReplyWithMessageSendContextCore as n, isDurableInboundReplyDeliveryHandled as r, deliverInboundReplyWithMessageSendContextCore as t };