Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

77.2 KB
Raw Blame
//! Native, provider-neutral web search backend.
//!
//! The `web_search` / `web_fetch` capability is defined by the [`SearchProvider`]
//! trait; concrete backends implement it and are selected at call time by
//! [`create_provider`]. Adding a backend is a new impl plus one match arm - the
//! plugin command contract and the whole frontend stay unchanged.
//!
//! The only backend shipped today is [`ExaProvider`] (the default). Exa is a
//! backend, never a product-facing identity:
//!
//! * Keyless (default): Exa's hosted endpoint at `https://mcp.exa.ai/mcp`
//!   answers over JSON-RPC with no API key.
//! * Keyed (opt-in): when an Exa API key is supplied the structured REST API
//!   (`https://api.exa.ai`) is used instead, for normalized JSON and higher
//!   rate limits.
//!
//! Every backend normalizes into [`SearchResult`] / [`FetchedPage`] and bounds
//! output size so tool results can't blow up the model's context.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

const FETCH_MAX_CHARS: usize = 40_000;
pub const SEARCH_DEFAULT_COUNT: u32 = 5;
pub const SEARCH_MAX_COUNT: u32 = 20;
const REQUEST_TIMEOUT_SECS: u64 = 30;

const EXA_HOSTED_URL: &str = "https://mcp.exa.ai/mcp";
const EXA_REST_SEARCH_URL: &str = "https://api.exa.ai/search";
const EXA_REST_CONTENTS_URL: &str = "https://api.exa.ai/contents";

const TAVILY_SEARCH_URL: &str = "https://api.tavily.com/search";
const TAVILY_EXTRACT_URL: &str = "https://api.tavily.com/extract";

const YOU_COM_HOSTED_URL: &str = "https://api.you.com/mcp?profile=free";
// Identifies the calling application to You.com, per the convention its other
// integrations follow. Carries no user or query data beyond the request itself,
// and is sent only on requests to You.com.
const YOU_COM_USER_AGENT: &str = concat!(
    "jan-websearch/",
    env!("CARGO_PKG_VERSION"),
    " (https://github.com/janhq/jan)"
);
const YOU_COM_CLIENT_INFO: &str = concat!(
    "plugin; client=jan-websearch/",
    env!("CARGO_PKG_VERSION"),
    "; ua=rust/unknown"
);
const YOU_COM_SEARCH_URL: &str = "https://ydc-index.io/v1/search";
const YOU_COM_CONTENTS_URL: &str = "https://ydc-index.io/v1/contents";
const YOU_COM_EXTRACT_URL: &str = "https://api.you.com/v1/extract";

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SearchResult {
    pub title: String,
    pub url: String,
    pub snippet: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub published_at: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FetchedPage {
    pub url: String,
    pub title: String,
    pub content: String,
    pub truncated: bool,
}

/// A pluggable web search backend. Implementors turn a query into normalized
/// [`SearchResult`]s and a URL into a bounded [`FetchedPage`].
#[async_trait]
pub trait SearchProvider: Send + Sync {
    async fn search(&self, query: &str, count: u32) -> Result<Vec<SearchResult>, String>;
    async fn fetch(&self, url: &str) -> Result<FetchedPage, String>;
}

/// Static description of one selectable backend: what settings surfaces need to
/// render its form (does it need a key, an instance URL) and where its API key
/// lives in the secret store. The desktop's `WEB_SEARCH_PROVIDERS` uses the
/// same ids and secret keys, so a key saved on one surface is found by the other.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProviderMeta {
    /// Id accepted by [`create_provider`].
    pub id: &'static str,
    pub label: &'static str,
    /// Works with no API key (a key only upgrades it).
    pub keyless: bool,
    /// Secret-store record holding the API key; `None` for key-less backends.
    pub secret_key: Option<&'static str>,
    /// Needs a user-supplied instance URL (`endpoint`).
    pub requires_endpoint: bool,
}

/// Every backend [`create_provider`] accepts, default first.
pub const PROVIDERS: &[ProviderMeta] = &[
    ProviderMeta {
        id: "exa",
        label: "Exa",
        keyless: true,
        secret_key: Some("exa-api-key"),
        requires_endpoint: false,
    },
    ProviderMeta {
        id: "tavily",
        label: "Tavily",
        keyless: false,
        secret_key: Some("tavily-api-key"),
        requires_endpoint: false,
    },
    ProviderMeta {
        id: "searxng",
        label: "SearXNG",
        keyless: true,
        secret_key: None,
        requires_endpoint: true,
    },
    ProviderMeta {
        id: "you",
        label: "You.com",
        keyless: true,
        secret_key: Some("you-api-key"),
        requires_endpoint: false,
    },
];

/// Look up a backend by id (case-insensitive; empty/absent is the default).
pub fn provider_meta(provider: Option<&str>) -> Option<&'static ProviderMeta> {
    match provider.map(|s| s.trim().to_ascii_lowercase()).as_deref() {
        None | Some("") => PROVIDERS.first(),
        Some(id) => PROVIDERS.iter().find(|p| p.id == id),
    }
}

/// A resolved backend selection: what [`create_provider`] is called with. Built
/// by the host from its own settings store; `Default` is the keyless default.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct WebSearchConfig {
    pub provider: Option<String>,
    pub api_key: Option<String>,
    pub endpoint: Option<String>,
}

impl std::fmt::Debug for WebSearchConfig {
    /// Hand-written so the API key never reaches a log: hosts embed this in
    /// structs that derive `Debug`.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WebSearchConfig")
            .field("provider", &self.provider)
            .field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
            .field("endpoint", &self.endpoint)
            .finish()
    }
}

impl WebSearchConfig {
    /// Build the backend this selection names.
    pub fn create(&self) -> Result<Box<dyn SearchProvider>, String> {
        create_provider(
            self.provider.as_deref(),
            self.api_key.clone(),
            self.endpoint.clone(),
        )
    }
}

/// Build the backend for `provider` (case-insensitive; empty/absent selects the
/// default). `api_key` is used by keyed backends; `endpoint` by self-hosted ones
/// (e.g. a SearXNG instance URL).
pub fn create_provider(
    provider: Option<&str>,
    api_key: Option<String>,
    endpoint: Option<String>,
) -> Result<Box<dyn SearchProvider>, String> {
    match provider.map(|s| s.trim().to_ascii_lowercase()).as_deref() {
        None | Some("") | Some("exa") => Ok(Box::new(ExaProvider::new(api_key)?)),
        Some("tavily") => Ok(Box::new(TavilyProvider::new(api_key)?)),
        Some("searxng") => Ok(Box::new(SearxngProvider::new(endpoint)?)),
        Some("you") => Ok(Box::new(YouComProvider::new(api_key)?)),
        Some(other) => Err(format!("Unknown web search provider '{other}'")),
    }
}

fn require_key(provider: &str, api_key: Option<String>) -> Result<String, String> {
    normalize_key(api_key).ok_or_else(|| format!("{provider} requires an API key"))
}

fn build_http_client(provider: &str) -> Result<reqwest::Client, String> {
    reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(REQUEST_TIMEOUT_SECS))
        .build()
        .map_err(|e| format!("failed to build HTTP client for {provider}: {e}"))
}

/// Which Exa transport the adapter uses.
#[derive(Debug, Clone, PartialEq, Eq)]
enum ExaMode {
    Hosted,
    Rest(String),
}

fn normalize_key(api_key: Option<String>) -> Option<String> {
    api_key.and_then(|v| {
        let v = v.trim().to_string();
        if v.is_empty() || v == "YOUR_EXA_API_KEY_HERE" {
            None
        } else {
            Some(v)
        }
    })
}

/// Exa backend. Defaults to the keyless hosted endpoint; upgrades to the
/// structured REST API when a key is supplied.
pub struct ExaProvider {
    mode: ExaMode,
    client: reqwest::Client,
}

impl ExaProvider {
    pub fn new(api_key: Option<String>) -> Result<Self, String> {
        let mode = match normalize_key(api_key) {
            Some(key) => ExaMode::Rest(key),
            None => ExaMode::Hosted,
        };
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(REQUEST_TIMEOUT_SECS))
            .build()
            .map_err(|e| format!("failed to build HTTP client for Exa: {e}"))?;
        Ok(Self { mode, client })
    }

    async fn hosted_call(&self, tool: &str, arguments: Value) -> Result<String, String> {
        let body = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "tools/call",
            "params": { "name": tool, "arguments": arguments }
        });
        let resp = self
            .client
            .post(EXA_HOSTED_URL)
            .header("content-type", "application/json")
            .header("accept", "application/json, text/event-stream")
            .json(&body)
            .send()
            .await
            .map_err(|e| format!("Exa request failed: {e}"))?;
        let status = resp.status();
        let text = resp
            .text()
            .await
            .map_err(|e| format!("Exa: failed to read response body: {e}"))?;
        if !status.is_success() {
            return Err(format!(
                "Exa failed with HTTP {}: {}",
                status.as_u16(),
                text.chars().take(400).collect::<String>()
            ));
        }
        parse_hosted_result_text(&text, "Exa")
    }
}

