refactor: reuse dispatcher helper for native commands

This commit is contained in:
Peter Steinberger
2026-01-13 01:07:50 +00:00
parent 92760de472
commit dd8f7552ad
3 changed files with 66 additions and 53 deletions

View File

@@ -4,7 +4,9 @@ import type { GetReplyOptions } from "../types.js";
import type { DispatchFromConfigResult } from "./dispatch-from-config.js"; import type { DispatchFromConfigResult } from "./dispatch-from-config.js";
import { dispatchReplyFromConfig } from "./dispatch-from-config.js"; import { dispatchReplyFromConfig } from "./dispatch-from-config.js";
import { import {
createReplyDispatcher,
createReplyDispatcherWithTyping, createReplyDispatcherWithTyping,
type ReplyDispatcherOptions,
type ReplyDispatcherWithTypingOptions, type ReplyDispatcherWithTypingOptions,
} from "./reply-dispatcher.js"; } from "./reply-dispatcher.js";
@@ -32,3 +34,24 @@ export async function dispatchReplyWithBufferedBlockDispatcher(params: {
markDispatchIdle(); markDispatchIdle();
return result; return result;
} }
export async function dispatchReplyWithDispatcher(params: {
ctx: MsgContext;
cfg: ClawdbotConfig;
dispatcherOptions: ReplyDispatcherOptions;
replyOptions?: Omit<GetReplyOptions, "onToolResult" | "onBlockReply">;
replyResolver?: typeof import("../reply.js").getReplyFromConfig;
}): Promise<DispatchFromConfigResult> {
const dispatcher = createReplyDispatcher(params.dispatcherOptions);
const result = await dispatchReplyFromConfig({
ctx: params.ctx,
cfg: params.cfg,
dispatcher,
replyResolver: params.replyResolver,
replyOptions: params.replyOptions,
});
await dispatcher.waitForIdle();
return result;
}

View File

@@ -46,10 +46,8 @@ import {
buildMentionRegexes, buildMentionRegexes,
matchesMentionPatterns, matchesMentionPatterns,
} from "../auto-reply/reply/mentions.js"; } from "../auto-reply/reply/mentions.js";
import { import { dispatchReplyWithDispatcher } from "../auto-reply/reply/provider-dispatcher.js";
createReplyDispatcher, import { createReplyDispatcherWithTyping } from "../auto-reply/reply/reply-dispatcher.js";
createReplyDispatcherWithTyping,
} from "../auto-reply/reply/reply-dispatcher.js";
import { createReplyReferencePlanner } from "../auto-reply/reply/reply-reference.js"; import { createReplyReferencePlanner } from "../auto-reply/reply/reply-reference.js";
import type { ReplyPayload } from "../auto-reply/types.js"; import type { ReplyPayload } from "../auto-reply/types.js";
import { import {
@@ -1832,7 +1830,10 @@ export function createDiscordNativeCommand(params: {
}; };
let didReply = false; let didReply = false;
const dispatcher = createReplyDispatcher({ await dispatchReplyWithDispatcher({
ctx: ctxPayload,
cfg,
dispatcherOptions: {
responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId) responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId)
.responsePrefix, .responsePrefix,
humanDelay: resolveHumanDelayConfig(cfg, route.agentId), humanDelay: resolveHumanDelayConfig(cfg, route.agentId),
@@ -1851,12 +1852,7 @@ export function createDiscordNativeCommand(params: {
onError: (err, info) => { onError: (err, info) => {
console.error(`discord slash ${info.kind} reply failed`, err); console.error(`discord slash ${info.kind} reply failed`, err);
}, },
}); },
await dispatchReplyFromConfig({
ctx: ctxPayload,
cfg,
dispatcher,
replyOptions: { replyOptions: {
skillFilter: channelConfig?.skills, skillFilter: channelConfig?.skills,
disableBlockStreaming: disableBlockStreaming:
@@ -1865,7 +1861,6 @@ export function createDiscordNativeCommand(params: {
: undefined, : undefined,
}, },
}); });
await dispatcher.waitForIdle();
} }
})(); })();
} }

View File

@@ -34,10 +34,8 @@ import {
buildMentionRegexes, buildMentionRegexes,
matchesMentionPatterns, matchesMentionPatterns,
} from "../auto-reply/reply/mentions.js"; } from "../auto-reply/reply/mentions.js";
import { import { dispatchReplyWithDispatcher } from "../auto-reply/reply/provider-dispatcher.js";
createReplyDispatcher, import { createReplyDispatcherWithTyping } from "../auto-reply/reply/reply-dispatcher.js";
createReplyDispatcherWithTyping,
} from "../auto-reply/reply/reply-dispatcher.js";
import { createReplyReferencePlanner } from "../auto-reply/reply/reply-reference.js"; import { createReplyReferencePlanner } from "../auto-reply/reply/reply-reference.js";
import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js"; import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
import type { ReplyPayload } from "../auto-reply/types.js"; import type { ReplyPayload } from "../auto-reply/types.js";
@@ -1923,7 +1921,10 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
OriginatingTo: `user:${command.user_id}`, OriginatingTo: `user:${command.user_id}`,
}; };
const dispatcher = createReplyDispatcher({ const { counts } = await dispatchReplyWithDispatcher({
ctx: ctxPayload,
cfg,
dispatcherOptions: {
responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId) responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId)
.responsePrefix, .responsePrefix,
deliver: async (payload) => { deliver: async (payload) => {
@@ -1939,15 +1940,9 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
danger(`slack slash ${info.kind} reply failed: ${String(err)}`), danger(`slack slash ${info.kind} reply failed: ${String(err)}`),
); );
}, },
}); },
const { counts } = await dispatchReplyFromConfig({
ctx: ctxPayload,
cfg,
dispatcher,
replyOptions: { skillFilter: channelConfig?.skills }, replyOptions: { skillFilter: channelConfig?.skills },
}); });
await dispatcher.waitForIdle();
if (counts.final + counts.tool + counts.block === 0) { if (counts.final + counts.tool + counts.block === 0) {
await deliverSlackSlashReplies({ await deliverSlackSlashReplies({
replies: [], replies: [],