Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-icons-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilocode/cli": patch
---

Prevent icon images fetched from the web from causing provider request errors.
4 changes: 2 additions & 2 deletions packages/opencode/src/tool/webfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HttpClient, HttpClientRequest } from "effect/unstable/http"
import * as Tool from "./tool"
import TurndownService from "turndown"
import DESCRIPTION from "./webfetch.txt"
import { isImageAttachment } from "@/util/media"
import { isIconMimeType, isImageAttachment } from "@/util/media" // kilocode_change
import { normalizeUrls } from "@/kilocode/util/url" // kilocode_change

const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
Expand Down Expand Up @@ -108,7 +108,7 @@ export const WebFetchTool = Tool.define(
const contentType = response.headers["content-type"] || ""
const mime = contentType.split(";")[0]?.trim().toLowerCase() || ""
const title = `${url} (${contentType})` // kilocode_change

if (isIconMimeType(mime)) throw new Error(`Unsupported image format: ${mime}`) // kilocode_change
if (isImageAttachment(mime)) {
const base64Content = Buffer.from(arrayBuffer).toString("base64")
return {
Expand Down
9 changes: 8 additions & 1 deletion packages/opencode/src/util/media.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const startsWith = (bytes: Uint8Array, prefix: number[]) => prefix.every((value, index) => bytes[index] === value)
const icons = new Set(["image/vnd.microsoft.icon", "image/x-icon", "image/x-ico", "image/ico", "image/icon"]) // kilocode_change

export function isPdfAttachment(mime: string) {
return mime === "application/pdf"
Expand All @@ -8,9 +9,15 @@ export function isMedia(mime: string) {
return mime.startsWith("image/") || isPdfAttachment(mime)
}

// kilocode_change start
export function isIconMimeType(mime: string) {
return icons.has(mime)
}

export function isImageAttachment(mime: string) {
return mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
return mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet" && !isIconMimeType(mime)
}
// kilocode_change end

export function sniffAttachmentMime(bytes: Uint8Array, fallback: string) {
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png"
Expand Down
52 changes: 52 additions & 0 deletions packages/opencode/test/tool/webfetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,58 @@ describe("tool.webfetch", () => {
)
})

// kilocode_change start
test.each(["image/x-icon", "image/vnd.microsoft.icon"])("rejects %s attachments", async (mime) => {
const bytes = new Uint8Array([0, 0, 1, 0])
await withFetch(
() => new Response(bytes, { status: 200, headers: { "content-type": mime } }),
async (url) => {
await WithInstance.provide({
directory: projectRoot,
fn: async () => {
await expect(exec({ url: new URL("/favicon.ico", url).toString(), format: "markdown" })).rejects.toThrow(
`Unsupported image format: ${mime}`,
)
},
})
},
)
})

test("returns non-icon image responses as file attachments", async () => {
const bytes = new Uint8Array([0, 0, 0, 0])
await withFetch(
() => new Response(bytes, { status: 200, headers: { "content-type": "image/avif" } }),
async (url) => {
await WithInstance.provide({
directory: projectRoot,
fn: async () => {
const result = await exec({ url: new URL("/image.avif", url).toString(), format: "markdown" })
expect(result.output).toBe("Image fetched successfully")
expect(result.attachments?.[0].mime).toBe("image/avif")
},
})
},
)
})

test("keeps fastbidsheet responses as text output", async () => {
await withFetch(
() => new Response("sheet", { status: 200, headers: { "content-type": "image/vnd.fastbidsheet" } }),
async (url) => {
await WithInstance.provide({
directory: projectRoot,
fn: async () => {
const result = await exec({ url: new URL("/sheet", url).toString(), format: "text" })
expect(result.output).toBe("sheet")
expect(result.attachments).toBeUndefined()
},
})
},
)
})
// kilocode_change end

test("keeps text responses as text output", async () => {
await withFetch(
() =>
Expand Down
Loading