#[async_trait]
impl SearchProvider for ExaProvider {
    async fn search(&self, query: &str, count: u32) -> Result<Vec<SearchResult>, String> {
        match &self.mode {
            ExaMode::Hosted => {
                let text = self
                    .hosted_call(
                        "web_search_exa",
                        json!({ "query": query, "numResults": count }),
                    )
                    .await?;
                Ok(parse_hosted_search_text(&text))
            }
            ExaMode::Rest(key) => {
                let body = json!({
                    "query": query,
                    "type": "auto",
                    "numResults": count,
                    "contents": {
                        "text": { "maxCharacters": 800 },
                        "highlights": { "numSentences": 3, "highlightsPerUrl": 1 }
                    }
                });
                let resp = self
                    .client
                    .post(EXA_REST_SEARCH_URL)
                    .header("x-api-key", key)
                    .header("content-type", "application/json")
                    .json(&body)
                    .send()
                    .await
                    .map_err(|e| format!("Exa search request failed: {e}"))?;
                let status = resp.status();
                let text = resp
                    .text()
                    .await
                    .map_err(|e| format!("Exa search: failed to read response body: {e}"))?;
                if !status.is_success() {
                    return Err(format!(
                        "Exa search failed with HTTP {}: {}",
                        status.as_u16(),
                        text.chars().take(400).collect::<String>()
                    ));
                }
                let parsed: Value = serde_json::from_str(&text)
                    .map_err(|e| format!("Exa search: invalid JSON response: {e}"))?;
                Ok(normalize_exa_rest_search(&parsed))
            }
        }
    }

    async fn fetch(&self, url: &str) -> Result<FetchedPage, String> {
        match &self.mode {
            ExaMode::Hosted => {
                let text = self
                    .hosted_call(
                        "web_fetch_exa",
                        json!({ "urls": [url], "maxCharacters": FETCH_MAX_CHARS }),
                    )
                    .await?;
                Ok(parse_hosted_fetch_text(&text, url))
            }
            ExaMode::Rest(key) => {
                let body = json!({ "ids": [url], "text": true });
                let resp = self
                    .client
                    .post(EXA_REST_CONTENTS_URL)
                    .header("x-api-key", key)
                    .header("content-type", "application/json")
                    .json(&body)
                    .send()
                    .await
                    .map_err(|e| format!("Exa fetch request failed: {e}"))?;
                let status = resp.status();
                let text = resp
                    .text()
                    .await
                    .map_err(|e| format!("Exa fetch: failed to read response body: {e}"))?;
                if !status.is_success() {
                    return Err(format!(
                        "Exa fetch failed with HTTP {}: {}",
                        status.as_u16(),
                        text.chars().take(400).collect::<String>()
                    ));
                }
                let parsed: Value = serde_json::from_str(&text)
                    .map_err(|e| format!("Exa fetch: invalid JSON response: {e}"))?;
                normalize_exa_rest_fetch(&parsed, url)
            }
        }
    }
}

/// Pull the tool payload out of an MCP `tools/call` response. Streamable HTTP
/// answers with either a bare JSON-RPC object or an SSE stream, and a stream may
/// carry progress notifications ahead of the answer, so the frame holding
/// `result` or `error` is the one to read.
fn parse_hosted_result_text(body: &str, provider: &str) -> Result<String, String> {
    let envelope = match body
        .lines()
        .filter_map(|l| l.strip_prefix("data:").map(str::trim))
        .filter_map(|frame| serde_json::from_str::<Value>(frame).ok())
        .find(|v| v.get("result").is_some() || v.get("error").is_some())
    {
        Some(frame) => frame,
        None => serde_json::from_str::<Value>(body.trim())
            .map_err(|e| format!("{provider}: invalid response payload: {e}"))?,
    };
    if let Some(err) = envelope.get("error") {
        return Err(format!("{provider} returned an error: {err}"));
    }
    let result = envelope
        .get("result")
        .ok_or_else(|| format!("{provider} response missing 'result'"))?;
    let text = result
        .get("content")
        .and_then(|c| c.as_array())
        .and_then(|a| a.first())
        .and_then(|c| c.get("text"))
        .and_then(|v| v.as_str());
    // A throttled hosted endpoint answers HTTP 200 with no `isError`, putting
    // the refusal in the text block where it parses as zero results. Reported
    // as success that reads to a model as "the web has nothing", so the flag in
    // `_meta` is the only thing separating a refusal from a real empty answer.
    if is_rate_limited(result) {
        return Err(format!(
            "{provider} rate limit reached: {}",
            text.unwrap_or("no quota remaining on the keyless endpoint")
        ));
    }
    if result.get("isError").and_then(|v| v.as_bool()) == Some(true) {
        return Err(format!(
            "{provider} tool call failed: {}",
            text.unwrap_or("unknown error")
        ));
    }
    text.map(str::to_string)
        .ok_or_else(|| format!("{provider} response had no text content"))
}

/// Whether a hosted `result` carries a provider rate-limit marker in `_meta`.
///
/// Namespaced per vendor (`ai.exa/rateLimited`), so the suffix is matched
/// rather than one hard-coded key: the hosted transport is shared by every
/// keyless backend here.
fn is_rate_limited(result: &Value) -> bool {
    result
        .get("_meta")
        .and_then(|m| m.as_object())
        .is_some_and(|meta| {
            meta.iter().any(|(k, v)| {
                (k == "rateLimited" || k.ends_with("/rateLimited")) && v.as_bool() == Some(true)
            })
        })
}

fn parse_hosted_search_text(text: &str) -> Vec<SearchResult> {
    let mut out = Vec::new();
    for block in text.split("\n---\n") {
        let block = block.trim();
        if block.is_empty() {
            continue;
        }
        let mut title = String::new();
        let mut url = String::new();
        let mut published: Option<String> = None;
        let mut in_highlights = false;
        let mut snippet_lines: Vec<String> = Vec::new();
        for line in block.lines() {
            let trimmed = line.trim();
            if let Some(v) = trimmed.strip_prefix("Title:") {
                title = v.trim().to_string();
            } else if let Some(v) = trimmed.strip_prefix("URL:") {
                url = v.trim().to_string();
            } else if let Some(v) = trimmed.strip_prefix("Published:") {
                let v = v.trim();
                if !v.is_empty() && v != "N/A" {
                    published = Some(v.to_string());
                }
            } else if trimmed.starts_with("Author:") {
                // Ignored in the normalized contract.
            } else if trimmed.starts_with("Highlights:") {
                in_highlights = true;
            } else if in_highlights && trimmed != "..." && !trimmed.is_empty() {
                snippet_lines.push(trimmed.to_string());
            }
        }
        if url.is_empty() && title.is_empty() {
            continue;
        }
        let snippet = clip_chars(&snippet_lines.join(" "), 500);
        out.push(SearchResult {
            title,
            url,
            snippet,
            published_at: published,
        });
    }
    out
}

fn parse_hosted_fetch_text(text: &str, requested_url: &str) -> FetchedPage {
    let mut title = String::new();
    let mut url = requested_url.to_string();
    let mut body_start = 0usize;
    for (i, line) in text.lines().enumerate() {
        let trimmed = line.trim();
        if i == 0 && trimmed.starts_with("# ") {
            title = trimmed[2..].trim().to_string();
        } else if let Some(v) = trimmed.strip_prefix("URL:") {
            url = v.trim().to_string();
            body_start = i + 1;
            break;
        } else if i > 2 {
            break;
        }
    }
    let body: String = text
        .lines()
        .skip(body_start)
        .collect::<Vec<_>>()
        .join("\n")
        .trim()
        .to_string();
    let source = if body.is_empty() { text.trim() } else { &body };
    let (content, truncated) = bound_text(source);
    FetchedPage {
        url,
        title,
        content,
        truncated,
    }
}

fn normalize_exa_rest_search(body: &Value) -> Vec<SearchResult> {
    let Some(results) = body.get("results").and_then(|v| v.as_array()) else {
        return Vec::new();
    };
    results
        .iter()
        .map(|r| {
            let title = r
                .get("title")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let url = r
                .get("url")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let snippet = r
                .get("highlights")
                .and_then(|v| v.as_array())
                .and_then(|a| a.first())
                .and_then(|v| v.as_str())
                .map(str::to_string)
                .or_else(|| {
                    r.get("text")
                        .and_then(|v| v.as_str())
                        .map(|t| clip_chars(t, 300))
                })
                .unwrap_or_default();
            let published_at = r
                .get("publishedDate")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty())
                .map(str::to_string);
            SearchResult {
                title,
                url,
                snippet,
                published_at,
            }
        })
        .collect()
}

fn normalize_exa_rest_fetch(body: &Value, requested_url: &str) -> Result<FetchedPage, String> {
    let first = body
        .get("results")
        .and_then(|v| v.as_array())
        .and_then(|a| a.first())
        .ok_or_else(|| format!("Exa fetch returned no content for {requested_url}"))?;
    let url = first
        .get("url")
        .and_then(|v| v.as_str())
        .unwrap_or(requested_url)
        .to_string();
    let title = first
        .get("title")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let raw = first.get("text").and_then(|v| v.as_str()).unwrap_or("");
    let (content, truncated) = bound_text(raw);
    Ok(FetchedPage {
        url,
        title,
        content,
        truncated,
    })
}

/// Tavily backend (key-only). Uses the structured `/search` and `/extract`
/// REST endpoints, authenticated with a bearer token.
pub struct TavilyProvider {
    api_key: String,
    client: reqwest::Client,
}

impl TavilyProvider {
    pub fn new(api_key: Option<String>) -> Result<Self, String> {
        Ok(Self {
            api_key: require_key("Tavily", api_key)?,
            client: build_http_client("Tavily")?,
        })
    }

    async fn post(&self, url: &str, body: Value) -> Result<Value, String> {
        let resp = self
            .client
            .post(url)
            .bearer_auth(&self.api_key)
            .header("content-type", "application/json")
            .json(&body)
            .send()
            .await
            .map_err(|e| format!("Tavily request failed: {e}"))?;
        let status = resp.status();
        let text = resp
            .text()
            .await
            .map_err(|e| format!("Tavily: failed to read response body: {e}"))?;
        if !status.is_success() {
            return Err(format!(
                "Tavily failed with HTTP {}: {}",
                status.as_u16(),
                text.chars().take(400).collect::<String>()
            ));
        }
        serde_json::from_str(&text).map_err(|e| format!("Tavily: invalid JSON response: {e}"))
    }
}

#[async_trait]
impl SearchProvider for TavilyProvider {
    async fn search(&self, query: &str, count: u32) -> Result<Vec<SearchResult>, String> {
        let parsed = self
            .post(
                TAVILY_SEARCH_URL,
                json!({ "query": query, "max_results": count }),
            )
            .await?;
        Ok(normalize_tavily_search(&parsed))
    }

    async fn fetch(&self, url: &str) -> Result<FetchedPage, String> {
        let parsed = self
            .post(TAVILY_EXTRACT_URL, json!({ "urls": [url] }))
            .await?;
        normalize_tavily_extract(&parsed, url)
    }
}

fn normalize_tavily_search(body: &Value) -> Vec<SearchResult> {
    let Some(results) = body.get("results").and_then(|v| v.as_array()) else {
        return Vec::new();
    };
    results
        .iter()
        .map(|r| {
            let title = r.get("title").and_then(|v| v.as_str()).unwrap_or("");
            let url = r.get("url").and_then(|v| v.as_str()).unwrap_or("");
            let snippet = r
                .get("content")
                .and_then(|v| v.as_str())
                .map(|t| clip_chars(t, 500))
                .unwrap_or_default();
            let published_at = r
                .get("published_date")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty())
                .map(str::to_string);
            SearchResult {
                title: title.to_string(),
                url: url.to_string(),
                snippet,
                published_at,
            }
        })
        .collect()
}

fn normalize_tavily_extract(body: &Value, requested_url: &str) -> Result<FetchedPage, String> {
    let first = body
        .get("results")
        .and_then(|v| v.as_array())
        .and_then(|a| a.first())
        .ok_or_else(|| format!("Tavily returned no content for {requested_url}"))?;
    let url = first
        .get("url")
        .and_then(|v| v.as_str())
        .unwrap_or(requested_url)
        .to_string();
    let raw = first
        .get("raw_content")
        .and_then(|v| v.as_str())
        .unwrap_or("");
    let (content, truncated) = bound_text(raw);
    Ok(FetchedPage {
        url,
        title: String::new(),
        content,
        truncated,
    })
}

/// Attach the shared You.com request headers. Callers set the body with
/// `.json()`, which already sets `Content-Type: application/json` — setting it
/// again here would duplicate the header (reqwest's `.header()` appends), and
/// api.you.com rejects a multi-valued `Content-Type` with HTTP 415.
fn with_youcom_headers(request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
    request
        .header("user-agent", YOU_COM_USER_AGENT)
        .header("x-client-info", YOU_COM_CLIENT_INFO)
}

/// Which You.com transport the adapter uses.
#[derive(Debug, Clone, PartialEq, Eq)]
enum YouComMode {
    Hosted,
    Rest(String),
}

/// You.com backend. Defaults to the keyless hosted endpoint; upgrades to the
/// structured REST API when a key is supplied.
///
/// * Keyless (default): the hosted MCP endpoint answers `you-search` with no
///   credentials, and `web_fetch` reads the page as markdown from the keyless
///   `/v1/extract` endpoint. When extraction is refused, `web_fetch` falls back
///   to a direct GET, the same way [`SearxngProvider`] reads pages.
/// * Keyed (opt-in): the structured REST API at `https://ydc-index.io`, which
///   adds `/v1/contents` for real content extraction and lifts the free tier's
///   request cap.
///
/// Both transports return the same search payload, so one normalizer serves
/// both.
pub struct YouComProvider {
    mode: YouComMode,
    client: reqwest::Client,
}

impl YouComProvider {
    pub fn new(api_key: Option<String>) -> Result<Self, String> {
        let mode = match normalize_key(api_key) {
            Some(key) => YouComMode::Rest(key),
            None => YouComMode::Hosted,
        };
        Ok(Self {
            mode,
            client: build_http_client("You.com")?,
        })
    }

    /// Send a prepared request and return its status and body, failing only
    /// on transport errors.
    async fn send_raw(&self, request: reqwest::RequestBuilder) -> Result<(u16, String), String> {
        let resp = with_youcom_headers(request)
            .send()
            .await
            .map_err(|e| format!("You.com request failed: {e}"))?;
        let status = resp.status().as_u16();
        let text = resp
            .text()
            .await
            .map_err(|e| format!("You.com: failed to read response body: {e}"))?;
        Ok((status, text))
    }

    /// Send a prepared request and return the body, mapping transport and HTTP
    /// failures into one message shape for both transports.
    async fn send(&self, request: reqwest::RequestBuilder) -> Result<String, String> {
        let (status, text) = self.send_raw(request).await?;
        if !(200..300).contains(&status) {
            return Err(format!(
                "You.com failed with HTTP {status}: {}",
                clip_chars(&text, 400)
            ));
        }
        Ok(text)
    }

    /// Keyed transport: plain JSON POST authenticated with `X-API-Key`.
    async fn post_rest(&self, url: &str, key: &str, body: Value) -> Result<Value, String> {
        let text = self
            .send(self.client.post(url).header("X-API-Key", key).json(&body))
            .await?;
        serde_json::from_str(&text).map_err(|e| format!("You.com: invalid JSON response: {e}"))
    }

    /// Keyless transport: one MCP `tools/call` for `you-search`, the only tool
    /// the free profile exposes. The tool payload is the same JSON the REST
    /// endpoint returns, so the caller can hand it to the same normalizer.
    async fn hosted_search(&self, arguments: Value) -> Result<Value, String> {
        let text = self
            .send(
                self.client
                    .post(YOU_COM_HOSTED_URL)
                    .header("accept", "application/json, text/event-stream")
                    .json(&json!({
                        "jsonrpc": "2.0",
                        "id": 1,
                        "method": "tools/call",
                        "params": { "name": "you-search", "arguments": arguments }
                    })),
            )
            .await?;
        let payload = parse_hosted_result_text(&text, "You.com")?;
        serde_json::from_str(&payload)
            .map_err(|e| format!("You.com: invalid JSON in tool result: {e}"))
    }

    /// Keyless page extraction: a plain JSON POST with no credentials.
    fn extract_request(&self, url: &str) -> reqwest::RequestBuilder {
        self.client
            .post(YOU_COM_EXTRACT_URL)
            .json(&json!({ "url": url }))
    }

    /// Keyless `web_fetch`: markdown from `/v1/extract`, falling back to a
    /// direct GET when extraction is refused (see [`extract_fallback`]).
    async fn keyless_fetch(&self, url: &str) -> Result<FetchedPage, String> {
        let note = match self.send_raw(self.extract_request(url)).await {
            Ok((200, body)) => {
                let page = serde_json::from_str::<Value>(&body)
                    .ok()
                    .and_then(|v| normalize_youcom_extract(&v, url).ok());
                if let Some(page) = page {
                    return Ok(page);
                }
                None
            }
            Ok((status, body)) => extract_fallback(status, &body)?,
            Err(_) => None,
        };
        match (fetch_url_direct(url, "You.com").await, note) {
            (Ok(page), Some(note)) => Ok(prefix_extract_note(page, &note)),
            (Ok(page), None) => Ok(page),
            (Err(e), Some(note)) => Err(format!("You.com extraction unavailable ({note}); {e}")),
            (Err(e), None) => Err(e),
        }
    }
}

#[async_trait]
impl SearchProvider for YouComProvider {
    async fn search(&self, query: &str, count: u32) -> Result<Vec<SearchResult>, String> {
        let args = json!({ "query": query, "count": count });
        let parsed = match &self.mode {
            YouComMode::Hosted => self.hosted_search(args).await?,
            YouComMode::Rest(key) => self.post_rest(YOU_COM_SEARCH_URL, key, args).await?,
        };
        Ok(normalize_youcom_search(&parsed))
    }

