-
Notifications
You must be signed in to change notification settings - Fork 518
Expand file tree
/
Copy pathuse-chat-keyboard.ts
More file actions
301 lines (271 loc) · 8.03 KB
/
use-chat-keyboard.ts
File metadata and controls
301 lines (271 loc) · 8.03 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
import { useKeyboard } from '@opentui/react'
import { useCallback, useRef } from 'react'
import { hasClipboardImage, readClipboardText, readClipboardImageFilePath, getImageFilePathFromText } from '../utils/clipboard-image'
import { getProjectRoot } from '../project-files'
import { reportActivity } from '../utils/activity-tracker'
import {
resolveChatKeyboardAction,
type ChatKeyboardState,
type ChatKeyboardAction,
} from '../utils/keyboard-actions'
import type { KeyEvent } from '@opentui/core'
// Throttle interval for keyboard activity reporting (ms)
const KEYBOARD_ACTIVITY_THROTTLE_MS = 1000
/**
* Handlers for chat keyboard actions.
* Each handler corresponds to a ChatKeyboardAction type.
*/
export type ChatKeyboardHandlers = {
// Mode handlers
onExitInputMode: () => void
onExitFeedbackMode: () => void
onClearFeedbackInput: () => void
// Input handlers
onClearInput: () => void
onBackspaceExitMode: () => void
// Stream handlers
onInterruptStream: () => void
// Slash menu handlers
onSlashMenuDown: () => void
onSlashMenuUp: () => void
onSlashMenuTab: () => void
onSlashMenuShiftTab: () => void
onSlashMenuSelect: () => Promise<void> | void
onSlashMenuComplete: () => void
// Mention menu handlers
onMentionMenuDown: () => void
onMentionMenuUp: () => void
onMentionMenuTab: () => void
onMentionMenuShiftTab: () => void
onMentionMenuSelect: () => void
onMentionMenuComplete: () => void
// File menu handler
onOpenFileMenuWithTab: () => boolean // Returns true if menu was opened
// History handlers
onHistoryUp: () => void
onHistoryDown: () => void
// Agent handlers
onToggleAgentMode: () => void
onUnfocusAgent: () => void
// Queue handlers
onClearQueue: () => void
// Exit handlers
onExitAppWarning: () => void
onExitApp: () => void
// Bash history handlers
onBashHistoryUp: () => void
onBashHistoryDown: () => void
// Clipboard handlers
onPasteImage: () => void
onPasteImagePath: (imagePath: string) => void
onPasteText: (text: string) => void
// Scroll handlers
onScrollUp: () => void
onScrollDown: () => void
// Out of credits handler
onOpenBuyCredits: () => void
}
/**
* Options for the useChatKeyboard hook.
*/
export type UseChatKeyboardOptions = {
/** Current keyboard state extracted from stores */
state: ChatKeyboardState
/** Handlers for keyboard actions */
handlers: ChatKeyboardHandlers
/** Whether keyboard handling is disabled (e.g., during ask-user) */
disabled?: boolean
}
/**
* Dispatches a keyboard action to the appropriate handler.
*/
function assertNever(action: never): never {
throw new Error(`Unhandled chat keyboard action: ${String(action)}`)
}
function dispatchAction(
action: ChatKeyboardAction,
handlers: ChatKeyboardHandlers,
): boolean {
switch (action.type) {
case 'exit-input-mode':
handlers.onExitInputMode()
return true
case 'exit-feedback-mode':
handlers.onExitFeedbackMode()
return true
case 'clear-feedback-input':
handlers.onClearFeedbackInput()
return true
case 'clear-input':
handlers.onClearInput()
return true
case 'backspace-exit-mode':
handlers.onBackspaceExitMode()
return true
case 'interrupt-stream':
handlers.onInterruptStream()
return true
case 'slash-menu-down':
handlers.onSlashMenuDown()
return true
case 'slash-menu-up':
handlers.onSlashMenuUp()
return true
case 'slash-menu-tab':
handlers.onSlashMenuTab()
return true
case 'slash-menu-shift-tab':
handlers.onSlashMenuShiftTab()
return true
case 'slash-menu-select':
handlers.onSlashMenuSelect()
return true
case 'slash-menu-complete':
handlers.onSlashMenuComplete()
return true
case 'mention-menu-down':
handlers.onMentionMenuDown()
return true
case 'mention-menu-up':
handlers.onMentionMenuUp()
return true
case 'mention-menu-tab':
handlers.onMentionMenuTab()
return true
case 'mention-menu-shift-tab':
handlers.onMentionMenuShiftTab()
return true
case 'mention-menu-select':
handlers.onMentionMenuSelect()
return true
case 'mention-menu-complete':
handlers.onMentionMenuComplete()
return true
case 'open-file-menu-with-tab': {
const opened = handlers.onOpenFileMenuWithTab()
if (!opened) {
handlers.onToggleAgentMode()
}
return true
}
case 'history-up':
handlers.onHistoryUp()
return true
case 'history-down':
handlers.onHistoryDown()
return true
case 'toggle-agent-mode':
handlers.onToggleAgentMode()
return true
case 'unfocus-agent':
handlers.onUnfocusAgent()
return true
case 'clear-queue':
handlers.onClearQueue()
return true
case 'exit-app-warning':
handlers.onExitAppWarning()
return true
case 'exit-app':
handlers.onExitApp()
return true
case 'bash-history-up':
handlers.onBashHistoryUp()
return true
case 'bash-history-down':
handlers.onBashHistoryDown()
return true
case 'paste': {
const cwd = getProjectRoot() ?? process.cwd()
// First, check if clipboard contains a copied image file (e.g., from Finder)
// This is different from text - it's when you Cmd+C a file in Finder
const copiedImagePath = readClipboardImageFilePath()
if (copiedImagePath) {
handlers.onPasteImagePath(copiedImagePath)
return true
}
// Next, read clipboard text to check if it's a file path
// This handles the case where a file is dragged/dropped - we want to use
// the file path, not any stale image data that might be in the clipboard
const text = readClipboardText()
if (text) {
// Check if the text is a path to an image file
const imagePath = getImageFilePathFromText(text, cwd)
if (imagePath) {
handlers.onPasteImagePath(imagePath)
return true
}
}
// Check for actual image data in clipboard (screenshots, copied images)
// This comes AFTER the file path check so dragged files take priority
if (hasClipboardImage()) {
handlers.onPasteImage()
return true
}
// Regular text paste
if (text) {
handlers.onPasteText(text)
return true
}
return true
}
case 'scroll-up':
handlers.onScrollUp()
return true
case 'scroll-down':
handlers.onScrollDown()
return true
case 'open-buy-credits':
handlers.onOpenBuyCredits()
return true
case 'none':
return false
}
return assertNever(action)
}
/**
* Hook for handling keyboard input in chat text input contexts.
* Integrates priority-based action resolution with handlers.
*
* This hook handles:
* - Mode switching (bash, referral, etc.)
* - Stream interruption
* - Suggestion menu navigation (slash and mention menus)
* - History navigation
* - Agent mode toggle
* - Exit handling
*
* For feedback mode, the hook respects the feedbackMode state and routes
* escape/ctrl-c appropriately.
*/
export function useChatKeyboard({
state,
handlers,
disabled = false,
}: UseChatKeyboardOptions): void {
const lastKeyboardActivityRef = useRef<number>(0)
useKeyboard(
useCallback(
(key: KeyEvent) => {
if (disabled) return
// Report keyboard activity for activity-aware features (throttled)
const now = Date.now()
if (now - lastKeyboardActivityRef.current > KEYBOARD_ACTIVITY_THROTTLE_MS) {
lastKeyboardActivityRef.current = now
reportActivity()
}
const action = resolveChatKeyboardAction(key, state)
const handled = dispatchAction(action, handlers)
// Prevent default for handled actions
if (
handled &&
'preventDefault' in key &&
typeof key.preventDefault === 'function'
) {
key.preventDefault()
}
},
[state, handlers, disabled],
),
)
}