auto-coder-web 0.1.6__py3-none-any.whl → 0.1.7__py3-none-any.whl

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.
@@ -1,84 +1,90 @@
1
1
  from fastapi import WebSocket
2
2
  import asyncio
3
3
  import websockets
4
- import pty
5
4
  import os
6
5
  import json
7
6
  import struct
8
- import fcntl
9
- import termios
10
7
  import signal
11
8
  from typing import Dict, Optional
12
9
  import threading
13
10
  import select
14
11
  import psutil
15
12
  import time
13
+ import sys
14
+ import platform
16
15
 
16
+ # 为不同平台选择合适的终端库
17
+ if platform.system() == 'Windows':
18
+ import winpty
19
+ else:
20
+ import pty
21
+ import fcntl
22
+ import termios
17
23
 
18
24
  class TerminalSession:
19
- def __init__(self, websocket: WebSocket, shell: str = '/bin/bash'):
25
+ def __init__(self, websocket: WebSocket, shell: str = None):
20
26
  self.websocket = websocket
21
- self.shell = shell
27
+ # 根据平台选择默认shell
28
+ if shell is None:
29
+ if platform.system() == 'Windows':
30
+ self.shell = 'cmd.exe'
31
+ else:
32
+ self.shell = '/bin/bash'
33
+ else:
34
+ self.shell = shell
35
+
22
36
  self.fd: Optional[int] = None
23
37
  self.pid: Optional[int] = None
24
38
  self.running = False
25
39
  self._lock = threading.Lock()
26
40
  self._last_heartbeat = time.time()
41
+ self.platform = platform.system()
42
+ self.pty = None # 用于Windows的winpty实例
27
43
 
28
44
  async def start(self):
29
45
  """Start the terminal session"""
30
- # Fork a new process for the shell
31
- self.pid, self.fd = pty.fork()
32
-
33
- if self.pid == 0: # Child process
34
- # Execute the shell
35
- env = os.environ.copy()
36
- env["TERM"] = "xterm-256color"
37
- os.execvpe(self.shell, [self.shell], env)
38
- else: # Parent process
39
- self.running = True
40
- asyncio.create_task(self._handle_io())
41
- # await self._handle_io()
42
-
43
- def resize(self, rows: int, cols: int):
44
- """Resize the terminal"""
45
- if self.fd is not None:
46
- # Get the current window size
47
- size = struct.pack("HHHH", rows, cols, 0, 0)
48
- # Set new window size
49
- fcntl.ioctl(self.fd, termios.TIOCSWINSZ, size)
50
-
51
- async def _send_heartbeat(self):
52
- while self.running:
46
+ if self.platform == 'Windows':
47
+ # Windows下使用winpty
53
48
  try:
54
- # Shorter heartbeat timeout
55
- if time.time() - self._last_heartbeat > 15: # Reduced from 30 to 15 seconds
56
- print(
57
- "No heartbeat received for 15 seconds, initiating reconnection")
58
- self.running = False
59
- try:
60
- await self.websocket.close(code=1000, reason="Heartbeat timeout")
61
- except Exception:
62
- pass
63
- break
64
-
65
- if self.websocket.client_state.CONNECTED:
66
- await self.websocket.send_json({"type": "heartbeat"})
67
- else:
68
- print("WebSocket disconnected, stopping heartbeat")
69
- break
70
-
71
- await asyncio.sleep(5) # Reduced from 15 to 5 seconds
72
-
73
- except websockets.exceptions.ConnectionClosed:
74
- print("Connection closed normally during heartbeat")
75
- break
49
+ self.pty = winpty.PTY(
50
+ cols=80,
51
+ rows=24
52
+ )
53
+ # 在Windows下,pid就是process.pid
54
+ self.pid = self.pty.spawn(self.shell)
55
+ self.fd = self.pty.fd # winpty提供了类似的文件描述符
56
+ self.running = True
57
+ asyncio.create_task(self._handle_io())
76
58
  except Exception as e:
77
- print(f"Error in heartbeat: {str(e)}")
78
- if not self.running:
79
- break
80
- await asyncio.sleep(1) # Brief pause before retry
81
- continue
59
+ print(f"Failed to start Windows terminal: {e}")
60
+ raise
61
+ else:
62
+ # Unix系统使用原有的pty
63
+ self.pid, self.fd = pty.fork()
64
+
65
+ if self.pid == 0: # Child process
66
+ # Execute the shell
67
+ env = os.environ.copy()
68
+ env["TERM"] = "xterm-256color"
69
+ os.execvpe(self.shell, [self.shell], env)
70
+ else: # Parent process
71
+ self.running = True
72
+ asyncio.create_task(self._handle_io())
73
+
74
+ def resize(self, rows: int, cols: int):
75
+ """Resize the terminal"""
76
+ if not self.running:
77
+ return
78
+
79
+ if self.platform == 'Windows':
80
+ if self.pty:
81
+ self.pty.set_size(rows, cols)
82
+ else:
83
+ if self.fd is not None:
84
+ # Get the current window size
85
+ size = struct.pack("HHHH", rows, cols, 0, 0)
86
+ # Set new window size
87
+ fcntl.ioctl(self.fd, termios.TIOCSWINSZ, size)
82
88
 
83
89
  async def _handle_io(self):
84
90
  """Handle I/O between PTY and WebSocket"""
@@ -87,18 +93,29 @@ class TerminalSession:
87
93
  def _read_pty(fd, size=1024):
88
94
  """Synchronous PTY read with timeout"""
89
95
  try:
90
- r, _, _ = select.select([fd], [], [], 0.1)
91
- if r:
92
- try:
93
- data = os.read(fd, size)
94
- return data if data else None # 返回None表示EOF
95
- except (OSError, EOFError) as e:
96
- print(f"Error reading PTY: {e}")
97
- return None
98
- return b'' # 没有数据可读但不是错误
96
+ if self.platform == 'Windows':
97
+ # Windows下使用winpty的读取方法
98
+ if self.pty:
99
+ try:
100
+ data = self.pty.read(size)
101
+ return data.encode('utf-8') if isinstance(data, str) else data
102
+ except Exception as e:
103
+ print(f"Error reading from winpty: {e}")
104
+ return None
105
+ else:
106
+ # Unix系统使用select
107
+ r, _, _ = select.select([fd], [], [], 0.1)
108
+ if r:
109
+ try:
110
+ data = os.read(fd, size)
111
+ return data if data else None
112
+ except (OSError, EOFError) as e:
113
+ print(f"Error reading PTY: {e}")
114
+ return None
115
+ return b''
99
116
  except Exception as e:
100
117
  print(f"Fatal error in PTY read: {e}")
101
- return None # 严重错误
118
+ return None
102
119
 
103
120
  async def _safe_send(data: bytes):
104
121
  """Safely send data to websocket with error handling"""
@@ -113,8 +130,8 @@ class TerminalSession:
113
130
  print(f"WebSocket send error: {e}")
114
131
  return False
115
132
 
116
- read_errors = 0 # 跟踪连续读取错误
117
- MAX_READ_ERRORS = 3 # 最大允许的连续读取错误
133
+ read_errors = 0
134
+ MAX_READ_ERRORS = 3
118
135
 
119
136
  try:
120
137
  while self.running:
@@ -126,23 +143,21 @@ class TerminalSession:
126
143
  if not self.running:
127
144
  break
128
145
 
129
- if data is None: # 严重错误或EOF
146
+ if data is None:
130
147
  read_errors += 1
131
148
  if read_errors >= MAX_READ_ERRORS:
132
- print(
133
- f"Too many PTY read errors ({read_errors}), stopping")
149
+ print(f"Too many PTY read errors ({read_errors}), stopping")
134
150
  break
135
- await asyncio.sleep(0.1) # 错误后短暂等待
151
+ await asyncio.sleep(0.1)
136
152
  continue
137
153
 
138
- read_errors = 0 # 重置错误计数
154
+ read_errors = 0
139
155
 
140
- if data: # 有实际数据
141
- print(f"Received data from PTY: {data.decode('utf-8', errors='replace')}")
156
+ if data:
142
157
  if not await _safe_send(data):
143
158
  break
144
159
 
145
- await asyncio.sleep(0.001) # 防止CPU过度使用
160
+ await asyncio.sleep(0.001)
146
161
 
147
162
  except Exception as e:
148
163
  print(f"IO handling error: {e}")
@@ -154,45 +169,48 @@ class TerminalSession:
154
169
  except Exception as e:
155
170
  print(f"Fatal error in IO handling: {e}")
156
171
  finally:
157
- self.running = False # 确保设置运行状态
172
+ self.running = False
158
173
  print("IO handling stopped")
159
- # 确保清理
160
- if self.fd is not None:
161
- try:
162
- os.close(self.fd)
163
- except:
164
- pass
165
- self.fd = None
174
+ self.cleanup()
166
175
 
167
176
  def write(self, data: str):
168
177
  """Write data to the terminal"""
169
- if self.fd is not None:
170
- try:
171
- print(f"Writing data to pty: {data}")
172
- encoded_data = data.encode('utf-8')
173
- bytes_written = os.write(self.fd, encoded_data)
174
- if bytes_written != len(encoded_data):
175
- print(
176
- f"Warning: Not all bytes written. Expected {len(encoded_data)}, wrote {bytes_written}")
177
- except Exception as e:
178
- print(f"Error writing to terminal: {e}")
178
+ if not self.running:
179
+ return
180
+
181
+ try:
182
+ encoded_data = data.encode('utf-8')
183
+ if self.platform == 'Windows':
184
+ if self.pty:
185
+ self.pty.write(data) # winpty接受字符串输入
186
+ else:
187
+ if self.fd is not None:
188
+ os.write(self.fd, encoded_data)
189
+ except Exception as e:
190
+ print(f"Error writing to terminal: {e}")
179
191
 
180
192
  def cleanup(self):
181
193
  """Clean up the terminal session"""
182
194
  print("Cleaning up terminal session...")
183
195
  self.running = False
184
- if self.pid:
185
- try:
186
- os.kill(self.pid, signal.SIGTERM)
187
- except ProcessLookupError:
188
- pass
189
- if self.fd is not None:
190
- try:
191
- os.close(self.fd)
192
- except OSError:
193
- # File descriptor may already be closed
194
- pass
195
-
196
+
197
+ if self.platform == 'Windows':
198
+ if self.pty:
199
+ try:
200
+ self.pty.close()
201
+ except Exception as e:
202
+ print(f"Error closing winpty: {e}")
203
+ else:
204
+ if self.pid:
205
+ try:
206
+ os.kill(self.pid, signal.SIGTERM)
207
+ except ProcessLookupError:
208
+ pass
209
+ if self.fd is not None:
210
+ try:
211
+ os.close(self.fd)
212
+ except OSError:
213
+ pass
196
214
 
197
215
  class TerminalManager:
198
216
  def __init__(self):
@@ -200,9 +218,6 @@ class TerminalManager:
200
218
 
201
219
  async def create_session(self, websocket: WebSocket, session_id: str):
202
220
  """Create a new terminal session"""
203
-
204
- # if self.sessions:
205
- # return list(self.sessions.values())[-1]
206
221
  if session_id in self.sessions:
207
222
  await self.close_session(session_id)
208
223
 
@@ -237,7 +252,6 @@ class TerminalManager:
237
252
  else:
238
253
  session.write(data)
239
254
  except json.JSONDecodeError:
240
- # 如果不是JSON,就当作普通输入处理
241
255
  session.write(data)
242
256
  except RuntimeError as e:
243
257
  if "WebSocket is not connected" in str(e):
@@ -255,5 +269,4 @@ class TerminalManager:
255
269
  await self.close_session(session_id)
256
270
  raise
257
271
 
258
-
259
- terminal_manager = TerminalManager()
272
+ terminal_manager = TerminalManager()
auto_coder_web/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.1.6"
1
+ __version__ = "0.1.7"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: auto_coder_web
3
- Version: 0.1.6
3
+ Version: 0.1.7
4
4
  Summary: auto-coder.web: A Python Project
5
5
  Author: allwefantasy
6
6
  Classifier: Programming Language :: Python :: 3.9
@@ -12,6 +12,7 @@ Requires-Dist: aiofiles
12
12
  Requires-Dist: psutil
13
13
  Requires-Dist: sse-starlette
14
14
  Requires-Dist: websockets
15
+ Requires-Dist: pywinpty ; sys_platform == "win32"
15
16
 
16
17
  # auto-coder.web
17
18
 
@@ -45,6 +46,7 @@ docker run \
45
46
  -e BASE_URL=https://api.deepseek.com/v1 \
46
47
  -e API_KEY=$MODEL_DEEPSEEK_TOKEN \
