@things-factory/figure-service 10.1.277 → 10.1.279

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 (128) hide show
  1. package/package.json +23 -8
  2. package/dist-server/index.js.map +0 -1
  3. package/dist-server/migrations/index.js.map +0 -1
  4. package/dist-server/routers/figure-mesh-router.js.map +0 -1
  5. package/dist-server/routers/figure-package-router.js.map +0 -1
  6. package/dist-server/routers/figure-thumbnail-router.js.map +0 -1
  7. package/dist-server/routes.js.map +0 -1
  8. package/dist-server/service/figure/event-subscriber.js.map +0 -1
  9. package/dist-server/service/figure/figure-ai-scenarios-run.js.map +0 -1
  10. package/dist-server/service/figure/figure-ai-scenarios.js.map +0 -1
  11. package/dist-server/service/figure/figure-categories.js.map +0 -1
  12. package/dist-server/service/figure/figure-context.js.map +0 -1
  13. package/dist-server/service/figure/figure-format.js.map +0 -1
  14. package/dist-server/service/figure/figure-glb.js.map +0 -1
  15. package/dist-server/service/figure/figure-history.js.map +0 -1
  16. package/dist-server/service/figure/figure-llm-provider.js.map +0 -1
  17. package/dist-server/service/figure/figure-mesh-store.js.map +0 -1
  18. package/dist-server/service/figure/figure-mutation.js.map +0 -1
  19. package/dist-server/service/figure/figure-namespace-entity.js.map +0 -1
  20. package/dist-server/service/figure/figure-namespace-resolver.js.map +0 -1
  21. package/dist-server/service/figure/figure-namespace.js.map +0 -1
  22. package/dist-server/service/figure/figure-package-export.js.map +0 -1
  23. package/dist-server/service/figure/figure-proposal-record.js.map +0 -1
  24. package/dist-server/service/figure/figure-proposal.js.map +0 -1
  25. package/dist-server/service/figure/figure-propose-steps.js.map +0 -1
  26. package/dist-server/service/figure/figure-propose-type.js.map +0 -1
  27. package/dist-server/service/figure/figure-public.js.map +0 -1
  28. package/dist-server/service/figure/figure-query.js.map +0 -1
  29. package/dist-server/service/figure/figure-rename.js.map +0 -1
  30. package/dist-server/service/figure/figure-replaced-draft.js.map +0 -1
  31. package/dist-server/service/figure/figure-revert.js.map +0 -1
  32. package/dist-server/service/figure/figure-subject.js.map +0 -1
  33. package/dist-server/service/figure/figure-thumbnail-reader.js.map +0 -1
  34. package/dist-server/service/figure/figure-thumbnail-store.js.map +0 -1
  35. package/dist-server/service/figure/figure-thumbnail-upgrade.js.map +0 -1
  36. package/dist-server/service/figure/figure-tools.js.map +0 -1
  37. package/dist-server/service/figure/figure-type.js.map +0 -1
  38. package/dist-server/service/figure/figure-write.js.map +0 -1
  39. package/dist-server/service/figure/figure.js.map +0 -1
  40. package/dist-server/service/figure/index.js.map +0 -1
  41. package/dist-server/service/figure-recipe/figure-recipe-resolver.js.map +0 -1
  42. package/dist-server/service/figure-recipe/figure-recipe-templates.js.map +0 -1
  43. package/dist-server/service/figure-recipe/figure-recipe-type.js.map +0 -1
  44. package/dist-server/service/figure-recipe/index.js.map +0 -1
  45. package/dist-server/service/index.js.map +0 -1
  46. package/dist-server/tsconfig.tsbuildinfo +0 -1
  47. package/scripts/build-templates.mts +0 -64
  48. package/scripts/figure-ai-scenarios-agent.cjs +0 -98
  49. package/scripts/rename-figures-to-full-names.mjs +0 -211
  50. package/scripts/templates.test.mts +0 -112
  51. package/server/index.ts +0 -46
  52. package/server/migrations/index.ts +0 -9
  53. package/server/routers/figure-mesh-router.ts +0 -63
  54. package/server/routers/figure-package-router.ts +0 -53
  55. package/server/routers/figure-thumbnail-router.ts +0 -159
  56. package/server/routes.ts +0 -30
  57. package/server/service/figure/catalog-db.test.ts +0 -162
  58. package/server/service/figure/event-subscriber.ts +0 -66
  59. package/server/service/figure/figure-ai-scenarios-run.ts +0 -127
  60. package/server/service/figure/figure-ai-scenarios.live.test.ts +0 -59
  61. package/server/service/figure/figure-ai-scenarios.test.ts +0 -109
  62. package/server/service/figure/figure-ai-scenarios.ts +0 -798
  63. package/server/service/figure/figure-blueprint-query.test.ts +0 -536
  64. package/server/service/figure/figure-catalog-seen-db.test.ts +0 -153
  65. package/server/service/figure/figure-categories.ts +0 -50
  66. package/server/service/figure/figure-category-db.test.ts +0 -127
  67. package/server/service/figure/figure-context.test.ts +0 -402
  68. package/server/service/figure/figure-context.ts +0 -510
  69. package/server/service/figure/figure-deprecation-db.test.ts +0 -218
  70. package/server/service/figure/figure-format.test.ts +0 -423
  71. package/server/service/figure/figure-format.ts +0 -783
  72. package/server/service/figure/figure-glb.test.ts +0 -112
  73. package/server/service/figure/figure-glb.ts +0 -251
  74. package/server/service/figure/figure-history.ts +0 -192
  75. package/server/service/figure/figure-inspect-db.test.ts +0 -131
  76. package/server/service/figure/figure-llm-provider.ts +0 -20
  77. package/server/service/figure/figure-llm-smoke.test.ts +0 -95
  78. package/server/service/figure/figure-mesh-db.test.ts +0 -246
  79. package/server/service/figure/figure-mesh-store.ts +0 -91
  80. package/server/service/figure/figure-mutation.ts +0 -656
  81. package/server/service/figure/figure-namespace-db.test.ts +0 -315
  82. package/server/service/figure/figure-namespace-entity.ts +0 -67
  83. package/server/service/figure/figure-namespace-resolver.ts +0 -54
  84. package/server/service/figure/figure-namespace.ts +0 -156
  85. package/server/service/figure/figure-package-export.ts +0 -136
  86. package/server/service/figure/figure-proposal-db.test.ts +0 -117
  87. package/server/service/figure/figure-proposal-record.ts +0 -38
  88. package/server/service/figure/figure-proposal.ts +0 -41
  89. package/server/service/figure/figure-propose-clips.test.ts +0 -69
  90. package/server/service/figure/figure-propose-steps.test.ts +0 -324
  91. package/server/service/figure/figure-propose-steps.ts +0 -354
  92. package/server/service/figure/figure-propose-type.ts +0 -46
  93. package/server/service/figure/figure-public-db.test.ts +0 -288
  94. package/server/service/figure/figure-public.ts +0 -60
  95. package/server/service/figure/figure-query.ts +0 -511
  96. package/server/service/figure/figure-release-check.test.ts +0 -63
  97. package/server/service/figure/figure-release-record-db.test.ts +0 -153
  98. package/server/service/figure/figure-release-size-db.test.ts +0 -156
  99. package/server/service/figure/figure-rename-db.test.ts +0 -237
  100. package/server/service/figure/figure-rename.test.ts +0 -117
  101. package/server/service/figure/figure-rename.ts +0 -143
  102. package/server/service/figure/figure-replaced-draft.ts +0 -99
  103. package/server/service/figure/figure-revert-db.test.ts +0 -163
  104. package/server/service/figure/figure-revert-race.pg.test.ts +0 -168
  105. package/server/service/figure/figure-revert.ts +0 -186
  106. package/server/service/figure/figure-subject.test.ts +0 -41
  107. package/server/service/figure/figure-subject.ts +0 -21
  108. package/server/service/figure/figure-thumbnail-db.test.ts +0 -279
  109. package/server/service/figure/figure-thumbnail-reader.ts +0 -20
  110. package/server/service/figure/figure-thumbnail-store.ts +0 -111
  111. package/server/service/figure/figure-thumbnail-upgrade.pg.test.ts +0 -93
  112. package/server/service/figure/figure-thumbnail-upgrade.ts +0 -70
  113. package/server/service/figure/figure-tool-doors.test.ts +0 -127
  114. package/server/service/figure/figure-tools-door-db.test.ts +0 -227
  115. package/server/service/figure/figure-tools.test.ts +0 -817
  116. package/server/service/figure/figure-tools.ts +0 -611
  117. package/server/service/figure/figure-type.ts +0 -241
  118. package/server/service/figure/figure-write.test.ts +0 -15
  119. package/server/service/figure/figure-write.ts +0 -131
  120. package/server/service/figure/figure.ts +0 -241
  121. package/server/service/figure/index.ts +0 -13
  122. package/server/service/figure-recipe/figure-recipe-resolver.ts +0 -38
  123. package/server/service/figure-recipe/figure-recipe-templates.test.ts +0 -47
  124. package/server/service/figure-recipe/figure-recipe-templates.ts +0 -70
  125. package/server/service/figure-recipe/figure-recipe-type.ts +0 -31
  126. package/server/service/figure-recipe/index.ts +0 -3
  127. package/server/service/index.ts +0 -34
  128. package/tsconfig.json +0 -14
