ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Source: ~/claudecode/openclaudecode/src/utils/hooks/sessionHooks.ts
#![allow(dead_code)]

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use crate::utils::hooks::hooks_settings::{HookCommand, HookEvent, is_hook_equal};

/// Function hook callback - returns true if check passes, false to block
pub type FunctionHookCallback = Box<dyn Fn(&[serde_json::Value]) -> bool + Send + Sync>;

/// Function hook type with callback embedded.
/// Session-scoped only, cannot be persisted to settings.json.
#[derive(Clone)]
pub struct FunctionHook {
    pub id: Option<String>,
    pub timeout: Option<u64>,
    pub callback: Arc<dyn Fn(&[serde_json::Value]) -> bool + Send + Sync>,
    pub error_message: String,
    pub status_message: Option<String>,
}

impl FunctionHook {
    pub fn new(
        id: Option<String>,
        timeout: Option<u64>,
        callback: Arc<dyn Fn(&[serde_json::Value]) -> bool + Send + Sync>,
        error_message: String,
    ) -> Self {
        Self {
            id,
            timeout,
            callback,
            error_message,
            status_message: None,
        }
    }
}

/// Extended hook command that can be either a regular hook or a function hook
#[derive(Clone)]
pub enum SessionHookCommand {
    Regular(HookCommand),
    Function(FunctionHook),
}

/// On hook success callback
pub type OnHookSuccess = Arc<dyn Fn(&SessionHookCommand, &AggregatedHookResult) + Send + Sync>;

/// Aggregated hook result
pub struct AggregatedHookResult {
    pub success: bool,
    pub output: Option<String>,
}

/// Session hook matcher
#[derive(Clone)]
pub struct SessionHookMatcher {
    pub matcher: String,
    pub skill_root: Option<String>,
    pub hooks: Vec<SessionHookEntry>,
}

/// A single hook entry in a matcher
#[derive(Clone)]
pub struct SessionHookEntry {
    pub hook: SessionHookCommand,
    pub on_hook_success: Option<OnHookSuccess>,
}

/// Session store for hooks
#[derive(Clone, Default)]
pub struct SessionStore {
    pub hooks: HashMap<HookEvent, Vec<SessionHookMatcher>>,
}

/// Session hooks state - uses Arc<Mutex<>> for interior mutability
/// This mimics the TypeScript Map pattern where .set/.delete don't change
/// the container's identity.
pub struct SessionHooksState {
    hooks: HashMap<String, SessionStore>,
}

impl SessionHooksState {
    pub fn new() -> Self {
        Self {
            hooks: HashMap::new(),
        }
    }
}

lazy_static::lazy_static! {
    static ref SESSION_HOOKS_STATE: Arc<Mutex<SessionHooksState>> = Arc::new(Mutex::new(
        SessionHooksState::new()
    ));
}

/// Add a command or prompt hook to the session.
/// Session hooks are temporary, in-memory only, and cleared when session ends.
pub fn add_session_hook(
    set_app_state: &dyn Fn(&dyn Fn(&mut serde_json::Value)),
    session_id: &str,
    event: &HookEvent,
    matcher: &str,
    hook: HookCommand,
    on_hook_success: Option<OnHookSuccess>,
    skill_root: Option<&str>,
) {
    add_hook_to_session(
        set_app_state,
        session_id,
        event,
        matcher,
        SessionHookCommand::Regular(hook),
        on_hook_success,
        skill_root.map(|s| s.to_string()),
    );
}

/// Add a function hook to the session.
/// Function hooks execute TypeScript callbacks in-memory for validation.
/// Returns the hook ID (for removal)
pub fn add_function_hook(
    set_app_state: &dyn Fn(&dyn Fn(&mut serde_json::Value)),
    session_id: &str,
    event: &HookEvent,
    matcher: &str,
    callback: Arc<dyn Fn(&[serde_json::Value]) -> bool + Send + Sync>,
    error_message: String,
    timeout: Option<u64>,
    id: Option<String>,
) -> String {
    let hook_id = id.unwrap_or_else(|| {
        format!(
            "function-hook-{}-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis(),
            rand::random::<u64>()
        )
    });

    let hook = FunctionHook::new(Some(hook_id.clone()), timeout, callback, error_message);

    add_hook_to_session(
        set_app_state,
        session_id,
        event,
        matcher,
        SessionHookCommand::Function(hook),
        None,
        None,
    );

    hook_id
}

/// Remove a function hook by ID from the session
pub fn remove_function_hook(
    set_app_state: &dyn Fn(&dyn Fn(&mut serde_json::Value)),
    session_id: &str,
    event: &HookEvent,
    hook_id: &str,
) {
    set_app_state(&|state: &mut serde_json::Value| {
        // In a real implementation, we'd access the session hooks from app state
        // For now, we use the global state
    });

    log_for_debugging(&format!(
        "Removed function hook {} for event {} in session {}",
        hook_id,
        event.as_str(),
        session_id
    ));
}

/// Internal helper to add a hook to session state
fn add_hook_to_session(
    set_app_state: &dyn Fn(&dyn Fn(&mut serde_json::Value)),
    session_id: &str,
    event: &HookEvent,
    matcher: &str,
    hook: SessionHookCommand,
    on_hook_success: Option<OnHookSuccess>,
    skill_root: Option<String>,
) {
    // Call set_app_state to notify state change (matches TypeScript behavior)
    set_app_state(&|state: &mut serde_json::Value| {
        // Update state with the new hook
        if let Some(session_hooks) = state.get_mut("session_hooks") {
            if let Some(session_map) = session_hooks.as_object_mut() {
                let _ = session_map.entry(session_id.to_string());
            }
        }
    });

    let mut state = SESSION_HOOKS_STATE.lock().unwrap();
    let store = state
        .hooks
        .entry(session_id.to_string())
        .or_insert_with(SessionStore::default);

    let event_matchers = store.hooks.entry(event.clone()).or_default();

    // Find existing matcher or create new one
    let existing_matcher_index = event_matchers
        .iter()
        .position(|m| m.matcher == matcher && m.skill_root == skill_root);

    if let Some(idx) = existing_matcher_index {
        // Add to existing matcher
        event_matchers[idx].hooks.push(SessionHookEntry {
            hook,
            on_hook_success,
        });
    } else {
        // Create new matcher
        event_matchers.push(SessionHookMatcher {
            matcher: matcher.to_string(),
            skill_root,
            hooks: vec![SessionHookEntry {
                hook,
                on_hook_success,
            }],
        });
    }

    log_for_debugging(&format!(
        "Added session hook for event {} in session {}",
        event.as_str(),
        session_id
    ));
}

/// Remove a specific hook from the session
pub fn remove_session_hook(
    set_app_state: &dyn Fn(&dyn Fn(&mut serde_json::Value)),
    session_id: &str,
    event: &HookEvent,
    hook: &HookCommand,
) {
    set_app_state(&|state: &mut serde_json::Value| {
        // In a real implementation, we'd access the session hooks from app state
    });

    let mut state = SESSION_HOOKS_STATE.lock().unwrap();
    if let Some(store) = state.hooks.get_mut(session_id) {
        if let Some(event_matchers) = store.hooks.get_mut(event) {
            // Remove the hook from all matchers
            for matcher in event_matchers.iter_mut() {
                matcher.hooks.retain(|entry| {
                    if let SessionHookCommand::Regular(ref regular_hook) = entry.hook {
                        !is_hook_equal(regular_hook, hook)
                    } else {
                        true // Don't remove function hooks by HookCommand
                    }
                });
            }
            // Remove empty matchers
            event_matchers.retain(|m| !m.hooks.is_empty());

            // Remove empty event matchers
            store.hooks.retain(|_, matchers| !matchers.is_empty());
        }
    }

    log_for_debugging(&format!(
        "Removed session hook for event {} in session {}",
        event.as_str(),
        session_id
    ));
}

/// Extended hook matcher that includes optional skillRoot for skill-scoped hooks
#[derive(Clone)]
pub struct SessionDerivedHookMatcher {
    pub matcher: String,
    pub hooks: Vec<HookCommand>,
    pub skill_root: Option<String>,
}

/// Function hook matcher
#[derive(Clone)]
pub struct FunctionHookMatcher {
    pub matcher: String,
    pub hooks: Vec<FunctionHook>,
}

/// Get all session hooks for a specific event (excluding function hooks)
pub fn get_session_hooks(
    _session_id: &str,
    event: Option<&HookEvent>,
) -> HashMap<HookEvent, Vec<SessionDerivedHookMatcher>> {
    let state = SESSION_HOOKS_STATE.lock().unwrap();
    let store = match state.hooks.get(_session_id) {
        Some(s) => s,
        None => return HashMap::new(),
    };

    let mut result = HashMap::new();

    if let Some(event) = event {
        if let Some(session_matchers) = store.hooks.get(event) {
            let derived_matchers = convert_to_hook_matchers(session_matchers);
            if !derived_matchers.is_empty() {
                result.insert(event.clone(), derived_matchers);
            }
        }
    } else {
        for (evt, session_matchers) in &store.hooks {
            let derived_matchers = convert_to_hook_matchers(session_matchers);
            if !derived_matchers.is_empty() {
                result.insert(evt.clone(), derived_matchers);
            }
        }
    }

    result
}

/// Get all session function hooks for a specific event
pub fn get_session_function_hooks(
    session_id: &str,
    event: Option<&HookEvent>,
) -> HashMap<HookEvent, Vec<FunctionHookMatcher>> {
    let state = SESSION_HOOKS_STATE.lock().unwrap();
    let store = match state.hooks.get(session_id) {
        Some(s) => s,
        None => return HashMap::new(),
    };

    let mut result = HashMap::new();

    let extract_function_hooks =
        |session_matchers: &[SessionHookMatcher]| -> Vec<FunctionHookMatcher> {
            session_matchers
                .iter()
                .map(|sm| {
                    let function_hooks: Vec<FunctionHook> = sm
                        .hooks
                        .iter()
                        .filter_map(|entry| {
                            if let SessionHookCommand::Function(ref fh) = entry.hook {
                                Some(fh.clone())
                            } else {
                                None
                            }
                        })
                        .collect();
                    FunctionHookMatcher {
                        matcher: sm.matcher.clone(),
                        hooks: function_hooks,
                    }
                })
                .filter(|m| !m.hooks.is_empty())
                .collect()
        };

    if let Some(event) = event {
        if let Some(session_matchers) = store.hooks.get(event) {
            let function_matchers = extract_function_hooks(session_matchers);
            if !function_matchers.is_empty() {
                result.insert(event.clone(), function_matchers);
            }
        }
    } else {
        for (evt, session_matchers) in &store.hooks {
            let function_matchers = extract_function_hooks(session_matchers);
            if !function_matchers.is_empty() {
                result.insert(evt.clone(), function_matchers);
            }
        }
    }

    result
}

/// Get the full hook entry (including callbacks) for a specific session hook
pub fn get_session_hook_callback(
    session_id: &str,
    event: &HookEvent,
    matcher: &str,
    hook: &HookCommand,
) -> Option<SessionHookEntry> {
    let state = SESSION_HOOKS_STATE.lock().unwrap();
    let store = state.hooks.get(session_id)?;
    let event_matchers = store.hooks.get(event)?;

    // Find the hook in the matchers
    for matcher_entry in event_matchers {
        if matcher_entry.matcher == matcher || matcher.is_empty() {
            for entry in &matcher_entry.hooks {
                if let SessionHookCommand::Regular(ref regular_hook) = entry.hook {
                    if is_hook_equal(regular_hook, hook) {
                        return Some(entry.clone());
                    }
                }
            }
        }
    }

    None
}

/// Clear all session hooks for a specific session
pub fn clear_session_hooks(
    set_app_state: &dyn Fn(&dyn Fn(&mut serde_json::Value)),
    session_id: &str,
) {
    // Call set_app_state to notify state change (matches TypeScript behavior)
    set_app_state(&|state: &mut serde_json::Value| {
        if let Some(session_hooks) = state.get_mut("session_hooks") {
            if let Some(session_map) = session_hooks.as_object_mut() {
                session_map.remove(session_id);
            }
        }
    });

    let mut state = SESSION_HOOKS_STATE.lock().unwrap();
    state.hooks.remove(session_id);

    log_for_debugging(&format!(
        "Cleared all session hooks for session {}",
        session_id
    ));
}

