0
Fork 0
mirror of https://github.com/TanStack/ai.git synced 2026-11-18 19:34:58 +00:00
ai/examples/ts-code-mode-web
renovate[bot] d45f976d46
chore(deps): update build & test tooling (#1124)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
History 2026-08-17 04:52:40 +00:00
..
public Code mode (#362) 2026-04-01 11:40:41 -07:00
src refactor(code-mode)!: rename Code Mode "skills" to "snippets" (#1114) 2026-08-15 15:57:37 -07:00
.env.example Code mode (#362) 2026-04-01 11:40:41 -07:00
.gitignore refactor(code-mode)!: rename Code Mode "skills" to "snippets" (#1114) 2026-08-15 15:57:37 -07:00
package.json chore(deps): update build & test tooling (#1124) 2026-08-17 04:52:40 +00:00
README.md feat(ai-isolate): add native QuickJS Code Mode isolate driver for Bun (#750) 2026-08-07 11:05:31 +02:00
tsconfig.json Code mode (#362) 2026-04-01 11:40:41 -07:00
vite.config.ts feat(ai-isolate): add native QuickJS Code Mode isolate driver for Bun (#750) 2026-08-07 11:05:31 +02:00

ts-code-mode-web

TanStack AI Code Mode web example using React and TanStack Start.

Setup

Prerequisites

  • Node.js >=18
  • pnpm@10.17.0
  • Xcode Command Line Tools (macOS)

Installation

pnpm install

Rebuilding isolated-vm (native module)

@tanstack/ai-isolate-node depends on isolated-vm, a native Node.js addon that must be compiled from source when a prebuilt binary is not available for your Node.js version.

When is this needed?

The isolated-vm package ships prebuilt binaries for select Node.js ABI versions. If you are running a newer Node.js version whose ABI is not yet included (e.g. Node.js 25.x / ABI 141), you will see an error like:

Error: No native build was found for platform=darwin arch=arm64 runtime=node abi=141 ...
    loaded from: .../isolated-vm

How to fix it

  1. Ensure Xcode Command Line Tools are installed (macOS):

    xcode-select --install
    
  2. Run node-gyp via npx inside the isolated-vm package directory (from the monorepo root):

    ISOLATED_VM_DIR="node_modules/.pnpm/isolated-vm@6.1.0/node_modules/isolated-vm"
    cd "$ISOLATED_VM_DIR" && npx node-gyp rebuild
    

    Or as a one-liner from the monorepo root (/Users/jherr/tanstack/ai/code-mode):

    cd node_modules/.pnpm/isolated-vm@6.1.0/node_modules/isolated-vm && npx node-gyp rebuild
    
  3. A successful build ends with gyp info ok and produces build/Release/isolated_vm.node.

Notes

  • Linker warnings about macOS version mismatches (building for macOS-11.0, but linking with dylib ... built for newer version) are harmless and can be ignored.
  • The compiled .node file lives in the pnpm content-addressable store and will need to be rebuilt after pnpm install --force or after upgrading Node.js to a different ABI version.
  • Python 3 is required by node-gyp. It is detected automatically from $PATH.

Node.js 25 + isolated-vm runtime crash (SIGSEGV)

Even after successfully compiling from source, isolated-vm@6.1.0 crashes the server process (exit code 139, SIGSEGV) when run under Node.js 25.x. This is a V8 API incompatibility — Node 25 ships V8 14.1 whose internal C++ API has changed in ways that break isolated-vm's isolate creation code at runtime.

Symptom: The server dies silently with no JavaScript error or log output when the first code mode request is made.

Fix applied: All server routes in this example have been switched from the node isolate driver to the quickjs driver (@tanstack/ai-isolate-quickjs), which is a pure-JS sandbox with no native addon dependency and works on any Node.js version.

When isolated-vm works again: Once isolated-vm publishes a release compatible with Node 25 / V8 14.1, switch the driver back to 'node' in src/lib/create-isolate-driver.ts (default) and in each API route file. The node driver provides stronger isolation (true V8 process boundary) and is preferred in production.

Development

# Node (default) — Node isolate / QuickJS WASM / Cloudflare drivers
pnpm dev   # http://localhost:3001

# Bun — native QuickJS via bun:ffi (@tanstack/ai-isolate-quickjs-bun)
pnpm dev:bun
# equivalent:
#   CODE_MODE_BUN=1 bun --bun vite dev --port 3001

Bun mode (CODE_MODE_BUN=1)

When set (or when the process is Bun), this example:

  1. Defaults the isolate VM to quickjs-bun (sidebar + server routes that call createIsolateDriver())
  2. Uses Nitro preset: 'bun' for the server build
  3. Keeps client resolve.conditions without bun (required so TanStack Router hydrates — see router-core isServer dual package)

Optional overrides:

Env Effect
CODE_MODE_BUN=1 Bun defaults + Nitro bun preset (also set by pnpm dev:bun)
CODE_MODE_DEFAULT_VM=node|quickjs|quickjs-bun|cloudflare Force default isolate regardless of Bun

Build

pnpm build

# Bun production build + run
pnpm build:bun
pnpm start:bun