| 1 | import { ToolImpl } from "."; |
| 2 | import { getUrlContextItems } from "../../context/providers/URLContextProvider"; |
| 3 | |
| 4 | const DEFAULT_FETCH_URL_CHAR_LIMIT = 20000; |
| 5 | |
| 6 | export const fetchUrlContentImpl: ToolImpl = async (args, extras) => { |
| 7 | if (!args?.url) { |
| 8 | throw new Error("`url` argument is required and cannot be empty"); |
| 9 | } |
| 10 | |
| 11 | const contextItems = await getUrlContextItems(args.url, extras.fetch); |
| 12 | |
| 13 | // Track truncated content |
| 14 | const truncatedUrls: string[] = []; |
| 15 | |
| 16 | // Check and truncate each context item |
| 17 | const processedItems = contextItems.map((item) => { |
| 18 | if (item.content.length > DEFAULT_FETCH_URL_CHAR_LIMIT) { |
| 19 | truncatedUrls.push(args.url); |
| 20 | return { |
| 21 | ...item, |
| 22 | content: item.content.substring(0, DEFAULT_FETCH_URL_CHAR_LIMIT), |
| 23 | }; |
| 24 | } |
| 25 | return item; |
| 26 | }); |
| 27 | |
| 28 | // Add truncation warning if needed |
| 29 | if (truncatedUrls.length > 0) { |
| 30 | processedItems.push({ |
| 31 | name: "URL content truncation warning", |
| 32 | description: "Informs that URL content was truncated", |
| 33 | content: `The content from ${truncatedUrls.join(", ")} was truncated because it exceeded the ${DEFAULT_FETCH_URL_CHAR_LIMIT} character limit. If you need more content, consider fetching specific sections or using a more targeted approach.`, |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | return processedItems; |
| 38 | }; |