interstellar_ai/app/InputOutputHandler.tsx

110 lines
3 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";
import ConversationFrontend from "./ConversationFrontend";
import InputFrontend from "./InputFrontend";
const handleMicClick = () => {
console.log('Mic clicked!');
// Do something when the mic button is clicked
};
const handleResendClick = () => {
console.log('Resend button clicked');
// Handle resend action
};
const handleEditClick = () => {
console.log('Edit button clicked');
// Handle edit action
};
const handleCopyClick = () => {
console.log('Copy button clicked');
// Handle copy action
};
2024-09-19 13:02:56 +02:00
const InputOutputBackend: React.FC = () => {
const [accessToken, setAccessToken] = useState("")
2024-09-19 15:56:26 +02:00
const workerRef = useRef<Worker | null>(null)
2024-09-20 09:17:28 +02:00
type Message = {
role: string
content: string
}
type SystemPrompt = {
}
2024-09-19 15:56:26 +02:00
2024-09-20 09:17:28 +02:00
const handleSendClick = (message:string) => {
var system:Message = {role:"system" ,content:"You are a helpful assistant that gives short answers."}
2024-09-19 15:56:26 +02:00
2024-09-20 09:17:28 +02:00
addMessage("user",message);
HandlePostRequest(messages, "phi3.5", system)
2024-09-19 15:56:26 +02:00
};
const [messages, setMessages] = useState([
2024-09-20 09:17:28 +02:00
{ role:"assistant", content:'Hello. I\'m Your AI Virtual Assistant' }
2024-09-19 15:56:26 +02:00
]);
2024-09-20 09:17:28 +02:00
const addMessage = (role:string ,content: string) => {
setMessages((prevMessages) => [...prevMessages, {role,content}]);
2024-09-19 15:56:26 +02:00
};
2024-09-19 13:02:56 +02:00
2024-09-20 09:17:28 +02:00
2024-09-19 13:02:56 +02:00
useEffect(() => {
2024-09-19 15:56:26 +02:00
workerRef.current = new Worker(new URL("./ProcessAPI.js", import.meta.url))
workerRef.current.postMessage({})
workerRef.current.onmessage = (e) => {
setAccessToken(e.data)
2024-09-19 13:02:56 +02:00
}
2024-09-19 15:56:26 +02:00
2024-09-19 13:02:56 +02:00
return () => {
2024-09-19 15:56:26 +02:00
if (workerRef.current) {
workerRef.current.terminate()
}
2024-09-19 13:02:56 +02:00
}
2024-09-19 15:56:26 +02:00
},[])
2024-09-19 13:02:56 +02:00
2024-09-20 09:17:28 +02:00
const HandleGetRequest = (messages: Message[], ai_model: string, system_prompt: Message) => {
2024-09-19 15:56:26 +02:00
if (workerRef.current) {
2024-09-20 09:17:28 +02:00
workerRef.current.postMessage({ functionName: "getResponse", access_token: accessToken, messages: messages, ai_model: ai_model, system_prompt: system_prompt })
2024-09-19 15:56:26 +02:00
workerRef.current.onmessage = (e) => {
2024-09-20 09:17:28 +02:00
addMessage("assistant",e.data)
2024-09-19 15:56:26 +02:00
}
}
2024-09-19 13:02:56 +02:00
}
2024-09-20 09:17:28 +02:00
const HandlePostRequest = (messages: Message[], ai_model: string, system_prompt: Message) => {
2024-09-19 15:56:26 +02:00
if (workerRef.current) {
2024-09-20 09:17:28 +02:00
workerRef.current.postMessage({ functionName: "postRequest", access_token: accessToken, messages: messages, ai_model: ai_model, system_prompt: system_prompt })
2024-09-19 15:56:26 +02:00
workerRef.current.onmessage = (e) => {
2024-09-20 09:17:28 +02:00
HandleGetRequest(messages,ai_model,system_prompt)
2024-09-19 15:56:26 +02:00
}
}
2024-09-19 13:02:56 +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