    async fn fetch(&self, url: &str) -> Result<FetchedPage, String> {
        match &self.mode {
            YouComMode::Hosted => self.keyless_fetch(url).await,
            YouComMode::Rest(key) => {
                let parsed = self
                    .post_rest(
                        YOU_COM_CONTENTS_URL,
                        key,
                        json!({ "urls": [url], "formats": ["markdown"] }),
                    )
                    .await?;
                normalize_youcom_contents(&parsed, url)
            }
        }
    }
}

/// `/v1/search` groups results into sections. `web` is the section jan's
/// `web_search` contract maps to, so a response carrying no `web` section
/// (news-only, or empty) yields no results rather than falling back to `news`.
fn normalize_youcom_search(body: &Value) -> Vec<SearchResult> {
    let Some(web) = body
        .get("results")
        .and_then(|r| r.get("web"))
        .and_then(|v| v.as_array())
    else {
        return Vec::new();
    };
    web.iter()
        .map(|r| {
            let title = r.get("title").and_then(|v| v.as_str()).unwrap_or("");
            let url = r.get("url").and_then(|v| v.as_str()).unwrap_or("");
            // `description` is the whole-result summary; `snippets` are keyword
            // fragments, used only when a result carries no description.
            let snippet = r
                .get("description")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty())
                .or_else(|| {
                    r.get("snippets")
                        .and_then(|v| v.as_array())
                        .and_then(|a| a.first())
                        .and_then(|v| v.as_str())
                })
                .map(|s| clip_chars(s, 500))
                .unwrap_or_default();
            let published_at = r
                .get("page_age")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty())
                .map(str::to_string);
            SearchResult {
                title: title.to_string(),
                url: url.to_string(),
                snippet,
                published_at,
            }
        })
        .collect()
}

/// `/v1/contents` answers with a bare array, one entry per requested URL.
/// The request asks for `markdown`, so that is the only content field read.
/// The spec types `markdown` as nullable — null is how the API reports a
/// failed extraction — so a missing or empty `markdown` is an error, not an
/// empty page.
fn normalize_youcom_contents(body: &Value, requested_url: &str) -> Result<FetchedPage, String> {
    let first = body
        .as_array()
        .and_then(|a| a.first())
        .ok_or_else(|| format!("You.com returned no content for {requested_url}"))?;
    let url = first
        .get("url")
        .and_then(|v| v.as_str())
        .unwrap_or(requested_url)
        .to_string();
    let title = first
        .get("title")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    let raw = first
        .get("markdown")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .ok_or_else(|| format!("You.com returned no content for {requested_url}"))?;
    let (content, truncated) = bound_text(raw);
    Ok(FetchedPage {
        url,
        title,
        content,
        truncated,
    })
}

/// Keyless `/v1/extract` answers one object per page, with the markdown under
/// `contents.full_page`. A missing or empty `markdown` is an error, not an
/// empty page: it is also how a skipped PDF comes back (`{}`).
fn normalize_youcom_extract(body: &Value, requested_url: &str) -> Result<FetchedPage, String> {
    let raw = body
        .pointer("/contents/full_page/markdown")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .ok_or_else(|| format!("You.com returned no content for {requested_url}"))?;
    let (content, truncated) = bound_text(raw);
    Ok(FetchedPage {
        url: body
            .get("url")
            .and_then(|v| v.as_str())
            .unwrap_or(requested_url)
            .to_string(),
        title: body
            .get("title")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string(),
        content,
        truncated,
    })
}

/// What a refused keyless extraction means for `web_fetch`:
///
/// * `Err`: report it. Only for a PDF, which the free tier won't extract and a
///   direct GET would return as binary.
/// * `Ok(Some(note))`: fall back, and tell the user why. A 402 means the free
///   allowance is spent or paused; the note is the server's own message.
/// * `Ok(None)`: fall back quietly (rate limits, outages, URLs the endpoint
///   won't take but a direct GET can still read).
fn extract_fallback(status: u16, body: &str) -> Result<Option<String>, String> {
    let parsed = serde_json::from_str::<Value>(body).ok();
    let field = |key: &str| {
        parsed
            .as_ref()
            .and_then(|v| v.get(key))
            .and_then(|v| v.as_str())
            .filter(|s| !s.is_empty())
    };
    match status {
        422 if field("error") == Some("pdf_not_supported") => Err(field("message")
            .unwrap_or("You.com can't extract PDFs without an API key.")
            .to_string()),
        402 => Ok(Some(clip_chars(
            field("message").unwrap_or("You.com's free extraction allowance is used up."),
            400,
        ))),
        _ => Ok(None),
    }
}

/// Lead a fallback page with why it isn't extracted, keeping the page within
/// the fetch bound.
fn prefix_extract_note(page: FetchedPage, note: &str) -> FetchedPage {
    let (content, truncated) = bound_text(&format!(
        "[You.com extraction unavailable: {note} Showing the page's raw HTML instead.]\n\n{}",
        page.content
    ));
    FetchedPage {
        content,
        truncated: page.truncated || truncated,
        ..page
    }
}

/// SearXNG backend (self-hosted, key-less). Queries a user-supplied instance's
/// JSON search API. SearXNG has no content-extraction endpoint, so `fetch` does
/// a plain HTTP GET of the URL and returns the bounded raw response body.
pub struct SearxngProvider {
    base_url: String,
    client: reqwest::Client,
}

impl SearxngProvider {
    pub fn new(endpoint: Option<String>) -> Result<Self, String> {
        let base = endpoint
            .map(|e| e.trim().trim_end_matches('/').to_string())
            .filter(|e| !e.is_empty())
            .ok_or("SearXNG requires an instance URL")?;
        if !(base.starts_with("http://") || base.starts_with("https://")) {
            return Err(format!(
                "SearXNG instance URL must be an http(s) URL, got: {base}"
            ));
        }
        Ok(Self {
            base_url: base,
            client: build_http_client("SearXNG")?,
        })
    }
}

#[async_trait]
impl SearchProvider for SearxngProvider {
    async fn search(&self, query: &str, count: u32) -> Result<Vec<SearchResult>, String> {
        let resp = self
            .client
            .get(format!("{}/search", self.base_url))
            .query(&[("q", query), ("format", "json")])
            .send()
            .await
            .map_err(|e| format!("SearXNG request failed: {e}"))?;
        let status = resp.status();
        let text = resp
            .text()
            .await
            .map_err(|e| format!("SearXNG: failed to read response body: {e}"))?;
        if !status.is_success() {
            return Err(format!(
                "SearXNG failed with HTTP {}: {}",
                status.as_u16(),
                text.chars().take(400).collect::<String>()
            ));
        }
        let parsed: Value = serde_json::from_str(&text).map_err(|e| {
            format!("SearXNG: invalid JSON response (is the JSON API enabled?): {e}")
        })?;
        Ok(normalize_searxng_search(&parsed, count))
    }

    async fn fetch(&self, url: &str) -> Result<FetchedPage, String> {
        fetch_url_direct(url, "SearXNG").await
    }
}

fn normalize_searxng_search(body: &Value, count: u32) -> Vec<SearchResult> {
    let Some(results) = body.get("results").and_then(|v| v.as_array()) else {
        return Vec::new();
    };
    results
        .iter()
        .take(count as usize)
        .map(|r| {
            let title = r.get("title").and_then(|v| v.as_str()).unwrap_or("");
            let url = r.get("url").and_then(|v| v.as_str()).unwrap_or("");
            let snippet = r
                .get("content")
                .and_then(|v| v.as_str())
                .map(|t| clip_chars(t, 500))
                .unwrap_or_default();
            let published_at = r
                .get("publishedDate")
                .and_then(|v| v.as_str())
                .filter(|s| !s.is_empty())
                .map(str::to_string);
            SearchResult {
                title: title.to_string(),
                url: url.to_string(),
                snippet,
                published_at,
            }
        })
        .collect()
}

/// Fetch a URL directly and return its bounded body, titled from its `<title>`.
/// Used by backends that have no content-extraction endpoint on the active
/// transport, so `web_fetch` still answers instead of erroring.
/// Redirect hops a direct fetch follows, each re-vetted.
const DIRECT_FETCH_MAX_REDIRECTS: usize = 5;

