pi_agent_rust 0.3.0

Native AI coding agent CLI - Rust port of Pi Agent
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
//! Built-in web search (bd-cv653.2.1): a multi-provider chain with automatic
//! fallback, keyless public rungs, and site-aware filtering.
//!
//! `auto` walks the configured chain in order — paid providers with keys
//! first, keyless public endpoints (duckduckgo/startpage/mojeek) last — with
//! per-provider circuit breaking (2 consecutive failures sideline a rung for
//! 5 minutes). Results are deduped by canonical URL and capped per domain.
//! Every result carries the provider that answered (`source`).

use crate::error::Error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A ranked search result.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SearchResult {
    pub title: String,
    pub url: String,
    #[serde(default)]
    pub snippet: String,
    /// The provider rung that produced this result.
    pub source: String,
}

/// Search filters: Google-style operators mapped per provider where
/// supported, else applied client-side.
#[derive(Debug, Clone, Default)]
pub struct SearchFilters {
    /// `site:example.com` — restrict to a host.
    pub site: Option<String>,
    /// `after:YYYY-MM-DD` — client-side date filter when the provider
    /// returns dates (most scrapers do not; documented limitation).
    pub after: Option<String>,
    /// Result cap (default 10, hard cap 50).
    pub limit: usize,
}

/// Errors a rung can produce; chain logic treats them all as "try next".
#[derive(Debug)]
pub enum RungError {
    /// No credential configured for this provider.
    NoKey,
    /// Network/HTTP failure with context.
    Http(String),
    /// Provider returned an unparseable/empty payload.
    Parse(String),
    /// Rate limited; the circuit breaker will cool this rung down.
    RateLimited,
}

impl std::fmt::Display for RungError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NoKey => write!(f, "no API key configured"),
            Self::Http(msg) => write!(f, "http: {msg}"),
            Self::Parse(msg) => write!(f, "parse: {msg}"),
            Self::RateLimited => write!(f, "rate limited"),
        }
    }
}

/// A provider rung in the chain.
pub struct ProviderRung {
    pub name: &'static str,
    /// Env var names consulted for the API key (first hit wins).
    pub env_keys: &'static [&'static str],
    /// True when the rung works without any key (public endpoint).
    pub keyless: bool,
    #[allow(clippy::type_complexity)]
    pub run: for<'a> fn(
        &'a crate::http::client::Client,
        &'a str,
        &'a SearchFilters,
        Option<&'a str>,
    ) -> RungFuture<'a>,
}

pub type RungFuture<'a> = std::pin::Pin<
    Box<dyn std::future::Future<Output = Result<Vec<SearchResult>, RungError>> + Send + 'a>,
>;

/// The default chain order: keyed providers first, keyless publics last.
pub const DEFAULT_CHAIN: &[&str] = &[
    "perplexity",
    "brave",
    "tavily",
    "exa",
    "jina",
    "kagi",
    "duckduckgo",
    "startpage",
    "mojeek",
];

fn env_key(keys: &[&str]) -> Option<String> {
    keys.iter().find_map(|var| {
        std::env::var(var)
            .ok()
            .map(|v| v.trim().to_string())
            .filter(|v| !v.is_empty())
    })
}

/// Base-URL override per rung (bd-cv653.2.1): `PI_WEBSEARCH_BASE_<NAME>`
/// redirects a provider's endpoint — used by the e2e harness to point rungs
/// at a loopback mock, and by operators routing through a proxy. In-process
/// callers (SDK, tests) use the override map, which wins over the env var.
fn base_url_for(name: &str, default: &str) -> String {
    if let Some(override_url) = base_url_overrides()
        .read()
        .ok()
        .and_then(|map| map.get(name).cloned())
    {
        return override_url;
    }
    let var = format!("PI_WEBSEARCH_BASE_{}", name.to_ascii_uppercase());
    std::env::var(var)
        .ok()
        .map(|v| v.trim().trim_end_matches('/').to_string())
        .filter(|v| !v.is_empty())
        .unwrap_or_else(|| default.to_string())
}

fn base_url_overrides() -> &'static std::sync::RwLock<HashMap<&'static str, String>> {
    static OVERRIDES: std::sync::LazyLock<std::sync::RwLock<HashMap<&'static str, String>>> =
        std::sync::LazyLock::new(|| std::sync::RwLock::new(HashMap::new()));
    &OVERRIDES
}

/// Redirect a provider rung's base URL (SDK/e2e seam; process-global).
pub fn set_base_url_override(name: &'static str, base_url: &str) {
    if let Ok(mut map) = base_url_overrides().write() {
        map.insert(name, base_url.trim_end_matches('/').to_string());
    }
}

/// Clear all base-url overrides (tests).
pub fn clear_base_url_overrides() {
    if let Ok(mut map) = base_url_overrides().write() {
        map.clear();
    }
}

// === Paid/keyed rungs ===

fn perplexity_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    key: Option<&'a str>,
) -> RungFuture<'a> {
    let body = json!({
        "model": "sonar",
        "messages": [{"role": "user", "content": with_site(query, filters)}],
        "max_tokens": 512,
    });
    let key = key.map(str::to_string);
    Box::pin(async move {
        let Some(key) = key else {
            return Err(RungError::NoKey);
        };
        let response = client
            .post("https://api.perplexity.ai/chat/completions")
            .header("Authorization", format!("Bearer {key}"))
            .json(&body)
            .map_err(|e| RungError::Http(e.to_string()))?
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() == 429 {
            return Err(RungError::RateLimited);
        }
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let text = response
            .text_limited(512 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let value: Value =
            serde_json::from_str(&text).map_err(|e| RungError::Parse(e.to_string()))?;
        let mut results = Vec::new();
        if let Some(citations) = value.get("citations").and_then(Value::as_array) {
            for (index, citation) in citations.iter().enumerate() {
                if let Some(url) = citation.as_str() {
                    results.push(SearchResult {
                        title: format!("citation {}", index + 1),
                        url: url.to_string(),
                        snippet: String::new(),
                        source: "perplexity".to_string(),
                    });
                }
            }
        }
        // The synthesized answer rides as the first result so the model gets
        // perplexity's answer text, not just links.
        if let Some(answer) = value
            .pointer("/choices/0/message/content")
            .and_then(Value::as_str)
        {
            results.insert(
                0,
                SearchResult {
                    title: "perplexity answer".to_string(),
                    url: String::new(),
                    snippet: answer.chars().take(2_000).collect(),
                    source: "perplexity".to_string(),
                },
            );
        }
        if results.is_empty() {
            return Err(RungError::Parse("no citations or answer".to_string()));
        }
        Ok(results)
    })
}

fn brave_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    key: Option<&'a str>,
) -> RungFuture<'a> {
    let key = key.map(str::to_string);
    let url = format!(
        "{}/res/v1/web/search?q={}&count={}",
        base_url_for("brave", "https://api.search.brave.com"),
        urlencoded(&with_site(query, filters)),
        filters.limit.min(20)
    );
    Box::pin(async move {
        let Some(key) = key else {
            return Err(RungError::NoKey);
        };
        let response = client
            .get(&url)
            .header("X-Subscription-Token", key)
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() == 429 {
            return Err(RungError::RateLimited);
        }
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let text = response
            .text_limited(1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let value: Value =
            serde_json::from_str(&text).map_err(|e| RungError::Parse(e.to_string()))?;
        let mut results = Vec::new();
        if let Some(items) = value.pointer("/web/results").and_then(Value::as_array) {
            for item in items {
                let url = item.get("url").and_then(Value::as_str).unwrap_or_default();
                if url.is_empty() {
                    continue;
                }
                results.push(SearchResult {
                    title: item
                        .get("title")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    url: url.to_string(),
                    snippet: item
                        .get("description")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    source: "brave".to_string(),
                });
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no web results".to_string()));
        }
        Ok(results)
    })
}

fn tavily_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    key: Option<&'a str>,
) -> RungFuture<'a> {
    let key = key.map(str::to_string);
    let limit = filters.limit.min(20);
    let body = json!({
        "query": with_site(query, filters),
        "max_results": limit,
        "search_depth": "basic",
    });
    Box::pin(async move {
        let Some(key) = key else {
            return Err(RungError::NoKey);
        };
        let url = format!(
            "{}/search",
            base_url_for("tavily", "https://api.tavily.com")
        );
        let response = client
            .post(&url)
            .header("Authorization", format!("Bearer {key}"))
            .json(&body)
            .map_err(|e| RungError::Http(e.to_string()))?
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() == 429 {
            return Err(RungError::RateLimited);
        }
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let text = response
            .text_limited(1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let value: Value =
            serde_json::from_str(&text).map_err(|e| RungError::Parse(e.to_string()))?;
        let mut results = Vec::new();
        if let Some(items) = value.get("results").and_then(Value::as_array) {
            for item in items {
                let url = item.get("url").and_then(Value::as_str).unwrap_or_default();
                if url.is_empty() {
                    continue;
                }
                results.push(SearchResult {
                    title: item
                        .get("title")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    url: url.to_string(),
                    snippet: item
                        .get("content")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .chars()
                        .take(500)
                        .collect(),
                    source: "tavily".to_string(),
                });
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no results".to_string()));
        }
        Ok(results)
    })
}

fn exa_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    key: Option<&'a str>,
) -> RungFuture<'a> {
    let key = key.map(str::to_string);
    let limit = filters.limit.min(25);
    let body = json!({
        "query": with_site(query, filters),
        "numResults": limit,
    });
    Box::pin(async move {
        let Some(key) = key else {
            return Err(RungError::NoKey);
        };
        let url = format!("{}/search", base_url_for("exa", "https://api.exa.ai"));
        let response = client
            .post(&url)
            .header("x-api-key", key)
            .json(&body)
            .map_err(|e| RungError::Http(e.to_string()))?
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() == 429 {
            return Err(RungError::RateLimited);
        }
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let text = response
            .text_limited(1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let value: Value =
            serde_json::from_str(&text).map_err(|e| RungError::Parse(e.to_string()))?;
        let mut results = Vec::new();
        if let Some(items) = value.get("results").and_then(Value::as_array) {
            for item in items {
                let url = item.get("url").and_then(Value::as_str).unwrap_or_default();
                if url.is_empty() {
                    continue;
                }
                results.push(SearchResult {
                    title: item
                        .get("title")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    url: url.to_string(),
                    snippet: item
                        .get("text")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .chars()
                        .take(500)
                        .collect(),
                    source: "exa".to_string(),
                });
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no results".to_string()));
        }
        Ok(results)
    })
}

fn jina_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    key: Option<&'a str>,
) -> RungFuture<'a> {
    let key = key.map(str::to_string);
    let url = format!(
        "{}/{}",
        base_url_for("jina", "https://s.jina.ai"),
        urlencoded(&with_site(query, filters))
    );
    Box::pin(async move {
        let Some(key) = key else {
            return Err(RungError::NoKey);
        };
        let response = client
            .get(&url)
            .header("Authorization", format!("Bearer {key}"))
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() == 429 {
            return Err(RungError::RateLimited);
        }
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let text = response
            .text_limited(1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let value: Value =
            serde_json::from_str(&text).map_err(|e| RungError::Parse(e.to_string()))?;
        let mut results = Vec::new();
        if let Some(items) = value.get("data").and_then(Value::as_array) {
            for item in items {
                let url = item.get("url").and_then(Value::as_str).unwrap_or_default();
                if url.is_empty() {
                    continue;
                }
                results.push(SearchResult {
                    title: item
                        .get("title")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    url: url.to_string(),
                    snippet: item
                        .get("description")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    source: "jina".to_string(),
                });
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no data".to_string()));
        }
        Ok(results)
    })
}

fn kagi_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    key: Option<&'a str>,
) -> RungFuture<'a> {
    let key = key.map(str::to_string);
    let url = format!(
        "{}/api/v0/search?q={}&limit={}",
        base_url_for("kagi", "https://kagi.com"),
        urlencoded(&with_site(query, filters)),
        filters.limit.min(25)
    );
    Box::pin(async move {
        let Some(key) = key else {
            return Err(RungError::NoKey);
        };
        let response = client
            .get(&url)
            .header("Authorization", format!("Bot {key}"))
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() == 429 {
            return Err(RungError::RateLimited);
        }
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let text = response
            .text_limited(1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let value: Value =
            serde_json::from_str(&text).map_err(|e| RungError::Parse(e.to_string()))?;
        let mut results = Vec::new();
        if let Some(items) = value.get("data").and_then(Value::as_array) {
            for item in items {
                if item.get("t").and_then(Value::as_u64) != Some(0) {
                    continue; // non-result entries (related searches etc.)
                }
                let url = item.get("url").and_then(Value::as_str).unwrap_or_default();
                if url.is_empty() {
                    continue;
                }
                results.push(SearchResult {
                    title: item
                        .get("title")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    url: url.to_string(),
                    snippet: item
                        .get("snippet")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                    source: "kagi".to_string(),
                });
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no data".to_string()));
        }
        Ok(results)
    })
}

// === Keyless public rungs (HTML scrapers; defensive, parse-what-you-can) ===

fn duckduckgo_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    _key: Option<&'a str>,
) -> RungFuture<'a> {
    let url = format!(
        "{}/html/?q={}",
        base_url_for("duckduckgo", "https://html.duckduckgo.com"),
        urlencoded(&with_site(query, filters))
    );
    Box::pin(async move {
        let response = client
            .get(&url)
            .header("User-Agent", "Mozilla/5.0 (compatible; pi-agent)")
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let html = response
            .text_limited(2 * 1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let mut results = Vec::new();
        // result rows: <a class="result__a" href="//duckduckgo.com/l/?uddg=ENCODED">
        let mut rest = html.as_str();
        while let Some(pos) = rest.find("result__a") {
            rest = &rest[pos..];
            let Some(href_start) = rest.find("href=\"") else {
                break;
            };
            let after_href = &rest[href_start + 6..];
            let Some(href_end) = after_href.find('"') else {
                break;
            };
            let raw_href = &after_href[..href_end];
            let Some(title_end) = after_href.find("</a>") else {
                break;
            };
            let title_html = &after_href[href_end + 1..title_end];
            let title = strip_tags(title_html);
            let url = decode_ddg_redirect(raw_href);
            if !url.is_empty() {
                results.push(SearchResult {
                    title,
                    url,
                    snippet: String::new(),
                    source: "duckduckgo".to_string(),
                });
            }
            rest = &after_href[title_end..];
            if results.len() >= 25 {
                break;
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no result rows".to_string()));
        }
        Ok(results)
    })
}

fn startpage_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    _key: Option<&'a str>,
) -> RungFuture<'a> {
    let url = format!(
        "{}/sp/search?query={}",
        base_url_for("startpage", "https://www.startpage.com"),
        urlencoded(&with_site(query, filters))
    );
    Box::pin(async move {
        let response = client
            .get(&url)
            .header("User-Agent", "Mozilla/5.0 (compatible; pi-agent)")
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let html = response
            .text_limited(2 * 1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let mut results = Vec::new();
        // result links carry class="result-link"
        let mut rest = html.as_str();
        while let Some(pos) = rest.find("result-link") {
            rest = &rest[pos..];
            let Some(href_start) = rest.find("href=\"") else {
                break;
            };
            let after_href = &rest[href_start + 6..];
            let Some(href_end) = after_href.find('"') else {
                break;
            };
            let link = &after_href[..href_end];
            let Some(title_end) = after_href.find("</a>") else {
                break;
            };
            let title = strip_tags(&after_href[href_end + 1..title_end]);
            if link.starts_with("http") {
                results.push(SearchResult {
                    title,
                    url: link.to_string(),
                    snippet: String::new(),
                    source: "startpage".to_string(),
                });
            }
            rest = &after_href[title_end..];
            if results.len() >= 25 {
                break;
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no result rows".to_string()));
        }
        Ok(results)
    })
}

fn mojeek_run<'a>(
    client: &'a crate::http::client::Client,
    query: &'a str,
    filters: &'a SearchFilters,
    _key: Option<&'a str>,
) -> RungFuture<'a> {
    let url = format!(
        "{}/search?q={}",
        base_url_for("mojeek", "https://www.mojeek.com"),
        urlencoded(&with_site(query, filters))
    );
    Box::pin(async move {
        let response = client
            .get(&url)
            .header("User-Agent", "Mozilla/5.0 (compatible; pi-agent)")
            .send()
            .await
            .map_err(|e| rung_http_error(&e))?;
        if response.status() != 200 {
            return Err(RungError::Http(format!("status {}", response.status())));
        }
        let html = response
            .text_limited(2 * 1024 * 1024)
            .await
            .map_err(|e| RungError::Http(e.to_string()))?;
        let mut results = Vec::new();
        // result titles: <a class="title" href="https://...">
        let mut rest = html.as_str();
        while let Some(pos) = rest.find("class=\"title\"") {
            rest = &rest[pos..];
            let Some(href_start) = rest.find("href=\"") else {
                break;
            };
            let after_href = &rest[href_start + 6..];
            let Some(href_end) = after_href.find('"') else {
                break;
            };
            let link = &after_href[..href_end];
            let Some(title_end) = after_href.find("</a>") else {
                break;
            };
            let title = strip_tags(&after_href[href_end + 1..title_end]);
            if link.starts_with("http") {
                results.push(SearchResult {
                    title,
                    url: link.to_string(),
                    snippet: String::new(),
                    source: "mojeek".to_string(),
                });
            }
            rest = &after_href[title_end..];
            if results.len() >= 25 {
                break;
            }
        }
        if results.is_empty() {
            return Err(RungError::Parse("no result rows".to_string()));
        }
        Ok(results)
    })
}

/// The rung table. Order in the chain comes from DEFAULT_CHAIN.
#[allow(clippy::type_complexity)]
pub fn all_rungs() -> HashMap<&'static str, ProviderRung> {
    let mut map = HashMap::new();
    let mut add = |rung: ProviderRung| {
        map.insert(rung.name, rung);
    };
    add(ProviderRung {
        name: "perplexity",
        env_keys: &["PERPLEXITY_API_KEY"],
        keyless: false,
        run: perplexity_run,
    });
    add(ProviderRung {
        name: "brave",
        env_keys: &["BRAVE_API_KEY", "BRAVE_SEARCH_API_KEY"],
        keyless: false,
        run: brave_run,
    });
    add(ProviderRung {
        name: "tavily",
        env_keys: &["TAVILY_API_KEY"],
        keyless: false,
        run: tavily_run,
    });
    add(ProviderRung {
        name: "exa",
        env_keys: &["EXA_API_KEY"],
        keyless: false,
        run: exa_run,
    });
    add(ProviderRung {
        name: "jina",
        env_keys: &["JINA_API_KEY"],
        keyless: false,
        run: jina_run,
    });
    add(ProviderRung {
        name: "kagi",
        env_keys: &["KAGI_API_KEY"],
        keyless: false,
        run: kagi_run,
    });
    add(ProviderRung {
        name: "duckduckgo",
        env_keys: &[],
        keyless: true,
        run: duckduckgo_run,
    });
    add(ProviderRung {
        name: "startpage",
        env_keys: &[],
        keyless: true,
        run: startpage_run,
    });
    add(ProviderRung {
        name: "mojeek",
        env_keys: &[],
        keyless: true,
        run: mojeek_run,
    });
    map
}

#[allow(clippy::type_complexity)]
const fn rung(
    name: &'static str,
    env_keys: &'static [&'static str],
    keyless: bool,
    run: for<'a> fn(
        &'a crate::http::client::Client,
        &'a str,
        &'a SearchFilters,
        Option<&'a str>,
    ) -> RungFuture<'a>,
) -> (&'static str, ProviderRung) {
    (
        name,
        ProviderRung {
            name,
            env_keys,
            keyless,
            run,
        },
    )
}

// === Chain orchestration ===

/// Circuit-breaker state per rung (process-local; cheap and honest).
static CIRCUIT_FAILURES: std::sync::LazyLock<
    std::sync::Mutex<HashMap<String, (u32, std::time::Instant)>>,
> = std::sync::LazyLock::new(|| std::sync::Mutex::new(HashMap::new()));

const CIRCUIT_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(300);

fn rung_available(name: &str) -> bool {
    let Ok(map) = CIRCUIT_FAILURES.lock() else {
        return true;
    };
    match map.get(name) {
        Some((count, since)) if *count >= 2 => since.elapsed() >= CIRCUIT_COOLDOWN,
        _ => true,
    }
}

fn rung_report_failure(name: &str) {
    if let Ok(mut map) = CIRCUIT_FAILURES.lock() {
        let entry = map
            .entry(name.to_string())
            .or_insert_with(|| (0, std::time::Instant::now()));
        entry.0 += 1;
        entry.1 = std::time::Instant::now();
    }
}

