feat(plugins): expose llm input/output hook payloads (openclaw#16724) thanks @SecondThread

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: SecondThread <18317476+SecondThread@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
David Harmeyer
2026-02-15 14:01:00 -08:00
committed by GitHub
parent 3c6cff5758
commit 7c822d039b
8 changed files with 222 additions and 3 deletions

View File

@@ -14,6 +14,8 @@ import type {
PluginHookBeforeAgentStartEvent,
PluginHookBeforeAgentStartResult,
PluginHookBeforeCompactionEvent,
PluginHookLlmInputEvent,
PluginHookLlmOutputEvent,
PluginHookBeforeResetEvent,
PluginHookBeforeToolCallEvent,
PluginHookBeforeToolCallResult,
@@ -41,6 +43,8 @@ export type {
PluginHookAgentContext,
PluginHookBeforeAgentStartEvent,
PluginHookBeforeAgentStartResult,
PluginHookLlmInputEvent,
PluginHookLlmOutputEvent,
PluginHookAgentEndEvent,
PluginHookBeforeCompactionEvent,
PluginHookBeforeResetEvent,
@@ -212,6 +216,24 @@ export function createHookRunner(registry: PluginRegistry, options: HookRunnerOp
return runVoidHook("agent_end", event, ctx);
}
/**
* Run llm_input hook.
* Allows plugins to observe the exact input payload sent to the LLM.
* Runs in parallel (fire-and-forget).
*/
async function runLlmInput(event: PluginHookLlmInputEvent, ctx: PluginHookAgentContext) {
return runVoidHook("llm_input", event, ctx);
}
/**
* Run llm_output hook.
* Allows plugins to observe the exact output payload returned by the LLM.
* Runs in parallel (fire-and-forget).
*/
async function runLlmOutput(event: PluginHookLlmOutputEvent, ctx: PluginHookAgentContext) {
return runVoidHook("llm_output", event, ctx);
}
/**
* Run before_compaction hook.
*/
@@ -458,6 +480,8 @@ export function createHookRunner(registry: PluginRegistry, options: HookRunnerOp
return {
// Agent hooks
runBeforeAgentStart,
runLlmInput,
runLlmOutput,
runAgentEnd,
runBeforeCompaction,
runAfterCompaction,

View File

@@ -297,6 +297,8 @@ export type PluginDiagnostic = {
export type PluginHookName =
| "before_agent_start"
| "llm_input"
| "llm_output"
| "agent_end"
| "before_compaction"
| "after_compaction"
@@ -332,6 +334,35 @@ export type PluginHookBeforeAgentStartResult = {
prependContext?: string;
};
// llm_input hook
export type PluginHookLlmInputEvent = {
runId: string;
sessionId: string;
provider: string;
model: string;
systemPrompt?: string;
prompt: string;
historyMessages: unknown[];
imagesCount: number;
};
// llm_output hook
export type PluginHookLlmOutputEvent = {
runId: string;
sessionId: string;
provider: string;
model: string;
assistantTexts: string[];
lastAssistant?: unknown;
usage?: {
input?: number;
output?: number;
cacheRead?: number;
cacheWrite?: number;
total?: number;
};
};
// agent_end hook
export type PluginHookAgentEndEvent = {
messages: unknown[];
@@ -498,6 +529,11 @@ export type PluginHookHandlerMap = {
event: PluginHookBeforeAgentStartEvent,
ctx: PluginHookAgentContext,
) => Promise<PluginHookBeforeAgentStartResult | void> | PluginHookBeforeAgentStartResult | void;
llm_input: (event: PluginHookLlmInputEvent, ctx: PluginHookAgentContext) => Promise<void> | void;
llm_output: (
event: PluginHookLlmOutputEvent,
ctx: PluginHookAgentContext,
) => Promise<void> | void;
agent_end: (event: PluginHookAgentEndEvent, ctx: PluginHookAgentContext) => Promise<void> | void;
before_compaction: (
event: PluginHookBeforeCompactionEvent,

View File

@@ -0,0 +1,96 @@
import { describe, expect, it, vi } from "vitest";
import type { PluginRegistry } from "./registry.js";
import { createHookRunner } from "./hooks.js";
function createMockRegistry(
hooks: Array<{ hookName: string; handler: (...args: unknown[]) => unknown }>,
): PluginRegistry {
return {
hooks: hooks as never[],
typedHooks: hooks.map((h) => ({
pluginId: "test-plugin",
hookName: h.hookName,
handler: h.handler,
priority: 0,
source: "test",
})),
tools: [],
httpHandlers: [],
httpRoutes: [],
channelRegistrations: [],
gatewayHandlers: {},
cliRegistrars: [],
services: [],
providers: [],
commands: [],
} as unknown as PluginRegistry;
}
describe("llm hook runner methods", () => {
it("runLlmInput invokes registered llm_input hooks", async () => {
const handler = vi.fn();
const registry = createMockRegistry([{ hookName: "llm_input", handler }]);
const runner = createHookRunner(registry);
await runner.runLlmInput(
{
runId: "run-1",
sessionId: "session-1",
provider: "openai",
model: "gpt-5",
systemPrompt: "be helpful",
prompt: "hello",
historyMessages: [],
imagesCount: 0,
},
{
agentId: "main",
sessionId: "session-1",
},
);
expect(handler).toHaveBeenCalledWith(
expect.objectContaining({ runId: "run-1", prompt: "hello" }),
expect.objectContaining({ sessionId: "session-1" }),
);
});
it("runLlmOutput invokes registered llm_output hooks", async () => {
const handler = vi.fn();
const registry = createMockRegistry([{ hookName: "llm_output", handler }]);
const runner = createHookRunner(registry);
await runner.runLlmOutput(
{
runId: "run-1",
sessionId: "session-1",
provider: "openai",
model: "gpt-5",
assistantTexts: ["hi"],
lastAssistant: { role: "assistant", content: "hi" },
usage: {
input: 10,
output: 20,
total: 30,
},
},
{
agentId: "main",
sessionId: "session-1",
},
);
expect(handler).toHaveBeenCalledWith(
expect.objectContaining({ runId: "run-1", assistantTexts: ["hi"] }),
expect.objectContaining({ sessionId: "session-1" }),
);
});
it("hasHooks returns true for registered llm hooks", () => {
const registry = createMockRegistry([{ hookName: "llm_input", handler: vi.fn() }]);
const runner = createHookRunner(registry);
expect(runner.hasHooks("llm_input")).toBe(true);
expect(runner.hasHooks("llm_output")).toBe(false);
});
});