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
|
import { useState, useCallback, useRef, useEffect } from "react";
import { chatWithFile, type BodyElement, type LlmModel, type ChatMessage } from "../../lib/api";
import { SimpleMarkdown } from "../SimpleMarkdown";
interface CliInputProps {
fileId: string;
onUpdate: (body: BodyElement[], summary: string | null) => void;
}
interface Message {
id: string;
type: "user" | "assistant" | "error";
content: string;
toolCalls?: { name: string; success: boolean; message: string }[];
}
const MODEL_OPTIONS: { value: LlmModel; label: string }[] = [
{ value: "claude-opus", label: "Claude Opus" },
{ value: "claude-sonnet", label: "Claude Sonnet" },
{ value: "groq", label: "Groq Kimi" },
];
export function CliInput({ fileId, onUpdate }: CliInputProps) {
const [input, setInput] = useState("");
const [loading, setLoading] = useState(false);
const [messages, setMessages] = useState<Message[]>([]);
const [expanded, setExpanded] = useState(false);
const [model, setModel] = useState<LlmModel>("claude-opus");
// Track conversation history for context continuity
const [conversationHistory, setConversationHistory] = useState<ChatMessage[]>([]);
const inputRef = useRef<HTMLInputElement>(null);
const messagesRef = useRef<HTMLDivElement>(null);
// Auto-scroll to bottom when messages change
useEffect(() => {
if (messagesRef.current) {
messagesRef.current.scrollTop = messagesRef.current.scrollHeight;
}
}, [messages]);
const handleSubmit = useCallback(
async (e: React.FormEvent) => {
e.preventDefault();
if (!input.trim() || loading) return;
const userMessage = input.trim();
setInput("");
setExpanded(true);
// Add user message
const userMsgId = Date.now().toString();
setMessages((prev) => [
...prev,
{ id: userMsgId, type: "user", content: userMessage },
]);
setLoading(true);
try {
// Send request with conversation history for context
const response = await chatWithFile(fileId, userMessage, model, conversationHistory);
// Add assistant response
const assistantMsgId = (Date.now() + 1).toString();
setMessages((prev) => [
...prev,
{
id: assistantMsgId,
type: "assistant",
content: response.response,
toolCalls: response.toolCalls.map((tc) => ({
name: tc.name,
success: tc.result.success,
message: tc.result.message,
})),
},
]);
// Update conversation history for next request
setConversationHistory((prev) => [
...prev,
{ role: "user", content: userMessage },
{ role: "assistant", content: response.response },
]);
// Update parent with new body/summary
onUpdate(response.updatedBody, response.updatedSummary);
} catch (err) {
const errorMsgId = (Date.now() + 1).toString();
setMessages((prev) => [
...prev,
{
id: errorMsgId,
type: "error",
content: err instanceof Error ? err.message : "An error occurred",
},
]);
} finally {
setLoading(false);
inputRef.current?.focus();
}
},
[input, loading, fileId, model, onUpdate, conversationHistory]
);
const clearMessages = useCallback(() => {
setMessages([]);
setConversationHistory([]);
}, []);
return (
<div className="border-t border-[rgba(117,170,252,0.35)] bg-[#0d1b2d]">
{/* Messages Panel (expandable) */}
{expanded && messages.length > 0 && (
<div
ref={messagesRef}
className="max-h-48 overflow-y-auto p-3 space-y-2 border-b border-[rgba(117,170,252,0.2)]"
>
{messages.map((msg) => (
<div key={msg.id} className="font-mono text-xs">
{msg.type === "user" && (
<div className="flex gap-2">
<span className="text-[#9bc3ff]">></span>
<span className="text-white/80">{msg.content}</span>
</div>
)}
{msg.type === "assistant" && (
<div className="pl-4 space-y-1">
<SimpleMarkdown content={msg.content} className="text-[#75aafc]" />
{msg.toolCalls && msg.toolCalls.length > 0 && (
<div className="text-[#555] text-[10px] space-y-0.5">
{msg.toolCalls.map((tc, i) => (
<div key={i}>
<span
className={
tc.success ? "text-green-500" : "text-red-400"
}
>
{tc.success ? "+" : "x"}
</span>{" "}
{tc.name}: {tc.message}
</div>
))}
</div>
)}
</div>
)}
{msg.type === "error" && (
<div className="pl-4 text-red-400">{msg.content}</div>
)}
</div>
))}
</div>
)}
{/* Input Bar */}
<form onSubmit={handleSubmit} className="flex items-center gap-2 p-3">
<select
value={model}
onChange={(e) => setModel(e.target.value as LlmModel)}
disabled={loading}
className="bg-[#0d1b2d] border border-[rgba(117,170,252,0.25)] text-[#9bc3ff] font-mono text-xs px-2 py-1 rounded-none outline-none focus:border-[#3f6fb3] disabled:opacity-50"
>
{MODEL_OPTIONS.map((opt) => (
<option key={opt.value} value={opt.value}>
{opt.label}
</option>
))}
</select>
<span className="text-[#9bc3ff] font-mono text-sm">></span>
<input
ref={inputRef}
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder={loading ? "Processing..." : "Add a heading, chart, or summary..."}
disabled={loading}
className="flex-1 bg-transparent border-none outline-none font-mono text-sm text-white placeholder-[#555]"
/>
{messages.length > 0 && (
<button
type="button"
onClick={clearMessages}
className="text-[#555] hover:text-[#9bc3ff] font-mono text-xs transition-colors"
>
clear
</button>
)}
<button
type="submit"
disabled={loading || !input.trim()}
className="px-3 py-1 font-mono text-xs text-[#9bc3ff] border border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3] disabled:opacity-50 disabled:cursor-not-allowed transition-colors uppercase"
>
{loading ? "..." : "Send"}
</button>
</form>
</div>
);
}
|