@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/settings.js
ADDED
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin settings — the schema an extension declares, and the four things core does
|
|
3
|
+
* with it (12 §6).
|
|
4
|
+
*
|
|
5
|
+
* 12 §6 promises "the same schema strategy as node config and review steps — one
|
|
6
|
+
* renderer, three uses." That promise is only real if it is literally the same
|
|
7
|
+
* declaration, so `FieldDecl` here is the same shape node `params` use, plus the two
|
|
8
|
+
* fields that only mean something for plugin settings (`scope`, `side`). An extension
|
|
9
|
+
* author who has written a node's `params` schema already knows this one.
|
|
10
|
+
*
|
|
11
|
+
* One declaration, four uses:
|
|
12
|
+
*
|
|
13
|
+
* 1. **The form** core renders in plugin settings — no UI work by the author.
|
|
14
|
+
* 2. **Validation** of stored values, on save and on update.
|
|
15
|
+
* 3. **The manifest entry**, extracted statically by the compiler — never by running
|
|
16
|
+
* the author's code (F6, 03 §3).
|
|
17
|
+
* 4. **Typed access** from the extension's own hooks: `settings.values` is inferred,
|
|
18
|
+
* so `apiKey` is a `SecretValue` and a mistyped key does not compile.
|
|
19
|
+
*
|
|
20
|
+
* ## The `secret` field, and why it is typed
|
|
21
|
+
*
|
|
22
|
+
* The ruling that produced this reads backwards at first. "SP declines custody of plugin
|
|
23
|
+
* secrets" sounds safer than storing them — but an extension keeping credentials in its
|
|
24
|
+
* own data directory has no key and no crypto facility, so the realistic outcome is
|
|
25
|
+
* plaintext on the user's disk, unencrypted *and* unauditable. Declining custody produced
|
|
26
|
+
* the worse result (13 §6).
|
|
27
|
+
*
|
|
28
|
+
* What makes accepting it defensible is that the field is **typed**, which is what lets
|
|
29
|
+
* core mechanically redact it from receipts (F16), exclude it from export (12 §7) and
|
|
30
|
+
* keep it write-only in the UI. A free-form column cannot tell a key from a note.
|
|
31
|
+
*/
|
|
32
|
+
import { pluginRuleRef } from './pluginRuleRef.js';
|
|
33
|
+
import { i18nFindings, i18nText } from './i18n.js';
|
|
34
|
+
/** @experimental */
|
|
35
|
+
export const secret = (value) => ({ $secret: true, value });
|
|
36
|
+
/** @internal */
|
|
37
|
+
export const isSecret = (v) => !!v && typeof v === 'object' && v.$secret === true;
|
|
38
|
+
/**
|
|
39
|
+
* The label to render, resolved through `i18nText` in `language` — a bare
|
|
40
|
+
* string is itself, a map answers the locale or falls back to `en`.
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export function fieldLabel(decl, language = 'en') {
|
|
44
|
+
return i18nText(decl.label, language);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Every display-text fault in one settings schema (R-20), as sentences that
|
|
48
|
+
* name the field: each field's `label` and its `description`; each
|
|
49
|
+
* `members[]` band's label and description; and the
|
|
50
|
+
* same for a `list`'s `item` and an `object`'s `fields`, recursively. A schema
|
|
51
|
+
* that is not an object is one finding. Run at every door a schema arrives
|
|
52
|
+
* through — `register()` for node params, `genre()` for a shape's fields,
|
|
53
|
+
* a widget's `settings`, `validate()` for a document — so a blank label is
|
|
54
|
+
* refused where the author is and never reaches a form.
|
|
55
|
+
*
|
|
56
|
+
* `of` stays unread: an enum's options are stored values, not display text,
|
|
57
|
+
* and their labels live on `members[]`.
|
|
58
|
+
* @experimental
|
|
59
|
+
*/
|
|
60
|
+
export function settingsSchemaFindings(schema, where) {
|
|
61
|
+
if (schema === undefined)
|
|
62
|
+
return [];
|
|
63
|
+
if (!schema || typeof schema !== 'object' || Array.isArray(schema))
|
|
64
|
+
return [`${where}: a settings schema is an object keyed by field name — { depth: { type: 'integer', label: 'Depth' } }`];
|
|
65
|
+
const out = [];
|
|
66
|
+
for (const [key, raw] of Object.entries(schema)) {
|
|
67
|
+
const at = `${where}.${key}`;
|
|
68
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
69
|
+
out.push(`${at}: a field declaration is an object — { type: 'string', label: 'Name' }`);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const f = raw;
|
|
73
|
+
out.push(...i18nFindings(f.label, `${at}.label`));
|
|
74
|
+
out.push(...i18nFindings(f.description, `${at}.description`));
|
|
75
|
+
if (f.members !== undefined) {
|
|
76
|
+
if (!Array.isArray(f.members))
|
|
77
|
+
out.push(`${at}.members: the bands are an array — [{ key: 'lore', label: 'Lore' }]`);
|
|
78
|
+
else
|
|
79
|
+
f.members.forEach((m, i) => {
|
|
80
|
+
const band = `${at}.members[${typeof m?.key === 'string' ? m.key : i}]`;
|
|
81
|
+
if (!m || typeof m !== 'object') {
|
|
82
|
+
out.push(`${band}: a band is an object — { key: 'lore', label: 'Lore' }`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
out.push(...i18nFindings(m.label, `${band}.label`));
|
|
86
|
+
out.push(...i18nFindings(m.description, `${band}.description`));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (f.item !== undefined)
|
|
90
|
+
out.push(...settingsSchemaFindings({ item: f.item }, at));
|
|
91
|
+
if (f.fields !== undefined)
|
|
92
|
+
out.push(...settingsSchemaFindings(f.fields, `${at}.fields`));
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
/** The media kinds a `media` field offers. Images unless it says otherwise. @experimental */
|
|
97
|
+
export function fieldAccepts(decl) {
|
|
98
|
+
return decl.accepts?.length ? decl.accepts : ['image'];
|
|
99
|
+
}
|
|
100
|
+
// ── Declaration-time checks ─────────────────────────────────────────────────
|
|
101
|
+
/**
|
|
102
|
+
* A nested declaration's mistakes, reported at the path they live at.
|
|
103
|
+
*
|
|
104
|
+
* `list` and `object` make a schema a tree, and a finding that named only the
|
|
105
|
+
* top-level key would send an author looking at the wrong declaration. The path
|
|
106
|
+
* is the address a reader can follow: `blocks[].id`, `layout.columns[]`.
|
|
107
|
+
*
|
|
108
|
+
* ⚠ **A `secret` may not be nested**, and this is the guard for it rather than a
|
|
109
|
+
* documented caution. Redaction is flat everywhere it happens — `forClient`,
|
|
110
|
+
* `forExport` and `forOwningHookSplit` all walk the schema's own keys and switch on
|
|
111
|
+
* `type === 'secret'` — so a credential inside a list would be exported, sent to
|
|
112
|
+
* the browser, and written into a receipt with nothing anywhere saying so. The
|
|
113
|
+
* refusal is at declaration time, which is the only place it is cheap.
|
|
114
|
+
*/
|
|
115
|
+
function checkNested(decl, path) {
|
|
116
|
+
const f = [];
|
|
117
|
+
if (decl.type === 'secret')
|
|
118
|
+
f.push({
|
|
119
|
+
field: path,
|
|
120
|
+
severity: 'error',
|
|
121
|
+
message: `'${path}' is a secret nested inside a list or object`,
|
|
122
|
+
fix: 'declare it as a top-level field — redaction, export and receipts read the schema flat, so a nested secret would leak',
|
|
123
|
+
});
|
|
124
|
+
if (decl.type === 'list') {
|
|
125
|
+
if (!decl.item)
|
|
126
|
+
f.push({
|
|
127
|
+
field: path,
|
|
128
|
+
severity: 'error',
|
|
129
|
+
message: `'${path}' is a list with no element declaration`,
|
|
130
|
+
fix: "declare `item: { type: 'string' }` — a list whose elements are undeclared cannot be rendered or checked",
|
|
131
|
+
});
|
|
132
|
+
else
|
|
133
|
+
f.push(...checkNested(decl.item, `${path}[]`));
|
|
134
|
+
}
|
|
135
|
+
if (decl.type === 'object') {
|
|
136
|
+
if (!decl.fields || !Object.keys(decl.fields).length)
|
|
137
|
+
f.push({
|
|
138
|
+
field: path,
|
|
139
|
+
severity: 'error',
|
|
140
|
+
message: `'${path}' is an object with no member declarations`,
|
|
141
|
+
fix: 'declare `fields: { … }` — a free-form map is `text` with `format: "json"`',
|
|
142
|
+
});
|
|
143
|
+
else
|
|
144
|
+
for (const [k, member] of Object.entries(decl.fields))
|
|
145
|
+
f.push(...checkNested(member, `${path}.${k}`));
|
|
146
|
+
}
|
|
147
|
+
if (decl.type === 'enum' && !decl.of?.length && !decl.members?.length && !decl.from)
|
|
148
|
+
f.push({
|
|
149
|
+
field: path,
|
|
150
|
+
severity: 'error',
|
|
151
|
+
message: `'${path}' is an enum with no options`,
|
|
152
|
+
fix: "declare `of: ['a','b'] as const`, or source them from the connection with `from`",
|
|
153
|
+
});
|
|
154
|
+
return f;
|
|
155
|
+
}
|
|
156
|
+
/** Mistakes that would otherwise become silent leaks or dead form fields. @experimental */
|
|
157
|
+
export function checkSchema(schema) {
|
|
158
|
+
const f = [];
|
|
159
|
+
for (const [key, d] of Object.entries(schema)) {
|
|
160
|
+
// The tree below a `list` or an `object`, checked at its own address.
|
|
161
|
+
// The top-level cases below stay as they are: they are about the
|
|
162
|
+
// *field*, not the element, and two of them (`scope`, `side`) have no
|
|
163
|
+
// meaning inside a row.
|
|
164
|
+
if (d.type === 'list' || d.type === 'object')
|
|
165
|
+
f.push(...checkNested(d, key));
|
|
166
|
+
if (d.lend !== undefined && d.type !== 'secret')
|
|
167
|
+
f.push({
|
|
168
|
+
field: key,
|
|
169
|
+
severity: 'error',
|
|
170
|
+
message: `'${key}' says 'lend', which only a secret has`,
|
|
171
|
+
fix: "remove 'lend' — only a secret is withheld from other packages' pipelines, so only a secret can be lent" +
|
|
172
|
+
pluginRuleRef('secrets'),
|
|
173
|
+
});
|
|
174
|
+
if (d.type === 'secret') {
|
|
175
|
+
if (!SECRET_KEY.test(key))
|
|
176
|
+
f.push({
|
|
177
|
+
field: key,
|
|
178
|
+
severity: 'error',
|
|
179
|
+
message: `'${key}' is a secret whose name a handle cannot carry`,
|
|
180
|
+
fix: 'name it with letters, digits, "_", "." and "-" only',
|
|
181
|
+
});
|
|
182
|
+
if (d.side === 'component') {
|
|
183
|
+
f.push({
|
|
184
|
+
field: key,
|
|
185
|
+
severity: 'error',
|
|
186
|
+
message: `'${key}' is a secret declared component-side`,
|
|
187
|
+
fix: 'a component runs in the browser, so the value would be delivered to the client — declare it extension-side',
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
if (d.default !== undefined) {
|
|
191
|
+
f.push({
|
|
192
|
+
field: key,
|
|
193
|
+
severity: 'error',
|
|
194
|
+
message: `'${key}' is a secret with a default`,
|
|
195
|
+
fix: 'remove it — a shipped default credential is not a credential',
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (d.type === 'enum' && !d.of?.length && !d.from) {
|
|
200
|
+
f.push({
|
|
201
|
+
field: key,
|
|
202
|
+
severity: 'error',
|
|
203
|
+
message: `'${key}' is an enum with no options`,
|
|
204
|
+
fix: "declare `of: ['a','b'] as const`, or source them from the connection with `from`",
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
if (d.required && d.default !== undefined) {
|
|
208
|
+
f.push({
|
|
209
|
+
field: key,
|
|
210
|
+
severity: 'warning',
|
|
211
|
+
message: `'${key}' is required and has a default, so it can never be unset`,
|
|
212
|
+
fix: 'drop `required`, or drop the default if the admin genuinely has to choose',
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (d.showIf && !schema[d.showIf.field]) {
|
|
216
|
+
f.push({
|
|
217
|
+
field: key,
|
|
218
|
+
severity: 'error',
|
|
219
|
+
message: `'${key}' is shown conditionally on '${d.showIf.field}', which is not a field`,
|
|
220
|
+
fix: `name a field this schema declares (${Object.keys(schema).join(', ')})`,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return f;
|
|
225
|
+
}
|
|
226
|
+
// ── Value validation ────────────────────────────────────────────────────────
|
|
227
|
+
/**
|
|
228
|
+
* One value against one declaration, at the address it lives at.
|
|
229
|
+
*
|
|
230
|
+
* Extracted from `checkValues`'s loop so a `list`'s elements and an `object`'s
|
|
231
|
+
* members are checked by the same rules as a top-level field — a second copy of
|
|
232
|
+
* "an integer is whole and within its range" is a second set of rules to keep
|
|
233
|
+
* in step, and the first divergence is a stored value one layer accepts and the
|
|
234
|
+
* other refuses.
|
|
235
|
+
*/
|
|
236
|
+
function checkOne(decl, value, path) {
|
|
237
|
+
const f = [];
|
|
238
|
+
const bad = (why, fix) => f.push({ field: path, severity: 'error', message: `'${path}' ${why}`, fix });
|
|
239
|
+
switch (decl.type) {
|
|
240
|
+
case 'secret':
|
|
241
|
+
if (!isSecret(value))
|
|
242
|
+
bad('is not a secret value', 'write it through the settings form; secrets are never set as plain strings');
|
|
243
|
+
break;
|
|
244
|
+
case 'boolean':
|
|
245
|
+
if (typeof value !== 'boolean')
|
|
246
|
+
bad(`should be a boolean, got ${typeof value}`, 'store true or false');
|
|
247
|
+
break;
|
|
248
|
+
case 'integer':
|
|
249
|
+
case 'number': {
|
|
250
|
+
if (typeof value !== 'number' || Number.isNaN(value)) {
|
|
251
|
+
bad(`should be a number, got ${typeof value}`, 'store a number');
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
if (decl.type === 'integer' && !Number.isInteger(value))
|
|
255
|
+
bad('should be a whole number', 'round it, or declare the field as `number`');
|
|
256
|
+
if (decl.min !== undefined && value < decl.min)
|
|
257
|
+
bad(`is below the minimum ${decl.min}`, `use a value ≥ ${decl.min}`);
|
|
258
|
+
if (decl.max !== undefined && value > decl.max)
|
|
259
|
+
bad(`is above the maximum ${decl.max}`, `use a value ≤ ${decl.max}`);
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
case 'enum':
|
|
263
|
+
// `decl.of` only, deliberately: a `members`-declared enum has never
|
|
264
|
+
// been checked here, and starting to check it is a tightening this
|
|
265
|
+
// extension has no business making.
|
|
266
|
+
if (decl.of && !decl.of.includes(value))
|
|
267
|
+
bad(`is not one of ${decl.of.join(', ')}`, `use one of: ${decl.of.join(', ')}`);
|
|
268
|
+
break;
|
|
269
|
+
case 'string[]':
|
|
270
|
+
if (!Array.isArray(value))
|
|
271
|
+
bad('should be a list of strings', 'store an array');
|
|
272
|
+
break;
|
|
273
|
+
case 'list': {
|
|
274
|
+
if (!Array.isArray(value)) {
|
|
275
|
+
bad('should be a list', 'store an array — the order is part of the value');
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
// `min`/`max` are the element COUNT on a list, not a numeric range.
|
|
279
|
+
if (decl.min !== undefined && value.length < decl.min)
|
|
280
|
+
bad(`has fewer than ${decl.min} entries`, `keep at least ${decl.min}`);
|
|
281
|
+
if (decl.max !== undefined && value.length > decl.max)
|
|
282
|
+
bad(`has more than ${decl.max} entries`, `keep at most ${decl.max}`);
|
|
283
|
+
if (decl.item)
|
|
284
|
+
for (let i = 0; i < value.length; i++)
|
|
285
|
+
f.push(...checkOne(decl.item, value[i], `${path}[${i}]`));
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
case 'object': {
|
|
289
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
290
|
+
bad('should be an object', 'store a record of the declared members');
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
const row = value;
|
|
294
|
+
for (const [k, member] of Object.entries(decl.fields ?? {})) {
|
|
295
|
+
const mv = row[k];
|
|
296
|
+
if (mv === undefined || mv === null) {
|
|
297
|
+
if (member.required && member.default === undefined)
|
|
298
|
+
f.push({
|
|
299
|
+
field: `${path}.${k}`,
|
|
300
|
+
severity: 'error',
|
|
301
|
+
message: `'${path}.${k}' is required and not set`,
|
|
302
|
+
fix: 'fill it in — the row is incomplete without it',
|
|
303
|
+
});
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
f.push(...checkOne(member, mv, `${path}.${k}`));
|
|
307
|
+
}
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
default:
|
|
311
|
+
if (typeof value !== 'string')
|
|
312
|
+
bad(`should be a string, got ${typeof value}`, 'store a string');
|
|
313
|
+
}
|
|
314
|
+
return f;
|
|
315
|
+
}
|
|
316
|
+
/** @internal */
|
|
317
|
+
export function checkValues(schema, values) {
|
|
318
|
+
const f = [];
|
|
319
|
+
for (const [key, d] of Object.entries(schema)) {
|
|
320
|
+
const v = values[key];
|
|
321
|
+
if (v === undefined || v === null) {
|
|
322
|
+
if (d.required && d.default === undefined) {
|
|
323
|
+
f.push({
|
|
324
|
+
field: key,
|
|
325
|
+
severity: 'error',
|
|
326
|
+
message: `'${key}' is required and not set`,
|
|
327
|
+
fix: `set it in plugin settings — the plugin stays installed and listed until then, it is not broken`,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
f.push(...checkOne(d, v, key));
|
|
333
|
+
}
|
|
334
|
+
return f;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* What an update does to values that already exist.
|
|
338
|
+
*
|
|
339
|
+
* The rule is the one 12 §5 already applies to a node swap's orphaned slots: **unmigrated
|
|
340
|
+
* values land in diagnostics rather than disappearing.** An author who renames a field
|
|
341
|
+
* and an admin who then downgrades should both get their data back; silently dropping it
|
|
342
|
+
* makes the update irreversible in the one direction that matters.
|
|
343
|
+
* @internal
|
|
344
|
+
*/
|
|
345
|
+
export function reconcile(schema, stored) {
|
|
346
|
+
const values = {};
|
|
347
|
+
const orphaned = [];
|
|
348
|
+
for (const [key, d] of Object.entries(schema)) {
|
|
349
|
+
values[key] = key in stored ? stored[key] : d.default;
|
|
350
|
+
}
|
|
351
|
+
for (const [key, value] of Object.entries(stored)) {
|
|
352
|
+
if (key in schema)
|
|
353
|
+
continue;
|
|
354
|
+
orphaned.push({
|
|
355
|
+
field: key,
|
|
356
|
+
value: isSecret(value) ? '[secret]' : value,
|
|
357
|
+
reason: 'the updated schema no longer declares this field',
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
return { values, orphaned, findings: checkValues(schema, values) };
|
|
361
|
+
}
|
|
362
|
+
// ── The three audiences ─────────────────────────────────────────────────────
|
|
363
|
+
/** What the settings form sends back. A secret reports only whether it is set. @internal */
|
|
364
|
+
export function forClient(schema, values) {
|
|
365
|
+
const out = {};
|
|
366
|
+
for (const [key, d] of Object.entries(schema)) {
|
|
367
|
+
out[key] = d.type === 'secret' ? { $secretSet: isSecret(values[key]) } : values[key];
|
|
368
|
+
}
|
|
369
|
+
return out;
|
|
370
|
+
}
|
|
371
|
+
/** What an export carries. Secrets never leave, on the same footing as credentials. @experimental */
|
|
372
|
+
export function forExport(schema, values) {
|
|
373
|
+
const out = {};
|
|
374
|
+
for (const [key, d] of Object.entries(schema)) {
|
|
375
|
+
if (d.type === 'secret')
|
|
376
|
+
continue;
|
|
377
|
+
out[key] = values[key];
|
|
378
|
+
}
|
|
379
|
+
return out;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* What the declaring extension's own hook receives — the only place plaintext appears,
|
|
383
|
+
* and only for the extension that owns the field. Same shape as F18's per-call injection
|
|
384
|
+
* of connection material.
|
|
385
|
+
*/
|
|
386
|
+
/**
|
|
387
|
+
* The handle plugin code holds for a secret setting — never its value (R63).
|
|
388
|
+
* `nonce` is minted by the host per plugin load, so a handle cannot be forged
|
|
389
|
+
* from text that came from anywhere but the host.
|
|
390
|
+
* @experimental
|
|
391
|
+
*/
|
|
392
|
+
export const secretHandle = (key, nonce) => `\u27E6secret:${key}:${nonce}\u27E7`;
|
|
393
|
+
/** A secret's key, as a handle can carry it. @experimental */
|
|
394
|
+
export const SECRET_KEY = /^[A-Za-z0-9_.-]+$/;
|
|
395
|
+
/**
|
|
396
|
+
* What a hook's `settings` carries (R63): every value, and a **secret handle**
|
|
397
|
+
* in place of each secret — so the plugin's code never holds a key — beside
|
|
398
|
+
* the plaintext the host keeps for the fetch bridge and the scrub, and the
|
|
399
|
+
* keys the package lends to its nodes in other packages' pipelines.
|
|
400
|
+
* @internal
|
|
401
|
+
*/
|
|
402
|
+
export function forOwningHookSplit(schema, values, decrypt, nonce) {
|
|
403
|
+
const settings = {};
|
|
404
|
+
const secrets = {};
|
|
405
|
+
const lent = [];
|
|
406
|
+
for (const [key, d] of Object.entries(schema)) {
|
|
407
|
+
const v = values[key];
|
|
408
|
+
if (d.type !== 'secret') {
|
|
409
|
+
settings[key] = v;
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
// Unset stays unset.
|
|
413
|
+
if (v === undefined || v === null)
|
|
414
|
+
continue;
|
|
415
|
+
// A value stored before the field was a secret is plaintext already;
|
|
416
|
+
// it is treated as the secret it now is — a handle, never the value.
|
|
417
|
+
const plain = isSecret(v) ? decrypt(v.value) : typeof v === 'string' ? v : '';
|
|
418
|
+
if (!plain) {
|
|
419
|
+
settings[key] = '';
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
secrets[key] = plain;
|
|
423
|
+
settings[key] = secretHandle(key, nonce);
|
|
424
|
+
if (d.lend)
|
|
425
|
+
lent.push(key);
|
|
426
|
+
}
|
|
427
|
+
return { settings, secrets, lent };
|
|
428
|
+
}
|
|
429
|
+
/** What a component receives at render — extension-side fields never reach the browser. @experimental */
|
|
430
|
+
export function forComponent(schema, values) {
|
|
431
|
+
const out = {};
|
|
432
|
+
for (const [key, d] of Object.entries(schema)) {
|
|
433
|
+
if (d.side !== 'component')
|
|
434
|
+
continue;
|
|
435
|
+
out[key] = values[key];
|
|
436
|
+
}
|
|
437
|
+
return out;
|
|
438
|
+
}
|
|
439
|
+
/** @internal */
|
|
440
|
+
export function configState(schema, values) {
|
|
441
|
+
const missing = Object.entries(schema)
|
|
442
|
+
.filter(([k, d]) => d.required && d.default === undefined && (values[k] === undefined || values[k] === null))
|
|
443
|
+
.map(([k]) => k);
|
|
444
|
+
if (!missing.length)
|
|
445
|
+
return { state: 'ready' };
|
|
446
|
+
return {
|
|
447
|
+
state: 'needs-configuration',
|
|
448
|
+
missing,
|
|
449
|
+
message: `waiting on ${missing.join(', ')} in plugin settings`,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
/** Declaration order within a group; group order is first appearance. @experimental */
|
|
453
|
+
export function formLayout(schema) {
|
|
454
|
+
const groups = [];
|
|
455
|
+
for (const [key, decl] of Object.entries(schema)) {
|
|
456
|
+
const name = decl.group ?? 'General';
|
|
457
|
+
let g = groups.find((x) => x.group === name);
|
|
458
|
+
if (!g)
|
|
459
|
+
groups.push((g = { group: name, fields: [] }));
|
|
460
|
+
g.fields.push({ key, decl });
|
|
461
|
+
}
|
|
462
|
+
return groups;
|
|
463
|
+
}
|
|
464
|
+
/** Is this field currently shown, given the values? One level of `showIf`, no rules engine. @experimental */
|
|
465
|
+
export const isVisible = (decl, values) => !decl.showIf || values[decl.showIf.field] === decl.showIf.equals;
|
|
466
|
+
/** @experimental */
|
|
467
|
+
export class SettingsError extends Error {
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Declare a plugin's settings. The compiler extracts this statically into the manifest,
|
|
471
|
+
* so it must be a literal — a schema assembled at runtime cannot be read without running
|
|
472
|
+
* the author's code, which the packager never does (F6, 03 §3).
|
|
473
|
+
* @public
|
|
474
|
+
*/
|
|
475
|
+
export function defineSettings(schema) {
|
|
476
|
+
const errs = checkSchema(schema).filter((x) => x.severity === 'error');
|
|
477
|
+
if (errs.length) {
|
|
478
|
+
throw new SettingsError('invalid settings schema:\n' + errs.map((e) => ` ${e.message}\n → ${e.fix}`).join('\n'));
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
schema,
|
|
482
|
+
defaults: () => Object.fromEntries(Object.entries(schema)
|
|
483
|
+
.filter(([, d]) => d.default !== undefined)
|
|
484
|
+
.map(([k, d]) => [k, d.default])),
|
|
485
|
+
layout: () => formLayout(schema),
|
|
486
|
+
check: (v) => checkValues(schema, v),
|
|
487
|
+
reconcile: (v) => reconcile(schema, v),
|
|
488
|
+
state: (v) => configState(schema, v),
|
|
489
|
+
forClient: (v) => forClient(schema, v),
|
|
490
|
+
forExport: (v) => forExport(schema, v),
|
|
491
|
+
forComponent: (v) => forComponent(schema, v),
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
// ── Forms from data (review pauses, arbitrary extension forms) ──────────────
|
|
495
|
+
const humanize = (key) => key
|
|
496
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
497
|
+
.replace(/[_-]+/g, ' ')
|
|
498
|
+
.replace(/^./, (c) => c.toUpperCase());
|
|
499
|
+
/**
|
|
500
|
+
* A `SettingsSchema` inferred from a payload — the review gate's form producer.
|
|
501
|
+
*
|
|
502
|
+
* One field language for everything a person edits in a generated form: an
|
|
503
|
+
* extension's declared settings, an extension's arbitrary forms, and a paused
|
|
504
|
+
* node's payload all render through the same schema and the same renderer. A
|
|
505
|
+
* review form is therefore 100% defined by the data the node received: a
|
|
506
|
+
* string is a text field, a number is a number field, a flag is a checkbox,
|
|
507
|
+
* and structure a form cannot decompose arrives as JSON rather than being
|
|
508
|
+
* silently dropped — an edit surface that hides part of the payload is a
|
|
509
|
+
* review gate a write can sneak past.
|
|
510
|
+
* @experimental
|
|
511
|
+
*/
|
|
512
|
+
export function inferSchema(payload) {
|
|
513
|
+
const source = payload && typeof payload === 'object' && !Array.isArray(payload)
|
|
514
|
+
? payload
|
|
515
|
+
: { value: payload };
|
|
516
|
+
const schema = {};
|
|
517
|
+
for (const [key, v] of Object.entries(source)) {
|
|
518
|
+
const label = humanize(key);
|
|
519
|
+
if (typeof v === 'string') {
|
|
520
|
+
schema[key] = {
|
|
521
|
+
type: v.length > 80 || v.includes('\n') ? 'text' : 'string',
|
|
522
|
+
label,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
else if (typeof v === 'number') {
|
|
526
|
+
schema[key] = { type: Number.isInteger(v) ? 'integer' : 'number', label };
|
|
527
|
+
}
|
|
528
|
+
else if (typeof v === 'boolean') {
|
|
529
|
+
schema[key] = { type: 'boolean', label };
|
|
530
|
+
}
|
|
531
|
+
else if (Array.isArray(v) && v.every((x) => typeof x === 'string')) {
|
|
532
|
+
schema[key] = { type: 'string[]', label };
|
|
533
|
+
}
|
|
534
|
+
else {
|
|
535
|
+
schema[key] = { type: 'text', label, format: 'json' };
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return schema;
|
|
539
|
+
}
|
|
540
|
+
/** The payload as form values — JSON-format fields serialized for editing. @internal */
|
|
541
|
+
export function valuesForForm(schema, payload) {
|
|
542
|
+
const source = payload && typeof payload === 'object' && !Array.isArray(payload)
|
|
543
|
+
? payload
|
|
544
|
+
: { value: payload };
|
|
545
|
+
const out = {};
|
|
546
|
+
for (const [key, decl] of Object.entries(schema)) {
|
|
547
|
+
const v = source[key];
|
|
548
|
+
out[key] = decl.format === 'json' ? JSON.stringify(v ?? null, null, 2) : v;
|
|
549
|
+
}
|
|
550
|
+
return out;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Fold edited form values back into the payload shape the node expects.
|
|
554
|
+
*
|
|
555
|
+
* The inverse of `valuesForForm`: JSON-format fields parse back (an
|
|
556
|
+
* unparseable edit throws with the field named rather than committing a
|
|
557
|
+
* string where an object stood), untouched keys keep their original values —
|
|
558
|
+
* a form is an edit surface, never a filter.
|
|
559
|
+
* @internal
|
|
560
|
+
*/
|
|
561
|
+
export function applyFormValues(schema, payload, edited) {
|
|
562
|
+
const wrapped = !(payload &&
|
|
563
|
+
typeof payload === 'object' &&
|
|
564
|
+
!Array.isArray(payload));
|
|
565
|
+
const base = wrapped
|
|
566
|
+
? { value: payload }
|
|
567
|
+
: { ...payload };
|
|
568
|
+
for (const [key, decl] of Object.entries(schema)) {
|
|
569
|
+
if (!(key in edited))
|
|
570
|
+
continue;
|
|
571
|
+
const v = edited[key];
|
|
572
|
+
if (decl.format === 'json') {
|
|
573
|
+
try {
|
|
574
|
+
base[key] = JSON.parse(String(v));
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
throw new SettingsError(`'${key}' is not valid JSON — the field holds structure the form ` +
|
|
578
|
+
`cannot decompose, so it must parse before it can be committed.`);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
else if (decl.type === 'number' || decl.type === 'integer') {
|
|
582
|
+
const n = typeof v === 'number' ? v : Number(v);
|
|
583
|
+
if (Number.isNaN(n))
|
|
584
|
+
throw new SettingsError(`'${key}' must be a number.`);
|
|
585
|
+
base[key] = decl.type === 'integer' ? Math.trunc(n) : n;
|
|
586
|
+
}
|
|
587
|
+
else if (decl.type === 'boolean') {
|
|
588
|
+
base[key] = !!v;
|
|
589
|
+
}
|
|
590
|
+
else if (decl.type === 'string[]') {
|
|
591
|
+
base[key] = Array.isArray(v)
|
|
592
|
+
? v.map(String)
|
|
593
|
+
: String(v ?? '')
|
|
594
|
+
.split('\n')
|
|
595
|
+
.map((l) => l.trim())
|
|
596
|
+
.filter(Boolean);
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
base[key] = String(v ?? '');
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return wrapped ? base['value'] : base;
|
|
603
|
+
}
|
|
604
|
+
//# sourceMappingURL=settings.js.map
|