mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-07 22:09:57 +00:00
refactor: centralize dispatcher lifecycle ownership
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { ReplyDispatcher } from "./reply/reply-dispatcher.js";
|
||||
import { withReplyDispatcher } from "./dispatch.js";
|
||||
import { dispatchInboundMessage, withReplyDispatcher } from "./dispatch.js";
|
||||
import { buildTestCtx } from "./reply/test-ctx.js";
|
||||
|
||||
function createDispatcher(record: string[]): ReplyDispatcher {
|
||||
return {
|
||||
@@ -58,4 +60,32 @@ describe("withReplyDispatcher", () => {
|
||||
expect(onSettled).toHaveBeenCalledTimes(1);
|
||||
expect(order).toEqual(["run", "markComplete", "waitForIdle", "onSettled"]);
|
||||
});
|
||||
|
||||
it("dispatchInboundMessage owns dispatcher lifecycle", async () => {
|
||||
const order: string[] = [];
|
||||
const dispatcher = {
|
||||
sendToolResult: () => true,
|
||||
sendBlockReply: () => true,
|
||||
sendFinalReply: () => {
|
||||
order.push("sendFinalReply");
|
||||
return true;
|
||||
},
|
||||
getQueuedCounts: () => ({ tool: 0, block: 0, final: 0 }),
|
||||
markComplete: () => {
|
||||
order.push("markComplete");
|
||||
},
|
||||
waitForIdle: async () => {
|
||||
order.push("waitForIdle");
|
||||
},
|
||||
} satisfies ReplyDispatcher;
|
||||
|
||||
await dispatchInboundMessage({
|
||||
ctx: buildTestCtx(),
|
||||
cfg: {} as OpenClawConfig,
|
||||
dispatcher,
|
||||
replyResolver: async () => ({ text: "ok" }),
|
||||
});
|
||||
|
||||
expect(order).toEqual(["sendFinalReply", "markComplete", "waitForIdle"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -40,12 +40,16 @@ export async function dispatchInboundMessage(params: {
|
||||
replyResolver?: typeof import("./reply.js").getReplyFromConfig;
|
||||
}): Promise<DispatchInboundResult> {
|
||||
const finalized = finalizeInboundContext(params.ctx);
|
||||
return await dispatchReplyFromConfig({
|
||||
ctx: finalized,
|
||||
cfg: params.cfg,
|
||||
return await withReplyDispatcher({
|
||||
dispatcher: params.dispatcher,
|
||||
replyOptions: params.replyOptions,
|
||||
replyResolver: params.replyResolver,
|
||||
run: () =>
|
||||
dispatchReplyFromConfig({
|
||||
ctx: finalized,
|
||||
cfg: params.cfg,
|
||||
dispatcher: params.dispatcher,
|
||||
replyOptions: params.replyOptions,
|
||||
replyResolver: params.replyResolver,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -59,23 +63,20 @@ export async function dispatchInboundMessageWithBufferedDispatcher(params: {
|
||||
const { dispatcher, replyOptions, markDispatchIdle } = createReplyDispatcherWithTyping(
|
||||
params.dispatcherOptions,
|
||||
);
|
||||
return await withReplyDispatcher({
|
||||
dispatcher,
|
||||
run: async () =>
|
||||
dispatchInboundMessage({
|
||||
ctx: params.ctx,
|
||||
cfg: params.cfg,
|
||||
dispatcher,
|
||||
replyResolver: params.replyResolver,
|
||||
replyOptions: {
|
||||
...params.replyOptions,
|
||||
...replyOptions,
|
||||
},
|
||||
}),
|
||||
onSettled: () => {
|
||||
markDispatchIdle();
|
||||
},
|
||||
});
|
||||
try {
|
||||
return await dispatchInboundMessage({
|
||||
ctx: params.ctx,
|
||||
cfg: params.cfg,
|
||||
dispatcher,
|
||||
replyResolver: params.replyResolver,
|
||||
replyOptions: {
|
||||
...params.replyOptions,
|
||||
...replyOptions,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
markDispatchIdle();
|
||||
}
|
||||
}
|
||||
|
||||
export async function dispatchInboundMessageWithDispatcher(params: {
|
||||
@@ -86,15 +87,11 @@ export async function dispatchInboundMessageWithDispatcher(params: {
|
||||
replyResolver?: typeof import("./reply.js").getReplyFromConfig;
|
||||
}): Promise<DispatchInboundResult> {
|
||||
const dispatcher = createReplyDispatcher(params.dispatcherOptions);
|
||||
return await withReplyDispatcher({
|
||||
return await dispatchInboundMessage({
|
||||
ctx: params.ctx,
|
||||
cfg: params.cfg,
|
||||
dispatcher,
|
||||
run: async () =>
|
||||
dispatchInboundMessage({
|
||||
ctx: params.ctx,
|
||||
cfg: params.cfg,
|
||||
dispatcher,
|
||||
replyResolver: params.replyResolver,
|
||||
replyOptions: params.replyOptions,
|
||||
}),
|
||||
replyResolver: params.replyResolver,
|
||||
replyOptions: params.replyOptions,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -278,7 +278,6 @@ export async function dispatchReplyFromConfig(params: {
|
||||
} else {
|
||||
queuedFinal = dispatcher.sendFinalReply(payload);
|
||||
}
|
||||
await dispatcher.waitForIdle();
|
||||
const counts = dispatcher.getQueuedCounts();
|
||||
counts.final += routedFinalCount;
|
||||
recordProcessed("completed", { reason: "fast_abort" });
|
||||
@@ -443,8 +442,6 @@ export async function dispatchReplyFromConfig(params: {
|
||||
}
|
||||
}
|
||||
|
||||
await dispatcher.waitForIdle();
|
||||
|
||||
const counts = dispatcher.getQueuedCounts();
|
||||
counts.final += routedFinalCount;
|
||||
recordProcessed("completed");
|
||||
@@ -454,9 +451,5 @@ export async function dispatchReplyFromConfig(params: {
|
||||
recordProcessed("error", { error: String(err) });
|
||||
markIdle("message_error");
|
||||
throw err;
|
||||
} finally {
|
||||
// Always clear the dispatcher reservation so a leaked pending count
|
||||
// can never permanently block gateway restarts.
|
||||
dispatcher.markComplete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user