Feature Request: Add Agent Lifecycle Hook Events
Summary
Add hook events for agent message lifecycle (agent:response, message:sent, tool:complete) to enable automatic logging and automation workflows.
Motivation
The current hook system supports command events (command:new, command:reset) and bootstrap events (agent:bootstrap), but lacks events for the agent response lifecycle. This prevents automation use cases like:
- Automatic interaction logging - Track what the agent did, which tools were used, and outcomes
- Pattern detection - Analyze repeated workflows to suggest optimizations or create new skills
- Audit trails - Log all agent actions for debugging and compliance
- Post-response automation - Trigger follow-up actions after agent completes a task
Proposed Events
1. agent:response
Triggered after the agent completes a response but before it's sent to the user.
Event context:
{
type: 'agent',
action: 'response',
sessionKey: string,
timestamp: Date,
context: {
sessionEntry?: SessionEntry,
responseText: string,
toolCalls: Array<{
tool: string,
args: any,
result: any,
success: boolean
}>,
tokenUsage: {
input: number,
output: number
}
}
}
2. message:sent
Triggered after a message is successfully sent to the channel.
Event context:
{
type: 'message',
action: 'sent',
sessionKey: string,
timestamp: Date,
context: {
channel: string, // 'telegram', 'whatsapp', etc.
messageId?: string,
recipientId: string
}
}
3. tool:complete (optional)
Triggered after each individual tool call completes.
Event context:
{
type: 'tool',
action: 'complete',
sessionKey: string,
timestamp: Date,
context: {
toolName: string,
args: any,
result: any,
success: boolean,
durationMs: number
}
}
Use Case Example
Automatic Interaction Logger Hook:
// hooks/auto-logger/handler.ts
import type { HookHandler } from '../../src/hooks/hooks.js';
const autoLogger: HookHandler = async (event) => {
if (event.type !== 'agent' || event.action !== 'response') {
return;
}
const interaction = {
timestamp: event.timestamp.toISOString(),
sessionKey: event.sessionKey,
tools: event.context.toolCalls?.map(t => t.tool) || [],
outcome: event.context.toolCalls?.every(t => t.success) ? 'success' : 'partial',
tokens: event.context.tokenUsage
};
// Append to interactions.jsonl
await fs.appendFile('data/interactions.jsonl', JSON.stringify(interaction) + '\n');
};
export default autoLogger;
Benefits
- Zero user intervention - Logging happens automatically without manual commands
- Extensibility - Enables community to build powerful automation workflows
- Consistency - Follows existing hook patterns (command:*, agent:bootstrap)
- Backward compatibility - New events, existing hooks unaffected
Alternative Considered
Plugin API tool_result_persist - Already exists but:
- Only works in plugins, not standalone hooks
- Requires more complex setup
- Synchronous only
Adding proper lifecycle events would complement the plugin API and enable simpler use cases.
Questions
- Should
agent:response fire before or after message queueing?
- Should there be separate events for streaming vs non-streaming responses?
- Should tool errors trigger
tool:error or just include success: false in tool:complete?
Related: Docs mention planned events like session:start, session:end, message:received - this request aligns with that roadmap.
Feature Request: Add Agent Lifecycle Hook Events
Summary
Add hook events for agent message lifecycle (
agent:response,message:sent,tool:complete) to enable automatic logging and automation workflows.Motivation
The current hook system supports command events (
command:new,command:reset) and bootstrap events (agent:bootstrap), but lacks events for the agent response lifecycle. This prevents automation use cases like:Proposed Events
1.
agent:responseTriggered after the agent completes a response but before it's sent to the user.
Event context:
2.
message:sentTriggered after a message is successfully sent to the channel.
Event context:
3.
tool:complete(optional)Triggered after each individual tool call completes.
Event context:
Use Case Example
Automatic Interaction Logger Hook:
Benefits
Alternative Considered
Plugin API
tool_result_persist- Already exists but:Adding proper lifecycle events would complement the plugin API and enable simpler use cases.
Questions
agent:responsefire before or after message queueing?tool:erroror just includesuccess: falseintool:complete?Related: Docs mention planned events like
session:start,session:end,message:received- this request aligns with that roadmap.