Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { generateWorld } from '@tanstack/ai'
3import { reactorWorld } from '@tanstack/ai-reactor'
4import { worldlabsWorld } from '@tanstack/ai-worldlabs'
5
6const EXPIRES_AT = 1_800_000_000
7const WORLD_ID = 'world-e2e'
8const OPERATION_ID = 'op-e2e'
9const MARBLE_URL = `https://marble.worldlabs.ai/world/${WORLD_ID}`
10
11function jsonResponse(
12 body: unknown,
13 status = 200,
14 statusText = 'OK',
15): Response {
16 return new Response(typeof body === 'string' ? body : JSON.stringify(body), {
17 status,
18 statusText,
19 headers: { 'Content-Type': 'application/json' },
20 })
21}
22
23function jsonFetch(
24 body: unknown,
25 status = 200,
26 statusText = 'OK',
27): typeof fetch {
28 return async () => jsonResponse(body, status, statusText)
29}
30
31function worldLabsFetch(fail: boolean): typeof fetch {
32 if (fail) {
33 return jsonFetch({ detail: 'no credits' }, 402, 'Payment Required')
34 }
35 return async (input: RequestInfo | URL) => {
36 const raw =
37 typeof input === 'string'
38 ? input
39 : input instanceof URL
40 ? input.href
41 : input.url
42 const url = decodeURIComponent(raw)
43 if (url.includes('/marble/v1/worlds:generate')) {
44 return jsonResponse({
45 operation_id: OPERATION_ID,
46 done: true,
47 response: {
48 world_id: WORLD_ID,
49 display_name: 'E2E World',
50 world_marble_url: MARBLE_URL,
51 model: 'marble-1.1',
52 },
53 })
54 }
55 return jsonResponse({ detail: `unexpected url ${url}` }, 500)
56 }
57}
58
59export const Route = createFileRoute('/api/world')({
60 server: {
61 handlers: {
62 POST: async ({ request }) => {
63 const body = (await request.json()) as {
64 prompt?: unknown
65 fail?: unknown
66 provider?: unknown
67 }
68 if (
69 typeof body.prompt !== 'string' ||
70 body.prompt.trim().length === 0
71 ) {
72 return Response.json(
73 { ok: false, error: 'Prompt is required' },
74 { status: 400 },
75 )
76 }
77
78 const fail = body.fail === true
79 const prompt = body.prompt.trim()
80
81 try {
82 if (body.provider === 'worldlabs') {
83 const result = await generateWorld({
84 threadId: 'world',
85 adapter: worldlabsWorld('marble-1.1', {
86 apiKey: 'wlt_e2e',
87 fetch: worldLabsFetch(fail),
88 }),
89 prompt,
90 debug: false,
91 })
92 return Response.json({
93 ok: true,
94 url: result.url,
95 worldId: result.worldId,
96 operationId: result.operationId,
97 model: result.model,
98 prompt: result.prompt,
99 status: result.status,
100 })
101 }
102
103 const fetchImpl = fail
104 ? jsonFetch('no credits', 402, 'Payment Required')
105 : jsonFetch({ jwt: 'jwt-e2e', expires_at: EXPIRES_AT })
106
107 const result = await generateWorld({
108 threadId: 'world',
109 adapter: reactorWorld('visko-orbis-stable', {
110 apiKey: 'rk_e2e',
111 fetch: fetchImpl,
112 }),
113 prompt,
114 debug: false,
115 })
116 return Response.json({
117 ok: true,
118 token: result.token,
119 model: result.model,
120 prompt: result.prompt,
121 status: result.status,
122 expiresAt: result.expiresAt,
123 })
124 } catch (error) {
125 return Response.json(
126 {
127 ok: false,
128 error: error instanceof Error ? error.message : 'unknown',
129 },
130 { status: 500 },
131 )
132 }
133 },
134 },
135 },
136})