Implement before_agent_start hook event

- Add BeforeAgentStartEvent and BeforeAgentStartEventResult types
- Add emitBeforeAgentStart to HookRunner
- Call in AgentSession.prompt() before agent.prompt()
- Hook can return a message to inject into context (persisted + visible)
- Add test hook demonstrating custom message rendering and before_agent_start
This commit is contained in:
Mario Zechner 2025-12-28 14:50:22 +01:00
parent bbdc350394
commit 57146de202
6 changed files with 170 additions and 16 deletions

View file

@ -1,24 +1,81 @@
/**
* Test hook that registers a /greet command.
* Usage: /greet [name]
* Test hook demonstrating custom commands, message rendering, and before_agent_start.
*/
import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
import type { HookAPI } from "@mariozechner/pi-coding-agent";
import { Box, Text } from "@mariozechner/pi-tui";
export default function (pi: HookAPI) {
pi.registerCommand("greet", {
description: "Send a greeting message to the LLM",
handler: async (ctx) => {
const name = ctx.args.trim() || "world";
// Track whether injection is enabled
let injectEnabled = false;
// Insert a custom message and trigger LLM response
pi.sendMessage(
{
customType: "greeting",
content: `Hello, ${name}! Please say something nice about them.`,
display: true,
},
true, // triggerTurn - get LLM to respond
);
// Register a custom message renderer for our "test-info" type
pi.registerMessageRenderer("test-info", (message, options, theme) => {
const box = new Box(0, 0, (t) => theme.bg("success", t));
const label = theme.fg("successText", "[TEST INFO]");
box.addChild(new Text(label, 0, 0));
const content =
typeof message.content === "string"
? message.content
: message.content.map((c) => (c.type === "text" ? c.text : "[image]")).join("");
box.addChild(new Text(theme.fg("successText", content), 0, 1));
if (options.expanded && message.details) {
box.addChild(new Text(theme.fg("dim", `Details: ${JSON.stringify(message.details)}`), 0, 2));
}
return box;
});
// Register /test-msg command
pi.registerCommand("test-msg", {
description: "Send a test custom message",
handler: async (ctx) => {
pi.sendMessage({
customType: "test-info",
content: "This is a test message with custom rendering!",
display: true,
details: { timestamp: Date.now(), source: "test-command hook" },
});
},
});
// Register /test-hidden command
pi.registerCommand("test-hidden", {
description: "Send a hidden message (display: false)",
handler: async (ctx) => {
pi.sendMessage({
customType: "test-info",
content: "This message is in context but not displayed",
display: false,
});
ctx.ui.notify("Sent hidden message (check session file)");
},
});
// Register /test-inject command to toggle before_agent_start injection
pi.registerCommand("test-inject", {
description: "Toggle context injection before agent starts",
handler: async (ctx) => {
injectEnabled = !injectEnabled;
ctx.ui.notify(`Context injection ${injectEnabled ? "enabled" : "disabled"}`);
},
});
// Demonstrate before_agent_start: inject context when enabled
pi.on("before_agent_start", async (event, ctx) => {
if (!injectEnabled) return;
// Return a message to inject before the user's prompt
return {
message: {
customType: "test-info",
content: `[Injected context for prompt: "${event.prompt.slice(0, 50)}..."]`,
display: true,
details: { injectedAt: Date.now() },
},
};
});
}