refactor(discord): extract native command session targets

This commit is contained in:
Peter Steinberger
2026-03-08 01:15:56 +00:00
parent 9d10697227
commit 8f719e541a
3 changed files with 68 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
import { describe, expect, it } from "vitest";
import { resolveDiscordNativeCommandSessionTargets } from "./native-command-session-targets.js";
describe("resolveDiscordNativeCommandSessionTargets", () => {
it("uses the bound session for both targets when present", () => {
expect(
resolveDiscordNativeCommandSessionTargets({
boundSessionKey: "agent:codex:acp:binding:discord:default:seed",
effectiveRoute: {
agentId: "codex",
sessionKey: "agent:codex:discord:channel:chan-1",
},
sessionPrefix: "discord:slash",
userId: "user-1",
}),
).toEqual({
sessionKey: "agent:codex:acp:binding:discord:default:seed",
commandTargetSessionKey: "agent:codex:acp:binding:discord:default:seed",
});
});
it("falls back to the routed slash and command target session keys", () => {
expect(
resolveDiscordNativeCommandSessionTargets({
effectiveRoute: {
agentId: "qwen",
sessionKey: "agent:qwen:discord:channel:chan-1",
},
sessionPrefix: "discord:slash",
userId: "user-1",
}),
).toEqual({
sessionKey: "agent:qwen:discord:slash:user-1",
commandTargetSessionKey: "agent:qwen:discord:channel:chan-1",
});
});
});

View File

@@ -0,0 +1,22 @@
export type ResolveDiscordNativeCommandSessionTargetsParams = {
boundSessionKey?: string;
effectiveRoute: {
agentId: string;
sessionKey: string;
};
sessionPrefix: string;
userId: string;
};
export function resolveDiscordNativeCommandSessionTargets(
params: ResolveDiscordNativeCommandSessionTargetsParams,
) {
const sessionKey =
params.boundSessionKey ??
`agent:${params.effectiveRoute.agentId}:${params.sessionPrefix}:${params.userId}`;
const commandTargetSessionKey = params.boundSessionKey ?? params.effectiveRoute.sessionKey;
return {
sessionKey,
commandTargetSessionKey,
};
}

View File

@@ -83,6 +83,7 @@ import {
type DiscordModelPickerCommandContext,
} from "./model-picker.js";
import { buildDiscordNativeCommandContext } from "./native-command-context.js";
import { resolveDiscordNativeCommandSessionTargets } from "./native-command-session-targets.js";
import {
buildDiscordRoutePeer,
resolveDiscordConversationRoute,
@@ -1651,11 +1652,17 @@ async function dispatchDiscordCommandInteraction(params: {
configuredRoute,
matchedBy: configuredBinding ? "binding.channel" : undefined,
});
const { sessionKey, commandTargetSessionKey } = resolveDiscordNativeCommandSessionTargets({
boundSessionKey,
effectiveRoute,
sessionPrefix,
userId: user.id,
});
const ctxPayload = buildDiscordNativeCommandContext({
prompt,
commandArgs,
sessionKey: boundSessionKey ?? `agent:${effectiveRoute.agentId}:${sessionPrefix}:${user.id}`,
commandTargetSessionKey: boundSessionKey ?? effectiveRoute.sessionKey,
sessionKey,
commandTargetSessionKey,
accountId: effectiveRoute.accountId,
interactionId,
channelId,