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