//! Web search. //! //! The result will be read aloud, so what matters is not a list of links but //! an answer. That is why a search engine that summarizes is preferred //! (Tavily returns an already written paragraph), and the headlines only come //! along as a fallback when there is no summary. use std::path::PathBuf; use std::time::{Duration, Instant}; use asist_core::error::{Error, Result}; use asist_core::proc; use asist_core::tools::Tool; use serde_json::{json, Value}; /// Where the results come from. #[derive(Debug, Clone)] pub enum SearchBackend { /// Tavily API. It returns an already written answer besides the /// results, which is exactly what is needed to speak it. Tavily { api_key: String }, /// SearXNG instance, your own or a trusted one. No key, but it only /// returns results: the model has to do the summary. SearxNG { base_url: String }, /// An external program that prints the results as JSON. /// /// It is the keyless path: `scripts/search-ddgs.sh` queries DuckDuckGo and /// friends through the `ddgs` library. It works for anything else that writes /// JSON to standard output (a bridge to an MCP server, an internal search /// engine), so it is also the extension point of the search feature. /// /// In `args`, `{query}` and `{max}` are substituted before running. Command { program: PathBuf, args: Vec }, } impl SearchBackend { /// Default keyless backend: the script that wraps ddgs. pub fn ddgs(script: impl Into) -> Self { SearchBackend::Command { program: script.into(), args: vec!["{query}".into(), "{max}".into()], } } } impl SearchBackend { pub fn label(&self) -> &'static str { match self { SearchBackend::Tavily { .. } => "Tavily", SearchBackend::SearxNG { .. } => "SearXNG", SearchBackend::Command { .. } => "command", } } } pub struct WebSearch { backend: SearchBackend, max_results: usize, timeout: Duration, } impl WebSearch { pub fn new(backend: SearchBackend, max_results: usize, timeout: Duration) -> Self { Self { backend, max_results: max_results.clamp(1, 10), timeout, } } pub fn backend(&self) -> &SearchBackend { &self.backend } fn agent(&self) -> ureq::Agent { ureq::Agent::config_builder() .timeout_global(Some(self.timeout)) .build() .into() } fn fail(message: impl Into) -> Error { Error::Tool { tool: "buscar_en_internet".into(), message: message.into(), } } fn tavily(&self, key: &str, query: &str) -> Result { let body = json!({ "query": query, "max_results": self.max_results, "search_depth": "basic", // The written answer is the reason to use this engine: without it // another model round would be spent summarizing. "include_answer": true, }); let mut response = self .agent() .post("https://api.tavily.com/search") .header("Authorization", &format!("Bearer {key}")) .send_json(&body) .map_err(|e| Self::fail(describe_ureq(&e)))?; let parsed: Value = response .body_mut() .read_json() .map_err(|e| Self::fail(format!("respuesta ilegible: {e}")))?; let (answer, results) = extract(&parsed, self.max_results); compose(&answer, &results) } /// Runs the configured program and translates whatever it prints. fn command(&self, program: &std::path::Path, args: &[String], query: &str) -> Result { let rendered: Vec = args .iter() .map(|arg| { arg.replace("{query}", query) .replace("{max}", &self.max_results.to_string()) }) .collect(); // The arguments go to `execve` as they are: the query comes from what // the microphone heard, and it must never end up interpreted by a // shell. let output = proc::run(program, &rendered, self.timeout, None) .map_err(|e| Self::fail(e.to_string()))?; if !output.success() { return Err(Self::fail(format!( "el buscador falló: {}", output.last_error_line() ))); } let parsed: Value = serde_json::from_slice(&output.stdout) .map_err(|e| Self::fail(format!("el buscador no devolvió JSON válido: {e}")))?; let (answer, results) = extract(&parsed, self.max_results); compose(&answer, &results) } fn searxng(&self, base_url: &str, query: &str) -> Result { let url = format!( "{}/search?q={}&format=json&language=es", base_url.trim_end_matches('/'), urlencode(query) ); let mut response = self .agent() .get(&url) .call() .map_err(|e| Self::fail(describe_ureq(&e)))?; let parsed: Value = response .body_mut() .read_json() .map_err(|e| Self::fail(format!("respuesta ilegible: {e}")))?; let (answer, results) = extract(&parsed, self.max_results); compose(&answer, &results) } } impl Tool for WebSearch { fn name(&self) -> &str { "buscar_en_internet" } fn description(&self) -> &str { "Busca información actual en internet y devuelve un resumen. Úsala para \ noticias, precios, resultados, el tiempo o cualquier cosa posterior a tu \ entrenamiento, en vez de responder de memoria." } fn parameters(&self) -> Value { json!({ "type": "object", "properties": { "consulta": { "type": "string", "description": "Qué buscar, en lenguaje natural. Por ejemplo: tiempo en Buenos Aires mañana" } }, "required": ["consulta"] }) } fn acknowledgement(&self) -> Option<&str> { Some("Déjame que lo busque.") } fn call(&self, args: &Value) -> Result { let query = args .get("consulta") .and_then(Value::as_str) .map(str::trim) .filter(|q| !q.is_empty()) .ok_or_else(|| Self::fail("falta «consulta»"))?; let started = Instant::now(); let result = match &self.backend { SearchBackend::Tavily { api_key } => self.tavily(api_key, query), SearchBackend::SearxNG { base_url } => self.searxng(base_url, query), SearchBackend::Command { program, args } => self.command(program, args, query), }; tracing::info!( target: "tools", backend = self.backend.label(), query = query, ms = started.elapsed().as_millis(), ok = result.is_ok(), "search" ); result } } /// Pulls the answer and results out of a JSON without requiring a specific /// shape. /// /// Each engine names the fields its own way (`href` or `url`, `body` or /// `content`, a bare list or inside `results`), and what matters here is that /// a new script works without touching Rust. fn extract(parsed: &Value, max: usize) -> (String, Vec) { let answer = parsed .get("answer") .and_then(Value::as_str) .unwrap_or("") .trim() .to_string(); let rows = parsed .as_array() .or_else(|| parsed.get("results").and_then(Value::as_array)); let results = rows .map(|items| { items .iter() .take(max) .filter_map(|item| { let pick = |keys: &[&str]| { keys.iter() .find_map(|k| item.get(*k).and_then(Value::as_str)) .unwrap_or("") .trim() .to_string() }; let title = pick(&["title", "titulo", "name"]); let snippet = pick(&["body", "content", "snippet", "description"]); if title.is_empty() && snippet.is_empty() { return None; } Some(if title.is_empty() { clip(&snippet, 200) } else { format!("{title}: {}", clip(&snippet, 200)) }) }) .collect() }) .unwrap_or_default(); (answer, results) } /// Joins the summarized answer with the headlines. /// /// The summary goes first because it is what will probably be spoken; the /// headlines stay behind so the model has something to draw on if the /// question asked for a detail the summary does not cover. fn compose(answer: &str, results: &[String]) -> Result { if answer.is_empty() && results.is_empty() { return Err(Error::Tool { tool: "buscar_en_internet".into(), message: "la búsqueda no devolvió nada".into(), }); } let mut out = String::new(); if !answer.is_empty() { out.push_str(answer); } if !results.is_empty() { if !out.is_empty() { out.push_str("\n\nFuentes:\n"); } for (i, result) in results.iter().enumerate() { out.push_str(&format!("{}. {result}\n", i + 1)); } } Ok(out.trim().to_string()) } fn clip(text: &str, max: usize) -> String { let flat = text.split_whitespace().collect::>().join(" "); if flat.chars().count() <= max { return flat; } flat.chars().take(max).collect::() + "…" } /// A ureq error carries the whole chain with the URL inside, and for Tavily /// that URL does not hold the key (it goes in a header), but better not to get /// used to it: the error is summarized instead of dumped whole. fn describe_ureq(err: &ureq::Error) -> String { match err { ureq::Error::StatusCode(code) => match code { 401 | 403 => format!("el buscador rechazó la clave (HTTP {code})"), 429 => "se ha superado el límite de consultas del buscador".into(), other => format!("el buscador respondió HTTP {other}"), }, ureq::Error::Timeout(_) => "el buscador tardó demasiado".into(), other => format!("no se pudo consultar el buscador: {other}"), } } fn urlencode(text: &str) -> String { let mut out = String::with_capacity(text.len()); for byte in text.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('+'), other => out.push_str(&format!("%{other:02X}")), } } out } #[cfg(test)] mod tests { use super::*; #[test] fn synthesized_answer_goes_before_the_sources() { let out = compose("Hace 17 grados.", &["Meteored: parcialmente nuboso".into()]).unwrap(); assert!(out.starts_with("Hace 17 grados.")); assert!(out.contains("Fuentes:")); } #[test] fn without_an_answer_the_headlines_are_used() { let out = compose("", &["Uno: algo".into(), "Dos: otra cosa".into()]).unwrap(); assert!(out.starts_with("1. Uno")); assert!(out.contains("2. Dos")); } #[test] fn search_without_results_is_an_error_not_an_empty_string() { // If it returned "" the model would make the answer up believing the // tool had worked. assert!(compose("", &[]).is_err()); } #[test] fn long_snippets_are_truncated() { let long_text = "palabra ".repeat(80); assert!(clip(&long_text, 100).chars().count() <= 101); assert_eq!(clip("corto", 100), "corto"); } #[test] fn snippet_newlines_are_flattened() { assert_eq!(clip("uno\n\n dos", 100), "uno dos"); } #[test] fn query_is_url_encoded() { assert_eq!(urlencode("qué tiempo hace"), "qu%C3%A9+tiempo+hace"); assert_eq!(urlencode("a&b=c"), "a%26b%3Dc"); } #[test] fn empty_query_is_rejected_without_network() { let tool = WebSearch::new( SearchBackend::Tavily { api_key: "x".into(), }, 3, Duration::from_secs(1), ); assert!(tool.call(&json!({ "consulta": " " })).is_err()); assert!(tool.call(&json!({})).is_err()); } #[test] fn bare_ddgs_list_is_understood() { let raw = serde_json::json!([ { "title": "Canberra", "href": "https://x", "body": "es la capital" }, { "title": "Sídney", "href": "https://y", "body": "no lo es" } ]); let (answer, results) = extract(&raw, 5); assert!(answer.is_empty(), "ddgs no sintetiza"); assert_eq!(results.len(), 2); assert!(results[0].starts_with("Canberra: es la capital")); } #[test] fn results_and_answer_shape_is_understood_too() { let raw = serde_json::json!({ "answer": "Canberra.", "results": [{ "title": "T", "url": "https://x", "content": "C" }] }); let (answer, results) = extract(&raw, 5); assert_eq!(answer, "Canberra."); assert_eq!(results, vec!["T: C"]); } #[test] fn maximum_results_is_honoured() { let raw = serde_json::json!([ { "title": "1", "body": "a" }, { "title": "2", "body": "b" }, { "title": "3", "body": "c" } ]); assert_eq!(extract(&raw, 2).1.len(), 2); } #[test] fn row_without_title_or_text_is_ignored() { let raw = serde_json::json!([{ "href": "https://x" }, { "title": "T", "body": "C" }]); assert_eq!(extract(&raw, 5).1, vec!["T: C"]); } #[test] fn command_placeholders_are_substituted() { let tool = WebSearch::new(SearchBackend::ddgs("/bin/echo"), 4, Duration::from_secs(5)); let SearchBackend::Command { args, .. } = tool.backend() else { panic!("expected a command backend"); }; let rendered: Vec = args .iter() .map(|a| a.replace("{query}", "hola").replace("{max}", "4")) .collect(); assert_eq!(rendered, vec!["hola", "4"]); } #[test] fn missing_command_gives_a_useful_error() { let tool = WebSearch::new( SearchBackend::ddgs("/no/existe/buscador.sh"), 3, Duration::from_secs(5), ); let err = tool .call(&json!({ "consulta": "algo" })) .unwrap_err() .to_string(); assert!(err.contains("no se pudo ejecutar"), "{err}"); } #[test] fn result_count_stays_in_a_sensible_range() { let tool = WebSearch::new( SearchBackend::Tavily { api_key: "x".into(), }, 99, Duration::from_secs(1), ); assert_eq!(tool.max_results, 10); } }