| 1 | import TurndownService from "turndown"; |
| 2 | |
| 3 | import { Tool } from "./types.js"; |
| 4 | |
| 5 | const turndownService = new TurndownService({ |
| 6 | headingStyle: "atx", |
| 7 | codeBlockStyle: "fenced", |
| 8 | fence: "```", |
| 9 | emDelimiter: "*", |
| 10 | strongDelimiter: "**", |
| 11 | linkStyle: "inlined", |
| 12 | linkReferenceStyle: "full", |
| 13 | }); |
| 14 | |
| 15 | // Configure turndown for better markdown output |
| 16 | turndownService.addRule("strikethrough", { |
| 17 | filter: ["del", "s"], |
| 18 | replacement: function (content) { |
| 19 | return "~~" + content + "~~"; |
| 20 | }, |
| 21 | }); |
| 22 | |
| 23 | // Handle code blocks better |
| 24 | turndownService.addRule("pre", { |
| 25 | filter: "pre", |
| 26 | replacement: function (content, node) { |
| 27 | const codeEl = node.querySelector("code"); |
| 28 | const language = codeEl?.className.match(/language-(\w+)/)?.[1] || ""; |
| 29 | return "\n\n```" + language + "\n" + content + "\n```\n\n"; |
| 30 | }, |
| 31 | }); |
| 32 | |
| 33 | export const fetchTool: Tool = { |
| 34 | name: "Fetch", |
| 35 | displayName: "Fetch", |
| 36 | description: "Fetches content from a URL and converts it to markdown format", |
| 37 | parameters: { |
| 38 | url: { |
| 39 | type: "string", |
| 40 | description: "The URL to fetch content from", |
| 41 | required: true, |
| 42 | }, |
| 43 | timeout: { |
| 44 | type: "number", |
| 45 | description: "Request timeout in milliseconds (default: 10000)", |
| 46 | required: false, |
| 47 | }, |
| 48 | }, |
| 49 | readonly: true, |
| 50 | isBuiltIn: true, |
| 51 | preprocess: async (args) => { |
| 52 | return { |
| 53 | preview: [ |
| 54 | { |
| 55 | type: "text", |
| 56 | content: `Will fetch: ${args.url}`, |
| 57 | }, |
| 58 | ], |
| 59 | args, |
| 60 | }; |
| 61 | }, |
| 62 | run: async (args: { url: string; timeout?: number }): Promise<string> => { |
| 63 | const { url, timeout = 10000 } = args; |
| 64 | |
| 65 | try { |
| 66 | // Validate URL |
| 67 | const urlObj = new URL(url); |
| 68 | if (!["http:", "https:"].includes(urlObj.protocol)) { |
| 69 | return "Error: Only HTTP and HTTPS URLs are supported"; |
| 70 | } |
| 71 | |
| 72 | // Fetch the content |
| 73 | const controller = new AbortController(); |
| 74 | const timeoutId = setTimeout(() => controller.abort(), timeout); |
| 75 | |
| 76 | const response = await fetch(url, { |
| 77 | signal: controller.signal, |
| 78 | headers: { |
| 79 | "User-Agent": "Continue-CLI/1.0 (https://continue.dev)", |
| 80 | Accept: |
| 81 | "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
| 82 | "Accept-Language": "en-US,en;q=0.5", |
| 83 | "Accept-Encoding": "gzip, deflate", |
| 84 | "Cache-Control": "no-cache", |
| 85 | }, |
| 86 | }); |
| 87 | |
| 88 | clearTimeout(timeoutId); |
| 89 | |
| 90 | if (!response.ok) { |
| 91 | return `Error: HTTP ${response.status} - ${response.statusText}`; |
| 92 | } |
| 93 | |
| 94 | const contentType = response.headers.get("content-type") || ""; |
| 95 | |
| 96 | // Handle different content types |
| 97 | if (contentType.includes("application/json")) { |
| 98 | const json = await response.json(); |
| 99 | return "```json\n" + JSON.stringify(json, null, 2) + "\n```"; |
| 100 | } |
| 101 | |
| 102 | if (contentType.includes("text/plain")) { |
| 103 | const text = await response.text(); |
| 104 | return text; |
| 105 | } |
| 106 | |
| 107 | if (!contentType.includes("text/html")) { |
| 108 | return `Error: Unsupported content type: ${contentType}`; |
| 109 | } |
| 110 | |
| 111 | // Process HTML content |
| 112 | const html = await response.text(); |
| 113 | |
| 114 | // Convert HTML to markdown |
| 115 | const markdown = turndownService.turndown(html); |
| 116 | |
| 117 | // Clean up the markdown |
| 118 | const cleanedMarkdown = markdown |
| 119 | .replace(/\n\s*\n\s*\n/g, "\n\n") // Remove excessive newlines |
| 120 | .replace(/^\s+|\s+$/g, "") // Trim whitespace |
| 121 | .replace(/\\\[/g, "[") // Unescape brackets |
| 122 | .replace(/\\\]/g, "]"); |
| 123 | |
| 124 | return `# Content from ${url}\n\n${cleanedMarkdown}`; |
| 125 | } catch (error) { |
| 126 | if (error instanceof TypeError && error.message.includes("fetch")) { |
| 127 | return `Error: Failed to fetch URL "${url}". Please check the URL and try again.`; |
| 128 | } |
| 129 | if (error instanceof Error && error.name === "AbortError") { |
| 130 | return `Error: Request timed out after ${timeout}ms`; |
| 131 | } |
| 132 | return `Error: ${error instanceof Error ? error.message : String(error)}`; |
| 133 | } |
| 134 | }, |
| 135 | }; |