@serene-pub/sdk 0.6.0-pr-1

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 (187) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +17 -0
  3. package/README.md +368 -0
  4. package/dist/actions.d.ts +572 -0
  5. package/dist/actions.js +829 -0
  6. package/dist/annexFields.d.ts +234 -0
  7. package/dist/annexFields.js +413 -0
  8. package/dist/announce.d.ts +464 -0
  9. package/dist/announce.js +426 -0
  10. package/dist/attributes.d.ts +670 -0
  11. package/dist/attributes.js +904 -0
  12. package/dist/bands.d.ts +125 -0
  13. package/dist/bands.js +256 -0
  14. package/dist/builder.d.ts +683 -0
  15. package/dist/builder.js +677 -0
  16. package/dist/candidates.d.ts +109 -0
  17. package/dist/candidates.js +118 -0
  18. package/dist/capabilities.d.ts +744 -0
  19. package/dist/capabilities.js +679 -0
  20. package/dist/channels.d.ts +106 -0
  21. package/dist/channels.js +129 -0
  22. package/dist/clauses.d.ts +40 -0
  23. package/dist/clauses.js +43 -0
  24. package/dist/component.d.ts +33 -0
  25. package/dist/component.js +27 -0
  26. package/dist/componentBuild.d.ts +48 -0
  27. package/dist/componentBuild.js +84 -0
  28. package/dist/componentClient.d.ts +100 -0
  29. package/dist/componentClient.js +13 -0
  30. package/dist/componentImports.d.ts +51 -0
  31. package/dist/componentImports.js +106 -0
  32. package/dist/componentModule.d.ts +8 -0
  33. package/dist/componentModule.js +270 -0
  34. package/dist/componentWire.d.ts +130 -0
  35. package/dist/componentWire.js +280 -0
  36. package/dist/config.d.ts +295 -0
  37. package/dist/config.js +231 -0
  38. package/dist/connections.d.ts +89 -0
  39. package/dist/connections.js +131 -0
  40. package/dist/declarations.d.ts +290 -0
  41. package/dist/declarations.js +794 -0
  42. package/dist/descriptors.d.ts +1774 -0
  43. package/dist/descriptors.js +1329 -0
  44. package/dist/dev.d.ts +136 -0
  45. package/dist/dev.js +161 -0
  46. package/dist/document.d.ts +202 -0
  47. package/dist/document.js +312 -0
  48. package/dist/engines.d.ts +117 -0
  49. package/dist/engines.js +203 -0
  50. package/dist/eventMapLaws.d.ts +63 -0
  51. package/dist/eventMapLaws.js +128 -0
  52. package/dist/events.d.ts +554 -0
  53. package/dist/events.js +633 -0
  54. package/dist/executor.d.ts +771 -0
  55. package/dist/executor.js +1829 -0
  56. package/dist/extension.d.ts +448 -0
  57. package/dist/extension.js +315 -0
  58. package/dist/facets.d.ts +53 -0
  59. package/dist/facets.js +60 -0
  60. package/dist/foldedSections.d.ts +74 -0
  61. package/dist/foldedSections.js +161 -0
  62. package/dist/genres.d.ts +507 -0
  63. package/dist/genres.js +633 -0
  64. package/dist/guardedConnection.d.ts +138 -0
  65. package/dist/guardedConnection.js +341 -0
  66. package/dist/handlebarsReach.d.ts +122 -0
  67. package/dist/handlebarsReach.js +222 -0
  68. package/dist/hash.d.ts +146 -0
  69. package/dist/hash.js +166 -0
  70. package/dist/hookGrants.d.ts +47 -0
  71. package/dist/hookGrants.js +68 -0
  72. package/dist/hooks.d.ts +290 -0
  73. package/dist/hooks.js +113 -0
  74. package/dist/hostElements.d.ts +447 -0
  75. package/dist/hostElements.js +702 -0
  76. package/dist/i18n.d.ts +94 -0
  77. package/dist/i18n.js +123 -0
  78. package/dist/identity.d.ts +93 -0
  79. package/dist/identity.js +135 -0
  80. package/dist/index.d.ts +85 -0
  81. package/dist/index.js +176 -0
  82. package/dist/layout.d.ts +194 -0
  83. package/dist/layout.js +40 -0
  84. package/dist/media.d.ts +202 -0
  85. package/dist/media.js +70 -0
  86. package/dist/messageBlocks.d.ts +487 -0
  87. package/dist/messageBlocks.js +683 -0
  88. package/dist/migration.d.ts +128 -0
  89. package/dist/migration.js +261 -0
  90. package/dist/names.d.ts +78 -0
  91. package/dist/names.js +136 -0
  92. package/dist/nodeInput.d.ts +399 -0
  93. package/dist/nodeInput.js +178 -0
  94. package/dist/participants.d.ts +291 -0
  95. package/dist/participants.js +252 -0
  96. package/dist/personGate.d.ts +62 -0
  97. package/dist/personGate.js +77 -0
  98. package/dist/pick.d.ts +84 -0
  99. package/dist/pick.js +103 -0
  100. package/dist/pluginHarness.d.ts +235 -0
  101. package/dist/pluginHarness.js +558 -0
  102. package/dist/pluginPermissions.d.ts +25 -0
  103. package/dist/pluginPermissions.js +97 -0
  104. package/dist/pluginRuleRef.d.ts +13 -0
  105. package/dist/pluginRuleRef.js +10 -0
  106. package/dist/predicates.d.ts +182 -0
  107. package/dist/predicates.js +249 -0
  108. package/dist/preview.d.ts +120 -0
  109. package/dist/preview.js +82 -0
  110. package/dist/promptBlocks.d.ts +130 -0
  111. package/dist/promptBlocks.js +197 -0
  112. package/dist/rankingDecisions.d.ts +60 -0
  113. package/dist/rankingDecisions.js +48 -0
  114. package/dist/receipt.d.ts +313 -0
  115. package/dist/receipt.js +60 -0
  116. package/dist/receiverRules.d.ts +24 -0
  117. package/dist/receiverRules.js +94 -0
  118. package/dist/refs.d.ts +117 -0
  119. package/dist/refs.js +137 -0
  120. package/dist/registry.d.ts +230 -0
  121. package/dist/registry.js +273 -0
  122. package/dist/review.d.ts +108 -0
  123. package/dist/review.js +97 -0
  124. package/dist/sampling.d.ts +152 -0
  125. package/dist/sampling.js +712 -0
  126. package/dist/scope.d.ts +65 -0
  127. package/dist/scope.js +128 -0
  128. package/dist/scripts.d.ts +243 -0
  129. package/dist/scripts.js +326 -0
  130. package/dist/sessionLayout.d.ts +339 -0
  131. package/dist/sessionLayout.js +503 -0
  132. package/dist/sessionSettings.d.ts +70 -0
  133. package/dist/sessionSettings.js +2 -0
  134. package/dist/settings.d.ts +501 -0
  135. package/dist/settings.js +604 -0
  136. package/dist/settingsSlot.d.ts +108 -0
  137. package/dist/settingsSlot.js +166 -0
  138. package/dist/shapes.d.ts +373 -0
  139. package/dist/shapes.js +408 -0
  140. package/dist/sheets.d.ts +165 -0
  141. package/dist/sheets.js +251 -0
  142. package/dist/statShapes.d.ts +96 -0
  143. package/dist/statShapes.js +112 -0
  144. package/dist/status.d.ts +103 -0
  145. package/dist/status.js +125 -0
  146. package/dist/storage.d.ts +145 -0
  147. package/dist/storage.js +33 -0
  148. package/dist/storyTime.d.ts +308 -0
  149. package/dist/storyTime.js +560 -0
  150. package/dist/surfaces.d.ts +501 -0
  151. package/dist/surfaces.js +292 -0
  152. package/dist/template.d.ts +312 -0
  153. package/dist/template.js +554 -0
  154. package/dist/templateCheck.d.ts +60 -0
  155. package/dist/templateCheck.js +516 -0
  156. package/dist/templateFit.d.ts +157 -0
  157. package/dist/templateFit.js +349 -0
  158. package/dist/templateIds.d.ts +138 -0
  159. package/dist/templateIds.js +144 -0
  160. package/dist/templateScopeAt.d.ts +131 -0
  161. package/dist/templateScopeAt.js +354 -0
  162. package/dist/testing.d.ts +177 -0
  163. package/dist/testing.js +383 -0
  164. package/dist/tokenizers.d.ts +113 -0
  165. package/dist/tokenizers.js +184 -0
  166. package/dist/turnLock.d.ts +96 -0
  167. package/dist/turnLock.js +79 -0
  168. package/dist/turnOrder.d.ts +143 -0
  169. package/dist/turnOrder.js +128 -0
  170. package/dist/validate.d.ts +74 -0
  171. package/dist/validate.js +1399 -0
  172. package/dist/values.d.ts +212 -0
  173. package/dist/values.js +390 -0
  174. package/dist/variables.d.ts +493 -0
  175. package/dist/variables.js +662 -0
  176. package/dist/verdicts.d.ts +275 -0
  177. package/dist/verdicts.js +348 -0
  178. package/dist/widgetDecls.d.ts +88 -0
  179. package/dist/widgetDecls.js +119 -0
  180. package/dist/widgets.d.ts +1527 -0
  181. package/dist/widgets.js +208 -0
  182. package/dist/wire.d.ts +126 -0
  183. package/dist/wire.js +140 -0
  184. package/dist/worldinfo.d.ts +150 -0
  185. package/dist/worldinfo.js +212 -0
  186. package/host-elements.d.ts +914 -0
  187. package/package.json +67 -0
