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