@@ -1,510 +0,0 @@
1
- /**
2
- * What of a figure the model is shown (ADR-0061 2026-10-16 decision 4).
3
- *
4
- * The kernel keeps a figure up to 1 MiB (`FIGURE_JSON_MAX_BYTES`); that is whether the figure is valid. How much of it the
5
- * model reads at once is another question with its own number. A figure that fits is shown whole. One that does not is
6
- * shown as an outline -- its sections, and every part by name, shape and what it is fastened to -- and in full only the
7
- * parts and sections asked for, while they fit. A section too large for the outline is left out, named by its JSON
8
- * Pointer with the entries inside it, and read by pointer. The full source stays on the server: a proposal is applied
9
- * and judged on it, on the version the request was made on.
10
- *
11
- * Beside the source or the outline the view carries what the writer works from that the source does not say plainly: each
12
- * pipe as set-pipe takes it and the palette's token names. They are chosen inside the same budget -- a pipe with the part
13
- * shown, the tokens of the parts shown first -- so nothing is added to the view after it was measured (outside review
14
- * 2026-11-09: an outline of 2,335 characters went out as a prompt of 129,994, every pipe and token appended to it).
15
- *
16
- * A proposal on a figure shown in part changes only what it was shown: no part not read in full, and nothing inside a
17
- * section left out and not read. Read is what the server put before the writer, never what a model says it read.
18
- */
19
- import { figurePipesOf } from '@hatiolab/figure-model'
20
- import type { FigureAsset, FigureProposalStep } from '@hatiolab/figure-model'
21
-
22
- /** The most of a figure, serialized, the model is shown in one read. Not a validity limit: that is the kernel's. */
23
- export const FIGURE_CONTEXT_BUDGET = 100_000
24
-
25
- /** Of the budget, what the outline's sections may take; the rest is for the parts and sections read beside it. */
26
- const OUTLINE_SHARE = FIGURE_CONTEXT_BUDGET / 2
27
-
28
- /** How many entries of a section left out, or pointers in a reason, are named: the largest first. */
29
- const ENTRIES_NAMED = 20
30
-
31
- type Dynamic = any
32
-
33
- export interface FigureOutlinePart {
34
- id: string
35
- /** The operation that makes its shape (`box@1`, `extrude@1`, ...). */
36
- shape?: string
37
- /** What it is fastened to: `<part>.<face>`, `mounting-plane`, or nothing when unfastened. */
38
- on?: string
39
- /** Its joint, and how it moves: each axis's type in order (ADR-0159); no motion for a fastening. */
40
- joint?: { id: string; motion?: string[] }
41
- material?: string
42
- /** How long its part read is: what reading it costs. */
43
- chars: number
44
- }
45
-
46
- export interface FigureOmittedSection {
47
- /** The JSON Pointer of the section left out. */
48
- section: string
49
- chars: number
50
- /** Its largest entries, by pointer: what to read when the whole does not fit. */
51
- entries?: { section: string; chars: number }[]
52
- }
53
-
54
- export interface FigureOutline {
55
- /** The whole source's length, and the budget it does not fit. */
56
- chars: number
57
- budget: number
58
- parts: FigureOutlinePart[]
59
- /** Parts the outline had no room to list, past the ones listed. */
60
- partsNotListed?: number
61
- /** Sections left out because they would crowd the outline. */
62
- omitted?: FigureOmittedSection[]
63
- [section: string]: unknown
64
- }
65
-
66
- export interface FigurePartRead {
67
- appearance: Dynamic
68
- /** The fastenings that name it: its own (body1) and its children's (body0). */
69
- joints: Dynamic[]
70
- /** The graph nodes and constants under its name: `<part>` and `<part>.*` (a part id has no dot). */
71
- nodes: Dynamic[]
72
- constants: Dynamic[]
73
- }
74
-
75
- export interface FigureReadRequest {
76
- parts?: readonly string[]
77
- /** JSON Pointers (RFC 6901) into the source, such as `/document/capabilities/bindings/0`. */
78
- sections?: readonly string[]
79
- }
80
-
81
- export interface FigureView {
82
- whole: boolean
83
- /** Whole: the source as it is. */
84
- source?: FigureAsset
85
- outline?: FigureOutline
86
- /** The parts shown in full, by id, in the order asked. */
87
- parts?: Record<string, FigurePartRead>
88
- shown?: string[]
89
- /** The sections read, by pointer. */
90
- sections?: Record<string, unknown>
91
- /** Each pipe and duct of the parts in the view, as set-pipe takes it: every one when whole, the shown parts' in part. */
92
- pipes?: Record<string, unknown>
93
- /**
94
- * The palette's token names, when the view does not already hold the palette: the shown parts' first, as many as fit,
95
- * with how many more there are in /palette.
96
- */
97
- palette?: { tokens: string[]; more?: number }
98
- /** What was asked for and not shown, with why and what to read instead. */
99
- notRead?: ({ part: string; reason: string } | { section: string; reason: string })[]
100
- }
101
-
102
- const length = (value: unknown) => JSON.stringify(value)?.length ?? 0
103
- const under = (part: string) => (id: unknown) => id === part || (typeof id === 'string' && id.startsWith(`${part}.`))
104
-
105
- /* ── JSON Pointer (RFC 6901) ─────────────────────────────────────────────────────────── */
106
-
107
- const escapeToken = (token: string) => token.replace(/~/g, '~0').replace(/\//g, '~1')
108
- const pointerOf = (tokens: readonly (string | number)[]) => tokens.map(t => `/${escapeToken(String(t))}`).join('')
109
- function tokensOf(pointer: string): string[] | undefined {
110
- if (typeof pointer !== 'string' || !pointer.startsWith('/')) return undefined
111
- return pointer.slice(1).split('/').map(t => t.replace(/~1/g, '/').replace(/~0/g, '~'))
112
- }
113
- function valueAt(root: unknown, tokens: readonly string[]): { found: boolean; value?: unknown } {
114
- let at: Dynamic = root
115
- for (const token of tokens) {
116
- if (at === null || typeof at !== 'object' || !Object.prototype.hasOwnProperty.call(at, token)) return { found: false }
117
- at = at[token]
118
- }
119
- return { found: true, value: at }
120
- }
121
- /** `inner` is `outer` or inside it. */
122
- const within = (inner: string, outer: string) => inner === outer || inner.startsWith(`${outer}/`)
123
-
124
- /** A value's entries by pointer, largest first, the first ENTRIES_NAMED of them. */
125
- function entriesOf(pointer: string, value: unknown): { section: string; chars: number }[] {
126
- if (value === null || typeof value !== 'object') return []
127
- const tokens = tokensOf(pointer)!
128
- return Object.entries(value as object)
129
- .map(([key, entry]) => ({ section: pointerOf([...tokens, key]), chars: length(entry) }))
130
- .sort((a, b) => b.chars - a.chars)
131
- .slice(0, ENTRIES_NAMED)
132
- }
133
-
134
- /* ── Parts ───────────────────────────────────────────────────────────────────────────── */
135
-
136
- export function figurePartReadOf(asset: FigureAsset, part: string): FigurePartRead | undefined {
137
- const appearance = asset.appearance.find(a => a.target === part)
138
- if (!appearance) return undefined
139
- const model = asset.document.model as Dynamic
140
- const own = under(part)
141
- return {
142
- appearance,
143
- joints: ((asset as Dynamic).joints ?? []).filter((j: Dynamic) => j?.body1?.part === part || j?.body0?.part === part),
144
- nodes: model.nodes.filter((n: Dynamic) => own(n.id)),
145
- constants: model.constants.filter((c: Dynamic) => own(c.id))
146
- }
147
- }
148
-
149
- /** Where a part's records are, by pointer: for a part too large to read as one. */
150
- function partPointers(asset: FigureAsset, part: string): string[] {
151
- const model = asset.document.model as Dynamic
152
- const own = under(part)
153
- return [
154
- ...asset.appearance.flatMap((a, i) => (a.target === part ? [pointerOf(['appearance', i])] : [])),
155
- ...((asset as Dynamic).joints ?? []).flatMap((j: Dynamic, i: number) => (j?.body1?.part === part || j?.body0?.part === part ? [pointerOf(['joints', i])] : [])),
156
- ...model.nodes.flatMap((n: Dynamic, i: number) => (own(n.id) ? [pointerOf(['document', 'model', 'nodes', i])] : [])),
157
- ...model.constants.flatMap((c: Dynamic, i: number) => (own(c.id) ? [pointerOf(['document', 'model', 'constants', i])] : []))
158
- ]
159
- }
160
-
161
- /* ── The outline ─────────────────────────────────────────────────────────────────────── */
162
-
163
- /*
164
- The sections of an outline: every top-level field but the parts' records, the document's fields but its model, and the
165
- model's fields but its graph. Each may be left out by its pointer.
166
- */
167
- const PART_RECORDS = ['/document', '/appearance', '/joints', '/document/model', '/document/model/nodes', '/document/model/constants']
168
- function sectionsOf(asset: FigureAsset): string[] {
169
- const out: string[] = []
170
- const walk = (tokens: string[]) => {
171
- const { value } = valueAt(asset, tokens)
172
- for (const key of Object.keys(value as object)) {
173
- const pointer = pointerOf([...tokens, key])
174
- if (pointer === '/document' || pointer === '/document/model') walk([...tokens, key])
175
- else if (!PART_RECORDS.includes(pointer)) out.push(pointer)
176
- }
177
- }
178
- walk([])
179
- return out
180
- }
181
-
182
- function outlinePartsOf(asset: FigureAsset): FigureOutlinePart[] {
183
- const nodes = (asset.document.model as Dynamic).nodes
184
- const joints = (asset as Dynamic).joints ?? []
185
- return asset.appearance.map((look: Dynamic) => {
186
- const id = look.target
187
- const joint = joints.find((j: Dynamic) => j?.body1?.part === id)
188
- const body0 = joint?.body0
189
- const on = !joint ? undefined : body0?.part ? `${body0.part}.${body0.face}` : (body0?.plane ?? 'mounting-plane')
190
- const shape = nodes.find((n: Dynamic) => n.id === `${id}.shape`)?.op
191
- return {
192
- id,
193
- ...(shape ? { shape } : {}),
194
- ...(on ? { on } : {}),
195
- /* How it moves: each axis of its joint in order (ADR-0159); none for a fastening. */
196
- ...(joint ? { joint: { id: joint.id, ...(joint.motion?.length ? { motion: joint.motion.map((m: Dynamic) => m.type) } : {}) } } : {}),
197
- ...(look.material?.token ? { material: look.material.token } : {}),
198
- chars: length(figurePartReadOf(asset, id))
199
- }
200
- })
201
- }
202
-
203
- /** Writes a section into the outline at its pointer, making the objects on the way. */
204
- function place(outline: Dynamic, tokens: readonly string[], value: unknown) {
205
- let at = outline
206
- tokens.slice(0, -1).forEach(token => (at = at[token] ??= {}))
207
- at[tokens[tokens.length - 1]!] = value
208
- }
209
-
210
- /**
211
- * What the view wraps the outline in, read or not: an outline that took the whole budget left the view over it by these
212
- * (53 characters, a figure of 3000 long-named parts, 2026-10-19).
213
- */
214
- const VIEW_ENVELOPE = () => length({ whole: false, outline: {}, parts: {}, shown: [], sections: {}, notRead: [] }) - length({})
215
-
216
- /**
217
- * The outline: within OUTLINE_SHARE for its sections, the largest left out first; and, every field counted, within the
218
- * budget less what the view wraps it in -- a figure of very many parts lists them by id only, then as many as fit.
219
- */
220
- export function figureOutlineOf(asset: FigureAsset, chars = length(asset)): FigureOutline {
221
- const limit = FIGURE_CONTEXT_BUDGET - VIEW_ENVELOPE()
222
- const model = asset.document.model as Dynamic
223
- const sections = sectionsOf(asset).map(pointer => ({ pointer, value: valueAt(asset, tokensOf(pointer)!).value }))
224
- const sized = sections.map(s => ({ ...s, chars: length(s.value) })).sort((a, b) => b.chars - a.chars)
225
-
226
- let total = sized.reduce((sum, s) => sum + s.chars, 0)
227
- const omitted: FigureOmittedSection[] = []
228
- const kept = new Set(sized.map(s => s.pointer))
229
- for (const s of sized) {
230
- if (total <= OUTLINE_SHARE) break
231
- kept.delete(s.pointer)
232
- total -= s.chars
233
- const entries = entriesOf(s.pointer, s.value)
234
- omitted.push({ section: s.pointer, chars: s.chars, ...(entries.length ? { entries } : {}) })
235
- }
236
-
237
- const outline: Dynamic = { chars, budget: FIGURE_CONTEXT_BUDGET }
238
- place(outline, ['document', 'model', 'nodes'], model.nodes.length)
239
- place(outline, ['document', 'model', 'constants'], model.constants.length)
240
- for (const s of sections) if (kept.has(s.pointer)) place(outline, tokensOf(s.pointer)!, s.value)
241
- if (omitted.length) outline.omitted = omitted
242
-
243
- let parts: Dynamic[] = outlinePartsOf(asset)
244
- outline.parts = parts
245
- if (length(outline) > limit) outline.parts = parts = parts.map(p => ({ id: p.id }))
246
- if (length(outline) > limit) {
247
- /* As many as fit, in part order, with how many more there are. */
248
- let room = limit - length({ ...outline, parts: [], partsNotListed: parts.length })
249
- const listed: Dynamic[] = []
250
- for (const p of parts) {
251
- room -= length(p) + 1
252
- if (room < 0) break
253
- listed.push(p)
254
- }
255
- outline.parts = listed
256
- outline.partsNotListed = parts.length - listed.length
257
- }
258
- return outline as FigureOutline
259
- }
260
-
261
- /**
262
- * Every pipe and duct of a figure as set-pipe takes it, from one evaluation that every part shown takes its own from.
263
- * A figure with no swept shape is not evaluated for it.
264
- */
265
- function pipesOf(asset: FigureAsset): Record<string, unknown> {
266
- const swept = ((asset.document.model as Dynamic).nodes as Dynamic[]).some(n => typeof n?.op === 'string' && n.op.startsWith('sweep@'))
267
- return swept ? figurePipesOf(asset) : {}
268
- }
269
-
270
- /* ── The view ────────────────────────────────────────────────────────────────────────── */
271
-
272
- /**
273
- * The view of a figure the model is shown: whole when it fits, else the outline and what was asked for -- parts, then
274
- * sections, each in the order asked -- while it fits beside it. Every field counted, the view is within the budget.
275
- */
276
- export function figureViewOf(asset: FigureAsset, request: FigureReadRequest = {}): FigureView {
277
- const chars = length(asset)
278
- const pipes = pipesOf(asset)
279
- if (chars <= FIGURE_CONTEXT_BUDGET) {
280
- const whole: FigureView = { whole: true, source: asset, ...(Object.keys(pipes).length ? { pipes } : {}), palette: { tokens: Object.keys(asset.palette ?? {}) } }
281
- if (length(whole) <= FIGURE_CONTEXT_BUDGET) return whole
282
- }
283
-
284
- const outline = figureOutlineOf(asset, chars)
285
- const view: FigureView = { whole: false, outline, parts: {}, shown: [], sections: {}, pipes: {}, notRead: [] }
286
- const room = () => FIGURE_CONTEXT_BUDGET - length(view)
287
- const refuse = (one: { part: string; reason: string } | { section: string; reason: string }) => {
288
- if (length(one) + 1 <= room()) view.notRead!.push(one)
289
- }
290
-
291
- for (const part of [...new Set(request.parts ?? [])]) {
292
- const one = figurePartReadOf(asset, part)
293
- if (!one) {
294
- refuse({ part, reason: `${part} is not a part of this figure; the outline lists its parts` })
295
- continue
296
- }
297
- /* A pipe is shown with its set-pipe reading or not at all: the reading is how its path is written back. */
298
- const pipe = pipes[part]
299
- const cost = length(one) + length(part) + length(part) + 3 + (pipe ? length(pipe) + length(part) + 2 : 0)
300
- view.parts![part] = one
301
- view.shown!.push(part)
302
- if (pipe) view.pipes![part] = pipe
303
- if (room() >= 0) continue
304
- delete view.parts![part]
305
- delete view.pipes![part]
306
- view.shown!.pop()
307
- if (cost <= FIGURE_CONTEXT_BUDGET - length(outline)) {
308
- refuse({ part, reason: `${part} (${cost} characters) does not fit beside what is already read; read it in another call with fewer parts or sections` })
309
- } else {
310
- const pointers = partPointers(asset, part)
311
- refuse({
312
- part,
313
- reason:
314
- `${part} (${cost} characters) is too large to show beside the outline; read its records as sections: ` +
315
- `${pointers.slice(0, ENTRIES_NAMED).join(', ')}${pointers.length > ENTRIES_NAMED ? ` and ${pointers.length - ENTRIES_NAMED} more` : ''}. ` +
316
- 'Parts read as sections are not counted as read in full: they cannot be changed'
317
- })
318
- }
319
- }
320
-
321
- for (const section of [...new Set(request.sections ?? [])]) {
322
- const tokens = tokensOf(section)
323
- const at = tokens && tokens.length ? valueAt(asset, tokens) : { found: false }
324
- if (!at.found) {
325
- refuse({ section, reason: `there is no section ${section}; a section is a JSON Pointer such as /document/capabilities/bindings/0` })
326
- continue
327
- }
328
- const cost = length(at.value) + length(section) + 2
329
- view.sections![section] = at.value
330
- if (room() >= 0) continue
331
- delete view.sections![section]
332
- const entries = entriesOf(section, at.value)
333
- refuse({
334
- section,
335
- reason: entries.length
336
- ? `${section} (${cost} characters) is too large to show at once here; read its entries: ${entries.map(e => `${e.section} (${e.chars})`).join(', ')}`
337
- : `${section} is one value of ${length(at.value)} characters, larger than can be shown here; it cannot be read or changed by a proposal`
338
- })
339
- }
340
-
341
- /*
342
- The palette's token names, where the outline left /palette out: the shown parts' tokens first, then the rest, while
343
- they fit; the rest are counted, and /palette is read by pointer like any section left out.
344
- */
345
- if (!view.notRead!.length) delete view.notRead
346
- if (!Object.keys(view.pipes!).length) delete view.pipes
347
- if ((outline.omitted ?? []).some(o => o.section === '/palette')) {
348
- const used = view.shown!.map(part => (view.parts![part]!.appearance as Dynamic)?.material?.token).filter((t): t is string => typeof t === 'string')
349
- const all = Object.keys(asset.palette ?? {})
350
- const ordered = [...new Set([...used.filter(t => all.includes(t)), ...all])]
351
- const palette: { tokens: string[]; more?: number } = { tokens: [], more: ordered.length }
352
- view.palette = palette
353
- for (const token of ordered) {
354
- palette.tokens.push(token)
355
- palette.more = ordered.length - palette.tokens.length
356
- if (room() < 0) {
357
- palette.tokens.pop()
358
- palette.more = ordered.length - palette.tokens.length
359
- break
360
- }
361
- }
362
- if (!palette.more) delete palette.more
363
- }
364
- return view
365
- }
366
-
367
- /* ── What a proposal may change ──────────────────────────────────────────────────────── */
368
-
369
- type StepOf<K extends FigureProposalStep['kind']> = Extract<FigureProposalStep, { kind: K }>
370
- type Subject = string | undefined
371
-
372
- /**
373
- * The part each kind of step changes, read from the field the kernel reads for that kind (audit D4-R1: a `part` beside a
374
- * `set-material`'s `target` is not read by the kernel, so it may not stand in for it). Every kind of the kernel's step
375
- * type is here, or this does not compile. A step that changes no one part says so with undefined: what it does to the
376
- * figure's sections is held by `unreadSectionEdits`.
377
- */
378
- const SUBJECT: { [K in FigureProposalStep['kind']]: (step: StepOf<K>, base: FigureAsset) => Subject } = {
379
- 'add-part': () => undefined,
380
- 'set-section-corner': step => step.part,
381
- 'set-cavity': step => step.part,
382
- 'set-section': step => step.part,
383
- 'set-pipe': step => step.part,
384
- attach: step => step.part,
385
- 'link-dimension': step => step.part,
386
- 'follow-size': step => step.part,
387
- 'unlink-dimension': step => step.part,
388
- 'size-by-state': step => step.part,
389
- 'scale-position': step => step.part,
390
- 'fix-position': step => step.part,
391
- 'nudge-part': step => step.part,
392
- detach: step => step.part,
393
- 'make-repeat': step => step.part,
394
- 'set-repeat': step => step.part,
395
- 'remove-repeat': step => step.part,
396
- 'set-mounting-face': step => step.part,
397
- 'add-binding': step => step.part,
398
- 'set-joint': step => step.joint?.body1?.part,
399
- 'remove-joint': (step, base) => ((base as Dynamic).joints ?? []).find((j: Dynamic) => j?.id === step.id)?.body1?.part,
400
- 'set-material': step => step.target,
401
- 'set-segments': step => step.target,
402
- 'rename-part': step => step.target,
403
- 'set-visibility': step => step.target,
404
- 'remove-part': step => step.target,
405
- 'duplicate-part': step => step.target,
406
- 'mirror-part': step => step.target,
407
- 'move-part': step => step.target,
408
- 'set-placement': () => undefined,
409
- 'declare-instance-size': () => undefined,
410
- 'clear-mounting-face': () => undefined,
411
- 'scale-uniformly': () => undefined,
412
- 'follow-size-in-proportion': () => undefined,
413
- 'set-detail-level': () => undefined,
414
- 'set-item': () => undefined,
415
- 'add-design-input': () => undefined,
416
- 'set-input-range': () => undefined,
417
- 'publish-input': () => undefined,
418
- 'unpublish-input': () => undefined,
419
- 'remove-binding': () => undefined,
420
- 'set-slot-layout': () => undefined,
421
- 'remove-slot-layout': () => undefined,
422
- 'set-extension': () => undefined,
423
- rename: () => undefined,
424
- 'set-capabilities': () => undefined,
425
- 'set-capability-settings': () => undefined,
426
- 'set-state-input': () => undefined,
427
- 'set-state-defaults': () => undefined,
428
- 'set-driver': () => undefined,
429
- 'remove-driver': () => undefined,
430
- 'set-clip': () => undefined,
431
- 'copy-clip': () => undefined,
432
- 'set-motion-group': () => undefined,
433
- 'remove-motion-group': () => undefined,
434
- 'set-data-schema': () => undefined,
435
- 'rename-data-field': () => undefined,
436
- 'set-data-binding': () => undefined,
437
- 'remove-data-binding': () => undefined
438
- }
439
-
440
- /**
441
- * The steps that change a part of the base not shown in full, as reasons said back to the writer. A part may still be
442
- * named to fasten to or to follow -- the kernel works from it, nothing about it is guessed -- and a new part is the
443
- * steps' own. A kind this table does not know is refused: it is not let through unjudged.
444
- */
445
- export function unreadPartEdits(base: FigureAsset, steps: readonly FigureProposalStep[], view: FigureView): string[] {
446
- const existing = new Set(base.appearance.map(a => a.target))
447
- const shown = new Set(view.shown ?? [])
448
- const allowed = [...shown].join(', ') || 'none'
449
- return steps.flatMap((step, i) => {
450
- const kind = (step as Dynamic)?.kind
451
- if (typeof kind !== 'string') return []
452
- const judge = (SUBJECT as Record<string, (step: Dynamic, base: FigureAsset) => Subject>)[kind]
453
- if (!Object.prototype.hasOwnProperty.call(SUBJECT, kind) || !judge)
454
- return [`UNKNOWN_STEP: steps[${i}] has the kind "${kind}", which is not one of the commands; use only the commands taught.`]
455
- const part = judge(step, base)
456
- if (typeof part !== 'string' || !existing.has(part) || shown.has(part)) return []
457
- return [
458
- `UNREAD_PART: steps[${i}] (${kind}) changes "${part}", which was not shown in full, so its source is not known here. ` +
459
- `Change only the parts shown in full (${allowed}) or parts the steps add. To change ${part}, read it with getFigureSource parts and pass it in parts to proposeFigureCandidate.`
460
- ]
461
- })
462
- }
463
-
464
- /** Entries of a value, keyed by `id` when every entry of a list has a distinct one, else by place. */
465
- function keyedEntries(value: unknown): Map<string, { token: string; value: unknown }> {
466
- const out = new Map<string, { token: string; value: unknown }>()
467
- if (Array.isArray(value)) {
468
- const ids = value.map(v => (v && typeof v === 'object' && typeof (v as Dynamic).id === 'string' ? (v as Dynamic).id : undefined))
469
- const byId = ids.every(Boolean) && new Set(ids).size === ids.length
470
- value.forEach((v, i) => out.set(byId ? `id:${ids[i]}` : `at:${i}`, { token: String(i), value: v }))
471
- } else if (value && typeof value === 'object') {
472
- for (const [key, v] of Object.entries(value)) out.set(`key:${key}`, { token: key, value: v })
473
- }
474
- return out
475
- }
476
-
477
- /**
478
- * What of the sections left out of the outline and not read the steps changed, as reasons said back to the writer. Judged
479
- * on what the steps made, not on what they say: nothing that was there may be changed or taken away. A new entry -- a
480
- * record in a list, a key in a map -- is the steps' own.
481
- */
482
- export function unreadSectionEdits(base: FigureAsset, after: FigureAsset, view: FigureView): string[] {
483
- const read = Object.keys(view.sections ?? {})
484
- const reasons: string[] = []
485
- const say = (pointer: string, what: string) =>
486
- reasons.push(
487
- `UNREAD_SECTION: the steps ${what} ${pointer}, which was left out of the outline and not read, so it is not known here. ` +
488
- `Leave it as it is; to change it, read it with getFigureSource sections and pass it in sections to proposeFigureCandidate.`
489
- )
490
- const check = (pointer: string, before: unknown, now: unknown) => {
491
- if (reasons.length >= ENTRIES_NAMED || read.some(r => within(pointer, r))) return
492
- const records = (v: unknown) => v !== null && typeof v === 'object'
493
- if (!records(before) || !records(now) || Array.isArray(before) !== Array.isArray(now)) {
494
- if (JSON.stringify(before) !== JSON.stringify(now)) say(pointer, 'change')
495
- return
496
- }
497
- const later = keyedEntries(now)
498
- for (const [key, entry] of keyedEntries(before)) {
499
- const child = `${pointer}/${escapeToken(entry.token)}`
500
- const kept = later.get(key)
501
- if (kept) check(child, entry.value, kept.value)
502
- else if (!read.some(r => within(child, r))) say(child, 'remove')
503
- }
504
- }
505
- for (const omitted of view.outline?.omitted ?? []) {
506
- const tokens = tokensOf(omitted.section)!
507
- check(omitted.section, valueAt(base, tokens).value, valueAt(after, tokens).value)
508
- }
509
- return reasons
510
- }