openpaw-tools 0.1.0

Core tool set: 21 tools, ToolHost registry, bash AST guard, local sandbox (M2, staged).
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
//! WebSearch 适配器架构 + 反爬修复。
//!
//! 统一的搜索引擎适配器 trait,支持 bing / duckduckgo 双引擎。
//! 多引擎降级 + 反爬检测 + 浏览器标头伪装。

pub mod adapters;
pub mod headers;
pub mod utils;

use std::path::Path;
use std::sync::OnceLock;

use async_trait::async_trait;
use openpaw_core::{Result, ToolCallOutcome, ToolCategory, ToolConcurrency, ToolSpec};
use serde::Deserialize;

// ── Types ─────────────────────────────────────────────────────────────

/// 搜索结果统一格式
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SearchResult {
    pub title: String,
    pub url: String,
    pub snippet: Option<String>,
}

/// 搜索选项
#[derive(Debug, Clone, Default)]
pub struct SearchOptions {
    pub max_results: Option<usize>,
    pub allowed_domains: Option<Vec<String>>,
    pub blocked_domains: Option<Vec<String>>,
}

/// 适配器类型
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdapterKind {
    Bing,
    DuckDuckGo,
}

impl AdapterKind {
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "bing" => Some(Self::Bing),
            "duckduckgo" => Some(Self::DuckDuckGo),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Bing => "bing",
            Self::DuckDuckGo => "duckduckgo",
        }
    }
}

// ── Error types ───────────────────────────────────────────────────────

#[derive(Debug, thiserror::Error)]
pub enum SearchError {
    #[error("HTTP error: {0}")]
    Http(String),
    #[error("Parse error: {0}")]
    Parse(String),
    #[error("Rate limited")]
    RateLimited,
    #[error("Blocked by anti-bot detection — try again later or use a different engine")]
    BlockedByAntiBot,
    #[error("Invalid query: {0}")]
    InvalidQuery(String),
    #[error("Adapter init failed: {0}")]
    AdapterInit(String),
    /// 全部引擎均失败时的聚合错误(携带各引擎的错误摘要)。
    #[error("all search engines failed: {0}")]
    AllEnginesFailed(String),
}

// ── Adapter trait ─────────────────────────────────────────────────────

#[async_trait]
pub trait WebSearchAdapter: Send + Sync {
    fn name(&self) -> &'static str;
    async fn search(
        &self,
        query: &str,
        options: &SearchOptions,
    ) -> std::result::Result<Vec<SearchResult>, SearchError>;
}

// ── Factory + fallback ────────────────────────────────────────────────

/// 进程级共享搜索 HTTP client。配置与两 adapter 原有构建一致(30s 总超时 /
/// 10s 连接超时 / browser headers / limited redirect);`reqwest::Client`
/// 内部为 Arc——adapter 每次实例化时 clone 复用同一连接池与 DNS 缓存,
/// 而非每次 `web_search` 调用重建。
pub(crate) fn shared_search_client() -> std::result::Result<reqwest::Client, SearchError> {
    static CLIENT: OnceLock<std::result::Result<reqwest::Client, String>> = OnceLock::new();
    CLIENT
        .get_or_init(|| {
            reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(30))
                .connect_timeout(std::time::Duration::from_secs(10))
                .default_headers(headers::browser_headers())
                .redirect(reqwest::redirect::Policy::limited(5))
                .build()
                .map_err(|e| e.to_string())
        })
        .clone()
        .map_err(SearchError::AdapterInit)
}

/// 创建适配器实例
pub fn create_adapter(
    kind: AdapterKind,
) -> std::result::Result<Box<dyn WebSearchAdapter>, SearchError> {
    match kind {
        AdapterKind::Bing => Ok(Box::new(adapters::bing::BingAdapter::new()?)),
        AdapterKind::DuckDuckGo => Ok(Box::new(adapters::duckduckgo::DuckDuckGoAdapter::new()?)),
    }
}

/// 多引擎降级搜索 — 按引擎列表顺序尝试,返回第一个成功的结果。
/// 全部失败时返回聚合错误;全部被反爬拦截时保留统一的反爬错误。
pub async fn search_with_fallback(
    query: &str,
    options: &SearchOptions,
    engines: &[AdapterKind],
) -> std::result::Result<Vec<SearchResult>, SearchError> {
    if engines.is_empty() {
        return Err(SearchError::InvalidQuery("no engines configured".into()));
    }

    let mut errors: Vec<String> = Vec::new();
    let mut all_blocked = true;

    for &kind in engines {
        let adapter = match create_adapter(kind) {
            Ok(a) => a,
            Err(e) => {
                all_blocked = false;
                errors.push(format!("{}: {e}", kind.as_str()));
                continue;
            }
        };
        match adapter.search(query, options).await {
            Ok(results) if !results.is_empty() => return Ok(results),
            Ok(_) => {
                all_blocked = false;
                errors.push(format!("{}: no results", kind.as_str()));
            }
            Err(SearchError::BlockedByAntiBot) => {
                errors.push(format!("{}: blocked by anti-bot", kind.as_str()));
            }
            Err(e) => {
                all_blocked = false;
                errors.push(format!("{}: {e}", kind.as_str()));
            }
        }
    }

    if all_blocked {
        return Err(SearchError::BlockedByAntiBot);
    }
    Err(SearchError::AllEnginesFailed(errors.join("; ")))
}

// ── Language-aware engine selection ───────────────────────────────────

/// 检测查询语言:包含 CJK 字符判定为中文,否则英文
pub fn detect_query_language(query: &str) -> &'static str {
    let has_cjk = query.chars().any(|c| {
        ('\u{4E00}'..='\u{9FFF}').contains(&c) // CJK Unified Ideographs
            || ('\u{3400}'..='\u{4DBF}').contains(&c) // CJK Extension A
    });
    if has_cjk {
        "zh"
    } else {
        "en"
    }
}

/// 构建搜索引擎降级链。中文与英文查询统一以 Bing 优先、DuckDuckGo 兜底
/// (查询语言经由 [`detect_query_language`] 驱动各适配器的市场/区域参数)。
/// 用户显式指定引擎时只用该引擎;无法识别的引擎名回退默认降级链。
pub fn engines_for_query(_query: &str, user_engine: Option<&str>) -> Vec<AdapterKind> {
    if let Some(kind) = user_engine.and_then(AdapterKind::from_str) {
        return vec![kind];
    }
    vec![AdapterKind::Bing, AdapterKind::DuckDuckGo]
}

// ── Domain filtering ──────────────────────────────────────────────────

/// 按域名过滤搜索结果
pub fn filter_by_domains(
    mut results: Vec<SearchResult>,
    allowed: &Option<Vec<String>>,
    blocked: &Option<Vec<String>>,
) -> Vec<SearchResult> {
    if let Some(ref allowed) = allowed {
        results.retain(|r| {
            if let Ok(host) = url::Url::parse(&r.url) {
                let host_str = host.host_str().unwrap_or("");
                allowed
                    .iter()
                    .any(|d| host_str == d || host_str.ends_with(&format!(".{d}")))
            } else {
                false
            }
        });
    }

    if let Some(ref blocked) = blocked {
        results.retain(|r| {
            if let Ok(host) = url::Url::parse(&r.url) {
                let host_str = host.host_str().unwrap_or("");
                !blocked
                    .iter()
                    .any(|d| host_str == d || host_str.ends_with(&format!(".{d}")))
            } else {
                true
            }
        });
    }

    results
}

// ── Tool spec + run ───────────────────────────────────────────────────

#[derive(Deserialize)]
struct Args {
    query: String,
    #[serde(default = "default_limit")]
    limit: usize,
    #[serde(default)]
    engine: Option<String>,
    #[serde(default)]
    allowed_domains: Option<Vec<String>>,
    #[serde(default)]
    blocked_domains: Option<Vec<String>>,
    /// When true (default), automatically fetch the full page content of each
    /// search result using web_fetch, instead of returning only snippets.
    #[serde(default = "default_fetch_content")]
    fetch_content: bool,
    /// Max chars to fetch per page (only used when fetch_content=true).
    #[serde(default = "default_fetch_max_chars")]
    fetch_max_chars: usize,
}

fn default_limit() -> usize {
    5
}

fn default_fetch_content() -> bool {
    true
}

fn default_fetch_max_chars() -> usize {
    20_000
}

pub fn spec() -> ToolSpec {
    ToolSpec {
        name: "web_search".into(),
        description: "Search the web for current information. Returns titles, URLs, snippets, \
            AND the full page content of each result (fetched automatically). \
            Supports multiple search engines with automatic fallback: bing (default, first for all languages), \
            duckduckgo (fallback). Domain filtering via allowed_domains/blocked_domains.".into(),
        input_schema: serde_json::json!({
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "Search query"},
                "limit": {"type": "integer", "description": "Max results (default 5)", "default": 5},
                "engine": {
                    "type": "string",
                    "enum": ["bing", "duckduckgo"],
                    "description": "Search engine to use. Default: bing (with fallback to duckduckgo)."
                },
                "allowed_domains": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Only include results from these domains"
                },
                "blocked_domains": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Exclude results from these domains"
                },
                "fetch_content": {
                    "type": "boolean",
                    "description": "Automatically fetch full page content for each result (default: true). Set to false for faster search with snippets only.",
                    "default": true
                },
                "fetch_max_chars": {
                    "type": "integer",
                    "description": "Max chars to fetch per page when fetch_content=true (default: 20000)",
                    "default": 20000
                }
            },
            "required": ["query"]
        }),
        category: Some(ToolCategory::Network),
        concurrency: Some(ToolConcurrency::ParallelSafe),
        is_destructive: Some(false),
        ..Default::default()
    }
}