/// Whether `ip` is an address on the public internet. A direct fetch runs
/// from the user's machine on a URL the model chose, so loopback, private,
/// link-local (cloud metadata), CGNAT, multicast and unspecified targets are
/// refused: otherwise a prompt-injected page could make the agent read local
/// services and LAN hosts through an auto-allowed tool.
fn is_public_ip(ip: std::net::IpAddr) -> bool {
    use std::net::IpAddr;
    match ip {
        IpAddr::V4(v4) => {
            let o = v4.octets();
            !(v4.is_loopback()
                || v4.is_private()
                || v4.is_link_local()
                || v4.is_unspecified()
                || v4.is_broadcast()
                || v4.is_multicast()
                || v4.is_documentation()
                || o[0] == 0
                // 100.64.0.0/10, carrier-grade NAT.
                || (o[0] == 100 && (o[1] & 0xc0) == 64))
        }
        IpAddr::V6(v6) => {
            if let Some(v4) = v6.to_ipv4_mapped() {
                return is_public_ip(IpAddr::V4(v4));
            }
            let s = v6.segments();
            let embedded = |hi: u16, lo: u16| {
                std::net::Ipv4Addr::new((hi >> 8) as u8, hi as u8, (lo >> 8) as u8, lo as u8)
            };
            // NAT64 (64:ff9b::/96) and 6to4 (2002::/16) carry an IPv4 address
            // a translating network delivers to; judge them by it.
            if s[..6] == [0x64, 0xff9b, 0, 0, 0, 0] {
                return is_public_ip(IpAddr::V4(embedded(s[6], s[7])));
            }
            if s[0] == 0x2002 {
                return is_public_ip(IpAddr::V4(embedded(s[1], s[2])));
            }
            !(v6.is_loopback()
                || v6.is_unspecified()
                || v6.is_multicast()
                // fc00::/7 unique local, fe80::/10 link-local, fec0::/10
                // deprecated site-local.
                || (s[0] & 0xfe00) == 0xfc00
                || (s[0] & 0xffc0) == 0xfe80
                || (s[0] & 0xffc0) == 0xfec0)
        }
    }
}

/// Resolve `url`'s host and return the address to connect to, refusing any
/// target that is not public. Every resolved address must pass, so a name with
/// one public and one private record cannot be used to reach the private one.
async fn vet_direct_target(url: &reqwest::Url) -> Result<std::net::SocketAddr, String> {
    if !matches!(url.scheme(), "http" | "https") {
        return Err(format!("only http(s) URLs can be fetched, got: {url}"));
    }
    let port = url
        .port_or_known_default()
        .ok_or_else(|| format!("no port for {url}"))?;
    let host = url
        .host_str()
        .filter(|h| !h.is_empty())
        .ok_or_else(|| format!("no host in {url}"))?;
    // An IPv6 literal comes back bracketed; a literal is used as-is so it is
    // vetted without a lookup.
    let literal = host.trim_start_matches('[').trim_end_matches(']');
    let addrs: Vec<std::net::SocketAddr> = match literal.parse::<std::net::IpAddr>() {
        Ok(ip) => vec![(ip, port).into()],
        Err(_) => tokio::net::lookup_host((host, port))
            .await
            .map_err(|e| format!("could not resolve {host}: {e}"))?
            .collect(),
    };
    let first = *addrs
        .first()
        .ok_or_else(|| format!("no address for {url}"))?;
    if let Some(bad) = addrs.iter().find(|a| !is_public_ip(a.ip())) {
        return Err(format!(
            "refusing to fetch {url}: it resolves to a local or private address ({})",
            bad.ip()
        ));
    }
    Ok(first)
}

/// GET a URL straight from this machine. Each hop (the URL and every
/// redirect) is vetted by [`vet_direct_target`], and the connection is pinned
/// to the vetted address so a second DNS answer cannot swap in a private one.
///
/// Proxies from the environment are ignored on purpose: a proxy resolves the
/// name itself, so the pin would no longer decide what is fetched, and a name
/// that is only private inside the proxy's network would pass the vetting.
async fn fetch_url_direct(url: &str, provider: &str) -> Result<FetchedPage, String> {
    let mut current =
        reqwest::Url::parse(url).map_err(|e| format!("{provider} fetch: invalid URL {url}: {e}"))?;
    let mut hops = 0;
    let resp = loop {
        let addr = vet_direct_target(&current).await?;
        let mut builder = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(REQUEST_TIMEOUT_SECS))
            .redirect(reqwest::redirect::Policy::none())
            .no_proxy();
        if let Some(domain) = current.domain() {
            builder = builder.resolve(domain, addr);
        }
        let client = builder
            .build()
            .map_err(|e| format!("failed to build HTTP client for {provider}: {e}"))?;
        let resp = client
            .get(current.clone())
            .send()
            .await
            .map_err(|e| format!("{provider} fetch request failed: {e}"))?;
        if !resp.status().is_redirection() {
            break resp;
        }
        hops += 1;
        if hops > DIRECT_FETCH_MAX_REDIRECTS {
            return Err(format!("{provider} fetch: too many redirects for {url}"));
        }
        let location = resp
            .headers()
            .get(reqwest::header::LOCATION)
            .and_then(|v| v.to_str().ok())
            .ok_or_else(|| format!("{provider} fetch: redirect without a Location header"))?;
        current = current
            .join(location)
            .map_err(|e| format!("{provider} fetch: bad redirect target {location}: {e}"))?;
    };
    let status = resp.status();
    let body = resp
        .text()
        .await
        .map_err(|e| format!("{provider} fetch: failed to read response body: {e}"))?;
    if !status.is_success() {
        return Err(format!(
            "{provider} fetch failed with HTTP {}",
            status.as_u16()
        ));
    }
    let title = extract_html_title(&body).unwrap_or_default();
    let (content, truncated) = bound_text(&body);
    Ok(FetchedPage {
        url: url.to_string(),
        title,
        content,
        truncated,
    })
}

fn extract_html_title(html: &str) -> Option<String> {
    let lower = html.to_ascii_lowercase();
    let start = lower.find("<title")?;
    let open_end = lower[start..].find('>')? + start + 1;
    let close = lower[open_end..].find("</title>")? + open_end;
    let title = html[open_end..close].trim();
    if title.is_empty() {
        None
    } else {
        Some(title.to_string())
    }
}

fn clip_chars(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_string()
    } else {
        s.chars().take(max).collect()
    }
}

fn bound_text(s: &str) -> (String, bool) {
    if s.chars().count() <= FETCH_MAX_CHARS {
        (s.to_string(), false)
    } else {
        (s.chars().take(FETCH_MAX_CHARS).collect(), true)
    }
}

