vtcode-core 0.181.6

Core library for VT Code - a Rust-based terminal coding agent
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! WebSearch tool: query -> ranked web results (title, url, snippet).
//!
//! The keyless DuckDuckGo HTML endpoint provides best-effort ranked results and
//! may be rate-limited or challenged by anti-bot defenses.
//!
//! Safety/rate-limit guard rails:
//! - A **cooldown** between consecutive network requests (default 3s) prevents
//!   hammering DDG and triggering anti-bot challenges.
//! - A short **result cache** (default 5min TTL) means repeated identical
//!   queries are answered from memory without any network call.
//! - A **session-wide cap** (default 12 requests) ensures the tool cannot
//!   run away with varied queries. This is a conservative VTCode budget, not
//!   a DuckDuckGo-published quota.
//!
//! All three are configurable via `WebSearchConfig` and turned off in tests.

use super::traits::Tool;
use crate::config::constants::tools;
use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use vtcode_config::WebSearchConfig;

mod parser;
mod transport;

pub use parser::parse_duckduckgo_html;
use transport::duckduckgo_search;

const MAX_QUERY_CHARS: usize = 500;
const MAX_RESULTS_CAP: usize = 20;

/// Shared fallback hint for GitHub-flavored queries. The `gh` CLI queries
/// `api.github.com` instead of the DuckDuckGo HTML endpoint, so it stays
/// usable while web_search is cooling down, capped, blocked, or empty.
const GITHUB_GH_FALLBACK: &str =
    "For GitHub queries, use `gh search`/`gh api` via exec_command instead (subject to exec policy).";

pub(crate) const WEB_SEARCH_DESCRIPTION: &str = "Searches the web with keyless DuckDuckGo HTML search and returns ranked results (title, url, snippet). Accepts { query: string (up to 500 characters), max_results?: number }. Results are cached briefly to reduce rate limiting; cooldown and session caps bound requests. Use web_fetch on a promising result URL to read full content, subject to separate approval. Returns { query, provider, count, cached, results: [{ title, url, snippet }] } or a structured error with error_type, retryable, and next_action.";

/// Shared declaration schema for both builtin registration paths.
pub(crate) fn web_search_parameter_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "query": {
                "type": "string",
                "description": "A nonblank search query (a topic, question, or keywords), up to 500 characters.",
                "minLength": 1,
                "maxLength": MAX_QUERY_CHARS
            },
            "max_results": {
                "type": "integer",
                "description": "Maximum number of results to return (default: 8, max: 20)."
            }
        },
        "required": ["query"],
        "additionalProperties": false
    })
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct WebSearchArgs {
    /// Search query.
    query: String,
    #[serde(default)]
    max_results: Option<usize>,
}

pub struct SearchResult {
    pub title: String,
    pub url: String,
    pub snippet: String,
}

impl SearchResult {
    pub fn new(title: String, url: String, snippet: String) -> Self {
        Self { title, url, snippet }
    }
}

/// Lightweight in-process state shared between calls on the same tool
/// instance. Owns the cooldown clock, the request counter, and the result
/// cache.
#[derive(Default)]
struct SessionState {
    last_request_at: Option<Instant>,
    requests_made: u32,
    cache: HashMap<String, CachedResults>,
}

struct CachedResults {
    stored_at: Instant,
    payload: Value,
}

impl SessionState {
    fn cache_get(&self, key: &str, ttl: Duration) -> Option<Value> {
        let entry = self.cache.get(key)?;
        if entry.stored_at.elapsed() > ttl {
            return None;
        }
        Some(entry.payload.clone())
    }

    fn cache_put(&mut self, key: String, payload: Value) {
        self.cache.insert(key, CachedResults { stored_at: Instant::now(), payload });
    }
}

/// WebSearch tool. Stateless over the network (DDG) but tracks per-instance
/// cooldown, request cap, and short-lived result cache to avoid hammering
/// DDG's HTML endpoint and triggering anti-bot challenges.
#[derive(Clone, Default)]
pub struct WebSearchTool {
    config: Arc<Mutex<WebSearchConfig>>,
    state: Arc<Mutex<SessionState>>,
}

impl WebSearchTool {
    pub fn new() -> Self {
        Self {
            config: Arc::new(Mutex::new(WebSearchConfig::default())),
            state: Arc::new(Mutex::new(SessionState::default())),
        }
    }

    /// Construct a tool with an explicit configuration. The config drives the
    /// result-count default, request timeout, cooldown, cache TTL, and
    /// session-wide request cap.
    pub fn with_config(config: WebSearchConfig) -> Self {
        Self {
            config: Arc::new(Mutex::new(config)),
            state: Arc::new(Mutex::new(SessionState::default())),
        }
    }

