refactor: centralize bootstrap dependency assembly

This commit is contained in:
leefer
2026-07-29 17:18:03 +08:00
parent 12c0f4777d
commit 3994387935
9 changed files with 216 additions and 51 deletions
+20 -47
View File
@@ -4,7 +4,6 @@ import argparse
import copy
import json
import mimetypes
import os
import re
import secrets
import threading
@@ -16,10 +15,10 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Any
from urllib.parse import parse_qs, unquote, urlparse
from alert_service import AlertService
from assistant_agent import ReviewAssistantError, stream_review_assistant
from api_access import required_role
from chart_data_provider import ChartDataError, EastmoneyChartClient, MarketChartClient
from backend.bootstrap import build_application_container, load_runtime_settings
from chart_data_provider import ChartDataError
from app_config import (
DATA_DIR,
MENTOR_SKILLS_DIR,
@@ -30,12 +29,9 @@ from app_config import (
TOKEN_PATTERN,
USERNAME_PATTERN,
add_months as _add_months,
load_local_env,
membership_boundary as _membership_boundary,
normalize_date,
parse_iso_datetime as _parse_iso_datetime,
remove_local_env,
save_local_env,
tushare_code,
validate_stock_code,
validate_text,
@@ -50,17 +46,15 @@ from heaven_engine import (
build_personal_field,
hexagram_from_lines,
)
from ifind_client import IfindError, IfindHttpClient
from ifind_client import IfindError
from llm_strategy import LLMCompilerError, compile_strategy_with_llm, test_llm_connection
from mentor_agent import MentorAgentError, MentorSkillRegistry, stream_with_mentor
from mentor_agent import MentorAgentError, stream_with_mentor
from market_insights import MarketInsightsService
from realtime_aggregator import WebRealtimeAggregator
from screener import (
FACTOR_FIELDS,
FACTOR_GROUPS,
REGIMES,
FactorDataService,
ScreenerEngine,
compile_local_strategy,
)
from security import SecretVault, hash_password, token_hash, verify_password
@@ -71,8 +65,6 @@ from sentiment_engine import (
build_sentiment_history,
latest_contiguous_history,
)
from strategy_tracking import StrategyTrackingService
from trade_journal import TradeJournalService
from tushare_client import TushareClient, TushareError, _sector_coverage_issue
@@ -172,30 +164,8 @@ MENTOR_ETF_UNIVERSE = (
class DashboardService:
def __init__(self) -> None:
load_local_env()
environment_credentials = {
"tushare_token": os.environ.get("TUSHARE_TOKEN", "").strip(),
"ifind_refresh_token": os.environ.get("IFIND_REFRESH_TOKEN", "").strip(),
"ifind_access_token": os.environ.get("IFIND_ACCESS_TOKEN", "").strip(),
"platform_llm_primary_api_key": os.environ.get(
"LLM_PRIMARY_API_KEY", os.environ.get("LLM_API_KEY", "")
).strip(),
"platform_llm_primary_base_url": os.environ.get(
"LLM_PRIMARY_BASE_URL", os.environ.get("LLM_BASE_URL", "https://api.openai.com/v1")
).strip(),
"platform_llm_primary_model": os.environ.get(
"LLM_PRIMARY_MODEL", os.environ.get("LLM_MODEL", "")
).strip(),
"platform_llm_fallback_api_key": os.environ.get("LLM_FALLBACK_API_KEY", "").strip(),
"platform_llm_fallback_base_url": os.environ.get("LLM_FALLBACK_BASE_URL", "").strip(),
"platform_llm_fallback_model": os.environ.get("LLM_FALLBACK_MODEL", "").strip(),
}
encryption_key = os.environ.get("APP_ENCRYPTION_KEY", "").strip()
if not encryption_key:
encryption_key = SecretVault.generate_key()
save_local_env({"APP_ENCRYPTION_KEY": encryption_key})
os.environ["APP_ENCRYPTION_KEY"] = encryption_key
self.vault = SecretVault(encryption_key)
runtime = load_runtime_settings()
self.vault = SecretVault(runtime.encryption_key)
self.database = ReviewDatabase(DATA_DIR / "review.db")
self.sync_lock = threading.Lock()
self.auth_lock = threading.Lock()
@@ -204,18 +174,21 @@ class DashboardService:
self._auto_screener_last_attempt: dict[str, datetime] = {}
self._ifind_event_lock = threading.Lock()
self._request_context = threading.local()
self._system_credentials = self._load_system_credentials(environment_credentials)
self.ifind = IfindHttpClient(
str(self._system_credentials.get("ifind_refresh_token") or ""),
str(self._system_credentials.get("ifind_access_token") or ""),
self._system_credentials = self._load_system_credentials(runtime.initial_credentials)
self.container = build_application_container(
self.database,
self._system_credentials,
MENTOR_SKILLS_DIR,
PRIVATE_MENTOR_SKILLS_DIR,
)
self.screener = ScreenerEngine(self.database)
self.strategy_tracking = StrategyTrackingService(self.database)
self.alert_service = AlertService(self.database)
self.trade_journal = TradeJournalService(self.database)
self.mentor_skills = MentorSkillRegistry(MENTOR_SKILLS_DIR, PRIVATE_MENTOR_SKILLS_DIR)
self.realtime_aggregator = WebRealtimeAggregator()
self.chart_data = MarketChartClient(self.ifind, EastmoneyChartClient())
self.ifind = self.container.ifind
self.screener = self.container.screener
self.strategy_tracking = self.container.strategy_tracking
self.alert_service = self.container.alert_service
self.trade_journal = self.container.trade_journal
self.mentor_skills = self.container.mentor_skills
self.realtime_aggregator = self.container.realtime_aggregator
self.chart_data = self.container.chart_data
self.screener.ensure_builtin_strategies()
self._background_stop = threading.Event()
self._background_thread = threading.Thread(