tool-webfetch.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { describe, expect, test } from "bun:test"
  2. import { Duration, Effect, Fiber, Layer, Schema } from "effect"
  3. import * as TestClock from "effect/testing/TestClock"
  4. import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
  5. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  7. import { LayerNodePlatform } from "@opencode-ai/util/effect/app-node-platform"
  8. import { PermissionV2 } from "@opencode-ai/core/permission"
  9. import { SessionV2 } from "@opencode-ai/core/session"
  10. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  11. import { WebFetchTool } from "@opencode-ai/core/tool/webfetch"
  12. import { ToolOutputStore } from "@opencode-ai/core/tool-output-store"
  13. import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
  14. import { Image } from "@opencode-ai/core/image"
  15. import { testEffect } from "./lib/effect"
  16. import { imagePassthrough } from "./lib/image"
  17. import { toolIdentity, executeTool, registerToolPlugin, settleTool, toolDefinitions } from "./lib/tool"
  18. const webFetchToolNode = makeLocationNode({
  19. name: "test/webfetch-tool-plugin",
  20. layer: Layer.effectDiscard(registerToolPlugin(WebFetchTool.Plugin)),
  21. deps: [ToolRegistry.toolsNode, PermissionV2.node, LayerNodePlatform.httpClient],
  22. })
  23. const sessionID = SessionV2.ID.make("ses_webfetch_test")
  24. const requests: Array<{ readonly url: string; readonly headers: Record<string, string> }> = []
  25. const assertions: PermissionV2.AssertInput[] = []
  26. let respond = (_request: HttpClientRequest.HttpClientRequest) =>
  27. Effect.succeed(new Response("hello", { headers: { "content-type": "text/plain" } }))
  28. const http = Layer.succeed(
  29. HttpClient.HttpClient,
  30. HttpClient.make((request) =>
  31. Effect.sync(() => requests.push({ url: request.url, headers: request.headers })).pipe(
  32. Effect.andThen(respond(request)),
  33. Effect.map((response) => HttpClientResponse.fromWeb(request, response)),
  34. ),
  35. ),
  36. )
  37. const permission = Layer.succeed(
  38. PermissionV2.Service,
  39. PermissionV2.Service.of({
  40. assert: (input) => Effect.sync(() => assertions.push(input)),
  41. ask: () => Effect.die("unused"),
  42. reply: () => Effect.die("unused"),
  43. get: () => Effect.die("unused"),
  44. forSession: () => Effect.die("unused"),
  45. list: () => Effect.die("unused"),
  46. }),
  47. )
  48. const toolLayer = (replacements: LayerNode.Replacements = []) =>
  49. AppNodeBuilder.build(LayerNode.group([ToolRegistry.node, ToolRegistry.toolsNode, webFetchToolNode]), [
  50. [PermissionV2.node, permission],
  51. [ToolOutputStore.node, ToolOutputStore.nodeWithoutConfig],
  52. [Image.node, imagePassthrough],
  53. ...replacements,
  54. ])
  55. const it = testEffect(toolLayer([[LayerNodePlatform.httpClient, http]]))
  56. const live = testEffect(toolLayer())
  57. const reset = () => {
  58. requests.length = 0
  59. assertions.length = 0
  60. respond = () => Effect.succeed(new Response("hello", { headers: { "content-type": "text/plain" } }))
  61. }
  62. const call = (input: typeof WebFetchTool.Input.Type, id = "call-webfetch") => ({
  63. sessionID,
  64. ...toolIdentity,
  65. call: { type: "tool-call" as const, id, name: "webfetch", input },
  66. })
  67. describe("WebFetchTool helpers", () => {
  68. test("defaults format and rejects invalid timeout controls", () => {
  69. const decode = Schema.decodeUnknownSync(WebFetchTool.Input)
  70. expect(decode({ url: "https://example.com" })).toEqual({ url: "https://example.com", format: "markdown" })
  71. expect(() => decode({ url: "https://example.com", timeout: 0 })).toThrow()
  72. expect(() => decode({ url: "https://example.com", timeout: WebFetchTool.MAX_TIMEOUT_SECONDS + 1 })).toThrow()
  73. })
  74. test("ports HTML text and markdown conversions without active content", () => {
  75. const html = "<h1>Hello</h1><script>bad()</script><p>world <strong>wide</strong></p><style>.bad {}</style>"
  76. expect(WebFetchTool.extractTextFromHTML(html)).toBe("Helloworld wide")
  77. expect(WebFetchTool.convertHTMLToMarkdown(html)).toBe("# Hello\n\nworld **wide**")
  78. })
  79. })
  80. describe("WebFetchTool registration", () => {
  81. it.effect("registers and fetches an ordinary hostname HTTP URL without rewriting it", () =>
  82. Effect.gen(function* () {
  83. reset()
  84. const registry = yield* ToolRegistry.Service
  85. const url = "http://example.com/public"
  86. expect((yield* toolDefinitions(registry)).map((tool) => tool.name)).toEqual(["webfetch"])
  87. expect(yield* settleTool(registry, call({ url, format: "text", timeout: 4 }))).toEqual({
  88. result: { type: "text", value: "hello" },
  89. output: {
  90. structured: { url, contentType: "text/plain", format: "text", output: "hello" },
  91. content: [{ type: "text", text: "hello" }],
  92. },
  93. })
  94. expect(assertions).toMatchObject([
  95. { sessionID, action: "webfetch", resources: [url], save: ["*"], metadata: { url, format: "text", timeout: 4 } },
  96. ])
  97. expect(requests).toMatchObject([{ url, headers: { accept: expect.stringContaining("text/plain;q=1.0") } }])
  98. }),
  99. )
  100. it.effect("accepts localhost URLs with the same requested-URL permission check", () =>
  101. Effect.gen(function* () {
  102. reset()
  103. const registry = yield* ToolRegistry.Service
  104. const url = "http://localhost/private"
  105. expect(yield* executeTool(registry, call({ url, format: "text" }))).toEqual({
  106. type: "text",
  107. value: "hello",
  108. })
  109. expect(assertions).toMatchObject([
  110. { sessionID, action: "webfetch", resources: [url], save: ["*"], metadata: { url, format: "text" } },
  111. ])
  112. expect(requests.map((request) => request.url)).toEqual([url])
  113. }),
  114. )
  115. live.effect("follows redirects while approving only the requested URL", () =>
  116. Effect.acquireUseRelease(
  117. Effect.sync(() =>
  118. Bun.serve({
  119. port: 0,
  120. fetch: (request) =>
  121. new URL(request.url).pathname === "/redirect"
  122. ? new Response("", { status: 302, headers: { location: "/target" } })
  123. : new Response("redirected", { headers: { "content-type": "text/plain" } }),
  124. }),
  125. ),
  126. (server) =>
  127. Effect.gen(function* () {
  128. reset()
  129. const registry = yield* ToolRegistry.Service
  130. const url = new URL("/redirect", server.url).toString()
  131. expect(yield* executeTool(registry, call({ url, format: "text" }))).toEqual({
  132. type: "text",
  133. value: "redirected",
  134. })
  135. expect(assertions).toMatchObject([
  136. { sessionID, action: "webfetch", resources: [url], save: ["*"], metadata: { url, format: "text" } },
  137. ])
  138. }),
  139. (server) => Effect.promise(() => server.stop(true)),
  140. ),
  141. )
  142. it.effect("rejects non-HTTP schemes before permission or transport", () =>
  143. Effect.gen(function* () {
  144. reset()
  145. const registry = yield* ToolRegistry.Service
  146. expect(yield* executeTool(registry, call({ url: "file:///etc/passwd", format: "text" }))).toEqual({
  147. type: "error",
  148. value: "Unable to fetch file:///etc/passwd",
  149. })
  150. expect(assertions).toEqual([])
  151. expect(requests).toEqual([])
  152. }),
  153. )
  154. it.effect("converts HTML to requested markdown and text", () =>
  155. Effect.gen(function* () {
  156. reset()
  157. respond = () =>
  158. Effect.succeed(
  159. new Response("<h1>Hello</h1><p>world</p><script>bad()</script>", {
  160. headers: { "content-type": "text/html; charset=utf-8" },
  161. }),
  162. )
  163. const registry = yield* ToolRegistry.Service
  164. expect(yield* executeTool(registry, call({ url: "https://1.1.1.1", format: "markdown" }))).toEqual({
  165. type: "text",
  166. value: "# Hello\n\nworld",
  167. })
  168. expect(yield* executeTool(registry, call({ url: "https://1.1.1.1", format: "text" }))).toEqual({
  169. type: "text",
  170. value: "Helloworld",
  171. })
  172. }),
  173. )
  174. it.effect("returns an error result when HTML-to-Markdown conversion throws", () =>
  175. Effect.gen(function* () {
  176. reset()
  177. respond = () =>
  178. Effect.succeed(
  179. new Response("<div>".repeat(10_000) + "content" + "</div>".repeat(10_000), {
  180. headers: { "content-type": "text/html" },
  181. }),
  182. )
  183. const registry = yield* ToolRegistry.Service
  184. const url = "https://1.1.1.1/deep-html"
  185. expect(yield* executeTool(registry, call({ url, format: "markdown" }))).toEqual({
  186. type: "error",
  187. value: `Unable to fetch ${url}`,
  188. })
  189. }),
  190. )
  191. it.effect("rejects declared and streamed oversized bodies", () =>
  192. Effect.gen(function* () {
  193. reset()
  194. const registry = yield* ToolRegistry.Service
  195. respond = () =>
  196. Effect.succeed(
  197. new Response("small", {
  198. headers: { "content-type": "text/plain", "content-length": String(WebFetchTool.MAX_RESPONSE_BYTES + 1) },
  199. }),
  200. )
  201. expect(yield* executeTool(registry, call({ url: "https://1.1.1.1/declared", format: "text" }))).toEqual({
  202. type: "error",
  203. value: "Unable to fetch https://1.1.1.1/declared",
  204. })
  205. respond = () =>
  206. Effect.succeed(
  207. new Response("x".repeat(WebFetchTool.MAX_RESPONSE_BYTES + 1), { headers: { "content-type": "text/plain" } }),
  208. )
  209. expect(yield* executeTool(registry, call({ url: "https://1.1.1.1/streamed", format: "text" }))).toEqual({
  210. type: "error",
  211. value: "Unable to fetch https://1.1.1.1/streamed",
  212. })
  213. }),
  214. )
  215. it.effect("keeps images and files unsupported until typed settlement can carry attachments", () =>
  216. Effect.gen(function* () {
  217. reset()
  218. const registry = yield* ToolRegistry.Service
  219. respond = () => Effect.succeed(new Response("png", { headers: { "content-type": "image/png" } }))
  220. expect(yield* executeTool(registry, call({ url: "https://1.1.1.1/image", format: "html" }))).toEqual({
  221. type: "error",
  222. value: "Unable to fetch https://1.1.1.1/image",
  223. })
  224. respond = () => Effect.succeed(new Response("pdf", { headers: { "content-type": "application/pdf" } }))
  225. expect(yield* executeTool(registry, call({ url: "https://1.1.1.1/file", format: "html" }))).toEqual({
  226. type: "error",
  227. value: "Unable to fetch https://1.1.1.1/file",
  228. })
  229. }),
  230. )
  231. it.effect("retries Cloudflare challenges with an honest user agent", () =>
  232. Effect.gen(function* () {
  233. reset()
  234. let count = 0
  235. respond = () =>
  236. Effect.succeed(
  237. ++count === 1
  238. ? new Response("challenge", { status: 403, headers: { "cf-mitigated": "challenge" } })
  239. : new Response("ok", { headers: { "content-type": "text/plain" } }),
  240. )
  241. const registry = yield* ToolRegistry.Service
  242. expect(yield* executeTool(registry, call({ url: "https://1.1.1.1", format: "text" }))).toEqual({
  243. type: "text",
  244. value: "ok",
  245. })
  246. expect(requests).toHaveLength(2)
  247. expect(requests[0]?.headers["user-agent"]).toContain("Mozilla/5.0")
  248. expect(requests[1]?.headers["user-agent"]).toBe("opencode")
  249. }),
  250. )
  251. it.effect("times out stalled requests", () =>
  252. Effect.gen(function* () {
  253. reset()
  254. respond = () => Effect.never
  255. const registry = yield* ToolRegistry.Service
  256. const fiber = yield* executeTool(
  257. registry,
  258. call({ url: "https://1.1.1.1/slow", format: "text", timeout: 1 }),
  259. ).pipe(Effect.forkChild)
  260. yield* TestClock.adjust(Duration.seconds(1))
  261. expect(yield* Fiber.join(fiber)).toEqual({ type: "error", value: "Unable to fetch https://1.1.1.1/slow" })
  262. }),
  263. )
  264. })