run formatter

This commit is contained in:
Dax Raad
2025-05-31 14:41:00 -04:00
parent 6df19f1828
commit 3b746162d2
52 changed files with 1376 additions and 1390 deletions

View File

@@ -1,11 +1,10 @@
import { z } from "zod";
import { Tool } from "./tool";
import { JSDOM } from "jsdom";
import TurndownService from "turndown";
import { z } from "zod"
import { Tool } from "./tool"
import TurndownService from "turndown"
const MAX_RESPONSE_SIZE = 5 * 1024 * 1024; // 5MB
const DEFAULT_TIMEOUT = 30 * 1000; // 30 seconds
const MAX_TIMEOUT = 120 * 1000; // 2 minutes
const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
const MAX_TIMEOUT = 120 * 1000 // 2 minutes
const DESCRIPTION = `Fetches content from a URL and returns it in the specified format.
@@ -35,7 +34,7 @@ TIPS:
- Use text format for plain text content or simple API responses
- Use markdown format for content that should be rendered with formatting
- Use html format when you need the raw HTML structure
- Set appropriate timeouts for potentially slow websites`;
- Set appropriate timeouts for potentially slow websites`
export const Fetch = Tool.define({
name: "opencode.fetch",
@@ -60,18 +59,18 @@ export const Fetch = Tool.define({
!params.url.startsWith("http://") &&
!params.url.startsWith("https://")
) {
throw new Error("URL must start with http:// or https://");
throw new Error("URL must start with http:// or https://")
}
const timeout = Math.min(
(params.timeout ?? DEFAULT_TIMEOUT / 1000) * 1000,
MAX_TIMEOUT,
);
)
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), timeout)
if (opts?.abortSignal) {
opts.abortSignal.addEventListener("abort", () => controller.abort());
opts.abortSignal.addEventListener("abort", () => controller.abort())
}
const response = await fetch(params.url, {
@@ -79,59 +78,59 @@ export const Fetch = Tool.define({
headers: {
"User-Agent": "opencode/1.0",
},
});
})
clearTimeout(timeoutId);
clearTimeout(timeoutId)
if (!response.ok) {
throw new Error(`Request failed with status code: ${response.status}`);
throw new Error(`Request failed with status code: ${response.status}`)
}
// Check content length
const contentLength = response.headers.get("content-length");
const contentLength = response.headers.get("content-length")
if (contentLength && parseInt(contentLength) > MAX_RESPONSE_SIZE) {
throw new Error("Response too large (exceeds 5MB limit)");
throw new Error("Response too large (exceeds 5MB limit)")
}
const arrayBuffer = await response.arrayBuffer();
const arrayBuffer = await response.arrayBuffer()
if (arrayBuffer.byteLength > MAX_RESPONSE_SIZE) {
throw new Error("Response too large (exceeds 5MB limit)");
throw new Error("Response too large (exceeds 5MB limit)")
}
const content = new TextDecoder().decode(arrayBuffer);
const contentType = response.headers.get("content-type") || "";
const content = new TextDecoder().decode(arrayBuffer)
const contentType = response.headers.get("content-type") || ""
switch (params.format) {
case "text":
if (contentType.includes("text/html")) {
const text = extractTextFromHTML(content);
return { output: text };
const text = extractTextFromHTML(content)
return { output: text }
}
return { output: content };
return { output: content }
case "markdown":
if (contentType.includes("text/html")) {
const markdown = convertHTMLToMarkdown(content);
return { output: markdown };
const markdown = convertHTMLToMarkdown(content)
return { output: markdown }
}
return { output: "```\n" + content + "\n```" };
return { output: "```\n" + content + "\n```" }
case "html":
return { output: content };
return { output: content }
default:
return { output: content };
return { output: content }
}
},
});
})
function extractTextFromHTML(html: string): string {
const dom = new JSDOM(html);
const text = dom.window.document.body?.textContent || "";
return text.replace(/\s+/g, " ").trim();
const doc = new DOMParser().parseFromString(html, "text/html")
const text = doc.body.textContent || doc.body.innerText || ""
return text.replace(/\s+/g, " ").trim()
}
function convertHTMLToMarkdown(html: string): string {
const turndownService = new TurndownService();
return turndownService.turndown(html);
const turndownService = new TurndownService()
return turndownService.turndown(html)
}