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,364 @@
1
+ // The round's picture: the target and the space as it is now, side by side
2
+ // at one height, labelled "target | now", with the marks drawn on the "now"
3
+ // half (REFERENCE_LOOP.md 2.4 and 2.7): what the round message shows the
4
+ // creator, what a judge is handed, and what a creator's circle on a still
5
+ // comes back as. Both pictures come from the same hero camera, so the "now"
6
+ // half is cut to the target's shape when the two differ, and marks given
7
+ // in the current picture's pixels follow that cut.
8
+ //
9
+ // Marks are the chat's (notes.cjs marksFor): { kind: 'pen' | 'circle' |
10
+ // 'arrow' | 'text', points?: [[x, y], ...], rect?: [x, y, width, height],
11
+ // text? }. A circle is the ellipse inside its rect and may carry words,
12
+ // drawn beside the ring; an arrow runs from its first point to its last;
13
+ // text starts at its one point. They are drawn on the "now" half itself
14
+ // before it goes on the sheet, so a mark past its edge stops there and
15
+ // never reaches the target.
16
+ //
17
+ // No image library: the pictures are decoded as the reference tools decode
18
+ // them (decode.mjs: sharp, or the viewport's own decoder over the pixels
19
+ // rpc), drawn here pixel by pixel and written by png.mjs. The lettering is a
20
+ // 5 x 7 bitmap font of capitals, figures and the usual marks, scaled up; a
21
+ // character it does not have (another script) is drawn as an outlined box,
22
+ // and the words of a creator's note reach the agent with their message.
23
+
24
+ import fsp from 'node:fs/promises'
25
+ import path from 'node:path'
26
+ import { decodeImage } from './decode.mjs'
27
+ import { fitAspect, resample } from './facts.mjs'
28
+ import { canvas, encodePng, fillRect, line } from './png.mjs'
29
+
30
+ /** The tallest the two halves are drawn, pixels (each half its own width). */
31
+ export const COMPOSE_HEIGHT = 720
32
+ /** The label bar above the halves and the gap between them. */
33
+ const BAR = 40
34
+ const GAP = 6
35
+ /** Process colours of the sheet (never a space's): a dark sheet, light
36
+ * lettering, and one loud colour for the marks, so they read on any picture. */
37
+ const SHEET = [22, 22, 26]
38
+ const INK = [236, 236, 240]
39
+ const MARK = [255, 47, 208]
40
+ const HALO = [0, 0, 0]
41
+
42
+ // 5 x 7 glyphs, a row a number, the top row first, the left column the 16s bit.
43
+ const GLYPHS = {
44
+ A: [14, 17, 17, 31, 17, 17, 17], B: [30, 17, 17, 30, 17, 17, 30], C: [14, 17, 16, 16, 16, 17, 14], D: [30, 17, 17, 17, 17, 17, 30],
45
+ E: [31, 16, 16, 30, 16, 16, 31], F: [31, 16, 16, 30, 16, 16, 16], G: [14, 17, 16, 23, 17, 17, 15], H: [17, 17, 17, 31, 17, 17, 17],
46
+ I: [14, 4, 4, 4, 4, 4, 14], J: [7, 2, 2, 2, 2, 18, 12], K: [17, 18, 20, 24, 20, 18, 17], L: [16, 16, 16, 16, 16, 16, 31],
47
+ M: [17, 27, 21, 21, 17, 17, 17], N: [17, 17, 25, 21, 19, 17, 17], O: [14, 17, 17, 17, 17, 17, 14], P: [30, 17, 17, 30, 16, 16, 16],
48
+ Q: [14, 17, 17, 17, 21, 18, 13], R: [30, 17, 17, 30, 20, 18, 17], S: [15, 16, 16, 14, 1, 1, 30], T: [31, 4, 4, 4, 4, 4, 4],
49
+ U: [17, 17, 17, 17, 17, 17, 14], V: [17, 17, 17, 17, 17, 10, 4], W: [17, 17, 17, 21, 21, 21, 10], X: [17, 17, 10, 4, 10, 17, 17],
50
+ Y: [17, 17, 17, 10, 4, 4, 4], Z: [31, 1, 2, 4, 8, 16, 31],
51
+ 0: [14, 17, 19, 21, 25, 17, 14], 1: [4, 12, 4, 4, 4, 4, 14], 2: [14, 17, 1, 2, 4, 8, 31], 3: [31, 2, 4, 2, 1, 17, 14],
52
+ 4: [2, 6, 10, 18, 31, 2, 2], 5: [31, 16, 30, 1, 1, 17, 14], 6: [6, 8, 16, 30, 17, 17, 14], 7: [31, 1, 2, 4, 8, 8, 8],
53
+ 8: [14, 17, 17, 14, 17, 17, 14], 9: [14, 17, 17, 15, 1, 2, 12],
54
+ ' ': [0, 0, 0, 0, 0, 0, 0], '.': [0, 0, 0, 0, 0, 12, 12], ',': [0, 0, 0, 0, 12, 4, 8], ':': [0, 12, 12, 0, 12, 12, 0],
55
+ ';': [0, 12, 12, 0, 12, 4, 8], '!': [4, 4, 4, 4, 4, 0, 4], '?': [14, 17, 1, 2, 4, 0, 4], '|': [4, 4, 4, 4, 4, 4, 4],
56
+ '-': [0, 0, 0, 31, 0, 0, 0], _: [0, 0, 0, 0, 0, 0, 31], '(': [2, 4, 8, 8, 8, 4, 2], ')': [8, 4, 2, 2, 2, 4, 8],
57
+ '/': [1, 1, 2, 4, 8, 16, 16], "'": [4, 4, 8, 0, 0, 0, 0], '"': [10, 10, 0, 0, 0, 0, 0], '+': [0, 4, 4, 31, 4, 4, 0],
58
+ '=': [0, 0, 31, 0, 31, 0, 0], '%': [24, 25, 2, 4, 8, 19, 3], '#': [10, 10, 31, 10, 31, 10, 10], '<': [2, 4, 8, 16, 8, 4, 2],
59
+ '>': [8, 4, 2, 1, 2, 4, 8], '*': [0, 4, 21, 14, 21, 4, 0], '[': [14, 8, 8, 8, 8, 8, 14], ']': [14, 2, 2, 2, 2, 2, 14],
60
+ '&': [12, 18, 20, 8, 21, 18, 13],
61
+ }
62
+ /** What a character the font has no glyph for is drawn as: an outlined box. */
63
+ const MISSING = [31, 17, 17, 17, 17, 17, 31]
64
+
65
+ /** Lettering at `scale` pixels a font pixel; answers its width. */
66
+ export function drawText(pic, text, x, y, rgb, scale = 2) {
67
+ let at = Math.round(x)
68
+ for (const ch of String(text ?? '')) {
69
+ const rows = GLYPHS[ch.toUpperCase()] ?? MISSING
70
+ for (let r = 0; r < 7; r++) {
71
+ for (let c = 0; c < 5; c++) {
72
+ if (rows[r] & (16 >> c)) fillRect(pic, at + c * scale, y + r * scale, scale, scale, rgb)
73
+ }
74
+ }
75
+ at += 6 * scale
76
+ }
77
+ return at - Math.round(x)
78
+ }
79
+ export const textWidth = (text, scale = 2) => [...String(text ?? '')].length * 6 * scale
80
+
81
+ /** A line drawn twice, a dark halo under the mark's colour, so it reads on light and dark alike. */
82
+ function stroke(pic, x0, y0, x1, y1, width) {
83
+ line(pic, x0, y0, x1, y1, HALO, width + 2)
84
+ line(pic, x0, y0, x1, y1, MARK, width)
85
+ }
86
+
87
+ /** The ellipse inside a box, as a closed run of strokes. */
88
+ function ellipse(pic, x0, y0, x1, y1, width) {
89
+ const cx = (x0 + x1) / 2
90
+ const cy = (y0 + y1) / 2
91
+ const rx = Math.max(2, Math.abs(x1 - x0) / 2)
92
+ const ry = Math.max(2, Math.abs(y1 - y0) / 2)
93
+ const steps = Math.max(24, Math.ceil((rx + ry) / 2))
94
+ const at = (i) => [cx + rx * Math.cos((i / steps) * Math.PI * 2), cy + ry * Math.sin((i / steps) * Math.PI * 2)]
95
+ // The whole halo first, then the colour over it, so the ring stays one line.
96
+ for (const [rgb, w] of [[HALO, width + 2], [MARK, width]]) {
97
+ for (let i = 1; i <= steps; i++) {
98
+ const [ax, ay] = at(i - 1)
99
+ const [bx, by] = at(i)
100
+ line(pic, ax, ay, bx, by, rgb, w)
101
+ }
102
+ }
103
+ }
104
+
105
+ /** The most words a mark carries (notes.cjs MAX_MARK_TEXT) and the most lines they are drawn on. */
106
+ export const MARK_TEXT_MAX = 300
107
+ const MARK_TEXT_LINES = 6
108
+
109
+ /** Words broken into lines of at most `fit` characters; a word longer than
110
+ * a line is cut across lines. At most MARK_TEXT_LINES, the last one ending
111
+ * in ".." when words were left over. */
112
+ function wrapWords(text, fit) {
113
+ const lines = []
114
+ let lineNow = ''
115
+ for (let word of String(text).split(/\s+/).filter(Boolean)) {
116
+ while (word.length > fit) {
117
+ if (lineNow) lines.push(lineNow)
118
+ lines.push(word.slice(0, fit))
119
+ word = word.slice(fit)
120
+ lineNow = ''
121
+ }
122
+ if (!lineNow) lineNow = word
123
+ else if (lineNow.length + 1 + word.length <= fit) lineNow += ` ${word}`
124
+ else {
125
+ lines.push(lineNow)
126
+ lineNow = word
127
+ }
128
+ }
129
+ if (lineNow) lines.push(lineNow)
130
+ if (lines.length <= MARK_TEXT_LINES) return lines
131
+ const kept = lines.slice(0, MARK_TEXT_LINES)
132
+ kept[MARK_TEXT_LINES - 1] = `${kept[MARK_TEXT_LINES - 1].slice(0, Math.max(1, fit - 2))}..`
133
+ return kept
134
+ }
135
+
136
+ /** A mark's words on a dark plate with its top left at (x, y), or its
137
+ * right edge at x (endAt), kept inside the picture: moved left or up when
138
+ * they would run off its edge. */
139
+ function drawNote(pic, words, x, y, scale, endAt = false) {
140
+ const fit = Math.max(4, Math.floor((pic.width - 12) / (6 * scale)))
141
+ const lines = wrapWords(String(words).trim().slice(0, MARK_TEXT_MAX), fit)
142
+ if (lines.length === 0) return
143
+ const w = Math.max(...lines.map((l) => textWidth(l, scale)))
144
+ const lineH = 9 * scale
145
+ const h = lines.length * lineH - 2 * scale
146
+ const left = Math.max(3, Math.min(Math.round(endAt ? x - w : x), pic.width - w - 3))
147
+ const top = Math.max(3, Math.min(Math.round(y), pic.height - h - 3))
148
+ fillRect(pic, left - 3, top - 3, w + 6, h + 6, HALO)
149
+ lines.forEach((l, i) => drawText(pic, l, left, top + i * lineH, MARK, scale))
150
+ }
151
+
152
+ /**
153
+ * Draws marks (REFERENCE_LOOP.md 2.7, as the chat sends them: points, or a
154
+ * rect [x, y, width, height]) onto `pic`: `to(x, y)` takes a point in the
155
+ * marked picture's pixels to `pic`'s. Any mark may carry words (the chat
156
+ * lets each have them), drawn by it. Answers how many it drew; one with
157
+ * nothing to draw it by is left out.
158
+ */
159
+ export function drawMarks(pic, marks, to, scale = 1) {
160
+ let drawn = 0
161
+ const width = Math.max(2, Math.round(3 * scale))
162
+ const letters = Math.max(2, Math.round(2 * scale))
163
+ for (const m of Array.isArray(marks) ? marks : []) {
164
+ const pts = (Array.isArray(m?.points) ? m.points : []).filter((p) => Array.isArray(p) && p.length >= 2 && p.every(Number.isFinite)).map(([x, y]) => to(x, y))
165
+ // [x, y, width, height] as the chat sends it (notes.cjs), as two corners on the sheet.
166
+ const r = Array.isArray(m?.rect) && m.rect.length === 4 && m.rect.every(Number.isFinite) ? m.rect : null
167
+ const rect = r ? [...to(r[0], r[1]), ...to(r[0] + r[2], r[1] + r[3])] : null
168
+ const words = typeof m?.text === 'string' && m.text.trim() ? m.text.trim() : ''
169
+ if (m?.kind === 'pen' && pts.length >= 2) {
170
+ for (let i = 1; i < pts.length; i++) stroke(pic, pts[i - 1][0], pts[i - 1][1], pts[i][0], pts[i][1], width)
171
+ if (words) drawNote(pic, words, pts[0][0] + 6, pts[0][1] + 6, letters)
172
+ } else if (m?.kind === 'circle' && (rect || pts.length >= 2)) {
173
+ // The ellipse inside the rect, or a centre and a point on the rim.
174
+ let box = rect
175
+ if (!box) {
176
+ const rad = Math.hypot(pts[1][0] - pts[0][0], pts[1][1] - pts[0][1])
177
+ box = [pts[0][0] - rad, pts[0][1] - rad, pts[0][0] + rad, pts[0][1] + rad]
178
+ }
179
+ ellipse(pic, box[0], box[1], box[2], box[3], width)
180
+ // Its words beside the ring, on the right when they fit there.
181
+ if (words) {
182
+ const right = Math.max(box[0], box[2]) + 8
183
+ const fits = right + Math.min(textWidth(words, letters), 24 * 6 * letters) < pic.width
184
+ drawNote(pic, words, fits ? right : Math.min(box[0], box[2]) - 8, Math.min(box[1], box[3]), letters, !fits)
185
+ }
186
+ } else if (m?.kind === 'arrow' && pts.length >= 2) {
187
+ const [a, b] = [pts[0], pts[pts.length - 1]]
188
+ stroke(pic, a[0], a[1], b[0], b[1], width)
189
+ const ang = Math.atan2(b[1] - a[1], b[0] - a[0])
190
+ const head = 6 * width
191
+ for (const side of [-1, 1]) stroke(pic, b[0], b[1], b[0] - head * Math.cos(ang + side * 0.45), b[1] - head * Math.sin(ang + side * 0.45), width)
192
+ // Its words at the tail, on the side away from the head, so they do not cover the line.
193
+ if (words) {
194
+ const toRight = b[0] >= a[0]
195
+ drawNote(pic, words, toRight ? a[0] - 8 : a[0] + 8, a[1] - 4 * letters, letters, toRight)
196
+ }
197
+ } else if (m?.kind === 'text' && words && (pts.length >= 1 || rect)) {
198
+ const [x, y] = pts.length >= 1 ? pts[0] : [rect[0], rect[1]]
199
+ drawNote(pic, words, x, y, letters)
200
+ } else continue
201
+ drawn++
202
+ }
203
+ return drawn
204
+ }
205
+
206
+ /** A picture's bytes, read from inside `root` when it is given. */
207
+ async function readPicture(file, root) {
208
+ const abs = root ? path.resolve(root, file) : path.resolve(file)
209
+ if (root) {
210
+ const base = path.resolve(root)
211
+ if (!inside(base, abs)) return { error: `${file} is outside the project.` }
212
+ // A link inside the project that leads out of it is outside too.
213
+ if (!inside(await realish(base), await realish(abs))) return { error: `${file} is outside the project.` }
214
+ }
215
+ try {
216
+ return { bytes: await fsp.readFile(abs), abs }
217
+ } catch {
218
+ return { error: `There is no ${file}.` }
219
+ }
220
+ }
221
+
222
+ const mimeOf = (bytes) =>
223
+ bytes[0] === 0x89 && bytes[1] === 0x50 ? 'image/png' : bytes[0] === 0xff && bytes[1] === 0xd8 ? 'image/jpeg' : bytes.slice(8, 12).toString('latin1') === 'WEBP' ? 'image/webp' : null
224
+
225
+ /** The label without a "target | now" of its own (the bar says that), cut
226
+ * to what fits `width` pixels at `scale`: "H2 round 3" stays as it is. */
227
+ export function labelFor(label, width, scale) {
228
+ let words = String(label ?? '')
229
+ .replace(/\s+/g, ' ')
230
+ .trim()
231
+ .replace(/[\s:,-]*target\s*\|\s*now\s*$/i, '')
232
+ .trim()
233
+ const fit = Math.max(0, Math.floor(width / (6 * scale)))
234
+ if (words.length > fit) words = fit > 2 ? `${words.slice(0, fit - 2).trimEnd()}..` : ''
235
+ return words
236
+ }
237
+
238
+ /** A path with its links followed as far as it exists: the real path of
239
+ * its nearest folder that is there, and the rest as written. */
240
+ async function realish(p) {
241
+ let head = p
242
+ const rest = []
243
+ for (;;) {
244
+ try {
245
+ return path.join(await fsp.realpath(head), ...rest.reverse())
246
+ } catch {
247
+ const up = path.dirname(head)
248
+ if (up === head) return p
249
+ rest.push(path.basename(head))
250
+ head = up
251
+ }
252
+ }
253
+ }
254
+
255
+ /** Whether `inner` is `outer` or inside it. */
256
+ const inside = (outer, inner, strict = false) => {
257
+ const rel = path.relative(outer, inner)
258
+ return !rel.startsWith('..') && !path.isAbsolute(rel) && (!strict || rel !== '')
259
+ }
260
+
261
+ /** Where `out` goes: inside `root` when it is given (and inside `within`,
262
+ * a folder of root's, when that is given too), anywhere for a host that
263
+ * passes no root. A link on the way that leads out counts as out: the
264
+ * check is on the real path, before any folder is made. Answers { file }
265
+ * or { error }. */
266
+ async function outFile(out, root, within) {
267
+ if (!root) return { file: path.resolve(out) }
268
+ const base = path.resolve(root)
269
+ const file = path.resolve(base, String(out))
270
+ const fence = within ? path.resolve(base, within) : base
271
+ const say = within ? `in ${within}/` : 'in the project'
272
+ if (!inside(fence, file, true)) return { error: `${out} is not ${say}: the picture is written there.` }
273
+ if (!/\.png$/i.test(file)) return { error: `${out} is not a .png: the picture is written as a PNG.` }
274
+ const [realBase, realFence, realFile] = await Promise.all([realish(base), realish(fence), realish(file)])
275
+ if (!inside(realBase, realFence) || !inside(realFence, realFile, true)) return { error: `${out} leads out of ${within ? `${within}/` : 'the project'} through a link.` }
276
+ try {
277
+ if ((await fsp.lstat(file)).isSymbolicLink()) return { error: `${out} is a link; the picture is not written through it.` }
278
+ } catch {
279
+ // not there yet
280
+ }
281
+ return { file }
282
+ }
283
+
284
+ /**
285
+ * compose(targetPath, currentPath, out, { label, marks }): the target and
286
+ * the current still side by side at one height (at most COMPOSE_HEIGHT),
287
+ * the bar above them saying "<label> target | now", the marks drawn on the
288
+ * "now" half in the current picture's pixels, written to `out` as a PNG.
289
+ * label: the view and round ("H2 round 3"); a trailing "target | now" is
290
+ * dropped, the bar says it. root: every path is inside this folder (the
291
+ * project), and within: `out` is inside this folder of it (nw_compose
292
+ * passes .nowhere/compose); a host that passes no root (the round loop,
293
+ * writing .nowhere/rounds/<n>/composite-H#.png) gives paths as they are.
294
+ * rpc: the host's, for a decoder where sharp is missing. Answers { ok,
295
+ * path, width, height, panels, marks, cut } or { ok: false, error }.
296
+ */
297
+ export async function compose(targetPath, currentPath, out, { label = '', marks = [], root = null, within = null, rpc = null, height = COMPOSE_HEIGHT } = {}) {
298
+ const pics = []
299
+ for (const file of [targetPath, currentPath]) {
300
+ const read = await readPicture(file, root)
301
+ if (read.error) return { ok: false, error: read.error }
302
+ const mime = mimeOf(read.bytes)
303
+ if (!mime) return { ok: false, error: `${file} is not a PNG, JPEG or WebP picture.` }
304
+ const pic = await decodeImage(read.bytes, { mime, maxEdge: 4096, rpc })
305
+ if (!pic.ok) return { ok: false, error: `${file}: ${pic.error}` }
306
+ pics.push(pic)
307
+ }
308
+ const [target, current] = pics
309
+ // The "now" half cut to the target's shape, as nw_compare cuts a shot:
310
+ // the same hero camera, so what differs is the frame's edge, not the view.
311
+ const aspect = target.width / target.height
312
+ const cut = fitAspect(current, aspect)
313
+ const offX = (current.width - cut.width) / 2
314
+ const offY = (current.height - cut.height) / 2
315
+ const H = Math.max(64, Math.min(Math.round(height), Math.max(target.height, cut.height)))
316
+ const wT = Math.max(1, Math.round(H * aspect))
317
+ const wN = Math.max(1, Math.round((H * cut.width) / cut.height))
318
+ const W = wT + GAP + wN
319
+ const sheet = canvas(W, BAR + H, SHEET)
320
+ blitInto(sheet, resample(target, wT, H), 0, BAR)
321
+ // Marks on the "now" half itself, from the current picture's pixels to
322
+ // its own: what runs past its edge stops there.
323
+ const k = H / cut.height
324
+ const nowPic = resample(cut, wN, H)
325
+ const drawn = drawMarks(nowPic, marks, (x, y) => [(x - offX) * k, (y - offY) * k], Math.max(1, H / 540))
326
+ blitInto(sheet, nowPic, wT + GAP, BAR)
327
+ // The bar: the label and "target" over the left half, "now" over the
328
+ // right, a rule between. A label too long for the target half is drawn
329
+ // smaller, then cut.
330
+ const room = wT - 20 - textWidth(' target', 3)
331
+ const s = textWidth(labelFor(label, Number.MAX_SAFE_INTEGER, 3), 3) <= room ? 3 : 2
332
+ const words = labelFor(label, wT - 20 - textWidth(' target', s), s)
333
+ const ty = Math.round((BAR - 7 * s) / 2)
334
+ drawText(sheet, `${words ? `${words} ` : ''}target`, 10, ty, INK, s)
335
+ drawText(sheet, '|', wT + GAP / 2 - 3 * s, ty, INK, s)
336
+ drawText(sheet, 'now', wT + GAP + 10, ty, INK, s)
337
+ fillRect(sheet, wT, BAR, GAP, H, SHEET)
338
+ const where = await outFile(out, root, within)
339
+ if (where.error) return { ok: false, error: where.error }
340
+ await fsp.mkdir(path.dirname(where.file), { recursive: true })
341
+ await fsp.writeFile(where.file, encodePng(sheet))
342
+ return {
343
+ ok: true,
344
+ path: where.file,
345
+ width: W,
346
+ height: BAR + H,
347
+ panels: { target: { x: 0, y: BAR, w: wT, h: H }, now: { x: wT + GAP, y: BAR, w: wN, h: H } },
348
+ marks: drawn,
349
+ cut: cut === current ? null : { x: Math.round(offX), y: Math.round(offY), w: cut.width, h: cut.height },
350
+ }
351
+ }
352
+
353
+ /** `src` copied into `dst` at (x, y), as it is (already the right size). */
354
+ function blitInto(dst, src, x, y) {
355
+ for (let row = 0; row < src.height; row++) {
356
+ const ty = y + row
357
+ if (ty < 0 || ty >= dst.height) continue
358
+ const from = row * src.width * 4
359
+ const to = (ty * dst.width + x) * 4
360
+ const n = Math.min(src.width, dst.width - x) * 4
361
+ dst.data.set(src.data.subarray(from, from + n), to)
362
+ for (let i = 3; i < n; i += 4) dst.data[to + i] = 255
363
+ }
364
+ }
@@ -0,0 +1,56 @@
1
+ // A picture's bytes (PNG, JPEG or WebP) as RGBA, for the reference tools.
2
+ // sharp when it resolves (it does in the repo and the CLI, through the
3
+ // glTF pipeline's dependencies), else the studio's own decoder over the
4
+ // `pixels` rpc (the viewport draws the picture with createImageBitmap and
5
+ // reads it back). With neither, the answer says so instead of a number.
6
+
7
+ let sharpModule // undefined: not tried yet; null: not there
8
+
9
+ async function loadSharp() {
10
+ if (sharpModule !== undefined) return sharpModule
11
+ try {
12
+ sharpModule = (await import('sharp')).default
13
+ } catch {
14
+ sharpModule = null
15
+ }
16
+ return sharpModule
17
+ }
18
+
19
+ /** Whether sharp decodes in this process. */
20
+ export async function hasSharp() {
21
+ return (await loadSharp()) !== null
22
+ }
23
+
24
+ /**
25
+ * The picture in `bytes` as { ok: true, data (RGBA), width, height, via },
26
+ * at most `maxEdge` on its long edge (never enlarged), or { ok: false, error }.
27
+ * `rpc` is the host's (ctx.rpc), used when sharp is not there or when
28
+ * `sharp: false` asks for the studio's decoder.
29
+ */
30
+ export async function decodeImage(bytes, { mime = 'image/png', maxEdge = 1568, rpc = null, sharp = true } = {}) {
31
+ const buf = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes)
32
+ const lib = sharp ? await loadSharp() : null
33
+ if (lib) {
34
+ try {
35
+ const { data, info } = await lib(buf, { failOn: 'none' })
36
+ .rotate()
37
+ .resize({ width: maxEdge, height: maxEdge, fit: 'inside', withoutEnlargement: true })
38
+ .ensureAlpha()
39
+ .raw()
40
+ .toBuffer({ resolveWithObject: true })
41
+ return { ok: true, data: new Uint8Array(data.buffer, data.byteOffset, data.length), width: info.width, height: info.height, via: 'sharp' }
42
+ } catch (err) {
43
+ return { ok: false, error: `The picture could not be decoded: ${err?.message ?? err}` }
44
+ }
45
+ }
46
+ if (typeof rpc === 'function') {
47
+ const r = await rpc('pixels', { data: buf.toString('base64'), mime, longEdge: maxEdge }, 9000)
48
+ if (r?.ok && typeof r.rgba === 'string' && r.width > 0 && r.height > 0) {
49
+ const data = new Uint8Array(Buffer.from(r.rgba, 'base64'))
50
+ if (data.length === r.width * r.height * 4) return { ok: true, data, width: r.width, height: r.height, via: 'pixels' }
51
+ return { ok: false, error: 'The studio decoded the picture, but its pixels came back the wrong size.' }
52
+ }
53
+ return { ok: false, error: `The picture could not be decoded: ${r?.error ?? 'the studio did not answer the pixels call.'}` }
54
+ }
55
+ return { ok: false, error: 'No image decoder is available here (neither sharp nor the studio\'s viewport), so the picture cannot be measured.' }
56
+ }