Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

README.md

106 lines4.2 KB

1# ts-code-mode-web
2​
3TanStack AI Code Mode web example using React and TanStack Start.
4​
5## Setup
6​
7### Prerequisites
8​
9- Node.js >=18
10- pnpm@10.17.0
11- Xcode Command Line Tools (macOS)
12​
13### Installation
14​
15```bash
16pnpm install
17```
18​
19### Rebuilding `isolated-vm` (native module)
20​
21`@tanstack/ai-isolate-node` depends on [`isolated-vm`](https://github.com/laverdet/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.
22​
23**When is this needed?**
24​
25The `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:
26​
27```
28Error: No native build was found for platform=darwin arch=arm64 runtime=node abi=141 ...
29 loaded from: .../isolated-vm
30```
31​
32**How to fix it**
33​
341. Ensure Xcode Command Line Tools are installed (macOS):
35​
36 ```bash
37 xcode-select --install
38 ```
39​
402. Run `node-gyp` via `npx` inside the `isolated-vm` package directory (from the monorepo root):
41​
42 ```bash
43 ISOLATED_VM_DIR="node_modules/.pnpm/isolated-vm@6.1.0/node_modules/isolated-vm"
44 cd "$ISOLATED_VM_DIR" && npx node-gyp rebuild
45 ```
46​
47 Or as a one-liner from the monorepo root (`/Users/jherr/tanstack/ai/code-mode`):
48​
49 ```bash
50 cd node_modules/.pnpm/isolated-vm@6.1.0/node_modules/isolated-vm && npx node-gyp rebuild
51 ```
52​
533. A successful build ends with `gyp info ok` and produces `build/Release/isolated_vm.node`.
54​
55**Notes**
56​
57- 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.
58- 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.
59- Python 3 is required by `node-gyp`. It is detected automatically from `$PATH`.
60​
61### Node.js 25 + `isolated-vm` runtime crash (SIGSEGV)
62​
63Even 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.
64​
65**Symptom:** The server dies silently with no JavaScript error or log output when the first code mode request is made.
66​
67**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.
68​
69**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.
70​
71## Development
72​
73```bash
74# Node (default) — Node isolate / QuickJS WASM / Cloudflare drivers
75pnpm dev # http://localhost:3001
76​
77# Bun — native QuickJS via bun:ffi (@tanstack/ai-isolate-quickjs-bun)
78pnpm dev:bun
79# equivalent:
80# CODE_MODE_BUN=1 bun --bun vite dev --port 3001
81```
82​
83### Bun mode (`CODE_MODE_BUN=1`)
84​
85When set (or when the process is Bun), this example:
86​
871. Defaults the isolate VM to **`quickjs-bun`** (sidebar + server routes that call `createIsolateDriver()`)
882. Uses Nitro `preset: 'bun'` for the server build
893. Keeps client `resolve.conditions` **without** `bun` (required so TanStack Router hydrates — see router-core `isServer` dual package)
90​
91Optional overrides:
92​
93| Env | Effect |
94| ------------------------------------------------------------- | ------------------------------------------------------------ |
95| `CODE_MODE_BUN=1` | Bun defaults + Nitro bun preset (also set by `pnpm dev:bun`) |
96| `CODE_MODE_DEFAULT_VM=node\|quickjs\|quickjs-bun\|cloudflare` | Force default isolate regardless of Bun |
97​
98## Build
99​
100```bash
101pnpm build
102​
103# Bun production build + run
104pnpm build:bun
105pnpm start:bun
106```