Skip to content

Commit a69b8ff

Browse files
committed
Improved run_agent API: added agent_id support, enhanced documentation, and updated subscriber handling.
1 parent 8190578 commit a69b8ff

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

sdks/community/rust/crates/ag-ui-client/examples/basic_agent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
1111
// Create run parameters
1212
let params = RunAgentParams::new().add_message(message);
1313

14-
// Run the agent with the subscriber
14+
// Run the agent without subscriber
1515
let result = agent.run_agent(&params, ()).await?;
1616

1717
println!("{:#?}", result);

sdks/community/rust/crates/ag-ui-client/src/agent.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,49 @@ where
146146
input: &RunAgentInput<StateT, FwdPropsT>,
147147
) -> Result<EventStream<'async_trait, StateT>, AgentError>;
148148

149-
// TODO: Expand documentation
150-
/// The main execution method, containing the full pipeline logic.
149+
/// Triggers an Agent run.
150+
///
151+
/// # Parameters
152+
/// * `params`: The run parameters as given in [RunAgentParams]
153+
/// * `subscribers`: A (sequence of) type(s) that implement [crate::subscriber::AgentSubscriber];
154+
/// can also be a unit type `()` or `None` if none are needed. Valid types are `T`, `(T,)`,
155+
/// `Vec<T>`, `&[T]`, `()`, `Option<()>` where `T: AgentSubscriber`.
156+
///
157+
/// # Examples
158+
/// ```no_run
159+
/// # use ag_ui_client::{Agent, HttpAgent, RunAgentParams, core::types::Message};
160+
/// # use std::error::Error;
161+
///
162+
/// # #[tokio::main]
163+
/// # async fn main() -> Result<(), Box<dyn Error>> {
164+
/// let agent = HttpAgent::builder()
165+
/// .with_url_str("http://127.0.0.1:3000/")?
166+
/// .build()?;
167+
///
168+
/// let message = Message::new_user("Can you give me the current temperature in New York?");
169+
/// // Create run parameters
170+
/// let params = RunAgentParams::new().add_message(message);
171+
///
172+
/// // Run the agent without subscriber
173+
/// let result = agent.run_agent(&params, ()).await?;
174+
/// # Ok(())
175+
/// # }
176+
/// ```
177+
///
178+
/// # Notes
179+
/// Currently the subscriber pattern is the only way to subscriber to an Agent run's lifecycle.
151180
async fn run_agent(
152181
&self,
153182
params: &RunAgentParams<StateT, FwdPropsT>,
154183
subscribers: impl IntoSubscribers<StateT, FwdPropsT>,
155184
) -> Result<RunAgentResult<StateT>, AgentError> {
156-
// TODO: Use Agent ID?
157-
let _agent_id = AgentId::random();
158-
159185
let input = RunAgentInput {
160186
thread_id: ThreadId::random(),
161187
run_id: params.run_id.clone().unwrap_or_else(RunId::random),
162188
state: params.state.clone(),
163189
messages: params.messages.clone(),
164190
tools: params.tools.clone(),
165191
context: params.context.clone(),
166-
// TODO: Find suitable default value
167192
forwarded_props: params.forwarded_props.clone(),
168193
};
169194
let current_message_ids: HashSet<&MessageId> =
@@ -210,4 +235,8 @@ where
210235
new_state: event_handler.state,
211236
})
212237
}
238+
239+
fn agent_id(&self) -> Option<&AgentId> {
240+
None
241+
}
213242
}

sdks/community/rust/crates/ag-ui-client/src/http.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
use async_trait::async_trait;
2-
use futures::StreamExt;
3-
use log::{debug, trace};
4-
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
5-
use reqwest::{Client as HttpClient, Url};
6-
use std::str::FromStr;
7-
81
use crate::Agent;
92
use crate::agent::AgentError;
103
use crate::core::event::Event;
114
use crate::core::types::RunAgentInput;
125
use crate::core::{AgentState, FwdProps};
136
use crate::sse::SseResponseExt;
147
use crate::stream::EventStream;
8+
use ag_ui_core::types::AgentId;
9+
use async_trait::async_trait;
10+
use futures::StreamExt;
11+
use log::{debug, trace};
12+
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
13+
use reqwest::{Client as HttpClient, Url};
14+
use std::str::FromStr;
1515

1616
pub struct HttpAgent {
1717
http_client: HttpClient,
1818
base_url: Url,
1919
header_map: HeaderMap,
20+
agent_id: Option<AgentId>,
2021
}
2122

2223
impl HttpAgent {
@@ -29,6 +30,7 @@ impl HttpAgent {
2930
http_client,
3031
base_url,
3132
header_map,
33+
agent_id: None,
3234
}
3335
}
3436

@@ -41,14 +43,13 @@ pub struct HttpAgentBuilder {
4143
base_url: Option<Url>,
4244
header_map: HeaderMap,
4345
http_client: Option<HttpClient>,
46+
agent_id: Option<AgentId>,
4447
}
4548

4649
impl HttpAgentBuilder {
4750
pub fn new() -> Self {
4851
Self {
49-
base_url: None,
50-
header_map: HeaderMap::new(),
51-
http_client: None,
52+
..Default::default()
5253
}
5354
}
5455

@@ -113,6 +114,12 @@ impl HttpAgentBuilder {
113114
self
114115
}
115116

117+
/// Set Agent ID
118+
pub fn with_agent_id(mut self, agent_id: AgentId) -> Self {
119+
self.agent_id = Some(agent_id);
120+
self
121+
}
122+
116123
pub fn build(self) -> Result<HttpAgent, AgentError> {
117124
let base_url = self.base_url.ok_or(AgentError::Config {
118125
message: "Base URL is required".to_string(),
@@ -131,6 +138,7 @@ impl HttpAgentBuilder {
131138
http_client,
132139
base_url,
133140
header_map: self.header_map,
141+
agent_id: self.agent_id,
134142
})
135143
}
136144
}
@@ -185,4 +193,8 @@ impl<StateT: AgentState, FwdPropsT: FwdProps> Agent<StateT, FwdPropsT> for HttpA
185193
.boxed();
186194
Ok(stream)
187195
}
196+
197+
fn agent_id(&self) -> Option<&AgentId> {
198+
self.agent_id.as_ref()
199+
}
188200
}

sdks/community/rust/crates/ag-ui-client/src/subscriber.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct AgentSubscriberParams<'a, StateT: AgentState, FwdPropsT: FwdProps> {
1515
pub input: &'a RunAgentInput<StateT, FwdPropsT>,
1616
}
1717

18-
/// Subscriber trait for handling agent events
18+
/// Subscriber trait for hooking into Agent run lifecycle events.
1919
#[async_trait::async_trait]
2020
pub trait AgentSubscriber<StateT = JsonValue, FwdPropsT = JsonValue>: Send + Sync
2121
where
@@ -376,6 +376,17 @@ where
376376
}
377377
}
378378

379+
// Implementation for no subscriber
380+
impl<StateT, FwdPropsT> IntoSubscribers<StateT, FwdPropsT> for Option<()>
381+
where
382+
StateT: AgentState,
383+
FwdPropsT: FwdProps,
384+
{
385+
fn into_subscribers(self) -> Subscribers<StateT, FwdPropsT> {
386+
Subscribers::new(vec![])
387+
}
388+
}
389+
379390
// Implementation for single subscribers, as a unit-sized tuple
380391
impl<StateT, FwdPropsT, T> IntoSubscribers<StateT, FwdPropsT> for (T,)
381392
where

0 commit comments

Comments
 (0)