@dreamboard-games/sdk 0.2.0 → 0.2.1-alpha.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.
Files changed (90) hide show
  1. package/dist/{ThemeProvider-fy0_QzgO.d.ts → ThemeProvider-BBMVT3KG.d.ts} +1 -1
  2. package/dist/attributes-BeRyboMS.d.ts +279 -0
  3. package/dist/browser-interaction.d.ts +708 -0
  4. package/dist/browser-interaction.js +106 -0
  5. package/dist/browser-interaction.js.map +1 -0
  6. package/dist/{bundle-TIZcw8LB.d.ts → bundle-CDd5FKeD.d.ts} +3 -1
  7. package/dist/{chunk-U5C6BONG.js → chunk-326PGVAA.js} +2 -2
  8. package/dist/{chunk-VFTAA4WO.js → chunk-MKXPVOUT.js} +4 -2
  9. package/dist/chunk-MKXPVOUT.js.map +1 -0
  10. package/dist/{chunk-GKKBPPSW.js → chunk-MZNVHMJ5.js} +4 -4
  11. package/dist/{chunk-KAELH4KC.js → chunk-NKCRKGR2.js} +2 -2
  12. package/dist/{chunk-WN74KVNY.js → chunk-PEI3FIL2.js} +2 -2
  13. package/dist/chunk-PEI3FIL2.js.map +1 -0
  14. package/dist/chunk-QLG6VEMW.js +1691 -0
  15. package/dist/chunk-QLG6VEMW.js.map +1 -0
  16. package/dist/{chunk-WYPQ3GG5.js → chunk-WG4JQL3S.js} +4 -1
  17. package/dist/{chunk-WYPQ3GG5.js.map → chunk-WG4JQL3S.js.map} +1 -1
  18. package/dist/{chunk-7YAHLYBR.js → chunk-XV6D3ET4.js} +8 -4
  19. package/dist/{chunk-7YAHLYBR.js.map → chunk-XV6D3ET4.js.map} +1 -1
  20. package/dist/{chunk-TDSWKVZ4.js → chunk-ZABVH7AO.js} +1236 -17
  21. package/dist/chunk-ZABVH7AO.js.map +1 -0
  22. package/dist/{components-D5ZRE2Hl.d.ts → components-BoiVSYqx.d.ts} +1 -1
  23. package/dist/generated/runtime/primitives.d.ts +5 -4
  24. package/dist/generated/runtime/primitives.js +4 -3
  25. package/dist/generated/runtime-api.d.ts +1 -1
  26. package/dist/generated/runtime.d.ts +5 -4
  27. package/dist/generated/runtime.js +7 -6
  28. package/dist/generated/workspace-contract.d.ts +5 -4
  29. package/dist/generated/workspace-contract.js +6 -5
  30. package/dist/{hex-board-view-D_07hO6O.d.ts → hex-board-view-1iAyJRFn.d.ts} +1 -0
  31. package/dist/index.js +1 -1
  32. package/dist/infrastructure/reducer-bundle-abi.d.ts +113 -113
  33. package/dist/infrastructure/reducer-bundle-abi.js +1 -1
  34. package/dist/package-set.d.ts +2 -2
  35. package/dist/package-set.js +1 -1
  36. package/dist/reducer.d.ts +1 -1
  37. package/dist/reducer.js +305 -12
  38. package/dist/reducer.js.map +1 -1
  39. package/dist/runtime/primitives.d.ts +6 -5
  40. package/dist/runtime/primitives.js +4 -3
  41. package/dist/runtime/workspace-contract.d.ts +6 -5
  42. package/dist/runtime/workspace-contract.js +6 -5
  43. package/dist/{runtime-api-DWxvTr-O.d.ts → runtime-api-CPLm_XDG.d.ts} +6 -0
  44. package/dist/runtime.d.ts +5 -4
  45. package/dist/runtime.js +6 -5
  46. package/dist/testing.d.ts +2 -2
  47. package/dist/ui/components.d.ts +2 -2
  48. package/dist/ui/components.js +1 -1
  49. package/dist/{ui-contract-iQfTtUSL.d.ts → ui-contract-rzKBwOLC.d.ts} +5 -3
  50. package/dist/ui.d.ts +5 -5
  51. package/dist/ui.js +2 -2
  52. package/package.json +15 -9
  53. package/src/browser-interaction/attributes.ts +211 -0
  54. package/src/browser-interaction/canonical.ts +77 -0
  55. package/src/browser-interaction/constants.ts +77 -0
  56. package/src/browser-interaction/effects.ts +176 -0
  57. package/src/browser-interaction/index.ts +111 -0
  58. package/src/browser-interaction/normalize.ts +997 -0
  59. package/src/browser-interaction/registry.ts +70 -0
  60. package/src/browser-interaction/resolve.ts +596 -0
  61. package/src/browser-interaction/schemas.ts +152 -0
  62. package/src/browser-interaction/types.ts +304 -0
  63. package/src/browser-interaction.ts +1 -0
  64. package/src/generated/reducer-contract/wire.ts +1 -1
  65. package/src/generated/reducer-contract/zod.ts +3 -1
  66. package/src/package-set.ts +1 -1
  67. package/src/reducer/bundle/ingress-bundle.ts +1 -1
  68. package/src/reducer/bundle/trusted/interaction-types.ts +3 -0
  69. package/src/reducer/bundle/trusted/projection-builder.ts +337 -13
  70. package/src/reducer/ingress/input-codec.ts +1 -1
  71. package/src/reducer/ingress/session-codec.ts +1 -1
  72. package/src/runtime-internal/components/InteractionForm.tsx +345 -7
  73. package/src/runtime-internal/components/PluginRuntime.tsx +2 -0
  74. package/src/runtime-internal/components/board/target-layer.ts +2 -0
  75. package/src/runtime-internal/context/PluginStateContext.tsx +41 -0
  76. package/src/runtime-internal/hooks/useBoardInteractions.ts +73 -11
  77. package/src/runtime-internal/primitives/board.tsx +71 -0
  78. package/src/runtime-internal/primitives/interaction.tsx +160 -1
  79. package/src/runtime-internal/types/plugin-state.ts +6 -0
  80. package/src/runtime-internal/utils/browser-interaction-effects.ts +240 -0
  81. package/src/runtime-internal/utils/interaction-draft-digest.ts +252 -0
  82. package/src/runtime-internal/utils/semantic-projection-digest.ts +407 -0
  83. package/src/ui/components/board/HexGrid.tsx +3 -0
  84. package/src/ui/components/board/target-layer.ts +1 -0
  85. package/dist/chunk-TDSWKVZ4.js.map +0 -1
  86. package/dist/chunk-VFTAA4WO.js.map +0 -1
  87. package/dist/chunk-WN74KVNY.js.map +0 -1
  88. /package/dist/{chunk-U5C6BONG.js.map → chunk-326PGVAA.js.map} +0 -0
  89. /package/dist/{chunk-GKKBPPSW.js.map → chunk-MZNVHMJ5.js.map} +0 -0
  90. /package/dist/{chunk-KAELH4KC.js.map → chunk-NKCRKGR2.js.map} +0 -0
