The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
feat(search): honor Brave/Tavily provider settings + German search intents
purple4faa84ccdee7
diff --git a/src-tauri/src/commands/remote.rs b/src-tauri/src/commands/remote.rsindex 7c2ef4ba..477fd49f 100644--- a/src-tauri/src/commands/remote.rs+++ b/src-tauri/src/commands/remote.rs@@ -509,7 +509,9 @@ async fn handle_agent_tool(.and_then(|v| v.as_u64()).map(|n| n as usize);if query.is_empty() { Err("web_search needs a non-empty `query` argument.".into()) }- else { crate::commands::search::web_search(query, count, app_state).await }+ // Remote clients carry no provider settings — None/None/None =+ // 'auto' without keys, i.e. the free tiers (pre-2.5.3 behaviour).+ else { crate::commands::search::web_search(query, count, None, None, None, app_state).await }}"web_fetch" => {let url = body.args.get("url").and_then(|v| v.as_str()).unwrap_or("").to_string();diff --git a/src-tauri/src/commands/search.rs b/src-tauri/src/commands/search.rsindex 79f7e487..2a13e00d 100644--- a/src-tauri/src/commands/search.rs+++ b/src-tauri/src/commands/search.rs@@ -108,6 +108,118 @@ async fn try_ddg(query: &str, count: usize) -> Result<Vec<SearchResult>, String>}}+/// Parse a Brave Search API response body into results. Split out of+/// `try_brave` so the shape mapping is unit-testable without network.+fn parse_brave_results(json: &serde_json::Value, count: usize) -> Vec<SearchResult> {+ json.pointer("/web/results")+ .and_then(|r| r.as_array())+ .map(|arr| {+ arr.iter()+ .take(count)+ .filter_map(|r| {+ Some(SearchResult {+ title: html_decode(&strip_html(r.get("title")?.as_str()?)),+ url: r.get("url")?.as_str()?.to_string(),+ snippet: r+ .get("description")+ .and_then(|d| d.as_str())+ .map(|d| html_decode(&strip_html(d)))+ .unwrap_or_default(),+ })+ })+ .collect()+ })+ .unwrap_or_default()+}++/// Brave Search API (paid/free-tier key from Settings → Agent → Search+/// Provider). The settings fields existed since v2.4 but were never wired+/// into this command — the key was silently ignored (same bug class as+/// GitHub #59's silent reset button). Now: explicit provider support.+async fn try_brave(query: &str, count: usize, api_key: &str) -> Result<Vec<SearchResult>, String> {+ let url = format!(+ "https://api.search.brave.com/res/v1/web/search?q={}&count={}",+ urlencoding::encode(query),+ count.min(20)+ );+ let client = reqwest::Client::builder()+ .timeout(std::time::Duration::from_secs(10))+ .build()+ .map_err(|e| e.to_string())?;+ let resp = client+ .get(&url)+ .header("Accept", "application/json")+ .header("X-Subscription-Token", api_key)+ .send()+ .await+ .map_err(|e| format!("Brave: {}", e))?;+ let status = resp.status();+ if !status.is_success() {+ return Err(format!("Brave: HTTP {} (check the API key)", status.as_u16()));+ }+ let json: serde_json::Value = resp.json().await.map_err(|e| format!("Brave: {}", e))?;+ let results = parse_brave_results(&json, count);+ if results.is_empty() {+ Err("Brave: no results".to_string())+ } else {+ Ok(results)+ }+}++/// Parse a Tavily API response body into results (unit-testable, no network).+fn parse_tavily_results(json: &serde_json::Value, count: usize) -> Vec<SearchResult> {+ json.get("results")+ .and_then(|r| r.as_array())+ .map(|arr| {+ arr.iter()+ .take(count)+ .filter_map(|r| {+ Some(SearchResult {+ title: r.get("title")?.as_str()?.to_string(),+ url: r.get("url")?.as_str()?.to_string(),+ snippet: r+ .get("content")+ .and_then(|c| c.as_str())+ .unwrap_or("")+ .to_string(),+ })+ })+ .collect()+ })+ .unwrap_or_default()+}++/// Tavily search API. Key in the JSON body (legacy) AND as Bearer header+/// (current docs) — both are accepted by Tavily, covering old + new plans.+async fn try_tavily(query: &str, count: usize, api_key: &str) -> Result<Vec<SearchResult>, String> {+ let client = reqwest::Client::builder()+ .timeout(std::time::Duration::from_secs(10))+ .build()+ .map_err(|e| e.to_string())?;+ let resp = client+ .post("https://api.tavily.com/search")+ .header("Authorization", format!("Bearer {}", api_key))+ .json(&serde_json::json!({+ "api_key": api_key,+ "query": query,+ "max_results": count.min(20),+ }))+ .send()+ .await+ .map_err(|e| format!("Tavily: {}", e))?;+ let status = resp.status();+ if !status.is_success() {+ return Err(format!("Tavily: HTTP {} (check the API key)", status.as_u16()));+ }+ let json: serde_json::Value = resp.json().await.map_err(|e| format!("Tavily: {}", e))?;+ let results = parse_tavily_results(&json, count);+ if results.is_empty() {+ Err("Tavily: no results".to_string())+ } else {+ Ok(results)+ }+}+async fn try_wikipedia(query: &str, count: usize) -> Result<Vec<SearchResult>, String> {let url = format!("https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={}&format=json&srlimit={}",@@ -165,28 +277,92 @@ fn strip_html(s: &str) -> String {pub async fn web_search(query: String,count: Option<usize>,+ provider: Option<String>,+ brave_api_key: Option<String>,+ tavily_api_key: Option<String>,state: State<'_, AppState>,) -> Result<serde_json::Value, String> {let count = count.unwrap_or(5);+ let provider = provider.unwrap_or_else(|| "auto".to_string());+ let brave_key = brave_api_key.unwrap_or_default();+ let tavily_key = tavily_api_key.unwrap_or_default();++ // The frontend has offered Brave / Tavily in Settings → Agent → Search+ // Provider since v2.4, but this command silently dropped the choice and+ // always ran the free tiers. Honor it now:+ // - explicit 'brave' / 'tavily' → use that provider; on failure fall+ // back to the free tiers but surface why in `providerError`.+ // - 'auto' → paid provider first when a key is configured, then the+ // free tiers (SearXNG → DuckDuckGo → Wikipedia).+ let mut provider_error: Option<String> = None;++ match provider.as_str() {+ "brave" => {+ if brave_key.is_empty() {+ provider_error = Some(+ "Brave Search is selected but no API key is configured (Settings → Agent → Search Provider).".to_string(),+ );+ } else {+ match try_brave(&query, count, &brave_key).await {+ Ok(results) => return Ok(serde_json::json!({"results": results, "provider": "brave"})),+ Err(e) => provider_error = Some(e),+ }+ }+ }+ "tavily" => {+ if tavily_key.is_empty() {+ provider_error = Some(+ "Tavily is selected but no API key is configured (Settings → Agent → Search Provider).".to_string(),+ );+ } else {+ match try_tavily(&query, count, &tavily_key).await {+ Ok(results) => return Ok(serde_json::json!({"results": results, "provider": "tavily"})),+ Err(e) => provider_error = Some(e),+ }+ }+ }+ _ => {+ // auto: a configured key signals intent — prefer that provider.+ if !brave_key.is_empty() {+ if let Ok(results) = try_brave(&query, count, &brave_key).await {+ return Ok(serde_json::json!({"results": results, "provider": "brave"}));+ }+ }+ if !tavily_key.is_empty() {+ if let Ok(results) = try_tavily(&query, count, &tavily_key).await {+ return Ok(serde_json::json!({"results": results, "provider": "tavily"}));+ }+ }+ }+ }++ // Free tiers. Reached directly in 'auto' or as fallback after a paid+ // provider failed (provider_error carries the reason to the model).+ let attach = |mut v: serde_json::Value| {+ if let Some(err) = &provider_error {+ v["providerError"] = serde_json::json!(err);+ }+ v+ };// Try SearXNG firstif state.searxng_available.load(Ordering::Relaxed) {if let Ok(results) = try_searxng(&query, count).await {- return Ok(serde_json::json!({"results": results}));+ return Ok(attach(serde_json::json!({"results": results, "provider": "searxng"})));}}// Fallback to DuckDuckGoif let Ok(results) = try_ddg(&query, count).await {- return Ok(serde_json::json!({"results": results}));+ return Ok(attach(serde_json::json!({"results": results, "provider": "duckduckgo"})));}// Fallback to Wikipediaif let Ok(results) = try_wikipedia(&query, count).await {- return Ok(serde_json::json!({"results": results}));+ return Ok(attach(serde_json::json!({"results": results, "provider": "wikipedia"})));}- Ok(serde_json::json!({"results": [], "error": "All search tiers failed"}))+ Ok(attach(serde_json::json!({"results": [], "error": "All search tiers failed"})))}#[tauri::command]@@ -445,3 +621,70 @@ fn collapse_whitespace(s: &str) -> String {}out.trim().to_string()}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn brave_parses_web_results() {+ let json: serde_json::Value = serde_json::json!({+ "web": { "results": [+ { "title": "Rust <strong>lang</strong>", "url": "https://rust-lang.org", "description": "A & B systems language" },+ { "title": "Second", "url": "https://example.com", "description": "More" },+ { "title": "Third", "url": "https://third.example" }+ ]}+ });+ let r = parse_brave_results(&json, 2);+ assert_eq!(r.len(), 2);+ assert_eq!(r[0].title, "Rust lang");+ assert_eq!(r[0].url, "https://rust-lang.org");+ assert_eq!(r[0].snippet, "A & B systems language");+ }++ #[test]+ fn brave_missing_description_is_empty_snippet() {+ let json: serde_json::Value = serde_json::json!({+ "web": { "results": [ { "title": "T", "url": "https://x.example" } ] }+ });+ let r = parse_brave_results(&json, 5);+ assert_eq!(r.len(), 1);+ assert_eq!(r[0].snippet, "");+ }++ #[test]+ fn brave_unexpected_shape_yields_empty() {+ let json: serde_json::Value = serde_json::json!({ "message": "invalid key" });+ assert!(parse_brave_results(&json, 5).is_empty());+ }++ #[test]+ fn tavily_parses_results() {+ let json: serde_json::Value = serde_json::json!({+ "results": [+ { "title": "Doc", "url": "https://docs.example", "content": "Body text" },+ { "title": "NoContent", "url": "https://b.example" }+ ],+ "answer": "ignored"+ });+ let r = parse_tavily_results(&json, 5);+ assert_eq!(r.len(), 2);+ assert_eq!(r[0].snippet, "Body text");+ assert_eq!(r[1].snippet, "");+ }++ #[test]+ fn tavily_unexpected_shape_yields_empty() {+ let json: serde_json::Value = serde_json::json!({ "detail": "unauthorized" });+ assert!(parse_tavily_results(&json, 5).is_empty());+ }++ #[test]+ fn tavily_respects_count_cap() {+ let arr: Vec<serde_json::Value> = (0..10)+ .map(|i| serde_json::json!({ "title": format!("t{}", i), "url": format!("https://e{}.example", i), "content": "" }))+ .collect();+ let json = serde_json::json!({ "results": arr });+ assert_eq!(parse_tavily_results(&json, 3).len(), 3);+ }+}diff --git a/src/api/mcp/builtin-tools.ts b/src/api/mcp/builtin-tools.tsindex 99c2bf7a..2ba17b65 100644--- a/src/api/mcp/builtin-tools.ts+++ b/src/api/mcp/builtin-tools.ts@@ -609,10 +609,21 @@ async function executeWebSearch(args: Record<string, any>): Promise<string> {braveApiKey: searchSettings.braveApiKey || '',tavilyApiKey: searchSettings.tavilyApiKey || '',})- if (Array.isArray(data.results)) {- return data.results+ if (Array.isArray(data.results) && data.results.length > 0) {+ const lines = data.results.map((r: any, i: number) => `${i + 1}. ${r.title}\n ${r.url}\n ${r.snippet}`).join('\n\n')+ // When the configured paid provider failed we still return free-tier+ // results, but say why the configured one didn't answer — a silently+ // swallowed bad API key would look like "search is broken".+ const note = typeof data.providerError === 'string' && data.providerError+ ? `\n\n[Note: configured search provider failed — ${data.providerError}. Results above are from the free fallback (${data.provider || 'fallback'}).]`+ : ''+ return lines + note+ }+ if (typeof data.error === 'string' && data.error) {+ const extra = typeof data.providerError === 'string' && data.providerError ? ` (${data.providerError})` : ''+ return `Web search failed: ${data.error}${extra}`}return JSON.stringify(data)}diff --git a/src/lib/__tests__/tool-selection-web.test.ts b/src/lib/__tests__/tool-selection-web.test.tsnew file mode 100644index 00000000..36ac7c76--- /dev/null+++ b/src/lib/__tests__/tool-selection-web.test.ts@@ -0,0 +1,66 @@+import { describe, it, expect } from 'vitest'+import { selectRelevantTools } from '../tool-selection'+import type { MCPToolDefinition, PermissionMap } from '../../api/mcp/types'+import { DEFAULT_PERMISSIONS } from '../../api/mcp/types'++// Minimal tool defs — names + categories are all selectRelevantTools needs.+const T = (name: string, category: MCPToolDefinition['category']): MCPToolDefinition => ({+ name,+ description: name,+ inputSchema: { type: 'object', properties: {} },+ category,+ source: 'builtin',+})++const ALL: MCPToolDefinition[] = [+ T('web_search', 'web'),+ T('web_fetch', 'web'),+ T('file_read', 'filesystem'),+ T('file_write', 'filesystem'),+ T('file_list', 'filesystem'),+ T('file_search', 'filesystem'),+ T('shell_execute', 'terminal'),+ T('code_execute', 'terminal'),+ T('system_info', 'system'),+ T('get_current_time', 'system'),+ T('image_generate', 'image'),+ T('video_generate', 'video'),+]++const PERMS: PermissionMap = { ...DEFAULT_PERMISSIONS, video: 'auto' }++const names = (msg: string, perms: PermissionMap = PERMS) =>+ selectRelevantTools(msg, ALL, perms).map((t) => t.name)++describe('web search tool selection (Websearch im Chat)', () => {+ it('selects web tools for English search intents', () => {+ expect(names('search the web for the latest Tauri release')).toContain('web_search')+ expect(names('look up the weather in Berlin tomorrow')).toContain('web_search')+ expect(names('fetch https://example.com/changelog and summarize it')).toContain('web_fetch')+ })++ it('selects web tools for German search intents', () => {+ expect(names('such nach den neuesten ollama versionen')).toContain('web_search')+ expect(names('recherchiere bitte die aktuellen GPU preise')).toContain('web_search')+ expect(names('was sind die neuesten nachrichten zu rust?')).toContain('web_search')+ expect(names('wie ist das wetter morgen in hamburg')).toContain('web_search')+ })++ it('does NOT trigger on the English phrase "such as"', () => {+ // "such " as a keyword would match every "such as" sentence — pinned here+ // so nobody re-adds it.+ const sel = names('write a poem about animals such as cats and dogs')+ expect(sel).not.toContain('web_search')+ })++ it('web tools drop out when the web category is blocked', () => {+ const sel = names('search the web for rust news', { ...PERMS, web: 'blocked' })+ expect(sel).not.toContain('web_search')+ expect(sel).not.toContain('web_fetch')+ })++ it('generic short prompts include web_search as flexibility fallback', () => {+ // Existing behaviour (selectedNames.size <= 3 → add common tools) — pinned.+ expect(names('help me with this project')).toContain('web_search')+ })+})diff --git a/src/lib/tool-selection.ts b/src/lib/tool-selection.tsindex e04615d0..9a945901 100644--- a/src/lib/tool-selection.ts+++ b/src/lib/tool-selection.ts@@ -15,7 +15,13 @@ interface ToolGroup {const TOOL_GROUPS: ToolGroup[] = [{- keywords: ['search', 'find online', 'look up', 'google', 'internet', 'news', 'latest', 'current'],+ // Web search intents. German terms included (image group below has them+ // too) — without them a German "such im internet nach…" never surfaced+ // web_search and the model answered from stale training data instead.+ keywords: ['search', 'find online', 'look up', 'google', 'bing', 'duckduckgo', 'internet',+ 'news', 'latest', 'current', 'web search', 'websearch', 'browse', 'website', 'webseite',+ 'url', 'http://', 'https://', 'weather', 'wetter',+ 'suche', 'such nach', 'such im', 'recherch', 'nachrichten', 'neueste', 'aktuell'],tools: ['web_search', 'web_fetch'],},{