@inflatable-cookie/longhorn 0.1.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 (178) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -0
  3. package/package.json +205 -0
  4. package/src/agent-control/index.ts +20 -0
  5. package/src/agent-control/inject.ts +4 -0
  6. package/src/agent-control/shim.ts +722 -0
  7. package/src/bridge/direct.ts +22 -0
  8. package/src/bridge/generated/fields.ts +32 -0
  9. package/src/bridge/generated/protocol.ts +218 -0
  10. package/src/bridge/generated/variant-fields.ts +10 -0
  11. package/src/bridge/index.ts +15 -0
  12. package/src/bridge/job.ts +51 -0
  13. package/src/bridge/loopback.ts +84 -0
  14. package/src/bridge/operation.ts +166 -0
  15. package/src/bridge/router.ts +71 -0
  16. package/src/bridge/runtime/connection.ts +318 -0
  17. package/src/bridge/runtime/policy.ts +89 -0
  18. package/src/bridge/runtime/retry.ts +60 -0
  19. package/src/bridge/runtime.ts +3 -0
  20. package/src/bridge/session.ts +67 -0
  21. package/src/bridge/stream/connection.ts +146 -0
  22. package/src/bridge/stream/source.ts +61 -0
  23. package/src/bridge/stream/tracker.ts +109 -0
  24. package/src/bridge/stream.ts +6 -0
  25. package/src/bridge/supervision.ts +204 -0
  26. package/src/bridge/validation/base.ts +164 -0
  27. package/src/bridge/validation/jobs.ts +105 -0
  28. package/src/bridge/validation/negotiation.ts +242 -0
  29. package/src/bridge/validation/operations.ts +213 -0
  30. package/src/bridge/validation/streams.ts +47 -0
  31. package/src/bridge/validation.ts +5 -0
  32. package/src/commands/client.ts +124 -0
  33. package/src/commands/controller.ts +411 -0
  34. package/src/commands/generated/fields.ts +43 -0
  35. package/src/commands/generated/protocol.ts +876 -0
  36. package/src/commands/generated/variant-fields.ts +88 -0
  37. package/src/commands/index.ts +13 -0
  38. package/src/commands/keyboard.ts +361 -0
  39. package/src/commands/ports.ts +82 -0
  40. package/src/commands/projectors.ts +306 -0
  41. package/src/commands/validation.ts +444 -0
  42. package/src/config/client.ts +215 -0
  43. package/src/config/generated/base.ts +714 -0
  44. package/src/config/generated/fields.ts +54 -0
  45. package/src/config/generated/label-template.ts +14 -0
  46. package/src/config/generated/labels.ts +30 -0
  47. package/src/config/generated/protocol.ts +3 -0
  48. package/src/config/generated/restore.ts +658 -0
  49. package/src/config/generated/variant-fields.ts +129 -0
  50. package/src/config/index.ts +12 -0
  51. package/src/config/names.ts +19 -0
  52. package/src/config/validation/commands.ts +155 -0
  53. package/src/config/validation/outcomes.ts +243 -0
  54. package/src/config/validation/primitives.ts +174 -0
  55. package/src/config/validation/projection.ts +351 -0
  56. package/src/config/validation.ts +7 -0
  57. package/src/core/connection.ts +252 -0
  58. package/src/core/index.ts +2 -0
  59. package/src/core/transport.ts +24 -0
  60. package/src/history/client.ts +65 -0
  61. package/src/history/controller.ts +385 -0
  62. package/src/history/direct.ts +21 -0
  63. package/src/history/generated/fields.ts +18 -0
  64. package/src/history/generated/protocol.ts +389 -0
  65. package/src/history/generated/variant-fields.ts +27 -0
  66. package/src/history/index.ts +13 -0
  67. package/src/history/ports.ts +31 -0
  68. package/src/history/serialized.ts +38 -0
  69. package/src/history/validation.ts +334 -0
  70. package/src/history-tree/client.ts +16 -0
  71. package/src/history-tree/controller.ts +199 -0
  72. package/src/history-tree/direct.ts +3 -0
  73. package/src/history-tree/generated/fields.ts +26 -0
  74. package/src/history-tree/generated/protocol.ts +711 -0
  75. package/src/history-tree/generated/variant-fields.ts +39 -0
  76. package/src/history-tree/index.ts +13 -0
  77. package/src/history-tree/ports.ts +43 -0
  78. package/src/history-tree/serialized.ts +15 -0
  79. package/src/history-tree/validation.ts +269 -0
  80. package/src/layout/generated/protocol.ts +289 -0
  81. package/src/layout/index.ts +4 -0
  82. package/src/layout/ratio.ts +22 -0
  83. package/src/layout/validation.ts +110 -0
  84. package/src/layout/visibility.ts +30 -0
  85. package/src/licence/client.ts +32 -0
  86. package/src/licence/controller.ts +301 -0
  87. package/src/licence/direct.ts +3 -0
  88. package/src/licence/generated/fields.ts +17 -0
  89. package/src/licence/generated/key-conformance.json +42 -0
  90. package/src/licence/generated/protocol.ts +249 -0
  91. package/src/licence/generated/variant-fields.ts +34 -0
  92. package/src/licence/index.ts +14 -0
  93. package/src/licence/key.ts +125 -0
  94. package/src/licence/ports.ts +39 -0
  95. package/src/licence/serialized.ts +13 -0
  96. package/src/licence/validation.ts +226 -0
  97. package/src/native-content/client.ts +474 -0
  98. package/src/native-content/direct.ts +16 -0
  99. package/src/native-content/generated/fields.ts +32 -0
  100. package/src/native-content/generated/protocol.ts +498 -0
  101. package/src/native-content/generated/variant-fields.ts +77 -0
  102. package/src/native-content/index.ts +12 -0
  103. package/src/native-content/ports.ts +46 -0
  104. package/src/native-content/serialized.ts +50 -0
  105. package/src/native-content/validation/base.ts +194 -0
  106. package/src/native-content/validation/messages.ts +192 -0
  107. package/src/native-content/validation/state.ts +404 -0
  108. package/src/native-content/validation.ts +7 -0
  109. package/src/notifications/client.ts +106 -0
  110. package/src/notifications/controller.ts +240 -0
  111. package/src/notifications/direct.ts +12 -0
  112. package/src/notifications/generated/fields.ts +19 -0
  113. package/src/notifications/generated/labels.ts +20 -0
  114. package/src/notifications/generated/protocol.ts +522 -0
  115. package/src/notifications/generated/variant-fields.ts +37 -0
  116. package/src/notifications/index.ts +16 -0
  117. package/src/notifications/ports.ts +22 -0
  118. package/src/notifications/serialized.ts +27 -0
  119. package/src/notifications/toast.ts +41 -0
  120. package/src/notifications/tone.ts +27 -0
  121. package/src/notifications/validation.ts +246 -0
  122. package/src/operation/bridge.ts +168 -0
  123. package/src/operation/client.ts +167 -0
  124. package/src/operation/controller.ts +321 -0
  125. package/src/operation/direct.ts +15 -0
  126. package/src/operation/generated/fields.ts +20 -0
  127. package/src/operation/generated/labels.ts +15 -0
  128. package/src/operation/generated/protocol.ts +762 -0
  129. package/src/operation/generated/variant-fields.ts +60 -0
  130. package/src/operation/index.ts +14 -0
  131. package/src/operation/ports.ts +29 -0
  132. package/src/operation/serialized.ts +39 -0
  133. package/src/operation/validation.ts +479 -0
  134. package/src/settings/client.ts +142 -0
  135. package/src/settings/connection.ts +246 -0
  136. package/src/settings/generated/fields.ts +34 -0
  137. package/src/settings/generated/protocol.ts +668 -0
  138. package/src/settings/generated/variant-fields.ts +26 -0
  139. package/src/settings/index.ts +12 -0
  140. package/src/settings/names.ts +8 -0
  141. package/src/settings/registry.ts +141 -0
  142. package/src/settings/validation/authority.ts +66 -0
  143. package/src/settings/validation/command.ts +165 -0
  144. package/src/settings/validation/error.ts +33 -0
  145. package/src/settings/validation/primitives.ts +203 -0
  146. package/src/settings/validation/projection.ts +50 -0
  147. package/src/settings/validation/registry.ts +170 -0
  148. package/src/settings/validation.ts +4 -0
  149. package/src/surface-transfer/client.ts +54 -0
  150. package/src/surface-transfer/generated/fields.ts +12 -0
  151. package/src/surface-transfer/generated/protocol.ts +61 -0
  152. package/src/surface-transfer/generated/variant-fields.ts +30 -0
  153. package/src/surface-transfer/index.ts +9 -0
  154. package/src/surface-transfer/validation.ts +209 -0
  155. package/src/surfaces/client.ts +140 -0
  156. package/src/surfaces/generated/fields.ts +19 -0
  157. package/src/surfaces/generated/protocol.ts +217 -0
  158. package/src/surfaces/generated/variant-fields.ts +42 -0
  159. package/src/surfaces/index.ts +9 -0
  160. package/src/surfaces/validation.ts +199 -0
  161. package/src/transfer/client.ts +133 -0
  162. package/src/transfer/connection.ts +87 -0
  163. package/src/transfer/generated/fields.ts +23 -0
  164. package/src/transfer/generated/protocol.ts +171 -0
  165. package/src/transfer/generated/variant-fields.ts +45 -0
  166. package/src/transfer/index.ts +11 -0
  167. package/src/transfer/payload.ts +71 -0
  168. package/src/transfer/validation.ts +282 -0
  169. package/src/update/client.ts +29 -0
  170. package/src/update/controller.ts +255 -0
  171. package/src/update/direct.ts +3 -0
  172. package/src/update/generated/fields.ts +14 -0
  173. package/src/update/generated/protocol.ts +238 -0
  174. package/src/update/generated/variant-fields.ts +44 -0
  175. package/src/update/index.ts +14 -0
  176. package/src/update/ports.ts +36 -0
  177. package/src/update/serialized.ts +12 -0
  178. package/src/update/validation.ts +191 -0
