@@ -52,8 +52,7 @@ import { createSubsystemLogger } from "../../logging/subsystem.js";
5252import { getAgentScopedMediaLocalRoots } from "../../media/local-roots.js" ;
5353import { buildPairingReply } from "../../pairing/pairing-messages.js" ;
5454import { executePluginCommand , matchPluginCommand } from "../../plugins/commands.js" ;
55- import { resolveAgentRoute } from "../../routing/resolve-route.js" ;
56- import { resolveAgentIdFromSessionKey } from "../../routing/session-key.js" ;
55+ import type { ResolvedAgentRoute } from "../../routing/resolve-route.js" ;
5756import { buildUntrustedChannelMetadata } from "../../security/channel-metadata.js" ;
5857import { chunkItems } from "../../utils/chunk-items.js" ;
5958import { withTimeout } from "../../utils/with-timeout.js" ;
@@ -86,6 +85,11 @@ import {
8685 toDiscordModelPickerMessagePayload ,
8786 type DiscordModelPickerCommandContext ,
8887} from "./model-picker.js" ;
88+ import {
89+ buildDiscordRoutePeer ,
90+ resolveDiscordConversationRoute ,
91+ resolveDiscordEffectiveRoute ,
92+ } from "./route-resolution.js" ;
8993import { resolveDiscordSenderIdentity } from "./sender-identity.js" ;
9094import type { ThreadBindingManager } from "./thread-bindings.js" ;
9195import { resolveDiscordThreadParentInfo } from "./threading.js" ;
@@ -448,36 +452,32 @@ async function resolveDiscordModelPickerRoute(params: {
448452 threadParentId = parentInfo . id ;
449453 }
450454
451- const route = resolveAgentRoute ( {
455+ const route = resolveDiscordConversationRoute ( {
452456 cfg,
453- channel : "discord" ,
454457 accountId,
455458 guildId : interaction . guild ?. id ?? undefined ,
456459 memberRoleIds,
457- peer : {
458- kind : isDirectMessage ? "direct" : isGroupDm ? "group" : "channel" ,
459- id : isDirectMessage ? ( interaction . user ?. id ?? rawChannelId ) : rawChannelId ,
460- } ,
461- parentPeer : threadParentId ? { kind : "channel" , id : threadParentId } : undefined ,
460+ peer : buildDiscordRoutePeer ( {
461+ isDirectMessage,
462+ isGroupDm,
463+ directUserId : interaction . user ?. id ?? rawChannelId ,
464+ conversationId : rawChannelId ,
465+ } ) ,
466+ parentConversationId : threadParentId ,
462467 } ) ;
463468
464469 const threadBinding = isThreadChannel
465470 ? params . threadBindings . getByThreadId ( rawChannelId )
466471 : undefined ;
467- const boundSessionKey = threadBinding ?. targetSessionKey ?. trim ( ) ;
468- const boundAgentId = boundSessionKey ? resolveAgentIdFromSessionKey ( boundSessionKey ) : undefined ;
469- return boundSessionKey
470- ? {
471- ...route ,
472- sessionKey : boundSessionKey ,
473- agentId : boundAgentId ?? route . agentId ,
474- }
475- : route ;
472+ return resolveDiscordEffectiveRoute ( {
473+ route,
474+ boundSessionKey : threadBinding ?. targetSessionKey ,
475+ } ) ;
476476}
477477
478478function resolveDiscordModelPickerCurrentModel ( params : {
479479 cfg : ReturnType < typeof loadConfig > ;
480- route : ReturnType < typeof resolveAgentRoute > ;
480+ route : ResolvedAgentRoute ;
481481 data : Awaited < ReturnType < typeof loadDiscordModelPickerData > > ;
482482} ) : string {
483483 const fallback = buildDiscordModelPickerCurrentModel (
@@ -1606,17 +1606,18 @@ async function dispatchDiscordCommandInteraction(params: {
16061606 const isGuild = Boolean ( interaction . guild ) ;
16071607 const channelId = rawChannelId || "unknown" ;
16081608 const interactionId = interaction . rawData . id ;
1609- const route = resolveAgentRoute ( {
1609+ const route = resolveDiscordConversationRoute ( {
16101610 cfg,
1611- channel : "discord" ,
16121611 accountId,
16131612 guildId : interaction . guild ?. id ?? undefined ,
16141613 memberRoleIds,
1615- peer : {
1616- kind : isDirectMessage ? "direct" : isGroupDm ? "group" : "channel" ,
1617- id : isDirectMessage ? user . id : channelId ,
1618- } ,
1619- parentPeer : threadParentId ? { kind : "channel" , id : threadParentId } : undefined ,
1614+ peer : buildDiscordRoutePeer ( {
1615+ isDirectMessage,
1616+ isGroupDm,
1617+ directUserId : user . id ,
1618+ conversationId : channelId ,
1619+ } ) ,
1620+ parentConversationId : threadParentId ,
16201621 } ) ;
16211622 const threadBinding = isThreadChannel ? threadBindings . getByThreadId ( rawChannelId ) : undefined ;
16221623 const configuredRoute =
@@ -1646,15 +1647,12 @@ async function dispatchDiscordCommandInteraction(params: {
16461647 }
16471648 const configuredBoundSessionKey = configuredRoute ?. boundSessionKey ?. trim ( ) || undefined ;
16481649 const boundSessionKey = threadBinding ?. targetSessionKey ?. trim ( ) || configuredBoundSessionKey ;
1649- const boundAgentId = boundSessionKey ? resolveAgentIdFromSessionKey ( boundSessionKey ) : undefined ;
1650- const effectiveRoute = boundSessionKey
1651- ? {
1652- ...route ,
1653- sessionKey : boundSessionKey ,
1654- agentId : boundAgentId ?? route . agentId ,
1655- ...( configuredBinding ? { matchedBy : "binding.channel" as const } : { } ) ,
1656- }
1657- : ( configuredRoute ?. route ?? route ) ;
1650+ const effectiveRoute = resolveDiscordEffectiveRoute ( {
1651+ route,
1652+ boundSessionKey,
1653+ configuredRoute,
1654+ matchedBy : configuredBinding ? "binding.channel" : undefined ,
1655+ } ) ;
16581656 const conversationLabel = isDirectMessage ? ( user . globalName ?? user . username ) : channelId ;
16591657 const ownerAllowFrom = resolveDiscordOwnerAllowFrom ( {
16601658 channelConfig,
0 commit comments