Skip to content

Commit 26b0635

Browse files
docs: move serialization snippet to overview.ts for type checking
Address @pgrayy's review nit: extract the inline TypeScript code block from index.mdx into overview.ts with --8<-- markers, matching the existing pattern used by all other code snippets on this page. Also drops 'wire-safe' from the async-iterators.ts comment per earlier review feedback to use 'serialization' language consistently.
1 parent de103df commit 26b0635

3 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/content/docs/user-guide/concepts/streaming/async-iterators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function expressExample() {
4141
})
4242

4343
for await (const event of agent.stream(prompt)) {
44-
// Events automatically serialize to compact, wire-safe JSON via toJSON().
44+
// Events automatically serialize to compact JSON via toJSON().
4545
// Only relevant data fields are included — the full Agent instance,
4646
// Tool classes, and mutable hook flags (cancel/retry) are excluded.
4747
res.write(`${JSON.stringify(event)}\n`)

src/content/docs/user-guide/concepts/streaming/index.mdx

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,29 +228,7 @@ Properties that hold large runtime references — such as `agent`, `orchestrator
228228
The same behavior applies to multi-agent and A2A events. Multi-agent events include identifying context like `nodeId` and `nodeType` alongside their data, while the `orchestrator` and `state` references are filtered out. This means you can serialize any event from `agent.stream()` or a multi-agent orchestrator's stream without additional processing:
229229

230230
```typescript
231-
for await (const event of agent.stream('Hello')) {
232-
// Every event is a class instance. In-process, it carries the full agent reference:
233-
//
234-
// event.type → "modelStreamUpdateEvent"
235-
// event.agent → LocalAgent { messages, model, tools, hooks, ... }
236-
// event.event → { type: "modelContentBlockDeltaEvent",
237-
// delta: { type: "textDelta", text: "Hi" } }
238-
239-
// JSON.stringify() calls the event's toJSON() automatically.
240-
// The agent reference and other runtime objects are stripped out,
241-
// leaving only the type discriminator and relevant data fields:
242-
//
243-
// JSON.stringify(event) →
244-
// {
245-
// "type": "modelStreamUpdateEvent",
246-
// "event": {
247-
// "type": "modelContentBlockDeltaEvent",
248-
// "delta": { "type": "textDelta", "text": "Hi" }
249-
// }
250-
// }
251-
252-
res.write(`data: ${JSON.stringify(event)}\n\n`)
253-
}
231+
--8<-- "user-guide/concepts/streaming/overview.ts:event_serialization"
254232
```
255233
</Tab>
256234
</Tabs>

src/content/docs/user-guide/concepts/streaming/overview.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,34 @@ async function subAgentStreamingExample() {
120120

121121
// --8<-- [end:sub_agent_basic]
122122
}
123+
124+
// Event Serialization Example
125+
async function eventSerializationExample() {
126+
const agent = new Agent()
127+
128+
// --8<-- [start:event_serialization]
129+
for await (const event of agent.stream('Hello')) {
130+
// Every event is a class instance. In-process, it carries the full agent reference:
131+
//
132+
// event.type → "modelStreamUpdateEvent"
133+
// event.agent → LocalAgent { messages, model, tools, hooks, ... }
134+
// event.event → { type: "modelContentBlockDeltaEvent",
135+
// delta: { type: "textDelta", text: "Hi" } }
136+
137+
// JSON.stringify() calls the event's toJSON() automatically.
138+
// The agent reference and other runtime objects are stripped out,
139+
// leaving only the type discriminator and relevant data fields:
140+
//
141+
// JSON.stringify(event) →
142+
// {
143+
// "type": "modelStreamUpdateEvent",
144+
// "event": {
145+
// "type": "modelContentBlockDeltaEvent",
146+
// "delta": { "type": "textDelta", "text": "Hi" }
147+
// }
148+
// }
149+
150+
console.log(`data: ${JSON.stringify(event)}`)
151+
}
152+
// --8<-- [end:event_serialization]
153+
}

0 commit comments

Comments
 (0)