fix(review): api_key→anthropic_key bug, sync-in-async event loop block, orchestrator per-message re-init, dead code cleanup

This commit is contained in:
陈少杰 2026-04-09 22:55:36 +08:00
parent e8ac2c81f7
commit d210099fa9
4 changed files with 17 additions and 19 deletions

View File

@ -1,9 +1,8 @@
import logging import logging
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import List, Optional from typing import List
from orchestrator.config import OrchestratorConfig
from orchestrator.signals import FinalSignal from orchestrator.signals import FinalSignal
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -1,5 +1,4 @@
import asyncio import asyncio
import json
import logging import logging
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import List, Optional from typing import List, Optional
@ -27,7 +26,9 @@ class LiveMode:
results = [] results = []
for ticker in tickers: for ticker in tickers:
try: try:
sig = self._orchestrator.get_combined_signal(ticker, date) sig = await asyncio.to_thread(
self._orchestrator.get_combined_signal, ticker, date
)
results.append({ results.append({
"ticker": ticker, "ticker": ticker,
"date": date, "date": date,

View File

@ -52,8 +52,6 @@ class SignalMerger:
# 只有 LLMquant 失败) # 只有 LLMquant 失败)
if quant is None: if quant is None:
if llm is None:
raise ValueError("llm signal is None when quant is None")
return FinalSignal( return FinalSignal(
ticker=ticker, ticker=ticker,
direction=llm.direction, direction=llm.direction,

View File

@ -363,7 +363,7 @@ async def start_analysis(request: AnalysisRequest, api_key: Optional[str] = Head
# Use clean environment - don't inherit parent env # Use clean environment - don't inherit parent env
clean_env = {k: v for k, v in os.environ.items() clean_env = {k: v for k, v in os.environ.items()
if not k.startswith(("PYTHON", "CONDA", "VIRTUAL"))} if not k.startswith(("PYTHON", "CONDA", "VIRTUAL"))}
clean_env["ANTHROPIC_API_KEY"] = api_key clean_env["ANTHROPIC_API_KEY"] = anthropic_key
clean_env["ANTHROPIC_BASE_URL"] = "https://api.minimaxi.com/anthropic" clean_env["ANTHROPIC_BASE_URL"] = "https://api.minimaxi.com/anthropic"
proc = await asyncio.create_subprocess_exec( proc = await asyncio.create_subprocess_exec(
@ -1103,6 +1103,18 @@ async def root():
@app.websocket("/ws/orchestrator") @app.websocket("/ws/orchestrator")
async def ws_orchestrator(websocket: WebSocket): async def ws_orchestrator(websocket: WebSocket):
"""WebSocket endpoint for orchestrator live signals.""" """WebSocket endpoint for orchestrator live signals."""
import sys
sys.path.insert(0, str(REPO_ROOT))
from orchestrator.config import OrchestratorConfig
from orchestrator.orchestrator import TradingOrchestrator
from orchestrator.live_mode import LiveMode
config = OrchestratorConfig(
quant_backtest_path=os.environ.get("QUANT_BACKTEST_PATH", ""),
)
orchestrator = TradingOrchestrator(config)
live = LiveMode(orchestrator)
await websocket.accept() await websocket.accept()
try: try:
while True: while True:
@ -1111,18 +1123,6 @@ async def ws_orchestrator(websocket: WebSocket):
tickers = payload.get("tickers", []) tickers = payload.get("tickers", [])
date = payload.get("date") date = payload.get("date")
# Lazy import to avoid loading heavy deps at startup
import sys
sys.path.insert(0, str(REPO_ROOT))
from orchestrator.config import OrchestratorConfig
from orchestrator.orchestrator import TradingOrchestrator
from orchestrator.live_mode import LiveMode
config = OrchestratorConfig(
quant_backtest_path=os.environ.get("QUANT_BACKTEST_PATH", ""),
)
orchestrator = TradingOrchestrator(config)
live = LiveMode(orchestrator)
results = await live.run_once(tickers, date) results = await live.run_once(tickers, date)
await websocket.send_text(json.dumps({"signals": results})) await websocket.send_text(json.dumps({"signals": results}))
except WebSocketDisconnect: except WebSocketDisconnect: