second-brain-core 0.2.0

Core library for second-brain: KuzuDB graph storage, BGE embeddings, and weighted query engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};

use anyhow::{Context, Result};
use chrono::Utc;
use kuzu::{Connection, Database, SystemConfig, Value};
use uuid::Uuid;

use crate::schema::{
    Conversation, Entity, Memory, MemoryType, Relation, RelationType, SyncEntry, SyncNodeType,
    SyncOp, SyncState,
};
use crate::store::Store;

pub struct KuzuStore {
    db: Database,
    machine_id: String,
    sync_seq: AtomicU64,
}

impl KuzuStore {
    pub fn open(path: &Path, machine_id: String) -> Result<Self> {
        let db = Database::new(path, SystemConfig::default()).context("opening KùzuDB database")?;
        let store = Self {
            db,
            machine_id,
            sync_seq: AtomicU64::new(1),
        };
        store.init_schema()?;
        store.init_sync_seq()?;
        Ok(store)
    }

    pub fn in_memory(machine_id: String) -> Result<Self> {
        let db =
            Database::in_memory(SystemConfig::default()).context("creating in-memory KùzuDB")?;
        let store = Self {
            db,
            machine_id,
            sync_seq: AtomicU64::new(1),
        };
        store.init_schema()?;
        Ok(store)
    }

    pub fn machine_id(&self) -> &str {
        &self.machine_id
    }

    fn conn(&self) -> Result<Connection<'_>> {
        Connection::new(&self.db).context("creating connection")
    }

    pub fn diagnostic(&self) -> Result<String> {
        let conn = self.conn()?;

        let mut result = conn.query("MATCH (m:Memory) RETURN count(m);")?;
        let total: i64 = result
            .next()
            .map(|r| match &r[0] {
                Value::Int64(v) => *v,
                _ => -1,
            })
            .unwrap_or(-1);

        let mut result = conn.query(
            "MATCH (m:Memory) WHERE size(m.embedding) > 0 AND m.embedding[1] <> 0.0 RETURN count(m);",
        )?;
        let with_emb: i64 = result
            .next()
            .map(|r| match &r[0] {
                Value::Int64(v) => *v,
                _ => -1,
            })
            .unwrap_or(-1);

        let zeros: Vec<String> = (0..384).map(|_| "0.0".to_string()).collect();
        let zeros_str = format!("[{}]", zeros.join(","));
        let idx_query = format!(
            "CALL QUERY_VECTOR_INDEX('Memory', 'memory_emb_idx', {}, 1) YIELD node, distance RETURN distance;",
            zeros_str
        );
        let idx_status = match conn.query(&idx_query) {
            Ok(mut r) => match r.next() {
                Some(row) => format!("ok (distance: {:?})", &row[0]),
                None => "ok (0 results)".to_string(),
            },
            Err(e) => format!("error: {e}"),
        };

        Ok(format!(
            "total memories: {total}\nwith embeddings: {with_emb}\nvector index: {idx_status}"
        ))
    }

    fn init_schema(&self) -> Result<()> {
        let conn = self.conn()?;

        conn.query(
            "CREATE NODE TABLE IF NOT EXISTS Memory(
                id STRING PRIMARY KEY,
                content STRING,
                embedding FLOAT[384],
                memory_type STRING,
                confidence FLOAT,
                created_at STRING,
                last_accessed STRING,
                access_count INT64,
                source STRING,
                source_id STRING
            );",
        )
        .context("creating Memory table")?;

        conn.query(
            "CREATE NODE TABLE IF NOT EXISTS Entity(
                id STRING PRIMARY KEY,
                name STRING,
                entity_type STRING,
                embedding FLOAT[384],
                aliases STRING[]
            );",
        )
        .context("creating Entity table")?;

        conn.query(
            "CREATE NODE TABLE IF NOT EXISTS Conversation(
                id STRING PRIMARY KEY,
                source STRING,
                machine_id STRING,
                started_at STRING,
                project_path STRING
            );",
        )
        .context("creating Conversation table")?;

        conn.query(
            "CREATE NODE TABLE IF NOT EXISTS IngestLog(
                file_path STRING PRIMARY KEY,
                file_hash STRING,
                ingested_at STRING,
                memory_count INT64,
                source STRING
            );",
        )
        .context("creating IngestLog table")?;

        conn.query(
            "CREATE REL TABLE IF NOT EXISTS RELATES_TO(
                FROM Memory TO Memory,
                strength FLOAT,
                context STRING
            );",
        )
        .context("creating RELATES_TO rel")?;

        conn.query(
            "CREATE REL TABLE IF NOT EXISTS MENTIONS(
                FROM Memory TO Entity,
                position INT64
            );",
        )
        .context("creating MENTIONS rel")?;

        conn.query(
            "CREATE REL TABLE IF NOT EXISTS DERIVED_FROM(
                FROM Memory TO Conversation,
                transformation STRING
            );",
        )
        .context("creating DERIVED_FROM rel")?;

        conn.query(
            "CREATE REL TABLE IF NOT EXISTS DISTILLED_FROM(
                FROM Memory TO Memory,
                model STRING
            );",
        )
        .context("creating DISTILLED_FROM rel")?;

        conn.query(
            "CREATE REL TABLE IF NOT EXISTS CONTRADICTS(
                FROM Memory TO Memory,
                resolution STRING
            );",
        )
        .context("creating CONTRADICTS rel")?;

        conn.query(
            "CREATE REL TABLE IF NOT EXISTS REINFORCES(
                FROM Memory TO Memory
            );",
        )
        .context("creating REINFORCES rel")?;

        conn.query(
            "CREATE REL TABLE IF NOT EXISTS SUPERSEDES(
                FROM Memory TO Memory,
                reason STRING
            );",
        )
        .context("creating SUPERSEDES rel")?;

        conn.query(
            "CREATE NODE TABLE IF NOT EXISTS SyncLog(
                seq INT64 PRIMARY KEY,
                op STRING,
                node_type STRING,
                node_id STRING,
                machine_id STRING,
                timestamp STRING,
                data STRING
            );",
        )
        .context("creating SyncLog table")?;

        conn.query(
            "CREATE NODE TABLE IF NOT EXISTS SyncState(
                peer_id STRING PRIMARY KEY,
                last_seq INT64,
                last_sync_at STRING
            );",
        )
        .context("creating SyncState table")?;

        conn.query(
            "CREATE NODE TABLE IF NOT EXISTS ConsolidationLog(
                memory_id STRING PRIMARY KEY,
                distilled_id STRING,
                consolidated_at STRING,
                model STRING
            );",
        )
        .context("creating ConsolidationLog table")?;

        conn.query(
            "CALL CREATE_VECTOR_INDEX('Memory', 'memory_emb_idx', 'embedding', metric := 'cosine');",
        )
        .ok();

        Ok(())
    }

    fn init_sync_seq(&self) -> Result<()> {
        let conn = self.conn()?;
        let mut result = conn.query("MATCH (s:SyncLog) RETURN max(s.seq);")?;
        if let Some(row) = result.next() {
            if let Value::Int64(max_seq) = &row[0] {
                self.sync_seq.store((*max_seq as u64) + 1, Ordering::SeqCst);
            }
        }
        Ok(())
    }

    fn log_sync_entry(
        &self,
        conn: &Connection<'_>,
        op: SyncOp,
        node_type: SyncNodeType,
        node_id: &str,
        data: Option<&str>,
    ) -> Result<()> {
        let seq = self.sync_seq.fetch_add(1, Ordering::SeqCst);
        let timestamp = chrono::Utc::now().to_rfc3339();
        let op_str = match op {
            SyncOp::Create => "create",
            SyncOp::Update => "update",
            SyncOp::Delete => "delete",
        };
        let nt_str = match node_type {
            SyncNodeType::Memory => "memory",
            SyncNodeType::Entity => "entity",
            SyncNodeType::Conversation => "conversation",
            SyncNodeType::Relation => "relation",
        };
        let machine_escaped = escape_cypher(&self.machine_id);
        let node_id_escaped = escape_cypher(node_id);
        let data_escaped = data.map(|d| escape_cypher(d)).unwrap_or_default();

        let query = format!(
            "CREATE (:SyncLog {{
                seq: {seq},
                op: '{op_str}',
                node_type: '{nt_str}',
                node_id: '{node_id_escaped}',
                machine_id: '{machine_escaped}',
                timestamp: '{timestamp}',
                data: '{data_escaped}'
            }});"
        );
        conn.query(&query)?;
        Ok(())
    }
}

