@cueloop/client 0.1.0-alpha.7 → 0.1.0-alpha.71

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 (248) hide show
  1. package/native/build-pty.sh +51 -0
  2. package/native/build.sh +82 -0
  3. package/native/darwin-arm64/libcuelooppty.dylib +0 -0
  4. package/native/darwin-arm64/libcueloopvt.dylib +0 -0
  5. package/native/src/pty.zig +193 -0
  6. package/native/src/shim.zig +114 -0
  7. package/package.json +39 -32
  8. package/src/App.appearance.test.tsx +53 -0
  9. package/src/App.autoclose.test.tsx +76 -50
  10. package/src/App.diff.test.tsx +156 -42
  11. package/src/App.export.test.tsx +58 -40
  12. package/src/App.plan-review.test.tsx +363 -0
  13. package/src/App.prototype-markdown.test.tsx +92 -0
  14. package/src/App.prototype.test.tsx +176 -0
  15. package/src/App.readonly.test.tsx +128 -67
  16. package/src/App.rendering.test.tsx +61 -35
  17. package/src/App.submit-confirm.test.tsx +191 -0
  18. package/src/App.test.tsx +310 -114
  19. package/src/App.toast.test.tsx +89 -0
  20. package/src/App.tsx +1043 -798
  21. package/src/App.walk.test.tsx +298 -0
  22. package/src/App.workbench.test.tsx +495 -0
  23. package/src/__snapshots__/view-plan.regression.test.ts.snap +100 -0
  24. package/src/annotation-palette.ts +61 -0
  25. package/src/app-key-state.ts +61 -0
  26. package/src/app-screens.tsx +432 -0
  27. package/src/app-view-model.ts +159 -0
  28. package/src/attribution.test.ts +31 -0
  29. package/src/attribution.ts +42 -0
  30. package/src/clipboard.test.ts +23 -0
  31. package/src/clipboard.ts +33 -0
  32. package/src/components/AnnotatableFileView.stories.tsx +42 -0
  33. package/src/components/AnnotatableFileView.test.tsx +102 -0
  34. package/src/components/AnnotatableFileView.tsx +126 -0
  35. package/src/components/AnnotationCards.stories.tsx +87 -0
  36. package/src/components/AnnotationCards.tsx +303 -0
  37. package/src/components/AppHeader.stories.tsx +50 -0
  38. package/src/components/AppHeader.tsx +83 -0
  39. package/src/components/AppShell.stories.tsx +89 -0
  40. package/src/components/AppShell.tsx +290 -0
  41. package/src/components/AppShell.zoom.test.tsx +135 -0
  42. package/src/components/BareWorkbenchFileView.stories.tsx +27 -0
  43. package/src/components/BareWorkbenchFileView.tsx +64 -0
  44. package/src/components/Breadcrumb.stories.tsx +19 -0
  45. package/src/components/Breadcrumb.tsx +45 -0
  46. package/src/components/ChangesColumn.stories.tsx +39 -0
  47. package/src/components/ChangesColumn.tsx +171 -0
  48. package/src/components/CodeBlock.stories.tsx +42 -0
  49. package/src/components/CodeBlock.tsx +84 -0
  50. package/src/components/CompletionOverlay.stories.tsx +23 -0
  51. package/src/components/CompletionOverlay.tsx +60 -0
  52. package/src/components/ConfirmCard.stories.tsx +35 -0
  53. package/src/components/ConfirmCard.tsx +128 -0
  54. package/src/components/ConfirmDialog.stories.tsx +21 -0
  55. package/src/components/ConfirmDialog.tsx +66 -0
  56. package/src/components/DiffContentView.scroll.test.tsx +121 -0
  57. package/src/components/DiffContentView.split.test.tsx +140 -0
  58. package/src/components/DiffContentView.stories.tsx +80 -0
  59. package/src/components/DiffContentView.syntax.test.tsx +82 -0
  60. package/src/components/DiffContentView.tsx +859 -0
  61. package/src/components/DiscussionMarkerRail.stories.tsx +54 -0
  62. package/src/components/DiscussionMarkerRail.tsx +181 -0
  63. package/src/components/EditorGrid.stories.tsx +59 -0
  64. package/src/components/EditorGrid.tabs.test.tsx +118 -0
  65. package/src/components/EditorGrid.tsx +368 -0
  66. package/src/components/EditorGrid.zoom.test.tsx +68 -0
  67. package/src/components/FileContentsView.stories.tsx +16 -0
  68. package/src/components/FileContentsView.test.tsx +29 -0
  69. package/src/components/FileContentsView.tsx +98 -0
  70. package/src/components/KeybindsDialog.stories.tsx +31 -0
  71. package/src/components/KeybindsDialog.tsx +103 -0
  72. package/src/components/MenuBar.stories.tsx +28 -0
  73. package/src/components/MenuBar.tsx +44 -0
  74. package/src/components/OverlayScrollbar.stories.tsx +33 -0
  75. package/src/components/OverlayScrollbar.test.tsx +139 -0
  76. package/src/components/OverlayScrollbar.tsx +129 -0
  77. package/src/components/PaneGrid.stories.tsx +84 -0
  78. package/src/components/PaneGrid.tsx +128 -0
  79. package/src/components/PanelColumn.stories.tsx +25 -0
  80. package/src/components/PanelColumn.tsx +95 -0
  81. package/src/components/ProjectTreeView.stories.tsx +25 -0
  82. package/src/components/ProjectTreeView.test.tsx +32 -0
  83. package/src/components/ProjectTreeView.tsx +83 -0
  84. package/src/components/PromptDialog.stories.tsx +36 -0
  85. package/src/components/PromptDialog.tsx +87 -0
  86. package/src/components/PrototypeContentView.stories.tsx +19 -0
  87. package/src/components/PrototypeContentView.tsx +446 -0
  88. package/src/components/RootOverlay.stories.tsx +44 -0
  89. package/src/components/RootOverlay.tsx +35 -0
  90. package/src/components/ScrollArea.stories.tsx +20 -0
  91. package/src/components/ScrollArea.tsx +44 -0
  92. package/src/components/SettingsDialog.stories.tsx +117 -0
  93. package/src/components/SettingsDialog.tsx +195 -0
  94. package/src/components/SettingsRows.stories.tsx +18 -0
  95. package/src/components/SettingsRows.tsx +112 -0
  96. package/src/components/ShellHeader.stories.tsx +50 -0
  97. package/src/components/ShellHeader.tsx +137 -0
  98. package/src/components/ThreadFooter.stories.tsx +30 -0
  99. package/src/components/ThreadFooter.test.tsx +31 -0
  100. package/src/components/ThreadFooter.tsx +54 -0
  101. package/src/components/ThreadTree.stories.tsx +46 -0
  102. package/src/components/ThreadTree.tsx +219 -0
  103. package/src/components/ThreadView.selection.test.tsx +812 -0
  104. package/src/components/ThreadView.slash.test.tsx +158 -0
  105. package/src/components/ThreadView.stories.tsx +26 -0
  106. package/src/components/ThreadView.tsx +330 -0
  107. package/src/components/ThreadsSidebar.stories.tsx +36 -0
  108. package/src/components/ThreadsSidebar.tsx +71 -0
  109. package/src/components/Toast.stories.tsx +12 -0
  110. package/src/components/Toast.tsx +45 -0
  111. package/src/components/Tooltip.stories.tsx +26 -0
  112. package/src/components/Tooltip.tsx +72 -0
  113. package/src/components/TreePane.stories.tsx +106 -0
  114. package/src/components/TreePane.tsx +157 -0
  115. package/src/components/WalkWizard.stories.tsx +100 -0
  116. package/src/components/WalkWizard.tsx +152 -0
  117. package/src/components/WelcomePlayground.stories.tsx +12 -0
  118. package/src/components/WelcomePlayground.test.tsx +47 -0
  119. package/src/components/WelcomePlayground.tsx +140 -0
  120. package/src/components/__snapshots__/stories.test.tsx.snap +1845 -0
  121. package/src/components/agent-launcher.stories.tsx +26 -0
  122. package/src/components/agent-launcher.test.tsx +50 -0
  123. package/src/components/agent-launcher.tsx +149 -0
  124. package/src/components/diff-content-view-layout.test.ts +198 -0
  125. package/src/components/diff-content-view-layout.ts +84 -0
  126. package/src/components/editor-grid.test.ts +86 -0
  127. package/src/components/editor-grid.ts +138 -0
  128. package/src/components/fade-title.test.ts +49 -0
  129. package/src/components/fade-title.ts +61 -0
  130. package/src/components/file-tree.test.ts +60 -0
  131. package/src/components/file-tree.ts +112 -0
  132. package/src/components/pages/Workspace.stories.tsx +181 -0
  133. package/src/components/primitives/Button.stories.tsx +32 -0
  134. package/src/components/primitives/Button.tsx +52 -0
  135. package/src/components/primitives/Card.stories.tsx +25 -0
  136. package/src/components/primitives/Card.tsx +60 -0
  137. package/src/components/primitives/Dialog.stories.tsx +24 -0
  138. package/src/components/primitives/Dialog.tsx +66 -0
  139. package/src/components/primitives/IconButton.stories.tsx +36 -0
  140. package/src/components/primitives/IconButton.tsx +62 -0
  141. package/src/components/primitives/StatusBar.stories.tsx +11 -0
  142. package/src/components/primitives/StatusBar.tsx +23 -0
  143. package/src/components/primitives/Tabs.stories.tsx +29 -0
  144. package/src/components/primitives/Tabs.tsx +102 -0
  145. package/src/components/primitives/Toolbar.stories.tsx +17 -0
  146. package/src/components/primitives/Toolbar.tsx +14 -0
  147. package/src/components/primitives/Tree.stories.tsx +124 -0
  148. package/src/components/primitives/Tree.tsx +96 -0
  149. package/src/components/primitives/frame.test.ts +12 -0
  150. package/src/components/primitives/frame.ts +14 -0
  151. package/src/components/primitives/icons.ts +61 -0
  152. package/src/components/primitives/tree-model.test.ts +99 -0
  153. package/src/components/primitives/tree-model.ts +137 -0
  154. package/src/components/quick-actions-editor.stories.tsx +54 -0
  155. package/src/components/quick-actions-editor.test.tsx +49 -0
  156. package/src/components/quick-actions-editor.tsx +150 -0
  157. package/src/components/session-tree.test.ts +134 -0
  158. package/src/components/session-tree.ts +108 -0
  159. package/src/components/stories-app.tsx +184 -0
  160. package/src/components/stories.test.tsx +66 -0
  161. package/src/components/story-fixtures.ts +102 -0
  162. package/src/components/story-tree.test.ts +100 -0
  163. package/src/components/story.ts +142 -0
  164. package/src/components/syntax-highlight.ts +110 -0
  165. package/src/components/terminal-pane.test.tsx +40 -0
  166. package/src/components/terminal-pane.ts +199 -0
  167. package/src/components/theme-context.stories.tsx +46 -0
  168. package/src/components/theme-context.tsx +37 -0
  169. package/src/components/truncate-text.ts +7 -0
  170. package/src/config.test.ts +461 -31
  171. package/src/config.ts +389 -57
  172. package/src/diff-fold.test.ts +78 -0
  173. package/src/diff-fold.ts +126 -0
  174. package/src/diff-hunk-curate.test.ts +315 -0
  175. package/src/diff-hunk-curate.ts +153 -0
  176. package/src/diff-intraline.test.ts +165 -0
  177. package/src/diff-intraline.ts +146 -0
  178. package/src/diff-syntax.test.ts +112 -0
  179. package/src/diff-syntax.ts +179 -0
  180. package/src/discussions.ts +79 -0
  181. package/src/editor.test.ts +149 -0
  182. package/src/editor.ts +139 -9
  183. package/src/ghostty-terminal.test.ts +85 -0
  184. package/src/ghostty-terminal.ts +199 -0
  185. package/src/herdr.test.ts +9 -34
  186. package/src/herdr.ts +25 -21
  187. package/src/index.ts +11 -0
  188. package/src/integrations.test.ts +41 -0
  189. package/src/integrations.ts +32 -0
  190. package/src/intent-dispatch.test.ts +698 -0
  191. package/src/intent-dispatch.ts +700 -0
  192. package/src/key-bindings.test.ts +156 -0
  193. package/src/key-bindings.ts +465 -0
  194. package/src/keymap.test.ts +611 -0
  195. package/src/keymap.ts +410 -0
  196. package/src/kitty-image.test.ts +66 -0
  197. package/src/kitty-image.ts +138 -0
  198. package/src/mark-runs.ts +120 -0
  199. package/src/prototype-browser.test.ts +56 -0
  200. package/src/prototype-browser.ts +307 -0
  201. package/src/prototype-pixels.tsx +35 -0
  202. package/src/pty.test.ts +81 -0
  203. package/src/pty.ts +245 -0
  204. package/src/ready-signal.test.ts +85 -0
  205. package/src/ready-signal.ts +52 -0
  206. package/src/run.ts +30 -9
  207. package/src/serve.test.ts +24 -12
  208. package/src/serve.ts +20 -9
  209. package/src/share.ts +215 -0
  210. package/src/skills.test.ts +74 -0
  211. package/src/skills.ts +68 -0
  212. package/src/slash-palette.test.ts +54 -0
  213. package/src/slash-palette.ts +104 -0
  214. package/src/split-diff.test.ts +110 -0
  215. package/src/split-diff.ts +149 -0
  216. package/src/test-support.test.ts +264 -0
  217. package/src/test-support.ts +355 -0
  218. package/src/theme-presets.test.ts +51 -0
  219. package/src/theme-presets.ts +214 -0
  220. package/src/theme.ts +84 -19
  221. package/src/thread-chords.test.ts +170 -0
  222. package/src/thread-chords.ts +169 -0
  223. package/src/thread-controller.curate.test.ts +438 -0
  224. package/src/thread-controller.live-diff.test.ts +199 -0
  225. package/src/thread-controller.share.test.ts +472 -0
  226. package/src/thread-controller.tree.test.ts +255 -0
  227. package/src/thread-controller.ts +1509 -0
  228. package/src/thread-selection.test.ts +210 -0
  229. package/src/thread-selection.ts +243 -0
  230. package/src/tree-view.test.ts +128 -0
  231. package/src/tree-view.ts +148 -0
  232. package/src/use-annotation-surface.tsx +851 -0
  233. package/src/use-changes-workbench.ts +153 -0
  234. package/src/use-frame-measure.ts +38 -0
  235. package/src/use-settings-dialog.tsx +274 -0
  236. package/src/use-terminal-virtualizer.ts +233 -0
  237. package/src/version.ts +5 -0
  238. package/src/view-diff.test.ts +66 -9
  239. package/src/view-diff.ts +231 -23
  240. package/src/view-plan.regression.test.ts +60 -0
  241. package/src/view-plan.test.ts +406 -0
  242. package/src/view-plan.ts +570 -0
  243. package/src/walk.test.ts +121 -0
  244. package/src/walk.ts +74 -0
  245. package/src/syntax.test.ts +0 -41
  246. package/src/syntax.ts +0 -88
  247. package/src/view.test.ts +0 -157
  248. package/src/view.ts +0 -312
