Hooks: preserve legacy before_agent_start event shape

This commit is contained in:
coderzc 2026-03-21 13:26:59 +08:00
parent f7079c09a4
commit 4314f44fe2
2 changed files with 30 additions and 2 deletions

View File

@ -114,6 +114,34 @@ describe("resolvePromptBuildHookResult", () => {
expect(result.prependContext).toBe("from-hook");
});
it("includes legacy hook agent context fields only when present", async () => {
const hookRunner = createLegacyOnlyHookRunner();
const messages = [{ role: "user", content: "ctx" }];
await resolvePromptBuildHookResult({
prompt: "hello",
messages,
hookCtx: {
sessionKey: "agent:assistant-beta:main",
agentId: "assistant-beta",
},
hookRunner,
});
expect(hookRunner.runBeforeAgentStart).toHaveBeenCalledWith(
{
prompt: "hello",
messages,
sessionKey: "agent:assistant-beta:main",
agentId: "assistant-beta",
},
{
sessionKey: "agent:assistant-beta:main",
agentId: "assistant-beta",
},
);
});
it("merges prompt-build and legacy context fields in deterministic order", async () => {
const hookRunner = {
hasHooks: vi.fn(() => true),

View File

@ -1439,8 +1439,8 @@ export async function resolvePromptBuildHookResult(params: {
{
prompt: params.prompt,
messages: params.messages,
sessionKey: params.hookCtx.sessionKey,
agentId: params.hookCtx.agentId,
...(params.hookCtx.sessionKey ? { sessionKey: params.hookCtx.sessionKey } : {}),
...(params.hookCtx.agentId ? { agentId: params.hookCtx.agentId } : {}),
},
params.hookCtx,
)