summaryrefslogtreecommitdiff
path: root/Omni/Agent/Engine.hs
blob: f137ddbce132bec5fce800bd06818eb376aec822 (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
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
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | LLM Agent Engine - Tool protocol and LLM provider abstraction.
--
-- This module provides the core abstractions for building LLM-powered agents:
-- - Tool: Defines tools that agents can use
-- - LLM: OpenAI-compatible chat completions API provider
-- - AgentConfig: Configuration for running agents
--
-- : out omni-agent-engine
-- : dep http-conduit
-- : dep aeson
-- : dep case-insensitive
module Omni.Agent.Engine
  ( Tool (..),
    LLM (..),
    EngineConfig (..),
    AgentConfig (..),
    AgentResult (..),
    Guardrails (..),
    GuardrailResult (..),
    Message (..),
    Role (..),
    ToolCall (..),
    FunctionCall (..),
    ToolResult (..),
    ChatCompletionRequest (..),
    ChatCompletionResponse (..),
    Choice (..),
    Usage (..),
    ToolApi (..),
    encodeToolForApi,
    defaultLLM,
    defaultEngineConfig,
    defaultAgentConfig,
    defaultGuardrails,
    chat,
    runAgent,
    runAgentWithProvider,
    runAgentWithProviderStreaming,
    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.Lazy as BL
import qualified Data.CaseInsensitive as CI
import Data.IORef (newIORef, writeIORef)
import qualified Data.Map.Strict as Map
import qualified Data.Text as Text
import qualified Data.Text.Encoding as TE
import qualified Network.HTTP.Simple as HTTP
import qualified Omni.Agent.Provider as Provider
import qualified Omni.Test as Test

main :: IO ()
main = Test.run test

test :: Test.Tree
test =
  Test.group
    "Omni.Agent.Engine"
    [ Test.unit "Tool JSON roundtrip" <| do
        let tool =
              Tool
                { toolName = "get_weather",
                  toolDescription = "Get weather for a location",
                  toolJsonSchema = Aeson.object ["type" .= ("object" :: Text), "properties" .= Aeson.object []],
                  toolExecute = \_ -> pure (Aeson.String "sunny")
                }
        let encoded = encodeToolForApi tool
        case Aeson.decode (Aeson.encode encoded) of
          Nothing -> Test.assertFailure "Failed to decode tool"
          Just decoded -> toolName tool Test.@=? toolApiName decoded,
      Test.unit "Message JSON roundtrip" <| do
        let msg = Message User "Hello" Nothing Nothing
        case Aeson.decode (Aeson.encode msg) of
          Nothing -> Test.assertFailure "Failed to decode message"
          Just decoded -> msgContent msg Test.@=? msgContent decoded,
      Test.unit "defaultLLM has correct endpoint" <| do
        llmBaseUrl defaultLLM Test.@=? "https://openrouter.ai/api/v1",
      Test.unit "defaultLLM has OpenRouter headers" <| do
        length (llmExtraHeaders defaultLLM) Test.@=? 2
        llmModel defaultLLM Test.@=? "anthropic/claude-sonnet-4.5",
      Test.unit "defaultAgentConfig has sensible defaults" <| do
        agentMaxIterations defaultAgentConfig Test.@=? 10,
      Test.unit "defaultEngineConfig has no-op callbacks" <| do
        engineOnCost defaultEngineConfig 100 5
        engineOnActivity defaultEngineConfig "test"
        engineOnToolCall defaultEngineConfig "tool" "result"
        True Test.@=? True,
      Test.unit "buildToolMap creates correct map" <| do
        let tool1 =
              Tool
                { toolName = "tool1",
                  toolDescription = "First tool",
                  toolJsonSchema = Aeson.object [],
                  toolExecute = \_ -> pure Aeson.Null
                }
            tool2 =
              Tool
                { toolName = "tool2",
                  toolDescription = "Second tool",
                  toolJsonSchema = Aeson.object [],
                  toolExecute = \_ -> pure Aeson.Null
                }
            toolMap = buildToolMap [tool1, tool2]
        Map.size toolMap Test.@=? 2
        Map.member "tool1" toolMap Test.@=? True
        Map.member "tool2" toolMap Test.@=? True,
      Test.unit "Usage JSON parsing" <| do
        let json = "{\"prompt_tokens\":100,\"completion_tokens\":50,\"total_tokens\":150}"
        case Aeson.decode json of
          Nothing -> Test.assertFailure "Failed to decode usage"
          Just usage -> do
            usagePromptTokens usage Test.@=? 100
            usageCompletionTokens usage Test.@=? 50
            usageTotalTokens usage Test.@=? 150
            usageCost usage Test.@=? Nothing,
      Test.unit "Usage JSON parsing with cost" <| do
        let json = "{\"prompt_tokens\":194,\"completion_tokens\":2,\"total_tokens\":196,\"cost\":0.95}"
        case Aeson.decode json of
          Nothing -> Test.assertFailure "Failed to decode usage with cost"
          Just usage -> do
            usagePromptTokens usage Test.@=? 194
            usageCompletionTokens usage Test.@=? 2
            usageTotalTokens usage Test.@=? 196
            usageCost usage Test.@=? Just 0.95,
      Test.unit "AgentResult JSON roundtrip" <| do
        let result =
              AgentResult
                { resultFinalMessage = "Done",
                  resultToolCallCount = 3,
                  resultIterations = 2,
                  resultTotalCost = 50,
                  resultTotalTokens = 1500
                }
        case Aeson.decode (Aeson.encode result) of
          Nothing -> Test.assertFailure "Failed to decode AgentResult"
          Just decoded -> do
            resultFinalMessage decoded Test.@=? "Done"
            resultToolCallCount decoded Test.@=? 3
            resultIterations decoded Test.@=? 2,
      Test.unit "estimateCost calculates correctly" <| do
        let gpt4oCost = estimateCost "gpt-4o" 1000
            gpt4oMiniCost = estimateCost "gpt-4o-mini" 1000
        (gpt4oCost >= gpt4oMiniCost) Test.@=? True
        (gpt4oCost > 0) Test.@=? True,
      Test.unit "ToolCall JSON roundtrip" <| do
        let tc =
              ToolCall
                { tcId = "call_123",
                  tcType = "function",
                  tcFunction = FunctionCall "read_file" "{\"path\":\"/tmp/test\"}"
                }
        case Aeson.decode (Aeson.encode tc) of
          Nothing -> Test.assertFailure "Failed to decode ToolCall"
          Just decoded -> tcId decoded Test.@=? "call_123",
      Test.unit "FunctionCall JSON roundtrip" <| do
        let fc = FunctionCall "test_func" "{\"arg\":\"value\"}"
        case Aeson.decode (Aeson.encode fc) of
          Nothing -> Test.assertFailure "Failed to decode FunctionCall"
          Just decoded -> do
            fcName decoded Test.@=? "test_func"
            fcArguments decoded Test.@=? "{\"arg\":\"value\"}",
      Test.unit "Role JSON roundtrip for all roles" <| do
        let roles = [System, User, Assistant, ToolRole]
        forM_ roles <| \role ->
          case Aeson.decode (Aeson.encode role) of
            Nothing -> Test.assertFailure ("Failed to decode Role: " <> show role)
            Just decoded -> decoded Test.@=? role,
      Test.unit "defaultGuardrails has sensible defaults" <| do
        guardrailMaxCostCents defaultGuardrails Test.@=? 100.0
        guardrailMaxTokens defaultGuardrails Test.@=? 500000
        guardrailMaxDuplicateToolCalls defaultGuardrails Test.@=? 3
        guardrailMaxTestFailures defaultGuardrails Test.@=? 3,
      Test.unit "checkCostGuardrail detects exceeded budget" <| do
        let g = defaultGuardrails {guardrailMaxCostCents = 50.0}
        checkCostGuardrail g 60.0 Test.@=? GuardrailCostExceeded 60.0 50.0
        checkCostGuardrail g 40.0 Test.@=? GuardrailOk,
      Test.unit "checkTokenGuardrail detects exceeded budget" <| do
        let g = defaultGuardrails {guardrailMaxTokens = 1000}
        checkTokenGuardrail g 1500 Test.@=? GuardrailTokensExceeded 1500 1000
        checkTokenGuardrail g 500 Test.@=? GuardrailOk,
      Test.unit "checkDuplicateGuardrail detects repeated calls" <| do
        let g = defaultGuardrails {guardrailMaxDuplicateToolCalls = 3}
            counts = Map.fromList [("bash", 3), ("read_file", 1)]
        case checkDuplicateGuardrail g counts of
          GuardrailDuplicateToolCalls name count -> do
            name Test.@=? "bash"
            count Test.@=? 3
          _ -> Test.assertFailure "Expected GuardrailDuplicateToolCalls"
        checkDuplicateGuardrail g (Map.fromList [("bash", 2)]) Test.@=? GuardrailOk,
      Test.unit "checkTestFailureGuardrail detects failures" <| do
        let g = defaultGuardrails {guardrailMaxTestFailures = 3}
        checkTestFailureGuardrail g 3 Test.@=? GuardrailTestFailures 3
        checkTestFailureGuardrail g 2 Test.@=? GuardrailOk,
      Test.unit "updateToolCallCounts accumulates correctly" <| do
        let tc1 = ToolCall "1" "function" (FunctionCall "bash" "{}")
            tc2 = ToolCall "2" "function" (FunctionCall "bash" "{}")
            tc3 = ToolCall "3" "function" (FunctionCall "read_file" "{}")
            counts = updateToolCallCounts Map.empty [tc1, tc2, tc3]
        Map.lookup "bash" counts Test.@=? Just 2
        Map.lookup "read_file" counts Test.@=? Just 1,
      Test.unit "Guardrails JSON roundtrip" <| do
        let g = Guardrails 75.0 100000 5 4 3
        case Aeson.decode (Aeson.encode g) of
          Nothing -> Test.assertFailure "Failed to decode Guardrails"
          Just decoded -> decoded Test.@=? g,
      Test.unit "GuardrailResult JSON roundtrip" <| do
        let results =
              [ GuardrailOk,
                GuardrailCostExceeded 100.0 50.0,
                GuardrailTokensExceeded 2000 1000,
                GuardrailDuplicateToolCalls "bash" 5,
                GuardrailTestFailures 3,
                GuardrailEditFailures 5
              ]
        forM_ results <| \r ->
          case Aeson.decode (Aeson.encode r) of
            Nothing -> Test.assertFailure ("Failed to decode GuardrailResult: " <> show r)
            Just decoded -> decoded Test.@=? r
    ]

data Tool = Tool
  { toolName :: Text,
    toolDescription :: Text,
    toolJsonSchema :: Aeson.Value,
    toolExecute :: Aeson.Value -> IO Aeson.Value
  }

data ToolApi = ToolApi
  { toolApiName :: Text,
    toolApiDescription :: Text,
    toolApiParameters :: Aeson.Value
  }
  deriving (Generic)

instance Aeson.ToJSON ToolApi where
  toJSON t =
    Aeson.object
      [ "type" .= ("function" :: Text),
        "function"
          .= Aeson.object
            [ "name" .= toolApiName t,
              "description" .= toolApiDescription t,
              "parameters" .= toolApiParameters t
            ]
      ]

instance Aeson.FromJSON ToolApi where
  parseJSON =
    Aeson.withObject "ToolApi" <| \v -> do
      fn <- v .: "function"
      (ToolApi </ (fn .: "name"))
        <*> (fn .: "description")
        <*> (fn .: "parameters")

encodeToolForApi :: Tool -> ToolApi
encodeToolForApi t =
  ToolApi
    { toolApiName = toolName t,
      toolApiDescription = toolDescription t,
      toolApiParameters = toolJsonSchema t
    }

encodeToolForProvider :: Tool -> Provider.ToolApi
encodeToolForProvider t =
  Provider.ToolApi
    { Provider.toolApiName = toolName t,
      Provider.toolApiDescription = toolDescription t,
      Provider.toolApiParameters = toolJsonSchema t
    }

data LLM = LLM
  { llmBaseUrl :: Text,
    llmApiKey :: Text,
    llmModel :: Text,
    llmExtraHeaders :: [(ByteString, ByteString)]
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON LLM where
  toJSON l =
    Aeson.object
      [ "llmBaseUrl" .= llmBaseUrl l,
        "llmApiKey" .= llmApiKey l,
        "llmModel" .= llmModel l
      ]

instance Aeson.FromJSON LLM where
  parseJSON =
    Aeson.withObject "LLM" <| \v ->
      (LLM </ (v .: "llmBaseUrl"))
        <*> (v .: "llmApiKey")
        <*> (v .: "llmModel")
        <*> pure []

defaultLLM :: LLM
defaultLLM =
  LLM
    { llmBaseUrl = "https://openrouter.ai/api/v1",
      llmApiKey = "",
      llmModel = "anthropic/claude-sonnet-4.5",
      llmExtraHeaders =
        [ ("HTTP-Referer", "https://omni.dev"),
          ("X-Title", "Omni Agent")
        ]
    }

data AgentConfig = AgentConfig
  { agentModel :: Text,
    agentTools :: [Tool],
    agentSystemPrompt :: Text,
    agentMaxIterations :: Int,
    agentGuardrails :: Guardrails
  }

data Guardrails = Guardrails
  { guardrailMaxCostCents :: Double,
    guardrailMaxTokens :: Int,
    guardrailMaxDuplicateToolCalls :: Int,
    guardrailMaxTestFailures :: Int,
    guardrailMaxEditFailures :: Int
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON Guardrails

instance Aeson.FromJSON Guardrails

data GuardrailResult
  = GuardrailOk
  | GuardrailCostExceeded Double Double
  | GuardrailTokensExceeded Int Int
  | GuardrailDuplicateToolCalls Text Int
  | GuardrailTestFailures Int
  | GuardrailEditFailures Int
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON GuardrailResult

instance Aeson.FromJSON GuardrailResult

defaultGuardrails :: Guardrails
defaultGuardrails =
  Guardrails
    { guardrailMaxCostCents = 100.0,
      guardrailMaxTokens = 500000,
      guardrailMaxDuplicateToolCalls = 3,
      guardrailMaxTestFailures = 3,
      guardrailMaxEditFailures = 5
    }

defaultAgentConfig :: AgentConfig
defaultAgentConfig =
  AgentConfig
    { agentModel = "gpt-4",
      agentTools = [],
      agentSystemPrompt = "You are a helpful assistant.",
      agentMaxIterations = 10,
      agentGuardrails = defaultGuardrails
    }

data EngineConfig = EngineConfig
  { engineLLM :: LLM,
    engineOnCost :: Int -> Double -> IO (),
    engineOnActivity :: Text -> IO (),
    engineOnToolCall :: Text -> Text -> IO (),
    engineOnAssistant :: Text -> IO (),
    engineOnToolResult :: Text -> Bool -> Text -> IO (),
    engineOnComplete :: IO (),
    engineOnError :: Text -> IO (),
    engineOnGuardrail :: GuardrailResult -> IO ()
  }

defaultEngineConfig :: EngineConfig
defaultEngineConfig =
  EngineConfig
    { engineLLM = defaultLLM,
      engineOnCost = \_ _ -> pure (),
      engineOnActivity = \_ -> pure (),
      engineOnToolCall = \_ _ -> pure (),
      engineOnAssistant = \_ -> pure (),
      engineOnToolResult = \_ _ _ -> pure (),
      engineOnComplete = pure (),
      engineOnError = \_ -> pure (),
      engineOnGuardrail = \_ -> pure ()
    }

data AgentResult = AgentResult
  { resultFinalMessage :: Text,
    resultToolCallCount :: Int,
    resultIterations :: Int,
    resultTotalCost :: Double,
    resultTotalTokens :: Int
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON AgentResult

instance Aeson.FromJSON AgentResult

data Role = System | User | Assistant | ToolRole
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON Role where
  toJSON System = Aeson.String "system"
  toJSON User = Aeson.String "user"
  toJSON Assistant = Aeson.String "assistant"
  toJSON ToolRole = Aeson.String "tool"

instance Aeson.FromJSON Role where
  parseJSON = Aeson.withText "Role" parseRole
    where
      parseRole "system" = pure System
      parseRole "user" = pure User
      parseRole "assistant" = pure Assistant
      parseRole "tool" = pure ToolRole
      parseRole _ = empty

data Message = Message
  { msgRole :: Role,
    msgContent :: Text,
    msgToolCalls :: Maybe [ToolCall],
    msgToolCallId :: Maybe Text
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON Message where
  toJSON m =
    Aeson.object
      <| catMaybes
        [ Just ("role" .= msgRole m),
          Just ("content" .= msgContent m),
          ("tool_calls" .=) </ msgToolCalls m,
          ("tool_call_id" .=) </ msgToolCallId m
        ]

instance Aeson.FromJSON Message where
  parseJSON =
    Aeson.withObject "Message" <| \v ->
      (Message </ (v .: "role"))
        <*> (v .:? "content" .!= "")
        <*> (v .:? "tool_calls")
        <*> (v .:? "tool_call_id")

data ToolCall = ToolCall
  { tcId :: Text,
    tcType :: Text,
    tcFunction :: FunctionCall
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON ToolCall where
  toJSON tc =
    Aeson.object
      [ "id" .= tcId tc,
        "type" .= tcType tc,
        "function" .= tcFunction tc
      ]

instance Aeson.FromJSON ToolCall where
  parseJSON =
    Aeson.withObject "ToolCall" <| \v ->
      (ToolCall </ (v .: "id"))
        <*> (v .:? "type" .!= "function")
        <*> (v .: "function")

data FunctionCall = FunctionCall
  { fcName :: Text,
    fcArguments :: Text
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON FunctionCall where
  toJSON fc =
    Aeson.object
      [ "name" .= fcName fc,
        "arguments" .= fcArguments fc
      ]

instance Aeson.FromJSON FunctionCall where
  parseJSON =
    Aeson.withObject "FunctionCall" <| \v ->
      (FunctionCall </ (v .: "name"))
        <*> (v .: "arguments")

data ToolResult = ToolResult
  { trToolCallId :: Text,
    trContent :: Text
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON ToolResult

instance Aeson.FromJSON ToolResult

data ChatCompletionRequest = ChatCompletionRequest
  { reqModel :: Text,
    reqMessages :: [Message],
    reqTools :: Maybe [ToolApi]
  }
  deriving (Generic)

instance Aeson.ToJSON ChatCompletionRequest where
  toJSON r =
    Aeson.object
      <| catMaybes
        [ Just ("model" .= reqModel r),
          Just ("messages" .= reqMessages r),
          ("tools" .=) </ reqTools r,
          Just ("usage" .= Aeson.object ["include" .= True])
        ]

data Choice = Choice
  { choiceIndex :: Int,
    choiceMessage :: Message,
    choiceFinishReason :: Maybe Text
  }
  deriving (Show, Eq, Generic)

instance Aeson.FromJSON Choice where
  parseJSON =
    Aeson.withObject "Choice" <| \v ->
      (Choice </ (v .: "index"))
        <*> (v .: "message")
        <*> (v .:? "finish_reason")

data Usage = Usage
  { usagePromptTokens :: Int,
    usageCompletionTokens :: Int,
    usageTotalTokens :: Int,
    usageCost :: Maybe Double
  }
  deriving (Show, Eq, Generic)

instance Aeson.FromJSON Usage where
  parseJSON =
    Aeson.withObject "Usage" <| \v ->
      (Usage </ (v .: "prompt_tokens"))
        <*> (v .: "completion_tokens")
        <*> (v .: "total_tokens")
        <*> (v .:? "cost")

data ChatCompletionResponse = ChatCompletionResponse
  { respId :: Text,
    respChoices :: [Choice],
    respModel :: Text,
    respUsage :: Maybe Usage
  }
  deriving (Show, Eq, Generic)

instance Aeson.FromJSON ChatCompletionResponse where
  parseJSON =
    Aeson.withObject "ChatCompletionResponse" <| \v ->
      (ChatCompletionResponse </ (v .: "id"))
        <*> (v .: "choices")
        <*> (v .: "model")
        <*> (v .:? "usage")

data ChatResult = ChatResult
  { chatMessage :: Message,
    chatUsage :: Maybe Usage
  }
  deriving (Show, Eq)

chatWithUsage :: LLM -> [Tool] -> [Message] -> IO (Either Text ChatResult)
chatWithUsage llm tools messages = do
  let url = Text.unpack (llmBaseUrl llm) <> "/chat/completions"
  req0 <- HTTP.parseRequest url
  let toolApis = [encodeToolForApi t | not (null tools), t <- tools]
      body =
        ChatCompletionRequest
          { reqModel = llmModel llm,
            reqMessages = messages,
            reqTools = if null toolApis then Nothing else Just toolApis
          }
      baseReq =
        HTTP.setRequestMethod "POST"
          <| HTTP.setRequestHeader "Content-Type" ["application/json"]
          <| HTTP.setRequestHeader "Authorization" ["Bearer " <> TE.encodeUtf8 (llmApiKey llm)]
          <| HTTP.setRequestBodyLBS (Aeson.encode body)
          <| req0
      req = foldr addHeader baseReq (llmExtraHeaders llm)
      addHeader (name, value) = HTTP.addRequestHeader (CI.mk name) value

  response <- HTTP.httpLBS req
  let status = HTTP.getResponseStatusCode response
  if status >= 200 && status < 300
    then case Aeson.decode (HTTP.getResponseBody response) of
      Just resp ->
        case respChoices resp of
          (c : _) -> pure (Right (ChatResult (choiceMessage c) (respUsage resp)))
          [] -> pure (Left "No choices in response")
      Nothing -> pure (Left "Failed to parse response")
    else pure (Left ("HTTP error: " <> tshow status <> " - " <> TE.decodeUtf8 (BL.toStrict (HTTP.getResponseBody response))))

chat :: LLM -> [Tool] -> [Message] -> IO (Either Text Message)
chat llm tools messages = do
  result <- chatWithUsage llm tools messages
  pure (chatMessage </ result)

runAgent :: EngineConfig -> AgentConfig -> Text -> IO (Either Text AgentResult)
runAgent engineCfg agentCfg userPrompt = do
  let llm =
        (engineLLM engineCfg)
          { llmModel = agentModel agentCfg
          }
      tools = agentTools agentCfg
      toolMap = buildToolMap tools
      systemMsg = Message System (agentSystemPrompt agentCfg) Nothing Nothing
      userMsg = Message User userPrompt Nothing Nothing
      initialMessages = [systemMsg, userMsg]

  engineOnActivity engineCfg "Starting agent loop"
  loop llm tools toolMap initialMessages 0 0 0 0.0 Map.empty 0 0
  where
    maxIter = agentMaxIterations agentCfg
    guardrails' = agentGuardrails agentCfg

    loop ::
      LLM ->
      [Tool] ->
      Map.Map Text Tool ->
      [Message] ->
      Int ->
      Int ->
      Int ->
      Double ->
      Map.Map Text Int ->
      Int ->
      Int ->
      IO (Either Text AgentResult)
    loop llm tools' toolMap msgs iteration totalCalls totalTokens totalCost toolCallCounts testFailures editFailures
      | iteration >= maxIter = do
          let errMsg = "Max iterations (" <> tshow maxIter <> ") reached"
          engineOnError engineCfg errMsg
          pure <| Left errMsg
      | otherwise = do
          let guardrailViolation = findGuardrailViolation guardrails' totalCost totalTokens toolCallCounts testFailures editFailures
          case guardrailViolation of
            Just (g, errMsg) -> do
              engineOnGuardrail engineCfg g
              pure <| Left errMsg
            Nothing -> do
              engineOnActivity engineCfg <| "Iteration " <> tshow (iteration + 1)
              result <- chatWithUsage llm tools' msgs
              case result of
                Left err -> do
                  engineOnError engineCfg err
                  pure (Left err)
                Right chatRes -> do
                  let msg = chatMessage chatRes
                      tokens = maybe 0 usageTotalTokens (chatUsage chatRes)
                      cost = case chatUsage chatRes +> usageCost of
                        Just actualCost -> actualCost * 100
                        Nothing -> estimateCost (llmModel llm) tokens
                  engineOnCost engineCfg tokens cost
                  let newTokens = totalTokens + tokens
                      newCost = totalCost + cost
                  let assistantText = msgContent msg
                  unless (Text.null assistantText)
                    <| engineOnAssistant engineCfg assistantText
                  case msgToolCalls msg of
                    Nothing
                      | Text.null (msgContent msg) && totalCalls > 0 -> do
                          engineOnActivity engineCfg "Empty response after tools, prompting for text"
                          let promptMsg = Message ToolRole "Please provide a response to the user." Nothing Nothing
                              newMsgs = msgs <> [msg, promptMsg]
                          loop llm tools' toolMap newMsgs (iteration + 1) totalCalls newTokens newCost toolCallCounts testFailures editFailures
                      | otherwise -> do
                          engineOnActivity engineCfg "Agent completed"
                          engineOnComplete engineCfg
                          pure
                            <| Right
                            <| AgentResult
                              { resultFinalMessage = msgContent msg,
                                resultToolCallCount = totalCalls,
                                resultIterations = iteration + 1,
                                resultTotalCost = newCost,
                                resultTotalTokens = newTokens
                              }
                    Just [] -> do
                      engineOnActivity engineCfg "Agent completed (empty tool calls)"
                      engineOnComplete engineCfg
                      pure
                        <| Right
                        <| AgentResult
                          { resultFinalMessage = msgContent msg,
                            resultToolCallCount = totalCalls,
                            resultIterations = iteration + 1,
                            resultTotalCost = newCost,
                            resultTotalTokens = newTokens
                          }
                    Just tcs -> do
                      (toolResults, newTestFailures, newEditFailures) <- executeToolCallsWithTracking engineCfg toolMap tcs testFailures editFailures
                      let newMsgs = msgs <> [msg] <> toolResults
                          newCalls = totalCalls + length tcs
                          newToolCallCounts = updateToolCallCounts toolCallCounts tcs
                      loop llm tools' toolMap newMsgs (iteration + 1) newCalls newTokens newCost newToolCallCounts newTestFailures newEditFailures

checkCostGuardrail :: Guardrails -> Double -> GuardrailResult
checkCostGuardrail g cost
  | cost > guardrailMaxCostCents g = GuardrailCostExceeded cost (guardrailMaxCostCents g)
  | otherwise = GuardrailOk

checkTokenGuardrail :: Guardrails -> Int -> GuardrailResult
checkTokenGuardrail g tokens
  | tokens > guardrailMaxTokens g = GuardrailTokensExceeded tokens (guardrailMaxTokens g)
  | otherwise = GuardrailOk

checkDuplicateGuardrail :: Guardrails -> Map.Map Text Int -> GuardrailResult
checkDuplicateGuardrail g counts =
  let maxAllowed = guardrailMaxDuplicateToolCalls g
      violations = [(name, count) | (name, count) <- Map.toList counts, count >= maxAllowed]
   in case violations of
        ((name, count) : _) -> GuardrailDuplicateToolCalls name count
        [] -> GuardrailOk

checkTestFailureGuardrail :: Guardrails -> Int -> GuardrailResult
checkTestFailureGuardrail g failures
  | failures >= guardrailMaxTestFailures g = GuardrailTestFailures failures
  | otherwise = GuardrailOk

checkEditFailureGuardrail :: Guardrails -> Int -> GuardrailResult
checkEditFailureGuardrail g failures
  | failures >= guardrailMaxEditFailures g = GuardrailEditFailures failures
  | otherwise = GuardrailOk

updateToolCallCounts :: Map.Map Text Int -> [ToolCall] -> Map.Map Text Int
updateToolCallCounts =
  foldr (\tc m -> Map.insertWith (+) (fcName (tcFunction tc)) 1 m)

findGuardrailViolation :: Guardrails -> Double -> Int -> Map.Map Text Int -> Int -> Int -> Maybe (GuardrailResult, Text)
findGuardrailViolation g cost tokens toolCallCounts testFailures editFailures =
  case checkCostGuardrail g cost of
    r@(GuardrailCostExceeded actual limit) ->
      Just (r, "Guardrail: cost budget exceeded (" <> tshow actual <> "/" <> tshow limit <> " cents)")
    _ -> case checkTokenGuardrail g tokens of
      r@(GuardrailTokensExceeded actual limit) ->
        Just (r, "Guardrail: token budget exceeded (" <> tshow actual <> "/" <> tshow limit <> " tokens)")
      _ -> case checkDuplicateGuardrail g toolCallCounts of
        r@(GuardrailDuplicateToolCalls tool count) ->
          Just (r, "Guardrail: duplicate tool calls (" <> tool <> " called " <> tshow count <> " times)")
        _ -> case checkTestFailureGuardrail g testFailures of
          r@(GuardrailTestFailures count) ->
            Just (r, "Guardrail: too many test failures (" <> tshow count <> ")")
          _ -> case checkEditFailureGuardrail g editFailures of
            r@(GuardrailEditFailures count) ->
              Just (r, "Guardrail: too many edit_file failures (" <> tshow count <> " 'old_str not found' errors)")
            _ -> Nothing

buildToolMap :: [Tool] -> Map.Map Text Tool
buildToolMap = Map.fromList <. map (\t -> (toolName t, t))

-- | Track both test failures and edit failures
-- Returns (messages, testFailures, editFailures)
executeToolCallsWithTracking :: EngineConfig -> Map.Map Text Tool -> [ToolCall] -> Int -> Int -> IO ([Message], Int, Int)
executeToolCallsWithTracking engineCfg toolMap tcs initialTestFailures initialEditFailures = do
  results <- traverse executeSingle tcs
  let msgs = map (\(m, _, _) -> m) results
      testDeltas = map (\(_, t, _) -> t) results
      editDeltas = map (\(_, _, e) -> e) results
      totalTestFailures = initialTestFailures + sum testDeltas
      totalEditFailures = initialEditFailures + sum editDeltas
  pure (msgs, totalTestFailures, totalEditFailures)
  where
    executeSingle tc = do
      let name = fcName (tcFunction tc)
          argsText = fcArguments (tcFunction tc)
          callId = tcId tc
      engineOnActivity engineCfg <| "Executing tool: " <> name
      engineOnToolCall engineCfg name argsText
      case Map.lookup name toolMap of
        Nothing -> do
          let errMsg = "Tool not found: " <> name
          engineOnToolResult engineCfg name False errMsg
          pure (Message ToolRole errMsg Nothing (Just callId), 0, 0)
        Just tool -> do
          case Aeson.decode (BL.fromStrict (TE.encodeUtf8 argsText)) of
            Nothing -> do
              let errMsg = "Invalid JSON arguments: " <> argsText
              engineOnToolResult engineCfg name False errMsg
              pure (Message ToolRole errMsg Nothing (Just callId), 0, 0)
            Just args -> do
              resultValue <- toolExecute tool args
              let resultText = TE.decodeUtf8 (BL.toStrict (Aeson.encode resultValue))
                  isTestCall = name == "bash" && ("bild --test" `Text.isInfixOf` argsText || "bild -t" `Text.isInfixOf` argsText)
                  isTestFailure = isTestCall && isFailureResult resultValue
                  testDelta = if isTestFailure then 1 else 0
                  isEditFailure = name == "edit_file" && isOldStrNotFoundError resultValue
                  editDelta = if isEditFailure then 1 else 0
              engineOnToolResult engineCfg name True resultText
              pure (Message ToolRole resultText Nothing (Just callId), testDelta, editDelta)

    isFailureResult :: Aeson.Value -> Bool
    isFailureResult (Aeson.Object obj) =
      case KeyMap.lookup "exit_code" obj of
        Just (Aeson.Number n) -> n /= 0
        _ -> False
    isFailureResult (Aeson.String s) =
      "error"
        `Text.isInfixOf` Text.toLower s
        || "failed"
        `Text.isInfixOf` Text.toLower s
        || "FAILED"
        `Text.isInfixOf` s
    isFailureResult _ = False

    isOldStrNotFoundError :: Aeson.Value -> Bool
    isOldStrNotFoundError (Aeson.Object obj) =
      case KeyMap.lookup "error" obj of
        Just (Aeson.String s) -> "old_str not found" `Text.isInfixOf` s
        _ -> False
    isOldStrNotFoundError _ = False

-- | Estimate cost in cents from token count.
-- Uses blended input/output rates (roughly 2:1 output:input ratio).
-- Prices as of Dec 2024 from OpenRouter.
estimateCost :: Text -> Int -> Double
estimateCost model tokens
  | "gpt-4o-mini" `Text.isInfixOf` model = fromIntegral tokens * 0.04 / 1000
  | "gpt-4o" `Text.isInfixOf` model = fromIntegral tokens * 0.7 / 1000
  | "gemini-2.0-flash" `Text.isInfixOf` model = fromIntegral tokens * 0.15 / 1000
  | "gemini-2.5-flash" `Text.isInfixOf` model = fromIntegral tokens * 0.15 / 1000
  | "claude-sonnet-4.5" `Text.isInfixOf` model = fromIntegral tokens * 0.9 / 1000
  | "claude-sonnet-4" `Text.isInfixOf` model = fromIntegral tokens * 0.9 / 1000
  | "claude-3-haiku" `Text.isInfixOf` model = fromIntegral tokens * 0.1 / 1000
  | "claude" `Text.isInfixOf` model = fromIntegral tokens * 0.9 / 1000
  | otherwise = fromIntegral tokens * 0.5 / 1000

-- | Run agent with a Provider instead of LLM.
-- This is the new preferred way to run agents with multiple backend support.
runAgentWithProvider :: EngineConfig -> Provider.Provider -> AgentConfig -> Text -> IO (Either Text AgentResult)
runAgentWithProvider engineCfg provider agentCfg userPrompt = do
  let tools = agentTools agentCfg
      toolApis = map encodeToolForProvider tools
      toolMap = buildToolMap tools
      systemMsg = providerMessage Provider.System (agentSystemPrompt agentCfg)
      userMsg = providerMessage Provider.User userPrompt
      initialMessages = [systemMsg, userMsg]

  engineOnActivity engineCfg "Starting agent loop (Provider)"
  loopProvider provider toolApis toolMap initialMessages 0 0 0 0.0 Map.empty 0 0
  where
    maxIter = agentMaxIterations agentCfg
    guardrails' = agentGuardrails agentCfg

    providerMessage :: Provider.Role -> Text -> Provider.Message
    providerMessage role content = Provider.Message role content Nothing Nothing

    loopProvider ::
      Provider.Provider ->
      [Provider.ToolApi] ->
      Map.Map Text Tool ->
      [Provider.Message] ->
      Int ->
      Int ->
      Int ->
      Double ->
      Map.Map Text Int ->
      Int ->
      Int ->
      IO (Either Text AgentResult)
    loopProvider prov toolApis' toolMap msgs iteration totalCalls totalTokens totalCost toolCallCounts testFailures editFailures
      | iteration >= maxIter = do
          let errMsg = "Max iterations (" <> tshow maxIter <> ") reached"
          engineOnError engineCfg errMsg
          pure <| Left errMsg
      | otherwise = do
          let guardrailViolation = findGuardrailViolation guardrails' totalCost totalTokens toolCallCounts testFailures editFailures
          case guardrailViolation of
            Just (g, errMsg) -> do
              engineOnGuardrail engineCfg g
              pure <| Left errMsg
            Nothing -> do
              engineOnActivity engineCfg <| "Iteration " <> tshow (iteration + 1)
              result <- Provider.chatWithUsage prov toolApis' msgs
              case result of
                Left err -> do
                  engineOnError engineCfg err
                  pure (Left err)
                Right chatRes -> do
                  let msg = Provider.chatMessage chatRes
                      tokens = maybe 0 Provider.usageTotalTokens (Provider.chatUsage chatRes)
                      cost = case Provider.chatUsage chatRes +> Provider.usageCost of
                        Just actualCost -> actualCost * 100
                        Nothing -> estimateCost (getProviderModel prov) tokens
                  engineOnCost engineCfg tokens cost
                  let newTokens = totalTokens + tokens
                      newCost = totalCost + cost
                  let assistantText = Provider.msgContent msg
                  unless (Text.null assistantText)
                    <| engineOnAssistant engineCfg assistantText
                  case Provider.msgToolCalls msg of
                    Nothing
                      | Text.null (Provider.msgContent msg) && totalCalls > 0 -> do
                          engineOnActivity engineCfg "Empty response after tools, prompting for text"
                          let promptMsg = Provider.Message Provider.ToolRole "Please provide a response to the user." Nothing Nothing
                              newMsgs = msgs <> [msg, promptMsg]
                          loopProvider prov toolApis' toolMap newMsgs (iteration + 1) totalCalls newTokens newCost toolCallCounts testFailures editFailures
                      | otherwise -> do
                          engineOnActivity engineCfg "Agent completed"
                          engineOnComplete engineCfg
                          pure
                            <| Right
                            <| AgentResult
                              { resultFinalMessage = Provider.msgContent msg,
                                resultToolCallCount = totalCalls,
                                resultIterations = iteration + 1,
                                resultTotalCost = newCost,
                                resultTotalTokens = newTokens
                              }
                    Just [] -> do
                      engineOnActivity engineCfg "Agent completed (empty tool calls)"
                      engineOnComplete engineCfg
                      pure
                        <| Right
                        <| AgentResult
                          { resultFinalMessage = Provider.msgContent msg,
                            resultToolCallCount = totalCalls,
                            resultIterations = iteration + 1,
                            resultTotalCost = newCost,
                            resultTotalTokens = newTokens
                          }
                    Just tcs -> do
                      (toolResults, newTestFailures, newEditFailures) <- executeProviderToolCalls engineCfg toolMap tcs testFailures editFailures
                      let newMsgs = msgs <> [msg] <> toolResults
                          newCalls = totalCalls + length tcs
                          newToolCallCounts = updateProviderToolCallCounts toolCallCounts tcs
                      loopProvider prov toolApis' toolMap newMsgs (iteration + 1) newCalls newTokens newCost newToolCallCounts newTestFailures newEditFailures

    getProviderModel :: Provider.Provider -> Text
    getProviderModel (Provider.OpenRouter cfg) = Provider.providerModel cfg
    getProviderModel (Provider.Ollama cfg) = Provider.providerModel cfg
    getProviderModel (Provider.AmpCLI _) = "amp"

    updateProviderToolCallCounts :: Map.Map Text Int -> [Provider.ToolCall] -> Map.Map Text Int
    updateProviderToolCallCounts =
      foldr (\tc m -> Map.insertWith (+) (Provider.fcName (Provider.tcFunction tc)) 1 m)

    executeProviderToolCalls :: EngineConfig -> Map.Map Text Tool -> [Provider.ToolCall] -> Int -> Int -> IO ([Provider.Message], Int, Int)
    executeProviderToolCalls eCfg tMap tcs initialTestFailures initialEditFailures = do
      results <- traverse (executeSingleProvider eCfg tMap) tcs
      let msgs = map (\(m, _, _) -> m) results
          testDeltas = map (\(_, t, _) -> t) results
          editDeltas = map (\(_, _, e) -> e) results
          totalTestFail = initialTestFailures + sum testDeltas
          totalEditFail = initialEditFailures + sum editDeltas
      pure (msgs, totalTestFail, totalEditFail)

    executeSingleProvider :: EngineConfig -> Map.Map Text Tool -> Provider.ToolCall -> IO (Provider.Message, Int, Int)
    executeSingleProvider eCfg tMap tc = do
      let name = Provider.fcName (Provider.tcFunction tc)
          argsText = Provider.fcArguments (Provider.tcFunction tc)
          callId = Provider.tcId tc
      engineOnActivity eCfg <| "Executing tool: " <> name
      engineOnToolCall eCfg name argsText
      case Map.lookup name tMap of
        Nothing -> do
          let errMsg = "Tool not found: " <> name
          engineOnToolResult eCfg name False errMsg
          pure (Provider.Message Provider.ToolRole errMsg Nothing (Just callId), 0, 0)
        Just tool -> do
          case Aeson.decode (BL.fromStrict (TE.encodeUtf8 argsText)) of
            Nothing -> do
              let errMsg = "Invalid JSON arguments: " <> argsText
              engineOnToolResult eCfg name False errMsg
              pure (Provider.Message Provider.ToolRole errMsg Nothing (Just callId), 0, 0)
            Just args -> do
              resultValue <- toolExecute tool args
              let resultText = TE.decodeUtf8 (BL.toStrict (Aeson.encode resultValue))
                  isTestCall = name == "bash" && ("bild --test" `Text.isInfixOf` argsText || "bild -t" `Text.isInfixOf` argsText)
                  isTestFailure = isTestCall && isFailureResultProvider resultValue
                  testDelta = if isTestFailure then 1 else 0
                  isEditFailure = name == "edit_file" && isOldStrNotFoundProvider resultValue
                  editDelta = if isEditFailure then 1 else 0
              engineOnToolResult eCfg name True resultText
              pure (Provider.Message Provider.ToolRole resultText Nothing (Just callId), testDelta, editDelta)

    isFailureResultProvider :: Aeson.Value -> Bool
    isFailureResultProvider (Aeson.Object obj) =
      case KeyMap.lookup "exit_code" obj of
        Just (Aeson.Number n) -> n /= 0
        _ -> False
    isFailureResultProvider (Aeson.String s) =
      "error"
        `Text.isInfixOf` Text.toLower s
        || "failed"
        `Text.isInfixOf` Text.toLower s
        || "FAILED"
        `Text.isInfixOf` s
    isFailureResultProvider _ = False

    isOldStrNotFoundProvider :: Aeson.Value -> Bool
    isOldStrNotFoundProvider (Aeson.Object obj) =
      case KeyMap.lookup "error" obj of
        Just (Aeson.String s) -> "old_str not found" `Text.isInfixOf` s
        _ -> False
    isOldStrNotFoundProvider _ = False

runAgentWithProviderStreaming ::
  EngineConfig ->
  Provider.Provider ->
  AgentConfig ->
  Text ->
  (Text -> IO ()) ->
  IO (Either Text AgentResult)
runAgentWithProviderStreaming engineCfg provider agentCfg userPrompt onStreamChunk = do
  let tools = agentTools agentCfg
      toolApis = map encodeToolForProvider tools
      toolMap = buildToolMap tools
      systemMsg = providerMessage Provider.System (agentSystemPrompt agentCfg)
      userMsg = providerMessage Provider.User userPrompt
      initialMessages = [systemMsg, userMsg]

  engineOnActivity engineCfg "Starting agent loop (Provider+Streaming)"
  loopProviderStreaming provider toolApis toolMap initialMessages 0 0 0 0.0 Map.empty 0 0
  where
    maxIter = agentMaxIterations agentCfg
    guardrails' = agentGuardrails agentCfg

    providerMessage :: Provider.Role -> Text -> Provider.Message
    providerMessage role content = Provider.Message role content Nothing Nothing

    loopProviderStreaming ::
      Provider.Provider ->
      [Provider.ToolApi] ->
      Map.Map Text Tool ->
      [Provider.Message] ->
      Int ->
      Int ->
      Int ->
      Double ->
      Map.Map Text Int ->
      Int ->
      Int ->
      IO (Either Text AgentResult)
    loopProviderStreaming prov toolApis' toolMap msgs iteration totalCalls totalTokens totalCost toolCallCounts testFailures editFailures
      | iteration >= maxIter = do
          let errMsg = "Max iterations (" <> tshow maxIter <> ") reached"
          engineOnError engineCfg errMsg
          pure <| Left errMsg
      | otherwise = do
          let guardrailViolation = findGuardrailViolation guardrails' totalCost totalTokens toolCallCounts testFailures editFailures
          case guardrailViolation of
            Just (g, errMsg) -> do
              engineOnGuardrail engineCfg g
              pure <| Left errMsg
            Nothing -> do
              engineOnActivity engineCfg <| "Iteration " <> tshow (iteration + 1)
              hasToolCalls <- newIORef False
              result <-
                Provider.chatStream prov toolApis' msgs <| \case
                  Provider.StreamContent txt -> onStreamChunk txt
                  Provider.StreamToolCall _ -> writeIORef hasToolCalls True
                  Provider.StreamToolCallDelta _ -> writeIORef hasToolCalls True
                  Provider.StreamError err -> engineOnError engineCfg err
                  Provider.StreamDone _ -> pure ()
              case result of
                Left err -> do
                  engineOnError engineCfg err
                  pure (Left err)
                Right chatRes -> do
                  let msg = Provider.chatMessage chatRes
                      tokens = maybe 0 Provider.usageTotalTokens (Provider.chatUsage chatRes)
                      cost = case Provider.chatUsage chatRes +> Provider.usageCost of
                        Just actualCost -> actualCost * 100
                        Nothing -> estimateCost (getProviderModelStreaming prov) tokens
                  engineOnCost engineCfg tokens cost
                  let newTokens = totalTokens + tokens
                      newCost = totalCost + cost
                  let assistantText = Provider.msgContent msg
                  unless (Text.null assistantText)
                    <| engineOnAssistant engineCfg assistantText
                  case Provider.msgToolCalls msg of
                    Nothing
                      | Text.null (Provider.msgContent msg) && totalCalls > 0 -> do
                          engineOnActivity engineCfg "Empty response after tools, prompting for text"
                          let promptMsg = Provider.Message Provider.ToolRole "Please provide a response to the user." Nothing Nothing
                              newMsgs = msgs <> [msg, promptMsg]
                          loopProviderStreaming prov toolApis' toolMap newMsgs (iteration + 1) totalCalls newTokens newCost toolCallCounts testFailures editFailures
                      | otherwise -> do
                          engineOnActivity engineCfg "Agent completed"
                          engineOnComplete engineCfg
                          pure
                            <| Right
                            <| AgentResult
                              { resultFinalMessage = Provider.msgContent msg,
                                resultToolCallCount = totalCalls,
                                resultIterations = iteration + 1,
                                resultTotalCost = newCost,
                                resultTotalTokens = newTokens
                              }
                    Just [] -> do
                      engineOnActivity engineCfg "Agent completed (empty tool calls)"
                      engineOnComplete engineCfg
                      pure
                        <| Right
                        <| AgentResult
                          { resultFinalMessage = Provider.msgContent msg,
                            resultToolCallCount = totalCalls,
                            resultIterations = iteration + 1,
                            resultTotalCost = newCost,
                            resultTotalTokens = newTokens
                          }
                    Just tcs -> do
                      (toolResults, newTestFailures, newEditFailures) <- executeToolCallsStreaming engineCfg toolMap tcs testFailures editFailures
                      let newMsgs = msgs <> [msg] <> toolResults
                          newCalls = totalCalls + length tcs
                          newToolCallCounts = updateToolCallCountsStreaming toolCallCounts tcs
                      loopProviderStreaming prov toolApis' toolMap newMsgs (iteration + 1) newCalls newTokens newCost newToolCallCounts newTestFailures newEditFailures

    getProviderModelStreaming :: Provider.Provider -> Text
    getProviderModelStreaming (Provider.OpenRouter cfg) = Provider.providerModel cfg
    getProviderModelStreaming (Provider.Ollama cfg) = Provider.providerModel cfg
    getProviderModelStreaming (Provider.AmpCLI _) = "amp"

    updateToolCallCountsStreaming :: Map.Map Text Int -> [Provider.ToolCall] -> Map.Map Text Int
    updateToolCallCountsStreaming =
      foldr (\tc m -> Map.insertWith (+) (Provider.fcName (Provider.tcFunction tc)) 1 m)

    executeToolCallsStreaming :: EngineConfig -> Map.Map Text Tool -> [Provider.ToolCall] -> Int -> Int -> IO ([Provider.Message], Int, Int)
    executeToolCallsStreaming eCfg tMap tcs initialTestFailures initialEditFailures = do
      results <- traverse (executeSingleStreaming eCfg tMap) tcs
      let msgs = map (\(m, _, _) -> m) results
          testDeltas = map (\(_, t, _) -> t) results
          editDeltas = map (\(_, _, e) -> e) results
          totalTestFail = initialTestFailures + sum testDeltas
          totalEditFail = initialEditFailures + sum editDeltas
      pure (msgs, totalTestFail, totalEditFail)

    executeSingleStreaming :: EngineConfig -> Map.Map Text Tool -> Provider.ToolCall -> IO (Provider.Message, Int, Int)
    executeSingleStreaming eCfg tMap tc = do
      let name = Provider.fcName (Provider.tcFunction tc)
          argsText = Provider.fcArguments (Provider.tcFunction tc)
          callId = Provider.tcId tc
      engineOnActivity eCfg <| "Executing tool: " <> name
      engineOnToolCall eCfg name argsText
      case Map.lookup name tMap of
        Nothing -> do
          let errMsg = "Tool not found: " <> name
          engineOnToolResult eCfg name False errMsg
          pure (Provider.Message Provider.ToolRole errMsg Nothing (Just callId), 0, 0)
        Just tool -> do
          case Aeson.decode (BL.fromStrict (TE.encodeUtf8 argsText)) of
            Nothing -> do
              let errMsg = "Invalid JSON arguments: " <> argsText
              engineOnToolResult eCfg name False errMsg
              pure (Provider.Message Provider.ToolRole errMsg Nothing (Just callId), 0, 0)
            Just args -> do
              resultValue <- toolExecute tool args
              let resultText = TE.decodeUtf8 (BL.toStrict (Aeson.encode resultValue))
                  isTestCall = name == "bash" && ("bild --test" `Text.isInfixOf` argsText || "bild -t" `Text.isInfixOf` argsText)
                  isTestFailure = isTestCall && isFailureResultStreaming resultValue
                  testDelta = if isTestFailure then 1 else 0
                  isEditFailure = name == "edit_file" && isOldStrNotFoundStreaming resultValue
                  editDelta = if isEditFailure then 1 else 0
              engineOnToolResult eCfg name True resultText
              pure (Provider.Message Provider.ToolRole resultText Nothing (Just callId), testDelta, editDelta)

    isFailureResultStreaming :: Aeson.Value -> Bool
    isFailureResultStreaming (Aeson.Object obj) =
      case KeyMap.lookup "exit_code" obj of
        Just (Aeson.Number n) -> n /= 0
        _ -> False
    isFailureResultStreaming (Aeson.String s) =
      "error"
        `Text.isInfixOf` Text.toLower s
        || "failed"
        `Text.isInfixOf` Text.toLower s
        || "FAILED"
        `Text.isInfixOf` s
    isFailureResultStreaming _ = False

    isOldStrNotFoundStreaming :: Aeson.Value -> Bool
    isOldStrNotFoundStreaming (Aeson.Object obj) =
      case KeyMap.lookup "error" obj of
        Just (Aeson.String s) -> "old_str not found" `Text.isInfixOf` s
        _ -> False
    isOldStrNotFoundStreaming _ = False