spider_agent_types 2.48.49

Pure data types and constants for spider_agent automation. Zero heavy dependencies.
Documentation
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
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
//! Configuration types for automation.
//!
//! Contains all configuration types for remote multimodal automation,
//! including runtime configs, retry policies, model selection, and capture profiles.

use std::time::Duration;

use crate::ContentAnalysis;

/// Recovery strategy for handling failures during automation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum RecoveryStrategy {
    /// Retry the same action up to max_retries times.
    #[default]
    Retry,
    /// Try an alternative approach (re-query LLM for different solution).
    Alternative,
    /// Skip the failed step and continue with the next action.
    Skip,
    /// Abort the entire execution on failure.
    Abort,
}

/// Retry policy for automation operations.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct RetryPolicy {
    /// Maximum number of attempts.
    pub max_attempts: usize,
    /// Backoff delay between attempts in milliseconds.
    pub backoff_ms: u64,
    /// Whether to retry on JSON parse errors.
    pub retry_on_parse_error: bool,
    /// Whether to retry on step failures.
    pub retry_on_step_failure: bool,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            backoff_ms: 1000,
            retry_on_parse_error: true,
            retry_on_step_failure: true,
        }
    }
}

impl RetryPolicy {
    /// Create a new retry policy.
    pub fn new(max_attempts: usize) -> Self {
        Self {
            max_attempts,
            ..Default::default()
        }
    }

    /// No retries.
    pub fn none() -> Self {
        Self {
            max_attempts: 1,
            backoff_ms: 0,
            retry_on_parse_error: false,
            retry_on_step_failure: false,
        }
    }

    /// Set backoff delay.
    pub fn with_backoff(mut self, ms: u64) -> Self {
        self.backoff_ms = ms;
        self
    }

    /// Get backoff duration.
    pub fn backoff_duration(&self) -> Duration {
        Duration::from_millis(self.backoff_ms)
    }
}

/// Cost tier for model selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum CostTier {
    /// Prefer cheaper/faster models.
    Low,
    /// Balanced cost/quality.
    #[default]
    Medium,
    /// Prefer higher quality models.
    High,
}

/// Policy for selecting models based on cost/quality tradeoffs.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ModelPolicy {
    /// Small/cheap model identifier.
    pub small: String,
    /// Medium model identifier.
    pub medium: String,
    /// Large/expensive model identifier.
    pub large: String,
    /// Whether large model is allowed.
    pub allow_large: bool,
    /// Maximum latency budget in ms.
    pub max_latency_ms: Option<u64>,
    /// Maximum cost tier allowed.
    pub max_cost_tier: CostTier,
}

impl Default for ModelPolicy {
    fn default() -> Self {
        Self {
            small: "gpt-4o-mini".to_string(),
            medium: "gpt-4o".to_string(),
            large: "gpt-4o".to_string(),
            allow_large: true,
            max_latency_ms: None,
            max_cost_tier: CostTier::High,
        }
    }
}

impl ModelPolicy {
    /// Get the appropriate model for the given cost tier.
    pub fn model_for_tier(&self, tier: CostTier) -> &str {
        match tier {
            CostTier::Low => &self.small,
            CostTier::Medium => &self.medium,
            CostTier::High if self.allow_large => &self.large,
            CostTier::High => &self.medium,
        }
    }

    /// Set small model.
    pub fn with_small(mut self, model: impl Into<String>) -> Self {
        self.small = model.into();
        self
    }

    /// Set medium model.
    pub fn with_medium(mut self, model: impl Into<String>) -> Self {
        self.medium = model.into();
        self
    }

    /// Set large model.
    pub fn with_large(mut self, model: impl Into<String>) -> Self {
        self.large = model.into();
        self
    }
}

/// A model endpoint override for dual-model routing.
///
/// When `api_url` or `api_key` is `None`, the parent
/// [`RemoteMultimodalConfigs`] values are inherited at resolve time.
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub struct ModelEndpoint {
    /// Model identifier for this endpoint (e.g. "gpt-4o-mini").
    pub model_name: String,
    /// Optional API URL override. `None` inherits from parent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_url: Option<String>,
    /// Optional API key override. `None` inherits from parent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_key: Option<String>,
}

impl ModelEndpoint {
    /// Create a new model endpoint with just a model name.
    pub fn new(model_name: impl Into<String>) -> Self {
        Self {
            model_name: model_name.into(),
            api_url: None,
            api_key: None,
        }
    }

    /// Set the API URL override.
    pub fn with_api_url(mut self, url: impl Into<String>) -> Self {
        self.api_url = Some(url.into());
        self
    }

    /// Set the API key override.
    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }
}

/// Routing mode that decides when to use the vision vs text model.
///
/// Only takes effect when [`RemoteMultimodalConfigs::has_dual_model_routing`]
/// returns `true` (i.e. at least one of `vision_model` / `text_model` is set).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum VisionRouteMode {
    /// No routing – always use the primary model (current behaviour).
    #[default]
    AlwaysPrimary,
    /// Text model by default; switch to vision on round 0, stagnation,
    /// stuck ≥ 3, or an explicit `request_vision` memory-op.
    TextFirst,
    /// Vision model for the first 2 rounds and when stagnated/stuck,
    /// then fall back to the text model for stable mid-rounds.
    VisionFirst,
    /// Text model always, vision ONLY on an explicit `request_vision`
    /// memory-op from the agent.
    AgentDriven,
}

/// Reasoning effort level for models that support explicit reasoning controls.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ReasoningEffort {
    /// Lower latency/cost reasoning.
    Low,
    /// Balanced reasoning effort.
    #[default]
    Medium,
    /// Higher quality reasoning at higher latency/cost.
    High,
}

/// HTML cleaning profile for content processing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum HtmlCleaningProfile {
    /// Standard cleaning - removes scripts, styles, comments.
    #[default]
    Default,
    /// Aggressive cleaning - heavy cleanup for extraction.
    Aggressive,
    /// Slim cleaning - removes SVGs, canvas, heavy nodes.
    Slim,
    /// Minimal cleaning - preserve interactivity.
    Minimal,
    /// No cleaning - raw HTML.
    Raw,
    /// Auto-select based on content analysis.
    Auto,
}

impl HtmlCleaningProfile {
    // Size thresholds for smart cleaning decisions (in bytes).
    const SVG_HEAVY_THRESHOLD: usize = 50_000; // 50KB of SVG is heavy
    const SVG_VERY_HEAVY_THRESHOLD: usize = 100_000; // 100KB of SVG is very heavy
    const BASE64_HEAVY_THRESHOLD: usize = 100_000; // 100KB of base64 data
    const SCRIPT_HEAVY_THRESHOLD: usize = 200_000; // 200KB of scripts
    const CLEANABLE_RATIO_HIGH: f32 = 0.4; // 40% of HTML is cleanable
    const CLEANABLE_RATIO_MEDIUM: f32 = 0.25; // 25% of HTML is cleanable

