# Chat

The chat answers questions about the filings, signals and companies **your package covers**, from RiskPilot's own
data. Before every answer the server retrieves the matching signals and companies inside your scope (classes, history
window, families) and gives only those to the model, so an answer can never draw on data outside your package. Each
answer cites the filings it used.

The chat is not investment advice: it never recommends buying, selling or holding a security and declines questions
outside RiskPilot's data. Every status, conversation and answer carries the `disclaimer` text to show next to it.

The routes take a session or the live key (the sandbox key gets `403 sandbox_read_only`; without paid access `402`).

## Check availability

```bash
curl -sS "$RISKPILOT_URL/api/v1/customer/chat" -H "Authorization: Bearer $RISKPILOT_SESSION"
```

```json
{
  "available": true,
  "reason": null,
  "message": "…",
  "quota": { "monthlyLimit": 300, "used": 12, "remaining": 288, "resetsAt": "2026-12-01T00:00:00.000Z", "perMinute": 5 },
  "maxInputChars": 2000,
  "disclaimer": "RiskPilot chat explains filings and signals from RiskPilot's own data. …"
}
```

`available` is false with `reason` `not_configured` (the chat is switched off on the server) or `package_disabled`
(not part of your package). Earlier conversations stay readable and deletable either way.

## Conversations

| Route | Does |
| --- | --- |
| `GET /api/v1/customer/chat/conversations?limit&offset` | Your conversations, newest first |
| `POST /api/v1/customer/chat/conversations` | Start one (optional `{ "title" }`); `409 chat_conversations_full` at 200 |
| `GET /api/v1/customer/chat/conversations/{id}` | One conversation with its messages |
| `DELETE /api/v1/customer/chat/conversations/{id}` | Delete it and its messages (204) |

A message is `{ id, role: "user" | "assistant", content, citations, createdAt }`. Another account's conversation id is
404.

## Ask a question

`POST /api/v1/customer/chat/conversations/{id}/messages` with `{ "message": "What did NVDA file this week?" }`.

### As one JSON answer

```json
{
  "conversationId": "41",
  "userMessage": { "id": "301", "role": "user", "content": "What did NVDA file this week?", … },
  "message": { "id": "302", "role": "assistant", "content": "NVIDIA filed one 8-K on … [0001045810-26-000112]", "citations": [ … ], … },
  "usage": { "inputTokens": 2811, "outputTokens": 164 },
  "quota": { "monthlyLimit": 300, "used": 13, "remaining": 287, … },
  "disclaimer": "…"
}
```

### Streamed, as server-sent events

Send `Accept: text/event-stream` (or add `?stream=1`). The answer arrives in pieces:

```text
event: start
data: {"conversationId":"41","userMessage":{…}}

event: delta
data: {"text":"NVIDIA filed one 8-K "}

event: delta
data: {"text":"on 6 November …"}

event: done
data: {"conversationId":"41","userMessage":{…},"message":{…},"usage":{…},"quota":{…},"disclaimer":"…"}
```

or, when the model fails midway, `event: error` with `{ "error": "…", "code": "chat_model_failed" }`. `done` carries
the same JSON as the non-streamed answer.

```js
const res = await fetch(`${base}/api/v1/customer/chat/conversations/${id}/messages`, {
  method: "POST",
  headers: { Authorization: `Bearer ${session}`, "Content-Type": "application/json", Accept: "text/event-stream" },
  body: JSON.stringify({ message: question }),
});
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
let buffer = "";
for (;;) {
  const { value, done } = await reader.read();
  if (done) break;
  buffer += value;
  let end;
  while ((end = buffer.indexOf("\n\n")) >= 0) {
    const block = buffer.slice(0, end);
    buffer = buffer.slice(end + 2);
    const event = /^event: (.*)$/m.exec(block)?.[1];
    const data = JSON.parse(/^data: (.*)$/m.exec(block)?.[1] ?? "null");
    if (event === "delta") render(data.text);
    else if (event === "done") finish(data.message);
    else if (event === "error") fail(data);
  }
}
```

Closing the request stops the model call.

## Citations

The model cites accession numbers in square brackets. The server keeps only filings that were in the data the model
was given, in order of first mention, and returns each as:

```json
{
  "accession": "0001045810-26-000112",
  "ticker": "NVDA",
  "issuerName": "NVIDIA Corp",
  "filedAt": "2026-11-06T21:05:12.000Z",
  "className": "WATCH",
  "appPath": "/c/signals/0001045810-26-000112",
  "url": "https://…/c/signals/0001045810-26-000112",
  "secUrl": "https://www.sec.gov/…"
}
```

An accession the model invents, or one outside your package, never becomes a citation. `secUrl` may be `null`.

## Limits and errors

| What | Default | Answer |
| --- | --- | --- |
| Questions per month (UTC) | per package: 100 `signal`, 300 `desk`, 1000 `firm` | `429 chat_quota_exceeded` |
| Questions per minute | 5 | `429 chat_rate_limited` |
| Question length | 2000 characters | `400 message_too_long` |
| Conversations kept | 200 | `409 chat_conversations_full` |
| Messages per conversation | 200 | `409 chat_conversation_full` |

Other answers: `400 message_required`, `403 chat_disabled` (not in your package), `404` (unknown conversation),
`502 chat_model_failed` (the model failed; not counted against your quota), `503 chat_unavailable` (switched off).
A deleted conversation still counts towards the month. The package's requests per minute apply as on every route.
