-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathComposer.tsx
More file actions
770 lines (716 loc) · 31.3 KB
/
Composer.tsx
File metadata and controls
770 lines (716 loc) · 31.3 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
import { useState, useRef, useEffect, useCallback, useMemo } from "react";
import { useStore } from "../store.js";
import { createClientMessageId, sendToSession } from "../ws.js";
import { CLAUDE_MODES, CODEX_MODES } from "../utils/backends.js";
import { api, type SavedPrompt } from "../api.js";
import type { ModeOption } from "../utils/backends.js";
import { ModelSwitcher } from "./ModelSwitcher.js";
import { MentionMenu } from "./MentionMenu.js";
import { useMentionMenu } from "../utils/use-mention-menu.js";
import { readFileAsBase64, type ImageAttachment } from "../utils/image.js";
/** Stable reference to avoid infinite re-renders in Zustand selectors. */
const emptyStringArray: string[] = [];
interface CommandItem {
name: string;
type: "command" | "skill";
}
export function Composer({ sessionId }: { sessionId: string }) {
const [text, setText] = useState("");
const [images, setImages] = useState<ImageAttachment[]>([]);
const [slashMenuOpen, setSlashMenuOpen] = useState(false);
const [slashMenuIndex, setSlashMenuIndex] = useState(0);
const [savePromptOpen, setSavePromptOpen] = useState(false);
const [savePromptName, setSavePromptName] = useState("");
const [savePromptScope, setSavePromptScope] = useState<"global" | "project">("global");
const [savePromptError, setSavePromptError] = useState<string | null>(null);
const [caretPos, setCaretPos] = useState(0);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const menuRef = useRef<HTMLDivElement>(null);
const pendingSelectionRef = useRef<number | null>(null);
const cliConnected = useStore((s) => s.cliConnected);
const sessionData = useStore((s) => s.sessions.get(sessionId));
const previousMode = useStore((s) => s.previousPermissionMode.get(sessionId) || "acceptEdits");
const isConnected = cliConnected.get(sessionId) ?? false;
const currentMode = sessionData?.permissionMode || "acceptEdits";
const isPlan = currentMode === "plan";
const isCodex = sessionData?.backend_type === "codex";
const modes: ModeOption[] = isCodex ? CODEX_MODES : CLAUDE_MODES;
const modeLabel = modes.find((m) => m.value === currentMode)?.label?.toLowerCase() || currentMode;
const mention = useMentionMenu({
text,
caretPos,
cwd: sessionData?.cwd,
enabled: !slashMenuOpen,
});
// Build command list from session data
const allCommands = useMemo<CommandItem[]>(() => {
const cmds: CommandItem[] = [];
if (sessionData?.slash_commands) {
for (const cmd of sessionData.slash_commands) {
cmds.push({ name: cmd, type: "command" });
}
}
if (sessionData?.skills) {
for (const skill of sessionData.skills) {
cmds.push({ name: skill, type: "skill" });
}
}
return cmds;
}, [sessionData?.slash_commands, sessionData?.skills]);
// Filter commands based on what the user typed after /
const filteredCommands = useMemo(() => {
if (!slashMenuOpen) return [];
// Extract the slash query: text starts with / and we match the part after /
const match = text.match(/^\/(\S*)$/);
if (!match) return [];
const query = match[1].toLowerCase();
if (query === "") return allCommands;
return allCommands.filter((cmd) => cmd.name.toLowerCase().includes(query));
}, [text, slashMenuOpen, allCommands]);
// Open/close slash menu based on text
useEffect(() => {
const shouldOpen = text.startsWith("/") && /^\/\S*$/.test(text) && allCommands.length > 0;
if (shouldOpen && !slashMenuOpen) {
setSlashMenuOpen(true);
setSlashMenuIndex(0);
} else if (!shouldOpen && slashMenuOpen) {
setSlashMenuOpen(false);
}
}, [text, allCommands.length, slashMenuOpen]);
// Keep slash menu selected index in bounds
useEffect(() => {
if (slashMenuIndex >= filteredCommands.length) {
setSlashMenuIndex(Math.max(0, filteredCommands.length - 1));
}
}, [filteredCommands.length, slashMenuIndex]);
// Scroll slash menu selected item into view
useEffect(() => {
if (!menuRef.current || !slashMenuOpen) return;
const items = menuRef.current.querySelectorAll("[data-cmd-index]");
const selected = items[slashMenuIndex];
if (selected) {
selected.scrollIntoView({ block: "nearest" });
}
}, [slashMenuIndex, slashMenuOpen]);
useEffect(() => {
if (pendingSelectionRef.current === null || !textareaRef.current) return;
const next = pendingSelectionRef.current;
textareaRef.current.setSelectionRange(next, next);
pendingSelectionRef.current = null;
}, [text]);
const selectCommand = useCallback((cmd: CommandItem) => {
setText(`/${cmd.name} `);
setSlashMenuOpen(false);
textareaRef.current?.focus();
}, []);
const selectPrompt = useCallback((prompt: SavedPrompt) => {
const result = mention.selectPrompt(prompt);
pendingSelectionRef.current = result.nextCursor;
setText(result.nextText);
mention.setMentionMenuOpen(false);
setCaretPos(result.nextCursor);
textareaRef.current?.focus();
// Auto-resize textarea after prompt insertion
if (textareaRef.current) {
textareaRef.current.style.height = "auto";
textareaRef.current.style.height = Math.min(textareaRef.current.scrollHeight, 200) + "px";
}
}, [mention]);
function handleSend() {
const msg = text.trim();
if (!msg || !isConnected) return;
const clientMsgId = createClientMessageId();
sendToSession(sessionId, {
type: "user_message",
content: msg,
session_id: sessionId,
images: images.length > 0 ? images.map((img) => ({ media_type: img.mediaType, data: img.base64 })) : undefined,
client_msg_id: clientMsgId,
});
useStore.getState().appendMessage(sessionId, {
id: clientMsgId,
role: "user",
content: msg,
images: images.length > 0 ? images.map((img) => ({ media_type: img.mediaType, data: img.base64 })) : undefined,
timestamp: Date.now(),
});
setText("");
setImages([]);
setSlashMenuOpen(false);
mention.setMentionMenuOpen(false);
if (textareaRef.current) {
textareaRef.current.style.height = "auto";
}
textareaRef.current?.focus();
}
function handleKeyDown(e: React.KeyboardEvent) {
// Slash menu navigation
if (slashMenuOpen && filteredCommands.length > 0) {
if (e.key === "ArrowDown") {
e.preventDefault();
setSlashMenuIndex((i) => (i + 1) % filteredCommands.length);
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
setSlashMenuIndex((i) => (i - 1 + filteredCommands.length) % filteredCommands.length);
return;
}
if (e.key === "Tab" && !e.shiftKey) {
e.preventDefault();
selectCommand(filteredCommands[slashMenuIndex]);
return;
}
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
selectCommand(filteredCommands[slashMenuIndex]);
return;
}
if (e.key === "Escape") {
e.preventDefault();
setSlashMenuOpen(false);
return;
}
}
if (mention.mentionMenuOpen) {
if (e.key === "Escape") {
e.preventDefault();
mention.setMentionMenuOpen(false);
return;
}
}
if (mention.mentionMenuOpen && mention.filteredPrompts.length > 0) {
if (e.key === "ArrowDown") {
e.preventDefault();
mention.setMentionMenuIndex((i) => (i + 1) % mention.filteredPrompts.length);
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
mention.setMentionMenuIndex((i) => (i - 1 + mention.filteredPrompts.length) % mention.filteredPrompts.length);
return;
}
if ((e.key === "Tab" && !e.shiftKey) || (e.key === "Enter" && !e.shiftKey)) {
e.preventDefault();
selectPrompt(mention.filteredPrompts[mention.mentionMenuIndex]);
return;
}
}
if (
mention.mentionMenuOpen
&& mention.filteredPrompts.length === 0
&& ((e.key === "Enter" && !e.shiftKey) || (e.key === "Tab" && !e.shiftKey))
) {
e.preventDefault();
return;
}
if (e.key === "Tab" && e.shiftKey) {
e.preventDefault();
toggleMode();
return;
}
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSend();
}
}
function handleInput(e: React.ChangeEvent<HTMLTextAreaElement>) {
setText(e.target.value);
setCaretPos(e.target.selectionStart ?? e.target.value.length);
const ta = e.target;
ta.style.height = "auto";
ta.style.height = Math.min(ta.scrollHeight, 200) + "px";
}
function syncCaret() {
if (!textareaRef.current) return;
setCaretPos(textareaRef.current.selectionStart ?? 0);
}
function handleInterrupt() {
sendToSession(sessionId, { type: "interrupt" });
}
async function handleFileSelect(e: React.ChangeEvent<HTMLInputElement>) {
const files = e.target.files;
if (!files) return;
const newImages: ImageAttachment[] = [];
for (const file of Array.from(files)) {
if (!file.type.startsWith("image/")) continue;
const { base64, mediaType } = await readFileAsBase64(file);
newImages.push({ name: file.name, base64, mediaType });
}
setImages((prev) => [...prev, ...newImages]);
e.target.value = "";
}
function removeImage(index: number) {
setImages((prev) => prev.filter((_, i) => i !== index));
}
async function handlePaste(e: React.ClipboardEvent) {
const items = e.clipboardData?.items;
if (!items) return;
const newImages: ImageAttachment[] = [];
for (const item of Array.from(items)) {
if (!item.type.startsWith("image/")) continue;
const file = item.getAsFile();
if (!file) continue;
const { base64, mediaType } = await readFileAsBase64(file);
newImages.push({ name: `pasted-${Date.now()}.${file.type.split("/")[1]}`, base64, mediaType });
}
if (newImages.length > 0) {
e.preventDefault();
setImages((prev) => [...prev, ...newImages]);
}
}
function toggleMode() {
if (!isConnected) return;
const store = useStore.getState();
if (!isPlan) {
store.setPreviousPermissionMode(sessionId, currentMode);
sendToSession(sessionId, { type: "set_permission_mode", mode: "plan" });
store.updateSession(sessionId, { permissionMode: "plan" });
} else {
const restoreMode = previousMode || (isCodex ? "bypassPermissions" : "acceptEdits");
sendToSession(sessionId, { type: "set_permission_mode", mode: restoreMode });
store.updateSession(sessionId, { permissionMode: restoreMode });
}
}
async function handleCreatePrompt() {
const content = text.trim();
const name = savePromptName.trim();
if (!content || !name) return;
if (savePromptScope === "project" && !sessionData?.cwd) {
setSavePromptError("No project folder available for this session");
return;
}
const payload: { name: string; content: string; scope: "global" | "project"; projectPaths?: string[] } = {
name,
content,
scope: savePromptScope,
};
if (savePromptScope === "project") {
payload.projectPaths = [sessionData!.cwd!];
}
try {
await api.createPrompt(payload);
await mention.refreshPrompts();
setSavePromptOpen(false);
setSavePromptName("");
setSavePromptScope("global");
setSavePromptError(null);
} catch (error) {
const message = error instanceof Error && error.message ? error.message : "Could not save prompt.";
setSavePromptError(message);
}
}
const promptSuggestionsRaw = useStore((s) => s.promptSuggestions.get(sessionId));
const promptSuggestions = promptSuggestionsRaw ?? emptyStringArray;
const clearPromptSuggestions = useStore((s) => s.clearPromptSuggestions);
const sessionStatus = useStore((s) => s.sessionStatus);
const isRunning = sessionStatus.get(sessionId) === "running";
const canSend = text.trim().length > 0 && isConnected;
return (
<div className="shrink-0 px-0 sm:px-6 pt-0 sm:pt-3 pb-5 sm:pb-4 bg-cc-input-bg/80 sm:bg-transparent backdrop-blur-sm">
<div className="max-w-3xl mx-auto">
{/* Image thumbnails */}
{images.length > 0 && (
<div className="flex items-center gap-2 mb-2 px-3 sm:px-0 flex-wrap">
{images.map((img, i) => (
<div key={i} className="relative group">
<img
src={`data:${img.mediaType};base64,${img.base64}`}
alt={img.name}
className="w-12 h-12 rounded-lg object-cover border border-cc-border"
/>
<button
onClick={() => removeImage(i)}
aria-label="Remove image"
className="absolute -top-1.5 -right-1.5 w-6 h-6 rounded-full bg-cc-error text-white flex items-center justify-center text-[10px] opacity-100 sm:opacity-0 sm:group-hover:opacity-100 transition-opacity cursor-pointer"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-2.5 h-2.5">
<path d="M4 4l8 8M12 4l-8 8" stroke="currentColor" strokeWidth="2" strokeLinecap="round" fill="none" />
</svg>
</button>
</div>
))}
</div>
)}
{/* Hidden file input */}
<input
ref={fileInputRef}
type="file"
accept="image/*"
multiple
onChange={handleFileSelect}
className="hidden"
aria-label="Attach images"
/>
{/* Prompt suggestion chips */}
{promptSuggestions.length > 0 && (
<div className="flex flex-wrap gap-1.5 px-2 sm:px-4 pb-2">
{promptSuggestions.map((suggestion, i) => (
<button
key={i}
onClick={() => {
if (!isConnected || isRunning) return;
const clientMsgId = createClientMessageId();
sendToSession(sessionId, {
type: "user_message",
content: suggestion,
session_id: sessionId,
client_msg_id: clientMsgId,
});
useStore.getState().appendMessage(sessionId, {
id: clientMsgId,
role: "user",
content: suggestion,
timestamp: Date.now(),
});
clearPromptSuggestions(sessionId);
}}
disabled={!isConnected || isRunning}
className="text-xs px-2.5 py-1.5 rounded-lg bg-cc-hover hover:bg-cc-active text-cc-fg border border-cc-border transition-colors cursor-pointer truncate max-w-[280px]"
title={suggestion}
>
{suggestion}
</button>
))}
</div>
)}
{/* Input container: flat separator on mobile, card on desktop */}
<div className={`relative overflow-visible transition-all duration-200 border-t border-cc-separator sm:border sm:border-cc-border sm:bg-cc-input-bg/95 sm:rounded-[14px] sm:backdrop-blur-sm composer-card ${
isPlan
? "sm:border-cc-primary/40 sm:shadow-[0_10px_30px_rgba(217,119,87,0.08)]"
: "sm:focus-within:border-cc-primary/25"
}`}>
{/* Slash command menu */}
{slashMenuOpen && filteredCommands.length > 0 && (
<div
ref={menuRef}
className="absolute left-2 right-2 bottom-full mb-1 max-h-[240px] overflow-y-auto bg-cc-card border border-cc-border rounded-[12px] shadow-xl z-20 py-1"
>
{filteredCommands.map((cmd, i) => (
<button
key={`${cmd.type}-${cmd.name}`}
data-cmd-index={i}
onClick={() => selectCommand(cmd)}
className={`w-full px-3 py-2 text-left flex items-center gap-2.5 transition-colors cursor-pointer ${
i === slashMenuIndex
? "bg-cc-hover"
: "hover:bg-cc-hover/50"
}`}
>
<span className="flex items-center justify-center w-6 h-6 rounded-md bg-cc-hover text-cc-muted shrink-0">
{cmd.type === "skill" ? (
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<path d="M8 1l1.796 3.64L14 5.255l-3 2.924.708 4.126L8 10.5l-3.708 1.805L5 8.18 2 5.255l4.204-.615L8 1z" />
</svg>
) : (
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" className="w-3.5 h-3.5">
<path d="M5 12L10 4" strokeLinecap="round" />
</svg>
)}
</span>
<div className="flex-1 min-w-0">
<span className="text-[13px] font-medium text-cc-fg">/{cmd.name}</span>
<span className="ml-2 text-[11px] text-cc-muted">{cmd.type}</span>
</div>
</button>
))}
</div>
)}
{/* @ prompt menu */}
<MentionMenu
open={mention.mentionMenuOpen}
loading={mention.promptsLoading}
prompts={mention.filteredPrompts}
selectedIndex={mention.mentionMenuIndex}
onSelect={selectPrompt}
menuRef={mention.mentionMenuRef}
className="absolute left-2 right-2 bottom-full mb-1"
/>
{savePromptOpen && (
<div className="absolute left-2 right-2 bottom-full mb-1 bg-cc-card border border-cc-border rounded-[12px] shadow-xl z-20 p-3 space-y-2">
<div className="text-xs font-semibold text-cc-fg">Save prompt</div>
<input
value={savePromptName}
onChange={(e) => {
setSavePromptName(e.target.value);
if (savePromptError) setSavePromptError(null);
}}
placeholder="Prompt title"
aria-label="Prompt title"
className="w-full px-2 py-1.5 text-sm bg-cc-input-bg border border-cc-border rounded-md text-cc-fg focus:outline-none focus:border-cc-primary/40"
/>
<div className="flex items-center gap-1.5">
<button
type="button"
aria-pressed={savePromptScope === "global"}
onClick={() => setSavePromptScope("global")}
className={`px-2 py-0.5 text-[11px] rounded border transition-colors cursor-pointer ${
savePromptScope === "global"
? "border-cc-primary/40 text-cc-primary bg-cc-primary/8"
: "border-cc-border text-cc-muted hover:text-cc-fg"
}`}
>
Global
</button>
<button
type="button"
aria-pressed={savePromptScope === "project"}
onClick={() => setSavePromptScope("project")}
className={`px-2 py-0.5 text-[11px] rounded border transition-colors cursor-pointer ${
savePromptScope === "project"
? "border-cc-primary/40 text-cc-primary bg-cc-primary/8"
: "border-cc-border text-cc-muted hover:text-cc-fg"
}`}
>
This project
</button>
</div>
{savePromptScope === "project" && sessionData?.cwd && (
<div className="text-[10px] text-cc-muted font-mono-code truncate" title={sessionData.cwd}>
{sessionData.cwd}
</div>
)}
{savePromptError ? (
<div className="text-[11px] text-cc-error">{savePromptError}</div>
) : null}
<div className="flex items-center gap-1.5 justify-end">
<button
onClick={() => {
setSavePromptOpen(false);
setSavePromptScope("global");
setSavePromptError(null);
}}
className="px-2 py-1 text-[11px] rounded-md border border-cc-border text-cc-muted hover:text-cc-fg cursor-pointer"
>
Cancel
</button>
<button
onClick={handleCreatePrompt}
disabled={!savePromptName.trim() || !text.trim()}
className={`px-2 py-1 text-[11px] rounded-md border ${
savePromptName.trim() && text.trim()
? "border-cc-primary/40 text-cc-primary bg-cc-primary/8 cursor-pointer"
: "border-cc-border text-cc-muted cursor-not-allowed"
}`}
>
Save
</button>
</div>
</div>
)}
{/* Mobile toolbar: mode toggle + model switcher + secondary actions (hidden on sm+) */}
<div className="flex items-center gap-1.5 px-3 pt-1.5 pb-0.5 sm:hidden">
<button
onClick={toggleMode}
disabled={!isConnected}
className={`flex items-center gap-1.5 px-2 py-1 rounded-md text-[12px] font-semibold transition-all border select-none shrink-0 ${
!isConnected
? "opacity-30 cursor-not-allowed text-cc-muted border-transparent"
: isPlan
? "text-cc-primary border-cc-primary/30 bg-cc-primary/8"
: "text-cc-muted border-cc-border"
}`}
title="Toggle mode (Shift+Tab)"
>
{isPlan ? (
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<rect x="3" y="3" width="3.5" height="10" rx="0.75" />
<rect x="9.5" y="3" width="3.5" height="10" rx="0.75" />
</svg>
) : (
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<path d="M2.5 4l4 4-4 4" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" fill="none" />
<path d="M8.5 4l4 4-4 4" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" fill="none" />
</svg>
)}
<span>{modeLabel}</span>
</button>
<ModelSwitcher sessionId={sessionId} />
<div className="flex-1" />
<button
onClick={() => {
const defaultName = text.trim().slice(0, 32);
setSavePromptName(defaultName || "");
setSavePromptError(null);
setSavePromptOpen((v) => !v);
}}
disabled={!isConnected || !text.trim()}
className={`flex items-center justify-center w-8 h-8 rounded-md transition-colors ${
isConnected && text.trim()
? "text-cc-muted hover:text-cc-fg hover:bg-cc-hover cursor-pointer"
: "text-cc-muted opacity-30 cursor-not-allowed"
}`}
title="Save as prompt"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" className="w-4 h-4">
<path d="M4 2.75h8A1.25 1.25 0 0113.25 4v9.25L8 10.5l-5.25 2.75V4A1.25 1.25 0 014 2.75z" />
</svg>
</button>
<button
onClick={() => fileInputRef.current?.click()}
disabled={!isConnected}
className={`flex items-center justify-center w-8 h-8 rounded-md transition-colors ${
isConnected
? "text-cc-muted hover:text-cc-fg hover:bg-cc-hover cursor-pointer"
: "text-cc-muted opacity-30 cursor-not-allowed"
}`}
title="Upload image"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" className="w-4 h-4">
<rect x="2" y="2" width="12" height="12" rx="2" />
<circle cx="5.5" cy="5.5" r="1" fill="currentColor" stroke="none" />
<path d="M2 11l3-3 2 2 3-4 4 5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</button>
</div>
{/* Textarea row */}
<div className="px-3 sm:px-3 pt-1.5 sm:pt-2.5">
<textarea
ref={textareaRef}
value={text}
onChange={handleInput}
onKeyDown={handleKeyDown}
onClick={syncCaret}
onKeyUp={syncCaret}
onPaste={handlePaste}
aria-label="Message input"
placeholder={isConnected
? "Type a message... (/ + @)"
: "Waiting for CLI connection..."}
disabled={!isConnected}
rows={1}
className="w-full px-1 py-1.5 text-base sm:text-sm bg-transparent resize-none outline-none text-cc-fg font-sans-ui placeholder:text-cc-muted disabled:opacity-50 overflow-y-auto leading-relaxed"
style={{ minHeight: "36px", maxHeight: "200px" }}
/>
</div>
{/* Mobile action row (hidden on sm+) */}
<div className="flex items-center justify-end gap-1 px-3 pb-1 sm:hidden">
{/* Send/stop */}
{isRunning ? (
<button
onClick={handleInterrupt}
className="flex items-center justify-center w-10 h-10 rounded-lg bg-cc-error/10 hover:bg-cc-error/20 text-cc-error transition-colors cursor-pointer"
title="Stop generation"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<rect x="3" y="3" width="10" height="10" rx="1" />
</svg>
</button>
) : (
<button
onClick={handleSend}
disabled={!canSend}
className={`flex items-center justify-center w-10 h-10 rounded-full transition-all duration-200 ${
canSend
? "bg-cc-primary hover:bg-cc-primary-hover active:scale-95 text-white cursor-pointer shadow-[0_4px_16px_rgba(217,119,87,0.25)]"
: "bg-cc-hover text-cc-muted cursor-not-allowed"
}`}
title="Send message"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<path d="M3 2l11 6-11 6V9.5l7-1.5-7-1.5V2z" />
</svg>
</button>
)}
</div>
{/* Desktop action bar: + bookmark mode spacer model send (hidden on mobile) */}
<div className="hidden sm:flex items-center gap-1.5 px-2.5 pb-2.5 border-t border-cc-border/70">
{/* + button (image upload) */}
<button
onClick={() => fileInputRef.current?.click()}
disabled={!isConnected}
className={`flex items-center justify-center w-8 h-8 rounded-md transition-colors ${
isConnected
? "text-cc-muted hover:text-cc-fg hover:bg-cc-hover cursor-pointer"
: "text-cc-muted opacity-30 cursor-not-allowed"
}`}
title="Attach image"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" className="w-4 h-4">
<path d="M8 3v10M3 8h10" strokeLinecap="round" />
</svg>
</button>
{/* Save prompt (bookmark) */}
<button
onClick={() => {
const defaultName = text.trim().slice(0, 32);
setSavePromptName(defaultName || "");
setSavePromptError(null);
setSavePromptOpen((v) => !v);
}}
disabled={!isConnected || !text.trim()}
className={`flex items-center justify-center w-8 h-8 rounded-md transition-colors ${
isConnected && text.trim()
? "text-cc-muted hover:text-cc-fg hover:bg-cc-hover cursor-pointer"
: "text-cc-muted opacity-30 cursor-not-allowed"
}`}
title="Save as prompt"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" className="w-4 h-4">
<path d="M4 2.75h8A1.25 1.25 0 0113.25 4v9.25L8 10.5l-5.25 2.75V4A1.25 1.25 0 014 2.75z" />
</svg>
</button>
{/* Mode toggle */}
<button
onClick={toggleMode}
disabled={!isConnected}
className={`flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-[12px] font-semibold transition-all border select-none shrink-0 ${
!isConnected
? "opacity-30 cursor-not-allowed text-cc-muted border-transparent"
: isPlan
? "text-cc-primary border-cc-primary/30 bg-cc-primary/8 hover:bg-cc-primary/12 cursor-pointer"
: "text-cc-muted border-cc-border hover:text-cc-fg hover:bg-cc-hover cursor-pointer"
}`}
title="Toggle mode (Shift+Tab)"
>
{isPlan ? (
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<rect x="3" y="3" width="3.5" height="10" rx="0.75" />
<rect x="9.5" y="3" width="3.5" height="10" rx="0.75" />
</svg>
) : (
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<path d="M2.5 4l4 4-4 4" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" fill="none" />
<path d="M8.5 4l4 4-4 4" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" fill="none" />
</svg>
)}
<span>{modeLabel}</span>
</button>
{/* Spacer */}
<div className="flex-1" />
{/* Model switcher */}
<ModelSwitcher sessionId={sessionId} />
{/* Send/stop */}
{isRunning ? (
<button
onClick={handleInterrupt}
className="flex items-center justify-center w-9 h-9 rounded-lg bg-cc-error/10 hover:bg-cc-error/20 text-cc-error transition-colors cursor-pointer"
title="Stop generation"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<rect x="3" y="3" width="10" height="10" rx="1" />
</svg>
</button>
) : (
<button
onClick={handleSend}
disabled={!canSend}
className={`flex items-center justify-center w-9 h-9 rounded-full transition-all duration-200 ${
canSend
? "bg-cc-primary hover:bg-cc-primary-hover hover:scale-105 text-white cursor-pointer shadow-[0_4px_16px_rgba(217,119,87,0.25)]"
: "bg-cc-hover text-cc-muted cursor-not-allowed"
}`}
title="Send message"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<path d="M3 2l11 6-11 6V9.5l7-1.5-7-1.5V2z" />
</svg>
</button>
)}
</div>
</div>
</div>
</div>
);
}