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