@oh-my-ai-sdk/code-mode-bun-compat
v0.1.2
Published
Make @ai-sdk/code-mode importable under Bun: a postinstall patcher that swaps its static node:module stripTypeScriptTypes import for a feature-test with an amaro fallback. Self-retiring once Bun ships the API (oven-sh/bun#35517).
Maintainers
Readme
code-mode-bun-compat
Part of Oh mAI SDK, a collection of community extensions for Vercel AI SDK.
Make @ai-sdk/code-mode work under
Bun. Two lines of integration, self-retiring when Bun catches up.
Quick start
bun add ai @ai-sdk/code-mode
bun add -d code-mode-bun-compatThen wire the postinstall (after the add above — a postinstall referencing a bin that isn't installed yet fails the next install with a bare exit 127):
// package.json
{
"scripts": {
"postinstall": "code-mode-bun-compat"
}
}That's the whole integration. bun install triggers the postinstall, the installed
@ai-sdk/code-mode gets patched in place (~50ms, idempotent), and code mode just works
under Bun:
import { experimental_runCodeMode } from "@ai-sdk/code-mode";
const result = await experimental_runCodeMode({
js: "return 6 * 7;",
tools: myTools,
options: { executionPolicy: { timeoutMs: 30_000, maxBridgeRequests: 10 } },
});Without the patch, that import line throws under Bun before any of your code runs.
What you get
- Reinstalls are handled. Every
bun installre-runs the postinstall; already-patched files are detected and skipped. A lockfile change can't silently un-patch you. - Node stays unaffected. The shim is a runtime feature-test — on Node it finds the native API immediately, same code path as unpatched. Dual-runtime projects (dev on Bun, CI on Node) need no conditionals.
- Nothing to unwind later. When Bun ships
stripTypeScriptTypesnatively (PR #35517 is in progress), the feature-test starts finding the real function and the fallback simply never runs again. Delete the dep at leisure, or don't. - Loud failure, never a weird one. If a future
@ai-sdk/code-moderestructures its dist files, the patcher exits 1 withUNEXPECTED SHAPEinstead of half-patching — your install fails legibly rather than your runtime failing mysteriously. - Monorepos:
code-mode-bun-compat packages/my-appresolves from a specific workspace; the bare command resolves from cwd. Resolution usescreateRequire, so hoisted layouts are handled.
What it actually fixes
Two Bun gaps break @ai-sdk/code-mode today:
node:modulehas nostripTypeScriptTypes(oven-sh/bun#25058). The Code Mode runtime imports it statically indist/utils/source-cache.js, so under Bun the import itself dies—even for programs that never feed it TypeScript. The runtime lives in@ai-sdk/code-modein older releases and itsrundependency in newer releases; the patcher resolves either layout.- The QuickJS worker boots from a multi-MB base64
data:URL, which Bun's module specifier canonicalization rejects (NameTooLong).
The patcher rewrites both, in place:
- The static import becomes a feature-test with an
amarofallback. amaro is the same SWC-wasm wrapper Node itself uses for type stripping, so strip-mode output keeps Node's position-preserving semantics (types replaced in place with whitespace). - The
data:worker URL becomes a process-private temp-filefile:URL, created exclusively and removed on exit—identical worker semantics, valid in both runtimes without a shared predictable path.
Test
bun run testRuns offline patch-shape fixtures, then installs two exact
@ai-sdk/code-mode / ai pairs in isolated scratch caches: the historical
in-package runtime and the extracted run layout. Both must reproduce the
known Bun import failure, work natively under pinned Node, accept the patcher,
accept an idempotent second run, and execute a real Bun cell end-to-end. The
consumer installs the packed compatibility package and does not install
amaro directly.
A separate scheduled canary installs current upstream releases and current Bun. It characterizes native support without making ordinary pull requests depend on release timing.
Scope and honesty
- This patches published dist files of a specific package shape — a bridge, not a
fork. The right long-term fixes live upstream: Bun implementing the API (#35517),
and/or
@ai-sdk/code-modefeature-testing its import. - Extracted from a durable agent runtime where it has been the load-bearing enabler for running code-mode cells under Bun since 2026-08.
License
Apache-2.0
