Skip to content

feat(core): expose fetch to code mode scripts - #49235

Merged
rekram1-node merged 3 commits into
v2from
core-fetch
Sep 16, 2026
Merged

rekram1-node merged 3 commits into
v2from
core-fetch

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Sep 15, 2026 •

Copy link
Copy Markdown
Collaborator

Scripts run by the execute tool can now call fetch. Built on #49196 (extensions are host functions) and #49242 (before/after hooks): one host function, plain globalThis.fetch, closures for the response, no new runtime types.

const res = await fetch("https://api.example.com/users", { method: "POST", body: JSON.stringify({ name }) })
if (!res.ok) throw new Error(`HTTP ${res.status}`)
return await res.json()

Three commits:

 packages/core/src/codemode/
+├── web.ts        Extension.make({ name: "web", globals: { fetch } }) + display(args) → { method, url }
 └── tool.ts
+  CodeMode.make({ tools, extensions: [CodeModeWeb.extension], hooks })
+  "extension.before": fetch rows get input: { method, url } so the TUI's existing summary shows them
-  "Do not use `fetch`; all external access goes through `tools`."
+  "`fetch` is available for HTTP requests. … all other external access goes through `tools`."

fetch(url, init?) takes method, headers, and a string / Uint8Array / URLSearchParams body, and resolves to { url, status, statusText, ok, redirected, headers: { get, has, entries }, text(), json(), bytes() }. 30 s timeout. Not a tool call; not in the catalog.

In the TUI, with no UI changes, a fetch appears under execute like a nested tool call:

✓ execute
  › fetch [method=GET, url=https://example.com]

Verified against a local server: rows appear in start order as running, settle to completed or error, and the final toolCalls metadata matches.

Not in this PR: permission prompting (webfetch's Permission.assert), body size caps, origin policy.

Checks: bun run check 35/35; packages/core test/tool-execute.test.ts + test/codemode 27 pass.

@rekram1-node
rekram1-node merged commit 194a006 into v2 Sep 16, 2026
10 checks passed
@rekram1-node
rekram1-node deleted the core-fetch branch September 16, 2026 04:29
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant