|
| 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 streams – Combine `TEXT_MESSAGE_*` sequences into a single message |
| 59 | + snapshot; concatenate adjacent `TEXT_MESSAGE_CONTENT` for the same message. |
| 60 | +- Tool calls – Collapse tool call start/content/end into a compact record. |
| 61 | +- State – Merge consecutive `STATE_DELTA` events into a single final |
| 62 | + `STATE_SNAPSHOT` and discard superseded updates. |
| 63 | +- Run input normalization – Remove 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 git‑like lineage. The |
| 69 | +stream becomes an immutable append‑only 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 (append‑only) |
| 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 append‑only; 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 | + |
0 commit comments