47
48
  -e MODEL=deepseek-chat \
49
+ -p 8006:8006 \
48
50
  -p 8007:8007 \
49
51
  -p 8265:8265 \
50
52
  -v <你的项目>:/app/work \
@@ -5,8 +5,8 @@ auto_coder_web/file_manager.py,sha256=wYe0NKqclhB22zTXT7du-5WRqne65tAZiTVAKsEZia
5
5
  auto_coder_web/hello.py,sha256=H-BM5gdpcfTT4T2pn7Q1qWfquf0jwzGRuOBozbc8dbA,741
6
6
  auto_coder_web/json_file_storage.py,sha256=elthpcdclXITX3jew2EtT-ypyxZzDAzG1U7_k3looHI,1757
7
7
  auto_coder_web/proxy.py,sha256=mqh8UiP9lsAuwPP0Cy5nmgVle4yn4rWCtI75W6uomjw,21478
8
- auto_coder_web/terminal.py,sha256=1oQRIeLCifdBG4KBGvVeigpDJXILbw1cTdMs1gFihn8,9539
9
- auto_coder_web/version.py,sha256=n3oM6B_EMz93NsTI18NNZd-jKFcUPzUkbIKj5VFK5ok,22
8
+ auto_coder_web/terminal.py,sha256=jtAH7FaC573cgxc7FnI_mOZ3D2dSCO3PrZN0OehtbNQ,9521
9
+ auto_coder_web/version.py,sha256=YpKDcdV7CqL8n45u267wKtyloM13FSVbOdrqgNZnSLM,22
10
10
  auto_coder_web/web/asset-manifest.json,sha256=ZQGrHji5YM4uPTc4Nk06E90t6PD1aSR3F9KunHkDKQ4,517
11
11
  auto_coder_web/web/favicon.ico,sha256=PRD32mxgMXg0AIFmjErFs66XQ8qaJiqw_NMS-7n0i90,3870
12
12
  auto_coder_web/web/index.html,sha256=UXEdzAYIuMzJZf52RPcMlm9A9mTGLsshVJwaYwMUnW0,644
@@ -44,8 +44,8 @@ auto_coder_web/web/static/js/main.af855afe.js.map,sha256=G2tu9r9hPw_lWkYlNUC6wv_
44
44
  auto_coder_web/web/static/js/main.ba643932.js,sha256=cYOdfCQIkfNoYhlX1XkHRH044pEkTVe6bz4odmfQe3c,1256033
45
45
  auto_coder_web/web/static/js/main.ba643932.js.LICENSE.txt,sha256=rseC9BHzhrHoQhEIcUKYbKYTmaZc3jx198-A7figAUo,22630
46
46
  auto_coder_web/web/static/js/main.ba643932.js.map,sha256=TnZnz40mVLDqMY_cD4XjcIoER5ewUPLyVZEgUuO3w50,5007072
47
- auto_coder_web-0.1.6.dist-info/METADATA,sha256=XBAL1guf_qOnxNoCPQTekKAdv8TIHC4bHmu3KgCAz94,1230
48
- auto_coder_web-0.1.6.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
49
- auto_coder_web-0.1.6.dist-info/entry_points.txt,sha256=oh8kd1ZecWDgdv-bIj_ru3phvl4scuxwXl9DK0Khltg,61
50
- auto_coder_web-0.1.6.dist-info/top_level.txt,sha256=UCzEw494Im0KvR-FTYf2jh-okqHvLsC_0JLOrQZoSpg,15
51
- auto_coder_web-0.1.6.dist-info/RECORD,,
47
+ auto_coder_web-0.1.7.dist-info/METADATA,sha256=J8DdSVRkTXK64VRkOKd-gDEcHC9JKYufAkAbfL2m6bM,1297
48
+ auto_coder_web-0.1.7.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
49
+ auto_coder_web-0.1.7.dist-info/entry_points.txt,sha256=oh8kd1ZecWDgdv-bIj_ru3phvl4scuxwXl9DK0Khltg,61
50
+ auto_coder_web-0.1.7.dist-info/top_level.txt,sha256=UCzEw494Im0KvR-FTYf2jh-okqHvLsC_0JLOrQZoSpg,15
51
+ auto_coder_web-0.1.7.dist-info/RECORD,,