nexus_memory_hooks/lib.rs
1//! Nexus Hooks - Agent hooks system for automated memory extraction
2//!
3//! This crate provides a four-layer extraction system for capturing
4//! agent session context with 95-100% reliability:
5//!
6//! 1. **Native Hooks** (98-100%): Claude Skills, pi-mono, oh-my-pi, pi-skills
7//! 2. **Session Monitor** (95%): Process monitoring via sysinfo
8//! 3. **Inactivity Detector** (90%): Configurable timeout detection
9//! 4. **Persistent Buffer** (99%): Crash recovery from buffer
10//!
11//! # Supported Agents
12//!
13//! ## Native Lifecycle (dedicated hook implementation + skill installation)
14//!
15//! - **Claude Code**: Skills-based (SKILL.md format) — session start, end, checkpoint, error, compact
16//! - **pi-mono**: Skills-based (TypeScript/Bun) — session end, checkpoint, compact
17//! - **oh-my-pi**: Skills-based (TypeScript/Bun + Rust N-API) — session end, checkpoint, error, compact
18//! - **pi-skills**: Cross-compatible skills — session end, checkpoint, compact
19//!
20//! ## Monitor Only (process detection, no native hooks)
21//!
22//! - **Gemini**: Process monitoring only (function calling not yet wired)
23//! - **Qwen**: Process monitoring only (hooks subagent not yet wired)
24//!
25//! ## Wrapper Lifecycle (generic CLI wrapper, atexit + process detection)
26//!
27//! - **CLI Agents**: Amp, Droid, OpenCode, Codex, Hermes (shared CLIHook implementation)
28//!
29//! # Example
30//!
31//! ```rust,no_run
32//! use nexus_memory_hooks::{HookFactory, AgentHook, MultiLayerExtractor};
33//!
34//! #[tokio::main]
35//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
36//! // Create hook for specific agent
37//! let factory = HookFactory::new();
38//! let mut hook = factory.create_hook("claude-code")?;
39//!
40//! // Check if session is active
41//! let activity = hook.detect_session_activity().await?;
42//! println!("Session active: {}", activity.is_active);
43//!
44//! // Extract session context
45//! if activity.is_active {
46//! let context = hook.extract_session_context().await?;
47//! println!("Extracted context: {:?}", context);
48//! }
49//!
50//! Ok(())
51//! }
52//! ```
53
54pub mod agents;
55pub mod base;
56pub mod buffer;
57pub mod candidate;
58pub mod claude_payload;
59pub mod detector;
60pub mod enrichment;
61pub mod error;
62pub mod extractor;
63pub mod factory;
64pub mod monitor;
65pub mod persistence;
66pub mod retry_buffer;
67pub mod session;
68pub mod signal;
69pub mod types;
70
71// Re-export main types
72pub use base::{AgentHook, HookResult, LifecycleCapabilities};
73pub use buffer::PersistentBuffer;
74pub use candidate::{derive_candidates, MemoryCandidate};
75pub use claude_payload::{
76 flatten_text_value, normalize_claude_payload, normalize_generic_payload, normalize_payload,
77 NormalizedHookEvent,
78};
79pub use detector::InactivityDetector;
80pub use enrichment::{EnrichedMemory, EnrichmentBatchResult, EnrichmentService};
81pub use error::{HookError, Result};
82pub use extractor::MultiLayerExtractor;
83pub use factory::HookFactory;
84pub use monitor::{ProcessMonitor, SessionMonitor};
85pub use persistence::{persist_enriched_memories, PersistResult};
86pub use retry_buffer::{RetryArtifact, RetryBuffer};
87pub use session::SessionContext;
88pub use types::{AgentType, DetectionLayer, ExtractionSource, SessionActivity, SupportTier};
89
90// Re-export agent hooks
91pub use agents::{
92 CLIHook, ClaudeCodeHook, GeminiHook, OhMyPiHook, PiMonoHook, PiSkillsHook, QwenHook,
93};
94
95/// Hook version
96pub const VERSION: &str = env!("CARGO_PKG_VERSION");
97
98/// Default inactivity timeout in seconds (5 minutes)
99pub const DEFAULT_INACTIVITY_TIMEOUT_SECS: u64 = 300;
100
101/// Default buffer flush interval in seconds
102pub const DEFAULT_BUFFER_FLUSH_INTERVAL_SECS: u64 = 10;
103
104/// Default process polling interval in seconds
105pub const DEFAULT_POLLING_INTERVAL_SECS: u64 = 5;