    /// Determine the best cleaning profile based on content analysis.
    ///
    /// This is used when `Auto` is selected to intelligently choose
    /// the appropriate cleaning level based on the HTML content.
    pub fn from_content_analysis(analysis: &ContentAnalysis) -> Self {
        Self::from_content_analysis_with_intent(analysis, CleaningIntent::General)
    }

    /// Determine the best cleaning profile based on content analysis and intended use.
    ///
    /// Uses **byte sizes** (not just counts) for accurate decisions:
    /// - SVG > 100KB → always Slim
    /// - base64 > 100KB → always Slim
    /// - cleanable_ratio > 40% → Slim
    ///
    /// Intent modifies behavior:
    /// - `Extraction` → more aggressive, removes nav/footer/heavy elements
    /// - `Action` → preserves interactive elements (buttons, forms, links)
    /// - `General` → balanced heuristics
    pub fn from_content_analysis_with_intent(
        analysis: &ContentAnalysis,
        intent: CleaningIntent,
    ) -> Self {
        // === BYTE-SIZE BASED DECISIONS (more accurate than counts!) ===

        // Very heavy SVGs - always slim regardless of intent
        if analysis.svg_bytes > Self::SVG_VERY_HEAVY_THRESHOLD {
            return HtmlCleaningProfile::Slim;
        }

        // Very heavy base64 data (inline images, fonts) - always slim
        if analysis.base64_bytes > Self::BASE64_HEAVY_THRESHOLD {
            return HtmlCleaningProfile::Slim;
        }

        // High cleanable ratio means lots of bloat - slim is worthwhile
        if analysis.cleanable_ratio > Self::CLEANABLE_RATIO_HIGH {
            return HtmlCleaningProfile::Slim;
        }

        match intent {
            CleaningIntent::Extraction => {
                // For extraction, we can be aggressive - we only need text content

                // Heavy SVGs (50KB+) - slim them out
                if analysis.svg_bytes > Self::SVG_HEAVY_THRESHOLD {
                    return HtmlCleaningProfile::Slim;
                }

                // Large HTML with lots of scripts - aggressive
                if analysis.script_bytes > Self::SCRIPT_HEAVY_THRESHOLD {
                    return HtmlCleaningProfile::Aggressive;
                }

                // Large HTML overall
                if analysis.html_length > 100_000 {
                    return HtmlCleaningProfile::Aggressive;
                }

                // Medium cleanable ratio - slim is beneficial
                if analysis.cleanable_ratio > Self::CLEANABLE_RATIO_MEDIUM {
                    return HtmlCleaningProfile::Slim;
                }

                // Canvas/video/embeds present - slim
                if analysis.canvas_count > 0 || analysis.video_count > 1 || analysis.embed_count > 0
                {
                    return HtmlCleaningProfile::Slim;
                }

                // Low text ratio with medium+ HTML - aggressive
                if analysis.text_ratio < 0.1 && analysis.html_length > 30_000 {
                    return HtmlCleaningProfile::Aggressive;
                }

                // Default to slim for extraction (safe choice)
                HtmlCleaningProfile::Slim
            }
            CleaningIntent::Action => {
                // For actions, preserve interactive elements but remove heavy visual bloat

                // Heavy SVGs - slim (they're not interactive)
                if analysis.svg_bytes > Self::SVG_HEAVY_THRESHOLD {
                    return HtmlCleaningProfile::Slim;
                }

                // Medium cleanable ratio - default cleaning preserves interactivity
                if analysis.cleanable_ratio > Self::CLEANABLE_RATIO_MEDIUM {
                    return HtmlCleaningProfile::Default;
                }

                // Very large HTML - need some cleaning
                if analysis.html_length > 150_000 {
                    return HtmlCleaningProfile::Default;
                }

                // Keep minimal to preserve interactive elements
                HtmlCleaningProfile::Minimal
            }
            CleaningIntent::General => {
                // Balanced approach based on content characteristics

                // Heavy SVGs - slim
                if analysis.svg_bytes > Self::SVG_HEAVY_THRESHOLD {
                    return HtmlCleaningProfile::Slim;
                }

                // Medium cleanable ratio - slim is beneficial
                if analysis.cleanable_ratio > Self::CLEANABLE_RATIO_MEDIUM {
                    return HtmlCleaningProfile::Slim;
                }

                // Canvas/video present - slim
                if analysis.canvas_count > 0 || analysis.video_count > 2 {
                    return HtmlCleaningProfile::Slim;
                }

                // Low text ratio with large HTML - aggressive
                if analysis.text_ratio < 0.05 && analysis.html_length > 50_000 {
                    return HtmlCleaningProfile::Aggressive;
                }

                // Embeds present - slim
                if analysis.embed_count > 0 {
                    return HtmlCleaningProfile::Slim;
                }

                // Large HTML with moderate text - default
                if analysis.html_length > 100_000 && analysis.text_ratio < 0.15 {
                    return HtmlCleaningProfile::Default;
                }

                // Medium HTML - default
                if analysis.html_length > 30_000 {
                    return HtmlCleaningProfile::Default;
                }

                // Small HTML - minimal cleaning
                HtmlCleaningProfile::Minimal
            }
        }
    }

    /// Quick check if this profile removes SVGs.
    pub fn removes_svgs(&self) -> bool {
        matches!(
            self,
            HtmlCleaningProfile::Slim | HtmlCleaningProfile::Aggressive
        )
    }

    /// Quick check if this profile removes video/canvas elements.
    pub fn removes_media(&self) -> bool {
        matches!(self, HtmlCleaningProfile::Slim)
    }

    /// Estimate bytes that will be removed by this cleaning profile.
    pub fn estimate_savings(&self, analysis: &ContentAnalysis) -> usize {
        match self {
            HtmlCleaningProfile::Raw => 0,
            HtmlCleaningProfile::Minimal => analysis.script_bytes + analysis.style_bytes,
            HtmlCleaningProfile::Default => {
                analysis.script_bytes + analysis.style_bytes + (analysis.base64_bytes / 2)
            }
            HtmlCleaningProfile::Slim => analysis.cleanable_bytes,
            HtmlCleaningProfile::Aggressive => {
                // Aggressive also removes nav/footer, estimate ~10% more
                analysis.cleanable_bytes + (analysis.html_length / 10)
            }
            HtmlCleaningProfile::Auto => 0, // Can't estimate without analyzing
        }
    }
}

/// Intent for HTML cleaning decisions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum CleaningIntent {
    /// General purpose - balanced cleaning.
    #[default]
    General,
    /// Extraction focused - aggressive, text-only.
    Extraction,
    /// Action focused - preserve interactivity.
    Action,
}

