bu
This commit is contained in:
parent
6ab09eb94e
commit
4b3ecfb122
5 changed files with 15 additions and 95 deletions
|
|
@ -17,8 +17,6 @@ async function AIResponse(userInputArray) {
|
||||||
return response.text;
|
return response.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
let userInput = [];
|
|
||||||
|
|
||||||
export default function ChatLayout() {
|
export default function ChatLayout() {
|
||||||
const [messages, setMessages] = useState([
|
const [messages, setMessages] = useState([
|
||||||
{
|
{
|
||||||
|
|
@ -27,6 +25,21 @@ export default function ChatLayout() {
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
async function handleSend(text) {
|
||||||
|
const userMsg = { role: "user", content: text };
|
||||||
|
userInput.push(text);
|
||||||
|
const res = await AIResponse(userInput);
|
||||||
|
setMessages((s) => [...s, userMsg]);
|
||||||
|
setTimeout(() => {
|
||||||
|
setMessages((s) => [...s, { role: "assistant", content: res }]);
|
||||||
|
}, 600);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDeleteAll() {
|
||||||
|
if (!window.confirm("Delete all messages?")) return;
|
||||||
|
setMessages([]);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col flex-start w-full max-w-3xl gap-4 p-4">
|
<div className="flex flex-col flex-start w-full max-w-3xl gap-4 p-4">
|
||||||
<ChatHeader onDeleteAll={handleDeleteAll} />
|
<ChatHeader onDeleteAll={handleDeleteAll} />
|
||||||
|
|
@ -35,42 +48,3 @@ export default function ChatLayout() {
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addMessage(role, content) {
|
|
||||||
const msg = { role, content };
|
|
||||||
setMessages((s) => [...s, msg]);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleSend(text) {
|
|
||||||
const { setMessages } = useChatBackend();
|
|
||||||
const userMsg = { role: "user", content: text };
|
|
||||||
|
|
||||||
switch (setMessages) {
|
|
||||||
case "gemini":
|
|
||||||
userInput.push(text);
|
|
||||||
const res = await AIResponse(userInput);
|
|
||||||
setMessages((s) => [...s, userMsg]);
|
|
||||||
setTimeout(() => {
|
|
||||||
setMessages((s) => [...s, { role: "assistant", content: res }]);
|
|
||||||
}, 600);
|
|
||||||
break;
|
|
||||||
case "rust":
|
|
||||||
setMessages((s) => [...s, userMsg]);
|
|
||||||
|
|
||||||
// fake assistant reply after short delay
|
|
||||||
setTimeout(() => {
|
|
||||||
setMessages((s) => [
|
|
||||||
...s,
|
|
||||||
{ role: "assistant", content: `You said: ${text}` },
|
|
||||||
]);
|
|
||||||
}, 600);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleDeleteAll() {
|
|
||||||
if (!window.confirm("Delete all messages?")) return;
|
|
||||||
setMessages([]);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
import React from "react";
|
|
||||||
import { motion } from "motion/react";
|
|
||||||
import { Cpu } from "lucide-react";
|
|
||||||
import { useChatBackend } from "src/context/chat-backend-context";
|
|
||||||
|
|
||||||
export default function BackendToggle({ className }) {
|
|
||||||
const { backend, toggleBackend } = useChatBackend();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<motion.button
|
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
toggleBackend();
|
|
||||||
}}
|
|
||||||
className={`bg-gray-700 p-2 rounded-2xl file-input border-2 border-gray-600 text-md flex items-center gap-2 ${className || ""}`}
|
|
||||||
whileHover={{ scale: 1.05 }}
|
|
||||||
whileTap={{ scale: 0.95 }}
|
|
||||||
title={`${backend}`}
|
|
||||||
>
|
|
||||||
<Cpu className="w-4 h-4" />
|
|
||||||
<span className="uppercase">
|
|
||||||
{backend === "rust" ? "rust" : "gemini"}
|
|
||||||
</span>
|
|
||||||
</motion.button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
import React, { useState, useRef, useEffect } from "react";
|
import React, { useState, useRef, useEffect } from "react";
|
||||||
import DownButton from "src/components/ui/button/down-button";
|
import DownButton from "src/components/ui/button/down-button";
|
||||||
import BackendToggle from "src/components/ui/button/backend-toggle";
|
|
||||||
import ChatBackendContext from "src/context/chat-backend-context";
|
|
||||||
import { motion } from "motion/react";
|
import { motion } from "motion/react";
|
||||||
import { BotMessageSquare } from "lucide-react";
|
import { BotMessageSquare } from "lucide-react";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
import React, { createContext, useContext, useState } from "react";
|
|
||||||
|
|
||||||
const ChatBackendContext = createContext(null);
|
|
||||||
|
|
||||||
export function ChatBackendProvider({ children }) {
|
|
||||||
const [backend, setBackend] = useState("gemini"); // default
|
|
||||||
|
|
||||||
function toggleBackend() {
|
|
||||||
setBackend((b) => (b === "gemini" ? "rust" : "gemini"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<ChatBackendContext.Provider value={{ backend, setBackend, toggleBackend }}>
|
|
||||||
{children}
|
|
||||||
</ChatBackendContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useChatBackend() {
|
|
||||||
const ctx = useContext(ChatBackendContext);
|
|
||||||
if (!ctx) throw new Error("useChatBackend must be used within ChatBackendProvider");
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default ChatBackendContext;
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import { createRoot } from "react-dom/client";
|
import { createRoot } from "react-dom/client";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
import App from "./app/index.jsx";
|
import App from "./app/index.jsx";
|
||||||
import { ChatBackendProvider } from "./context/chat-backend-context";
|
|
||||||
|
|
||||||
createRoot(document.getElementById("root")).render(<App />);
|
createRoot(document.getElementById("root")).render(<App />);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue