- ExcelRepository: canonical columns with ru/en aliases, date/number normalization (Excel dates, DD.MM.YYYY, '1 234,56', currency symbols, parentheses), account name->UUID resolution, per-row errors with sheet!row, warnings for formulas without cached values, duplicate names and default exchange rates - ExcelTemplate: skeleton generator (number/date formats, dropdowns for currency, frequency en/ru and account, 'Инструкция' sheet); ship data/model_template.xlsx - CLI: cf template-xlsx [--force]; cf import-xlsx --dry-run with rich error table, import writes nothing when the file has errors - RecurringCashflow.account (empty = all accounts) + name/UUID matching in forecast; cf config recurring-add --account, account column in info/recurring-list - Forecast: runs are idempotent, 'Итог: Баланс' sums all accounts of the last month - README: 'Сборка модели в Excel' section - ruff check . clean (9 pre-existing import/line-length findings fixed) - MetaAgent: T9 archived, req-T9 approved, manifest/checkpoints/state synced 118 tests pass
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,
|
|
}
|