@vspark/plugin-sdk 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +15 -2
  2. package/index.d.ts +176 -51
  3. package/package.json +7 -2
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @vspark/plugin-sdk
2
2
 
3
3
  TypeScript typings of the [vspark](https://github.com/fennsorenn/vspark) plugin
4
- API (`apiVersion` 1). **Types only:** vspark injects the SDK into each entry
4
+ API (`apiVersion` 2). **Types only:** vspark injects the SDK into each entry
5
5
  module's default-exported factory at runtime, so plugin code imports nothing
6
6
  but types from here:
7
7
 
@@ -17,4 +17,17 @@ export default plugin;
17
17
  ```
18
18
 
19
19
  Start a plugin from the `vspark_plugin_template` repository. The package
20
- major version follows `apiVersion`.
20
+ version is `0.<apiVersion>.x` (0.1.x: API 1, 0.2.x: API 2): a plugin built
21
+ against one API version needs the matching range, e.g. `^0.2.0`.
22
+
23
+ API 2 replaced Action kinds and response types with **trigger types**, **step
24
+ types** and **action presets** (`triggerTypes`, `stepTypes`, `actionPresets`
25
+ in the shared module's output), plus `legacyConverters` that turn actions
26
+ stored with a plugin's API 1 kinds into the new form.
27
+
28
+ A trigger type declares the **fields** it provides (`fields: [{ name, type }]`,
29
+ `type` one of `text`, `number`, `record`). They are the placeholders users
30
+ write step inputs with. A step type declares **inputs** (`text` or `number`).
31
+ The user fills each one in as a template (`${message}`, `Song: ${payload.song}`,
32
+ plain text) or, for a number, as an expression (`${message} - 1`). The step's
33
+ `build` gets each input already rendered in `ctx.inputs`.
package/index.d.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  /**
2
- * @vspark/plugin-sdk — typings of the vspark plugin API (apiVersion 1).
2
+ * @vspark/plugin-sdk — typings of the vspark plugin API (apiVersion 2).
3
3
  *
4
4
  * vspark INJECTS the SDK into each entry module's default-exported factory;
5
5
  * nothing in this package runs. Plugin code must only `import type` from it
6
6
  * (bundling vspark's modules would register nodes in a copy the engine never
7
7
  * sees). Kept in step with the host by hand: packages/backend/src/plugins/
8
8
  * (loader.ts BACKEND_SDK, context.ts, accounts/api.ts) and
9
- * @vspark/shared/actions (PLUGIN_ACTIONS_SDK, FormField, registries).
9
+ * @vspark/shared/actions (PLUGIN_ACTIONS_SDK, FormField, actionModel.ts).
10
10
  * See vspark dev-notes/modules/plugins.md.
11
11
  */
12
12
 
13
- export type ApiVersion = 1;
13
+ export type ApiVersion = 2;
14
14
 
15
15
  // ─────────────────────────────────────────────────────────────────────────────
16
16
  // Signal graph
@@ -149,7 +149,8 @@ export interface FormField {
149
149
  * when there is no translation). `account`: provider id
150
150
  * (`'overlive'` or `'<pluginId>:<provider>'`). */
151
151
  provider?: string;
152
- /** Placeholders the field accepts (shown as chips under a template field). */
152
+ /** Placeholders a text field accepts (filled by your node): shown as
153
+ * buttons under it; any other `${…}` it uses is flagged in the editor. */
153
154
  placeholders?: readonly string[];
154
155
  when?: (read: (path: string) => unknown) => boolean;
155
156
  }
@@ -160,72 +161,193 @@ export interface FormField {
160
161
 
161
162
  export type ActionSettings = Record<string, unknown>;
162
163
 
163
- export interface ActionSettingField {
164
- path: string;
165
- class: 'value' | 'shape';
166
- }
167
-
164
+ /**
165
+ * Plugin API 2: every action is a trigger → steps. A plugin contributes
166
+ * trigger types (what starts an action), step types (what it does) and
167
+ * presets (ready-made trigger + steps). vspark assembles the graph: trigger →
168
+ * rank gate → cooldown → per-viewer cooldown → counter → steps; the options
169
+ * (rank, cooldowns, counter) are the host's, not the plugin's.
170
+ *
171
+ * Labels: `triggers.<type>.label` / `.description`, `steps.<type>.label` /
172
+ * `.description`, `presets.<id>.label` / `.description` in the plugin's
173
+ * namespace. Ids must start with `<pluginId>_`.
174
+ */
168
175
  export type PreviewToken =
169
176
  | { t: 'text'; text: string }
170
177
  | { t: 'msg'; key: string; params?: Record<string, string | number> };
171
178
 
172
- export interface ActionPreview {
173
- headline: PreviewToken[];
174
- lines: PreviewToken[][];
179
+ export type Out = [nodeId: string, port: string];
180
+
181
+ /** What a trigger field holds. */
182
+ export type TriggerFieldType = 'text' | 'number' | 'record';
183
+
184
+ /**
185
+ * A named field a trigger provides: a placeholder steps use as `${name}`
186
+ * (a record's keys as `${name.key}`). Every action also has `${count}` (its
187
+ * counter). Built-in triggers use `user` (display name), `username` (login
188
+ * name — also enables the per-viewer cooldown), `message`, `args`, `reward`,
189
+ * `combo`, `groups` (a pattern's named groups) and `payload` (a record).
190
+ */
191
+ export interface TriggerField {
192
+ name: string;
193
+ type: TriggerFieldType;
175
194
  }
176
195
 
177
- export interface ActionKindDef {
178
- /** Must start with `<pluginId>_`. */
179
- kind: string;
180
- label: string;
181
- icon: string;
182
- fields: readonly ActionSettingField[];
183
- /** The generated form (host `SchemaForm`). Omit when `ui.mjs` provides one. */
184
- form?: readonly FormField[];
185
- defaults(): ActionSettings;
186
- normalize(raw: unknown): ActionSettings;
187
- build(shape: ActionSettings): TemplateGraph;
188
- preview(settings: ActionSettings): ActionPreview;
196
+ export interface RankOuts {
197
+ isSub: Out;
198
+ isMod: Out;
199
+ isBroadcaster: Out;
189
200
  }
190
201
 
191
- /** A response a Hotkey / Chat keyword / Keyword combo / Redemption / Named
192
- * trigger action can run. */
193
- export interface ResponseTypeDef {
194
- /** Sink node kind. */
195
- nodeKind: string;
196
- /** Event input the trigger wires into. */
197
- entryPort: string;
198
- /** Value inputs of the sink, fed from `response.<field>`. */
199
- fields: readonly string[];
200
- /** How the editor renders each field. */
201
- form: readonly FormField[];
202
- /** i18n key of the response's label. */
202
+ /** What a step input produces: text (a template) or a number (an expression). */
203
+ export type StepInputType = 'text' | 'number';
204
+
205
+ /**
206
+ * An input of a step. The user writes it as a template over the trigger's
207
+ * placeholders (`${message}`, `Song: ${payload.song}`, plain text) — or, for
208
+ * a number, as an expression (`${message} - 1`). The editor flags
209
+ * placeholders the action's trigger does not provide.
210
+ */
211
+ export interface StepInput {
212
+ /** Key in `ctx.inputs` and in the step's `inputs.<name>` setting. */
213
+ name: string;
214
+ type: StepInputType;
215
+ /** Shows "needs input" when it is empty or uses a placeholder the trigger lacks. */
216
+ required?: boolean;
217
+ /** i18n key (plugin namespace). */
203
218
  label: string;
219
+ /** The template a new step starts with, e.g. `${message}`. */
220
+ default?: string;
204
221
  }
205
222
 
206
- export type Out = [nodeId: string, port: string];
207
-
208
- /** The template builder the built-in Action kinds use (`Tpl`). */
223
+ /**
224
+ * The builder a trigger or step part gets. Node ids are local to the part
225
+ * (vspark prefixes them); settings fields are relative to the part's own
226
+ * settings. Outputs handed in from elsewhere (trigger fields, the fire
227
+ * signal, inputs) are absolute and start with `@` — `wire` accepts both.
228
+ */
209
229
  export interface TemplateBuilder {
210
230
  node(id: string, kind: string, x: number, y: number, defaultConfig?: Record<string, unknown>): string;
211
231
  wire(fromNodeId: string, fromPort: string, toNodeId: string, toPort: string): void;
212
- /** Feed settings field `field` into `to.toPort` through a `settings` node. */
232
+ /** Feed a settings field of this part into `to.toPort` (a `settings` node). */
213
233
  setting(field: string, to: string, toPort: string, x: number, y: number): void;
214
- out(): TemplateGraph;
234
+ /** A settings field of this part as an output. */
235
+ value(field: string, x: number, y: number): Out;
236
+ }
237
+
238
+ interface PartDef {
239
+ type: string;
240
+ /** English fallback label. */
241
+ label: string;
242
+ icon: string;
243
+ /** Settings paths (relative, dotted) that change the graph structure; every
244
+ * other field is a value read live by the graph. */
245
+ shapeFields?: readonly string[];
246
+ defaults(): ActionSettings;
247
+ normalize(raw: unknown): ActionSettings;
248
+ /** Tokens for the action list row. `msg` keys are in the plugin namespace. */
249
+ preview(settings: ActionSettings): PreviewToken[];
250
+ /** The generated form (host `SchemaForm`). */
251
+ form?: readonly FormField[];
215
252
  }
216
253
 
254
+ export interface TriggerParts {
255
+ /** Fires when the trigger matched (several outputs = any of them). */
256
+ fire: Out | Out[];
257
+ /** The output of each declared field, by name. */
258
+ fields: Record<string, Out>;
259
+ /** The chatter's rank, when the trigger declares `rank: true`. */
260
+ rank?: RankOuts;
261
+ /** Work done once the action runs (after the option gates). */
262
+ afterRun?(b: TemplateBuilder, fired: Out[]): void;
263
+ }
264
+
265
+ export interface TriggerTypeDef extends PartDef {
266
+ /** The placeholders `build` returns outputs for in `fields`. */
267
+ fields: readonly TriggerField[];
268
+ /** `build` returns the chatter's `rank`: enables the minimum-rank option. */
269
+ rank?: boolean;
270
+ /** The keys a record field is known to hold with these settings (the
271
+ * editor checks `${record.key}` against them); without an entry any key
272
+ * may be present. */
273
+ recordKeys?(settings: ActionSettings): Record<string, readonly string[]>;
274
+ /** Cooldown (seconds) a new action with this trigger starts with. */
275
+ defaultCooldown?: number;
276
+ /** Gets the shape projection of the trigger's settings. */
277
+ build(b: TemplateBuilder, shape: ActionSettings): TriggerParts;
278
+ }
279
+
280
+ export interface StepContext {
281
+ /** Fire signals (absolute): wire each into the step's entry. */
282
+ fire: Out[];
283
+ /** Each declared input, rendered from its template (text) or evaluated
284
+ * (number: no value when the result is not a number). */
285
+ inputs: Record<string, Out>;
286
+ /** The trigger's field outputs, by name (the ones it provides). */
287
+ fields: Record<string, Out>;
288
+ /** The action counter's value after this run's increment. */
289
+ counter: Out;
290
+ /**
291
+ * A formatter over every placeholder, its template read live from the
292
+ * step's setting `field`: `text` renders a template, `number` evaluates an
293
+ * expression, `record` renders rows of `{ key, value }` templates.
294
+ */
295
+ format(field: string, as: 'text' | 'number' | 'record', x: number, y: number): Out;
296
+ }
297
+
298
+ export interface StepTypeDef extends PartDef {
299
+ inputs?: readonly StepInput[];
300
+ /** The trigger a new action started from this step gets. */
301
+ defaultTrigger?: string;
302
+ /** Gets the shape projection of the step's settings. */
303
+ build(b: TemplateBuilder, shape: ActionSettings, ctx: StepContext): void;
304
+ }
305
+
306
+ export type StepMode = 'all' | 'cycle';
307
+
308
+ export interface ActionPresetDef {
309
+ id: string;
310
+ /** i18n key (plugin namespace), e.g. `presets.<id>.label`. */
311
+ label: string;
312
+ icon: string;
313
+ /** `{ type, ...settings }` or null (the user picks a trigger). */
314
+ trigger: ActionSettings | null;
315
+ /** `[{ type, ...settings }]`. */
316
+ steps: ActionSettings[];
317
+ stepMode?: StepMode;
318
+ /** `{ minRank?, cooldown?: { seconds, group }, userCooldown?: { seconds }, counter? }`. */
319
+ options?: ActionSettings;
320
+ }
321
+
322
+ /** Turns a stored action of a former (API 1) kind into trigger → steps
323
+ * settings (`{ trigger, steps: [...], stepMode?, options? }`). Return `null`
324
+ * to delete the action. Runs once when vspark starts. */
325
+ export type LegacyConverter = (settings: ActionSettings) => ActionSettings | null;
326
+
217
327
  export interface CooldownSettings {
218
328
  seconds: number;
219
329
  group: string;
220
330
  }
221
331
 
332
+ export interface SinkStepDef {
333
+ nodeKind: string;
334
+ /** Event input fired by the action (default `fire`). */
335
+ entryPort?: string;
336
+ /** Value inputs of the sink, each fed from the step setting of the same name. */
337
+ fields?: readonly string[];
338
+ /** Template inputs, each fed into the sink's port of the same name. */
339
+ inputs?: readonly StepInput[];
340
+ form?: readonly FormField[];
341
+ icon?: string;
342
+ defaults?: () => ActionSettings;
343
+ preview?: (settings: ActionSettings) => PreviewToken[];
344
+ defaultTrigger?: string;
345
+ }
346
+
222
347
  export interface ActionsSdk {
223
- Tpl: new () => TemplateBuilder;
224
- /** `chat_rank` gate fed by `minRank`. Returns the gate's pass output. */
225
- addRankGate(b: TemplateBuilder, source: string, entry: Out, x: number): Out;
226
- /** `cooldown` fed by `cooldown.seconds` / `cooldown.group`. */
227
- addCooldown(b: TemplateBuilder, entries: Out[], x: number): Out;
228
- /** Builds `!name` aliases → anchored regex (the Chat command kind's matcher). */
348
+ /** A step that fires one node (the common case). */
349
+ sinkStep(type: string, def: SinkStepDef): StepTypeDef;
350
+ /** `!name` aliases → names (the Chat command trigger's matcher). */
229
351
  commandNames(command: unknown, aliases: unknown): string[];
230
352
  commandPattern(names: string[]): string;
231
353
  escapeRegExp(text: string): string;
@@ -306,8 +428,11 @@ export interface SharedSdk {
306
428
  }
307
429
 
308
430
  export interface SharedPlugin {
309
- responseTypes?: Record<string, ResponseTypeDef>;
310
- actionKinds?: ActionKindDef[];
431
+ triggerTypes?: TriggerTypeDef[];
432
+ stepTypes?: StepTypeDef[];
433
+ actionPresets?: ActionPresetDef[];
434
+ /** Former (API 1) action kind → converter. */
435
+ legacyConverters?: Record<string, LegacyConverter>;
311
436
  compositeTypes?: CompositeTypeDef[];
312
437
  compositePresets?: FeedCompositePreset[];
313
438
  }
@@ -465,12 +590,12 @@ export interface UiSdk extends SharedSdk {
465
590
  }
466
591
 
467
592
  export interface UiPlugin {
468
- /** Custom forms per action kind (instead of the generated `SchemaForm`). */
593
+ /** Custom forms per trigger / step type (instead of the generated `SchemaForm`). */
469
594
  actionForms?: Record<string, unknown>;
470
595
  }
471
596
 
472
597
  /** `ui.mjs` default export. Reserved: the editor does not load `ui.mjs` yet
473
- * (apiVersion 1 generates forms from `form`). */
598
+ * (forms are generated from `form`). */
474
599
  export type UiFactory = (sdk: UiSdk) => UiPlugin;
475
600
 
476
601
  // ─────────────────────────────────────────────────────────────────────────────
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "@vspark/plugin-sdk",
3
- "version": "0.1.0",
4
- "description": "Typings of the vspark plugin API (apiVersion 1). Types only — vspark injects the SDK at runtime.",
3
+ "version": "0.2.0",
4
+ "description": "Typings of the vspark plugin API (apiVersion 2). Types only — vspark injects the SDK at runtime.",
5
5
  "license": "UNLICENSED",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/fennsorenn/vspark.git",
9
+ "directory": "packages/plugin-sdk"
10
+ },
6
11
  "type": "module",
7
12
  "types": "index.d.ts",
8
13
  "files": ["index.d.ts", "README.md"],