diff options
Diffstat (limited to 'backend/instance/instance.ts')
| -rwxr-xr-x | backend/instance/instance.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/backend/instance/instance.ts b/backend/instance/instance.ts new file mode 100755 index 0000000..3da9778 --- /dev/null +++ b/backend/instance/instance.ts @@ -0,0 +1,92 @@ +'use strict'; +import readline from 'readline'; + +import * as api from './api-connection.js'; +import * as server from './server.js'; +import * as wget from './wget.js'; +import * as chat from './chat.js'; + +let API_PORT = 3003; +let API_ADDRESS = `ws://localhost`; +let DOMAIN = 'http://localhost'; +let PORT = 3001; + +const rl = readline.createInterface( { + input: process.stdin, + output: process.stdout +} ); + +function loadConfig() { + let args = process.argv.slice( 2 ); + for( let i = 0; i < args.length; i++ ) { + if( args[i] == '-h' || args[i] == '--help' ) { + let helpStr = "--port [port] - port\n"; + helpStr += "--no-wget-verify - disable re-reading wget requests\n"; + helpStr += "-c [num_ctx] - change max context window tokens\n"; + helpStr += "--wget-max-len [num_chars] - maximum length WGET results should get trimmed to in characters.\n" + helpStr += "--api-endpoint [url] - API endpoint to connect to\n"; + helpStr += "--domain [url] - url the server is running on"; + + console.log( helpStr ); + process.abort(); + } + + if( args[i] == "--port" ) + PORT = parseInt( args[i + 1] ); + + if( args[i] === "--no-wget-verify" ) + wget.setConfig( { noVerify: true } ); + + if( args[i] == "-c" ) + chat.setConfig( { contextWindow: parseInt( args[i + 1] ) } ); + + if( args[i] == '--wget-max-len' ) + wget.setConfig( { lenMax: parseInt( args[i + 1 ] ) } ); + + if( args[i] == '--domain' ) + DOMAIN = args[i + 1]; + + if( args[i] == '--api-port' ) + API_PORT = parseInt( args[i + 1] ); + } +} + +async function input() { + return new Promise( ( resolve ) => { + rl.question( "\x1b[0m>> ", async ( user_input ) => { + const input = user_input.trim(); + const args = input.split( " " ); + + if( args[0] === "" ) { + rl.close(); + resolve( null ); + } + } ); + } ); +} + +async function onExcept( e: any ) { + console.log( e ); + api.setStatus( { isBusy: false } ); +} + +process.on( "uncaughtException", ( e ) => { + onExcept( e ); +} ); + +process.on( "unhandledRejection", ( e ) => { + onExcept( e ); +} ); + +input(); +( async () => { + try { + loadConfig(); + await wget.launchBrowser(); + console.log( 'browser launched' ); + api.serverOpen( API_ADDRESS + `:${API_PORT}` ); + server.listen( PORT, DOMAIN ); + } catch( e ) { + console.log( e ); + } +} )(); |