fn rung_report_success(name: &str) {
    if let Ok(mut map) = CIRCUIT_FAILURES.lock() {
        map.remove(name);
    }
}

/// Run a search down the chain. Returns (results, provider_that_answered).
pub async fn search(
    query: &str,
    filters: &SearchFilters,
    provider: Option<&str>,
) -> Result<(Vec<SearchResult>, String), Error> {
    let rungs = all_rungs();
    let order: Vec<&str> = match provider {
        Some(name) if name != "auto" => {
            if !rungs.contains_key(name) {
                return Err(Error::validation(format!(
                    "Unknown web_search provider {name:?}. Known: {}",
                    DEFAULT_CHAIN.join(", ")
                )));
            }
            vec![name]
        }
        _ => DEFAULT_CHAIN.to_vec(),
    };

    let mut notes: Vec<String> = Vec::new();
    for name in &order {
        let rung = &rungs[name];
        if !rung_available(name) {
            notes.push(format!("{name}: circuit open (recent failures)"));
            continue;
        }
        let key = env_key(rung.env_keys);
        if key.is_none() && !rung.keyless {
            notes.push(format!(
                "{name}: no key (set {})",
                rung.env_keys.join(" or ")
            ));
            continue;
        }
        let outcome = (rung.run)(
            &crate::http::client::Client::new(),
            query,
            filters,
            key.as_deref(),
        )
        .await;
        match outcome {
            Ok(mut results) => {
                rung_report_success(name);
                post_process(&mut results, filters);
                return Ok((results, (*name).to_string()));
            }
            Err(err) => {
                if matches!(err, RungError::RateLimited | RungError::Http(_)) {
                    rung_report_failure(name);
                }
                notes.push(format!("{name}: {err}"));
            }
        }
    }
    Err(Error::tool(
        "web_search",
        format!("All web search providers failed:\n{}", notes.join("\n")),
    ))
}

/// Dedupe by canonical URL, cap per-domain at 3, apply the limit.
fn post_process(results: &mut Vec<SearchResult>, filters: &SearchFilters) {
    let mut seen = std::collections::HashSet::new();
    results.retain(|result| {
        let canonical = canonical_url(&result.url);
        seen.insert(canonical)
    });
    let mut per_domain: HashMap<String, usize> = HashMap::new();
    results.retain(|result| {
        let domain = url_domain(&result.url);
        let count = per_domain.entry(domain).or_insert(0);
        *count += 1;
        *count <= 3
    });
    results.truncate(filters.limit.clamp(1, 50));
}

