prux 1.3.0

A minimal terminal coding agent: TUI, built-in coding tools, and an extension system
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
//! Web 搜索 provider 路由与实现。
//!
//! 本阶段实现 8 个纯 REST provider:`tavily` `brave` `exa`
//! `jina` `searxng` `duckduckgo`(免密钥)`serper` `perplexity`。
//!
//! - `provider` 省略 / `auto` → 用配置的默认 provider,否则按偏好顺序取第一个可用;
//!   若所有需密钥的 provider 都不可用,则兜底到 `duckduckgo`;
//! - `provider: "all"` → 所有「可自动选中」的 provider 并发搜索后合并;
//!   显式专用 provider(本阶段为 `duckduckgo`)不参与 `all`;
//! - `provider: [a, b]` → 并发搜索指定 provider 并合并;
//! - 单个 provider 失败时按 `searchRouting.fallbackOn` / `searchRouting.providers`
//!   顺序回退(未配置回退时直接返回错误)。

use super::{
    super::util::default_user_agent,
    config::{self, Config, LoadedConfig},
};
use crate::{core::tools::ToolError, utils::http};
use futures_util::StreamExt;
use regex::Regex;
use serde_json::Value;
use std::{collections::HashSet, sync::OnceLock, time::Duration};
use strum::IntoEnumIterator;
use strum_macros::{EnumIter, EnumString, IntoStaticStr};

/// 单次搜索的选项
#[derive(Debug, Clone, Default)]
pub struct SearchOptions {
    /// 期望结果条数上限;`None` 时用 provider 默认值(当前为 5,内部限制在 1-20)
    pub num_results: Option<usize>,
    /// 时间范围过滤(如 `day` / `week` / `month` / `year`);`None` 表示不限
    pub recency: Option<String>,
    /// 域名过滤:`-` 前缀表示排除,其余为包含
    pub domain_filter: Vec<String>,
}

/// 统一搜索结果条目
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SearchResult {
    /// 结果标题
    pub title: String,
    /// 结果链接(绝对 URL)
    pub url: String,
    /// 结果摘要片段
    pub snippet: String,
}

/// 统一搜索响应
#[derive(Debug, Clone)]
pub struct SearchResponse {
    /// provider 给出的聚合答案;多数 provider 为空
    pub answer: String,
    /// 合并后的搜索结果列表
    pub results: Vec<SearchResult>,
    /// 产出该响应的 provider 名称
    pub provider: String,
}

impl SearchResponse {
    /// 组装统一搜索响应;`provider` 记录产出者名称(并发聚合时为 `a+b`)。
    fn new(provider: &str, answer: String, results: Vec<SearchResult>) -> Self {
        SearchResponse {
            answer,
            results,
            provider: provider.to_string(),
        }
    }
}

/// 支持的 provider
///
/// 判别值即自动路由的偏好顺序(越小越优先),见 [`Provider::preference_rank`];
/// 变体名与字符串的互转由 `strum` 派生(全小写、大小写不敏感),成员遍历由 [`Provider::iter`] 提供。
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, EnumString, IntoStaticStr)]
#[strum(serialize_all = "lowercase", ascii_case_insensitive)]
pub enum Provider {
    /// 自建 SearXNG(需 base URL)
    Searxng = 0,
    /// Exa(需 API key)
    Exa = 1,
    /// Brave Search(需 API key)
    Brave = 2,
    /// Tavily(需 API key)
    Tavily = 3,
    /// Serper(需 API key)
    Serper = 4,
    /// Jina Reader Search(需 API key)
    Jina = 5,
    /// Perplexity(需 API key)
    Perplexity = 6,
    /// DuckDuckGo HTML 端(免密钥,仅显式指定时使用)
    #[strum(serialize = "duckduckgo", serialize = "ddg")]
    DuckDuckGo = 7,
}

impl Provider {
    /// 解析 provider 名称(去首尾空白、大小写不敏感)
    pub fn parse(name: &str) -> Option<Provider> {
        name.trim().parse().ok()
    }

    /// 规范名称(`&'static str`)
    pub fn name(&self) -> &'static str {
        self.into()
    }

    /// 是否可自动选中(`auto` 路由与 `all` 聚合)。DuckDuckGo 为显式专用。
    pub fn auto_selectable(&self) -> bool {
        !matches!(self, Provider::DuckDuckGo)
    }

    /// 自动路由偏好顺序(越小越优先),由变体判别值决定
    pub fn preference_rank(&self) -> usize {
        *self as usize
    }

    /// 当前配置/环境下是否可用
    pub fn is_available(&self, cfg: &Config) -> bool {
        match self {
            Provider::DuckDuckGo => true,
            Provider::Tavily => {
                config::resolve_credential(cfg.tavily_api_key.as_deref(), "TAVILY_API_KEY")
                    .is_some()
            }
            Provider::Brave => {
                config::resolve_credential(cfg.brave_api_key.as_deref(), "BRAVE_API_KEY").is_some()
            }
            Provider::Exa => {
                config::resolve_credential(cfg.exa_api_key.as_deref(), "EXA_API_KEY").is_some()
            }
            Provider::Jina => {
                config::resolve_credential(cfg.jina_api_key.as_deref(), "JINA_API_KEY").is_some()
            }
            Provider::Searxng => cfg
                .searxng_base_url
                .as_deref()
                .map(|s| !s.trim().is_empty())
                .unwrap_or(false),
            Provider::Serper => {
                config::resolve_credential(cfg.serper_api_key.as_deref(), "SERPER_API_KEY")
                    .is_some()
            }
            Provider::Perplexity => {
                config::resolve_credential(cfg.perplexity_api_key.as_deref(), "PERPLEXITY_API_KEY")
                    .is_some()
            }
        }
    }
}

/// 工具传入的 provider 选择
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProviderSelection {
    /// 未指定 / `auto`:用默认 provider,否则按偏好顺序取首个可用
    Auto,
    /// 指定单个 provider(名称,解析见 [`Provider::parse`])
    Single(String),
    /// `all`:所有可自动选中的 provider 并发搜索后合并
    All,
    /// 并发搜索指定的一组 provider 后合并
    List(Vec<String>),
}

