pub struct HookRegistry { /* private fields */ }Expand description
Hook 注册中心 — 管理已注册的 Hook 并按事件/matcher 分发。
§生命周期
- Agent 启动时构建(从配置 + 程序化注册)
- Agent loop 在各生命周期节点调用匹配的 Hook
- Session 结束时销毁
§线程安全
使用 Arc<dyn Hook> 存储,支持跨 await 共享。
HookRegistry 本身在构建后作为 Arc<HookRegistry> 或引用传入 agent loop。
§Examples
use std::sync::Arc;
use async_trait::async_trait;
use katu_core::hook::*;
struct MyHook;
#[async_trait]
impl Hook for MyHook {
fn name(&self) -> &str { "my_hook" }
fn events(&self) -> &[HookEvent] { &[HookEvent::PreToolUse] }
async fn on_event(&self, _input: &HookInput) -> HookOutput {
HookOutput::passthrough()
}
}
let mut registry = HookRegistry::new();
registry.register(Arc::new(MyHook), HookSource::Programmatic, 0);
assert_eq!(registry.len(), 1);Implementations§
Source§impl HookRegistry
impl HookRegistry
Sourcepub fn register(
&mut self,
hook: Arc<dyn Hook>,
source: HookSource,
priority: i32,
)
pub fn register( &mut self, hook: Arc<dyn Hook>, source: HookSource, priority: i32, )
注册一个 Hook。
Hook 按 priority 升序排列(数值越小越先执行)。
Sourcepub fn matching(&self, input: &HookInput) -> Vec<&RegisteredHook>
pub fn matching(&self, input: &HookInput) -> Vec<&RegisteredHook>
获取匹配指定输入的所有 Hook(按 priority 排序)。
匹配逻辑:
- 事件匹配 —
hook.events()为空(匹配所有)或包含input.event() - Matcher 匹配 —
hook.matcher()为 None(匹配所有)或模式匹配 tool_name
Sourcepub fn has_hooks_for(&self, event: HookEvent) -> bool
pub fn has_hooks_for(&self, event: HookEvent) -> bool
检查是否有任何 Hook 注册在指定事件上。
轻量级检查,不做 matcher 匹配,用于热路径的快速跳过。
Trait Implementations§
Auto Trait Implementations§
impl Freeze for HookRegistry
impl !RefUnwindSafe for HookRegistry
impl Send for HookRegistry
impl Sync for HookRegistry
impl Unpin for HookRegistry
impl UnsafeUnpin for HookRegistry
impl !UnwindSafe for HookRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more