Merge pull request #27 from continuedev/fetch

fetch
This commit is contained in:
Nate Sesti 2025-07-15 00:53:43 -07:00 • committed by GitHub
commit 2d2eda9f10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 165 additions and 24 deletions

23
package-lock.json generated
View file

@ -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",

View file

@ -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"
},

View file

@ -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<Assistant> {
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) {

121
src/tools/fetch.ts Normal file
View file

@ -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"],
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<string> => {
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)}`;
}
},
};

View file

@ -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)
}`;
}
}
}