AI booking agent project (TypeScript) · Chapter 6 of 12

Conversations in Postgres

By the end of this chapter your agent remembers a conversation: every message the model was sent and every message it wrote is stored in Postgres, in order, so Neha can answer the agent's question in her next message, or come back tomorrow. Only she can read her conversation, and two requests saving into it at the same moment can never mix it up. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Remembering
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of AI booking agent project (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Members do not write everything in one message. On WhatsApp, Neha writes "Book seats for Ghar Wapsi Express on Saturday evening", Datta's box office asks "How many?", and she answers "2". Many of the 286 trial bookings came in two or three messages like that.

Your agent asks the right question. Then it forgets. Chapter 5's server, two requests from Neha:

{"status":"answered","answer":"Happy to help. Could you tell me how many seats you want?","steps":1,"calls":[]}
[200]
{"status":"answered","answer":"Happy to help. Could you tell me which day you want to go?","steps":1,"calls":[]}
[200]

lab requests 1 shows why. The second request carried only this:

system    You are Parda's booking assistant in Pune. You book seats at Parda's cinemas and stages for the signed-in member with the tools, and you answer in short, plain sentences.
          Today is Thursday 2026-11-05. Prices are whole rupees, for one seat.
user      2 seats please
reply     Happy to help. Could you tell me which day you want to go?
tokens: 643 in, 15 out

As chapter 1 showed, the model keeps nothing between requests. A conversation exists only because you send all of it, every time. So it has to live somewhere between Neha's two requests, somewhere that survives a restart and is shared by every server: Postgres.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    store.ts: start a conversation, find the newest

    In src/agent/store.ts, write startConversation(userId): insert an agentConversations row for the member and return its id. Then latestConversation(userId): the id of the member's conversation with the highest id, or null when they have none.

    The Check asks for Neha's newest before she has any (null), starts two for her and one for Pranav, and wants two rows for Neha, their ids returned, and her second one as the newest.

  2. Task 2

    store.ts: load a conversation, only the member's own

    In src/agent/store.ts, write loadMessages(conversationId, userId): the message column of each of the conversation's rows in agentMessages, ordered by position. When the conversation does not exist or is not this member's, throw notFound('There is no conversation <id>'): the same 404 both times.

    The Check loads a new conversation (empty), stores two messages at positions 1 and 0 with SQL and wants them back in position order, then wants a 404 AppError for Pranav's conversation loaded as Neha and for conversation 999.

  3. Task 3 · interview question

    Interview: store.ts, save the new messages

    In src/agent/store.ts, write saveMessages(conversationId, messages): append the messages at the next positions (the highest position so far plus 1, or 0 for an empty conversation), set the conversation's updatedAt to sqlnow(), all in one transaction (db.transaction, every query on tx). Return how many it added.

    The Check saves two messages (positions 0 and 1, updated_at now), then a tool call (position 2, stored as it was). Then a transaction of the Check's own takes position 4 while your save of two messages runs, and commits first: your save must reject with Postgres's unique violation (23505) and keep neither message. Then, as Neha through the app, the agent asks how many seats, she answers "2 seats please" in the next request, and the run must go on from the stored conversation (find_showtimes, book_seats), with every message stored.

    Nikhil will ask: why store every message, the tool calls and the tool results too, and not just what Neha typed and what the agent answered?

What you will learn

  • Memory is the message list: what the model "remembers" is what you resend.
  • Two tables: conversations, and messages stored as jsonb at a position.
  • A row and its id: insert with returning; one value with orderBy, limit(1), or null.
  • Only the member's own: the same 404 for "not yours" and "does not exist".
  • The next position: max() in a select, and one transaction for the whole save.
  • Two saves at once: how the unique (conversation_id, position) refuses the second, with Postgres's real error.
  • What it costs: every turn resends everything, so prompts grow.

More in Remembering

Build "Conversations in Postgres" yourself

Parda members want to book by chat. You build the agent that finds the show, holds the seats through Parda's own API, and asks before it pays. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI booking agent project (TypeScript) are free. This one comes with the all labs plan.