refactor(slack): dedupe slash reply delivery

This commit is contained in:
Peter Steinberger
2026-02-16 01:35:18 +00:00
parent a881bd41eb
commit 1d7b2bc9c8

View File

@@ -1,5 +1,6 @@
import type { SlackActionMiddlewareArgs, SlackCommandMiddlewareArgs } from "@slack/bolt"; import type { SlackActionMiddlewareArgs, SlackCommandMiddlewareArgs } from "@slack/bolt";
import type { ChatCommandDefinition, CommandArgs } from "../../auto-reply/commands-registry.js"; import type { ChatCommandDefinition, CommandArgs } from "../../auto-reply/commands-registry.js";
import type { ReplyPayload } from "../../auto-reply/types.js";
import type { ResolvedSlackAccount } from "../accounts.js"; import type { ResolvedSlackAccount } from "../accounts.js";
import type { SlackMonitorContext } from "./context.js"; import type { SlackMonitorContext } from "./context.js";
import { formatAllowlistMatchMeta } from "../../channels/allowlist-match.js"; import { formatAllowlistMatchMeta } from "../../channels/allowlist-match.js";
@@ -449,23 +450,15 @@ export async function registerSlackMonitorSlashCommands(params: {
accountId: route.accountId, accountId: route.accountId,
}); });
const { counts } = await dispatchReplyWithDispatcher({ const deliverSlashPayloads = async (replies: ReplyPayload[]) => {
ctx: ctxPayload, const [{ deliverSlackSlashReplies }, { resolveChunkMode }, { resolveMarkdownTableMode }] =
cfg, await Promise.all([
dispatcherOptions: {
...prefixOptions,
deliver: async (payload) => {
const [
{ deliverSlackSlashReplies },
{ resolveChunkMode },
{ resolveMarkdownTableMode },
] = await Promise.all([
import("./replies.js"), import("./replies.js"),
import("../../auto-reply/chunk.js"), import("../../auto-reply/chunk.js"),
import("../../config/markdown-tables.js"), import("../../config/markdown-tables.js"),
]); ]);
await deliverSlackSlashReplies({ await deliverSlackSlashReplies({
replies: [payload], replies,
respond, respond,
ephemeral: slashCommand.ephemeral, ephemeral: slashCommand.ephemeral,
textLimit: ctx.textLimit, textLimit: ctx.textLimit,
@@ -476,7 +469,14 @@ export async function registerSlackMonitorSlashCommands(params: {
accountId: route.accountId, accountId: route.accountId,
}), }),
}); });
}, };
const { counts } = await dispatchReplyWithDispatcher({
ctx: ctxPayload,
cfg,
dispatcherOptions: {
...prefixOptions,
deliver: async (payload) => deliverSlashPayloads([payload]),
onError: (err, info) => { onError: (err, info) => {
runtime.error?.(danger(`slack slash ${info.kind} reply failed: ${String(err)}`)); runtime.error?.(danger(`slack slash ${info.kind} reply failed: ${String(err)}`));
}, },
@@ -487,24 +487,7 @@ export async function registerSlackMonitorSlashCommands(params: {
}, },
}); });
if (counts.final + counts.tool + counts.block === 0) { if (counts.final + counts.tool + counts.block === 0) {
const [{ deliverSlackSlashReplies }, { resolveChunkMode }, { resolveMarkdownTableMode }] = await deliverSlashPayloads([]);
await Promise.all([
import("./replies.js"),
import("../../auto-reply/chunk.js"),
import("../../config/markdown-tables.js"),
]);
await deliverSlackSlashReplies({
replies: [],
respond,
ephemeral: slashCommand.ephemeral,
textLimit: ctx.textLimit,
chunkMode: resolveChunkMode(cfg, "slack", route.accountId),
tableMode: resolveMarkdownTableMode({
cfg,
channel: "slack",
accountId: route.accountId,
}),
});
} }
} catch (err) { } catch (err) {
runtime.error?.(danger(`slack slash handler failed: ${String(err)}`)); runtime.error?.(danger(`slack slash handler failed: ${String(err)}`));