From 4d747796e034d7d22873f55dba0a81a991930962 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 16 Oct 2025 19:00:24 -0700 Subject: [PATCH 1/5] feat: return fetch errors to agent --- .../{fetchUrlContent.vitest.ts => fetchUrlContent.test.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core/tools/implementations/{fetchUrlContent.vitest.ts => fetchUrlContent.test.ts} (100%) diff --git a/core/tools/implementations/fetchUrlContent.vitest.ts b/core/tools/implementations/fetchUrlContent.test.ts similarity index 100% rename from core/tools/implementations/fetchUrlContent.vitest.ts rename to core/tools/implementations/fetchUrlContent.test.ts From 852dba83458c57ce5fef65fbd98a7b6ae8ca1e97 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 16 Oct 2025 19:03:13 -0700 Subject: [PATCH 2/5] Update URLContextProvider.ts --- core/context/providers/URLContextProvider.ts | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/core/context/providers/URLContextProvider.ts b/core/context/providers/URLContextProvider.ts index 3e025d08c..bfc2c29a1 100644 --- a/core/context/providers/URLContextProvider.ts +++ b/core/context/providers/URLContextProvider.ts @@ -37,6 +37,23 @@ export async function getUrlContextItems( const url = new URL(query); const icon = await fetchFavicon(url); const resp = await fetchFn(url); + + // Check if the response is not OK + if (!resp.ok) { + return [ + { + icon, + description: url.toString(), + content: `Failed to fetch URL: HTTP ${resp.status} ${resp.statusText}`, + name: `HTTP ${resp.status}`, + uri: { + type: "url", + value: url.toString(), + }, + }, + ]; + } + const html = await resp.text(); const dom = new JSDOM(html); @@ -66,6 +83,18 @@ export async function getUrlContextItems( ]; } catch (e) { console.log(e); - return []; + const errorMessage = e instanceof Error ? e.message : "Unknown error"; + + return [ + { + description: `Error fetching ${query}`, + content: `Failed to fetch URL: ${errorMessage}`, + name: "Fetch Error", + uri: { + type: "url", + value: query, + }, + }, + ]; } } From 96c15734747084c79ba4cea58b3babc1684d61d4 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 16 Oct 2025 21:12:32 -0700 Subject: [PATCH 3/5] bubble up error --- core/context/providers/URLContextProvider.ts | 96 +++++++------------- 1 file changed, 34 insertions(+), 62 deletions(-) diff --git a/core/context/providers/URLContextProvider.ts b/core/context/providers/URLContextProvider.ts index bfc2c29a1..5d846b575 100644 --- a/core/context/providers/URLContextProvider.ts +++ b/core/context/providers/URLContextProvider.ts @@ -33,68 +33,40 @@ export async function getUrlContextItems( query: string, fetchFn: FetchFunction, ): Promise { - try { - const url = new URL(query); - const icon = await fetchFavicon(url); - const resp = await fetchFn(url); + const url = new URL(query); + const icon = await fetchFavicon(url); + const resp = await fetchFn(url); - // Check if the response is not OK - if (!resp.ok) { - return [ - { - icon, - description: url.toString(), - content: `Failed to fetch URL: HTTP ${resp.status} ${resp.statusText}`, - name: `HTTP ${resp.status}`, - uri: { - type: "url", - value: url.toString(), - }, - }, - ]; - } - - const html = await resp.text(); - - const dom = new JSDOM(html); - let reader = new Readability(dom.window.document); - let article = reader.parse(); - const content = article?.content || ""; - const markdown = NodeHtmlMarkdown.translate( - content, - {}, - undefined, - undefined, - ); - - const title = article?.title || url.pathname; - - return [ - { - icon, - description: url.toString(), - content: markdown, - name: title, - uri: { - type: "url", - value: url.toString(), - }, - }, - ]; - } catch (e) { - console.log(e); - const errorMessage = e instanceof Error ? e.message : "Unknown error"; - - return [ - { - description: `Error fetching ${query}`, - content: `Failed to fetch URL: ${errorMessage}`, - name: "Fetch Error", - uri: { - type: "url", - value: query, - }, - }, - ]; + // Check if the response is not OK + if (!resp.ok) { + throw new Error(`HTTP ${resp.status} ${resp.statusText}`); } + + const html = await resp.text(); + + const dom = new JSDOM(html); + let reader = new Readability(dom.window.document); + let article = reader.parse(); + const content = article?.content || ""; + const markdown = NodeHtmlMarkdown.translate( + content, + {}, + undefined, + undefined, + ); + + const title = article?.title || url.pathname; + + return [ + { + icon, + description: url.toString(), + content: markdown, + name: title, + uri: { + type: "url", + value: url.toString(), + }, + }, + ]; } From 48cef4256d521db913bcfec600fb2a6a7eac568c Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Fri, 17 Oct 2025 10:00:17 -0700 Subject: [PATCH 4/5] add vitest for fetchUrlContent --- ...tchUrlContent.test.ts => fetchUrlContent.vitest.ts} | 10 ++++++++++ 1 file changed, 10 insertions(+) rename core/tools/implementations/{fetchUrlContent.test.ts => fetchUrlContent.vitest.ts} (89%) diff --git a/core/tools/implementations/fetchUrlContent.test.ts b/core/tools/implementations/fetchUrlContent.vitest.ts similarity index 89% rename from core/tools/implementations/fetchUrlContent.test.ts rename to core/tools/implementations/fetchUrlContent.vitest.ts index 0c8efb81b..3241bb0d2 100644 --- a/core/tools/implementations/fetchUrlContent.test.ts +++ b/core/tools/implementations/fetchUrlContent.vitest.ts @@ -89,3 +89,13 @@ test("fetchUrlContent should add truncation warning with multiple truncated item expect(result[1].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT); expect(result[2].name).toBe("Truncation warning"); }); + +test("fetchUrlContent should propagate errors when URL fetch fails", async () => { + const errorMessage = "HTTP 404 Not Found"; + + (getUrlContextItems as any).mockRejectedValue(new Error(errorMessage)); + + await expect( + fetchUrlContentImpl({ url: "https://example.com/404" }, mockExtras) + ).rejects.toThrow(errorMessage); +}); From c6780c095f82c479282cb4669449248d16a93470 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Fri, 17 Oct 2025 10:18:30 -0700 Subject: [PATCH 5/5] Update fetchUrlContent.vitest.ts --- core/tools/implementations/fetchUrlContent.vitest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/tools/implementations/fetchUrlContent.vitest.ts b/core/tools/implementations/fetchUrlContent.vitest.ts index 3241bb0d2..d0709bad1 100644 --- a/core/tools/implementations/fetchUrlContent.vitest.ts +++ b/core/tools/implementations/fetchUrlContent.vitest.ts @@ -96,6 +96,6 @@ test("fetchUrlContent should propagate errors when URL fetch fails", async () => (getUrlContextItems as any).mockRejectedValue(new Error(errorMessage)); await expect( - fetchUrlContentImpl({ url: "https://example.com/404" }, mockExtras) + fetchUrlContentImpl({ url: "https://example.com/404" }, mockExtras), ).rejects.toThrow(errorMessage); });