@serene-pub/sdk 0.6.0-pr-5 → 0.6.0-pr-7
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/dist/actions.d.ts +52 -2
- package/dist/actions.js +56 -6
- package/dist/annexFields.d.ts +32 -2
- package/dist/annexFields.js +48 -5
- package/dist/announce.d.ts +142 -2
- package/dist/announce.js +198 -2
- package/dist/attributes.d.ts +331 -6
- package/dist/attributes.js +339 -27
- package/dist/bridges.d.ts +61 -0
- package/dist/bridges.js +112 -0
- package/dist/builder.d.ts +278 -51
- package/dist/builder.js +267 -30
- package/dist/cadences.d.ts +585 -0
- package/dist/cadences.js +1118 -0
- package/dist/capabilities.d.ts +106 -17
- package/dist/capabilities.js +85 -7
- package/dist/checkMethods.d.ts +240 -0
- package/dist/checkMethods.js +362 -0
- package/dist/component.d.ts +6 -1
- package/dist/componentClient.d.ts +42 -6
- package/dist/componentWire.js +22 -4
- package/dist/composerStates.d.ts +96 -0
- package/dist/composerStates.js +190 -0
- package/dist/conditions.d.ts +424 -0
- package/dist/conditions.js +521 -0
- package/dist/config.d.ts +42 -6
- package/dist/config.js +48 -20
- package/dist/conversions.d.ts +41 -0
- package/dist/conversions.js +50 -0
- package/dist/declarations.d.ts +40 -2
- package/dist/declarations.js +178 -10
- package/dist/descriptors.d.ts +287 -4
- package/dist/descriptors.js +281 -18
- package/dist/document.d.ts +47 -2
- package/dist/document.js +46 -14
- package/dist/entities.d.ts +156 -0
- package/dist/entities.js +259 -0
- package/dist/events.d.ts +711 -1
- package/dist/events.js +412 -1
- package/dist/executor.d.ts +4 -3
- package/dist/executor.js +22 -8
- package/dist/exposure.d.ts +128 -0
- package/dist/exposure.js +168 -0
- package/dist/extension.d.ts +127 -3
- package/dist/extension.js +43 -0
- package/dist/fit.d.ts +198 -0
- package/dist/fit.js +351 -0
- package/dist/functions.d.ts +554 -0
- package/dist/functions.js +712 -0
- package/dist/genres.d.ts +303 -89
- package/dist/genres.js +335 -100
- package/dist/hookGrants.d.ts +15 -1
- package/dist/hookGrants.js +11 -10
- package/dist/hooks.d.ts +34 -5
- package/dist/hooks.js +5 -2
- package/dist/hostElements.d.ts +7 -1
- package/dist/hostElements.js +20 -1
- package/dist/hostState.d.ts +110 -0
- package/dist/hostState.js +140 -0
- package/dist/identity.d.ts +1 -1
- package/dist/identity.js +1 -1
- package/dist/index.d.ts +25 -0
- package/dist/index.js +48 -0
- package/dist/jsonSchema.d.ts +178 -0
- package/dist/jsonSchema.js +570 -0
- package/dist/libraryDraft.d.ts +27 -0
- package/dist/libraryDraft.js +25 -0
- package/dist/lorebookFile.d.ts +44 -0
- package/dist/lorebookFile.js +202 -0
- package/dist/media.d.ts +127 -12
- package/dist/media.js +45 -1
- package/dist/messageBlocks.d.ts +179 -5
- package/dist/messageBlocks.js +208 -6
- package/dist/participants.d.ts +32 -9
- package/dist/participants.js +13 -2
- package/dist/pluginHarness.d.ts +63 -33
- package/dist/pluginHarness.js +78 -144
- package/dist/pluginPermissions.d.ts +15 -1
- package/dist/pluginPermissions.js +29 -2
- package/dist/preview.d.ts +45 -4
- package/dist/preview.js +52 -12
- package/dist/primers.d.ts +177 -0
- package/dist/primers.js +265 -0
- package/dist/receipt.d.ts +11 -0
- package/dist/receiverRules.js +9 -0
- package/dist/refs.d.ts +9 -1
- package/dist/refs.js +12 -0
- package/dist/registrations.d.ts +384 -0
- package/dist/registrations.js +625 -0
- package/dist/repertoires.d.ts +502 -0
- package/dist/repertoires.js +666 -0
- package/dist/rulesetDocument.d.ts +116 -0
- package/dist/rulesetDocument.js +252 -0
- package/dist/rulesetSections.d.ts +631 -0
- package/dist/rulesetSections.js +1288 -0
- package/dist/rulesetTemplates.d.ts +161 -0
- package/dist/rulesetTemplates.js +242 -0
- package/dist/rulesets.d.ts +899 -0
- package/dist/rulesets.js +1378 -0
- package/dist/sceneSettings.d.ts +8 -0
- package/dist/sceneSettings.js +10 -2
- package/dist/scope.d.ts +44 -3
- package/dist/scope.js +18 -0
- package/dist/scripts.d.ts +31 -8
- package/dist/scripts.js +45 -8
- package/dist/sessionDossier.d.ts +157 -7
- package/dist/sessionLayout.d.ts +291 -138
- package/dist/sessionLayout.js +504 -211
- package/dist/sessionSettings.d.ts +24 -0
- package/dist/settings.d.ts +95 -4
- package/dist/settings.js +293 -4
- package/dist/shapes.d.ts +80 -1
- package/dist/shapes.js +100 -5
- package/dist/speech.d.ts +333 -0
- package/dist/speech.js +199 -0
- package/dist/statShapes.d.ts +8 -1
- package/dist/statShapes.js +8 -1
- package/dist/status.d.ts +14 -0
- package/dist/status.js +12 -0
- package/dist/storage.d.ts +53 -66
- package/dist/storage.js +7 -12
- package/dist/storyTime.d.ts +25 -1
- package/dist/storyTime.js +42 -2
- package/dist/surfaces.d.ts +15 -2
- package/dist/template.d.ts +19 -1
- package/dist/template.js +25 -11
- package/dist/templateScopeAt.d.ts +1 -1
- package/dist/templateScopeAt.js +11 -8
- package/dist/ties.d.ts +211 -0
- package/dist/ties.js +301 -0
- package/dist/turnOrder.d.ts +25 -14
- package/dist/turnOrder.js +21 -8
- package/dist/userAnnex.d.ts +126 -0
- package/dist/userAnnex.js +214 -0
- package/dist/validate.js +114 -19
- package/dist/values.js +6 -0
- package/dist/vocabularies.d.ts +192 -0
- package/dist/vocabularies.js +326 -0
- package/dist/widgetDecls.d.ts +12 -0
- package/dist/widgetDecls.js +2 -0
- package/dist/widgets.d.ts +1062 -29
- package/dist/widgets.js +218 -3
- package/host-elements.d.ts +90 -0
- package/package.json +1 -1
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🚧 **Registrations** — which pipeline runs on which trigger (PLAN-layering
|
|
3
|
+
* D1; R6, R7, R24, R30, R31, R34, A4, A8, A12; LS1).
|
|
4
|
+
*
|
|
5
|
+
* Pipelines and the things they listen to are two separate things. A
|
|
6
|
+
* pipeline declares what it does (and the events it **suits**, for pickers
|
|
7
|
+
* only); a **registration** says "this trigger runs this pipeline, here":
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* register(coreEvents.messageRespond, storyReply, { values: { … } }) // an event pipeline
|
|
11
|
+
* register(narrate.actions.narrate, narrate) // an action pipeline
|
|
12
|
+
* register(everyNTurns({ n: 5 }), storySweep, { mode: 'review' }) // a routine
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* The genre's defaults hold registrations; a preset adds, drops and changes
|
|
16
|
+
* them (one level, R2); a session keeps its own rows. Every layer's
|
|
17
|
+
* registrations are **data with an origin** (§11-1), and every origin passes
|
|
18
|
+
* one door: `registrationFindings` (§11-2).
|
|
19
|
+
*
|
|
20
|
+
* ## The key is derived, never typed (A8, R34)
|
|
21
|
+
*
|
|
22
|
+
* `register()` derives the registration's stable identity from what it is —
|
|
23
|
+
* the trigger and the pipeline: `message-respond→story-reply`,
|
|
24
|
+
* `every-n-turns→story-sweep`, `story-narrate#narrate→story-narrate`. A core
|
|
25
|
+
* id contributes its name; another package's its namespace and name
|
|
26
|
+
* (`acme.dice:roll`), so two packages' pipelines never share a key. Changing
|
|
27
|
+
* a cadence's options or a `when` keeps the key — stored session values and
|
|
28
|
+
* a routine's queue survive an administrator's edit; a different pipeline or
|
|
29
|
+
* trigger is a different registration. Two alike in one layer warn, never
|
|
30
|
+
* refuse (R34).
|
|
31
|
+
*
|
|
32
|
+
* ## Objects first; strings warn (R30, A18)
|
|
33
|
+
*
|
|
34
|
+
* `register()` takes the declared objects — an event (`coreEvents.…`, a
|
|
35
|
+
* package's `defineSessionEvent`), an action handle (`spec.actions.<key>`),
|
|
36
|
+
* a cadence (`everyNTurns(…)`), a pipeline (the spec you built, or
|
|
37
|
+
* `use('…')`). A string still works, as a fallback, and the door says so.
|
|
38
|
+
* @experimental
|
|
39
|
+
* @ruling R6, R7, R24, R30, R31, R34, A4, A8, A12, LS1, §11
|
|
40
|
+
*/
|
|
41
|
+
import { i18nFindings } from './i18n.js';
|
|
42
|
+
import { EVENT_ID, eventById, eventIdOf, isEventDef, isSessionEventDecl, notADeclaredEvent, } from './events.js';
|
|
43
|
+
import { CADENCE_ID, ON_EVENT, cadenceClock, cadenceOptionFindings, isCadenceRef, } from './cadences.js';
|
|
44
|
+
import { conditionFindings } from './conditions.js';
|
|
45
|
+
import { fitOf } from './entities.js';
|
|
46
|
+
import { actionsOf, isActionIdentity } from './actions.js';
|
|
47
|
+
import { getDefinition } from './descriptors.js';
|
|
48
|
+
import { exposureFindings } from './exposure.js';
|
|
49
|
+
/** The families core's host supports today. @experimental */
|
|
50
|
+
export const TRIGGER_FAMILIES = ['event', 'action', 'routine'];
|
|
51
|
+
/** The routine modes. @experimental */
|
|
52
|
+
export const ROUTINE_MODES = ['off', 'review', 'full'];
|
|
53
|
+
const OPTION_KEYS = ['when', 'values', 'order', 'mode', 'allowStacking', 'caps', 'label', 'minAge'];
|
|
54
|
+
/** 🚧 The routine default (ruled 2026-10-24). @experimental */
|
|
55
|
+
export const DEFAULT_ROUTINE_MIN_AGE = 20;
|
|
56
|
+
/** 🚧 A registration's effective minAge. @experimental */
|
|
57
|
+
export function minAgeOf(r) {
|
|
58
|
+
return r.minAge ?? (r.trigger.family === 'routine' ? DEFAULT_ROUTINE_MIN_AGE : 0);
|
|
59
|
+
}
|
|
60
|
+
/** Is this an action handle? @experimental */
|
|
61
|
+
export const isActionHandle = (v) => !!v &&
|
|
62
|
+
typeof v === 'object' &&
|
|
63
|
+
v.__handle === 'action' &&
|
|
64
|
+
typeof v.identity === 'string';
|
|
65
|
+
/** The handle of one declared action. @internal */
|
|
66
|
+
export function actionHandle(specId, a) {
|
|
67
|
+
const venues = (Array.isArray(a.venue) ? a.venue : [a.venue])
|
|
68
|
+
.map((v) => v?.kind)
|
|
69
|
+
.filter((k) => !!k);
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
__handle: 'action',
|
|
72
|
+
spec: specId,
|
|
73
|
+
key: a.key,
|
|
74
|
+
identity: `${specId}#${a.key}`,
|
|
75
|
+
venues: Object.freeze([...new Set(venues)]),
|
|
76
|
+
collects: Object.freeze(Object.keys(a.collects ?? {})),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The handles of every action a spec (or its document) declares, by key.
|
|
81
|
+
* `spec.actions` on a built spec is this, typed.
|
|
82
|
+
* @experimental
|
|
83
|
+
*/
|
|
84
|
+
export function actionHandlesOf(s) {
|
|
85
|
+
const contributes = s.meta ? s.meta.contributes : s.contributes;
|
|
86
|
+
const out = {};
|
|
87
|
+
for (const a of actionsOf({ id: s.id, contributes }))
|
|
88
|
+
out[a.key] = actionHandle(s.id, a);
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
// ── Keys ────────────────────────────────────────────────────────────────────
|
|
92
|
+
/** The arrow a registration key joins its two halves with. @experimental */
|
|
93
|
+
export const REGISTRATION_KEY_JOIN = '→';
|
|
94
|
+
/**
|
|
95
|
+
* The part of an id a key carries: a core id's name (`message-respond`,
|
|
96
|
+
* `story-reply`, `every-n-turns`), another package's namespace and name
|
|
97
|
+
* (`acme.dice:roll`) — so two packages never share a key.
|
|
98
|
+
*/
|
|
99
|
+
function keySegment(id) {
|
|
100
|
+
const bare = id.replace(/@[^@/#]*$/, '');
|
|
101
|
+
const colon = bare.indexOf(':');
|
|
102
|
+
if (colon === -1)
|
|
103
|
+
return bare;
|
|
104
|
+
const ns = bare.slice(0, colon);
|
|
105
|
+
const tail = bare.slice(bare.lastIndexOf('/') + 1).replace(/^[^:]*:/, '');
|
|
106
|
+
return ns === 'core' ? tail : `${ns}:${tail}`;
|
|
107
|
+
}
|
|
108
|
+
/** A trigger's half of a registration key. @experimental */
|
|
109
|
+
export function triggerKeyOf(trigger) {
|
|
110
|
+
switch (trigger.family) {
|
|
111
|
+
case 'event':
|
|
112
|
+
return keySegment(trigger.event);
|
|
113
|
+
case 'action': {
|
|
114
|
+
const identity = trigger.action;
|
|
115
|
+
const hash = identity.lastIndexOf('#');
|
|
116
|
+
return `${keySegment(identity.slice(0, hash))}#${identity.slice(hash + 1)}`;
|
|
117
|
+
}
|
|
118
|
+
case 'routine':
|
|
119
|
+
return keySegment(trigger.cadence.id);
|
|
120
|
+
default: {
|
|
121
|
+
const ref = trigger.ref;
|
|
122
|
+
return `${trigger.family}:${typeof ref === 'string' ? keySegment(ref) : ''}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* A registration's stable identity, derived (A8): `<trigger key>→<pipeline
|
|
128
|
+
* key>` — `message-respond→story-reply`, `every-n-turns→story-sweep`,
|
|
129
|
+
* `story-narrate#narrate→story-narrate`. Pure; the host derives the same key
|
|
130
|
+
* for a row it stores.
|
|
131
|
+
* @experimental
|
|
132
|
+
* @ruling A8, R34
|
|
133
|
+
*/
|
|
134
|
+
export function registrationKeyOf(trigger, pipeline) {
|
|
135
|
+
// Through `triggerOf`, which reads an event object (whose own `family` is
|
|
136
|
+
// data or action) before a trigger (whose `family` is event, action, …).
|
|
137
|
+
const t = triggerOf(trigger).trigger;
|
|
138
|
+
const id = typeof pipeline === 'string' ? pipeline : pipeline.id;
|
|
139
|
+
return `${triggerKeyOf(t)}${REGISTRATION_KEY_JOIN}${keySegment(id)}`;
|
|
140
|
+
}
|
|
141
|
+
// ── register() ──────────────────────────────────────────────────────────────
|
|
142
|
+
/** Registrations written with a string where an object exists (R30) — warned at the door. */
|
|
143
|
+
const spelled = new WeakMap();
|
|
144
|
+
/** The version range a `use('…@range')` pipeline carried — the preset's requirement (A16). */
|
|
145
|
+
const ranges = new WeakMap();
|
|
146
|
+
/** A trigger read from what `register()` was handed; `spelled` names the strings. @internal */
|
|
147
|
+
export function triggerOf(input) {
|
|
148
|
+
if (typeof input === 'string') {
|
|
149
|
+
if (EVENT_ID.test(input))
|
|
150
|
+
return { trigger: { family: 'event', event: input }, spelled: input };
|
|
151
|
+
if (CADENCE_ID.test(input))
|
|
152
|
+
return {
|
|
153
|
+
trigger: {
|
|
154
|
+
family: 'routine',
|
|
155
|
+
cadence: Object.freeze({ id: input, options: Object.freeze({}) }),
|
|
156
|
+
},
|
|
157
|
+
spelled: input,
|
|
158
|
+
};
|
|
159
|
+
if (isActionIdentity(input))
|
|
160
|
+
return { trigger: { family: 'action', action: input }, spelled: input };
|
|
161
|
+
throw new Error(`register(): '${input}' is not an event id, an action identity or a cadence id — pass the object: ` +
|
|
162
|
+
`coreEvents.messageRespond, spec.actions.<key>, everyNTurns({ n: 5 })`);
|
|
163
|
+
}
|
|
164
|
+
if (isSessionEventDecl(input) || isEventDef(input))
|
|
165
|
+
return { trigger: { family: 'event', event: eventIdOf(input) } };
|
|
166
|
+
if (isActionHandle(input))
|
|
167
|
+
return { trigger: { family: 'action', action: input.identity } };
|
|
168
|
+
if (isCadenceRef(input))
|
|
169
|
+
return { trigger: { family: 'routine', cadence: input } };
|
|
170
|
+
if (input &&
|
|
171
|
+
typeof input === 'object' &&
|
|
172
|
+
typeof input.family === 'string')
|
|
173
|
+
return { trigger: input };
|
|
174
|
+
throw new Error(`register(): the trigger is ${input === null ? 'null' : typeof input} — pass an event (coreEvents.…), ` +
|
|
175
|
+
`an action (spec.actions.<key>) or a cadence (everyNTurns(…), systemCron(…), …)`);
|
|
176
|
+
}
|
|
177
|
+
/** The spec id a registration names, and the range a `use()` carried. @internal */
|
|
178
|
+
export function pipelineOf(input) {
|
|
179
|
+
if (typeof input === 'string') {
|
|
180
|
+
if (!input)
|
|
181
|
+
throw new Error("register(): the pipeline is an empty string — pass the spec you built, or use('<spec id>')");
|
|
182
|
+
return { id: input.replace(/@\d+$/, ''), spelled: input };
|
|
183
|
+
}
|
|
184
|
+
if (input && typeof input === 'object' && input.kind === 'external-ref')
|
|
185
|
+
return {
|
|
186
|
+
id: input.id,
|
|
187
|
+
...(input.range ? { range: input.range } : {}),
|
|
188
|
+
};
|
|
189
|
+
if (input && typeof input === 'object' && typeof input.id === 'string')
|
|
190
|
+
return { id: input.id };
|
|
191
|
+
throw new Error(`register(): the pipeline is ${input === null ? 'null' : typeof input} — pass the spec you built, or use('<spec id>')`);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Register a pipeline on a trigger — the one way a pipeline is mounted. The
|
|
195
|
+
* key is derived (A8); `opts` are the registration's `when`, `values`,
|
|
196
|
+
* `order` and, for a routine, `mode`, `allowStacking` and `caps`. Nothing
|
|
197
|
+
* here refuses what the door judges (fit, cadence options, duplicates): call
|
|
198
|
+
* `registrationFindings`, which a package build and a host both do.
|
|
199
|
+
* @experimental
|
|
200
|
+
* @ruling D1, A8, R30
|
|
201
|
+
*/
|
|
202
|
+
export function register(trigger, pipeline, opts = {}) {
|
|
203
|
+
const t = triggerOf(trigger);
|
|
204
|
+
const p = pipelineOf(pipeline);
|
|
205
|
+
if (opts && typeof opts !== 'object')
|
|
206
|
+
throw new Error('register(): options are an object — { when?, values?, order?, mode?, … }');
|
|
207
|
+
for (const k of Object.keys(opts ?? {}))
|
|
208
|
+
if (!OPTION_KEYS.includes(k))
|
|
209
|
+
throw new Error(`register(): '${k}' is not a registration option — one of ${OPTION_KEYS.join(', ')}`);
|
|
210
|
+
const decl = Object.freeze({
|
|
211
|
+
key: registrationKeyOf(t.trigger, p.id),
|
|
212
|
+
trigger: Object.freeze({ ...t.trigger }),
|
|
213
|
+
pipeline: p.id,
|
|
214
|
+
...Object.fromEntries(OPTION_KEYS.filter((k) => opts?.[k] !== undefined).map((k) => [k, opts[k]])),
|
|
215
|
+
});
|
|
216
|
+
const strings = [t.spelled, p.spelled].filter((s) => !!s);
|
|
217
|
+
if (strings.length)
|
|
218
|
+
spelled.set(decl, strings);
|
|
219
|
+
if (p.range)
|
|
220
|
+
ranges.set(decl, p.range);
|
|
221
|
+
return decl;
|
|
222
|
+
}
|
|
223
|
+
/** The `use('…@range')` range a registration's pipeline carried, if any (A16). @internal */
|
|
224
|
+
export const registrationRange = (decl) => ranges.get(decl);
|
|
225
|
+
const MAX_ORDER = 1_000_000;
|
|
226
|
+
/** Every pipeline a registration list names that emits — `{ event → [emitted] }` per registration. */
|
|
227
|
+
function emitsOf(p) {
|
|
228
|
+
return [...(p?.emits ?? [])];
|
|
229
|
+
}
|
|
230
|
+
/** The event a registration listens to, when it listens to one (an event, or a routine on `onEvent`). */
|
|
231
|
+
function listenedEvent(d) {
|
|
232
|
+
if (d.trigger?.family === 'event')
|
|
233
|
+
return d.trigger.event;
|
|
234
|
+
if (d.trigger?.family === 'routine' && d.trigger.cadence?.id === ON_EVENT.id) {
|
|
235
|
+
const ev = d.trigger.cadence.options?.event;
|
|
236
|
+
return typeof ev === 'string' ? ev : undefined;
|
|
237
|
+
}
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Cycles among emitted events (A12): a registration listening to E runs a
|
|
242
|
+
* pipeline that emits F, a registration listening to F runs one that emits E.
|
|
243
|
+
* Each cycle once, as the events in order.
|
|
244
|
+
* @experimental
|
|
245
|
+
* @ruling A12
|
|
246
|
+
*/
|
|
247
|
+
export function emitCycles(decls, pipeline) {
|
|
248
|
+
const edges = new Map();
|
|
249
|
+
for (const d of decls) {
|
|
250
|
+
const ev = listenedEvent(d);
|
|
251
|
+
if (!ev)
|
|
252
|
+
continue;
|
|
253
|
+
for (const out of emitsOf(pipeline(d.pipeline))) {
|
|
254
|
+
const set = edges.get(ev) ?? new Set();
|
|
255
|
+
set.add(out);
|
|
256
|
+
edges.set(ev, set);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const cycles = [];
|
|
260
|
+
const seen = new Set();
|
|
261
|
+
const visit = (node, path) => {
|
|
262
|
+
const at = path.indexOf(node);
|
|
263
|
+
if (at !== -1) {
|
|
264
|
+
const cycle = path.slice(at);
|
|
265
|
+
const id = [...cycle].sort().join('|');
|
|
266
|
+
if (!seen.has(id)) {
|
|
267
|
+
seen.add(id);
|
|
268
|
+
cycles.push([...cycle, node]);
|
|
269
|
+
}
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (path.length > 64)
|
|
273
|
+
return;
|
|
274
|
+
for (const next of edges.get(node) ?? [])
|
|
275
|
+
visit(next, [...path, node]);
|
|
276
|
+
};
|
|
277
|
+
for (const start of [...edges.keys()].sort())
|
|
278
|
+
visit(start, []);
|
|
279
|
+
return cycles;
|
|
280
|
+
}
|
|
281
|
+
const err = (law, message, fix) => ({
|
|
282
|
+
law,
|
|
283
|
+
severity: 'error',
|
|
284
|
+
message,
|
|
285
|
+
fix,
|
|
286
|
+
});
|
|
287
|
+
const warn = (law, message, fix) => ({
|
|
288
|
+
law,
|
|
289
|
+
severity: 'warning',
|
|
290
|
+
message,
|
|
291
|
+
fix,
|
|
292
|
+
});
|
|
293
|
+
/** The slots a registration's values may set (R38): never a connection. */
|
|
294
|
+
const VALUE_SLOTS = [
|
|
295
|
+
'prompts',
|
|
296
|
+
'template',
|
|
297
|
+
'variables',
|
|
298
|
+
'sampling',
|
|
299
|
+
'speechSampling',
|
|
300
|
+
'loadout',
|
|
301
|
+
'params',
|
|
302
|
+
'settings',
|
|
303
|
+
'scripts',
|
|
304
|
+
];
|
|
305
|
+
/**
|
|
306
|
+
* **The one registration door** (§11-2): every origin — the genre's defaults,
|
|
307
|
+
* a preset, a session row, a future tool — is judged here, by the SDK at a
|
|
308
|
+
* package build and by the host at install, update and publish (A11).
|
|
309
|
+
*
|
|
310
|
+
* · the family is one the host supports (LS1);
|
|
311
|
+
* · the trigger names something declared — an event, an action its spec
|
|
312
|
+
* contributes, a cadence with valid options (R10, R15, R20) and, on the
|
|
313
|
+
* story clock, a session that has one;
|
|
314
|
+
* · the pipeline resolves, and **fits** the trigger (R4, R12);
|
|
315
|
+
* · routine-only fields (`mode`, `allowStacking`, `caps`) sit on routines,
|
|
316
|
+
* caps on the system clock and within the system's (R11);
|
|
317
|
+
* · `values` set the slots a registration may (never a connection), on
|
|
318
|
+
* nodes the pipeline has;
|
|
319
|
+
* · the pipeline exposes no model plumbing to the owner (ruled 10-12);
|
|
320
|
+
* · two registrations alike **warn** (R34); a cycle among emitted events
|
|
321
|
+
* **refuses** (A12); a string where an object exists **warns** (R30).
|
|
322
|
+
* @experimental
|
|
323
|
+
* @ruling §11-2, R4, R11, R12, R30, R34, A11, A12, LS1
|
|
324
|
+
*/
|
|
325
|
+
export function registrationFindings(decls, ctx = {}) {
|
|
326
|
+
const out = [];
|
|
327
|
+
const layer = ctx.layer ? ` in ${ctx.layer}` : '';
|
|
328
|
+
const families = ctx.families ?? TRIGGER_FAMILIES;
|
|
329
|
+
const counts = new Map();
|
|
330
|
+
for (const [i, d] of decls.entries()) {
|
|
331
|
+
const at = `registration '${d?.key ?? `#${i + 1}`}'${layer}`;
|
|
332
|
+
if (!d || typeof d !== 'object' || !d.trigger || typeof d.pipeline !== 'string') {
|
|
333
|
+
out.push(err('registration shape', `${at} is not a registration`, 'make it with register(trigger, pipeline, { … })'));
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
counts.set(d.key, (counts.get(d.key) ?? 0) + 1);
|
|
337
|
+
const strings = spelled.get(d);
|
|
338
|
+
if (strings)
|
|
339
|
+
out.push(warn('registration reference', `${at} names ${strings.map((s) => `'${s}'`).join(' and ')} as a string`, 'pass the declared object — coreEvents.…, spec.actions.<key>, a cadence, the spec value or use(…) — so a typo is a compile error (R30)'));
|
|
340
|
+
const family = d.trigger.family;
|
|
341
|
+
if (!families.includes(family)) {
|
|
342
|
+
out.push(err('registration family', `${at}: ${family} pipelines are not available on this host yet`, `register on ${families.join(', ')}`));
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
const expected = registrationKeyOf(d.trigger, d.pipeline);
|
|
346
|
+
if (d.key !== expected)
|
|
347
|
+
out.push(err('registration key', `${at}: the key is '${d.key}', and its trigger and pipeline derive '${expected}'`, 'never type a key — register() derives it (A8)'));
|
|
348
|
+
// The trigger.
|
|
349
|
+
let action;
|
|
350
|
+
if (d.trigger.family === 'event') {
|
|
351
|
+
const ev = d.trigger.event;
|
|
352
|
+
if (!eventById(ev))
|
|
353
|
+
out.push(err('registration trigger', `${at}: ${notADeclaredEvent(ev)}`, 'register on a declared event'));
|
|
354
|
+
else if (ev === 'core:event/turn-order-changed@1')
|
|
355
|
+
out.push(err('registration trigger', `${at}: '${ev}' is core-internal — the turn-order push reads it`, 'register on core:event/message-completed@1 or another session event'));
|
|
356
|
+
}
|
|
357
|
+
else if (d.trigger.family === 'action') {
|
|
358
|
+
const identity = d.trigger.action;
|
|
359
|
+
if (!isActionIdentity(identity))
|
|
360
|
+
out.push(err('registration trigger', `${at}: '${identity}' is not an action identity`, "name the action by its handle — spec.actions.<key> ('<spec slug>#<key>')"));
|
|
361
|
+
else if (ctx.pipeline) {
|
|
362
|
+
const hash = identity.lastIndexOf('#');
|
|
363
|
+
const specId = identity.slice(0, hash);
|
|
364
|
+
const declarer = ctx.external?.(specId) ? undefined : ctx.pipeline(specId);
|
|
365
|
+
if (declarer) {
|
|
366
|
+
const contributes = 'meta' in declarer
|
|
367
|
+
? declarer.meta.contributes
|
|
368
|
+
: declarer.contributes;
|
|
369
|
+
const found = actionsOf({ id: declarer.id, contributes }).find((a) => a.key === identity.slice(hash + 1));
|
|
370
|
+
if (!found)
|
|
371
|
+
out.push(err('registration trigger', `${at}: '${specId}' declares no action '${identity.slice(hash + 1)}'`, 'name an action the spec contributes — spec.actions.<key>'));
|
|
372
|
+
else
|
|
373
|
+
action = found;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
else if (d.trigger.family === 'routine') {
|
|
378
|
+
for (const f of cadenceOptionFindings(d.trigger.cadence, `${at} cadence`))
|
|
379
|
+
out.push(err('cadence options', f, 'state the cadence with its ready-made function — everyNTurns({ n }), systemCron({ every, unit }), …'));
|
|
380
|
+
if (ctx.storyClock === false &&
|
|
381
|
+
d.trigger.cadence &&
|
|
382
|
+
cadenceClock(d.trigger.cadence) === 'story')
|
|
383
|
+
out.push(err('cadence clock', `${at}: needs a story clock, and the session has none`, 'attach a lorebook with a calendar, or register on another cadence'));
|
|
384
|
+
}
|
|
385
|
+
// Routine-only fields.
|
|
386
|
+
const routine = d.trigger.family === 'routine';
|
|
387
|
+
for (const k of ['mode', 'allowStacking', 'caps'])
|
|
388
|
+
if (d[k] !== undefined && !routine)
|
|
389
|
+
out.push(err('routine fields', `${at}: '${k}' is a routine's — this registration is on ${d.trigger.family === 'event' ? 'an event' : 'an action'}`, `drop '${k}'`));
|
|
390
|
+
if (d.mode !== undefined && !ROUTINE_MODES.includes(d.mode))
|
|
391
|
+
out.push(err('routine fields', `${at}: mode '${String(d.mode)}' is not one of ${ROUTINE_MODES.join(', ')}`, "use 'off', 'review' or 'full'"));
|
|
392
|
+
if (d.allowStacking !== undefined && typeof d.allowStacking !== 'boolean')
|
|
393
|
+
out.push(err('routine fields', `${at}: allowStacking is true or false`, 'leave it out for the default (off)'));
|
|
394
|
+
if (d.minAge !== undefined && (typeof d.minAge !== 'number' || !Number.isInteger(d.minAge) || d.minAge < 0))
|
|
395
|
+
out.push(err('summary depth', `${at}: minAge is a whole number of lines, 0 or more`, 'leave it out for the default — 20 on a routine, 0 on an action or an event'));
|
|
396
|
+
if (d.caps !== undefined) {
|
|
397
|
+
if (routine && cadenceClock(d.trigger.cadence) !== 'system')
|
|
398
|
+
out.push(warn('registration caps', `${at}: caps apply to routines on the system clock, and this one is not`, 'drop caps'));
|
|
399
|
+
for (const k of ['idleTimeoutMinutes', 'maxRunsInRow']) {
|
|
400
|
+
const v = d.caps?.[k];
|
|
401
|
+
if (v === undefined)
|
|
402
|
+
continue;
|
|
403
|
+
if (typeof v !== 'number' || !Number.isInteger(v) || v < 1)
|
|
404
|
+
out.push(err('registration caps', `${at}: caps.${k} is a whole number, 1 or more`, `set caps.${k} to a whole number`));
|
|
405
|
+
else if (ctx.caps?.[k] !== undefined && v > ctx.caps[k])
|
|
406
|
+
out.push(err('registration caps', `${at}: caps.${k} is ${v}, above the system's ${ctx.caps[k]}`, `set it to ${ctx.caps[k]} or less — each layer is at most the one above (R11)`));
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (d.order !== undefined &&
|
|
410
|
+
(typeof d.order !== 'number' ||
|
|
411
|
+
!Number.isInteger(d.order) ||
|
|
412
|
+
Math.abs(d.order) > MAX_ORDER))
|
|
413
|
+
out.push(err('registration order', `${at}: order is a whole number`, 'order registrations on one trigger with 0, 10, 20 …'));
|
|
414
|
+
if (d.when !== undefined)
|
|
415
|
+
for (const f of conditionFindings(d.when, `${at} when`))
|
|
416
|
+
out.push(err('registration when', f, 'build the condition with the typed builders — isTrue(…), eq(…), and(…)'));
|
|
417
|
+
if (d.label !== undefined)
|
|
418
|
+
for (const f of i18nFindings(d.label, `${at} label`))
|
|
419
|
+
out.push(err('registration label', f, "a string or a locale map with 'en'"));
|
|
420
|
+
// The pipeline: resolvable, fitting, values on its nodes, no plumbing exposed.
|
|
421
|
+
const elsewhere = ctx.external?.(d.pipeline) === true;
|
|
422
|
+
const p = elsewhere ? undefined : ctx.pipeline?.(d.pipeline);
|
|
423
|
+
if (ctx.pipeline && !p && !elsewhere)
|
|
424
|
+
out.push(err('registration pipeline', `${at} runs '${d.pipeline}', which is not installed`, 'register a pipeline this package ships or requires'));
|
|
425
|
+
if (d.values !== undefined) {
|
|
426
|
+
if (!d.values || typeof d.values !== 'object' || Array.isArray(d.values))
|
|
427
|
+
out.push(err('registration values', `${at}: values are { [step]: { [slot]: value } }`, 'set values per step and slot'));
|
|
428
|
+
else
|
|
429
|
+
for (const [nodeKey, slots] of Object.entries(d.values)) {
|
|
430
|
+
if (p && !p.nodes.some((n) => n.key === nodeKey))
|
|
431
|
+
out.push(err('registration values', `${at} sets values on '${nodeKey}', which '${d.pipeline}' has no step called`, `one of ${p.nodes.map((n) => `'${n.key}'`).join(', ')}`));
|
|
432
|
+
for (const slot of Object.keys(slots ?? {}))
|
|
433
|
+
if (slot === 'connection')
|
|
434
|
+
out.push(err('registration values', `${at} sets a connection on '${nodeKey}'`, "a registration never names a connection — what a step runs on is an administrator's loadout"));
|
|
435
|
+
else if (!VALUE_SLOTS.includes(slot))
|
|
436
|
+
out.push(err('registration values', `${at} sets '${slot}' on '${nodeKey}'`, `set one of ${VALUE_SLOTS.join(', ')}`));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (p) {
|
|
440
|
+
const fit = fitOf(p, d.trigger, { action });
|
|
441
|
+
if (!fit.ok)
|
|
442
|
+
out.push(err('registration fit', `${at}: ${fit.sentence}`, 'register it on a trigger that carries what it reads, or a pipeline that reads less (R4)'));
|
|
443
|
+
for (const n of p.nodes) {
|
|
444
|
+
const def = getDefinition(`${n.definitionId}@${n.definitionVersion}`);
|
|
445
|
+
for (const [slotName, slot] of Object.entries(def?.slots ?? {})) {
|
|
446
|
+
const schema = slot.schema;
|
|
447
|
+
if (!schema)
|
|
448
|
+
continue;
|
|
449
|
+
for (const f of exposureFindings(schema, `'${d.pipeline}' ${n.key}.${slotName}`))
|
|
450
|
+
out.push(err('exposed plumbing', `${at}: ${f}`, 'model plumbing is never exposed to a member (ruled 10-12)'));
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
out.push(...pipelineDefaultsFindings(p).map((m) => warn('pipeline defaults', `${at}: ${m}`, "move the preset's values into a registration's values, or into .defaults()")));
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
for (const [key, n] of counts)
|
|
457
|
+
if (n > 1)
|
|
458
|
+
out.push(warn('registration duplicate', `registration '${key}'${layer} is declared ${n} times — the same trigger and pipeline`, 'keep one: two alike are one registration (R34)'));
|
|
459
|
+
if (ctx.pipeline)
|
|
460
|
+
for (const cycle of emitCycles(decls, ctx.pipeline))
|
|
461
|
+
out.push(err('emits cycle', `registrations${layer} form a cycle through emitted events: ${cycle.join(' → ')}`, "break it — drop one registration, or a pipeline's emits (A12)"));
|
|
462
|
+
return out;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Author presets a pipeline ships that are not its declared defaults (R38,
|
|
466
|
+
* PLAN-layering D5): only the `default: true` one (`.defaults()`) is read;
|
|
467
|
+
* the rest are ignored, each named here.
|
|
468
|
+
* @experimental
|
|
469
|
+
* @ruling R38
|
|
470
|
+
*/
|
|
471
|
+
export function pipelineDefaultsFindings(p) {
|
|
472
|
+
return (p.presets ?? [])
|
|
473
|
+
.filter((x) => !x.default)
|
|
474
|
+
.map((x) => `'${p.id}' ships the author preset '${x.slug}', which is ignored — a pipeline's values come from its declared defaults and the registrations that change them (R38)`);
|
|
475
|
+
}
|
|
476
|
+
const CHANGE_KEYS = ['values', 'mode', 'when', 'allowStacking', 'caps', 'order', 'label', 'minAge'];
|
|
477
|
+
/** A copy of a registration that keeps what `register()` noted about it (a string reference, a range). */
|
|
478
|
+
function carried(from, to) {
|
|
479
|
+
const s = spelled.get(from);
|
|
480
|
+
if (s)
|
|
481
|
+
spelled.set(to, s);
|
|
482
|
+
const r = ranges.get(from);
|
|
483
|
+
if (r)
|
|
484
|
+
ranges.set(to, r);
|
|
485
|
+
return to;
|
|
486
|
+
}
|
|
487
|
+
/** Merge one layer's values over another's: node → slot, a slot's value replaced whole. */
|
|
488
|
+
function mergeValues(base, over) {
|
|
489
|
+
if (!over)
|
|
490
|
+
return base;
|
|
491
|
+
if (!base)
|
|
492
|
+
return over;
|
|
493
|
+
const out = {};
|
|
494
|
+
for (const [k, v] of Object.entries(base))
|
|
495
|
+
out[k] = { ...v };
|
|
496
|
+
for (const [k, v] of Object.entries(over))
|
|
497
|
+
out[k] = { ...(out[k] ?? {}), ...v };
|
|
498
|
+
return out;
|
|
499
|
+
}
|
|
500
|
+
function applyChange(r, change, by) {
|
|
501
|
+
const next = { ...r };
|
|
502
|
+
for (const k of CHANGE_KEYS) {
|
|
503
|
+
if (change[k] === undefined)
|
|
504
|
+
continue;
|
|
505
|
+
next[k] = k === 'values' ? mergeValues(r.values, change.values) : change[k];
|
|
506
|
+
}
|
|
507
|
+
next.changedBy = [...(r.changedBy ?? []), by];
|
|
508
|
+
return carried(r, next);
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* **Effective registrations** — the genre's defaults ⊕ a preset's changes ⊕
|
|
512
|
+
* a session's rows (PLAN-layering D4). Pure; the host's resolver reuses it.
|
|
513
|
+
* A dropped registration is gone; a routine switched Off stays, with
|
|
514
|
+
* `mode: 'off'` (R8). Sorted by `order`, then key (R24). Within one layer the
|
|
515
|
+
* first of two alike wins — the door warns about the second (R34).
|
|
516
|
+
* @experimental
|
|
517
|
+
* @ruling R2, R8, R22, R24, R34
|
|
518
|
+
*/
|
|
519
|
+
export function effectiveRegistrations(genreDefaults, presetChanges, sessionRows) {
|
|
520
|
+
const byKey = new Map();
|
|
521
|
+
for (const r of genreDefaults ?? [])
|
|
522
|
+
if (!byKey.has(r.key))
|
|
523
|
+
byKey.set(r.key, carried(r, { ...r, origin: 'genre' }));
|
|
524
|
+
if (presetChanges) {
|
|
525
|
+
for (const key of presetChanges.drop ?? [])
|
|
526
|
+
byKey.delete(key);
|
|
527
|
+
for (const c of presetChanges.change ?? []) {
|
|
528
|
+
const r = byKey.get(c.key);
|
|
529
|
+
if (r)
|
|
530
|
+
byKey.set(c.key, applyChange(r, c, 'preset'));
|
|
531
|
+
}
|
|
532
|
+
const added = new Set();
|
|
533
|
+
for (const r of presetChanges.register ?? []) {
|
|
534
|
+
if (added.has(r.key))
|
|
535
|
+
continue;
|
|
536
|
+
added.add(r.key);
|
|
537
|
+
byKey.set(r.key, carried(r, { ...r, origin: 'preset' }));
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
for (const row of sessionRows ?? []) {
|
|
541
|
+
if (row.dropped) {
|
|
542
|
+
byKey.delete(row.key);
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (row.registration) {
|
|
546
|
+
byKey.set(row.registration.key, carried(row.registration, { ...row.registration, origin: row.origin ?? 'session' }));
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
const r = byKey.get(row.key);
|
|
550
|
+
if (r)
|
|
551
|
+
byKey.set(row.key, applyChange(r, row, 'session'));
|
|
552
|
+
}
|
|
553
|
+
return [...byKey.values()].sort((a, b) => (a.order ?? 0) - (b.order ?? 0) || (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
|
|
554
|
+
}
|
|
555
|
+
/** Do two triggers name the same thing (event, action, or cadence id)? @experimental */
|
|
556
|
+
export function sameTrigger(a, b) {
|
|
557
|
+
if (a.family !== b.family)
|
|
558
|
+
return false;
|
|
559
|
+
if (a.family === 'event')
|
|
560
|
+
return a.event === b.event;
|
|
561
|
+
if (a.family === 'action')
|
|
562
|
+
return a.action === b.action;
|
|
563
|
+
if (a.family === 'routine')
|
|
564
|
+
return a.cadence.id === b.cadence.id;
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* ⏳ A preset's changes, read from whatever it states (PLAN-layering D4):
|
|
569
|
+
* `registrations` when it states them; otherwise derived from its
|
|
570
|
+
* pre-registration form — the aliases kept to 0.7:
|
|
571
|
+
*
|
|
572
|
+
* · `bindings` → for each event, the genre's defaults on that event are
|
|
573
|
+
* dropped and the bound spec registered (a binding replaced the event's
|
|
574
|
+
* one pipeline); a binding the defaults already hold changes nothing.
|
|
575
|
+
* · `actions.include` → the listed actions registered, and the defaults'
|
|
576
|
+
* action registrations not listed dropped (the include list was the whole
|
|
577
|
+
* set). Absent: the defaults' actions stand.
|
|
578
|
+
*
|
|
579
|
+
* `aliasConfigs` names, per derived registration key, the named
|
|
580
|
+
* configuration a binding carried — configurations retire (R38); the host's
|
|
581
|
+
* data upgrade turns each into that registration's values.
|
|
582
|
+
* @experimental
|
|
583
|
+
*/
|
|
584
|
+
export function presetChangesOf(preset, genreDefaults) {
|
|
585
|
+
if (preset.registrations)
|
|
586
|
+
return preset.registrations;
|
|
587
|
+
const defaults = genreDefaults ?? [];
|
|
588
|
+
const register = [];
|
|
589
|
+
const drop = new Set();
|
|
590
|
+
const aliasConfigs = {};
|
|
591
|
+
for (const [event, binding] of Object.entries(preset.bindings ?? {})) {
|
|
592
|
+
if (!binding?.spec)
|
|
593
|
+
continue;
|
|
594
|
+
const trigger = { family: 'event', event };
|
|
595
|
+
const key = registrationKeyOf(trigger, binding.spec);
|
|
596
|
+
if (binding.config)
|
|
597
|
+
aliasConfigs[key] = binding.config;
|
|
598
|
+
if (defaults.some((d) => d.key === key))
|
|
599
|
+
continue;
|
|
600
|
+
for (const d of defaults)
|
|
601
|
+
if (d.trigger.family === 'event' && d.trigger.event === event)
|
|
602
|
+
drop.add(d.key);
|
|
603
|
+
register.push(Object.freeze({ key, trigger, pipeline: binding.spec }));
|
|
604
|
+
}
|
|
605
|
+
if (preset.actions) {
|
|
606
|
+
const included = new Set(preset.actions.include);
|
|
607
|
+
for (const d of defaults)
|
|
608
|
+
if (d.trigger.family === 'action' && !included.has(d.trigger.action))
|
|
609
|
+
drop.add(d.key);
|
|
610
|
+
for (const identity of preset.actions.include) {
|
|
611
|
+
const hash = identity.lastIndexOf('#');
|
|
612
|
+
const trigger = { family: 'action', action: identity };
|
|
613
|
+
const key = registrationKeyOf(trigger, identity.slice(0, hash));
|
|
614
|
+
if (defaults.some((d) => d.key === key))
|
|
615
|
+
continue;
|
|
616
|
+
register.push(Object.freeze({ key, trigger, pipeline: identity.slice(0, hash) }));
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return {
|
|
620
|
+
...(register.length ? { register } : {}),
|
|
621
|
+
...(drop.size ? { drop: [...drop] } : {}),
|
|
622
|
+
...(Object.keys(aliasConfigs).length ? { aliasConfigs } : {}),
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
//# sourceMappingURL=registrations.js.map
|