(
&self,
hook_id: String,
event_type: String,
matcher: Option<HookMatcherObject>,
config: Option<HookConfigObject>,
#[napi(
ts_arg_type = "(
| 4429 | /// continue. |
| 4430 | #[napi] |
| 4431 | pub fn register_hook( |
| 4432 | &self, |
| 4433 | hook_id: String, |
| 4434 | event_type: String, |
| 4435 | matcher: Option<HookMatcherObject>, |
| 4436 | config: Option<HookConfigObject>, |
| 4437 | #[napi( |
| 4438 | ts_arg_type = "((event: Record<string, unknown>) => { action: string; reason?: string } | null | undefined) | null | undefined" |
| 4439 | )] |
| 4440 | handler: Option<napi::JsFunction>, |
| 4441 | ) -> napi::Result<()> { |
| 4442 | use napi::threadsafe_function::{ErrorStrategy, ThreadSafeCallContext, ThreadsafeFunction}; |
| 4443 | |
| 4444 | let rust_event_type = parse_hook_event_type(&event_type)?; |
| 4445 | let mut hook = RustHook::new(&hook_id, rust_event_type); |
| 4446 | |
| 4447 | if let Some(m) = matcher { |
| 4448 | let mut rust_matcher = RustHookMatcher::new(); |
| 4449 | if let Some(tool) = m.tool { |
| 4450 | rust_matcher = rust_matcher.with_tool(tool); |
| 4451 | } |
| 4452 | if let Some(path) = m.path_pattern { |
| 4453 | rust_matcher = rust_matcher.with_path(path); |
| 4454 | } |
| 4455 | if let Some(cmd) = m.command_pattern { |
| 4456 | rust_matcher = rust_matcher.with_command(cmd); |
| 4457 | } |
| 4458 | if let Some(sid) = m.session_id { |
| 4459 | rust_matcher = rust_matcher.with_session(sid); |
| 4460 | } |
| 4461 | if let Some(skill) = m.skill { |
| 4462 | rust_matcher = rust_matcher.with_skill(skill); |
| 4463 | } |
| 4464 | hook = hook.with_matcher(rust_matcher); |
| 4465 | } |
| 4466 | |
| 4467 | if let Some(c) = config { |
| 4468 | hook = hook.with_config(RustHookConfig { |
| 4469 | priority: c.priority.unwrap_or(100), |
| 4470 | timeout_ms: c.timeout_ms.map(|v| v as u64).unwrap_or(30000), |
| 4471 | async_execution: c.async_execution.unwrap_or(false), |
| 4472 | max_retries: c.max_retries.unwrap_or(0), |
| 4473 | }); |
| 4474 | } |
| 4475 | |
| 4476 | let timeout_ms = hook.config.timeout_ms; |
| 4477 | self.inner.register_hook(hook); |
| 4478 | |
| 4479 | if let Some(js_fn) = handler { |
| 4480 | let tsfn: ThreadsafeFunction<serde_json::Value, ErrorStrategy::CalleeHandled> = js_fn |
| 4481 | .create_threadsafe_function( |
| 4482 | 0, |
| 4483 | |ctx: ThreadSafeCallContext<serde_json::Value>| { |
| 4484 | let js_val = ctx.env.to_js_value(&ctx.value)?; |
| 4485 | Ok(vec![js_val]) |
| 4486 | }, |
| 4487 | )?; |
| 4488 | self.inner.register_hook_handler( |
nothing calls this directly
no test coverage detected