/// Capture profile for screenshots and HTML.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CaptureProfile {
    /// Capture full page screenshot.
    pub full_page: bool,
    /// Omit background in screenshot.
    pub omit_background: bool,
    /// Clip viewport for screenshot.
    pub clip: Option<ClipViewport>,
    /// HTML cleaning profile.
    pub html_cleaning: HtmlCleaningProfile,
    /// Maximum HTML bytes to capture.
    pub html_max_bytes: usize,
    /// Note about this capture attempt.
    pub attempt_note: Option<String>,
}

impl Default for CaptureProfile {
    fn default() -> Self {
        Self {
            full_page: true,
            omit_background: true,
            clip: None,
            html_cleaning: HtmlCleaningProfile::Default,
            html_max_bytes: 24_000,
            attempt_note: None,
        }
    }
}

impl CaptureProfile {
    /// Create a profile for extraction (aggressive cleaning).
    pub fn for_extraction() -> Self {
        Self {
            html_cleaning: HtmlCleaningProfile::Aggressive,
            ..Default::default()
        }
    }

    /// Create a profile for actions (preserve interactivity).
    pub fn for_action() -> Self {
        Self {
            html_cleaning: HtmlCleaningProfile::Minimal,
            full_page: false,
            ..Default::default()
        }
    }

    /// Set HTML max bytes.
    pub fn with_max_bytes(mut self, bytes: usize) -> Self {
        self.html_max_bytes = bytes;
        self
    }
}

/// Clip viewport for screenshots.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ClipViewport {
    /// X coordinate.
    pub x: f64,
    /// Y coordinate.
    pub y: f64,
    /// Width.
    pub width: f64,
    /// Height.
    pub height: f64,
}

impl ClipViewport {
    /// Create a new clip viewport.
    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }
}

/// Main automation configuration.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct AutomationConfig {
    /// The goal to achieve.
    pub goal: String,
    /// Maximum number of steps before stopping.
    pub max_steps: usize,
    /// Timeout in milliseconds.
    pub timeout_ms: u64,
    /// Recovery strategy for failures.
    pub recovery_strategy: RecoveryStrategy,
    /// Maximum retries per step.
    pub max_retries: usize,
    /// Whether to use selector cache.
    pub use_cache: bool,
    /// Whether to capture screenshots after each step.
    pub capture_screenshots: bool,
    /// URLs that indicate success.
    pub success_urls: Vec<String>,
    /// Text patterns that indicate success.
    pub success_patterns: Vec<String>,
    /// Whether to extract data on success.
    pub extract_on_success: bool,
    /// Extraction prompt for final data.
    pub extraction_prompt: Option<String>,
    /// Capture profile.
    pub capture_profile: CaptureProfile,
    /// Retry policy.
    pub retry_policy: RetryPolicy,
    /// Model policy.
    pub model_policy: ModelPolicy,
    /// Optional system prompt for automation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_prompt: Option<String>,
    /// Optional extra system instructions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_prompt_extra: Option<String>,
    /// Optional extra user message instructions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_message_extra: Option<String>,
}

impl Default for AutomationConfig {
    fn default() -> Self {
        Self {
            goal: String::new(),
            max_steps: 20,
            timeout_ms: 600_000, // 10 minutes
            recovery_strategy: RecoveryStrategy::Retry,
            max_retries: 3,
            use_cache: true,
            capture_screenshots: true,
            success_urls: Vec::new(),
            success_patterns: Vec::new(),
            extract_on_success: false,
            extraction_prompt: None,
            capture_profile: CaptureProfile::default(),
            retry_policy: RetryPolicy::default(),
            model_policy: ModelPolicy::default(),
            system_prompt: None,
            system_prompt_extra: None,
            user_message_extra: None,
        }
    }
}

impl AutomationConfig {
    /// Create a new config with a goal.
    pub fn new(goal: impl Into<String>) -> Self {
        Self {
            goal: goal.into(),
            ..Default::default()
        }
    }

    /// Set maximum steps.
    pub fn with_max_steps(mut self, steps: usize) -> Self {
        self.max_steps = steps;
        self
    }

    /// Set timeout in milliseconds.
    pub fn with_timeout(mut self, ms: u64) -> Self {
        self.timeout_ms = ms;
        self
    }

    /// Set recovery strategy.
    pub fn with_recovery(mut self, strategy: RecoveryStrategy) -> Self {
        self.recovery_strategy = strategy;
        self
    }

    /// Set max retries.
    pub fn with_retries(mut self, retries: usize) -> Self {
        self.max_retries = retries;
        self
    }

    /// Enable/disable selector cache.
    pub fn with_cache(mut self, enabled: bool) -> Self {
        self.use_cache = enabled;
        self
    }

    /// Enable/disable screenshots.
    pub fn with_screenshots(mut self, enabled: bool) -> Self {
        self.capture_screenshots = enabled;
        self
    }

    /// Add a success URL pattern.
    pub fn with_success_url(mut self, url: impl Into<String>) -> Self {
        self.success_urls.push(url.into());
        self
    }

    /// Add a success text pattern.
    pub fn with_success_pattern(mut self, pattern: impl Into<String>) -> Self {
        self.success_patterns.push(pattern.into());
        self
    }

    /// Enable extraction on success.
    pub fn with_extraction(mut self, prompt: impl Into<String>) -> Self {
        self.extract_on_success = true;
        self.extraction_prompt = Some(prompt.into());
        self
    }

    /// Set capture profile.
    pub fn with_capture_profile(mut self, profile: CaptureProfile) -> Self {
        self.capture_profile = profile;
        self
    }

    /// Set retry policy.
    pub fn with_retry_policy(mut self, policy: RetryPolicy) -> Self {
        self.retry_policy = policy;
        self
    }

    /// Set model policy.
    pub fn with_model_policy(mut self, policy: ModelPolicy) -> Self {
        self.model_policy = policy;
        self
    }

    /// Set system prompt.
    pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.system_prompt = Some(prompt.into());
        self
    }

    /// Set extra system instructions.
    pub fn with_system_prompt_extra(mut self, extra: impl Into<String>) -> Self {
        self.system_prompt_extra = Some(extra.into());
        self
    }

    /// Set extra user message instructions.
    pub fn with_user_message_extra(mut self, extra: impl Into<String>) -> Self {
        self.user_message_extra = Some(extra.into());
        self
    }

    /// Get timeout as Duration.
    pub fn timeout_duration(&self) -> std::time::Duration {
        std::time::Duration::from_millis(self.timeout_ms)
    }

    /// Check if a URL matches success criteria.
    pub fn is_success_url(&self, url: &str) -> bool {
        self.success_urls
            .iter()
            .any(|pattern| url.contains(pattern))
    }

    /// Check if text matches success criteria.
    pub fn matches_success_pattern(&self, text: &str) -> bool {
        self.success_patterns
            .iter()
            .any(|pattern| text.contains(pattern))
    }
}

// =============================================================================
// REMOTE MULTIMODAL ENGINE CONFIGURATION
// =============================================================================

