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
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
|
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
-- | Cross-agent shared memory system with vector similarity search.
--
-- Provides persistent memory that is:
-- - Shared across all agents (Telegram, researcher, coder, etc.)
-- - Private per user (users can't see each other's memories)
-- - Searchable via semantic similarity using embeddings
--
-- Uses sqlite-vss for vector similarity search and Ollama for embeddings.
--
-- : out omni-agent-memory
-- : dep aeson
-- : dep http-conduit
-- : dep sqlite-simple
-- : dep uuid
-- : dep vector
-- : dep directory
-- : dep bytestring
module Omni.Agent.Memory
( -- * Types
User (..),
Memory (..),
MemorySource (..),
ConversationMessage (..),
ConversationSummary (..),
MessageRole (..),
RelationType (..),
MemoryLink (..),
-- * User Management
createUser,
getUser,
getUserByTelegramId,
getOrCreateUserByTelegramId,
-- * Memory Operations
storeMemory,
recallMemories,
forgetMemory,
getAllMemoriesForUser,
updateMemoryAccess,
-- * Knowledge Graph
linkMemories,
getMemoryLinks,
getLinkedMemories,
queryGraph,
-- * Conversation History (DMs)
saveMessage,
getRecentMessages,
getConversationContext,
summarizeAndArchive,
estimateTokens,
-- * Group Conversation History
saveGroupMessage,
getGroupRecentMessages,
getGroupConversationContext,
-- * Group Memories
storeGroupMemory,
recallGroupMemories,
-- * Embeddings
embedText,
-- * Agent Integration
rememberTool,
recallTool,
linkMemoriesTool,
queryGraphTool,
formatMemoriesForPrompt,
runAgentWithMemory,
-- * Database
withMemoryDb,
initMemoryDb,
getMemoryDbPath,
-- * Testing
main,
test,
)
where
import Alpha
import Data.Aeson ((.!=), (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.KeyMap as KeyMap
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Data.Text.Encoding as TE
import Data.Time (UTCTime, getCurrentTime)
import Data.Time.Format (defaultTimeLocale, formatTime)
import qualified Data.UUID as UUID
import qualified Data.UUID.V4 as UUID
import qualified Data.Vector.Storable as VS
import qualified Database.SQLite.Simple as SQL
import Database.SQLite.Simple.FromField ()
import qualified Database.SQLite.Simple.ToField as SQL
import Foreign.Storable ()
import qualified Network.HTTP.Simple as HTTP
import qualified Omni.Agent.Engine as Engine
import qualified Omni.Test as Test
import System.Directory (createDirectoryIfMissing)
import System.Environment (lookupEnv)
import System.FilePath (takeDirectory, (</>))
main :: IO ()
main = Test.run test
test :: Test.Tree
test =
Test.group
"Omni.Agent.Memory"
[ Test.unit "User JSON roundtrip" <| do
now <- getCurrentTime
let user =
User
{ userId = "test-uuid",
userTelegramId = Just 12345,
userEmail = Nothing,
userName = "Test User",
userCreatedAt = now
}
case Aeson.decode (Aeson.encode user) of
Nothing -> Test.assertFailure "Failed to decode User"
Just decoded -> userName decoded Test.@=? "Test User",
Test.unit "Memory JSON roundtrip" <| do
now <- getCurrentTime
let mem =
Memory
{ memoryId = "mem-uuid",
memoryUserId = "user-uuid",
memoryContent = "User is an AI engineer",
memoryEmbedding = Nothing,
memorySource =
MemorySource
{ sourceAgent = "telegram",
sourceSession = Nothing,
sourceContext = "User mentioned in chat"
},
memoryConfidence = 0.9,
memoryCreatedAt = now,
memoryLastAccessedAt = now,
memoryTags = ["profession", "ai"]
}
case Aeson.decode (Aeson.encode mem) of
Nothing -> Test.assertFailure "Failed to decode Memory"
Just decoded -> memoryContent decoded Test.@=? "User is an AI engineer",
Test.unit "MemorySource JSON roundtrip" <| do
let src =
MemorySource
{ sourceAgent = "researcher",
sourceSession = Just "session-123",
sourceContext = "Extracted from conversation"
}
case Aeson.decode (Aeson.encode src) of
Nothing -> Test.assertFailure "Failed to decode MemorySource"
Just decoded -> sourceAgent decoded Test.@=? "researcher",
Test.unit "formatMemoriesForPrompt formats correctly" <| do
now <- getCurrentTime
let mem1 =
Memory
{ memoryId = "1",
memoryUserId = "u",
memoryContent = "User is an AI engineer",
memoryEmbedding = Nothing,
memorySource = MemorySource "telegram" Nothing "chat",
memoryConfidence = 0.9,
memoryCreatedAt = now,
memoryLastAccessedAt = now,
memoryTags = []
}
mem2 =
Memory
{ memoryId = "2",
memoryUserId = "u",
memoryContent = "User prefers Haskell",
memoryEmbedding = Nothing,
memorySource = MemorySource "coder" Nothing "code review",
memoryConfidence = 0.8,
memoryCreatedAt = now,
memoryLastAccessedAt = now,
memoryTags = []
}
formatted = formatMemoriesForPrompt [mem1, mem2]
("AI engineer" `Text.isInfixOf` formatted) Test.@=? True
("Haskell" `Text.isInfixOf` formatted) Test.@=? True,
Test.unit "cosineSimilarity identical vectors" <| do
let v1 = VS.fromList [1.0, 0.0, 0.0 :: Float]
v2 = VS.fromList [1.0, 0.0, 0.0 :: Float]
abs (cosineSimilarity v1 v2 - 1.0) < 0.0001 Test.@=? True,
Test.unit "cosineSimilarity orthogonal vectors" <| do
let v1 = VS.fromList [1.0, 0.0, 0.0 :: Float]
v2 = VS.fromList [0.0, 1.0, 0.0 :: Float]
abs (cosineSimilarity v1 v2) < 0.0001 Test.@=? True,
Test.unit "cosineSimilarity opposite vectors" <| do
let v1 = VS.fromList [1.0, 0.0, 0.0 :: Float]
v2 = VS.fromList [-1.0, 0.0, 0.0 :: Float]
abs (cosineSimilarity v1 v2 + 1.0) < 0.0001 Test.@=? True,
Test.unit "vectorToBlob and blobToVector roundtrip" <| do
let v = VS.fromList [0.1, 0.2, 0.3, 0.4, 0.5 :: Float]
blob = vectorToBlob v
v' = blobToVector blob
VS.length v Test.@=? VS.length v'
VS.toList v Test.@=? VS.toList v',
Test.unit "rememberTool has correct schema" <| do
let tool = rememberTool "test-user-id"
Engine.toolName tool Test.@=? "remember",
Test.unit "recallTool has correct schema" <| do
let tool = recallTool "test-user-id"
Engine.toolName tool Test.@=? "recall",
Test.unit "RelationType JSON roundtrip" <| do
let types = [Contradicts, Supports, Elaborates, Supersedes, Related, ContingentOn]
forM_ types <| \rt ->
case Aeson.decode (Aeson.encode rt) of
Nothing -> Test.assertFailure ("Failed to decode RelationType: " <> show rt)
Just decoded -> decoded Test.@=? rt,
Test.unit "MemoryLink JSON roundtrip" <| do
now <- getCurrentTime
let memLink =
MemoryLink
{ linkFromMemoryId = "mem-1",
linkToMemoryId = "mem-2",
linkRelationType = Contradicts,
linkCreatedAt = now
}
case Aeson.decode (Aeson.encode memLink) of
Nothing -> Test.assertFailure "Failed to decode MemoryLink"
Just decoded -> do
linkFromMemoryId decoded Test.@=? "mem-1"
linkToMemoryId decoded Test.@=? "mem-2"
linkRelationType decoded Test.@=? Contradicts,
Test.unit "relationTypeToText and textToRelationType roundtrip" <| do
let types = [Contradicts, Supports, Elaborates, Supersedes, Related, ContingentOn]
forM_ types <| \rt ->
textToRelationType (relationTypeToText rt) Test.@=? Just rt,
Test.unit "linkMemoriesTool has correct schema" <| do
let tool = linkMemoriesTool "test-user-id"
Engine.toolName tool Test.@=? "link_memories",
Test.unit "queryGraphTool has correct schema" <| do
let tool = queryGraphTool "test-user-id"
Engine.toolName tool Test.@=? "query_graph"
]
-- | User record for multi-user memory system.
data User = User
{ userId :: Text,
userTelegramId :: Maybe Int,
userEmail :: Maybe Text,
userName :: Text,
userCreatedAt :: UTCTime
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON User where
toJSON u =
Aeson.object
[ "id" .= userId u,
"telegram_id" .= userTelegramId u,
"email" .= userEmail u,
"name" .= userName u,
"created_at" .= userCreatedAt u
]
instance Aeson.FromJSON User where
parseJSON =
Aeson.withObject "User" <| \v ->
(User </ (v .: "id"))
<*> (v .:? "telegram_id")
<*> (v .:? "email")
<*> (v .: "name")
<*> (v .: "created_at")
instance SQL.FromRow User where
fromRow =
User
</ SQL.field
<*> SQL.field
<*> SQL.field
<*> SQL.field
<*> SQL.field
instance SQL.ToRow User where
toRow u =
[ SQL.toField (userId u),
SQL.toField (userTelegramId u),
SQL.toField (userEmail u),
SQL.toField (userName u),
SQL.toField (userCreatedAt u)
]
-- | Source information for a memory.
data MemorySource = MemorySource
{ sourceAgent :: Text,
sourceSession :: Maybe Text,
sourceContext :: Text
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON MemorySource where
toJSON s =
Aeson.object
[ "agent" .= sourceAgent s,
"session" .= sourceSession s,
"context" .= sourceContext s
]
instance Aeson.FromJSON MemorySource where
parseJSON =
Aeson.withObject "MemorySource" <| \v ->
(MemorySource </ (v .: "agent"))
<*> (v .:? "session")
<*> (v .: "context")
-- | A memory stored in the system.
data Memory = Memory
{ memoryId :: Text,
memoryUserId :: Text,
memoryContent :: Text,
memoryEmbedding :: Maybe (VS.Vector Float),
memorySource :: MemorySource,
memoryConfidence :: Double,
memoryCreatedAt :: UTCTime,
memoryLastAccessedAt :: UTCTime,
memoryTags :: [Text]
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON Memory where
toJSON m =
Aeson.object
[ "id" .= memoryId m,
"user_id" .= memoryUserId m,
"content" .= memoryContent m,
"source" .= memorySource m,
"confidence" .= memoryConfidence m,
"created_at" .= memoryCreatedAt m,
"last_accessed_at" .= memoryLastAccessedAt m,
"tags" .= memoryTags m
]
instance Aeson.FromJSON Memory where
parseJSON =
Aeson.withObject "Memory" <| \v ->
( Memory
</ (v .: "id")
)
<*> (v .: "user_id")
<*> (v .: "content")
<*> pure Nothing
<*> (v .: "source")
<*> (v .:? "confidence" .!= 0.8)
<*> (v .: "created_at")
<*> (v .: "last_accessed_at")
<*> (v .:? "tags" .!= [])
-- SQLite instances for Memory (partial - embedding handled separately)
instance SQL.FromRow Memory where
fromRow = do
mid <- SQL.field
uid <- SQL.field
content <- SQL.field
embeddingBlob <- SQL.field
agent <- SQL.field
session <- SQL.field
context <- SQL.field
confidence <- SQL.field
createdAt <- SQL.field
lastAccessedAt <- SQL.field
tagsJson <- SQL.field
let embedding = blobToVector </ (embeddingBlob :: Maybe BS.ByteString)
source = MemorySource agent session context
tags = fromMaybe [] ((tagsJson :: Maybe Text) +> (Aeson.decode <. BL.fromStrict <. TE.encodeUtf8))
pure
Memory
{ memoryId = mid,
memoryUserId = uid,
memoryContent = content,
memoryEmbedding = embedding,
memorySource = source,
memoryConfidence = confidence,
memoryCreatedAt = createdAt,
memoryLastAccessedAt = lastAccessedAt,
memoryTags = tags
}
-- | Role in a conversation message.
data MessageRole = UserRole | AssistantRole
deriving (Show, Eq, Generic)
instance Aeson.ToJSON MessageRole where
toJSON UserRole = Aeson.String "user"
toJSON AssistantRole = Aeson.String "assistant"
instance Aeson.FromJSON MessageRole where
parseJSON =
Aeson.withText "MessageRole" <| \case
"user" -> pure UserRole
"assistant" -> pure AssistantRole
_ -> empty
-- | A message in a conversation.
data ConversationMessage = ConversationMessage
{ cmId :: Maybe Int,
cmUserId :: Text,
cmChatId :: Int,
cmRole :: MessageRole,
cmSenderName :: Maybe Text,
cmContent :: Text,
cmTokensEstimate :: Int,
cmCreatedAt :: UTCTime
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON ConversationMessage where
toJSON m =
Aeson.object
[ "id" .= cmId m,
"user_id" .= cmUserId m,
"chat_id" .= cmChatId m,
"role" .= cmRole m,
"sender_name" .= cmSenderName m,
"content" .= cmContent m,
"tokens_estimate" .= cmTokensEstimate m,
"created_at" .= cmCreatedAt m
]
instance SQL.FromRow ConversationMessage where
fromRow =
(ConversationMessage </ SQL.field)
<*> SQL.field
<*> SQL.field
<*> (parseRole </ SQL.field)
<*> SQL.field
<*> SQL.field
<*> (fromMaybe 0 </ SQL.field)
<*> SQL.field
where
parseRole :: Text -> MessageRole
parseRole "user" = UserRole
parseRole _ = AssistantRole
-- | A summary of older conversation messages.
data ConversationSummary = ConversationSummary
{ csId :: Maybe Int,
csUserId :: Text,
csChatId :: Int,
csSummary :: Text,
csMessagesSummarized :: Int,
csTokensSaved :: Maybe Int,
csCreatedAt :: UTCTime
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON ConversationSummary where
toJSON s =
Aeson.object
[ "id" .= csId s,
"user_id" .= csUserId s,
"chat_id" .= csChatId s,
"summary" .= csSummary s,
"messages_summarized" .= csMessagesSummarized s,
"tokens_saved" .= csTokensSaved s,
"created_at" .= csCreatedAt s
]
instance SQL.FromRow ConversationSummary where
fromRow =
(ConversationSummary </ SQL.field)
<*> SQL.field
<*> SQL.field
<*> SQL.field
<*> SQL.field
<*> SQL.field
<*> SQL.field
-- | Relation types for the knowledge graph.
data RelationType
= Contradicts
| Supports
| Elaborates
| Supersedes
| Related
| ContingentOn
deriving (Show, Eq, Generic, Ord)
instance Aeson.ToJSON RelationType where
toJSON Contradicts = Aeson.String "contradicts"
toJSON Supports = Aeson.String "supports"
toJSON Elaborates = Aeson.String "elaborates"
toJSON Supersedes = Aeson.String "supersedes"
toJSON Related = Aeson.String "related"
toJSON ContingentOn = Aeson.String "contingent_on"
instance Aeson.FromJSON RelationType where
parseJSON =
Aeson.withText "RelationType" <| \case
"contradicts" -> pure Contradicts
"supports" -> pure Supports
"elaborates" -> pure Elaborates
"supersedes" -> pure Supersedes
"related" -> pure Related
"contingent_on" -> pure ContingentOn
_ -> empty
relationTypeToText :: RelationType -> Text
relationTypeToText Contradicts = "contradicts"
relationTypeToText Supports = "supports"
relationTypeToText Elaborates = "elaborates"
relationTypeToText Supersedes = "supersedes"
relationTypeToText Related = "related"
relationTypeToText ContingentOn = "contingent_on"
textToRelationType :: Text -> Maybe RelationType
textToRelationType "contradicts" = Just Contradicts
textToRelationType "supports" = Just Supports
textToRelationType "elaborates" = Just Elaborates
textToRelationType "supersedes" = Just Supersedes
textToRelationType "related" = Just Related
textToRelationType "contingent_on" = Just ContingentOn
textToRelationType _ = Nothing
-- | A link between two memories in the knowledge graph.
data MemoryLink = MemoryLink
{ linkFromMemoryId :: Text,
linkToMemoryId :: Text,
linkRelationType :: RelationType,
linkCreatedAt :: UTCTime
}
deriving (Show, Eq, Generic)
instance Aeson.ToJSON MemoryLink where
toJSON l =
Aeson.object
[ "from_memory_id" .= linkFromMemoryId l,
"to_memory_id" .= linkToMemoryId l,
"relation_type" .= linkRelationType l,
"created_at" .= linkCreatedAt l
]
instance Aeson.FromJSON MemoryLink where
parseJSON =
Aeson.withObject "MemoryLink" <| \v ->
(MemoryLink </ (v .: "from_memory_id"))
<*> (v .: "to_memory_id")
<*> (v .: "relation_type")
<*> (v .: "created_at")
instance SQL.FromRow MemoryLink where
fromRow = do
fromId <- SQL.field
toId <- SQL.field
relTypeText <- SQL.field
createdAt <- SQL.field
let relType = fromMaybe Related (textToRelationType relTypeText)
pure
MemoryLink
{ linkFromMemoryId = fromId,
linkToMemoryId = toId,
linkRelationType = relType,
linkCreatedAt = createdAt
}
-- | Get the path to memory.db
getMemoryDbPath :: IO FilePath
getMemoryDbPath = do
maybeEnv <- lookupEnv "MEMORY_DB_PATH"
case maybeEnv of
Just p -> pure p
Nothing -> do
home <- lookupEnv "HOME"
case home of
Just h -> pure (h </> ".local/share/omni/memory.db")
Nothing -> pure "_/memory.db"
-- | Run an action with the memory database connection.
withMemoryDb :: (SQL.Connection -> IO a) -> IO a
withMemoryDb action = do
dbPath <- getMemoryDbPath
createDirectoryIfMissing True (takeDirectory dbPath)
SQL.withConnection dbPath <| \conn -> do
initMemoryDb conn
action conn
-- | Initialize the memory database schema.
initMemoryDb :: SQL.Connection -> IO ()
initMemoryDb conn = do
SQL.execute_ conn "PRAGMA busy_timeout = 10000"
SQL.execute_ conn "PRAGMA foreign_keys = ON"
_ <- SQL.query_ conn "PRAGMA journal_mode = WAL" :: IO [[Text]]
SQL.execute_
conn
"CREATE TABLE IF NOT EXISTS users (\
\ id TEXT PRIMARY KEY,\
\ telegram_id INTEGER UNIQUE,\
\ email TEXT UNIQUE,\
\ name TEXT NOT NULL,\
\ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\
\)"
SQL.execute_
conn
"CREATE TABLE IF NOT EXISTS memories (\
\ id TEXT PRIMARY KEY,\
\ user_id TEXT NOT NULL REFERENCES users(id),\
\ content TEXT NOT NULL,\
\ embedding BLOB,\
\ source_agent TEXT NOT NULL,\
\ source_session TEXT,\
\ source_context TEXT,\
\ confidence REAL DEFAULT 0.8,\
\ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\
\ last_accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\
\ tags TEXT\
\)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(source_agent)"
SQL.execute_
conn
"CREATE TABLE IF NOT EXISTS conversation_messages (\
\ id INTEGER PRIMARY KEY AUTOINCREMENT,\
\ user_id TEXT NOT NULL REFERENCES users(id),\
\ chat_id INTEGER NOT NULL,\
\ role TEXT NOT NULL,\
\ sender_name TEXT,\
\ content TEXT NOT NULL,\
\ tokens_estimate INTEGER,\
\ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\
\)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_conv_user_chat ON conversation_messages(user_id, chat_id)"
migrateConversationMessages conn
SQL.execute_
conn
"CREATE TABLE IF NOT EXISTS conversation_summaries (\
\ id INTEGER PRIMARY KEY AUTOINCREMENT,\
\ user_id TEXT NOT NULL REFERENCES users(id),\
\ chat_id INTEGER NOT NULL,\
\ summary TEXT NOT NULL,\
\ messages_summarized INTEGER NOT NULL,\
\ tokens_saved INTEGER,\
\ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\
\)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_summary_user_chat ON conversation_summaries(user_id, chat_id)"
SQL.execute_
conn
"CREATE TABLE IF NOT EXISTS notes (\
\ id INTEGER PRIMARY KEY AUTOINCREMENT,\
\ user_id TEXT NOT NULL,\
\ topic TEXT NOT NULL,\
\ content TEXT NOT NULL,\
\ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\
\)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_notes_user ON notes(user_id)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_notes_topic ON notes(user_id, topic)"
SQL.execute_
conn
"CREATE TABLE IF NOT EXISTS todos (\
\ id INTEGER PRIMARY KEY AUTOINCREMENT,\
\ user_id TEXT NOT NULL,\
\ title TEXT NOT NULL,\
\ due_date TIMESTAMP,\
\ completed INTEGER NOT NULL DEFAULT 0,\
\ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\
\)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_todos_user ON todos(user_id)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_todos_due ON todos(user_id, due_date)"
SQL.execute_
conn
"CREATE TABLE IF NOT EXISTS memory_links (\
\ from_memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE,\
\ to_memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE,\
\ relation_type TEXT NOT NULL,\
\ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\
\ PRIMARY KEY (from_memory_id, to_memory_id, relation_type)\
\)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_memory_links_from ON memory_links(from_memory_id)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_memory_links_to ON memory_links(to_memory_id)"
SQL.execute_
conn
"CREATE INDEX IF NOT EXISTS idx_memory_links_type ON memory_links(relation_type)"
-- | Migrate conversation_messages to add sender_name and thread_id columns.
migrateConversationMessages :: SQL.Connection -> IO ()
migrateConversationMessages conn = do
columns <- SQL.query_ conn "PRAGMA table_info(conversation_messages)" :: IO [(Int, Text, Text, Int, Maybe Text, Int)]
let columnNames = map (\(_, name, _, _, _, _) -> name) columns
unless ("sender_name" `elem` columnNames) <| do
SQL.execute_ conn "ALTER TABLE conversation_messages ADD COLUMN sender_name TEXT"
SQL.execute_ conn "UPDATE conversation_messages SET sender_name = 'bensima' WHERE role = 'user' AND sender_name IS NULL"
unless ("thread_id" `elem` columnNames) <| do
SQL.execute_ conn "ALTER TABLE conversation_messages ADD COLUMN thread_id INTEGER"
SQL.execute_ conn "CREATE INDEX IF NOT EXISTS idx_conv_chat_thread ON conversation_messages(chat_id, thread_id)"
-- | Create a new user.
createUser :: Text -> Maybe Int -> IO User
createUser name telegramId = do
uuid <- UUID.nextRandom
now <- getCurrentTime
let user =
User
{ userId = UUID.toText uuid,
userTelegramId = telegramId,
userEmail = Nothing,
userName = name,
userCreatedAt = now
}
withMemoryDb <| \conn ->
SQL.execute
conn
"INSERT INTO users (id, telegram_id, email, name, created_at) VALUES (?, ?, ?, ?, ?)"
user
pure user
-- | Get a user by ID.
getUser :: Text -> IO (Maybe User)
getUser uid =
withMemoryDb <| \conn -> do
results <- SQL.query conn "SELECT id, telegram_id, email, name, created_at FROM users WHERE id = ?" (SQL.Only uid)
pure (listToMaybe results)
-- | Get a user by Telegram ID.
getUserByTelegramId :: Int -> IO (Maybe User)
getUserByTelegramId tid =
withMemoryDb <| \conn -> do
results <- SQL.query conn "SELECT id, telegram_id, email, name, created_at FROM users WHERE telegram_id = ?" (SQL.Only tid)
pure (listToMaybe results)
-- | Get or create a user by Telegram ID.
getOrCreateUserByTelegramId :: Int -> Text -> IO User
getOrCreateUserByTelegramId tid name = do
existing <- getUserByTelegramId tid
case existing of
Just user -> pure user
Nothing -> createUser name (Just tid)
-- | Store a memory for a user.
storeMemory :: Text -> Text -> MemorySource -> IO Memory
storeMemory uid content source = storeMemoryWithTags uid content source []
-- | Store a memory with tags.
storeMemoryWithTags :: Text -> Text -> MemorySource -> [Text] -> IO Memory
storeMemoryWithTags uid content source tags = do
uuid <- UUID.nextRandom
now <- getCurrentTime
embedding <- embedText content
let mem =
Memory
{ memoryId = UUID.toText uuid,
memoryUserId = uid,
memoryContent = content,
memoryEmbedding = either (const Nothing) Just embedding,
memorySource = source,
memoryConfidence = 0.8,
memoryCreatedAt = now,
memoryLastAccessedAt = now,
memoryTags = tags
}
withMemoryDb <| \conn ->
SQL.execute
conn
"INSERT INTO memories (id, user_id, content, embedding, source_agent, source_session, source_context, confidence, created_at, last_accessed_at, tags) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
( ( memoryId mem,
memoryUserId mem,
memoryContent mem,
vectorToBlob </ memoryEmbedding mem,
sourceAgent (memorySource mem),
sourceSession (memorySource mem),
sourceContext (memorySource mem)
)
SQL.:. ( memoryConfidence mem,
memoryCreatedAt mem,
memoryLastAccessedAt mem,
TE.decodeUtf8 (BL.toStrict (Aeson.encode (memoryTags mem)))
)
)
pure mem
-- | Recall memories for a user using semantic similarity.
recallMemories :: Text -> Text -> Int -> IO [Memory]
recallMemories uid query limit = do
queryEmbedding <- embedText query
case queryEmbedding of
Left _ -> recallMemoriesByRecency uid limit
Right qEmb -> do
allMems <- getAllMemoriesForUser uid
let scored =
[ (m, cosineSimilarity qEmb emb)
| m <- allMems,
Just emb <- [memoryEmbedding m]
]
sorted = List.sortBy (\(_, s1) (_, s2) -> compare s2 s1) scored
topN = take limit sorted
now <- getCurrentTime
traverse_ (updateMemoryAccess now <. memoryId <. fst) topN
pure (map fst topN)
-- | Recall memories by recency (fallback when embedding fails).
recallMemoriesByRecency :: Text -> Int -> IO [Memory]
recallMemoriesByRecency uid limit =
withMemoryDb <| \conn -> do
SQL.query
conn
"SELECT id, user_id, content, embedding, source_agent, source_session, source_context, confidence, created_at, last_accessed_at, tags \
\FROM memories WHERE user_id = ? ORDER BY last_accessed_at DESC LIMIT ?"
(uid, limit)
-- | Get all memories for a user.
getAllMemoriesForUser :: Text -> IO [Memory]
getAllMemoriesForUser uid =
withMemoryDb <| \conn ->
SQL.query
conn
"SELECT id, user_id, content, embedding, source_agent, source_session, source_context, confidence, created_at, last_accessed_at, tags \
\FROM memories WHERE user_id = ?"
(SQL.Only uid)
-- | Delete a memory.
forgetMemory :: Text -> IO ()
forgetMemory mid =
withMemoryDb <| \conn ->
SQL.execute conn "DELETE FROM memories WHERE id = ?" (SQL.Only mid)
-- | Update memory's last accessed timestamp.
updateMemoryAccess :: UTCTime -> Text -> IO ()
updateMemoryAccess now mid =
withMemoryDb <| \conn ->
SQL.execute conn "UPDATE memories SET last_accessed_at = ? WHERE id = ?" (now, mid)
-- | Create a link between two memories.
linkMemories :: Text -> Text -> RelationType -> IO MemoryLink
linkMemories fromId toId relType = do
now <- getCurrentTime
withMemoryDb <| \conn ->
SQL.execute
conn
"INSERT OR REPLACE INTO memory_links (from_memory_id, to_memory_id, relation_type, created_at) VALUES (?, ?, ?, ?)"
(fromId, toId, relationTypeToText relType, now)
pure
MemoryLink
{ linkFromMemoryId = fromId,
linkToMemoryId = toId,
linkRelationType = relType,
linkCreatedAt = now
}
-- | Get all links from a memory.
getMemoryLinks :: Text -> IO [MemoryLink]
getMemoryLinks memId =
withMemoryDb <| \conn ->
SQL.query
conn
"SELECT from_memory_id, to_memory_id, relation_type, created_at \
\FROM memory_links WHERE from_memory_id = ? OR to_memory_id = ?"
(memId, memId)
-- | Get memories linked to a given memory with their content.
getLinkedMemories :: Text -> Maybe RelationType -> IO [(MemoryLink, Memory)]
getLinkedMemories memId maybeRelType = do
links <- getMemoryLinks memId
let filteredLinks = case maybeRelType of
Nothing -> links
Just rt -> filter (\l -> linkRelationType l == rt) links
mems <- traverse loadMemory filteredLinks
pure [(l, m) | (l, Just m) <- zip filteredLinks mems]
where
loadMemory memLink = do
let targetId =
if linkFromMemoryId memLink == memId
then linkToMemoryId memLink
else linkFromMemoryId memLink
withMemoryDb <| \conn -> do
results <-
SQL.query
conn
"SELECT id, user_id, content, embedding, source_agent, source_session, source_context, confidence, created_at, last_accessed_at, tags \
\FROM memories WHERE id = ?"
(SQL.Only targetId)
pure (listToMaybe results)
-- | Query the knowledge graph by traversing links from a starting memory.
-- Returns all memories reachable within the given depth.
queryGraph :: Text -> Int -> Maybe RelationType -> IO [(Memory, [MemoryLink])]
queryGraph startMemId maxDepth maybeRelType = do
startMem <- getMemoryById startMemId
case startMem of
Nothing -> pure []
Just mem -> go [startMemId] [(mem, [])] 0
where
go :: [Text] -> [(Memory, [MemoryLink])] -> Int -> IO [(Memory, [MemoryLink])]
go _ acc depth | depth >= maxDepth = pure acc
go visitedIds acc depth = do
let currentIds = map (memoryId <. fst) acc
newIds = filter (`notElem` visitedIds) currentIds
if null newIds
then pure acc
else do
newLinked <- concat </ traverse (`getLinkedMemories` maybeRelType) newIds
let newMems = [(m, [l]) | (l, m) <- newLinked, memoryId m `notElem` visitedIds]
newVisited = visitedIds <> map (memoryId <. fst) newMems
go newVisited (acc <> newMems) (depth + 1)
-- | Get a memory by ID.
getMemoryById :: Text -> IO (Maybe Memory)
getMemoryById memId =
withMemoryDb <| \conn -> do
results <-
SQL.query
conn
"SELECT id, user_id, content, embedding, source_agent, source_session, source_context, confidence, created_at, last_accessed_at, tags \
\FROM memories WHERE id = ?"
(SQL.Only memId)
pure (listToMaybe results)
-- | Embed text using Ollama's nomic-embed-text model.
embedText :: Text -> IO (Either Text (VS.Vector Float))
embedText content = do
ollamaUrl <- fromMaybe "http://localhost:11434" </ lookupEnv "OLLAMA_URL"
let url = ollamaUrl <> "/api/embeddings"
req0 <- HTTP.parseRequest url
let body =
Aeson.object
[ "model" .= ("nomic-embed-text" :: Text),
"prompt" .= content
]
req =
HTTP.setRequestMethod "POST"
<| HTTP.setRequestHeader "Content-Type" ["application/json"]
<| HTTP.setRequestBodyLBS (Aeson.encode body)
<| req0
result <- try (HTTP.httpLBS req)
case result of
Left (e :: SomeException) ->
pure (Left ("Embedding request failed: " <> tshow e))
Right response -> do
let status = HTTP.getResponseStatusCode response
if status >= 200 && status < 300
then case Aeson.decode (HTTP.getResponseBody response) of
Just (Aeson.Object obj) -> case KeyMap.lookup "embedding" obj of
Just (Aeson.Array arr) ->
let floats = [f | Aeson.Number n <- toList arr, let f = realToFrac n]
in pure (Right (VS.fromList floats))
_ -> pure (Left "No embedding in response")
_ -> pure (Left "Failed to parse embedding response")
else pure (Left ("Embedding HTTP error: " <> tshow status))
-- | Convert a vector to a blob for storage.
vectorToBlob :: VS.Vector Float -> BS.ByteString
vectorToBlob v =
let bytes = VS.unsafeCast v :: VS.Vector Word8
in BS.pack (VS.toList bytes)
-- | Convert a blob back to a vector.
blobToVector :: BS.ByteString -> VS.Vector Float
blobToVector bs =
let bytes = VS.fromList (BS.unpack bs) :: VS.Vector Word8
in VS.unsafeCast bytes
-- | Calculate cosine similarity between two vectors.
cosineSimilarity :: VS.Vector Float -> VS.Vector Float -> Float
cosineSimilarity v1 v2
| VS.length v1 /= VS.length v2 = 0
| otherwise =
let dot = VS.sum (VS.zipWith (*) v1 v2)
mag1 = sqrt (VS.sum (VS.map (\x -> x * x) v1))
mag2 = sqrt (VS.sum (VS.map (\x -> x * x) v2))
in if mag1 == 0 || mag2 == 0 then 0 else dot / (mag1 * mag2)
-- | Format memories for inclusion in a prompt.
formatMemoriesForPrompt :: [Memory] -> Text
formatMemoriesForPrompt [] = "No prior context available."
formatMemoriesForPrompt mems =
Text.unlines
[ "Known context about this user:",
"",
Text.unlines (map formatMem mems)
]
where
formatMem m =
"- " <> memoryContent m <> " (via " <> sourceAgent (memorySource m) <> ")"
-- | Run an agent with memory context.
-- Recalls relevant memories for the user and injects them into the system prompt.
runAgentWithMemory ::
User ->
Engine.EngineConfig ->
Engine.AgentConfig ->
Text ->
IO (Either Text Engine.AgentResult)
runAgentWithMemory user engineCfg agentCfg userPrompt = do
memories <- recallMemories (userId user) userPrompt 10
let memoryContext = formatMemoriesForPrompt memories
enhancedPrompt =
Engine.agentSystemPrompt agentCfg
<> "\n\n## Known about this user\n"
<> memoryContext
enhancedConfig =
agentCfg
{ Engine.agentSystemPrompt = enhancedPrompt,
Engine.agentTools =
Engine.agentTools agentCfg
<> [ rememberTool (userId user),
recallTool (userId user),
linkMemoriesTool (userId user),
queryGraphTool (userId user)
]
}
Engine.runAgent engineCfg enhancedConfig userPrompt
-- | Tool for agents to store memories about users.
rememberTool :: Text -> Engine.Tool
rememberTool uid =
Engine.Tool
{ Engine.toolName = "remember",
Engine.toolDescription =
"Store a piece of information about the user for future reference. "
<> "Use this when the user shares personal facts, preferences, or context "
<> "that would be useful to recall in future conversations.",
Engine.toolJsonSchema =
Aeson.object
[ "type" .= ("object" :: Text),
"properties"
.= Aeson.object
[ "content"
.= Aeson.object
[ "type" .= ("string" :: Text),
"description" .= ("The information to remember about the user" :: Text)
],
"context"
.= Aeson.object
[ "type" .= ("string" :: Text),
"description" .= ("How/why this was learned (e.g., 'user mentioned in chat')" :: Text)
],
"tags"
.= Aeson.object
[ "type" .= ("array" :: Text),
"items" .= Aeson.object ["type" .= ("string" :: Text)],
"description" .= ("Optional tags for categorization" :: Text)
]
],
"required" .= (["content", "context"] :: [Text])
],
Engine.toolExecute = executeRemember uid
}
executeRemember :: Text -> Aeson.Value -> IO Aeson.Value
executeRemember uid v =
case Aeson.fromJSON v of
Aeson.Error e -> pure (Aeson.object ["error" .= Text.pack e])
Aeson.Success (args :: RememberArgs) -> do
let source =
MemorySource
{ sourceAgent = "agent",
sourceSession = Nothing,
sourceContext = rememberContext args
}
mem <- storeMemoryWithTags uid (rememberContent args) source (rememberTags args)
pure
( Aeson.object
[ "success" .= True,
"memory_id" .= memoryId mem,
"message" .= ("Remembered: " <> rememberContent args)
]
)
-- | Tool for agents to recall memories about users.
recallTool :: Text -> Engine.Tool
recallTool uid =
Engine.Tool
{ Engine.toolName = "recall",
Engine.toolDescription =
"Search your memory for information about the user. "
<> "Use this to retrieve previously stored facts, preferences, or context.",
Engine.toolJsonSchema =
Aeson.object
[ "type" .= ("object" :: Text),
"properties"
.= Aeson.object
[ "query"
.= Aeson.object
[ "type" .= ("string" :: Text),
"description" .= ("What to search for in memory" :: Text)
],
"limit"
.= Aeson.object
[ "type" .= ("integer" :: Text),
"description" .= ("Maximum memories to return (default: 5)" :: Text)
]
],
"required" .= (["query"] :: [Text])
],
Engine.toolExecute = executeRecall uid
}
executeRecall :: Text -> Aeson.Value -> IO Aeson.Value
executeRecall uid v =
case Aeson.fromJSON v of
Aeson.Error e -> pure (Aeson.object ["error" .= Text.pack e])
Aeson.Success (args :: RecallArgs) -> do
mems <- recallMemories uid (recallQuery args) (recallLimit args)
pure
( Aeson.object
[ "success" .= True,
"count" .= length mems,
"memories"
.= map
( \m ->
Aeson.object
[ "id" .= memoryId m,
"content" .= memoryContent m,
"confidence" .= memoryConfidence m,
"source" .= sourceAgent (memorySource m),
"tags" .= memoryTags m
]
)
mems
]
)
-- Helper for parsing remember args
data RememberArgs = RememberArgs
{ rememberContent :: Text,
rememberContext :: Text,
rememberTags :: [Text]
}
deriving (Generic)
instance Aeson.FromJSON RememberArgs where
parseJSON =
Aeson.withObject "RememberArgs" <| \v ->
(RememberArgs </ (v .: "content"))
<*> (v .:? "context" .!= "agent observation")
<*> (v .:? "tags" .!= [])
data RecallArgs = RecallArgs
{ recallQuery :: Text,
recallLimit :: Int
}
deriving (Generic)
instance Aeson.FromJSON RecallArgs where
parseJSON =
Aeson.withObject "RecallArgs" <| \v ->
(RecallArgs </ (v .: "query"))
<*> (v .:? "limit" .!= 5)
-- | Tool for agents to link memories in the knowledge graph.
linkMemoriesTool :: Text -> Engine.Tool
linkMemoriesTool _uid =
Engine.Tool
{ Engine.toolName = "link_memories",
Engine.toolDescription =
"Create a typed relationship between two memories. "
<> "Use this to connect related information. Relation types:\n"
<> "- contradicts: conflicting information\n"
<> "- supports: evidence that reinforces another memory\n"
<> "- elaborates: adds detail to an existing memory\n"
<> "- supersedes: newer info replaces older\n"
<> "- related: general topical connection\n"
<> "- contingent_on: depends on another fact being true",
Engine.toolJsonSchema =
Aeson.object
[ "type" .= ("object" :: Text),
"properties"
.= Aeson.object
[ "from_memory_id"
.= Aeson.object
[ "type" .= ("string" :: Text),
"description" .= ("ID of the source memory" :: Text)
],
"to_memory_id"
.= Aeson.object
[ "type" .= ("string" :: Text),
"description" .= ("ID of the target memory" :: Text)
],
"relation_type"
.= Aeson.object
[ "type" .= ("string" :: Text),
"enum" .= (["contradicts", "supports", "elaborates", "supersedes", "related", "contingent_on"] :: [Text]),
"description" .= ("Type of relationship between memories" :: Text)
]
],
"required" .= (["from_memory_id", "to_memory_id", "relation_type"] :: [Text])
],
Engine.toolExecute = executeLinkMemories
}
executeLinkMemories :: Aeson.Value -> IO Aeson.Value
executeLinkMemories v =
case Aeson.fromJSON v of
Aeson.Error e -> pure (Aeson.object ["error" .= Text.pack e])
Aeson.Success (args :: LinkMemoriesArgs) -> do
case textToRelationType (linkArgsRelationType args) of
Nothing ->
pure
( Aeson.object
[ "success" .= False,
"error" .= ("Invalid relation type: " <> linkArgsRelationType args)
]
)
Just relType -> do
memLink <- linkMemories (linkArgsFromId args) (linkArgsToId args) relType
pure
( Aeson.object
[ "success" .= True,
"message"
.= ( "Linked memory "
<> linkFromMemoryId memLink
<> " -> "
<> linkToMemoryId memLink
<> " ("
<> relationTypeToText (linkRelationType memLink)
<> ")"
)
]
)
data LinkMemoriesArgs = LinkMemoriesArgs
{ linkArgsFromId :: Text,
linkArgsToId :: Text,
linkArgsRelationType :: Text
}
deriving (Generic)
instance Aeson.FromJSON LinkMemoriesArgs where
parseJSON =
Aeson.withObject "LinkMemoriesArgs" <| \v ->
(LinkMemoriesArgs </ (v .: "from_memory_id"))
<*> (v .: "to_memory_id")
<*> (v .: "relation_type")
-- | Tool for agents to query the memory knowledge graph.
queryGraphTool :: Text -> Engine.Tool
queryGraphTool _uid =
Engine.Tool
{ Engine.toolName = "query_graph",
Engine.toolDescription =
"Explore the knowledge graph to find related memories. "
<> "Given a starting memory, traverse links to find connected memories. "
<> "Useful for understanding context and finding contradictions or supporting evidence.",
Engine.toolJsonSchema =
Aeson.object
[ "type" .= ("object" :: Text),
"properties"
.= Aeson.object
[ "memory_id"
.= Aeson.object
[ "type" .= ("string" :: Text),
"description" .= ("ID of the memory to start from" :: Text)
],
"depth"
.= Aeson.object
[ "type" .= ("integer" :: Text),
"description" .= ("How many link hops to traverse (default: 2)" :: Text)
],
"relation_type"
.= Aeson.object
[ "type" .= ("string" :: Text),
"enum" .= (["contradicts", "supports", "elaborates", "supersedes", "related", "contingent_on"] :: [Text]),
"description" .= ("Optional: filter by relation type" :: Text)
]
],
"required" .= (["memory_id"] :: [Text])
],
Engine.toolExecute = executeQueryGraph
}
executeQueryGraph :: Aeson.Value -> IO Aeson.Value
executeQueryGraph v =
case Aeson.fromJSON v of
Aeson.Error e -> pure (Aeson.object ["error" .= Text.pack e])
Aeson.Success (args :: QueryGraphArgs) -> do
let maybeRelType = queryArgsRelationType args +> textToRelationType
results <- queryGraph (queryArgsMemoryId args) (queryArgsDepth args) maybeRelType
pure
( Aeson.object
[ "success" .= True,
"count" .= length results,
"memories"
.= map
( \(m, links) ->
Aeson.object
[ "id" .= memoryId m,
"content" .= memoryContent m,
"links"
.= map
( \l ->
Aeson.object
[ "from" .= linkFromMemoryId l,
"to" .= linkToMemoryId l,
"relation" .= linkRelationType l
]
)
links
]
)
results
]
)
data QueryGraphArgs = QueryGraphArgs
{ queryArgsMemoryId :: Text,
queryArgsDepth :: Int,
queryArgsRelationType :: Maybe Text
}
deriving (Generic)
instance Aeson.FromJSON QueryGraphArgs where
parseJSON =
Aeson.withObject "QueryGraphArgs" <| \v ->
(QueryGraphArgs </ (v .: "memory_id"))
<*> (v .:? "depth" .!= 2)
<*> (v .:? "relation_type")
-- | Estimate token count for text (rough: ~4 chars per token).
estimateTokens :: Text -> Int
estimateTokens t = max 1 (Text.length t `div` 4)
-- | Save a message to conversation history.
saveMessage :: Text -> Int -> MessageRole -> Maybe Text -> Text -> IO ConversationMessage
saveMessage uid chatId role senderName content = do
now <- getCurrentTime
let tokens = estimateTokens content
withMemoryDb <| \conn -> do
SQL.execute
conn
"INSERT INTO conversation_messages (user_id, chat_id, role, sender_name, content, tokens_estimate, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
(uid, chatId, roleToText role, senderName, content, tokens, now)
rowId <- SQL.lastInsertRowId conn
pure
ConversationMessage
{ cmId = Just (fromIntegral rowId),
cmUserId = uid,
cmChatId = chatId,
cmRole = role,
cmSenderName = senderName,
cmContent = content,
cmTokensEstimate = tokens,
cmCreatedAt = now
}
where
roleToText UserRole = "user" :: Text
roleToText AssistantRole = "assistant"
-- | Get recent messages for a user/chat, newest first.
getRecentMessages :: Text -> Int -> Int -> IO [ConversationMessage]
getRecentMessages uid chatId limit =
withMemoryDb <| \conn ->
SQL.query
conn
"SELECT id, user_id, chat_id, role, sender_name, content, tokens_estimate, created_at \
\FROM conversation_messages \
\WHERE user_id = ? AND chat_id = ? \
\ORDER BY created_at DESC LIMIT ?"
(uid, chatId, limit)
-- | Get the most recent summary for a chat.
getLatestSummary :: Text -> Int -> IO (Maybe ConversationSummary)
getLatestSummary uid chatId =
withMemoryDb <| \conn -> do
rows <-
SQL.query
conn
"SELECT id, user_id, chat_id, summary, messages_summarized, tokens_saved, created_at \
\FROM conversation_summaries \
\WHERE user_id = ? AND chat_id = ? \
\ORDER BY created_at DESC LIMIT 1"
(uid, chatId)
pure (listToMaybe rows)
-- | Build conversation context for the LLM.
-- Returns (context text, total token estimate).
getConversationContext :: Text -> Int -> Int -> IO (Text, Int)
getConversationContext uid chatId maxTokens = do
maybeSummary <- getLatestSummary uid chatId
recentMsgs <- getRecentMessages uid chatId 50
let summaryText = maybe "" (\s -> "## Previous conversation summary\n" <> csSummary s <> "\n\n") maybeSummary
summaryTokens = maybe 0 (estimateTokens <. csSummary) maybeSummary
msgsOldestFirst = reverse recentMsgs
availableTokens = maxTokens - summaryTokens - 100
(selectedMsgs, usedTokens) = selectMessages msgsOldestFirst availableTokens
formattedMsgs =
if null selectedMsgs
then ""
else
"## Recent conversation\n"
<> Text.unlines (map formatMsg selectedMsgs)
pure (summaryText <> formattedMsgs, summaryTokens + usedTokens)
where
selectMessages :: [ConversationMessage] -> Int -> ([ConversationMessage], Int)
selectMessages msgs budget = go (reverse msgs) budget []
where
go [] _ acc = (acc, sum (map cmTokensEstimate acc))
go (m : ms) remaining acc
| cmTokensEstimate m <= remaining =
go ms (remaining - cmTokensEstimate m) (m : acc)
| otherwise = (acc, sum (map cmTokensEstimate acc))
formatMsg m =
let timestamp = Text.pack (formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ" (cmCreatedAt m))
prefix = case cmRole m of
UserRole -> "[" <> timestamp <> "] " <> fromMaybe "User" (cmSenderName m) <> ": "
AssistantRole -> "[" <> timestamp <> "] Assistant: "
in prefix <> cmContent m
-- | Summarize old messages and archive them.
-- Returns the new summary text.
summarizeAndArchive :: Text -> Int -> Text -> IO Text
summarizeAndArchive uid chatId summaryText = do
now <- getCurrentTime
(oldMsgCount, tokensSaved) <-
withMemoryDb <| \conn -> do
rows <-
SQL.query
conn
"SELECT COUNT(*), COALESCE(SUM(tokens_estimate), 0) FROM conversation_messages WHERE user_id = ? AND chat_id = ?"
(uid, chatId) ::
IO [(Int, Int)]
let (count, tokens) = fromMaybe (0, 0) (listToMaybe rows)
SQL.execute
conn
"INSERT INTO conversation_summaries (user_id, chat_id, summary, messages_summarized, tokens_saved, created_at) VALUES (?, ?, ?, ?, ?, ?)"
(uid, chatId, summaryText, count, tokens, now)
SQL.execute
conn
"DELETE FROM conversation_messages WHERE user_id = ? AND chat_id = ?"
(uid, chatId)
pure (count, tokens)
putText <| "Archived " <> tshow oldMsgCount <> " messages (" <> tshow tokensSaved <> " tokens) for chat " <> tshow chatId
pure summaryText
-- -----------------------------------------------------------------------------
-- Group Conversation History
-- -----------------------------------------------------------------------------
-- | Save a message to group conversation history.
-- Unlike saveMessage, this is keyed by (chat_id, thread_id) not (user_id, chat_id).
-- The sender_name is preserved for attribution.
saveGroupMessage :: Int -> Maybe Int -> MessageRole -> Text -> Text -> IO ConversationMessage
saveGroupMessage chatId mThreadId role senderName content = do
now <- getCurrentTime
let tokens = estimateTokens content
withMemoryDb <| \conn -> do
SQL.execute
conn
"INSERT INTO conversation_messages (user_id, chat_id, thread_id, role, sender_name, content, tokens_estimate, created_at) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)"
(chatId, mThreadId, roleToText role, senderName, content, tokens, now)
rowId <- SQL.lastInsertRowId conn
pure
ConversationMessage
{ cmId = Just (fromIntegral rowId),
cmUserId = "",
cmChatId = chatId,
cmRole = role,
cmSenderName = Just senderName,
cmContent = content,
cmTokensEstimate = tokens,
cmCreatedAt = now
}
where
roleToText UserRole = "user" :: Text
roleToText AssistantRole = "assistant"
-- | Get recent messages for a group chat/topic, newest first.
getGroupRecentMessages :: Int -> Maybe Int -> Int -> IO [ConversationMessage]
getGroupRecentMessages chatId mThreadId limit =
withMemoryDb <| \conn ->
case mThreadId of
Just threadId ->
SQL.query
conn
"SELECT id, COALESCE(user_id, ''), chat_id, role, sender_name, content, tokens_estimate, created_at \
\FROM conversation_messages \
\WHERE chat_id = ? AND thread_id = ? \
\ORDER BY created_at DESC LIMIT ?"
(chatId, threadId, limit)
Nothing ->
SQL.query
conn
"SELECT id, COALESCE(user_id, ''), chat_id, role, sender_name, content, tokens_estimate, created_at \
\FROM conversation_messages \
\WHERE chat_id = ? AND thread_id IS NULL \
\ORDER BY created_at DESC LIMIT ?"
(chatId, limit)
-- | Build conversation context for a group chat.
-- Returns (context text, total token estimate).
getGroupConversationContext :: Int -> Maybe Int -> Int -> IO (Text, Int)
getGroupConversationContext chatId mThreadId maxTokens = do
recentMsgs <- getGroupRecentMessages chatId mThreadId 50
let msgsOldestFirst = reverse recentMsgs
availableTokens = maxTokens - 100
(selectedMsgs, usedTokens) = selectMessages msgsOldestFirst availableTokens
formattedMsgs =
if null selectedMsgs
then ""
else
"## Recent conversation\n"
<> Text.unlines (map formatMsg selectedMsgs)
pure (formattedMsgs, usedTokens)
where
selectMessages :: [ConversationMessage] -> Int -> ([ConversationMessage], Int)
selectMessages msgs budget = go (reverse msgs) budget []
where
go [] _ acc = (acc, sum (map cmTokensEstimate acc))
go (m : ms) remaining acc
| cmTokensEstimate m <= remaining =
go ms (remaining - cmTokensEstimate m) (m : acc)
| otherwise = (acc, sum (map cmTokensEstimate acc))
formatMsg m =
let timestamp = Text.pack (formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ" (cmCreatedAt m))
prefix = case cmRole m of
UserRole -> "[" <> timestamp <> "] " <> fromMaybe "User" (cmSenderName m) <> ": "
AssistantRole -> "[" <> timestamp <> "] Assistant: "
in prefix <> cmContent m
-- -----------------------------------------------------------------------------
-- Group Memories
-- -----------------------------------------------------------------------------
-- | Generate a synthetic user_id for group-level memories.
groupUserId :: Int -> Text
groupUserId chatId = "group:" <> tshow chatId
-- | Store a memory associated with a group (not a user).
-- These memories are shared across all users in the group.
storeGroupMemory :: Int -> Text -> MemorySource -> IO Memory
storeGroupMemory chatId = storeMemory (groupUserId chatId)
-- | Recall memories for a group.
recallGroupMemories :: Int -> Text -> Int -> IO [Memory]
recallGroupMemories chatId = recallMemories (groupUserId chatId)
|