pub fn clamp_count(requested: Option<u64>) -> u32 {
    match requested {
        Some(0) | None => SEARCH_DEFAULT_COUNT,
        Some(n) => (n as u32).min(SEARCH_MAX_COUNT),
    }
}

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

    #[test]
    fn clamp_count_defaults_and_caps() {
        assert_eq!(clamp_count(None), SEARCH_DEFAULT_COUNT);
        assert_eq!(clamp_count(Some(0)), SEARCH_DEFAULT_COUNT);
        assert_eq!(clamp_count(Some(3)), 3);
        assert_eq!(clamp_count(Some(1000)), SEARCH_MAX_COUNT);
    }

    #[test]
    fn empty_key_selects_hosted() {
        let p = ExaProvider::new(None).unwrap();
        assert_eq!(p.mode, ExaMode::Hosted);
        let p = ExaProvider::new(Some("  ".into())).unwrap();
        assert_eq!(p.mode, ExaMode::Hosted);
        let p = ExaProvider::new(Some("YOUR_EXA_API_KEY_HERE".into())).unwrap();
        assert_eq!(p.mode, ExaMode::Hosted);
    }

    #[test]
    fn real_key_selects_rest() {
        let p = ExaProvider::new(Some("abc123".into())).unwrap();
        assert_eq!(p.mode, ExaMode::Rest("abc123".into()));
    }

    #[test]
    fn create_provider_defaults_to_exa() {
        assert!(create_provider(None, None, None).is_ok());
        assert!(create_provider(Some(""), None, None).is_ok());
        assert!(create_provider(Some("Exa"), None, None).is_ok());
    }

    #[test]
    fn public_ip_check_refuses_local_and_private_ranges() {
        for ip in [
            "127.0.0.1",
            "10.1.2.3",
            "172.16.0.1",
            "192.168.1.1",
            "169.254.169.254",
            "100.64.0.1",
            "0.0.0.0",
            "::1",
            "fd00::1",
            "fe80::1",
            "::ffff:127.0.0.1",
            "64:ff9b::7f00:1",
            "64:ff9b::a00:1",
            "2002:c0a8:101::1",
            "2002:7f00:1::1",
            "fec0::1",
        ] {
            assert!(!is_public_ip(ip.parse().unwrap()), "{ip} must be refused");
        }
        for ip in [
            "1.1.1.1",
            "93.184.216.34",
            "2606:4700:4700::1111",
            "64:ff9b::101:101",
            "2002:101:101::1",
        ] {
            assert!(is_public_ip(ip.parse().unwrap()), "{ip} must be allowed");
        }
    }

    /// Literal and named local targets are refused before any request is made.
    #[tokio::test]
    async fn direct_fetch_refuses_local_targets() {
        for url in [
            "http://127.0.0.1:9/",
            "http://localhost:9/",
            "http://[::1]:9/",
            "http://169.254.169.254/latest/meta-data/",
        ] {
            let err = fetch_url_direct(url, "SearXNG").await.unwrap_err();
            assert!(err.contains("local or private"), "{url}: {err}");
        }
    }

    #[test]
    fn web_search_config_debug_redacts_the_key() {
        let config = WebSearchConfig {
            provider: Some("tavily".into()),
            api_key: Some("tvly-secret".into()),
            endpoint: None,
        };
        let printed = format!("{config:?}");
        assert!(!printed.contains("tvly-secret"), "{printed}");
        assert!(printed.contains("redacted"), "{printed}");
    }

    #[test]
    fn every_listed_provider_is_constructible() {
        for meta in PROVIDERS {
            let key = (!meta.keyless).then(|| "k".to_string());
            let endpoint = meta.requires_endpoint.then(|| "https://x.example".to_string());
            assert!(
                create_provider(Some(meta.id), key, endpoint).is_ok(),
                "{} must be accepted by create_provider",
                meta.id
            );
        }
        assert_eq!(provider_meta(None).map(|m| m.id), Some("exa"));
        assert_eq!(provider_meta(Some("Tavily")).map(|m| m.id), Some("tavily"));
        assert!(provider_meta(Some("brave")).is_none());
    }

    #[test]
    fn create_provider_rejects_unknown() {
        match create_provider(Some("brave"), None, None) {
            Ok(_) => panic!("expected unknown provider to error"),
            Err(e) => assert!(e.contains("brave")),
        }
    }

    #[test]
    fn create_provider_tavily_requires_key() {
        match create_provider(Some("tavily"), None, None) {
            Ok(_) => panic!("expected Tavily to require a key"),
            Err(e) => assert!(e.contains("Tavily")),
        }
        assert!(create_provider(Some("tavily"), Some("tvly-abc".into()), None).is_ok());
    }

    #[test]
    fn create_provider_searxng_requires_valid_url() {
        match create_provider(Some("searxng"), None, None) {
            Ok(_) => panic!("expected SearXNG to require an instance URL"),
            Err(e) => assert!(e.contains("SearXNG")),
        }
        match create_provider(Some("searxng"), None, Some("example.com".into())) {
            Ok(_) => panic!("expected SearXNG to reject a scheme-less URL"),
            Err(e) => assert!(e.contains("http")),
        }
        assert!(
            create_provider(Some("searxng"), None, Some("https://searx.example/".into())).is_ok()
        );
    }

    #[test]
    fn normalize_searxng_search_maps_and_caps() {
        let body = json!({
            "results": [
                { "title": "One", "url": "https://a.example", "content": "first", "publishedDate": "2024-01-01" },
                { "title": "Two", "url": "https://b.example", "content": "second" },
                { "title": "Three", "url": "https://c.example", "content": "third" }
            ]
        });
        let results = normalize_searxng_search(&body, 2);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].url, "https://a.example");
        assert_eq!(results[0].published_at.as_deref(), Some("2024-01-01"));
        assert!(results[1].published_at.is_none());
    }

    #[test]
    fn extract_html_title_reads_title_tag() {
        assert_eq!(
            extract_html_title("<html><head><TITLE>Hello</TITLE></head>").as_deref(),
            Some("Hello")
        );
        assert!(extract_html_title("<html>no title</html>").is_none());
    }

    #[test]
    fn normalize_tavily_search_maps_contract() {
        let body = json!({
            "results": [
                {
                    "title": "Example",
                    "url": "https://example.com",
                    "content": "A short excerpt.",
                    "published_date": "2024-05-01"
                },
                { "title": "No date", "url": "https://example.org", "content": "Body." }
            ]
        });
        let results = normalize_tavily_search(&body);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].url, "https://example.com");
        assert_eq!(results[0].snippet, "A short excerpt.");
        assert_eq!(results[0].published_at.as_deref(), Some("2024-05-01"));
        assert!(results[1].published_at.is_none());
    }

    #[test]
    fn normalize_tavily_search_empty_is_empty() {
        assert!(normalize_tavily_search(&json!({})).is_empty());
        assert!(normalize_tavily_search(&json!({"results": []})).is_empty());
    }

    #[test]
    fn normalize_tavily_extract_reads_raw_content() {
        let body = json!({
            "results": [
                { "url": "https://example.com", "raw_content": "hello world" }
            ]
        });
        let page = normalize_tavily_extract(&body, "https://example.com").unwrap();
        assert!(page.title.is_empty());
        assert_eq!(page.content, "hello world");
        assert!(!page.truncated);
    }

    #[test]
    fn normalize_tavily_extract_no_results_errors() {
        assert!(normalize_tavily_extract(&json!({"results": []}), "u").is_err());
    }

    #[test]
    fn parse_hosted_result_text_reads_sse_frame() {
        let sse = "event: message\ndata: {\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"hello\"}]}}\n\n";
        assert_eq!(parse_hosted_result_text(sse, "Exa").unwrap(), "hello");
    }

    #[test]
    fn parse_hosted_result_text_reads_raw_json() {
        let raw = "{\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"hi\"}]}}";
        assert_eq!(parse_hosted_result_text(raw, "Exa").unwrap(), "hi");
    }

    #[test]
    fn parse_hosted_result_text_surfaces_errors() {
        let err = "{\"error\":{\"code\":-32000,\"message\":\"boom\"}}";
        assert!(parse_hosted_result_text(err, "Exa").is_err());
        let tool_err = "{\"result\":{\"isError\":true,\"content\":[{\"type\":\"text\",\"text\":\"bad\"}]}}";
        assert!(parse_hosted_result_text(tool_err, "Exa")
            .unwrap_err()
            .contains("bad"));
    }

    #[test]
    fn parse_hosted_result_text_surfaces_rate_limit_as_error() {
        // Verbatim shape of a throttled `mcp.exa.ai/mcp` reply: the call
        // "succeeds" (no `isError`, HTTP 200) and the refusal is prose in the
        // text block, so only `_meta` distinguishes it from a real answer.
        let sse = format!(
            "event: message\ndata: {}\n\n",
            json!({
                "jsonrpc": "2.0",
                "id": 1,
                "result": {
                    "_meta": { "ai.exa/rateLimited": true },
                    "content": [{
                        "type": "text",
                        "text": "You've hit Exa's free MCP rate limit. To continue using without limits, create your own Exa API key."
                    }]
                }
            })
        );
        let err = parse_hosted_result_text(&sse, "Exa")
            .expect_err("a throttled reply must not read as a successful search");
        assert!(err.contains("rate limit"), "unexpected message: {err}");
    }

    #[test]
    fn parse_hosted_result_text_ignores_unrelated_meta() {
        let raw = json!({
            "result": {
                "_meta": { "ai.exa/cached": true },
                "content": [{ "type": "text", "text": "hello" }]
            }
        })
        .to_string();
        assert_eq!(parse_hosted_result_text(&raw, "Exa").unwrap(), "hello");
    }

    #[test]
    fn parse_hosted_result_text_rate_limit_flag_must_be_true() {
        let raw = json!({
            "result": {
                "_meta": { "ai.exa/rateLimited": false },
                "content": [{ "type": "text", "text": "hello" }]
            }
        })
        .to_string();
        assert_eq!(parse_hosted_result_text(&raw, "Exa").unwrap(), "hello");
    }

    #[test]
    fn parse_hosted_search_text_maps_contract() {
        let text = "Title: Paris | Britannica\nURL: https://www.britannica.com/place/Paris\nPublished: 1998-07-20T00:00:00.000Z\nAuthor: N/A\nHighlights:\nParis is the capital of France.\n...\nSecond highlight.\n---\nTitle: Paris\nURL: https://en.wikipedia.org/wiki/Paris\nPublished: N/A\nAuthor: N/A\nHighlights:\nParis is the capital and largest city of France.";
        let results = parse_hosted_search_text(text);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].title, "Paris | Britannica");
        assert_eq!(results[0].url, "https://www.britannica.com/place/Paris");
        assert_eq!(
            results[0].published_at.as_deref(),
            Some("1998-07-20T00:00:00.000Z")
        );
        assert!(results[0].snippet.contains("capital of France"));
        assert!(!results[0].snippet.contains("..."));
        assert!(results[1].published_at.is_none());
        assert_eq!(results[1].url, "https://en.wikipedia.org/wiki/Paris");
    }

    #[test]
    fn parse_hosted_search_text_empty_is_empty() {
        assert!(parse_hosted_search_text("").is_empty());
        assert!(parse_hosted_search_text("   \n  ").is_empty());
    }

    #[test]
    fn parse_hosted_fetch_text_extracts_title_url_body() {
        let text = "# Paris\nURL: https://en.wikipedia.org/wiki/Paris\n\nParis is the capital and largest city of France.";
        let page = parse_hosted_fetch_text(text, "https://en.wikipedia.org/wiki/Paris");
        assert_eq!(page.title, "Paris");
        assert_eq!(page.url, "https://en.wikipedia.org/wiki/Paris");
        assert!(page.content.starts_with("Paris is the capital"));
        assert!(!page.truncated);
    }

    #[test]
    fn parse_hosted_fetch_text_truncates_large_body() {
        let big = "a".repeat(FETCH_MAX_CHARS + 100);
        let text = format!("# T\nURL: https://x\n\n{big}");
        let page = parse_hosted_fetch_text(&text, "https://x");
        assert!(page.truncated);
        assert_eq!(page.content.chars().count(), FETCH_MAX_CHARS);
    }

    #[test]
    fn normalize_exa_rest_search_maps_contract() {
        let body = json!({
            "results": [
                {
                    "title": "Example",
                    "url": "https://example.com",
                    "highlights": ["Short result excerpt"],
                    "publishedDate": "2024-01-02T00:00:00.000Z"
                },
                {
                    "title": "No highlight",
                    "url": "https://example.org",
                    "text": "Body text fallback used as snippet."
                }
            ]
        });
        let results = normalize_exa_rest_search(&body);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].snippet, "Short result excerpt");
        assert_eq!(
            results[0].published_at.as_deref(),
            Some("2024-01-02T00:00:00.000Z")
        );
        assert!(results[1].snippet.starts_with("Body text fallback"));
        assert!(results[1].published_at.is_none());
    }

    #[test]
    fn normalize_exa_rest_search_empty_is_empty() {
        assert!(normalize_exa_rest_search(&json!({})).is_empty());
        assert!(normalize_exa_rest_search(&json!({"results": []})).is_empty());
    }

    #[test]
    fn normalize_exa_rest_fetch_bounds_and_titles() {
        let body = json!({
            "results": [ { "url": "https://example.com", "title": "T", "text": "hello world" } ]
        });
        let page = normalize_exa_rest_fetch(&body, "https://example.com").unwrap();
        assert_eq!(page.title, "T");
        assert_eq!(page.content, "hello world");
        assert!(!page.truncated);
    }

    #[test]
    fn normalize_exa_rest_fetch_no_results_errors() {
        assert!(normalize_exa_rest_fetch(&json!({"results": []}), "u").is_err());
    }

    #[test]
    fn youcom_attribution_follows_the_client_info_grammar() {
        // `<source>; client=<name>/<version>; ua=<runtime>/<version>`
        let segments: Vec<&str> = YOU_COM_CLIENT_INFO.split("; ").collect();
        assert_eq!(segments.len(), 3, "unexpected segment count");
        assert_eq!(segments[0], "plugin");
        let client = segments[1]
            .strip_prefix("client=")
            .expect("second segment names the client");
        let (name, version) = client.split_once('/').expect("client carries a version");
        assert_eq!(name, "jan-websearch");
        assert!(!version.is_empty(), "client version must not be empty");
        assert!(segments[2].starts_with("ua="));
        // Values are interpolated verbatim; a stray delimiter would corrupt
        // the segment split on the receiving side.
        assert!(segments.iter().all(|s| !s.contains(';')));
        assert!(YOU_COM_USER_AGENT.starts_with("jan-websearch/"));
        assert!(YOU_COM_USER_AGENT.contains("github.com/janhq/jan"));
    }

    #[test]
    fn youcom_requests_send_a_single_content_type() {
        // Regression test: reqwest's `.json()` sets `Content-Type` and
        // `.header()` appends, so an extra explicit header produced a
        // duplicated `content-type: application/json` — which api.you.com
        // rejects with HTTP 415 ("Content-Type must be application/json").
        let body = json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/call" });
        let req = with_youcom_headers(
            YouComProvider::new(None)
                .unwrap()
                .client
                .post(YOU_COM_HOSTED_URL)
                .header("accept", "application/json, text/event-stream")
                .json(&body),
        )
        .build()
        .unwrap();
        assert_eq!(req.headers().get_all("content-type").iter().count(), 1);
        assert_eq!(
            req.headers().get("content-type").unwrap(),
            "application/json"
        );
        assert!(req.headers().contains_key("user-agent"));
        assert!(req.headers().contains_key("x-client-info"));
    }

    #[test]
    fn youcom_key_selects_transport() {
        assert_eq!(YouComProvider::new(None).unwrap().mode, YouComMode::Hosted);
        assert_eq!(
            YouComProvider::new(Some("   ".into())).unwrap().mode,
            YouComMode::Hosted
        );
        assert_eq!(
            YouComProvider::new(Some("ydc-key".into())).unwrap().mode,
            YouComMode::Rest("ydc-key".into())
        );
    }

    #[test]
    fn create_provider_youcom_works_with_and_without_key() {
        assert!(create_provider(Some("you"), None, None).is_ok());
        assert!(create_provider(Some("you"), Some("ydc-key".into()), None).is_ok());
    }

    #[test]
    fn parse_hosted_result_text_youcom_skips_notification_frames() {
        // The hosted endpoint emits a progress notification ahead of the
        // answer, so the result is not the first `data:` frame.
        let payload = json!({
            "results": {
                "web": [
                    { "url": "https://example.com", "title": "T", "description": "D" }
                ]
            }
        });
        let sse = format!(
            "event: message\ndata: {}\n\nevent: message\ndata: {}\n\n",
            json!({
                "jsonrpc": "2.0",
                "method": "notifications/message",
                "params": { "level": "info", "data": "searching" }
            }),
            json!({
                "jsonrpc": "2.0",
                "id": 1,
                "result": { "content": [{ "type": "text", "text": payload.to_string() }] }
            })
        );
        let text = parse_hosted_result_text(&sse, "You.com").expect("result frame must be found");
        // The hosted payload is the same shape the REST endpoint returns, so
        // the REST normalizer reads it unchanged.
        let parsed: Value = serde_json::from_str(&text).expect("tool payload is JSON");
        let results = normalize_youcom_search(&parsed);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].url, "https://example.com");
        assert_eq!(results[0].snippet, "D");
    }

    #[test]
    fn parse_hosted_result_text_youcom_reads_bare_json_body() {
        let raw = json!({"result": {"content": [{"type": "text", "text": "hi"}]}}).to_string();
        assert_eq!(parse_hosted_result_text(&raw, "You.com").unwrap(), "hi");
    }

    #[test]
    fn parse_hosted_result_text_youcom_surfaces_errors() {
        let err = json!({"error": {"code": -32000, "message": "boom"}}).to_string();
        assert!(parse_hosted_result_text(&err, "You.com")
            .unwrap_err()
            .contains("boom"));

        let tool_err = json!({
            "result": {
                "isError": true,
                "content": [{"type": "text", "text": "quota exceeded"}]
            }
        })
        .to_string();
        assert!(parse_hosted_result_text(&tool_err, "You.com")
            .unwrap_err()
            .contains("quota exceeded"));

        // A stream that never carries an answer must error rather than
        // silently returning the notification.
        let only_notification = format!(
            "event: message\ndata: {}\n\n",
            json!({"jsonrpc": "2.0", "method": "notifications/message", "params": {}})
        );
        assert!(parse_hosted_result_text(&only_notification, "You.com").is_err());
    }

    #[test]
    fn normalize_youcom_search_maps_contract() {
        // Field set mirrors a live `ydc-index.io/v1/search` web result,
        // including the thumbnail and favicon keys the normalizer ignores.
        let body = json!({
            "results": {
                "web": [
                    {
                        "url": "https://example.com",
                        "title": "Example",
                        "description": "A short summary.",
                        "snippets": ["first excerpt", "second excerpt"],
                        "thumbnail_url": "https://cdn.example.com/img.jpg",
                        "original_thumbnail_url": "https://cdn.example.com/img.jpg",
                        "favicon_url": "https://example.com/favicon.ico",
                        "page_age": "2024-05-01T00:00:00.000Z"
                    },
                    { "url": "https://example.org", "title": "No date", "description": "Body." }
                ]
            }
        });
        let results = normalize_youcom_search(&body);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].url, "https://example.com");
        // Description wins over snippets when both are present.
        assert_eq!(results[0].snippet, "A short summary.");
        assert_eq!(
            results[0].published_at.as_deref(),
            Some("2024-05-01T00:00:00.000Z")
        );
        assert!(results[1].published_at.is_none());
    }

    #[test]
    fn normalize_youcom_search_falls_back_to_snippet_when_no_description() {
        let body = json!({
            "results": {
                "web": [
                    {
                        "url": "https://example.com",
                        "title": "Missing",
                        "snippets": ["passage one", "passage two"]
                    },
                    {
                        "url": "https://example.org",
                        "title": "Empty",
                        "description": "",
                        "snippets": ["passage three"]
                    }
                ]
            }
        });
        let results = normalize_youcom_search(&body);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].snippet, "passage one");
        // An empty description falls through to snippets rather than
        // yielding an empty snippet.
        assert_eq!(results[1].snippet, "passage three");
    }

    #[test]
    fn normalize_youcom_search_clips_long_snippet() {
        let body = json!({
            "results": {
                "web": [
                    { "url": "https://example.com", "description": "x".repeat(900) }
                ]
            }
        });
        let results = normalize_youcom_search(&body);
        assert_eq!(results[0].snippet.chars().count(), 500);
    }

    #[test]
    fn normalize_youcom_search_empty_or_news_only_is_empty() {
        // A news-only response must not be reported as web results.
        let news_only = json!({
            "results": { "news": [{ "url": "https://x", "title": "X" }] }
        });
        assert!(normalize_youcom_search(&news_only).is_empty());
        assert!(normalize_youcom_search(&json!({})).is_empty());
        assert!(normalize_youcom_search(&json!({"results": {}})).is_empty());
        assert!(normalize_youcom_search(&json!({"results": {"web": []}})).is_empty());
    }

    #[test]
    fn normalize_youcom_contents_reads_markdown() {
        let body = json!([
            { "url": "https://example.com", "title": "T", "markdown": "hello world" }
        ]);
        let page = normalize_youcom_contents(&body, "https://example.com").unwrap();
        assert_eq!(page.title, "T");
        assert_eq!(page.url, "https://example.com");
        assert_eq!(page.content, "hello world");
        assert!(!page.truncated);
    }

    #[test]
    fn normalize_youcom_contents_truncates_and_marks_truncated() {
        let big = "a".repeat(FETCH_MAX_CHARS + 100);
        let body = json!([
            { "url": "https://example.com", "title": "T", "markdown": big }
        ]);
        let page = normalize_youcom_contents(&body, "https://example.com").unwrap();
        assert!(page.truncated);
        assert_eq!(page.content.chars().count(), FETCH_MAX_CHARS);
    }

    #[test]
    fn normalize_youcom_contents_no_results_errors() {
        assert!(normalize_youcom_contents(&json!([]), "u").is_err());
        // A non-array body must error rather than panic.
        assert!(normalize_youcom_contents(&json!({"results": []}), "u").is_err());
        // The spec types `markdown` as nullable for failed extractions; a null,
        // missing, or empty `markdown` must error rather than return an empty page.
        let null_markdown = json!([{ "url": "https://example.com", "markdown": null }]);
        assert!(normalize_youcom_contents(&null_markdown, "u").is_err());
        let no_markdown = json!([{ "url": "https://example.com", "title": "T" }]);
        assert!(normalize_youcom_contents(&no_markdown, "u").is_err());
        let empty_markdown = json!([{ "url": "https://example.com", "markdown": "" }]);
        assert!(normalize_youcom_contents(&empty_markdown, "u").is_err());
    }

    #[test]
    fn youcom_extract_request_shape() {
        // Built the way `send_raw` sends it.
        let req = with_youcom_headers(
            YouComProvider::new(None)
                .unwrap()
                .extract_request("https://example.com"),
        )
        .build()
        .unwrap();
        assert_eq!(req.method(), reqwest::Method::POST);
        assert_eq!(req.url().as_str(), YOU_COM_EXTRACT_URL);
        let body: Value =
            serde_json::from_slice(req.body().and_then(|b| b.as_bytes()).unwrap()).unwrap();
        assert_eq!(body, json!({ "url": "https://example.com" }));
        // A duplicated Content-Type gets a 422 from /v1/extract, which the
        // fallback would silently hide behind a direct GET.
        for header in ["content-type", "user-agent", "x-client-info"] {
            assert_eq!(req.headers().get_all(header).iter().count(), 1, "{header}");
        }
        assert!(!req.headers().contains_key("x-api-key"));
    }

    #[test]
    fn normalize_youcom_extract_reads_markdown() {
        // Verbatim keyless `/v1/extract` response for https://example.com.
        let body = json!({
            "url": "https://example.com",
            "extraction_source": "blend",
            "contents": {
                "full_page": {
                    "markdown": "This domain is for use in documentation examples without needing permission. Avoid use in operations.\n\nLearn more"
                }
            },
            "title": "Example Domain",
            "metadata": {
                "site_name": "",
                "favicon_url": "https://api.ydc-index.io/favicon?domain=example.com&size=128"
            }
        });
        let page = normalize_youcom_extract(&body, "https://example.com").unwrap();
        assert_eq!(page.url, "https://example.com");
        assert_eq!(page.title, "Example Domain");
        assert!(page.content.starts_with("This domain is for use"));
        assert!(!page.truncated);
    }

    #[test]
    fn normalize_youcom_extract_bounds_to_fetch_max() {
        let body = json!({
            "contents": { "full_page": { "markdown": "a".repeat(FETCH_MAX_CHARS + 100) } }
        });
        let page = normalize_youcom_extract(&body, "u").unwrap();
        assert!(page.truncated);
        assert_eq!(page.content.chars().count(), FETCH_MAX_CHARS);
    }

    #[test]
    fn normalize_youcom_extract_missing_content_errors() {
        // `{}` is what the proxy returns for a PDF it skipped parsing.
        for body in [
            json!({}),
            json!({ "url": "https://example.com", "title": "T" }),
            json!({ "contents": { "full_page": { "markdown": null } } }),
            json!({ "contents": { "full_page": {} } }),
            json!({ "contents": { "full_page": { "markdown": "" } } }),
            json!([]),
        ] {
            assert!(normalize_youcom_extract(&body, "u").is_err(), "{body}");
        }
    }

    #[test]
    fn youcom_extract_fallback_rules() {
        // Bodies are the keyless proxy's own (search_api_proxy extract.py,
        // free_tier.py, and the governance rate limiter).
        let spent = json!({
            "error": "free_tier_limit_exceeded",
            "message": "You've used all your free requests. Sign up for free at https://you.com/platform to continue.",
            "upgrade_url": "https://you.com/platform",
            "limit": 100,
            "used": 100,
            "reset_at": "2026-10-30T00:00:00+00:00"
        });
        let paused = json!({
            "error": "free_tier_unavailable",
            "message": "Free extraction is currently unavailable. Sign up for free at https://you.com/platform to continue.",
            "upgrade_url": "https://you.com/platform"
        });
        for body in [&spent, &paused] {
            let note = extract_fallback(402, &body.to_string()).unwrap().unwrap();
            assert_eq!(note, body["message"].as_str().unwrap());
        }
        // A 402 with no usable message still tells the user.
        for body in ["not json", "{}", r#"{"message": 7}"#, r#"{"message": ""}"#] {
            assert!(extract_fallback(402, body).unwrap().is_some(), "{body}");
        }
        let long = json!({ "message": "x".repeat(1000) }).to_string();
        let note = extract_fallback(402, &long).unwrap().unwrap();
        assert_eq!(note.chars().count(), 400);

        let unavailable = json!({
            "error": "temporarily_unavailable",
            "message": "Extraction is temporarily unavailable. Please retry shortly."
        });
        let bad_url = json!({
            "error": "invalid_request",
            "message": "url host must be a public domain name, on the default port, with no credentials."
        });
        let blocked = json!({
            "error": "invalid_request",
            "message": "url must point to a public, resolvable host."
        });
        let rate_limited = json!({ "errors": [{ "title": "Rate limit exceeded" }] });
        for (status, body) in [
            (429, rate_limited.to_string()),
            (500, String::new()),
            (503, unavailable.to_string()),
            (504, unavailable.to_string()),
            (422, bad_url.to_string()),
            (422, blocked.to_string()),
            (422, "not json".to_string()),
        ] {
            assert_eq!(extract_fallback(status, &body), Ok(None), "{status} {body}");
        }

        let pdf = json!({
            "error": "pdf_not_supported",
            "message": "PDF extraction is not available on the free tier. Sign up for free at https://you.com/platform to use it.",
            "upgrade_url": "https://you.com/platform"
        });
        assert_eq!(
            extract_fallback(422, &pdf.to_string()),
            Err(pdf["message"].as_str().unwrap().to_string())
        );
    }

    #[test]
    fn youcom_extract_note_prefixes_and_stays_bounded() {
        let page = FetchedPage {
            url: "https://example.com".into(),
            title: "T".into(),
            content: "<html>body</html>".into(),
            truncated: false,
        };
        let noted = prefix_extract_note(page, "Out of free requests.");
        assert!(noted
            .content
            .starts_with("[You.com extraction unavailable: Out of free requests."));
        assert!(noted.content.ends_with("<html>body</html>"));
        assert_eq!(noted.url, "https://example.com");
        assert_eq!(noted.title, "T");
        assert!(!noted.truncated);

        // A page already at the cap stays within it once the note is added.
        let full = FetchedPage {
            url: "u".into(),
            title: String::new(),
            content: "a".repeat(FETCH_MAX_CHARS),
            truncated: false,
        };
        let noted = prefix_extract_note(full, "Out of free requests.");
        assert_eq!(noted.content.chars().count(), FETCH_MAX_CHARS);
        assert!(noted.truncated);
    }

    /// Live smoke test against keyless `/v1/extract`. Ignored by default (needs
    /// network, spends one free request); run with
    /// `cargo test -- --ignored youcom_fetch_live`.
    #[tokio::test]
    #[ignore]
    async fn youcom_fetch_live_keyless() {
        let page = YouComProvider::new(None)
            .unwrap()
            .fetch("https://example.com")
            .await
            .unwrap();
        // The direct-GET fallback would return the page's raw HTML.
        assert!(
            !page.content.to_lowercase().contains("<html"),
            "fell back: {}",
            page.content
        );
        assert!(page.content.contains("documentation examples"));
    }
}