/// Runtime configuration for `RemoteMultimodalEngine`.
///
/// This struct controls:
/// 1) what context is captured (URL/title/HTML),
/// 2) how chat completion is requested (temperature/max tokens/JSON mode),
/// 3) how long the engine loops and retries,
/// 4) capture/model selection policies.
///
/// The engine should be able to **export this config** to users, and it should
/// be safe to merge with user-provided prompts.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct RemoteMultimodalConfig {
    // -----------------------------------------------------------------
    // Context capture
    // -----------------------------------------------------------------
    /// Whether to include cleaned HTML in the model input.
    pub include_html: bool,
    /// Maximum number of bytes of cleaned HTML to include (global default).
    ///
    /// A `CaptureProfile` may override this with its own `html_max_bytes`.
    pub html_max_bytes: usize,
    /// Whether to include the current URL in the model input.
    pub include_url: bool,
    /// Whether to include the current document title in the model input.
    pub include_title: bool,
    /// Whether to include screenshots in the LLM request.
    ///
    /// When `None` (default), automatically detects based on model name.
    /// Vision models (gpt-4o, claude-3, etc.) will receive screenshots,
    /// while text-only models will not.
    ///
    /// Set to `Some(true)` to always include screenshots.
    /// Set to `Some(false)` to never include screenshots.
    pub include_screenshot: Option<bool>,

    // -----------------------------------------------------------------
    // LLM knobs
    // -----------------------------------------------------------------
    /// Sampling temperature used by the remote/local model.
    pub temperature: f32,
    /// Maximum tokens the model is allowed to generate for the plan.
    pub max_tokens: u16,
    /// If true, include `response_format: {"type":"json_object"}` in the request.
    ///
    /// Some local servers ignore or reject this; disable if you see 400 errors.
    pub request_json_object: bool,
    /// Best-effort JSON extraction (strip fences / extract `{...}`).
    pub best_effort_json_extract: bool,
    /// Optional explicit reasoning effort for supported models/endpoints.
    ///
    /// When set, outbound requests include `reasoning: {"effort":"low|medium|high"}`.
    /// Leave `None` to avoid sending provider-specific reasoning controls.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_effort: Option<ReasoningEffort>,

    /// Optional token budget for Anthropic extended thinking.
    ///
    /// When set, outbound requests to Anthropic endpoints include
    /// `thinking: {"type":"enabled","budget_tokens":N}`.
    /// Leave `None` to disable extended thinking.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_budget: Option<u32>,

    // -----------------------------------------------------------------
    // Skills injection limits
    // -----------------------------------------------------------------
    /// Maximum number of skills to inject per round (default 3).
    /// Only the highest-priority matching skills are included.
    #[serde(default = "default_max_skills_per_round")]
    pub max_skills_per_round: usize,
    /// Maximum characters for skill context injection per round (default 4000).
    /// Prevents large skill collections from bloating the system prompt.
    #[serde(default = "default_max_skill_context_chars")]
    pub max_skill_context_chars: usize,

    // -----------------------------------------------------------------
    // Loop + retry
    // -----------------------------------------------------------------
    /// Maximum number of plan/execute/re-capture rounds before giving up.
    ///
    /// Each round is:
    /// 1) capture state
    /// 2) ask model for plan
    /// 3) execute steps
    /// 4) optionally wait
    /// 5) re-capture and decide whether complete
    pub max_rounds: usize,

    /// Retry policy for model output parsing failures and/or execution failures.
    pub retry: RetryPolicy,

    // -----------------------------------------------------------------
    // Capture / model policies
    // -----------------------------------------------------------------
    /// Capture profiles to try across attempts.
    ///
    /// If empty, the engine should build a sensible default list.
    pub capture_profiles: Vec<CaptureProfile>,

    /// Model selection policy (small/medium/large).
    ///
    /// The engine may choose a model size depending on constraints such as
    /// latency limits, cost tier, and whether retries are escalating.
    pub model_policy: ModelPolicy,

    /// Optional: wait after executing a plan before re-capturing state (ms).
    ///
    /// This is useful for pages that animate, load asynchronously, or perform
    /// challenge transitions after clicks.
    pub post_plan_wait_ms: u64,
    /// Maximum number of concurrent LLM HTTP requests for this engine instance.
    /// If `None`, no throttling is applied.
    pub max_inflight_requests: Option<usize>,

    /// Overall timeout for the remote multimodal automation loop, in milliseconds.
    ///
    /// When set, this overrides the default page-request-based timeout that
    /// gates the `run_remote_multimodal_if_enabled` call. Useful for slow
    /// inference hardware where the default 5-minute cap is too short.
    ///
    /// `None` (default) keeps the existing behaviour (derived from request_timeout).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub automation_timeout_ms: Option<u64>,

    // -----------------------------------------------------------------
    // Extraction
    // -----------------------------------------------------------------
    /// Enable extraction mode to return structured data from pages.
    ///
    /// When enabled, the model is instructed to include an `extracted` field
    /// in its JSON response containing data extracted from the page.
    pub extra_ai_data: bool,
    /// Optional custom extraction prompt appended to the system prompt.
    ///
    /// Example: "Extract all product names and prices as a JSON array."
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extraction_prompt: Option<String>,
    /// Optional JSON schema for structured extraction output.
    ///
    /// When provided, the model is instructed to return the `extracted` field
    /// conforming to this schema. This enables type-safe extraction.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extraction_schema: Option<super::ExtractionSchema>,
    /// Take a screenshot after automation completes and include it in results.
    pub screenshot: bool,

    // -----------------------------------------------------------------
    // Claude-optimized features
    // -----------------------------------------------------------------
    /// Tool calling mode for structured action output.
    ///
    /// - `JsonObject` (default): Use JSON object mode
    /// - `ToolCalling`: Use OpenAI-compatible tool/function calling
    /// - `Auto`: Auto-select based on model capabilities
    #[serde(default)]
    pub tool_calling_mode: super::tool_calling::ToolCallingMode,

    /// HTML diff mode for condensed page state.
    ///
    /// When enabled, sends only HTML changes after the first round,
    /// potentially reducing tokens by 50-70%.
    #[serde(default)]
    pub html_diff_mode: super::html_diff::HtmlDiffMode,

    /// Planning mode configuration.
    ///
    /// When enabled, allows the LLM to plan multiple steps upfront,
    /// reducing round-trips. Set to `None` to disable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub planning_mode: Option<super::planning::PlanningModeConfig>,

    /// Multi-page synthesis configuration.
    ///
    /// When configured, enables analyzing multiple pages in a single
    /// LLM call. Set to `None` to disable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub synthesis_config: Option<super::synthesis::SynthesisConfig>,

    /// Confidence-based retry strategy.
    ///
    /// When configured, uses confidence scores to make smarter retry
    /// decisions. Set to `None` for default retry behavior.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub confidence_strategy: Option<super::confidence::ConfidenceRetryStrategy>,

    /// Self-healing configuration for automatic selector repair.
    ///
    /// When enabled, failed selectors trigger an LLM call to diagnose
    /// and suggest alternatives. Set to `None` to disable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub self_healing: Option<super::self_healing::SelfHealingConfig>,

    /// Enable concurrent execution of independent actions.
    ///
    /// When true, actions without dependencies can run in parallel
    /// using `tokio::JoinSet`.
    #[serde(default)]
    pub concurrent_execution: bool,

    // -----------------------------------------------------------------
    // Relevance gating
    // -----------------------------------------------------------------
    /// Enable relevance gating for crawled pages.
    /// When enabled, the LLM returns `"relevant": true|false` indicating
    /// whether the page is relevant to the crawl/extraction goals.
    /// Irrelevant pages can have their budget refunded.
    #[serde(default)]
    pub relevance_gate: bool,
    /// Optional custom relevance criteria prompt.
    /// When None, defaults to judging against extraction_prompt or general context.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub relevance_prompt: Option<String>,
    /// Enable URL-level pre-filtering before HTTP fetch.
    /// When enabled alongside `relevance_gate`, URLs are classified by the
    /// text model BEFORE fetching. Irrelevant URLs are skipped entirely.
    #[serde(default)]
    pub url_prefilter: bool,
    /// Batch size for URL classification calls (default 20).
    #[serde(default = "default_url_prefilter_batch_size")]
    pub url_prefilter_batch_size: usize,
    /// Max tokens for URL classification response (default 200).
    #[serde(default = "default_url_prefilter_max_tokens")]
    pub url_prefilter_max_tokens: u16,
}