impl Store for KuzuStore {
    fn store_memory(&self, memory: &Memory) -> Result<()> {
        let conn = self.conn()?;
        self.create_memory_node(&conn, memory)?;
        let data = serde_json::to_string(memory).ok();
        self.log_sync_entry(
            &conn,
            SyncOp::Create,
            SyncNodeType::Memory,
            &memory.id.to_string(),
            data.as_deref(),
        )?;
        Ok(())
    }

    fn get_memory(&self, id: Uuid) -> Result<Option<Memory>> {
        let conn = self.conn()?;
        let query = format!(
            "MATCH (m:Memory {{id: '{}'}}) RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id;",
            id
        );

        let mut result = conn.query(&query)?;
        match result.next() {
            Some(row) => Ok(Some(row_to_memory(&row)?)),
            None => Ok(None),
        }
    }

    fn delete_memory(&self, id: Uuid) -> Result<()> {
        let conn = self.conn()?;
        conn.query(&format!(
            "MATCH (m:Memory {{id: '{}'}}) DETACH DELETE m;",
            id
        ))?;
        self.log_sync_entry(
            &conn,
            SyncOp::Delete,
            SyncNodeType::Memory,
            &id.to_string(),
            None,
        )?;
        Ok(())
    }

    fn store_entity(&self, entity: &Entity) -> Result<()> {
        let conn = self.conn()?;
        self.create_entity_node(&conn, entity)?;
        let data = serde_json::to_string(entity).ok();
        self.log_sync_entry(
            &conn,
            SyncOp::Create,
            SyncNodeType::Entity,
            &entity.id.to_string(),
            data.as_deref(),
        )?;
        Ok(())
    }

    fn get_entity(&self, id: Uuid) -> Result<Option<Entity>> {
        let conn = self.conn()?;
        let mut result = conn.query(&format!(
            "MATCH (e:Entity {{id: '{}'}}) RETURN e.id, e.name, e.entity_type;",
            id
        ))?;

        match result.next() {
            Some(row) => Ok(Some(row_to_entity(&row)?)),
            None => Ok(None),
        }
    }

    fn find_entity_by_name(&self, name: &str) -> Result<Option<Entity>> {
        let conn = self.conn()?;
        let name_escaped = escape_cypher(name);
        let query = format!(
            "MATCH (e:Entity) WHERE e.name = '{}' RETURN e.id, e.name, e.entity_type;",
            name_escaped
        );
        let mut result = conn.query(&query)?;

        match result.next() {
            Some(row) => Ok(Some(row_to_entity(&row)?)),
            None => Ok(None),
        }
    }

