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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Consume a remote [AG-UI] agent: turn its event stream into messages and
//! state.
//!
//! An AG-UI run arrives as deltas — a message opens, text arrives a fragment at
//! a time, tool arguments accumulate as partial JSON, state moves by RFC 6902
//! patch, and the run may pause to ask a human something. This crate is the
//! consumer half of that protocol: the state machines that fold a stream back
//! into a conversation, the wire-format decoder that feeds them, and two levels
//! of API over the top.
//!
//! ```
//! use ag_ui::client::{RunEnd, Session, Update, transport::ReplayTransport};
//! use ag_ui::{Event, PatchOperation, TextMessageRole};
//! use futures_util::StreamExt;
//! use serde::Deserialize;
//!
//! /// The agent's state, in your own type.
//! #[derive(Clone, Debug, Deserialize, PartialEq)]
//! struct Weather {
//! checked: bool,
//! }
//!
//! # let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
//! # rt.block_on(async {
//! // A transport that replays a scripted run, so this example needs no network.
//! let transport = ReplayTransport::new([
//! Event::run_started("thread-1", "run-1"),
//! Event::text_message_start("msg-1", TextMessageRole::Assistant),
//! Event::text_message_content("msg-1", "It is "),
//! Event::text_message_content("msg-1", "sunny."),
//! Event::text_message_end("msg-1"),
//! Event::state_delta(vec![PatchOperation::add("/checked", true)]),
//! Event::run_finished_success("thread-1", "run-1"),
//! ]);
//!
//! let mut session = Session::new(transport, "thread-1");
//! let mut weather = None;
//! let mut ended = None;
//!
//! let mut run = session.send("what is the weather?");
//! while let Some(update) = run.next().await {
//! match update {
//! Update::Message(message) => println!("{:?}", message.change),
//! // The state arrives already typed — and this is where the type
//! // comes from, so `Session` needs no turbofish.
//! Update::State(state) => weather = Some(state),
//! Update::Error(error) => eprintln!("{error}"),
//! Update::Done(end) => ended = Some(end),
//! _ => {}
//! }
//! }
//! drop(run);
//!
//! assert!(matches!(ended, Some(RunEnd::Success { .. })));
//! assert_eq!(session.messages().len(), 2);
//! assert_eq!(weather, Some(Weather { checked: true }));
//! // The raw JSON is always there too, whether or not it fits the type.
//! assert_eq!(session.raw_state()["checked"], true);
//! # });
//! ```
//!
//! # Two levels
//!
//! [`RemoteAgent`] is the low level:
//! [`agent.run(params)`](RemoteAgent::run) gives you the events exactly as the
//! agent sent them, unassembled. That is what a proxy, a recorder or a bridge
//! to another protocol wants.
//!
//! It is called `RemoteAgent` and not `Agent` because the other half of this
//! SDK already owns that word from the other side:
//! [`crate::server::Agent`] is the trait you implement to *be* an agent, and an
//! agent that calls another agent imports both.
//!
//! [`crate::server::Agent`]: https://kimsoungryoul.github.io/ag-ui-rust/api/ag_ui/server/agent/trait.Agent.html
//!
//! [`Session`] is the high level: a thread, its accumulated messages, and typed
//! state. [`session.send(text)`](Session::send) yields [`Update`]s — "this
//! message grew", "the state changed", "the agent is waiting on you" — with
//! chunk normalization, protocol verification and delta application already
//! done.
//!
//! # Tools are yours to offer
//!
//! AG-UI has no tool discovery and no negotiation. The tool set travels on
//! every request, from the client, and an agent cannot ask for one it was not
//! sent — so offering none to an agent that needs one does not produce a
//! missing-tool error from this crate. It produces the *agent's* own error
//! ("the client offered no add_task tool", or whatever that agent says),
//! arriving as an ordinary failed run, which reads like a bug in the agent and
//! is not one. A client written against no particular agent therefore has to be
//! configured with a tool set the way it is configured with a URL:
//! [`SessionBuilder::tools`], or [`Session::set_tools`] from the next run on.
//!
//! # The pieces underneath
//!
//! - [`apply`] — the event applier. Deltas in, materialised messages and state
//! out, plus a report of what changed so a view can redraw one row.
//! - [`chunks`] — normalizes `*_CHUNK` events into explicit start/content/end
//! triples. Chunks carry their id only on the first one, so this stage
//! remembers.
//! - [`verify`] — the ordering rules, checked client-side as the TypeScript SDK
//! does. A malformed stream produces one clear error instead of a confused UI.
//! - [`interrupts`] — the human-in-the-loop round trip.
//! - [`transport`] — where events come from: [`Transport`], an SSE decoder, a
//! `reqwest` client, and a replay transport for tests.
//!
//! # Executor-agnostic, transport-agnostic
//!
//! Only [`transport`] is async. Everything else — application, normalization,
//! verification — is a plain synchronous state machine you can drive from a
//! loop, a test, or an event handler.
//!
//! The one async layer is a trait, so a wasm frontend or a non-tokio runtime
//! substitutes its own. `cargo check --no-default-features` is a CI job
//! precisely to keep that true: it must not pull in `reqwest` or tokio.
//!
//! # Features
//!
// Gated because a feature list names items the current build may not have, and
// a link to one of those is a rustdoc error rather than a dead link. See
// `doc-features` in CI.
//!
//! [AG-UI]: https://docs.ag-ui.com
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;