impl ProviderSelection {
    /// 从 JSON 参数解析 `provider`(字符串或字符串数组)。
    pub fn from_json(value: Option<&Value>) -> ProviderSelection {
        match value {
            None | Some(Value::Null) => ProviderSelection::Auto,
            Some(Value::String(s)) => {
                let s = s.trim();
                if s.is_empty() || s.eq_ignore_ascii_case("auto") {
                    ProviderSelection::Auto
                } else if s.eq_ignore_ascii_case("all") {
                    ProviderSelection::All
                } else {
                    ProviderSelection::Single(s.to_string())
                }
            }
            Some(Value::Array(items)) => {
                let list: Vec<String> = items
                    .iter()
                    .filter_map(|v| v.as_str())
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect();
                if list.is_empty() {
                    ProviderSelection::Auto
                } else {
                    ProviderSelection::List(list)
                }
            }
            _ => ProviderSelection::Auto,
        }
    }
}

/// 当前可用的 provider 列表,按偏好顺序返回。
pub fn available_providers(cfg: &Config) -> Vec<Provider> {
    let mut list: Vec<Provider> = Provider::iter().filter(|p| p.is_available(cfg)).collect();
    list.sort_by_key(|p| p.preference_rank());
    list
}

/// 解析路由候选:单个 provider 名称序列(用于 auto / 单 provider 回退)。
fn resolve_candidates(
    selection: &ProviderSelection,
    loaded: &LoadedConfig,
) -> Result<Vec<Provider>, ToolError> {
    let cfg = &loaded.config;
    match selection {
        ProviderSelection::All => {
            let list: Vec<Provider> = available_providers(cfg)
                .into_iter()
                .filter(|p| p.auto_selectable())
                .collect();
            if list.is_empty() {
                return Err(no_provider_error(cfg));
            }
            Ok(list)
        }
        ProviderSelection::List(names) => {
            let mut list = Vec::new();
            for name in names {
                let provider = Provider::parse(name).ok_or_else(|| {
                    ToolError(format!(
                        "Unknown search provider \"{name}\". Available: {}",
                        available_names(cfg)
                    ))
                })?;
                if !provider.is_available(cfg) {
                    return Err(ToolError(format!(
                        "Search provider \"{name}\" is not configured. Available: {}",
                        available_names(cfg)
                    )));
                }
                if !list.contains(&provider) {
                    list.push(provider);
                }
            }
            Ok(list)
        }
        ProviderSelection::Single(name) => {
            let provider = Provider::parse(name).ok_or_else(|| {
                ToolError(format!(
                    "Unknown search provider \"{name}\". Available: {}",
                    available_names(cfg)
                ))
            })?;
            if !provider.is_available(cfg) {
                return Err(ToolError(format!(
                    "Search provider \"{name}\" is not configured. Available: {}",
                    available_names(cfg)
                )));
            }
            let mut list = vec![provider];
            for fallback in fallback_providers(cfg, provider) {
                if !list.contains(&fallback) {
                    list.push(fallback);
                }
            }
            Ok(list)
        }
        ProviderSelection::Auto => {
            let mut list: Vec<Provider> = Vec::new();
            if let Some(configured) = cfg
                .provider
                .as_deref()
                .map(str::trim)
                .filter(|s| !s.is_empty())
                && !configured.eq_ignore_ascii_case("auto")
            {
                if let Some(provider) = Provider::parse(configured) {
                    if provider.is_available(cfg) {
                        list.push(provider);
                    }
                } else if !configured.eq_ignore_ascii_case("all") {
                    return Err(ToolError(format!(
                        "Unknown configured provider \"{configured}\". Available: {}",
                        available_names(cfg)
                    )));
                }
            }
            // searchRouting.providers 明确顺序优先
            for name in &cfg.search_routing.providers {
                if let Some(provider) = Provider::parse(name)
                    && provider.is_available(cfg)
                    && !list.contains(&provider)
                {
                    list.push(provider);
                }
            }
            // 全部可自动选中的可用 provider 按偏好顺序
            for provider in available_providers(cfg) {
                if provider.auto_selectable() && !list.contains(&provider) {
                    list.push(provider);
                }
            }
            // 兜底:无任何可用 provider 时,使用免密钥的 DuckDuckGo
            if list.is_empty() && Provider::DuckDuckGo.is_available(cfg) {
                list.push(Provider::DuckDuckGo);
            }
            if list.is_empty() {
                return Err(no_provider_error(cfg));
            }
            Ok(list)
        }
    }
}

/// 串行回退候选:取 `search_routing.providers` 中除主 provider 外当前可用的 provider,按配置顺序排列。
fn fallback_providers(cfg: &Config, primary: Provider) -> Vec<Provider> {
    let mut list = Vec::new();
    for name in &cfg.search_routing.providers {
        if let Some(provider) = Provider::parse(name)
            && provider != primary
            && provider.is_available(cfg)
        {
            list.push(provider);
        }
    }
    list
}

/// 逗号拼接当前可用的 provider 名供错误信息展示;无可用时返回 `none configured`。
fn available_names(cfg: &Config) -> String {
    let names: Vec<&str> = available_providers(cfg).iter().map(|p| p.name()).collect();
    if names.is_empty() {
        "none configured".to_string()
    } else {
        names.join(", ")
    }
}

/// 无任何可用 provider 时的统一报错,提示可配置的 API key 环境变量与配置文件路径。
fn no_provider_error(_cfg: &Config) -> ToolError {
    ToolError(format!(
        "No search provider is configured. Set an API key in {} or the matching env var \
         (TAVILY_API_KEY / BRAVE_API_KEY / EXA_API_KEY / JINA_API_KEY / SERPER_API_KEY / \
         PERPLEXITY_API_KEY), or set searxngBaseUrl.",
        config::config_path().display()
    ))
}

