Skip to main content

ag_ui/
input.rs

1//! The request body an agent receives to start or resume a run.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::context::Context;
7use crate::ids::{RunId, ThreadId};
8use crate::message::Message;
9use crate::outcome::ResumeEntry;
10use crate::tool::Tool;
11
12/// The AG-UI protocol version this SDK speaks on the wire.
13pub const PROTOCOL_VERSION: &str = "1.0";
14
15/// Everything an agent needs for one run.
16///
17/// This is the body of the AG-UI run request, and it is also embedded verbatim
18/// in [`RunStartedEvent::input`](crate::event::RunStartedEvent::input) so a
19/// recorded stream can be replayed without the original request.
20#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
23#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
24pub struct RunAgentInput {
25    /// The conversation this run belongs to.
26    pub thread_id: ThreadId,
27    /// This run's id, echoed on every lifecycle event.
28    pub run_id: RunId,
29    /// The protocol version this consumer speaks. Absent for a known legacy
30    /// peer that predates version declarations.
31    #[serde(default, skip_serializing_if = "Option::is_none")]
32    pub protocol_version: Option<String>,
33    /// The run that spawned this one, for nested / delegated agents.
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub parent_run_id: Option<RunId>,
36    /// Shared state, mutated by the agent through `STATE_SNAPSHOT` and
37    /// `STATE_DELTA`. Free-form JSON, opaque to the protocol.
38    /// Absent state remains the local null default; explicit null is invalid
39    /// under AG-UI 1.0 and is omitted when serializing that default.
40    #[serde(
41        default,
42        deserialize_with = "crate::serde_util::reject_null_value",
43        skip_serializing_if = "Value::is_null"
44    )]
45    pub state: Value,
46    /// Conversation history, oldest first.
47    pub messages: Vec<Message>,
48    /// Tools the client is offering for this run.
49    #[serde(default)]
50    pub tools: Vec<Tool>,
51    /// Ambient context entries.
52    #[serde(default)]
53    pub context: Vec<Context>,
54    /// Arbitrary passthrough properties, opaque to the protocol.
55    #[serde(
56        default,
57        deserialize_with = "crate::serde_util::reject_null_value",
58        skip_serializing_if = "Value::is_null"
59    )]
60    pub forwarded_props: Value,
61    /// Answers to the interrupts a previous run paused on. Present only when
62    /// resuming — see [`crate::outcome`].
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub resume: Option<Vec<ResumeEntry>>,
65}
66
67impl RunAgentInput {
68    /// Builds an input with only the two required identifiers set.
69    pub fn new(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
70        Self {
71            thread_id: thread_id.into(),
72            run_id: run_id.into(),
73            protocol_version: Some(PROTOCOL_VERSION.to_owned()),
74            ..Default::default()
75        }
76    }
77
78    /// Whether this request resumes a paused run.
79    pub fn is_resume(&self) -> bool {
80        self.resume.as_ref().is_some_and(|r| !r.is_empty())
81    }
82}