feat: Added Google PSE as an alternative web search provider (#65)

* Added Google PSE as an alternative web search tool

* Removed unecessary comments

* chore(ConfigManager): moved Google PSE input next to API key

---------

Co-authored-by: AnotiaWang <[email protected]>
This commit is contained in:
Jonathan Rampersad
2025-04-06 11:18:08 +08:00
committed by GitHub
co-authored by AnotiaWang
parent 651f445803
commit 5097c639d2
6 changed files with 97 additions and 1 deletions
+44
View File
@@ -46,6 +46,50 @@ export const useWebSearch = (): WebSearchFunction => {
}))
}
}
case 'google-pse': {
const apiKey = config.webSearch.apiKey
const pseId = config.webSearch.googlePseId
return async (q: string, o: WebSearchOptions) => {
if (!apiKey || !pseId) {
throw new Error('Google PSE API key or ID not set')
}
// Construct Google PSE API URL
// Ref: https://developers.google.com/custom-search/v1/using_rest
const searchParams = new URLSearchParams({
key: apiKey,
cx: pseId,
q: q,
num: o.maxResults?.toString() || '5',
});
if (o.lang) {
searchParams.append('lr', `lang_${o.lang}`);
}
const apiUrl = `https://www.googleapis.com/customsearch/v1?${searchParams.toString()}`;
try {
const response = await $fetch<{ items?: Array<{ title: string; link: string; snippet: string }> }>(apiUrl, { method: 'GET' });
if (!response.items) {
return [];
}
// Map response to WebSearchResult format
return response.items.map((item) => ({
content: item.snippet, // Use snippet as content
url: item.link,
title: item.title,
}));
} catch (error: any) {
console.error('Google PSE search failed:', error);
// Attempt to parse Google API error format
const errorMessage = error?.data?.error?.message || error.message || 'Unknown error';
throw new Error(`Google PSE Error: ${errorMessage}`);
}
}
}
case 'tavily':
default: {
const tvly = tavily({