    fn store_conversation(&self, conversation: &Conversation) -> Result<()> {
        let conn = self.conn()?;
        self.create_conversation_node(&conn, conversation)?;
        let data = serde_json::to_string(conversation).ok();
        self.log_sync_entry(
            &conn,
            SyncOp::Create,
            SyncNodeType::Conversation,
            &conversation.id.to_string(),
            data.as_deref(),
        )?;
        Ok(())
    }

    fn store_relation(&self, relation: &Relation) -> Result<()> {
        let conn = self.conn()?;
        self.create_relation_edge(&conn, relation)?;
        let rel_type = format!("{:?}", relation.relation_type).to_lowercase();
        let node_id = format!("{}:{}:{}", relation.from_id, relation.to_id, rel_type);
        let data = serde_json::to_string(relation).ok();
        self.log_sync_entry(
            &conn,
            SyncOp::Create,
            SyncNodeType::Relation,
            &node_id,
            data.as_deref(),
        )?;
        Ok(())
    }

    fn get_relations(
        &self,
        node_id: Uuid,
        relation_type: Option<RelationType>,
    ) -> Result<Vec<Relation>> {
        let conn = self.conn()?;
        let id = node_id.to_string();

        let query = match relation_type {
            Some(RelationType::RelatesTo) => format!(
                "MATCH (a:Memory {{id: '{}'}})-[r:RELATES_TO]->(b:Memory) RETURN b.id, r.strength, r.context;",
                id
            ),
            Some(RelationType::Contradicts) => format!(
                "MATCH (a:Memory {{id: '{}'}})-[r:CONTRADICTS]->(b:Memory) RETURN b.id, r.resolution;",
                id
            ),
            Some(RelationType::Reinforces) => format!(
                "MATCH (a:Memory {{id: '{}'}})-[:REINFORCES]->(b:Memory) RETURN b.id;",
                id
            ),
            Some(RelationType::Supersedes) => format!(
                "MATCH (a:Memory {{id: '{}'}})-[r:SUPERSEDES]->(b:Memory) RETURN b.id, r.reason;",
                id
            ),
            Some(RelationType::Mentions) => format!(
                "MATCH (a:Memory {{id: '{}'}})-[:MENTIONS]->(b:Entity) RETURN b.id;",
                id
            ),
            Some(RelationType::DerivedFrom) => format!(
                "MATCH (a:Memory {{id: '{}'}})-[:DERIVED_FROM]->(b:Conversation) RETURN b.id;",
                id
            ),
            Some(RelationType::DistilledFrom) => format!(
                "MATCH (a:Memory {{id: '{}'}})-[:DISTILLED_FROM]->(b:Memory) RETURN b.id;",
                id
            ),
            None => {
                let mut relations = Vec::new();
                let mem_to_mem = [
                    ("RELATES_TO", RelationType::RelatesTo),
                    ("REINFORCES", RelationType::Reinforces),
                    ("CONTRADICTS", RelationType::Contradicts),
                    ("SUPERSEDES", RelationType::Supersedes),
                    ("DISTILLED_FROM", RelationType::DistilledFrom),
                ];
                for (label, rt) in &mem_to_mem {
                    let q = format!(
                        "MATCH (a:Memory {{id: '{}'}})-[:{}]->(b:Memory) RETURN b.id;",
                        id, label
                    );
                    if let Ok(mut result) = conn.query(&q) {
                        for row in &mut result {
                            let to_id_str = value_to_string(&row[0]);
                            let to_id = Uuid::parse_str(&to_id_str).unwrap_or_default();
                            relations.push(Relation {
                                from_id: node_id,
                                to_id,
                                relation_type: *rt,
                                strength: 1.0,
                                context: None,
                            });
                        }
                    }
                }
                for (label, target, rt) in [
                    ("MENTIONS", "Entity", RelationType::Mentions),
                    ("DERIVED_FROM", "Conversation", RelationType::DerivedFrom),
                ] {
                    let q = format!(
                        "MATCH (a:Memory {{id: '{}'}})-[:{}]->(b:{}) RETURN b.id;",
                        id, label, target
                    );
                    if let Ok(mut result) = conn.query(&q) {
                        for row in &mut result {
                            let to_id_str = value_to_string(&row[0]);
                            let to_id = Uuid::parse_str(&to_id_str).unwrap_or_default();
                            relations.push(Relation {
                                from_id: node_id,
                                to_id,
                                relation_type: rt,
                                strength: 1.0,
                                context: None,
                            });
                        }
                    }
                }
                return Ok(relations);
            },
        };

        let mut result = conn.query(&query)?;
        let mut relations = Vec::new();

        for row in &mut result {
            let to_id_str = value_to_string(&row[0]);
            let to_id = Uuid::parse_str(&to_id_str).unwrap_or_default();

            relations.push(Relation {
                from_id: node_id,
                to_id,
                relation_type: relation_type.unwrap_or(RelationType::RelatesTo),
                strength: 1.0,
                context: None,
            });
        }

        Ok(relations)
    }

    fn vector_search(&self, embedding: &[f32], limit: usize) -> Result<Vec<(Memory, f32)>> {
        let conn = self.conn()?;
        let embedding_str = format_embedding(embedding);

        let query = format!(
            "CALL QUERY_VECTOR_INDEX('Memory', 'memory_emb_idx', {}, {}) YIELD node, distance
             RETURN node.id, node.content, node.memory_type, node.confidence,
                    node.created_at, node.last_accessed, node.access_count,
                    node.source, node.source_id, distance;",
            embedding_str, limit
        );

        let mut result = conn.query(&query)?;
        let mut results = Vec::new();

        for row in &mut result {
            let memory = row_to_memory(&row[..9])?;
            let distance = value_to_f32(&row[9]);
            let similarity = 1.0 - distance;
            results.push((memory, similarity));
        }

        Ok(results)
    }

