跳到正文
DEV Community· Pallavi Singla·· 2 小時前AI 評分56

Delivery Desk 客戶智能體如何保留跨日記憶

Agent memory that outlives the day: the customer agent in Delivery Desk

AI 導讀

Delivery Desk 示範中的客戶智能體把每位客戶的延誤及未能派送事件存入獨立記憶流,令新配送日重設當日資料時仍保留客戶歷史。回憶會按 stop id 限定客戶;同一事件以 stop、事件類型、clock 及事件 ID 生成相同資料,並以 dedup=True 避免步驟重送時重複記錄,歷史亦會改變 SMS 內容。

正文

Part of a short series on the features in the Delivery Desk demo, a parcel delivery desk run by agents on one durable log, built on Laser SDK. The first post, Killing agents in a delivery desk demo, covers crash and resume. This one covers agent memory.

The problem

The desk runs one delivery day at a time. New day deletes the day's stream and starts the rounds again. That is right for vans, drops and exceptions, but wrong for customers. A customer who was let down yesterday should not be treated as a stranger today.

So the customer agent keeps one thing that a new day does not reset: what happened to each customer before.

Where the memory lives

The customer agent remembers every delay and every missed drop on a memory topic, in a stream of its own (delivery-desk-memory by default). New day drops the day's stream, not this one. Laser Stack folds the memory topic into a read view, and recall reads that view.

Each fact is scoped to the customer by their stop id:

async def open(self):
    if self.handle is None:
        await self.laser.stream(C.MEMORY_STREAM).ensure()
        self.handle = await self.laser.memory_topic(C.MEMORY_TOPIC, stream=C.MEMORY_STREAM)
    return self.handle

async def recall(self, stop: str, limit: int = 10) -> list[dict]:
    memory = await self.open()
    items = await memory.recall(user=stop, limit=limit, stream=C.MEMORY_STREAM)
    ...

Remembering once, even after a redelivery

Agents in the desk can be killed at any point, and the log redelivers the step they had not finished. A notify step that runs twice must not remember the same incident twice.

Two things make that hold. The same incident always produces the same fact, because the fact is built only from the stop, the kind of incident, the clock and the incident id. And remember is called with dedup=True:

async def remember(self, value: dict) -> None:
    memory = await self.open()
    # dedup: a redelivered notify step remembers the same incident once.
    await memory.remember(json.dumps(value, sort_keys=True), user=value["stop"],
                          stream=C.MEMORY_STREAM, dedup=True)

Using what it remembers

When a road closure, an unreachable driver or a missed drop affects a customer, the customer agent recalls that customer's history before it writes the SMS. The history changes the message:

  • A customer who was not home before, and is not home again, is told the driver will leave the parcel somewhere safe.
  • A customer hit for the second time gets an apology that says so.
  • A customer hit more often than that is told how many times it has happened and that their address has been flagged.

The facts carry a time, taken from the incident's ULID, so the history is read in the order things happened, on any later day.

Forgetting

The desk's side panel shows Customer memory: the newest facts across all customers. Forget all removes them the way an erasure request would. Memory is a log, so forgetting does not delete records in place. It appends a tombstone for each fact, and recall stops returning it:

async def forget_all(self) -> int:
    memory = await self.open()
    gone = 0
    for item in await memory.recall(limit=1000, stream=C.MEMORY_STREAM):
        value = decode(item)
        await memory.forget(item.id, user=(value or {}).get("stop"), stream=C.MEMORY_STREAM)
        gone += 1
    return gone

Limits

Recall reads the read view that Laser Stack folds from the memory topic, so a new fact can take a moment to show up.

Why this matters beyond deliveries

Any agent that talks to the same people more than once needs this: support, incidents, finance. The pattern is small. Keep memory on its own stream so resets do not touch it, build each fact deterministically so redeliveries deduplicate, scope recall to one customer, and forget with tombstones.

Try it

You need Python 3.11+ and Docker.

git clone https://github.com/laserdata/laser-delivery-desk
cd laser-delivery-desk
docker compose up -d
python3 -m venv .venv && . .venv/bin/activate
pip install -e .
python -m fleet.server

The code is at laserdata/laser-delivery-desk, Laser SDK is at laserdata/laser-sdk, and the docs are at docs.laserdata.com/laser-sdk.

來源:DEV Community · dev.to