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