    fn traverse(&self, start_id: Uuid, depth: u32) -> Result<Vec<(Memory, Vec<Relation>)>> {
        let conn = self.conn()?;
        let query = format!(
            "MATCH (start:Memory {{id: '{}'}})-[r:RELATES_TO|REINFORCES*1..{}]->(m:Memory)
             RETURN DISTINCT m.id, m.content, m.memory_type, m.confidence,
                    m.created_at, m.last_accessed, m.access_count, m.source, m.source_id;",
            start_id, depth
        );

        let mut result = conn.query(&query)?;
        let mut results = Vec::new();

        for row in &mut result {
            let memory = row_to_memory(&row)?;
            results.push((memory, Vec::new()));
        }

        Ok(results)
    }

    fn memories_by_source(&self, source: &str) -> Result<Vec<Memory>> {
        let conn = self.conn()?;
        let source_escaped = escape_cypher(source);
        let query = format!(
            "MATCH (m:Memory) WHERE m.source = '{}' RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id;",
            source_escaped
        );
        let mut result = conn.query(&query)?;

        let mut memories = Vec::new();
        for row in &mut result {
            memories.push(row_to_memory(&row)?);
        }
        Ok(memories)
    }

    fn memories_by_type(&self, memory_type: MemoryType) -> Result<Vec<Memory>> {
        let conn = self.conn()?;
        let type_str = format!("{:?}", memory_type).to_lowercase();
        let query = format!(
            "MATCH (m:Memory) WHERE m.memory_type = '{}' RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id;",
            type_str
        );
        let mut result = conn.query(&query)?;

        let mut memories = Vec::new();
        for row in &mut result {
            memories.push(row_to_memory(&row)?);
        }
        Ok(memories)
    }

    fn memories_needing_decay(&self, threshold_days: u32) -> Result<Vec<Memory>> {
        let conn = self.conn()?;
        let cutoff = chrono::Utc::now() - chrono::Duration::days(threshold_days as i64);
        let cutoff_str = cutoff.to_rfc3339();

        let query = format!(
            "MATCH (m:Memory) WHERE m.last_accessed < '{}' AND m.confidence > 0.05 RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id;",
            cutoff_str
        );

        let mut result = conn.query(&query)?;
        let mut memories = Vec::new();
        for row in &mut result {
            memories.push(row_to_memory(&row)?);
        }
        Ok(memories)
    }

    fn update_memory(&self, memory: &Memory) -> Result<()> {
        let conn = self.conn()?;
        let id = memory.id.to_string();
        let last_accessed = memory.last_accessed.to_rfc3339();

        let query = format!(
            "MATCH (m:Memory {{id: '{}'}}) SET m.confidence = {}, m.last_accessed = '{}', m.access_count = {};",
            id, memory.confidence, last_accessed, memory.access_count
        );

        conn.query(&query)?;
        let data = serde_json::to_string(memory).ok();
        self.log_sync_entry(
            &conn,
            SyncOp::Update,
            SyncNodeType::Memory,
            &id,
            data.as_deref(),
        )?;
        Ok(())
    }

    fn text_search(&self, query: &str, limit: usize) -> Result<Vec<Memory>> {
        let conn = self.conn()?;
        let query_escaped = escape_cypher(&query.to_lowercase());
        let cypher = format!(
            "MATCH (m:Memory) WHERE lower(m.content) CONTAINS '{}' RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id LIMIT {};",
            query_escaped, limit
        );

        let mut result = conn.query(&cypher)?;
        let mut memories = Vec::new();
        for row in &mut result {
            memories.push(row_to_memory(&row)?);
        }
        Ok(memories)
    }

    fn memory_count(&self) -> Result<usize> {
        let conn = self.conn()?;
        let mut result = conn.query("MATCH (m:Memory) RETURN count(m);")?;
        match result.next() {
            Some(row) => match &row[0] {
                Value::Int64(n) => Ok(*n as usize),
                _ => Ok(0),
            },
            None => Ok(0),
        }
    }
}

impl KuzuStore {
    pub fn find_or_create_entity(&self, name: &str, entity_type: &str) -> Result<Entity> {
        if let Some(existing) = self.find_entity_by_name(name)? {
            return Ok(existing);
        }
        let entity = Entity::new(name.to_string(), entity_type.to_string());
        self.store_entity(&entity)?;
        Ok(entity)
    }

    pub fn memory_content_exists(&self, content_prefix: &str) -> Result<bool> {
        let conn = self.conn()?;
        let escaped = escape_cypher(content_prefix);
        let query = format!(
            "MATCH (m:Memory) WHERE starts_with(m.content, '{}') RETURN m.id LIMIT 1;",
            escaped
        );
        let mut result = conn.query(&query)?;
        Ok(result.next().is_some())
    }

    pub fn unconsolidated_memories(&self, limit: usize) -> Result<Vec<Memory>> {
        let conn = self.conn()?;
        let query = format!(
            "MATCH (m:Memory) WHERE NOT EXISTS {{MATCH (c:ConsolidationLog) WHERE c.memory_id = m.id}} AND m.source <> 'consolidation' RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id LIMIT {};",
            limit
        );
        let mut result = conn.query(&query)?;
        let mut memories = Vec::new();
        for row in &mut result {
            memories.push(row_to_memory(&row)?);
        }
        Ok(memories)
    }

