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
+9
View File
@@ -0,0 +1,9 @@
from .container import ApplicationContainer, build_application_container
from .settings import RuntimeSettings, load_runtime_settings
__all__ = [
"ApplicationContainer",
"RuntimeSettings",
"build_application_container",
"load_runtime_settings",
]
+50
View File
@@ -0,0 +1,50 @@
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from alert_service import AlertService
from chart_data_provider import EastmoneyChartClient, MarketChartClient
from database import ReviewDatabase
from ifind_client import IfindHttpClient
from mentor_agent import MentorSkillRegistry
from realtime_aggregator import WebRealtimeAggregator
from screener import ScreenerEngine
from strategy_tracking import StrategyTrackingService
from trade_journal import TradeJournalService
@dataclass(frozen=True)
class ApplicationContainer:
database: ReviewDatabase
ifind: IfindHttpClient
screener: ScreenerEngine
strategy_tracking: StrategyTrackingService
alert_service: AlertService
trade_journal: TradeJournalService
mentor_skills: MentorSkillRegistry
realtime_aggregator: WebRealtimeAggregator
chart_data: MarketChartClient
def build_application_container(
database: ReviewDatabase,
credentials: dict[str, object],
mentor_skills_dir: Path,
private_mentor_skills_dir: Path,
) -> ApplicationContainer:
ifind = IfindHttpClient(
str(credentials.get("ifind_refresh_token") or ""),
str(credentials.get("ifind_access_token") or ""),
)
return ApplicationContainer(
database=database,
ifind=ifind,
screener=ScreenerEngine(database),
strategy_tracking=StrategyTrackingService(database),
alert_service=AlertService(database),
trade_journal=TradeJournalService(database),
mentor_skills=MentorSkillRegistry(mentor_skills_dir, private_mentor_skills_dir),
realtime_aggregator=WebRealtimeAggregator(),
chart_data=MarketChartClient(ifind, EastmoneyChartClient()),
)
+55
View File
@@ -0,0 +1,55 @@
from __future__ import annotations
import os
from dataclasses import dataclass
from typing import Mapping
from app_config import load_local_env, save_local_env
from security import SecretVault
def environment_credentials(environment: Mapping[str, str]) -> dict[str, str]:
return {
"tushare_token": str(environment.get("TUSHARE_TOKEN") or "").strip(),
"ifind_refresh_token": str(environment.get("IFIND_REFRESH_TOKEN") or "").strip(),
"ifind_access_token": str(environment.get("IFIND_ACCESS_TOKEN") or "").strip(),
"platform_llm_primary_api_key": str(
environment.get("LLM_PRIMARY_API_KEY") or environment.get("LLM_API_KEY") or ""
).strip(),
"platform_llm_primary_base_url": str(
environment.get("LLM_PRIMARY_BASE_URL")
or environment.get("LLM_BASE_URL")
or "https://api.openai.com/v1"
).strip(),
"platform_llm_primary_model": str(
environment.get("LLM_PRIMARY_MODEL") or environment.get("LLM_MODEL") or ""
).strip(),
"platform_llm_fallback_api_key": str(
environment.get("LLM_FALLBACK_API_KEY") or ""
).strip(),
"platform_llm_fallback_base_url": str(
environment.get("LLM_FALLBACK_BASE_URL") or ""
).strip(),
"platform_llm_fallback_model": str(
environment.get("LLM_FALLBACK_MODEL") or ""
).strip(),
}
@dataclass(frozen=True)
class RuntimeSettings:
encryption_key: str
initial_credentials: dict[str, str]
def load_runtime_settings() -> RuntimeSettings:
load_local_env()
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
return RuntimeSettings(
encryption_key=encryption_key,
initial_credentials=environment_credentials(os.environ),
)