feat: Implement HookEventKind

This commit is contained in:
2026-01-09 19:18:20 +09:00
parent 33f1c218f2
commit 5691b09fc8
15 changed files with 916 additions and 416 deletions
+14 -14
View File
@@ -8,7 +8,10 @@ use std::time::{Duration, Instant};
use async_trait::async_trait;
use llm_worker::Worker;
use llm_worker::hook::{ControlFlow, HookError, ToolCall, ToolResult, WorkerHook};
use llm_worker::hook::{
AfterToolCall, AfterToolCallResult, BeforeToolCall, BeforeToolCallResult, Hook, HookError,
ToolCall, ToolResult,
};
use llm_worker::llm_client::event::{Event, ResponseStatus, StatusEvent};
use llm_worker::tool::{Tool, ToolError};
@@ -158,20 +161,17 @@ async fn test_before_tool_call_skip() {
struct BlockingHook;
#[async_trait]
impl WorkerHook for BlockingHook {
async fn before_tool_call(
&self,
tool_call: &mut ToolCall,
) -> Result<ControlFlow, HookError> {
impl Hook<BeforeToolCall> for BlockingHook {
async fn call(&self, tool_call: &mut ToolCall) -> Result<BeforeToolCallResult, HookError> {
if tool_call.name == "blocked_tool" {
Ok(ControlFlow::Skip)
Ok(BeforeToolCallResult::Skip)
} else {
Ok(ControlFlow::Continue)
Ok(BeforeToolCallResult::Continue)
}
}
}
worker.add_hook(BlockingHook);
worker.add_before_tool_call_hook(BlockingHook);
let _result = worker.run("Test hook").await;
@@ -242,19 +242,19 @@ async fn test_after_tool_call_modification() {
}
#[async_trait]
impl WorkerHook for ModifyingHook {
async fn after_tool_call(
impl Hook<AfterToolCall> for ModifyingHook {
async fn call(
&self,
tool_result: &mut ToolResult,
) -> Result<ControlFlow, HookError> {
) -> Result<AfterToolCallResult, HookError> {
tool_result.content = format!("[Modified] {}", tool_result.content);
*self.modified_content.lock().unwrap() = Some(tool_result.content.clone());
Ok(ControlFlow::Continue)
Ok(AfterToolCallResult::Continue)
}
}
let modified_content = Arc::new(std::sync::Mutex::new(None));
worker.add_hook(ModifyingHook {
worker.add_after_tool_call_hook(ModifyingHook {
modified_content: modified_content.clone(),
});