T5: Dependency Injection — services через composition root

- AssistantService теперь принимает ForecastService в конструкторе
  (раньше создавал new ForecastService() внутри analyze/advice)
- infrastructure/cli/main.py: _build_services() — composition root,
  собирает граф ForecastService, ScenarioService, AssistantService, CurrencyConverter
- Все CLI-команды используют _build_services(model) вместо прямого new
- test_ai.py: обновлён под новый контракт AssistantService

Tests: 67/67 pass.
This commit is contained in:
2026-10-08 16:53:04 +03:00
parent 3f7268d14c
commit 541a76835f
3 changed files with 36 additions and 30 deletions
+5 -5
View File
@@ -23,17 +23,18 @@ class AssistantService:
def __init__(
self,
model: FinancialModel,
forecast_service: ForecastService,
converter: CurrencyConverter | None = None,
display_currency: str | None = None,
):
self.model = model
self.forecast_service = forecast_service
self.converter = converter or CurrencyConverter(model.exchange_rates)
self.display_currency = display_currency or model.base_currency
def analyze(self, months: int = 12) -> dict:
forecast_service = ForecastService(self.model)
forecast_result = forecast_service.forecast_cashflow(months)
summary = forecast_service.summary(months)
forecast_result = self.forecast_service.forecast_cashflow(months)
summary = self.forecast_service.summary(months)
prompt = prompts.format_context(
model_json=json.dumps(self.model.to_dict(), indent=2, ensure_ascii=False, cls=_DecimalEncoder),
@@ -51,8 +52,7 @@ class AssistantService:
}
def advice(self, question: str, months: int = 12) -> dict:
forecast_service = ForecastService(self.model)
forecast_result = forecast_service.forecast_cashflow(months)
forecast_result = self.forecast_service.forecast_cashflow(months)
prompt = prompts.ADVICE_PROMPT.format(
model_json=json.dumps(self.model.to_dict(), indent=2, ensure_ascii=False, cls=_DecimalEncoder),