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

Long-term facts

By the end of this chapter you can have a model read a message and fill a fixed set of fields with structured output and zod, map those fields to fact keys, keep the facts in SQLite with who said them and when, and read them back into a request as notes. 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

Chapter 2 pinned the messages that state facts, and the request filled up with old values next to new ones. Chapter 3's summaries lost facts or kept stale ones. Rekha does not work that way. After every call with a family she updates one line of her fact sheet: the walima is now on the 25th, not "the walima was on the 24th and then on the 25th". Her sheet for Fatima and Imran's wedding is eleven lines.

Pooja wants the assistant to keep that sheet itself, from the threads, so Rekha stops typing it. And Karan has a reason of his own: the night assistant restarts every time he deploys it, and a history in a JavaScript array or a Map does not survive a restart. Everything you have built so far lives in the program's memory and in the request. Facts that must last a ten-month wedding belong in a database.

What you build: 5 tasks

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

  1. Task 1

    extract.ts: one message, its fields

    Write and export a zod schema Fields in extract.ts with the fields wedding_id, person, diet, allergy, event, date, venue, caterer, decorator, photographer and theme (each a string or null), budget and guests (each an integer or null), tentative (a boolean) and importance (an integer), and export its type with z.infer.

    Then write and export extract(message): message is one thread message, an object with sender, text and at. Ask lab-chat with structured output, client.chat.completions.parse with response_format: zodResponseFormat(Fields, 'fields'), with two messages: a system message holding an instruction to extract the facts the message states, followed by Today is and the message's date (the first 10 characters of its at) and a full stop; and a user message with the sender, a colon, a space and the text. Return (a promise of) the parsed fields. The check calls extract with messages of another wedding, one of them without a year in its date.

  2. Task 2

    facts-of.ts: from fields to facts

    Write and export factsOf(fields) in facts-of.ts: fields is one message's fields (what task 1's extract returns). Return the facts they state as an array of [key, value] pairs, in this order: budget when it is not null; guests when it is not null; the event, then _date, when there is both an event and a date; the event (or wedding when there is none), then _venue, when there is a venue; diet:, then the person, when there is a person and a diet; allergy:, then the person, when there is a person and an allergy; then caterer, decorator, photographer and theme, each when it is there.

    Inside if (import.meta.main), extract every message of WD1063 with task 1's extract, print each message that states some facts as its id, a space and its facts as JSON, and a last line: the number of messages whose facts equal their facts label (as key and value pairs), of, the number of messages, then messages agree with the labels, for example 9 of 10 messages agree with the labels.

  3. Task 3

    store.ts: facts that last

    Write and export a class FactStore in store.ts with node:sqlite. new FactStore(path) opens or creates a SQLite file with a table facts with the columns wedding_id, key, value, said_by, said_at and message_id, one row per wedding and key. put(weddingId, key, value, saidBy, saidAt, messageId) saves a fact, replacing the row of the same wedding and key; the value is stored as JSON text. get(weddingId, key) returns the value (undefined when there is none), and all(weddingId) returns that wedding's facts as an object of key and value.

    The check puts facts of two weddings, puts a new value for a key, opens the file again with a new FactStore, and reads the table itself.

  4. Task 4

    ingest.ts: a wedding's thread into memory

    In ingest.ts, extract every message of WD1063 in order (task 1), turn its fields into facts (task 2), and put each fact into a FactStore on memory.db (task 3), with the message's wedding_id, sender, at and id as weddingId, saidBy, saidAt and messageId. Then print the store's facts for WD1063, one per line, sorted by key, as the key, a colon, a space and the value.

    The check starts from a fresh memory.db and compares the rows with Rekha's fact sheet for WD1063 (facts.json): the values, who said each, when and in which message.

  5. Task 5 · interview question

    Interview: fix-me.ts, a memory that forgets on restart

    FactStore in fix-me.ts saves facts in SQLite, but they are gone every time the program starts again. Run node fix-me.ts twice: the second run should print remembered: 2027-01-25, the walima date the first run saved.

    Fix FactStore so that what put saves is still there for a new FactStore on the same file, in the same program or a new one. Keep the class's name, its methods and the export.

What you will learn

  • Short-term and long-term memory: what is resent, and what is stored.
  • Structured output with zod: a model filling a schema you define.
  • Today's date in the request, so "22 Jan" gets the right year.
  • Fact keys: one canonical name per fact.
  • Facts in SQLite with node:sqlite: an upsert per key, values as JSON, with provenance, saved for good.

More in Memory

Build "Long-term facts" 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.