interstellar_ai/app/backend/threads/PostWorker.ts
sageTheDM 91353bd051 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>
2024-10-11 10:18:33 +02:00

32 lines
1.1 KiB
TypeScript

import axios from "axios";
// Event listener for incoming messages
onmessage = (e) => {
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); // Log the window name
apiURL.hostname = windowname; // Set the hostname for the API request
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 }); // Send the response status back
})
.catch(error => {
console.log("Error calling API:", error);
postMessage({ status: 500 }); // Send error status if API call fails
});
}