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.steerQueuedMessage",
40 "thread.editQueuedMessage",
41 "thread.previous",
42 "thread.next",
43 "thread.copyReference",
44 "thread.settle",
45 "thread.pin",
46 "thread.undo",
47 ...THREAD_JUMP_KEYBINDING_COMMANDS,
48] as const;
49export type ThreadKeybindingCommand = (typeof THREAD_KEYBINDING_COMMANDS)[number];
50
51const MODEL_PICKER_KEYBINDING_COMMANDS = [
52 "modelPicker.toggle",
53 "modelPicker.previousProvider",
54 "modelPicker.nextProvider",
55 ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS,
56] as const;
57export type ModelPickerKeybindingCommand = (typeof MODEL_PICKER_KEYBINDING_COMMANDS)[number];
58
59export const STATIC_KEYBINDING_COMMANDS = [
60 "sidebar.toggle",
61 "navigation.back",
62 "navigation.forward",
63 "terminal.toggle",
64 "terminal.split",
65 "terminal.splitVertical",
66 "terminal.new",
67 "terminal.close",
68 "rightPanel.toggle",
69 "threadPanel.toggle",
70 "rightPanel.toggleMaximized",
71 "rightPanel.close",
72 "pullRequest.copyNumber",
73 "diff.toggle",
74 "preview.toggle",
75 "preview.refresh",
76 "preview.focusUrl",
77 "preview.zoomIn",
78 "preview.zoomOut",
79 "preview.resetZoom",
80 "commandPalette.toggle",
81 "filePicker.toggle",
82 "projectSearch.toggle",
83 "usage.open",
84 "theme.select",
85 "appearance.cycle",
86 "themeEditor.toggle",
87 "composer.stash",
88 "composer.sendAlternate",
89 "composer.sendBackground",
90 "composer.host",
91 "composer.effort",
92 "composer.mode",
93 "composer.workspace",
94 "composer.previousWorktree",
95 "composer.branch",
96 "chat.new",
97 "chat.newLocal",
98 "chat.newWithoutProject",
99 "editor.openFavorite",
100 "usage.cost",
101 "usage.tokens",
102 "usage.limits",
103 "usage.period.day",
104 "usage.period.week",
105 "usage.period.month",
106 "usage.period.quarter",
107 ...MODEL_PICKER_KEYBINDING_COMMANDS,
108 ...THREAD_KEYBINDING_COMMANDS,
109] as const;
110
111export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
112 Schema.Literal("script."),
113 Schema.NonEmptyString.check(
114 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
115 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
116 ),
117 Schema.Literal(".run"),
118]);
119
120export const KeybindingCommand = Schema.Union([
121 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
122 SCRIPT_RUN_COMMAND_PATTERN,
123]);
124export type KeybindingCommand = typeof KeybindingCommand.Type;
125
126export const KeybindingValue = TrimmedString.check(
127 Schema.isMinLength(1),
128 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
129);
130
131export const KeybindingWhen = TrimmedString.check(
132 Schema.isMinLength(1),
133 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
134);
135export const KeybindingRule = Schema.Struct({
136 key: KeybindingValue,
137 command: KeybindingCommand,
138 when: Schema.optional(KeybindingWhen),
139});
140export type KeybindingRule = typeof KeybindingRule.Type;
141
142export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
143 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
144);
145export type KeybindingsConfig = typeof KeybindingsConfig.Type;
146
147export const KeybindingShortcut = Schema.Struct({
148 key: KeybindingValue,
149 metaKey: Schema.Boolean,
150 ctrlKey: Schema.Boolean,
151 shiftKey: Schema.Boolean,
152 altKey: Schema.Boolean,
153 modKey: Schema.Boolean,
154});
155export type KeybindingShortcut = typeof KeybindingShortcut.Type;
156
157const KeybindingWhenNodeRef = Schema.suspend(
158 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
159);
160export const KeybindingWhenNode = Schema.Union([
161 Schema.Struct({
162 type: Schema.Literal("identifier"),
163 name: Schema.NonEmptyString,
164 }),
165 Schema.Struct({
166 type: Schema.Literal("not"),
167 node: KeybindingWhenNodeRef,
168 }),
169 Schema.Struct({
170 type: Schema.Literal("and"),
171 left: KeybindingWhenNodeRef,
172 right: KeybindingWhenNodeRef,
173 }),
174 Schema.Struct({
175 type: Schema.Literal("or"),
176 left: KeybindingWhenNodeRef,
177 right: KeybindingWhenNodeRef,
178 }),
179]);
180export type KeybindingWhenNode =
181 | { type: "identifier"; name: string }
182 | { type: "not"; node: KeybindingWhenNode }
183 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
184 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
185
186export const ResolvedKeybindingRule = Schema.Struct({
187 command: KeybindingCommand,
188 shortcut: KeybindingShortcut,
189 whenAst: Schema.optional(KeybindingWhenNode),
190}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
191export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
192
193/**
194 * The command set grows over time, so a client may receive rules it cannot
195 * represent (a command or `when` node added after that client shipped).
196 * Decoding drops those rules instead of failing the whole payload —
197 * rejecting the config would take down the connection over a shortcut the
198 * client couldn't dispatch anyway.
199 */
200export const ResolvedKeybindingsConfig = ForwardCompatibleArray(ResolvedKeybindingRule).check(
201 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
202);
203export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
204
205export class KeybindingsConfigError extends Schema.TaggedError<KeybindingsConfigError>()(
206 "KeybindingsConfigParseError",
207 {
208 configPath: Schema.String,
209 detail: Schema.String,
210 cause: Schema.optional(Schema.Defect()),
211 },
212) {
213 override get message(): string {
214 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
215 }
216}