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