@sdeck/plugin 0.0.0-stage → 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.
package/dist/index.js ADDED
@@ -0,0 +1,774 @@
1
+ // src/index.ts
2
+ export * from "@sdeck/app";
3
+
4
+ // src/bridge.ts
5
+ import { compile, sameValue, template } from "@sdeck/engine";
6
+ import {
7
+ createContext,
8
+ useCallback,
9
+ useContext,
10
+ useEffect,
11
+ useMemo,
12
+ useRef,
13
+ useState,
14
+ useSyncExternalStore
15
+ } from "react";
16
+ var HostBridgeContext = createContext(undefined);
17
+ function useBridge() {
18
+ const bridge = useContext(HostBridgeContext);
19
+ if (!bridge)
20
+ throw new Error("sdeck live-state hooks must be used inside an action drawn by the host");
21
+ return bridge;
22
+ }
23
+ function useClock(on) {
24
+ const [tick, setTick] = useState(0);
25
+ useEffect(() => {
26
+ if (!on)
27
+ return;
28
+ const timer = setInterval(() => setTick((t) => t + 1), 1000);
29
+ return () => clearInterval(timer);
30
+ }, [on]);
31
+ return tick;
32
+ }
33
+ function useWatched(paths, read) {
34
+ const bridge = useBridge();
35
+ const key = paths.join(`
36
+ `);
37
+ const subscribe = useCallback((listener) => bridge.watch(paths, listener), [bridge, key]);
38
+ const last = useRef(undefined);
39
+ const snapshot = () => {
40
+ const value = read();
41
+ if (last.current && sameValue(last.current.value, value))
42
+ return last.current.value;
43
+ last.current = { value };
44
+ return value;
45
+ };
46
+ return useSyncExternalStore(subscribe, snapshot);
47
+ }
48
+ function useExpression(source) {
49
+ const bridge = useBridge();
50
+ const expression = useMemo(() => compile(source), [source]);
51
+ useClock(expression.timeDependent);
52
+ return useWatched(expression.paths, () => expression.evaluate(bridge.scope()));
53
+ }
54
+ function useTemplate(source) {
55
+ const bridge = useBridge();
56
+ const compiled = useMemo(() => template(source), [source]);
57
+ useClock(compiled.timeDependent);
58
+ return useWatched(compiled.paths, () => compiled.render(bridge.scope()));
59
+ }
60
+ function useHostState(key) {
61
+ const bridge = useBridge();
62
+ const paths = useMemo(() => [key], [key]);
63
+ return useWatched(paths, () => {
64
+ const dot = key.indexOf(".");
65
+ const ns = bridge.scope()[key.slice(0, dot)];
66
+ return dot > 0 ? ns?.[key.slice(dot + 1)] : undefined;
67
+ });
68
+ }
69
+ function useVariable(name) {
70
+ const bridge = useBridge();
71
+ const paths = useMemo(() => [`vars.${name}`], [name]);
72
+ const value = useWatched(paths, () => bridge.scope().vars[name]);
73
+ const set = useCallback((v) => bridge.setVariable(name, v), [bridge, name]);
74
+ return [value, set];
75
+ }
76
+ function useRunAction() {
77
+ return useBridge().run;
78
+ }
79
+ function useImage(src, color) {
80
+ const bridge = useBridge();
81
+ return useSyncExternalStore(bridge.watchImages, () => src ? bridge.image(src, color) : undefined);
82
+ }
83
+ // src/connection.ts
84
+ class PermanentError extends Error {
85
+ }
86
+
87
+ class ManagedConnection {
88
+ #options;
89
+ #waiters = new Set;
90
+ #holders = 0;
91
+ #link;
92
+ #attempt;
93
+ #retry;
94
+ #linger;
95
+ #delay;
96
+ #failed;
97
+ constructor(options) {
98
+ this.#options = options;
99
+ this.#delay = options.retry?.minMs ?? 1000;
100
+ }
101
+ get current() {
102
+ return this.#link?.value;
103
+ }
104
+ hold() {
105
+ this.#holders++;
106
+ clearTimeout(this.#linger);
107
+ this.#connect();
108
+ let released = false;
109
+ return () => {
110
+ if (released)
111
+ return;
112
+ released = true;
113
+ this.#holders--;
114
+ if (this.#holders > 0)
115
+ return;
116
+ this.#linger = setTimeout(() => this.#drop(), this.#options.lingerMs ?? 3000);
117
+ };
118
+ }
119
+ async with(use, timeoutMs = 5000) {
120
+ const release = this.hold();
121
+ try {
122
+ return await use(await this.#ready(timeoutMs));
123
+ } finally {
124
+ release();
125
+ }
126
+ }
127
+ restart() {
128
+ this.#failed = undefined;
129
+ this.#delay = this.#options.retry?.minMs ?? 1000;
130
+ this.#drop();
131
+ if (this.#holders > 0)
132
+ this.#connect();
133
+ }
134
+ close() {
135
+ this.#holders = 0;
136
+ clearTimeout(this.#linger);
137
+ this.#drop();
138
+ this.#reject(new Error("the connection was closed"));
139
+ }
140
+ #ready(timeoutMs) {
141
+ if (this.#link)
142
+ return Promise.resolve(this.#link.value);
143
+ if (this.#failed)
144
+ return Promise.reject(this.#failed);
145
+ if (this.#retry) {
146
+ clearTimeout(this.#retry);
147
+ this.#retry = undefined;
148
+ this.#connect();
149
+ }
150
+ return new Promise((resolve, reject) => {
151
+ let last;
152
+ const waiter = {
153
+ resolve: (value) => {
154
+ clearTimeout(timer);
155
+ resolve(value);
156
+ },
157
+ reject: (error) => {
158
+ last = error;
159
+ clearTimeout(timer);
160
+ this.#waiters.delete(waiter);
161
+ reject(last);
162
+ }
163
+ };
164
+ const timer = setTimeout(() => waiter.reject(new Error("could not connect in time")), timeoutMs);
165
+ this.#waiters.add(waiter);
166
+ });
167
+ }
168
+ #connect() {
169
+ if (this.#link || this.#attempt || this.#retry || this.#failed)
170
+ return;
171
+ const attempt = new AbortController;
172
+ this.#attempt = attempt;
173
+ this.#options.onStatus?.("connecting");
174
+ let lostEarly;
175
+ let link;
176
+ const lost = (error) => {
177
+ if (!link) {
178
+ lostEarly = error;
179
+ return;
180
+ }
181
+ if (this.#link !== link)
182
+ return;
183
+ this.#link = undefined;
184
+ link.close();
185
+ this.#options.onStatus?.("disconnected", error);
186
+ this.#schedule(error);
187
+ };
188
+ const open = async () => {
189
+ const opened = await this.#options.open(lost, attempt.signal);
190
+ if (attempt.signal.aborted) {
191
+ opened.close();
192
+ return;
193
+ }
194
+ if (lostEarly) {
195
+ opened.close();
196
+ throw lostEarly;
197
+ }
198
+ link = opened;
199
+ this.#attempt = undefined;
200
+ this.#link = opened;
201
+ this.#delay = this.#options.retry?.minMs ?? 1000;
202
+ this.#options.onStatus?.("connected");
203
+ for (const waiter of this.#waiters)
204
+ waiter.resolve(opened.value);
205
+ this.#waiters.clear();
206
+ };
207
+ open().catch((error) => {
208
+ if (attempt.signal.aborted || this.#attempt !== attempt)
209
+ return;
210
+ this.#attempt = undefined;
211
+ this.#options.onStatus?.("disconnected", error);
212
+ this.#reject(error);
213
+ this.#schedule(error);
214
+ });
215
+ }
216
+ #schedule(error) {
217
+ if (error instanceof PermanentError) {
218
+ this.#failed = error;
219
+ this.#reject(error);
220
+ return;
221
+ }
222
+ if (this.#holders <= 0)
223
+ return;
224
+ clearTimeout(this.#retry);
225
+ this.#retry = setTimeout(() => {
226
+ this.#retry = undefined;
227
+ if (this.#holders > 0)
228
+ this.#connect();
229
+ }, this.#delay);
230
+ this.#delay = Math.min(this.#delay * 2, this.#options.retry?.maxMs ?? 30000);
231
+ }
232
+ #reject(error) {
233
+ for (const waiter of [...this.#waiters])
234
+ waiter.reject(error);
235
+ this.#waiters.clear();
236
+ }
237
+ #drop() {
238
+ clearTimeout(this.#retry);
239
+ this.#retry = undefined;
240
+ this.#attempt?.abort();
241
+ this.#attempt = undefined;
242
+ const link = this.#link;
243
+ this.#link = undefined;
244
+ if (!link)
245
+ return;
246
+ link.close();
247
+ this.#options.onStatus?.("disconnected");
248
+ }
249
+ }
250
+ // src/permissions.ts
251
+ var PERMISSION_TEXTS = {
252
+ network: {
253
+ name: "Network",
254
+ description: "Connect to the internet and to devices on your network.",
255
+ action: "use the network"
256
+ },
257
+ filesystem: {
258
+ name: "Files",
259
+ description: "Read and change your files. Its own data folder is always allowed.",
260
+ action: "use files outside its own folder"
261
+ },
262
+ process: {
263
+ name: "Start programs",
264
+ description: "Open apps and run programs that are installed on this computer.",
265
+ action: "start programs",
266
+ powerful: true
267
+ },
268
+ shell: {
269
+ name: "Run commands",
270
+ description: "Run any command line. Commands can do everything you can.",
271
+ action: "run commands",
272
+ powerful: true
273
+ },
274
+ input: {
275
+ name: "Keyboard and mouse",
276
+ description: "Press keys, type text and control media playback.",
277
+ action: "press keys"
278
+ },
279
+ clipboard: {
280
+ name: "Clipboard",
281
+ description: "Read and change what you copied.",
282
+ action: "use the clipboard"
283
+ },
284
+ audio: {
285
+ name: "Sound",
286
+ description: "Change volumes, mute apps and switch audio devices.",
287
+ action: "control sound"
288
+ },
289
+ environment: {
290
+ name: "Environment variables",
291
+ description: "Read environment variables, which can contain keys and tokens.",
292
+ action: "read environment variables"
293
+ },
294
+ variables: {
295
+ name: "Your variables",
296
+ description: "Read and change the variables your workflows use.",
297
+ action: "use your variables"
298
+ },
299
+ state: {
300
+ name: "Other plugins' values",
301
+ description: "Read live values from other plugins, such as the app in front.",
302
+ action: "read other plugins' values"
303
+ },
304
+ actions: {
305
+ name: "Other plugins' actions",
306
+ description: "Run actions of other plugins, with the permissions those have.",
307
+ action: "run other plugins' actions",
308
+ powerful: true
309
+ },
310
+ native: {
311
+ name: "Native code",
312
+ description: "Load compiled code, which sdeck cannot limit in any way.",
313
+ action: "load native code",
314
+ powerful: true
315
+ }
316
+ };
317
+ var PERMISSIONS = Object.keys(PERMISSION_TEXTS);
318
+
319
+ class PermissionError extends Error {
320
+ permission;
321
+ name = "PermissionError";
322
+ code = "EACCES";
323
+ constructor(plugin, permission, detail) {
324
+ const { name, action } = PERMISSION_TEXTS[permission];
325
+ super(`${plugin} is not allowed to ${action}${detail ? ` (${detail})` : ""}. ` + `Allow "${name}" in the plugin's permissions.`);
326
+ this.permission = permission;
327
+ }
328
+ }
329
+
330
+ // src/settings.ts
331
+ var HEX = /^#[0-9a-f]{6}$/i;
332
+ function defaultValue(field) {
333
+ switch (field.type) {
334
+ case "number":
335
+ return field.default ?? field.min ?? 0;
336
+ case "boolean":
337
+ return field.default ?? false;
338
+ case "color":
339
+ return field.default ?? "#000000";
340
+ case "groups":
341
+ return field.default ?? [];
342
+ case "select":
343
+ return field.default ?? field.options[0]?.value ?? "";
344
+ default:
345
+ return field.default ?? "";
346
+ }
347
+ }
348
+ function optionGroups(value) {
349
+ if (!Array.isArray(value))
350
+ return;
351
+ const ids = new Set;
352
+ return value.flatMap((group) => {
353
+ if (typeof group?.id !== "string" || !group.id || ids.has(group.id))
354
+ return [];
355
+ ids.add(group.id);
356
+ const members = Array.isArray(group.members) ? group.members : [];
357
+ return [
358
+ {
359
+ id: group.id,
360
+ name: typeof group.name === "string" ? group.name : "",
361
+ members: [...new Set(members.filter((m) => typeof m === "string"))]
362
+ }
363
+ ];
364
+ });
365
+ }
366
+ function coerce(field, value) {
367
+ switch (field.type) {
368
+ case "number": {
369
+ const n = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
370
+ if (typeof n !== "number" || !Number.isFinite(n))
371
+ return defaultValue(field);
372
+ return Math.min(field.max ?? Infinity, Math.max(field.min ?? -Infinity, n));
373
+ }
374
+ case "boolean":
375
+ return typeof value === "boolean" ? value : defaultValue(field);
376
+ case "color":
377
+ return typeof value === "string" && HEX.test(value) ? value.toLowerCase() : defaultValue(field);
378
+ case "groups":
379
+ return optionGroups(value) ?? defaultValue(field);
380
+ case "select":
381
+ if (typeof value !== "string")
382
+ return defaultValue(field);
383
+ if (field.source !== undefined)
384
+ return value;
385
+ return field.options.some((o) => o.value === value) ? value : defaultValue(field);
386
+ default:
387
+ return typeof value === "string" ? value : defaultValue(field);
388
+ }
389
+ }
390
+ function resolveSettings(schema, stored) {
391
+ const out = {};
392
+ for (const [key, field] of Object.entries(schema))
393
+ out[key] = coerce(field, stored?.[key]);
394
+ return out;
395
+ }
396
+ function behaviorFields(schema) {
397
+ return Object.fromEntries(Object.entries(schema).filter(([, field]) => !field.appearance));
398
+ }
399
+ function isFieldVisible(field, settings) {
400
+ if (!field.visibleIf)
401
+ return true;
402
+ return Object.entries(field.visibleIf).every(([key, want]) => {
403
+ const value = settings[key];
404
+ return Array.isArray(want) ? want.includes(value) : value === want;
405
+ });
406
+ }
407
+ function checkSchema(schema, sources = {}) {
408
+ const problems = [];
409
+ const types = new Set([
410
+ "text",
411
+ "number",
412
+ "boolean",
413
+ "color",
414
+ "select",
415
+ "page",
416
+ "image",
417
+ "groups",
418
+ "secret"
419
+ ]);
420
+ for (const [key, field] of Object.entries(schema)) {
421
+ if (!field || typeof field !== "object" || !types.has(field.type)) {
422
+ problems.push(`setting "${key}" has an unknown type`);
423
+ continue;
424
+ }
425
+ if (typeof field.label !== "string")
426
+ problems.push(`setting "${key}" needs a label`);
427
+ const source = "source" in field ? field.source : undefined;
428
+ if (field.type === "groups" && source === undefined) {
429
+ problems.push(`setting "${key}" needs a source`);
430
+ }
431
+ if (source !== undefined && !(source in sources)) {
432
+ problems.push(`setting "${key}" uses unknown option source "${source}"`);
433
+ }
434
+ if (field.type === "select" && (!Array.isArray(field.options) || field.options.length === 0 && source === undefined)) {
435
+ problems.push(`setting "${key}" needs at least one option or a source`);
436
+ }
437
+ for (const other of "dependsOn" in field && field.dependsOn || []) {
438
+ if (!(other in schema))
439
+ problems.push(`setting "${key}" depends on unknown "${other}"`);
440
+ }
441
+ if (field.visibleIf !== undefined) {
442
+ for (const other of Object.keys(field.visibleIf)) {
443
+ if (!(other in schema))
444
+ problems.push(`setting "${key}" depends on unknown "${other}"`);
445
+ }
446
+ }
447
+ }
448
+ return problems;
449
+ }
450
+
451
+ // src/plugin.ts
452
+ var PLUGIN_API_VERSION = 2;
453
+ var ID = /^(@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/;
454
+ var NAMESPACE = /^[a-z][\w-]{0,31}$/;
455
+ var LOCAL_ID = /^[\w.-]+$/;
456
+ function pluginNamespace(def) {
457
+ if (def.namespace)
458
+ return def.namespace;
459
+ const base = def.id.replace(/^@[^/]+\//, "").replace(/^sdeck-plugin-/, "").replace(/^plugin-/, "").toLowerCase().replace(/[^\w-]/g, "-");
460
+ return /^[a-z]/.test(base) ? base.slice(0, 32) : `p-${base}`.slice(0, 32);
461
+ }
462
+ function defineAction(def) {
463
+ return {
464
+ ...def,
465
+ slots: def.slots ?? ["key"],
466
+ settings: def.settings ?? {}
467
+ };
468
+ }
469
+ function defineTrigger(def) {
470
+ return { ...def, settings: def.settings ?? {} };
471
+ }
472
+ function definePlugin(def) {
473
+ const problems = checkPlugin(def);
474
+ if (problems.length > 0) {
475
+ throw new TypeError(`invalid plugin "${def?.id}": ${problems.join("; ")}`);
476
+ }
477
+ return def;
478
+ }
479
+ function checkNeeds(part, needs, asked) {
480
+ if (needs === undefined)
481
+ return [];
482
+ if (!Array.isArray(needs))
483
+ return [`${part}: permissions must be an array`];
484
+ const missing = needs.filter((p) => !Array.isArray(asked) || !asked.includes(p));
485
+ return missing.map((p) => `${part}: permission "${String(p)}" is not in the plugin's permissions`);
486
+ }
487
+ function checkLocalIds(kind, items, problems) {
488
+ const seen = new Set;
489
+ for (const item of items) {
490
+ const name = typeof item?.id === "string" ? item.id : "?";
491
+ if (typeof item?.id !== "string" || !LOCAL_ID.test(item.id)) {
492
+ problems.push(`${kind} "${name}": id must be letters, digits, ".", "-" or "_"`);
493
+ } else if (seen.has(item.id)) {
494
+ problems.push(`${kind} "${name}" is defined twice`);
495
+ }
496
+ seen.add(name);
497
+ }
498
+ return seen;
499
+ }
500
+ function checkPlugin(value) {
501
+ if (!value || typeof value !== "object")
502
+ return ["not an object"];
503
+ const def = value;
504
+ const problems = [];
505
+ if (typeof def.id !== "string" || !ID.test(def.id)) {
506
+ problems.push("id must be a package-style name (lowercase, e.g. `my-plugin` or `@me/plugin`)");
507
+ }
508
+ if (typeof def.name !== "string" || def.name === "")
509
+ problems.push("name is required");
510
+ if (typeof def.version !== "string" || def.version === "")
511
+ problems.push("version is required");
512
+ if (def.namespace !== undefined && !NAMESPACE.test(def.namespace)) {
513
+ problems.push("namespace must be lowercase letters, digits, - or _ (starting with a letter)");
514
+ }
515
+ if (def.activate !== undefined && typeof def.activate !== "function") {
516
+ problems.push("activate must be a function");
517
+ }
518
+ if (def.permissions !== undefined) {
519
+ if (!Array.isArray(def.permissions))
520
+ problems.push("permissions must be an array");
521
+ else
522
+ for (const p of def.permissions)
523
+ if (!PERMISSIONS.includes(p))
524
+ problems.push(`unknown permission "${String(p)}"`);
525
+ }
526
+ if (def.settings !== undefined) {
527
+ for (const p of checkSchema(def.settings, def.optionSources ?? {})) {
528
+ problems.push(`plugin ${p}`);
529
+ }
530
+ }
531
+ if (def.commands !== undefined) {
532
+ if (!Array.isArray(def.commands))
533
+ problems.push("commands must be an array");
534
+ else {
535
+ checkLocalIds("command", def.commands, problems);
536
+ for (const c of def.commands) {
537
+ const name = typeof c?.id === "string" ? c.id : "?";
538
+ if (typeof c?.name !== "string")
539
+ problems.push(`command "${name}": name is required`);
540
+ if (typeof c?.run !== "function")
541
+ problems.push(`command "${name}": run is required`);
542
+ problems.push(...checkNeeds(`command "${name}"`, c?.permissions, def.permissions));
543
+ }
544
+ }
545
+ }
546
+ if (def.onboarding !== undefined)
547
+ problems.push(...checkOnboarding(def));
548
+ const sources = def.optionSources ?? {};
549
+ if (typeof sources !== "object")
550
+ problems.push("optionSources must be an object");
551
+ else
552
+ for (const [name, fn] of Object.entries(sources))
553
+ if (typeof fn !== "function")
554
+ problems.push(`option source "${name}" must be a function`);
555
+ if (!Array.isArray(def.actions))
556
+ return [...problems, "actions must be an array"];
557
+ checkLocalIds("action", def.actions, problems);
558
+ for (const action of def.actions) {
559
+ const name = typeof action?.id === "string" ? action.id : "?";
560
+ if (typeof action?.name !== "string")
561
+ problems.push(`action "${name}": name is required`);
562
+ const hasComponent = typeof action?.component === "function" || typeof action?.component === "object";
563
+ const hasRun = typeof action?.run === "function";
564
+ if (!hasComponent && !hasRun && !action?.display) {
565
+ problems.push(`action "${name}": needs run or component`);
566
+ }
567
+ problems.push(...checkNeeds(`action "${name}"`, action?.permissions, def.permissions));
568
+ if (action?.look !== undefined && typeof action.look !== "function") {
569
+ problems.push(`action "${name}": look must be a function`);
570
+ }
571
+ const slots = action?.slots ?? ["key"];
572
+ if (!Array.isArray(slots) || slots.length === 0 || slots.some((s) => !["key", "sensor", "window"].includes(s))) {
573
+ problems.push(`action "${name}": slots must list "key", "sensor" and/or "window"`);
574
+ }
575
+ for (const p of checkSchema(action?.settings ?? {}, sources)) {
576
+ problems.push(`action "${name}": ${p}`);
577
+ }
578
+ for (const [key, field] of Object.entries(action?.settings ?? {})) {
579
+ if (field?.type === "secret") {
580
+ problems.push(`action "${name}": setting "${key}": secrets belong in the plugin settings`);
581
+ }
582
+ if (field?.type === "groups") {
583
+ problems.push(`action "${name}": setting "${key}": groups belong in the plugin settings`);
584
+ }
585
+ }
586
+ }
587
+ if (def.state !== undefined) {
588
+ if (!Array.isArray(def.state))
589
+ problems.push("state must be an array");
590
+ else {
591
+ checkLocalIds("state", def.state, problems);
592
+ for (const s of def.state) {
593
+ const name = typeof s?.id === "string" ? s.id : "?";
594
+ if (typeof s?.name !== "string")
595
+ problems.push(`state "${name}": name is required`);
596
+ if (!["number", "string", "boolean", "json"].includes(s?.type)) {
597
+ problems.push(`state "${name}": type must be number, string, boolean or json`);
598
+ }
599
+ problems.push(...checkNeeds(`state "${name}"`, s?.permissions, def.permissions));
600
+ if (s?.poll && (typeof s.poll.read !== "function" || !(s.poll.everyMs > 0))) {
601
+ problems.push(`state "${name}": poll needs read() and everyMs > 0`);
602
+ }
603
+ if (s?.start !== undefined && typeof s.start !== "function") {
604
+ problems.push(`state "${name}": start must be a function`);
605
+ }
606
+ }
607
+ }
608
+ }
609
+ if (def.triggers !== undefined) {
610
+ if (!Array.isArray(def.triggers))
611
+ problems.push("triggers must be an array");
612
+ else {
613
+ checkLocalIds("trigger", def.triggers, problems);
614
+ for (const t of def.triggers) {
615
+ const name = typeof t?.id === "string" ? t.id : "?";
616
+ if (typeof t?.name !== "string")
617
+ problems.push(`trigger "${name}": name is required`);
618
+ if (typeof t?.subscribe !== "function") {
619
+ problems.push(`trigger "${name}": subscribe is required`);
620
+ }
621
+ problems.push(...checkNeeds(`trigger "${name}"`, t?.permissions, def.permissions));
622
+ for (const p of checkSchema(t?.settings ?? {}, sources)) {
623
+ problems.push(`trigger "${name}": ${p}`);
624
+ }
625
+ }
626
+ }
627
+ }
628
+ return problems;
629
+ }
630
+ function checkOnboarding(def) {
631
+ const onboarding = def.onboarding;
632
+ if (!Array.isArray(onboarding?.steps))
633
+ return ["onboarding: steps must be an array"];
634
+ const problems = [];
635
+ checkLocalIds("onboarding step", onboarding.steps, problems);
636
+ const settings = def.settings ?? {};
637
+ const commands = new Set((def.commands ?? []).map((c) => c.id));
638
+ for (const step of onboarding.steps) {
639
+ const name = typeof step?.id === "string" ? step.id : "?";
640
+ if (typeof step?.title !== "string")
641
+ problems.push(`onboarding step "${name}": title is required`);
642
+ for (const key of step?.settings ?? []) {
643
+ if (!(key in settings))
644
+ problems.push(`onboarding step "${name}": no setting "${key}"`);
645
+ }
646
+ if (step?.command !== undefined && !commands.has(step.command)) {
647
+ problems.push(`onboarding step "${name}": no command "${step.command}"`);
648
+ }
649
+ if (step?.done !== undefined && typeof step.done !== "function") {
650
+ problems.push(`onboarding step "${name}": done must be a function`);
651
+ }
652
+ }
653
+ const page = onboarding.starterPage;
654
+ if (page) {
655
+ const actions = new Set((def.actions ?? []).map((a) => a.id));
656
+ const used = [
657
+ ...Object.values(page.keys ?? {}),
658
+ ...Object.values(page.sensors ?? {}),
659
+ ...page.window ? [page.window] : []
660
+ ];
661
+ for (const { action } of used) {
662
+ if (!actions.has(action))
663
+ problems.push(`onboarding starter page: no action "${action}"`);
664
+ }
665
+ }
666
+ return problems;
667
+ }
668
+
669
+ // src/manifest.ts
670
+ var PLUGIN_KEYWORD = "sdeck-plugin";
671
+ var HOST_MODULES = [
672
+ "react",
673
+ "react/jsx-runtime",
674
+ "react/jsx-dev-runtime",
675
+ "@sdeck/plugin",
676
+ "@sdeck/app",
677
+ "@sdeck/core",
678
+ "@sdeck/engine",
679
+ "@sdeck/schema"
680
+ ];
681
+ var SUPPORTED_API_VERSIONS = [1, PLUGIN_API_VERSION];
682
+ function parseManifest(pkg) {
683
+ if (!pkg || typeof pkg !== "object")
684
+ return { ok: false, error: "package.json is not an object" };
685
+ const p = pkg;
686
+ const sdeck = p.sdeck;
687
+ if (typeof p.name !== "string")
688
+ return { ok: false, error: "package.json has no name" };
689
+ if (!sdeck || typeof sdeck !== "object") {
690
+ return { ok: false, error: `${p.name} is not an sdeck plugin (no "sdeck" field)` };
691
+ }
692
+ const apiVersion = sdeck.apiVersion;
693
+ if (typeof apiVersion !== "number" || !SUPPORTED_API_VERSIONS.includes(apiVersion)) {
694
+ return {
695
+ ok: false,
696
+ error: `${p.name} targets plugin API ${String(apiVersion)}, this host supports ${SUPPORTED_API_VERSIONS.join(" and ")}`
697
+ };
698
+ }
699
+ const entry = typeof sdeck.entry === "string" ? sdeck.entry : typeof p.main === "string" ? p.main : "index.js";
700
+ return {
701
+ ok: true,
702
+ manifest: {
703
+ name: p.name,
704
+ version: typeof p.version === "string" ? p.version : "0.0.0",
705
+ ...typeof p.description === "string" && { description: p.description },
706
+ entry,
707
+ apiVersion
708
+ }
709
+ };
710
+ }
711
+ // src/shared.ts
712
+ class Shared {
713
+ #listeners = new Set;
714
+ #start;
715
+ #stop;
716
+ #last;
717
+ constructor(start) {
718
+ this.#start = start;
719
+ }
720
+ get last() {
721
+ return this.#last?.value;
722
+ }
723
+ listen(listener) {
724
+ this.#listeners.add(listener);
725
+ if (!this.#stop) {
726
+ this.#stop = this.#start((value) => {
727
+ this.#last = { value };
728
+ for (const l of [...this.#listeners])
729
+ l(value);
730
+ });
731
+ }
732
+ return () => {
733
+ this.#listeners.delete(listener);
734
+ if (this.#listeners.size === 0) {
735
+ this.#stop?.();
736
+ this.#stop = undefined;
737
+ this.#last = undefined;
738
+ }
739
+ };
740
+ }
741
+ }
742
+ export {
743
+ HOST_MODULES,
744
+ HostBridgeContext,
745
+ ManagedConnection,
746
+ PERMISSIONS,
747
+ PERMISSION_TEXTS,
748
+ PLUGIN_API_VERSION,
749
+ PLUGIN_KEYWORD,
750
+ PermanentError,
751
+ PermissionError,
752
+ SUPPORTED_API_VERSIONS,
753
+ Shared,
754
+ behaviorFields,
755
+ checkPlugin,
756
+ checkSchema,
757
+ defaultValue,
758
+ defineAction,
759
+ definePlugin,
760
+ defineTrigger,
761
+ isFieldVisible,
762
+ optionGroups,
763
+ parseManifest,
764
+ pluginNamespace,
765
+ resolveSettings,
766
+ useExpression,
767
+ useHostState,
768
+ useImage,
769
+ useRunAction,
770
+ useTemplate,
771
+ useVariable
772
+ };
773
+
774
+ //# debugId=C5D6970438DF66E064756E2164756E21