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

reactor.md

282 lines9.8 KB

1---
2title: Reactor
3id: reactor-adapter
4description: "Stream live worlds and live video from Reactor models in TanStack AI with @tanstack/ai-reactor. The server mints a session token, and the browser plays and steers the stream."
5keywords:
6 - tanstack ai
7 - reactor
8 - world generation
9 - live generation
10 - orbis
11 - helios
12 - generateWorld
13 - generateLiveVideo
14 - adapter
15---
16​
17The Reactor adapter opens a live world or live video session. The server mints a session token. The browser plays and steers the stream. It has no chat.
18​
19- `reactorWorld()` with `generateWorld()`: a world the user can look at and move through.
20- `reactorVideo()` with `generateLiveVideo()`: a live video session.
21​
22## 1. Install
23​
24The server needs `@tanstack/ai-reactor`. The browser needs `@reactor-team/js-sdk` to connect and play the stream.
25​
26<!-- ::start:tabs variant="package-manager" mode="install" -->
27​
28react: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
29vue: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
30solid: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
31svelte: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
32preact: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
33angular: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
34vanilla: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
35octane: @tanstack/ai @tanstack/ai-reactor @reactor-team/js-sdk
36​
37<!-- ::end:tabs -->
38​
39## 2. Set the API key
40​
41Create a key in the [Reactor dashboard](https://www.reactor.inc/dashboard). Keys start with `rk_`. Put the key in the environment of your server:
42​
43```bash
44REACTOR_API_KEY=rk_...
45```
46​
47`reactorWorld` and `reactorVideo` read `REACTOR_API_KEY`. To pass the key in code, use `createReactorWorld` or `createReactorVideo`:
48​
49```ts
50import { createReactorWorld } from '@tanstack/ai-reactor'
51​
52const adapter = createReactorWorld('visko-orbis-stable', process.env.REACTOR_API_KEY!)
53```
54​
55## 3. Mint a world session on the server
56​
57Add a `POST` route at `/api/world`:
58​
59```ts
60import { generateWorld } from '@tanstack/ai'
61import { reactorWorld } from '@tanstack/ai-reactor'
62​
63export async function POST() {
64 const world = await generateWorld({
65 adapter: reactorWorld('visko-orbis-stable'),
66 prompt: 'A neon cyberpunk city at night, slow aerial drift',
67 threadId: 'thread-1',
68 })
69​
70 return Response.json({
71 token: world.token,
72 model: world.model,
73 prompt: world.prompt,
74 expiresAt: world.expiresAt,
75 })
77```
78​
79The token works for this one model only. The API key stays on the server. `world.model` is the Reactor connect slug, here `reactor/visko-orbis-stable`.
80​
81## 4. Play the world in the browser
82​
83Connect with the token and play the stream in a `<video>` element:
84​
85```ts
86import { Reactor } from '@reactor-team/js-sdk'
87​
88const video = document.querySelector('video')
89if (!video) {
90 throw new Error('Missing video element')
92​
93const world = await fetch('/api/world', { method: 'POST' }).then(async (response) => {
94 if (!response.ok) {
95 throw new Error('World session failed')
96 }
97 return response.json()
98})
99​
100const token = typeof world.token === 'string' ? world.token : ''
101const model = typeof world.model === 'string' ? world.model : ''
102const prompt = typeof world.prompt === 'string' ? world.prompt : ''
103if (token.length === 0 || model.length === 0 || prompt.length === 0) {
104 throw new Error('World payload is incomplete')
106​
107const reactor = new Reactor({ modelName: model })
108​
109reactor.on('trackReceived', (name, _track, stream) => {
110 if (name !== 'main_video') return
111 video.muted = true
112 video.srcObject = stream
113 void video.play()
114})
115​
116await reactor.connect(token)
117await reactor.sendCommand('set_resolution', { resolution: '1080p' })
118await reactor.sendCommand('set_prompt', { prompt })
119await reactor.sendCommand('start', {})
120```
121​
122The video element now plays a live world. To steer it, send a new `set_prompt`. For a version that attaches the stream again when a track unmutes, see [World Generation](../media/world-generation).
123​
124A full working app is in [`examples/ts-react-media`](https://github.com/TanStack/ai/tree/main/examples/ts-react-media). Open the World or Live tab.
125​
126## Live video
127​
128Reactor video is a live stream, not a finished file. `generateLiveVideo()` returns a token, the same as `generateWorld()`:
129​
130```ts
131import { generateLiveVideo } from '@tanstack/ai'
132import { reactorVideo } from '@tanstack/ai-reactor'
133​
134const live = await generateLiveVideo({
135 adapter: reactorVideo('helios'),
136 prompt: 'A neon cyberpunk city at night, slow aerial drift',
137 threadId: 'thread-1',
138})
139```
140​
141Send `live.token`, `live.model`, and `live.prompt` to the browser. Then connect as in step 4, or see [Live Generation](../media/live-generation).
142​
143`helios` works with both adapters:
144​
145- Use `generateWorld()` for a world that the user moves through.
146- Use `generateLiveVideo()` for a video session.
147​
148## Models and options
149​
150Pass the id as a string literal, so TypeScript can narrow the options. The result `model` is the connect slug. Pass it to `new Reactor({ modelName })`.
151​
152### World models
153​
154| Id | Connect slug |
155| --- | --- |
156| `visko-orbis-stable` | `reactor/visko-orbis-stable` |
157| `visko-orbis-dynamic` | `reactor/visko-orbis-dynamic` |
158| `happy-oyster-adventure` | `reactor/happy-oyster-adventure` |
159| `happy-oyster-director` | `reactor/happy-oyster-director` |
160| `lingbot-world-2` | `reactor/lingbot-world-2` |
161| `lingbot` | `reactor/lingbot` |
162| `helios` | `reactor/helios` |
163​
164Happy Oyster (`happy-oyster-adventure`, `happy-oyster-director`) uses `createWorld` and `startTravel` after `connect`.
165​
166### Live video models
167​
168| Id | Connect slug |
169| --- | --- |
170| `helios` | `reactor/helios` |
171| `fast-h3` | `reactor/fast-h3` |
172| `longlive-v2` | `reactor/longlive-v2` |
173| `ltx2` | `reactor/ltx2` |
174​
175The package exports the lists as `REACTOR_WORLD_MODELS`, `REACTOR_VIDEO_MODELS`, `REACTOR_WORLD_SLUGS`, and `REACTOR_VIDEO_SLUGS`.
176​
177### Browser session options
178​
179`generateWorld()` and `generateLiveVideo()` send only the prompt when they mint the token. They do not send a resolution or a seed image. Keep these options in client state, and send them with `sendCommand` after `connect`. Orbis reads them on the next `start`:
180​
181```ts
182import { Reactor } from '@reactor-team/js-sdk'
183​
184const reactor = new Reactor({ modelName: 'reactor/visko-orbis-stable' })
185await reactor.sendCommand('set_resolution', { resolution: '2k' })
186await reactor.sendCommand('set_seed', { seed: 42 })
187await reactor.sendCommand('set_audio_enabled', { audio_enabled: true })
188```
189​
190| Option | Command | Meaning |
191| --- | --- | --- |
192| `resolution` | `set_resolution` | The delivery tier: `1080p`, `2k`, or `4k` |
193| `seed` | `set_seed` | The RNG seed for the next run |
194| `audioEnabled` | `set_audio_enabled` | When `false`, Reactor does not compute audio |
195| `audioPrompt` | `set_audio_prompt` | A description of the sound, or `""` for audio that follows the picture |
196​
197## Advanced
198​
199### Start from a seed image
200​
201Send seed images from the browser after `connect`. Pass a `File` from `<input type="file">`. The SDK uploads it and returns a `FileRef`. Do not send base64.
202​
203LingBot starts from a seed image. `start` also needs `set_prompt`. Write the prompt so that it describes what the image shows:
204​
205```ts
206import { Reactor } from '@reactor-team/js-sdk'
207​
208const reactor = new Reactor({ modelName: 'reactor/lingbot' })
209const picker = document.querySelector('input[type="file"]')
210if (!(picker instanceof HTMLInputElement)) {
211 throw new Error('Pick a seed image')
213const file = picker.files?.[0]
214if (file === undefined) {
215 throw new Error('Pick a seed image')
217const image = await reactor.uploadFile(file)
218await reactor.sendCommand('set_image', { image })
219await reactor.sendCommand('set_prompt', {
220 prompt: 'A narrow stone alley at night, wet cobbles, one lit shop window.',
221})
222await reactor.sendCommand('start', {})
223```
224​
225Other models:
226​
227- **Helios** can take the same `File` with `set_conditioning`, so the prompt and the image arrive together.
228- **Orbis** takes an optional seed image. Send `set_image` before `set_prompt` and `start`. Use a 16:9 image, because Orbis resizes other shapes without a crop. A new image during a run has no effect until `reset`.
229​
230### Bring your own key
231​
232The browser can save a Reactor key and send it in the `x-byok-reactor` header. The server reads that header first, then `REACTOR_API_KEY`:
233​
234```ts
235import { generateWorld } from '@tanstack/ai'
236import { byokMissing, getByokKey } from '@tanstack/ai/byok/server'
237import { reactorWorld } from '@tanstack/ai-reactor'
238import { reactorByok } from '@tanstack/ai-reactor/byok'
239​
240export async function POST(request: Request) {
241 const apiKey = getByokKey(request, reactorByok)
242 if (!apiKey) return byokMissing(reactorByok)
243​
244 const world = await generateWorld({
245 adapter: reactorWorld('visko-orbis-stable', { apiKey }),
246 prompt: 'A neon cyberpunk city at night, slow aerial drift',
247 threadId: 'thread-1',
248 })
249​
250 return Response.json({
251 token: world.token,
252 model: world.model,
253 prompt: world.prompt,
254 expiresAt: world.expiresAt,
255 })
257```
258​
259Do not put the API key in the JSON body. For the browser side, see [Let users bring their own Reactor key](../media/world-generation#let-users-bring-their-own-reactor-key) and [Bring Your Own Key](../advanced/byok).
260​
261### Custom endpoint
262​
263```ts
264import { reactorWorld } from '@tanstack/ai-reactor'
265​
266const adapter = reactorWorld('visko-orbis-stable', {
267 baseUrl: 'https://api.reactor.inc',
268})
269```
270​
271### API reference
272​
273| Factory | Creates |
274| --- | --- |
275| `reactorWorld(model, config?)` / `createReactorWorld(model, apiKey, config?)` | A world adapter for `generateWorld()` |
276| `reactorVideo(model, config?)` / `createReactorVideo(model, apiKey, config?)` | A live video adapter for `generateLiveVideo()` |
277​
278The short factory reads `REACTOR_API_KEY` when `config.apiKey` is not set. The `config` fields:
279​
280- `apiKey`: the Reactor API key.
281- `baseUrl`: the API base URL. The default is `https://api.reactor.inc`.
282- `fetch`: your own `fetch` function.