mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-30 11:46:26 +00:00
refactor(discord): extract native command session targets
This commit is contained in:
37
src/discord/monitor/native-command-session-targets.test.ts
Normal file
37
src/discord/monitor/native-command-session-targets.test.ts
Normal 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",
|
||||
});
|
||||
});
|
||||
});
|
||||
22
src/discord/monitor/native-command-session-targets.ts
Normal file
22
src/discord/monitor/native-command-session-targets.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user