| 1 | import { ToolImpl } from "."; |
| 2 | import { fetchSearchResults } from "../../context/providers/WebContextProvider"; |
| 3 | |
| 4 | const DEFAULT_WEB_SEARCH_CHAR_LIMIT = 8000; |
| 5 | |
| 6 | export const searchWebImpl: ToolImpl = async (args, extras) => { |
| 7 | const webResults = await fetchSearchResults(args.query, 5, extras.fetch); |
| 8 | |
| 9 | // Track truncated results |
| 10 | const truncatedResults: string[] = []; |
| 11 | |
| 12 | // Check and truncate each result |
| 13 | const processedResults = webResults.map((result, index) => { |
| 14 | if (result.content.length > DEFAULT_WEB_SEARCH_CHAR_LIMIT) { |
| 15 | truncatedResults.push( |
| 16 | result.name || result.description || `Result #${index + 1}`, |
| 17 | ); |
| 18 | return { |
| 19 | ...result, |
| 20 | content: result.content.substring(0, DEFAULT_WEB_SEARCH_CHAR_LIMIT), |
| 21 | }; |
| 22 | } |
| 23 | return result; |
| 24 | }); |
| 25 | |
| 26 | // Add truncation warning if needed |
| 27 | if (truncatedResults.length > 0) { |
| 28 | processedResults.push({ |
| 29 | name: "Web search truncation warning", |
| 30 | description: "Informs that search results were truncated", |
| 31 | content: `The content from the following search results was truncated because it exceeded the ${DEFAULT_WEB_SEARCH_CHAR_LIMIT} character limit: ${truncatedResults.join(", ")}. For more detailed information, consider refining your search query.`, |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | return processedResults; |
| 36 | }; |