Elffuss BraidHub

kikocis/continue public

b06f5503dce8 · 1.6 KiBRawPermalink
1import type { ContextItem } from "core/index.js";
2import { fetchUrlContentImpl } from "core/tools/implementations/fetchUrlContent.js";
3
4import { Tool } from "./types.js";
5
6export const fetchTool: Tool = {
7 name: "Fetch",
8 displayName: "Fetch",
9 description:
10 "Fetches content from a URL, converts to markdown, and handles long content with truncation",
11 parameters: {
12 url: {
13 type: "string",
14 description: "The URL to fetch content from",
15 required: true,
16 },
17 },
18 readonly: true,
19 isBuiltIn: true,
20 preprocess: async (args) => {
21 return {
22 preview: [
23 {
24 type: "text",
25 content: `Will fetch: ${args.url}`,
26 },
27 ],
28 args,
29 };
30 },
31 run: async (args: { url: string }): Promise<string> => {
32 const { url } = args;
33
34 try {
35 // Suppress console errors from JSDOM CSS parsing
36 const originalConsoleError = console.error;
37 console.error = () => {};
38
39 // Use the core fetchUrlContent implementation
40 const contextItems = await fetchUrlContentImpl({ url }, { fetch });
41
42 // Restore console.error
43 console.error = originalConsoleError;
44
45 if (contextItems.length === 0) {
46 return `Error: Could not fetch content from ${url}`;
47 }
48
49 // Format the results for CLI display
50 return contextItems
51 .map((item: ContextItem) => {
52 if (item.name === "Truncation warning") {
53 return item.content;
54 }
55 return item.content;
56 })
57 .join("\n\n");
58 } catch (error) {
59 return `Error: ${error instanceof Error ? error.message : String(error)}`;
60 }
61 },
62};