claude_cli: tool calls on the event bus, extensions can block, permissions deny/extensions-only (#182)
This commit is contained in:
1 parent
6ebf1c3180
commit
6978aad27a
12 files changed
+297
-12
No files matched your search
@@ -100,11 +100,16 @@ pub enum HookAction {
|
||||
#[async_trait]
|
||||
pub trait Hook: Send + Sync {
|
||||
async fn on(&self, ev: &Event) -> HookAction;
|
||||
/// `Some(alive)` if this hook guards `tool_call` (extensions subscribed to it).
|
||||
fn guard(&self) -> Option<bool> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Clones share the hook list (the permission bridge emits on the agent's bus).
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Bus {
|
||||
hooks: Vec<Arc<dyn Hook>>,
|
||||
hooks: Arc<std::sync::RwLock<Vec<Arc<dyn Hook>>>>,
|
||||
}
|
||||
|
||||
/// Aggregate outcome of dispatching one event to all hooks.
|
||||
@@ -117,19 +122,29 @@ pub struct Outcome {
|
||||
|
||||
impl Bus {
|
||||
pub fn add_arc(&mut self, h: Arc<dyn Hook>) -> &mut Self {
|
||||
self.hooks.push(h);
|
||||
self.hooks
|
||||
.write()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.push(h);
|
||||
self
|
||||
}
|
||||
pub fn add(&mut self, h: impl Hook + 'static) -> &mut Self {
|
||||
self.hooks.push(Arc::new(h));
|
||||
self
|
||||
self.add_arc(Arc::new(h))
|
||||
}
|
||||
|
||||
/// (number of `tool_call` guards, all of them alive).
|
||||
pub fn guards(&self) -> (usize, bool) {
|
||||
let hooks = self.hooks.read().unwrap_or_else(|e| e.into_inner());
|
||||
let g: Vec<bool> = hooks.iter().filter_map(|h| h.guard()).collect();
|
||||
(g.len(), g.iter().all(|a| *a))
|
||||
}
|
||||
|
||||
/// Hooks run in registration order. First `Block` wins; `Mutate` is visible to later hooks.
|
||||
pub async fn emit(&self, ev: Event) -> Outcome {
|
||||
let mut ev = ev;
|
||||
let mut o = Outcome::default();
|
||||
for h in &self.hooks {
|
||||
let hooks = self.hooks.read().unwrap_or_else(|e| e.into_inner()).clone();
|
||||
for h in &hooks {
|
||||
match h.on(&ev).await {
|
||||
HookAction::Continue => {}
|
||||
HookAction::Block(r) => {
|
||||
|
||||
Reference in new issue
Block a user