pub async fn run(_workspace: &Path, args: serde_json::Value) -> Result<ToolCallOutcome> {
    let a: Args = match serde_json::from_value(args) {
        Ok(a) => a,
        Err(e) => return Ok(ToolCallOutcome::error(format!("web_search: bad args: {e}"))),
    };

    // 空查询验证
    if a.query.trim().is_empty() {
        return Ok(ToolCallOutcome::error(
            "web_search: query must not be empty",
        ));
    }

    let options = SearchOptions {
        max_results: Some(a.limit),
        allowed_domains: a.allowed_domains.clone(),
        blocked_domains: a.blocked_domains.clone(),
    };

    // 语言感知引擎降级链
    let engines = engines_for_query(&a.query, a.engine.as_deref());

    match search_with_fallback(&a.query, &options, &engines).await {
        Ok(results) => {
            if results.is_empty() {
                return Ok(ToolCallOutcome::ok(format!("no results for: {}", a.query)));
            }

            // If fetch_content is disabled, return snippets only (legacy behavior)
            if !a.fetch_content {
                let out = results
                    .iter()
                    .enumerate()
                    .map(|(i, r)| match &r.snippet {
                        Some(s) => format!("{}. {} — {}\n   {}", i + 1, r.title, r.url, s),
                        None => format!("{}. {} — {}", i + 1, r.title, r.url),
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                return Ok(ToolCallOutcome::ok(out));
            }

            // Auto-fetch full page content for each search result (concurrently)
            let fetch_args: Vec<(usize, SearchResult)> = results.into_iter().enumerate().collect();

            let fetch_tasks: Vec<_> = fetch_args
                .into_iter()
                .map(|(idx, r)| {
                    let url = r.url.clone();
                    let title = r.title.clone();
                    let snippet = r.snippet.clone();
                    let max_chars = a.fetch_max_chars;
                    tokio::spawn(async move {
                        let fetch_input = serde_json::json!({
                            "url": url,
                            "max_chars": max_chars,
                        });
                        let content =
                            match crate::web::web_fetch::run(Path::new("."), fetch_input).await {
                                Ok(outcome) if !outcome.is_error => outcome.content,
                                Ok(outcome) => format!("[fetch failed: {}]", outcome.content),
                                Err(e) => format!("[fetch error: {e}]"),
                            };
                        (idx, title, url, snippet, content)
                    })
                })
                .collect();

            // Collect results, preserving original order
            let mut fetched: Vec<(usize, String, String, Option<String>, String)> =
                Vec::with_capacity(fetch_tasks.len());
            for task in fetch_tasks {
                if let Ok(item) = task.await {
                    fetched.push(item);
                }
            }
            fetched.sort_by_key(|(idx, _, _, _, _)| *idx);

            // Format output: snippet + full page content for each result
            let out = fetched
                .iter()
                .map(|(idx, title, url, snippet, content)| {
                    let header = match snippet {
                        Some(s) => format!("{}. {} — {}\n   摘要: {}", idx + 1, title, url, s),
                        None => format!("{}. {} — {}", idx + 1, title, url),
                    };
                    format!("{}\n   正文:\n{}", header, indent_content(content, 3))
                })
                .collect::<Vec<_>>()
                .join("\n\n---\n\n");

            Ok(ToolCallOutcome::ok(out))
        }
        Err(SearchError::BlockedByAntiBot) => Ok(ToolCallOutcome::error(
            "All search engines blocked by anti-bot. Try again later or use web_fetch directly.",
        )),
        Err(e) => Ok(ToolCallOutcome::error(format!("web_search error: {e}"))),
    }
}

/// Indent each line of `content` by `spaces` spaces for readable nesting.
fn indent_content(content: &str, spaces: usize) -> String {
    let prefix = " ".repeat(spaces);
    content
        .lines()
        .map(|line| format!("{prefix}{line}"))
        .collect::<Vec<_>>()
        .join("\n")
}