Memory and multi-agent systems in TypeScript · Chapter 8 of 16

Episodic memory

By the end of this chapter you can store events as episodes (when, who, what was decided), find them by time and by meaning together, and answer "what did we decide?" from the right meeting. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Memory
  • 5 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Memory and multi-agent systems in TypeScript are free. This one comes with the all labs plan.

Why you need this

The fact store from chapters 4 to 6 answers "what is the budget?". It cannot answer the questions Rekha gets every week:

  • Sameer: "What did we decide about Meera and Kabir's caterer in June?"
  • Pooja: "Why are we proposing Sawai Haveli? Who decided that, and when?"
  • A cousin: "Didn't we already talk about the mehndi venue?"

These are questions about events: a planning meeting, a day of messages, a decision and the discussion around it. Baraat Bliss has seven planning meetings in /workspace/data/meetings.jsonl, each with a date, who was there, every line said, the decisions and the action items, and months of client sessions in the threads. A fact keeps only the latest value, and chapter 5's vector search finds sentences by meaning but knows nothing about when. An event keeps what happened, and that is a different kind of memory.

What you build: 5 tasks

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

  1. Task 1

    episodes.ts: meetings as episodes

    Write and export the class Episodes in episodes.ts. new Episodes(path) creates, if missing, the SQLite table episodes(id, kind, wedding_id, date, title, attendees, decisions, vector), id the primary key. add(episodes) is async: it takes episode objects (those fields but vector; attendees and decisions are arrays, wedding_id is null for the team), embeds them all with lab-embed in one request, each one's text being its title then its decisions, one per line, and stores each (attendees and decisions as JSON, the vector as a Float32Array), replacing a row with the same id. get(episodeId) returns the episode as an object without the vector, or undefined.

    Inside if (import.meta.main), add every meeting (baraat's meetings) to episodes.db as an episode of kind meeting, then print one line per meeting, like MT09 2026-07-01 WD1000: Its title (2 decisions), with team instead of the wedding for the team's meeting.

  2. Task 2

    sessions.ts: past sessions as episodes

    Write and export sessionEpisode(weddingId, number, messages) in sessions.ts for one session's thread messages. It returns an episode: id the wedding id, -s and the number (WD1000-s4); kind session; wedding_id; date the day of its first message; title the wedding id, session and the number (WD1000 session 4); attendees who wrote, each once, in the order they first wrote; decisions every fact its messages' facts labels state, in order, as key, a colon, a space and value.

    Inside if (import.meta.main), add every session of every wedding (in the order of weddings()) to episodes.db with task 1's Episodes, and print one line per wedding like WD1000: 6 sessions, 21 facts stated.

  3. Task 3

    find.ts: find an episode by time and by meaning

    Write and export an async function find(store, weddingId, query, { start, end, k = 3, kind } = {}) in find.ts, store being task 1's Episodes and every option optional. Keep the episodes of the wedding or of the team (no wedding), whose date is between start and end (ISO days, both included; left out means no limit on that side), and of that kind when kind is given; rank them by cosine similarity to the query embedded with lab-embed, and return the best k, most similar first, each an object with id, kind, date, title and score.

    Inside if (import.meta.main), search WD1042's episodes for What did we decide about the guest count? at any time and in September 2026, and print the results.

  4. Task 4

    recall-meeting.ts: recall what a meeting decided

    Write and export monthRange(month, year) in recall-meeting.ts: the first and last day of a month as ISO days, in an array, the month's name in any case. Write and export an async recall(weddingId, question, month, year): find (task 3) in episodes.db the one meeting of that month most similar to the question, then ask lab-chat at temperature 0 with the system message You are Baraat Bliss's planning assistant. Answer from these notes. then a blank line, Notes: on its own line, and the meeting's notes: one line per decision, a dash, a space, We decided: and the decision with a full stop, then one line per transcript line, the speaker, a colon, a space and the text; the question is the user message. recall resolves to an array of the meeting's id and the reply.

    Inside if (import.meta.main), recall for WD1042 What did we decide about the mehndi in August? (August 2026) and print the meeting's id, then the reply.

  5. Task 5 · interview question

    Interview: fix-me.ts, a month filter that finds the wrong month

    inMonth(episodes, month) in fix-me.ts should keep the episodes whose ISO date falls in a month given as a number from 1 to 12. Run it: August has two meetings, and it prints September's instead.

    Fix inMonth so that it keeps exactly the episodes of that month, in their order. Keep the function's name, its parameters and the export. The check tries other months, including March, October and December.

What you will learn

  • Episodic memory: events stored with their time, people and outcome.
  • Episodes from meetings and from sessions, in one table.
  • Finding by time and by meaning: filter first, then rank.
  • Answering from an episode: the decisions first, then the discussion.
  • Months in JavaScript dates: counted from 0, and what that breaks.

More in Memory

Build "Episodic memory" yourself

Baraat Bliss, a Jaipur wedding planner, must remember months of family decisions across many agents. You build its assistant in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Memory and multi-agent systems in TypeScript are free. This one comes with the all labs plan.