@@ -0,0 +1,88 @@
1
+ // @generated by `effigy generate:commands`; do not edit.
2
+ // Allowed field names per tagged-union variant, from the Rust enums.
3
+ // Keyed by type, then by discriminant. The discriminant itself is in
4
+ // each list, because `exact()` compares every key.
5
+
6
+ export const COMMAND_VARIANT_FIELDS: Record<string, Record<string, readonly string[]>> = {
7
+ "CommandArgumentKind": {
8
+ "boolean": ["kind"],
9
+ "number": ["kind", "minimum", "maximum"],
10
+ "integer": ["kind", "minimum", "maximum"],
11
+ "string": ["kind", "maximumBytes"],
12
+ "enum": ["kind", "values"],
13
+ },
14
+ "CommandArgumentSchema": {
15
+ "none": ["shape"],
16
+ "object": ["shape", "fields"],
17
+ },
18
+ "CommandArgumentValue": {
19
+ "boolean": ["kind", "value"],
20
+ "number": ["kind", "value"],
21
+ "integer": ["kind", "value"],
22
+ "string": ["kind", "value"],
23
+ "enum": ["kind", "value"],
24
+ },
25
+ "CommandAvailabilityReasonCode": {
26
+ "contextNotAllowed": ["kind"],
27
+ "missingCapability": ["kind"],
28
+ "consumer": ["kind", "code"],
29
+ },
30
+ "CommandKeymapOverride": {
31
+ "disable": ["kind", "bindingId"],
32
+ "replace": ["kind", "bindingId", "replacement"],
33
+ "add": ["kind", "binding"],
34
+ },
35
+ "CommandBindingSource": {
36
+ "preset": ["kind", "presetId", "presetVersion"],
37
+ "replacement": ["kind", "presetId", "presetVersion"],
38
+ "addedOverride": ["kind"],
39
+ },
40
+ "CommandCandidateDisposition": {
41
+ "winner": ["kind"],
42
+ "equivalent": ["kind"],
43
+ "shadowed": ["kind", "byContextId"],
44
+ "conflict": ["kind"],
45
+ },
46
+ "CommandKeyResolution": {
47
+ "unbound": ["kind"],
48
+ "gated": ["kind", "gate", "candidates"],
49
+ "captured": ["kind", "chord", "label"],
50
+ "conflict": ["kind", "conflict", "candidates"],
51
+ "resolved": ["kind", "winner", "candidates"],
52
+ },
53
+ "CommandKeymapLoadOrigin": {
54
+ "default": ["kind"],
55
+ "file": ["kind"],
56
+ "migrated": ["kind", "from", "to"],
57
+ },
58
+ "CommandKeymapPreviewResult": {
59
+ "accepted": ["status", "evidence", "snapshot"],
60
+ "stale": ["status", "snapshot"],
61
+ "rejected": ["status", "rejection", "snapshot", "conflicts"],
62
+ },
63
+ "CommandKeymapLoadOutcome": {
64
+ "loaded": ["status", "snapshot"],
65
+ "recovery": ["status", "recovery"],
66
+ "unavailable": ["status", "detail"],
67
+ },
68
+ "CommandKeymapMutationResult": {
69
+ "applied": ["status", "snapshot", "receipt"],
70
+ "stale": ["status", "snapshot"],
71
+ "rejected": ["status", "rejection", "snapshot", "conflicts"],
72
+ },
73
+ };
74
+
75
+ export const COMMAND_VARIANT_FIELDS_DISCRIMINANTS: Record<string, string> = {
76
+ "CommandArgumentKind": "kind",
77
+ "CommandArgumentSchema": "shape",
78
+ "CommandArgumentValue": "kind",
79
+ "CommandAvailabilityReasonCode": "kind",
80
+ "CommandKeymapOverride": "kind",
81
+ "CommandBindingSource": "kind",
82
+ "CommandCandidateDisposition": "kind",
83
+ "CommandKeyResolution": "kind",
84
+ "CommandKeymapLoadOrigin": "kind",
85
+ "CommandKeymapPreviewResult": "status",
86
+ "CommandKeymapLoadOutcome": "status",
87
+ "CommandKeymapMutationResult": "status",
88
+ };
@@ -0,0 +1,13 @@
1
+ export * from "./generated/protocol.ts";
2
+ // The generated field map is public API. A consumer that mirrors these
3
+ // types structurally -- Poodle does, because it may not depend on Longhorn --
4
+ // has no compile-time link to them, so a field added here is silent on their
5
+ // side. Whoever sees both can assert their shape against this map and fail
6
+ // when it drifts.
7
+ export * from "./generated/fields.ts";
8
+ export * from "./validation.ts";
9
+ export * from "./ports.ts";
10
+ export * from "./client.ts";
11
+ export * from "./projectors.ts";
12
+ export * from "./keyboard.ts";
13
+ export * from "./controller.ts";
@@ -0,0 +1,361 @@
1
+ import { COMMAND_MAXIMUM_PHYSICAL_CODE_BYTES } from "./generated/protocol.ts";
2
+ import type {
3
+ CommandBindingCandidate,
4
+ CommandDiscoveryRecord,
5
+ CommandEffectiveBinding,
6
+ CommandInvocation,
7
+ CommandKeyChord,
8
+ CommandKeyboardInput,
9
+ CommandKeyboardMode,
10
+ CommandKeyResolution,
11
+ CommandKeymapConflict,
12
+ CommandPlatform,
13
+ } from "./generated/protocol.ts";
14
+ import { shortcutLabel } from "./projectors.ts";
15
+ import type {
16
+ CommandExecutionOutcome,
17
+ CommandExecutionSource,
18
+ } from "./ports.ts";
19
+
20
+ export interface KeyboardEventLike {
21
+ readonly code: string;
22
+ readonly ctrlKey: boolean;
23
+ readonly altKey: boolean;
24
+ readonly shiftKey: boolean;
25
+ readonly metaKey: boolean;
26
+ readonly repeat: boolean;
27
+ readonly isComposing?: boolean;
28
+ readonly target?: unknown;
29
+ preventDefault(): void;
30
+ stopPropagation(): void;
31
+ }
32
+
33
+ export interface CommandKeyboardResolutionOptions {
34
+ readonly platform: CommandPlatform;
35
+ readonly input: CommandKeyboardInput;
36
+ readonly contextPath: readonly string[];
37
+ readonly mode: CommandKeyboardMode;
38
+ readonly bindings: readonly CommandEffectiveBinding[];
39
+ readonly commands: readonly CommandDiscoveryRecord[];
40
+ readonly reserved?: (platform: CommandPlatform, chord: CommandKeyChord) => boolean;
41
+ }
42
+
43
+ export interface CommandKeyboardDispatch {
44
+ dispatchInvocation(
45
+ invocation: CommandInvocation,
46
+ source: CommandExecutionSource,
47
+ ): Promise<CommandExecutionOutcome>;
48
+ }
49
+
50
+ export interface RouteKeyboardEventOptions
51
+ extends Omit<CommandKeyboardResolutionOptions, "input"> {
52
+ readonly dispatcher: CommandKeyboardDispatch;
53
+ readonly editableTarget?: (target: unknown) => boolean;
54
+ readonly onCapture?: (chord: CommandKeyChord, label: string) => void;
55
+ }
56
+
57
+ export interface CommandKeyboardRouteResult {
58
+ readonly consumed: boolean;
59
+ readonly resolution: CommandKeyResolution;
60
+ readonly execution?: Promise<CommandExecutionOutcome>;
61
+ }
62
+
63
+ export function normalizeKeyboardEvent(
64
+ event: KeyboardEventLike,
65
+ editableTarget: (target: unknown) => boolean = isEditableCommandTarget,
66
+ ): CommandKeyboardInput {
67
+ return {
68
+ chord: {
69
+ code: event.code,
70
+ modifiers: {
71
+ control: event.ctrlKey,
72
+ alt: event.altKey,
73
+ shift: event.shiftKey,
74
+ meta: event.metaKey,
75
+ },
76
+ },
77
+ repeat: event.repeat,
78
+ composing: event.isComposing === true,
79
+ editableText: editableTarget(event.target),
80
+ };
81
+ }
82
+
83
+ export function resolveCommandKeyboard(
84
+ options: CommandKeyboardResolutionOptions,
85
+ ): CommandKeyResolution {
86
+ const { input } = options;
87
+ if (!validPhysicalCode(input.chord.code)) return { kind: "unbound" };
88
+ if (input.repeat) return gated("repeat");
89
+ if (input.composing) return gated("composition");
90
+ if (options.reserved?.(options.platform, input.chord) === true) {
91
+ return gated("reserved");
92
+ }
93
+ if (options.mode === "capture") {
94
+ return {
95
+ kind: "captured",
96
+ chord: input.chord,
97
+ label: shortcutLabel(input.chord, options.platform),
98
+ };
99
+ }
100
+
101
+ const positions = new Map(
102
+ options.contextPath.map((contextId, index) => [contextId, index]),
103
+ );
104
+ const matches = options.bindings
105
+ .flatMap((binding) => {
106
+ const specificity = positions.get(binding.contextId);
107
+ if (
108
+ specificity === undefined ||
109
+ !includesPlatform(binding.platform, options.platform) ||
110
+ !sameChord(resolveBindingChord(binding, options.platform), input.chord)
111
+ ) {
112
+ return [];
113
+ }
114
+ return [{ binding, specificity }];
115
+ })
116
+ .sort(
117
+ (left, right) =>
118
+ right.specificity - left.specificity ||
119
+ left.binding.id.localeCompare(right.binding.id),
120
+ );
121
+ if (matches.length === 0) return { kind: "unbound" };
122
+
123
+ const winningSpecificity = matches[0]!.specificity;
124
+ const winningContext = matches[0]!.binding.contextId;
125
+ const winning = matches.filter(
126
+ ({ specificity }) => specificity === winningSpecificity,
127
+ );
128
+ const distinctInvocations = uniqueInvocations(
129
+ winning.map(({ binding }) => binding.invocation),
130
+ );
131
+ const conflict = distinctInvocations.length > 1;
132
+ const representativeId = matches[0]!.binding.id;
133
+ const candidates: CommandBindingCandidate[] = matches.map(
134
+ ({ binding, specificity }) => ({
135
+ bindingId: binding.id,
136
+ source: binding.source,
137
+ matchedContextId: binding.contextId,
138
+ specificity,
139
+ invocation: binding.invocation,
140
+ disposition:
141
+ specificity < winningSpecificity
142
+ ? { kind: "shadowed", byContextId: winningContext }
143
+ : conflict
144
+ ? { kind: "conflict" }
145
+ : binding.id === representativeId
146
+ ? { kind: "winner" }
147
+ : { kind: "equivalent" },
148
+ }),
149
+ );
150
+
151
+ if (conflict) {
152
+ const record: CommandKeymapConflict = {
153
+ platform: options.platform,
154
+ chord: input.chord,
155
+ contextId: winningContext,
156
+ bindingIds: winning.map(({ binding }) => binding.id),
157
+ invocations: distinctInvocations,
158
+ };
159
+ return { kind: "conflict", conflict: record, candidates };
160
+ }
161
+
162
+ const binding = matches[0]!.binding;
163
+ const declaration = options.commands.find(
164
+ ({ id }) => id === binding.invocation.commandId,
165
+ );
166
+ if (input.editableText && declaration?.textInputPolicy === "blocked") {
167
+ return { kind: "gated", gate: "textInput", candidates };
168
+ }
169
+ return {
170
+ kind: "resolved",
171
+ winner: {
172
+ bindingId: binding.id,
173
+ matchedContextId: binding.contextId,
174
+ invocation: binding.invocation,
175
+ },
176
+ candidates,
177
+ };
178
+ }
179
+
180
+ export function routeCommandKeyboardEvent(
181
+ event: KeyboardEventLike,
182
+ options: RouteKeyboardEventOptions,
183
+ ): CommandKeyboardRouteResult {
184
+ const resolution = resolveCommandKeyboard({
185
+ ...options,
186
+ input: normalizeKeyboardEvent(event, options.editableTarget),
187
+ });
188
+ const consumed =
189
+ resolution.kind === "captured" || resolution.kind === "resolved";
190
+ if (consumed) {
191
+ event.preventDefault();
192
+ event.stopPropagation();
193
+ }
194
+ if (resolution.kind === "captured") {
195
+ options.onCapture?.(resolution.chord, resolution.label);
196
+ return { consumed, resolution };
197
+ }
198
+ if (resolution.kind === "resolved") {
199
+ return {
200
+ consumed,
201
+ resolution,
202
+ execution: options.dispatcher.dispatchInvocation(
203
+ resolution.winner.invocation,
204
+ "keyboard",
205
+ ),
206
+ };
207
+ }
208
+ return { consumed, resolution };
209
+ }
210
+
211
+ export function isEditableCommandTarget(target: unknown): boolean {
212
+ if (typeof target !== "object" || target === null) return false;
213
+ const record = target as {
214
+ readonly isContentEditable?: boolean;
215
+ readonly tagName?: unknown;
216
+ readonly role?: unknown;
217
+ getAttribute?(name: string): string | null;
218
+ };
219
+ if (record.isContentEditable === true) return true;
220
+ const tag =
221
+ typeof record.tagName === "string" ? record.tagName.toLowerCase() : "";
222
+ if (tag === "input" || tag === "textarea" || tag === "select") return true;
223
+ const role =
224
+ typeof record.getAttribute === "function"
225
+ ? record.getAttribute("role")
226
+ : typeof record.role === "string"
227
+ ? record.role
228
+ : null;
229
+ return role === "textbox" || role === "searchbox";
230
+ }
231
+
232
+ function gated(
233
+ gate: "repeat" | "composition" | "reserved",
234
+ ): CommandKeyResolution {
235
+ return { kind: "gated", gate, candidates: [] };
236
+ }
237
+
238
+ function includesPlatform(
239
+ scope: CommandEffectiveBinding["platform"],
240
+ platform: CommandPlatform,
241
+ ): boolean {
242
+ return scope === "any" || scope === platform;
243
+ }
244
+
245
+ function resolveBindingChord(
246
+ binding: CommandEffectiveBinding,
247
+ platform: CommandPlatform,
248
+ ): CommandKeyChord {
249
+ return {
250
+ code: binding.trigger.code,
251
+ modifiers: {
252
+ control:
253
+ binding.trigger.modifiers.control ||
254
+ (binding.trigger.modifiers.primary &&
255
+ (platform === "windows" || platform === "linux")),
256
+ alt: binding.trigger.modifiers.alt,
257
+ shift: binding.trigger.modifiers.shift,
258
+ meta:
259
+ binding.trigger.modifiers.meta ||
260
+ (binding.trigger.modifiers.primary && platform === "macOs"),
261
+ },
262
+ };
263
+ }
264
+
265
+ function sameChord(left: CommandKeyChord, right: CommandKeyChord): boolean {
266
+ return (
267
+ left.code === right.code &&
268
+ left.modifiers.control === right.modifiers.control &&
269
+ left.modifiers.alt === right.modifiers.alt &&
270
+ left.modifiers.shift === right.modifiers.shift &&
271
+ left.modifiers.meta === right.modifiers.meta
272
+ );
273
+ }
274
+
275
+ function uniqueInvocations(
276
+ invocations: readonly CommandInvocation[],
277
+ ): CommandInvocation[] {
278
+ const keyed = new Map<string, CommandInvocation>();
279
+ for (const invocation of invocations) {
280
+ keyed.set(invocationKey(invocation), invocation);
281
+ }
282
+ return [...keyed.values()].sort(compareInvocation);
283
+ }
284
+
285
+ function invocationKey(invocation: CommandInvocation): string {
286
+ const argumentsKey = Object.entries(invocation.arguments)
287
+ .sort(([left], [right]) => left.localeCompare(right))
288
+ .map(([key, value]) => [key, value]);
289
+ return JSON.stringify([invocation.commandId, argumentsKey]);
290
+ }
291
+
292
+ function compareInvocation(
293
+ left: CommandInvocation,
294
+ right: CommandInvocation,
295
+ ): number {
296
+ const id = compareText(left.commandId, right.commandId);
297
+ if (id !== 0) return id;
298
+ const leftArguments = Object.entries(left.arguments).sort(([a], [b]) =>
299
+ compareText(a, b),
300
+ );
301
+ const rightArguments = Object.entries(right.arguments).sort(([a], [b]) =>
302
+ compareText(a, b),
303
+ );
304
+ const length = Math.min(leftArguments.length, rightArguments.length);
305
+ for (let index = 0; index < length; index += 1) {
306
+ const [leftId, leftValue] = leftArguments[index]!;
307
+ const [rightId, rightValue] = rightArguments[index]!;
308
+ const field = compareText(leftId, rightId);
309
+ if (field !== 0) return field;
310
+ const value = compareArgumentValue(leftValue, rightValue);
311
+ if (value !== 0) return value;
312
+ }
313
+ return leftArguments.length - rightArguments.length;
314
+ }
315
+
316
+ function compareArgumentValue(
317
+ left: CommandInvocation["arguments"][string],
318
+ right: CommandInvocation["arguments"][string],
319
+ ): number {
320
+ const ranks = {
321
+ boolean: 0,
322
+ number: 1,
323
+ integer: 2,
324
+ string: 3,
325
+ enum: 4,
326
+ } as const;
327
+ const rank = ranks[left.kind] - ranks[right.kind];
328
+ if (rank !== 0) return rank;
329
+ if (
330
+ (left.kind === "number" || left.kind === "integer") &&
331
+ (right.kind === "number" || right.kind === "integer")
332
+ ) {
333
+ return left.value - right.value;
334
+ }
335
+ if (left.kind === "boolean" && right.kind === "boolean") {
336
+ return Number(left.value) - Number(right.value);
337
+ }
338
+ return compareText(String(left.value), String(right.value));
339
+ }
340
+
341
+ function compareText(left: string, right: string): number {
342
+ const leftValues = [...left];
343
+ const rightValues = [...right];
344
+ const length = Math.min(leftValues.length, rightValues.length);
345
+ for (let index = 0; index < length; index += 1) {
346
+ const difference =
347
+ leftValues[index]!.codePointAt(0)! -
348
+ rightValues[index]!.codePointAt(0)!;
349
+ if (difference !== 0) return difference;
350
+ }
351
+ return leftValues.length - rightValues.length;
352
+ }
353
+
354
+ function validPhysicalCode(code: string): boolean {
355
+ return (
356
+ code !== "Unidentified" &&
357
+ code.length > 0 &&
358
+ code.length <= COMMAND_MAXIMUM_PHYSICAL_CODE_BYTES &&
359
+ /^[A-Z][A-Za-z0-9]*$/u.test(code)
360
+ );
361
+ }
@@ -0,0 +1,82 @@
1
+ import type {
2
+ CommandArguments,
3
+ CommandAvailabilitySnapshot,
4
+ CommandCatalogueSnapshot,
5
+ CommandInvocation,
6
+ CommandKeymapCommit,
7
+ CommandKeymapLoadOutcome,
8
+ CommandKeymapMutationResult,
9
+ CommandKeymapPreview,
10
+ CommandKeymapPreviewResult,
11
+ CommandKeymapReset,
12
+ CommandRequestId,
13
+ } from "./generated/protocol.ts";
14
+
15
+ export type CommandUnlisten = () => void | Promise<void>;
16
+
17
+ export interface CommandSnapshotPort<Snapshot> {
18
+ load(): Promise<Snapshot>;
19
+ listen?(invalidate: () => void): CommandUnlisten | Promise<CommandUnlisten>;
20
+ }
21
+
22
+ export interface CommandCataloguePort
23
+ extends CommandSnapshotPort<CommandCatalogueSnapshot> {}
24
+
25
+ export interface CommandKeymapPort
26
+ extends CommandSnapshotPort<CommandKeymapLoadOutcome> {
27
+ preview?(request: CommandKeymapPreview): Promise<CommandKeymapPreviewResult>;
28
+ commit?(request: CommandKeymapCommit): Promise<CommandKeymapMutationResult>;
29
+ reset?(request: CommandKeymapReset): Promise<CommandKeymapMutationResult>;
30
+ }
31
+
32
+ export interface CommandAvailabilityPort
33
+ extends CommandSnapshotPort<CommandAvailabilitySnapshot> {}
34
+
35
+ export type CommandExecutionSource =
36
+ | "palette"
37
+ | "keyboard"
38
+ | "menu"
39
+ | "help"
40
+ | "consumer";
41
+
42
+ export interface CommandExecutionIntent {
43
+ readonly requestId: CommandRequestId;
44
+ readonly registryGeneration: number;
45
+ readonly observedContextRevision: number;
46
+ readonly invocation: CommandInvocation;
47
+ readonly source: CommandExecutionSource;
48
+ }
49
+
50
+ export type CommandExecutionOutcome =
51
+ | { readonly status: "succeeded"; readonly evidence?: unknown }
52
+ | {
53
+ readonly status:
54
+ | "unknownCommand"
55
+ | "staleRegistry"
56
+ | "invalidArguments"
57
+ | "unavailable"
58
+ | "unauthorized"
59
+ | "cancelled"
60
+ | "rejected"
61
+ | "failed"
62
+ | "indeterminate";
63
+ readonly evidence?: unknown;
64
+ };
65
+
66
+ export interface CommandExecutorPort {
67
+ execute(intent: CommandExecutionIntent): Promise<CommandExecutionOutcome>;
68
+ }
69
+
70
+ export interface CommandPorts {
71
+ readonly catalogue: CommandCataloguePort;
72
+ readonly keymap: CommandKeymapPort;
73
+ readonly availability: CommandAvailabilityPort;
74
+ readonly executor: CommandExecutorPort;
75
+ readonly nextRequestId: () => CommandRequestId;
76
+ }
77
+
78
+ export interface CommandDispatch {
79
+ readonly commandId: string;
80
+ readonly arguments?: CommandArguments;
81
+ readonly source: CommandExecutionSource;
82
+ }