#![cfg(all(feature = "axum", feature = "client"))]
use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use ag_ui::axum::{AgentEndpoint, RouterExt};
use ag_ui::server::{
Agent, CancellationToken, Error as AgentError, FilterToolCalls, Result, RunContext,
};
use ag_ui::{Event, EventType, Interrupt, RunAgentInput, RunOutcome};
use axum::Router;
use axum::routing::get;
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc::{UnboundedSender, unbounded_channel};
use tokio::time::{sleep, timeout};
struct Chatty;
impl Agent for Chatty {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
let mut step = ctx.step("answer")?;
let mut message = step.assistant_message()?;
message.delta("Hello, ")?;
message.delta("world.")?;
message.end()?;
let mut call = step.tool_call("internal_debug")?;
call.args("{}")?;
call.end()?;
drop(step);
Ok(RunOutcome::Success)
}
}
struct Broken;
impl Agent for Broken {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
ctx.say("Looking that up…")?;
Err(AgentError::agent("the weather service is down"))
}
}
struct Cautious;
impl Agent for Cautious {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
ctx.say("This will delete everything. Are you sure?")?;
Ok(RunOutcome::interrupt(vec![Interrupt::new(
"delete-everything",
"tool_approval",
)]))
}
}
struct Endless {
token: UnboundedSender<CancellationToken>,
emitted: Arc<AtomicUsize>,
}
impl Agent for Endless {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
let _ = self.token.send(ctx.cancellation_token());
let mut message = ctx.assistant_message()?;
for index in 0..10_000 {
message.delta(format!("token-{index} "))?;
self.emitted.fetch_add(1, Ordering::SeqCst);
sleep(Duration::from_millis(5)).await;
}
message.end()?;
Ok(RunOutcome::Success)
}
}
struct Prompt {
token: UnboundedSender<CancellationToken>,
}
impl Agent for Prompt {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
let _ = self.token.send(ctx.cancellation_token());
ctx.say("done")?;
Ok(RunOutcome::Success)
}
}
struct Slow {
quiet: Duration,
}
impl Agent for Slow {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
sleep(self.quiet).await;
ctx.say("finally")?;
Ok(RunOutcome::Success)
}
}
struct Waiting {
token: UnboundedSender<CancellationToken>,
}
impl Agent for Waiting {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
let _ = self.token.send(ctx.cancellation_token());
ctx.say("thinking…")?;
sleep(Duration::from_secs(60)).await;
Ok(RunOutcome::Success)
}
}
struct Verbose;
impl Agent for Verbose {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
ctx.emit(Event::state_snapshot(
serde_json::json!({ "cityName": "Seoul" }),
))?;
ctx.emit(Event::custom("ping", serde_json::json!({ "seqNo": 1 })))?;
let mut step = ctx.step("answer")?;
let mut thought = step.reasoning()?;
thought.delta("They want the weather.")?;
thought.end()?;
let mut message = step.assistant_message()?;
message.delta("Let me check.")?;
message.end()?;
let mut call = step.tool_call("get_weather")?;
call.args(r#"{"city":"Seoul"}"#)?;
call.result(r#"{"tempC":21}"#)?;
step.emit(Event::state_delta(vec![ag_ui::PatchOperation::add(
"/tempC",
serde_json::json!(21),
)]))?;
drop(step);
Ok(RunOutcome::Success)
}
}
#[tokio::test(flavor = "multi_thread")]
async fn a_bounded_endpoint_reports_overflow_as_a_valid_terminal_event() {
let endpoint =
AgentEndpoint::new(Chatty).event_buffer_capacity(std::num::NonZeroUsize::new(4).unwrap());
let addr = serve(Router::new().route_agui_with("/agent", endpoint)).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
let events = events(&body);
assert_eq!(events.len(), 5);
ag_ui::client::verify_all(&events).expect("overflow is a legal AG-UI error stream");
let Event::RunError(error) = events.last().unwrap() else {
panic!("the endpoint must expose its buffer limit over HTTP");
};
assert_eq!(error.code.as_deref(), Some("EVENT_BUFFER_FULL"));
}
#[tokio::test(flavor = "multi_thread")]
async fn a_run_streams_its_events_in_order() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
let types: Vec<EventType> = events(&body).iter().map(Event::event_type).collect();
assert_eq!(
types,
[
EventType::RunStarted,
EventType::StepStarted,
EventType::TextMessageStart,
EventType::TextMessageContent,
EventType::TextMessageContent,
EventType::TextMessageEnd,
EventType::ToolCallStart,
EventType::ToolCallArgs,
EventType::ToolCallEnd,
EventType::StepFinished,
EventType::RunFinished,
]
);
let Some(Event::RunFinished(finished)) = events(&body).pop() else {
panic!("the stream should end with RUN_FINISHED: {body}");
};
assert_eq!(finished.thread_id.as_str(), "thread-1");
assert_eq!(finished.run_id.as_str(), "run-1");
assert_eq!(finished.outcome, Some(RunOutcome::Success));
}
#[tokio::test(flavor = "multi_thread")]
async fn the_response_is_framed_as_an_unbuffered_event_stream() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
let (head, _) = request(addr, &[], &input()).await;
assert_eq!(head.header("content-type"), Some("text/event-stream"));
assert_eq!(
head.header("transfer-encoding"),
Some("chunked"),
"the body must stream, not arrive as one buffered response"
);
let cache_control = head.header("cache-control").unwrap_or_default();
assert!(cache_control.contains("no-cache"), "{cache_control}");
assert!(cache_control.contains("no-transform"), "{cache_control}");
assert_eq!(head.header("x-accel-buffering"), Some("no"));
assert_eq!(head.header("vary"), Some("accept"));
}
#[tokio::test(flavor = "multi_thread")]
async fn frames_carry_what_the_reference_encoders_put_on_the_wire() {
let addr = serve(Router::new().route_agui("/agent", Verbose)).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
for frame in body.split("\n\n").filter(|frame| !frame.trim().is_empty()) {
let lines: Vec<&str> = frame.lines().collect();
assert_eq!(lines.len(), 1, "one line per frame: {frame:?}");
let payload = lines[0]
.strip_prefix("data: ")
.unwrap_or_else(|| panic!("every frame is a `data:` line: {frame:?}"));
let value: serde_json::Value =
serde_json::from_str(payload).expect("every frame carries one JSON event");
walk(&value, &mut |path, node| {
assert!(
!node.is_null(),
"{path} is null; absent optionals are absent"
);
});
walk(&value, &mut |path, _| {
let key = path.rsplit('.').next().unwrap_or(path);
assert!(
!key.contains('_'),
"{path} is not camelCase — the wire is `by_alias`"
);
});
}
let seen: Vec<EventType> = events(&body).iter().map(Event::event_type).collect();
for expected in [
EventType::RunStarted,
EventType::StateSnapshot,
EventType::Custom,
EventType::StepStarted,
EventType::ReasoningStart,
EventType::ReasoningMessageStart,
EventType::TextMessageStart,
EventType::ToolCallStart,
EventType::ToolCallResult,
EventType::StateDelta,
EventType::RunFinished,
] {
assert!(seen.contains(&expected), "{expected} missing from {seen:?}");
}
}
#[tokio::test(flavor = "multi_thread")]
async fn what_a_client_streams_back_is_what_the_agent_emitted() {
let addr = serve(Router::new().route_agui("/agent", Verbose)).await;
let (_, body) = request(addr, &[], &input()).await;
let events = events(&body);
ag_ui::client::verify_all(&events).expect("the server's own stream must verify client-side");
let mut applier = ag_ui::client::apply::Applier::new();
for event in &events {
applier.apply(event).expect("every event should apply");
}
assert_eq!(applier.text_of("run-1-msg-2"), Some("Let me check."));
assert_eq!(
applier.reasoning_text(&"run-1-msg-1".into()),
Some("They want the weather.")
);
assert_eq!(
applier.state(),
&serde_json::json!({ "cityName": "Seoul", "tempC": 21 })
);
}
fn walk(value: &serde_json::Value, check: &mut impl FnMut(&str, &serde_json::Value)) {
fn go(path: &str, value: &serde_json::Value, check: &mut impl FnMut(&str, &serde_json::Value)) {
check(path, value);
match value {
serde_json::Value::Object(map) => {
for (key, child) in map {
go(&format!("{path}.{key}"), child, check);
}
}
serde_json::Value::Array(items) => {
for (index, child) in items.iter().enumerate() {
go(&format!("{path}[{index}]"), child, check);
}
}
_ => {}
}
}
go("event", value, check);
}
#[tokio::test(flavor = "multi_thread")]
async fn events_arrive_before_the_run_ends() {
let addr = serve(Router::new().route_agui(
"/agent",
Slow {
quiet: Duration::from_secs(30),
},
))
.await;
let mut client = Client::connect(addr).await;
client.post("/agent", &[], &input()).await;
assert_eq!(client.read_head().await.status, 200);
let first = timeout(Duration::from_secs(5), client.read_chunk())
.await
.expect("RUN_STARTED should not wait for the run to finish")
.expect("a chunk, not the terminator");
let first = String::from_utf8(first).expect("utf-8");
assert_eq!(
events(&first).first().map(Event::event_type),
Some(EventType::RunStarted)
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_failing_agent_still_ends_the_stream_with_run_error() {
let addr = serve(Router::new().route_agui("/agent", Broken)).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
let events = events(&body);
let types: Vec<EventType> = events.iter().map(Event::event_type).collect();
assert_eq!(
types,
[
EventType::RunStarted,
EventType::TextMessageStart,
EventType::TextMessageContent,
EventType::TextMessageEnd,
EventType::RunError,
]
);
let Some(Event::RunError(error)) = events.last() else {
panic!("the stream should end with RUN_ERROR: {body}");
};
assert_eq!(error.code.as_deref(), Some("AGENT_ERROR"));
assert!(
error.message.contains("the weather service is down"),
"{error:?}"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn an_interrupt_outcome_rides_out_on_run_finished() {
let addr = serve(Router::new().route_agui("/agent", Cautious)).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
let Some(Event::RunFinished(finished)) = events(&body).pop() else {
panic!("the stream should end with RUN_FINISHED: {body}");
};
let outcome = finished.outcome.expect("an interrupt outcome");
assert!(outcome.is_interrupt(), "{outcome:?}");
assert_eq!(outcome.interrupts().len(), 1);
assert_eq!(outcome.interrupts()[0].id, "delete-everything");
assert_eq!(outcome.interrupts()[0].reason, "tool_approval");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_malformed_body_is_a_400_that_says_why() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
let (head, body) = request(addr, &[], br#"{"threadId": "#).await;
assert_eq!(head.status, 400);
assert_eq!(head.header("content-type"), Some("application/json"));
let message = message_of(&body);
assert!(message.contains("RunAgentInput"), "{message}");
assert!(message.contains("line 1 column"), "{message}");
let (head, body) = request(addr, &[], br#"{"threadId":"t","runId":"r"}"#).await;
assert_eq!(head.status, 400);
assert!(message_of(&body).contains("messages"), "{body}");
let (head, body) = request(addr, &[], b"").await;
assert_eq!(head.status, 400);
assert!(message_of(&body).contains("empty"), "{body}");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_body_that_is_not_json_is_a_415() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
let (head, body) = request(
addr,
&[("content-type", "application/x-www-form-urlencoded")],
b"threadId=t&runId=r",
)
.await;
assert_eq!(head.status, 415);
assert!(
message_of(&body).contains("x-www-form-urlencoded"),
"{body}"
);
for content_type in ["multipart/form-data; boundary=x", "text/plain"] {
let (head, body) = request(addr, &[("content-type", content_type)], &input()).await;
assert_eq!(head.status, 415, "{content_type}: {body}");
}
}
#[tokio::test(flavor = "multi_thread")]
async fn a_body_with_no_content_type_is_a_415() {
let routed = serve(Router::new().route_agui("/agent", Chatty)).await;
let extracted = serve(Router::new().route(
"/agent",
axum::routing::post(|ag_ui::axum::AgUiInput(input)| async move { axum::Json(input) }),
))
.await;
for addr in [routed, extracted] {
let (head, body) = request_without_content_type(addr, &input()).await;
assert_eq!(head.status, 415, "{body}");
assert_eq!(code_of(&body), "UNSUPPORTED_MEDIA_TYPE");
assert!(message_of(&body).contains("Content-Type"), "{body}");
}
}
#[tokio::test(flavor = "multi_thread")]
async fn an_endpoint_can_opt_in_to_bodies_with_no_content_type() {
let endpoint = AgentEndpoint::new(Chatty).allow_missing_content_type();
let addr = serve(Router::new().route_agui_with("/agent", endpoint)).await;
let (head, body) = request_without_content_type(addr, &input()).await;
assert_eq!(head.status, 200, "{body}");
assert_eq!(
events(&body).last().map(Event::event_type),
Some(EventType::RunFinished)
);
let (head, body) = request(addr, &[("content-type", "text/plain")], &input()).await;
assert_eq!(head.status, 415, "{body}");
}
#[tokio::test(flavor = "multi_thread")]
async fn an_oversized_body_is_a_413() {
let app = Router::new()
.route_agui("/agent", Chatty)
.layer(axum::extract::DefaultBodyLimit::max(1024));
let addr = serve(app).await;
let mut oversized =
br#"{"threadId":"t","runId":"r","messages":[],"tools":[],"context":[],"forwardedProps":""#
.to_vec();
oversized.extend(std::iter::repeat_n(b'x', 4096));
oversized.extend(br#""}"#);
let (head, _) = request(addr, &[], &oversized).await;
assert_eq!(head.status, 413);
}
#[tokio::test(flavor = "multi_thread")]
async fn the_headers_the_typescript_client_sends_are_accepted() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
let (head, body) = request(
addr,
&[
("content-type", "application/json"),
("accept", "text/event-stream"),
],
&input(),
)
.await;
assert_eq!(head.status, 200, "{body}");
assert_eq!(head.header("content-type"), Some("text/event-stream"));
for content_type in [
"application/json; charset=utf-8",
"application/json;charset=UTF-8",
"Application/JSON; charset=utf-8",
] {
let (head, body) = request(addr, &[("content-type", content_type)], &input()).await;
assert_eq!(
head.status, 200,
"{content_type} should be accepted: {body}"
);
}
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200, "{body}");
assert_eq!(head.header("content-type"), Some("text/event-stream"));
}
#[cfg(feature = "protobuf")]
#[tokio::test(flavor = "multi_thread")]
async fn a_protobuf_preference_still_uses_the_implemented_sse_transport() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
let (head, body) = request(
addr,
&[(
"accept",
"application/vnd.ag-ui.event+proto;q=1, text/event-stream;q=0.1",
)],
&input(),
)
.await;
assert_eq!(head.status, 200, "{body}");
assert_eq!(head.header("content-type"), Some("text/event-stream"));
assert!(body.contains("RUN_FINISHED"));
let (head, _) = request(
addr,
&[("accept", "application/vnd.ag-ui.event+proto")],
&input(),
)
.await;
assert_eq!(head.status, 406);
}
#[tokio::test(flavor = "multi_thread")]
async fn an_input_may_omit_the_free_form_fields() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
for body in [
br#"{"threadId":"t","runId":"r","messages":[]}"#.as_slice(),
br#"{"threadId":"t","runId":"r","messages":[],"tools":[],"context":[],"state":{},"forwardedProps":{}}"#,
br#"{"threadId":"t","runId":"r","messages":[],"tools":[],"context":[],"somethingNewer":{"a":1}}"#,
br#"{"threadId":"agent:chat:1","runId":"1f0-a-b","messages":[],"tools":[],"context":[]}"#,
] {
let (head, response) = request(addr, &[], body).await;
assert_eq!(
head.status,
200,
"{} should be accepted: {response}",
String::from_utf8_lossy(body)
);
assert_eq!(
events(&response).first().map(Event::event_type),
Some(EventType::RunStarted)
);
}
}
#[tokio::test(flavor = "multi_thread")]
async fn explicit_null_request_values_are_rejected() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
for field in ["state", "protocolVersion", "resume"] {
let mut body = serde_json::json!({
"threadId": "t",
"runId": "r",
"messages": [],
});
body[field] = serde_json::Value::Null;
let (head, response) = request(addr, &[], body.to_string().as_bytes()).await;
assert_eq!(head.status, 400, "{field}: {response}");
assert!(
response.contains(&format!("/{field}")),
"{field}: {response}"
);
}
let body = br#"{"threadId":"t","runId":"r","messages":[],"forwardedProps":null}"#;
let (head, response) = request(addr, &[], body).await;
assert_eq!(head.status, 200, "forwardedProps: {response}");
}
#[tokio::test(flavor = "multi_thread")]
async fn input_enforcement_reaches_the_handler_over_http() {
let addr = serve(Router::new().route(
"/agent",
axum::routing::post(|ag_ui::axum::AgUiInput(input)| async move { axum::Json(input) }),
))
.await;
let body = serde_json::json!({
"threadId":"t", "runId":"r", "messages":[{
"id":"m", "role":"user", "content":[
{"type":"future_part","payload":true},
{"type":"text","text":"hello"}
]
}]
});
let (head, response) = request(addr, &[], body.to_string().as_bytes()).await;
assert_eq!(head.status, 200, "{response}");
let accepted: serde_json::Value = serde_json::from_str(&response).unwrap();
assert_eq!(
accepted["messages"][0]["content"],
serde_json::json!([
{"type":"text","text":"hello"}
])
);
for message in [
serde_json::json!({"id":"m","role":"assistant","content":null}),
serde_json::json!({"id":"m","role":"assistant","toolCalls":[{
"id":"c","function":{"name":"tool","arguments":"{}"}
}]}),
] {
let body = serde_json::json!({"threadId":"t","runId":"r","messages":[message]});
let (head, response) = request(addr, &[], body.to_string().as_bytes()).await;
assert_eq!(head.status, 400, "{response}");
assert!(response.contains("INVALID_INPUT"), "{response}");
}
}
#[tokio::test(flavor = "multi_thread")]
async fn an_accept_this_endpoint_cannot_satisfy_is_a_406() {
let addr = serve(Router::new().route_agui("/agent", Chatty)).await;
for accept in ["application/xml", "text/event-stream;q=0", "text/plain"] {
let (head, body) = request(addr, &[("accept", accept)], &input()).await;
assert_eq!(head.status, 406, "{accept} should be refused");
assert!(message_of(&body).contains("text/event-stream"), "{body}");
}
for accept in [
"*/*",
"text/event-stream",
"text/*",
"application/json, text/*;q=0.1",
] {
let (head, _) = request(addr, &[("accept", accept)], &input()).await;
assert_eq!(head.status, 200, "{accept} should be served");
}
}
#[tokio::test(flavor = "multi_thread")]
async fn dropping_the_client_cancels_the_run() {
let (tx, mut rx) = unbounded_channel();
let emitted = Arc::new(AtomicUsize::new(0));
let agent = Endless {
token: tx,
emitted: Arc::clone(&emitted),
};
let addr = serve(Router::new().route_agui("/agent", agent)).await;
let mut client = Client::connect(addr).await;
client.post("/agent", &[], &input()).await;
assert_eq!(client.read_head().await.status, 200);
for _ in 0..3 {
client
.read_chunk()
.await
.expect("the stream should still be running");
}
let token = rx
.recv()
.await
.expect("the agent should have handed out its token");
assert!(!token.is_cancelled(), "nothing has disconnected yet");
drop(client);
timeout(Duration::from_secs(5), token.cancelled())
.await
.expect("the disconnect should have cancelled the run");
sleep(Duration::from_millis(200)).await;
let after_cancel = emitted.load(Ordering::SeqCst);
sleep(Duration::from_millis(200)).await;
assert_eq!(
emitted.load(Ordering::SeqCst),
after_cancel,
"the agent kept running after the client left"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_client_that_leaves_mid_thought_cancels_the_run() {
let (tx, mut rx) = unbounded_channel();
let addr = serve(Router::new().route_agui("/agent", Waiting { token: tx })).await;
let mut client = Client::connect(addr).await;
client.post("/agent", &[], &input()).await;
assert_eq!(client.read_head().await.status, 200);
client
.read_chunk()
.await
.expect("the run should have started");
let token = rx
.recv()
.await
.expect("the agent should have handed out its token");
while let Ok(Some(_)) = timeout(Duration::from_millis(200), client.read_chunk()).await {}
drop(client);
timeout(Duration::from_secs(5), token.cancelled())
.await
.expect("leaving during a long model call should cancel the run");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_run_that_completes_is_never_reported_as_cancelled() {
let (tx, mut rx) = unbounded_channel();
let addr = serve(Router::new().route_agui("/agent", Prompt { token: tx })).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
assert_eq!(
events(&body).last().map(Event::event_type),
Some(EventType::RunFinished)
);
let token = rx
.recv()
.await
.expect("the agent should have handed out its token");
sleep(Duration::from_millis(200)).await;
assert!(
!token.is_cancelled(),
"a completed run must not look like a disconnected one"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn keep_alive_comments_fill_a_silent_run() {
let endpoint = AgentEndpoint::new(Slow {
quiet: Duration::from_millis(400),
})
.without_keep_alive()
.keep_alive(Duration::from_millis(50));
let addr = serve(Router::new().route_agui_with("/agent", endpoint)).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
let types: Vec<EventType> = events(&body).iter().map(Event::event_type).collect();
assert_eq!(
types,
[
EventType::RunStarted,
EventType::TextMessageStart,
EventType::TextMessageContent,
EventType::TextMessageEnd,
EventType::RunFinished,
]
);
assert!(
body.matches(":\n\n").count() >= 2,
"expected keep-alive comments in {body:?}"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn default_keep_alive_and_explicit_disable_work_over_real_http() {
use futures_util::StreamExt;
async fn record(addr: SocketAddr) -> (String, Option<Duration>) {
let mut client = Client::connect(addr).await;
client.post("/agent", &[], &input()).await;
let head = client.read_head().await;
assert_eq!(head.status, 200);
let started = tokio::time::Instant::now();
let mut body = Vec::new();
let mut first_comment = None;
while let Some(chunk) = client.read_chunk().await {
if first_comment.is_none() && chunk.windows(3).any(|bytes| bytes == b":\n\n") {
first_comment = Some(started.elapsed());
}
body.extend(chunk);
}
(String::from_utf8(body).unwrap(), first_comment)
}
let quiet = Duration::from_secs(16);
let default = serve(Router::new().route_agui("/agent", Slow { quiet })).await;
let disabled = serve(
Router::new().route_agui_with(
"/agent",
AgentEndpoint::new(Slow { quiet })
.keep_alive(Duration::from_millis(50))
.without_keep_alive(),
),
)
.await;
let ((body, first_comment), (silent, disabled_comment)) =
timeout(Duration::from_secs(25), async {
tokio::join!(record(default), record(disabled))
})
.await
.expect("both runs should finish after the 16-second quiet period");
let first_comment = first_comment.expect("the default endpoint sends an idle comment");
assert!(
first_comment >= Duration::from_secs(14),
"{first_comment:?}"
);
assert!(first_comment < Duration::from_secs(17), "{first_comment:?}");
assert!(
disabled_comment.is_none(),
"disabled endpoint sent {silent:?}"
);
let decoded = ag_ui::client::transport::decode_events(futures_util::stream::iter([Ok::<
_,
std::io::Error,
>(
body.into_bytes(),
)]))
.map(Result::unwrap)
.collect::<Vec<_>>()
.await;
let silent = events(&silent);
ag_ui::client::verify_all(&decoded).unwrap();
assert_eq!(decoded, silent, "comments must never become AG-UI events");
assert_eq!(decoded.last().unwrap().event_type(), EventType::RunFinished);
}
#[tokio::test(flavor = "multi_thread")]
async fn an_agent_mounts_inside_a_stateful_router_alongside_other_routes() {
#[derive(Clone)]
struct AppState {
greeting: &'static str,
}
let app: Router = Router::new()
.route(
"/health",
get(
|axum::extract::State(state): axum::extract::State<AppState>| async move {
state.greeting
},
),
)
.route_agui("/agent", Chatty)
.nest(
"/v2",
Router::new().route_agui_with("/agent", AgentEndpoint::new(Chatty)),
)
.with_state(AppState { greeting: "ok" });
let addr = serve(app).await;
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
assert_eq!(
events(&body).last().map(Event::event_type),
Some(EventType::RunFinished)
);
let mut client = Client::connect(addr).await;
client.post("/v2/agent", &[], &input()).await;
assert_eq!(client.read_head().await.status, 200);
let mut client = Client::connect(addr).await;
client.get("/health").await;
let head = client.read_head().await;
assert_eq!(head.status, 200);
assert_eq!(client.read_body(&head).await, b"ok");
let mut client = Client::connect(addr).await;
client.get("/agent").await;
assert_eq!(client.read_head().await.status, 405);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_transformer_rewrites_the_stream_per_run() {
let endpoint =
AgentEndpoint::new(Chatty).transformer(|| FilterToolCalls::deny(["internal_debug"]));
let addr = serve(Router::new().route_agui_with("/agent", endpoint)).await;
for attempt in 0..2 {
let (head, body) = request(addr, &[], &input()).await;
assert_eq!(head.status, 200);
let types: Vec<EventType> = events(&body).iter().map(Event::event_type).collect();
assert_eq!(
types,
[
EventType::RunStarted,
EventType::StepStarted,
EventType::TextMessageStart,
EventType::TextMessageContent,
EventType::TextMessageContent,
EventType::TextMessageEnd,
EventType::StepFinished,
EventType::RunFinished,
],
"run {attempt} should have no tool call in it"
);
}
}
#[tokio::test(flavor = "multi_thread")]
async fn concurrent_runs_do_not_interfere() {
let endpoint =
AgentEndpoint::new(Chatty).transformer(|| FilterToolCalls::deny(["internal_debug"]));
let addr = serve(Router::new().route_agui_with("/agent", endpoint)).await;
let runs: Vec<_> = (0..16)
.map(|_| tokio::spawn(async move { request(addr, &[], &input()).await }))
.collect();
for run in runs {
let (head, body) = run.await.expect("the request task to finish");
assert_eq!(head.status, 200);
let types: Vec<EventType> = events(&body).iter().map(Event::event_type).collect();
assert_eq!(
types.first(),
Some(&EventType::RunStarted),
"every run gets its own stream: {body}"
);
assert_eq!(types.last(), Some(&EventType::RunFinished), "{body}");
assert!(
!types.contains(&EventType::ToolCallStart),
"every run gets its own transformer chain: {body}"
);
}
}
async fn serve(app: Router) -> SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("a free port on loopback");
let addr = listener.local_addr().expect("the bound address");
tokio::spawn(async move {
axum::serve(listener, app).await.expect("the server to run");
});
addr
}
fn input() -> Vec<u8> {
serde_json::to_vec(&RunAgentInput::new("thread-1", "run-1")).expect("serializable input")
}
async fn request(addr: SocketAddr, headers: &[(&str, &str)], body: &[u8]) -> (Head, String) {
let mut client = Client::connect(addr).await;
client.post("/agent", headers, body).await;
let head = client.read_head().await;
let body = client.read_body(&head).await;
(
head,
String::from_utf8(body).expect("a utf-8 response body"),
)
}
async fn request_without_content_type(addr: SocketAddr, body: &[u8]) -> (Head, String) {
let mut client = Client::connect(addr).await;
client.post_exactly("/agent", &[], body).await;
let head = client.read_head().await;
let body = client.read_body(&head).await;
(
head,
String::from_utf8(body).expect("a utf-8 response body"),
)
}
fn message_of(body: &str) -> String {
let value: serde_json::Value = serde_json::from_str(body).expect("a JSON error body");
value["message"]
.as_str()
.expect("a message field")
.to_owned()
}
fn code_of(body: &str) -> String {
let value: serde_json::Value = serde_json::from_str(body).expect("a JSON error body");
value["code"].as_str().expect("a code field").to_owned()
}
fn events(body: &str) -> Vec<Event> {
body.split("\n\n")
.filter(|frame| !frame.trim().is_empty())
.filter_map(|frame| {
let data: Vec<&str> = frame
.lines()
.filter_map(|line| line.strip_prefix("data: "))
.collect();
if data.is_empty() {
return None;
}
Some(serde_json::from_str(&data.join("\n")).expect("an AG-UI event"))
})
.collect()
}
struct Head {
status: u16,
headers: Vec<(String, String)>,
}
impl Head {
fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|(key, _)| key == name)
.map(|(_, value)| value.as_str())
}
}
struct Client {
stream: TcpStream,
pending: Vec<u8>,
}
impl Client {
async fn connect(addr: SocketAddr) -> Self {
let stream = TcpStream::connect(addr).await.expect("a connection");
Self {
stream,
pending: Vec::new(),
}
}
async fn post(&mut self, path: &str, headers: &[(&str, &str)], body: &[u8]) {
let mut all = headers.to_vec();
if !headers.iter().any(|(name, _)| *name == "content-type") {
all.push(("content-type", "application/json"));
}
self.post_exactly(path, &all, body).await;
}
async fn post_exactly(&mut self, path: &str, headers: &[(&str, &str)], body: &[u8]) {
let mut request = format!(
"POST {path} HTTP/1.1\r\nhost: localhost\r\ncontent-length: {}\r\n",
body.len()
);
for (name, value) in headers {
request.push_str(&format!("{name}: {value}\r\n"));
}
request.push_str("\r\n");
self.stream
.write_all(request.as_bytes())
.await
.expect("the request head to go out");
self.stream
.write_all(body)
.await
.expect("the request body to go out");
}
async fn get(&mut self, path: &str) {
let request = format!("GET {path} HTTP/1.1\r\nhost: localhost\r\n\r\n");
self.stream
.write_all(request.as_bytes())
.await
.expect("the request to go out");
}
async fn read_head(&mut self) -> Head {
let raw = self.take_until(b"\r\n\r\n").await;
let text = String::from_utf8(raw).expect("a utf-8 response head");
let mut lines = text.lines();
let status = lines
.next()
.and_then(|line| line.split_whitespace().nth(1).map(str::to_owned))
.and_then(|code| code.parse().ok())
.expect("a status line");
let headers = lines
.filter(|line| !line.is_empty())
.filter_map(|line| line.split_once(':'))
.map(|(name, value)| (name.trim().to_ascii_lowercase(), value.trim().to_owned()))
.collect();
Head { status, headers }
}
async fn read_chunk(&mut self) -> Option<Vec<u8>> {
let header = self.take_until(b"\r\n").await;
let header = String::from_utf8_lossy(&header);
let size = usize::from_str_radix(
header
.trim()
.split(';')
.next()
.expect("a chunk size")
.trim(),
16,
)
.expect("a hexadecimal chunk size");
if size == 0 {
self.take_until(b"\r\n").await; return None;
}
let chunk = self.take(size).await;
self.take(2).await; Some(chunk)
}
async fn read_body(&mut self, head: &Head) -> Vec<u8> {
if let Some(length) = head.header("content-length") {
let length = length.parse().expect("a numeric content-length");
return self.take(length).await;
}
let mut body = Vec::new();
while let Some(chunk) = self.read_chunk().await {
body.extend(chunk);
}
body
}
async fn take(&mut self, count: usize) -> Vec<u8> {
while self.pending.len() < count {
assert!(self.fill().await, "the connection closed mid-body");
}
self.pending.drain(..count).collect()
}
async fn take_until(&mut self, needle: &[u8]) -> Vec<u8> {
loop {
if let Some(at) = self
.pending
.windows(needle.len())
.position(|window| window == needle)
{
return self.pending.drain(..at + needle.len()).collect();
}
assert!(
self.fill().await,
"the connection closed before {:?}",
String::from_utf8_lossy(needle)
);
}
}
async fn fill(&mut self) -> bool {
let mut buffer = [0_u8; 8192];
let read = self
.stream
.read(&mut buffer)
.await
.expect("the connection to stay readable");
self.pending.extend_from_slice(&buffer[..read]);
read > 0
}
}