diff --git a/web_app.py b/web_app.py index c1aa252..41ef87d 100644 --- a/web_app.py +++ b/web_app.py @@ -518,6 +518,9 @@ def task_status(task_id): def run_analysis(task_id, ticker, date): """后台运行TradingAgents分析。""" + import sys + import io + try: task = tasks[task_id] @@ -534,7 +537,7 @@ def run_analysis(task_id, ticker, date): log(f'📊 开始分析 {ticker} ({date})') log(f'🤖 模型: MiMo-v2.5-pro') - log(f'🌐 数据源: SSH中转代理') + log(f'🌐 数据源: Alpha Vantage + SSH中转代理') from tradingagents.graph.trading_graph import TradingAgentsGraph from tradingagents.default_config import DEFAULT_CONFIG @@ -558,11 +561,40 @@ def run_analysis(task_id, ticker, date): } log('🔧 配置完成,启动分析引擎...') + log('⏳ 分析通常需要3-8分钟,请耐心等待...') - ta = TradingAgentsGraph(debug=True, config=config) + # 捕获stdout输出以获取实时进度 + class LogCapture: + def __init__(self, task): + self.task = task + self.buffer = '' + def write(self, text): + self.buffer += text + if '\n' in self.buffer: + lines = self.buffer.split('\n') + self.buffer = lines[-1] + for line in lines[:-1]: + line = line.strip() + if line and len(line) > 5: + # 只保留有意义的输出 + if any(k in line for k in ['Tool', 'Message', 'Agent', 'failed', 'error', '✅', '❌', '📊', '📈', '💭', '📰', '🔬', '⚠️']): + self.task['logs'].append(f'📋 {line[:100]}') + if len(self.task['logs']) > 50: + self.task['logs'] = self.task['logs'][-50:] + def flush(self): + pass - log('📈 技术分析中...') - state, decision = ta.propagate(ticker, date) + # 临时捕获输出 + old_stdout = sys.stdout + sys.stdout = LogCapture(task) + + try: + ta = TradingAgentsGraph(debug=True, config=config) + + log('📈 技术分析师开始工作...') + state, decision = ta.propagate(ticker, date) + finally: + sys.stdout = old_stdout log(f'✅ 分析完成: {decision}')