forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage-input-logic.ts
More file actions
272 lines (243 loc) · 8.01 KB
/
message-input-logic.ts
File metadata and controls
272 lines (243 loc) · 8.01 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
/**
* Pure algorithm functions for MessageInput behavior.
*
* These functions contain no React dependencies — they are plain TypeScript
* and can be tested directly without any framework setup.
*/
import { BUILT_IN_COMMANDS, COMMAND_PROMPTS } from '@/lib/constants/commands';
import type { PopoverItem, PopoverMode, CommandBadge, CliBadge } from '@/types';
// ─── Result types ────────────────────────────────────────────────
export interface InsertResult {
action: 'immediate_command' | 'set_badge' | 'insert_file_mention';
commandValue?: string;
badge?: CommandBadge;
newInputValue?: string;
}
export interface BadgeDispatchResult {
prompt: string;
displayLabel: string;
}
export type KeyAction =
| { type: 'popover_navigate'; direction: 'up' | 'down' }
| { type: 'popover_select' }
| { type: 'close_popover' }
| { type: 'remove_badge' }
| { type: 'remove_cli_badge' }
| { type: 'passthrough' };
export interface DirectSlashResult {
action: 'immediate_command' | 'set_badge' | 'unknown_slash_badge' | 'not_slash';
commandValue?: string;
badge?: CommandBadge;
}
// ─── Functions ───────────────────────────────────────────────────
/**
* Detects popover trigger from input text and cursor position.
* Used by handleInputChange in useSlashCommands.
*/
export function detectPopoverTrigger(
text: string,
cursorPos: number,
): { mode: PopoverMode; filter: string; triggerPos: number } | null {
const beforeCursor = text.slice(0, cursorPos);
// Check for @ trigger
const atMatch = beforeCursor.match(/@([^\s@]*)$/);
if (atMatch) {
return {
mode: 'file',
filter: atMatch[1],
triggerPos: cursorPos - atMatch[0].length,
};
}
// Check for / trigger (only at start of line or after space)
const slashMatch = beforeCursor.match(/(^|\s)\/([^\s]*)$/);
if (slashMatch) {
return {
mode: 'skill',
filter: slashMatch[2],
triggerPos: cursorPos - slashMatch[2].length - 1,
};
}
return null;
}
/**
* Filters popover items by substring match on label or description.
* Used by the filteredItems useMemo in usePopoverState.
*/
export function filterItems(items: PopoverItem[], filter: string): PopoverItem[] {
const q = filter.toLowerCase();
return items.filter(
(item) =>
item.label.toLowerCase().includes(q) ||
(item.description || '').toLowerCase().includes(q),
);
}
/**
* Determines what happens when an item is selected from the popover.
* Used by insertItem in useSlashCommands.
*/
export function resolveItemSelection(
item: PopoverItem,
popoverMode: PopoverMode,
triggerPos: number,
inputValue: string,
popoverFilter: string,
): InsertResult {
// Immediate built-in commands
if (item.builtIn && item.immediate) {
return { action: 'immediate_command', commandValue: item.value };
}
// Non-immediate commands: show as badge
if (popoverMode === 'skill') {
return {
action: 'set_badge',
badge: {
command: item.value,
label: item.label,
description: item.description || '',
kind: item.kind || 'slash_command',
installedSource: item.installedSource,
},
};
}
// File mention: insert into text
const before = inputValue.slice(0, triggerPos);
const cursorEnd = triggerPos + popoverFilter.length + 1;
const after = inputValue.slice(cursorEnd);
const insertText = `@${item.value} `;
return {
action: 'insert_file_mention',
newInputValue: before + insertText + after,
};
}
/**
* Badge dispatch logic — what prompt is sent for each badge kind.
* Used by handleSubmit in MessageInput.
*/
export function dispatchBadge(badge: CommandBadge, userContent: string): BadgeDispatchResult {
const displayLabel = `/${badge.label}`;
switch (badge.kind) {
case 'agent_skill': {
const agentPrompt = userContent
? `Use the ${badge.label} skill. User context: ${userContent}`
: `Please use the ${badge.label} skill.`;
return { prompt: agentPrompt, displayLabel };
}
case 'slash_command':
case 'sdk_command': {
const slashPrompt = userContent
? `${badge.command} ${userContent}`
: badge.command;
return { prompt: slashPrompt, displayLabel };
}
case 'codepilot_command': {
const expandedPrompt = COMMAND_PROMPTS[badge.command] || '';
const finalPrompt = userContent
? `${expandedPrompt}\n\nUser context: ${userContent}`
: expandedPrompt || badge.command;
return { prompt: finalPrompt, displayLabel };
}
}
}
/**
* ArrowDown/ArrowUp index cycling logic.
* Used by handleKeyDown popover navigation in MessageInput.
*/
export function cycleIndex(current: number, direction: 'up' | 'down', length: number): number {
if (direction === 'down') return (current + 1) % length;
return (current - 1 + length) % length;
}
/**
* Submit gating logic — determines whether submit is enabled.
* Used by FileAwareSubmitButton disabled logic.
*/
export function isSubmitEnabled(opts: {
inputValue: string;
hasBadge: boolean;
hasFiles: boolean;
isStreaming: boolean;
disabled: boolean;
}): boolean {
if (opts.disabled) return false;
if (opts.isStreaming) return true; // streaming = stop button
return !!(opts.inputValue.trim() || opts.hasBadge || opts.hasFiles);
}
/**
* Keyboard dispatch logic — determines what action to take for a given key.
* Used by handleKeyDown in MessageInput.
*/
export function resolveKeyAction(
key: string,
state: {
popoverMode: PopoverMode;
popoverHasItems: boolean;
inputValue: string;
hasBadge: boolean;
hasCliBadge: boolean;
},
): KeyAction {
// Popover navigation (skill/file mode)
if (state.popoverMode && state.popoverMode !== 'cli' && state.popoverHasItems) {
if (key === 'ArrowDown') return { type: 'popover_navigate', direction: 'down' };
if (key === 'ArrowUp') return { type: 'popover_navigate', direction: 'up' };
if (key === 'Enter' || key === 'Tab') return { type: 'popover_select' };
if (key === 'Escape') return { type: 'close_popover' };
}
// CLI popover
if (state.popoverMode === 'cli') {
if (key === 'Escape') return { type: 'close_popover' };
}
// Backspace removes badge when input is empty
if (key === 'Backspace' && !state.inputValue) {
if (state.hasBadge) return { type: 'remove_badge' };
if (state.hasCliBadge) return { type: 'remove_cli_badge' };
}
// Escape removes badge
if (key === 'Escape') {
if (state.hasBadge) return { type: 'remove_badge' };
if (state.hasCliBadge) return { type: 'remove_cli_badge' };
}
return { type: 'passthrough' };
}
/**
* Direct slash command detection — when user types "/command" in input and submits.
* Used by handleSubmit in MessageInput.
*/
export function resolveDirectSlash(content: string): DirectSlashResult {
if (!content.startsWith('/')) return { action: 'not_slash' };
const cmd = BUILT_IN_COMMANDS.find((c) => c.value === content);
if (cmd) {
if (cmd.immediate) {
return { action: 'immediate_command', commandValue: content };
}
return {
action: 'set_badge',
badge: {
command: cmd.value,
label: cmd.label,
description: cmd.description || '',
kind: cmd.kind || 'sdk_command',
},
};
}
const skillName = content.slice(1);
if (skillName) {
return {
action: 'unknown_slash_badge',
badge: {
command: content,
label: skillName,
description: '',
kind: 'slash_command',
},
};
}
return { action: 'not_slash' };
}
/**
* CLI badge system prompt append generation.
* Used by handleSubmit in MessageInput.
*/
export function buildCliAppend(cliBadge: CliBadge | null): string | undefined {
if (!cliBadge) return undefined;
return `The user wants to use the installed CLI tool "${cliBadge.name}" if appropriate for this task. Prefer using "${cliBadge.name}" when suitable.`;
}