openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
193 lines (192 loc) • 8.05 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { n as waitForAgentDatabaseWriteAdmission } from "./agent-database-preparation-context-CkVoUMTY.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import { n as buildAgentMainSessionKey } from "./session-key-DW1jcHbM.mjs";
import "./session-key-CqAyZeVk.mjs";
import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs";
import { t as normalizeRouteBindingChannelId } from "./binding-scope-C3Xc3l9P.mjs";
import { t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-constants-CkbqRgGl.mjs";
import { n as assertAgentDatabaseAdmitted } from "./agent-database-admission-CL6wHJ_N.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import { n as isGatewayMessageChannel, t as isDeliverableMessageChannel } from "./message-channel-normalize-CFHSufeo.mjs";
import "./message-channel-DTYVLJT1.mjs";
import { r as listConfiguredBindings, t as isRouteBinding } from "./bindings-C2C-UuI7.mjs";
import { i as resolveChannelTarget } from "./target-resolver-DZ4okT3e.mjs";
import { i as resolveChannelDefaultAccountId } from "./helpers-ELZNDAnv.mjs";
import { t as resolveSessionDeliveryTarget } from "./targets-session--uC0H76w.mjs";
import { r as resolveOutboundChannelPlugin } from "./channel-resolution-DjtSMYUG.mjs";
import { a as resolveOutboundSessionRoute } from "./outbound-session-BGK8YYPT.mjs";
import { n as resolveOutboundTarget } from "./targets-CiRFkgHi.mjs";
//#region src/infra/outbound/agent-delivery.ts
function resolveAgentDeliveryPlan(e) {
const t = normalizeOptionalString(e.requestedChannel) ?? "";
const n = (t ? normalizeMessageChannel(t) : void 0) || "last";
const r = normalizeOptionalString(e.explicitTo);
const i = e.turnSourceChannel ? normalizeMessageChannel(e.turnSourceChannel) : void 0;
const a = i && isDeliverableMessageChannel(i) ? i : void 0;
const o = normalizeOptionalString(e.turnSourceTo);
const s = normalizeOptionalAccountId(e.turnSourceAccountId);
const c = e.turnSourceThreadId != null && e.turnSourceThreadId !== "" ? e.turnSourceThreadId : void 0;
const l = resolveSessionDeliveryTarget({
entry: e.sessionEntry,
requestedChannel: n === "webchat" ? "last" : n,
explicitTo: r,
explicitThreadId: e.explicitThreadId,
turnSourceChannel: a,
turnSourceTo: o,
turnSourceAccountId: s,
turnSourceThreadId: c
});
const u = n === "webchat" ? INTERNAL_MESSAGE_CHANNEL : n !== "last" && isGatewayMessageChannel(n) ? n : l.channel || "webchat";
const d = r ? "explicit" : isDeliverableMessageChannel(u) ? "implicit" : void 0;
const f = normalizeOptionalAccountId(e.accountId) ?? (d === "implicit" ? l.accountId : void 0);
let p = r;
if (!p && isDeliverableMessageChannel(u) && u === l.lastChannel) p = l.lastTo;
return {
baseDelivery: l,
resolvedChannel: u,
resolvedTo: p,
resolvedAccountId: f,
resolvedThreadId: l.threadId,
deliveryTargetMode: d
};
}
async function resolveAgentDeliveryPlanWithSessionRoute(e) {
await waitForAgentDatabaseWriteAdmission(e.agentId);
assertAgentDatabaseAdmitted(e.agentId);
const t = resolveAgentDeliveryPlan(e);
const { resolvedChannel: n } = t;
if (!e.wantsDelivery || !isDeliverableMessageChannel(n)) return t;
const r = e.preparedPlugin ?? resolveOutboundChannelPlugin({
channel: n,
cfg: e.cfg,
agentId: e.agentId,
allowBootstrap: true
});
if (!r) return t;
t.plugin = r;
const i = Boolean(r.messaging?.resolveOutboundSessionRoute);
const a = Boolean(r.messaging?.targetResolver);
const o = Boolean(r.messaging?.targetResolver?.resolveTarget);
if (!i && !a && e.sessionRouteMode !== "allow-fallback") return t;
t.resolvedAccountId ??= e.sessionRouteMode === "allow-fallback" ? resolveChannelDefaultAccountId({
plugin: r,
cfg: e.cfg
}) : void 0;
const s = await resolveOutboundTarget({
channel: n,
plugin: r,
to: t.resolvedTo,
cfg: e.cfg,
accountId: t.resolvedAccountId,
mode: t.deliveryTargetMode ?? "explicit"
});
const c = s.ok ? s.to : t.resolvedTo;
if (!c) return s.ok ? t : {
...t,
targetResolutionError: s.error
};
const l = await resolveChannelTarget({
cfg: e.cfg,
channel: n,
input: c,
accountId: t.resolvedAccountId,
unknownTargetMode: o ? "error" : "normalized",
plugin: r
});
if (!l.ok) return {
...t,
targetResolutionError: l.error
};
if (!s.ok && l.target.resolutionSource === "normalized") return {
...t,
targetResolutionError: s.error
};
const u = l.target.to;
const d = !s.ok || s.to !== l.target.to || l.target.resolutionSource === "directory" ? l.target : void 0;
t.resolvedTo = u;
if (!i && e.sessionRouteMode !== "allow-fallback") return t;
const f = e.explicitThreadId != null && e.explicitThreadId !== "" ? e.explicitThreadId : void 0;
const p = t.deliveryTargetMode === "explicit" ? f : t.resolvedThreadId;
const m = await resolveOutboundSessionRoute({
cfg: e.cfg,
channel: n,
plugin: r,
agentId: e.agentId,
accountId: t.resolvedAccountId,
target: u,
...d ? { resolvedTarget: d } : {},
currentSessionKey: e.currentSessionKey,
threadId: p
}).catch(() => null);
const h = e.cfg.session?.dmScope ?? "main";
const g = e.sessionRouteMode === "allow-fallback" && (m?.recipientSessionExact === false || m?.recipientSessionExact === "direct-alias");
const _ = buildAgentMainSessionKey({
agentId: e.agentId,
mainKey: e.cfg.session?.mainKey
});
const v = m?.recipientSessionExact === "direct-alias" && m.chatType === "direct" && m.sessionKey === m.baseSessionKey && m.sessionKey === _ && h === "main" && !listConfiguredBindings(e.cfg).some((e) => isRouteBinding(e) && e.session?.dmScope !== void 0 && e.session.dmScope !== "main" && normalizeRouteBindingChannelId(e.match.channel) === n);
const y = m?.recipientSessionExact === "delivery-identity" && m.baseSessionKey !== _ && m.baseSessionKey.startsWith(`agent:${normalizeAgentId(e.agentId)}:${n}:`) && (m.sessionKey === m.baseSessionKey || m.sessionKey.startsWith(`${m.baseSessionKey}:`));
const b = m && (m.recipientSessionExact === "delivery-identity" ? y : !g || v) ? m : null;
if (!b) return d ? {
...t,
resolvedTo: d.to,
resolvedThreadId: p
} : t;
return {
...t,
resolvedSessionKey: b.sessionKey,
resolvedTo: i ? b.to : d?.to ?? u,
resolvedThreadId: b.threadId ?? p
};
}
/** Resolves an explicit recipient into its canonical or stable provider-owned session. */
async function resolveAgentExplicitRecipientSession(e) {
const t = await resolveAgentDeliveryPlanWithSessionRoute({
cfg: e.cfg,
agentId: e.agentId,
requestedChannel: e.channel,
explicitTo: e.to,
explicitThreadId: e.threadId,
accountId: e.accountId,
wantsDelivery: true,
sessionRouteMode: "allow-fallback"
});
if (!t.resolvedSessionKey && !t.targetResolutionError) return { error: /* @__PURE__ */ new Error(`Unable to resolve a session route for channel "${e.channel}"`) };
return {
sessionKey: t.resolvedSessionKey,
channel: t.resolvedChannel,
to: t.resolvedTo,
accountId: t.resolvedAccountId,
threadId: t.resolvedThreadId,
error: t.targetResolutionError
};
}
async function resolveAgentOutboundTarget(e) {
const t = e.targetMode ?? e.plan.deliveryTargetMode ?? (e.plan.resolvedTo ? "explicit" : "implicit");
if (e.plan.targetResolutionError) return {
resolvedTarget: {
ok: false,
error: e.plan.targetResolutionError
},
resolvedTo: void 0
};
if (!isDeliverableMessageChannel(e.plan.resolvedChannel) || e.validateExplicitTarget !== true && e.plan.resolvedTo) return {
resolvedTarget: null,
resolvedTo: e.plan.resolvedTo
};
const n = await resolveOutboundTarget({
channel: e.plan.resolvedChannel,
...e.plan.plugin ? { plugin: e.plan.plugin } : {},
to: e.plan.resolvedTo,
cfg: e.cfg,
accountId: e.plan.resolvedAccountId,
mode: t
});
return {
resolvedTarget: n,
resolvedTo: n.ok ? n.to : e.plan.resolvedTo
};
}
//#endregion
export { resolveAgentExplicitRecipientSession as n, resolveAgentOutboundTarget as r, resolveAgentDeliveryPlanWithSessionRoute as t };