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

worldlabs.md

187 lines6.0 KB

1---
2title: World Labs
3id: worldlabs-adapter
4description: "Make finished 3D Marble worlds with World Labs in TanStack AI with @tanstack/ai-worldlabs: a viewer URL, splat files, and a mesh from a prompt, an image, or a video."
5keywords:
6 - tanstack ai
7 - worldlabs
8 - world labs
9 - marble
10 - world generation
11 - generateWorld
12 - adapter
13---
14​
15`generateWorld()` calls World Labs Marble. A prompt, image, or video returns a viewer URL, and often splat files, a mesh, and a panorama. The adapter has no chat.
16​
17## 1. Install
18​
19<!-- ::start:tabs variant="package-manager" mode="install" -->
20​
21react: @tanstack/ai @tanstack/ai-worldlabs
22vue: @tanstack/ai @tanstack/ai-worldlabs
23solid: @tanstack/ai @tanstack/ai-worldlabs
24svelte: @tanstack/ai @tanstack/ai-worldlabs
25preact: @tanstack/ai @tanstack/ai-worldlabs
26angular: @tanstack/ai @tanstack/ai-worldlabs
27vanilla: @tanstack/ai @tanstack/ai-worldlabs
28octane: @tanstack/ai @tanstack/ai-worldlabs
29​
30<!-- ::end:tabs -->
31​
32## 2. Set the API key
33​
34Create a key in the [World Labs platform](https://platform.worldlabs.ai/api-keys). Put it in the environment of your server:
35​
36```bash
37WORLDLABS_API_KEY=your-worldlabs-key
38```
39​
40`worldlabsWorld` reads `WORLDLABS_API_KEY`. To pass the key in code, use `createWorldLabsWorld`:
41​
42```ts
43import { createWorldLabsWorld } from '@tanstack/ai-worldlabs'
44​
45const adapter = createWorldLabsWorld('marble-1.1', process.env.WORLDLABS_API_KEY!)
46```
47​
48## 3. Generate a world on the server
49​
50Add a `POST` route at `/api/world`. The call waits until the world is ready, so give it a long `timeout`:
51​
52```ts
53import { generateWorld } from '@tanstack/ai'
54import { worldlabsWorld } from '@tanstack/ai-worldlabs'
55​
56export async function POST() {
57 const world = await generateWorld({
58 adapter: worldlabsWorld('marble-1.1'),
59 prompt: 'A mystical forest with glowing mushrooms',
60 threadId: 'thread-1',
61 timeout: 10 * 60 * 1000,
62 })
63​
64 return Response.json({
65 url: world.url,
66 thumbnailUrl: world.assets?.thumbnailUrl,
67 })
69```
70​
71World Labs bills in credits for each generation.
72​
73## 4. Show the world in the browser
74​
75`world.url` is the Marble viewer (`https://marble.worldlabs.ai/world/{id}`). Open it in a new tab. Do not put it in an iframe:
76​
77```ts
78const world = await fetch('/api/world', { method: 'POST' }).then((response) =>
79 response.json(),
81​
82if (typeof world.url === 'string') {
83 const link = document.createElement('a')
84 link.href = world.url
85 link.target = '_blank'
86 link.textContent = 'Open in Marble'
87 document.body.append(link)
89​
90if (typeof world.thumbnailUrl === 'string') {
91 const image = document.createElement('img')
92 image.src = world.thumbnailUrl
93 image.alt = 'Preview of the generated world'
94 document.body.append(image)
96```
97​
98You now have a 3D world and a link to explore it.
99​
100A full app is in [`examples/ts-react-media`](https://github.com/TanStack/ai/tree/main/examples/ts-react-media). Open the World tab and pick a Marble model. The example loads one SPZ splat file in Spark (it prefers `500k`) through `/api/marble-splat`. If there is no splat URL, it shows a thumbnail and an Open in Marble link.
101​
102## Image, multi-image, and video
103​
104`prompt` is the text. Put the media in `modelOptions`:
105​
106```ts
107import { generateWorld } from '@tanstack/ai'
108import { worldlabsWorld } from '@tanstack/ai-worldlabs'
109​
110const world = await generateWorld({
111 adapter: worldlabsWorld('marble-1.1'),
112 prompt: 'A cozy living room',
113 threadId: 'thread-1',
114 modelOptions: {
115 image: { uri: 'https://example.com/room.jpg' },
116 isPano: 'auto',
117 },
118})
119```
120​
121Pass only one of `image`, `images`, or `video`. See [Media options](#media-options) for each field.
122​
123## Models and options
124​
125Pass the id as a string literal, so TypeScript can narrow the options.
126​
127| Id | Notes |
128| --- | --- |
129| `marble-1.1-plus` | Dynamic world sizing |
130| `marble-1.1` | The World Labs default model id |
131| `marble-1.0` | Marble 1.0 |
132| `marble-1.0-draft` | Faster, at draft quality |
133​
134The package exports the list as `WORLDLABS_WORLD_MODELS`.
135​
136### Media options
137​
138| Option | Meaning |
139| --- | --- |
140| `image` | One image. Give `uri`, `mediaAssetId`, or `dataBase64` |
141| `images` | Several images of the same scene, each with an optional `azimuth` |
142| `video` | One video. The same source fields as `image` |
143| `isPano` | `auto`, `true`, or `false`, for a single image |
144​
145### Job options
146​
147| Option | Meaning |
148| --- | --- |
149| `wait` | The default is `true`. Set `false` to get the `operationId` at once |
150| `pollIntervalMs` | The delay between polls when `wait` is `true`. The default is 2000 |
151​
152Optional metadata: `displayName`, `seed`, `tags`, `disableRecaption`, and `permission`.
153​
154## Advanced
155​
156### What the result holds
157​
158- `world.url`: the Marble viewer URL.
159- `world.assets`: optional. It has splat, mesh, panorama, and thumbnail links only when the World Labs response includes them. These URLs are often signed CDN links.
160- `world.assets.splats.spzUrls`: splat URLs by quality key, for example `100k`, `500k`, and `full_res`.
161​
162### Jobs that do not wait
163​
164On serverless, a job that runs for minutes can hit the limit of your platform. You have two choices:
165​
166- Pass a long `timeout` to `generateWorld()`, as in step 3.
167- Set `modelOptions: { wait: false }` and poll the job yourself.
168​
169With `wait: false`, `world.status` is `waiting` and `world.operationId` is set. The adapter cannot resume that id. A new `generateWorld()` call with `wait: true` starts a new job. To get the result, poll `GET /marble/v1/operations/{operationId}` yourself.
170​
171### Bring your own key
172​
173`worldlabsByok` from `@tanstack/ai-worldlabs/byok` declares World Labs for the BYOK store, with `WORLDLABS_API_KEY` as its env var. See [Bring Your Own Key](../advanced/byok).
174​
175### API reference
176​
177| Factory | Creates |
178| --- | --- |
179| `worldlabsWorld(model, config?)` | A world adapter. Reads `WORLDLABS_API_KEY` when `config.apiKey` is not set. |
180| `createWorldLabsWorld(model, apiKey, config?)` | The same adapter with an explicit API key. |
181​
182The `config` fields:
183​
184- `apiKey`: the World Labs API key.
185- `baseUrl`: the API base URL. The default is `https://api.worldlabs.ai`.
186- `defaultHeaders`: extra request headers.
187- `fetch`: your own `fetch` function.