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
//! The boundary: implement [`Agent`], get an AG-UI endpoint.
//!
//! This crate depends on no LLM client. The .NET SDK can build on
//! `Microsoft.Extensions.AI` because .NET has one blessed chat abstraction;
//! Rust has `async-openai`, `rig-core` and `genai` with no winner, so binding
//! to any of them would make this crate useless to two thirds of the ecosystem.
//! A framework integration is an `impl Agent for …` in its own crate.
use Future;
use Pin;
use Arc;
use crateRunOutcome;
use Serialize;
use DeserializeOwned;
use crateRunContext;
use crateResult;
/// What a run's shared state must be.
///
/// A blanket implementation covers every type that qualifies; you never write
/// `impl AgentState`. Use `()` when the agent keeps no state.
/// An agent that can serve one AG-UI run.
///
/// This is the *hosting* side of the word. The consuming side —
/// a handle onto somebody else's agent — is
/// `client::RemoteAgent`, deliberately spelled differently so that an
/// agent which calls another agent can import both.
///
/// ```
/// use ag_ui::RunOutcome;
/// use ag_ui::server::{Agent, Result, RunContext};
///
/// struct Echo;
///
/// impl Agent for Echo {
/// type State = ();
///
/// async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
/// let mut message = ctx.assistant_message()?;
/// message.delta("you said something")?;
/// message.end()?;
/// Ok(RunOutcome::Success)
/// }
/// }
/// ```
///
/// # Why `async fn` and not `#[async_trait]`
///
/// The trait uses a native `-> impl Future + Send` return (an RPITIT), so
/// implementations are plain `async fn` with no macro, no `Box::pin` per call
/// and no allocation. The cost is that the trait is not `dyn`-compatible; when
/// you need `Box<dyn …>` — a registry of agents behind one endpoint, say —
/// [`DynAgent`] is the boxed adapter, and [`BoxAgent`] implements `Agent`
/// again, so the driver takes it like any other.
///
/// # Why `&mut RunContext` and not `RunContext`
///
/// The driver has to emit `RUN_FINISHED` or `RUN_ERROR` *after* the agent
/// returns, through the same transformer chain and the same ordering verifier
/// the agent used. Handing the context over by value would drop both with the
/// agent's last statement.
/// The `dyn`-compatible form of [`Agent`].
///
/// Implemented for every `Agent`, so `Box::new(my_agent) as BoxAgent<_>` just
/// works. The only difference is one boxed future per run.
/// A type-erased agent over state `S`.
///
/// ```
/// # use ag_ui::RunOutcome;
/// # use ag_ui::server::{Agent, BoxAgent, Result, RunContext};
/// struct Fixed(&'static str);
///
/// impl Agent for Fixed {
/// type State = ();
/// async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
/// ctx.say(self.0)?;
/// Ok(RunOutcome::Success)
/// }
/// }
///
/// let agents: Vec<BoxAgent<()>> = vec![Box::new(Fixed("a")), Box::new(Fixed("b"))];
/// assert_eq!(agents.len(), 2);
/// ```
pub type BoxAgent<S> = ;