multithread trial 3
This commit is contained in:
parent
5ff6913426
commit
f587864d3c
3 changed files with 37 additions and 16 deletions
|
@ -2,20 +2,22 @@
|
|||
import React, { useEffect, useRef, useState } from "react";
|
||||
import ConversationFrontend from "../components/ConversationFrontend";
|
||||
import InputFrontend from "../components/InputFrontend";
|
||||
import { error, log } from "console";
|
||||
import axios from "axios";
|
||||
import { log } from 'console';
|
||||
|
||||
const InputOutputBackend: React.FC = () => {
|
||||
const [accessToken, setAccessToken] = useState("")
|
||||
const postWorkerRef = useRef<Worker| null>(null)
|
||||
const getWorkerRef = useRef<Worker | null>(null)
|
||||
const [messages, setMessages] = useState([{role:"system", content:"You are a helpful assistant"}])
|
||||
const [messages, setMessages] = useState([{role:"assistant", content:"Hello! How can I help you?"}])
|
||||
const [liveMessage, setLiveMessage] = useState("")
|
||||
|
||||
useEffect(() => {
|
||||
console.log("getting access");
|
||||
axios.get("http://localhost:5000/interstellar/api/ai_create")
|
||||
.then(response => {
|
||||
setAccessToken(response.data.access_token)
|
||||
setAccessToken(response.data.access_token)
|
||||
console.log(response.data.access_token);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log("error:", error.message);
|
||||
|
@ -52,12 +54,12 @@ const InputOutputBackend: React.FC = () => {
|
|||
if (!getWorkerRef.current) {
|
||||
getWorkerRef.current = new Worker(new URL("./threads/GetWorker.js", import.meta.url))
|
||||
|
||||
getWorkerRef.current.postMessage("start")
|
||||
getWorkerRef.current.postMessage({ action: "start", access_token:accessToken})
|
||||
|
||||
getWorkerRef.current.onmessage = (event) => {
|
||||
const data = event.data
|
||||
|
||||
if (data.error) {
|
||||
if (event.data == "error") {
|
||||
setLiveMessage("error getting AI response: "+ data.error)
|
||||
} else {
|
||||
console.log("Received data:", data);
|
||||
|
@ -74,8 +76,9 @@ const InputOutputBackend: React.FC = () => {
|
|||
const endGetWorker = () => {
|
||||
if (getWorkerRef.current) {
|
||||
addMessage("assistant", liveMessage)
|
||||
getWorkerRef.current.postMessage("terminate")
|
||||
getWorkerRef.current.postMessage({action:"terminate"})
|
||||
getWorkerRef.current.terminate()
|
||||
console.log(messages);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue