2025-12-24 17:17:35 +00:00
|
|
|
import { useState, useRef, useEffect } from "react";
|
|
|
|
|
import { invoke } from "@tauri-apps/api/core";
|
|
|
|
|
import Markdown from "react-markdown";
|
|
|
|
|
import { Message, ProviderConfig } from "../types";
|
|
|
|
|
|
|
|
|
|
export function Chat() {
|
|
|
|
|
const [messages, setMessages] = useState<Message[]>([]);
|
|
|
|
|
const [input, setInput] = useState("");
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [model, setModel] = useState("llama3.1"); // Default local model
|
2025-12-24 17:32:46 +00:00
|
|
|
const [enableTools, setEnableTools] = useState(true);
|
2025-12-25 12:21:58 +00:00
|
|
|
const [availableModels, setAvailableModels] = useState<string[]>([]);
|
2025-12-24 17:17:35 +00:00
|
|
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
2025-12-25 12:21:58 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
invoke<string[]>("get_ollama_models")
|
|
|
|
|
.then((models) => {
|
|
|
|
|
if (models.length > 0) {
|
|
|
|
|
setAvailableModels(models);
|
|
|
|
|
// If we have models and current one isn't valid, switch to first
|
|
|
|
|
if (!models.includes(model)) {
|
|
|
|
|
setModel(models[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => console.error(err));
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-12-24 17:17:35 +00:00
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(scrollToBottom, [messages]);
|
|
|
|
|
|
|
|
|
|
const sendMessage = async () => {
|
|
|
|
|
if (!input.trim() || loading) return;
|
|
|
|
|
|
|
|
|
|
const userMsg: Message = { role: "user", content: input };
|
|
|
|
|
const newHistory = [...messages, userMsg];
|
2025-12-24 17:32:46 +00:00
|
|
|
|
2025-12-24 17:17:35 +00:00
|
|
|
setMessages(newHistory);
|
|
|
|
|
setInput("");
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const config: ProviderConfig = {
|
|
|
|
|
provider: "ollama",
|
|
|
|
|
model: model,
|
|
|
|
|
base_url: "http://localhost:11434",
|
2025-12-24 17:32:46 +00:00
|
|
|
enable_tools: enableTools,
|
2025-12-24 17:17:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Invoke backend chat command
|
|
|
|
|
// The backend returns the *new* messages (assistant response + tool outputs)
|
|
|
|
|
const response = await invoke<Message[]>("chat", {
|
|
|
|
|
messages: newHistory,
|
|
|
|
|
config: config,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setMessages((prev) => [...prev, ...response]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
setMessages((prev) => [
|
|
|
|
|
...prev,
|
|
|
|
|
{ role: "assistant", content: `**Error:** ${e}` },
|
|
|
|
|
]);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-12-24 17:32:46 +00:00
|
|
|
<div
|
|
|
|
|
className="chat-container"
|
|
|
|
|
style={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
height: "100%",
|
|
|
|
|
maxWidth: "800px",
|
|
|
|
|
margin: "0 auto",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-12-24 17:17:35 +00:00
|
|
|
{/* Settings Bar */}
|
2025-12-24 17:32:46 +00:00
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
padding: "10px",
|
|
|
|
|
borderBottom: "1px solid #ddd",
|
|
|
|
|
display: "flex",
|
|
|
|
|
gap: "10px",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-12-24 17:17:35 +00:00
|
|
|
<label>Ollama Model:</label>
|
2025-12-25 12:21:58 +00:00
|
|
|
{availableModels.length > 0 ? (
|
|
|
|
|
<select
|
|
|
|
|
value={model}
|
|
|
|
|
onChange={(e) => setModel(e.target.value)}
|
|
|
|
|
style={{ padding: "5px" }}
|
|
|
|
|
>
|
|
|
|
|
{availableModels.map((m) => (
|
|
|
|
|
<option key={m} value={m}>
|
|
|
|
|
{m}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
|
|
|
|
) : (
|
|
|
|
|
<input
|
|
|
|
|
value={model}
|
|
|
|
|
onChange={(e) => setModel(e.target.value)}
|
|
|
|
|
placeholder="e.g. llama3, mistral"
|
|
|
|
|
style={{ padding: "5px" }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-12-24 17:32:46 +00:00
|
|
|
<label
|
|
|
|
|
style={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
gap: "5px",
|
|
|
|
|
marginLeft: "10px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={enableTools}
|
|
|
|
|
onChange={(e) => setEnableTools(e.target.checked)}
|
|
|
|
|
/>
|
|
|
|
|
Enable Tools
|
|
|
|
|
</label>
|
2025-12-24 17:17:35 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Messages Area */}
|
2025-12-24 17:32:46 +00:00
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
flex: 1,
|
|
|
|
|
overflowY: "auto",
|
|
|
|
|
padding: "20px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: "15px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-12-24 17:17:35 +00:00
|
|
|
{messages.map((msg, idx) => (
|
2025-12-24 17:32:46 +00:00
|
|
|
<div
|
|
|
|
|
key={idx}
|
2025-12-24 17:17:35 +00:00
|
|
|
className={`message ${msg.role}`}
|
|
|
|
|
style={{
|
|
|
|
|
alignSelf: msg.role === "user" ? "flex-end" : "flex-start",
|
|
|
|
|
maxWidth: "80%",
|
|
|
|
|
padding: "10px 15px",
|
|
|
|
|
borderRadius: "10px",
|
2025-12-24 17:32:46 +00:00
|
|
|
background:
|
|
|
|
|
msg.role === "user"
|
|
|
|
|
? "#007AFF"
|
|
|
|
|
: msg.role === "tool"
|
|
|
|
|
? "#f0f0f0"
|
|
|
|
|
: "#E5E5EA",
|
2025-12-24 17:17:35 +00:00
|
|
|
color: msg.role === "user" ? "white" : "black",
|
|
|
|
|
border: msg.role === "tool" ? "1px solid #ccc" : "none",
|
|
|
|
|
fontFamily: msg.role === "tool" ? "monospace" : "inherit",
|
|
|
|
|
fontSize: msg.role === "tool" ? "0.9em" : "1em",
|
2025-12-24 17:32:46 +00:00
|
|
|
whiteSpace: msg.role === "tool" ? "pre-wrap" : "normal",
|
2025-12-24 17:17:35 +00:00
|
|
|
}}
|
|
|
|
|
>
|
2025-12-24 17:32:46 +00:00
|
|
|
<strong>
|
|
|
|
|
{msg.role === "user"
|
|
|
|
|
? "You"
|
|
|
|
|
: msg.role === "tool"
|
|
|
|
|
? "Tool Output"
|
|
|
|
|
: "Agent"}
|
|
|
|
|
</strong>
|
2025-12-24 17:17:35 +00:00
|
|
|
{msg.role === "tool" ? (
|
2025-12-24 17:32:46 +00:00
|
|
|
<div style={{ maxHeight: "200px", overflow: "auto" }}>
|
|
|
|
|
{msg.content}
|
|
|
|
|
</div>
|
2025-12-24 17:17:35 +00:00
|
|
|
) : (
|
|
|
|
|
<Markdown>{msg.content}</Markdown>
|
|
|
|
|
)}
|
2025-12-24 17:32:46 +00:00
|
|
|
|
2025-12-24 17:17:35 +00:00
|
|
|
{/* Show Tool Calls if present */}
|
|
|
|
|
{msg.tool_calls && (
|
2025-12-24 17:32:46 +00:00
|
|
|
<div
|
|
|
|
|
style={{ marginTop: "10px", fontSize: "0.85em", color: "#666" }}
|
|
|
|
|
>
|
2025-12-24 17:17:35 +00:00
|
|
|
{msg.tool_calls.map((tc, i) => (
|
2025-12-24 17:32:46 +00:00
|
|
|
<div
|
|
|
|
|
key={i}
|
|
|
|
|
style={{
|
|
|
|
|
background: "rgba(0,0,0,0.05)",
|
|
|
|
|
padding: "5px",
|
|
|
|
|
borderRadius: "4px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
🛠{" "}
|
|
|
|
|
<code>
|
|
|
|
|
{tc.function.name}({tc.function.arguments})
|
|
|
|
|
</code>
|
2025-12-24 17:17:35 +00:00
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
2025-12-24 17:32:46 +00:00
|
|
|
{loading && (
|
|
|
|
|
<div style={{ alignSelf: "flex-start", color: "#888" }}>
|
|
|
|
|
Thinking...
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-12-24 17:17:35 +00:00
|
|
|
<div ref={messagesEndRef} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Input Area */}
|
2025-12-24 17:32:46 +00:00
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
padding: "20px",
|
|
|
|
|
borderTop: "1px solid #ddd",
|
|
|
|
|
display: "flex",
|
|
|
|
|
gap: "10px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-12-24 17:17:35 +00:00
|
|
|
<input
|
|
|
|
|
value={input}
|
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
|
|
|
|
onKeyDown={(e) => e.key === "Enter" && sendMessage()}
|
|
|
|
|
placeholder="Ask the agent to do something..."
|
2025-12-24 17:32:46 +00:00
|
|
|
style={{
|
|
|
|
|
flex: 1,
|
|
|
|
|
padding: "10px",
|
|
|
|
|
borderRadius: "4px",
|
|
|
|
|
border: "1px solid #ccc",
|
|
|
|
|
}}
|
2025-12-24 17:17:35 +00:00
|
|
|
disabled={loading}
|
|
|
|
|
/>
|
2025-12-24 17:32:46 +00:00
|
|
|
<button
|
|
|
|
|
onClick={sendMessage}
|
|
|
|
|
disabled={loading}
|
|
|
|
|
style={{ padding: "10px 20px" }}
|
|
|
|
|
>
|
2025-12-24 17:17:35 +00:00
|
|
|
Send
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|