@@ -0,0 +1,156 @@
1
+ /** Config keys resolve to per-layer actions, and status hints derive from getActiveKeys() so rebinds surface. */
2
+
3
+ import { describe, expect, test } from "bun:test";
4
+ import { DEFAULT_KEYS } from "./config";
5
+ import { KeyBindings } from "./key-bindings";
6
+
7
+ function bindings(
8
+ context: { overlay?: "none" | "walk" | "compose" | "submit"; spanMode?: boolean } = {},
9
+ ): KeyBindings {
10
+ const resolver = new KeyBindings(DEFAULT_KEYS);
11
+
12
+ resolver.setContext({ overlay: context.overlay ?? "none", spanMode: context.spanMode ?? false });
13
+
14
+ return resolver;
15
+ }
16
+
17
+ describe("grammar-layer resolution", () => {
18
+ const table: [name: string, shift: boolean, action: string | undefined][] = [
19
+ ["j", false, "down"],
20
+ ["down", false, "down"],
21
+ ["k", false, "up"],
22
+ ["g", false, "top"],
23
+ ["g", true, "bottom"],
24
+ ["v", false, "span"],
25
+ ["c", false, "comment"],
26
+ ["s", false, "split_diff"],
27
+ ["x", false, "cut"],
28
+ ["e", false, "edit"],
29
+ ["n", false, "next_annotation"],
30
+ ["p", false, "prev_annotation"],
31
+ ["backspace", false, "delete_annotation"],
32
+ ["return", false, "submit"],
33
+ ["enter", false, "submit"],
34
+ ["q", false, "quit"],
35
+ ["z", false, undefined],
36
+ ];
37
+
38
+ for (const [name, shift, action] of table) {
39
+ test(`${shift ? "shift+" : ""}${name} -> ${action}`, () => {
40
+ expect(bindings().resolveAction({ name, shift })).toBe(action!);
41
+ });
42
+ }
43
+ });
44
+
45
+ describe("mode layers own their keys", () => {
46
+ test("span mode resolves the span primitives, not the grammar", () => {
47
+ // Arrange
48
+ const resolver = bindings({ spanMode: true });
49
+
50
+ // Assert
51
+ expect(resolver.resolveAction({ name: "l", shift: false })).toBe("span_grow");
52
+ expect(resolver.resolveAction({ name: "c", shift: false })).toBe("span_comment");
53
+ expect(resolver.resolveAction({ name: "escape", shift: false })).toBe("span_cancel");
54
+ });
55
+
56
+ test("the compose overlay only knows save and cancel", () => {
57
+ // Arrange
58
+ const resolver = bindings({ overlay: "compose" });
59
+
60
+ // Assert
61
+ expect(resolver.resolveAction({ name: "return", shift: false })).toBe("save_compose");
62
+ expect(resolver.resolveAction({ name: "escape", shift: false })).toBe("cancel_overlay");
63
+ expect(resolver.resolveAction({ name: "j", shift: false })).toBeUndefined();
64
+ });
65
+
66
+ test("the submit overlay adds the verdict arrows", () => {
67
+ // Arrange
68
+ const resolver = bindings({ overlay: "submit" });
69
+
70
+ // Assert
71
+ expect(resolver.resolveAction({ name: "left", shift: false })).toBe("cycle_verdict_left");
72
+ expect(resolver.resolveAction({ name: "right", shift: false })).toBe("cycle_verdict_right");
73
+ expect(resolver.resolveAction({ name: "return", shift: false })).toBe("submit_verdict");
74
+ });
75
+
76
+ test("the walk overlay owns the bracket stepping and leaves the grammar keys dead", () => {
77
+ // Arrange
78
+ const resolver = bindings({ overlay: "walk" });
79
+
80
+ // Assert
81
+ expect(resolver.resolveAction({ name: "]", shift: false })).toBe("walk_next");
82
+ expect(resolver.resolveAction({ name: "[", shift: false })).toBe("walk_prev");
83
+ expect(resolver.resolveAction({ name: "escape", shift: false })).toBe("walk_leave");
84
+ expect(resolver.resolveAction({ name: "return", shift: false })).toBe("walk_submit");
85
+ expect(resolver.resolveAction({ name: "q", shift: false })).toBe("quit");
86
+ expect(resolver.resolveAction({ name: "j", shift: false })).toBeUndefined();
87
+ expect(resolver.resolveAction({ name: "c", shift: false })).toBeUndefined();
88
+ });
89
+ });
90
+
91
+ describe("getActiveKeys-generated status hints", () => {
92
+ test("normal mode hint matches the locked string", () => {
93
+ expect(bindings().statusHint("normal")).toBe(
94
+ "j/k move · v span · drag selects · c comment · x cut · e edit · n/p annotations · enter submit · q quit",
95
+ );
96
+ });
97
+
98
+ test("card-selected hint keeps the locked Cut label", () => {
99
+ expect(bindings().statusHint("card")).toBe(
100
+ "card · e edit · x Cut · n/p cards · esc deselect · enter submit",
101
+ );
102
+ });
103
+
104
+ test("span hint", () => {
105
+ expect(bindings({ spanMode: true }).statusHint("span")).toBe(
106
+ "span · l/h grow/shrink · w/b slide · $ end · c comment · esc",
107
+ );
108
+ });
109
+
110
+ test("compose hint", () => {
111
+ expect(bindings({ overlay: "compose" }).statusHint("compose")).toBe(
112
+ "typing · enter save · esc cancel",
113
+ );
114
+ });
115
+
116
+ test("submit hint", () => {
117
+ expect(bindings({ overlay: "submit" }).statusHint("submit")).toBe(
118
+ "decision ←/→ · enter submit · esc cancel",
119
+ );
120
+ });
121
+
122
+ test("walk hint", () => {
123
+ expect(bindings({ overlay: "walk" }).statusHint("walk")).toBe(
124
+ "walk · ] next (marks viewed) · [ back · esc leave walk · q quit",
125
+ );
126
+ });
127
+
128
+ test("read-only hint", () => {
129
+ expect(bindings().statusHint("read-only")).toBe(
130
+ "observer - read-only · j/k move · n/p annotations · q quit",
131
+ );
132
+ });
133
+
134
+ test("collaborator hint annotates but shows no plan edit or submit", () => {
135
+ // Arrange / Act
136
+ const hint = bindings().statusHint("collaborator");
137
+
138
+ // Assert
139
+ expect(hint).toBe("j/k move · v span · drag selects · c comment · n/p annotations · q quit");
140
+ expect(hint).not.toContain("cut");
141
+ expect(hint).not.toContain("edit");
142
+ expect(hint).not.toContain("submit");
143
+ });
144
+
145
+ test("a rebound key shows its real binding in the hint", () => {
146
+ // Arrange
147
+ const resolver = new KeyBindings({ ...DEFAULT_KEYS, comment: ["m"] });
148
+
149
+ resolver.setContext({ overlay: "none", spanMode: false });
150
+
151
+ // Assert
152
+ expect(resolver.resolveAction({ name: "m", shift: false })).toBe("comment");
153
+ expect(resolver.resolveAction({ name: "c", shift: false })).toBeUndefined();
154
+ expect(resolver.statusHint("normal")).toContain("m comment");
155
+ });
156
+ });
@@ -0,0 +1,465 @@
1
+ /**
2
+ * Binding resolution over @opentui/keymap: the loaded config keymap
3
+ * registers as layers (grammar, span, compose, submit, completion) on one
4
+ * Keymap instance, key events resolve to action names through its dispatch,
5
+ * and the status-line hints generate from getActiveKeys() - a rebound key
6
+ * shows its real binding in the hints. The pure intent reducer (keymap.ts)
7
+ * stays the dispatch layer beneath: this module answers "which action", the
8
+ * reducer answers "which intents".
9
+ */
10
+
11
+ import { Keymap, type KeymapEvent, type KeymapHost } from "@opentui/keymap";
12
+ import { registerDefaultKeys } from "@opentui/keymap/addons";
13
+ import * as v from "valibot";
14
+ import type { KeyState } from "./keymap";
15
+
16
+ export interface ResolvableKey {
17
+ name: string;
18
+ shift: boolean;
19
+ }
20
+
21
+ /** The layer-activation slice of the view state. */
22
+ export interface KeyLayerContext {
23
+ overlay: KeyState["overlay"];
24
+ spanMode: boolean;
25
+ }
26
+
27
+ /** Hint templates keyed by the same view states the status line shows. */
28
+ export type HintMode =
29
+ | "normal"
30
+ | "collaborator"
31
+ | "card"
32
+ | "span"
33
+ | "compose"
34
+ | "submit"
35
+ | "walk"
36
+ | "read-only";
37
+
38
+ /** Display glyph for the Agent-tab terminal detach chord (ctrl+], App-owned). */
39
+ export const AGENT_DETACH_HINT = "⌃]";
40
+
41
+ export interface CheatsheetEntry {
42
+ keys: string;
43
+ label: string;
44
+ }
45
+ export interface CheatsheetSection {
46
+ title: string;
47
+ entries: CheatsheetEntry[];
48
+ }
49
+
50
+ type HintEntry = { text: string } | { commands: string[]; label: string; labelFirst?: boolean };
51
+
52
+ const HINT_TEMPLATES: Record<HintMode, HintEntry[]> = {
53
+ normal: [
54
+ { commands: ["down", "up"], label: "move" },
55
+ { commands: ["span"], label: "span" },
56
+ { text: "drag selects" },
57
+ { commands: ["comment"], label: "comment" },
58
+ { commands: ["cut"], label: "cut" },
59
+ { commands: ["edit"], label: "edit" },
60
+ { commands: ["next_annotation", "prev_annotation"], label: "annotations" },
61
+ { commands: ["submit"], label: "submit" },
62
+ { commands: ["quit"], label: "quit" },
63
+ ],
64
+ // A share viewer: annotate and navigate, no plan edit / verdict submit.
65
+ collaborator: [
66
+ { commands: ["down", "up"], label: "move" },
67
+ { commands: ["span"], label: "span" },
68
+ { text: "drag selects" },
69
+ { commands: ["comment"], label: "comment" },
70
+ { commands: ["next_annotation", "prev_annotation"], label: "annotations" },
71
+ { commands: ["quit"], label: "quit" },
72
+ ],
73
+ card: [
74
+ { text: "card" },
75
+ { commands: ["edit"], label: "edit" },
76
+ { commands: ["cut"], label: "Cut" },
77
+ { commands: ["next_annotation", "prev_annotation"], label: "cards" },
78
+ { commands: ["deselect"], label: "deselect" },
79
+ { commands: ["submit"], label: "submit" },
80
+ ],
81
+ span: [
82
+ { text: "span" },
83
+ { commands: ["span_grow", "span_shrink"], label: "grow/shrink" },
84
+ { commands: ["span_slide_forward", "span_slide_back"], label: "slide" },
85
+ { commands: ["span_to_end"], label: "end" },
86
+ { commands: ["span_comment"], label: "comment" },
87
+ { commands: ["span_cancel"], label: "" },
88
+ ],
89
+ compose: [
90
+ { text: "typing" },
91
+ { commands: ["save_compose"], label: "save" },
92
+ { commands: ["cancel_overlay"], label: "cancel" },
93
+ ],
94
+ submit: [
95
+ {
96
+ commands: ["cycle_verdict_left", "cycle_verdict_right"],
97
+ label: "decision",
98
+ labelFirst: true,
99
+ },
100
+ { commands: ["submit_verdict"], label: "submit" },
101
+ { commands: ["cancel_submit"], label: "cancel" },
102
+ ],
103
+ walk: [
104
+ { text: "walk" },
105
+ { commands: ["walk_next"], label: "next (marks viewed)" },
106
+ { commands: ["walk_prev"], label: "back" },
107
+ { commands: ["walk_leave"], label: "leave walk" },
108
+ { commands: ["quit"], label: "quit" },
109
+ ],
110
+ "read-only": [
111
+ { text: "observer - read-only" },
112
+ { commands: ["down", "up"], label: "move" },
113
+ { commands: ["next_annotation", "prev_annotation"], label: "annotations" },
114
+ { commands: ["quit"], label: "quit" },
115
+ ],
116
+ };
117
+
118
+ /** Status-line glyphs for named keys. */
119
+ interface KeyGlyphs {
120
+ [key: string]: string;
121
+ }
122
+
123
+ const KEY_GLYPHS: KeyGlyphs = {
124
+ return: "enter",
125
+ enter: "enter",
126
+ escape: "esc",
127
+ left: "←",
128
+ right: "→",
129
+ backspace: "⌫",
130
+ down: "↓",
131
+ up: "↑",
132
+ };
133
+
134
+ function makeKeymapEvent(name: string, shift = false): KeymapEvent {
135
+ return {
136
+ name,
137
+ ctrl: false,
138
+ shift,
139
+ meta: false,
140
+ preventDefault: () => {},
141
+ stopPropagation: () => {},
142
+ propagationStopped: false,
143
+ };
144
+ }
145
+
146
+ class HeadlessKeymapHost implements KeymapHost<object, KeymapEvent> {
147
+ readonly metadata = {
148
+ platform: "unknown" as const,
149
+ primaryModifier: "unknown" as const,
150
+ modifiers: {
151
+ ctrl: "supported" as const,
152
+ shift: "supported" as const,
153
+ meta: "supported" as const,
154
+ super: "unknown" as const,
155
+ hyper: "unknown" as const,
156
+ },
157
+ };
158
+ readonly rootTarget = {};
159
+ readonly isDestroyed = false;
160
+ private readonly pressListeners = new Set<(event: KeymapEvent) => void>();
161
+
162
+ getFocusedTarget(): object | null {
163
+ return null;
164
+ }
165
+ getParentTarget(): object | null {
166
+ return null;
167
+ }
168
+ isTargetDestroyed(): boolean {
169
+ return false;
170
+ }
171
+ onKeyPress(listener: (event: KeymapEvent) => void): () => void {
172
+ this.pressListeners.add(listener);
173
+
174
+ return () => this.pressListeners.delete(listener);
175
+ }
176
+ onKeyRelease(): () => void {
177
+ return () => {};
178
+ }
179
+ onFocusChange(): () => void {
180
+ return () => {};
181
+ }
182
+ onTargetDestroy(): () => void {
183
+ return () => {};
184
+ }
185
+ createCommandEvent(): KeymapEvent {
186
+ return makeKeymapEvent("");
187
+ }
188
+ emitKeyPress(event: KeymapEvent): void {
189
+ for (const listener of this.pressListeners) listener(event);
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Terminal key events name shifted letters either "g"+shift or "G"; the
195
+ * keymap's canonical form is lowercase + the shift modifier. Non-letter
196
+ * single characters ("$", "0") already encode their shift in the character.
197
+ */
198
+ function normalizedEvent(key: ResolvableKey) {
199
+ if (key.name.length === 1) {
200
+ if (/[a-zA-Z]/.test(key.name)) {
201
+ return {
202
+ name: key.name.toLowerCase(),
203
+ shift: key.shift || key.name !== key.name.toLowerCase(),
204
+ };
205
+ }
206
+
207
+ return { name: key.name, shift: false };
208
+ }
209
+
210
+ return { name: key.name, shift: key.shift };
211
+ }
212
+
213
+ /** Config combos write shifted letters as uppercase; the parser wants shift+. */
214
+ function bindingKeyFor(combo: string): string {
215
+ if (combo.length === 1 && /[A-Z]/.test(combo)) return `shift+${combo.toLowerCase()}`;
216
+
217
+ return combo;
218
+ }
219
+
220
+ /** The grammar actions live in the base layer; these live in mode layers. */
221
+ const SPAN_COMMANDS: [key: string, command: string][] = [
222
+ ["l", "span_grow"],
223
+ ["h", "span_shrink"],
224
+ ["w", "span_slide_forward"],
225
+ ["b", "span_slide_back"],
226
+ ["$", "span_to_end"],
227
+ ["0", "span_to_start"],
228
+ ["c", "span_comment"],
229
+ ["escape", "span_cancel"],
230
+ ];
231
+
232
+ function isConditionMatcher(value: unknown): value is () => boolean {
233
+ return typeof value === "function";
234
+ }
235
+
236
+ export class KeyBindings {
237
+ private readonly host = new HeadlessKeymapHost();
238
+ private readonly keymap = new Keymap<object, KeymapEvent>(this.host);
239
+ private context: KeyLayerContext = { overlay: "none", spanMode: false };
240
+ private resolvedCommand: string | undefined;
241
+ private unregisterGrammarLayer: (() => void) | null = null;
242
+
243
+ constructor(keys: Record<string, string[]>) {
244
+ // the standard binding-string parser and event matcher
245
+ registerDefaultKeys(this.keymap);
246
+ // binding/layer `when` fields register runtime matchers, so getActiveKeys
247
+ // and dispatch only see the layer that owns the current mode
248
+ this.keymap.registerLayerFields({
249
+ when: (value, fieldContext) => {
250
+ if (isConditionMatcher(value)) fieldContext.activeWhen(value);
251
+ },
252
+ });
253
+ this.registerModeLayers();
254
+ this.setKeys(keys);
255
+ }
256
+
257
+ /** Rebuild the grammar layer from a (re)loaded config keymap. */
258
+ setKeys(keys: Record<string, string[]>): void {
259
+ this.unregisterGrammarLayer?.();
260
+ const bindings: { key: string; cmd: string }[] = [];
261
+ const commandNames = new Set<string>();
262
+
263
+ for (const [action, combos] of Object.entries(keys)) {
264
+ commandNames.add(action);
265
+ for (const combo of combos) bindings.push({ key: bindingKeyFor(combo), cmd: action });
266
+ }
267
+ // deselect is a hardwired escape in the plan grammar; hints show it
268
+ commandNames.add("deselect");
269
+ bindings.push({ key: "escape", cmd: "deselect" });
270
+ this.unregisterGrammarLayer = this.keymap.registerLayer({
271
+ priority: 0,
272
+ when: () => this.context.overlay === "none" && !this.context.spanMode,
273
+ bindings,
274
+ commands: [...commandNames].map((name) => ({
275
+ name,
276
+ run: () => {
277
+ this.resolvedCommand = name;
278
+
279
+ return true;
280
+ },
281
+ })),
282
+ });
283
+ }
284
+
285
+ private registerModeLayers(): void {
286
+ const record = (name: string) => ({
287
+ name,
288
+ run: () => {
289
+ this.resolvedCommand = name;
290
+
291
+ return true;
292
+ },
293
+ });
294
+
295
+ this.keymap.registerLayer({
296
+ priority: 10,
297
+ when: () => this.context.overlay === "none" && this.context.spanMode,
298
+ bindings: SPAN_COMMANDS.map(([key, command]) => ({ key, cmd: command })),
299
+ commands: SPAN_COMMANDS.map(([, command]) => record(command)),
300
+ });
301
+ // the walk wizard: prev/next stepping, leave, and submit from the end card
302
+ this.keymap.registerLayer({
303
+ priority: 20,
304
+ when: () => this.context.overlay === "walk",
305
+ bindings: [
306
+ { key: "]", cmd: "walk_next" },
307
+ { key: "[", cmd: "walk_prev" },
308
+ { key: "escape", cmd: "walk_leave" },
309
+ { key: "return", cmd: "walk_submit" },
310
+ { key: "enter", cmd: "walk_submit" },
311
+ { key: "q", cmd: "quit" },
312
+ ],
313
+ commands: [
314
+ record("walk_next"),
315
+ record("walk_prev"),
316
+ record("walk_leave"),
317
+ record("walk_submit"),
318
+ record("quit"),
319
+ ],
320
+ });
321
+ this.keymap.registerLayer({
322
+ priority: 20,
323
+ when: () => this.context.overlay === "compose",
324
+ bindings: [
325
+ { key: "return", cmd: "save_compose" },
326
+ { key: "enter", cmd: "save_compose" },
327
+ { key: "escape", cmd: "cancel_overlay" },
328
+ ],
329
+ commands: [record("save_compose"), record("cancel_overlay")],
330
+ });
331
+ this.keymap.registerLayer({
332
+ priority: 20,
333
+ when: () => this.context.overlay === "submit",
334
+ bindings: [
335
+ { key: "return", cmd: "submit_verdict" },
336
+ { key: "enter", cmd: "submit_verdict" },
337
+ { key: "left", cmd: "cycle_verdict_left" },
338
+ { key: "right", cmd: "cycle_verdict_right" },
339
+ { key: "escape", cmd: "cancel_submit" },
340
+ ],
341
+ commands: [
342
+ record("submit_verdict"),
343
+ record("cycle_verdict_left"),
344
+ record("cycle_verdict_right"),
345
+ record("cancel_submit"),
346
+ ],
347
+ });
348
+ this.keymap.registerLayer({
349
+ priority: 20,
350
+ when: () =>
351
+ this.context.overlay === "completion-prompt" ||
352
+ this.context.overlay === "completion-counting",
353
+ bindings: [
354
+ { key: "return", cmd: "finish_review" },
355
+ { key: "enter", cmd: "finish_review" },
356
+ { key: "q", cmd: "finish_review" },
357
+ { key: "a", cmd: "opt_in_auto_close" },
358
+ { key: "escape", cmd: "dismiss_completion" },
359
+ ],
360
+ commands: [
361
+ record("finish_review"),
362
+ record("opt_in_auto_close"),
363
+ record("dismiss_completion"),
364
+ ],
365
+ });
366
+ }
367
+
368
+ setContext(context: KeyLayerContext): void {
369
+ this.context = context;
370
+ }
371
+
372
+ /**
373
+ * Resolve a key event to its command name through the keymap's dispatch.
374
+ * In the base grammar layer the command IS the config action name, so the
375
+ * result feeds straight into the intent reducer.
376
+ */
377
+ resolveAction(key: ResolvableKey): string | undefined {
378
+ const normalized = normalizedEvent(key);
379
+
380
+ this.resolvedCommand = undefined;
381
+ this.host.emitKeyPress(makeKeymapEvent(normalized.name, normalized.shift));
382
+
383
+ return this.resolvedCommand;
384
+ }
385
+
386
+ /** First active key display for a command, mapped through the glyph table. */
387
+ private keyDisplayFor(command: string): string | null {
388
+ for (const activeKey of this.keymap.getActiveKeys({ includeBindings: true })) {
389
+ const parsedCommand = v.safeParse(v.string(), activeKey.command);
390
+ const bindingCommand = parsedCommand.success
391
+ ? parsedCommand.output
392
+ : activeKey.bindings?.[0]?.command;
393
+
394
+ if (bindingCommand !== command) continue;
395
+
396
+ return KEY_GLYPHS[activeKey.stroke.name] ?? activeKey.display;
397
+ }
398
+
399
+ return null;
400
+ }
401
+
402
+ /** The status-line hint for a view state, generated from the active keys. */
403
+ statusHint(mode: HintMode): string {
404
+ const fragments: string[] = [];
405
+
406
+ for (const entry of HINT_TEMPLATES[mode]) {
407
+ if ("text" in entry) {
408
+ fragments.push(entry.text);
409
+ continue;
410
+ }
411
+ const displays = entry.commands
412
+ .map((command) => this.keyDisplayFor(command))
413
+ .filter((display): display is string => display !== null);
414
+
415
+ if (!displays.length) continue;
416
+ const keyPart = displays.join("/");
417
+
418
+ if (!entry.label) fragments.push(keyPart);
419
+ else
420
+ fragments.push(
421
+ entry.labelFirst ? `${entry.label} ${keyPart}` : `${keyPart} ${entry.label}`,
422
+ );
423
+ }
424
+
425
+ return fragments.join(" · ");
426
+ }
427
+
428
+ /**
429
+ * The full keybinding cheatsheet, grouped by mode. Each section sets the
430
+ * layer context so getActiveKeys resolves that mode's real bindings, then
431
+ * restores it - a rebound key shows its live binding here too.
432
+ */
433
+ cheatsheet(): CheatsheetSection[] {
434
+ const saved = this.context;
435
+ const build = (context: KeyLayerContext, mode: HintMode, title: string): CheatsheetSection => {
436
+ this.context = context;
437
+ const entries: CheatsheetEntry[] = [];
438
+
439
+ for (const entry of HINT_TEMPLATES[mode]) {
440
+ if ("text" in entry || !entry.label) continue;
441
+ const keys = entry.commands
442
+ .map((command) => this.keyDisplayFor(command))
443
+ .filter((display): display is string => display !== null)
444
+ .join(" / ");
445
+
446
+ if (keys) entries.push({ keys, label: entry.label });
447
+ }
448
+
449
+ return { title, entries };
450
+ };
451
+ const sections = [
452
+ build({ overlay: "none", spanMode: false }, "normal", "Review"),
453
+ build({ overlay: "none", spanMode: true }, "span", "Selection"),
454
+ build({ overlay: "submit", spanMode: false }, "submit", "Submit"),
455
+ build({ overlay: "walk", spanMode: false }, "walk", "Walk"),
456
+ // The Agent-tab terminal chord is owned by App (not the rebindable keymap),
457
+ // so it is listed statically rather than resolved from the active keys.
458
+ { title: "Agent terminal", entries: [{ keys: AGENT_DETACH_HINT, label: "detach" }] },
459
+ ];
460
+
461
+ this.context = saved;
462
+
463
+ return sections.filter((section) => section.entries.length > 0);
464
+ }
465
+ }