    /// Apply a new configuration (e.g., after `vtcode.toml` reload).
    pub fn set_config(&self, config: WebSearchConfig) {
        if let Ok(mut guard) = self.config.lock() {
            *guard = config;
        }
    }

    /// Reset the in-process state (cooldown, counter, cache). Tests use this.
    pub fn reset(&self) {
        if let Ok(mut guard) = self.state.lock() {
            *guard = SessionState::default();
        }
    }

    fn snapshot_config(&self) -> Result<WebSearchConfig> {
        self.config
            .lock()
            .map(|guard| guard.clone())
            .map_err(|error| anyhow!("web_search config lock poisoned: {error}"))
    }

    async fn run(&self, raw_args: Value) -> Result<Value> {
        let args: WebSearchArgs =
            serde_json::from_value(raw_args).context("Invalid arguments for web_search. Provide a 'query' string.")?;

        let query = args.query.trim().to_string();
        if query.is_empty() || query.chars().count() > MAX_QUERY_CHARS {
            return Err(anyhow!("web_search requires a nonblank query of at most {MAX_QUERY_CHARS} characters"));
        }

        let snapshot = self.snapshot_config()?;
        let default_max = snapshot.max_results.clamp(1, MAX_RESULTS_CAP);
        let max_results = args.max_results.unwrap_or(default_max).clamp(1, MAX_RESULTS_CAP);
        let cooldown = Duration::from_millis(snapshot.cooldown_ms);
        let cache_ttl = Duration::from_secs(snapshot.cache_ttl_secs);
        let session_cap = snapshot.session_max_requests;
        let provider = snapshot.provider.backend_id();

        // Include `max_results` in the key so a repeat query with a
        // different cap gets a fresh, larger or smaller result set. With the
        // hard 20-result cap this is rarely observable, but it keeps the
        // cache semantics tight: the value the agent sees was produced for
        // the same `max_results` it asked for.
        let cache_key = format!("{max_results}::{query}");

        // Fast path: cache hit. Avoid the network entirely.
        let cached = self
            .state
            .lock()
            .map_err(|error| anyhow!("web_search state lock poisoned: {error}"))?
            .cache_get(&cache_key, cache_ttl);
        if let Some(cached) = cached {
            return Ok(mark_cached(cached, provider));
        }

        // Reserve the request before touching the network. The reservation is
        // atomic with the cap/cooldown check, so concurrent calls cannot all
        // pass admission and exceed the session budget.
        if let Some(response) = self.admit_request(&query, provider, cooldown, session_cap)? {
            return Ok(response);
        }

        let results = duckduckgo_search(&query, max_results, snapshot.timeout_secs).await;

        match results {
            Ok(results) => {
                let payload = results_response(&query, provider, results);
                self.cache_put(&cache_key, &payload)?;
                Ok(payload)
            }
            Err(e) => {
                // Categorize the error so the agent can act on it.
                Ok(search_error_response(&query, provider, &e.to_string()))
            }
        }
    }

    fn cache_put(&self, key: &str, payload: &Value) -> Result<()> {
        let mut state = self
            .state
            .lock()
            .map_err(|error| anyhow!("web_search state lock poisoned: {error}"))?;
        state.cache_put(key.to_string(), payload.clone());
        Ok(())
    }

