| 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 | const contextItems = await getUrlContextItems(args.url, extras.fetch); |
| 8 | |
| 9 | // Track truncated content |
| 10 | const truncatedUrls: string[] = []; |
| 11 | |
| 12 | // Check and truncate each context item |
| 13 | const processedItems = contextItems.map((item) => { |
| 14 | if (item.content.length > DEFAULT_FETCH_URL_CHAR_LIMIT) { |
| 15 | truncatedUrls.push(args.url); |
| 16 | return { |
| 17 | ...item, |
| 18 | content: item.content.substring(0, DEFAULT_FETCH_URL_CHAR_LIMIT), |
| 19 | }; |
| 20 | } |
| 21 | return item; |
| 22 | }); |
| 23 | |
| 24 | // Add truncation warning if needed |
| 25 | if (truncatedUrls.length > 0) { |
| 26 | processedItems.push({ |
| 27 | name: "URL content truncation warning", |
| 28 | description: "Informs that URL content was truncated", |
| 29 | 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.`, |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | return processedItems; |
| 34 | }; |