forked from React-Group/interstellar_ai
first part of refactoring
This commit is contained in:
parent
f09ffc6b86
commit
6a6954023c
9 changed files with 140 additions and 172 deletions
|
@ -1,34 +0,0 @@
|
|||
import React, { useState, useRef } from 'react'
|
||||
|
||||
export const AudioRecorder= () => {
|
||||
const [isRecording, setIsRecording] = useState(false)
|
||||
const [audioURL, setAudioURL] = useState<string | null>(null)
|
||||
const mediaRecorderRef = useRef<MediaRecorder | null>(null)
|
||||
const audioChunks = useRef<Blob[]>([])
|
||||
|
||||
const startRecording = async () => {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
|
||||
const mediaRecorder = new MediaRecorder(stream)
|
||||
mediaRecorderRef.current = mediaRecorder
|
||||
|
||||
mediaRecorder.ondataavailable = (event) => {
|
||||
audioChunks.current.push(event.data)
|
||||
}
|
||||
|
||||
mediaRecorder.onstop = () => {
|
||||
const audioBlob = new Blob(audioChunks.current, { type: "audio/wav" })
|
||||
const url = URL.createObjectURL(audioBlob)
|
||||
setAudioURL(url)
|
||||
audioChunks.current = []
|
||||
}
|
||||
|
||||
mediaRecorder.start()
|
||||
setIsRecording(true)
|
||||
|
||||
}
|
||||
|
||||
const stopRecording = () => {
|
||||
mediaRecorderRef.current?.stop()
|
||||
setIsRecording(false)
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
type ChatMessage = {
|
||||
/* type ChatMessage = {
|
||||
name: string;
|
||||
messages: any;
|
||||
timestamp: number;
|
||||
|
@ -25,3 +25,4 @@ function removeMessageFromHistory(timestamp: number): void {
|
|||
console.log(`Message not found with timestamp: ${timestamp}`);
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -1,12 +1,9 @@
|
|||
"use client"
|
||||
import React, { use, useEffect, useRef, useState } from "react";
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import ConversationFrontend from '../components/ConversationFrontend';
|
||||
import InputFrontend from "../components/InputFrontend";
|
||||
import { sendToVoiceRecognition } from "./voice_backend"
|
||||
import axios from "axios";
|
||||
import { resolve } from "path";
|
||||
import { FFmpeg } from "@ffmpeg/ffmpeg";
|
||||
import { fetchFile, toBlobURL } from "@ffmpeg/util"
|
||||
|
||||
const InputOutputBackend: React.FC = () => {
|
||||
// # variables
|
||||
|
@ -66,7 +63,6 @@ const InputOutputBackend: React.FC = () => {
|
|||
const [accessToken, setAccessToken] = useState("")
|
||||
const postWorkerRef = useRef<Worker | null>(null)
|
||||
const getWorkerRef = useRef<Worker | null>(null)
|
||||
const [liveMessage, setLiveMessage] = useState("")
|
||||
const [inputMessage, setInputMessage] = useState<string>("")
|
||||
const [inputDisabled, setInputDisabled] = useState(false)
|
||||
const [isRecording, setIsRecording] = useState(false)
|
||||
|
@ -128,7 +124,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
const data = event.data
|
||||
|
||||
if (event.data == "error") {
|
||||
setLiveMessage("error getting AI response: " + data.error)
|
||||
console.log("Error getting ai message.")
|
||||
} else {
|
||||
console.log("Received data:", data);
|
||||
editLastMessage(data.response)
|
||||
|
@ -176,7 +172,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
if (postWorkerRef.current) {
|
||||
addMessage("user", inputValue)
|
||||
const type = localStorage.getItem('type')
|
||||
var api_key: string = ""
|
||||
let api_key: string = ""
|
||||
if (type != null && type != 'local') {
|
||||
const try_key = localStorage.getItem(type)
|
||||
if (try_key) {
|
||||
|
@ -241,7 +237,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
}
|
||||
|
||||
const handleResendClick = () => {
|
||||
var temporary_message = messages[messages.length - 2]['content']
|
||||
const temporary_message = messages[messages.length - 2]['content']
|
||||
const updatedMessages = messages.slice(0, -2)
|
||||
setMessages(updatedMessages)
|
||||
endGetWorker()
|
||||
|
@ -251,7 +247,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
}
|
||||
|
||||
const handleEditClick = () => {
|
||||
let newestMessage = messages[messages.length - 2].content
|
||||
const newestMessage = messages[messages.length - 2].content
|
||||
setInputMessage(newestMessage)
|
||||
const updatedMessages = messages.slice(0, messages.length - 2)
|
||||
setMessages(updatedMessages)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { Settings } from 'electron'
|
||||
import React from 'react'
|
||||
/* import { Settings } from 'electron'
|
||||
|
||||
type Message = {
|
||||
role: string
|
||||
|
@ -14,7 +13,7 @@ type Chat = {
|
|||
type Data = {
|
||||
chats: Chat[]
|
||||
settings: Settings[]
|
||||
}
|
||||
} */
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ to check if the request was accepted or declined, check response.data.response,
|
|||
const apiURL = new URL("http://localhost:5000/interstellar_ai/db")
|
||||
apiURL.hostname = window.location.hostname;
|
||||
|
||||
export const sendToDatabase = async (data: any): Promise<boolean> => {
|
||||
export const sendToDatabase = async (data: object): Promise<boolean> => {
|
||||
try {
|
||||
const response = await axios.post(apiURL.href, data);
|
||||
const status = response.data.status;
|
||||
|
@ -30,11 +30,12 @@ export const sendToDatabase = async (data: any): Promise<boolean> => {
|
|||
return success;
|
||||
} catch (error) {
|
||||
postMessage({ status: 500, success: false });
|
||||
console.log(error)
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const sendToDatabaseAndGetString = async (data: any): Promise<string> => {
|
||||
export const sendToDatabaseAndGetString = async (data: object): Promise<string> => {
|
||||
try {
|
||||
const response = await axios.post(apiURL.href, data);
|
||||
const status = response.data.status;
|
||||
|
@ -43,6 +44,7 @@ export const sendToDatabaseAndGetString = async (data: any): Promise<string> =>
|
|||
return success;
|
||||
} catch (error) {
|
||||
postMessage({ status: 500, success: false });
|
||||
console.log(error)
|
||||
return "false";
|
||||
}
|
||||
};
|
||||
|
@ -99,7 +101,7 @@ export const getName = async (usernameOrEmail: string, password: string): Promis
|
|||
return await sendToDatabaseAndGetString(data);
|
||||
};
|
||||
|
||||
export const changeData = async (usernameOrEmail: string, password: string, newData: any) => {
|
||||
export const changeData = async (usernameOrEmail: string, password: string, newData: object) => {
|
||||
const data = {
|
||||
action: "change_settings",
|
||||
username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail,
|
||||
|
@ -117,7 +119,7 @@ export const checkCredentials = async (usernameOrEmail: string, password: string
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
var sendBack = await sendToDatabase(data);
|
||||
const sendBack = await sendToDatabase(data);
|
||||
if (sendBack) {
|
||||
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password))
|
||||
localStorage.setItem("accountName", await getName(usernameOrEmail, password))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue