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,431 @@
1
+ // The studio's library calls, as the agent host serves them: the asset
2
+ // libraries (search Poly Haven and ambientCG, then import one hit) and the
3
+ // measuring half of the reference board. The chat's choice cards
4
+ // (tools/choices.mjs) search, measure and import through the same functions,
5
+ // exported here, with the agent's own context. The creator's clicks go through the
6
+ // same code as the agent's calls: asset_search for the hits, asset_fetch and
7
+ // asset_import for an import (so the quarantine, the licence checks, the
8
+ // ledger, CREDITS.md and the brief's sources are the agent's), asset_inspect
9
+ // for the report shown after it, and nw_reference's measurements for a
10
+ // reference. Main asks the creator about the web first, on the agent's own
11
+ // consent card, and hands the answer over with each call (main.cjs
12
+ // registerPanelIpc); nothing here asks anything.
13
+ //
14
+ // main --{type:'panel', id, call, args, projectRoot, web}--> agent host --> handle()
15
+ // main --{type:'panel-cancel', id}--> agent host --> cancel() (an import main gave up on)
16
+ // agent host --{type:'panel-progress', id, step}--> main (an import's steps)
17
+ // agent host --{type:'panel-result', id, result}--> main
18
+ //
19
+ // It runs in the agent host, not in main, because an import decodes, scores
20
+ // and encodes pictures for seconds at a time, and main must stay free for the
21
+ // window. Electron-free and fetch from deps, so the checks drive all of it
22
+ // with fakes (scripts/panels-check.mjs).
23
+
24
+ import { createHash } from 'node:crypto'
25
+ import fs from 'node:fs'
26
+ import path from 'node:path'
27
+ import { ASSET_KINDS, DECODERS_LIVE, TEXTURE_BUDGETS } from '../schema/dist/index.js'
28
+ import { readBrief } from './assets/brief.mjs'
29
+ import { materialFiles, materialMaps } from './assets/inspect.mjs'
30
+ import { readLedgerState } from './assets/ledger.mjs'
31
+ import { fetchOf, fetchThumbnail, userAgentFor } from './assets/net.mjs'
32
+ import { SOURCES } from './assets/sources/index.mjs'
33
+ import { decodeImage } from './reference/decode.mjs'
34
+ import { factsSummary, referenceFacts } from './reference/facts.mjs'
35
+ import { runDef } from './tools/index.mjs'
36
+ import { densityOf, makeAssetTools } from './tools/assets.mjs'
37
+ import { readReference } from './tools/look.mjs'
38
+
39
+ /** The libraries the browser searches: the scanned CC0 ones (SPACE_FIRST.md
40
+ * 2, the order of sources). Poly Pizza needs a key and is the agent's. */
41
+ export const BROWSER_SOURCES = ['polyhaven', 'ambientcg']
42
+ export const REFS_PREFIX = '.nowhere/refs/'
43
+ /** What one source shows of a search: Poly Haven gives sizes for its first
44
+ * eight hits only, so a source is asked for no more than that. */
45
+ const PER_SOURCE = 8
46
+ const NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/
47
+ const CLASSES = new Set(['small', 'prop', 'large', 'hero', 'character', 'building', 'terrain'])
48
+ const RESOLUTIONS = new Set(['1k', '2k', '4k'])
49
+ /** The longest side of a download at each resolution, px. */
50
+ const RES_PX = { '1k': 1024, '2k': 2048, '4k': 4096 }
51
+ const KINDS = new Set(['model', 'material', 'hdri'])
52
+ /** Files an import may write beside its own, which the stop gate is told
53
+ * about only when the import did change them. */
54
+ const SHARED = ['assets.lock.json', 'CREDITS.md', 'brief.json']
55
+
56
+ /** What asset_import keeps of a download, for the browser to say before
57
+ * anything is fetched: a material's maps at the resolution fetched (the
58
+ * import is asked for exactly that, importHit), each at most mapMb while
59
+ * maps are written as pictures (KTX2 is weighed only once it is encoded);
60
+ * a sky at most hdriMb; a model's textures at its class's longest side. */
61
+ export const LIMITS = Object.freeze({
62
+ mapMb: DECODERS_LIVE.ktx2 ? null : ASSET_KINDS.texture.fileMb,
63
+ hdriMb: ASSET_KINDS.hdri.fileMb,
64
+ modelTexture: TEXTURE_BUDGETS,
65
+ })
66
+
67
+ const isObj = (v) => !!v && typeof v === 'object' && !Array.isArray(v)
68
+ const fail = (error, more = {}) => ({ ok: false, error, ...more })
69
+ const dataUrl = (img) => (img ? `data:${img.mime};base64,${img.data}` : null)
70
+
71
+ /** The name an import takes by default: the library's id, as assets/ names go. */
72
+ export function nameFor(id) {
73
+ const bare = String(id ?? '').slice(String(id ?? '').indexOf(':') + 1)
74
+ const clean = bare.toLowerCase().replace(/[^a-z0-9_-]+/g, '_').replace(/^[_-]+/, '').slice(0, 64)
75
+ return clean || 'asset'
76
+ }
77
+
78
+ /** A hit as the browser shows it: the card asset_search made, its density by
79
+ * resolution, and whether this space already has it. */
80
+ export function hitOf(card, imported) {
81
+ const id = `${card.provider}:${card.id}`
82
+ return {
83
+ id,
84
+ provider: card.provider,
85
+ title: card.title,
86
+ kind: card.kind,
87
+ author: card.author,
88
+ license: card.license?.spdx ?? 'unknown',
89
+ url: card.url,
90
+ thumbnail: card.thumbnail ?? null,
91
+ preview: card.preview ?? null,
92
+ tags: card.tags ?? [],
93
+ maps: card.maps ?? [],
94
+ ...(card.dims_m ? { dims_m: card.dims_m } : {}),
95
+ ...(card.covers_m ? { covers_m: card.covers_m } : {}),
96
+ ...(card.triangles ? { triangles: card.triangles } : {}),
97
+ sizes: (card.sizes ?? []).filter((s) => RESOLUTIONS.has(s.res)).map((s) => ({ res: s.res, label: s.label, bytes: Number(s.bytes) || 0, ...(Number(s.largest) > 0 ? { largest: Number(s.largest) } : {}) })),
98
+ density: densityOf(card) ?? {},
99
+ name: nameFor(id),
100
+ imported: imported.get(id) ?? [],
101
+ }
102
+ }
103
+
104
+ /** Which ledger files came from each library item: "polyhaven:rock_wall_10" ->
105
+ * ["assets/rock_wall_10_color.jpg", ...]. */
106
+ export async function importedBySource(projectRoot) {
107
+ const out = new Map()
108
+ const state = await readLedgerState(projectRoot)
109
+ for (const [file, e] of Object.entries(state.lock.files)) {
110
+ if (!e.source?.provider || !e.source.id) continue
111
+ const key = `${e.source.provider}:${e.source.id}`
112
+ out.set(key, [...(out.get(key) ?? []), file])
113
+ }
114
+ return out
115
+ }
116
+
117
+ /** What the brief says a search should be held to, for the browser to pick a
118
+ * resolution by: its libraries, the density a surface without its own target
119
+ * is held to, and each material's own target and pick. */
120
+ export async function briefFacts(projectRoot) {
121
+ const r = await readBrief(projectRoot)
122
+ if (r.status !== 'ok') return { status: r.status, ...(r.error ? { error: r.error } : {}) }
123
+ const b = r.brief
124
+ return {
125
+ status: 'ok',
126
+ libraries: b.sources?.libraries ?? [],
127
+ texelDensity: b.budget.texelDensity ?? null,
128
+ materials: b.materials.map((m) => ({ name: m.name, texelDensity: m.texelDensity, ...(m.source ? { source: m.source } : {}) })),
129
+ }
130
+ }
131
+
132
+ /** The ledger's files, with what tells a write apart: its sha256. */
133
+ async function ledgerSnapshot(projectRoot) {
134
+ const state = await readLedgerState(projectRoot)
135
+ return Object.fromEntries(Object.entries(state.lock.files).map(([file, e]) => [file, e]))
136
+ }
137
+
138
+ /** The files an import of `as` lands on: the model or the sky, or every map
139
+ * the material's name holds. Only these are the import's: the ledger may
140
+ * gain the agent's own entries while it runs. */
141
+ function targetFiles(projectRoot, kind, name, as) {
142
+ if (kind === 'material') return materialFiles(projectRoot, name)
143
+ return fs.existsSync(path.join(projectRoot, ...as.split('/'))) ? [as] : []
144
+ }
145
+
146
+ /** Size and modified time of the files beside an import's own, by name. */
147
+ function sharedStats(projectRoot) {
148
+ return Object.fromEntries(
149
+ SHARED.map((f) => {
150
+ try {
151
+ const st = fs.statSync(path.join(projectRoot, f))
152
+ return [f, `${st.size}:${st.mtimeMs}`]
153
+ } catch {
154
+ return [f, null]
155
+ }
156
+ }),
157
+ )
158
+ }
159
+
160
+ /** What an import would land on that is there already: the file, or null.
161
+ * A sky's format is the library's (Poly Haven's .hdr, ambientCG's .exr),
162
+ * so before it is fetched either one counts. */
163
+ function taken(projectRoot, kind, name, skyExt) {
164
+ if (kind === 'material') return Object.keys(materialMaps(projectRoot, name)).length > 0 ? `assets/${name}` : null
165
+ const exts = kind === 'model' ? ['.glb'] : skyExt ? [skyExt] : ['.hdr', '.exr']
166
+ const hit = exts.find((ext) => fs.existsSync(path.join(projectRoot, 'assets', `${name}${ext}`)))
167
+ return hit ? `assets/${name}${hit}` : null
168
+ }
169
+
170
+ /** The measurements a reference board shows: nw_reference's facts, cut to
171
+ * what the board reads and named as the brief's references name them
172
+ * (BriefReference.measured), so a picture reads the same on the board and
173
+ * in brief.json. */
174
+ export function boardMeasure(facts) {
175
+ const r1 = (v) => Math.round(v * 10) / 10
176
+ const r3 = (v) => Math.round(v * 1000) / 1000
177
+ return {
178
+ size: facts.size,
179
+ palette: facts.palette.map((p) => ({ hex: p.hex, share: r3(p.share) })),
180
+ accents: facts.accents.map((a) => ({ hex: a.hex, share: r3(a.share) })),
181
+ value: { key: facts.value.key, p5: r1(facts.value.p5), p50: r1(facts.value.p50), p95: r1(facts.value.p95) },
182
+ // The brief's names: valueKey is mean lightness 0..1, horizonY the row.
183
+ valueKey: r3(facts.value.meanL / 100),
184
+ contrast: r1(facts.contrast.stdL),
185
+ saturation: r1(facts.saturation.meanChroma),
186
+ warmth: r1(facts.temperature.warmth),
187
+ tint: r1(facts.temperature.tint),
188
+ haze: { score: facts.haze.score, direction: facts.haze.direction },
189
+ horizon: { y: facts.horizon.y, tiltDeg: facts.horizon.tiltDeg, confidence: facts.horizon.confidence },
190
+ camera: { guess: facts.camera.guess, pitchDeg: facts.camera.pitchDeg, confidence: facts.camera.confidence },
191
+ lettering: facts.text?.boxes?.length ?? 0,
192
+ border: facts.frame?.suggestedCrop ?? null,
193
+ summary: factsSummary(facts),
194
+ }
195
+ }
196
+
197
+ /** A board measurement under the brief's names (BriefReference.measured), for
198
+ * a citation: the palette, the value key, contrast, saturation, warmth, the
199
+ * haze score and the horizon's row when one was found with confidence. */
200
+ export function briefMeasured(m) {
201
+ if (!isObj(m)) return undefined
202
+ const clamp01 = (v) => Math.min(1, Math.max(0, v))
203
+ const out = {}
204
+ if (Array.isArray(m.palette) && m.palette.length) out.palette = m.palette.slice(0, 16).map((p) => ({ hex: p.hex, share: clamp01(Number(p.share) || 0) }))
205
+ if (Number.isFinite(m.valueKey)) out.valueKey = clamp01(m.valueKey)
206
+ if (Number.isFinite(m.contrast)) out.contrast = Math.max(0, m.contrast)
207
+ if (Number.isFinite(m.saturation)) out.saturation = Math.max(0, m.saturation)
208
+ if (Number.isFinite(m.warmth)) out.warmth = m.warmth
209
+ if (Number.isFinite(m.haze?.score)) out.haze = clamp01(m.haze.score)
210
+ if (Number.isFinite(m.horizon?.y) && (m.horizon.confidence ?? 0) >= 0.5) out.horizonY = clamp01(m.horizon.y)
211
+ return out
212
+ }
213
+
214
+ /**
215
+ * One board picture measured as the board shows it: nw_reference's rules
216
+ * (inside the project, a picture, at most 20 MB, never one recorded as from
217
+ * ArtStation, Pinterest or Behance), read inside a uniform border when it
218
+ * has one. file: a name inside .nowhere/refs/.
219
+ * Answers { ok, file, sha256, measured } or { ok: false, error }.
220
+ */
221
+ export async function measurePicture(projectRoot, file, { rpc } = {}) {
222
+ const name = String(file ?? '')
223
+ if (!/^[^/\\]{1,200}$/.test(name) || name.startsWith('.')) return fail('Not a picture on the board.')
224
+ const rel = `${REFS_PREFIX}${name}`
225
+ const ref = await readReference(projectRoot, rel, 'The reference board')
226
+ if (ref.error) return fail(ref.error)
227
+ const pic = await decodeImage(Buffer.from(ref.data, 'base64'), { mime: ref.mime, maxEdge: 1568, rpc })
228
+ if (!pic.ok) return fail(pic.error)
229
+ const whole = referenceFacts(pic, { source: rel })
230
+ // A uniform border (a letterbox, a mat) is not the art: nw_reference
231
+ // tells the agent to read the picture again inside the crop it suggests,
232
+ // and the board shows those numbers, with the crop it took them from and
233
+ // the size of the picture itself.
234
+ const crop = whole.frame?.suggestedCrop ?? null
235
+ const measured = crop ? { ...boardMeasure(referenceFacts(pic, { source: rel, crop })), size: whole.size, border: crop } : boardMeasure(whole)
236
+ return { ok: true, file: name, sha256: createHash('sha256').update(Buffer.from(ref.data, 'base64')).digest('hex'), measured }
237
+ }
238
+
239
+ /**
240
+ * The libraries searched as the browser shows them: asset_search's cards
241
+ * from Poly Haven and ambientCG (CC0 only), each as a hit with its density
242
+ * by resolution and the files this space already has from it, with the
243
+ * brief's facts and what an import keeps. defs: the asset ToolDefs by name;
244
+ * ctx: whose web consent and fetch it runs under.
245
+ */
246
+ export async function searchLibraries(defs, args, ctx) {
247
+ const kind = KINDS.has(args?.kind) ? args.kind : null
248
+ const query = String(args?.query ?? '').trim().slice(0, 80)
249
+ if (!kind) return fail('Pick models, materials or skies.')
250
+ if (!query) return fail('Type what to look for.')
251
+ // An empty list is the creator turning every library off: nothing is
252
+ // searched, rather than both.
253
+ if (Array.isArray(args.sources) && args.sources.length === 0) return fail('Pick a library to search: Poly Haven or ambientCG.')
254
+ const asked = (Array.isArray(args.sources) ? args.sources : BROWSER_SOURCES).filter((s) => BROWSER_SOURCES.includes(s) && SOURCES[s].kinds.includes(kind))
255
+ if (!asked.length) return fail(`${kind === 'model' ? 'ambientCG has no models' : 'No library here has that'}: pick Poly Haven.`)
256
+ const r = await runDef(defs.asset_search, { query, kind, sources: asked, license: 'cc0', limit: Math.min(24, PER_SOURCE * asked.length), thumbnails: false }, ctx)
257
+ if (r.isError || !Array.isArray(r.cards)) return fail(r.text || 'The libraries could not be searched.')
258
+ const imported = await importedBySource(ctx.projectRoot)
259
+ return { ok: true, query, kind, hits: r.cards.map((c) => hitOf(c, imported)), notes: r.notes ?? [], brief: await briefFacts(ctx.projectRoot), limits: LIMITS }
260
+ }
261
+
262
+ /**
263
+ * One library hit into the space, as a click on Import brings it: asset_fetch
264
+ * at the resolution chosen, asset_import (a material's maps kept at that
265
+ * resolution, a model at its class's texture size), then asset_inspect's
266
+ * report. A name that is taken is said before anything is downloaded.
267
+ * step(s): told 'fetching', 'importing' and 'inspecting' as they begin.
268
+ * Answers what was written, the ledger entries of it and the inspection.
269
+ */
270
+ export async function importLibraryHit(defs, args, { ctx, step = () => {} }) {
271
+ const id = String(args?.id ?? '')
272
+ const provider = id.slice(0, id.indexOf(':'))
273
+ if (!BROWSER_SOURCES.includes(provider)) return fail('Not a library the browser imports from.')
274
+ const resolution = RESOLUTIONS.has(args?.resolution) ? args.resolution : '1k'
275
+ const name = String(args?.name ?? nameFor(id))
276
+ if (!NAME.test(name)) return fail('A name is letters, digits, _ and -, starting with a letter or digit, at most 64.')
277
+ const cls = CLASSES.has(args?.class) ? args.class : undefined
278
+ const root = ctx.projectRoot
279
+ // A name that is taken is said before anything is downloaded; the
280
+ // creator decides whether this one replaces it.
281
+ const kindAsked = KINDS.has(args?.kind) ? args.kind : null
282
+ const there = kindAsked && !args.replace ? taken(root, kindAsked, name) : null
283
+ if (there) return fail(`${there} is already in this space.`, { conflict: true })
284
+
285
+ step('fetching')
286
+ const fetched = await runDef(defs.asset_fetch, { id, resolution }, ctx)
287
+ if (fetched.isError || !fetched.qid) return fail(fetched.text || 'Nothing was downloaded.', { step: 'fetch' })
288
+ const kind = fetched.kind
289
+ const skyExt = fetched.skyExt ?? '.hdr'
290
+ // The kind the library says, when the browser's was not given.
291
+ const landed = !kindAsked && !args.replace ? taken(root, kind, name, kind === 'hdri' ? skyExt : undefined) : null
292
+ if (landed) return fail(`${landed} is already in this space.`, { conflict: true, qid: fetched.qid })
293
+ const as = kind === 'model' ? `assets/${name}.glb` : kind === 'hdri' ? `assets/${name}${skyExt}` : `assets/${name}`
294
+
295
+ step('importing')
296
+ const before = await ledgerSnapshot(root)
297
+ const targetBefore = targetFiles(root, kind, name, as)
298
+ const sharedBefore = sharedStats(root)
299
+ // A material's maps are kept at the resolution chosen: without maxTexture
300
+ // asset_import holds them to 2048 px, so a 4k pick would land as 2k maps
301
+ // while the brief's sources record 4k. A model keeps its class's texture
302
+ // size (the browser shows that cap beside the class); a sky is not resized.
303
+ const imported = await runDef(defs.asset_import, { from: fetched.qid, as, ...(cls && kind === 'model' ? { class: cls } : {}), ...(kind === 'material' ? { maxTexture: RES_PX[resolution] } : {}), replace: args.replace === true }, ctx)
304
+ const after = await ledgerSnapshot(root)
305
+ const targetAfter = targetFiles(root, kind, name, as)
306
+ const sharedAfter = sharedStats(root)
307
+ const written = targetAfter.filter((f) => after[f] && before[f]?.sha256 !== after[f].sha256)
308
+ const removed = targetBefore.filter((f) => !targetAfter.includes(f))
309
+ // The ledger, CREDITS.md and brief.json, when this import changed them:
310
+ // main tells a running request's stop gate these are the studio's.
311
+ const touched = SHARED.filter((f) => sharedAfter[f] !== sharedBefore[f])
312
+ const result = {
313
+ qid: fetched.qid,
314
+ kind,
315
+ as,
316
+ resolution,
317
+ fetched: fetched.text,
318
+ imported: imported.text,
319
+ images: (imported.images ?? []).map(dataUrl).filter(Boolean),
320
+ written,
321
+ removed,
322
+ touched,
323
+ entries: written.map((f) => ({ path: f, bytes: after[f].bytes, license: after[f].license?.spdx, credit: after[f].license?.credit ?? null, source: after[f].source ?? null })),
324
+ credits: fs.existsSync(path.join(root, 'CREDITS.md')),
325
+ }
326
+ if (imported.isError) return fail(imported.text, { step: 'import', ...result })
327
+
328
+ // What the browser shows next is asset_inspect's report, as the agent
329
+ // reads it; a sky has none (asset_inspect reads models and pictures).
330
+ if (kind !== 'hdri') {
331
+ step('inspecting')
332
+ const inspected = await runDef(defs.asset_inspect, { path: kind === 'model' ? `assets/${name}.glb` : `assets/${name}` }, ctx)
333
+ result.inspect = { text: inspected.text, isError: inspected.isError === true, images: (inspected.images ?? []).map(dataUrl).filter(Boolean) }
334
+ }
335
+ return { ok: true, ...result }
336
+ }
337
+
338
+ /**
339
+ * @param {{ rpc?: Function, log?: Function, tools?: object[], fetch?: Function, send?: Function }} options
340
+ * tools: the asset ToolDefs (default makeAssetTools()), so a check passes
341
+ * fakes for Blender and the picture resize; fetch: the network (default
342
+ * the global one); send: where an import's steps go (the host's channel).
343
+ */
344
+ export function createPanels({ rpc = async () => ({ ok: false, error: 'The studio is not connected.' }), log = () => {}, tools, fetch, send = () => {} } = {}) {
345
+ const defs = Object.fromEntries((tools ?? makeAssetTools()).map((d) => [d.name, d]))
346
+ // One import at a time, so each one's files are its own to report. One
347
+ // that has to wait says so ('queued'), and one main gave up on while it
348
+ // waited is dropped before it downloads anything.
349
+ let queue = Promise.resolve()
350
+ let pending = 0
351
+ /** Imports waiting their turn: id -> the answer to give if dropped. */
352
+ const waiting = new Map()
353
+
354
+ const ctxFor = (msg) => ({
355
+ runId: null,
356
+ projectRoot: msg.projectRoot,
357
+ locale: typeof msg.locale === 'string' ? msg.locale : 'en',
358
+ rpc,
359
+ // Main asked the creator on the agent's own card before this call.
360
+ web: async () => (msg.web === 'allow' || msg.web === 'deny' ? msg.web : 'unavailable'),
361
+ secret: () => undefined,
362
+ host: 'studio',
363
+ ...(fetch ? { fetch } : {}),
364
+ log,
365
+ })
366
+
367
+ const search = (args, msg) => searchLibraries(defs, args, ctxFor(msg))
368
+
369
+ async function thumbnail(args, msg) {
370
+ const source = SOURCES[args?.provider]
371
+ if (!source || !BROWSER_SOURCES.includes(source.id)) return fail('Not a library the browser shows.')
372
+ const ctx = ctxFor(msg)
373
+ const img = await fetchThumbnail(String(args.url ?? ''), { fetch: fetchOf(ctx), hosts: source.hosts, ua: userAgentFor(ctx), maxBytes: args.large ? 2 * 1024 * 1024 : 512 * 1024 })
374
+ return img ? { ok: true, url: dataUrl(img) } : fail('No picture.')
375
+ }
376
+
377
+ const importHit = (args, msg) => importLibraryHit(defs, args, { ctx: ctxFor(msg), step: (s) => send({ type: 'panel-progress', id: msg.id, step: s }) })
378
+
379
+ const measure = (args, msg) => measurePicture(msg.projectRoot, args?.file, { rpc })
380
+
381
+ const CALLS = {
382
+ 'assets.search': search,
383
+ 'assets.thumbnail': thumbnail,
384
+ 'assets.import': (args, msg) =>
385
+ new Promise((resolve) => {
386
+ if (pending > 0) send({ type: 'panel-progress', id: msg.id, step: 'queued' })
387
+ pending++
388
+ waiting.set(msg.id, resolve)
389
+ queue = queue
390
+ .then(async () => {
391
+ // Dropped while it waited: answered then, and nothing runs.
392
+ if (!waiting.delete(msg.id)) return
393
+ try {
394
+ resolve(await importHit(args, msg))
395
+ } catch (err) {
396
+ log(`[panels] ${msg.call} failed: ${err?.stack ?? err}`)
397
+ resolve(fail(`${msg.call} failed: ${err?.message ?? err}`))
398
+ }
399
+ })
400
+ .finally(() => {
401
+ pending--
402
+ })
403
+ }),
404
+ 'refs.measure': measure,
405
+ }
406
+
407
+ /** One call from main; answers, never throws. */
408
+ async function handle(msg) {
409
+ const fn = CALLS[msg?.call]
410
+ if (!fn) return fail(`Unknown panel call: ${msg?.call}`)
411
+ if (!msg.projectRoot || !isObj(msg.args ?? {})) return fail('Open a project first.')
412
+ try {
413
+ return await fn(msg.args ?? {}, msg)
414
+ } catch (err) {
415
+ log(`[panels] ${msg.call} failed: ${err?.stack ?? err}`)
416
+ return fail(`${msg.call} failed: ${err?.message ?? err}`)
417
+ }
418
+ }
419
+
420
+ /** Main gave up on an import: one still waiting its turn is dropped and
421
+ * answered now; one that has begun runs to its end. */
422
+ function cancel(id) {
423
+ const resolve = waiting.get(id)
424
+ if (!resolve) return false
425
+ waiting.delete(id)
426
+ resolve(fail('It waited too long behind another import, so nothing was downloaded.'))
427
+ return true
428
+ }
429
+
430
+ return { handle, cancel, calls: Object.keys(CALLS) }
431
+ }