/// Convert session hook matchers to regular hook matchets
fn convert_to_hook_matchers(
    session_matchers: &[SessionHookMatcher],
) -> Vec<SessionDerivedHookMatcher> {
    session_matchers
        .iter()
        .map(|sm| SessionDerivedHookMatcher {
            matcher: sm.matcher.clone(),
            skill_root: sm.skill_root.clone(),
            // Filter out function hooks - they can't be persisted to HookMatcher format
            hooks: sm
                .hooks
                .iter()
                .filter_map(|entry| {
                    if let SessionHookCommand::Regular(ref h) = entry.hook {
                        Some(h.clone())
                    } else {
                        None
                    }
                })
                .collect(),
        })
        .collect()
}

/// Log for debugging
fn log_for_debugging(msg: &str) {
    log::debug!("{}", msg);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add_and_get_session_hook() {
        // Clean up any leftover state from other tests
        {
            let mut state = SESSION_HOOKS_STATE.lock().unwrap();
            state.hooks.remove("test-session");
        }

        let hook = HookCommand::Command {
            command: "echo test".to_string(),
            shell: None,
            if_condition: None,
            timeout: None,
            status_message: None,
            once: None,
            r#async: None,
            async_rewake: None,
        };

        // Use the internal state directly for testing
        let mut state = SESSION_HOOKS_STATE.lock().unwrap();
        let store = state
            .hooks
            .entry("test-session".to_string())
            .or_insert_with(SessionStore::default);

        store
            .hooks
            .entry(HookEvent::Stop)
            .or_default()
            .push(SessionHookMatcher {
                matcher: String::new(),
                skill_root: None,
                hooks: vec![SessionHookEntry {
                    hook: SessionHookCommand::Regular(hook.clone()),
                    on_hook_success: None,
                }],
            });

        // Verify it was added
        let store = state.hooks.get("test-session").unwrap();
        let stop_hooks = store.hooks.get(&HookEvent::Stop).unwrap();
        assert_eq!(stop_hooks.len(), 1);
    }

    #[test]
    fn test_clear_session_hooks() {
        // Clean up any leftover state from other tests
        {
            let mut state = SESSION_HOOKS_STATE.lock().unwrap();
            state.hooks.remove("clear-test-session");
        }

        // Add some hooks first
        {
            let mut state = SESSION_HOOKS_STATE.lock().unwrap();
            let store = state
                .hooks
                .entry("clear-test-session".to_string())
                .or_insert_with(SessionStore::default);

            store
                .hooks
                .entry(HookEvent::Stop)
                .or_default()
                .push(SessionHookMatcher {
                    matcher: String::new(),
                    skill_root: None,
                    hooks: vec![SessionHookEntry {
                        hook: SessionHookCommand::Regular(HookCommand::Command {
                            command: "echo test".to_string(),
                            shell: None,
                            if_condition: None,
                            timeout: None,
                            status_message: None,
                            once: None,
                            r#async: None,
                            async_rewake: None,
                        }),
                        on_hook_success: None,
                    }],
                });
        }

        // Clear them
        let _set_app_state = |_: &dyn Fn(&mut serde_json::Value)| {};
        clear_session_hooks(&_set_app_state, "clear-test-session");

        // Verify they were cleared
        let state = SESSION_HOOKS_STATE.lock().unwrap();
        assert!(state.hooks.get("clear-test-session").is_none());
    }

    #[test]
    fn test_function_hook() {
        let callback = Arc::new(|_messages: &[serde_json::Value]| true);
        let hook = FunctionHook::new(
            Some("test-fn-hook".to_string()),
            Some(5000),
            callback,
            "Function hook failed".to_string(),
        );

        assert_eq!(hook.id, Some("test-fn-hook".to_string()));
        assert_eq!(hook.timeout, Some(5000));
    }
}