summaryrefslogtreecommitdiff
path: root/backend/instance/instance.ts
blob: 3da977858f010c12a06b40cd7fd45ee8a43b14be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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 );
  }
} )();