2024-09-23 11:11:45 +02:00
|
|
|
import axios from "axios";
|
|
|
|
|
2024-10-11 09:14:03 +02:00
|
|
|
// Event listener for incoming messages
|
2024-09-23 11:11:45 +02:00
|
|
|
onmessage = (e) => {
|
2024-10-11 09:14:03 +02:00
|
|
|
const { messages, ai_model, model_type, access_token, api_key, windowname } = e.data;
|
2024-09-23 14:54:13 +02:00
|
|
|
|
2024-10-11 09:14:03 +02:00
|
|
|
// Construct the message object to send to the API
|
2024-09-23 14:54:13 +02:00
|
|
|
const Message = {
|
2024-09-23 16:34:55 +02:00
|
|
|
messages: messages,
|
2024-09-30 15:26:31 +02:00
|
|
|
ai_model: ai_model,
|
|
|
|
model_type: model_type,
|
|
|
|
access_token: access_token,
|
|
|
|
api_key: api_key
|
2024-10-11 09:14:03 +02:00
|
|
|
};
|
2024-09-23 14:54:13 +02:00
|
|
|
|
2024-10-11 09:14:03 +02:00
|
|
|
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
|
2024-10-03 14:10:21 +02:00
|
|
|
|
2024-10-11 09:14:03 +02:00
|
|
|
console.log(apiURL.href); // Log the constructed API URL
|
2024-10-03 14:10:21 +02:00
|
|
|
|
2024-10-11 09:14:03 +02:00
|
|
|
// Make a POST request to the API with the message object
|
2024-10-03 14:10:21 +02:00
|
|
|
axios.post(apiURL.href, Message)
|
2024-09-23 11:11:45 +02:00
|
|
|
.then(response => {
|
2024-10-11 09:14:03 +02:00
|
|
|
const status = response.data.status;
|
|
|
|
postMessage({ status }); // Send the response status back
|
2024-09-23 11:11:45 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2024-10-11 09:14:03 +02:00
|
|
|
console.log("Error calling API:", error);
|
|
|
|
postMessage({ status: 500 }); // Send error status if API call fails
|
|
|
|
});
|
|
|
|
}
|