summaryrefslogtreecommitdiff
path: root/Omni/Fact.hs
diff options
context:
space:
mode:
authorBen Sima <ben@bensima.com>2025-11-27 22:26:02 -0500
committerBen Sima <ben@bensima.com>2025-11-27 22:26:02 -0500
commitecbc8385d590cf8f52d437796ff91d6e55bfd55e (patch)
treead12740de96a6be60e990db7150f670fab2dae2b /Omni/Fact.hs
parentbfe8be3a1a85aa2104bb8e9f2fb2c5147d9fbcaf (diff)
Add Omni/Fact.hs core module with CRUD operations
The Omni/Fact.hs module is complete with CRUD operations: - **createFact**: Create a new fact with project, content, related files - **getFact**: Retrieve a fact by ID - **getAllFacts**: Get all facts from the database - **getFactsByProject**: Get facts filtered by project - **getFactsByFile**: Get facts related to a specific file - **updateFact**: Update an existing fact's content, related files, and - **deleteFact**: Delete a fact by ID The module properly re-exports the `Fact` data type from `Omni.Task.Core Task-Id: t-158.2
Diffstat (limited to 'Omni/Fact.hs')
-rw-r--r--Omni/Fact.hs81
1 files changed, 81 insertions, 0 deletions
diff --git a/Omni/Fact.hs b/Omni/Fact.hs
new file mode 100644
index 0000000..57db7fc
--- /dev/null
+++ b/Omni/Fact.hs
@@ -0,0 +1,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