forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard-actions.ts
More file actions
389 lines (343 loc) · 10.9 KB
/
Copy pathkeyboard-actions.ts
File metadata and controls
389 lines (343 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import { getInputModeConfig, type InputMode } from './input-modes'
import type { KeyEvent } from '@opentui/core'
/**
* State needed to determine keyboard actions in chat input contexts.
* This is a focused subset of app state relevant to keyboard handling.
*/
export type ChatKeyboardState = {
// Input state
inputMode: InputMode
inputValue: string
cursorPosition: number
// Stream state
isStreaming: boolean
isWaitingForResponse: boolean
// Feedback mode
feedbackMode: boolean
// Focus state
focusedAgentId: string | null
// Menu state
slashMenuActive: boolean
mentionMenuActive: boolean
slashSelectedIndex: number
agentSelectedIndex: number
slashMatchesLength: number
totalMentionMatches: number
disableSlashSuggestions: boolean
// Queue state
queuePaused: boolean
queuedCount: number
// History navigation state
historyNavUpEnabled: boolean
historyNavDownEnabled: boolean
// Exit handler state
nextCtrlCWillExit: boolean
}
/**
* All possible keyboard actions for chat input.
* Each action represents a distinct behavior to be handled.
*/
export type ChatKeyboardAction =
// Mode actions
| { type: 'exit-input-mode' }
| { type: 'exit-feedback-mode' }
| { type: 'clear-feedback-input' }
// Input actions
| { type: 'clear-input' }
| { type: 'backspace-exit-mode' }
// Stream actions
| { type: 'interrupt-stream' }
// Menu navigation
| { type: 'slash-menu-down' }
| { type: 'slash-menu-up' }
| { type: 'slash-menu-tab' }
| { type: 'slash-menu-shift-tab' }
| { type: 'slash-menu-select' }
| { type: 'slash-menu-complete' }
| { type: 'mention-menu-down' }
| { type: 'mention-menu-up' }
| { type: 'mention-menu-tab' }
| { type: 'mention-menu-shift-tab' }
| { type: 'mention-menu-select' }
| { type: 'mention-menu-complete' }
| { type: 'open-file-menu-with-tab' }
// History navigation
| { type: 'history-up' }
| { type: 'history-down' }
// Agent mode
| { type: 'toggle-agent-mode' }
| { type: 'unfocus-agent' }
// Toggle all collapsed/expanded
| { type: 'toggle-all' }
// Queue actions
| { type: 'clear-queue' }
// Exit actions
| { type: 'exit-app-warning' }
| { type: 'exit-app' }
// Bash history navigation
| { type: 'bash-history-up' }
| { type: 'bash-history-down' }
// Scroll actions
| { type: 'scroll-up' }
| { type: 'scroll-down' }
// Paste action (dispatcher checks clipboard content to route to image or text handler)
| { type: 'paste' }
// Out of credits action
| { type: 'open-buy-credits' }
// No action needed
| { type: 'none' }
const hasModifier = (key: KeyEvent) =>
Boolean(key.ctrl || key.meta || key.option)
/**
* Pure function that resolves a keyboard action based on key event and state.
* This implements the priority-based keyboard handling logic.
*/
export function resolveChatKeyboardAction(
key: KeyEvent,
state: ChatKeyboardState,
): ChatKeyboardAction {
const isEscape = key.name === 'escape'
const isCtrlC = key.ctrl && key.name === 'c'
const isCtrlV = key.ctrl && key.name === 'v'
const isBackspace = key.name === 'backspace'
const isUp = key.name === 'up' && !hasModifier(key)
const isDown = key.name === 'down' && !hasModifier(key)
const isTab = key.name === 'tab' && !hasModifier(key)
const isShiftTab =
key.name === 'tab' && key.shift && !key.ctrl && !key.meta && !key.option
const isEnter =
(key.name === 'return' || key.name === 'enter') &&
!key.shift &&
!hasModifier(key)
const isPageUp = key.name === 'pageup' && !hasModifier(key)
const isPageDown = key.name === 'pagedown' && !hasModifier(key)
// Priority 0: Out of credits mode - Enter opens buy credits page
if (state.inputMode === 'outOfCredits') {
if (isEnter) {
return { type: 'open-buy-credits' }
}
// Allow Escape or Ctrl+C to exit out-of-credits mode (return to normal input)
if (isEscape || isCtrlC) {
return { type: 'exit-input-mode' }
}
// Block most other inputs in this mode
return { type: 'none' }
}
// Priority 1: Feedback mode - block global keys except Escape/Ctrl-C/Ctrl-V
if (state.feedbackMode) {
if (isEscape) {
return { type: 'exit-feedback-mode' }
}
if (isCtrlC) {
return state.inputValue.length === 0
? { type: 'exit-feedback-mode' }
: { type: 'clear-feedback-input' }
}
if (isCtrlV) {
return { type: 'paste' }
}
return { type: 'none' }
}
// Priority 2: Non-default input mode escape
// Escape should exit the current mode BEFORE interrupting streams
// Exception: modes with blockKeyboardExit cannot be escaped
const modeConfig = getInputModeConfig(state.inputMode)
if (isEscape && state.inputMode !== 'default' && !modeConfig.blockKeyboardExit) {
return { type: 'exit-input-mode' }
}
// Priority 3: Clear input with ctrl-c when there's text
if (isCtrlC && state.inputValue.trim().length > 0) {
return { type: 'clear-input' }
}
// Priority 4: Interrupt streaming
if (
(isEscape || isCtrlC) &&
(state.isStreaming || state.isWaitingForResponse)
) {
return { type: 'interrupt-stream' }
}
// Priority 5: Backspace at position 0 exits non-default mode
// Exception: modes with blockKeyboardExit cannot be exited via keyboard
if (
isBackspace &&
state.cursorPosition === 0 &&
state.inputMode !== 'default' &&
!modeConfig.blockKeyboardExit &&
state.inputValue.length === 0
) {
return { type: 'backspace-exit-mode' }
}
// Priority 6: Slash menu navigation (when active and not disabled)
// Skip menu navigation for Up/Down if history navigation is enabled (user is paging through history)
if (
state.slashMenuActive &&
state.slashMatchesLength > 0 &&
!state.disableSlashSuggestions
) {
if (isDown) {
// If user is navigating history (historyNavDownEnabled), skip menu navigation entirely
if (state.historyNavDownEnabled) {
// Fall through to history navigation
} else if (state.slashSelectedIndex < state.slashMatchesLength - 1) {
return { type: 'slash-menu-down' }
} else {
return { type: 'none' } // At bottom, don't navigate
}
}
if (isUp) {
// If user is navigating history (historyNavUpEnabled), skip menu navigation entirely
if (state.historyNavUpEnabled) {
// Fall through to history navigation
} else if (state.slashSelectedIndex > 0) {
return { type: 'slash-menu-up' }
} else {
return { type: 'none' } // At top, don't navigate
}
}
if (isShiftTab) {
return { type: 'slash-menu-shift-tab' }
}
if (isTab) {
// Multiple matches: cycle through options
// Single match: complete the word without executing
if (state.slashMatchesLength > 1) {
return { type: 'slash-menu-tab' }
}
return { type: 'slash-menu-complete' }
}
if (isEnter) {
return { type: 'slash-menu-select' }
}
}
// Priority 7: Mention menu navigation (when active)
// Skip menu navigation for Up/Down if history navigation is enabled (user is paging through history)
if (state.mentionMenuActive && state.totalMentionMatches > 0) {
if (isDown) {
// If user is navigating history (historyNavDownEnabled), skip menu navigation entirely
if (state.historyNavDownEnabled) {
// Fall through to history navigation
} else if (state.agentSelectedIndex < state.totalMentionMatches - 1) {
return { type: 'mention-menu-down' }
} else {
return { type: 'none' } // At bottom, don't navigate
}
}
if (isUp) {
// If user is navigating history (historyNavUpEnabled), skip menu navigation entirely
if (state.historyNavUpEnabled) {
// Fall through to history navigation
} else if (state.agentSelectedIndex > 0) {
return { type: 'mention-menu-up' }
} else {
return { type: 'none' } // At top, don't navigate
}
}
if (isShiftTab) {
return { type: 'mention-menu-shift-tab' }
}
if (isTab) {
// Multiple matches: cycle through options
// Single match: complete the word without executing
if (state.totalMentionMatches > 1) {
return { type: 'mention-menu-tab' }
}
return { type: 'mention-menu-complete' }
}
if (isEnter) {
return { type: 'mention-menu-select' }
}
}
// Priority 8: Tab to open file menu (when not in a menu, not shift-tab, and suggestions enabled)
// This is handled by the hook since it needs to check word at cursor
if (
isTab &&
!key.shift &&
!state.mentionMenuActive &&
!state.slashMenuActive &&
!state.disableSlashSuggestions
) {
return { type: 'open-file-menu-with-tab' }
}
// Priority 9: Queue management
if (isCtrlC && state.queuePaused && state.queuedCount > 0) {
return { type: 'clear-queue' }
}
// Priority 10: Bash history navigation (when in bash mode)
if (state.inputMode === 'bash') {
if (isUp && state.historyNavUpEnabled) {
return { type: 'bash-history-up' }
}
if (isDown && state.historyNavDownEnabled) {
return { type: 'bash-history-down' }
}
}
// Priority 10.5: Regular history navigation (when at edges and enabled)
if (isUp && state.historyNavUpEnabled) {
return { type: 'history-up' }
}
if (isDown && state.historyNavDownEnabled) {
return { type: 'history-down' }
}
// Priority 11: Toggle all collapsed/expanded (Ctrl+T)
const isCtrlT = key.ctrl && key.name === 't' && !key.meta && !key.option
if (isCtrlT) {
return { type: 'toggle-all' }
}
// Priority 12: Agent mode toggle (tab or shift-tab when not in menus)
if (
(isShiftTab || isTab) &&
!state.slashMenuActive &&
!state.mentionMenuActive
) {
return { type: 'toggle-agent-mode' }
}
// Priority 13: Unfocus agent
if (isEscape && state.focusedAgentId !== null) {
return { type: 'unfocus-agent' }
}
// Priority 14: Scroll with PageUp/PageDown
if (isPageUp) {
return { type: 'scroll-up' }
}
if (isPageDown) {
return { type: 'scroll-down' }
}
// Priority 15: Paste (ctrl-v)
if (isCtrlV) {
return { type: 'paste' }
}
// Priority 16: Exit app (ctrl-c double-tap)
if (isCtrlC) {
if (state.nextCtrlCWillExit) {
return { type: 'exit-app' }
}
return { type: 'exit-app-warning' }
}
return { type: 'none' }
}
/**
* Creates default chat keyboard state for initialization.
*/
export function createDefaultChatKeyboardState(): ChatKeyboardState {
return {
inputMode: 'default',
inputValue: '',
cursorPosition: 0,
isStreaming: false,
isWaitingForResponse: false,
feedbackMode: false,
focusedAgentId: null,
slashMenuActive: false,
mentionMenuActive: false,
slashSelectedIndex: 0,
agentSelectedIndex: 0,
slashMatchesLength: 0,
totalMentionMatches: 0,
disableSlashSuggestions: false,
queuePaused: false,
queuedCount: 0,
historyNavUpEnabled: false,
historyNavDownEnabled: false,
nextCtrlCWillExit: false,
}
}