    pub fn mark_consolidated(&self, raw_id: Uuid, distilled_id: Uuid, model: &str) -> Result<()> {
        let conn = self.conn()?;
        let now = Utc::now().to_rfc3339();
        let query = format!(
            "CREATE (:ConsolidationLog {{memory_id: '{}', distilled_id: '{}', consolidated_at: '{}', model: '{}'}});",
            raw_id, distilled_id, now, escape_cypher(model)
        );
        conn.query(&query)?;
        Ok(())
    }

    pub fn consolidation_count(&self) -> Result<usize> {
        let conn = self.conn()?;
        let mut result = conn.query("MATCH (c:ConsolidationLog) RETURN count(c);")?;
        match result.next() {
            Some(row) => match &row[0] {
                Value::Int64(n) => Ok(*n as usize),
                _ => Ok(0),
            },
            None => Ok(0),
        }
    }

    pub fn clear_ingest_log(&self) -> Result<usize> {
        let conn = self.conn()?;
        let mut result = conn.query("MATCH (l:IngestLog) RETURN count(l);")?;
        let count = match result.next() {
            Some(row) => match &row[0] {
                Value::Int64(n) => *n as usize,
                _ => 0,
            },
            None => 0,
        };
        conn.query("MATCH (l:IngestLog) DETACH DELETE l;")?;
        Ok(count)
    }

    pub fn delete_memories_by_source(&self, source: &str) -> Result<usize> {
        let conn = self.conn()?;
        let escaped = escape_cypher(source);
        let mut result = conn.query(&format!(
            "MATCH (m:Memory) WHERE m.source = '{}' RETURN count(m);",
            escaped
        ))?;
        let count = match result.next() {
            Some(row) => match &row[0] {
                Value::Int64(n) => *n as usize,
                _ => 0,
            },
            None => 0,
        };
        conn.query(&format!(
            "MATCH (m:Memory) WHERE m.source = '{}' DETACH DELETE m;",
            escaped
        ))?;
        Ok(count)
    }

    pub fn is_file_ingested(&self, file_path: &str) -> Result<bool> {
        let conn = self.conn()?;
        let escaped = escape_cypher(file_path);
        let mut result = conn.query(&format!(
            "MATCH (l:IngestLog {{file_path: '{}'}}) RETURN l.file_path;",
            escaped
        ))?;
        Ok(result.next().is_some())
    }

    pub fn is_file_changed(&self, file_path: &str, file_hash: &str) -> Result<bool> {
        let conn = self.conn()?;
        let escaped = escape_cypher(file_path);
        let mut result = conn.query(&format!(
            "MATCH (l:IngestLog {{file_path: '{}'}}) RETURN l.file_hash;",
            escaped
        ))?;
        match result.next() {
            Some(row) => {
                let stored_hash = value_to_string(&row[0]);
                Ok(stored_hash != file_hash)
            }
            None => Ok(true),
        }
    }

    pub fn mark_ingested(
        &self,
        file_path: &str,
        file_hash: &str,
        memory_count: usize,
        source: &str,
    ) -> Result<()> {
        let conn = self.conn()?;
        let path_escaped = escape_cypher(file_path);
        let hash_escaped = escape_cypher(file_hash);
        let source_escaped = escape_cypher(source);
        let now = chrono::Utc::now().to_rfc3339();

        conn.query(&format!(
            "MERGE (l:IngestLog {{file_path: '{}'}})
             SET l.file_hash = '{}', l.ingested_at = '{}', l.memory_count = {}, l.source = '{}';",
            path_escaped, hash_escaped, now, memory_count as i64, source_escaped
        ))?;
        Ok(())
    }

    pub fn ingested_file_count(&self) -> Result<usize> {
        let conn = self.conn()?;
        let mut result = conn.query("MATCH (l:IngestLog) RETURN count(l);")?;
        match result.next() {
            Some(row) => match &row[0] {
                Value::Int64(n) => Ok(*n as usize),
                _ => Ok(0),
            },
            None => Ok(0),
        }
    }
}

