2024-09-19 13:02:56 +02:00
import axios from 'axios'
2024-09-20 09:17:28 +02:00
import { type } from 'os' ;
2024-09-19 13:02:56 +02:00
onmessage = function ( e ) {
2024-09-20 09:17:28 +02:00
const { functionName = "getAccess" , access _token = "" , messages = [ ] , ai _model = "phi3.5" , system _prompt = { role : "system" , content : "You are a helpful assistant that gives short answers" } } = e . data
let data = {
ai _model : ai _model ,
messages : messages ,
access _token : access _token
2024-09-19 15:56:26 +02:00
} ;
2024-09-20 09:58:13 +02:00
const getResponse = ( ) => {
2024-09-20 10:54:21 +02:00
messageComplete : boolean = false
while ( ! messageComplete )
2024-09-20 09:58:13 +02:00
axios . get ( 'https://localhost:5000/interstellar/api/ai_get?access_token=' + access _token )
. then ( Response => {
postMessage ( Response . data . response )
2024-09-20 10:54:21 +02:00
if ( Response . data . status == 200 ) {
messageComplete = true
}
2024-09-20 09:58:13 +02:00
} ) . catch ( error => {
console . error ( "Error with GET response request:" , error )
} )
}
2024-09-19 13:02:56 +02:00
switch ( functionName ) {
case "getAccess" :
2024-09-20 09:17:28 +02:00
console . log ( "getting access..." )
axios . get ( 'https://localhost:5000/interstellar/api/ai_create' )
. then ( Response => {
postMessage ( Response . data . access _token )
} ) . catch ( error => {
console . error ( "Error with GET Token request:" , error )
} )
2024-09-19 13:02:56 +02:00
break
case "postRequest" :
2024-09-20 09:17:28 +02:00
messages . unshift ( system _prompt )
console . log ( "sending..." )
console . log ( messages )
axios . post ( 'https://localhost:5000/interstellar/api/ai_send' , data )
. then ( Response => {
2024-09-20 09:58:13 +02:00
getResponse ( )
2024-09-20 09:17:28 +02:00
} ) . catch ( error => {
console . error ( "Error:" , error )
} )
2024-09-19 13:02:56 +02:00
break
}
2024-09-19 16:24:16 +02:00
2024-09-19 13:02:56 +02:00
}