Skip to main content

nexus_memory_storage/
models.rs

1//! Database models for SQLx
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use sqlx::FromRow;
6
7/// Database row for memories table
8#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
9pub struct MemoryRow {
10    pub id: i64,
11    pub namespace_id: i64,
12    pub content: String,
13    pub category: String,
14    pub memory_lane_type: Option<String>,
15    pub labels: String,   // JSON array
16    pub metadata: String, // JSON object
17    pub similarity_score: Option<f32>,
18    pub relevance_score: Option<f32>,
19    pub content_embedding: Option<String>, // JSON array of f32
20    pub embedding_model: Option<String>,
21    pub created_at: DateTime<Utc>,
22    pub updated_at: Option<DateTime<Utc>>,
23    pub last_accessed: Option<DateTime<Utc>>,
24    pub is_active: bool,
25    pub is_archived: bool,
26    pub access_count: i64,
27}
28
29/// Database row for agent_namespaces table
30#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
31pub struct AgentNamespaceRow {
32    pub id: i64,
33    pub name: String,
34    pub description: Option<String>,
35    pub agent_type: String,
36    pub created_at: DateTime<Utc>,
37    pub updated_at: Option<DateTime<Utc>>,
38}
39
40/// Database row for task_specifications table
41#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
42pub struct TaskSpecificationRow {
43    pub id: i64,
44    pub namespace_id: i64,
45    pub spec_id: String,
46    pub task_description: String,
47    pub spec_content: String, // JSON
48    pub complexity_score: f32,
49    pub usage_count: i64,
50    pub success_rate: f32,
51    pub created_at: DateTime<Utc>,
52    pub updated_at: Option<DateTime<Utc>>,
53}
54
55/// Database row for memory_relations table
56#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
57pub struct MemoryRelationRow {
58    pub id: i64,
59    pub source_memory_id: i64,
60    pub target_memory_id: i64,
61    pub relation_type: String,
62    pub strength: f32,
63    pub metadata: Option<String>, // JSON
64    pub created_at: DateTime<Utc>,
65}
66
67/// Database row for system_metrics table
68#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
69pub struct SystemMetricRow {
70    pub id: i64,
71    pub metric_name: String,
72    pub metric_value: f64,
73    pub labels: String, // JSON
74    pub recorded_at: DateTime<Utc>,
75}
76
77/// Represents a processed file record for inbox deduplication
78#[derive(Debug, Clone, sqlx::FromRow)]
79pub struct ProcessedFileRow {
80    pub id: i64,
81    pub namespace_id: i64,
82    pub path: String,
83    pub content_hash: Option<String>,
84    pub status: String,
85    pub memory_id: Option<i64>,
86    pub last_error: Option<String>,
87    pub processed_at: Option<chrono::DateTime<chrono::Utc>>,
88    pub created_at: chrono::DateTime<chrono::Utc>,
89    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
90}
91
92/// Status values for processed files
93pub mod processed_file_status {
94    pub const PENDING: &str = "pending";
95    pub const PROCESSING: &str = "processing";
96    pub const COMPLETED: &str = "completed";
97    pub const FAILED: &str = "failed";
98}
99
100// ---------------------------------------------------------------------------
101// Cognition tables (Phase 2)
102// ---------------------------------------------------------------------------
103
104/// Database row for memory_jobs table
105#[derive(Debug, Clone, sqlx::FromRow)]
106pub struct MemoryJobRow {
107    pub id: i64,
108    pub namespace_id: i64,
109    pub job_type: String,
110    pub status: String,
111    pub priority: i64,
112    pub perspective_json: Option<String>,
113    pub payload_json: String,
114    pub lease_owner: Option<String>,
115    pub claim_token: Option<String>,
116    pub lease_expires_at: Option<String>,
117    pub attempts: i64,
118    pub last_error: Option<String>,
119    pub created_at: String,
120    pub updated_at: String,
121}
122
123/// Database row for session_digests table
124#[derive(Debug, Clone, sqlx::FromRow)]
125pub struct SessionDigestRow {
126    pub id: i64,
127    pub namespace_id: i64,
128    pub session_key: String,
129    pub digest_kind: String,
130    pub memory_id: i64,
131    pub start_memory_id: Option<i64>,
132    pub end_memory_id: Option<i64>,
133    pub token_count: i64,
134    pub created_at: String,
135}
136
137/// Database row for memory_evidence table
138#[derive(Debug, Clone, sqlx::FromRow)]
139pub struct MemoryEvidenceRow {
140    pub id: i64,
141    pub derived_memory_id: i64,
142    pub source_memory_id: i64,
143    pub evidence_role: String,
144    pub created_at: String,
145}
146
147/// Status values for memory jobs
148pub mod memory_job_status {
149    pub const PENDING: &str = "pending";
150    pub const RUNNING: &str = "running";
151    pub const COMPLETED: &str = "completed";
152    pub const FAILED: &str = "failed";
153}
154
155/// Evidence role constants
156pub mod evidence_role {
157    pub const SOURCE: &str = "source";
158    pub const DERIVED_FROM: &str = "derived_from";
159    pub const CONTRADICTS: &str = "contradicts";
160    pub const SUPPORTS: &str = "supports";
161}
162
163/// A claimed job with deserialized perspective and payload.
164#[derive(Debug, Clone)]
165pub struct ClaimedMemoryJob {
166    pub row: MemoryJobRow,
167    pub perspective: Option<nexus_core::PerspectiveKey>,
168    pub payload: serde_json::Value,
169}
170
171/// Parameters for enqueuing a new memory job.
172pub struct EnqueueJobParams<'a> {
173    pub namespace_id: i64,
174    pub job_type: &'a str,
175    pub priority: i64,
176    pub perspective: Option<&'a serde_json::Value>,
177    pub payload: &'a serde_json::Value,
178}
179
180/// Lineage info for a derived memory.
181#[derive(Debug, Clone)]
182pub struct MemoryLineageEntry {
183    pub derived_memory_id: i64,
184    pub source_memory_id: i64,
185    pub evidence_role: String,
186}