impl KuzuStore {
    fn create_memory_node(&self, conn: &Connection<'_>, memory: &Memory) -> Result<()> {
        let id = memory.id.to_string();
        let memory_type = format!("{:?}", memory.memory_type).to_lowercase();
        let created_at = memory.created_at.to_rfc3339();
        let last_accessed = memory.last_accessed.to_rfc3339();
        let embedding_str = format_embedding(&memory.embedding);
        let content_escaped = escape_cypher(&memory.content);
        let source_escaped = escape_cypher(&memory.source);
        let source_id_escaped = escape_cypher(&memory.source_id);

        let query = format!(
            "CREATE (:Memory {{
                id: '{id}',
                content: '{content_escaped}',
                embedding: {embedding_str},
                memory_type: '{memory_type}',
                confidence: {confidence},
                created_at: '{created_at}',
                last_accessed: '{last_accessed}',
                access_count: {access_count},
                source: '{source_escaped}',
                source_id: '{source_id_escaped}'
            }});",
            confidence = memory.confidence,
            access_count = memory.access_count,
        );

        conn.query(&query)?;
        Ok(())
    }

    fn create_entity_node(&self, conn: &Connection<'_>, entity: &Entity) -> Result<()> {
        let id = entity.id.to_string();
        let embedding_str = format_embedding(&entity.embedding);
        let aliases_str = format_string_array(&entity.aliases);
        let name_escaped = escape_cypher(&entity.name);
        let etype_escaped = escape_cypher(&entity.entity_type);

        let query = format!(
            "CREATE (:Entity {{
                id: '{id}',
                name: '{name_escaped}',
                entity_type: '{etype_escaped}',
                embedding: {embedding_str},
                aliases: {aliases_str}
            }});"
        );

        conn.query(&query)?;
        Ok(())
    }

    fn create_conversation_node(
        &self,
        conn: &Connection<'_>,
        conversation: &Conversation,
    ) -> Result<()> {
        let id = conversation.id.to_string();
        let started_at = conversation.started_at.to_rfc3339();
        let source_escaped = escape_cypher(&conversation.source);
        let machine_escaped = escape_cypher(&conversation.machine_id);
        let project_escaped = escape_cypher(conversation.project_path.as_deref().unwrap_or(""));

        let query = format!(
            "CREATE (:Conversation {{
                id: '{id}',
                source: '{source_escaped}',
                machine_id: '{machine_escaped}',
                started_at: '{started_at}',
                project_path: '{project_escaped}'
            }});"
        );

        conn.query(&query)?;
        Ok(())
    }

    fn create_relation_edge(&self, conn: &Connection<'_>, relation: &Relation) -> Result<()> {
        let from_id = relation.from_id.to_string();
        let to_id = relation.to_id.to_string();

        let query = match relation.relation_type {
            RelationType::RelatesTo => format!(
                "MATCH (a:Memory {{id: '{}'}}), (b:Memory {{id: '{}'}}) CREATE (a)-[:RELATES_TO {{strength: {}, context: '{}'}}]->(b);",
                from_id, to_id, relation.strength, relation.context.as_deref().unwrap_or("")
            ),
            RelationType::Mentions => format!(
                "MATCH (a:Memory {{id: '{}'}}), (b:Entity {{id: '{}'}}) CREATE (a)-[:MENTIONS {{position: 0}}]->(b);",
                from_id, to_id
            ),
            RelationType::DerivedFrom => format!(
                "MATCH (a:Memory {{id: '{}'}}), (b:Conversation {{id: '{}'}}) CREATE (a)-[:DERIVED_FROM {{transformation: '{}'}}]->(b);",
                from_id, to_id, relation.context.as_deref().unwrap_or("direct")
            ),
            RelationType::Contradicts => format!(
                "MATCH (a:Memory {{id: '{}'}}), (b:Memory {{id: '{}'}}) CREATE (a)-[:CONTRADICTS {{resolution: '{}'}}]->(b);",
                from_id, to_id, relation.context.as_deref().unwrap_or("")
            ),
            RelationType::Reinforces => format!(
                "MATCH (a:Memory {{id: '{}'}}), (b:Memory {{id: '{}'}}) CREATE (a)-[:REINFORCES]->(b);",
                from_id, to_id
            ),
            RelationType::DistilledFrom => format!(
                "MATCH (a:Memory {{id: '{}'}}), (b:Memory {{id: '{}'}}) CREATE (a)-[:DISTILLED_FROM {{model: '{}'}}]->(b);",
                from_id, to_id, relation.context.as_deref().unwrap_or("")
            ),
            RelationType::Supersedes => format!(
                "MATCH (a:Memory {{id: '{}'}}), (b:Memory {{id: '{}'}}) CREATE (a)-[:SUPERSEDES {{reason: '{}'}}]->(b);",
                from_id, to_id, relation.context.as_deref().unwrap_or("")
            ),
        };

        conn.query(&query)?;
        Ok(())
    }
}

impl KuzuStore {
    pub fn get_conversation(&self, id: Uuid) -> Result<Option<Conversation>> {
        let conn = self.conn()?;
        let mut result = conn.query(&format!(
            "MATCH (c:Conversation {{id: '{}'}}) RETURN c.id, c.source, c.machine_id, c.started_at, c.project_path;",
            id
        ))?;

        match result.next() {
            Some(row) => {
                let id = Uuid::parse_str(&value_to_string(&row[0])).unwrap_or_default();
                let source = value_to_string(&row[1]);
                let machine_id = value_to_string(&row[2]);
                let started_at_str = value_to_string(&row[3]);
                let project_path = {
                    let s = value_to_string(&row[4]);
                    if s.is_empty() {
                        None
                    } else {
                        Some(s)
                    }
                };
                let started_at = chrono::DateTime::parse_from_rfc3339(&started_at_str)
                    .map(|dt| dt.with_timezone(&chrono::Utc))
                    .unwrap_or_else(|_| chrono::Utc::now());

                Ok(Some(Conversation {
                    id,
                    source,
                    machine_id,
                    started_at,
                    project_path,
                }))
            }
            None => Ok(None),
        }
    }

    pub fn get_memory_with_embedding(&self, id: Uuid) -> Result<Option<Memory>> {
        let conn = self.conn()?;
        let query = format!(
            "MATCH (m:Memory {{id: '{}'}}) RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id, m.embedding;",
            id
        );

        let mut result = conn.query(&query)?;
        match result.next() {
            Some(row) => {
                let mut memory = row_to_memory(&row[..9])?;
                memory.embedding = value_to_f32_vec(&row[9]);
                Ok(Some(memory))
            }
            None => Ok(None),
        }
    }

    pub fn import_memory(&self, memory: &Memory) -> Result<()> {
        let conn = self.conn()?;
        self.create_memory_node(&conn, memory)
    }

    pub fn import_conversation(&self, conversation: &Conversation) -> Result<()> {
        let conn = self.conn()?;
        self.create_conversation_node(&conn, conversation)
    }

    pub fn import_entity(&self, entity: &Entity) -> Result<()> {
        let conn = self.conn()?;
        self.create_entity_node(&conn, entity)
    }

    pub fn import_relation(&self, relation: &Relation) -> Result<()> {
        let conn = self.conn()?;
        self.create_relation_edge(&conn, relation)
    }

