64 lines
8.6 KiB
Markdown
64 lines
8.6 KiB
Markdown
# The Conversations tab: pumping the LibreDesk conversations
|
|
|
|
2026-11-18. Two repositories: `~/aimsight-libredesk` (the pumping) and `aimsight-app` (the tab).
|
|
|
|
## Intent
|
|
|
|
The client requests arrive by mail in LibreDesk (`helpdesk.sensima.ch`, v2.9.0). The user wants their conversations on the workstation, in full (the messages, the inline images, the attachments), for two uses: to help the support (a conversation read next to the station's data, the analyses and the support answers), and as training data. Nothing is ever written to LibreDesk.
|
|
|
|
## Goals, non-goals, anti-goals
|
|
|
|
- **Goals**: a **Conversations** page in the app that pumps the conversations on demand and shows them; each conversation stored in `labeling/auto/conversations/<reference>/`, committed in `~/aimsight-labeling` like the analyses and the support answers; the second pumping fetches only what changed.
|
|
- **Non-goals (for now)**: linking a conversation to its stations (later: `acces/` of `~/aimsight-libredesk` maps a contact's e-mail to its serials); teaching the support agent to read `labeling/auto/conversations/` (`agents/agents/support.md`, a later change in `~/aimsight-agents`); turning the conversations into a training set (a later step; the raw material comes first); a scheduled pumping.
|
|
- **Anti-goals**: any request to LibreDesk other than a `GET`; the API key passing through the app or read by Claude; the HTML of a client's mail rendered as HTML in the page (a mail can carry scripts or tracking pixels).
|
|
|
|
## Constraints
|
|
|
|
- Read-only on LibreDesk, enforced by a guard test in `~/aimsight-libredesk`, like `tests/test_garde_fou_snippets.py` for the snippets.
|
|
- The key: a dedicated LibreDesk agent "Lecture conversations" whose role has only `conversations:read_all` and `messages:read`, created by the user; its key and secret in `~/aimsight-libredesk/.env` (`LIBREDESK_LECTURE_KEY`, `LIBREDESK_LECTURE_SECRET`), never read by Claude. The `Authorization: token <key>:<secret>` header goes only to the LibreDesk host (`add_unredirected_header`, as in `snippets/envoyer.py`).
|
|
- Standard library only in `~/aimsight-libredesk` (its rule); its code and docs in French (its language); the app in English.
|
|
- The app holds no domain logic: it calls the pumping of `~/aimsight-libredesk` as it calls `extraction/pumping.py` of `~/aimsight-data`.
|
|
- Every conversation, by the user's decision: all the inboxes, all the statuses, no filter.
|
|
- Committed in git, by the user's decision: the contacts' names and e-mails, and the attachments, go into the history of `~/aimsight-labeling`.
|
|
- Every time in UTC.
|
|
|
|
## Approaches
|
|
|
|
- **A. The pumping in `~/aimsight-libredesk`, called by the app** (chosen): all the LibreDesk code in one repository, the key never in the app, the guard test next to the existing one. Costs: two repositories touched, one more link in the app.
|
|
- **B. Everything in the app** (`app/server/conversations.py`): shorter, one repository; but the app would carry LibreDesk's logic and the API key, which the rule of the repository forbids.
|
|
- Dropped: a `claude` agent that pumps: too heavy for a download.
|
|
|
|
## The design
|
|
|
|
### `~/aimsight-libredesk/conversations/` (French)
|
|
|
|
- `pomper.py`: the client restricted to three routes, all `GET`: `/api/v1/conversations/all` (`page`, `page_size` up to 500, `order_by=updated_at`, `order=desc`), `/api/v1/conversations/{uuid}/messages` (every page, private notes included), and the attachment URLs that the messages carry (`attachments[].url`, signed: `/uploads/{uuid}?…` on LibreDesk, or a bucket's host if production stores in S3), downloaded wherever they point, the `Authorization` header only to the LibreDesk host; never a URL found in a message's text. `python3 -m conversations.pomper [--depuis YYYY-MM-DD --jusqu-a YYYY-MM-DD] --sortie <folder>`.
|
|
- **Incremental by default**: `<sortie>/pumped.json` (`{"until": <UTC time>}`, committed with the conversations) holds the start time of the last pumping that **completed**; a run walks the list from the most recently updated and stops below that mark (minus an hour of margin), and writes the new mark only at its end: a run cut short is caught up by the next one. No mark: the whole history. With `--depuis/--jusqu-a`, the conversations updated in that range (`--jusqu-a` excluded), stored ones fetched again; the mark untouched. *(Builder's call.)*
|
|
- A conversation is fetched in full each time it changed: `conversation.json` and `messages.json` rewritten, attachments already present not downloaded again (same name and size).
|
|
- `pomper.sh`: loads `.env` (`set -a`, as `extraction/pump.sh`), one pumping at a time (`flock` on `out/conversations.lock`), output appended to `out/conversations.log`.
|
|
- `pompage.py`: what the app calls, the twin of `extraction/pumping.py`: `start(depuis, jusqu_a, sortie)` → the process, `running()`, `log(lines)`.
|
|
- Tests (`tests/`, `unittest`, the fake server `tests/faux_libredesk.py` extended): only `GET`, the key never sent to another host, the incremental stop, the transcript (below), an attachment re-download skipped.
|
|
|
|
### One conversation: `labeling/auto/conversations/<reference_number>/`
|
|
|
|
- `conversation.json`: the conversation as the API gives it (subject, status, contact, inbox, dates, tags), `previous_conversations` removed.
|
|
- `messages.json`: the messages as the API gives them, the oldest first, attachment URLs replaced by their local path (`attachments/<name>`; a signed URL expires).
|
|
- `attachments/`: the files, under their name (a duplicate name prefixed by the attachment's uuid).
|
|
- `transcript.md`: the readable thread, written from `messages.json`: per message, a heading `## <UTC time> — <author> (<client | agent | private note>)`, the text (`text_content`, or the HTML reduced to text: tags removed, entities decoded, paragraphs kept), the images as ``, the other attachments as links. The `activity` messages (assignments, status changes) on one line in italics. No raw HTML in it.
|
|
|
|
### The app: the **Conversations** page
|
|
|
|
- A link `libredesk/` → `~/aimsight-libredesk` (ignored by git, in the README's table and install line, and in `CLAUDE.md`'s links).
|
|
- `app/server/conversations.py`: imports `pompage` (as `station.py` imports `pumping`); `pump(body)` (optional From/To), `pump_state()` (`running`, `log`), `list()` (each folder of `labeling/auto/conversations/`: reference, subject, contact name, status, last message time, message and attachment counts, read from `conversation.json`/`messages.json`), `transcript(reference, dark)` (`transcript.md` HTML-escaped **before** Markdown, so a client's `<script>` stays text; the page sent with `Content-Security-Policy: default-src 'none'; img-src 'self'; style-src 'unsafe-inline'`: no script, no remote image or tracking pixel), and the files of the folder through `analysis.file(…, FOLDER)`, images inline, any other attachment as a download (`Content-Disposition: attachment`: an `.html` or `.svg` attachment never runs at the app's address). The frame of the transcript sandboxed. *(Builder's call: the protection in the app, `transcript.md` stays plain readable text for the agents and the training.)*
|
|
- `server.py`: `GET/POST /api/conversations/pump`, `GET /api/conversations`, `GET /api/conversations/{reference}/report.html`, `GET /api/conversations/{reference}/{path}`.
|
|
- `web/src/pages/Conversations.jsx`, a page of the header next to Data and Stations: a card "Pump" (From/To optional, empty: what changed since the last pumping; the button; the end of the log read every 2 s), a table of the pumped conversations (the most recent first, a search field on subject and contact), a click shows its transcript in a frame, as the report of an analysis.
|
|
- `check.py`: the list and the transcript on a temporary folder, without LibreDesk.
|
|
- Docs: `app/README.md` (the page), `README.md` (the link, the tab), `docs/architecture.drawio` (the arrow app → aimsight-libredesk → labeling); `~/aimsight-libredesk/README.md` (the pumping, the key's role).
|
|
|
|
## Left to the builder
|
|
|
|
- The exact fields of an attachment in v2.9.0 (`name`, `content_type`, `size`, `url`, `content_id` expected): confirm on the first real response; the fake server follows it.
|
|
- Whether `updated_at` moves on a new message; if not, compare `last_message_at` as well.
|
|
- The commit in `~/aimsight-labeling` after a pumping: by the user, by path (`auto/conversations/`), not by the app.
|
|
- The order of the work: everything against the fake server first; then the user creates the read-only agent and puts its key in `.env`; then a first real pumping on a short range (Claude may launch it: the key stays in the subprocess), which settles the two points above; the whole history after, at a quiet hour. Nothing committed in `~/aimsight-labeling` before the user has looked at that first range.
|