AI booking agent project (TypeScript) · Chapter 8 of 12
Memory of preferences
By the end of this chapter your agent remembers how a member likes to sit and what they watch, across every conversation: "I always sit on the aisle. Please remember that" is saved in one row of Postgres with an upsert that merges the languages in SQL, the member's preferences go into the system message of every turn, and your code, not the model, makes sure Pranav's seats are at the aisle. 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
Pranav has a bad knee. He always sits on the aisle, so he can stretch his leg, and he has told Datta's box office so on WhatsApp more than once. In the trial weekend he still got two seats in the middle of row P for a three-hour film, because the person typing that booking did not know.
Chapter 6 gave the agent a memory, but only of one conversation. Pranav says "I always sit on the aisle" on Monday; on Wednesday he starts a new conversation and the agent knows nothing about him. Members will not repeat it every time, and they should not have to. The agent needs a different kind of memory: a few facts about the member, kept across conversations, used on every turn. And because Pranav's knee depends on it, the most important use must not depend on the model choosing to follow a sentence.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
preferences.ts: remember how the member likes to sit
In src/agent/preferences.ts, write recall(userId): { seating: row.seating, languages: [...row.languages] } for the member's row of agentPreferences, or { seating: null, languages: [] } when there is none. Then remember(userId, { seating, languages }): one statement, db.insert(agentPreferences).values({ userId, seating, languages: [...new Set(words(languages))].sort() }) with .onConflictDoUpdate({ target: agentPreferences.userId, set: { seating: sql
coalesce(excluded.seating, ${agentPreferences.seating}), languages: MERGED, updatedAt: sqlnow()} }); then return recall(userId).The Check recalls Pranav with nothing saved, remembers Neha as aisle watching "marathi, Hindi " (Hindi, Marathi), then "English, marathi" (the seating kept, three languages), then centre (the languages kept), and wants one row for her. Then a transaction of its own adds Gujarati to Pranav's languages and commits while your remember adds Tamil: all three of his languages must be kept.
- Task 2
preferences.ts: a line for the system message
In src/agent/preferences.ts, write preferenceNote(prefs): null when there is no seating and no language; otherwise one sentence: 'The member likes aisle seats and watches Hindi and Marathi shows. Choose what fits.' With only a seating, 'The member likes centre seats. Choose what fits.'; with only languages, 'The member watches English shows. Choose what fits.'; three or more languages are joined like 'English, Hindi and Marathi'.
The Check compares your sentences for five sets of preferences, then saves Asha's (aisle, Marathi) and wants the system message of her next chat to hold the note.
- Task 3 · interview question
Interview: preferences.ts, use the memory in the tools
In src/agent/preferences.ts, write applyPreferences(name, args, prefs): for book_seats whose args have no seating, when prefs has a seating, return a new object with that seating; otherwise return args unchanged (another tool, a seating already given, none saved, no prefs). Never change the object you were given.
The Check calls it for six cases, then, through the app, Pranav says "I always sit on the aisle. Please remember that" (the agent calls save_preference) and then "Two seats for Ghar Wapsi Express on Saturday evening": the model calls book_seats without a seating, and his seats must be at the aisle (P1 and P2, not the centre of row P).
Nikhil will ask: the system message already tells the model that Pranav likes aisle seats. Why fill the seating in in code?
What you will learn
- Facts, not messages: one row per member, read at the start of every turn.
- The upsert: INSERT ... ON CONFLICT DO UPDATE, and excluded, in SQL and in Drizzle.
- coalesce and arrays: keep the old value when no new one is given; merge two lists of languages inside the statement.
- The lost update: why a merge made from a read you did earlier loses data, shown with two transactions.
- The memory in the prompt: one plain sentence in the system message.
- The memory in code: filling in the seating for book_seats, because a sentence is not a rule.
More in Remembering
Build "Memory of preferences" 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.