AI SDK chat, agent, multi-agent
Three Vercel AI SDK apps on one Node server. Every message, tool step and agent turn is a record in a PicoMQ stream. Restart the server and the UI comes back from the streams.
Source: examples/agents/ai-sdk.
| Page | Streams | Records |
|---|---|---|
/chat.html | chat/{id}, one per conversation | AI SDK ModelMessage JSON |
/agent.html | agent/run-{id}, one per run | run_start, step, run_end |
/multi.html | multi/{room}/bus, multi/{room}/agent/{ada,remy,quill} | bus turns, per-agent memory |
All under prefix /examples/agents/ai-sdk. The server uses @picomq/client with protocol pico and a Producer per stream, lingerMs: 10. Each append waits for durable() before the seq is shown in the UI.
Run
bash
cd harness/aio
cp .env.example .env
docker compose up --buildbash
export OPENAI_API_KEY=...
export PICO_ENDPOINT=http://127.0.0.1:4437
cd examples/agents/ai-sdk
npm install
npm run devOpen http://localhost:3456.
Chat persistence
- New chat creates
chat/{id}. Each user and assistant message is appended as one record. - Reply is
streamTextwithgpt-4o-mini, streamed to the browser over SSE, then appended once complete. - Context sent to the model is the last 40 messages,
AI_SDK_MAX_CONTEXT_MESSAGES. - On start the server lists the prefix and opens the newest stream. Recents are built from the first user message of each stream.
- Restart reads the stream from the beginning and replays it into the UI.
Agent audit trail
generateTextwith toolslookupCompanyandcalculate,stopWhen: stepCountIs(10).onStepFinishappends asteprecord withtext,toolCalls,toolResults,finishReason.run_endcarries the final text, step count andtotalTokens. The producer is closed after it.- Continuing a run reuses the stream if it is not closed. Prior
run_startandrun_endrecords rebuild the message history.
Multi-agent persistence
- The host posts a topic to the bus. Every agent memory gets
[Host]: topicas auserrecord with the bus seq. - Advance runs
generateTextfor the next agent over its own memory stream, posts the reply to the bus, appends it to its memory asassistantand to the others asuser. - Each memory record stores
busSeq. On reload the server compares it with the bus and appends anything an agent has not seen. - Delete removes the bus and the three memory streams.
Environment
| Variable | Default |
|---|---|
PICO_ENDPOINT | http://127.0.0.1:4437 |
PORT | 3456 |
OPENAI_API_KEY | required |
AI_SDK_MAX_CONTEXT_MESSAGES | 40 |
PICO_CHAT_PREFIX | /examples/agents/ai-sdk/chat |
PICO_AGENT_PREFIX | /examples/agents/ai-sdk/agent |
PICO_MULTI_PREFIX | /examples/agents/ai-sdk/multi |