Было: `ruff check .` → 10 ошибок, CI-гейт на ruff был бы красным сразу. - 9× I001 (порядок импортов) — application/forecast.py:1, application/scenarios.py:1, domain/currency.py:1, infrastructure/ai/assistant.py:1, infrastructure/cli/app.py:11, infrastructure/cli/commands/init.py:2, infrastructure/cli/services.py:2, tests/test_model.py:1, tests/test_repositories.py:1 - 1× E501 — infrastructure/cli/services.py:30, сигнатура build_services перенесена на три строки Поведение не меняется: переупорядочены только импорты, убраны пустые строки, перенесена длинная сигнатура. Порядок импорта в app.py не влияет на момент вызова setup_i18n() — он по-прежнему после импорта команд. Проверка: 75/75 тестов зелёные, ruff check . — All checks passed.
80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
import json
|
|
from decimal import Decimal
|
|
|
|
from application.forecast import ForecastService
|
|
from domain import CurrencyConverter, FinancialModel
|
|
from infrastructure.ai import prompts
|
|
|
|
|
|
class AssistantError(Exception):
|
|
pass
|
|
|
|
|
|
class _DecimalEncoder(json.JSONEncoder):
|
|
"""JSON-сериализатор: Decimal → str (для AI-промптов)."""
|
|
|
|
def default(self, o):
|
|
if isinstance(o, Decimal):
|
|
return str(o)
|
|
return super().default(o)
|
|
|
|
|
|
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_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),
|
|
forecast_json=json.dumps(forecast_result, indent=2, ensure_ascii=False, cls=_DecimalEncoder),
|
|
months=months,
|
|
base_currency=self.model.base_currency,
|
|
display_currency=self.display_currency,
|
|
)
|
|
|
|
return {
|
|
"prompt": prompt,
|
|
"summary": summary,
|
|
"forecast": forecast_result,
|
|
"ai_response": None,
|
|
}
|
|
|
|
def advice(self, question: str, months: int = 12) -> dict:
|
|
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),
|
|
forecast_json=json.dumps(forecast_result, indent=2, ensure_ascii=False, cls=_DecimalEncoder),
|
|
question=question,
|
|
base_currency=self.model.base_currency,
|
|
display_currency=self.display_currency,
|
|
)
|
|
|
|
return {
|
|
"prompt": prompt,
|
|
"ai_response": None,
|
|
}
|
|
|
|
def compare_scenarios(self, scenarios_json: str) -> dict:
|
|
prompt = prompts.SCENARIO_COMPARISON_PROMPT.format(
|
|
scenarios_json=scenarios_json,
|
|
base_currency=self.model.base_currency,
|
|
display_currency=self.display_currency,
|
|
)
|
|
return {
|
|
"prompt": prompt,
|
|
"ai_response": None,
|
|
}
|