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.

8.2 KB
Raw Blame
//! Native, provider-neutral web tools compiled into the agent core.
//!
//! This module implements `web_search` and `web_fetch` as first-class built-in
//! tools - NOT as an MCP server registered with the agent. The agent calls the
//! stable, provider-neutral tool names (`web_search` / `web_fetch`) and never a
//! provider-branded name such as `exa_search`.
//!
//! The search backend, credential handling, and response normalization live in
//! `tauri_plugin_websearch::provider` and are shared with the desktop
//! `web_search`/`web_fetch` Tauri commands so there is a single implementation.
//! This module owns only the agent tool contract: argument parsing/validation,
//! picking the backend (the host's [`WebSearchConfig`] when it supplies one,
//! else Exa with an optional key from the process environment), and rendering
//! normalized results as compact, model-friendly text.

use serde_json::Value;
use tauri_plugin_websearch::provider::{
    clamp_count, provider_meta, FetchedPage, SearchProvider, SearchResult, WebSearchConfig,
};

/// Upper bound on the readable text `web_fetch` returns. Mirrors the plugin's
/// internal bound; used here only to phrase the truncation notice.
const FETCH_MAX_CHARS: usize = 40_000;

/// Environment variables consulted for an optional Exa credential, in priority
/// order. `JAN_EXA_API_KEY` is Jan's namespaced variable; `EXA_API_KEY` matches
/// Exa's own convention and the key already used by the legacy Exa MCP
/// registration, so an existing configuration keeps working. When neither is
/// set, the keyless hosted path is used automatically.
const EXA_API_KEY_ENVS: &[&str] = &["JAN_EXA_API_KEY", "EXA_API_KEY"];

fn exa_api_key() -> Option<String> {
    for var in EXA_API_KEY_ENVS {
        if let Ok(v) = std::env::var(var) {
            let v = v.trim();
            if !v.is_empty() && v != "YOUR_EXA_API_KEY_HERE" {
                return Some(v.to_string());
            }
        }
    }
    None
}

/// The backend for one call. The host's selection wins; an Exa selection with
/// no stored key (and no selection at all) still honours the Exa env vars, so
/// an existing `EXA_API_KEY` setup keeps working after a provider is chosen.
fn backend(config: Option<&WebSearchConfig>) -> Result<Box<dyn SearchProvider>, String> {
    let mut config = config.cloned().unwrap_or_default();
    let is_exa = provider_meta(config.provider.as_deref()).is_some_and(|m| m.id == "exa");
    if is_exa && !config.api_key.as_deref().is_some_and(|k| !k.trim().is_empty()) {
        config.api_key = exa_api_key();
    }
    config.create().map_err(|e| format!("ERROR: {e}"))
}

/// Execute the `web_search` built-in. Returns the tool-result text; errors are
/// returned as a `String` starting with "ERROR" to match the other handlers.
pub async fn web_search(args: &Value, config: Option<&WebSearchConfig>) -> String {
    let Some(query) = args.get("query").and_then(|v| v.as_str()).map(str::trim) else {
        return "ERROR: web_search requires a 'query' string argument.".to_string();
    };
    if query.is_empty() {
        return "ERROR: web_search 'query' must not be empty.".to_string();
    }
    let count = clamp_count(args.get("count").and_then(|v| v.as_u64()));
    let provider = match backend(config) {
        Ok(p) => p,
        Err(e) => return e,
    };
    match provider.search(query, count).await {
        Ok(results) if results.is_empty() => {
            format!("No web search results found for query: {query}")
        }
        Ok(results) => render_search_results(&results),
        Err(e) => format!("ERROR: {e}"),
    }
}

/// Execute the `web_fetch` built-in. Returns bounded readable content with its
/// source URL/title; errors start with "ERROR".
pub async fn web_fetch(args: &Value, config: Option<&WebSearchConfig>) -> String {
    let Some(url) = args.get("url").and_then(|v| v.as_str()).map(str::trim) else {
        return "ERROR: web_fetch requires a 'url' string argument.".to_string();
    };
    if url.is_empty() {
        return "ERROR: web_fetch 'url' must not be empty.".to_string();
    }
    if !(url.starts_with("http://") || url.starts_with("https://")) {
        return format!("ERROR: web_fetch 'url' must be an http(s) URL, got: {url}");
    }
    let provider = match backend(config) {
        Ok(p) => p,
        Err(e) => return e,
    };
    match provider.fetch(url).await {
        Ok(page) => render_fetched_page(&page),
        Err(e) => format!("ERROR: {e}"),
    }
}

