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