Skip to content

Commit ba54cee

Browse files
authored
feat(tool): return image attachments from webfetch (anomalyco#13331)
1 parent 847e06f commit ba54cee

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

packages/opencode/src/tool/webfetch.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Tool } from "./tool"
33
import TurndownService from "turndown"
44
import DESCRIPTION from "./webfetch.txt"
55
import { abortAfterAny } from "../util/abort"
6+
import { Identifier } from "../id/id"
67

78
const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
89
const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
@@ -87,11 +88,34 @@ export const WebFetchTool = Tool.define("webfetch", {
8788
throw new Error("Response too large (exceeds 5MB limit)")
8889
}
8990

90-
const content = new TextDecoder().decode(arrayBuffer)
9191
const contentType = response.headers.get("content-type") || ""
92-
92+
const mime = contentType.split(";")[0]?.trim().toLowerCase() || ""
9393
const title = `${params.url} (${contentType})`
9494

95+
// Check if response is an image
96+
const isImage = mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
97+
98+
if (isImage) {
99+
const base64Content = Buffer.from(arrayBuffer).toString("base64")
100+
return {
101+
title,
102+
output: "Image fetched successfully",
103+
metadata: {},
104+
attachments: [
105+
{
106+
id: Identifier.ascending("part"),
107+
sessionID: ctx.sessionID,
108+
messageID: ctx.messageID,
109+
type: "file",
110+
mime,
111+
url: `data:${mime};base64,${base64Content}`,
112+
},
113+
],
114+
}
115+
}
116+
117+
const content = new TextDecoder().decode(arrayBuffer)
118+
95119
// Handle content based on requested format and actual content type
96120
switch (params.format) {
97121
case "markdown":
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "path"
3+
import { Instance } from "../../src/project/instance"
4+
import { WebFetchTool } from "../../src/tool/webfetch"
5+
6+
const projectRoot = path.join(import.meta.dir, "../..")
7+
8+
const ctx = {
9+
sessionID: "test",
10+
messageID: "message",
11+
callID: "",
12+
agent: "build",
13+
abort: AbortSignal.any([]),
14+
messages: [],
15+
metadata: () => {},
16+
ask: async () => {},
17+
}
18+
19+
async function withFetch(
20+
mockFetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>,
21+
fn: () => Promise<void>,
22+
) {
23+
const originalFetch = globalThis.fetch
24+
globalThis.fetch = mockFetch as unknown as typeof fetch
25+
try {
26+
await fn()
27+
} finally {
28+
globalThis.fetch = originalFetch
29+
}
30+
}
31+
32+
describe("tool.webfetch", () => {
33+
test("returns image responses as file attachments", async () => {
34+
const bytes = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10])
35+
await withFetch(
36+
async () => new Response(bytes, { status: 200, headers: { "content-type": "IMAGE/PNG; charset=binary" } }),
37+
async () => {
38+
await Instance.provide({
39+
directory: projectRoot,
40+
fn: async () => {
41+
const webfetch = await WebFetchTool.init()
42+
const result = await webfetch.execute({ url: "https://example.com/image.png", format: "markdown" }, ctx)
43+
expect(result.output).toBe("Image fetched successfully")
44+
expect(result.attachments).toBeDefined()
45+
expect(result.attachments?.length).toBe(1)
46+
expect(result.attachments?.[0].type).toBe("file")
47+
expect(result.attachments?.[0].mime).toBe("image/png")
48+
expect(result.attachments?.[0].url.startsWith("data:image/png;base64,")).toBe(true)
49+
},
50+
})
51+
},
52+
)
53+
})
54+
55+
test("keeps svg as text output", async () => {
56+
const svg = '<svg xmlns="http://www.w3.org/2000/svg"><text>hello</text></svg>'
57+
await withFetch(
58+
async () =>
59+
new Response(svg, {
60+
status: 200,
61+
headers: { "content-type": "image/svg+xml; charset=UTF-8" },
62+
}),
63+
async () => {
64+
await Instance.provide({
65+
directory: projectRoot,
66+
fn: async () => {
67+
const webfetch = await WebFetchTool.init()
68+
const result = await webfetch.execute({ url: "https://example.com/image.svg", format: "html" }, ctx)
69+
expect(result.output).toContain("<svg")
70+
expect(result.attachments).toBeUndefined()
71+
},
72+
})
73+
},
74+
)
75+
})
76+
77+
test("keeps text responses as text output", async () => {
78+
await withFetch(
79+
async () =>
80+
new Response("hello from webfetch", {
81+
status: 200,
82+
headers: { "content-type": "text/plain; charset=utf-8" },
83+
}),
84+
async () => {
85+
await Instance.provide({
86+
directory: projectRoot,
87+
fn: async () => {
88+
const webfetch = await WebFetchTool.init()
89+
const result = await webfetch.execute({ url: "https://example.com/file.txt", format: "text" }, ctx)
90+
expect(result.output).toBe("hello from webfetch")
91+
expect(result.attachments).toBeUndefined()
92+
},
93+
})
94+
},
95+
)
96+
})
97+
})

0 commit comments

Comments
 (0)