Skip to content

Commit d5f0474

Browse files
committed
update docs
1 parent cc21c3b commit d5f0474

File tree

6 files changed

+270
-329
lines changed

6 files changed

+270
-329
lines changed

docs/concepts/events.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ The `RunFinished` event gains new fields to support interrupt-aware workflows.
644644
| `outcome` | Optional: "success" or "interrupt" |
645645
| `interrupt` | Optional: Contains interrupt details when paused |
646646

647-
<span style={{backgroundColor: '#3b82f6', color: 'white', padding: '2px 6px', borderRadius: '4px', fontSize: '0.75em', fontWeight: 'bold'}}>DRAFT</span> [View Proposal](/drafts/serialization)
647+
See [Serialization](/concepts/serialization) for lineage and input capture.
648648

649649
#### RunStarted (Extended)
650650

docs/concepts/serialization.mdx

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
title: "Serialization"
3+
description: "Serialize event streams for history restore, branching, and compaction in AG-UI"
4+
---
5+
6+
# Serialization
7+
8+
Serialization in AG-UI provides a standard way to persist and restore the event
9+
stream that drives an agent–UI session. With a serialized stream you can:
10+
11+
- Restore chat history and UI state after reloads or reconnects
12+
- Attach to running agents and continue receiving events
13+
- Create branches (time travel) from any prior run
14+
- Compact stored history to reduce size without losing meaning
15+
16+
This page explains the model, the updated event fields, and practical usage
17+
patterns with examples.
18+
19+
## Core Concepts
20+
21+
- Stream serialization – Convert the full event history to and from a portable
22+
representation (e.g., JSON) for storage in databases, files, or logs.
23+
- Event compaction – Reduce verbose streams to snapshots while preserving
24+
semantics (e.g., merge content chunks, collapse deltas into snapshots).
25+
- Run lineage – Track branches of conversation using a `parentRunId`, forming
26+
a git‑like append‑only log that enables time travel and alternative paths.
27+
28+
## Updated Event Fields
29+
30+
The `RunStarted` event includes additional optional fields:
31+
32+
```ts
33+
type RunStartedEvent = BaseEvent & {
34+
type: EventType.RUN_STARTED
35+
threadId: string
36+
runId: string
37+
/** Parent for branching/time travel within the same thread */
38+
parentRunId?: string
39+
/** Exact agent input for this run (may omit messages already in history) */
40+
input?: AgentInput
41+
}
42+
```
43+
44+
These fields enable lineage tracking and let implementations record precisely
45+
what was passed to the agent, independent of previously recorded messages.
46+
47+
## Event Compaction
48+
49+
Compaction reduces noise in an event stream while keeping the same observable
50+
outcome. A typical implementation provides a utility:
51+
52+
```ts
53+
declare function compactEvents(events: BaseEvent[]): BaseEvent[]
54+
```
55+
56+
Common compaction rules include:
57+
58+
- Message streamsCombine `TEXT_MESSAGE_*` sequences into a single message
59+
snapshot; concatenate adjacent `TEXT_MESSAGE_CONTENT` for the same message.
60+
- Tool callsCollapse tool call start/content/end into a compact record.
61+
- StateMerge consecutive `STATE_DELTA` events into a single final
62+
`STATE_SNAPSHOT` and discard superseded updates.
63+
- Run input normalizationRemove from `RunStarted.input.messages` any
64+
messages already present earlier in the stream.
65+
66+
## Branching and Time Travel
67+
68+
Setting `parentRunId` on a `RunStarted` event creates a gitlike lineage. The
69+
stream becomes an immutable appendonly log where each run can branch from any
70+
previous run.
71+
72+
```mermaid
73+
gitGraph
74+
commit id: "run1"
75+
commit id: "run2"
76+
branch alternative
77+
checkout alternative
78+
commit id: "run3 (parent run2)"
79+
commit id: "run4"
80+
checkout main
81+
commit id: "run5 (parent run2)"
82+
commit id: "run6"
83+
```
84+
85+
Benefits:
86+
87+
- Multiple branches in the same serialized log
88+
- Immutable history (appendonly)
89+
- Deterministic time travel to any point
90+
91+
## Examples
92+
93+
### Basic Serialization
94+
95+
```ts
96+
// Serialize event stream
97+
const events: BaseEvent[] = [...];
98+
const serialized = JSON.stringify(events);
99+
100+
await storage.save(threadId, serialized);
101+
102+
// Restore and compact later
103+
const restored = JSON.parse(await storage.load(threadId));
104+
const compacted = compactEvents(restored);
105+
```
106+
107+
### Event Compaction
108+
109+
Before:
110+
111+
```ts
112+
[
113+
{ type: "TEXT_MESSAGE_START", messageId: "msg1", role: "user" },
114+
{ type: "TEXT_MESSAGE_CONTENT", messageId: "msg1", delta: "Hello " },
115+
{ type: "TEXT_MESSAGE_CONTENT", messageId: "msg1", delta: "world" },
116+
{ type: "TEXT_MESSAGE_END", messageId: "msg1" },
117+
{ type: "STATE_DELTA", patch: { op: "add", path: "/foo", value: 1 } },
118+
{ type: "STATE_DELTA", patch: { op: "replace", path: "/foo", value: 2 } },
119+
]
120+
```
121+
122+
After:
123+
124+
```ts
125+
[
126+
{
127+
type: "MESSAGES_SNAPSHOT",
128+
messages: [{ id: "msg1", role: "user", content: "Hello world" }],
129+
},
130+
{
131+
type: "STATE_SNAPSHOT",
132+
state: { foo: 2 },
133+
},
134+
]
135+
```
136+
137+
### Branching With `parentRunId`
138+
139+
```ts
140+
// Original run
141+
{
142+
type: "RUN_STARTED",
143+
threadId: "thread1",
144+
runId: "run1",
145+
input: { messages: ["Tell me about Paris"] },
146+
}
147+
148+
// Branch from run1
149+
{
150+
type: "RUN_STARTED",
151+
threadId: "thread1",
152+
runId: "run2",
153+
parentRunId: "run1",
154+
input: { messages: ["Actually, tell me about London instead"] },
155+
}
156+
```
157+
158+
### Normalized Input
159+
160+
```ts
161+
// First run includes full message
162+
{
163+
type: "RUN_STARTED",
164+
runId: "run1",
165+
input: { messages: [{ id: "msg1", role: "user", content: "Hello" }] },
166+
}
167+
168+
// Second run omits already‑present message
169+
{
170+
type: "RUN_STARTED",
171+
runId: "run2",
172+
input: { messages: [{ id: "msg2", role: "user", content: "How are you?" }] },
173+
// msg1 omitted; it already exists in history
174+
}
175+
```
176+
177+
## Implementation Notes
178+
179+
- Provide SDK helpers for compaction and (de)serialization.
180+
- Store streams appendonly; prefer incremental writes when possible.
181+
- Consider compression when persisting long histories.
182+
- Add indexes by `threadId`, `runId`, and timestamps for fast retrieval.
183+
184+
## See Also
185+
186+
- Concepts: [Events](/concepts/events), [State Management](/concepts/state)
187+
- SDKs: TypeScript encoder and core event types
188+

