Skip to main content

HookRegistry

Struct HookRegistry 

Source
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

Source

pub fn new() -> Self

创建空的注册中心。

Source

pub fn register( &mut self, hook: Arc<dyn Hook>, source: HookSource, priority: i32, )

注册一个 Hook。

Hook 按 priority 升序排列(数值越小越先执行)。

Source

pub fn remove(&mut self, name: &str)

移除指定名称的所有 Hook。

Source

pub fn len(&self) -> usize

已注册的 Hook 数量。

Source

pub fn is_empty(&self) -> bool

是否为空。

Source

pub fn matching(&self, input: &HookInput) -> Vec<&RegisteredHook>

获取匹配指定输入的所有 Hook(按 priority 排序)。

匹配逻辑:

  1. 事件匹配 — hook.events() 为空(匹配所有)或包含 input.event()
  2. Matcher 匹配 — hook.matcher() 为 None(匹配所有)或模式匹配 tool_name
Source

pub fn has_hooks_for(&self, event: HookEvent) -> bool

检查是否有任何 Hook 注册在指定事件上。

轻量级检查,不做 matcher 匹配,用于热路径的快速跳过。

Trait Implementations§

Source§

impl Default for HookRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.