/// 执行搜索。`selection` 为工具参数;返回合并后的响应。
pub async fn search(
    query: &str,
    selection: &ProviderSelection,
    options: &SearchOptions,
    loaded: &LoadedConfig,
    proxy: Option<&str>,
) -> Result<SearchResponse, ToolError> {
    let query = query.trim();
    if query.is_empty() {
        return Err(ToolError("Search query is empty".to_string()));
    }
    let candidates = resolve_candidates(selection, loaded)?;
    let cfg = &loaded.config;

    // 单候选:串行回退;多候选(all / 数组):并发聚合
    if candidates.len() == 1 {
        return run_with_fallback(query, &candidates, options, cfg, proxy).await;
    }
    if matches!(
        selection,
        ProviderSelection::All | ProviderSelection::List(_)
    ) {
        return run_parallel(query, &candidates, options, cfg, proxy).await;
    }
    run_with_fallback(query, &candidates, options, cfg, proxy).await
}

/// 按候选顺序逐个搜索,返回首个成功结果;全部失败时返回最后一个错误。
async fn run_with_fallback(
    query: &str,
    candidates: &[Provider],
    options: &SearchOptions,
    cfg: &Config,
    proxy: Option<&str>,
) -> Result<SearchResponse, ToolError> {
    let mut last_error: Option<ToolError> = None;
    for provider in candidates {
        match run_single(*provider, query, options, cfg, proxy).await {
            Ok(response) => return Ok(response),
            Err(err) => {
                last_error = Some(err);
            }
        }
    }
    Err(last_error.unwrap_or_else(|| ToolError("No search provider available".to_string())))
}

/// 并发(最多 3 路)搜索所有候选并合并:答案按顺序拼接,结果按 URL 去重,`provider` 记为 `a+b`;
/// 全部失败时报 `All providers failed`。
async fn run_parallel(
    query: &str,
    candidates: &[Provider],
    options: &SearchOptions,
    cfg: &Config,
    proxy: Option<&str>,
) -> Result<SearchResponse, ToolError> {
    let concurrency = 3usize;
    let results: Vec<Result<SearchResponse, ToolError>> = futures_util::stream::iter(
        candidates
            .iter()
            .copied()
            .map(|provider| run_single(provider, query, options, cfg, proxy)),
    )
    .buffered(concurrency)
    .collect()
    .await;

    let mut answers: Vec<String> = Vec::new();
    let mut merged: Vec<SearchResult> = Vec::new();
    let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut providers: Vec<&str> = Vec::new();
    let mut errors: Vec<String> = Vec::new();
    for (provider, result) in candidates.iter().zip(results) {
        match result {
            Ok(response) => {
                providers.push(provider.name());
                if !response.answer.trim().is_empty() {
                    answers.push(response.answer);
                }
                for item in response.results {
                    if seen.insert(item.url.clone()) {
                        merged.push(item);
                    }
                }
            }
            Err(err) => errors.push(format!("{}: {}", provider.name(), err.0)),
        }
    }
    if providers.is_empty() {
        return Err(ToolError(format!(
            "All providers failed: {}",
            errors.join("; ")
        )));
    }
    Ok(SearchResponse {
        answer: answers.join("\n\n"),
        results: merged,
        provider: providers.join("+"),
    })
}

/// 建好 HTTP client 后按 provider 分派到各自的请求实现。
async fn run_single(
    provider: Provider,
    query: &str,
    options: &SearchOptions,
    cfg: &Config,
    proxy: Option<&str>,
) -> Result<SearchResponse, ToolError> {
    let client = build_client(cfg, proxy)?;
    match provider {
        Provider::Tavily => tavily(&client, cfg, query, options).await,
        Provider::Brave => brave(&client, cfg, query, options).await,
        Provider::Exa => exa(&client, cfg, query, options).await,
        Provider::Jina => jina(&client, cfg, query, options).await,
        Provider::Searxng => searxng(&client, cfg, query, options).await,
        Provider::DuckDuckGo => duckduckgo(&client, query, options).await,
        Provider::Serper => serper(&client, cfg, query, options).await,
        Provider::Perplexity => perplexity(&client, cfg, query, options).await,
    }
}

/// 构造带超时/UA/代理的 reqwest client;`proxy` 参数优先于配置里的 proxy,空串视为未设置。
fn build_client(cfg: &Config, proxy: Option<&str>) -> Result<reqwest::Client, ToolError> {
    let proxy = proxy
        .map(str::to_string)
        .or_else(|| cfg.proxy.clone())
        .filter(|p| !p.trim().is_empty());
    let user_agent = cfg.user_agent.clone().unwrap_or_else(default_user_agent);
    http::build_client_with_options(&http::ClientOptions {
        timeout: config::http_timeout(cfg),
        connect_timeout: Some(Duration::from_secs(15)),
        user_agent: Some(&user_agent),
        proxy: proxy.as_deref(),
        pool_max_idle_per_host: 4,
        ..Default::default()
    })
    .map_err(ToolError)
}

/// 结果条数:未指定时默认 5,显式值钳制到 1~20。
fn num_results(options: &SearchOptions) -> usize {
    options.num_results.map(|n| n.clamp(1, 20)).unwrap_or(5)
}

/// 归一化时间范围,仅接受 day/week/month/year,其余(含未指定)返回 None。
fn recency(options: &SearchOptions) -> Option<&str> {
    options
        .recency
        .as_deref()
        .filter(|r| matches!(*r, "day" | "week" | "month" | "year"))
}

/// 领域过滤归一化:返回 (include, exclude)
fn split_domains(domain_filter: &[String]) -> (Vec<String>, Vec<String>) {
    let mut include = Vec::new();
    let mut exclude = Vec::new();
    for raw in domain_filter {
        let raw = raw.trim();
        if raw.is_empty() {
            continue;
        }
        let (negative, value) = match raw.strip_prefix('-') {
            Some(rest) => (true, rest.trim()),
            None => (false, raw),
        };
        let domain = http::normalize_hostname(value);
        let Some(domain) = domain else { continue };
        let target = if negative { &mut exclude } else { &mut include };
        if !target.contains(&domain) {
            target.push(domain);
        }
    }
    (include, exclude)
}

