refactor: centralize reply dispatch

This commit is contained in:
Peter Steinberger
2026-01-06 04:55:00 +01:00
parent 319dd14e8e
commit 13eb9c9ee9
10 changed files with 90 additions and 131 deletions

View File

@@ -0,0 +1,46 @@
import type { ClawdbotConfig } from "../../config/config.js";
import { getReplyFromConfig } from "../reply.js";
import type { MsgContext } from "../templating.js";
import type { GetReplyOptions, ReplyPayload } from "../types.js";
import type { ReplyDispatcher, ReplyDispatchKind } from "./reply-dispatcher.js";
type DispatchFromConfigResult = {
queuedFinal: boolean;
counts: Record<ReplyDispatchKind, number>;
};
export async function dispatchReplyFromConfig(params: {
ctx: MsgContext;
cfg: ClawdbotConfig;
dispatcher: ReplyDispatcher;
replyOptions?: Omit<GetReplyOptions, "onToolResult" | "onBlockReply">;
replyResolver?: typeof getReplyFromConfig;
}): Promise<DispatchFromConfigResult> {
const replyResult = await (params.replyResolver ?? getReplyFromConfig)(
params.ctx,
{
...params.replyOptions,
onToolResult: (payload: ReplyPayload) => {
params.dispatcher.sendToolResult(payload);
},
onBlockReply: (payload: ReplyPayload) => {
params.dispatcher.sendBlockReply(payload);
},
},
params.cfg,
);
const replies = replyResult
? Array.isArray(replyResult)
? replyResult
: [replyResult]
: [];
let queuedFinal = false;
for (const reply of replies) {
queuedFinal = params.dispatcher.sendFinalReply(reply) || queuedFinal;
}
await params.dispatcher.waitForIdle();
return { queuedFinal, counts: params.dispatcher.getQueuedCounts() };
}

View File

@@ -22,7 +22,7 @@ export type ReplyDispatcherOptions = {
onError?: ReplyDispatchErrorHandler;
};
type ReplyDispatcher = {
export type ReplyDispatcher = {
sendToolResult: (payload: ReplyPayload) => boolean;
sendBlockReply: (payload: ReplyPayload) => boolean;
sendFinalReply: (payload: ReplyPayload) => boolean;