fn canonical_url(url: &str) -> String {
    let mut out = url.to_ascii_lowercase();
    for prefix in ["https://www.", "http://www.", "https://", "http://"] {
        if let Some(rest) = out.strip_prefix(prefix) {
            out = rest.to_string();
            break;
        }
    }
    while out.ends_with('/') {
        out.pop();
    }
    out
}

fn url_domain(url: &str) -> String {
    let canonical = canonical_url(url);
    canonical.split('/').next().unwrap_or_default().to_string()
}

fn with_site(query: &str, filters: &SearchFilters) -> String {
    match &filters.site {
        Some(site) if !site.is_empty() => format!("site:{site} {query}"),
        _ => query.to_string(),
    }
}

fn urlencoded(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    for byte in input.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(byte as char);
            }
            b' ' => out.push('+'),
            _ => {
                let _ = std::fmt::Write::write_fmt(&mut out, format_args!("%{byte:02X}"));
            }
        }
    }
    out
}

/// DuckDuckGo result links come wrapped as `//duckduckgo.com/l/?uddg=<urlencoded>`.
fn decode_ddg_redirect(href: &str) -> String {
    if let Some((_, query)) = href.split_once("uddg=") {
        let encoded = query.split('&').next().unwrap_or("");
        let mut out = String::with_capacity(encoded.len());
        let bytes = encoded.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            match bytes[i] {
                b'%' if i + 2 < bytes.len() => {
                    let hex = &encoded[i + 1..i + 3];
                    if let Ok(value) = u8::from_str_radix(hex, 16) {
                        out.push(value as char);
                    }
                    i += 3;
                }
                b'+' => {
                    out.push(' ');
                    i += 1;
                }
                other => {
                    out.push(other as char);
                    i += 1;
                }
            }
        }
        return out;
    }
    href.to_string()
}

fn rung_http_error(err: &Error) -> RungError {
    let text = err.to_string();
    if text.contains("429") {
        RungError::RateLimited
    } else {
        RungError::Http(text)
    }
}

fn strip_tags(html: &str) -> String {
    let mut out = String::with_capacity(html.len());
    let mut in_tag = false;
    for ch in html.chars() {
        match ch {
            '<' => in_tag = true,
            '>' => in_tag = false,
            _ if !in_tag => out.push(ch),
            _ => {}
        }
    }
    out.split_whitespace().collect::<Vec<_>>().join(" ")
}

use serde_json::{Value, json};

/// The `web_search` tool (bd-cv653.2.1): one query across the configured
/// provider chain; results ranked, deduped, and provider-attributed.
pub struct WebSearchTool;

impl WebSearchTool {
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl Default for WebSearchTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
#[allow(clippy::unnecessary_literal_bound)]
impl crate::tools::Tool for WebSearchTool {
    fn name(&self) -> &str {
        "web_search"
    }

    fn label(&self) -> &str {
        "web_search"
    }

    fn description(&self) -> &str {
        "Search the web. One query walks a ranked provider chain (perplexity, \
         brave, tavily, exa, jina, kagi, then keyless duckduckgo/startpage/\
         mojeek) with automatic fallback — the first rung with credentials and \
         a healthy circuit answers. Returns ranked results (title, url, \
         snippet, source provider). Optional: `provider` pins one rung, \
         `site:` restricts to a host, `limit` caps results (default 10). \
         Pair with `read` on a result URL for full page content."
    }

