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