nexus-memory-hooks 1.1.2

Agent hooks system for Nexus Memory System - automated memory extraction
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
//! Session context for extracted data

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Extracted session context from an agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionContext {
    /// Agent type that created this context
    pub agent_type: String,

    /// When extraction started
    pub extraction_started: DateTime<Utc>,

    /// When extraction completed
    pub extraction_completed: Option<DateTime<Utc>>,

    /// Session ID (if available)
    pub session_id: Option<String>,

    /// Conversation messages
    pub conversation: Vec<ConversationMessage>,

    /// Decisions made during session
    pub decisions: Vec<Decision>,

    /// Files worked on
    pub files: Vec<FileInfo>,

    /// Tasks completed
    pub tasks: Vec<TaskInfo>,

    /// Key insights/learnings
    pub insights: Vec<String>,

    /// Errors encountered
    pub errors: Vec<ErrorInfo>,

    /// Subagent executions (for pi-mono, oh-my-pi)
    pub subagent_executions: Vec<SubagentExecution>,

    /// Commands run
    pub commands_run: Vec<String>,

    /// Custom context data
    pub custom: HashMap<String, serde_json::Value>,

    /// Source of extraction
    pub extraction_source: String,

    /// Reliability score (0.0-1.0)
    pub reliability_score: f32,
}

impl Default for SessionContext {
    fn default() -> Self {
        Self {
            agent_type: String::new(),
            extraction_started: Utc::now(),
            extraction_completed: None,
            session_id: None,
            conversation: Vec::new(),
            decisions: Vec::new(),
            files: Vec::new(),
            tasks: Vec::new(),
            insights: Vec::new(),
            errors: Vec::new(),
            subagent_executions: Vec::new(),
            commands_run: Vec::new(),
            custom: HashMap::new(),
            extraction_source: "unknown".to_string(),
            reliability_score: 1.0,
        }
    }
}

impl SessionContext {
    /// Create a new session context for an agent
    pub fn new(agent_type: impl Into<String>) -> Self {
        Self {
            agent_type: agent_type.into(),
            ..Default::default()
        }
    }

    /// Create with extraction source
    pub fn with_source(mut self, source: impl Into<String>) -> Self {
        self.extraction_source = source.into();
        self
    }

    /// Create with reliability score
    pub fn with_reliability(mut self, score: f32) -> Self {
        self.reliability_score = score.clamp(0.0, 1.0);
        self
    }

    /// Mark extraction as complete
    pub fn complete(&mut self) {
        self.extraction_completed = Some(Utc::now());
    }

    /// Add a conversation message
    pub fn add_message(&mut self, role: impl Into<String>, content: impl Into<String>) {
        self.conversation.push(ConversationMessage {
            role: role.into(),
            content: content.into(),
            timestamp: Utc::now(),
        });
    }

    /// Add a decision
    pub fn add_decision(&mut self, decision: Decision) {
        self.decisions.push(decision);
    }

    /// Add a file
    pub fn add_file(&mut self, file: FileInfo) {
        self.files.push(file);
    }

    /// Add a task
    pub fn add_task(&mut self, task: TaskInfo) {
        self.tasks.push(task);
    }

    /// Add an insight
    pub fn add_insight(&mut self, insight: impl Into<String>) {
        self.insights.push(insight.into());
    }

    /// Add an error
    pub fn add_error(&mut self, error: ErrorInfo) {
        self.errors.push(error);
    }

    /// Add a command
    pub fn add_command(&mut self, command: impl Into<String>) {
        self.commands_run.push(command.into());
    }

    /// Add custom data
    pub fn add_custom(&mut self, key: impl Into<String>, value: serde_json::Value) {
        self.custom.insert(key.into(), value);
    }

    /// Check if context is empty
    pub fn is_empty(&self) -> bool {
        self.conversation.is_empty()
            && self.decisions.is_empty()
            && self.files.is_empty()
            && self.tasks.is_empty()
            && self.insights.is_empty()
            && self.errors.is_empty()
            && self.commands_run.is_empty()
    }

    /// Get summary statistics
    pub fn stats(&self) -> SessionStats {
        SessionStats {
            message_count: self.conversation.len(),
            decision_count: self.decisions.len(),
            file_count: self.files.len(),
            task_count: self.tasks.len(),
            insight_count: self.insights.len(),
            error_count: self.errors.len(),
            command_count: self.commands_run.len(),
        }
    }

    /// Convert to memory content string
    pub fn to_memory_content(&self) -> String {
        let mut parts = Vec::new();

        parts.push(format!("Agent: {}", self.agent_type));
        parts.push(format!("Source: {}", self.extraction_source));
        parts.push(format!(
            "Reliability: {:.0}%",
            self.reliability_score * 100.0
        ));

        if !self.conversation.is_empty() {
            parts.push(format!(
                "\nConversation: {} messages",
                self.conversation.len()
            ));
        }

        if !self.decisions.is_empty() {
            parts.push(format!("\nDecisions: {}", self.decisions.len()));
            for decision in &self.decisions {
                parts.push(format!("  - {}", decision.summary));
            }
        }

        if !self.files.is_empty() {
            parts.push(format!("\nFiles: {}", self.files.len()));
            for file in &self.files {
                parts.push(format!("  - {} ({})", file.path, file.action));
            }
        }

        if !self.insights.is_empty() {
            parts.push(format!("\nInsights: {}", self.insights.len()));
            for insight in &self.insights {
                parts.push(format!("  - {}", insight));
            }
        }

        if !self.errors.is_empty() {
            parts.push(format!("\nErrors: {}", self.errors.len()));
            for error in &self.errors {
                parts.push(format!("  - {}: {}", error.error_type, error.message));
            }
        }

        parts.join("\n")
    }
}

