File size: 6,519 Bytes
c2c8c8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { useRef, useEffect, useLayoutEffect, useState } from 'react';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import { getSocket } from '@/services/socket';
import { WS_EVENTS } from '@glmpilot/shared';
import { useEditorStore } from '@/stores/editorStore';
import { useEnvStore } from '@/stores/envStore';
import 'xterm/css/xterm.css';

export default function TerminalPanel() {
  const containerRef = useRef<HTMLDivElement>(null);
  const terminalRef = useRef<Terminal>();
  const [isExecuting, setIsExecuting] = useState(false);
  const isExecutingRef = useRef(false);

  const activeFilePath = useEditorStore((s) => s.activeFilePath);
  const openFiles = useEditorStore((s) => s.openFiles);
  const environment = useEnvStore((s) => s.environment);
  /** Buffered line while a program is waiting on stdin (submit on Enter as one message). */
  const pendingStdinLineRef = useRef('');
  /** Server-assigned id for this run (stdin + process map); set by execute:started. */
  const currentRunIdRef = useRef<string | null>(null);

  useEffect(() => {
    if (!containerRef.current) return;

    const terminal = new Terminal({
      theme: {
        background: '#080810',
        foreground: '#e8e8e8',
        cursor: '#81f084',
        selectionBackground: '#81f08433',
        black: '#1a1a2a',
        red: '#f07178',
        green: '#81f084',
        yellow: '#ffcb6b',
        blue: '#82aaff',
        magenta: '#c792ea',
        cyan: '#89ddff',
        white: '#e8e8e8',
      },
      fontFamily: "'Geist Mono', 'JetBrains Mono', 'Fira Code', monospace",
      fontSize: 13,
      cursorBlink: true,
      cursorStyle: 'bar',
    });

    const fitAddon = new FitAddon();
    terminal.loadAddon(fitAddon);
    terminal.open(containerRef.current);
    fitAddon.fit();

    terminal.writeln('\x1b[1;32m✦ GLMPilot Terminal\x1b[0m');
    terminal.writeln('\x1b[90mConnected to local environment.\x1b[0m');
    terminal.writeln('');
    terminal.write('\x1b[32m❯\x1b[0m ');

    terminalRef.current = terminal;

    terminal.onData((data) => {
      if (!isExecutingRef.current) return;

      const flushLine = () => {
        const line = pendingStdinLineRef.current;
        pendingStdinLineRef.current = '';
        terminal.write('\r\n');
        const runId = currentRunIdRef.current;
        getSocket().emit(
          WS_EVENTS.EXECUTE_INPUT,
          { runId: runId ?? '', input: line + '\n' },
          (resp: { ok?: boolean } | undefined) => {
            if (resp?.ok === false) {
              terminal.writeln(
                '\r\n\x1b[31m[Input did not reach the program — try Run again after output appears]\x1b[0m'
              );
            }
          }
        );
      };

      const normalized = data.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
      for (const ch of normalized) {
        if (ch === '\n') {
          flushLine();
        } else if (ch === '\x7f' || ch === '\b') {
          if (pendingStdinLineRef.current.length > 0) {
            pendingStdinLineRef.current = pendingStdinLineRef.current.slice(0, -1);
            terminal.write('\b \b');
          }
        } else if (ch === '\t') {
          pendingStdinLineRef.current += '\t';
          terminal.write('\t');
        } else if (ch < ' ' && ch !== '\t') {
          // ignore other C0 controls
        } else {
          pendingStdinLineRef.current += ch;
          terminal.write(ch);
        }
      }
    });

    const resizeObserver = new ResizeObserver(() => fitAddon.fit());
    resizeObserver.observe(containerRef.current);

    return () => {
      resizeObserver.disconnect();
      terminal.dispose();
    };
  }, []);

  useLayoutEffect(() => {
    isExecutingRef.current = isExecuting;
  }, [isExecuting]);

  useEffect(() => {
    const handleExecute = () => {
      if (!activeFilePath) return;
      const file = openFiles[activeFilePath];
      if (!file || !environment) return;

      console.log('[Terminal] handleExecute called, setting isExecuting=true');
      setIsExecuting(true);
      isExecutingRef.current = true; // Update ref immediately
      pendingStdinLineRef.current = '';
      currentRunIdRef.current = null;
      terminalRef.current?.clear();
      terminalRef.current?.writeln(`\x1b[33mRunning ${file.path}...\x1b[0m\n`);

      // Focus the terminal to capture keyboard input
      terminalRef.current?.focus();

      const socket = getSocket();
      socket.emit(WS_EVENTS.EXECUTE_REQUEST, {
        language: environment,
        content: file.content
      });
    };

    window.addEventListener('glmpilot:execute', handleExecute);
    return () => window.removeEventListener('glmpilot:execute', handleExecute);
  }, [activeFilePath, openFiles, environment]);

  useEffect(() => {
    const socket = getSocket();

    const onStarted = (data: { runId: string }) => {
      currentRunIdRef.current = data.runId;
    };

    const onToken = (data: { token: string; isError?: boolean }) => {
      const color = data.isError ? '\x1b[31m' : '\x1b[0m';
      const text = data.token.replace(/\n/g, '\r\n');
      terminalRef.current?.write(`${color}${text}\x1b[0m`);
    };

    const onComplete = () => {
      console.log('[Terminal] EXECUTE_COMPLETE received, setting isExecuting=false');
      setIsExecuting(false);
      isExecutingRef.current = false; // Update ref immediately
      pendingStdinLineRef.current = '';
      currentRunIdRef.current = null;
      terminalRef.current?.writeln('\n\n\x1b[32m❯ Execution finished.\x1b[0m ');
    };

    const onError = (data: { error: string }) => {
      console.log('[Terminal] EXECUTE_ERROR received:', data.error);
      setIsExecuting(false);
      isExecutingRef.current = false; // Update ref immediately
      pendingStdinLineRef.current = '';
      currentRunIdRef.current = null;
      terminalRef.current?.writeln(`\r\n\x1b[31m[Error] ${data.error}\x1b[0m\r\n`);
      terminalRef.current?.writeln('\x1b[32m❯\x1b[0m ');
    };

    socket.on(WS_EVENTS.EXECUTE_STARTED, onStarted);
    socket.on(WS_EVENTS.EXECUTE_TOKEN, onToken);
    socket.on(WS_EVENTS.EXECUTE_COMPLETE, onComplete);
    socket.on(WS_EVENTS.EXECUTE_ERROR, onError);

    return () => {
      socket.off(WS_EVENTS.EXECUTE_STARTED, onStarted);
      socket.off(WS_EVENTS.EXECUTE_TOKEN, onToken);
      socket.off(WS_EVENTS.EXECUTE_COMPLETE, onComplete);
      socket.off(WS_EVENTS.EXECUTE_ERROR, onError);
    };
  }, []);

  return <div ref={containerRef} className="h-full w-full" />;
}