    fn admit_request(
        &self,
        query: &str,
        provider: &str,
        cooldown: Duration,
        session_cap: u32,
    ) -> Result<Option<Value>> {
        let mut state = self.state.lock().map_err(|e| anyhow!("web_search state lock poisoned: {e}"))?;
        if state.requests_made >= session_cap {
            return Ok(Some(session_cap_reached_response(provider, query, session_cap)));
        }
        if let Some(last) = state.last_request_at {
            let elapsed = last.elapsed();
            if elapsed < cooldown {
                return Ok(Some(cooldown_response(provider, query, cooldown.saturating_sub(elapsed))));
            }
        }
        state.requests_made = state.requests_made.saturating_add(1);
        state.last_request_at = Some(Instant::now());
        Ok(None)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct SearchErrorClassification {
    error_type: &'static str,
    next_action: Cow<'static, str>,
    retryable: bool,
}

/// Classify a search-provider error into the stable fields exposed to the
/// agent. Anti-bot blocks are deliberately non-retryable; ordinary transport
/// and upstream 5xx failures remain explicitly retryable.
fn classify_search_error(message: &str) -> SearchErrorClassification {
    let lower = message.to_lowercase();
    let status = crate::tools::web_fetch::classify_helpers::extract_http_status(&lower);
    if matches!(status, Some(202 | 403 | 429)) || lower.contains("anti-bot") || lower.contains("challenge") {
        return SearchErrorClassification {
            error_type: "antiban_blocked",
            next_action: Cow::Owned(format!(
                "DuckDuckGo declined this request (likely an anti-bot challenge for this network). Do not retry immediately from this network; pick a result URL from this session's earlier searches and use web_fetch on it instead, or ask the user to confirm a different search provider. {GITHUB_GH_FALLBACK}"
            )),
            retryable: false,
        };
    }
    if lower.contains("response exceeds") || lower.contains("body exceeded") {
        return SearchErrorClassification {
            error_type: "response_too_large",
            next_action: Cow::Borrowed(
                "DuckDuckGo returned an oversized response. Narrow the query before explicitly retrying.",
            ),
            retryable: false,
        };
    }
    if status.is_some_and(|status| (500..=599).contains(&status)) {
        return SearchErrorClassification {
            error_type: "upstream_error",
            next_action: Cow::Borrowed(
                "The search service is currently unavailable. Retry once after a short delay, or use web_fetch on a known URL as a fallback.",
            ),
            retryable: true,
        };
    }
    if lower.contains("timeout") || lower.contains("timed out") {
        return SearchErrorClassification {
            error_type: "network_error",
            next_action: Cow::Borrowed(
                "DuckDuckGo timed out. Retry once after a short delay, or use web_fetch on a known URL as a fallback.",
            ),
            retryable: true,
        };
    }
    SearchErrorClassification {
        error_type: "network_error",
        next_action: Cow::Borrowed(
            "Retry once after a short delay, or use web_fetch directly if you already know a relevant URL.",
        ),
        retryable: true,
    }
}

fn results_response(query: &str, provider: &str, results: Vec<SearchResult>) -> Value {
    let count = results.len();
    let mut payload = json!({
        "query": query,
        "provider": provider,
        "count": count,
        "cached": false,
        "results": results
            .into_iter()
            .map(|result| json!({ "title": result.title, "url": result.url, "snippet": result.snippet }))
            .collect::<Vec<_>>(),
    });

    if count == 0 {
        payload["warning"] = json!(format!(
            "No results were returned. DuckDuckGo may have rate-limited the request or matched nothing. Try a different query, or wait a few seconds and try again. {GITHUB_GH_FALLBACK}"
        ));
    }

    payload
}

fn search_error_response(query: &str, provider: &str, message: &str) -> Value {
    let classification = classify_search_error(message);
    json!({
        "error": format!("web_search failed: {message}"),
        "query": query,
        "provider": provider,
        "error_type": classification.error_type,
        "retryable": classification.retryable,
        "next_action": classification.next_action,
    })
}

fn mark_cached(mut payload: Value, provider: &str) -> Value {
    if let Value::Object(map) = &mut payload {
        map.insert("provider".to_string(), Value::String(provider.to_owned()));
        map.insert("cached".to_string(), Value::Bool(true));
    }
    payload
}

fn cooldown_response(provider: &str, query: &str, wait: Duration) -> Value {
    let retry_after_ms = wait.as_millis().clamp(1, u64::MAX as u128) as u64;
    json!({
        "error": "web_search cooldown active",
        "query": query,
        "provider": provider,
        "error_type": "cooldown",
        "retryable": true,
        "retry_after_ms": retry_after_ms,
        "next_action": format!("Wait at least {retry_after_ms} ms before the next web search to avoid being rate-limited. {GITHUB_GH_FALLBACK}")
    })
}

fn session_cap_reached_response(provider: &str, query: &str, cap: u32) -> Value {
    json!({
        "error": "web_search session request cap reached",
        "query": query,
        "provider": provider,
        "error_type": "session_cap",
        "retryable": false,
        "session_max_requests": cap,
        "request_budget_remaining": 0,
        "next_action": format!("This session has used its {cap} web searches. Use web_fetch on a known URL or restart the session to search again. {GITHUB_GH_FALLBACK}")
    })
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn truncate_chars(input: &str, max_chars: usize) -> String {
    vtcode_commons::formatting::truncate_text(input.trim(), max_chars, "…")
}

#[async_trait]
impl Tool for WebSearchTool {
    async fn execute(&self, args: Value) -> Result<Value> {
        self.run(args).await
    }

    fn name(&self) -> &str {
        tools::WEB_SEARCH
    }

    fn description(&self) -> &str {
        WEB_SEARCH_DESCRIPTION
    }
}

#[cfg(test)]
mod tests;