docs/docs.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"concepts/middleware",
4444
"concepts/messages",
4545
"concepts/state",
46+
"concepts/serialization",
4647
"concepts/tools"
4748
]
4849
},
@@ -52,7 +53,6 @@
5253
"drafts/overview",
5354
"drafts/activity-events",
5455
"drafts/reasoning",
55-
"drafts/serialization",
5656
"drafts/multimodal-messages",
5757
"drafts/interrupts",
5858
"drafts/generative-ui",
@@ -87,7 +87,8 @@
8787
"sdk/js/client/abstract-agent",
8888
"sdk/js/client/http-agent",
8989
"sdk/js/client/middleware",
90-
"sdk/js/client/subscriber"
90+
"sdk/js/client/subscriber",
91+
"sdk/js/client/compaction"
9192
]
9293
},
9394
"sdk/js/encoder",

docs/drafts/overview.mdx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@ This section contains draft changes being considered for the AG-UI protocol. The
2828
>
2929
Support for LLM reasoning visibility and continuity with encrypted content
3030
</Card>
31-
<Card
32-
title="Serialization"
33-
href="/drafts/serialization"
34-
icon="file-lines"
35-
color="#3B82F6"
36-
iconType="light"
37-
>
38-
Stream serialization for chat history restoration and event compaction
39-
</Card>
31+
4032
<Card
4133
title="Multi-modal Messages"
4234
href="/drafts/multimodal-messages"
@@ -81,4 +73,4 @@ This section contains draft changes being considered for the AG-UI protocol. The
8173
- **Under Review** - Active development and testing
8274
- **Accepted** - Approved for implementation
8375
- **Implemented** - Merged into the main protocol specification
84-
- **Withdrawn** - Proposal has been withdrawn or superseded
76+
- **Withdrawn** - Proposal has been withdrawn or superseded

0 commit comments

Comments
 (0)