1use std::sync::Arc;
4
5use super::{Domain, MemoryId, Namespace};
6use crate::current_timestamp;
7use uuid::Uuid;
8
9#[derive(Debug, Clone)]
11pub struct EventMeta {
12 pub event_id: String,
14 pub correlation_id: Option<String>,
16 pub source: &'static str,
18 pub timestamp: u64,
20}
21
22impl EventMeta {
23 #[must_use]
25 pub fn new(source: &'static str, correlation_id: Option<String>) -> Self {
26 Self::with_timestamp(source, correlation_id, current_timestamp())
27 }
28
29 #[must_use]
31 pub fn with_timestamp(
32 source: &'static str,
33 correlation_id: Option<String>,
34 timestamp: u64,
35 ) -> Self {
36 Self {
37 event_id: Uuid::new_v4().to_string(),
38 correlation_id,
39 source,
40 timestamp,
41 }
42 }
43}
44
45#[derive(Debug, Clone)]
47pub enum MemoryEvent {
48 Captured {
50 meta: EventMeta,
52 memory_id: MemoryId,
54 namespace: Namespace,
56 domain: Domain,
58 content_length: usize,
60 },
61 Retrieved {
63 meta: EventMeta,
65 memory_id: MemoryId,
67 query: Arc<str>,
69 score: f32,
71 },
72 Updated {
74 meta: EventMeta,
76 memory_id: MemoryId,
78 modified_fields: Vec<String>,
80 },
81 Archived {
83 meta: EventMeta,
85 memory_id: MemoryId,
87 reason: String,
89 },
90 Deleted {
92 meta: EventMeta,
94 memory_id: MemoryId,
96 reason: String,
98 },
99 Redacted {
101 meta: EventMeta,
103 memory_id: MemoryId,
105 redaction_type: String,
107 },
108 Synced {
110 meta: EventMeta,
112 pushed: usize,
114 pulled: usize,
116 conflicts: usize,
118 },
119 Consolidated {
121 meta: EventMeta,
123 processed: usize,
125 archived: usize,
127 merged: usize,
129 },
130 McpStarted {
132 meta: EventMeta,
134 transport: String,
136 port: Option<u16>,
138 },
139 McpAuthFailed {
141 meta: EventMeta,
143 client_id: Option<String>,
145 reason: String,
147 },
148 McpToolExecuted {
150 meta: EventMeta,
152 tool_name: String,
154 status: String,
156 duration_ms: u64,
158 error: Option<String>,
160 },
161 McpRequestError {
163 meta: EventMeta,
165 operation: String,
167 error: String,
169 },
170 HookInvoked {
172 meta: EventMeta,
174 hook: String,
176 },
177 HookClassified {
179 meta: EventMeta,
181 hook: String,
183 classification: String,
185 classifier: String,
187 confidence: f32,
189 },
190 HookCaptureDecision {
192 meta: EventMeta,
194 hook: String,
196 decision: String,
198 namespace: Option<String>,
200 memory_id: Option<MemoryId>,
202 },
203 HookFailed {
205 meta: EventMeta,
207 hook: String,
209 error: String,
211 },
212}
213
214impl MemoryEvent {
215 #[must_use]
217 pub const fn event_type(&self) -> &'static str {
218 match self {
219 Self::Captured { .. } => "captured",
220 Self::Retrieved { .. } => "retrieved",
221 Self::Updated { .. } => "updated",
222 Self::Archived { .. } => "archived",
223 Self::Deleted { .. } => "deleted",
224 Self::Redacted { .. } => "redacted",
225 Self::Synced { .. } => "synced",
226 Self::Consolidated { .. } => "consolidated",
227 Self::McpStarted { .. } => "mcp.started",
228 Self::McpAuthFailed { .. } => "mcp.auth_failed",
229 Self::McpToolExecuted { .. } => "mcp.tool_executed",
230 Self::McpRequestError { .. } => "mcp.request_error",
231 Self::HookInvoked { .. } => "hook.invoked",
232 Self::HookClassified { .. } => "hook.classified",
233 Self::HookCaptureDecision { .. } => "hook.capture_decision",
234 Self::HookFailed { .. } => "hook.failed",
235 }
236 }
237
238 #[must_use]
240 pub const fn timestamp(&self) -> u64 {
241 match self {
242 Self::Captured { meta, .. }
243 | Self::Retrieved { meta, .. }
244 | Self::Updated { meta, .. }
245 | Self::Archived { meta, .. }
246 | Self::Deleted { meta, .. }
247 | Self::Redacted { meta, .. }
248 | Self::Synced { meta, .. }
249 | Self::Consolidated { meta, .. }
250 | Self::McpStarted { meta, .. }
251 | Self::McpAuthFailed { meta, .. }
252 | Self::McpToolExecuted { meta, .. }
253 | Self::McpRequestError { meta, .. }
254 | Self::HookInvoked { meta, .. }
255 | Self::HookClassified { meta, .. }
256 | Self::HookCaptureDecision { meta, .. }
257 | Self::HookFailed { meta, .. } => meta.timestamp,
258 }
259 }
260
261 #[must_use]
263 pub const fn meta(&self) -> &EventMeta {
264 match self {
265 Self::Captured { meta, .. }
266 | Self::Retrieved { meta, .. }
267 | Self::Updated { meta, .. }
268 | Self::Archived { meta, .. }
269 | Self::Deleted { meta, .. }
270 | Self::Redacted { meta, .. }
271 | Self::Synced { meta, .. }
272 | Self::Consolidated { meta, .. }
273 | Self::McpStarted { meta, .. }
274 | Self::McpAuthFailed { meta, .. }
275 | Self::McpToolExecuted { meta, .. }
276 | Self::McpRequestError { meta, .. }
277 | Self::HookInvoked { meta, .. }
278 | Self::HookClassified { meta, .. }
279 | Self::HookCaptureDecision { meta, .. }
280 | Self::HookFailed { meta, .. } => meta,
281 }
282 }
283}