File size: 2,158 Bytes
bccbbe3
 
 
 
 
 
 
 
 
 
 
 
 
 
27f36ae
bccbbe3
 
 
 
27f36ae
bccbbe3
15d65ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bccbbe3
 
27f36ae
bccbbe3
 
9e5b59b
7180982
15d65ed
 
 
 
bccbbe3
 
27f36ae
bccbbe3
 
 
27f36ae
 
bccbbe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os from 'os';
import pty from 'node-pty';

let sharedPtyProcess = null;
let sharedTerminalMode = false;

const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';

const spawnShell = () => {
    return pty.spawn(shell, [], {
        name: 'xterm-color',
        env: process.env,
    });
};

export const setSharedTerminalMode = (useSharedTerminal) => {
    sharedTerminalMode = useSharedTerminal;
    if (sharedTerminalMode && !sharedPtyProcess) {
        sharedPtyProcess = spawnShell();
    }
};
class IA {
  constructor(){
    this.messages=[]
    this.listeners={}
  }
  write(data){
    this.messages.append({role:"user",content:data})
    fetch('https://text.pollinations.ai/openai',{
      body:JSON.stringify({
        messages:this.messages,
        private:true,
        model:"openai-large"
      })
    }).then(resp=>resp.json()).then(respuesta=>{
      this.messages.append(respuesta.choices[0].message)
      this.emit('data',this.messages[messages.length-1].content)
    })
  }
  emit(text,data){
    v = this.listeners[text]
    for(var i in v){
      i.apply(null,data)
    }
    
  }
  on(text,cb){
    if (!this.listeners[text]){
      this.listeners[text]=[]
    }
    this.listeners[text].push(cb)
  }
}
export const handleTerminalConnection = (ws) => {
    let ptyProcess = sharedTerminalMode ? sharedPtyProcess : spawnShell();

    ws.on('message', command => {
        const processedCommand = commandProcessor(command);
        console.log(processedCommand.toString())
        if(processedCommand.toString()=="ia"){
          ptyProcess.kill()
          sharedPtyProcess = IA()
          
        }
        ptyProcess.write(processedCommand);
    });

    ptyProcess.on('data', (rawOutput) => {
        const processedOutput = outputProcessor(rawOutput);
        ws.send(processedOutput);
    });

    ws.on('close', () => {
        if (!sharedTerminalMode) {
            ptyProcess.kill();
        }
    });
};

// Utility function to process commands
const commandProcessor = (command) => {
    return command;
};

// Utility function to process output
const outputProcessor = (output) => {
    return output;
};