@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,125 @@
1
+ /**
2
+ * Bands as template variables (typed templates P2, 2026-09-27).
3
+ *
4
+ * A **band** is the ranker's word for one source's slice of the context window
5
+ * (`bandIntent`, candidates.ts). A source that puts its own band in the window
6
+ * — Twenty Questions' secret entry — now also **declares** it: the key it
7
+ * publishes and the registered variable that says how the band is laid out.
8
+ *
9
+ * ```ts
10
+ * const varSecretEntry = definePluginVariable('showcase.twenty-questions', {
11
+ * id: 'showcase.twenty-questions:var/secret-entry@1',
12
+ * scope: { secretEntry: { type: 'record', of: { type: 'string' } } },
13
+ * sample: { 'The Brass Clock': 'A clock that rings when nobody winds it.' },
14
+ * })
15
+ * describeTaskDefinition({ …, bands: { secretEntry: varSecretEntry } })
16
+ * // …and a context template places it as {{{secretEntry}}}.
17
+ * ```
18
+ *
19
+ * Declared, a band is a **top-level template name**: Assemble exposes it as
20
+ * `{{{secretEntry}}}`, rendered through the variable's selected layout (the
21
+ * in-code fallback is the band as title-keyed minified JSON). So a band key is
22
+ * an identifier, and it means one thing: two declarers naming the same key
23
+ * with different variables, or a band shadowing a name core already renders,
24
+ * is refused with both declarers named.
25
+ *
26
+ * Which bands a node renders is its own `variables` slot's `renders` plus —
27
+ * when the slot says `rendersBands` — every band declared upstream of the
28
+ * named in-port, followed through any node that takes candidates in
29
+ * (concatenation, fusion, ranking). `rendersAt` is that answer.
30
+ */
31
+ import { type Descriptor, type SlotDecl } from './descriptors.js';
32
+ import type { DocEdge, DocNode } from './document.js';
33
+ import { type VariableDecl, type VariableId } from './variables.js';
34
+ /** A band's key — a top-level template name, so an identifier. @experimental */
35
+ export type BandKey = string;
36
+ /** @experimental */
37
+ export declare const isBandKey: (key: string) => boolean;
38
+ /** `secret-entry` → `secretEntry`: the identifier a refusal suggests. @internal */
39
+ export declare function bandKeySuggestion(key: string): string;
40
+ /**
41
+ * Names Assemble puts at the top of a context template that are not bands and
42
+ * not registered variables — what a band key may never be.
43
+ *
44
+ * Assemble's own values. A band called `budget` would shadow the budget a
45
+ * template reads, silently, on every turn.
46
+ * @internal
47
+ */
48
+ export declare const ASSEMBLE_OWN_TEMPLATE_NAMES: readonly string[];
49
+ /**
50
+ * Refuse a definition's band declarations — at `describe…Definition`, where
51
+ * the author is.
52
+ *
53
+ * - the key is an identifier (a band is a template name);
54
+ * - the variable is registered (`defineVariable` / `definePluginVariable`) and
55
+ * its scope declares the key, because a layout renders `{ [key]: value }`;
56
+ * - the key does not shadow a name core renders — Assemble's own, or a
57
+ * core variable's that is not this band's own variable;
58
+ * - no other registered definition declares the key with a different variable.
59
+ *
60
+ * Each refusal names the fix.
61
+ * @internal
62
+ */
63
+ export declare function checkBandDeclarations(d: Descriptor, others: Iterable<Descriptor>): void;
64
+ /**
65
+ * What `bandsReaching` needs from a node's definition: its in-ports and the
66
+ * bands it declares. A `Descriptor` satisfies it, and so does a registry row
67
+ * (whose policy carries the band ids, `DefinitionPolicy.bands`) — which is how
68
+ * a host reading rows answers without loading a plugin (F6).
69
+ * @experimental
70
+ */
71
+ export interface BandSource {
72
+ ports?: {
73
+ in?: Record<string, unknown>;
74
+ };
75
+ bands?: Record<BandKey, VariableDecl | VariableId>;
76
+ /**
77
+ * The out-ports that carry a band (`Descriptor.bandPorts`); a band not
78
+ * named here is carried on every out-port.
79
+ */
80
+ bandPorts?: Record<BandKey, readonly string[]>;
81
+ }
82
+ /** One declared band as it reaches a node. @experimental */
83
+ export interface ReachingBand {
84
+ variable: VariableId;
85
+ /** The node that declares it — `key (definitionId)`, for a refusal to name. */
86
+ declarer: string;
87
+ }
88
+ /** Two declarers naming one band differently, or a band shadowing a name the node renders. @experimental */
89
+ export declare class BandCollisionError extends Error {
90
+ }
91
+ /**
92
+ * Every band declared upstream of `nodeKey`'s in-port `port`.
93
+ *
94
+ * Walks the edges backwards from the port: each producer's declared `bands`
95
+ * are collected, and the walk continues through every in-port the producer
96
+ * declares as a candidates port — so a band declared at a source reaches a
97
+ * ranker through a concatenation or a fusion, and Assemble through the ranker.
98
+ * A producer declaring no candidates in-port is where the walk stops. A band
99
+ * whose declarer names the out-ports carrying it (`bandPorts`) reaches only
100
+ * through an edge from one of those ports.
101
+ *
102
+ * Throws `BandCollisionError`, naming both declarers, when two of them name
103
+ * one key with different variables.
104
+ * @experimental
105
+ */
106
+ export declare function bandsReaching(doc: {
107
+ nodes: readonly DocNode[];
108
+ edges: readonly DocEdge[];
109
+ }, nodeKey: string, port: string, describe?: (node: DocNode) => BandSource | undefined): Record<BandKey, ReachingBand>;
110
+ /**
111
+ * What a `variables` slot renders at one node of a document: its static
112
+ * `renders`, plus — when it declares `rendersBands` — every band declared
113
+ * upstream of that in-port.
114
+ *
115
+ * A band the static half already renders under the same variable is the same
116
+ * name, not a collision (core's lore queries declare `worldLore`; Assemble
117
+ * renders `worldLore`). One declared under a different variable, or shadowing
118
+ * a name Assemble renders itself, is refused naming both.
119
+ * @experimental
120
+ */
121
+ export declare function rendersAt(doc: {
122
+ nodes: readonly DocNode[];
123
+ edges: readonly DocEdge[];
124
+ }, nodeKey: string, slot: Pick<SlotDecl, 'renders' | 'rendersBands'>, describe?: (node: DocNode) => BandSource | undefined): Record<string, VariableId>;
125
+ //# sourceMappingURL=bands.d.ts.map
package/dist/bands.js ADDED
@@ -0,0 +1,256 @@
1
+ /**
2
+ * Bands as template variables (typed templates P2, 2026-09-27).
3
+ *
4
+ * A **band** is the ranker's word for one source's slice of the context window
5
+ * (`bandIntent`, candidates.ts). A source that puts its own band in the window
6
+ * — Twenty Questions' secret entry — now also **declares** it: the key it
7
+ * publishes and the registered variable that says how the band is laid out.
8
+ *
9
+ * ```ts
10
+ * const varSecretEntry = definePluginVariable('showcase.twenty-questions', {
11
+ * id: 'showcase.twenty-questions:var/secret-entry@1',
12
+ * scope: { secretEntry: { type: 'record', of: { type: 'string' } } },
13
+ * sample: { 'The Brass Clock': 'A clock that rings when nobody winds it.' },
14
+ * })
15
+ * describeTaskDefinition({ …, bands: { secretEntry: varSecretEntry } })
16
+ * // …and a context template places it as {{{secretEntry}}}.
17
+ * ```
18
+ *
19
+ * Declared, a band is a **top-level template name**: Assemble exposes it as
20
+ * `{{{secretEntry}}}`, rendered through the variable's selected layout (the
21
+ * in-code fallback is the band as title-keyed minified JSON). So a band key is
22
+ * an identifier, and it means one thing: two declarers naming the same key
23
+ * with different variables, or a band shadowing a name core already renders,
24
+ * is refused with both declarers named.
25
+ *
26
+ * Which bands a node renders is its own `variables` slot's `renders` plus —
27
+ * when the slot says `rendersBands` — every band declared upstream of the
28
+ * named in-port, followed through any node that takes candidates in
29
+ * (concatenation, fusion, ranking). `rendersAt` is that answer.
30
+ */
31
+ import { getDefinition } from './descriptors.js';
32
+ import { allVariables, getVariable } from './variables.js';
33
+ /**
34
+ * The shape a candidates port carries. Written out rather than imported from
35
+ * `shapes.ts`, which reaches the descriptor module this one is read from.
36
+ */
37
+ const CANDIDATES_SHAPE = 'core:shape/context-candidates@1';
38
+ /** Letters, digits and `_`, not starting with a digit — a bare name in both core engines. */
39
+ const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
40
+ /** @experimental */
41
+ export const isBandKey = (key) => IDENTIFIER.test(key);
42
+ /** `secret-entry` → `secretEntry`: the identifier a refusal suggests. @internal */
43
+ export function bandKeySuggestion(key) {
44
+ const camel = key
45
+ .replace(/[^A-Za-z0-9_]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''))
46
+ .replace(/^[^A-Za-z_]+/, '');
47
+ return camel || 'band';
48
+ }
49
+ /**
50
+ * Names Assemble puts at the top of a context template that are not bands and
51
+ * not registered variables — what a band key may never be.
52
+ *
53
+ * Assemble's own values. A band called `budget` would shadow the budget a
54
+ * template reads, silently, on every turn.
55
+ * @internal
56
+ */
57
+ export const ASSEMBLE_OWN_TEMPLATE_NAMES = [
58
+ 'sessionMessages',
59
+ 'injectionsByIndex',
60
+ 'budget',
61
+ 'postHistory',
62
+ 'blocks',
63
+ 'prompts',
64
+ ];
65
+ /** The declared variable id of one band entry — a declaration, or its id as a registry row carries it. */
66
+ const variableIdOf = (v) => typeof v === 'string' ? v : v?.id;
67
+ /**
68
+ * A **core** variable that already renders a top-level name, if any — the
69
+ * names core's own nodes put in a template (`characters`, `scenario`, …). A
70
+ * plugin variable sharing a root is not by itself a collision: two plugins'
71
+ * bands meaning different things is, and the definitions check names both.
72
+ */
73
+ const otherVariableRendering = (key, id) => allVariables().find((v) => v.id !== id &&
74
+ v.id.startsWith('core:') &&
75
+ Object.prototype.hasOwnProperty.call(v.scope, key));
76
+ /**
77
+ * Refuse a definition's band declarations — at `describe…Definition`, where
78
+ * the author is.
79
+ *
80
+ * - the key is an identifier (a band is a template name);
81
+ * - the variable is registered (`defineVariable` / `definePluginVariable`) and
82
+ * its scope declares the key, because a layout renders `{ [key]: value }`;
83
+ * - the key does not shadow a name core renders — Assemble's own, or a
84
+ * core variable's that is not this band's own variable;
85
+ * - no other registered definition declares the key with a different variable.
86
+ *
87
+ * Each refusal names the fix.
88
+ * @internal
89
+ */
90
+ export function checkBandDeclarations(d, others) {
91
+ const bands = d.bands;
92
+ for (const key of Object.keys(d.bandPorts ?? {}))
93
+ if (!bands || !Object.prototype.hasOwnProperty.call(bands, key))
94
+ throw new Error(`'${d.id}' names band '${key}' in bandPorts but does not declare it in bands. ` +
95
+ `Declare it (bands: { ${key}: varMyBand }), or drop it from bandPorts.`);
96
+ if (!bands)
97
+ return;
98
+ for (const [key, decl] of Object.entries(bands)) {
99
+ if (!isBandKey(key))
100
+ throw new Error(`'${d.id}' declares band '${key}': a band key is a top-level template name, so it must ` +
101
+ `be an identifier — letters, digits and '_', not starting with a digit. Rename it ` +
102
+ `'${bandKeySuggestion(key)}', and emit that same key from bandIntent() and as each ` +
103
+ `candidate's source.`);
104
+ const id = variableIdOf(decl);
105
+ if (!id || !decl || typeof decl !== 'object')
106
+ throw new Error(`'${d.id}' declares band '${key}' without a variable. Declare one with ` +
107
+ `definePluginVariable() (defineVariable() in core) whose scope names '${key}', and ` +
108
+ `pass the declaration: bands: { ${key}: varMyBand }.`);
109
+ const registered = getVariable(id);
110
+ if (!registered)
111
+ throw new Error(`'${d.id}' declares band '${key}' with variable '${id}', which is not registered. ` +
112
+ `Declare it with definePluginVariable() (defineVariable() in core) before the ` +
113
+ `definition that names it — the layout picker and the template editor read it ` +
114
+ `from the registry.`);
115
+ if (!Object.prototype.hasOwnProperty.call(registered.scope, key))
116
+ throw new Error(`'${d.id}' declares band '${key}' with variable '${id}', whose scope does not declare ` +
117
+ `'${key}' (it declares ${Object.keys(registered.scope).map((k) => `'${k}'`).join(', ') || 'nothing'}). ` +
118
+ `A layout renders the band as {{{${key}}}}, so add '${key}' to the variable's scope — ` +
119
+ `or rename the band to the key the variable declares.`);
120
+ if (ASSEMBLE_OWN_TEMPLATE_NAMES.includes(key))
121
+ throw new Error(`'${d.id}' declares band '${key}', which collides with Assemble's own '${key}' — a ` +
122
+ `template reading {{{${key}}}} would get one or the other depending on order. ` +
123
+ `Rename the band.`);
124
+ const core = otherVariableRendering(key, id);
125
+ if (core)
126
+ throw new Error(`'${d.id}' declares band '${key}' as '${id}', which collides with '${core.id}' — that ` +
127
+ `variable already renders the top-level name '${key}'. A band key means one thing; ` +
128
+ `rename the band.`);
129
+ const ports = d.bandPorts?.[key];
130
+ if (ports !== undefined) {
131
+ const out = Object.keys(d.ports?.out ?? {});
132
+ const unknown = ports.filter((p) => !out.includes(p));
133
+ if (!ports.length || unknown.length)
134
+ throw new Error(`'${d.id}' says band '${key}' is carried on ` +
135
+ (ports.length
136
+ ? `${unknown.map((p) => `'${p}'`).join(', ')}, which ${unknown.length === 1 ? 'is not an out-port' : 'are not out-ports'} it declares`
137
+ : 'no out-port at all') +
138
+ ` (it declares ${out.map((p) => `'${p}'`).join(', ') || 'none'}). Name the out-ports ` +
139
+ `that publish the band's candidates in bandPorts, or leave '${key}' out of bandPorts ` +
140
+ `if every out-port may carry it.`);
141
+ }
142
+ for (const other of others) {
143
+ if (other.id === d.id)
144
+ continue;
145
+ const theirs = variableIdOf(other.bands?.[key]);
146
+ if (theirs && theirs !== id)
147
+ throw new Error(`'${d.id}' declares band '${key}' as '${id}', but '${other.id}' already declares ` +
148
+ `'${key}' as '${theirs}'. A band key is a top-level template name and means one ` +
149
+ `thing — rename one of the two bands.`);
150
+ }
151
+ }
152
+ }
153
+ /** Two declarers naming one band differently, or a band shadowing a name the node renders. @experimental */
154
+ export class BandCollisionError extends Error {
155
+ }
156
+ const shapeIdOf = (s) => typeof s === 'string' ? s : (s?.id ?? undefined);
157
+ const defaultDescribe = (n) => getDefinition(`${n.definitionId}@${n.definitionVersion}`);
158
+ /**
159
+ * Every band declared upstream of `nodeKey`'s in-port `port`.
160
+ *
161
+ * Walks the edges backwards from the port: each producer's declared `bands`
162
+ * are collected, and the walk continues through every in-port the producer
163
+ * declares as a candidates port — so a band declared at a source reaches a
164
+ * ranker through a concatenation or a fusion, and Assemble through the ranker.
165
+ * A producer declaring no candidates in-port is where the walk stops. A band
166
+ * whose declarer names the out-ports carrying it (`bandPorts`) reaches only
167
+ * through an edge from one of those ports.
168
+ *
169
+ * Throws `BandCollisionError`, naming both declarers, when two of them name
170
+ * one key with different variables.
171
+ * @experimental
172
+ */
173
+ export function bandsReaching(doc, nodeKey, port, describe = defaultDescribe) {
174
+ const out = {};
175
+ const seen = new Set();
176
+ const byKey = new Map(doc.nodes.map((n) => [n.key, n]));
177
+ const head = (p) => p.split('.')[0];
178
+ // Each producer is reached through one of its OUT-ports, and a band that
179
+ // names the ports carrying it (`bandPorts`) reaches only through those —
180
+ // entity-search's recalled lines ride `messages`, never `main`, so a spec
181
+ // wiring only `main` does not offer a band that can never fill.
182
+ const into = (to, toPort) => doc.edges
183
+ .filter((e) => e.to === to && head(e.toPort) === toPort)
184
+ .map((e) => ({ key: e.from, port: head(e.fromPort) }));
185
+ const queue = into(nodeKey, port);
186
+ const walked = new Set();
187
+ while (queue.length) {
188
+ const { key, port: outPort } = queue.shift();
189
+ if (seen.has(`${key}|${outPort}`))
190
+ continue;
191
+ seen.add(`${key}|${outPort}`);
192
+ const node = byKey.get(key);
193
+ if (!node)
194
+ continue;
195
+ const d = describe(node);
196
+ if (!d)
197
+ continue;
198
+ const declarer = `'${node.key}' (${node.definitionId}@${node.definitionVersion})`;
199
+ for (const [band, v] of Object.entries(d.bands ?? {})) {
200
+ const carriers = d.bandPorts?.[band];
201
+ if (carriers && !carriers.includes(outPort))
202
+ continue;
203
+ const variable = variableIdOf(v);
204
+ if (!variable)
205
+ continue;
206
+ const existing = out[band];
207
+ if (existing && existing.variable !== variable)
208
+ throw new BandCollisionError(`band '${band}' reaches '${nodeKey}.${port}' from two declarers with different ` +
209
+ `variables: ${existing.declarer} as '${existing.variable}', and ${declarer} as ` +
210
+ `'${variable}'. A band key is a top-level template name and means one thing — ` +
211
+ `rename one of the two bands.`);
212
+ if (!existing)
213
+ out[band] = { variable, declarer };
214
+ }
215
+ if (walked.has(key))
216
+ continue;
217
+ walked.add(key);
218
+ for (const [inPort, shape] of Object.entries(d.ports?.in ?? {}))
219
+ if (shapeIdOf(shape) === CANDIDATES_SHAPE)
220
+ queue.push(...into(node.key, inPort));
221
+ }
222
+ return out;
223
+ }
224
+ /**
225
+ * What a `variables` slot renders at one node of a document: its static
226
+ * `renders`, plus — when it declares `rendersBands` — every band declared
227
+ * upstream of that in-port.
228
+ *
229
+ * A band the static half already renders under the same variable is the same
230
+ * name, not a collision (core's lore queries declare `worldLore`; Assemble
231
+ * renders `worldLore`). One declared under a different variable, or shadowing
232
+ * a name Assemble renders itself, is refused naming both.
233
+ * @experimental
234
+ */
235
+ export function rendersAt(doc, nodeKey, slot, describe) {
236
+ const out = { ...(slot.renders ?? {}) };
237
+ const from = slot.rendersBands?.from;
238
+ if (!from)
239
+ return out;
240
+ const reaching = bandsReaching(doc, nodeKey, from, describe);
241
+ for (const [band, { variable, declarer }] of Object.entries(reaching)) {
242
+ const own = slot.renders?.[band];
243
+ if (own !== undefined) {
244
+ if (own !== variable)
245
+ throw new BandCollisionError(`band '${band}' declared by ${declarer} as '${variable}' collides with '${nodeKey}''s ` +
246
+ `own '${band}', which it renders as '${own}'. Rename the band.`);
247
+ continue;
248
+ }
249
+ if (ASSEMBLE_OWN_TEMPLATE_NAMES.includes(band))
250
+ throw new BandCollisionError(`band '${band}' declared by ${declarer} collides with Assemble's own '${band}' at ` +
251
+ `'${nodeKey}'. Rename the band.`);
252
+ out[band] = variable;
253
+ }
254
+ return out;
255
+ }
256
+ //# sourceMappingURL=bands.js.map