-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathapp_v2.py
More file actions
146 lines (119 loc) · 5.09 KB
/
app_v2.py
File metadata and controls
146 lines (119 loc) · 5.09 KB
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
# app.py
from flask import Flask, render_template, request, jsonify,send_from_directory,Response
import mimetypes
import os
import time
from pathlib import Path
import asyncio
from app.agent.manus import Manus
from app.logger import logger
import threading
import shutil
app = Flask(__name__)
app.config['WORKSPACE'] = 'workspace'
# 初始化工作目录
os.makedirs(app.config['WORKSPACE'], exist_ok=True)
LOG_FILE = 'logs/root_stream.log'
FILE_CHECK_INTERVAL = 2 # 文件检查间隔(秒)
PROCESS_TIMEOUT = 600 # 最长处理时间(秒)
def get_files_pathlib(root_dir):
"""使用pathlib递归获取文件路径"""
root = Path(root_dir)
return [str(path) for path in root.glob('**/*') if path.is_file()]
@app.route('/')
def index():
files = os.listdir(app.config['WORKSPACE'])
return render_template('index.html', files=files)
@app.route('/file/<filename>')
def file(filename):
file_path = os.path.join(app.config['WORKSPACE'], filename)
if os.path.isfile(file_path):
mime_type, _ = mimetypes.guess_type(filename)
if mime_type and mime_type.startswith('text/'):
if mime_type == 'text/html':
return send_from_directory(app.config['WORKSPACE'], filename)
else:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
return render_template('code.html', filename=filename, content=content)
elif mime_type == 'application/pdf':
return send_from_directory(app.config['WORKSPACE'], filename)
# elif mime_type in ['application/vnd.ms-powerpoint',
# 'application/vnd.openxmlformats-officedocument.presentationml.presentation']:
# return render_template('google_viewer.html', filename=filename, viewer_type='ppt')
# elif mime_type in ['application/msword',
# 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']:
# return render_template('google_viewer.html', filename=filename, viewer_type='word')
# elif mime_type in ['application/vnd.ms-excel',
# 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']:
# return render_template('google_viewer.html', filename=filename, viewer_type='excel')
# elif mime_type in ['application/vnd.ms-powerpoint',
# 'application/vnd.openxmlformats-officedocument.presentationml.presentation']:
# return render_template('google_viewer.html', filename=filename, viewer_type='ppt')
# elif mime_type in ['application/msword',
# 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']:
# return render_template('google_viewer.html', filename=filename, viewer_type='word')
# elif mime_type in ['application/vnd.ms-excel',
# 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']:
# return render_template('google_viewer.html', filename=filename, viewer_type='excel')
else:
return send_from_directory(app.config['WORKSPACE'], filename)
else:
return "File not found", 404
async def main(prompt):
agent = Manus()
await agent.run(prompt)
# 线程包装器
def run_async_task(message):
"""在新线程中运行异步任务"""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main(message))
loop.close()
@app.route('/api/chat-stream', methods=['POST'])
def chat_stream():
"""流式日志接口"""
# 清空日志文件
if os.path.exists(LOG_FILE):
os.remove(LOG_FILE)
# 获取请求数据
prompt = request.get_json()
print("收到请求:", prompt)
# 启动异步任务线程
task_thread = threading.Thread(
target=run_async_task,
args=(prompt["message"],)
)
task_thread.start()
# 流式生成器
def generate():
start_time = time.time()
last_size = 0
process_active = True
while process_active:
# 超时检查
if time.time() - start_time > PROCESS_TIMEOUT:
yield "\n[错误] 处理超时\n"
break
shutil.copy('logs/root.log',LOG_FILE)
try:
# 读取新增内容
with open(LOG_FILE, "r", encoding="utf-8") as f:
f.seek(last_size)
new_content = f.read()
last_size = f.tell()
if new_content:
yield new_content
# 检查任务状态
process_active = task_thread.is_alive()
# 无新内容时暂停
if not new_content:
time.sleep(FILE_CHECK_INTERVAL)
except Exception as e:
yield f"\n[错误] 读取日志失败: {str(e)}\n"
break
# 最终确认
yield "\n[完成] 处理结束\n"
return Response(generate(), mimetype="text/plain")
if __name__ == '__main__':
app.run(debug=True)