fix: preserve subagent thread routing (#1241)

Thanks @gnarco.

Co-authored-by: gnarco <gnarco@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-20 17:22:07 +00:00
parent ae1c6f4313
commit 02ca148583
32 changed files with 195 additions and 32 deletions

View File

@@ -22,6 +22,7 @@ export type AgentDeliveryPlan = {
resolvedChannel: GatewayMessageChannel;
resolvedTo?: string;
resolvedAccountId?: string;
resolvedThreadId?: string | number;
deliveryTargetMode?: ChannelOutboundTargetMode;
};
@@ -29,6 +30,7 @@ export function resolveAgentDeliveryPlan(params: {
sessionEntry?: SessionEntry;
requestedChannel?: string;
explicitTo?: string;
explicitThreadId?: string | number;
accountId?: string;
wantsDelivery: boolean;
}): AgentDeliveryPlan {
@@ -46,6 +48,7 @@ export function resolveAgentDeliveryPlan(params: {
entry: params.sessionEntry,
requestedChannel: requestedChannel === INTERNAL_MESSAGE_CHANNEL ? "last" : requestedChannel,
explicitTo,
explicitThreadId: params.explicitThreadId,
});
const resolvedChannel = (() => {
@@ -89,6 +92,7 @@ export function resolveAgentDeliveryPlan(params: {
resolvedChannel,
resolvedTo,
resolvedAccountId,
resolvedThreadId: baseDelivery.threadId,
deliveryTargetMode,
};
}

View File

@@ -124,10 +124,12 @@ describe("resolveSessionDeliveryTarget", () => {
channel: "whatsapp",
to: "+1555",
accountId: "acct-1",
threadId: undefined,
mode: "implicit",
lastChannel: "whatsapp",
lastTo: "+1555",
lastAccountId: "acct-1",
lastThreadId: undefined,
});
});
@@ -146,10 +148,12 @@ describe("resolveSessionDeliveryTarget", () => {
channel: "telegram",
to: undefined,
accountId: undefined,
threadId: undefined,
mode: "implicit",
lastChannel: "whatsapp",
lastTo: "+1555",
lastAccountId: undefined,
lastThreadId: undefined,
});
});
@@ -169,10 +173,12 @@ describe("resolveSessionDeliveryTarget", () => {
channel: "telegram",
to: "+1555",
accountId: undefined,
threadId: undefined,
mode: "implicit",
lastChannel: "whatsapp",
lastTo: "+1555",
lastAccountId: undefined,
lastThreadId: undefined,
});
});
@@ -192,10 +198,12 @@ describe("resolveSessionDeliveryTarget", () => {
channel: "slack",
to: undefined,
accountId: undefined,
threadId: undefined,
mode: "implicit",
lastChannel: "whatsapp",
lastTo: "+1555",
lastAccountId: undefined,
lastThreadId: undefined,
});
});
});

View File

@@ -35,16 +35,19 @@ export type SessionDeliveryTarget = {
channel?: DeliverableMessageChannel;
to?: string;
accountId?: string;
threadId?: string | number;
mode: ChannelOutboundTargetMode;
lastChannel?: DeliverableMessageChannel;
lastTo?: string;
lastAccountId?: string;
lastThreadId?: string | number;
};
export function resolveSessionDeliveryTarget(params: {
entry?: SessionEntry;
requestedChannel?: GatewayMessageChannel | "last";
explicitTo?: string;
explicitThreadId?: string | number;
fallbackChannel?: DeliverableMessageChannel;
allowMismatchedLastTo?: boolean;
mode?: ChannelOutboundTargetMode;
@@ -54,6 +57,7 @@ export function resolveSessionDeliveryTarget(params: {
context?.channel && isDeliverableMessageChannel(context.channel) ? context.channel : undefined;
const lastTo = context?.to;
const lastAccountId = context?.accountId;
const lastThreadId = context?.threadId;
const rawRequested = params.requestedChannel ?? "last";
const requested = rawRequested === "last" ? "last" : normalizeMessageChannel(rawRequested);
@@ -68,6 +72,10 @@ export function resolveSessionDeliveryTarget(params: {
typeof params.explicitTo === "string" && params.explicitTo.trim()
? params.explicitTo.trim()
: undefined;
const explicitThreadId =
params.explicitThreadId != null && params.explicitThreadId !== ""
? params.explicitThreadId
: undefined;
let channel = requestedChannel === "last" ? lastChannel : requestedChannel;
if (!channel && params.fallbackChannel && isDeliverableMessageChannel(params.fallbackChannel)) {
@@ -84,16 +92,19 @@ export function resolveSessionDeliveryTarget(params: {
}
const accountId = channel && channel === lastChannel ? lastAccountId : undefined;
const threadId = channel && channel === lastChannel ? lastThreadId : undefined;
const mode = params.mode ?? (explicitTo ? "explicit" : "implicit");
return {
channel,
to,
accountId,
threadId: explicitThreadId ?? threadId,
mode,
lastChannel,
lastTo,
lastAccountId,
lastThreadId,
};
}