Elffuss BraidHub

kikocis/continue public

truncate output for fetchUrlContent, searchWeb, and viewDiff
Changeset30d31a10f5b10864bf2e357f1642aa35764f6eeafd8f3b0de4a05edac5c45c9f
AuthorDallin Romney <dallinromney@gmail.com> · 2025-07-03 15:03 -07:00
CommitterDallin Romney <dallinromney@gmail.com> · 2025-07-03 15:03 -07:00
Parentsd40a7b9638b1

Browse the files at this changeset

6 files changed

modified core/tools/implementations/fetchUrlContent.ts +29 −1
@@ -1,6 +1,34 @@
11 import { ToolImpl } from ".";
22 import { getUrlContextItems } from "../../context/providers/URLContextProvider";
33
4+const DEFAULT_FETCH_URL_CHAR_LIMIT = 20000;
5+
46 export const fetchUrlContentImpl: ToolImpl = async (args, extras) => {
5- return getUrlContextItems(args.url, extras.fetch);
7+ const contextItems = await getUrlContextItems(args.url, extras.fetch);
8+
9+ // Track truncated content
10+ const truncatedUrls: string[] = [];
11+
12+ // Check and truncate each context item
13+ const processedItems = contextItems.map((item) => {
14+ if (item.content.length > DEFAULT_FETCH_URL_CHAR_LIMIT) {
15+ truncatedUrls.push(args.url);
16+ return {
17+ ...item,
18+ content: item.content.substring(0, DEFAULT_FETCH_URL_CHAR_LIMIT),
19+ };
20+ }
21+ return item;
22+ });
23+
24+ // Add truncation warning if needed
25+ if (truncatedUrls.length > 0) {
26+ processedItems.push({
27+ name: "URL content truncation warning",
28+ description: "Informs that URL content was truncated",
29+ content: `The content from ${truncatedUrls.join(", ")} was truncated because it exceeded the ${DEFAULT_FETCH_URL_CHAR_LIMIT} character limit. If you need more content, consider fetching specific sections or using a more targeted approach.`,
30+ });
31+ }
32+
33+ return processedItems;
634 };
added core/tools/implementations/fetchUrlContent.vitest.ts +91 −0
@@ -0,0 +1,91 @@
1+import { expect, test, vi } from "vitest";
2+import { fetchUrlContentImpl } from "./fetchUrlContent";
3+
4+vi.mock("../../context/providers/URLContextProvider", () => ({
5+ getUrlContextItems: vi.fn(),
6+}));
7+
8+import { ToolExtras } from "../..";
9+import { getUrlContextItems } from "../../context/providers/URLContextProvider";
10+const mockExtras = {
11+ fetch: vi.fn() as any,
12+ ide: {} as any,
13+} as unknown as ToolExtras;
14+
15+test("fetchUrlContent should not truncate content under character limit", async () => {
16+ const shortContent = "This is a short content";
17+ const mockContextItems = [
18+ {
19+ name: "Test URL",
20+ description: "Test URL description",
21+ content: shortContent,
22+ },
23+ ];
24+
25+ (getUrlContextItems as any).mockResolvedValue(mockContextItems);
26+
27+ const result = await fetchUrlContentImpl(
28+ { url: "https://example.com" },
29+ mockExtras,
30+ );
31+
32+ expect(result).toHaveLength(1);
33+ expect(result[0].content).toBe(shortContent);
34+});
35+
36+test("fetchUrlContent should truncate content exceeding character limit", async () => {
37+ const DEFAULT_FETCH_URL_CHAR_LIMIT = 20000;
38+ // Create a string exceeding the character limit
39+ const longContent = "a".repeat(DEFAULT_FETCH_URL_CHAR_LIMIT + 1000);
40+ const mockContextItems = [
41+ {
42+ name: "Test URL",
43+ description: "Test URL description",
44+ content: longContent,
45+ },
46+ ];
47+
48+ (getUrlContextItems as any).mockResolvedValue(mockContextItems);
49+
50+ const result = await fetchUrlContentImpl(
51+ { url: "https://example.com" },
52+ mockExtras,
53+ );
54+
55+ expect(result).toHaveLength(2); // Original item + warning
56+ expect(result[0].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT);
57+ expect(result[1].name).toBe("URL content truncation warning");
58+ expect(result[1].content).toContain("was truncated");
59+ expect(result[1].content).toContain("20000 character limit");
60+});
61+
62+test("fetchUrlContent should add truncation warning with multiple truncated items", async () => {
63+ const DEFAULT_FETCH_URL_CHAR_LIMIT = 20000;
64+ // Create strings exceeding the character limit
65+ const longContent1 = "a".repeat(DEFAULT_FETCH_URL_CHAR_LIMIT + 500);
66+ const longContent2 = "b".repeat(DEFAULT_FETCH_URL_CHAR_LIMIT + 1000);
67+ const mockContextItems = [
68+ {
69+ name: "Test URL 1",
70+ description: "Test URL description 1",
71+ content: longContent1,
72+ },
73+ {
74+ name: "Test URL 2",
75+ description: "Test URL description 2",
76+ content: longContent2,
77+ },
78+ ];
79+
80+ (getUrlContextItems as any).mockResolvedValue(mockContextItems);
81+
82+ const result = await fetchUrlContentImpl(
83+ { url: "https://example.com" },
84+ mockExtras,
85+ );
86+
87+ expect(result).toHaveLength(3); // Two original items + warning
88+ expect(result[0].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT);
89+ expect(result[1].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT);
90+ expect(result[2].name).toBe("URL content truncation warning");
91+});
modified core/tools/implementations/searchWeb.ts +30 −2
@@ -1,8 +1,36 @@
1+import { ToolImpl } from ".";
12 import { fetchSearchResults } from "../../context/providers/WebContextProvider";
23
3-import { ToolImpl } from ".";
4+const DEFAULT_WEB_SEARCH_CHAR_LIMIT = 8000;
45
56 export const searchWebImpl: ToolImpl = async (args, extras) => {
67 const webResults = await fetchSearchResults(args.query, 5, extras.fetch);
7- return webResults;
8+
9+ // Track truncated results
10+ const truncatedResults: string[] = [];
11+
12+ // Check and truncate each result
13+ const processedResults = webResults.map((result, index) => {
14+ if (result.content.length > DEFAULT_WEB_SEARCH_CHAR_LIMIT) {
15+ truncatedResults.push(
16+ result.name || result.description || `Result #${index + 1}`,
17+ );
18+ return {
19+ ...result,
20+ content: result.content.substring(0, DEFAULT_WEB_SEARCH_CHAR_LIMIT),
21+ };
22+ }
23+ return result;
24+ });
25+
26+ // Add truncation warning if needed
27+ if (truncatedResults.length > 0) {
28+ processedResults.push({
29+ name: "Web search truncation warning",
30+ description: "Informs that search results were truncated",
31+ content: `The content from the following search results was truncated because it exceeded the ${DEFAULT_WEB_SEARCH_CHAR_LIMIT} character limit: ${truncatedResults.join(", ")}. For more detailed information, consider refining your search query.`,
32+ });
33+ }
34+
35+ return processedResults;
836 };
added core/tools/implementations/searchWeb.vitest.ts +107 −0
@@ -0,0 +1,107 @@
1+import { expect, test, vi } from "vitest";
2+import { searchWebImpl } from "./searchWeb";
3+
4+// Mock the fetchSearchResults function
5+vi.mock("../../context/providers/WebContextProvider", () => ({
6+ fetchSearchResults: vi.fn(),
7+}));
8+const mockExtras = {
9+ fetch: vi.fn() as any,
10+ ide: {} as any,
11+} as unknown as ToolExtras;
12+
13+import { ToolExtras } from "../..";
14+import { fetchSearchResults } from "../../context/providers/WebContextProvider";
15+
16+test("searchWeb should not truncate results under character limit", async () => {
17+ const shortContent = "This is a short search result content";
18+ const mockSearchResults = [
19+ {
20+ name: "Result 1",
21+ description: "Search result description 1",
22+ content: shortContent,
23+ },
24+ {
25+ name: "Result 2",
26+ description: "Search result description 2",
27+ content: shortContent,
28+ },
29+ ];
30+
31+ (fetchSearchResults as any).mockResolvedValue(mockSearchResults);
32+
33+ const result = await searchWebImpl({ query: "test query" }, mockExtras);
34+
35+ expect(result).toHaveLength(2);
36+ expect(result[0].content).toBe(shortContent);
37+ expect(result[1].content).toBe(shortContent);
38+});
39+
40+test("searchWeb should truncate results exceeding character limit", async () => {
41+ const DEFAULT_WEB_SEARCH_CHAR_LIMIT = 8000;
42+ // Create a string exceeding the character limit
43+ const longContent = "a".repeat(DEFAULT_WEB_SEARCH_CHAR_LIMIT + 1000);
44+ const shortContent = "This is a short search result content";
45+ const mockSearchResults = [
46+ {
47+ name: "Result 1",
48+ description: "Search result description 1",
49+ content: longContent,
50+ },
51+ {
52+ name: "Result 2",
53+ description: "Search result description 2",
54+ content: shortContent,
55+ },
56+ ];
57+
58+ (fetchSearchResults as any).mockResolvedValue(mockSearchResults);
59+
60+ const result = await searchWebImpl({ query: "test query" }, mockExtras);
61+
62+ expect(result).toHaveLength(3); // Two results + warning
63+ expect(result[0].content.length).toBe(DEFAULT_WEB_SEARCH_CHAR_LIMIT);
64+ expect(result[1].content).toBe(shortContent);
65+ expect(result[2].name).toBe("Web search truncation warning");
66+ expect(result[2].content).toContain("truncated");
67+ expect(result[2].content).toContain("Result 1");
68+ expect(result[2].content).not.toContain("Result 2");
69+});
70+
71+test("searchWeb should include all truncated results in the warning", async () => {
72+ const DEFAULT_WEB_SEARCH_CHAR_LIMIT = 8000;
73+ // Create strings exceeding the character limit
74+ const longContent1 = "a".repeat(DEFAULT_WEB_SEARCH_CHAR_LIMIT + 500);
75+ const longContent2 = "b".repeat(DEFAULT_WEB_SEARCH_CHAR_LIMIT + 1000);
76+ const shortContent = "This is a short search result content";
77+ const mockSearchResults = [
78+ {
79+ name: "Result 1",
80+ description: "Search result description 1",
81+ content: longContent1,
82+ },
83+ {
84+ name: "Result 2",
85+ description: "Search result description 2",
86+ content: longContent2,
87+ },
88+ {
89+ name: "Result 3",
90+ description: "Search result description 3",
91+ content: shortContent,
92+ },
93+ ];
94+
95+ (fetchSearchResults as any).mockResolvedValue(mockSearchResults);
96+
97+ const result = await searchWebImpl({ query: "test query" }, mockExtras);
98+
99+ expect(result).toHaveLength(4); // Three results + warning
100+ expect(result[0].content.length).toBe(DEFAULT_WEB_SEARCH_CHAR_LIMIT);
101+ expect(result[1].content.length).toBe(DEFAULT_WEB_SEARCH_CHAR_LIMIT);
102+ expect(result[2].content).toBe(shortContent);
103+ expect(result[3].name).toBe("Web search truncation warning");
104+ expect(result[3].content).toContain("Result 1");
105+ expect(result[3].content).toContain("Result 2");
106+ expect(result[3].content).not.toContain("Result 3");
107+});
modified core/tools/implementations/viewDiff.ts +26 −2
@@ -1,15 +1,39 @@
11 import { ToolImpl } from ".";
2+import { ContextItem } from "../..";
23 import { getDiffsFromCache } from "../../autocomplete/snippets/gitDiffCache";
34
5+const DEFAULT_GIT_DIFF_LINE_LIMIT = 5000;
6+
47 export const viewDiffImpl: ToolImpl = async (args, extras) => {
58 const diffs = await getDiffsFromCache(extras.ide); // const diffs = await extras.ide.getDiff(true);
69 // TODO includeUnstaged should be an option
710
8- return [
11+ const combinedDiff = diffs.join("\n");
12+ const diffLines = combinedDiff.split("\n");
13+
14+ let truncated = false;
15+ let processedDiff = combinedDiff;
16+
17+ if (diffLines.length > DEFAULT_GIT_DIFF_LINE_LIMIT) {
18+ truncated = true;
19+ processedDiff = diffLines.slice(0, DEFAULT_GIT_DIFF_LINE_LIMIT).join("\n");
20+ }
21+
22+ const contextItems: ContextItem[] = [
923 {
1024 name: "Diff",
1125 description: "The current git diff",
12- content: diffs.join("\n"),
26+ content: processedDiff,
1327 },
1428 ];
29+
30+ if (truncated) {
31+ contextItems.push({
32+ name: "Diff truncation warning",
33+ description: "Informs that git diff was truncated",
34+ content: `The git diff was truncated because it exceeded ${DEFAULT_GIT_DIFF_LINE_LIMIT} lines. Consider viewing specific files or focusing on smaller changes.`,
35+ });
36+ }
37+
38+ return contextItems;
1539 };
added core/tools/implementations/viewDiff.vitest.ts +89 −0
@@ -0,0 +1,89 @@
1+import { expect, test, vi } from "vitest";
2+import { viewDiffImpl } from "./viewDiff";
3+
4+// Mock the getDiffsFromCache function
5+vi.mock("../../autocomplete/snippets/gitDiffCache", () => ({
6+ getDiffsFromCache: vi.fn(),
7+}));
8+
9+import { ToolExtras } from "../..";
10+import { getDiffsFromCache } from "../../autocomplete/snippets/gitDiffCache";
11+const mockExtras = {
12+ fetch: vi.fn() as any,
13+ ide: {} as any,
14+} as unknown as ToolExtras;
15+
16+test("viewDiff should not truncate diffs under line limit", async () => {
17+ // Create a small diff with just a few lines
18+ const smallDiff = [
19+ "diff --git a/file1.txt b/file1.txt",
20+ "index 1234..5678 100644",
21+ "--- a/file1.txt",
22+ "+++ b/file1.txt",
23+ "@@ -1,3 +1,4 @@",
24+ " line1",
25+ "-line2",
26+ "+line2 modified",
27+ "+line3 added",
28+ ];
29+
30+ (getDiffsFromCache as any).mockResolvedValue(smallDiff);
31+
32+ const result = await viewDiffImpl({}, mockExtras);
33+
34+ expect(result).toHaveLength(1); // Just the diff, no warning
35+ expect(result[0].name).toBe("Diff");
36+ expect(result[0].content).toBe(smallDiff.join("\n"));
37+});
38+
39+test("viewDiff should truncate diffs exceeding line limit", async () => {
40+ const DEFAULT_GIT_DIFF_LINE_LIMIT = 5000;
41+
42+ // Create a large diff that exceeds the line limit
43+ const largeDiffLines = Array(DEFAULT_GIT_DIFF_LINE_LIMIT + 1000).fill(
44+ "line content",
45+ );
46+ const largeDiff = [
47+ "diff --git a/file1.txt b/file1.txt",
48+ "index 1234..5678 100644",
49+ ...largeDiffLines,
50+ ];
51+
52+ (getDiffsFromCache as any).mockResolvedValue([largeDiff.join("\n")]);
53+
54+ const result = await viewDiffImpl({}, mockExtras);
55+
56+ expect(result).toHaveLength(2); // Diff + warning
57+
58+ // Count the number of lines in the result
59+ const resultLines = result[0].content.split("\n");
60+ expect(resultLines.length).toBe(DEFAULT_GIT_DIFF_LINE_LIMIT);
61+
62+ // Check the warning
63+ expect(result[1].name).toBe("Diff truncation warning");
64+ expect(result[1].content).toContain("truncated");
65+ expect(result[1].content).toContain(`${DEFAULT_GIT_DIFF_LINE_LIMIT} lines`);
66+});
67+
68+test("viewDiff should handle multiple diffs correctly", async () => {
69+ const DEFAULT_GIT_DIFF_LINE_LIMIT = 5000;
70+
71+ // Create multiple diffs that together exceed the line limit
72+ const diff1Lines = Array(3000).fill("diff1 line");
73+ const diff2Lines = Array(3000).fill("diff2 line");
74+ const diff1 = diff1Lines.join("\n");
75+ const diff2 = diff2Lines.join("\n");
76+
77+ (getDiffsFromCache as any).mockResolvedValue([diff1, diff2]);
78+
79+ const result = await viewDiffImpl({}, mockExtras);
80+
81+ expect(result).toHaveLength(2); // Diff + warning
82+
83+ // Count the number of lines in the result
84+ const resultLines = result[0].content.split("\n");
85+ expect(resultLines.length).toBe(DEFAULT_GIT_DIFF_LINE_LIMIT);
86+
87+ // Check the warning
88+ expect(result[1].name).toBe("Diff truncation warning");
89+});