Skip to main content

second_brain_core/
schema.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum MemoryType {
8    Episodic,
9    Semantic,
10    Procedural,
11    Decision,
12    Architecture,
13    Debugging,
14    Task,
15    Question,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct Memory {
20    pub id: Uuid,
21    pub content: String,
22    pub embedding: Vec<f32>,
23    pub memory_type: MemoryType,
24    pub confidence: f32,
25    pub created_at: DateTime<Utc>,
26    pub last_accessed: DateTime<Utc>,
27    pub access_count: u32,
28    pub source: String,
29    pub source_id: String,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct Entity {
34    pub id: Uuid,
35    pub name: String,
36    pub entity_type: String,
37    pub embedding: Vec<f32>,
38    pub aliases: Vec<String>,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct Conversation {
43    pub id: Uuid,
44    pub source: String,
45    pub machine_id: String,
46    pub started_at: DateTime<Utc>,
47    pub project_path: Option<String>,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
51#[serde(rename_all = "snake_case")]
52pub enum RelationType {
53    RelatesTo,
54    Mentions,
55    DerivedFrom,
56    Contradicts,
57    Reinforces,
58    Supersedes,
59    DistilledFrom,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct Relation {
64    pub from_id: Uuid,
65    pub to_id: Uuid,
66    pub relation_type: RelationType,
67    pub strength: f32,
68    pub context: Option<String>,
69}
70
71impl Memory {
72    pub fn new(
73        content: String,
74        memory_type: MemoryType,
75        source: String,
76        source_id: String,
77    ) -> Self {
78        let now = Utc::now();
79        Self {
80            id: Uuid::new_v4(),
81            content,
82            embedding: Vec::new(),
83            memory_type,
84            confidence: 1.0,
85            created_at: now,
86            last_accessed: now,
87            access_count: 0,
88            source,
89            source_id,
90        }
91    }
92
93    pub fn reinforce(&mut self) {
94        self.access_count += 1;
95        self.last_accessed = Utc::now();
96        self.confidence = (self.confidence + 0.1).min(1.0);
97    }
98}
99
100impl Entity {
101    pub fn new(name: String, entity_type: String) -> Self {
102        Self {
103            id: Uuid::new_v4(),
104            name,
105            entity_type,
106            embedding: Vec::new(),
107            aliases: Vec::new(),
108        }
109    }
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
113#[serde(rename_all = "snake_case")]
114pub enum SyncOp {
115    Create,
116    Update,
117    Delete,
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
121#[serde(rename_all = "snake_case")]
122pub enum SyncNodeType {
123    Memory,
124    Entity,
125    Conversation,
126    Relation,
127}
128
129#[derive(Debug, Clone, Serialize, Deserialize)]
130pub struct SyncEntry {
131    pub seq: u64,
132    pub op: SyncOp,
133    pub node_type: SyncNodeType,
134    pub node_id: String,
135    pub machine_id: String,
136    pub timestamp: DateTime<Utc>,
137    pub data: Option<String>,
138}
139
140#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct SyncState {
142    pub peer_id: String,
143    pub last_seq: u64,
144    pub last_sync_at: DateTime<Utc>,
145}