summaryrefslogtreecommitdiff
path: root/Omni/Agent/Memory.hs
blob: 136ac1e32f397017a81b48e89cea263b1b22b093 (plain)
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
{-# 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 (..),

    -- * User Management
    createUser,
    getUser,
    getUserByTelegramId,
    getOrCreateUserByTelegramId,

    -- * Memory Operations
    storeMemory,
    recallMemories,
    forgetMemory,
    getAllMemoriesForUser,
    updateMemoryAccess,

    -- * Conversation History
    saveMessage,
    getRecentMessages,
    getConversationContext,
    summarizeAndArchive,
    estimateTokens,

    -- * Embeddings
    embedText,

    -- * Agent Integration
    rememberTool,
    recallTool,
    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 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"
    ]

-- | 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

-- | 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 foreign_keys = ON"
  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)"

-- | Migrate conversation_messages to add sender_name column.
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"

-- | 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)

-- | 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)]
          }
  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
                        [ "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)

-- | 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 prefix = case cmRole m of
            UserRole -> fromMaybe "User" (cmSenderName m) <> ": "
            AssistantRole -> "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