    pub fn import_delete_memory(&self, id: Uuid) -> Result<()> {
        let conn = self.conn()?;
        conn.query(&format!(
            "MATCH (m:Memory {{id: '{}'}}) DETACH DELETE m;",
            id
        ))?;
        Ok(())
    }

    pub fn import_or_update_memory(&self, memory: &Memory) -> Result<()> {
        let conn = self.conn()?;
        let id = memory.id.to_string();
        let last_accessed = memory.last_accessed.to_rfc3339();
        let content_escaped = escape_cypher(&memory.content);
        let embedding_str = format_embedding(&memory.embedding);
        let memory_type = format!("{:?}", memory.memory_type).to_lowercase();
        let source_escaped = escape_cypher(&memory.source);
        let source_id_escaped = escape_cypher(&memory.source_id);
        let created_at = memory.created_at.to_rfc3339();

        let query = format!(
            "MATCH (m:Memory {{id: '{id}'}}) SET m.content = '{content_escaped}', m.embedding = {embedding_str}, m.memory_type = '{memory_type}', m.confidence = {confidence}, m.created_at = '{created_at}', m.last_accessed = '{last_accessed}', m.access_count = {access_count}, m.source = '{source_escaped}', m.source_id = '{source_id_escaped}';",
            confidence = memory.confidence,
            access_count = memory.access_count,
        );

        conn.query(&query)?;
        Ok(())
    }

    pub fn sync_log_since(&self, after_seq: u64) -> Result<Vec<SyncEntry>> {
        self.sync_log_page(after_seq, None)
    }

    pub fn sync_log_page(&self, after_seq: u64, limit: Option<usize>) -> Result<Vec<SyncEntry>> {
        let conn = self.conn()?;
        let machine_escaped = escape_cypher(&self.machine_id);
        let limit_clause = match limit {
            Some(n) => format!(" LIMIT {}", n),
            None => String::new(),
        };
        let query = format!(
            "MATCH (s:SyncLog) WHERE s.seq > {} AND s.machine_id = '{}' RETURN s.seq, s.op, s.node_type, s.node_id, s.machine_id, s.timestamp, s.data ORDER BY s.seq{};",
            after_seq, machine_escaped, limit_clause
        );

        let mut result = conn.query(&query)?;
        let mut entries = Vec::new();

        for row in &mut result {
            let seq = match &row[0] {
                Value::Int64(n) => *n as u64,
                _ => 0,
            };
            let op_str = value_to_string(&row[1]);
            let node_type_str = value_to_string(&row[2]);
            let node_id = value_to_string(&row[3]);
            let machine_id = value_to_string(&row[4]);
            let timestamp_str = value_to_string(&row[5]);
            let data_str = value_to_string(&row[6]);

            let op = match op_str.as_str() {
                "create" => SyncOp::Create,
                "update" => SyncOp::Update,
                "delete" => SyncOp::Delete,
                _ => continue,
            };
            let node_type = match node_type_str.as_str() {
                "memory" => SyncNodeType::Memory,
                "entity" => SyncNodeType::Entity,
                "conversation" => SyncNodeType::Conversation,
                "relation" => SyncNodeType::Relation,
                _ => continue,
            };
            let timestamp = chrono::DateTime::parse_from_rfc3339(&timestamp_str)
                .map(|dt| dt.with_timezone(&chrono::Utc))
                .unwrap_or_else(|_| chrono::Utc::now());

            let data = if data_str.is_empty() {
                None
            } else {
                Some(data_str)
            };

            entries.push(SyncEntry {
                seq,
                op,
                node_type,
                node_id,
                machine_id,
                timestamp,
                data,
            });
        }

        Ok(entries)
    }

    pub fn get_sync_state(&self, peer_id: &str) -> Result<Option<SyncState>> {
        let conn = self.conn()?;
        let escaped = escape_cypher(peer_id);
        let mut result = conn.query(&format!(
            "MATCH (s:SyncState {{peer_id: '{}'}}) RETURN s.peer_id, s.last_seq, s.last_sync_at;",
            escaped
        ))?;

        match result.next() {
            Some(row) => {
                let peer_id = value_to_string(&row[0]);
                let last_seq = match &row[1] {
                    Value::Int64(n) => *n as u64,
                    _ => 0,
                };
                let last_sync_at_str = value_to_string(&row[2]);
                let last_sync_at = chrono::DateTime::parse_from_rfc3339(&last_sync_at_str)
                    .map(|dt| dt.with_timezone(&chrono::Utc))
                    .unwrap_or_else(|_| chrono::Utc::now());

                Ok(Some(SyncState {
                    peer_id,
                    last_seq,
                    last_sync_at,
                }))
            }
            None => Ok(None),
        }
    }

    pub fn set_sync_state(&self, state: &SyncState) -> Result<()> {
        let conn = self.conn()?;
        let peer_escaped = escape_cypher(&state.peer_id);
        let now = state.last_sync_at.to_rfc3339();

        conn.query(&format!(
            "MERGE (s:SyncState {{peer_id: '{}'}}) SET s.last_seq = {}, s.last_sync_at = '{}';",
            peer_escaped, state.last_seq, now
        ))?;
        Ok(())
    }

    pub fn get_all_sync_states(&self) -> Result<Vec<SyncState>> {
        let conn = self.conn()?;
        let mut result =
            conn.query("MATCH (s:SyncState) RETURN s.peer_id, s.last_seq, s.last_sync_at;")?;
        let mut states = Vec::new();

        for row in &mut result {
            let peer_id = value_to_string(&row[0]);
            let last_seq = match &row[1] {
                Value::Int64(n) => *n as u64,
                _ => 0,
            };
            let last_sync_at_str = value_to_string(&row[2]);
            let last_sync_at = chrono::DateTime::parse_from_rfc3339(&last_sync_at_str)
                .map(|dt| dt.with_timezone(&chrono::Utc))
                .unwrap_or_else(|_| chrono::Utc::now());

            states.push(SyncState {
                peer_id,
                last_seq,
                last_sync_at,
            });
        }

        Ok(states)
    }

