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