summaryrefslogtreecommitdiff
path: root/Omni/Fact.hs
blob: 57db7fc2e60f111939e4be06a033cd4d06a52643 (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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | Fact module for the Jr knowledge base.
--
-- Facts are pieces of knowledge learned during task execution that can
-- inform future work on similar tasks or files.
module Omni.Fact
  ( Fact (..),
    createFact,
    getFact,
    getAllFacts,
    getFactsByProject,
    getFactsByFile,
    updateFact,
    deleteFact,
  )
where

import Alpha
import Data.Aeson (encode)
import qualified Data.ByteString.Lazy.Char8 as BLC
import qualified Data.Text as Text
import Data.Time (getCurrentTime)
import qualified Database.SQLite.Simple as SQL
import Omni.Task.Core
  ( Fact (..),
    getFactsForFile,
    getFactsForProject,
    loadFacts,
    saveFact,
    withDb,
  )
import qualified Omni.Task.Core as TaskCore

-- | Create a new fact and return its ID.
createFact :: Text -> Text -> [Text] -> Maybe Text -> Double -> IO Int
createFact project content relatedFiles sourceTask confidence = do
  now <- getCurrentTime
  let fact =
        Fact
          { factId = Nothing,
            factProject = project,
            factContent = content,
            factRelatedFiles = relatedFiles,
            factSourceTask = sourceTask,
            factConfidence = confidence,
            factCreatedAt = now
          }
  saveFact fact

-- | Get a fact by its ID.
getFact :: Int -> IO (Maybe Fact)
getFact fid = do
  facts <- getAllFacts
  pure <| find (\f -> factId f == Just fid) facts

-- | Get all facts from the database.
getAllFacts :: IO [Fact]
getAllFacts = loadFacts

-- | Get facts for a specific project.
getFactsByProject :: Text -> IO [Fact]
getFactsByProject = getFactsForProject

-- | Get facts related to a specific file.
getFactsByFile :: Text -> IO [Fact]
getFactsByFile = getFactsForFile

-- | Update an existing fact.
updateFact :: Int -> Text -> [Text] -> Double -> IO ()
updateFact fid content relatedFiles confidence =
  withDb <| \conn ->
    SQL.execute
      conn
      "UPDATE facts SET fact = ?, related_files = ?, confidence = ? WHERE id = ?"
      (content, Text.pack (BLC.unpack (encode relatedFiles)), confidence, fid)

-- | Delete a fact by ID.
deleteFact :: Int -> IO ()
deleteFact = TaskCore.deleteFact