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