/// 生成查询后缀:单个 include 用 `site:x`,多个用 `site:x OR site:y`,exclude 逐个追加 `NOT site:x`。
fn domain_query_suffixes(include: &[String], exclude: &[String]) -> String {
    let mut parts = Vec::new();
    if include.len() == 1 {
        parts.push(format!("site:{}", include[0]));
    } else if include.len() > 1 {
        parts.push(
            include
                .iter()
                .map(|d| format!("site:{d}"))
                .collect::<Vec<_>>()
                .join(" OR "),
        );
    }
    for domain in exclude {
        parts.push(format!("NOT site:{domain}"));
    }
    parts.join(" ")
}

/// Tavily `/search`:取聚合答案与结果,支持时间范围与域名 include/exclude;缺 key 或响应非 2xx 时报错。
async fn tavily(
    client: &reqwest::Client,
    cfg: &Config,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let key = config::resolve_credential(cfg.tavily_api_key.as_deref(), "TAVILY_API_KEY")
        .ok_or_else(|| ToolError("Tavily API key not found".to_string()))?;
    let url = format!(
        "{}/search",
        http::resolve_base_url(
            cfg.tavily_base_url.as_deref(),
            "TAVILY_BASE_URL",
            "https://api.tavily.com"
        )
    );
    let (include, exclude) = split_domains(&options.domain_filter);
    let mut body = serde_json::json!({
        "query": query,
        "search_depth": "basic",
        "max_results": num_results(options),
        "include_answer": "basic",
        "include_raw_content": false,
    });
    if let Some(range) = recency(options) {
        body["time_range"] = serde_json::json!(range);
    }
    if !include.is_empty() {
        body["include_domains"] = serde_json::json!(include);
    }
    if !exclude.is_empty() {
        body["exclude_domains"] = serde_json::json!(exclude);
    }
    let response = client
        .post(&url)
        .bearer_auth(key)
        .json(&body)
        .send()
        .await
        .map_err(|err| ToolError(format!("Tavily request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "Tavily API error {}",
            http::error_summary(response).await
        )));
    }
    let data: serde_json::Value = response
        .json()
        .await
        .map_err(|err| ToolError(format!("Tavily returned invalid JSON: {err}")))?;
    let answer = data
        .get("answer")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let results = map_generic_results(
        data.get("results"),
        num_results(options),
        "title",
        "url",
        "content",
    );
    Ok(SearchResponse::new("tavily", answer, results))
}

/// Brave `/res/v1/web/search`:域名过滤经查询后缀实现,无聚合答案,由结果自行拼 `build_answer`。
async fn brave(
    client: &reqwest::Client,
    cfg: &Config,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let key = config::resolve_credential(cfg.brave_api_key.as_deref(), "BRAVE_API_KEY")
        .ok_or_else(|| ToolError("Brave Search API key not found".to_string()))?;
    let base = http::resolve_base_url(
        cfg.brave_base_url.as_deref(),
        "BRAVE_BASE_URL",
        "https://api.search.brave.com/res/v1",
    );
    let (include, exclude) = split_domains(&options.domain_filter);
    let suffix = domain_query_suffixes(&include, &exclude);
    let search_query = if suffix.is_empty() {
        query.to_string()
    } else {
        format!("{query} {suffix}")
    };
    let count = if options.domain_filter.is_empty() {
        num_results(options)
    } else {
        20
    };
    let mut params: Vec<(&str, String)> =
        vec![("q", search_query.clone()), ("count", count.to_string())];
    if let Some(range) = recency(options) {
        let freshness = match range {
            "day" => "pd",
            "week" => "pw",
            "month" => "pm",
            "year" => "py",
            _ => "",
        };
        if !freshness.is_empty() {
            params.push(("freshness", freshness.to_string()));
        }
    }
    let borrowed: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();
    let response = client
        .get(http::with_query(&format!("{base}/web/search"), &borrowed))
        .header("X-Subscription-Token", key)
        .header("accept", "application/json")
        .send()
        .await
        .map_err(|err| ToolError(format!("Brave request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "Brave API error {}",
            http::error_summary(response).await
        )));
    }
    let data: serde_json::Value = response
        .json()
        .await
        .map_err(|err| ToolError(format!("Brave returned invalid JSON: {err}")))?;
    let mut results = Vec::new();
    if let Some(items) = data.pointer("/web/results").and_then(|v| v.as_array()) {
        for item in items {
            let url = item.get("url").and_then(|v| v.as_str()).unwrap_or("");
            if url.is_empty() || !http::passes_domain_filter(url, &include, &exclude) {
                continue;
            }
            let title = item
                .get("title")
                .and_then(|v| v.as_str())
                .filter(|s| !s.trim().is_empty())
                .unwrap_or(url);
            let snippet = item
                .get("description")
                .and_then(|v| v.as_str())
                .unwrap_or("");
            results.push(SearchResult {
                title: title.to_string(),
                url: url.to_string(),
                snippet: snippet.to_string(),
            });
            if results.len() >= num_results(options) {
                break;
            }
        }
    }
    let answer = build_answer(&results);
    Ok(SearchResponse::new("brave", answer, results))
}

