diff --git a/packages/core/src/codemode/tool.ts b/packages/core/src/codemode/tool.ts index 8514b9d50ac..c75a42f5c93 100644 --- a/packages/core/src/codemode/tool.ts +++ b/packages/core/src/codemode/tool.ts @@ -13,6 +13,7 @@ import type { import { Effect, Ref, Schema, Semaphore } from "effect" import { definition, normalizedName } from "../tool/runtime.js" import { CodeModeCatalog } from "./catalog.js" +import { CodeModeWeb } from "./web.js" const ExecuteFile = Schema.Struct({ data: Schema.String, @@ -59,8 +60,8 @@ export type Inventory = { // Invariant model-facing guidance; the changing tool catalog is delivered through Instructions. const description = [ - "Run JavaScript in a confined Code Mode runtime to orchestrate tool calls and compose their results.", - "Imports, direct filesystem access, and timers are unavailable. Do not use `fetch`; all external access goes through `tools`.", + "Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.", + "`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable; all other external access goes through `tools`.", "Within `{ code }`, the only callable tools are those explicitly listed in the Code Mode catalog instructions or returned by the `search` function. Inside `{ code }`, ignore tools shown outside the Code Mode catalog. They are not available in the Code Mode runtime.", 'Call tools through `tools` using only exact paths and signatures from the catalog. Do not infer or normalize tool names; preserve bracket notation such as `tools.["tool-name"](input)`.', "Prefer an explicit `return`; if omitted, the final top-level expression becomes the result.", @@ -160,7 +161,12 @@ function progressHooks(record: (update: (items: Array) => Array start(call, { tool: call.name, status: "running" }), + "extension.before": (call) => + start(call, { + tool: call.name, + status: "running", + ...(call.name === "fetch" ? { input: CodeModeWeb.display(call.args) } : {}), + }), "extension.after": settle, } satisfies CodeMode.Hooks } @@ -226,7 +232,7 @@ function runtime( }) } const tools = renderTools(root) - return CodeMode.make({ tools, hooks }) + return CodeMode.make({ tools, extensions: [CodeModeWeb.extension], hooks }) } function getNode(root: Node, path: string) { diff --git a/packages/core/src/codemode/web.ts b/packages/core/src/codemode/web.ts new file mode 100644 index 00000000000..cd70343c7e0 --- /dev/null +++ b/packages/core/src/codemode/web.ts @@ -0,0 +1,46 @@ +export * as CodeModeWeb from "./web.js" + +import { Extension } from "@opencode/codemode" + +const TIMEOUT_MS = 30_000 + +type Init = { + readonly method?: string + readonly headers?: Record | Array<[string, string]> + readonly body?: string | Uint8Array | URLSearchParams +} + +const fetch = async (input: string | URL, init: Init = {}) => { + const response = await globalThis.fetch(input, { + method: init.method, + headers: init.headers, + body: init.body, + signal: AbortSignal.timeout(TIMEOUT_MS), + }) + const bytes = await response.bytes() + const headers = Object.fromEntries(response.headers) + const text = () => new TextDecoder().decode(bytes) + return { + url: response.url, + status: response.status, + statusText: response.statusText, + ok: response.ok, + redirected: response.redirected, + headers: { + get: (name: string) => headers[name.toLowerCase()] ?? null, + has: (name: string) => name.toLowerCase() in headers, + entries: () => Object.entries(headers), + }, + text: async () => text(), + json: async () => JSON.parse(text()) as unknown, + bytes: async () => bytes, + } +} + +export const extension = Extension.make({ name: "web", globals: { fetch } }) + +/** What to show for a fetch call: its method and URL. */ +export const display = (args: ReadonlyArray) => { + const [input, init] = args as [string | URL, Init | undefined] + return { method: init?.method?.toUpperCase() ?? "GET", url: String(input) } +} diff --git a/packages/core/test/tool-execute.test.ts b/packages/core/test/tool-execute.test.ts index e99d73701c1..59ab70d569f 100644 --- a/packages/core/test/tool-execute.test.ts +++ b/packages/core/test/tool-execute.test.ts @@ -22,8 +22,8 @@ const createCodeMode = (tools: ReadonlyMap) => test("execute describes invariant Code Mode behavior", () => { expect(createCodeMode(new Map()).description).toBe( [ - "Run JavaScript in a confined Code Mode runtime to orchestrate tool calls and compose their results.", - "Imports, direct filesystem access, and timers are unavailable. Do not use `fetch`; all external access goes through `tools`.", + "Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.", + "`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable; all other external access goes through `tools`.", "Within `{ code }`, the only callable tools are those explicitly listed in the Code Mode catalog instructions or returned by the `search` function. Inside `{ code }`, ignore tools shown outside the Code Mode catalog. They are not available in the Code Mode runtime.", 'Call tools through `tools` using only exact paths and signatures from the catalog. Do not infer or normalize tool names; preserve bracket notation such as `tools.["tool-name"](input)`.', "Prefer an explicit `return`; if omitted, the final top-level expression becomes the result.",