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 "rightPanel.toggleMaximized",
65 "rightPanel.close",
66 "pullRequest.copyNumber",
67 "diff.toggle",
68 "preview.toggle",
69 "preview.refresh",
70 "preview.focusUrl",
71 "preview.zoomIn",
72 "preview.zoomOut",
73 "preview.resetZoom",
74 "commandPalette.toggle",
75 "filePicker.toggle",
76 "projectSearch.toggle",
77 "themeEditor.toggle",
78 "composer.stash",
79 "composer.host",
80 "composer.effort",
81 "composer.mode",
82 "composer.workspace",
83 "composer.previousWorktree",
84 "composer.branch",
85 "chat.new",
86 "chat.newLocal",
87 "editor.openFavorite",
88 ...MODEL_PICKER_KEYBINDING_COMMANDS,
89 ...THREAD_KEYBINDING_COMMANDS,
90] as const;
91
92export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
93 Schema.Literal("script."),
94 Schema.NonEmptyString.check(
95 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
96 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
97 ),
98 Schema.Literal(".run"),
99]);
100
101export const KeybindingCommand = Schema.Union([
102 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
103 SCRIPT_RUN_COMMAND_PATTERN,
104]);
105export type KeybindingCommand = typeof KeybindingCommand.Type;
106
107export const KeybindingValue = TrimmedString.check(
108 Schema.isMinLength(1),
109 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
110);
111
112export const KeybindingWhen = TrimmedString.check(
113 Schema.isMinLength(1),
114 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
115);
116export const KeybindingRule = Schema.Struct({
117 key: KeybindingValue,
118 command: KeybindingCommand,
119 when: Schema.optional(KeybindingWhen),
120});
121export type KeybindingRule = typeof KeybindingRule.Type;
122
123export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
124 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
125);
126export type KeybindingsConfig = typeof KeybindingsConfig.Type;
127
128export const KeybindingShortcut = Schema.Struct({
129 key: KeybindingValue,
130 metaKey: Schema.Boolean,
131 ctrlKey: Schema.Boolean,
132 shiftKey: Schema.Boolean,
133 altKey: Schema.Boolean,
134 modKey: Schema.Boolean,
135});
136export type KeybindingShortcut = typeof KeybindingShortcut.Type;
137
138const KeybindingWhenNodeRef = Schema.suspend(
139 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
140);
141export const KeybindingWhenNode = Schema.Union([
142 Schema.Struct({
143 type: Schema.Literal("identifier"),
144 name: Schema.NonEmptyString,
145 }),
146 Schema.Struct({
147 type: Schema.Literal("not"),
148 node: KeybindingWhenNodeRef,
149 }),
150 Schema.Struct({
151 type: Schema.Literal("and"),
152 left: KeybindingWhenNodeRef,
153 right: KeybindingWhenNodeRef,
154 }),
155 Schema.Struct({
156 type: Schema.Literal("or"),
157 left: KeybindingWhenNodeRef,
158 right: KeybindingWhenNodeRef,
159 }),
160]);
161export type KeybindingWhenNode =
162 | { type: "identifier"; name: string }
163 | { type: "not"; node: KeybindingWhenNode }
164 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
165 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
166
167export const ResolvedKeybindingRule = Schema.Struct({
168 command: KeybindingCommand,
169 shortcut: KeybindingShortcut,
170 whenAst: Schema.optional(KeybindingWhenNode),
171}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
172export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
173
174/**
175 * The command set grows over time, so a client may receive rules it cannot
176 * represent (a command or `when` node added after that client shipped).
177 * Decoding drops those rules instead of failing the whole payload —
178 * rejecting the config would take down the connection over a shortcut the
179 * client couldn't dispatch anyway.
180 */
181export const ResolvedKeybindingsConfig = ForwardCompatibleArray(ResolvedKeybindingRule).check(
182 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
183);
184export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
185
186export class KeybindingsConfigError extends Schema.TaggedError<KeybindingsConfigError>()(
187 "KeybindingsConfigParseError",
188 {
189 configPath: Schema.String,
190 detail: Schema.String,
191 cause: Schema.optional(Schema.Defect()),
192 },
193) {
194 override get message(): string {
195 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
196 }
197}