/// Exa:无域名/时间/条数约束时走 `/answer` 只取答案,否则走 `/search`(域名、时间范围、条数);
/// 时间范围换算为起始发布日期。
async fn exa(
    client: &reqwest::Client,
    cfg: &Config,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let key = config::resolve_credential(cfg.exa_api_key.as_deref(), "EXA_API_KEY")
        .ok_or_else(|| ToolError("Exa API key not found".to_string()))?;
    let base = http::resolve_base_url(
        cfg.exa_base_url.as_deref(),
        "EXA_BASE_URL",
        "https://api.exa.ai",
    );
    let (include, exclude) = split_domains(&options.domain_filter);
    let use_search = !options.domain_filter.is_empty()
        || recency(options).is_some()
        || options.num_results.map(|n| n != 5).unwrap_or(false);

    if !use_search {
        let response = client
            .post(format!("{base}/answer"))
            .header("x-api-key", key)
            .json(&serde_json::json!({ "query": query }))
            .send()
            .await
            .map_err(|err| ToolError(format!("Exa request failed: {err}")))?;
        if !response.status().is_success() {
            return Err(ToolError(format!(
                "Exa API error {}",
                http::error_summary(response).await
            )));
        }
        let data: serde_json::Value = response
            .json()
            .await
            .map_err(|err| ToolError(format!("Exa returned invalid JSON: {err}")))?;
        let answer = data
            .get("answer")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let results = map_generic_results(data.get("citations"), 20, "title", "url", "");
        return Ok(SearchResponse::new("exa", answer, results));
    }

    let mut body = serde_json::json!({
        "query": query,
        "type": "auto",
        "numResults": num_results(options),
        "contents": { "highlights": true },
    });
    if !include.is_empty() {
        body["includeDomains"] = serde_json::json!(include);
    }
    if !exclude.is_empty() {
        body["excludeDomains"] = serde_json::json!(exclude);
    }
    if let Some(range) = recency(options) {
        let days = match range {
            "day" => 1,
            "week" => 7,
            "month" => 30,
            _ => 365,
        };
        let start = crate::utils::time::rfc3339_days_ago(days);
        body["startPublishedDate"] = serde_json::json!(start);
    }
    let response = client
        .post(format!("{base}/search"))
        .header("x-api-key", key)
        .json(&body)
        .send()
        .await
        .map_err(|err| ToolError(format!("Exa request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "Exa API error {}",
            http::error_summary(response).await
        )));
    }
    let data: serde_json::Value = response
        .json()
        .await
        .map_err(|err| ToolError(format!("Exa returned invalid JSON: {err}")))?;
    let mut results = Vec::new();
    let mut answer_parts = Vec::new();
    if let Some(items) = data.get("results").and_then(|v| v.as_array()) {
        for item in items {
            let url = item.get("url").and_then(|v| v.as_str()).unwrap_or("");
            if url.is_empty() {
                continue;
            }
            let title = item
                .get("title")
                .and_then(|v| v.as_str())
                .filter(|s| !s.trim().is_empty())
                .map(str::to_string)
                .unwrap_or_else(|| format!("Source {}", results.len() + 1));
            let content = item
                .get("highlights")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str())
                        .collect::<Vec<_>>()
                        .join(" ")
                })
                .filter(|s| !s.trim().is_empty())
                .or_else(|| {
                    item.get("text")
                        .and_then(|v| v.as_str())
                        .map(|s| s.chars().take(1000).collect())
                })
                .unwrap_or_default();
            if !content.trim().is_empty() {
                answer_parts.push(format!("{content}\nSource: {title} ({url})"));
            }
            results.push(SearchResult {
                title,
                url: url.to_string(),
                snippet: String::new(),
            });
            if results.len() >= num_results(options) {
                break;
            }
        }
    }
    Ok(SearchResponse::new(
        "exa",
        answer_parts.join("\n\n"),
        results,
    ))
}

/// Jina Reader Search:把排除域名/时间范围拼进查询串,经 `s.jina.ai` 取 JSON,
/// 再按域名过滤与 URL 去重。
async fn jina(
    client: &reqwest::Client,
    cfg: &Config,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let key = config::resolve_credential(cfg.jina_api_key.as_deref(), "JINA_API_KEY")
        .ok_or_else(|| ToolError("Jina Search API key not found".to_string()))?;
    let (include, exclude) = split_domains(&options.domain_filter);
    let exclusions = exclude
        .iter()
        .map(|d| format!(" -site:{d}"))
        .collect::<String>();
    let recency_suffix = recency(options)
        .map(|r| format!(" published in the past {r}"))
        .unwrap_or_default();
    let constrained = format!("{}{exclusions}{recency_suffix}", query.trim());
    let mut url = url::Url::parse(&format!(
        "https://s.jina.ai/{}",
        http::urlencode(&constrained)
    ))
    .map_err(|err| ToolError(format!("Invalid Jina URL: {err}")))?;
    url.query_pairs_mut()
        .append_pair("count", &num_results(options).to_string());
    for domain in &include {
        url.query_pairs_mut().append_pair("site", domain);
    }
    let response = client
        .get(url)
        .header("accept", "application/json")
        .bearer_auth(key)
        .header("x-respond-with", "no-content")
        .header("x-retain-images", "none")
        .send()
        .await
        .map_err(|err| ToolError(format!("Jina request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "Jina API error {}",
            http::error_summary(response).await
        )));
    }
    let data: serde_json::Value = response
        .json()
        .await
        .map_err(|err| ToolError(format!("Jina returned invalid JSON: {err}")))?;
    let items = match data.get("data") {
        Some(serde_json::Value::Array(items)) => items.clone(),
        _ => {
            return Err(ToolError(
                "Jina Search API returned invalid response: expected data array".to_string(),
            ));
        }
    };
    let mut results = Vec::new();
    let mut seen = HashSet::new();
    for item in &items {
        let url = item
            .get("url")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .trim();
        if url.is_empty() || !seen.insert(url.to_string()) {
            continue;
        }
        if !http::passes_domain_filter(url, &include, &exclude) {
            continue;
        }
        let title = item
            .get("title")
            .and_then(|v| v.as_str())
            .filter(|s| !s.trim().is_empty())
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|| format!("Source {}", results.len() + 1));
        let snippet = item
            .get("description")
            .and_then(|v| v.as_str())
            .map(|s| s.split_whitespace().collect::<Vec<_>>().join(" "))
            .unwrap_or_default();
        results.push(SearchResult {
            title,
            url: url.to_string(),
            snippet,
        });
        if results.len() >= num_results(options) {
            break;
        }
    }
    let answer = build_answer(&results);
    Ok(SearchResponse::new("jina", answer, results))
}

