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

api.world.ts

134 lines3.6 KB

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 })
22​
23function jsonFetch(
24 body: unknown,
25 status = 200,
26 statusText = 'OK',
27): typeof fetch {
28 return async () => jsonResponse(body, status, statusText)
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 }
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 adapter: worldlabsWorld('marble-1.1', {
85 apiKey: 'wlt_e2e',
86 fetch: worldLabsFetch(fail),
87 }),
88 prompt,
89 debug: false,
90 })
91 return Response.json({
92 ok: true,
93 url: result.url,
94 worldId: result.worldId,
95 operationId: result.operationId,
96 model: result.model,
97 prompt: result.prompt,
98 status: result.status,
99 })
100 }
101​
102 const fetchImpl = fail
103 ? jsonFetch('no credits', 402, 'Payment Required')
104 : jsonFetch({ jwt: 'jwt-e2e', expires_at: EXPIRES_AT })
105​
106 const result = await generateWorld({
107 adapter: reactorWorld('visko-orbis-stable', {
108 apiKey: 'rk_e2e',
109 fetch: fetchImpl,
110 }),
111 prompt,
112 debug: false,
113 })
114 return Response.json({
115 ok: true,
116 token: result.token,
117 model: result.model,
118 prompt: result.prompt,
119 status: result.status,
120 expiresAt: result.expiresAt,
121 })
122 } catch (error) {
123 return Response.json(
124 {
125 ok: false,
126 error: error instanceof Error ? error.message : 'unknown',
127 },
128 { status: 500 },
129 )
130 }
131 },
132 },
133 },
134})