1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! Metadata: extra information attached to events, messages, tool calls and
//! resume entries.
//!
//! Token usage, a trace id, a finish reason — anything an application needs to
//! carry alongside the conversation goes in `metadata`, an object that is open
//! by key. Before it existed producers hung undeclared properties off events
//! and hoped consumers passed them through; metadata is the declared, typed
//! replacement, and consumers are required to carry it.
//!
//! # Where it lives
//!
//! Four places, all optional:
//!
//! - every event, declared once on [`BaseEvent`](crate::event::BaseEvent) so
//! all 36 types have it;
//! - every message, all seven roles;
//! - every [`ToolCall`](crate::tool::ToolCall) — a tool call is not a message,
//! and several calls can share one parent, so each carries its own;
//! - every [`ResumeEntry`](crate::outcome::ResumeEntry), for envelope data
//! about an answer — signatures, routing keys — as opposed to the answer.
//!
//! # Shape
//!
//! Any JSON value is allowed under a key, `null` included. The object itself
//! is **absent or an object, never `null`**: an optional field with no value
//! is omitted from the JSON entirely, in every official SDK, and this crate
//! rejects `"metadata": null` at parse time rather than reading it as absent.
//! Unlike some older optional fields, metadata has no legacy producers to
//! tolerate — the crate-private `serde_util::reject_null` is the rule. An
//! empty object is valid and means the same as omitting it.
//!
//! The [`AGUI_METADATA_KEY`] (`"ag-ui"`) is reserved for the protocol's own
//! use. Every other key is yours. Nothing rejects a write to it at runtime —
//! that would contradict open-by-key — but treat it as off limits.
//!
//! # Merging into messages
//!
//! A message is assembled from a sequence of events, and the interesting
//! values are only known at the end: a provider does not know its token usage
//! until it has finished generating. So a consumer merges each event's
//! metadata into the message that event builds, as the sequence arrives, with
//! [`merge_metadata`]: last write wins, key by key, and a nested object or
//! array is replaced whole rather than blended. The client's applier does
//! this for the text, tool-call, activity and reasoning-message families; an
//! event that builds no message — `RUN_*`, `STEP_*`, `STATE_*`, `RAW`,
//! `CUSTOM`, `REASONING_START`/`END`/`ENCRYPTED_VALUE`, `MESSAGES_SNAPSHOT`,
//! `SUBAGENT_*` — keeps its metadata to itself.
//!
//! ```
//! use ag_ui::{JsonObject, merge_metadata};
//! use serde_json::json;
//!
//! let start: JsonObject = json!({ "source": "openai", "stage": "start" })
//! .as_object().unwrap().clone();
//! let end: JsonObject = json!({ "stage": "end", "usage": { "output": 340 } })
//! .as_object().unwrap().clone();
//!
//! let merged = merge_metadata(Some(&start), Some(&end)).unwrap();
//! assert_eq!(merged["source"], "openai"); // nothing later set it
//! assert_eq!(merged["stage"], "end"); // last write wins
//! assert_eq!(merged["usage"]["output"], 340); // arrived only at the end
//! ```
use crateJsonObject;
/// The key reserved for the protocol's own use inside a metadata object.
///
/// Reserved by convention: metadata is open by key, so nothing rejects a
/// write to it. AG-UI may put its own values under it in future versions,
/// which is why an application should not.
pub const AGUI_METADATA_KEY: &str = "ag-ui";
/// Folds `incoming` into `existing`, key by key, with the last write winning.
///
/// Returns a new object rather than mutating either argument. An absent
/// `incoming` returns `existing` unchanged (cloned); an empty `incoming`
/// changes nothing. A key's value is replaced outright — this never recurses,
/// so an object or array under any key, [`AGUI_METADATA_KEY`] included, is
/// replaced wholesale rather than blended with what was there before. To add
/// to a nested structure, send the complete new value.
/// The in-place form of [`merge_metadata`], for a consumer folding a stream
/// of events into one message without cloning the accumulated object on every
/// delta.