/// Conversation message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationMessage {
    pub role: String,
    pub content: String,
    pub timestamp: DateTime<Utc>,
}

/// Decision made during session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Decision {
    pub summary: String,
    pub rationale: Option<String>,
    pub alternatives: Vec<String>,
    pub impact: Option<String>,
    pub timestamp: DateTime<Utc>,
}

impl Decision {
    pub fn new(summary: impl Into<String>) -> Self {
        Self {
            summary: summary.into(),
            rationale: None,
            alternatives: Vec::new(),
            impact: None,
            timestamp: Utc::now(),
        }
    }
}

/// File operation info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileInfo {
    pub path: String,
    pub action: FileAction,
    pub lines_added: Option<usize>,
    pub lines_removed: Option<usize>,
    pub timestamp: DateTime<Utc>,
}

impl FileInfo {
    pub fn new(path: impl Into<String>, action: FileAction) -> Self {
        Self {
            path: path.into(),
            action,
            lines_added: None,
            lines_removed: None,
            timestamp: Utc::now(),
        }
    }
}

/// File action type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FileAction {
    Created,
    Modified,
    Deleted,
    Read,
}

impl std::fmt::Display for FileAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FileAction::Created => write!(f, "created"),
            FileAction::Modified => write!(f, "modified"),
            FileAction::Deleted => write!(f, "deleted"),
            FileAction::Read => write!(f, "read"),
        }
    }
}

/// Task info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskInfo {
    pub description: String,
    pub status: TaskStatus,
    pub started_at: Option<DateTime<Utc>>,
    pub completed_at: Option<DateTime<Utc>>,
    pub subagent: Option<String>,
}

impl TaskInfo {
    pub fn new(description: impl Into<String>) -> Self {
        Self {
            description: description.into(),
            status: TaskStatus::Pending,
            started_at: None,
            completed_at: None,
            subagent: None,
        }
    }
}

/// Task status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TaskStatus {
    Pending,
    InProgress,
    Completed,
    Failed,
}

/// Error info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorInfo {
    pub error_type: String,
    pub message: String,
    pub stack_trace: Option<String>,
    pub timestamp: DateTime<Utc>,
}

impl ErrorInfo {
    pub fn new(error_type: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            error_type: error_type.into(),
            message: message.into(),
            stack_trace: None,
            timestamp: Utc::now(),
        }
    }
}

/// Subagent execution (for pi-mono, oh-my-pi)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubagentExecution {
    pub subagent_type: String,
    pub task: String,
    pub status: String,
    pub started_at: DateTime<Utc>,
    pub completed_at: Option<DateTime<Utc>>,
    pub result_summary: Option<String>,
}

/// Session statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionStats {
    pub message_count: usize,
    pub decision_count: usize,
    pub file_count: usize,
    pub task_count: usize,
    pub insight_count: usize,
    pub error_count: usize,
    pub command_count: usize,
}

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

    #[test]
    fn test_session_context_new() {
        let ctx = SessionContext::new("claude-code");
        assert_eq!(ctx.agent_type, "claude-code");
        assert!(ctx.is_empty());
    }

    #[test]
    fn test_session_context_add_items() {
        let mut ctx = SessionContext::new("test");

        ctx.add_message("user", "Hello");
        ctx.add_message("assistant", "Hi there!");
        ctx.add_decision(Decision::new("Use Rust"));
        ctx.add_file(FileInfo::new("/src/main.rs", FileAction::Created));
        ctx.add_insight("Rust is fast");
        ctx.add_command("cargo build");

        assert!(!ctx.is_empty());
        assert_eq!(ctx.conversation.len(), 2);
        assert_eq!(ctx.decisions.len(), 1);
        assert_eq!(ctx.files.len(), 1);
        assert_eq!(ctx.insights.len(), 1);
        assert_eq!(ctx.commands_run.len(), 1);
    }

    #[test]
    fn test_session_context_to_memory_content() {
        let mut ctx = SessionContext::new("claude-code")
            .with_source("native")
            .with_reliability(0.95);

        ctx.add_insight("Test insight");

        let content = ctx.to_memory_content();
        assert!(content.contains("claude-code"));
        assert!(content.contains("native"));
        assert!(content.contains("95%"));
        assert!(content.contains("Test insight"));
    }

    #[test]
    fn test_session_stats() {
        let mut ctx = SessionContext::new("test");
        ctx.add_message("user", "test");
        ctx.add_decision(Decision::new("decide"));
        ctx.add_error(ErrorInfo::new("test", "error"));

        let stats = ctx.stats();
        assert_eq!(stats.message_count, 1);
        assert_eq!(stats.decision_count, 1);
        assert_eq!(stats.error_count, 1);
    }
}