@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.
- package/LICENSE +202 -0
- package/NOTICE +17 -0
- package/README.md +368 -0
- package/dist/actions.d.ts +572 -0
- package/dist/actions.js +829 -0
- package/dist/annexFields.d.ts +234 -0
- package/dist/annexFields.js +413 -0
- package/dist/announce.d.ts +464 -0
- package/dist/announce.js +426 -0
- package/dist/attributes.d.ts +670 -0
- package/dist/attributes.js +904 -0
- package/dist/bands.d.ts +125 -0
- package/dist/bands.js +256 -0
- package/dist/builder.d.ts +683 -0
- package/dist/builder.js +677 -0
- package/dist/candidates.d.ts +109 -0
- package/dist/candidates.js +118 -0
- package/dist/capabilities.d.ts +744 -0
- package/dist/capabilities.js +679 -0
- package/dist/channels.d.ts +106 -0
- package/dist/channels.js +129 -0
- package/dist/clauses.d.ts +40 -0
- package/dist/clauses.js +43 -0
- package/dist/component.d.ts +33 -0
- package/dist/component.js +27 -0
- package/dist/componentBuild.d.ts +48 -0
- package/dist/componentBuild.js +84 -0
- package/dist/componentClient.d.ts +100 -0
- package/dist/componentClient.js +13 -0
- package/dist/componentImports.d.ts +51 -0
- package/dist/componentImports.js +106 -0
- package/dist/componentModule.d.ts +8 -0
- package/dist/componentModule.js +270 -0
- package/dist/componentWire.d.ts +130 -0
- package/dist/componentWire.js +280 -0
- package/dist/config.d.ts +295 -0
- package/dist/config.js +231 -0
- package/dist/connections.d.ts +89 -0
- package/dist/connections.js +131 -0
- package/dist/declarations.d.ts +290 -0
- package/dist/declarations.js +794 -0
- package/dist/descriptors.d.ts +1774 -0
- package/dist/descriptors.js +1329 -0
- package/dist/dev.d.ts +136 -0
- package/dist/dev.js +161 -0
- package/dist/document.d.ts +202 -0
- package/dist/document.js +312 -0
- package/dist/engines.d.ts +117 -0
- package/dist/engines.js +203 -0
- package/dist/eventMapLaws.d.ts +63 -0
- package/dist/eventMapLaws.js +128 -0
- package/dist/events.d.ts +554 -0
- package/dist/events.js +633 -0
- package/dist/executor.d.ts +771 -0
- package/dist/executor.js +1829 -0
- package/dist/extension.d.ts +448 -0
- package/dist/extension.js +315 -0
- package/dist/facets.d.ts +53 -0
- package/dist/facets.js +60 -0
- package/dist/foldedSections.d.ts +74 -0
- package/dist/foldedSections.js +161 -0
- package/dist/genres.d.ts +507 -0
- package/dist/genres.js +633 -0
- package/dist/guardedConnection.d.ts +138 -0
- package/dist/guardedConnection.js +341 -0
- package/dist/handlebarsReach.d.ts +122 -0
- package/dist/handlebarsReach.js +222 -0
- package/dist/hash.d.ts +146 -0
- package/dist/hash.js +166 -0
- package/dist/hookGrants.d.ts +47 -0
- package/dist/hookGrants.js +68 -0
- package/dist/hooks.d.ts +290 -0
- package/dist/hooks.js +113 -0
- package/dist/hostElements.d.ts +447 -0
- package/dist/hostElements.js +702 -0
- package/dist/i18n.d.ts +94 -0
- package/dist/i18n.js +123 -0
- package/dist/identity.d.ts +93 -0
- package/dist/identity.js +135 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +176 -0
- package/dist/layout.d.ts +194 -0
- package/dist/layout.js +40 -0
- package/dist/media.d.ts +202 -0
- package/dist/media.js +70 -0
- package/dist/messageBlocks.d.ts +487 -0
- package/dist/messageBlocks.js +683 -0
- package/dist/migration.d.ts +128 -0
- package/dist/migration.js +261 -0
- package/dist/names.d.ts +78 -0
- package/dist/names.js +136 -0
- package/dist/nodeInput.d.ts +399 -0
- package/dist/nodeInput.js +178 -0
- package/dist/participants.d.ts +291 -0
- package/dist/participants.js +252 -0
- package/dist/personGate.d.ts +62 -0
- package/dist/personGate.js +77 -0
- package/dist/pick.d.ts +84 -0
- package/dist/pick.js +103 -0
- package/dist/pluginHarness.d.ts +235 -0
- package/dist/pluginHarness.js +558 -0
- package/dist/pluginPermissions.d.ts +25 -0
- package/dist/pluginPermissions.js +97 -0
- package/dist/pluginRuleRef.d.ts +13 -0
- package/dist/pluginRuleRef.js +10 -0
- package/dist/predicates.d.ts +182 -0
- package/dist/predicates.js +249 -0
- package/dist/preview.d.ts +120 -0
- package/dist/preview.js +82 -0
- package/dist/promptBlocks.d.ts +130 -0
- package/dist/promptBlocks.js +197 -0
- package/dist/rankingDecisions.d.ts +60 -0
- package/dist/rankingDecisions.js +48 -0
- package/dist/receipt.d.ts +313 -0
- package/dist/receipt.js +60 -0
- package/dist/receiverRules.d.ts +24 -0
- package/dist/receiverRules.js +94 -0
- package/dist/refs.d.ts +117 -0
- package/dist/refs.js +137 -0
- package/dist/registry.d.ts +230 -0
- package/dist/registry.js +273 -0
- package/dist/review.d.ts +108 -0
- package/dist/review.js +97 -0
- package/dist/sampling.d.ts +152 -0
- package/dist/sampling.js +712 -0
- package/dist/scope.d.ts +65 -0
- package/dist/scope.js +128 -0
- package/dist/scripts.d.ts +243 -0
- package/dist/scripts.js +326 -0
- package/dist/sessionLayout.d.ts +339 -0
- package/dist/sessionLayout.js +503 -0
- package/dist/sessionSettings.d.ts +70 -0
- package/dist/sessionSettings.js +2 -0
- package/dist/settings.d.ts +501 -0
- package/dist/settings.js +604 -0
- package/dist/settingsSlot.d.ts +108 -0
- package/dist/settingsSlot.js +166 -0
- package/dist/shapes.d.ts +373 -0
- package/dist/shapes.js +408 -0
- package/dist/sheets.d.ts +165 -0
- package/dist/sheets.js +251 -0
- package/dist/statShapes.d.ts +96 -0
- package/dist/statShapes.js +112 -0
- package/dist/status.d.ts +103 -0
- package/dist/status.js +125 -0
- package/dist/storage.d.ts +145 -0
- package/dist/storage.js +33 -0
- package/dist/storyTime.d.ts +308 -0
- package/dist/storyTime.js +560 -0
- package/dist/surfaces.d.ts +501 -0
- package/dist/surfaces.js +292 -0
- package/dist/template.d.ts +312 -0
- package/dist/template.js +554 -0
- package/dist/templateCheck.d.ts +60 -0
- package/dist/templateCheck.js +516 -0
- package/dist/templateFit.d.ts +157 -0
- package/dist/templateFit.js +349 -0
- package/dist/templateIds.d.ts +138 -0
- package/dist/templateIds.js +144 -0
- package/dist/templateScopeAt.d.ts +131 -0
- package/dist/templateScopeAt.js +354 -0
- package/dist/testing.d.ts +177 -0
- package/dist/testing.js +383 -0
- package/dist/tokenizers.d.ts +113 -0
- package/dist/tokenizers.js +184 -0
- package/dist/turnLock.d.ts +96 -0
- package/dist/turnLock.js +79 -0
- package/dist/turnOrder.d.ts +143 -0
- package/dist/turnOrder.js +128 -0
- package/dist/validate.d.ts +74 -0
- package/dist/validate.js +1399 -0
- package/dist/values.d.ts +212 -0
- package/dist/values.js +390 -0
- package/dist/variables.d.ts +493 -0
- package/dist/variables.js +662 -0
- package/dist/verdicts.d.ts +275 -0
- package/dist/verdicts.js +348 -0
- package/dist/widgetDecls.d.ts +88 -0
- package/dist/widgetDecls.js +119 -0
- package/dist/widgets.d.ts +1527 -0
- package/dist/widgets.js +208 -0
- package/dist/wire.d.ts +126 -0
- package/dist/wire.js +140 -0
- package/dist/worldinfo.d.ts +150 -0
- package/dist/worldinfo.js +212 -0
- package/host-elements.d.ts +914 -0
- package/package.json +67 -0
package/dist/template.js
ADDED
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal template engine, enough to demonstrate variable awareness (16 §4).
|
|
3
|
+
*
|
|
4
|
+
* Supports `{{ a.b.c }}` and `{% for x in items %}…{% endfor %}`. Not Jinja — just
|
|
5
|
+
* enough surface to answer the question the docs make a promise about: *can the editor
|
|
6
|
+
* tell an author which variables exist, and flag one that doesn't?*
|
|
7
|
+
*
|
|
8
|
+
* ⚠ Writing this surfaced a correction to 16 §4 — see `templateScope`.
|
|
9
|
+
*/
|
|
10
|
+
const EXPR = /\{\{\s*([^}]+?)\s*\}\}/g;
|
|
11
|
+
const FOR = /\{%\s*for\s+(\w+)\s+in\s+([\w.]+)\s*%\}/g;
|
|
12
|
+
const IF = /\{%\s*if\s+([^%]+?)\s*%\}/g;
|
|
13
|
+
/**
|
|
14
|
+
* Extract top-level variable references. Loop-bound names are marked so they are not
|
|
15
|
+
* reported as unknown, and anything computed is marked dynamic rather than verified —
|
|
16
|
+
* an editor that promises correctness and lets a typo through is worse than one that
|
|
17
|
+
* says what it checks (16 §4).
|
|
18
|
+
* @experimental
|
|
19
|
+
*/
|
|
20
|
+
export function extractRefs(src) {
|
|
21
|
+
const bound = new Set();
|
|
22
|
+
const loopSources = [];
|
|
23
|
+
for (const m of src.matchAll(FOR))
|
|
24
|
+
bound.add(m[1]);
|
|
25
|
+
for (const m of src.matchAll(FOR)) {
|
|
26
|
+
const parts = m[2].split('.');
|
|
27
|
+
// ⚠ This used to be an unconditional `bound: false`, which made every
|
|
28
|
+
// nested loop a false error: `{% for l in c.lore %}` reported `'c' is
|
|
29
|
+
// not available to this template`, because the thing being iterated is
|
|
30
|
+
// itself a loop variable and nothing said so. Two passes, because a
|
|
31
|
+
// loop can be bound by one that appears later in the source.
|
|
32
|
+
loopSources.push({
|
|
33
|
+
root: parts[0],
|
|
34
|
+
path: parts.slice(1),
|
|
35
|
+
bound: bound.has(parts[0]),
|
|
36
|
+
dynamic: false,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const refs = [...loopSources];
|
|
40
|
+
for (const m of src.matchAll(EXPR)) {
|
|
41
|
+
const expr = m[1].trim();
|
|
42
|
+
const dynamic = /[\[\(]/.test(expr);
|
|
43
|
+
const parts = expr.split('.');
|
|
44
|
+
refs.push({
|
|
45
|
+
root: parts[0].replace(/[\[\(].*$/, ''),
|
|
46
|
+
path: parts.slice(1),
|
|
47
|
+
bound: bound.has(parts[0].replace(/[\[\(].*$/, '')),
|
|
48
|
+
dynamic,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return refs;
|
|
52
|
+
}
|
|
53
|
+
/** Render. Missing values become empty strings — templates never throw at run time. @experimental */
|
|
54
|
+
export function render(src, baseScope) {
|
|
55
|
+
let out = src;
|
|
56
|
+
let prev;
|
|
57
|
+
const scope = { ...baseScope };
|
|
58
|
+
// {% set name = expr %} — bind a value for the rest of this scope. This is what makes
|
|
59
|
+
// depth positioning a template concern: capture the outer loop's index before entering
|
|
60
|
+
// an inner loop, where `loop` would otherwise be shadowed.
|
|
61
|
+
//
|
|
62
|
+
// Loop bodies are masked first, so a set belonging to an inner scope is left for that
|
|
63
|
+
// scope's own render pass rather than being stripped by this one.
|
|
64
|
+
{
|
|
65
|
+
const { masked, blocks } = maskLoops(out);
|
|
66
|
+
out = masked;
|
|
67
|
+
out = out.replace(/\{%\s*set\s+(\w+)\s*=\s*([^%]+?)\s*%\}/g, (_m, name, expr) => {
|
|
68
|
+
const raw = expr.trim();
|
|
69
|
+
scope[name] = /^-?\d+$/.test(raw)
|
|
70
|
+
? Number(raw)
|
|
71
|
+
: /^['"].*['"]$/.test(raw)
|
|
72
|
+
? raw.slice(1, -1)
|
|
73
|
+
: get(scope, raw.split('.'));
|
|
74
|
+
return '';
|
|
75
|
+
});
|
|
76
|
+
out = out.replace(/\u0000(\d+)\u0000/g, (_m, i) => blocks[Number(i)]);
|
|
77
|
+
}
|
|
78
|
+
// loops — outermost first, so inner loops render inside their parent's scope.
|
|
79
|
+
// (An innermost-first pass evaluates the inner loop before `set`/item bindings exist,
|
|
80
|
+
// which is a real bug this replaced.)
|
|
81
|
+
out = renderLoops(out, scope);
|
|
82
|
+
// conditionals
|
|
83
|
+
do {
|
|
84
|
+
prev = out;
|
|
85
|
+
out = out.replace(/\{%\s*if\s+([^%]+?)\s*%\}((?:(?!\{%\s*if\s)[\s\S])*?)\{%\s*endif\s*%\}/g, (_m, cond, body) => (evaluate(cond, scope) ? body : ''));
|
|
86
|
+
} while (out !== prev);
|
|
87
|
+
return out.replace(EXPR, (_m, expr) => {
|
|
88
|
+
const v = get(scope, expr.trim().split('.'));
|
|
89
|
+
return v === undefined || v === null ? '' : String(v);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/** `a.b == c`, `a.b != c`, or a truthy path. Enough for positioning, not a language. */
|
|
93
|
+
function evaluate(cond, scope) {
|
|
94
|
+
const cmp = /^(.+?)\s*(==|!=)\s*(.+)$/.exec(cond.trim());
|
|
95
|
+
if (!cmp)
|
|
96
|
+
return Boolean(get(scope, cond.trim().split('.')));
|
|
97
|
+
const left = get(scope, cmp[1].trim().split('.'));
|
|
98
|
+
const rightRaw = cmp[3].trim();
|
|
99
|
+
const right = /^-?\d+$/.test(rightRaw)
|
|
100
|
+
? Number(rightRaw)
|
|
101
|
+
: /^['"].*['"]$/.test(rightRaw)
|
|
102
|
+
? rightRaw.slice(1, -1)
|
|
103
|
+
: get(scope, rightRaw.split('.'));
|
|
104
|
+
return cmp[2] === '==' ? left === right : left !== right;
|
|
105
|
+
}
|
|
106
|
+
function get(scope, path) {
|
|
107
|
+
let cur = scope;
|
|
108
|
+
for (const k of path) {
|
|
109
|
+
if (cur === undefined || cur === null)
|
|
110
|
+
return undefined;
|
|
111
|
+
cur = cur[k];
|
|
112
|
+
}
|
|
113
|
+
return cur;
|
|
114
|
+
}
|
|
115
|
+
/** @experimental */
|
|
116
|
+
export function templateScope(decl) {
|
|
117
|
+
return decl?.variables ?? {};
|
|
118
|
+
}
|
|
119
|
+
/** @experimental */
|
|
120
|
+
export function checkTemplate(src, scope) {
|
|
121
|
+
const known = Object.keys(scope);
|
|
122
|
+
const bindings = loopBindings(src);
|
|
123
|
+
const out = [];
|
|
124
|
+
const report = (r, base) => out.push({
|
|
125
|
+
severity: 'error',
|
|
126
|
+
message: r.message ?? `'${base}' does not exist`,
|
|
127
|
+
fix: r.available?.length
|
|
128
|
+
? `'${base}' has: ${r.available.join(', ')}`
|
|
129
|
+
: 'check the shape this template declares — the path does not exist on it',
|
|
130
|
+
});
|
|
131
|
+
for (const ref of extractRefs(src)) {
|
|
132
|
+
if (ref.dynamic) {
|
|
133
|
+
out.push({
|
|
134
|
+
severity: 'warning',
|
|
135
|
+
message: `'${ref.root}' is accessed dynamically and cannot be checked`,
|
|
136
|
+
fix: 'this is allowed — a computed key is not knowable here, so confirm this one yourself',
|
|
137
|
+
});
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
// A loop-bound name is one *element* of what it iterates. Before the
|
|
141
|
+
// schema there was no way to say what that was, so every reference
|
|
142
|
+
// through a loop variable went unchecked — which is where the typos
|
|
143
|
+
// live, because a loop body is the only place a template writes a
|
|
144
|
+
// nested path at all.
|
|
145
|
+
if (ref.bound) {
|
|
146
|
+
const element = boundType(ref.root, bindings, scope, new Set());
|
|
147
|
+
if (!element)
|
|
148
|
+
continue;
|
|
149
|
+
const r = resolvePath(element, ref.path, ref.root);
|
|
150
|
+
if (!r.ok)
|
|
151
|
+
report(r, ref.root);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (!known.includes(ref.root)) {
|
|
155
|
+
out.push({
|
|
156
|
+
severity: 'error',
|
|
157
|
+
message: `'${ref.root}' is not available to this template`,
|
|
158
|
+
fix: known.length
|
|
159
|
+
? `available here: ${known.join(', ')}`
|
|
160
|
+
: 'this template slot declares no variables — check the node type',
|
|
161
|
+
});
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const r = resolvePath(scope[ref.root], ref.path, ref.root);
|
|
165
|
+
if (!r.ok)
|
|
166
|
+
report(r, ref.root);
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
/** `{% for x in a.b %}` — every loop variable, and what it iterates. */
|
|
171
|
+
function loopBindings(src) {
|
|
172
|
+
const out = new Map();
|
|
173
|
+
for (const m of src.matchAll(FOR))
|
|
174
|
+
out.set(m[1], m[2].split('.'));
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The element type a loop variable is bound to, or `undefined` when it cannot
|
|
179
|
+
* be known.
|
|
180
|
+
*
|
|
181
|
+
* `seen` guards a template that binds a name from itself. That is not a
|
|
182
|
+
* template anyone means to write, but it is one somebody can type, and a lint
|
|
183
|
+
* that hangs the editor on it is worse than the typo.
|
|
184
|
+
*/
|
|
185
|
+
function boundType(name, bindings, scope, seen) {
|
|
186
|
+
const source = bindings.get(name);
|
|
187
|
+
if (!source || seen.has(name))
|
|
188
|
+
return undefined;
|
|
189
|
+
seen.add(name);
|
|
190
|
+
const [root, ...rest] = source;
|
|
191
|
+
const decl = bindings.has(root)
|
|
192
|
+
? boundType(root, bindings, scope, seen)
|
|
193
|
+
: scope[root];
|
|
194
|
+
const r = resolvePath(decl, rest);
|
|
195
|
+
return r.ok ? elementOf(r.field) : undefined;
|
|
196
|
+
}
|
|
197
|
+
const UNCHECKED = { ok: true, checked: false };
|
|
198
|
+
/**
|
|
199
|
+
* `length` is not a declared field and never will be, but Handlebars and the
|
|
200
|
+
* `{% for %}` engine both answer it on a list and a string. Flagging it would
|
|
201
|
+
* be the first false positive an author hit.
|
|
202
|
+
*/
|
|
203
|
+
const INTRINSIC = 'length';
|
|
204
|
+
/**
|
|
205
|
+
* Resolve `path` against a declared type.
|
|
206
|
+
*
|
|
207
|
+
* `list` is the interesting case. A list is reached by *position*, so
|
|
208
|
+
* `characters.name` is not a near-miss to be corrected — it is a category
|
|
209
|
+
* error, and saying so is the whole reason the old flattened `string[]` form
|
|
210
|
+
* had to go: it accepted exactly that and rejected `characters.0.name`.
|
|
211
|
+
* @internal
|
|
212
|
+
*/
|
|
213
|
+
export function resolvePath(decl, path, base = '') {
|
|
214
|
+
if (decl === undefined || decl === 'any')
|
|
215
|
+
return UNCHECKED;
|
|
216
|
+
// The legacy form answers for its own first segment and nothing deeper —
|
|
217
|
+
// exactly what it could answer before schemas existed.
|
|
218
|
+
if (Array.isArray(decl)) {
|
|
219
|
+
if (!path.length)
|
|
220
|
+
return UNCHECKED;
|
|
221
|
+
if (decl.includes(path[0]))
|
|
222
|
+
return UNCHECKED;
|
|
223
|
+
return {
|
|
224
|
+
ok: false,
|
|
225
|
+
checked: true,
|
|
226
|
+
at: path[0],
|
|
227
|
+
available: decl,
|
|
228
|
+
message: `'${label(base, [path[0]])}' does not exist`,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
let cur = decl;
|
|
232
|
+
for (let i = 0; i < path.length; i++) {
|
|
233
|
+
const seg = path[i];
|
|
234
|
+
const where = label(base, path.slice(0, i + 1));
|
|
235
|
+
switch (cur.type) {
|
|
236
|
+
case 'object': {
|
|
237
|
+
const fields = cur.fields;
|
|
238
|
+
if (!fields)
|
|
239
|
+
return UNCHECKED;
|
|
240
|
+
const next = fields[seg];
|
|
241
|
+
if (!next)
|
|
242
|
+
return {
|
|
243
|
+
ok: false,
|
|
244
|
+
checked: true,
|
|
245
|
+
at: seg,
|
|
246
|
+
available: Object.keys(fields),
|
|
247
|
+
message: `'${where}' does not exist`,
|
|
248
|
+
};
|
|
249
|
+
cur = next;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
case 'record': {
|
|
253
|
+
// Keyed by whatever the author of the *data* chose. Any key is
|
|
254
|
+
// plausible and none can be verified, so the walk continues
|
|
255
|
+
// with the value's type and stops claiming certainty.
|
|
256
|
+
if (!cur.of)
|
|
257
|
+
return UNCHECKED;
|
|
258
|
+
cur = cur.of;
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case 'list': {
|
|
262
|
+
if (seg === INTRINSIC)
|
|
263
|
+
return { ok: true, checked: true, field: { type: 'number' } };
|
|
264
|
+
if (!/^\d+$/.test(seg))
|
|
265
|
+
return {
|
|
266
|
+
ok: false,
|
|
267
|
+
checked: true,
|
|
268
|
+
at: seg,
|
|
269
|
+
message: `'${where}' does not exist — '${label(base, path.slice(0, i)) || 'this'}' is a ` +
|
|
270
|
+
`list, so it is reached by position. Loop over it and read '${seg}' from ` +
|
|
271
|
+
`each entry instead.`,
|
|
272
|
+
};
|
|
273
|
+
if (!cur.of)
|
|
274
|
+
return UNCHECKED;
|
|
275
|
+
cur = cur.of;
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
default: {
|
|
279
|
+
if (seg === INTRINSIC && cur.type === 'string')
|
|
280
|
+
return { ok: true, checked: true, field: { type: 'number' } };
|
|
281
|
+
return {
|
|
282
|
+
ok: false,
|
|
283
|
+
checked: true,
|
|
284
|
+
at: seg,
|
|
285
|
+
message: `'${where}' does not exist — '${label(base, path.slice(0, i)) || 'this'}' is a ${cur.type}.`,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return { ok: true, checked: true, field: cur };
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* What one iteration of a collection is.
|
|
294
|
+
*
|
|
295
|
+
* `undefined` where the answer is unknown rather than absent — a scalar has no
|
|
296
|
+
* element, but so does an `'any'`, and a caller that cannot tell those apart
|
|
297
|
+
* would report iterating an unchecked value as an error.
|
|
298
|
+
*/
|
|
299
|
+
function label(base, path) {
|
|
300
|
+
return [base, ...path].filter(Boolean).join('.');
|
|
301
|
+
}
|
|
302
|
+
/** @internal */
|
|
303
|
+
export function elementOf(decl) {
|
|
304
|
+
if (!decl || decl === 'any' || Array.isArray(decl))
|
|
305
|
+
return undefined;
|
|
306
|
+
if (decl.type === 'list' || decl.type === 'record')
|
|
307
|
+
return decl.of;
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Does a value match a declared schema?
|
|
312
|
+
*
|
|
313
|
+
* This exists so a `sample` and its `scope` cannot drift apart. A sample is not
|
|
314
|
+
* decoration: it is what the layout editor renders a live preview against, so a
|
|
315
|
+
* sample whose shape is wrong shows the author a preview that works and a chat
|
|
316
|
+
* that doesn't — a lie told at exactly the moment they are trusting the tool.
|
|
317
|
+
* The declaration and the example of it have to be checkable against each
|
|
318
|
+
* other, and this is the check.
|
|
319
|
+
*
|
|
320
|
+
* Findings are errors, not warnings, and an undeclared key is one of them. A
|
|
321
|
+
* sample carrying a field the schema omits means the schema is incomplete, and
|
|
322
|
+
* an incomplete schema is a completion list missing an entry the author needs —
|
|
323
|
+
* silently, with no way to tell it apart from a field that truly is not there.
|
|
324
|
+
* @experimental
|
|
325
|
+
*/
|
|
326
|
+
export function checkValue(value, field, path = '') {
|
|
327
|
+
const at = path || 'value';
|
|
328
|
+
const out = [];
|
|
329
|
+
const wrong = (want) => [`${at}: expected ${want}, got ${describe(value)}`];
|
|
330
|
+
switch (field.type) {
|
|
331
|
+
case 'string':
|
|
332
|
+
return typeof value === 'string' ? [] : wrong('a string');
|
|
333
|
+
case 'number':
|
|
334
|
+
return typeof value === 'number' ? [] : wrong('a number');
|
|
335
|
+
case 'boolean':
|
|
336
|
+
return typeof value === 'boolean' ? [] : wrong('a boolean');
|
|
337
|
+
case 'list': {
|
|
338
|
+
if (!Array.isArray(value))
|
|
339
|
+
return wrong('a list');
|
|
340
|
+
if (!field.of)
|
|
341
|
+
return out;
|
|
342
|
+
value.forEach((item, i) => out.push(...checkValue(item, field.of, `${at}[${i}]`)));
|
|
343
|
+
return out;
|
|
344
|
+
}
|
|
345
|
+
case 'record': {
|
|
346
|
+
if (!isPlainObject(value))
|
|
347
|
+
return wrong('a record');
|
|
348
|
+
if (!field.of)
|
|
349
|
+
return out;
|
|
350
|
+
for (const [k, v] of Object.entries(value))
|
|
351
|
+
out.push(...checkValue(v, field.of, `${at}.${k}`));
|
|
352
|
+
return out;
|
|
353
|
+
}
|
|
354
|
+
case 'object': {
|
|
355
|
+
if (!isPlainObject(value))
|
|
356
|
+
return wrong('an object');
|
|
357
|
+
const fields = field.fields ?? {};
|
|
358
|
+
for (const [k, f] of Object.entries(fields)) {
|
|
359
|
+
const has = k in value && value[k] !== undefined && value[k] !== null;
|
|
360
|
+
if (!has) {
|
|
361
|
+
if (!f.optional)
|
|
362
|
+
out.push(`${at}.${k}: declared but missing from the sample`);
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
out.push(...checkValue(value[k], f, `${at}.${k}`));
|
|
366
|
+
}
|
|
367
|
+
for (const k of Object.keys(value)) {
|
|
368
|
+
if (k in fields)
|
|
369
|
+
continue;
|
|
370
|
+
if (value[k] === undefined)
|
|
371
|
+
continue;
|
|
372
|
+
out.push(`${at}.${k}: present in the sample but not declared — ` +
|
|
373
|
+
`add it to \`fields\`, or drop it from the sample`);
|
|
374
|
+
}
|
|
375
|
+
return out;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Check every key of a scope against the values a declaration samples for it.
|
|
381
|
+
*
|
|
382
|
+
* Legacy declarations are skipped rather than guessed at. `'any'` means
|
|
383
|
+
* unchecked by definition, and a bare `string[]` carries no types to check
|
|
384
|
+
* against — pretending otherwise would fail honest declarations that simply
|
|
385
|
+
* predate the schema.
|
|
386
|
+
* @experimental
|
|
387
|
+
*/
|
|
388
|
+
export function checkScopeSample(values, scope, label = '') {
|
|
389
|
+
const out = [];
|
|
390
|
+
const prefix = label ? `${label} ` : '';
|
|
391
|
+
for (const [key, decl] of Object.entries(scope)) {
|
|
392
|
+
if (decl === 'any' || Array.isArray(decl))
|
|
393
|
+
continue;
|
|
394
|
+
if (!(key in values) || values[key] === undefined) {
|
|
395
|
+
out.push(`${prefix}${key}: declared in scope but the sample supplies no value for it`);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
out.push(...checkValue(values[key], decl, `${prefix}${key}`));
|
|
399
|
+
}
|
|
400
|
+
return out;
|
|
401
|
+
}
|
|
402
|
+
function isPlainObject(v) {
|
|
403
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
404
|
+
}
|
|
405
|
+
function describe(v) {
|
|
406
|
+
if (v === null)
|
|
407
|
+
return 'null';
|
|
408
|
+
if (v === undefined)
|
|
409
|
+
return 'undefined';
|
|
410
|
+
if (Array.isArray(v))
|
|
411
|
+
return 'a list';
|
|
412
|
+
return `a ${typeof v}`;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Mask top-level `{% for %}…{% endfor %}` blocks with placeholders, matching them balanced
|
|
416
|
+
* rather than by regex — nested loops make non-greedy matching wrong, which is exactly the
|
|
417
|
+
* bug this replaced.
|
|
418
|
+
*/
|
|
419
|
+
function maskLoops(src) {
|
|
420
|
+
const blocks = [];
|
|
421
|
+
let out = '';
|
|
422
|
+
let i = 0;
|
|
423
|
+
const FOR_OPEN = /\{%\s*for\s/g;
|
|
424
|
+
const TAG = /\{%\s*(for|endfor)\b[^%]*%\}/g;
|
|
425
|
+
while (i < src.length) {
|
|
426
|
+
FOR_OPEN.lastIndex = i;
|
|
427
|
+
const open = FOR_OPEN.exec(src);
|
|
428
|
+
if (!open) {
|
|
429
|
+
out += src.slice(i);
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
out += src.slice(i, open.index);
|
|
433
|
+
let depth = 0;
|
|
434
|
+
TAG.lastIndex = open.index;
|
|
435
|
+
let m;
|
|
436
|
+
let end = -1;
|
|
437
|
+
while ((m = TAG.exec(src))) {
|
|
438
|
+
if (m[1] === 'for')
|
|
439
|
+
depth++;
|
|
440
|
+
else if (--depth === 0) {
|
|
441
|
+
end = m.index + m[0].length;
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (end === -1) {
|
|
446
|
+
out += src.slice(open.index);
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
out += `\u0000${blocks.push(src.slice(open.index, end)) - 1}\u0000`;
|
|
450
|
+
i = end;
|
|
451
|
+
}
|
|
452
|
+
return { masked: out, blocks };
|
|
453
|
+
}
|
|
454
|
+
/** Render top-level `{% for %}` blocks; nested ones are handled by the recursive call. */
|
|
455
|
+
function renderLoops(src, scope) {
|
|
456
|
+
const { masked, blocks } = maskLoops(src);
|
|
457
|
+
if (!blocks.length)
|
|
458
|
+
return src;
|
|
459
|
+
return masked.replace(/\u0000(\d+)\u0000/g, (_m, idx) => {
|
|
460
|
+
const block = blocks[Number(idx)];
|
|
461
|
+
const head = /^\{%\s*for\s+(\w+)\s+in\s+([\w.]+)\s*%\}/.exec(block);
|
|
462
|
+
if (!head)
|
|
463
|
+
return block;
|
|
464
|
+
const body = block.slice(head[0].length, block.lastIndexOf('{%'));
|
|
465
|
+
const items = get(scope, head[2].split('.'));
|
|
466
|
+
if (!Array.isArray(items))
|
|
467
|
+
return '';
|
|
468
|
+
return items
|
|
469
|
+
.map((item, i) => render(body, {
|
|
470
|
+
...scope,
|
|
471
|
+
[head[1]]: item,
|
|
472
|
+
loop: { index: i + 1, index0: i, revindex: items.length - i, length: items.length },
|
|
473
|
+
}))
|
|
474
|
+
.join('');
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* A `FieldDecl`, in the template type language — `undefined` for what never enters a template.
|
|
479
|
+
* @internal
|
|
480
|
+
*/
|
|
481
|
+
export function fieldToVarField(f) {
|
|
482
|
+
if (!f)
|
|
483
|
+
return undefined;
|
|
484
|
+
const description = f.description;
|
|
485
|
+
const base = (type, extra = {}) => ({
|
|
486
|
+
type,
|
|
487
|
+
...(description !== undefined ? { description } : {}),
|
|
488
|
+
...extra,
|
|
489
|
+
});
|
|
490
|
+
switch (f.type) {
|
|
491
|
+
case 'string':
|
|
492
|
+
case 'text':
|
|
493
|
+
case 'enum':
|
|
494
|
+
case 'media':
|
|
495
|
+
return base('string');
|
|
496
|
+
case 'number':
|
|
497
|
+
case 'integer':
|
|
498
|
+
return base('number');
|
|
499
|
+
case 'boolean':
|
|
500
|
+
return base('boolean');
|
|
501
|
+
case 'string[]':
|
|
502
|
+
return base('list', { of: { type: 'string' } });
|
|
503
|
+
case 'share':
|
|
504
|
+
case 'perMember':
|
|
505
|
+
case 'strengths':
|
|
506
|
+
return base('record', { of: { type: 'number' } });
|
|
507
|
+
case 'list': {
|
|
508
|
+
const of = fieldToVarField(f.item);
|
|
509
|
+
return base('list', of ? { of } : {});
|
|
510
|
+
}
|
|
511
|
+
case 'object': {
|
|
512
|
+
const fields = {};
|
|
513
|
+
for (const [k, v] of Object.entries(f.fields ?? {})) {
|
|
514
|
+
const vf = fieldToVarField(v);
|
|
515
|
+
if (vf)
|
|
516
|
+
fields[k] = vf;
|
|
517
|
+
}
|
|
518
|
+
return base('object', { fields });
|
|
519
|
+
}
|
|
520
|
+
// A secret never enters a template scope (typed templates §1 "safe" iv).
|
|
521
|
+
case 'secret':
|
|
522
|
+
default:
|
|
523
|
+
return undefined;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* What a slot's configured value looks like to a template, as a `VarField`.
|
|
528
|
+
*
|
|
529
|
+
* `prompts` — an object of the authored text fields, each a string.
|
|
530
|
+
* `parameters` / `settings` — an object of the declared schema's fields, typed
|
|
531
|
+
* from the field language; a `secret` field is left out, because a secret
|
|
532
|
+
* never enters a template's scope. Every other kind (a template, a connection,
|
|
533
|
+
* a variables slot) is not a value a template reads, and is `undefined`.
|
|
534
|
+
* @experimental
|
|
535
|
+
*/
|
|
536
|
+
export function slotToVarField(slot) {
|
|
537
|
+
if (slot.kind === 'prompts') {
|
|
538
|
+
const fields = {};
|
|
539
|
+
for (const name of Object.keys(slot.fields ?? {}))
|
|
540
|
+
fields[name] = { type: 'string' };
|
|
541
|
+
return { type: 'object', fields };
|
|
542
|
+
}
|
|
543
|
+
if (slot.kind === 'parameters' || slot.kind === 'settings') {
|
|
544
|
+
const fields = {};
|
|
545
|
+
for (const [name, f] of Object.entries(slot.schema ?? {})) {
|
|
546
|
+
const vf = fieldToVarField(f);
|
|
547
|
+
if (vf)
|
|
548
|
+
fields[name] = vf;
|
|
549
|
+
}
|
|
550
|
+
return { type: 'object', fields };
|
|
551
|
+
}
|
|
552
|
+
return undefined;
|
|
553
|
+
}
|
|
554
|
+
//# sourceMappingURL=template.js.map
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one template checker (typed templates P4): does this Handlebars or
|
|
3
|
+
* Liquid source parse, and does every name it reads exist in the scope it will
|
|
4
|
+
* be rendered against?
|
|
5
|
+
*
|
|
6
|
+
* ## Why a separate entry point
|
|
7
|
+
*
|
|
8
|
+
* `@serene-pub/sdk/template-check`, not the barrel. The checker parses with
|
|
9
|
+
* the real engines — `handlebars` and `liquidjs`, parse only (Q5) — and the
|
|
10
|
+
* barrel is what every node author and plugin bundle imports; it has no
|
|
11
|
+
* dependencies and stays that way. A host (the editor lint, a save, a
|
|
12
|
+
* packager) imports this on purpose. Nothing here renders: the host keeps its
|
|
13
|
+
* own engines with its own helpers, and hands this module their NAMES.
|
|
14
|
+
*
|
|
15
|
+
* ## What it reports, and what it leaves alone
|
|
16
|
+
*
|
|
17
|
+
* - **syntax** — the engine's own words and position. Always an error: a
|
|
18
|
+
* template that does not parse breaks every pipeline that selects it.
|
|
19
|
+
* - **unknown-name** — a root the scope does not have. Renders as nothing.
|
|
20
|
+
* - **unknown-path** — a field the declared type positively contradicts
|
|
21
|
+
* (`characters.nmae`, a field read straight off a list).
|
|
22
|
+
* - **unknown-helper** — a Handlebars helper nobody registered.
|
|
23
|
+
*
|
|
24
|
+
* Conservative by construction: an `'any'` root, a record's author-chosen
|
|
25
|
+
* key, a dynamic lookup (`lookup`, `a[k]`) and a subexpression's result are
|
|
26
|
+
* unchecked, never guessed. Loop items, `{% assign %}`/`{% capture %}`
|
|
27
|
+
* locals, `@data` and `forloop` are never scope names.
|
|
28
|
+
*
|
|
29
|
+
* **Untyped sources.** When `templateScopeReport().untyped` is non-empty some
|
|
30
|
+
* producer supplies keys nobody declared, so a name missing from the scope may
|
|
31
|
+
* still arrive: name and path findings are then WARNINGS, never errors, and
|
|
32
|
+
* say which producer might supply them.
|
|
33
|
+
* @experimental
|
|
34
|
+
*/
|
|
35
|
+
import type { TemplateCheckOptions, TemplateFinding, TemplateSourceCheck, TemplateScope } from './template.js';
|
|
36
|
+
export type { TemplateCheckOptions, TemplateFinding, TemplateFindingKind, TemplateSourceCheck, } from './template.js';
|
|
37
|
+
/**
|
|
38
|
+
* Helpers Handlebars itself registers. A host's own (`json`, `isSet`, …) are
|
|
39
|
+
* an input — {@link TemplateCheckOptions.helpers} — never hard-coded here.
|
|
40
|
+
* @experimental
|
|
41
|
+
*/
|
|
42
|
+
export declare const HANDLEBARS_BUILTIN_HELPERS: readonly string[];
|
|
43
|
+
/** Whether {@link checkTemplateSource} can say anything about this engine. @experimental */
|
|
44
|
+
export declare const canCheckTemplateEngine: (engine: string) => boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Check `src` in `engine` against `scope`. The findings only — see
|
|
47
|
+
* {@link checkTemplateSourceReport} for whether anything was checked at all.
|
|
48
|
+
* @experimental
|
|
49
|
+
*/
|
|
50
|
+
export declare function checkTemplateSource(engine: string, src: string, scope: TemplateScope, options?: TemplateCheckOptions): TemplateFinding[];
|
|
51
|
+
/**
|
|
52
|
+
* Check `src` in `engine` against `scope`, and say whether it was checked.
|
|
53
|
+
*
|
|
54
|
+
* A syntax finding short-circuits: nothing useful can be said about the names
|
|
55
|
+
* in a template that does not parse, and saying it anyway buries the one
|
|
56
|
+
* message that matters. Each finding is reported once per place it appears.
|
|
57
|
+
* @experimental
|
|
58
|
+
*/
|
|
59
|
+
export declare function checkTemplateSourceReport(engine: string, src: string, scope: TemplateScope, options?: TemplateCheckOptions): TemplateSourceCheck;
|
|
60
|
+
//# sourceMappingURL=templateCheck.d.ts.map
|