codex-lens 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,249 @@
1
+ import React from 'react';
2
+ import { Terminal } from '@xterm/xterm';
3
+ import { FitAddon } from '@xterm/addon-fit';
4
+ import { WebLinksAddon } from '@xterm/addon-web-links';
5
+ import '@xterm/xterm/css/xterm.css';
6
+
7
+ const VIRTUAL_KEYS = [
8
+ { label: '↑', seq: '\x1b[A' },
9
+ { label: '↓', seq: '\x1b[B' },
10
+ { label: '←', seq: '\x1b[D' },
11
+ { label: '→', seq: '\x1b[C' },
12
+ { label: 'Enter', seq: '\r' },
13
+ { label: 'Tab', seq: '\t' },
14
+ { label: 'Esc', seq: '\x1b' },
15
+ { label: 'Ctrl+C', seq: '\x03' },
16
+ ];
17
+
18
+ export class TerminalPanel extends React.Component {
19
+ constructor(props) {
20
+ super(props);
21
+ this.containerRef = React.createRef();
22
+ this.terminal = null;
23
+ this.fitAddon = null;
24
+ this.ws = null;
25
+ this.resizeObserver = null;
26
+ this._writeBuffer = '';
27
+ this._writeTimer = null;
28
+ }
29
+
30
+ componentDidMount() {
31
+ this.initTerminal();
32
+ this.connectWebSocket();
33
+ this.setupResizeObserver();
34
+ }
35
+
36
+ componentWillUnmount() {
37
+ if (this._writeTimer) {
38
+ cancelAnimationFrame(this._writeTimer);
39
+ }
40
+ if (this.ws) {
41
+ this.ws.close();
42
+ this.ws = null;
43
+ }
44
+ if (this.resizeObserver) {
45
+ this.resizeObserver.disconnect();
46
+ }
47
+ if (this.terminal) {
48
+ this.terminal.dispose();
49
+ }
50
+ }
51
+
52
+ initTerminal() {
53
+ this.terminal = new Terminal({
54
+ cursorBlink: true,
55
+ cursorStyle: 'bar',
56
+ fontSize: 13,
57
+ fontFamily: 'Menlo, Monaco, "Courier New", monospace',
58
+ theme: {
59
+ background: '#0a0a0a',
60
+ foreground: '#d4d4d4',
61
+ cursor: '#d4d4d4',
62
+ selectionBackground: '#264f78',
63
+ },
64
+ scrollback: 3000,
65
+ allowProposedApi: true,
66
+ });
67
+
68
+ this.fitAddon = new FitAddon();
69
+ this.terminal.loadAddon(this.fitAddon);
70
+ this.terminal.loadAddon(new WebLinksAddon());
71
+
72
+ this.terminal.open(this.containerRef.current);
73
+
74
+ requestAnimationFrame(() => {
75
+ if (this.fitAddon) {
76
+ this.fitAddon.fit();
77
+ this.terminal.focus();
78
+ }
79
+ });
80
+
81
+ this.terminal.onData((data) => {
82
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
83
+ this.ws.send(JSON.stringify({ type: 'input', data }));
84
+ }
85
+ });
86
+ }
87
+
88
+ connectWebSocket() {
89
+ const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
90
+ const host = window.location.hostname;
91
+ const port = window.location.port === '5173' ? '5174' : window.location.port;
92
+ const wsUrl = `${protocol}//${host}:${port}/ws/terminal`;
93
+
94
+ this.ws = new WebSocket(wsUrl);
95
+
96
+ this.ws.onopen = () => {
97
+ console.log('[Terminal] Connected to PTY service');
98
+ this.sendResize();
99
+ };
100
+
101
+ this.ws.onmessage = (event) => {
102
+ try {
103
+ const msg = JSON.parse(event.data);
104
+ if (msg.type === 'data') {
105
+ this._throttledWrite(msg.data);
106
+ } else if (msg.type === 'exit') {
107
+ this._flushWrite();
108
+ if (this.terminal) {
109
+ this.terminal.write(`\r\n[Process exited with code ${msg.exitCode ?? '?'}]\r\n`);
110
+ }
111
+ } else if (msg.type === 'state') {
112
+ if (!msg.running && this.terminal) {
113
+ this._flushWrite();
114
+ }
115
+ }
116
+ } catch (e) {
117
+ console.error('[Terminal] Parse error:', e);
118
+ }
119
+ };
120
+
121
+ this.ws.onclose = () => {
122
+ console.log('[Terminal] Disconnected, reconnecting in 3s...');
123
+ setTimeout(() => {
124
+ if (this.containerRef.current) {
125
+ this.connectWebSocket();
126
+ }
127
+ }, 3000);
128
+ };
129
+
130
+ this.ws.onerror = (error) => {
131
+ console.error('[Terminal] WebSocket error:', error);
132
+ };
133
+ }
134
+
135
+ sendResize() {
136
+ if (this.ws && this.ws.readyState === WebSocket.OPEN && this.terminal) {
137
+ this.ws.send(JSON.stringify({
138
+ type: 'resize',
139
+ cols: this.terminal.cols,
140
+ rows: this.terminal.rows,
141
+ }));
142
+ }
143
+ }
144
+
145
+ setupResizeObserver() {
146
+ if (!this.containerRef.current) return;
147
+
148
+ this.resizeObserver = new ResizeObserver(() => {
149
+ if (this._resizeTimer) clearTimeout(this._resizeTimer);
150
+ this._resizeTimer = setTimeout(() => {
151
+ if (this.fitAddon && this.containerRef.current) {
152
+ try {
153
+ this.fitAddon.fit();
154
+ this.sendResize();
155
+ } catch {}
156
+ }
157
+ }, 150);
158
+ });
159
+
160
+ this.resizeObserver.observe(this.containerRef.current);
161
+ }
162
+
163
+ _throttledWrite(data) {
164
+ this._writeBuffer += data;
165
+ if (!this._writeTimer) {
166
+ this._writeTimer = requestAnimationFrame(() => {
167
+ this._flushWrite();
168
+ });
169
+ }
170
+ }
171
+
172
+ _flushWrite() {
173
+ if (this._writeTimer) {
174
+ cancelAnimationFrame(this._writeTimer);
175
+ this._writeTimer = null;
176
+ }
177
+ if (!this._writeBuffer || !this.terminal) return;
178
+
179
+ const CHUNK_SIZE = 32768;
180
+ if (this._writeBuffer.length <= CHUNK_SIZE) {
181
+ const buf = this._writeBuffer;
182
+ this._writeBuffer = '';
183
+ this.terminal.write(buf);
184
+ } else {
185
+ const chunk = this._writeBuffer.slice(0, CHUNK_SIZE);
186
+ this._writeBuffer = this._writeBuffer.slice(CHUNK_SIZE);
187
+ this.terminal.write(chunk);
188
+ this._writeTimer = requestAnimationFrame(() => {
189
+ this._flushWrite();
190
+ });
191
+ }
192
+ }
193
+
194
+ handleVirtualKey = (seq) => {
195
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
196
+ this.ws.send(JSON.stringify({ type: 'input', data: seq }));
197
+ }
198
+ this.terminal?.focus();
199
+ };
200
+
201
+ render() {
202
+ return (
203
+ <div style={{
204
+ height: '100%',
205
+ display: 'flex',
206
+ flexDirection: 'column',
207
+ background: '#0a0a0a',
208
+ }}>
209
+ <div
210
+ ref={this.containerRef}
211
+ style={{
212
+ flex: 1,
213
+ overflow: 'hidden',
214
+ padding: '4px 8px',
215
+ }}
216
+ />
217
+ <div style={{
218
+ display: 'flex',
219
+ gap: '4px',
220
+ padding: '8px',
221
+ background: '#111',
222
+ borderTop: '1px solid #222',
223
+ flexWrap: 'wrap',
224
+ }}>
225
+ {VIRTUAL_KEYS.map((key) => (
226
+ <button
227
+ key={key.label}
228
+ onClick={() => this.handleVirtualKey(key.seq)}
229
+ style={{
230
+ padding: '8px 12px',
231
+ border: '1px solid #333',
232
+ borderRadius: '4px',
233
+ background: '#1a1a1a',
234
+ color: '#ccc',
235
+ fontSize: '13px',
236
+ fontFamily: 'Menlo, Monaco, monospace',
237
+ cursor: 'pointer',
238
+ minWidth: '44px',
239
+ minHeight: '44px',
240
+ }}
241
+ >
242
+ {key.label}
243
+ </button>
244
+ ))}
245
+ </div>
246
+ </div>
247
+ );
248
+ }
249
+ }