impl Default for RemoteMultimodalConfig {
    fn default() -> Self {
        Self {
            include_html: true,
            html_max_bytes: 24_000,
            include_url: true,
            include_title: true,
            include_screenshot: None, // Auto-detect based on model
            temperature: 0.1,
            max_tokens: 1024,
            request_json_object: true,
            best_effort_json_extract: true,
            reasoning_effort: None,
            thinking_budget: None,
            max_rounds: 6,
            retry: RetryPolicy::default(),
            model_policy: ModelPolicy::default(),
            capture_profiles: Vec::new(),
            post_plan_wait_ms: 350,
            max_inflight_requests: None,
            automation_timeout_ms: None,
            extra_ai_data: false,
            extraction_prompt: None,
            extraction_schema: None,
            screenshot: true,
            // Claude-optimized features (all disabled by default for backward compatibility)
            tool_calling_mode: super::tool_calling::ToolCallingMode::default(),
            html_diff_mode: super::html_diff::HtmlDiffMode::default(),
            planning_mode: None,
            synthesis_config: None,
            confidence_strategy: None,
            self_healing: None,
            concurrent_execution: false,
            relevance_gate: false,
            relevance_prompt: None,
            url_prefilter: false,
            url_prefilter_batch_size: default_url_prefilter_batch_size(),
            url_prefilter_max_tokens: default_url_prefilter_max_tokens(),
            max_skills_per_round: default_max_skills_per_round(),
            max_skill_context_chars: default_max_skill_context_chars(),
        }
    }
}

fn default_url_prefilter_batch_size() -> usize {
    20
}

fn default_url_prefilter_max_tokens() -> u16 {
    200
}

fn default_max_skills_per_round() -> usize {
    3
}

fn default_max_skill_context_chars() -> usize {
    4000
}

impl RemoteMultimodalConfig {
    /// Create a new config with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a config optimized for maximum speed and efficiency.
    ///
    /// Enables all performance-positive features:
    /// - `ToolCallingMode::Auto` for reliable action parsing
    /// - `HtmlDiffMode::Auto` for 50-70% token reduction
    /// - `ConfidenceRetryStrategy` for smarter retries
    /// - `concurrent_execution` for parallel action execution
    ///
    /// These features have zero or positive performance impact.
    pub fn fast() -> Self {
        Self {
            tool_calling_mode: super::tool_calling::ToolCallingMode::Auto,
            html_diff_mode: super::html_diff::HtmlDiffMode::Auto,
            confidence_strategy: Some(super::confidence::ConfidenceRetryStrategy::default()),
            concurrent_execution: true,
            ..Self::default()
        }
    }

    /// Create a config optimized for maximum speed with planning enabled.
    ///
    /// Includes all `fast()` features plus:
    /// - `PlanningModeConfig` for multi-step planning (fewer round-trips)
    /// - `SelfHealingConfig` for auto-repair of failed selectors
    ///
    /// Best for complex multi-step automations.
    pub fn fast_with_planning() -> Self {
        Self {
            planning_mode: Some(super::planning::PlanningModeConfig::default()),
            self_healing: Some(super::self_healing::SelfHealingConfig::default()),
            ..Self::fast()
        }
    }

    /// Returns `true` when the config is set up for pure data extraction
    /// (extraction enabled, single round). Used to auto-detect extraction-only
    /// mode and optimize prompts / screenshot handling.
    pub fn is_extraction_only(&self) -> bool {
        self.extra_ai_data && self.max_rounds <= 1
    }

    /// Set whether to include HTML.
    pub fn with_html(mut self, include: bool) -> Self {
        self.include_html = include;
        self
    }

    /// Set maximum HTML bytes.
    pub fn with_html_max_bytes(mut self, bytes: usize) -> Self {
        self.html_max_bytes = bytes;
        self
    }

    /// Set temperature.
    pub fn with_temperature(mut self, temp: f32) -> Self {
        self.temperature = temp;
        self
    }

    /// Set max tokens.
    pub fn with_max_tokens(mut self, tokens: u16) -> Self {
        self.max_tokens = tokens;
        self
    }

    /// Set explicit reasoning effort for supported models/endpoints.
    pub fn with_reasoning_effort(mut self, effort: Option<ReasoningEffort>) -> Self {
        self.reasoning_effort = effort;
        self
    }

    /// Set token budget for Anthropic extended thinking.
    pub fn with_thinking_budget(mut self, budget: Option<u32>) -> Self {
        self.thinking_budget = budget;
        self
    }

    /// Set max rounds.
    pub fn with_max_rounds(mut self, rounds: usize) -> Self {
        self.max_rounds = rounds;
        self
    }

    /// Set retry policy.
    pub fn with_retry(mut self, retry: RetryPolicy) -> Self {
        self.retry = retry;
        self
    }

    /// Set model policy.
    pub fn with_model_policy(mut self, policy: ModelPolicy) -> Self {
        self.model_policy = policy;
        self
    }

    /// Enable extraction mode.
    pub fn with_extraction(mut self, enabled: bool) -> Self {
        self.extra_ai_data = enabled;
        self
    }

