interstellar_ai/app/backend/InputOutputHandler.tsx

168 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-09-19 13:02:56 +02:00
"use client"
2024-09-19 15:56:26 +02:00
import React, { useEffect, useRef, useState } from "react";
2024-09-20 10:34:16 +02:00
import ConversationFrontend from "../components/ConversationFrontend";
import InputFrontend from "../components/InputFrontend";
2024-09-23 11:11:45 +02:00
import axios from "axios";
2024-09-24 09:51:16 +02:00
let sendable = true
2024-09-19 13:02:56 +02:00
const InputOutputBackend: React.FC = () => {
2024-09-23 16:34:55 +02:00
type Message = {
role: string
content:string
}
2024-09-23 11:11:45 +02:00
const [accessToken, setAccessToken] = useState("")
const postWorkerRef = useRef<Worker| null>(null)
const getWorkerRef = useRef<Worker | null>(null)
2024-09-23 16:34:55 +02:00
const [messages, setMessages] = useState<Message[]>([{role:"assistant", content:"Hello! How can I help you?"}])
2024-09-23 11:11:45 +02:00
const [liveMessage, setLiveMessage] = useState("")
2024-09-23 16:34:55 +02:00
console.log(messages);
2024-09-23 11:11:45 +02:00
useEffect(() => {
2024-09-23 14:54:13 +02:00
console.log("getting access");
2024-09-23 11:11:45 +02:00
axios.get("http://localhost:5000/interstellar/api/ai_create")
.then(response => {
2024-09-23 14:54:13 +02:00
setAccessToken(response.data.access_token)
console.log(response.data.access_token);
2024-09-23 11:11:45 +02:00
})
.catch(error => {
console.log("error:", error.message);
2024-09-20 09:58:13 +02:00
2024-09-23 11:11:45 +02:00
})
postWorkerRef.current = new Worker(new URL("./threads/PostWorker.js", import.meta.url))
postWorkerRef.current.onmessage = (event) => {
const status = event.data.status
if (status == 200) {
2024-09-24 09:51:16 +02:00
sendable = true
2024-09-23 11:11:45 +02:00
endGetWorker()
} else if (status == 500) {
2024-09-24 09:51:16 +02:00
sendable = true
2024-09-23 11:11:45 +02:00
if (getWorkerRef.current) {
addMessage("assistant", "There was an Error with the AI response")
getWorkerRef.current.postMessage("terminate")
getWorkerRef.current.terminate()
}
}
}
return () => {
if (postWorkerRef.current) {
postWorkerRef.current.terminate()
}
if (getWorkerRef.current) {
getWorkerRef.current.postMessage("terminate")
getWorkerRef.current.terminate()
}
}
},[])
const startGetWorker = () => {
if (!getWorkerRef.current) {
getWorkerRef.current = new Worker(new URL("./threads/GetWorker.js", import.meta.url))
2024-09-23 14:54:13 +02:00
getWorkerRef.current.postMessage({ action: "start", access_token:accessToken})
2024-09-23 11:11:45 +02:00
2024-09-23 16:34:55 +02:00
addMessage("assistant","")
2024-09-23 11:11:45 +02:00
getWorkerRef.current.onmessage = (event) => {
const data = event.data
2024-09-23 14:54:13 +02:00
if (event.data == "error") {
2024-09-23 11:11:45 +02:00
setLiveMessage("error getting AI response: "+ data.error)
} else {
console.log("Received data:", data);
2024-09-23 16:34:55 +02:00
editLastMessage(data.response)
2024-09-23 11:11:45 +02:00
}
}
getWorkerRef.current.onerror = (error) => {
console.error("Worker error:", error)
}
}
}
const endGetWorker = () => {
if (getWorkerRef.current) {
2024-09-23 14:54:13 +02:00
getWorkerRef.current.postMessage({action:"terminate"})
2024-09-23 11:11:45 +02:00
getWorkerRef.current.terminate()
2024-09-23 14:54:13 +02:00
console.log(messages);
2024-09-23 11:11:45 +02:00
}
}
2024-09-23 16:34:55 +02:00
const editLastMessage = (newContent: string) => {
setMessages((prevMessages) => {
const updatedMessages = prevMessages.slice(); // Create a shallow copy of the current messages
if (updatedMessages.length > 0) {
const lastMessage = updatedMessages[updatedMessages.length - 1];
updatedMessages[updatedMessages.length - 1] = {
...lastMessage, // Keep the existing role and other properties
content: newContent, // Update only the content
};
}
return updatedMessages; // Return the updated array
});
};
2024-09-23 11:11:45 +02:00
const addMessage = (role: string, content: string) => {
setMessages(previous => [...previous,{role,content}])
}
const handleSendClick = (inputValue: string) => {
2024-09-24 10:22:50 +02:00
if (inputValue != "") {
if (sendable) {
sendable=false
if (postWorkerRef.current) {
addMessage("user", inputValue)
console.log("input:",inputValue);
postWorkerRef.current.postMessage({messages:[...messages, { role: "user", content: inputValue }], ai_model:"phi3.5", access_token:accessToken})
startGetWorker()
}
2024-09-24 09:51:16 +02:00
}
2024-09-23 11:11:45 +02:00
}
}
2024-09-19 15:56:26 +02:00
2024-09-23 11:11:45 +02:00
const handleMicClick = () => {
// do stuff
}
2024-09-19 15:56:26 +02:00
2024-09-23 11:11:45 +02:00
const handleResendClick = () => {
// do stuff
}
2024-09-19 15:56:26 +02:00
2024-09-23 11:11:45 +02:00
const handleEditClick = () => {
// do stuff
}
2024-09-19 13:02:56 +02:00
2024-09-23 11:11:45 +02:00
const handleCopyClick = () => {
// do stuff
}
2024-09-20 09:17:28 +02:00
2024-09-19 15:56:26 +02:00
return (
<div>
<ConversationFrontend
messages={messages}
onResendClick={handleResendClick}
onEditClick={handleEditClick}
onCopyClick={handleCopyClick}
/>
<InputFrontend
message=""
onSendClick={handleSendClick}
onMicClick={handleMicClick}
/>
</div>
2024-09-20 09:17:28 +02:00
)
2024-09-19 13:02:56 +02:00
}
export default InputOutputBackend