AI support copilot project (TypeScript) · Chapter 10 of 12

Conversations

By the end of this chapter you can store a conversation and its messages in Postgres, read back its last few turns in the right order, turn a follow-up such as "What about Kala Talkies?" into a question that stands on its own, and answer it inside the conversation, with both turns saved in one transaction. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ready for members
  • 3 tasks
  • TypeScript
  • In the all labs plan

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

Why you need this

Members do not write one perfect question. Neha's father uses a wheelchair. She asks "Can a wheelchair user get into The Attic?", reads the answer (no: it is up a flight of stairs), and then writes "What about Kala Talkies?". Datta sees messages like that all day on WhatsApp: "is it free?", "and for a play?", "what about Sunday?". Each one means something only next to the message before it.

The copilot answers every question alone. This is chapter 9's POST /ask with Neha's second message:

{"question":"What about Kala Talkies?","answer":"Kala Talkies is a short walk from Mandai, and most autos know it by name. Kala Talkies is known for its vada pav and cutting chai.","found":true,"citations":[{"chunkId":"venues-05","pageId":"venues","title":"Venues and addresses","heading":"Getting there"},{"chunkId":"food-and-drinks-03","pageId":"food-and-drinks","title":"Food and drinks at the venue","heading":"Buying at the counter"}],"tokens":{"input":487,"output":55}}

Directions and vada pav, cited and sure of itself, and nothing about the ramp Neha needs. Search did its job: on its own, "What about Kala Talkies?" is about Kala Talkies in general. The meaning is in the turn before it, and nothing in the app remembers the turn before.

What you build: 3 tasks

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

  1. Task 1

    service.ts: store a conversation and read its history

    In src/conversations/service.ts write three functions. startConversation(): insert a conversation whose id is 8 random bytes as 16 hex characters (randomBytes from node:crypto) and return the id. addMessage(q, conversationId, role, content): insert one message (role "member" or "copilot") with q, the db or the transaction's tx it is given, never with the imported db. history(conversationId, limit = 6): when there is no such conversation, throw new AppError(404, 'conversation_not_found', ...); otherwise return its last limit messages, oldest first, as { role, content } (newest first by messages.id with the limit, then reversed).

    The Check starts two conversations, adds a message inside a transaction it rolls back (nothing may stay), stores nine turns, reads the last 6 and the last 2, a new conversation's empty history, and a conversation that does not exist.

  2. Task 2

    service.ts: rewrite a follow-up as a standalone question

    Write rewriteQuestion(ai, turns, question) in src/conversations/service.ts. When no turn is the member's, return the question as it is, with no model request. Otherwise send one request with ai.chat.completions.create (model config.chatModel, temperature 0): REWRITE_INSTRUCTIONS as the system message, then each turn (a member's as "user", the copilot's as "assistant"), then the question as the last "user" message. Return the reply trimmed, or the question when the reply is empty or null.

    The Check gives your function stand-in models: with no turns, with only the copilot's greeting, with a follow-up about Kala Talkies after a question about The Attic (it reads the request you sent), and with replies of spaces and null. Then it asks the lab model once.

  3. Task 3 · interview question

    Interview: service.ts, answer in a conversation

    Write askInConversation(ai, conversationId, question) in src/conversations/service.ts. Read the history first (a 404 for a conversation that does not exist), rewrite the question with rewriteQuestion, and answer the standalone question with answerQuestion. Store the member's question as they typed it and the copilot's answer with addMessage, both inside one db.transaction. Return the answer with question (as typed), standaloneQuestion and conversationId added.

    The Check needs Tasks 1 and 2. It asks "Can a wheelchair user get into The Attic?" and then "What about Kala Talkies?" in one conversation, reads what the lab model was sent, the stored messages and the transaction each was written in, then a conversation that does not exist, POST /conversations and POST /conversations/:id/ask.

    Nikhil will ask: why rewrite the follow-up before searching, instead of searching with the follow-up as it is, or with the whole conversation pasted together?

What you will learn

  • Memory is your job: a chat model keeps nothing between requests.
  • A conversation in Postgres: an id nobody can guess, a row per message, and who decides the transaction.
  • The last few turns: newest first in SQL, then turned around in TypeScript.
  • Chat roles: how stored turns become user and assistant messages.
  • Rewriting a follow-up: one small model request that makes a standalone question, and why search needs one.

More in Ready for members

Build "Conversations" yourself

Parda answers the same audience questions all day. You build the copilot that answers them from its own help pages, with sources, and says so when it does not know. 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 support copilot project (TypeScript) are free. This one comes with the all labs plan.