    /// Set extraction prompt.
    pub fn with_extraction_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.extraction_prompt = Some(prompt.into());
        self
    }

    /// Set extraction schema.
    pub fn with_extraction_schema(mut self, schema: super::ExtractionSchema) -> Self {
        self.extraction_schema = Some(schema);
        self
    }

    /// Enable/disable screenshots.
    pub fn with_screenshot(mut self, enabled: bool) -> Self {
        self.screenshot = enabled;
        self
    }

    /// Set whether to include screenshots in LLM requests.
    ///
    /// - `Some(true)`: Always include screenshots
    /// - `Some(false)`: Never include screenshots
    /// - `None`: Auto-detect based on model name (default)
    pub fn with_include_screenshot(mut self, include: Option<bool>) -> Self {
        self.include_screenshot = include;
        self
    }

    /// Add a capture profile.
    pub fn add_capture_profile(&mut self, profile: CaptureProfile) {
        self.capture_profiles.push(profile);
    }

    // -------------------------------------------------------------------------
    // Claude-optimized feature builders
    // -------------------------------------------------------------------------

    /// Set tool calling mode.
    pub fn with_tool_calling_mode(mut self, mode: super::tool_calling::ToolCallingMode) -> Self {
        self.tool_calling_mode = mode;
        self
    }

    /// Set HTML diff mode for condensed page state.
    pub fn with_html_diff_mode(mut self, mode: super::html_diff::HtmlDiffMode) -> Self {
        self.html_diff_mode = mode;
        self
    }

    /// Enable planning mode with configuration.
    pub fn with_planning_mode(mut self, config: super::planning::PlanningModeConfig) -> Self {
        self.planning_mode = Some(config);
        self
    }

    /// Enable multi-page synthesis with configuration.
    pub fn with_synthesis_config(mut self, config: super::synthesis::SynthesisConfig) -> Self {
        self.synthesis_config = Some(config);
        self
    }

    /// Set confidence-based retry strategy.
    pub fn with_confidence_strategy(
        mut self,
        strategy: super::confidence::ConfidenceRetryStrategy,
    ) -> Self {
        self.confidence_strategy = Some(strategy);
        self
    }

    /// Enable self-healing with configuration.
    pub fn with_self_healing(mut self, config: super::self_healing::SelfHealingConfig) -> Self {
        self.self_healing = Some(config);
        self
    }

    /// Enable/disable concurrent execution of independent actions.
    pub fn with_concurrent_execution(mut self, enabled: bool) -> Self {
        self.concurrent_execution = enabled;
        self
    }

    /// Enable relevance gating with optional custom criteria prompt.
    pub fn with_relevance_gate(mut self, prompt: Option<String>) -> Self {
        self.relevance_gate = true;
        self.relevance_prompt = prompt;
        self
    }

    /// Enable URL-level pre-filtering before HTTP fetch.
    /// Requires `relevance_gate` to also be enabled.
    pub fn with_url_prefilter(mut self, batch_size: Option<usize>) -> Self {
        self.url_prefilter = true;
        if let Some(bs) = batch_size {
            self.url_prefilter_batch_size = bs;
        }
        self
    }
}

/// Re-exports from llm_models_spider for auto-updated model intelligence.
///
/// This uses the `llm_models_spider` crate which is automatically updated
/// via GitHub Actions to fetch the latest model capabilities from
/// OpenRouter, LiteLLM, and Chatbot Arena.
pub use llm_models_spider::{
    arena_rank, model_profile, supports_pdf, supports_video, supports_vision, ModelCapabilities,
    ModelInfoEntry, ModelPricing, ModelProfile, ModelRanks, MODEL_INFO,
};

/// Merge a base config with an override config.
///
/// Override values take precedence. This is used for URL-specific config overrides.
pub fn merged_config(
    base: &RemoteMultimodalConfig,
    override_cfg: &RemoteMultimodalConfig,
) -> RemoteMultimodalConfig {
    let mut out = base.clone();

    out.include_html = override_cfg.include_html;
    out.html_max_bytes = override_cfg.html_max_bytes;
    out.include_url = override_cfg.include_url;
    out.include_title = override_cfg.include_title;
    out.include_screenshot = override_cfg.include_screenshot;

    out.temperature = override_cfg.temperature;
    out.max_tokens = override_cfg.max_tokens;
    out.request_json_object = override_cfg.request_json_object;
    out.best_effort_json_extract = override_cfg.best_effort_json_extract;
    out.reasoning_effort = override_cfg.reasoning_effort;
    out.thinking_budget = override_cfg.thinking_budget;

    out.max_rounds = override_cfg.max_rounds;
    out.post_plan_wait_ms = override_cfg.post_plan_wait_ms;

    out.retry = override_cfg.retry.clone();
    out.model_policy = override_cfg.model_policy.clone();

    if !override_cfg.capture_profiles.is_empty() {
        out.capture_profiles = override_cfg.capture_profiles.clone();
    }

    // Extraction settings
    out.extra_ai_data = override_cfg.extra_ai_data;
    out.extraction_prompt = override_cfg.extraction_prompt.clone();
    out.extraction_schema = override_cfg.extraction_schema.clone();
    out.screenshot = override_cfg.screenshot;

    // Relevance gating
    out.relevance_gate = override_cfg.relevance_gate;
    out.relevance_prompt = override_cfg.relevance_prompt.clone();

    // URL pre-filter
    out.url_prefilter = override_cfg.url_prefilter;
    out.url_prefilter_batch_size = override_cfg.url_prefilter_batch_size;
    out.url_prefilter_max_tokens = override_cfg.url_prefilter_max_tokens;

    out
}

/// Build a provider-compatible reasoning payload when configured.
///
/// Returns `Some({"effort":"..."})` if reasoning effort is configured,
/// otherwise returns `None`.
pub fn reasoning_payload(cfg: &RemoteMultimodalConfig) -> Option<serde_json::Value> {
    cfg.reasoning_effort.map(|effort| {
        let effort = match effort {
            ReasoningEffort::Low => "low",
            ReasoningEffort::Medium => "medium",
            ReasoningEffort::High => "high",
        };
        serde_json::json!({ "effort": effort })
    })
}

/// Build an Anthropic-compatible thinking payload when configured.
///
/// Returns `Some({"type":"enabled","budget_tokens":N})` if thinking budget
/// is configured, otherwise returns `None`.
#[inline]
pub fn thinking_payload(cfg: &RemoteMultimodalConfig) -> Option<serde_json::Value> {
    cfg.thinking_budget.map(|budget| {
        serde_json::json!({
            "type": "enabled",
            "budget_tokens": budget
        })
    })
}

/// Check if a URL points to an Anthropic Messages API endpoint.
///
/// Uses a fast byte-level check — no allocations, no regex.
#[inline]
pub fn is_anthropic_endpoint(url: &str) -> bool {
    url.contains("api.anthropic.com")
}