/// 自建 SearXNG 的 `/search?format=json`:拼接域名后缀、时间范围并带上配置的附加请求头;
/// 未配置 base URL 时报错,答案取 `answers` 字段再并入结果摘要。
async fn searxng(
    client: &reqwest::Client,
    cfg: &Config,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let base = cfg
        .searxng_base_url
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .ok_or_else(|| ToolError("SearXNG base URL not configured".to_string()))?
        .trim_end_matches('/')
        .to_string();
    let (include, exclude) = split_domains(&options.domain_filter);
    let suffix = domain_query_suffixes(&include, &exclude);
    let search_query = if suffix.is_empty() {
        query.to_string()
    } else {
        format!("{query} {suffix}")
    };
    let mut params: Vec<(&str, String)> =
        vec![("q", search_query.clone()), ("format", "json".to_string())];
    if let Some(range) = recency(options) {
        params.push(("time_range", range.to_string()));
    }
    let borrowed: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();
    let mut request = client.get(http::with_query(&format!("{base}/search"), &borrowed));
    if let Some(headers) = &cfg.searxng_headers {
        for (name, value) in headers {
            request = request.header(name, value);
        }
    }
    let response = request
        .send()
        .await
        .map_err(|err| ToolError(format!("SearXNG request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "SearXNG error {}",
            http::error_summary(response).await
        )));
    }
    let data: serde_json::Value = response
        .json()
        .await
        .map_err(|err| ToolError(format!("SearXNG returned invalid JSON: {err}")))?;
    let mut results = Vec::new();
    if let Some(items) = data.get("results").and_then(|v| v.as_array()) {
        for item in items {
            let url = item.get("url").and_then(|v| v.as_str()).unwrap_or("");
            if url.is_empty() || !http::passes_domain_filter(url, &include, &exclude) {
                continue;
            }
            let title = item
                .get("title")
                .and_then(|v| v.as_str())
                .filter(|s| !s.trim().is_empty())
                .unwrap_or(url);
            let snippet = item.get("content").and_then(|v| v.as_str()).unwrap_or("");
            results.push(SearchResult {
                title: title.to_string(),
                url: url.to_string(),
                snippet: snippet.to_string(),
            });
            if results.len() >= num_results(options) {
                break;
            }
        }
    }
    let mut answer_parts: Vec<String> = data
        .get("answers")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str())
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default();
    answer_parts.push(build_answer(&results));
    let answer = answer_parts
        .into_iter()
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("\n\n");
    Ok(SearchResponse::new("searxng", answer, results))
}

/// DuckDuckGo HTML 端(免密钥):抓取 HTML 后解析结果,解析为空视为无效响应并报错。
async fn duckduckgo(
    client: &reqwest::Client,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let response = client
        .get(http::with_query(
            "https://html.duckduckgo.com/html/",
            &[("q", query)],
        ))
        .header("accept", "text/html")
        .send()
        .await
        .map_err(|err| ToolError(format!("DuckDuckGo request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "DuckDuckGo search error {}",
            http::error_summary(response).await
        )));
    }
    let html = response
        .text()
        .await
        .map_err(|err| ToolError(format!("DuckDuckGo body read failed: {err}")))?;
    let parsed = parse_duckduckgo_html(&html);
    if parsed.is_empty() {
        return Err(ToolError(
            "DuckDuckGo returned no parseable results (invalid response)".to_string(),
        ));
    }
    let (include, exclude) = split_domains(&options.domain_filter);
    let mut results = Vec::new();
    for item in parsed {
        if !http::passes_domain_filter(&item.url, &include, &exclude) {
            continue;
        }
        results.push(item);
        if results.len() >= num_results(options) {
            break;
        }
    }
    let answer = build_answer(&results);
    Ok(SearchResponse::new("duckduckgo", answer, results))
}

