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