Skip to content

Commit 7fc1089

Browse files
steipetePeter Steinberger
andauthored
fix(slack): unify slash command registration ownership (#118735)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent df0cf76 commit 7fc1089

2 files changed

Lines changed: 43 additions & 25 deletions

File tree

‎extensions/slack/src/monitor/slash.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,9 @@ describe("Slack native command argument menus", () => {
839839
slashCommand: { enabled: boolean };
840840
}
841841
).slashCommand.enabled = true;
842-
await registerCommands(configuredHarness.ctx, configuredHarness.account);
842+
const registration = await registerCommands(configuredHarness.ctx, configuredHarness.account);
843843

844+
expect(registration).toEqual({ mode: "single", name: "openclaw" });
844845
expect(
845846
[...configuredHarness.commands.keys()].some(
846847
(command) => command instanceof RegExp && command.test("/openclaw"),
@@ -849,6 +850,24 @@ describe("Slack native command argument menus", () => {
849850
expect(configuredHarness.commands.has("/usage")).toBe(false);
850851
});
851852

853+
it("does not register native argument handlers for a configured slash command", async () => {
854+
const configuredHarness = createArgMenusHarness();
855+
const slashCommand = (
856+
configuredHarness.ctx as {
857+
slashCommand: { enabled: boolean; name: string };
858+
}
859+
).slashCommand;
860+
slashCommand.enabled = true;
861+
slashCommand.name = "acme";
862+
863+
await expect(
864+
registerCommands(configuredHarness.ctx, configuredHarness.account),
865+
).resolves.toEqual({ mode: "single", name: "acme" });
866+
867+
expect(configuredHarness.actions.size).toBe(0);
868+
expect(configuredHarness.options.size).toBe(0);
869+
});
870+
852871
it("registers options handlers without losing app receiver binding", async () => {
853872
const testHarness = createArgMenusHarness();
854873
await registerCommands(testHarness.ctx, testHarness.account);

‎extensions/slack/src/monitor/slash.ts‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ export async function registerSlackMonitorSlashCommands(params: {
387387
const slashCommand = resolveSlackSlashCommandConfig(
388388
ctx.slashCommand ?? account.config.slashCommand,
389389
);
390+
// App Home and argument handlers must share the registered command mode;
391+
// explicit single-command mode also avoids loading inactive native runtimes.
392+
let registration: SlackCommandRegistration = slashCommand.enabled
393+
? { mode: "single", name: slashCommand.name }
394+
: { mode: "disabled" };
390395

391396
const handleSlashCommand = async (p: {
392397
command: SlackCommandMiddlewareArgs["command"];
@@ -872,22 +877,22 @@ export async function registerSlackMonitorSlashCommands(params: {
872877
}
873878
};
874879

875-
const nativeEnabled = resolveNativeCommandsEnabled({
876-
providerId: "slack",
877-
providerSetting: account.config.commands?.native,
878-
globalSetting: startupCfg.commands?.native,
879-
});
880-
const nativeSkillsEnabled = resolveNativeSkillsEnabled({
881-
providerId: "slack",
882-
providerSetting: account.config.commands?.nativeSkills,
883-
globalSetting: startupCfg.commands?.nativeSkills,
884-
});
885-
886880
let nativeCommands: Array<{ name: string }> = [];
887881
let slashCommandsRuntime: typeof import("./slash-commands.runtime.js") | null = null;
888-
if (nativeEnabled) {
882+
if (
883+
registration.mode === "disabled" &&
884+
resolveNativeCommandsEnabled({
885+
providerId: "slack",
886+
providerSetting: account.config.commands?.native,
887+
globalSetting: startupCfg.commands?.native,
888+
})
889+
) {
889890
slashCommandsRuntime = await loadSlashCommandsRuntime();
890-
const skillCommands = nativeSkillsEnabled
891+
const skillCommands = resolveNativeSkillsEnabled({
892+
providerId: "slack",
893+
providerSetting: account.config.commands?.nativeSkills,
894+
globalSetting: startupCfg.commands?.nativeSkills,
895+
})
891896
? (await loadSlashSkillCommandsRuntime()).listSkillCommandsForAgents({ cfg: startupCfg })
892897
: [];
893898
nativeCommands = slashCommandsRuntime.listNativeCommandSpecsForConfig(startupCfg, {
@@ -906,11 +911,12 @@ export async function registerSlackMonitorSlashCommands(params: {
906911
existingNativeNames.add(normalizedName);
907912
nativeCommands.push(pluginCommand);
908913
}
914+
registration = nativeCommands.length > 0 ? { mode: "native" } : { mode: "disabled" };
909915
}
910916

911-
if (slashCommand.enabled) {
917+
if (registration.mode === "single") {
912918
ctx.app.command(
913-
buildSlackSlashCommandMatcher(slashCommand.name),
919+
buildSlackSlashCommandMatcher(registration.name),
914920
async ({ command, ack, respond, body }: SlackCommandMiddlewareArgs) => {
915921
await handleSlashCommand({
916922
command,
@@ -921,7 +927,7 @@ export async function registerSlackMonitorSlashCommands(params: {
921927
});
922928
},
923929
);
924-
} else if (nativeCommands.length > 0) {
930+
} else if (registration.mode === "native") {
925931
if (!slashCommandsRuntime) {
926932
throw new Error("Missing commands runtime for native Slack commands.");
927933
}
@@ -960,14 +966,7 @@ export async function registerSlackMonitorSlashCommands(params: {
960966
logVerbose("slack: slash commands disabled");
961967
}
962968

963-
const registration: SlackCommandRegistration =
964-
nativeCommands.length > 0
965-
? { mode: "native" }
966-
: slashCommand.enabled
967-
? { mode: "single", name: slashCommand.name }
968-
: { mode: "disabled" };
969-
970-
if (nativeCommands.length === 0 || !supportsInteractiveArgMenus) {
969+
if (registration.mode !== "native" || !supportsInteractiveArgMenus) {
971970
return registration;
972971
}
973972

0 commit comments

Comments
 (0)