oda-engine 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/README.md +151 -2
  2. package/bin/nowhere.mjs +352 -0
  3. package/lib/account.mjs +57 -0
  4. package/lib/agentModules.mjs +57 -0
  5. package/lib/devServer.mjs +351 -0
  6. package/lib/headless.mjs +231 -0
  7. package/lib/headlessPreview.mjs +212 -0
  8. package/lib/keys.mjs +517 -0
  9. package/lib/make.mjs +855 -0
  10. package/lib/mcp.mjs +537 -0
  11. package/lib/open.mjs +22 -0
  12. package/lib/preview.mjs +87 -0
  13. package/lib/previewRpc.mjs +134 -0
  14. package/lib/project.mjs +359 -0
  15. package/lib/publishFlow.mjs +90 -0
  16. package/lib/resources.mjs +50 -0
  17. package/lib/rules.mjs +60 -0
  18. package/lib/scaffold.mjs +42 -0
  19. package/lib/setup.mjs +59 -0
  20. package/lib/tools.mjs +668 -0
  21. package/package.json +51 -4
  22. package/template/game.config.json +9 -0
  23. package/template/scene.json +4 -0
  24. package/template/src/main.ts +11 -0
  25. package/vendor/DOCS.md +447 -0
  26. package/vendor/agent/appRun.mjs +520 -0
  27. package/vendor/agent/assets/brief.mjs +242 -0
  28. package/vendor/agent/assets/collider.mjs +531 -0
  29. package/vendor/agent/assets/environments/index.mjs +12 -0
  30. package/vendor/agent/assets/environments/worldlabs.mjs +161 -0
  31. package/vendor/agent/assets/fetch.mjs +168 -0
  32. package/vendor/agent/assets/footprint.mjs +13 -0
  33. package/vendor/agent/assets/gltf.mjs +547 -0
  34. package/vendor/agent/assets/hdr.mjs +269 -0
  35. package/vendor/agent/assets/imageOps.mjs +171 -0
  36. package/vendor/agent/assets/images/codex.mjs +307 -0
  37. package/vendor/agent/assets/images/fal.mjs +75 -0
  38. package/vendor/agent/assets/images/gemini.mjs +84 -0
  39. package/vendor/agent/assets/images/index.mjs +25 -0
  40. package/vendor/agent/assets/images/openai.mjs +75 -0
  41. package/vendor/agent/assets/images/stability.mjs +71 -0
  42. package/vendor/agent/assets/images/views.mjs +31 -0
  43. package/vendor/agent/assets/import.mjs +1352 -0
  44. package/vendor/agent/assets/inspect.mjs +465 -0
  45. package/vendor/agent/assets/jobs.mjs +196 -0
  46. package/vendor/agent/assets/ktx2.mjs +290 -0
  47. package/vendor/agent/assets/ledger.mjs +466 -0
  48. package/vendor/agent/assets/net.mjs +254 -0
  49. package/vendor/agent/assets/prices.json +39 -0
  50. package/vendor/agent/assets/providers/fal.mjs +115 -0
  51. package/vendor/agent/assets/providers/index.mjs +22 -0
  52. package/vendor/agent/assets/providers/meshy.mjs +119 -0
  53. package/vendor/agent/assets/providers/tripo.mjs +188 -0
  54. package/vendor/agent/assets/sources/ambientcg.mjs +129 -0
  55. package/vendor/agent/assets/sources/index.mjs +23 -0
  56. package/vendor/agent/assets/sources/polyhaven.mjs +237 -0
  57. package/vendor/agent/assets/sources/polypizza.mjs +121 -0
  58. package/vendor/agent/assets/splat.mjs +631 -0
  59. package/vendor/agent/assets/targets.mjs +122 -0
  60. package/vendor/agent/assets/tile.mjs +261 -0
  61. package/vendor/agent/assets/unzip.mjs +180 -0
  62. package/vendor/agent/blender/cleanup.py +360 -0
  63. package/vendor/agent/blender/convert.py +167 -0
  64. package/vendor/agent/blender/find.mjs +192 -0
  65. package/vendor/agent/blender/nw_export.py +104 -0
  66. package/vendor/agent/blender/nw_rubric.py +426 -0
  67. package/vendor/agent/blender/run.mjs +193 -0
  68. package/vendor/agent/blender/runner.py +252 -0
  69. package/vendor/agent/blender/sheet.py +743 -0
  70. package/vendor/agent/depth.mjs +134 -0
  71. package/vendor/agent/director/board.mjs +451 -0
  72. package/vendor/agent/director/contracts.mjs +663 -0
  73. package/vendor/agent/director/director.mjs +2365 -0
  74. package/vendor/agent/director/estimate.mjs +200 -0
  75. package/vendor/agent/director/gate.mjs +802 -0
  76. package/vendor/agent/director/limits.json +16 -0
  77. package/vendor/agent/director/methods.mjs +457 -0
  78. package/vendor/agent/director/policy.json +33 -0
  79. package/vendor/agent/director/prompts.mjs +244 -0
  80. package/vendor/agent/director/reading.mjs +341 -0
  81. package/vendor/agent/director/router.mjs +184 -0
  82. package/vendor/agent/director/seams.mjs +243 -0
  83. package/vendor/agent/director/session.mjs +643 -0
  84. package/vendor/agent/director/store.mjs +119 -0
  85. package/vendor/agent/director/taskGate.mjs +110 -0
  86. package/vendor/agent/director/teamLedger.mjs +118 -0
  87. package/vendor/agent/director/triage.mjs +63 -0
  88. package/vendor/agent/downloads.cjs +363 -0
  89. package/vendor/agent/flow.mjs +78 -0
  90. package/vendor/agent/guard.mjs +613 -0
  91. package/vendor/agent/host.mjs +1736 -0
  92. package/vendor/agent/language.mjs +58 -0
  93. package/vendor/agent/ledger.mjs +360 -0
  94. package/vendor/agent/locale.cjs +186 -0
  95. package/vendor/agent/loop/capacity.mjs +74 -0
  96. package/vendor/agent/loop/compose.mjs +127 -0
  97. package/vendor/agent/loop/gate.mjs +205 -0
  98. package/vendor/agent/loop/judge.mjs +189 -0
  99. package/vendor/agent/loop/levels.mjs +273 -0
  100. package/vendor/agent/loop/state.mjs +358 -0
  101. package/vendor/agent/notes.cjs +278 -0
  102. package/vendor/agent/pace.mjs +258 -0
  103. package/vendor/agent/panels.mjs +431 -0
  104. package/vendor/agent/prompt.mjs +342 -0
  105. package/vendor/agent/reference/board.mjs +410 -0
  106. package/vendor/agent/reference/compose.mjs +364 -0
  107. package/vendor/agent/reference/decode.mjs +56 -0
  108. package/vendor/agent/reference/facts.mjs +1874 -0
  109. package/vendor/agent/reference/png.mjs +107 -0
  110. package/vendor/agent/reference/records.mjs +63 -0
  111. package/vendor/agent/reference/web.mjs +252 -0
  112. package/vendor/agent/roles/artist.mjs +69 -0
  113. package/vendor/agent/roles/builder.mjs +79 -0
  114. package/vendor/agent/roles/checks.mjs +223 -0
  115. package/vendor/agent/roles/common.mjs +188 -0
  116. package/vendor/agent/roles/critic.mjs +171 -0
  117. package/vendor/agent/roles/index.mjs +39 -0
  118. package/vendor/agent/roles/modeller.mjs +142 -0
  119. package/vendor/agent/roles/qa.mjs +58 -0
  120. package/vendor/agent/roles/rubrics.mjs +79 -0
  121. package/vendor/agent/roles/sound.mjs +69 -0
  122. package/vendor/agent/rounds.cjs +333 -0
  123. package/vendor/agent/shared.mjs +284 -0
  124. package/vendor/agent/stopGate.mjs +574 -0
  125. package/vendor/agent/subagents.mjs +129 -0
  126. package/vendor/agent/tools/assets.mjs +499 -0
  127. package/vendor/agent/tools/blender.mjs +754 -0
  128. package/vendor/agent/tools/cards.mjs +479 -0
  129. package/vendor/agent/tools/choices.mjs +479 -0
  130. package/vendor/agent/tools/compose.mjs +74 -0
  131. package/vendor/agent/tools/environment.mjs +488 -0
  132. package/vendor/agent/tools/eyes.mjs +733 -0
  133. package/vendor/agent/tools/generate.mjs +338 -0
  134. package/vendor/agent/tools/guide.mjs +46 -0
  135. package/vendor/agent/tools/harness.mjs +193 -0
  136. package/vendor/agent/tools/image.mjs +643 -0
  137. package/vendor/agent/tools/index.mjs +162 -0
  138. package/vendor/agent/tools/look.mjs +578 -0
  139. package/vendor/agent/tools/loop.mjs +858 -0
  140. package/vendor/agent/tools/others.mjs +123 -0
  141. package/vendor/agent/tools/pictures.mjs +535 -0
  142. package/vendor/agent/tools/reference.mjs +366 -0
  143. package/vendor/agent/transcripts.mjs +118 -0
  144. package/vendor/agent-loop.mjs +602 -0
  145. package/vendor/agent-plugin/.claude-plugin/plugin.json +5 -0
  146. package/vendor/agent-plugin/skills/art-direction/SKILL.md +82 -0
  147. package/vendor/agent-plugin/skills/asset-legality/SKILL.md +101 -0
  148. package/vendor/agent-plugin/skills/asset-sources/SKILL.md +111 -0
  149. package/vendor/agent-plugin/skills/blender-modeling/SKILL.md +97 -0
  150. package/vendor/agent-plugin/skills/look-dev/SKILL.md +104 -0
  151. package/vendor/agent-plugin/skills/playtesting/SKILL.md +74 -0
  152. package/vendor/agent-plugin/skills/reference-loop/SKILL.md +102 -0
  153. package/vendor/agent-plugin/skills/sound-design/SKILL.md +60 -0
  154. package/vendor/agent-plugin/skills/style/SKILL.md +35 -0
  155. package/vendor/agent-plugin/skills/tsl-r185/SKILL.md +107 -0
  156. package/vendor/auth.cjs +570 -0
  157. package/vendor/build.cjs +953 -0
  158. package/vendor/catalog.json +455 -0
  159. package/vendor/connectors.cjs +688 -0
  160. package/vendor/eyes.mjs +733 -0
  161. package/vendor/gameExports.json +1390 -0
  162. package/vendor/git.cjs +222 -0
  163. package/vendor/models.json +57 -0
  164. package/vendor/publish.cjs +1018 -0
  165. package/vendor/refBoard.cjs +301 -0
  166. package/vendor/schema/dist/assets.d.ts +335 -0
  167. package/vendor/schema/dist/assets.js +439 -0
  168. package/vendor/schema/dist/brief.d.ts +357 -0
  169. package/vendor/schema/dist/brief.js +302 -0
  170. package/vendor/schema/dist/common.d.ts +9 -0
  171. package/vendor/schema/dist/common.js +13 -0
  172. package/vendor/schema/dist/config.d.ts +406 -0
  173. package/vendor/schema/dist/config.js +355 -0
  174. package/vendor/schema/dist/contract.d.ts +599 -0
  175. package/vendor/schema/dist/contract.js +1146 -0
  176. package/vendor/schema/dist/index.d.ts +14 -0
  177. package/vendor/schema/dist/index.js +21 -0
  178. package/vendor/schema/dist/look.d.ts +463 -0
  179. package/vendor/schema/dist/look.js +681 -0
  180. package/vendor/schema/dist/lookStats.d.ts +230 -0
  181. package/vendor/schema/dist/lookStats.js +768 -0
  182. package/vendor/schema/dist/pilot.d.ts +32 -0
  183. package/vendor/schema/dist/pilot.js +44 -0
  184. package/vendor/schema/dist/publish.d.ts +214 -0
  185. package/vendor/schema/dist/publish.js +291 -0
  186. package/vendor/schema/dist/scene.d.ts +75 -0
  187. package/vendor/schema/dist/scene.js +120 -0
  188. package/vendor/schema/dist/skyLight.d.ts +141 -0
  189. package/vendor/schema/dist/skyLight.js +436 -0
  190. package/vendor/schema/dist/style.d.ts +131 -0
  191. package/vendor/schema/dist/style.js +155 -0
  192. package/vendor/schema/package.json +1 -0
  193. package/vendor/selectionPrompt.mjs +51 -0
  194. package/vendor/useTools.mjs +195 -0
  195. package/vendor/userMessage.mjs +26 -0
  196. package/web-dist/assets/GaussianSplatUtils-CB77cP9U.js +1 -0
  197. package/web-dist/assets/KTX2Loader-6OIWUF7s.js +2 -0
  198. package/web-dist/assets/SPZLoader-zYj0Alfv.js +1 -0
  199. package/web-dist/assets/gaussianSplat-CJTI1sEh.js +1 -0
  200. package/web-dist/assets/index-CcjyyQco.css +1 -0
  201. package/web-dist/assets/index-DMyJcoZJ.js +693 -0
  202. package/web-dist/assets/meshopt_decoder.module-BvwdjiD3.js +1 -0
  203. package/web-dist/assets/zstddec.module-CfFeQW7d.js +1 -0
  204. package/web-dist/index.html +34 -0
@@ -0,0 +1,161 @@
1
+ // World Labs' Marble: a whole explorable world from a sentence, a picture or a
2
+ // panorama, as Gaussian splats with a collider and the panorama it was made
3
+ // from, through the World API with the creator's key (connector worldlabs,
4
+ // env WORLDLABS_API_KEY, header WLT-Api-Key). The shapes below are the
5
+ // documented API (docs.worldlabs.ai/api and its OpenAPI file, read
6
+ // 2026-10-24), NOT exercised against the live API from here: no paid call is
7
+ // ever made by a check, and the checks use a fake. Verify one real world
8
+ // before release.
9
+ //
10
+ // POST /marble/v1/worlds:generate { display_name, model, world_prompt, seed }
11
+ // -> an operation { operation_id, done, error, metadata, response }
12
+ // GET /marble/v1/operations/{id} -> done: false while it runs (about five
13
+ // minutes), then response: the World, cost: { total_credits }
14
+ // World.assets: splats.spz_urls { 100k, 500k, full_res },
15
+ // splats.semantics_metadata { metric_scale_factor, ground_plane_offset },
16
+ // mesh.collider_mesh_url (GLB), imagery.pano_url, thumbnail_url, caption
17
+ // GET /marble/v1/credits -> { remaining_credits }: free, asked before
18
+ // anything is spent, since a request that starts on a low balance can
19
+ // run the account into an overage billed weeks later
20
+ //
21
+ // What it makes is in OpenCV's frame (y down) in arbitrary units; the scale
22
+ // to metres and the height of the ground come with it (semantics_metadata),
23
+ // and asset_import bakes all three into the splat and the collider alike.
24
+ // API usage is a paid account's, whose output the creator owns (terms of
25
+ // service 3.3(b), updated 2026-01-21), with attribution to World Labs on
26
+ // their reasonable request (3.7(b)): the credit line goes in every time.
27
+ //
28
+ // Prices (docs.worldlabs.ai/api/pricing, read 2026-10-24): 1,250 credits to
29
+ // the dollar, at least $5 bought at a time; a world 1,500 credits (marble-1.1,
30
+ // 1.0), 150 (the draft), 1,500 to 3,000 (1.1 Plus, by the world's size); a
31
+ // panorama made first from text or a picture 80 more, none when the picture
32
+ // is a panorama already. The collider and the SPZ files come with it.
33
+
34
+ import { fetchJson } from '../net.mjs'
35
+
36
+ const API = 'https://api.worldlabs.ai/marble/v1'
37
+
38
+ const STATUS_WORDS = { IN_PROGRESS: 'running', PENDING: 'pending', QUEUED: 'pending', SUCCEEDED: 'succeeded', FAILED: 'failed', CANCELLED: 'failed', CANCELED: 'failed' }
39
+
40
+ export const worldlabs = {
41
+ id: 'worldlabs',
42
+ label: 'World Labs Marble',
43
+ connector: 'worldlabs',
44
+ envVar: 'WORLDLABS_API_KEY',
45
+ keyUrl: 'https://platform.worldlabs.ai/api-keys',
46
+ billingUrl: 'https://platform.worldlabs.ai/billing',
47
+ hosts: ['api.worldlabs.ai'],
48
+ // Where the files are: signed links the operation hands back (Google Cloud
49
+ // Storage, World Labs' own CDN). Read from the docs; a host not listed is
50
+ // refused, and the answer names it, so the first real world says if one is missing.
51
+ fileHosts: ['api.worldlabs.ai', '*.worldlabs.ai', 'storage.googleapis.com', '*.storage.googleapis.com', 'wlt-ai-cdn.art', '*.wlt-ai-cdn.art'],
52
+ models: ['marble-1.1', 'marble-1.1-plus', 'marble-1.0', 'marble-1.0-draft'],
53
+ inputs: ['text', 'image', 'pano'],
54
+ resolutions: ['100k', '500k', 'full_res'],
55
+
56
+ defaultModel() {
57
+ return 'marble-1.1'
58
+ },
59
+
60
+ licenseOf(model) {
61
+ return {
62
+ spdx: 'LicenseRef-Generated-Owned',
63
+ url: 'https://worldlabs.ai/terms-of-service',
64
+ credit: `World generated with Marble by World Labs (${model}), worldlabs.ai`,
65
+ }
66
+ },
67
+
68
+ /** Credits and dollars, lower and upper, for one world: the world itself
69
+ * and, unless the picture is a panorama already, the panorama made first. */
70
+ price(input, model, prices) {
71
+ const p = prices?.worldlabs
72
+ const world = p?.world?.[model]
73
+ const pano = p?.pano?.[input]
74
+ const rate = p?.creditUsd
75
+ if (!Array.isArray(world) || !Number.isFinite(pano) || !Number.isFinite(rate)) return null
76
+ const low = world[0] + pano
77
+ const high = world[1] + pano
78
+ return { credits: [low, high], low: low * rate, high: high * rate, unit: `${low === high ? high : `${low} to ${high}`} credits` }
79
+ },
80
+
81
+ /** The account's API credits. */
82
+ async credits({ key, fetch, ua }) {
83
+ const r = await fetchJson(`${API}/credits`, { fetch, hosts: worldlabs.hosts, ua, headers: { 'WLT-Api-Key': key } })
84
+ const n = Number(r?.remaining_credits)
85
+ if (!Number.isFinite(n)) throw new Error('World Labs did not say how many credits are left.')
86
+ return n
87
+ },
88
+
89
+ /** Starts one world; answers its operation id. */
90
+ async submit({ input, model, prompt, image, name, seed, key, fetch, ua }) {
91
+ const world_prompt =
92
+ input === 'text'
93
+ ? { type: 'text', text_prompt: prompt }
94
+ : {
95
+ type: 'image',
96
+ image_prompt: { source: 'data_base64', data_base64: image.data, extension: image.extension },
97
+ ...(prompt ? { text_prompt: prompt } : {}),
98
+ is_pano: input === 'pano' ? true : 'auto',
99
+ }
100
+ const body = { model, world_prompt, ...(name ? { display_name: name.slice(0, 64) } : {}), ...(Number.isInteger(seed) ? { seed } : {}) }
101
+ const r = await fetchJson(`${API}/worlds:generate`, {
102
+ fetch,
103
+ hosts: worldlabs.hosts,
104
+ ua,
105
+ method: 'POST',
106
+ headers: { 'WLT-Api-Key': key, 'content-type': 'application/json' },
107
+ body: JSON.stringify(body),
108
+ })
109
+ if (!r?.operation_id) throw new Error('World Labs did not give an operation id.')
110
+ return { vendorId: String(r.operation_id), worldId: r?.metadata?.world_id ?? null }
111
+ },
112
+
113
+ /** Where an operation is now, and its world once done. */
114
+ async poll(candidate, { key, fetch, ua }) {
115
+ const r = await fetchJson(`${API}/operations/${encodeURIComponent(candidate.vendorId)}`, { fetch, hosts: worldlabs.hosts, ua, headers: { 'WLT-Api-Key': key } })
116
+ const word = String(r?.metadata?.progress?.status ?? '').toUpperCase()
117
+ const worldId = r?.metadata?.world_id ?? r?.response?.world_id ?? r?.response?.id ?? null
118
+ if (r?.done && r?.error) return { status: 'failed', error: String(r.error.message ?? r.error.code ?? 'World Labs reported a failure').slice(0, 300), worldId }
119
+ if (!r?.done) {
120
+ const pct = Number(r?.metadata?.progress?.percent ?? r?.metadata?.progress?.percentage)
121
+ return { status: STATUS_WORDS[word] === 'pending' ? 'pending' : 'running', progress: Number.isFinite(pct) ? Math.round(pct) : undefined, note: r?.metadata?.progress?.description ? String(r.metadata.progress.description).slice(0, 160) : undefined, worldId }
122
+ }
123
+ if (!r?.response?.assets) return { status: 'failed', error: 'World Labs finished but gave no files.', worldId }
124
+ const credits = Number(r?.cost?.total_credits)
125
+ return { status: 'succeeded', progress: 100, world: r.response, worldId, ...(Number.isFinite(credits) ? { credits } : {}) }
126
+ },
127
+
128
+ /**
129
+ * What to download from a finished world: the splat at the resolution
130
+ * asked for (or the nearest it has), its collider, its panorama and its
131
+ * thumbnail, with the frame asset_import bakes in. { files, frame, meta } or
132
+ * { error }.
133
+ */
134
+ files(world, { resolution = '500k' } = {}) {
135
+ const a = world?.assets ?? {}
136
+ const urls = a.splats?.spz_urls && typeof a.splats.spz_urls === 'object' ? a.splats.spz_urls : {}
137
+ const order = [resolution, '500k', 'full_res', '100k'].filter((r, i, all) => all.indexOf(r) === i)
138
+ const picked = order.find((r) => typeof urls[r] === 'string' && urls[r])
139
+ if (!picked) return { error: 'World Labs finished but gave no splat (no spz_urls).' }
140
+ const files = [{ name: 'world.spz', url: urls[picked], role: 'splat' }]
141
+ if (typeof a.mesh?.collider_mesh_url === 'string' && a.mesh.collider_mesh_url) files.push({ name: 'collider.glb', url: a.mesh.collider_mesh_url, role: 'collider' })
142
+ if (typeof a.imagery?.pano_url === 'string' && a.imagery.pano_url) files.push({ name: 'pano', url: a.imagery.pano_url, role: 'pano' })
143
+ if (typeof a.thumbnail_url === 'string' && a.thumbnail_url) files.push({ name: 'thumbnail', url: a.thumbnail_url, role: 'thumbnail' })
144
+ const sem = a.splats?.semantics_metadata ?? {}
145
+ const scale = Number(sem.metric_scale_factor)
146
+ const ground = Number(sem.ground_plane_offset)
147
+ return {
148
+ resolution: picked,
149
+ files,
150
+ frame: { orientation: 'y-down', ...(Number.isFinite(scale) && scale > 0 ? { scale } : {}), ...(Number.isFinite(ground) ? { ground } : {}) },
151
+ meta: {
152
+ world_id: world.world_id ?? world.id ?? null,
153
+ marble_url: world.world_marble_url ?? null,
154
+ caption: typeof a.caption === 'string' ? a.caption.slice(0, 600) : null,
155
+ resolution: picked,
156
+ // 1 means World Labs could not tell the scale: the world is in its own units.
157
+ scale_inferred: Number.isFinite(scale) && scale !== 1,
158
+ },
159
+ }
160
+ },
161
+ }
@@ -0,0 +1,168 @@
1
+ // asset_fetch: one library item into quarantine, never into the project.
2
+ //
3
+ // .nowhere/quarantine/<qid>/ every file of the item, in its own folder
4
+ // .nowhere/quarantine/<qid>/source.json where it came from and under which licence
5
+ // .nowhere/quarantine/index.json the quarantine manifest (downloads.cjs),
6
+ // one entry with `source` and `files`
7
+ //
8
+ // qid is <provider>-<slug>-<yyyymmdd-hhmmss>. Each file streams under a
9
+ // running byte cap for its kind, is checked against the size and md5 the
10
+ // source gave, and then against what its name says it is (checkFile). A zip
11
+ // is unpacked in place by unzip.mjs. asset_import is what reads it next.
12
+
13
+ import fsp from 'node:fs/promises'
14
+ import { createRequire } from 'node:module'
15
+ import path from 'node:path'
16
+ import { download, mbOf, NetError } from './net.mjs'
17
+ import { safeUnzip } from './unzip.mjs'
18
+ import { today } from './ledger.mjs'
19
+
20
+ const require = createRequire(import.meta.url)
21
+ const { QUARANTINE, QUARANTINE_LIMITS, checkFile, readManifest, writeManifest } = require('../downloads.cjs')
22
+
23
+ /** The byte cap for one item, by what it is. */
24
+ export function capFor(kind) {
25
+ if (kind === 'model') return QUARANTINE_LIMITS.model
26
+ if (kind === 'hdri') return QUARANTINE_LIMITS.hdri
27
+ return QUARANTINE_LIMITS.material
28
+ }
29
+
30
+ function stamp(now) {
31
+ const d = new Date(now)
32
+ const p = (n) => String(n).padStart(2, '0')
33
+ return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`
34
+ }
35
+
36
+ export const slugOf = (s) =>
37
+ String(s ?? '')
38
+ .toLowerCase()
39
+ .replace(/[^a-z0-9]+/g, '-')
40
+ .replace(/^-+|-+$/g, '')
41
+ .slice(0, 60) || 'item'
42
+
43
+ /** A path inside the item's folder, or null if the source named one that
44
+ * would leave it. */
45
+ function inside(dir, rel) {
46
+ const clean = String(rel ?? '').replace(/\\/g, '/')
47
+ if (!clean || clean.startsWith('/') || /^[A-Za-z]:/.test(clean) || clean.split('/').some((s) => s === '..' || s === '')) return null
48
+ return path.join(dir, ...clean.split('/'))
49
+ }
50
+
51
+ // Writes to the manifest from fetches that finish together go one after
52
+ // another, as downloads.cjs does for the browser's.
53
+ let manifestWrites = Promise.resolve()
54
+ export function addToManifest(root, entry) {
55
+ manifestWrites = manifestWrites
56
+ .then(() => {
57
+ const doc = readManifest(root)
58
+ doc.files = [...doc.files.filter((f) => f.id !== entry.id), entry]
59
+ writeManifest(root, doc)
60
+ })
61
+ .catch(() => {})
62
+ return manifestWrites
63
+ }
64
+
65
+ /**
66
+ * Downloads an item's files (from an adapter's files()) into a new
67
+ * quarantine folder.
68
+ *
69
+ * @returns {Promise<{ ok: true, qid: string, dir: string, files: object[], bytes: number, skipped: string[], rejected: object[] }
70
+ * | { ok: false, error: string }>}
71
+ */
72
+ export async function fetchIntoQuarantine({ projectRoot, adapter, id, item, fetch, ua, key, now = Date.now(), headers }) {
73
+ const at = now
74
+ const qid = `${adapter.id}-${slugOf(id)}-${stamp(at)}`
75
+ const dir = path.join(projectRoot, QUARANTINE, qid)
76
+ const cap = capFor(item.kind)
77
+ const planned = item.files.reduce((n, f) => n + (Number(f.bytes) || 0), 0)
78
+ if (planned > cap) {
79
+ return { ok: false, error: `${item.title} comes to ${mbOf(planned)} MB at this resolution, over the ${mbOf(cap)} MB a ${item.kind} download may be. Ask for a lower resolution.` }
80
+ }
81
+ await fsp.mkdir(dir, { recursive: true })
82
+ const landed = []
83
+ const skipped = []
84
+ const rejected = []
85
+ let used = 0
86
+ const fail = async (error) => {
87
+ await fsp.rm(dir, { recursive: true, force: true })
88
+ return { ok: false, error }
89
+ }
90
+ try {
91
+ for (const f of item.files) {
92
+ const target = inside(dir, f.path)
93
+ if (!target) return await fail(`${adapter.label} named a file that would land outside its folder (${String(f.path).slice(0, 80)}), so nothing was kept.`)
94
+ const got = await download(f.url, target, {
95
+ fetch,
96
+ hosts: adapter.hosts,
97
+ ua,
98
+ // The cap is for the whole item, so each file may use what is left.
99
+ maxBytes: cap - used,
100
+ size: f.sizeExact === false ? undefined : f.bytes,
101
+ md5: f.md5,
102
+ headers: adapter.id === 'polypizza' && key ? { 'x-auth-token': key } : headers,
103
+ })
104
+ used += got.bytes
105
+ const rel = path.relative(dir, target).split(path.sep).join('/')
106
+ // A glTF's buffers are not a type checkFile knows; they are judged as
107
+ // what they are, the binary half of the .gltf beside them.
108
+ if (/\.bin$/i.test(rel)) {
109
+ const head = (await fsp.readFile(target)).subarray(0, 512).toString('latin1').toLowerCase()
110
+ if (head.includes('<html') || head.includes('<!doctype')) return await fail(`${rel} is a web page, not a glTF buffer, so nothing was kept.`)
111
+ landed.push({ path: rel, bytes: got.bytes, sha256: got.sha256, ...(f.md5 ? { md5: f.md5 } : {}), host: got.host, kind: 'buffer' })
112
+ continue
113
+ }
114
+ const check = await checkFile(target)
115
+ if (!check.ok) return await fail(`${rel} failed its check (${check.problem}), so nothing was kept.`)
116
+ if (check.kind === 'archive') {
117
+ const unpacked = await safeUnzip(target, path.join(dir, path.basename(rel, path.extname(rel))))
118
+ await fsp.rm(target, { force: true })
119
+ if (!unpacked.ok) return await fail(unpacked.error)
120
+ for (const u of unpacked.files) {
121
+ landed.push({ path: `${path.basename(rel, path.extname(rel))}/${u.path}`, bytes: u.bytes, kind: u.kind, host: got.host, from: rel })
122
+ }
123
+ skipped.push(...unpacked.skipped)
124
+ rejected.push(...unpacked.rejected)
125
+ continue
126
+ }
127
+ landed.push({ path: rel, bytes: got.bytes, sha256: got.sha256, ...(f.md5 ? { md5: f.md5 } : {}), host: got.host, kind: check.kind, ...(f.role ? { role: f.role } : {}) })
128
+ }
129
+ } catch (err) {
130
+ const why = err instanceof NetError ? err.message : `the download failed (${err?.message ?? err})`
131
+ return await fail(`${item.title}: ${why}`)
132
+ }
133
+
134
+ const host = landed.find((f) => f.host)?.host ?? null
135
+ const source = {
136
+ provider: adapter.id,
137
+ id,
138
+ kind: item.kind,
139
+ title: item.title,
140
+ author: item.author,
141
+ url: item.url,
142
+ host,
143
+ license: item.license,
144
+ fetchedAt: today(new Date(at)),
145
+ format: item.format,
146
+ files: landed.map(({ host: _h, ...rest }) => rest),
147
+ meta: item.meta ?? {},
148
+ }
149
+ await fsp.writeFile(path.join(dir, 'source.json'), `${JSON.stringify(source, null, 2)}\n`)
150
+ const bytes = landed.reduce((n, f) => n + f.bytes, 0)
151
+ await addToManifest(projectRoot, {
152
+ id: qid,
153
+ file: qid,
154
+ name: item.title,
155
+ url: item.url,
156
+ referrer: '',
157
+ host: host ?? '',
158
+ bytes,
159
+ sha256: '',
160
+ kind: item.kind,
161
+ ok: true,
162
+ problem: null,
163
+ at: new Date(at).toISOString(),
164
+ source: { provider: adapter.id, id, title: item.title, author: item.author, url: item.url, license: item.license },
165
+ files: source.files,
166
+ })
167
+ return { ok: true, qid, dir, files: landed, bytes, skipped, rejected, source }
168
+ }
@@ -0,0 +1,13 @@
1
+ // How much of one box's footprint another covers, on the ground (x, z): the
2
+ // one measure asset_import holds a splat's collider to (it must cover at
3
+ // least half of the splat's core) and the mood board says of it. Its own
4
+ // small module, so main reads it without loading the importer.
5
+
6
+ /** How much of the smaller of two boxes' footprints (x, z) the other covers.
7
+ * Each box is { min: [x, y, z], max: [x, y, z] }. */
8
+ export function footprintOverlap(a, b) {
9
+ const w = Math.max(0, Math.min(a.max[0], b.max[0]) - Math.max(a.min[0], b.min[0]))
10
+ const d = Math.max(0, Math.min(a.max[2], b.max[2]) - Math.max(a.min[2], b.min[2]))
11
+ const area = (x) => Math.max(1e-9, (x.max[0] - x.min[0]) * (x.max[2] - x.min[2]))
12
+ return (w * d) / Math.min(area(a), area(b))
13
+ }