/// Resolve the effective thinking budget for an Anthropic endpoint.
///
/// If `thinking_budget` is explicitly set, returns it directly.
/// Otherwise, auto-translates `reasoning_effort` to a sensible budget:
/// - Low → 4096
/// - Medium → 8192
/// - High → 16384
///
/// Returns `None` if neither is configured.
#[inline]
pub fn effective_thinking_budget(cfg: &RemoteMultimodalConfig) -> Option<u32> {
    if let Some(budget) = cfg.thinking_budget {
        return Some(budget);
    }
    cfg.reasoning_effort.map(|effort| match effort {
        ReasoningEffort::Low => 4096,
        ReasoningEffort::Medium => 8192,
        ReasoningEffort::High => 16384,
    })
}

/// Build the Anthropic thinking payload using effective budget resolution.
///
/// Auto-translates `reasoning_effort` → `thinking_budget` when the explicit
/// budget is not set, so users targeting Anthropic don't need extra config.
#[inline]
pub fn effective_thinking_payload(cfg: &RemoteMultimodalConfig) -> Option<serde_json::Value> {
    effective_thinking_budget(cfg).map(|budget| {
        serde_json::json!({
            "type": "enabled",
            "budget_tokens": budget
        })
    })
}

/// Check if a URL is allowed by the gate.
///
/// Returns:
/// - `true` if the URL is allowed (no gate, or gate allows the URL)
/// - `false` if the URL is blocked
pub fn is_url_allowed(gate: Option<&crate::PromptUrlGate>, url: &str) -> bool {
    match gate {
        Some(g) => g.is_allowed(url),
        None => true,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_automation_config() {
        let config = AutomationConfig::new("Login to dashboard")
            .with_max_steps(10)
            .with_timeout(60_000)
            .with_success_url("/dashboard")
            .with_extraction("Extract user info");

        assert_eq!(config.goal, "Login to dashboard");
        assert_eq!(config.max_steps, 10);
        assert!(config.extract_on_success);
        assert!(config.is_success_url("https://example.com/dashboard"));
    }

    #[test]
    fn test_retry_policy() {
        let policy = RetryPolicy::new(5).with_backoff(2000);

        assert_eq!(policy.max_attempts, 5);
        assert_eq!(policy.backoff_ms, 2000);
        assert_eq!(policy.backoff_duration(), Duration::from_millis(2000));
    }

    #[test]
    fn test_model_policy() {
        let policy = ModelPolicy::default();

        assert_eq!(policy.model_for_tier(CostTier::Low), "gpt-4o-mini");
        assert_eq!(policy.model_for_tier(CostTier::High), "gpt-4o");
    }

    #[test]
    fn test_remote_multimodal_config_defaults() {
        let cfg = RemoteMultimodalConfig::default();

        assert!(cfg.include_html);
        assert_eq!(cfg.html_max_bytes, 24_000);
        assert!(cfg.include_url);
        assert!(cfg.include_title);
        assert_eq!(cfg.temperature, 0.1);
        assert_eq!(cfg.max_tokens, 1024);
        assert!(cfg.request_json_object);
        assert!(cfg.best_effort_json_extract);
        assert!(cfg.reasoning_effort.is_none());
        assert_eq!(cfg.max_rounds, 6);
        assert!(cfg.screenshot);
        assert!(!cfg.extra_ai_data);
    }

    #[test]
    fn test_remote_multimodal_config_builder() {
        let cfg = RemoteMultimodalConfig::new()
            .with_html(false)
            .with_temperature(0.5)
            .with_reasoning_effort(Some(ReasoningEffort::High))
            .with_max_rounds(10)
            .with_extraction(true)
            .with_extraction_prompt("Extract products");

        assert!(!cfg.include_html);
        assert_eq!(cfg.temperature, 0.5);
        assert_eq!(cfg.reasoning_effort, Some(ReasoningEffort::High));
        assert_eq!(cfg.max_rounds, 10);
        assert!(cfg.extra_ai_data);
        assert_eq!(cfg.extraction_prompt, Some("Extract products".to_string()));
    }

    #[test]
    fn test_reasoning_payload_helper() {
        let cfg = RemoteMultimodalConfig::default();
        assert!(reasoning_payload(&cfg).is_none());

        let cfg =
            RemoteMultimodalConfig::default().with_reasoning_effort(Some(ReasoningEffort::Low));
        assert_eq!(
            reasoning_payload(&cfg),
            Some(serde_json::json!({ "effort": "low" }))
        );

        let cfg =
            RemoteMultimodalConfig::default().with_reasoning_effort(Some(ReasoningEffort::Medium));
        assert_eq!(
            reasoning_payload(&cfg),
            Some(serde_json::json!({ "effort": "medium" }))
        );

        let cfg =
            RemoteMultimodalConfig::default().with_reasoning_effort(Some(ReasoningEffort::High));
        assert_eq!(
            reasoning_payload(&cfg),
            Some(serde_json::json!({ "effort": "high" }))
        );
    }

    #[test]
    fn test_merged_config_includes_reasoning_effort() {
        let base =
            RemoteMultimodalConfig::default().with_reasoning_effort(Some(ReasoningEffort::Low));
        let override_cfg =
            RemoteMultimodalConfig::default().with_reasoning_effort(Some(ReasoningEffort::High));

        let merged = merged_config(&base, &override_cfg);
        assert_eq!(merged.reasoning_effort, Some(ReasoningEffort::High));
    }

    #[test]
    fn test_thinking_payload() {
        let cfg = RemoteMultimodalConfig::default();
        assert!(thinking_payload(&cfg).is_none());

        let cfg = RemoteMultimodalConfig::default().with_thinking_budget(Some(10000));
        assert_eq!(
            thinking_payload(&cfg),
            Some(serde_json::json!({"type": "enabled", "budget_tokens": 10000}))
        );
    }

    #[test]
    fn test_is_anthropic_endpoint() {
        assert!(is_anthropic_endpoint(
            "https://api.anthropic.com/v1/messages"
        ));
        assert!(!is_anthropic_endpoint(
            "https://api.openai.com/v1/chat/completions"
        ));
        assert!(!is_anthropic_endpoint(
            "https://openrouter.ai/api/v1/chat/completions"
        ));
    }

    #[test]
    fn test_merged_config_includes_thinking_budget() {
        let base = RemoteMultimodalConfig::default().with_thinking_budget(Some(5000));
        let override_cfg = RemoteMultimodalConfig::default().with_thinking_budget(Some(15000));

        let merged = merged_config(&base, &override_cfg);
        assert_eq!(merged.thinking_budget, Some(15000));
    }

    #[test]
    fn test_html_cleaning_profile_analysis() {
        use super::ContentAnalysis;

        // Test with high SVG bytes - should return Slim
        let analysis = ContentAnalysis {
            svg_bytes: 150_000, // > SVG_VERY_HEAVY_THRESHOLD
            ..Default::default()
        };
        assert_eq!(
            HtmlCleaningProfile::from_content_analysis(&analysis),
            HtmlCleaningProfile::Slim
        );

        // Test with small HTML - should return Minimal
        let analysis = ContentAnalysis {
            html_length: 5_000,
            text_ratio: 0.3,
            ..Default::default()
        };
        assert_eq!(
            HtmlCleaningProfile::from_content_analysis(&analysis),
            HtmlCleaningProfile::Minimal
        );
    }

    #[test]
    fn test_html_cleaning_profile_estimate_savings() {
        use super::ContentAnalysis;

        let analysis = ContentAnalysis {
            script_bytes: 10_000,
            style_bytes: 5_000,
            cleanable_bytes: 20_000,
            html_length: 50_000,
            ..Default::default()
        };

        assert_eq!(HtmlCleaningProfile::Raw.estimate_savings(&analysis), 0);
        assert_eq!(
            HtmlCleaningProfile::Minimal.estimate_savings(&analysis),
            15_000
        );
        assert_eq!(
            HtmlCleaningProfile::Slim.estimate_savings(&analysis),
            20_000
        );
    }

    #[test]
    fn test_supports_vision_openai() {
        // GPT-4o variants (all vision)
        assert!(supports_vision("gpt-4o"));
        assert!(supports_vision("gpt-4o-mini"));

        // GPT-4 Turbo with vision
        assert!(supports_vision("gpt-4-turbo"));

        // o1/o3 models
        assert!(supports_vision("o1"));
        assert!(supports_vision("o3"));

        // Non-vision models
        assert!(!supports_vision("gpt-3.5-turbo"));
    }

    #[test]
    fn test_supports_vision_anthropic() {
        // Claude 3+ are multimodal
        assert!(supports_vision("claude-3-sonnet-20240229"));
        assert!(supports_vision("claude-3-opus-20240229"));
        assert!(supports_vision("claude-3-haiku-20240307"));
        assert!(supports_vision("claude-3-5-sonnet-20241022"));

        // Claude 2 is not vision
        assert!(!supports_vision("claude-2"));
        assert!(!supports_vision("claude-2.1"));
        assert!(!supports_vision("claude-instant-1.2"));
    }

    #[test]
    fn test_supports_vision_qwen() {
        assert!(supports_vision("qwen2-vl-72b"));
        assert!(supports_vision("qwen2.5-vl-7b"));
        assert!(supports_vision("qwen-vl-max"));
        assert!(supports_vision("qwq-32b"));

        // Non-VL Qwen
        assert!(!supports_vision("qwen2-72b"));
        assert!(!supports_vision("qwen2.5-7b"));
    }

    #[test]
    fn test_supports_vision_gemini() {
        assert!(supports_vision("gemini-1.5-pro"));
        assert!(supports_vision("gemini-1.5-flash"));
        assert!(supports_vision("gemini-2.0-flash"));
        assert!(supports_vision("gemini-pro-vision"));
    }

    #[test]
    fn test_supports_vision_other() {
        // Models from OpenRouter's vision list
        assert!(supports_vision("pixtral-12b"));
        assert!(supports_vision("llama-3.2-11b-vision-instruct"));
        // internvl3-78b may or may not be listed depending on llm_models_spider version
        assert!(supports_vision("molmo-2-8b"));

        // Non-vision models
        assert!(!supports_vision("llama-3-70b-instruct"));
        assert!(!supports_vision("mistral-7b-instruct"));
        assert!(!supports_vision("mixtral-8x7b-instruct"));
    }

    #[test]
    fn test_supports_vision_case_insensitive() {
        assert!(supports_vision("GPT-4O"));
        assert!(supports_vision("Claude-3-Sonnet"));
        assert!(supports_vision("QWEN2-VL"));
    }

    #[test]
    fn test_is_extraction_only() {
        // Default config: extra_ai_data=false, max_rounds=6 → false
        let cfg = RemoteMultimodalConfig::default();
        assert!(!cfg.is_extraction_only());

        // Extraction enabled but multi-round → false
        let cfg = RemoteMultimodalConfig::new()
            .with_extraction(true)
            .with_max_rounds(6);
        assert!(!cfg.is_extraction_only());

        // Single round but no extraction → false
        let cfg = RemoteMultimodalConfig::new().with_max_rounds(1);
        assert!(!cfg.is_extraction_only());

        // Extraction + single round → true
        let cfg = RemoteMultimodalConfig::new()
            .with_extraction(true)
            .with_max_rounds(1);
        assert!(cfg.is_extraction_only());

        // Extraction + zero rounds → true
        let cfg = RemoteMultimodalConfig::new()
            .with_extraction(true)
            .with_max_rounds(0);
        assert!(cfg.is_extraction_only());
    }

    #[test]
    fn test_model_endpoint_new() {
        let ep = ModelEndpoint::new("gpt-4o-mini");
        assert_eq!(ep.model_name, "gpt-4o-mini");
        assert!(ep.api_url.is_none());
        assert!(ep.api_key.is_none());
    }

    #[test]
    fn test_model_endpoint_with_overrides() {
        let ep = ModelEndpoint::new("gpt-4o")
            .with_api_url("https://api.openai.com/v1/chat/completions")
            .with_api_key("sk-test");
        assert_eq!(ep.model_name, "gpt-4o");
        assert_eq!(
            ep.api_url.as_deref(),
            Some("https://api.openai.com/v1/chat/completions")
        );
        assert_eq!(ep.api_key.as_deref(), Some("sk-test"));
    }

    #[test]
    fn test_model_endpoint_serde_roundtrip() {
        let ep = ModelEndpoint::new("gpt-4o")
            .with_api_url("https://api.openai.com/v1/chat/completions")
            .with_api_key("sk-test");

        let json = serde_json::to_string(&ep).unwrap();
        let deserialized: ModelEndpoint = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.model_name, "gpt-4o");
        assert_eq!(
            deserialized.api_url.as_deref(),
            Some("https://api.openai.com/v1/chat/completions")
        );
        assert_eq!(deserialized.api_key.as_deref(), Some("sk-test"));
    }

    #[test]
    fn test_model_endpoint_serde_minimal() {
        // Only model_name, no optional fields
        let json = r#"{"model_name":"gpt-4o-mini"}"#;
        let ep: ModelEndpoint = serde_json::from_str(json).unwrap();

        assert_eq!(ep.model_name, "gpt-4o-mini");
        assert!(ep.api_url.is_none());
        assert!(ep.api_key.is_none());
    }

    #[test]
    fn test_vision_route_mode_serde() {
        // VisionRouteMode should serialize/deserialize properly
        let mode = VisionRouteMode::TextFirst;
        let json = serde_json::to_string(&mode).unwrap();
        let deserialized: VisionRouteMode = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, VisionRouteMode::TextFirst);

        let mode = VisionRouteMode::VisionFirst;
        let json = serde_json::to_string(&mode).unwrap();
        let deserialized: VisionRouteMode = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, VisionRouteMode::VisionFirst);
    }
}