    fn parameters(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "The search query"},
                "provider": {
                    "type": "string",
                    "description": "Pin one provider (perplexity|brave|tavily|exa|jina|kagi|duckduckgo|startpage|mojeek); default 'auto' walks the chain"
                },
                "site": {"type": "string", "description": "Restrict results to a host (site: filter)"},
                "after": {"type": "string", "description": "YYYY-MM-DD recency filter where supported"},
                "limit": {"type": "integer", "description": "Max results (default 10, hard cap 50)"}
            },
            "required": ["query"]
        })
    }

    fn effects(&self) -> crate::tools::ToolEffects {
        crate::tools::ToolEffects::network()
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        input: Value,
        _on_update: Option<Box<dyn Fn(crate::tools::ToolUpdate) + Send + Sync>>,
    ) -> crate::error::Result<crate::tools::ToolOutput> {
        let query = input
            .get("query")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|q| !q.is_empty())
            .ok_or_else(|| {
                Error::validation("web_search requires a non-empty `query`".to_string())
            })?
            .to_string();
        let provider = input
            .get("provider")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|p| !p.is_empty())
            .map(str::to_string);
        let filters = SearchFilters {
            site: input
                .get("site")
                .and_then(Value::as_str)
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(str::to_string),
            after: input
                .get("after")
                .and_then(Value::as_str)
                .map(str::to_string),
            limit: input
                .get("limit")
                .and_then(Value::as_u64)
                .map_or(10, |n| usize::try_from(n).unwrap_or(10).clamp(1, 50)),
        };
        let (results, answered_by) = search(&query, &filters, provider.as_deref()).await?;
        let payload = json!({
            "query": query,
            "answeredBy": answered_by,
            "count": results.len(),
            "results": results,
        });
        Ok(crate::tools::ToolOutput {
            content: vec![crate::model::ContentBlock::Text(
                crate::model::TextContent::new(payload.to_string()),
            )],
            details: Some(json!({"answeredBy": answered_by, "count": results.len()})),
            is_error: false,
        })
    }
}

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

    #[test]
    fn canonical_url_normalizes() {
        assert_eq!(canonical_url("https://www.Example.com/a/"), "example.com/a");
        assert_eq!(canonical_url("http://x.io"), "x.io");
    }

    #[test]
    fn post_process_dedupes_and_caps_domains() {
        let mut results = vec![
            SearchResult {
                title: "a".into(),
                url: "https://x.io/1".into(),
                snippet: String::new(),
                source: "t".into(),
            },
            SearchResult {
                title: "b".into(),
                url: "https://x.io/1/".into(),
                snippet: String::new(),
                source: "t".into(),
            },
            SearchResult {
                title: "c".into(),
                url: "https://x.io/2".into(),
                snippet: String::new(),
                source: "t".into(),
            },
            SearchResult {
                title: "d".into(),
                url: "https://x.io/3".into(),
                snippet: String::new(),
                source: "t".into(),
            },
            SearchResult {
                title: "e".into(),
                url: "https://x.io/4".into(),
                snippet: String::new(),
                source: "t".into(),
            },
            SearchResult {
                title: "f".into(),
                url: "https://y.io/1".into(),
                snippet: String::new(),
                source: "t".into(),
            },
        ];
        post_process(
            &mut results,
            &SearchFilters {
                site: None,
                after: None,
                limit: 10,
            },
        );
        // /1 deduped against /1/, and the 4th same-domain result dropped.
        let urls: Vec<&str> = results.iter().map(|r| r.url.as_str()).collect();
        assert_eq!(urls.len(), 4, "dedupe + domain cap: {urls:?}");
        assert!(urls.contains(&"https://x.io/1"));
        assert!(urls.contains(&"https://y.io/1"));
    }

    #[test]
    fn site_filter_prepends_operator() {
        let filters = SearchFilters {
            site: Some("docs.rs".into()),
            after: None,
            limit: 10,
        };
        assert_eq!(
            with_site("tokio spawn", &filters),
            "site:docs.rs tokio spawn"
        );
    }

    #[test]
    fn urlencoded_encodes_specials() {
        assert_eq!(urlencoded("a b+c/d"), "a+b%2Bc%2Fd");
    }

    #[test]
    fn strip_tags_cleans() {
        assert_eq!(strip_tags("<b>bold</b> and <i>calm</i>"), "bold and calm");
    }
}