@@ -0,0 +1,997 @@
1
+ import {
2
+ BROWSER_INTERACTION_ACTUATOR_KINDS,
3
+ BROWSER_INTERACTION_ATTRIBUTES,
4
+ BROWSER_INTERACTION_CANDIDATE_STATES,
5
+ BROWSER_INTERACTION_READINESS_VALUES,
6
+ DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_NAME,
7
+ DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_VERSION,
8
+ GAMEPLAY_BROWSER_INTERACTION_SURFACE,
9
+ } from "./constants.js";
10
+ import {
11
+ decodeCanonicalCandidateValue,
12
+ encodeCanonicalCandidateValue,
13
+ } from "./canonical.js";
14
+ import {
15
+ browserInteractionEffectPatternMatches,
16
+ decodeBrowserInteractionEffect,
17
+ decodeBrowserInteractionEffectPattern,
18
+ encodeBrowserInteractionEffect,
19
+ encodeBrowserInteractionEffectPattern,
20
+ } from "./effects.js";
21
+ import { createBrowserInteractionActuatorKey } from "./attributes.js";
22
+ import { defaultBrowserInteractionRegistry } from "./registry.js";
23
+ import type {
24
+ BrowserInteractionActuator,
25
+ BrowserInteractionActuatorKind,
26
+ BrowserInteractionCandidateState,
27
+ BrowserInteractionDiagnostic,
28
+ BrowserInteractionEffectPattern,
29
+ BrowserInteractionEntity,
30
+ BrowserInteractionRawRecord,
31
+ BrowserInteractionReadiness,
32
+ BrowserInteractionRegistry,
33
+ BrowserInteractionSemanticSurfaceSnapshot,
34
+ BrowserInteractionSnapshot,
35
+ BrowserInteractionSurfaceEffect,
36
+ BrowserInteractionSurface,
37
+ BrowserInteractionSurfaceSnapshot,
38
+ } from "./types.js";
39
+
40
+ interface PendingInteraction {
41
+ interactionKey: string;
42
+ interactionId: string;
43
+ descriptorDigest?: string;
44
+ draftDigest?: string;
45
+ readiness: BrowserInteractionReadiness;
46
+ rootSeen: boolean;
47
+ actuators: BrowserInteractionActuator[];
48
+ diagnostics: BrowserInteractionDiagnostic[];
49
+ }
50
+
51
+ interface PendingSemanticSurface {
52
+ kind: "semantic";
53
+ surface: BrowserInteractionSurface;
54
+ scopeId: string;
55
+ interactions: Map<string, PendingInteraction>;
56
+ diagnostics: BrowserInteractionDiagnostic[];
57
+ }
58
+
59
+ interface PendingUnknownSurface {
60
+ kind: "unknown";
61
+ surface: BrowserInteractionSurface;
62
+ scopeId: string;
63
+ diagnostics: BrowserInteractionDiagnostic[];
64
+ }
65
+
66
+ type PendingSurface = PendingSemanticSurface | PendingUnknownSurface;
67
+
68
+ export interface NormalizeBrowserInteractionRecordsOptions {
69
+ readonly registry?: BrowserInteractionRegistry;
70
+ }
71
+
72
+ export function normalizeBrowserInteractionRecords(
73
+ records: readonly BrowserInteractionRawRecord[],
74
+ options: NormalizeBrowserInteractionRecordsOptions = {},
75
+ ): BrowserInteractionSnapshot {
76
+ const registry = options.registry ?? defaultBrowserInteractionRegistry;
77
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
78
+ const surfaces = new Map<string, PendingSurface>();
79
+
80
+ const getSurface = (
81
+ surface: string,
82
+ scopeId: string,
83
+ ): PendingSurface | null => {
84
+ const registered = registry.surfaces.get(surface);
85
+ const key = `${surface}\0${scopeId}`;
86
+ const existing = surfaces.get(key);
87
+ if (existing) return existing;
88
+ if (!registered) {
89
+ const diagnostic = diagnosticFor({
90
+ code: "unknown-surface",
91
+ message: `Unknown browser interaction surface '${surface}'.`,
92
+ surface,
93
+ scopeId,
94
+ });
95
+ diagnostics.push(diagnostic);
96
+ const pending: PendingUnknownSurface = {
97
+ kind: "unknown",
98
+ surface,
99
+ scopeId,
100
+ diagnostics: [diagnostic],
101
+ };
102
+ surfaces.set(key, pending);
103
+ return pending;
104
+ }
105
+ const pending: PendingSemanticSurface = {
106
+ kind: "semantic",
107
+ surface,
108
+ scopeId,
109
+ interactions: new Map(),
110
+ diagnostics: [],
111
+ };
112
+ surfaces.set(key, pending);
113
+ return pending;
114
+ };
115
+
116
+ for (const record of records) {
117
+ const attributes = record.attributes;
118
+ const protocol = text(attributes, BROWSER_INTERACTION_ATTRIBUTES.protocol);
119
+ const surface = text(attributes, BROWSER_INTERACTION_ATTRIBUTES.surface);
120
+ const scopeId = text(attributes, BROWSER_INTERACTION_ATTRIBUTES.scope);
121
+ const role = text(attributes, BROWSER_INTERACTION_ATTRIBUTES.role);
122
+
123
+ if (protocol !== DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_VERSION) {
124
+ diagnostics.push(
125
+ diagnosticFor({
126
+ code: "invalid-protocol",
127
+ message: `Expected browser interaction protocol ${DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_VERSION}.`,
128
+ surface,
129
+ scopeId,
130
+ }),
131
+ );
132
+ continue;
133
+ }
134
+ if (
135
+ !surface ||
136
+ !scopeId ||
137
+ (role !== "interaction" && role !== "actuator")
138
+ ) {
139
+ diagnostics.push(
140
+ diagnosticFor({
141
+ code: "invalid-record",
142
+ message:
143
+ "Browser interaction records require protocol, surface, scope and role.",
144
+ surface,
145
+ scopeId,
146
+ }),
147
+ );
148
+ continue;
149
+ }
150
+
151
+ const pendingSurface = getSurface(surface, scopeId);
152
+ if (!pendingSurface || pendingSurface.kind !== "semantic") {
153
+ continue;
154
+ }
155
+ const interactionKey = text(
156
+ attributes,
157
+ BROWSER_INTERACTION_ATTRIBUTES.interactionKey,
158
+ );
159
+ const interactionId = text(
160
+ attributes,
161
+ BROWSER_INTERACTION_ATTRIBUTES.interactionId,
162
+ );
163
+ if (!interactionKey || !interactionId) {
164
+ pushSurfaceDiagnostic(
165
+ pendingSurface,
166
+ diagnostics,
167
+ diagnosticFor({
168
+ code: "invalid-record",
169
+ message:
170
+ "Browser interaction records require interaction key and id.",
171
+ surface,
172
+ scopeId,
173
+ }),
174
+ );
175
+ continue;
176
+ }
177
+ const interaction = getInteraction(
178
+ pendingSurface,
179
+ interactionKey,
180
+ interactionId,
181
+ );
182
+
183
+ if (role === "interaction") {
184
+ interaction.rootSeen = true;
185
+ interaction.descriptorDigest = optionalText(
186
+ attributes,
187
+ BROWSER_INTERACTION_ATTRIBUTES.descriptorDigest,
188
+ );
189
+ interaction.draftDigest = optionalText(
190
+ attributes,
191
+ BROWSER_INTERACTION_ATTRIBUTES.draftDigest,
192
+ );
193
+ interaction.readiness = parseReadiness(
194
+ text(attributes, BROWSER_INTERACTION_ATTRIBUTES.readiness),
195
+ );
196
+ continue;
197
+ }
198
+
199
+ const actuator = parseGameplayActuator(attributes, {
200
+ surface,
201
+ scopeId,
202
+ interactionKey,
203
+ interactionId,
204
+ });
205
+ if (actuator.diagnostics.length > 0) {
206
+ interaction.diagnostics.push(...actuator.diagnostics);
207
+ diagnostics.push(...actuator.diagnostics);
208
+ }
209
+ const registered = registry.surfaces.get(surface);
210
+ if (registered && !registered.intents.includes(actuator.intent)) {
211
+ const diagnostic = diagnosticFor({
212
+ code: "unknown-intent",
213
+ message: `Unknown browser interaction intent '${actuator.intent}' for surface '${surface}'.`,
214
+ surface,
215
+ scopeId,
216
+ interactionKey,
217
+ intent: actuator.intent,
218
+ actuatorId: actuator.actuatorId,
219
+ });
220
+ interaction.diagnostics.push(diagnostic);
221
+ diagnostics.push(diagnostic);
222
+ }
223
+ if (registered?.effectKinds) {
224
+ for (const effect of actuator.semanticEffects) {
225
+ if (!registered.effectKinds.includes(effect.kind)) {
226
+ const diagnostic = diagnosticFor({
227
+ code: "unknown-surface-effect",
228
+ message: `Unknown browser interaction effect '${effect.kind}' for surface '${surface}'.`,
229
+ surface,
230
+ scopeId,
231
+ interactionKey,
232
+ intent: actuator.intent,
233
+ actuatorId: actuator.actuatorId,
234
+ });
235
+ interaction.diagnostics.push(diagnostic);
236
+ diagnostics.push(diagnostic);
237
+ }
238
+ }
239
+ for (const pattern of [
240
+ ...actuator.acceptedEffectPatterns,
241
+ ...actuator.preparationPatterns,
242
+ ]) {
243
+ const effectKind =
244
+ pattern.kind === "exact" ? pattern.effect.kind : pattern.effectKind;
245
+ if (!registered.effectKinds.includes(effectKind)) {
246
+ const diagnostic = diagnosticFor({
247
+ code: "unknown-surface-effect",
248
+ message: `Unknown browser interaction effect '${effectKind}' for surface '${surface}'.`,
249
+ surface,
250
+ scopeId,
251
+ interactionKey,
252
+ intent: actuator.intent,
253
+ actuatorId: actuator.actuatorId,
254
+ });
255
+ interaction.diagnostics.push(diagnostic);
256
+ diagnostics.push(diagnostic);
257
+ }
258
+ }
259
+ }
260
+ interaction.actuators.push(actuator);
261
+ }
262
+
263
+ for (const surface of surfaces.values()) {
264
+ if (surface.kind !== "semantic") continue;
265
+ for (const interaction of surface.interactions.values()) {
266
+ if (interaction.rootSeen || interaction.actuators.length === 0) continue;
267
+ const diagnostic = diagnosticFor({
268
+ code: "orphan-actuator",
269
+ message:
270
+ "Browser interaction actuators require a rendered semantic root.",
271
+ surface: surface.surface,
272
+ scopeId: surface.scopeId,
273
+ interactionKey: interaction.interactionKey,
274
+ });
275
+ interaction.diagnostics.push(diagnostic);
276
+ diagnostics.push(diagnostic);
277
+ }
278
+ }
279
+
280
+ const snapshot: BrowserInteractionSnapshot = {
281
+ protocol: {
282
+ name: DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_NAME,
283
+ version: DREAMBOARD_BROWSER_INTERACTION_PROTOCOL_VERSION,
284
+ },
285
+ surfaces: [...surfaces.values()].map(finalizeSurface).sort(compareSurface),
286
+ diagnostics: [],
287
+ };
288
+ const validationDiagnostics = validateBrowserInteractionSnapshot(snapshot);
289
+ return {
290
+ ...snapshot,
291
+ diagnostics: [...diagnostics, ...validationDiagnostics].sort(
292
+ compareDiagnostic,
293
+ ),
294
+ };
295
+ }
296
+
297
+ export function validateBrowserInteractionSnapshot(
298
+ snapshot: BrowserInteractionSnapshot,
299
+ ): readonly BrowserInteractionDiagnostic[] {
300
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
301
+ for (const surface of snapshot.surfaces) {
302
+ if (!isSemanticSurfaceSnapshot(surface)) continue;
303
+ for (const interaction of surface.interactions) {
304
+ const enabledByKey = new Map<string, BrowserInteractionActuator[]>();
305
+ for (const actuator of interaction.actuators) {
306
+ if (!actuator.enabled) continue;
307
+ const key = actuatorIdentityKey({
308
+ surface: surface.surface,
309
+ scopeId: surface.scopeId,
310
+ interactionKey: interaction.interactionKey,
311
+ actuator,
312
+ });
313
+ const group = enabledByKey.get(key) ?? [];
314
+ group.push(actuator);
315
+ enabledByKey.set(key, group);
316
+ }
317
+ for (const [key, actuators] of enabledByKey) {
318
+ if (actuators.length > 1) {
319
+ diagnostics.push(
320
+ diagnosticFor({
321
+ code: "duplicate-enabled-actuator",
322
+ message: `Duplicate enabled actuator for '${key}'.`,
323
+ surface: surface.surface,
324
+ scopeId: surface.scopeId,
325
+ interactionKey: interaction.interactionKey,
326
+ intent: actuators[0]?.intent,
327
+ actuatorId: actuators[0]?.actuatorId,
328
+ }),
329
+ );
330
+ }
331
+ }
332
+ diagnostics.push(
333
+ ...diagnosticsForPreparationCycles(surface, interaction),
334
+ ...diagnosticsForPreparationPatternAmbiguity(surface, interaction),
335
+ ...diagnosticsForInvalidAcceptedPatterns(surface, interaction),
336
+ ...diagnosticsForGameplayEffectCompatibility(surface, interaction),
337
+ );
338
+ }
339
+ }
340
+ return diagnostics.sort(compareDiagnostic);
341
+ }
342
+
343
+ function diagnosticsForInvalidAcceptedPatterns(
344
+ surface: BrowserInteractionSemanticSurfaceSnapshot,
345
+ interaction: BrowserInteractionEntity,
346
+ ): BrowserInteractionDiagnostic[] {
347
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
348
+ for (const actuator of interaction.actuators) {
349
+ for (const pattern of actuator.acceptedEffectPatterns) {
350
+ if (
351
+ pattern.kind === "match" &&
352
+ Object.keys(pattern.fields ?? {}).length === 0 &&
353
+ pattern.scalar === undefined
354
+ ) {
355
+ diagnostics.push(
356
+ diagnosticFor({
357
+ code: "invalid-effect-pattern",
358
+ message:
359
+ "Accepted-effect match patterns must be bounded by fields or scalar constraints.",
360
+ surface: surface.surface,
361
+ scopeId: surface.scopeId,
362
+ interactionKey: interaction.interactionKey,
363
+ intent: actuator.intent,
364
+ actuatorId: actuator.actuatorId,
365
+ }),
366
+ );
367
+ }
368
+ }
369
+ }
370
+ return diagnostics;
371
+ }
372
+
373
+ function diagnosticsForGameplayEffectCompatibility(
374
+ surface: BrowserInteractionSemanticSurfaceSnapshot,
375
+ interaction: BrowserInteractionEntity,
376
+ ): BrowserInteractionDiagnostic[] {
377
+ if (surface.surface !== GAMEPLAY_BROWSER_INTERACTION_SURFACE) return [];
378
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
379
+ for (const actuator of interaction.actuators) {
380
+ for (const effect of actuator.semanticEffects) {
381
+ if (!gameplayIntentCanPerformEffect(actuator.intent, effect.kind)) {
382
+ diagnostics.push(
383
+ diagnosticFor({
384
+ code: "effect-intent-incompatibility",
385
+ message:
386
+ "Gameplay browser interaction intent is incompatible with its semantic effect.",
387
+ surface: surface.surface,
388
+ scopeId: surface.scopeId,
389
+ interactionKey: interaction.interactionKey,
390
+ intent: actuator.intent,
391
+ actuatorId: actuator.actuatorId,
392
+ }),
393
+ );
394
+ }
395
+ }
396
+ }
397
+ return diagnostics;
398
+ }
399
+
400
+ function gameplayIntentCanPerformEffect(intent: string, effectKind: string) {
401
+ switch (effectKind) {
402
+ case "setCandidate":
403
+ return intent === "select" || intent === "toggle";
404
+ case "adjustResource":
405
+ return intent === "increment" || intent === "decrement";
406
+ case "setScalar":
407
+ return (
408
+ intent === "fill" || intent === "increment" || intent === "decrement"
409
+ );
410
+ case "commit":
411
+ return intent === "submit" || intent === "invoke";
412
+ case "invoke":
413
+ return intent === "invoke" || intent === "select" || intent === "toggle";
414
+ default:
415
+ return true;
416
+ }
417
+ }
418
+
419
+ function parseGameplayActuator(
420
+ attributes: Readonly<Record<string, string | boolean | null | undefined>>,
421
+ context: {
422
+ surface: string;
423
+ scopeId: string;
424
+ interactionKey: string;
425
+ interactionId: string;
426
+ },
427
+ ): BrowserInteractionActuator {
428
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
429
+ const intent = text(attributes, BROWSER_INTERACTION_ATTRIBUTES.intent);
430
+ const actuatorKind = parseActuatorKind(
431
+ text(attributes, BROWSER_INTERACTION_ATTRIBUTES.actuatorKind),
432
+ );
433
+ const inputKey = optionalText(
434
+ attributes,
435
+ BROWSER_INTERACTION_ATTRIBUTES.inputKey,
436
+ );
437
+ const candidate = parseCandidate(
438
+ attributes,
439
+ BROWSER_INTERACTION_ATTRIBUTES.candidateValue,
440
+ context,
441
+ intent,
442
+ diagnostics,
443
+ );
444
+ const candidateState = parseCandidateState(
445
+ optionalText(attributes, BROWSER_INTERACTION_ATTRIBUTES.candidateState),
446
+ );
447
+ const explicitId = optionalText(
448
+ attributes,
449
+ BROWSER_INTERACTION_ATTRIBUTES.actuatorId,
450
+ );
451
+ const enabled = parseEnabled(
452
+ attributes[BROWSER_INTERACTION_ATTRIBUTES.enabled],
453
+ );
454
+ const actuatorId =
455
+ explicitId ??
456
+ createBrowserInteractionActuatorKey({
457
+ surface: context.surface,
458
+ scopeId: context.scopeId,
459
+ interactionKey: context.interactionKey,
460
+ intent,
461
+ inputKey,
462
+ candidateValueKey: candidate?.candidateValueKey,
463
+ actuatorKind,
464
+ });
465
+ const preparesIntent = optionalText(
466
+ attributes,
467
+ BROWSER_INTERACTION_ATTRIBUTES.preparesIntent,
468
+ );
469
+ const preparesCandidate = parseCandidate(
470
+ attributes,
471
+ BROWSER_INTERACTION_ATTRIBUTES.preparesCandidateValue,
472
+ context,
473
+ preparesIntent ?? intent,
474
+ diagnostics,
475
+ );
476
+ const prepares =
477
+ preparesIntent === undefined
478
+ ? undefined
479
+ : {
480
+ intent: preparesIntent,
481
+ inputKey: optionalText(
482
+ attributes,
483
+ BROWSER_INTERACTION_ATTRIBUTES.preparesInputKey,
484
+ ),
485
+ candidateValue: preparesCandidate?.candidateValue,
486
+ candidateValueKey: preparesCandidate?.candidateValueKey,
487
+ actuatorKind: parseOptionalActuatorKind(
488
+ optionalText(
489
+ attributes,
490
+ BROWSER_INTERACTION_ATTRIBUTES.preparesActuatorKind,
491
+ ),
492
+ ),
493
+ };
494
+ const semanticEffects = parseEffectList(
495
+ attributes,
496
+ BROWSER_INTERACTION_ATTRIBUTES.semanticEffects,
497
+ context,
498
+ intent,
499
+ diagnostics,
500
+ );
501
+ const acceptedEffectPatterns = parseEffectPatternList(
502
+ attributes,
503
+ BROWSER_INTERACTION_ATTRIBUTES.acceptedEffectPatterns,
504
+ context,
505
+ intent,
506
+ diagnostics,
507
+ );
508
+ const preparationPatterns = parseEffectPatternList(
509
+ attributes,
510
+ BROWSER_INTERACTION_ATTRIBUTES.preparationPatterns,
511
+ context,
512
+ intent,
513
+ diagnostics,
514
+ );
515
+
516
+ if (!intent) {
517
+ diagnostics.push(
518
+ diagnosticFor({
519
+ code: "invalid-record",
520
+ message: "Browser interaction actuator records require an intent.",
521
+ ...context,
522
+ }),
523
+ );
524
+ }
525
+
526
+ return {
527
+ actuatorId,
528
+ intent,
529
+ descriptorDigest: optionalText(
530
+ attributes,
531
+ BROWSER_INTERACTION_ATTRIBUTES.descriptorDigest,
532
+ ),
533
+ draftDigest: optionalText(
534
+ attributes,
535
+ BROWSER_INTERACTION_ATTRIBUTES.draftDigest,
536
+ ),
537
+ inputKey,
538
+ candidateValue: candidate?.candidateValue,
539
+ candidateValueKey: candidate?.candidateValueKey,
540
+ candidateState,
541
+ enabled,
542
+ actuatorKind,
543
+ semanticEffects,
544
+ acceptedEffectPatterns,
545
+ preparationPatterns,
546
+ prepares,
547
+ diagnostics: diagnostics.sort(compareDiagnostic),
548
+ };
549
+ }
550
+
551
+ function parseEffectList(
552
+ attributes: Readonly<Record<string, string | boolean | null | undefined>>,
553
+ attribute: string,
554
+ context: {
555
+ surface: string;
556
+ scopeId: string;
557
+ interactionKey: string;
558
+ },
559
+ intent: string,
560
+ diagnostics: BrowserInteractionDiagnostic[],
561
+ ): BrowserInteractionSurfaceEffect[] {
562
+ const encodedList = optionalText(attributes, attribute);
563
+ if (encodedList === undefined) return [];
564
+ try {
565
+ const raw = JSON.parse(encodedList) as unknown;
566
+ if (!Array.isArray(raw) || raw.some((item) => typeof item !== "string")) {
567
+ throw new Error("Expected encoded effect array.");
568
+ }
569
+ return raw
570
+ .map((encoded) => decodeBrowserInteractionEffect(encoded))
571
+ .sort((a, b) =>
572
+ encodeBrowserInteractionEffect(a).localeCompare(
573
+ encodeBrowserInteractionEffect(b),
574
+ ),
575
+ );
576
+ } catch {
577
+ diagnostics.push(
578
+ diagnosticFor({
579
+ code: "invalid-effect-payload",
580
+ message: "Invalid browser interaction semantic effect payload.",
581
+ ...context,
582
+ intent,
583
+ }),
584
+ );
585
+ return [];
586
+ }
587
+ }
588
+
589
+ function parseEffectPatternList(
590
+ attributes: Readonly<Record<string, string | boolean | null | undefined>>,
591
+ attribute: string,
592
+ context: {
593
+ surface: string;
594
+ scopeId: string;
595
+ interactionKey: string;
596
+ },
597
+ intent: string,
598
+ diagnostics: BrowserInteractionDiagnostic[],
599
+ ): BrowserInteractionEffectPattern[] {
600
+ const encodedList = optionalText(attributes, attribute);
601
+ if (encodedList === undefined) return [];
602
+ try {
603
+ const raw = JSON.parse(encodedList) as unknown;
604
+ if (!Array.isArray(raw) || raw.some((item) => typeof item !== "string")) {
605
+ throw new Error("Expected encoded effect pattern array.");
606
+ }
607
+ return raw
608
+ .map((encoded) => decodeBrowserInteractionEffectPattern(encoded))
609
+ .sort((a, b) =>
610
+ encodeBrowserInteractionEffectPattern(a).localeCompare(
611
+ encodeBrowserInteractionEffectPattern(b),
612
+ ),
613
+ );
614
+ } catch {
615
+ diagnostics.push(
616
+ diagnosticFor({
617
+ code: "invalid-effect-pattern",
618
+ message: "Invalid browser interaction semantic effect pattern.",
619
+ ...context,
620
+ intent,
621
+ }),
622
+ );
623
+ return [];
624
+ }
625
+ }
626
+
627
+ function parseCandidate(
628
+ attributes: Readonly<Record<string, string | boolean | null | undefined>>,
629
+ attribute: string,
630
+ context: {
631
+ surface: string;
632
+ scopeId: string;
633
+ interactionKey: string;
634
+ },
635
+ intent: string,
636
+ diagnostics: BrowserInteractionDiagnostic[],
637
+ ) {
638
+ const encoded = optionalText(attributes, attribute);
639
+ if (encoded === undefined) return undefined;
640
+ try {
641
+ const candidateValue = decodeCanonicalCandidateValue(encoded);
642
+ return {
643
+ candidateValue,
644
+ candidateValueKey: encodeCanonicalCandidateValue(candidateValue),
645
+ };
646
+ } catch {
647
+ diagnostics.push(
648
+ diagnosticFor({
649
+ code: "invalid-candidate",
650
+ message: `Invalid canonical browser interaction candidate '${encoded}'.`,
651
+ ...context,
652
+ intent,
653
+ }),
654
+ );
655
+ return undefined;
656
+ }
657
+ }
658
+
659
+ function getInteraction(
660
+ surface: PendingSemanticSurface,
661
+ interactionKey: string,
662
+ interactionId: string,
663
+ ): PendingInteraction {
664
+ const existing = surface.interactions.get(interactionKey);
665
+ if (existing) return existing;
666
+ const next: PendingInteraction = {
667
+ interactionKey,
668
+ interactionId,
669
+ readiness: "ready",
670
+ rootSeen: false,
671
+ actuators: [],
672
+ diagnostics: [],
673
+ };
674
+ surface.interactions.set(interactionKey, next);
675
+ return next;
676
+ }
677
+
678
+ function finalizeSurface(
679
+ surface: PendingSurface,
680
+ ): BrowserInteractionSurfaceSnapshot {
681
+ if (surface.kind !== "semantic") {
682
+ return {
683
+ surface: surface.surface,
684
+ scopeId: surface.scopeId,
685
+ diagnostics: surface.diagnostics.sort(compareDiagnostic),
686
+ };
687
+ }
688
+ const interactions: BrowserInteractionEntity[] = [
689
+ ...surface.interactions.values(),
690
+ ]
691
+ .map((interaction) => ({
692
+ interactionKey: interaction.interactionKey,
693
+ interactionId: interaction.interactionId,
694
+ descriptorDigest: interaction.descriptorDigest,
695
+ draftDigest: interaction.draftDigest,
696
+ readiness: interaction.readiness,
697
+ actuators: interaction.actuators.sort(compareActuator),
698
+ diagnostics: interaction.diagnostics.sort(compareDiagnostic),
699
+ }))
700
+ .sort((a, b) => a.interactionKey.localeCompare(b.interactionKey));
701
+ return {
702
+ surface: surface.surface,
703
+ scopeId: surface.scopeId,
704
+ interactions,
705
+ diagnostics: surface.diagnostics.sort(compareDiagnostic),
706
+ };
707
+ }
708
+
709
+ function diagnosticsForPreparationCycles(
710
+ surface: BrowserInteractionSemanticSurfaceSnapshot,
711
+ interaction: BrowserInteractionEntity,
712
+ ): BrowserInteractionDiagnostic[] {
713
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
714
+ diagnostics.push(
715
+ ...diagnosticsForEffectPreparationCycles(surface, interaction),
716
+ );
717
+ const actuators = new Map<string, BrowserInteractionActuator>();
718
+ for (const actuator of interaction.actuators) {
719
+ actuators.set(
720
+ actuatorIdentityKey({
721
+ surface: surface.surface,
722
+ scopeId: surface.scopeId,
723
+ interactionKey: interaction.interactionKey,
724
+ actuator,
725
+ }),
726
+ actuator,
727
+ );
728
+ }
729
+ for (const actuator of interaction.actuators) {
730
+ const visited = new Set<string>();
731
+ let current: BrowserInteractionActuator | undefined = actuator;
732
+ while (current?.prepares) {
733
+ const key = actuatorIdentityKey({
734
+ surface: surface.surface,
735
+ scopeId: surface.scopeId,
736
+ interactionKey: interaction.interactionKey,
737
+ actuator: current,
738
+ });
739
+ if (visited.has(key)) {
740
+ diagnostics.push(
741
+ diagnosticFor({
742
+ code: "preparation-cycle",
743
+ message: `Preparation cycle detected for actuator '${current.actuatorId}'.`,
744
+ surface: surface.surface,
745
+ scopeId: surface.scopeId,
746
+ interactionKey: interaction.interactionKey,
747
+ intent: current.intent,
748
+ actuatorId: current.actuatorId,
749
+ }),
750
+ );
751
+ break;
752
+ }
753
+ visited.add(key);
754
+ current = actuators.get(
755
+ targetIdentityKey({
756
+ surface: surface.surface,
757
+ scopeId: surface.scopeId,
758
+ interactionKey: interaction.interactionKey,
759
+ target: current.prepares,
760
+ }),
761
+ );
762
+ }
763
+ }
764
+ return diagnostics;
765
+ }
766
+
767
+ function diagnosticsForEffectPreparationCycles(
768
+ surface: BrowserInteractionSemanticSurfaceSnapshot,
769
+ interaction: BrowserInteractionEntity,
770
+ ): BrowserInteractionDiagnostic[] {
771
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
772
+ const actuatorKeys = new Map<BrowserInteractionActuator, string>();
773
+ interaction.actuators.forEach((actuator, index) => {
774
+ actuatorKeys.set(actuator, `${actuator.actuatorId}\0${index}`);
775
+ });
776
+ for (const actuator of interaction.actuators) {
777
+ const visited = new Set<string>();
778
+ let current: BrowserInteractionActuator | undefined = actuator;
779
+ while (current) {
780
+ const key = actuatorKeys.get(current);
781
+ if (!key) break;
782
+ if (visited.has(key)) {
783
+ diagnostics.push(
784
+ diagnosticFor({
785
+ code: "preparation-cycle",
786
+ message: `Semantic preparation cycle detected for actuator '${current.actuatorId}'.`,
787
+ surface: surface.surface,
788
+ scopeId: surface.scopeId,
789
+ interactionKey: interaction.interactionKey,
790
+ intent: current.intent,
791
+ actuatorId: current.actuatorId,
792
+ }),
793
+ );
794
+ break;
795
+ }
796
+ visited.add(key);
797
+ current = interaction.actuators.find((candidate) =>
798
+ current?.preparationPatterns.some((pattern) =>
799
+ candidate.semanticEffects.some((effect) =>
800
+ browserInteractionEffectPatternMatches(pattern, effect),
801
+ ),
802
+ ),
803
+ );
804
+ }
805
+ }
806
+ return diagnostics;
807
+ }
808
+
809
+ function diagnosticsForPreparationPatternAmbiguity(
810
+ surface: BrowserInteractionSemanticSurfaceSnapshot,
811
+ interaction: BrowserInteractionEntity,
812
+ ): BrowserInteractionDiagnostic[] {
813
+ const diagnostics: BrowserInteractionDiagnostic[] = [];
814
+ for (const target of interaction.actuators) {
815
+ for (const effect of target.semanticEffects) {
816
+ const matches = interaction.actuators.filter((actuator) =>
817
+ actuator.preparationPatterns.some((pattern) =>
818
+ browserInteractionEffectPatternMatches(pattern, effect),
819
+ ),
820
+ );
821
+ const uniqueActuatorIds = new Set(
822
+ matches.map((match) => match.actuatorId),
823
+ );
824
+ if (uniqueActuatorIds.size > 1) {
825
+ diagnostics.push(
826
+ diagnosticFor({
827
+ code: "ambiguous-preparation-pattern",
828
+ message:
829
+ "Multiple preparation patterns can prepare the same semantic effect.",
830
+ surface: surface.surface,
831
+ scopeId: surface.scopeId,
832
+ interactionKey: interaction.interactionKey,
833
+ intent: target.intent,
834
+ actuatorId: target.actuatorId,
835
+ }),
836
+ );
837
+ }
838
+ }
839
+ }
840
+ return diagnostics;
841
+ }
842
+
843
+ export function actuatorIdentityKey(input: {
844
+ readonly surface: string;
845
+ readonly scopeId: string;
846
+ readonly interactionKey: string;
847
+ readonly actuator: Pick<
848
+ BrowserInteractionActuator,
849
+ "intent" | "inputKey" | "candidateValueKey" | "actuatorKind"
850
+ >;
851
+ }): string {
852
+ return createBrowserInteractionActuatorKey({
853
+ surface: input.surface,
854
+ scopeId: input.scopeId,
855
+ interactionKey: input.interactionKey,
856
+ intent: input.actuator.intent,
857
+ inputKey: input.actuator.inputKey,
858
+ candidateValueKey: input.actuator.candidateValueKey,
859
+ actuatorKind: input.actuator.actuatorKind,
860
+ });
861
+ }
862
+
863
+ export function isGameplaySurfaceSnapshot(
864
+ surface: BrowserInteractionSurfaceSnapshot,
865
+ ): surface is BrowserInteractionSemanticSurfaceSnapshot<
866
+ typeof GAMEPLAY_BROWSER_INTERACTION_SURFACE
867
+ > {
868
+ return surface.surface === GAMEPLAY_BROWSER_INTERACTION_SURFACE;
869
+ }
870
+
871
+ export function isSemanticSurfaceSnapshot(
872
+ surface: BrowserInteractionSurfaceSnapshot,
873
+ ): surface is BrowserInteractionSemanticSurfaceSnapshot {
874
+ return "interactions" in surface;
875
+ }
876
+
877
+ export function targetIdentityKey(input: {
878
+ readonly surface: string;
879
+ readonly scopeId: string;
880
+ readonly interactionKey: string;
881
+ readonly target: {
882
+ readonly intent: string;
883
+ readonly inputKey?: string;
884
+ readonly candidateValueKey?: string;
885
+ readonly actuatorKind?: BrowserInteractionActuatorKind;
886
+ };
887
+ }): string {
888
+ return createBrowserInteractionActuatorKey({
889
+ surface: input.surface,
890
+ scopeId: input.scopeId,
891
+ interactionKey: input.interactionKey,
892
+ intent: input.target.intent,
893
+ inputKey: input.target.inputKey,
894
+ candidateValueKey: input.target.candidateValueKey,
895
+ actuatorKind: input.target.actuatorKind ?? "click",
896
+ });
897
+ }
898
+
899
+ function pushSurfaceDiagnostic(
900
+ surface: PendingSemanticSurface,
901
+ all: BrowserInteractionDiagnostic[],
902
+ diagnostic: BrowserInteractionDiagnostic,
903
+ ) {
904
+ surface.diagnostics.push(diagnostic);
905
+ all.push(diagnostic);
906
+ }
907
+
908
+ function diagnosticFor(
909
+ input: Omit<BrowserInteractionDiagnostic, "severity">,
910
+ ): BrowserInteractionDiagnostic {
911
+ return { severity: "error", ...input };
912
+ }
913
+
914
+ function text(
915
+ attributes: Readonly<Record<string, string | boolean | null | undefined>>,
916
+ key: string,
917
+ ): string {
918
+ const value = attributes[key];
919
+ if (typeof value === "boolean") return value ? "true" : "false";
920
+ return value ?? "";
921
+ }
922
+
923
+ function optionalText(
924
+ attributes: Readonly<Record<string, string | boolean | null | undefined>>,
925
+ key: string,
926
+ ): string | undefined {
927
+ const value = text(attributes, key);
928
+ return value === "" ? undefined : value;
929
+ }
930
+
931
+ function parseReadiness(value: string): BrowserInteractionReadiness {
932
+ return BROWSER_INTERACTION_READINESS_VALUES.includes(
933
+ value as BrowserInteractionReadiness,
934
+ )
935
+ ? (value as BrowserInteractionReadiness)
936
+ : "blocked";
937
+ }
938
+
939
+ function parseCandidateState(
940
+ value: string | undefined,
941
+ ): BrowserInteractionCandidateState | undefined {
942
+ return BROWSER_INTERACTION_CANDIDATE_STATES.includes(
943
+ value as BrowserInteractionCandidateState,
944
+ )
945
+ ? (value as BrowserInteractionCandidateState)
946
+ : undefined;
947
+ }
948
+
949
+ function parseActuatorKind(value: string): BrowserInteractionActuatorKind {
950
+ return parseOptionalActuatorKind(value) ?? "click";
951
+ }
952
+
953
+ function parseOptionalActuatorKind(
954
+ value: string | undefined,
955
+ ): BrowserInteractionActuatorKind | undefined {
956
+ return BROWSER_INTERACTION_ACTUATOR_KINDS.includes(
957
+ value as BrowserInteractionActuatorKind,
958
+ )
959
+ ? (value as BrowserInteractionActuatorKind)
960
+ : undefined;
961
+ }
962
+
963
+ function parseEnabled(value: string | boolean | null | undefined): boolean {
964
+ if (value === false || value === "false") return false;
965
+ return true;
966
+ }
967
+
968
+ function compareSurface(
969
+ a: BrowserInteractionSurfaceSnapshot,
970
+ b: BrowserInteractionSurfaceSnapshot,
971
+ ): number {
972
+ return (
973
+ a.surface.localeCompare(b.surface) || a.scopeId.localeCompare(b.scopeId)
974
+ );
975
+ }
976
+
977
+ function compareActuator(
978
+ a: BrowserInteractionActuator,
979
+ b: BrowserInteractionActuator,
980
+ ): number {
981
+ return a.actuatorId.localeCompare(b.actuatorId);
982
+ }
983
+
984
+ function compareDiagnostic(
985
+ a: BrowserInteractionDiagnostic,
986
+ b: BrowserInteractionDiagnostic,
987
+ ): number {
988
+ return (
989
+ a.code.localeCompare(b.code) ||
990
+ (a.surface ?? "").localeCompare(b.surface ?? "") ||
991
+ (a.scopeId ?? "").localeCompare(b.scopeId ?? "") ||
992
+ (a.interactionKey ?? "").localeCompare(b.interactionKey ?? "") ||
993
+ (a.intent ?? "").localeCompare(b.intent ?? "") ||
994
+ (a.actuatorId ?? "").localeCompare(b.actuatorId ?? "") ||
995
+ a.message.localeCompare(b.message)
996
+ );
997
+ }