Conversation History
How conversations are created, how prior messages reach each model call, and how a turn is persisted.
Every turn loads the conversation’s prior messages from Postgres. The answering call receives all of them; the grade, rewrite and draft calls receive the last six as a transcript. The graph’s turn is written to the database after the graph stops.
Data model
conversations
id UUID PK
client_id VARCHAR(64) scopes the conversation to one client
started_at DATETIME
total_cost FLOAT accumulated USD across all turns
total_tokens INT
messages
id INTEGER PK autoincrement
conversation_id UUID FK → conversations.id (CASCADE DELETE)
role VARCHAR(32) "user" | "assistant"
content JSON the message text
citations JSON { segments: [...] } on assistant messages
run_id VARCHAR(64) the graph run that produced an assistant message
created_at DATETIME
run_id is the join from a reply back to its step trail in runs.
Creating a conversation
On the first turn, pass conversation_id: null. The API creates a Conversation row and returns its id in the run and done SSE events:
// POST /api/c/gcp-platform-support/chat/stream
{ "message": "What does the PORT error mean?", "conversation_id": null }
// done event
{ "conversation_id": "3f2a1b4c-...", "run_id": "9c1e...", "outcome": "answer", ... }
Include the returned conversation_id on every later request to continue the conversation.
The conversation row commits before the graph runs. The step trail is written on its own session (D3) and references the conversation, so the parent must already be visible to that session. A turn that then fails leaves an empty conversation, not corrupted history.
Loading history
prepare_conversation() in agent/common.py loads every prior message, ordered by id:
history = SELECT role, content FROM messages WHERE conversation_id = ? ORDER BY id
| Call | What it sees of the history |
|---|---|
answer | Every prior message, then the passages and the question |
grade, rewrite, escalate | The last 6 text turns, pasted into the user message as a transcript |
There is no sliding window or summarization. The history is not cached; see Prompt Caching.
Persisting a turn
After the graph stops, stream_turn() adds two rows and commits once:
- The user message, as sent.
- The assistant reply, with its
segmentsand therun_id.
A confirmed ticket adds a third. confirm_ticket() writes the confirmation as its own assistant message, so reloading the conversation shows it and not just the offer.
On any exception the request session rolls back and the stream ends with error. The step trail survives, because it was never written on that session.
Reloading a conversation
GET /api/c/{client_id}/conversations/{conversation_id} returns each user message’s text and each assistant message’s segments. An assistant message with a run_id also carries that run’s steps, loaded from runs in one query, so the trail renders on reload exactly as it did live.
Multi-client isolation
conversation_id is a global UUID, but every load checks the Conversation row’s client_id. A conversation created under one client returns 404 when requested under another. The streaming endpoint runs that check before the response starts, so the rejection is a plain HTTP 404 rather than an error event.
Request flow
POST /api/c/:client_id/chat/stream
│
├─ ownership + budget check own short-lived session, before streaming
│
└─ stream_turn()
├─ prepare_conversation() new → INSERT + commit · else SELECT history
├─ RunRecorder.start() INSERT runs row
├─ graph.astream(...) nodes stream steps, text, citations
├─ INSERT user + assistant messages
├─ update_conversation_totals()
└─ db.commit() → yield done
Querying history directly
SELECT role, created_at, content, run_id
FROM messages
WHERE conversation_id = '3f2a1b4c-...'
ORDER BY id;