/// Render normalized results as compact, model-friendly text with citations.
fn render_search_results(results: &[SearchResult]) -> String {
    let mut out = String::new();
    for (i, r) in results.iter().enumerate() {
        out.push_str(&format!("{}. {}\n", i + 1, r.title));
        out.push_str(&format!("   URL: {}\n", r.url));
        if let Some(date) = &r.published_at {
            out.push_str(&format!("   Published: {date}\n"));
        }
        if !r.snippet.is_empty() {
            out.push_str(&format!("   {}\n", r.snippet.replace('\n', " ")));
        }
        out.push('\n');
    }
    out.trim_end().to_string()
}

/// Render a fetched page as text headed by its source URL/title.
fn render_fetched_page(page: &FetchedPage) -> String {
    let mut out = String::new();
    if !page.title.is_empty() {
        out.push_str(&format!("Title: {}\n", page.title));
    }
    out.push_str(&format!("URL: {}\n\n", page.url));
    out.push_str(&page.content);
    if page.truncated {
        out.push_str(&format!(
            "\n\n[content truncated to {FETCH_MAX_CHARS} characters]"
        ));
    }
    out
}

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

    #[tokio::test]
    async fn web_search_requires_query() {
        assert!(web_search(&json!({}), None).await.starts_with("ERROR"));
        assert!(web_search(&json!({"query": "   "}), None).await.starts_with("ERROR"));
    }

    #[tokio::test]
    async fn web_fetch_validates_url() {
        assert!(web_fetch(&json!({}), None).await.starts_with("ERROR"));
        assert!(web_fetch(&json!({"url": "ftp://x"}), None).await.starts_with("ERROR"));
    }

    /// A configured backend that cannot be built (a keyed provider with no
    /// key) surfaces as a tool ERROR before any request is made, instead of
    /// silently falling back to Exa.
    #[tokio::test]
    async fn configured_provider_is_used_and_validated() {
        let tavily = WebSearchConfig {
            provider: Some("tavily".into()),
            ..Default::default()
        };
        let out = web_search(&json!({"query": "q"}), Some(&tavily)).await;
        assert!(out.starts_with("ERROR") && out.contains("Tavily"), "got: {out}");
        let searx = WebSearchConfig {
            provider: Some("searxng".into()),
            ..Default::default()
        };
        let out = web_fetch(&json!({"url": "https://x.example"}), Some(&searx)).await;
        assert!(out.starts_with("ERROR") && out.contains("SearXNG"), "got: {out}");
    }

    #[test]
    fn render_search_results_cites_urls() {
        let results = vec![SearchResult {
            title: "T".into(),
            url: "https://example.com".into(),
            snippet: "snip".into(),
            published_at: None,
        }];
        let rendered = render_search_results(&results);
        assert!(rendered.contains("https://example.com"));
        assert!(rendered.contains("snip"));
    }

    #[test]
    fn render_fetched_page_includes_source() {
        let page = FetchedPage {
            url: "https://example.com".into(),
            title: "Title".into(),
            content: "body".into(),
            truncated: true,
        };
        let rendered = render_fetched_page(&page);
        assert!(rendered.contains("URL: https://example.com"));
        assert!(rendered.contains("Title: Title"));
        assert!(rendered.contains("truncated"));
    }

    /// Live smoke test against the keyless hosted endpoint. Ignored by default
    /// (needs network); run with `cargo test -- --ignored web_search_live`.
    #[tokio::test]
    #[ignore]
    async fn web_search_live_keyless() {
        let out = web_search(&json!({ "query": "capital of France", "count": 2 }), None).await;
        assert!(!out.starts_with("ERROR"), "unexpected error: {out}");
        assert!(out.to_lowercase().contains("paris"), "got: {out}");
    }
}