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