Files
sure/app/models/assistant.rb
T
Brandon ce92b36351 feat(bills): assistant and MCP tools for bills (#3203)
* feat(bills): assistant and MCP tools for bills

Last of three chunks carved out of #3083, stacked on the UI bundle. Exposes
bills to the builtin assistant and to MCP clients. Everything here is gated
behind preview features, so the tools are absent from tools/list until a user
opts in.

Seven tools:

- get_bills, get_bill_details and get_paycheck_plan for reads
- get_bill_audit, a deterministic review that surfaces likely duplicates, price
  changes, trials about to convert, upcoming renewals and long-overdue bills
- create_bill, update_bill and record_bill_payment for writes

Shared argument parsing, permission checks and error shapes live in
BillsSupport, so every tool answers with the same {error, hint} contract the
existing tools use, and a bad argument never aborts the turn.

The write tools mutate financial records on a model's say-so, so they refuse
rather than guess: a payment cannot exceed what its cycle still owes, a repeated
settle will not quietly close next month, an unrecognized frequency is an error
instead of a silent monthly default, and non-finite or negative amounts are
rejected before they reach the database.

The read tools say what they filtered. An empty result names the statuses that
do hold matches, the paycheck plan discloses the unconfirmed series it excluded
from spending headroom, and history and price-change windows report their real
totals rather than letting a caller sum a truncated list.

A not-found no longer returns the scoped relation's SQL, which handed any MCP
client the access-control schema for the cost of a guessed id.

The in-page AI helpers are not here. Smart fill and smart configuration are
buttons on the bills pages, so they ship with the UI bundle along with the
provider-side suggester they call.

Suite 7,854 runs, 0 failures. Rubocop clean, eager loading verified.

* Address the ready-review round

* Reject an out-of-range audit lookback out loud

* Speak the cycle remainder guard through the allocator locale
2026-09-02 07:06:13 +02:00

92 lines
2.8 KiB
Ruby

module Assistant
Error = Class.new(StandardError)
REGISTRY = {
"builtin" => Assistant::Builtin,
"external" => Assistant::External
}.freeze
# Tools for users who opted into preview features in Settings -> Preferences.
#
# Statement Vault + provenance tools back the wealth agent-harness workflow
# documented in docs/llm-guides/wealth-agent-harness.md. GetValuations is the
# read pair for RecordValuation; GetInsights reads the Insights feed, which is
# itself preview-gated app-wide.
#
# The bills tools back the preview-gated Bills subsystem.
PREVIEW_FUNCTION_CLASSES = [
Function::UploadAccountStatement,
Function::ListAccountStatements,
Function::GetAccountStatement,
Function::GetStatementCoverage,
Function::RecordValuation,
Function::GetValuations,
Function::GetInsights,
# Bills: the whole subsystem is preview-gated, so its tools ride the same
# per-user flag as the surfaces they operate on. Each tool additionally
# re-checks the family's recurring feature gate and the user's
# account-access scope itself.
Function::GetBills,
Function::GetBillDetails,
Function::GetPaycheckPlan,
Function::GetBillAudit,
Function::CreateBill,
Function::UpdateBill,
Function::RecordBillPayment
].freeze
class << self
def for_chat(chat)
implementation_for(chat).for_chat(chat)
end
def config_for(chat)
raise Error, "chat is required" if chat.blank?
Assistant::Builtin.config_for(chat)
end
def available_types
REGISTRY.keys
end
# The single registry behind both the builtin chat and the /mcp endpoint's
# tools/list — a function class added here is immediately callable by an
# external agent, so pass the user to keep preview tools out of the default
# surface.
def function_classes(user = nil)
classes = [
Function::GetTransactions,
Function::GetRecurringTransactions,
Function::GetAccounts,
Function::GetHoldings,
Function::GetBalanceSheet,
Function::GetIncomeStatement,
Function::GetBudget,
Function::ImportBankStatement,
Function::SearchFamilyFiles,
Function::CreateGoal,
Function::GetTags,
Function::CreateTag,
Function::UpdateTag,
Function::GetCategories,
Function::CreateCategory,
Function::UpdateCategory,
Function::GetMerchants,
Function::UpdateTransaction,
Function::UpdateBudget
]
classes += PREVIEW_FUNCTION_CLASSES if user&.preview_features_enabled?
classes
end
private
def implementation_for(chat)
raise Error, "chat is required" if chat.blank?
type = ENV["ASSISTANT_TYPE"].presence || chat.user&.family&.assistant_type.presence || "builtin"
REGISTRY.fetch(type) { REGISTRY["builtin"] }
end
end
end