Skip to content

Commit 5f1486d

Browse files
committed
feat: add pre_route hook support with AbortSignal handling and tests
1 parent d43d3e7 commit 5f1486d

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎src/auto-reply/reply/dispatch-from-config.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,42 @@ describe("dispatchReplyFromConfig", () => {
24292429
});
24302430
});
24312431

2432+
it("threads pre_route agentId override into reply execution", async () => {
2433+
setNoAbort();
2434+
hookMocks.runner.hasHooks.mockImplementation(
2435+
((hookName?: string) => hookName === "pre_route") as (hookName?: string) => boolean,
2436+
);
2437+
hookMocks.runner.runPreRoute.mockResolvedValue({
2438+
handled: true,
2439+
routeOverride: {
2440+
sessionKey: "session:override:without-embedded-agent",
2441+
agentId: "codex",
2442+
},
2443+
});
2444+
2445+
const dispatcher = createDispatcher();
2446+
const ctx = buildTestCtx({
2447+
SessionKey: "agent:main:main",
2448+
MessageSid: "msg-pre-route-agent-id-forward-1",
2449+
});
2450+
const replyResolver = vi.fn(async () => ({ text: "ok" }) satisfies ReplyPayload);
2451+
2452+
await dispatchReplyFromConfig({
2453+
ctx,
2454+
cfg: emptyConfig,
2455+
dispatcher,
2456+
replyResolver,
2457+
});
2458+
2459+
expect(replyResolver).toHaveBeenCalledWith(
2460+
expect.any(Object),
2461+
expect.objectContaining({
2462+
agentIdOverride: "codex",
2463+
}),
2464+
undefined,
2465+
);
2466+
});
2467+
24322468
it("uses native command target session in pre_route hook context", async () => {
24332469
setNoAbort();
24342470
hookMocks.runner.hasHooks.mockImplementation(

‎src/plugins/hooks.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,9 @@ export function createHookRunner(registry: PluginRegistry, options: HookRunnerOp
625625
hook.handler as (
626626
event: unknown,
627627
ctx: unknown,
628+
signal?: AbortSignal,
628629
) => Promise<PluginHookPreRouteResult | void>
629-
)(event, ctx);
630+
)(event, ctx, signal);
630631
if (handlerResult?.handled) {
631632
return handlerResult;
632633
}

‎src/plugins/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,7 @@ export type PluginHookHandlerMap = {
25422542
pre_route: (
25432543
event: PluginHookPreRouteEvent,
25442544
ctx: PluginHookPreRouteContext,
2545+
signal?: AbortSignal,
25452546
) => Promise<PluginHookPreRouteResult | void> | PluginHookPreRouteResult | void;
25462547
inbound_claim: (
25472548
event: PluginHookInboundClaimEvent,

‎src/plugins/wired-hooks-pre-route.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,26 @@ describe("pre_route hook runner", () => {
6868
);
6969
expect(succeeding).toHaveBeenCalledTimes(1);
7070
});
71+
72+
it("passes AbortSignal to pre_route handlers", async () => {
73+
const handler = vi.fn().mockResolvedValue(undefined);
74+
const { runner } = createHookRunnerWithRegistry([{ hookName: "pre_route", handler }]);
75+
const controller = new AbortController();
76+
77+
await runner.runPreRoute(preRouteEvent, preRouteCtx, controller.signal);
78+
79+
expect(handler).toHaveBeenCalledWith(preRouteEvent, preRouteCtx, controller.signal);
80+
});
81+
82+
it("skips handlers when signal is already aborted", async () => {
83+
const handler = vi.fn().mockResolvedValue({ handled: true });
84+
const { runner } = createHookRunnerWithRegistry([{ hookName: "pre_route", handler }]);
85+
const controller = new AbortController();
86+
controller.abort();
87+
88+
const result = await runner.runPreRoute(preRouteEvent, preRouteCtx, controller.signal);
89+
90+
expect(result).toBeUndefined();
91+
expect(handler).not.toHaveBeenCalled();
92+
});
7193
});

0 commit comments

Comments
 (0)