Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keybindings.ts

226 lines6.5 KB

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