    pub fn backfill_sync_log(&self) -> Result<u64> {
        let conn = self.conn()?;
        let mut count = 0u64;

        let mut result = conn.query(
            "MATCH (m:Memory) RETURN m.id, m.content, m.memory_type, m.confidence, m.created_at, m.last_accessed, m.access_count, m.source, m.source_id, m.embedding;",
        )?;
        for row in &mut result {
            let mut memory = row_to_memory(&row[..9])?;
            memory.embedding = value_to_f32_vec(&row[9]);
            let data = serde_json::to_string(&memory).ok();
            self.log_sync_entry(
                &conn,
                SyncOp::Create,
                SyncNodeType::Memory,
                &memory.id.to_string(),
                data.as_deref(),
            )?;
            count += 1;
        }

        let mut result = conn.query(
            "MATCH (c:Conversation) RETURN c.id, c.source, c.machine_id, c.started_at, c.project_path;",
        )?;
        for row in &mut result {
            let id = Uuid::parse_str(&value_to_string(&row[0])).unwrap_or_default();
            let source = value_to_string(&row[1]);
            let machine_id = value_to_string(&row[2]);
            let started_at_str = value_to_string(&row[3]);
            let project_path = {
                let s = value_to_string(&row[4]);
                if s.is_empty() {
                    None
                } else {
                    Some(s)
                }
            };
            let started_at = chrono::DateTime::parse_from_rfc3339(&started_at_str)
                .map(|dt| dt.with_timezone(&chrono::Utc))
                .unwrap_or_else(|_| chrono::Utc::now());

            let conv = Conversation {
                id,
                source,
                machine_id,
                started_at,
                project_path,
            };
            let data = serde_json::to_string(&conv).ok();
            self.log_sync_entry(
                &conn,
                SyncOp::Create,
                SyncNodeType::Conversation,
                &conv.id.to_string(),
                data.as_deref(),
            )?;
            count += 1;
        }

        Ok(count)
    }
}

fn format_embedding(embedding: &[f32]) -> String {
    if embedding.is_empty() {
        let zeros: Vec<String> = (0..384).map(|_| "0.0".to_string()).collect();
        return format!("[{}]", zeros.join(","));
    }
    let parts: Vec<String> = embedding.iter().map(|v| format!("{}", v)).collect();
    format!("[{}]", parts.join(","))
}

fn escape_cypher(s: &str) -> String {
    s.replace('\\', "\\\\").replace('\'', "\\'")
}

fn format_string_array(items: &[String]) -> String {
    let parts: Vec<String> = items
        .iter()
        .map(|s| format!("'{}'", s.replace('\'', "''")))
        .collect();
    format!("[{}]", parts.join(","))
}

fn value_to_string(val: &Value) -> String {
    match val {
        Value::String(s) => s.clone(),
        other => format!("{:?}", other),
    }
}

fn value_to_f32(val: &Value) -> f32 {
    match val {
        Value::Float(f) => *f,
        Value::Double(d) => *d as f32,
        Value::Int64(i) => *i as f32,
        _ => 0.0,
    }
}

fn value_to_f32_vec(val: &Value) -> Vec<f32> {
    match val {
        Value::Array(_, items) | Value::List(_, items) => {
            items.iter().map(|v| value_to_f32(v)).collect()
        }
        _ => Vec::new(),
    }
}

fn row_to_memory(row: &[Value]) -> Result<Memory> {
    let id_str = value_to_string(&row[0]);
    let id = Uuid::parse_str(&id_str).unwrap_or_default();
    let content = value_to_string(&row[1]);
    let memory_type_str = value_to_string(&row[2]);
    let confidence = value_to_f32(&row[3]);
    let created_at_str = value_to_string(&row[4]);
    let last_accessed_str = value_to_string(&row[5]);
    let access_count = match &row[6] {
        Value::Int64(i) => *i as u32,
        _ => 0,
    };
    let source = value_to_string(&row[7]);
    let source_id = value_to_string(&row[8]);

    let memory_type = match memory_type_str.as_str() {
        "episodic" => MemoryType::Episodic,
        "procedural" => MemoryType::Procedural,
        _ => MemoryType::Semantic,
    };

    let created_at = chrono::DateTime::parse_from_rfc3339(&created_at_str)
        .map(|dt| dt.with_timezone(&chrono::Utc))
        .unwrap_or_else(|_| chrono::Utc::now());

    let last_accessed = chrono::DateTime::parse_from_rfc3339(&last_accessed_str)
        .map(|dt| dt.with_timezone(&chrono::Utc))
        .unwrap_or_else(|_| chrono::Utc::now());

    Ok(Memory {
        id,
        content,
        embedding: Vec::new(),
        memory_type,
        confidence,
        created_at,
        last_accessed,
        access_count,
        source,
        source_id,
    })
}

fn row_to_entity(row: &[Value]) -> Result<Entity> {
    let id_str = value_to_string(&row[0]);
    let id = Uuid::parse_str(&id_str).unwrap_or_default();
    let name = value_to_string(&row[1]);
    let entity_type = value_to_string(&row[2]);

    Ok(Entity {
        id,
        name,
        entity_type,
        embedding: Vec::new(),
        aliases: Vec::new(),
    })
}

unsafe impl Send for KuzuStore {}
unsafe impl Sync for KuzuStore {}