File size: 11,105 Bytes
1625bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# modules/code_executor/routes.py
from flask import Blueprint, request, jsonify
import sys
import io
import threading
import queue
import time
import traceback
import os
import re
from openai import OpenAI
from config import API_KEY, BASE_URL, OPENAI_MODEL

code_executor_bp = Blueprint('code_executor', __name__)

# 创建OpenAI客户端
client = OpenAI(
    api_key=API_KEY,
    base_url=BASE_URL
)

# 执行上下文存储
execution_contexts = {}

class CustomStdin:
    def __init__(self, input_queue):
        self.input_queue = input_queue
        self.buffer = ""
    
    def readline(self):
        if not self.buffer:
            self.buffer = self.input_queue.get() + "\n"
        
        result = self.buffer
        self.buffer = ""
        return result

class InteractiveExecution:
    """管理Python代码的交互式执行"""
    def __init__(self, code):
        self.code = code
        self.context_id = str(time.time())
        self.is_complete = False
        self.is_waiting_for_input = False
        self.stdout_buffer = io.StringIO()
        self.last_read_position = 0
        self.input_queue = queue.Queue()
        self.error = None
        self.thread = None
        self.should_terminate = False
    
    def run(self):
        """在单独的线程中启动执行"""
        self.thread = threading.Thread(target=self._execute)
        self.thread.daemon = True
        self.thread.start()
        
        # 给执行一点时间开始
        time.sleep(0.1)
        return self.context_id
    
    def _execute(self):
        """执行代码,处理标准输入输出"""
        try:
            # 保存原始的stdin/stdout
            orig_stdin = sys.stdin
            orig_stdout = sys.stdout
            
            # 创建自定义stdin
            custom_stdin = CustomStdin(self.input_queue)
            
            # 重定向stdin和stdout
            sys.stdin = custom_stdin
            sys.stdout = self.stdout_buffer
            
            try:
                # 检查终止的函数
                self._last_check_time = 0
                
                def check_termination():
                    if self.should_terminate:
                        raise KeyboardInterrupt("Execution terminated by user")
                
                # 设置一个模拟__main__模块的命名空间
                shared_namespace = {
                    "__builtins__": __builtins__,
                    "_check_termination": check_termination,
                    "time": time, 
                    "__name__": "__main__"
                }
                
                # 在这个命名空间中执行用户代码
                try:
                    exec(self.code, shared_namespace)
                except KeyboardInterrupt:
                    print("\nExecution terminated by user")
                
            except Exception as e:
                self.error = {
                    "error": str(e),
                    "traceback": traceback.format_exc()
                }
            
            finally:
                # 恢复原始stdin/stdout
                sys.stdin = orig_stdin
                sys.stdout = orig_stdout
                
                # 标记执行完成
                self.is_complete = True
                
        except Exception as e:
            self.error = {
                "error": str(e),
                "traceback": traceback.format_exc()
            }
            self.is_complete = True
    
    def terminate(self):
        """终止执行"""
        self.should_terminate = True
        
        # 如果在等待输入,放入一些内容以解除阻塞
        if self.is_waiting_for_input:
            self.input_queue.put("\n")
        
        # 给执行一点时间终止
        time.sleep(0.2)
        
        # 标记为完成
        self.is_complete = True
        
        return True
    
    def provide_input(self, user_input):
        """为运行的代码提供输入"""
        self.input_queue.put(user_input)
        self.is_waiting_for_input = False
        return True
    
    def get_output(self):
        """获取stdout缓冲区的当前内容"""
        output = self.stdout_buffer.getvalue()
        return output
        
    def get_new_output(self):
        """只获取自上次读取以来的新输出"""
        current_value = self.stdout_buffer.getvalue()
        if self.last_read_position < len(current_value):
            new_output = current_value[self.last_read_position:]
            self.last_read_position = len(current_value)
            return new_output
        return ""

