




















@@ -105,6 +105,33 @@ function loadReplyMediaPathsRuntime() {
105105return replyMediaPathsRuntimePromise;
106106}
107107108+function isSystemEventProvider(provider?: string): boolean {
109+return provider === "heartbeat" || provider === "cron-event" || provider === "exec-event";
110+}
111+112+function resolveEffectiveReplyRoute(params: {
113+ctx: Pick<FinalizedMsgContext, "Provider" | "OriginatingChannel" | "OriginatingTo" | "AccountId">;
114+entry?: Pick<SessionEntry, "deliveryContext" | "lastChannel" | "lastTo" | "lastAccountId">;
115+}): { channel?: string; to?: string; accountId?: string } {
116+if (!isSystemEventProvider(params.ctx.Provider)) {
117+return {
118+channel: params.ctx.OriginatingChannel,
119+to: params.ctx.OriginatingTo,
120+accountId: params.ctx.AccountId,
121+};
122+}
123+const persistedDeliveryContext = params.entry?.deliveryContext;
124+return {
125+channel:
126+params.ctx.OriginatingChannel ??
127+persistedDeliveryContext?.channel ??
128+params.entry?.lastChannel,
129+to: params.ctx.OriginatingTo ?? persistedDeliveryContext?.to ?? params.entry?.lastTo,
130+accountId:
131+params.ctx.AccountId ?? persistedDeliveryContext?.accountId ?? params.entry?.lastAccountId,
132+};
133+}
134+108135async function maybeApplyTtsToReplyPayload(
109136params: Parameters<Awaited<ReturnType<typeof loadTtsRuntime>>["maybeApplyTtsToPayload"]>[0],
110137) {
@@ -287,18 +314,7 @@ export async function dispatchReplyFromConfig(
287314"",
288315) ?? "off",
289316});
290-const isSystemEventTurn =
291-ctx.Provider === "heartbeat" || ctx.Provider === "cron-event" || ctx.Provider === "exec-event";
292-const persistedDeliveryContext = sessionStoreEntry.entry?.deliveryContext;
293-const fallbackOriginatingChannel = isSystemEventTurn
294- ? (persistedDeliveryContext?.channel ?? sessionStoreEntry.entry?.lastChannel)
295- : undefined;
296-const fallbackOriginatingTo = isSystemEventTurn
297- ? (persistedDeliveryContext?.to ?? sessionStoreEntry.entry?.lastTo)
298- : undefined;
299-const fallbackOriginatingAccountId = isSystemEventTurn
300- ? (persistedDeliveryContext?.accountId ?? sessionStoreEntry.entry?.lastAccountId)
301- : undefined;
317+const effectiveReplyRoute = resolveEffectiveReplyRoute({ ctx, entry: sessionStoreEntry.entry });
302318// Restore route thread context only from the active turn or the thread-scoped session key.
303319// Do not read thread ids from the normalised session store here: `origin.threadId` can be
304320// folded back into lastThreadId/deliveryContext during store normalisation and resurrect a
@@ -331,10 +347,7 @@ export async function dispatchReplyFromConfig(
331347//
332348// Debug: `pnpm test src/auto-reply/reply/dispatch-from-config.test.ts`
333349const suppressAcpChildUserDelivery = isParentOwnedBackgroundAcpSession(sessionStoreEntry.entry);
334-const effectiveOriginatingChannel = ctx.OriginatingChannel ?? fallbackOriginatingChannel;
335-const effectiveOriginatingTo = ctx.OriginatingTo ?? fallbackOriginatingTo;
336-const routeAccountId = ctx.AccountId ?? fallbackOriginatingAccountId;
337-const normalizedOriginatingChannel = normalizeMessageChannel(effectiveOriginatingChannel);
350+const normalizedOriginatingChannel = normalizeMessageChannel(effectiveReplyRoute.channel);
338351const normalizedProviderChannel = normalizeMessageChannel(ctx.Provider);
339352const normalizedSurfaceChannel = normalizeMessageChannel(ctx.Surface);
340353const normalizedCurrentSurface = normalizedProviderChannel ?? normalizedSurfaceChannel;
@@ -346,7 +359,7 @@ export async function dispatchReplyFromConfig(
346359!suppressAcpChildUserDelivery &&
347360!isInternalWebchatTurn &&
348361normalizedOriginatingChannel &&
349-effectiveOriginatingTo &&
362+effectiveReplyRoute.to &&
350363normalizedOriginatingChannel !== normalizedCurrentSurface,
351364);
352365const routeReplyRuntime = hasRouteReplyCandidate ? await loadRouteReplyRuntime() : undefined;
@@ -355,20 +368,20 @@ export async function dispatchReplyFromConfig(
355368provider: ctx.Provider,
356369surface: ctx.Surface,
357370explicitDeliverRoute: ctx.ExplicitDeliverRoute,
358-originatingChannel: effectiveOriginatingChannel,
359-originatingTo: effectiveOriginatingTo,
371+originatingChannel: effectiveReplyRoute.channel,
372+originatingTo: effectiveReplyRoute.to,
360373suppressDirectUserDelivery: suppressAcpChildUserDelivery,
361374isRoutableChannel: routeReplyRuntime?.isRoutableChannel ?? (() => false),
362375});
363-const originatingTo = effectiveOriginatingTo;
376+const originatingTo = effectiveReplyRoute.to;
364377const ttsChannel = shouldRouteToOriginating ? originatingChannel : currentSurface;
365378const { createReplyMediaPathNormalizer } = await loadReplyMediaPathsRuntime();
366379const normalizeReplyMediaPaths = createReplyMediaPathNormalizer({
367380 cfg,
368381sessionKey: acpDispatchSessionKey,
369382workspaceDir: resolveAgentWorkspaceDir(cfg, sessionAgentId),
370383messageProvider: ttsChannel,
371-accountId: routeAccountId,
384+accountId: effectiveReplyRoute.accountId,
372385 groupId,
373386groupChannel: ctx.GroupChannel,
374387groupSpace: ctx.GroupSpace,
@@ -400,7 +413,7 @@ export async function dispatchReplyFromConfig(
400413ctx.CommandSource === "native"
401414 ? (ctx.CommandTargetSessionKey ?? ctx.SessionKey)
402415 : ctx.SessionKey,
403-accountId: routeAccountId,
416+accountId: effectiveReplyRoute.accountId,
404417requesterSenderId: ctx.SenderId,
405418requesterSenderName: ctx.SenderName,
406419requesterSenderUsername: ctx.SenderUsername,
@@ -487,8 +500,9 @@ export async function dispatchReplyFromConfig(
487500entry: sessionStoreEntry.entry,
488501sessionKey: sessionStoreEntry.sessionKey ?? sessionKey,
489502channel:
503+(shouldRouteToOriginating ? originatingChannel : undefined) ??
490504sessionStoreEntry.entry?.channel ??
491-ctx.OriginatingChannel ??
505+effectiveReplyRoute.channel ??
492506ctx.Surface ??
493507ctx.Provider ??
494508undefined,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。