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.sendAndNewThread",
91 "composer.host",
92 "composer.cycleHost",
93 "composer.effort",
94 "composer.mode",
95 "composer.workspace",
96 "composer.previousWorktree",
97 "composer.branch",
98 "chat.new",
99 "chat.newLocal",
100 "chat.newWithoutProject",
101 "editor.openFavorite",
102 "usage.cost",
103 "usage.tokens",
104 "usage.limits",
105 "usage.period.day",
106 "usage.period.week",
107 "usage.period.month",
108 "usage.period.quarter",
109 ...MODEL_PICKER_KEYBINDING_COMMANDS,
110 ...THREAD_KEYBINDING_COMMANDS,
111] as const;
112
113export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
114 Schema.Literal("script."),
115 Schema.NonEmptyString.check(
116 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
117 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
118 ),
119 Schema.Literal(".run"),
120]);
121
122export const KeybindingCommand = Schema.Union([
123 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
124 SCRIPT_RUN_COMMAND_PATTERN,
125]);
126export type KeybindingCommand = typeof KeybindingCommand.Type;
127
128export const KeybindingValue = TrimmedString.check(
129 Schema.isMinLength(1),
130 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
131);
132
133export const KeybindingWhen = TrimmedString.check(
134 Schema.isMinLength(1),
135 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
136);
137export const KeybindingRule = Schema.Struct({
138 key: KeybindingValue,
139 command: KeybindingCommand,
140 when: Schema.optional(KeybindingWhen),
141});
142export type KeybindingRule = typeof KeybindingRule.Type;
143
144export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
145 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
146);
147export type KeybindingsConfig = typeof KeybindingsConfig.Type;
148
149export const KeybindingShortcut = Schema.Struct({
150 key: KeybindingValue,
151 metaKey: Schema.Boolean,
152 ctrlKey: Schema.Boolean,
153 shiftKey: Schema.Boolean,
154 altKey: Schema.Boolean,
155 modKey: Schema.Boolean,
156});
157export type KeybindingShortcut = typeof KeybindingShortcut.Type;
158
159const KeybindingWhenNodeRef = Schema.suspend(
160 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
161);
162export const KeybindingWhenNode = Schema.Union([
163 Schema.Struct({
164 type: Schema.Literal("identifier"),
165 name: Schema.NonEmptyString,
166 }),
167 Schema.Struct({
168 type: Schema.Literal("not"),
169 node: KeybindingWhenNodeRef,
170 }),
171 Schema.Struct({
172 type: Schema.Literal("and"),
173 left: KeybindingWhenNodeRef,
174 right: KeybindingWhenNodeRef,
175 }),
176 Schema.Struct({
177 type: Schema.Literal("or"),
178 left: KeybindingWhenNodeRef,
179 right: KeybindingWhenNodeRef,
180 }),
181]);
182export type KeybindingWhenNode =
183 | { type: "identifier"; name: string }
184 | { type: "not"; node: KeybindingWhenNode }
185 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
186 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
187
188export const ResolvedKeybindingRule = Schema.Struct({
189 command: KeybindingCommand,
190 shortcut: KeybindingShortcut,
191 whenAst: Schema.optional(KeybindingWhenNode),
192}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
193export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
194
195/**
196 * The command set grows over time, so a client may receive rules it cannot
197 * represent (a command or `when` node added after that client shipped).
198 * Decoding drops those rules instead of failing the whole payload —
199 * rejecting the config would take down the connection over a shortcut the
200 * client couldn't dispatch anyway.
201 */
202export const ResolvedKeybindingsConfig = ForwardCompatibleArray(ResolvedKeybindingRule).check(
203 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
204);
205export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
206
207export class KeybindingsConfigError extends Schema.TaggedError<KeybindingsConfigError>()(
208 "KeybindingsConfigParseError",
209 {
210 configPath: Schema.String,
211 detail: Schema.String,
212 cause: Schema.optional(Schema.Defect()),
213 },
214) {
215 override get message(): string {
216 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
217 }
218}