|
| 1 | +--- |
| 2 | +title: "Agent" |
| 3 | +description: "Agent trait with core event handling" |
| 4 | +--- |
| 5 | + |
| 6 | +# Agent trait |
| 7 | + |
| 8 | +The Agent trait defines the contract for connecting any execution backend to AG‑UI. Concrete implementations (like HttpAgent) stream core events that are handled by subscribers to maintain messages and state. |
| 9 | + |
| 10 | +```rust |
| 11 | +use ag_ui_client::Agent; |
| 12 | +``` |
| 13 | + |
| 14 | +## Generics |
| 15 | + |
| 16 | +- StateT: the agent state type. Must implement ag_ui_client::core::AgentState (Serialize + Deserialize + Clone + Debug + Send + Sync). |
| 17 | +- FwdPropsT: forwarded properties type passed through to downstream systems. Must implement ag_ui_client::core::FwdProps. |
| 18 | + |
| 19 | +Defaults for both are serde_json::Value, which works well for quick starts. |
| 20 | + |
| 21 | +## Running |
| 22 | + |
| 23 | +The core trait method you implement is run, which returns an asynchronous stream of core events: |
| 24 | + |
| 25 | +```rust |
| 26 | +#[async_trait::async_trait] |
| 27 | +pub trait Agent<StateT = JsonValue, FwdPropsT = JsonValue> |
| 28 | +where |
| 29 | + StateT: AgentState, |
| 30 | + FwdPropsT: FwdProps, |
| 31 | +{ |
| 32 | + async fn run( |
| 33 | + &self, |
| 34 | + input: &RunAgentInput<StateT, FwdPropsT>, |
| 35 | + ) -> Result<EventStream<'async_trait, StateT>, AgentError>; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +For most consumers, use the provided convenience wrapper run_agent which: |
| 40 | + |
| 41 | +- constructs an internal RunAgentInput from RunAgentParams |
| 42 | +- initializes an event handler with your subscribers |
| 43 | +- consumes the event stream and applies mutations |
| 44 | +- returns RunAgentResult with final result, new messages and new state |
| 45 | + |
| 46 | +```rust |
| 47 | +use ag_ui_client::{Agent}; |
| 48 | +use ag_ui_client::agent::{RunAgentParams, RunAgentResult}; |
| 49 | + |
| 50 | +let params = RunAgentParams::new().user("Hello!"); |
| 51 | +let result: RunAgentResult<_> = my_agent.run_agent(¶ms, ()).await?; |
| 52 | +``` |
| 53 | + |
| 54 | +## RunAgentParams |
| 55 | + |
| 56 | +A builder for run inputs. Supports typed state and forwarded props via new_typed. |
| 57 | + |
| 58 | +```rust |
| 59 | +use ag_ui_client::agent::RunAgentParams; |
| 60 | +use ag_ui_client::core::types::{Message, Tool, Context}; |
| 61 | + |
| 62 | +// JSON state (default) |
| 63 | +let params = RunAgentParams::new() |
| 64 | + .user("User message") |
| 65 | + .add_tool(Tool::new("search".into(), "Search".into(), serde_json::json!({"type":"object"}))) |
| 66 | + .add_context(Context::new("trace_id".into(), "123".into())); |
| 67 | + |
| 68 | +// Strongly-typed state/forwarded props |
| 69 | +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default)] |
| 70 | +struct MyState { count: u32 } |
| 71 | +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default)] |
| 72 | +struct MyProps { tenant: String } |
| 73 | + |
| 74 | +let params_typed = RunAgentParams::<MyState, MyProps>::new_typed() |
| 75 | + .with_state(MyState { count: 1 }) |
| 76 | + .with_forwarded_props(MyProps { tenant: "acme".into() }) |
| 77 | + .user("Go!"); |
| 78 | +``` |
| 79 | + |
| 80 | +Builder helpers include: |
| 81 | + |
| 82 | +- with_run_id(run_id) |
| 83 | +- add_tool(tool) |
| 84 | +- add_context(ctx) |
| 85 | +- with_forwarded_props(props) |
| 86 | +- with_state(state) |
| 87 | +- add_message(message) |
| 88 | +- user(content: impl Into<String>) |
| 89 | + |
| 90 | +## RunAgentResult |
| 91 | + |
| 92 | +Returned by run_agent: |
| 93 | + |
| 94 | +```rust |
| 95 | +pub struct RunAgentResult<StateT: AgentState> { |
| 96 | + pub result: serde_json::Value, |
| 97 | + pub new_messages: Vec<Message>, |
| 98 | + pub new_state: StateT, |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## AgentStateMutation |
| 103 | + |
| 104 | +Subscriber methods may return AgentStateMutation to update messages and/or state and optionally stop propagation to later subscribers. |
| 105 | + |
| 106 | +```rust |
| 107 | +pub struct AgentStateMutation<StateT = JsonValue> { |
| 108 | + pub messages: Option<Vec<Message>>, |
| 109 | + pub state: Option<StateT>, |
| 110 | + pub stop_propagation: bool, |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Errors |
| 115 | + |
| 116 | +Most Agent operations return Result<_, AgentError> where AgentError = ag_ui_client::error::AgUiClientError. See the client error page for details. Common categories include configuration errors, HTTP transport/status errors (for HttpAgent), JSON errors, and subscriber errors. |
0 commit comments