@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,349 @@
1
+ /**
2
+ * Does this template fit this node? (typed templates P5, 2026-09-27)
3
+ *
4
+ * The refusals of design §4, in one place: a template is checked against the
5
+ * typed scope of the node that renders it (`templateScopeReport`), and a name
6
+ * or path nothing there supplies is REFUSED — at selection (the host), at a
7
+ * spec's save and publish (law T1, `validate()`), and at packaging (a plugin's
8
+ * shipped template seeds). Warnings never refuse, and nor does anything while
9
+ * a producer upstream declares no types (`untyped`): its keys may still
10
+ * arrive, so the checker only warns (owner Q6: refuse new selections and new
11
+ * saves; a stored row becomes a notice, never a retroactive refusal).
12
+ *
13
+ * ## The checker is a value
14
+ *
15
+ * The checker parses with the real engines (`@serene-pub/sdk/template-check`,
16
+ * which carries `handlebars` and `liquidjs`); this module is on the barrel,
17
+ * which stays dependency-free. So every door here takes the checker as an
18
+ * argument — `{ check: checkTemplateSourceReport }` — and a caller without one
19
+ * gets the checker-free half of T1 (scope collisions, forbidden kinds, a band
20
+ * with no variable) and no name check. One way to check; the engines stay
21
+ * where they were imported on purpose.
22
+ *
23
+ * ## What refuses
24
+ *
25
+ * `SCOPE_FINDING_KINDS` — `unknown-name` and `unknown-path` at error
26
+ * severity. A syntax finding and an unknown helper depend on the host's
27
+ * vocabulary (its helpers, its Liquid tags), which a spec document and a
28
+ * package do not carry; a host that passes its own vocabulary may widen
29
+ * `refuse`.
30
+ * @experimental
31
+ */
32
+ import { BandCollisionError, rendersAt } from './bands.js';
33
+ import { getDefinition } from './descriptors.js';
34
+ import { ForbiddenTemplateFieldError, TEMPLATE_ANNEX_PORT, templateScopeReport, } from './templateScopeAt.js';
35
+ import { getVariable } from './variables.js';
36
+ /** What a scope decides — a name or a path nothing supplies. @experimental */
37
+ export const SCOPE_FINDING_KINDS = ['unknown-name', 'unknown-path'];
38
+ /** Check one template against a scope report already computed. */
39
+ function fitWithin(report, template, checking) {
40
+ const result = checking.check(template.engine, template.source, report.scope, {
41
+ ...checking.options,
42
+ ...(report.untyped.length ? { untyped: report.untyped } : {}),
43
+ });
44
+ const refuse = new Set(checking.refuse ?? SCOPE_FINDING_KINDS);
45
+ const refusals = [];
46
+ const warnings = [];
47
+ for (const f of result.findings)
48
+ (f.severity === 'error' && f.kind && refuse.has(f.kind) ? refusals : warnings).push(f);
49
+ return { checked: result.checked, untyped: [...report.untyped], refusals, warnings };
50
+ }
51
+ /**
52
+ * Check `template` against the typed scope of its template slot at `nodeKey`.
53
+ * Throws what `templateScopeReport` throws — a root two declarers claim, a
54
+ * forbidden kind — because then the document is wrong, not the template.
55
+ * @experimental
56
+ */
57
+ export function templateFit(doc, nodeKey, template, checking, env = {}) {
58
+ return fitWithin(templateScopeReport(doc, nodeKey, env), template, checking);
59
+ }
60
+ const AVAILABLE_SHOWN = 40;
61
+ const quoted = (names) => names.map((n) => `\`${n}\``).join(', ');
62
+ /** One finding as the clause after "it". */
63
+ function clause(f) {
64
+ const written = f.path ?? f.name ?? '';
65
+ switch (f.kind) {
66
+ case 'syntax':
67
+ return `it does not parse (${f.message})`;
68
+ case 'unknown-helper':
69
+ return `it calls \`${f.name}\`, which is not a helper here`;
70
+ default:
71
+ return `it uses \`${written}\`, which nothing supplies here`;
72
+ }
73
+ }
74
+ /**
75
+ * The refusal, in the design's words:
76
+ *
77
+ * > 'speakPrompt' can't render 'Riddle layout': it uses `secretEntri`, which
78
+ * > nothing supplies here. Did you mean `secretEntry`? Available: …
79
+ *
80
+ * `where` names the step (a node key); `what` names the template (its name, or
81
+ * "the 'x' preset's template").
82
+ * @experimental
83
+ */
84
+ export function templateFitSentence(where, what, refusals) {
85
+ const [first, ...rest] = refusals;
86
+ if (!first)
87
+ return `'${where}' can render ${what}.`;
88
+ let s = `'${where}' can't render ${what}: ${clause(first)}.`;
89
+ if (first.suggestion)
90
+ s += ` Did you mean \`${first.suggestion}\`?`;
91
+ const available = first.available ?? [];
92
+ if (available.length && first.kind !== 'syntax')
93
+ s +=
94
+ ` Available: ${quoted(available.slice(0, AVAILABLE_SHOWN))}` +
95
+ (available.length > AVAILABLE_SHOWN
96
+ ? `, and ${available.length - AVAILABLE_SHOWN} more`
97
+ : '') +
98
+ '.';
99
+ const others = [
100
+ ...new Set(rest
101
+ .map((f) => f.path ?? f.name ?? '')
102
+ .filter((n) => n && n !== (first.path ?? first.name))),
103
+ ];
104
+ if (others.length)
105
+ s += ` It also uses ${quoted(others)}, which nothing supplies either.`;
106
+ return s;
107
+ }
108
+ // ── Law T1 — a spec's templates fit where they are rendered ─────────────────
109
+ const isTemplateValue = (v) => !!v &&
110
+ typeof v === 'object' &&
111
+ typeof v.source === 'string' &&
112
+ typeof v.engine === 'string';
113
+ const defaultDescribe = (n) => getDefinition(`${n.definitionId}@${n.definitionVersion}`);
114
+ /**
115
+ * Every template source a document carries for one template slot: the node's
116
+ * own configured value, and each preset's (`p.template(nodeKey, …)` writes
117
+ * slot `'template'`, meaning the node's template slot).
118
+ */
119
+ function templateSourcesAt(doc, node, slotName, first) {
120
+ const out = [];
121
+ const own = node.config?.[slotName];
122
+ if (isTemplateValue(own))
123
+ out.push({ what: 'its configured template', template: own });
124
+ for (const p of doc.presets ?? [])
125
+ for (const v of p.values ?? [])
126
+ if (v.nodeKey === node.key &&
127
+ (v.slot === slotName || (first && v.slot === 'template')) &&
128
+ isTemplateValue(v.value))
129
+ out.push({ what: `the '${p.slug}' preset's template`, template: v.value });
130
+ return out;
131
+ }
132
+ /**
133
+ * Law T1 over one document (design §4.2):
134
+ *
135
+ * - a root two declarers claim at a template node, or a forbidden kind in its
136
+ * scope — refused, naming both declarers (`templateScopeReport`);
137
+ * - a band reaching a `rendersBands` slot whose variable is not registered —
138
+ * refused (a band with no variable renders under a name nothing declares);
139
+ * - with `checking`: every template source the document carries (a node's
140
+ * configured value, each preset's) is checked against the typed scope of
141
+ * the slot it fills, and a name or path nothing supplies is refused.
142
+ *
143
+ * Also law T2 (P6, `annexPortFindings`): the annex in-port.
144
+ *
145
+ * Errors only: a warning is the editor's and the receipt's to say.
146
+ * @experimental
147
+ */
148
+ export function templateLawFindings(doc, checking, env = {}) {
149
+ const f = [];
150
+ const describe = env.describe ?? defaultDescribe;
151
+ const variable = env.variable ?? getVariable;
152
+ for (const node of doc.nodes) {
153
+ const d = describe(node);
154
+ if (!d)
155
+ continue;
156
+ const slots = (d.slots ?? {});
157
+ for (const slot of Object.values(slots)) {
158
+ if (slot.kind !== 'variables' || !slot.rendersBands)
159
+ continue;
160
+ let renders;
161
+ try {
162
+ renders = rendersAt(doc, node.key, slot, describe);
163
+ }
164
+ catch (e) {
165
+ if (!(e instanceof BandCollisionError))
166
+ throw e;
167
+ f.push({
168
+ law: 'T1',
169
+ severity: 'error',
170
+ nodeKey: node.key,
171
+ message: e.message,
172
+ fix: 'rename one of the two bands — a band key is a top-level template name and means one thing',
173
+ });
174
+ continue;
175
+ }
176
+ for (const [band, id] of Object.entries(renders))
177
+ if (!(slot.renders ?? {})[band] && !variable(id))
178
+ f.push({
179
+ law: 'T1',
180
+ severity: 'error',
181
+ nodeKey: node.key,
182
+ message: `band '${band}' reaches '${node.key}' with variable '${id}', which is not registered — a template would read {{{${band}}}} with no layout and no type`,
183
+ fix: `declare '${id}' with definePluginVariable() (defineVariable() in core) and register it before the definition that names it`,
184
+ });
185
+ }
186
+ const templateSlots = Object.entries(slots).filter(([, s]) => s.kind === 'template');
187
+ templateSlots.forEach(([slotName], i) => {
188
+ let report;
189
+ try {
190
+ report = templateScopeReport(doc, node.key, { ...env, slot: slotName });
191
+ }
192
+ catch (e) {
193
+ if (e instanceof BandCollisionError || e instanceof ForbiddenTemplateFieldError)
194
+ f.push({
195
+ law: 'T1',
196
+ severity: 'error',
197
+ nodeKey: node.key,
198
+ message: e.message,
199
+ fix: e instanceof BandCollisionError
200
+ ? 'rename one of the two declarers — a top-level template name means one thing'
201
+ : 'take the forbidden value out of what reaches this template; it may never enter a prompt',
202
+ });
203
+ // Anything else (an unknown definition) is another law's to say.
204
+ return;
205
+ }
206
+ if (!checking)
207
+ return;
208
+ for (const { what, template } of templateSourcesAt(doc, node, slotName, i === 0)) {
209
+ const fit = fitWithin(report, template, checking);
210
+ if (!fit.refusals.length)
211
+ continue;
212
+ f.push({
213
+ law: 'T1',
214
+ severity: 'error',
215
+ nodeKey: node.key,
216
+ message: templateFitSentence(node.key, what, fit.refusals),
217
+ fix: fit.refusals[0].fix,
218
+ });
219
+ }
220
+ });
221
+ }
222
+ f.push(...annexPortFindings(doc, describe));
223
+ return f;
224
+ }
225
+ // ── Law T2 — the annex reaches a template on one road ───────────────────────
226
+ /** The one query that may feed a template's annex in-port, and the view it must read. @experimental */
227
+ export const TEMPLATE_ANNEX_QUERY = 'core:query/session-annex';
228
+ /** @experimental */
229
+ export const TEMPLATE_ANNEX_VIEW = 'template';
230
+ const headOf = (p) => p.split('.')[0];
231
+ const isDataRef = (v) => !!v && typeof v === 'object' && v.__ref === 'data';
232
+ /**
233
+ * Law T2 (typed templates P6, owner ruling Q1): a template node's `annex`
234
+ * in-port is fed only by `core:query/session-annex@1` reading `view:
235
+ * 'template'` — every declared key of every owner in scope, and nothing a
236
+ * declaration does not cover — and a template-view read feeds nothing but
237
+ * such a port. Either half broken would put undeclared data into a prompt, or
238
+ * the prompt's data somewhere a prompt is not. Errors only.
239
+ * @experimental
240
+ */
241
+ export function annexPortFindings(doc, describe = defaultDescribe) {
242
+ const f = [];
243
+ const byKey = new Map(doc.nodes.map((n) => [n.key, n]));
244
+ const annexPortOf = (n) => {
245
+ const d = describe(n);
246
+ if (!d)
247
+ return false;
248
+ const takesTemplate = Object.values((d.slots ?? {})).some((s) => s.kind === 'template');
249
+ return takesTemplate && Object.prototype.hasOwnProperty.call(d.ports?.in ?? {}, TEMPLATE_ANNEX_PORT);
250
+ };
251
+ const isTemplateView = (n) => !!n && n.definitionId === TEMPLATE_ANNEX_QUERY && n.config?.view === TEMPLATE_ANNEX_VIEW;
252
+ const road = `wire ${TEMPLATE_ANNEX_QUERY}@1 with view: '${TEMPLATE_ANNEX_VIEW}' (a literal) into the ` +
253
+ `'${TEMPLATE_ANNEX_PORT}' port — nothing else`;
254
+ for (const node of doc.nodes) {
255
+ if (annexPortOf(node)) {
256
+ const literal = node.config?.[TEMPLATE_ANNEX_PORT];
257
+ if (literal !== undefined && !isDataRef(literal))
258
+ f.push({
259
+ law: 'T2',
260
+ severity: 'error',
261
+ nodeKey: node.key,
262
+ message: `'${node.key}' sets its '${TEMPLATE_ANNEX_PORT}' port to a value; a template's annex is read from the session, never written into the spec`,
263
+ fix: road,
264
+ });
265
+ for (const e of doc.edges) {
266
+ if (e.to !== node.key || headOf(e.toPort) !== TEMPLATE_ANNEX_PORT)
267
+ continue;
268
+ const from = byKey.get(e.from);
269
+ if (isTemplateView(from) && headOf(e.fromPort) === 'main')
270
+ continue;
271
+ f.push({
272
+ law: 'T2',
273
+ severity: 'error',
274
+ nodeKey: node.key,
275
+ message: `'${node.key}.${TEMPLATE_ANNEX_PORT}' is fed by '${e.from}.${e.fromPort}'` +
276
+ (from?.definitionId === TEMPLATE_ANNEX_QUERY
277
+ ? `, a session-annex read whose view is not '${TEMPLATE_ANNEX_VIEW}'`
278
+ : '') +
279
+ ` — only the declared annex keys of the owners in scope may reach a template`,
280
+ fix: road,
281
+ });
282
+ }
283
+ }
284
+ if (isTemplateView(node))
285
+ for (const e of doc.edges) {
286
+ if (e.from !== node.key || e.implicit)
287
+ continue;
288
+ const to = byKey.get(e.to);
289
+ if (to && annexPortOf(to) && headOf(e.toPort) === TEMPLATE_ANNEX_PORT)
290
+ continue;
291
+ f.push({
292
+ law: 'T2',
293
+ severity: 'error',
294
+ nodeKey: node.key,
295
+ message: `'${node.key}' reads the annex with view: '${TEMPLATE_ANNEX_VIEW}' — every owner's declared keys — ` +
296
+ `and feeds '${e.to}.${e.toPort}', which is not a template's '${TEMPLATE_ANNEX_PORT}' port`,
297
+ fix: `read one owner's document with view: 'ai' (or no view) for a pipeline's own use; the template view feeds only a template's '${TEMPLATE_ANNEX_PORT}' port`,
298
+ });
299
+ }
300
+ }
301
+ return f;
302
+ }
303
+ /**
304
+ * Check a package's `context template` seeds (`TemplateSeed` kind
305
+ * `template`) against every node of its own specs that renders them — the
306
+ * seed's pool is the node definition, so each such node is a place it can be
307
+ * selected. A seed no spec of the package places is left to the host, whose
308
+ * selection check sees the spec that selects it.
309
+ * @experimental
310
+ */
311
+ export function templateSeedFindings(seeds, documents, checking, env = {}) {
312
+ const out = [];
313
+ const describe = env.describe ?? defaultDescribe;
314
+ for (const seed of seeds) {
315
+ if (seed.kind !== 'template' || typeof seed.body !== 'string' || !seed.engine)
316
+ continue;
317
+ const template = { engine: seed.engine, source: seed.body };
318
+ for (const doc of documents)
319
+ for (const node of doc.nodes) {
320
+ if (node.definitionId !== seed.nodeDefinitionId)
321
+ continue;
322
+ const slots = (describe(node)?.slots ?? {});
323
+ for (const [slotName, slot] of Object.entries(slots)) {
324
+ if (slot.kind !== 'template')
325
+ continue;
326
+ let report;
327
+ try {
328
+ report = templateScopeReport(doc, node.key, { ...env, slot: slotName });
329
+ }
330
+ catch {
331
+ // The document's own fault — T1 says it over the document.
332
+ continue;
333
+ }
334
+ const fit = fitWithin(report, template, checking);
335
+ if (!fit.refusals.length)
336
+ continue;
337
+ out.push({
338
+ template: seed.id,
339
+ specId: doc.id,
340
+ nodeKey: node.key,
341
+ message: templateFitSentence(node.key, `'${seed.label ?? seed.id}'`, fit.refusals),
342
+ fix: fit.refusals[0].fix,
343
+ });
344
+ }
345
+ }
346
+ }
347
+ return out;
348
+ }
349
+ //# sourceMappingURL=templateFit.js.map
@@ -0,0 +1,138 @@
1
+ /**
2
+ * **Template ids** — the owner-namespaced name a spec uses to reference a
3
+ * template row, and the declaration a plugin ships one under (R19).
4
+ *
5
+ * ## The problem this closes
6
+ *
7
+ * A prompt, a context template and a variable layout are all ROWS in the
8
+ * instance, and a spec's shipped configuration points at one by its integer id
9
+ * — a number an identity sequence assigns, different on every install, which no
10
+ * shipped document can write. Core's rows carry a `seedKey` as their storage
11
+ * identity, and that got core as far as core needed to go: an unnamespaced,
12
+ * bare string like `pipeline-prompt:core:task/assemble:prompts:tool-loop-default`
13
+ * is fine when there is exactly one party minting them.
14
+ *
15
+ * It does not survive a second party. An extension could already ship
16
+ * `pipelines`, and had no way at all to ship a template row one of them
17
+ * references; and a third-party spec added to somebody else's genre had no
18
+ * stable name for that genre's prompt except a seed key with no owner in it,
19
+ * which is a name anyone can collide with and nobody can version.
20
+ *
21
+ * So a template row gains a second identity column — the same deliberate
22
+ * second-identity pattern `completion_templates.key` states — holding an id in
23
+ * the grammar every other declared thing already uses:
24
+ *
25
+ * ```
26
+ * owner:template/name@major core:template/pipeline-context-template-core-default@1
27
+ * acme.dice:template/table-narration@1
28
+ * ```
29
+ *
30
+ * `seedKey` is untouched and stays what it is: core's storage identity, matched
31
+ * on by the seed pass, NULL on every row a person wrote.
32
+ *
33
+ * ## `@N` governs breakage
34
+ *
35
+ * A template id is pinned like every other id in this vocabulary. Rewording a
36
+ * template is an edit to the row the id already names. A change that would make
37
+ * a spec pinned to it render something it did not ask for — a field removed, a
38
+ * variable renamed, a different language — is a **new major**, which is a NEW
39
+ * ROW under a new id. Both ids resolve; every spec keeps rendering what it was
40
+ * written against, and moving is an edit somebody makes on purpose.
41
+ *
42
+ * ## ⚠ Not a template ENGINE id
43
+ *
44
+ * `core:template/handlebars@1` and `core:template/liquid@1` are **engine** ids
45
+ * (`engines.ts`) and share this kind segment. The two never meet in one field:
46
+ * an engine id is only ever read from an `engine` column or an engine registry,
47
+ * a template id only from `template_id` or a config value's `templateId`. The
48
+ * overlap is recorded here rather than left to be discovered — see the report
49
+ * accompanying R19.
50
+ */
51
+ import type { EngineId } from './engines.js';
52
+ /**
53
+ * The grammar, mirroring the attribute slot id's exactly one kind segment over.
54
+ * Owner is a plugin slug (`vendor.plugin`) or `core`; name is kebab-case;
55
+ * major is the pin.
56
+ * @experimental
57
+ */
58
+ export declare const TEMPLATE_ID: RegExp;
59
+ /** The three parts of a template id, or `null` when it is not one. @experimental */
60
+ export interface TemplateIdParts {
61
+ owner: string;
62
+ name: string;
63
+ major: number;
64
+ }
65
+ /** Type guard: is this string a well-formed template id? @internal */
66
+ export declare function isTemplateId(value: unknown): value is string;
67
+ /** Split a template id, or `null` when it is not one. @internal */
68
+ export declare function parseTemplateId(value: unknown): TemplateIdParts | null;
69
+ /**
70
+ * Which kind of row a shipped template is. The three values are `SlotKind`
71
+ * members, deliberately — a template row exists to fill one kind of slot, and
72
+ * saying which is what routes it to the right table without anything having to
73
+ * infer it from the shape of `body`.
74
+ * @experimental
75
+ */
76
+ export type TemplateSeedKind = 'prompts' | 'template' | 'variables';
77
+ /**
78
+ * A template row an extension ships, beside its `pipelines`.
79
+ *
80
+ * Mirrors `CorePromptSeed` — core's own shipped-prompt shape — with the seed
81
+ * key replaced by the namespaced `id`, because a plugin's row is referenced by
82
+ * a name that carries its owner and core's is not.
83
+ *
84
+ * ⚠ There is no `description`. None of the three tables has a column for one,
85
+ * and a declared field that silently reaches no row is worse than an absent
86
+ * one; it can be added with the column, additively, the day there is somewhere
87
+ * to put it.
88
+ * @experimental
89
+ */
90
+ export interface TemplateSeed {
91
+ /**
92
+ * `<pluginId>:template/<name>@<major>`. The owner segment must be the
93
+ * declaring plugin's own slug — `defineExtension` refuses anything else,
94
+ * including `core:`, because ownership is what lets an update replace your
95
+ * rows and leave everybody else's alone.
96
+ */
97
+ id: string;
98
+ kind: TemplateSeedKind;
99
+ /**
100
+ * `prompts` and `template`: the pool's node definition id, **UNVERSIONED**
101
+ * (`core:task/build-template-context`) — which may belong to another
102
+ * package. Unversioned so a node's @1 → @2 does not strand the prose
103
+ * written for it.
104
+ */
105
+ nodeDefinitionId?: string;
106
+ /**
107
+ * `prompts`: which prompts slot on that node. A definition may declare more
108
+ * than one, each with its own field set, so it is half the pool key.
109
+ */
110
+ slot?: string;
111
+ /** `variables`: the registered variable this renders (`core:var/characters@1`). */
112
+ variableId?: string;
113
+ /**
114
+ * `template` and `variables`: the registered engine id `body` is written in.
115
+ * Required for both — a stored template keeps the language it was authored
116
+ * in rather than inheriting whatever core renders with later, and a Liquid
117
+ * source handed to a Handlebars slot stores cleanly and ships `{% %}` to the
118
+ * model as prose.
119
+ */
120
+ engine?: EngineId;
121
+ /**
122
+ * The content. Field name → prose for `prompts` (exactly the fields that
123
+ * slot declares); the template source for `template` and `variables`.
124
+ */
125
+ body: string | Record<string, string>;
126
+ /** The human title. Defaults to the id's name segment where absent. */
127
+ label?: string;
128
+ }
129
+ /**
130
+ * Everything wrong with one declared template, in the author's words.
131
+ *
132
+ * Separate from `defineExtension` so the CLI's packager and the instance's
133
+ * install check can hold a manifest to the same rule the author was held to,
134
+ * without re-deriving it and coming to a different answer.
135
+ * @internal
136
+ */
137
+ export declare function templateSeedProblems(t: TemplateSeed, owner: string): string[];
138
+ //# sourceMappingURL=templateIds.d.ts.map
@@ -0,0 +1,144 @@
1
+ /**
2
+ * **Template ids** — the owner-namespaced name a spec uses to reference a
3
+ * template row, and the declaration a plugin ships one under (R19).
4
+ *
5
+ * ## The problem this closes
6
+ *
7
+ * A prompt, a context template and a variable layout are all ROWS in the
8
+ * instance, and a spec's shipped configuration points at one by its integer id
9
+ * — a number an identity sequence assigns, different on every install, which no
10
+ * shipped document can write. Core's rows carry a `seedKey` as their storage
11
+ * identity, and that got core as far as core needed to go: an unnamespaced,
12
+ * bare string like `pipeline-prompt:core:task/assemble:prompts:tool-loop-default`
13
+ * is fine when there is exactly one party minting them.
14
+ *
15
+ * It does not survive a second party. An extension could already ship
16
+ * `pipelines`, and had no way at all to ship a template row one of them
17
+ * references; and a third-party spec added to somebody else's genre had no
18
+ * stable name for that genre's prompt except a seed key with no owner in it,
19
+ * which is a name anyone can collide with and nobody can version.
20
+ *
21
+ * So a template row gains a second identity column — the same deliberate
22
+ * second-identity pattern `completion_templates.key` states — holding an id in
23
+ * the grammar every other declared thing already uses:
24
+ *
25
+ * ```
26
+ * owner:template/name@major core:template/pipeline-context-template-core-default@1
27
+ * acme.dice:template/table-narration@1
28
+ * ```
29
+ *
30
+ * `seedKey` is untouched and stays what it is: core's storage identity, matched
31
+ * on by the seed pass, NULL on every row a person wrote.
32
+ *
33
+ * ## `@N` governs breakage
34
+ *
35
+ * A template id is pinned like every other id in this vocabulary. Rewording a
36
+ * template is an edit to the row the id already names. A change that would make
37
+ * a spec pinned to it render something it did not ask for — a field removed, a
38
+ * variable renamed, a different language — is a **new major**, which is a NEW
39
+ * ROW under a new id. Both ids resolve; every spec keeps rendering what it was
40
+ * written against, and moving is an edit somebody makes on purpose.
41
+ *
42
+ * ## ⚠ Not a template ENGINE id
43
+ *
44
+ * `core:template/handlebars@1` and `core:template/liquid@1` are **engine** ids
45
+ * (`engines.ts`) and share this kind segment. The two never meet in one field:
46
+ * an engine id is only ever read from an `engine` column or an engine registry,
47
+ * a template id only from `template_id` or a config value's `templateId`. The
48
+ * overlap is recorded here rather than left to be discovered — see the report
49
+ * accompanying R19.
50
+ */
51
+ /**
52
+ * The grammar, mirroring the attribute slot id's exactly one kind segment over.
53
+ * Owner is a plugin slug (`vendor.plugin`) or `core`; name is kebab-case;
54
+ * major is the pin.
55
+ * @experimental
56
+ */
57
+ export const TEMPLATE_ID = /^[a-z0-9]+(?:[.-][a-z0-9]+)*:template\/[a-z0-9]+(?:-[a-z0-9]+)*@\d+$/;
58
+ /** Type guard: is this string a well-formed template id? @internal */
59
+ export function isTemplateId(value) {
60
+ return typeof value === 'string' && TEMPLATE_ID.test(value);
61
+ }
62
+ /** Split a template id, or `null` when it is not one. @internal */
63
+ export function parseTemplateId(value) {
64
+ if (!isTemplateId(value))
65
+ return null;
66
+ const [owner, rest] = value.split(':');
67
+ const [name, major] = rest.slice('template/'.length).split('@');
68
+ return { owner, name, major: Number(major) };
69
+ }
70
+ /**
71
+ * Everything wrong with one declared template, in the author's words.
72
+ *
73
+ * Separate from `defineExtension` so the CLI's packager and the instance's
74
+ * install check can hold a manifest to the same rule the author was held to,
75
+ * without re-deriving it and coming to a different answer.
76
+ * @internal
77
+ */
78
+ export function templateSeedProblems(t, owner) {
79
+ const problems = [];
80
+ const parts = parseTemplateId(t.id);
81
+ if (!parts) {
82
+ problems.push(`template id '${t.id}' is not a template id — it must read ` +
83
+ `'${owner}:template/<kebab-name>@<major>' (e.g. '${owner}:template/table-narration@1').`);
84
+ }
85
+ else if (parts.owner === 'core') {
86
+ problems.push(`template '${t.id}' claims the 'core' namespace, which is reserved. ` +
87
+ `Name it '${owner}:template/${parts.name}@${parts.major}'.`);
88
+ }
89
+ else if (parts.owner !== owner) {
90
+ problems.push(`template '${t.id}' sits under namespace '${parts.owner}', not '${owner}'. ` +
91
+ `Ownership is what lets an update replace your rows and leave everyone else's alone.`);
92
+ }
93
+ const isFields = !!t.body && typeof t.body === 'object' && !Array.isArray(t.body);
94
+ const isSource = typeof t.body === 'string';
95
+ if (t.kind === 'prompts') {
96
+ if (!t.nodeDefinitionId)
97
+ problems.push(`template '${t.id}' is a prompts row and names no nodeDefinitionId — a prompt ` +
98
+ `follows the node it was written for, and the node is half its pool key.`);
99
+ if (!t.slot)
100
+ problems.push(`template '${t.id}' is a prompts row and names no slot — a definition may declare ` +
101
+ `more than one prompts slot, each with its own fields, so the slot is the other half.`);
102
+ if (!isFields)
103
+ problems.push(`template '${t.id}' is a prompts row, so 'body' is field name → prose ` +
104
+ `({ systemPrompt: '…' }), not a single string.`);
105
+ if (t.engine)
106
+ problems.push(`template '${t.id}' is a prompts row and declares an engine. Prompts are authored ` +
107
+ `text fields, not a rendered document — drop 'engine'.`);
108
+ if (t.variableId)
109
+ problems.push(`template '${t.id}' is a prompts row and declares a variableId.`);
110
+ }
111
+ else if (t.kind === 'template') {
112
+ if (!t.nodeDefinitionId)
113
+ problems.push(`template '${t.id}' is a context template and names no nodeDefinitionId — the node ` +
114
+ `whose context it renders is half its pool key.`);
115
+ if (!isSource)
116
+ problems.push(`template '${t.id}' is a context template, so 'body' is the source string.`);
117
+ if (!t.engine)
118
+ problems.push(`template '${t.id}' is a context template and declares no engine. The engine travels ` +
119
+ `on the value: a source with none would be rendered in whatever core ships instead ` +
120
+ `of what you wrote it in.`);
121
+ if (t.variableId)
122
+ problems.push(`template '${t.id}' is a context template and declares a variableId.`);
123
+ }
124
+ else if (t.kind === 'variables') {
125
+ if (!t.variableId)
126
+ problems.push(`template '${t.id}' is a variable layout and names no variableId — a layout is keyed ` +
127
+ `by what it renders, never by a node or a spec.`);
128
+ if (!isSource)
129
+ problems.push(`template '${t.id}' is a variable layout, so 'body' is the source string.`);
130
+ if (!t.engine)
131
+ problems.push(`template '${t.id}' is a variable layout and declares no engine. A layout written in ` +
132
+ `one language and selected into a slot that renders another stores cleanly and ` +
133
+ `renders as raw markup.`);
134
+ if (t.nodeDefinitionId || t.slot)
135
+ problems.push(`template '${t.id}' is a variable layout and names a node — layouts are keyed by the ` +
136
+ `variable they render, so that any pipeline rendering it may select this row.`);
137
+ }
138
+ else {
139
+ problems.push(`template '${t.id}' has kind '${t.kind}'. ` +
140
+ `A shipped template fills a 'prompts', 'template' or 'variables' slot.`);
141
+ }
142
+ return problems;
143
+ }
144
+ //# sourceMappingURL=templateIds.js.map