pub struct SseResponse { /* private fields */ }Expand description
A negotiated event-stream response, waiting for the stream to put in it.
The full manual wiring, for a handler that does its own work before starting
the run — route_agui is this, with the
defaults filled in:
use ag_ui::axum::SseResponse;
use ag_ui::{RunAgentInput, RunOutcome};
use ag_ui::server::{Agent, Result, RunContext, Runner};
let response = SseResponse::negotiate(accept)?;
let runner = Runner::new(Greeter);
// Take the token *before* `run` consumes the runner.
let response = response.cancellation(runner.cancellation_token());
Ok(response.stream(runner.run(input)))Implementations§
Source§impl SseResponse
impl SseResponse
Sourcepub fn negotiate(accept: Option<&str>) -> Result<Self>
pub fn negotiate(accept: Option<&str>) -> Result<Self>
Negotiates the encoding for an Accept header — see negotiate.
Sourcepub fn cancellation(self, token: CancellationToken) -> Self
pub fn cancellation(self, token: CancellationToken) -> Self
Trips token when the client disconnects.
The token to pass is the one the run was built with —
Runner::cancellation_token.
§How the disconnect is noticed
There is no callback to register: hyper drops the response body when
the connection breaks, so a Drop impl on the body is the signal. The
guard disarms itself when the stream ends normally, so a completed run
is never reported as cancelled.
Dropping the body would already stop the agent — the future lives inside the stream. The token is what reaches everything outside it: a spawned tool call, an in-flight model request, a lock the run holds.
Sourcepub fn keep_alive(self, interval: Duration) -> Self
pub fn keep_alive(self, interval: Duration) -> Self
Sends an SSE comment whenever the agent has produced nothing for
interval.
Off by default. Turn it on when something between the agent and the browser closes idle connections — most reverse proxies do, at 30 to 60 seconds, which is well inside the time a slow first token can take.
Trait Implementations§
Source§impl Clone for SseResponse
impl Clone for SseResponse
Source§fn clone(&self) -> SseResponse
fn clone(&self) -> SseResponse
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more