Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(agent): keyless web search by default + teach the model how to call the tools
tikikuna46df33f09fd
diff --git a/src-tauri/src/core/agent/context.rs b/src-tauri/src/core/agent/context.rsindex 864fabacb..26e8e7d59 100644--- a/src-tauri/src/core/agent/context.rs+++ b/src-tauri/src/core/agent/context.rs@@ -102,10 +102,26 @@ pub(crate) fn load_skills(project_root: &Path) -> Option<String> {/// capability. Per jan-internal#196 the tools are provider-neutral: the model/// must call `web_search`/`web_fetch`, never a provider-branded name like/// `exa_search`, and should cite the URLs it relies on.-const WEB_TOOLS_GUIDE: &str = "# Web Access\n\nYou have native `web_search` and `web_fetch` tools built into the agent. \-Use `web_search` to find current information and sources, then `web_fetch` to read a specific result's page. \-These are provider-neutral built-in tools (the search backend is configured by Jan); do not look for or call a \-provider-branded tool such as `exa_search`. Cite the source URLs you rely on.";+const WEB_TOOLS_GUIDE: &str = "# Web Access\n\nYou have two native, built-in tools for the live web. They are provider-neutral \+(the search backend is configured by Jan) and work out of the box — do NOT look for, ask for, or call a \+provider-branded tool such as `exa_search`, and do not say you lack internet access.\n\n\+## When to use them\n\n\+Reach for the web whenever the answer depends on current, external, or fast-changing information: recent events, \+library/API versions and docs, error messages, prices, people, or anything you are unsure about or that is outside \+your training data. Prefer verifying over guessing.\n\n\+## How to call them\n\n\+- `web_search` — find sources. Arguments: `query` (required string; write a specific, natural-language description \+of the ideal page, not just keywords) and optional `count` (integer, default 5, max 20). Returns a numbered list of \+results with title, URL, and a snippet.\n\+- `web_fetch` — read one page. Argument: `url` (required http(s) string, typically a URL returned by `web_search`). \+Returns the page's readable text with its title and source URL (bounded in length).\n\n\+## Workflow\n\n\+1. Call `web_search` with a focused query.\n\+2. Pick the most relevant result(s) and call `web_fetch` on their URLs to read the full content — don't rely on \+snippets alone for anything important.\n\+3. Base your answer on what you read and cite the source URLs you used. If results are thin, refine the query and \+search again. If a tool returns text starting with `ERROR`, read it, adjust your arguments, and retry or tell the \+user what's wrong.";/// Guidance injected only when subagent tools are actually available, so the/// model delegates context-heavy exploration instead of exhausting its own@@ -234,6 +250,11 @@ mod tests {assert!(out.contains("web_fetch"));// Provider-neutral: the model must not be told to call a branded tool.assert!(out.contains("exa_search"), "guide names the anti-pattern to avoid");+ // Teaches how to call the tools, not just that they exist.+ assert!(out.contains("query"), "documents the web_search query arg");+ assert!(out.contains("count"), "documents the web_search count arg");+ assert!(out.contains("url"), "documents the web_fetch url arg");+ assert!(out.contains("Workflow"), "describes the search->fetch->cite flow");let _ = std::fs::remove_dir_all(&root);}diff --git a/src-tauri/src/core/agent/tools/web.rs b/src-tauri/src/core/agent/tools/web.rsindex 1e2ab0410..4278db784 100644--- a/src-tauri/src/core/agent/tools/web.rs+++ b/src-tauri/src/core/agent/tools/web.rs@@ -1,15 +1,27 @@//! 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. 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 is selected behind a small adapter-//! trait so a provider can change without touching the agent tool contract.+//! 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 is selected+//! behind a small adapter so a provider can change without touching the agent+//! tool contract.//!//! Per jan-internal#196, **Exa is the default backend**, not a product-facing-//! identity. The adapter normalizes each provider's response into the shared-//! [`SearchResult`] / [`FetchedPage`] contracts and bounds output size so tool-//! results can't blow up the model's context window.+//! identity. Exa exposes two zero-config surfaces and this adapter picks the+//! right one automatically so web search works out of the box:+//!+//! * **Keyless (default):** Exa's hosted endpoint at `https://mcp.exa.ai/mcp`+//! answers `web_search`/`web_fetch` over JSON-RPC with **no API key**. We call+//! it directly over HTTP from the Rust core (plain `reqwest`), so this is a+//! native compiled-in capability, not the agent's MCP client/registration.+//! * **Keyed (opt-in):** when an Exa API key is present in the environment we+//! use the structured REST API (`https://api.exa.ai`) instead, which returns+//! normalized JSON and higher rate limits.+//!+//! Either way the adapter normalizes results into the shared [`SearchResult`] /+//! [`FetchedPage`] contracts and bounds output size so tool results can't blow+//! up the model's context window.use serde::{Deserialize, Serialize};use serde_json::{json, Value};@@ -24,14 +36,24 @@ const SEARCH_MAX_COUNT: u32 = 20;/// Network timeout for a single provider request.const REQUEST_TIMEOUT_SECS: u64 = 30;-/// Environment variables consulted for the 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 after the native cutover.+/// Exa's keyless hosted endpoint. Answers `web_search_exa` / `web_fetch_exa`+/// over JSON-RPC (MCP wire protocol) with no credential — this is the free path+/// that makes web search work out of the box.+const EXA_HOSTED_URL: &str = "https://mcp.exa.ai/mcp";+/// Exa's structured REST search endpoint (used only when a key is configured).+const EXA_REST_SEARCH_URL: &str = "https://api.exa.ai/search";+/// Exa's structured REST contents endpoint (used only when a key is configured).+const EXA_REST_CONTENTS_URL: &str = "https://api.exa.ai/contents";++/// 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"];/// A single normalized web-search result. This is the stable contract the agent-/// sees, regardless of which backend produced it.+/// sees, regardless of which backend or transport produced it.#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]pub struct SearchResult {pub title: String,@@ -83,7 +105,7 @@ impl Provider {pub const DEFAULT: Provider = Provider::Exa;}-/// Read the first present, non-empty Exa API key from the environment.+/// Read the first present, non-empty Exa API key from the environment, if any.fn exa_api_key() -> Option<String> {for var in EXA_API_KEY_ENVS {if let Ok(v) = std::env::var(var) {@@ -96,111 +118,296 @@ fn exa_api_key() -> Option<String> {None}-/// Exa backend hitting the public REST API (`https://api.exa.ai`). Chosen as the-/// default because it offers a free credential path; it stays behind the adapter-/// so it is never exposed as the tool identity.+/// Which Exa transport the adapter uses for a given process environment.+#[derive(Debug, Clone, PartialEq, Eq)]+enum ExaMode {+ /// Keyless hosted JSON-RPC endpoint (the default free path).+ Hosted,+ /// Structured REST API, authenticated with the configured key.+ Rest(String),+}++/// Exa backend. Defaults to the keyless hosted endpoint so web search works with+/// no configuration; upgrades to the structured REST API when a key is present.+/// Kept behind the adapter so Exa is never exposed as the tool identity.pub struct ExaProvider {- api_key: String,+ mode: ExaMode,client: reqwest::Client,}impl ExaProvider {- /// Build the Exa adapter, resolving the credential from the environment.- /// Returns a clear, provider-identified configuration error when no key is- /// available, naming the variables to set.+ /// Build the Exa adapter, choosing the transport from the environment:+ /// a configured key selects the REST API, otherwise the keyless hosted+ /// endpoint. This never errors on missing credentials — web search is+ /// available out of the box.pub fn from_env() -> Result<Self, String> {- let api_key = exa_api_key().ok_or_else(|| {- format!(- "ERROR: web search provider 'Exa' is not configured. Set an Exa API key via one of these environment variables: {}. Get a free key at https://dashboard.exa.ai/api-keys.",- EXA_API_KEY_ENVS.join(" or ")- )- })?;+ let mode = match exa_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!("ERROR: failed to build HTTP client for Exa: {e}"))?;- Ok(Self { api_key, client })- }-}--impl SearchProvider for ExaProvider {- fn name(&self) -> &'static str {- "Exa"+ Ok(Self { mode, client })}- async fn search(&self, query: &str, count: u32) -> Result<Vec<SearchResult>, String> {+ /// Call the keyless hosted endpoint's `tools/call` and return the text of the+ /// first content block. The endpoint speaks MCP-over-HTTP (JSON-RPC framed as+ /// an SSE `data:` line); we parse it directly rather than going through the+ /// agent's MCP client, keeping this a native, compiled-in capability.+ async fn hosted_call(&self, tool: &str, arguments: Value) -> Result<String, String> {let body = json!({- "query": query,- "type": "auto",- "numResults": count,- "contents": {- "text": { "maxCharacters": 800 },- "highlights": { "numSentences": 3, "highlightsPerUrl": 1 }- }+ "jsonrpc": "2.0",+ "id": 1,+ "method": "tools/call",+ "params": { "name": tool, "arguments": arguments }});let resp = self.client- .post("https://api.exa.ai/search")- .header("x-api-key", &self.api_key)+ .post(EXA_HOSTED_URL).header("content-type", "application/json")+ .header("accept", "application/json, text/event-stream").json(&body).send().await- .map_err(|e| format!("ERROR: Exa search request failed: {e}"))?;+ .map_err(|e| format!("ERROR: Exa request failed: {e}"))?;let status = resp.status();let text = resp.text().await- .map_err(|e| format!("ERROR: Exa search: failed to read response body: {e}"))?;+ .map_err(|e| format!("ERROR: Exa: failed to read response body: {e}"))?;if !status.is_success() {return Err(format!(- "ERROR: Exa search failed with HTTP {}: {}",+ "ERROR: Exa failed with HTTP {}: {}",status.as_u16(),text.chars().take(400).collect::<String>()));}- let parsed: Value = serde_json::from_str(&text)- .map_err(|e| format!("ERROR: Exa search: invalid JSON response: {e}"))?;- Ok(normalize_exa_search(&parsed))+ parse_hosted_result_text(&text)+ }+}++impl SearchProvider for ExaProvider {+ fn name(&self) -> &'static str {+ "Exa"+ }++ 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!("ERROR: Exa search request failed: {e}"))?;+ let status = resp.status();+ let text = resp.text().await.map_err(|e| {+ format!("ERROR: Exa search: failed to read response body: {e}")+ })?;+ if !status.is_success() {+ return Err(format!(+ "ERROR: 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!("ERROR: Exa search: invalid JSON response: {e}"))?;+ Ok(normalize_exa_rest_search(&parsed))+ }+ }}async fn fetch(&self, url: &str) -> Result<FetchedPage, String> {- let body = json!({- "ids": [url],- "text": true+ 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!("ERROR: Exa fetch request failed: {e}"))?;+ let status = resp.status();+ let text = resp+ .text()+ .await+ .map_err(|e| format!("ERROR: Exa fetch: failed to read response body: {e}"))?;+ if !status.is_success() {+ return Err(format!(+ "ERROR: 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!("ERROR: Exa fetch: invalid JSON response: {e}"))?;+ normalize_exa_rest_fetch(&parsed, url)+ }+ }+ }+}++/// Extract `result.content[0].text` from a hosted-endpoint response. The body is+/// either raw JSON or an SSE frame (`event: message\ndata: {json}`); handle both.+fn parse_hosted_result_text(body: &str) -> Result<String, String> {+ let json_str = body+ .lines()+ .find_map(|l| l.strip_prefix("data:").map(str::trim))+ .unwrap_or_else(|| body.trim());+ let parsed: Value = serde_json::from_str(json_str)+ .map_err(|e| format!("ERROR: Exa: invalid response payload: {e}"))?;+ if let Some(err) = parsed.get("error") {+ return Err(format!("ERROR: Exa returned an error: {err}"));+ }+ let result = parsed+ .get("result")+ .ok_or("ERROR: Exa response missing 'result'")?;+ if result.get("isError").and_then(|v| v.as_bool()) == Some(true) {+ return Err(format!(+ "ERROR: Exa tool call failed: {}",+ 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())+ .unwrap_or("unknown error")+ ));+ }+ 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())+ .ok_or("ERROR: Exa response had no text content")?;+ Ok(text.to_string())+}++/// Parse the hosted `web_search_exa` text output into normalized results. The+/// endpoint returns human-readable blocks separated by `---`, each with+/// `Title:` / `URL:` / `Published:` lines followed by a `Highlights:` section.+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,});- let resp = self- .client- .post("https://api.exa.ai/contents")- .header("x-api-key", &self.api_key)- .header("content-type", "application/json")- .json(&body)- .send()- .await- .map_err(|e| format!("ERROR: Exa fetch request failed: {e}"))?;- let status = resp.status();- let text = resp- .text()- .await- .map_err(|e| format!("ERROR: Exa fetch: failed to read response body: {e}"))?;- if !status.is_success() {- return Err(format!(- "ERROR: Exa fetch failed with HTTP {}: {}",- status.as_u16(),- text.chars().take(400).collect::<String>()- ));+ }+ out+}++/// Parse the hosted `web_fetch_exa` text output into a bounded [`FetchedPage`].+/// The output leads with a `# Title` line and a `URL: ...` line, then the body.+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 parsed: Value = serde_json::from_str(&text)- .map_err(|e| format!("ERROR: Exa fetch: invalid JSON response: {e}"))?;- normalize_exa_fetch(&parsed, url)+ }+ 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,}}-/// Normalize an Exa `/search` response body into the shared result contract.-/// Pulls the first highlight as the snippet, falling back to the text excerpt.-fn normalize_exa_search(body: &Value) -> Vec<SearchResult> {- let results = body.get("results").and_then(|v| v.as_array());- let Some(results) = results else {+/// Normalize an Exa REST `/search` response body into the shared result+/// contract. Pulls the first highlight as the snippet, falling back to the text+/// excerpt.+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@@ -225,7 +432,7 @@ fn normalize_exa_search(body: &Value) -> Vec<SearchResult> {.or_else(|| {r.get("text").and_then(|v| v.as_str())- .map(|t| t.chars().take(300).collect::<String>())+ .map(|t| clip_chars(t, 300))}).unwrap_or_default();let published_at = r@@ -243,8 +450,8 @@ fn normalize_exa_search(body: &Value) -> Vec<SearchResult> {.collect()}-/// Normalize an Exa `/contents` response body into a bounded [`FetchedPage`].-fn normalize_exa_fetch(body: &Value, requested_url: &str) -> Result<FetchedPage, String> {+/// Normalize an Exa REST `/contents` response body into a bounded [`FetchedPage`].+fn normalize_exa_rest_fetch(body: &Value, requested_url: &str) -> Result<FetchedPage, String> {let first = body.get("results").and_then(|v| v.as_array())@@ -270,6 +477,15 @@ fn normalize_exa_fetch(body: &Value, requested_url: &str) -> Result<FetchedPage,})}+/// Truncate `s` to `max` chars at a char boundary (no truncation note).+fn clip_chars(s: &str, max: usize) -> String {+ if s.chars().count() <= max {+ s.to_string()+ } else {+ s.chars().take(max).collect()+ }+}+/// Truncate readable text to the fetch bound at a char boundary, reporting/// whether truncation occurred.fn bound_text(s: &str) -> (String, bool) {@@ -394,7 +610,83 @@ mod tests {}#[test]- fn normalize_exa_search_maps_contract() {+ fn from_env_defaults_to_keyless_hosted() {+ // No key configured in the test environment -> keyless hosted path.+ // (Guard against a stray real key in CI by only asserting the variant+ // shape when both vars are unset.)+ if std::env::var("JAN_EXA_API_KEY").is_err() && std::env::var("EXA_API_KEY").is_err() {+ let p = ExaProvider::from_env().unwrap();+ assert_eq!(p.mode, ExaMode::Hosted);+ }+ }++ #[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).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).unwrap(), "hi");+ }++ #[test]+ fn parse_hosted_result_text_surfaces_errors() {+ let err = "{\"error\":{\"code\":-32000,\"message\":\"boom\"}}";+ assert!(parse_hosted_result_text(err).unwrap_err().starts_with("ERROR"));+ let tool_err = "{\"result\":{\"isError\":true,\"content\":[{\"type\":\"text\",\"text\":\"bad\"}]}}";+ assert!(parse_hosted_result_text(tool_err)+ .unwrap_err()+ .contains("bad"));+ }++ #[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("..."));+ // "N/A" publish date normalizes to None.+ 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": [{@@ -410,69 +702,50 @@ mod tests {}]});- let results = normalize_exa_search(&body);+ let results = normalize_exa_rest_search(&body);assert_eq!(results.len(), 2);- assert_eq!(results[0].title, "Example");- assert_eq!(results[0].url, "https://example.com");assert_eq!(results[0].snippet, "Short result excerpt");assert_eq!(results[0].published_at.as_deref(),Some("2024-01-02T00:00:00.000Z"));- // Falls back to text excerpt when no highlight is present.assert!(results[1].snippet.starts_with("Body text fallback"));assert!(results[1].published_at.is_none());}#[test]- fn normalize_exa_search_empty_is_empty() {- assert!(normalize_exa_search(&json!({})).is_empty());- assert!(normalize_exa_search(&json!({"results": []})).is_empty());+ 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_fetch_bounds_and_titles() {+ fn normalize_exa_rest_fetch_bounds_and_titles() {let body = json!({- "results": [- { "url": "https://example.com", "title": "T", "text": "hello world" }- ]+ "results": [ { "url": "https://example.com", "title": "T", "text": "hello world" } ]});- let page = normalize_exa_fetch(&body, "https://example.com").unwrap();- assert_eq!(page.url, "https://example.com");+ 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_fetch_truncates_large_text() {- let big = "a".repeat(FETCH_MAX_CHARS + 100);- let body = json!({ "results": [ { "url": "u", "title": "t", "text": big } ] });- let page = normalize_exa_fetch(&body, "u").unwrap();- assert!(page.truncated);- assert_eq!(page.content.chars().count(), FETCH_MAX_CHARS);- }-- #[test]- fn normalize_exa_fetch_no_results_errors() {- let err = normalize_exa_fetch(&json!({"results": []}), "u").unwrap_err();+ fn normalize_exa_rest_fetch_no_results_errors() {+ let err = normalize_exa_rest_fetch(&json!({"results": []}), "u").unwrap_err();assert!(err.starts_with("ERROR"));}#[tokio::test]async fn web_search_requires_query() {- let out = web_search(&json!({})).await;- assert!(out.starts_with("ERROR"));- let out = web_search(&json!({"query": " "})).await;- assert!(out.starts_with("ERROR"));+ assert!(web_search(&json!({})).await.starts_with("ERROR"));+ assert!(web_search(&json!({"query": " "})).await.starts_with("ERROR"));}#[tokio::test]async fn web_fetch_validates_url() {- let out = web_fetch(&json!({})).await;- assert!(out.starts_with("ERROR"));- let out = web_fetch(&json!({"url": "ftp://x"})).await;- assert!(out.starts_with("ERROR"));+ assert!(web_fetch(&json!({})).await.starts_with("ERROR"));+ assert!(web_fetch(&json!({"url": "ftp://x"})).await.starts_with("ERROR"));}#[test]@@ -485,7 +758,6 @@ mod tests {}];let rendered = render_search_results(&results);assert!(rendered.contains("https://example.com"));- assert!(rendered.contains("T"));assert!(rendered.contains("snip"));}@@ -502,4 +774,14 @@ mod tests {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 })).await;+ assert!(!out.starts_with("ERROR"), "unexpected error: {out}");+ assert!(out.to_lowercase().contains("paris"), "got: {out}");+ }}