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

worldlabs.ts

169 lines5.0 KB

1/**
2 * World Labs Marble. The public OpenAPI document is the Mintlify spec at
3 * docs.worldlabs.ai (the API host's /openapi.json 404s). TanStack AI
4 * generates its client from GET /v1/openapi/worldlabs.
5 *
6 * The model list is the public models page, not a keyed endpoint, so
7 * listModels does not skip when WORLDLABS_API_KEY is absent. The key is
8 * the data-plane `WLT-Api-Key` header.
9 */
10import type { Activity } from '#/db/schema.ts'
11import { headerApiKeyConnect } from '../connect.ts'
12import {
13 docsReport,
14 docsRun,
15 tryDocs,
16 unavailable,
17 assertParsed,
18 markdownTableRows,
19} from '../model-facts.ts'
20import { tagDocsFacts } from '../fact-sources.ts'
21import { overlayModelFacts } from '../reasoning-config.ts'
22import { fetchOpenApi, fetchText } from '../types.ts'
23import type {
24 ListModelsResult,
25 ModelInfo,
26 ProviderConfig,
27 ProviderSecrets,
28 SpecFetchResult,
29} from '../types.ts'
30import {
31 WORLDLABS_PRICING_URL,
32 worldlabsModelPricing,
33} from '../worldlabs-pricing.ts'
34​
35export const WORLDLABS_OPENAPI_URL =
36 'https://docs.worldlabs.ai/api/reference/openapi.yaml'
37export const WORLDLABS_MODELS_URL = 'https://docs.worldlabs.ai/api/models.md'
38const GENERATE_DOCS_URL =
39 'https://docs.worldlabs.ai/api/reference/worlds/generate'
40export const WORLDLABS_API_BASE = 'https://api.worldlabs.ai'
41const GENERATE_PATH = '/marble/v1/worlds:generate'
42​
43const WORLD_PATHS = new Set([
44 GENERATE_PATH,
45 '/marble/v1/worlds/{world_id}',
46 '/marble/v1/worlds/{world_id}:export',
47 '/marble/v1/worlds:list',
48 '/marble/v1/operations/{operation_id}',
49 '/marble/v1/media-assets:prepare_upload',
50 '/marble/v1/media-assets/{media_asset_id}',
51 '/marble/v1/pano:depth_to_rgb',
52 '/marble/v1/credits',
53])
54​
55const API_ID = /^marble-\d+\.\d+(?:-[a-z0-9]+)?$/
56​
57const MODALITIES = {
58 input: ['text', 'image', 'video'],
59 output: null,
61​
62function unquote(cell: string): string {
63 return cell.replace(/`/g, '').trim()
65​
66export interface WorldlabsModelRow {
67 rawId: string
68 displayName: string
69 aliases: Array<string>
71​
72/** Models page mapping table, plus the legacy `model` values it names. */
73export function parseWorldlabsModels(
74 page: string,
75): Map<string, WorldlabsModelRow> {
76 const models = new Map<string, WorldlabsModelRow>()
77 const aliases = new Map<string, Array<string>>()
78 for (const row of markdownTableRows(page)) {
79 const left = unquote(row[0] ?? '')
80 const right = unquote(row[1] ?? '')
81 if (API_ID.test(right) && left.length > 0 && !API_ID.test(left)) {
82 models.set(right, { rawId: right, displayName: left, aliases: [] })
83 } else if (API_ID.test(left) && right.length > 0 && !API_ID.test(right)) {
84 const prior = aliases.get(left) ?? []
85 prior.push(right)
86 aliases.set(left, prior)
87 }
88 }
89 if (models.size === 0) return models
90 for (const [rawId, names] of aliases) {
91 const model = models.get(rawId)
92 if (!model) {
93 throw new Error(
94 `worldlabs models: legacy alias for ${rawId}, which the mapping table omits`,
95 )
96 }
97 model.aliases = names
98 }
99 for (const id of [
100 'marble-1.1-plus',
101 'marble-1.1',
102 'marble-1.0',
103 'marble-1.0-draft',
104 ]) {
105 if (!models.has(id)) throw new Error(`worldlabs models: page omits ${id}`)
106 }
107 return models
109​
110function classify(path: string): Activity | null {
111 const bare = path.split('?')[0] ?? path
112 return WORLD_PATHS.has(bare) ? 'world' : null
114​
115async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
116 const { spec, hash } = await fetchOpenApi(WORLDLABS_OPENAPI_URL)
117 return {
118 specs: [spec],
119 sources: [{ url: WORLDLABS_OPENAPI_URL, hash }],
120 outputStrategy: 'post-200',
121 }
123​
124async function listModels(
125 _env: ProviderSecrets,
126 kv?: KVNamespace,
127): Promise<ListModelsResult> {
128 const text = await fetchText(WORLDLABS_MODELS_URL)
129 const parsed = parseWorldlabsModels(text)
130 assertParsed(parsed, 'worldlabs models page')
131 const docs = docsRun()
132 const pricing = await tryDocs(docs, WORLDLABS_PRICING_URL, (cached) =>
133 worldlabsModelPricing(kv, cached),
134 )
135 const modalitySources = tagDocsFacts(
136 { modalities: MODALITIES },
137 GENERATE_DOCS_URL,
138 )
139 const models: Array<ModelInfo> = [...parsed.values()].map((model) =>
140 overlayModelFacts(
141 {
142 rawId: model.rawId,
143 displayName: model.displayName,
144 activity: 'world',
145 aliases: model.aliases.length > 0 ? model.aliases : null,
146 modalities: MODALITIES,
147 factSources: modalitySources,
148 },
149 pricing?.(model.rawId) ?? unavailable('pricing'),
150 ),
151 )
152 return { models, docsFailures: docsReport(docs) }
154​
155export const provider: ProviderConfig = {
156 id: 'worldlabs',
157 displayName: 'World Labs',
158 authEnvVar: 'WORLDLABS_API_KEY',
159 specSourceUrl: WORLDLABS_OPENAPI_URL,
160 modelsEndpoint: WORLDLABS_MODELS_URL,
161 defaultDerivation: 'upstream-spec',
162 connect: headerApiKeyConnect(WORLDLABS_API_BASE, 'WLT-Api-Key'),
163 fetchSpec,
164 listModels,
165 classify,
166 classifyGets: true,
167 generationEndpointId: ({ activity }) =>
168 activity === 'world' ? GENERATE_PATH.slice(1) : null,