diff options
| author | Ben Sima <ben@bensima.com> | 2025-12-01 04:29:51 -0500 |
|---|---|---|
| committer | Ben Sima <ben@bensima.com> | 2025-12-01 04:29:51 -0500 |
| commit | 3945b6fad4f1620612beb259e8601d165b9f4f12 (patch) | |
| tree | 04958c178b59e92022c44567ebef071e13998199 /Omni/Agent/Worker.hs | |
| parent | 7df031715fabd6eb84ef315e8fe78dc7d0b7344d (diff) | |
Fix cost reporting - parse actual cost from OpenRouter API response
I have successfully completed task t-197.8 to fix cost reporting
by pars
**Omni/Agent/Engine.hs:** 1. Added `usageCost :: Maybe Double`
field to the `Usage` type to captur 2. Updated `FromJSON` instance to
parse the optional `"cost"` field 3. Modified `ChatCompletionRequest`
ToJSON instance to include `"usage": 4. Changed cost types from `Int`
to `Double` throughout (engineOnCost ca 5. Updated `estimateCost`
to use floating-point division instead of inte 6. Modified `runAgent`
to use actual cost from API when available, conve 7. Added new test
case for parsing usage with cost field
**Omni/Agent/Worker.hs:** 1. Updated `runWithEngine` signature to
return `Double` for cost 2. Changed `totalCostRef` from `IORef Int`
to `IORef Double` 3. Added rounding when storing cost in DB metrics
to maintain backward c
✅ **All tests pass:** - Omni/Agent/Engine.hs - 16 unit tests pass
- Omni/Agent/Worker.hs - Builds successfully - Omni/Agent.hs - All
integration tests pass - Omni/Jr.hs - All 12 tests pass
✅ **All lint checks pass:** - No hlint issues - No ormolu formatting
issues
The implementation correctly handles OpenRouter's cost format
(credits w
Task-Id: t-197.8
Diffstat (limited to 'Omni/Agent/Worker.hs')
| -rw-r--r-- | Omni/Agent/Worker.hs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/Omni/Agent/Worker.hs b/Omni/Agent/Worker.hs index 79cf3c8..2949ac3 100644 --- a/Omni/Agent/Worker.hs +++ b/Omni/Agent/Worker.hs @@ -98,8 +98,8 @@ processTask worker task = do endTime <- Data.Time.getCurrentTime say ("[worker] Agent exited with: " <> tshow exitCode) - -- Update the activity record with metrics - TaskCore.updateActivityMetrics activityId Nothing (Just endTime) (Just costCents) Nothing + -- Update the activity record with metrics (convert Double to Int by rounding) + TaskCore.updateActivityMetrics activityId Nothing (Just endTime) (Just (round costCents)) Nothing case exitCode of Exit.ExitSuccess -> do @@ -199,7 +199,7 @@ tryCommit repo msg = do -- | Run task using native Engine -- Returns (ExitCode, output text, cost in cents) -runWithEngine :: Core.Worker -> FilePath -> TaskCore.Task -> IO (Exit.ExitCode, Text, Int) +runWithEngine :: Core.Worker -> FilePath -> TaskCore.Task -> IO (Exit.ExitCode, Text, Double) runWithEngine worker repo task = do -- Read API key from environment maybeApiKey <- Env.lookupEnv "OPENROUTER_API_KEY" @@ -253,7 +253,7 @@ runWithEngine worker repo task = do TaskCore.insertAgentEvent tid sessionId eventType contentJson -- Build Engine config with callbacks - totalCostRef <- newIORef (0 :: Int) + totalCostRef <- newIORef (0 :: Double) let quiet = Core.workerQuiet worker sayLog msg = if quiet then putText msg else AgentLog.log msg engineCfg = |
