Let the billing assistant look up the customer's invoices
Node · Node · intermediate · modification
Lets the billing assistant answer questions about the customer's invoices from live data. Adds a `list_invoices` function tool (status filter, up to 20 invoices) whose arguments are parsed and checked against a strict zod schema, so a malformed or out-of-range call goes back to the model as a tool error instead of reaching billing. `answer` now runs the standard tool loop (the assistant turn with its `tool_calls`, then one `tool` message per call id), capped at four model calls with a 502 after that, and the system prompt tells the model which account is signed in and to stay on it. The route passes the session's account id into `answer`.
TypeScript Express 4 customer portal of Ledgerly, a billing SaaS; `openai` 7.43.0, `zod` 4.6.7. `requireCustomer` (existing, unchanged) answers 401 without a valid portal session and otherwise sets `req.customer = { accountId, email }` (typed by the existing augmentation in `src/auth.ts`). `src/billing.ts` (unchanged) wraps the billing provider with the platform's secret key, which can read every account: `billing.listInvoices(accountId, { status, limit })` returns up to `limit` invoices of that account, newest first, as `{ number, status, amountDue, currency, dueDate }`, and `[]` for an unknown account id; `GET /api/invoices` calls it with `req.customer.accountId`. Account ids (`acct_` plus up to 32 letters and digits) are not secret: they are printed on every invoice PDF and in shared payment links. Per-customer rate limiting of the assistant is done by the existing portal middleware. `HELP_FACTS`, `src/openai.ts` and `src/logger.ts` are unchanged.
Requirements
- `POST /api/assistant/chat` (existing, behind `requireCustomer`) keeps its request and response: `{ messages: [{ role: 'user' | 'assistant', content }] }`, 1 to 20 turns of 1 to 4,000 characters with the customer's turn last (the existing zod schema answers 400 otherwise), answered with `{ reply }`. This PR gives the assistant a `list_invoices` tool so it can answer questions such as "is my last invoice paid?" from live data, newest first, optionally narrowed by status (`open`, `paid`, `void`) and count (1 to 20, default 5).
- A customer may see only their own account's invoices, exactly as on `GET /api/invoices`, whatever the conversation says. The portal widget sends the whole conversation, so every turn in `messages`, assistant turns included, is written or editable by the customer, and anything that reaches the model can come back to them in `reply`.
- Tool arguments are an untrusted JSON string from the model. Malformed JSON, a non-object, an unknown key, or a value outside the declared types and bounds (`limit` must be an integer: `5.0` counts, `true` does not) goes back to the model as `{"error": ...}` in that call's `role: "tool"` message and never reaches the billing service. A billing-service failure is not handled here: it propagates to the app's error handler (500), as on `GET /api/invoices`.
- Chat-completions contract: the assistant message carrying `tool_calls` is appended before its tool results, with one result per `tool_call_id`. At most `MAX_MODEL_CALLS = 4` model calls per request; after that the endpoint answers 502 `assistant_unavailable`, as it does for a provider error (the SDK's built-in retries are the only retry). A reply without tool calls ends the loop, `null` content as `""`. The model is the dated snapshot `gpt-4.1-mini-2025-04-14`.
Files touched
- src/assistant/tools.ts
- src/assistant/chat.ts
- src/routes/assistant.ts