@code_executor_bp.route('/generate', methods=['POST'])
def generate_code():
    """使用AI生成Python代码"""
    try:
        prompt = request.json.get('prompt')
        if not prompt:
            return jsonify({
                "success": False,
                "error": "No prompt provided"
            })

        # 构建带有适当上下文的提示
        full_prompt = f"""You are a Python programming assistant. Generate Python code based on this requirement:

        {prompt}

        

        Provide only the Python code without any explanation or markdown formatting."""

        # 调用OpenAI API
        response = client.chat.completions.create(
            model=OPENAI_MODEL,
            messages=[{"role": "user", "content": full_prompt}]
        )

        # 提取和清理代码
        code = response.choices[0].message.content.strip()
        
        # 删除Markdown代码块标记(如果存在)
        code = re.sub(r'```python\n', '', code)
        code = re.sub(r'```', '', code)

        return jsonify({
            "success": True,
            "code": code
        })

    except Exception as e:
        return jsonify({
            "success": False,
            "error": str(e)
        })

@code_executor_bp.route('/execute', methods=['POST'])
def execute_code():
    """执行Python代码"""
    try:
        code = request.json.get('code')
        if not code:
            return jsonify({
                "success": False,
                "error": "No code provided"
            })

        # 创建并启动执行
        execution = InteractiveExecution(code)
        context_id = execution.run()
        
        # 存储在全局上下文中
        execution_contexts[context_id] = execution
        
        # 检查初始状态
        if execution.error:
            # 执行立即失败
            error_info = execution.error
            del execution_contexts[context_id]
            
            return jsonify({
                "success": False,
                "error": error_info["error"],
                "traceback": error_info["traceback"]
            })
        
        # 获取初始输出
        output = execution.get_output()
        # 更新上次读取位置以标记此输出为已读
        execution.last_read_position = len(output)
        
        # 检查是否命中input()调用或完成执行
        if execution.is_complete:
            # 执行完成,不需要输入
            del execution_contexts[context_id]
            
            return jsonify({
                "success": True,
                "output": output,
                "needsInput": False
            })
        else:
            # 假设我们正在等待输入
            execution.is_waiting_for_input = True
            
            return jsonify({
                "success": True,
                "output": output,
                "needsInput": True,
                "context_id": context_id
            })

    except Exception as e:
        return jsonify({
            "success": False,
            "error": str(e),
            "traceback": traceback.format_exc()
        })

@code_executor_bp.route('/input', methods=['POST'])
def provide_input():
    """为正在执行的代码提供输入"""
    try:
        user_input = request.json.get('input', '')
        context_id = request.json.get('context_id')
        
        if not context_id or context_id not in execution_contexts:
            return jsonify({
                "success": False,
                "error": "Invalid or expired execution context"
            })
        
        execution = execution_contexts[context_id]
        
        # 提供输入
        execution.provide_input(user_input)
        
        # 给一点时间处理
        time.sleep(0.1)
        
        # 获取输入后的新输出
        new_output = execution.get_new_output()
        
        # 检查执行状态
        if execution.is_complete:
            # 执行已完成
            if execution.error:
                # 有错误
                error_info = execution.error
                del execution_contexts[context_id]
                
                return jsonify({
                    "success": False,
                    "error": error_info["error"],
                    "traceback": error_info["traceback"]
                })
            else:
                # 干净执行
                del execution_contexts[context_id]
                
                return jsonify({
                    "success": True,
                    "output": new_output,
                    "needsInput": False
                })
        else:
            # 执行仍在运行,假设我们需要更多输入
            execution.is_waiting_for_input = True
            
            return jsonify({
                "success": True,
                "output": new_output,
                "needsInput": True
            })
            
    except Exception as e:
        return jsonify({
            "success": False,
            "error": str(e),
            "traceback": traceback.format_exc()
        })

@code_executor_bp.route('/stop', methods=['POST'])
def stop_execution():
    """停止执行"""
    try:
        context_id = request.json.get('context_id')
        
        if not context_id or context_id not in execution_contexts:
            return jsonify({
                "success": False,
                "error": "Invalid or expired execution context"
            })
        
        execution = execution_contexts[context_id]
        
        # 终止执行
        execution.terminate()
        
        # 获取最终输出
        output = execution.get_output()
        
        # 清理
        del execution_contexts[context_id]
        
        return jsonify({
            "success": True,
            "output": output,
            "message": "Execution terminated"
        })
            
    except Exception as e:
        return jsonify({
            "success": False,
            "error": str(e),
            "traceback": traceback.format_exc()
        })