/// 从 DuckDuckGo HTML 里抽取 `result__a` 链接与 `result__snippet` 摘要,
/// 剥标签、解码实体并压缩空白;标题为空或链接非 http(s) 的条目丢弃。
fn parse_duckduckgo_html(html: &str) -> Vec<SearchResult> {
    /// 结果链接(`result__a`)正则缓存
    static ANCHOR: OnceLock<Regex> = OnceLock::new();
    /// 结果摘要(`result__snippet`)正则缓存
    static SNIPPET: OnceLock<Regex> = OnceLock::new();
    /// HTML 标签剥离正则缓存
    static TAGS: OnceLock<Regex> = OnceLock::new();

    let anchor = ANCHOR.get_or_init(|| {
        Regex::new(r#"(?is)<a\b[^>]*class="[^"]*result__a[^"]*"[^>]*href="([^"]+)"[^>]*>(.*?)</a>"#)
            .expect("ddg anchor regex")
    });
    let snippet = SNIPPET.get_or_init(|| {
        Regex::new(r#"(?is)<a\b[^>]*class="[^"]*result__snippet[^"]*"[^>]*>(.*?)</a>"#)
            .expect("ddg snippet regex")
    });
    let tags = TAGS.get_or_init(|| Regex::new(r"(?is)<[^>]+>").expect("tag regex"));

    let snippets: Vec<String> = snippet
        .captures_iter(html)
        .map(|caps| {
            let raw = tags.replace_all(&caps[1], "");
            collapse_ws(&super::fetch::decode_entities(&raw))
        })
        .collect();

    let mut results = Vec::new();
    for (index, caps) in anchor.captures_iter(html).enumerate() {
        let href = caps.get(1).map(|m| m.as_str()).unwrap_or("");
        let title_raw = tags.replace_all(&caps[2], "");
        let title = collapse_ws(&super::fetch::decode_entities(&title_raw));
        let Some(url) = decode_duckduckgo_url(href) else {
            continue;
        };
        if title.is_empty() {
            continue;
        }
        results.push(SearchResult {
            title,
            url,
            snippet: snippets.get(index).cloned().unwrap_or_default(),
        });
    }
    results
}

/// 把任意空白序列压缩为单个空格并去掉首尾空白。
fn collapse_ws(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

/// 解出 DuckDuckGo 跳转链接的真实目标(`uddg` 参数);非 http(s) 或解析失败返回 None。
fn decode_duckduckgo_url(href: &str) -> Option<String> {
    let base = url::Url::parse("https://html.duckduckgo.com/html/").ok()?;
    let link = base.join(href).ok()?;
    let destination = link
        .query_pairs()
        .find(|(k, _)| k == "uddg")
        .map(|(_, v)| v.into_owned())
        .unwrap_or_else(|| link.to_string());
    let parsed = url::Url::parse(&destination).ok()?;
    match parsed.scheme() {
        "http" | "https" => Some(parsed.to_string()),
        _ => None,
    }
}

/// Serper(Google)`/search`:域名后缀拼进查询,有域名过滤时多取 5 条以补偿被过滤项;
/// 时间范围映射为 `tbs`(qdr:d/w/m/y)。
async fn serper(
    client: &reqwest::Client,
    cfg: &Config,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let key = config::resolve_credential(cfg.serper_api_key.as_deref(), "SERPER_API_KEY")
        .ok_or_else(|| ToolError("Serper API key not found".to_string()))?;
    let (include, exclude) = split_domains(&options.domain_filter);
    let suffix = domain_query_suffixes(&include, &exclude);
    let search_query = if suffix.is_empty() {
        query.to_string()
    } else {
        format!("{query} {suffix}")
    };
    let count = if options.domain_filter.is_empty() {
        num_results(options)
    } else {
        (num_results(options) + 5).min(20)
    };
    let mut body = serde_json::json!({ "q": search_query, "num": count });
    if let Some(range) = recency(options) {
        let tbs = match range {
            "day" => "qdr:d",
            "week" => "qdr:w",
            "month" => "qdr:m",
            _ => "qdr:y",
        };
        body["tbs"] = serde_json::json!(tbs);
    }
    let response = client
        .post("https://google.serper.dev/search")
        .header("X-API-KEY", key)
        .json(&body)
        .send()
        .await
        .map_err(|err| ToolError(format!("Serper request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "Serper API error {}",
            http::error_summary(response).await
        )));
    }
    let data: serde_json::Value = response
        .json()
        .await
        .map_err(|err| ToolError(format!("Serper returned invalid JSON: {err}")))?;
    let mut results = Vec::new();
    if let Some(entries) = data.get("organic").and_then(|v| v.as_array()) {
        for entry in entries {
            let link = entry.get("link").and_then(|v| v.as_str()).unwrap_or("");
            if link.is_empty() || !http::passes_domain_filter(link, &include, &exclude) {
                continue;
            }
            let title = entry
                .get("title")
                .and_then(|v| v.as_str())
                .filter(|s| !s.trim().is_empty())
                .map(str::to_string)
                .unwrap_or_else(|| format!("Source {}", results.len() + 1));
            let snippet = entry.get("snippet").and_then(|v| v.as_str()).unwrap_or("");
            results.push(SearchResult {
                title,
                url: link.to_string(),
                snippet: snippet.to_string(),
            });
            if results.len() >= num_results(options) {
                break;
            }
        }
    }
    let answer = build_answer(&results);
    Ok(SearchResponse::new("serper", answer, results))
}

/// Perplexity `/chat/completions`(sonar):取 `choices[0].message.content` 为答案,
/// `citations` 映射为结果;域名过滤用 `search_domain_filter`(排除项加 `-` 前缀)。
async fn perplexity(
    client: &reqwest::Client,
    cfg: &Config,
    query: &str,
    options: &SearchOptions,
) -> Result<SearchResponse, ToolError> {
    let key = config::resolve_credential(cfg.perplexity_api_key.as_deref(), "PERPLEXITY_API_KEY")
        .ok_or_else(|| ToolError("Perplexity API key not found".to_string()))?;
    let (include, exclude) = split_domains(&options.domain_filter);
    let mut body = serde_json::json!({
        "model": "sonar",
        "messages": [{ "role": "user", "content": query }],
        "max_tokens": 1024,
        "return_related_questions": false,
    });
    if let Some(range) = recency(options) {
        body["search_recency_filter"] = serde_json::json!(range);
    }
    let mut domains = include.clone();
    domains.extend(exclude.iter().map(|d| format!("-{d}")));
    if !domains.is_empty() {
        body["search_domain_filter"] = serde_json::json!(domains);
    }
    let response = client
        .post("https://api.perplexity.ai/chat/completions")
        .bearer_auth(key)
        .json(&body)
        .send()
        .await
        .map_err(|err| ToolError(format!("Perplexity request failed: {err}")))?;
    if !response.status().is_success() {
        return Err(ToolError(format!(
            "Perplexity API error {}",
            http::error_summary(response).await
        )));
    }
    let data: serde_json::Value = response
        .json()
        .await
        .map_err(|err| ToolError(format!("Perplexity returned invalid JSON: {err}")))?;
    let answer = data
        .pointer("/choices/0/message/content")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let results = map_generic_results(data.get("citations"), 20, "title", "url", "");
    Ok(SearchResponse::new("perplexity", answer, results))
}

/// 把 `[{title,url,snippet}]` 结构映射为统一结果。
fn map_generic_results(
    value: Option<&Value>,
    limit: usize,
    title_key: &str,
    url_key: &str,
    snippet_key: &str,
) -> Vec<SearchResult> {
    let Some(items) = value.and_then(|v| v.as_array()) else {
        return Vec::new();
    };
    let mut results = Vec::new();
    for item in items {
        // citations 可能是纯字符串数组
        if let Some(url) = item.as_str() {
            results.push(SearchResult {
                title: format!("Source {}", results.len() + 1),
                url: url.to_string(),
                snippet: String::new(),
            });
            if results.len() >= limit {
                break;
            }
            continue;
        }
        let url = item.get(url_key).and_then(|v| v.as_str()).unwrap_or("");
        if url.is_empty() {
            continue;
        }
        let title = item
            .get(title_key)
            .and_then(|v| v.as_str())
            .filter(|s| !s.trim().is_empty())
            .map(str::to_string)
            .unwrap_or_else(|| format!("Source {}", results.len() + 1));
        let snippet = if snippet_key.is_empty() {
            String::new()
        } else {
            item.get(snippet_key)
                .and_then(|v| v.as_str())
                .map(|s| s.split_whitespace().collect::<Vec<_>>().join(" "))
                .unwrap_or_default()
        };
        results.push(SearchResult {
            title,
            url: url.to_string(),
            snippet,
        });
        if results.len() >= limit {
            break;
        }
    }
    results
}

/// 无 answer 的 provider 用「snippet + Source:」拼出 answer。
fn build_answer(results: &[SearchResult]) -> String {
    results
        .iter()
        .map(|r| {
            if r.snippet.is_empty() {
                format!("Source: {} ({})", r.title, r.url)
            } else {
                format!("{}\nSource: {} ({})", r.snippet, r.title, r.url)
            }
        })
        .collect::<Vec<_>>()
        .join("\n\n")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::extensions::web_access::config::LoadedConfig;

    #[test]
    fn parses_provider_selection() {
        assert_eq!(ProviderSelection::from_json(None), ProviderSelection::Auto);
        assert_eq!(
            ProviderSelection::from_json(Some(&serde_json::json!("auto"))),
            ProviderSelection::Auto
        );
        assert_eq!(
            ProviderSelection::from_json(Some(&serde_json::json!("all"))),
            ProviderSelection::All
        );
        assert_eq!(
            ProviderSelection::from_json(Some(&serde_json::json!("Brave"))),
            ProviderSelection::Single("Brave".to_string())
        );
        assert_eq!(
            ProviderSelection::from_json(Some(&serde_json::json!(["brave", "tavily"]))),
            ProviderSelection::List(vec!["brave".to_string(), "tavily".to_string()])
        );
        assert_eq!(
            ProviderSelection::from_json(Some(&serde_json::json!([]))),
            ProviderSelection::Auto
        );
    }

    #[test]
    fn provider_strum_conversions() {
        // 名称 → 变体(去空白、大小写不敏感、支持别名)
        assert_eq!(Provider::parse("  DDG "), Some(Provider::DuckDuckGo));
        assert_eq!(Provider::parse("duckduckgo"), Some(Provider::DuckDuckGo));
        assert_eq!(Provider::parse("Perplexity"), Some(Provider::Perplexity));
        assert_eq!(Provider::parse("SEARXNG"), Some(Provider::Searxng));
        assert_eq!(Provider::parse("nope"), None);

        // 变体 → 规范名称(别名不改变规范名)
        assert_eq!(Provider::DuckDuckGo.name(), "duckduckgo");
        assert_eq!(Provider::Jina.name(), "jina");

        // 判别值即偏好顺序,`iter()` 按声明顺序遍历且覆盖全部成员
        let ranks: Vec<usize> = Provider::iter().map(|p| p.preference_rank()).collect();
        assert_eq!(ranks, (0..8).collect::<Vec<_>>());

        // `ALL` 已移除,`iter()` 是唯一来源
        assert_eq!(Provider::iter().count(), 8);
    }

    #[test]
    fn domain_filter_helpers() {
        let (inc, exc) = split_domains(&[
            "github.com".to_string(),
            "-old.example.com".to_string(),
            "https://docs.rs/crate".to_string(),
        ]);
        assert_eq!(inc, vec!["github.com", "docs.rs"]);
        assert_eq!(exc, vec!["old.example.com"]);
        assert!(http::passes_domain_filter(
            "https://github.com/a/b",
            &inc,
            &exc
        ));
        assert!(!http::passes_domain_filter(
            "https://old.example.com/x",
            &inc,
            &exc
        ));
        assert!(!http::passes_domain_filter(
            "https://other.com/x",
            &inc,
            &exc
        ));
    }

    #[test]
    fn duckduckgo_url_decoding() {
        let href = "//duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Fpage&rut=abc";
        assert_eq!(
            decode_duckduckgo_url(href),
            Some("https://example.com/page".to_string())
        );
        assert_eq!(decode_duckduckgo_url("javascript:void(0)"), None);
    }

    #[test]
    fn duckduckgo_html_parsing() {
        let html = r#"
          <div class="result"><a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Fone">Example <b>One</b></a>
          <a class="result__snippet">Snippet &amp; one</a></div>
          <div class="result result--ad"><a class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fads.example%2F">Ad</a></div>
        "#;
        let results = parse_duckduckgo_html(html);
        assert_eq!(
            results.len(),
            2,
            "ad 也被解析(上游按 class 过滤,解析层不做广告判别)"
        );
        assert_eq!(results[0].title, "Example One");
        assert_eq!(results[0].url, "https://example.com/one");
        assert_eq!(results[0].snippet, "Snippet & one");
    }

    #[test]
    fn availability_and_routing() {
        // 显式只配 Brave/SearxNG 时,Tavily 必须判为不可用 —— 而 `is_available` 会回退读
        // 进程环境变量,故先清空全部搜索凭据(drop 时自动还原)。
        let _env = crate::test_support::clear_search_credential_env();
        let loaded = LoadedConfig {
            config: Config {
                brave_api_key: Some("k".to_string()),
                searxng_base_url: Some("https://search.local".to_string()),
                ..Config::default()
            },
            error: None,
        };
        let available = available_providers(&loaded.config);
        assert!(available.contains(&Provider::Brave));
        assert!(available.contains(&Provider::Searxng));
        assert!(available.contains(&Provider::DuckDuckGo));
        assert!(!available.contains(&Provider::Tavily));
        // auto 路由:SearXNG 优先于 Brave
        let candidates = resolve_candidates(&ProviderSelection::Auto, &loaded).unwrap();
        assert_eq!(candidates[0], Provider::Searxng);
        assert!(candidates.contains(&Provider::Brave));
        // all 排除显式专用 DuckDuckGo
        let all = resolve_candidates(&ProviderSelection::All, &loaded).unwrap();
        assert!(!all.contains(&Provider::DuckDuckGo));
        assert!(all.contains(&Provider::Searxng));
        // 未配置 provider 报错
        assert!(resolve_candidates(&ProviderSelection::Single("tavily".into()), &loaded).is_err());
    }

    #[test]
    fn auto_routing_falls_back_to_duckduckgo_when_nothing_configured() {
        // 需密钥的 provider 都依赖进程级环境变量,故清空后串行断言(drop 时还原)
        let _env = crate::test_support::clear_search_credential_env();

        let loaded = LoadedConfig {
            config: Config::default(),
            error: None,
        };
        // 其它 provider 全不可用 → 兜底到免密钥的 DuckDuckGo
        assert_eq!(
            available_providers(&loaded.config),
            vec![Provider::DuckDuckGo]
        );
        assert_eq!(
            resolve_candidates(&ProviderSelection::Auto, &loaded).unwrap(),
            vec![Provider::DuckDuckGo]
        );
    }

    #[test]
    fn urlencoding_helper() {
        // Jina 把查询编进路径,空格必须是 %20
        assert_eq!(http::urlencode("a b/c"), "a%20b%2Fc");
    }
}