forked from React-Group/interstellar_ai
main (#137)
Reviewed-on: https://interstellardevelopment.org/code/code/React-Group/interstellar_ai/pulls/137 Reviewed-by: Patrick <patrick_pluto@noreply.localhost> Co-authored-by: sageTheDM <info@photofuel.tech> Co-committed-by: sageTheDM <info@photofuel.tech>
This commit is contained in:
parent
f9bce3b22a
commit
91353bd051
34 changed files with 682 additions and 567 deletions
|
@ -1,16 +1,19 @@
|
|||
// getLocalStorageData.ts
|
||||
|
||||
// Function to retrieve all items from localStorage
|
||||
export const getAllLocalStorageItems = (): Record<string, string | null> => {
|
||||
const allData: Record<string, string | null> = {};
|
||||
const allData: Record<string, string | null> = {}; // Object to hold key-value pairs from localStorage
|
||||
|
||||
// Check if localStorage is available
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
for (let i = 0; i < localStorage.length; i++) {
|
||||
const key = localStorage.key(i);
|
||||
if (key) {
|
||||
const value = localStorage.getItem(key);
|
||||
allData[key] = value;
|
||||
// Iterate through all localStorage keys
|
||||
for (let i = 0; i < localStorage.length; i++) {
|
||||
const key = localStorage.key(i); // Get the key at the current index
|
||||
if (key) {
|
||||
const value = localStorage.getItem(key); // Retrieve the value associated with the key
|
||||
allData[key] = value; // Store the key-value pair in the allData object
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return allData;
|
||||
};
|
||||
|
||||
return allData; // Return the object containing all localStorage items
|
||||
};
|
||||
|
|
|
@ -9,7 +9,7 @@ import { getWeather } from "./weather";
|
|||
import { changeHistory, getHistory } from "./database";
|
||||
|
||||
const InputOutputBackend: React.FC = () => {
|
||||
// # variables
|
||||
//#region variables
|
||||
type Message = {
|
||||
role: string
|
||||
content: string
|
||||
|
@ -37,7 +37,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
}
|
||||
|
||||
console.log(setSelectedIndex)
|
||||
|
||||
//#region useEffect
|
||||
useEffect(() => {
|
||||
setMessages(chatHistory.chats[chatHistory.selectedIndex].messages)
|
||||
}, [chatHistory.selectedIndex])
|
||||
|
@ -86,6 +86,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
}
|
||||
}, [chatHistory])
|
||||
|
||||
//#region functions
|
||||
const getWeatherHere = async () => {
|
||||
setWeatherData(await getWeather({ "unit_type": preferredMeasurement, "city": localStorage.getItem("weatherInfo") || "New York" }))
|
||||
console.log("Got the Data!")
|
||||
|
@ -104,7 +105,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
}
|
||||
setChatHistoryTriggered(true)
|
||||
}
|
||||
|
||||
//#region system-prompt
|
||||
useEffect(() => {
|
||||
console.log("creating system prompt")
|
||||
console.log(weatherData)
|
||||
|
@ -133,7 +134,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
console.log(messages)
|
||||
}, [systemMessage])
|
||||
|
||||
|
||||
//#region more variables and functions
|
||||
const conversationRef = useRef<HTMLDivElement>(null)
|
||||
const [copyClicked, setCopyClicked] = useState(false)
|
||||
const [accessToken, setAccessToken] = useState("")
|
||||
|
@ -144,7 +145,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
const [isRecording, setIsRecording] = useState(false)
|
||||
const mediaRecorderRef = useRef<MediaRecorder | null>(null)
|
||||
const audioChunks = useRef<Blob[]>([])
|
||||
|
||||
//#region chat functions
|
||||
useEffect(() => {
|
||||
getNewToken()
|
||||
|
||||
|
@ -266,7 +267,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
//#region speech recognition
|
||||
const startRecording = async (): Promise<string> => {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
const mediaRecorder = new MediaRecorder(stream);
|
||||
|
@ -309,7 +310,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
stopRecording();
|
||||
}
|
||||
};
|
||||
|
||||
//#region chat buttons
|
||||
const handleStopClick = () => {
|
||||
endGetWorker()
|
||||
getNewToken()
|
||||
|
@ -356,7 +357,7 @@ const InputOutputBackend: React.FC = () => {
|
|||
setCopyClicked(false)
|
||||
}
|
||||
|
||||
|
||||
//#region The "html" return
|
||||
return (
|
||||
<>
|
||||
<ConversationFrontend
|
||||
|
|
|
@ -1,55 +1,40 @@
|
|||
import axios from "axios";
|
||||
|
||||
/*
|
||||
This is the guide on how to user this function:
|
||||
|
||||
data should be the json containing everything relevant, the json can contain the following keys:
|
||||
|
||||
action -> contains the action you want to do, there are: create_account, change_password, get_data, change_data, check_credentials, delete_account
|
||||
username -> contains the current username, required for create_account, but can be omitted in favor of email in other requests. Preffered over email authentication.
|
||||
email -> contains the current email, required for create_account, but just like the username, it can be omitted, in favor of the other, sending both is possible too.
|
||||
password -> contains the password, required for all requests.
|
||||
new_password -> in the case you are changing your password, you will need to use this in addition to password, to specify the new password.
|
||||
data -> data contains all the data you want to store, you have to always give the entire data, because the data you give here overwrites the data in the database,
|
||||
so if you only give the chat history for example, all settings will be deleted, and if you only give settings, all chat histories will get deleted.
|
||||
|
||||
|
||||
if all went well, you will get the status 200 in response.data.status
|
||||
to check if the request was accepted or declined, check response.data.response, it will be either true or false depending on if it worked, or not.
|
||||
*/
|
||||
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/db")
|
||||
// Construct the base API URL based on the environment
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/db");
|
||||
if (typeof window !== 'undefined') {
|
||||
apiURL.hostname = window.location.hostname;
|
||||
apiURL.hostname = window.location.hostname; // Set hostname for browsers
|
||||
} else {
|
||||
apiURL.hostname = "localhost"
|
||||
apiURL.hostname = "localhost"; // Default to localhost for non-browser environments
|
||||
}
|
||||
|
||||
// Function to send data to the database and return a success status
|
||||
export const sendToDatabase = async (data: object): Promise<boolean> => {
|
||||
try {
|
||||
const response = await axios.post(apiURL.href, data);
|
||||
const status = response.data.status;
|
||||
const success = response.data.response;
|
||||
postMessage({ status, success });
|
||||
return success;
|
||||
postMessage({ status, success }); // Send status back to the main thread
|
||||
return success; // Return success status
|
||||
} catch (error) {
|
||||
postMessage({ status: 500, success: false });
|
||||
console.log(error)
|
||||
return false;
|
||||
postMessage({ status: 500, success: false }); // Handle errors
|
||||
console.log(error);
|
||||
return false; // Return false on error
|
||||
}
|
||||
};
|
||||
|
||||
// Function to send data and get a string response
|
||||
export const sendToDatabaseAndGetString = async (data: object): Promise<string> => {
|
||||
try {
|
||||
const response = await axios.post(apiURL.href, data);
|
||||
const status = response.data.status;
|
||||
const success = response.data.response;
|
||||
postMessage({ status, success });
|
||||
return success;
|
||||
return success; // Return response string
|
||||
} catch (error) {
|
||||
postMessage({ status: 500, success: false });
|
||||
console.log(error)
|
||||
return "false";
|
||||
console.log(error);
|
||||
return "false"; // Return "false" on error
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -61,7 +46,7 @@ export const createAccount = async (username: string, email: string, password: s
|
|||
email: email,
|
||||
password: password,
|
||||
};
|
||||
return await sendToDatabase(data);
|
||||
return await sendToDatabase(data); // Send account creation request
|
||||
};
|
||||
|
||||
export const changePassword = async (usernameOrEmail: string, password: string, newPassword: string) => {
|
||||
|
@ -72,7 +57,7 @@ export const changePassword = async (usernameOrEmail: string, password: string,
|
|||
password,
|
||||
new_password: newPassword,
|
||||
};
|
||||
return await sendToDatabase(data);
|
||||
return await sendToDatabase(data); // Send password change request
|
||||
};
|
||||
|
||||
export const getSettings = async (usernameOrEmail: string, password: string) => {
|
||||
|
@ -82,7 +67,7 @@ export const getSettings = async (usernameOrEmail: string, password: string) =>
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
return await sendToDatabaseAndGetString(data);
|
||||
return await sendToDatabaseAndGetString(data); // Get user settings
|
||||
};
|
||||
|
||||
export const changeSettings = async (usernameOrEmail: string, password: string, newData: object) => {
|
||||
|
@ -93,7 +78,7 @@ export const changeSettings = async (usernameOrEmail: string, password: string,
|
|||
password,
|
||||
data: newData,
|
||||
};
|
||||
return await sendToDatabase(data);
|
||||
return await sendToDatabase(data); // Send settings change request
|
||||
};
|
||||
|
||||
export const getHistory = async (usernameOrEmail: string, password: string) => {
|
||||
|
@ -103,7 +88,7 @@ export const getHistory = async (usernameOrEmail: string, password: string) => {
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
return await sendToDatabaseAndGetString(data);
|
||||
return await sendToDatabaseAndGetString(data); // Get user history
|
||||
};
|
||||
|
||||
export const changeHistory = async (usernameOrEmail: string, password: string, newData: object) => {
|
||||
|
@ -114,7 +99,7 @@ export const changeHistory = async (usernameOrEmail: string, password: string, n
|
|||
password,
|
||||
data: newData,
|
||||
};
|
||||
return await sendToDatabase(data);
|
||||
return await sendToDatabase(data); // Send history change request
|
||||
};
|
||||
|
||||
export const getEmail = async (usernameOrEmail: string, password: string): Promise<string> => {
|
||||
|
@ -124,7 +109,7 @@ export const getEmail = async (usernameOrEmail: string, password: string): Promi
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
return await sendToDatabaseAndGetString(data);
|
||||
return await sendToDatabaseAndGetString(data); // Get user email
|
||||
};
|
||||
|
||||
export const getName = async (usernameOrEmail: string, password: string): Promise<string> => {
|
||||
|
@ -134,10 +119,9 @@ export const getName = async (usernameOrEmail: string, password: string): Promis
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
return await sendToDatabaseAndGetString(data);
|
||||
return await sendToDatabaseAndGetString(data); // Get user name
|
||||
};
|
||||
|
||||
|
||||
export const checkCredentials = async (usernameOrEmail: string, password: string) => {
|
||||
const data = {
|
||||
action: "check_credentials",
|
||||
|
@ -145,15 +129,16 @@ export const checkCredentials = async (usernameOrEmail: string, password: string
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
const sendBack = await sendToDatabase(data);
|
||||
const sendBack = await sendToDatabase(data); // Check user credentials
|
||||
if (sendBack) {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password))
|
||||
localStorage.setItem("accountName", await getName(usernameOrEmail, password))
|
||||
localStorage.setItem("accountPassword", password)
|
||||
// Store user data in localStorage if credentials are valid
|
||||
localStorage.setItem("accountEmail", await getEmail(usernameOrEmail, password));
|
||||
localStorage.setItem("accountName", await getName(usernameOrEmail, password));
|
||||
localStorage.setItem("accountPassword", password);
|
||||
}
|
||||
}
|
||||
return sendBack
|
||||
return sendBack; // Return success status
|
||||
};
|
||||
|
||||
export const deleteAccount = async (usernameOrEmail: string, password: string) => {
|
||||
|
@ -163,5 +148,5 @@ export const deleteAccount = async (usernameOrEmail: string, password: string) =
|
|||
email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined,
|
||||
password,
|
||||
};
|
||||
return await sendToDatabase(data);
|
||||
return await sendToDatabase(data); // Send account deletion request
|
||||
};
|
||||
|
|
|
@ -1,37 +1,32 @@
|
|||
import axios from "axios";
|
||||
|
||||
let windownameGlobal = ""
|
||||
let windownameGlobal = ""; // Global variable to hold the window name
|
||||
let accesstoken = ""; // Variable to store the access token
|
||||
|
||||
let accesstoken = ""
|
||||
onmessage = (event) => {
|
||||
const { action, access_token, windowname } = event.data
|
||||
accesstoken = access_token
|
||||
|
||||
windownameGlobal = windowname
|
||||
const { action, access_token, windowname } = event.data;
|
||||
accesstoken = access_token;
|
||||
windownameGlobal = windowname;
|
||||
|
||||
if (action === "start") {
|
||||
fetchData()
|
||||
} else if (action === "terminate") {
|
||||
fetchData(); // Start fetching data on 'start' action
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const fetchData = () => {
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken);
|
||||
apiURL.hostname = windownameGlobal; // Set the hostname
|
||||
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken)
|
||||
apiURL.hostname = windownameGlobal;
|
||||
|
||||
console.log(apiURL.href)
|
||||
console.log(apiURL.href); // Log the constructed URL
|
||||
|
||||
axios.get(apiURL.href)
|
||||
.then(response => {
|
||||
const data = response.data
|
||||
postMessage(data)
|
||||
setTimeout(fetchData, 100)
|
||||
postMessage(response.data); // Send data back on success
|
||||
setTimeout(fetchData, 100); // Schedule next fetch
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('Error fetching data:', error);
|
||||
postMessage({ error: "failed fetching data" })
|
||||
setTimeout(() => fetchData(), 1000)
|
||||
})
|
||||
}
|
||||
postMessage({ error: "failed fetching data" }); // Send error message
|
||||
setTimeout(() => fetchData(), 1000); // Retry after 1 second
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,31 +1,32 @@
|
|||
import axios from "axios";
|
||||
|
||||
// Event listener for incoming messages
|
||||
onmessage = (e) => {
|
||||
const { messages, ai_model, model_type, access_token, api_key, windowname } = e.data
|
||||
|
||||
const { messages, ai_model, model_type, access_token, api_key, windowname } = e.data;
|
||||
|
||||
// Construct the message object to send to the API
|
||||
const Message = {
|
||||
messages: messages,
|
||||
ai_model: ai_model,
|
||||
model_type: model_type,
|
||||
access_token: access_token,
|
||||
api_key: api_key
|
||||
}
|
||||
};
|
||||
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_send")
|
||||
console.log(windowname)
|
||||
apiURL.hostname = windowname;
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_send");
|
||||
console.log(windowname); // Log the window name
|
||||
apiURL.hostname = windowname; // Set the hostname for the API request
|
||||
|
||||
console.log(apiURL.href)
|
||||
console.log(apiURL.href); // Log the constructed API URL
|
||||
|
||||
// Make a POST request to the API with the message object
|
||||
axios.post(apiURL.href, Message)
|
||||
.then(response => {
|
||||
const status = response.data.status
|
||||
postMessage({ status })
|
||||
|
||||
const status = response.data.status;
|
||||
postMessage({ status }); // Send the response status back
|
||||
})
|
||||
.catch(error => {
|
||||
console.log("Error calling API:", error)
|
||||
postMessage({ status: 500 })
|
||||
})
|
||||
}
|
||||
console.log("Error calling API:", error);
|
||||
postMessage({ status: 500 }); // Send error status if API call fails
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,24 +1,26 @@
|
|||
import axios from "axios";
|
||||
|
||||
export const sendToVoiceRecognition = (audio_data: Blob): Promise<string> => {
|
||||
// Create a new FormData instance to send the audio file
|
||||
const formdata = new FormData();
|
||||
formdata.append("audio", audio_data); // Append the audio data to the FormData
|
||||
|
||||
const formdata = new FormData()
|
||||
formdata.append("audio", audio_data)
|
||||
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/voice_recognition")
|
||||
// Set the API URL dynamically based on the environment
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/voice_recognition");
|
||||
if (typeof window !== 'undefined') {
|
||||
apiURL.hostname = window.location.hostname;
|
||||
apiURL.hostname = window.location.hostname; // Use the current hostname in the browser
|
||||
} else {
|
||||
apiURL.hostname = "localhost"
|
||||
apiURL.hostname = "localhost"; // Fallback for server-side
|
||||
}
|
||||
|
||||
// Send the audio data to the API using POST request
|
||||
return axios.post(apiURL.href, formdata)
|
||||
.then((response) => {
|
||||
return response.data.response
|
||||
return response.data.response; // Return the response from the API
|
||||
})
|
||||
.catch(error => {
|
||||
console.log("Error calling API:", error)
|
||||
postMessage({ status: 500 })
|
||||
return "Error"
|
||||
})
|
||||
}
|
||||
console.log("Error calling API:", error); // Log any error that occurs
|
||||
postMessage({ status: 500 }); // Indicate an error status to the worker
|
||||
return "Error"; // Return a fallback error message
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,23 +1,29 @@
|
|||
import axios from "axios";
|
||||
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather")
|
||||
// Initialize the API URL for the weather service
|
||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather");
|
||||
if (typeof window !== 'undefined') {
|
||||
apiURL.hostname = window.location.hostname;
|
||||
apiURL.hostname = window.location.hostname; // Use current hostname in the browser
|
||||
} else {
|
||||
apiURL.hostname = "localhost"
|
||||
apiURL.hostname = "localhost"; // Fallback for server-side
|
||||
}
|
||||
|
||||
// Function to get weather data
|
||||
export const getWeather = async (data: object): Promise<string> => {
|
||||
try {
|
||||
// Make a POST request to the weather API with the provided data
|
||||
const response = await axios.post(apiURL.href, data);
|
||||
const status = response.data.status;
|
||||
const success = response.data.response;
|
||||
const status = response.data.status; // Extract the status from the response
|
||||
const success = response.data.response; // Extract the actual weather data from the response
|
||||
|
||||
// Send the status and success response back to the worker
|
||||
postMessage({ status, success });
|
||||
console.log(JSON.stringify(success))
|
||||
return JSON.stringify(success);
|
||||
console.log(JSON.stringify(success)); // Log the successful response for debugging
|
||||
return JSON.stringify(success); // Return the weather data as a JSON string
|
||||
} catch (error) {
|
||||
// Handle any errors that occur during the request
|
||||
postMessage({ status: 500, success: false });
|
||||
console.log(error)
|
||||
return "";
|
||||
console.log(error); // Log the error for debugging
|
||||
return ""; // Return an empty string in case of an error
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue