From bda748eb7eb284a3321267530de7cdf8546844f7 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 15 Jul 2025 00:26:39 -0700 Subject: [PATCH 1/3] refactor to loadAssistant function --- src/index.ts | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2806078ef..5448d1730 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { constructLlmApi, LLMConfig, } from "@continuedev/openai-adapters"; -import { Assistant, ContinueClient } from "@continuedev/sdk"; +import { Assistant } from "@continuedev/sdk"; import chalk from "chalk"; import { ChatCompletionMessageParam } from "openai/resources.mjs"; import * as readlineSync from "readline-sync"; @@ -75,6 +75,15 @@ function getLlmApi( return [llmApi, model.model]; } +async function loadAssistant( + accessToken: string | undefined, + configPath: string +): Promise { + const continueSdk = await initializeContinueSDK(accessToken, configPath); + + return continueSdk.assistant; +} + async function chat() { const isAuthenticated = await ensureAuthenticated(true); @@ -114,28 +123,12 @@ async function chat() { // } // Initialize ContinueSDK and MCPService once - let assistant: ContinueClient["assistant"]; - let model: string; - let llmApi: BaseLlmApi; - let mcpService: MCPService; - - try { - const continueSdk = await initializeContinueSDK( - authConfig.accessToken, - args.configPath - ); - - assistant = continueSdk.assistant; - [llmApi, model] = getLlmApi(assistant, authConfig); - - mcpService = await MCPService.create(assistant.config); - } catch (error) { - console.error( - chalk.red(`Error loading assistant ${args.configPath}:`), - error - ); - throw error; - } + const assistant = await loadAssistant( + authConfig.accessToken, + args.configPath + ); + const [llmApi, model] = getLlmApi(assistant, authConfig); + const mcpService = await MCPService.create(assistant.config); // If not in headless mode, start the TUI chat (default) if (!args.isHeadless) { From 323bb57e7a38d02a38d985f3f667cacbfa8af949 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 15 Jul 2025 00:36:59 -0700 Subject: [PATCH 2/3] Add fetch tool with HTML to markdown conversion using turndown --- package-lock.json | 23 +++++++++ package.json | 2 + src/tools/fetch.ts | 121 +++++++++++++++++++++++++++++++++++++++++++++ src/tools/index.ts | 4 +- 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/tools/fetch.ts diff --git a/package-lock.json b/package-lock.json index 410d97a02..863704155 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@continuedev/sdk": "^0.0.12", "@modelcontextprotocol/sdk": "^1.9.0", "@types/react": "^19.1.8", + "@types/turndown": "^5.0.5", "@workos-inc/node": "^7.45.0", "axios": "^1.8.4", "chalk": "^5.4.1", @@ -28,6 +29,7 @@ "open": "^10.1.0", "react": "^19.1.0", "readline-sync": "^1.4.10", + "turndown": "^7.2.0", "typescript": "^5.8.3", "uuid": "^9.0.1" }, @@ -2569,6 +2571,12 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@mixmark-io/domino": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@mixmark-io/domino/-/domino-2.2.0.tgz", + "integrity": "sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw==", + "license": "BSD-2-Clause" + }, "node_modules/@modelcontextprotocol/sdk": { "version": "1.11.0", "resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.11.0.tgz", @@ -4348,6 +4356,12 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/turndown": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@types/turndown/-/turndown-5.0.5.tgz", + "integrity": "sha512-TL2IgGgc7B5j78rIccBtlYAnkuv8nUQqhQc+DSYV5j9Be9XOcm/SKOVRuA47xAVI3680Tk9B1d8flK2GWT2+4w==", + "license": "MIT" + }, "node_modules/@types/unist": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.3.tgz", @@ -15223,6 +15237,15 @@ "fsevents": "~2.3.3" } }, + "node_modules/turndown": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.2.0.tgz", + "integrity": "sha512-eCZGBN4nNNqM9Owkv9HAtWRYfLA4h909E/WGAWWBpmB275ehNhZyk87/Tpvjbp0jjNl9XwCsbe6bm6CqFsgD+A==", + "license": "MIT", + "dependencies": { + "@mixmark-io/domino": "^2.2.0" + } + }, "node_modules/type-detect": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", diff --git a/package.json b/package.json index ab5fb9831..7f8c41e92 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@continuedev/sdk": "^0.0.12", "@modelcontextprotocol/sdk": "^1.9.0", "@types/react": "^19.1.8", + "@types/turndown": "^5.0.5", "@workos-inc/node": "^7.45.0", "axios": "^1.8.4", "chalk": "^5.4.1", @@ -51,6 +52,7 @@ "open": "^10.1.0", "react": "^19.1.0", "readline-sync": "^1.4.10", + "turndown": "^7.2.0", "typescript": "^5.8.3", "uuid": "^9.0.1" }, diff --git a/src/tools/fetch.ts b/src/tools/fetch.ts new file mode 100644 index 000000000..15b13bc24 --- /dev/null +++ b/src/tools/fetch.ts @@ -0,0 +1,121 @@ +import { Tool } from "./types.js"; +import TurndownService from "turndown"; + +const turndownService = new TurndownService({ + headingStyle: "atx", + codeBlockStyle: "fenced", + fence: "```", + emDelimiter: "*", + strongDelimiter: "**", + linkStyle: "inlined", + linkReferenceStyle: "full", +}); + +// Configure turndown for better markdown output +turndownService.addRule("strikethrough", { + filter: ["del", "s", "strike"], + replacement: function (content) { + return "~~" + content + "~~"; + }, +}); + +// Handle code blocks better +turndownService.addRule("pre", { + filter: "pre", + replacement: function (content, node) { + const codeEl = node.querySelector("code"); + const language = codeEl?.className.match(/language-(\w+)/)?.[1] || ""; + return "\n\n```" + language + "\n" + content + "\n```\n\n"; + }, +}); + +export const fetchTool: Tool = { + name: "fetch", + displayName: "Fetch URL", + description: "Fetches content from a URL and converts it to markdown format", + parameters: { + url: { + type: "string", + description: "The URL to fetch content from", + required: true, + }, + timeout: { + type: "number", + description: "Request timeout in milliseconds (default: 10000)", + required: false, + }, + }, + run: async (args: { url: string; timeout?: number }): Promise => { + const { url, timeout = 10000 } = args; + + try { + // Validate URL + const urlObj = new URL(url); + if (!["http:", "https:"].includes(urlObj.protocol)) { + return "Error: Only HTTP and HTTPS URLs are supported"; + } + + // Fetch the content + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), timeout); + + const response = await fetch(url, { + signal: controller.signal, + headers: { + "User-Agent": "Continue-CLI/1.0 (https://continue.dev)", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.5", + "Accept-Encoding": "gzip, deflate", + "Cache-Control": "no-cache", + }, + }); + + clearTimeout(timeoutId); + + if (!response.ok) { + return `Error: HTTP ${response.status} - ${response.statusText}`; + } + + const contentType = response.headers.get("content-type") || ""; + + // Handle different content types + if (contentType.includes("application/json")) { + const json = await response.json(); + return "```json\n" + JSON.stringify(json, null, 2) + "\n```"; + } + + if (contentType.includes("text/plain")) { + const text = await response.text(); + return text; + } + + if (!contentType.includes("text/html")) { + return `Error: Unsupported content type: ${contentType}`; + } + + // Process HTML content + const html = await response.text(); + + // Convert HTML to markdown + const markdown = turndownService.turndown(html); + + // Clean up the markdown + const cleanedMarkdown = markdown + .replace(/\n\s*\n\s*\n/g, "\n\n") // Remove excessive newlines + .replace(/^\s+|\s+$/g, "") // Trim whitespace + .replace(/\\\[/g, "[") // Unescape brackets + .replace(/\\\]/g, "]"); + + return `# Content from ${url}\n\n${cleanedMarkdown}`; + + } catch (error) { + if (error instanceof TypeError && error.message.includes("fetch")) { + return `Error: Failed to fetch URL "${url}". Please check the URL and try again.`; + } + if (error instanceof Error && error.name === "AbortError") { + return `Error: Request timed out after ${timeout}ms`; + } + return `Error: ${error instanceof Error ? error.message : String(error)}`; + } + }, +}; \ No newline at end of file diff --git a/src/tools/index.ts b/src/tools/index.ts index b413826da..6b1b3fb1b 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -1,6 +1,7 @@ import { parseArgs } from "../args.js"; import { MCPService } from "../mcp.js"; import { exitTool } from "./exit.js"; +import { fetchTool } from "./fetch.js"; import { listFilesTool } from "./listFiles.js"; import { readFileTool } from "./readFile.js"; import { runTerminalCommandTool } from "./runTerminalCommand.js"; @@ -18,6 +19,7 @@ export const BUILTIN_TOOLS: Tool[] = [ viewDiffTool, searchCodeTool, runTerminalCommandTool, + fetchTool, // When in headless mode, there is a tool that the LLM can call to make the GitHub Action fail ...(parseArgs().isHeadless ? [exitTool] : []), ]; @@ -136,4 +138,4 @@ export async function executeToolCall(toolCall: { error instanceof Error ? error.message : String(error) }`; } -} +} \ No newline at end of file From f5f1bca7c6a9f2c4f2842605847d55b765fd3499 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 15 Jul 2025 00:47:00 -0700 Subject: [PATCH 3/3] fix: type err --- src/tools/fetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/fetch.ts b/src/tools/fetch.ts index 15b13bc24..84fd9de01 100644 --- a/src/tools/fetch.ts +++ b/src/tools/fetch.ts @@ -13,7 +13,7 @@ const turndownService = new TurndownService({ // Configure turndown for better markdown output turndownService.addRule("strikethrough", { - filter: ["del", "s", "strike"], + filter: ["del", "s"], replacement: function (content) { return "~~" + content + "~~"; },