Skip to main content

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**: Extension-based (TypeScript) — session start, end, checkpoint, error, 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 injection;
66pub mod monitor;
67pub mod persistence;
68pub mod rescorer;
69pub mod retrieval;
70pub mod retry_buffer;
71pub mod session;
72pub mod signal;
73pub mod sync_state;
74pub mod transcript;
75pub mod types;
76
77// Re-export main types
78pub use base::{AgentHook, HookResult, LifecycleCapabilities};
79pub use buffer::PersistentBuffer;
80pub use candidate::{derive_candidates, MemoryCandidate};
81pub use claude_payload::{
82    flatten_text_value, normalize_claude_payload, normalize_generic_payload, normalize_payload,
83    NormalizedHookEvent,
84};
85pub use detector::InactivityDetector;
86pub use enrichment::{EnrichedMemory, EnrichmentBatchResult, EnrichmentService};
87pub use error::{HookError, Result};
88pub use extractor::MultiLayerExtractor;
89pub use factory::HookFactory;
90pub use monitor::{ProcessMonitor, SessionMonitor};
91pub use persistence::{persist_enriched_memories, PersistResult};
92pub use retrieval::{RetrievalEngine, RetrievalResult, SubconsciousMode};
93pub use retry_buffer::{RetryArtifact, RetryBuffer};
94pub use session::SessionContext;
95pub use sync_state::SyncState;
96pub use transcript::{read_transcript, read_transcript_from, TranscriptEntry};
97pub use types::{AgentType, DetectionLayer, ExtractionSource, SessionActivity, SupportTier};
98
99// Re-export agent hooks
100pub use agents::{
101    CLIHook, ClaudeCodeHook, DroidHook, GeminiHook, OhMyPiHook, PiMonoHook, PiSkillsHook, QwenHook,
102};
103
104/// Hook version
105pub const VERSION: &str = env!("CARGO_PKG_VERSION");
106
107/// Default inactivity timeout in seconds (5 minutes)
108pub const DEFAULT_INACTIVITY_TIMEOUT_SECS: u64 = 300;
109
110/// Default buffer flush interval in seconds
111pub const DEFAULT_BUFFER_FLUSH_INTERVAL_SECS: u64 = 10;
112
113/// Default process polling interval in seconds
114pub const DEFAULT_POLLING_INTERVAL_SECS: u64 = 5;