Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ OPENAI_API_KEY=APIKEYGOESHERE
SERPER_API=APIKEYGOESHERE
# Brave Search API Key (Serper is the default, brave is an alternative option for search)
BRAVE_SEARCH_API_KEY=APIKEYGOESHERE
# OPTIONAL - Tavily Search API Key (alternative search provider)
TAVILY_API_KEY=APIKEYGOESHERE


# OPTIONAL - Set LAN GPU server, examples:
Expand Down
2 changes: 1 addition & 1 deletion app/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
export const config = {
useOllamaInference: false,
useOllamaEmbeddings: false,
searchProvider: 'serper', // 'serper', 'google' // 'serper' is the default
searchProvider: 'serper', // 'serper', 'google', 'brave', 'tavily' // 'serper' is the default
inferenceModel: 'llama-3.1-70b-versatile', // Groq: 'mixtral-8x7b-32768', 'gemma-7b-it' // OpenAI: 'gpt-3.5-turbo', 'gpt-4' // Ollama 'mistral', 'llama3' etc
inferenceAPIKey: process.env.GROQ_API_KEY, // Groq: process.env.GROQ_API_KEY // OpenAI: process.env.OPENAI_API_KEY // Ollama: 'ollama' is the default
embeddingsModel: 'text-embedding-3-small', // Ollama: 'llama2', 'nomic-embed-text' // OpenAI 'text-embedding-3-small', 'text-embedding-3-large'
Expand Down
19 changes: 19 additions & 0 deletions app/tools/searchProviders.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";
import { SearchResult } from '@/components/answer/SearchResultsComponent';
import { config } from '../config';
import { tavily } from '@tavily/core';

export async function getSearchResults(userMessage: string): Promise<any> {
switch (config.searchProvider) {
Expand All @@ -10,6 +11,8 @@ export async function getSearchResults(userMessage: string): Promise<any> {
return serperSearch(userMessage);
case "google":
return googleSearch(userMessage);
case "tavily":
return tavilySearch(userMessage);
default:
return Promise.reject(new Error(`Unsupported search provider: ${config.searchProvider}`));
}
Expand Down Expand Up @@ -100,6 +103,22 @@ export async function serperSearch(message: string, numberOfPagesToScan = config
}
}

export async function tavilySearch(message: string, numberOfPagesToScan = config.numberOfPagesToScan): Promise<SearchResult[]> {
try {
const tvly = tavily({ apiKey: process.env.TAVILY_API_KEY as string });
const response = await tvly.search(message, { maxResults: numberOfPagesToScan });
const final = response.results.map((result: any): SearchResult => ({
title: result.title,
link: result.url,
favicon: `https://www.google.com/s2/favicons?domain=${new URL(result.url).hostname}&sz=128`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 A single malformed search result URL crashes the entire search instead of gracefully skipping it

A URL is parsed with a throwing constructor (new URL(result.url) at app/tools/searchProviders.tsx:113) inside a .map() over all results, so one malformed URL from the search API causes every valid result to be discarded and the search to fail entirely.

Impact: Users see a complete search failure instead of results whenever the upstream API returns even one result with an unusual or malformed URL.

Mechanism: new URL() throws inside .map(), aborting the entire transformation

The tavilySearch function at app/tools/searchProviders.tsx:106-120 maps over response.results and calls new URL(result.url).hostname to construct a favicon URL. The new URL() constructor throws a TypeError for malformed URLs. Because this is inside .map(), a single bad URL aborts the entire mapping. The error propagates to the catch block at line 116-118, which re-throws, causing the whole search operation to fail.

Other search providers (brave, google, serper) don't have this risk because they don't parse URLs with new URL(). A safer approach would be to wrap the new URL() call in a try/catch or use a fallback for the favicon.

Suggested change
favicon: `https://www.google.com/s2/favicons?domain=${new URL(result.url).hostname}&sz=128`
favicon: (() => { try { return `https://www.google.com/s2/favicons?domain=${new URL(result.url).hostname}&sz=128`; } catch { return ''; } })()
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

}));
return final;
} catch (error) {
console.error('Error fetching search results:', error);
throw error;
}
}

export async function getImages(message: string): Promise<{ title: string; link: string }[]> {
const url = 'https://google.serper.dev/images';
const data = JSON.stringify({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@langchain/community": "latest",
"@langchain/openai": "^0.0.25",
"@phosphor-icons/react": "^2.1.5",
"@tavily/core": "^0.6.1",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-label": "^2.0.2",
Expand Down