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    #[serde(default)]
31    pub project_path: Option<String>,
32    #[serde(default)]
33    pub machine_id: String,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct Entity {
38    pub id: Uuid,
39    pub name: String,
40    pub entity_type: String,
41    pub embedding: Vec<f32>,
42    pub aliases: Vec<String>,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct Conversation {
47    pub id: Uuid,
48    pub source: String,
49    pub machine_id: String,
50    pub started_at: DateTime<Utc>,
51    pub project_path: Option<String>,
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
55#[serde(rename_all = "snake_case")]
56pub enum RelationType {
57    RelatesTo,
58    Mentions,
59    DerivedFrom,
60    Contradicts,
61    Reinforces,
62    Supersedes,
63    DistilledFrom,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct Relation {
68    pub from_id: Uuid,
69    pub to_id: Uuid,
70    pub relation_type: RelationType,
71    pub strength: f32,
72    pub context: Option<String>,
73}
74
75impl Memory {
76    pub fn new(
77        content: String,
78        memory_type: MemoryType,
79        source: String,
80        source_id: String,
81    ) -> Self {
82        let now = Utc::now();
83        Self {
84            id: Uuid::new_v4(),
85            content,
86            embedding: Vec::new(),
87            memory_type,
88            confidence: 1.0,
89            created_at: now,
90            last_accessed: now,
91            access_count: 0,
92            source,
93            source_id,
94            project_path: None,
95            machine_id: String::new(),
96        }
97    }
98
99    pub fn with_project_path(mut self, path: Option<String>) -> Self {
100        self.project_path = path;
101        self
102    }
103
104    pub fn with_machine_id(mut self, machine_id: String) -> Self {
105        self.machine_id = machine_id;
106        self
107    }
108
109    pub fn reinforce(&mut self) {
110        self.access_count += 1;
111        self.last_accessed = Utc::now();
112        self.confidence = (self.confidence + 0.1).min(1.0);
113    }
114}
115
116impl Entity {
117    pub fn new(name: String, entity_type: String) -> Self {
118        Self {
119            id: Uuid::new_v4(),
120            name,
121            entity_type,
122            embedding: Vec::new(),
123            aliases: Vec::new(),
124        }
125    }
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
129#[serde(rename_all = "snake_case")]
130pub enum SyncOp {
131    Create,
132    Update,
133    Delete,
134}
135
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
137#[serde(rename_all = "snake_case")]
138pub enum SyncNodeType {
139    Memory,
140    Entity,
141    Conversation,
142    Relation,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct SyncEntry {
147    pub seq: u64,
148    pub op: SyncOp,
149    pub node_type: SyncNodeType,
150    pub node_id: String,
151    pub machine_id: String,
152    pub timestamp: DateTime<Utc>,
153    pub data: Option<String>,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct SyncState {
158    pub peer_id: String,
159    pub last_seq: u64,
160    pub last_sync_at: DateTime<Utc>,
161}