@@ -0,0 +1,426 @@
1
+ import { compile } from './document.js';
2
+ const isDocument = (s) => 'schemaVersion' in s;
3
+ /** @public */
4
+ export function use(ref) {
5
+ const m = /^(.*?)@([~^]?\d[^@]*)$/.exec(ref);
6
+ return Object.freeze(m
7
+ ? { kind: 'external-ref', id: m[1], range: m[2] }
8
+ : { kind: 'external-ref', id: ref });
9
+ }
10
+ const isExternalRef = (v) => !!v && typeof v === 'object' && v.kind === 'external-ref';
11
+ /** The id a `SpecRef` names; a bare string is refused with the fix. @experimental */
12
+ export function specIdOf(v, where) {
13
+ if (typeof v === 'string')
14
+ throw new Error(`${where} names the spec '${v}' as a string — pass the spec value you built, ` +
15
+ `or use('${v}') for another package's`);
16
+ if (!v || typeof v !== 'object' || typeof v.id !== 'string')
17
+ throw new Error(`${where} names no spec — pass the spec value you built, or use('<spec id>') for another package's`);
18
+ return v.id;
19
+ }
20
+ /** The events a spec's inlet lock answers; undefined for another package's (`use()`). */
21
+ const lockEventsOf = (v) => {
22
+ if (isExternalRef(v))
23
+ return undefined;
24
+ const lock = v.input;
25
+ return lock?.events ? [...lock.events] : lock?.event ? [lock.event] : [];
26
+ };
27
+ /** The action keys a spec contributes; undefined for another package's (`use()`). */
28
+ const actionKeysOf = (v) => {
29
+ if (isExternalRef(v))
30
+ return undefined;
31
+ const contributes = 'meta' in v ? v.meta.contributes : v.contributes;
32
+ return actionsOf({ id: v.id, contributes }).map((a) => a.key);
33
+ };
34
+ import { genre as makeGenre, genreIdOf, sessionEvents } from './genres.js';
35
+ import { eventById, isEventId, isSessionEventDecl } from './events.js';
36
+ import { makeValueToolkit } from './values.js';
37
+ import { ACTION_IDENTITY, actionsOf } from './actions.js';
38
+ import { i18nFindings } from './i18n.js';
39
+ import { declarationFindings, labelFindings, storedSwap, subjectsOf, swapInputFindings, } from './declarations.js';
40
+ /**
41
+ * Author a config against a spec value, or another package's through `use()`.
42
+ * With a spec value the node keys are checked at build; through `use()` they
43
+ * are recorded and checked by the instance that has the spec.
44
+ * @experimental
45
+ */
46
+ export function config(spec, slug, meta, values) {
47
+ assertDisplayText(`config '${slug}'`, meta);
48
+ specIdOf(spec, `config '${slug}'`);
49
+ return { spec, slug, label: meta.label, description: meta.description, values };
50
+ }
51
+ /** The stored form of a config — the spec read down to its id. @experimental */
52
+ export function storedConfig(c) {
53
+ return {
54
+ spec: specIdOf(c?.spec, `config '${c?.slug}'`),
55
+ slug: c.slug,
56
+ label: c.label,
57
+ ...(c.description !== undefined ? { description: c.description } : {}),
58
+ values: c.values,
59
+ };
60
+ }
61
+ /**
62
+ * The display text rule (R-20) as a throw for `config()` / `preset()`, where
63
+ * the author is. `build()` collects the same findings instead, through the
64
+ * shared declaration pass.
65
+ */
66
+ function assertDisplayText(at, meta) {
67
+ const findings = labelFindings(at, meta);
68
+ if (findings.length)
69
+ throw new Error(`${at} declares display text a publish refuses (R-20):\n · ${findings.join('\n · ')}`);
70
+ }
71
+ /** The stored form of an event declaration; throws the first mistake, with the fix. @experimental */
72
+ export function storedEventDeclaration(d) {
73
+ const where = `event '${d?.event?.id ?? '?'}'`;
74
+ if (!isSessionEventDecl(d?.event))
75
+ throw new Error(`${where}: 'event' is the value defineSessionEvent() returned, not an id`);
76
+ if (typeof d.genre === 'string')
77
+ throw new Error(`${where} names its genre as a string — pass the genre value, or use('${d.genre}')`);
78
+ const genre = isExternalRef(d.genre) ? d.genre.id : genreIdOf(d.genre);
79
+ let recordedBy;
80
+ if (d.recordedBy === 'any')
81
+ recordedBy = 'any';
82
+ else {
83
+ if (!Array.isArray(d.recordedBy) || !d.recordedBy.length)
84
+ throw new Error(`${where} names nothing that may record it — list the subjects whose pipelines record it, or 'any'`);
85
+ recordedBy = d.recordedBy.flatMap((entry, n) => {
86
+ const at = `${where} recordedBy ${n + 1}`;
87
+ if (typeof entry === 'string') {
88
+ if (!isEventId(entry) || !eventById(entry))
89
+ throw new Error(`${at}: '${entry}' is not a declared event id — pass an event of the genre, a spec, or { spec, key }`);
90
+ if (entry === sessionEvents.sessionAction)
91
+ throw new Error(`${at}: the action event serves each action on its own — pick the action: { spec, key }, or pass the spec`);
92
+ return [entry];
93
+ }
94
+ if (isBindingObject(entry)) {
95
+ const { spec: ref, key } = entry;
96
+ const id = specIdOf(ref, at);
97
+ if (typeof key !== 'string')
98
+ throw new Error(`${at} picks from '${id}' without a key — { spec, key }`);
99
+ const keys = actionKeysOf(ref);
100
+ if (keys && !keys.includes(key))
101
+ throw new Error(`${at} picks '${key}' from '${id}', which contributes ${keys.map((k) => `'${k}'`).join(', ') || 'no actions'}`);
102
+ return [`${id}#${key}`];
103
+ }
104
+ specIdOf(entry, at);
105
+ const subjects = subjectsOf(entry);
106
+ if (!subjects.length)
107
+ throw new Error(`${at}: '${entry.id}' has no inlet lock — it serves no subject`);
108
+ return subjects;
109
+ });
110
+ }
111
+ return {
112
+ event: d.event.id,
113
+ payload: d.event.payload,
114
+ name: d.event.name,
115
+ description: d.event.description,
116
+ domain: d.event.domain,
117
+ genre,
118
+ recordedBy,
119
+ };
120
+ }
121
+ const isBindingObject = (v) => !!v && typeof v === 'object' && 'spec' in v;
122
+ /**
123
+ * The stored form of a preset. Every reference is read down to its id and
124
+ * checked against what the value says: a binding's events against its spec's
125
+ * lock, a config against the spec it was made for, an action key against
126
+ * the keys the spec declares. Throws the first mistake, with the fix.
127
+ * @experimental
128
+ */
129
+ export function preset(input) {
130
+ const where = `preset '${input?.slug}'`;
131
+ assertDisplayText(where, input);
132
+ if (typeof input.genre === 'string')
133
+ throw new Error(`${where} names its genre as a string — pass the genre value (import it), or use('${input.genre}')`);
134
+ const genre = isExternalRef(input.genre) ? input.genre.id : genreIdOf(input.genre);
135
+ const bindings = {};
136
+ if (!Array.isArray(input.bindings))
137
+ throw new Error(`${where}: bindings is a list of specs — [createSession, { spec: respond, config: tuned }]; ` +
138
+ `each is bound on the events its inlet lock answers`);
139
+ for (const [i, raw] of input.bindings.entries()) {
140
+ const entry = isBindingObject(raw) ? raw : { spec: raw };
141
+ const at = `${where} binding ${i + 1}`;
142
+ const id = specIdOf(entry.spec, at);
143
+ const locked = lockEventsOf(entry.spec);
144
+ let events = entry.events;
145
+ if (events === undefined) {
146
+ if (!locked)
147
+ throw new Error(`${at} binds '${id}', another package's spec — name the events it answers: ` +
148
+ `{ spec: use('${id}'), events: [ … ] }`);
149
+ if (!locked.length)
150
+ throw new Error(`${at} binds '${id}', which has no inlet lock — a preset binds a spec on the events its lock answers`);
151
+ events = locked;
152
+ }
153
+ else if (!events.length) {
154
+ throw new Error(`${at} binds '${id}' on no events — drop \`events\` to bind every event its lock answers`);
155
+ }
156
+ else if (locked) {
157
+ const outside = events.filter((e) => !locked.includes(e));
158
+ if (outside.length)
159
+ throw new Error(`${at} binds '${id}' on ${outside.map((e) => `'${e}'`).join(', ')}, which its inlet lock ` +
160
+ `does not answer (${locked.join(', ') || 'no lock'})`);
161
+ }
162
+ let configSlug;
163
+ if (entry.config !== undefined) {
164
+ const configured = specIdOf(entry.config?.spec, `${at} config`);
165
+ if (configured !== id)
166
+ throw new Error(`${at} binds '${id}' with config '${entry.config.slug}', which was made for '${configured}'`);
167
+ configSlug = entry.config.slug;
168
+ }
169
+ for (const event of events) {
170
+ if (bindings[event])
171
+ throw new Error(`${where} binds '${event}' twice — to '${bindings[event].spec}' and to '${id}'`);
172
+ bindings[event] = { spec: id, ...(configSlug !== undefined ? { config: configSlug } : {}) };
173
+ }
174
+ }
175
+ const include = (pick, n) => {
176
+ const at = `${where} action ${n + 1}`;
177
+ if (typeof pick === 'string')
178
+ throw new Error(`${at} names '${pick}' as a string — pass the spec value (every action it contributes) ` +
179
+ `or { spec, key } for one`);
180
+ if (isBindingObject(pick)) {
181
+ const { spec: ref, key } = pick;
182
+ const id = specIdOf(ref, at);
183
+ if (typeof key !== 'string' || !ACTION_IDENTITY.test(`${id}#${key}`))
184
+ throw new Error(`${at} picks from '${id}' without a valid key — { spec, key: '<action key>' }`);
185
+ const keys = actionKeysOf(ref);
186
+ if (keys && !keys.includes(key))
187
+ throw new Error(`${at} picks '${key}' from '${id}', which contributes ${keys.length ? keys.map((k) => `'${k}'`).join(', ') : 'no actions'}`);
188
+ return [`${id}#${key}`];
189
+ }
190
+ const id = specIdOf(pick, at);
191
+ const keys = actionKeysOf(pick);
192
+ if (!keys)
193
+ throw new Error(`${at} includes '${id}', another package's spec — name the action: { spec: use('${id}'), key: '<action key>' }`);
194
+ if (!keys.length)
195
+ throw new Error(`${at} includes '${id}', which contributes no actions — nothing to bring along`);
196
+ return keys.map((k) => `${id}#${k}`);
197
+ };
198
+ const swapsOf = (swaps) => {
199
+ const faults = swapInputFindings(swaps);
200
+ if (faults.length)
201
+ throw new Error(`${where} defaults.swaps: ${faults.join('; ')}`);
202
+ return swaps.map((c) => storedSwap(c));
203
+ };
204
+ return {
205
+ slug: input.slug,
206
+ genre,
207
+ label: input.label,
208
+ description: input.description,
209
+ bindings,
210
+ ...(input.actions ? { actions: { include: input.actions.include.flatMap(include) } } : {}),
211
+ ...(input.defaults
212
+ ? {
213
+ defaults: {
214
+ ...input.defaults,
215
+ ...(input.defaults.swaps ? { swaps: swapsOf(input.defaults.swaps) } : {}),
216
+ },
217
+ }
218
+ : {}),
219
+ // Omitted rather than defaulted to `false`, so a declaration that says
220
+ // nothing hashes as it always did — the announcement is content-hashed
221
+ // like every other declaration here.
222
+ ...(input.enabled === undefined ? {} : { enabled: input.enabled }),
223
+ };
224
+ }
225
+ const NS = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/;
226
+ /** @internal The builder behind `announcementOf()`; author a package with `defineExtension()`. */
227
+ export class AnnouncementBuilder {
228
+ identity;
229
+ /** The context-bound value toolkit — custom kinds mint under this package. */
230
+ v;
231
+ _genres = new Map();
232
+ _hooks = {};
233
+ _pipelines = [];
234
+ _configs = [];
235
+ _presets = [];
236
+ _prompts = [];
237
+ _surfaces;
238
+ _components = [];
239
+ _swaps = [];
240
+ _events = [];
241
+ constructor(identity) {
242
+ this.identity = identity;
243
+ if (!NS.test(identity.ns))
244
+ throw new Error(`'${identity.ns}' is not a valid package namespace — lowercase, digits, hyphens, dots`);
245
+ this.v = makeValueToolkit(identity.ns);
246
+ }
247
+ /** Context sugar: mints `${ns}:genre/${name}` so the id cannot be mistyped. */
248
+ genre(name, props) {
249
+ const decl = makeGenre(`${this.identity.ns}:genre/${name}`, props);
250
+ this._genres.set(decl.id, decl);
251
+ return decl;
252
+ }
253
+ genres(map) {
254
+ for (const decl of Object.values(map)) {
255
+ const owner = decl.id.slice(0, decl.id.indexOf(':'));
256
+ if (owner !== this.identity.ns)
257
+ throw new Error(`genre '${decl.id}' is owned by '${owner}' — a package declares only its own ` +
258
+ `genres; referencing another's is done from a spec's input binding`);
259
+ this._genres.set(decl.id, decl);
260
+ }
261
+ return this;
262
+ }
263
+ /** Declarations only — implementations bind by id per trust domain (24 §11). */
264
+ hooks(map) {
265
+ for (const [key, decl] of Object.entries(map))
266
+ this._hooks[`${this.identity.ns}:hook/${key}`] = decl;
267
+ return this;
268
+ }
269
+ pipelines(...specs) {
270
+ this._pipelines.push(...specs);
271
+ return this;
272
+ }
273
+ configs(...configs) {
274
+ this._configs.push(...configs.map(storedConfig));
275
+ return this;
276
+ }
277
+ prompts(...prompts) {
278
+ this._prompts.push(...prompts);
279
+ return this;
280
+ }
281
+ presets(...presets) {
282
+ this._presets.push(...presets.map(preset));
283
+ return this;
284
+ }
285
+ /**
286
+ * The frame surfaces this package ships (20 §12): documents mounted in
287
+ * opaque-origin iframes. Declared, never discovered — an instance renders
288
+ * what the manifest says and `serene-pub preview` renders the same list,
289
+ * so a surface a modder previewed is a surface an instance will offer.
290
+ */
291
+ surfaces(decl) {
292
+ this._surfaces = { ...this._surfaces, ...decl };
293
+ return this;
294
+ }
295
+ /** In-document components (10 §2, virtual tier) — code trust at install. */
296
+ components(...components) {
297
+ this._components.push(...components);
298
+ return this;
299
+ }
300
+ /**
301
+ * Offer this package's definitions on another package's swappable nodes:
302
+ * `.swaps({ spec: chatTurnOrder, node: 'decide.rules.strategy', definition: myStrategy })`,
303
+ * the spec a value you imported, or `use('<spec id>')`. For a node of this package's own spec, list
304
+ * the swap on the node itself with `expose.swaps` instead.
305
+ */
306
+ swaps(...contributions) {
307
+ const faults = swapInputFindings(contributions);
308
+ if (faults.length)
309
+ throw new Error(`swaps:\n · ${faults.join('\n · ')}`);
310
+ this._swaps.push(...contributions);
311
+ return this;
312
+ }
313
+ /**
314
+ * Validate the announcement as a whole and compile it to the document.
315
+ * Errors carry exact paths — the teaching-error pattern (15 §1.3).
316
+ */
317
+ build() {
318
+ const errors = [];
319
+ const ns = this.identity.ns;
320
+ // The display text (R-20, U5i): the package's own title, summary and
321
+ // description. Every prompt's, config's and preset's label is checked
322
+ // by the shared declaration pass below, so a hand-built document gets
323
+ // the same answer the constructor gives.
324
+ errors.push(...i18nFindings(this.identity.title, 'identity.title', { required: true }));
325
+ errors.push(...i18nFindings(this.identity.summary, 'identity.summary'));
326
+ errors.push(...i18nFindings(this.identity.description, 'identity.description'));
327
+ // Every announced id lives under the package namespace. Kept here rather
328
+ // than in the shared pass because `defineExtension` refuses the same
329
+ // thing in its own words, naming the plugin slug (D-1).
330
+ for (const s of this._pipelines) {
331
+ const owner = s.id.includes(':') ? s.id.slice(0, s.id.indexOf(':')) : undefined;
332
+ if (owner !== ns)
333
+ errors.push(`pipeline '${s.id}' is not under this package's namespace '${ns}'`);
334
+ }
335
+ // Everything else a package declares — genres, the input lock, create
336
+ // pipelines, actions, prompts, configs, presets, surfaces, components —
337
+ // is checked by the pass `defineExtension` runs too (D-1), so one
338
+ // mistake gets one sentence wherever it was written.
339
+ const found = declarationFindings({
340
+ ns,
341
+ genres: [...this._genres.values()],
342
+ pipelines: this._pipelines,
343
+ prompts: this._prompts,
344
+ configs: this._configs,
345
+ presets: this._presets,
346
+ surfaces: this._surfaces,
347
+ components: this._components,
348
+ swaps: this._swaps,
349
+ events: this._events,
350
+ });
351
+ errors.push(...found.errors);
352
+ const coverage = found.coverage;
353
+ if (errors.length)
354
+ throw new AnnouncementError(errors, coverage);
355
+ return {
356
+ document: {
357
+ schemaVersion: 1,
358
+ identity: this.identity,
359
+ genres: [...this._genres.values()],
360
+ hooks: this._hooks,
361
+ pipelines: this._pipelines.map((s) => (isDocument(s) ? s : compile(s))),
362
+ prompts: this._prompts,
363
+ configs: this._configs,
364
+ presets: this._presets,
365
+ ...(this._surfaces ? { surfaces: this._surfaces } : {}),
366
+ components: this._components,
367
+ ...(this._swaps.length ? { swaps: this._swaps.map(storedSwap) } : {}),
368
+ ...(this._events.length ? { events: this._events } : {}),
369
+ requires: found.requires,
370
+ },
371
+ coverage,
372
+ };
373
+ }
374
+ }
375
+ /** Build refusal that still carries the coverage — the report is the error's context. @experimental */
376
+ export class AnnouncementError extends Error {
377
+ errors;
378
+ coverage;
379
+ constructor(errors, coverage) {
380
+ super(`announcement refused:\n - ${errors.join('\n - ')}`);
381
+ this.errors = errors;
382
+ this.coverage = coverage;
383
+ this.name = 'AnnouncementError';
384
+ }
385
+ }
386
+ /** @internal Author a package with `defineExtension()`; this is the serializer behind it. */
387
+ export function announce(identity) {
388
+ return new AnnouncementBuilder(identity);
389
+ }
390
+ /**
391
+ * The announcement document a `defineExtension()` declaration compiles to —
392
+ * what a host syncs and what the coverage report reads. Core declares itself
393
+ * with `defineExtension()` like any package and is announced through this.
394
+ *
395
+ * @internal Author a package with `defineExtension()`; this is the host's
396
+ * view of it.
397
+ */
398
+ export function announcementOf(e) {
399
+ const builder = announce({
400
+ ns: e.slug,
401
+ author: e.author ?? e.slug,
402
+ title: e.name,
403
+ ...(e.repo !== undefined ? { repo: e.repo } : {}),
404
+ ...(e.description !== undefined ? { summary: e.description } : {}),
405
+ });
406
+ if (e.hooks && Object.keys(e.hooks).length)
407
+ builder.hooks(e.hooks);
408
+ if (e.genres?.length)
409
+ builder.genres(Object.fromEntries(e.genres.map((g) => [g.id, g])));
410
+ if (e.pipelines?.length)
411
+ builder.pipelines(...e.pipelines);
412
+ if (e.prompts?.length)
413
+ builder.prompts(...e.prompts);
414
+ if (e.surfaces)
415
+ builder.surfaces(e.surfaces);
416
+ if (e.components?.length)
417
+ builder.components(...e.components);
418
+ // Already read down to ids by `defineExtension()`: the stored shapes go in as they are.
419
+ const stored = builder;
420
+ stored._configs.push(...(e.configs ?? []));
421
+ stored._presets.push(...(e.presets ?? []));
422
+ stored._swaps.push(...(e.swaps ?? []));
423
+ builder._events.push(...(e.events ?? []));
424
+ return builder.build();
425
+ }
426
+ //# sourceMappingURL=announce.js.map