Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { ForwardCompatibleArray, TrimmedString } from "./baseSchemas.ts";
3
4export const MAX_KEYBINDING_VALUE_LENGTH = 64;
5const MAX_KEYBINDING_WHEN_LENGTH = 256;
6export const MAX_WHEN_EXPRESSION_DEPTH = 64;
7export const MAX_SCRIPT_ID_LENGTH = 24;
8export const MAX_KEYBINDINGS_COUNT = 256;
9
10export const THREAD_JUMP_KEYBINDING_COMMANDS = [
11 "thread.jump.1",
12 "thread.jump.2",
13 "thread.jump.3",
14 "thread.jump.4",
15 "thread.jump.5",
16 "thread.jump.6",
17 "thread.jump.7",
18 "thread.jump.8",
19 "thread.jump.9",
20] as const;
21export type ThreadJumpKeybindingCommand = (typeof THREAD_JUMP_KEYBINDING_COMMANDS)[number];
22
23export const MODEL_PICKER_JUMP_KEYBINDING_COMMANDS = [
24 "modelPicker.jump.1",
25 "modelPicker.jump.2",
26 "modelPicker.jump.3",
27 "modelPicker.jump.4",
28 "modelPicker.jump.5",
29 "modelPicker.jump.6",
30 "modelPicker.jump.7",
31 "modelPicker.jump.8",
32 "modelPicker.jump.9",
33] as const;
34export type ModelPickerJumpKeybindingCommand =
35 (typeof MODEL_PICKER_JUMP_KEYBINDING_COMMANDS)[number];
36
37const THREAD_KEYBINDING_COMMANDS = [
38 "thread.stop",
39 "thread.previous",
40 "thread.next",
41 "thread.copyReference",
42 "thread.settle",
43 "thread.pin",
44 ...THREAD_JUMP_KEYBINDING_COMMANDS,
45] as const;
46export type ThreadKeybindingCommand = (typeof THREAD_KEYBINDING_COMMANDS)[number];
47
48const MODEL_PICKER_KEYBINDING_COMMANDS = [
49 "modelPicker.toggle",
50 "modelPicker.previousProvider",
51 "modelPicker.nextProvider",
52 ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS,
53] as const;
54export type ModelPickerKeybindingCommand = (typeof MODEL_PICKER_KEYBINDING_COMMANDS)[number];
55
56export const STATIC_KEYBINDING_COMMANDS = [
57 "sidebar.toggle",
58 "terminal.toggle",
59 "terminal.split",
60 "terminal.splitVertical",
61 "terminal.new",
62 "terminal.close",
63 "rightPanel.toggle",
64 "threadPanel.toggle",
65 "rightPanel.toggleMaximized",
66 "rightPanel.close",
67 "pullRequest.copyNumber",
68 "diff.toggle",
69 "preview.toggle",
70 "preview.refresh",
71 "preview.focusUrl",
72 "preview.zoomIn",
73 "preview.zoomOut",
74 "preview.resetZoom",
75 "commandPalette.toggle",
76 "filePicker.toggle",
77 "projectSearch.toggle",
78 "themeEditor.toggle",
79 "composer.stash",
80 "composer.host",
81 "composer.effort",
82 "composer.mode",
83 "composer.workspace",
84 "composer.previousWorktree",
85 "composer.branch",
86 "chat.new",
87 "chat.newLocal",
88 "editor.openFavorite",
89 ...MODEL_PICKER_KEYBINDING_COMMANDS,
90 ...THREAD_KEYBINDING_COMMANDS,
91] as const;
92
93export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
94 Schema.Literal("script."),
95 Schema.NonEmptyString.check(
96 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
97 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
98 ),
99 Schema.Literal(".run"),
100]);
101
102export const KeybindingCommand = Schema.Union([
103 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
104 SCRIPT_RUN_COMMAND_PATTERN,
105]);
106export type KeybindingCommand = typeof KeybindingCommand.Type;
107
108export const KeybindingValue = TrimmedString.check(
109 Schema.isMinLength(1),
110 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
111);
112
113export const KeybindingWhen = TrimmedString.check(
114 Schema.isMinLength(1),
115 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
116);
117export const KeybindingRule = Schema.Struct({
118 key: KeybindingValue,
119 command: KeybindingCommand,
120 when: Schema.optional(KeybindingWhen),
121});
122export type KeybindingRule = typeof KeybindingRule.Type;
123
124export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
125 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
126);
127export type KeybindingsConfig = typeof KeybindingsConfig.Type;
128
129export const KeybindingShortcut = Schema.Struct({
130 key: KeybindingValue,
131 metaKey: Schema.Boolean,
132 ctrlKey: Schema.Boolean,
133 shiftKey: Schema.Boolean,
134 altKey: Schema.Boolean,
135 modKey: Schema.Boolean,
136});
137export type KeybindingShortcut = typeof KeybindingShortcut.Type;
138
139const KeybindingWhenNodeRef = Schema.suspend(
140 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
141);
142export const KeybindingWhenNode = Schema.Union([
143 Schema.Struct({
144 type: Schema.Literal("identifier"),
145 name: Schema.NonEmptyString,
146 }),
147 Schema.Struct({
148 type: Schema.Literal("not"),
149 node: KeybindingWhenNodeRef,
150 }),
151 Schema.Struct({
152 type: Schema.Literal("and"),
153 left: KeybindingWhenNodeRef,
154 right: KeybindingWhenNodeRef,
155 }),
156 Schema.Struct({
157 type: Schema.Literal("or"),
158 left: KeybindingWhenNodeRef,
159 right: KeybindingWhenNodeRef,
160 }),
161]);
162export type KeybindingWhenNode =
163 | { type: "identifier"; name: string }
164 | { type: "not"; node: KeybindingWhenNode }
165 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
166 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
167
168export const ResolvedKeybindingRule = Schema.Struct({
169 command: KeybindingCommand,
170 shortcut: KeybindingShortcut,
171 whenAst: Schema.optional(KeybindingWhenNode),
172}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
173export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
174
175/**
176 * The command set grows over time, so a client may receive rules it cannot
177 * represent (a command or `when` node added after that client shipped).
178 * Decoding drops those rules instead of failing the whole payload —
179 * rejecting the config would take down the connection over a shortcut the
180 * client couldn't dispatch anyway.
181 */
182export const ResolvedKeybindingsConfig = ForwardCompatibleArray(ResolvedKeybindingRule).check(
183 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
184);
185export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
186
187export class KeybindingsConfigError extends Schema.TaggedError<KeybindingsConfigError>()(
188 "KeybindingsConfigParseError",
189 {
190 configPath: Schema.String,
191 detail: Schema.String,
192 cause: Schema.optional(Schema.Defect()),
193 },
194) {
195 override get message(): string {
196 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
197 }
198}