Skip to main content

rig_a2a/
lib.rs

1//! Rig agents over the Agent2Agent (A2A) protocol, version 1.0, both ways:
2//! serve a Rig [`Agent`](rig_agent::Agent) to any A2A client with
3//! [`A2aServer`], and call any A2A agent from a Rig agent as a tool with
4//! [`RemoteAgent`].
5//!
6//! [`A2aServer`] builds the agent card and an Axum router with the JSON-RPC
7//! and HTTP+JSON bindings, streaming over server-sent events, and task get,
8//! list and cancel. The protocol itself comes from the official A2A Rust SDK,
9//! re-exported as [`a2a`] and [`a2a_server`].
10//!
11//! Each message starts a task. The reply streams into one text artifact and
12//! the task completes, fails, or is canceled. Messages that share a
13//! `contextId` share the agent's conversation history, which lives in memory
14//! for the life of the server. [`A2aExecutor`] is the same agent as an
15//! [`a2a_server::AgentExecutor`] for a handler you assemble yourself.
16//!
17//! ```no_run
18//! use rig_a2a::A2aServer;
19//! use rig_agent::AgentBuilder;
20//! use rig_core::providers::openai::{self, OpenAI};
21//!
22//! async fn serve() -> Result<(), Box<dyn std::error::Error>> {
23//!     let model = OpenAI::from_env()?.completion(openai::GPT_5_4_MINI);
24//!     let agent = AgentBuilder::new(model).name("helper").build();
25//!     let app = A2aServer::new(agent, "http://localhost:3000").into_router();
26//!     let listener = tokio::net::TcpListener::bind("localhost:3000").await?;
27//!     rig_a2a::axum::serve(listener, app).await?;
28//!     Ok(())
29//! }
30//! ```
31//!
32//! `examples/a2a_serve.rs` runs this with a model from the environment.
33//!
34//! Ask a remote A2A agent directly, or give it to a model as a tool:
35//!
36//! ```no_run
37//! use rig_a2a::RemoteAgent;
38//!
39//! async fn ask(question: &str) -> Result<String, Box<dyn std::error::Error>> {
40//!     let remote = RemoteAgent::connect("http://127.0.0.1:3000").await?;
41//!     let reply = remote.send(question, None).await?;
42//!     Ok(reply.text)
43//! }
44//! ```
45//!
46//! ```no_run
47//! use rig_a2a::RemoteAgent;
48//! use rig_agent::AgentBuilder;
49//!
50//! async fn as_tool(builder: AgentBuilder) -> Result<rig_agent::Agent, Box<dyn std::error::Error>> {
51//!     let remote = RemoteAgent::connect("http://127.0.0.1:3000").await?;
52//!     // One turn is the default, so allow the tool call and the answer.
53//!     Ok(builder.dynamic_tool(remote.into_tool()?).default_max_turns(4).build())
54//! }
55//! ```
56//!
57//! `examples/call.rs` streams an answer from any A2A agent, and
58//! `examples/remote_tool.rs` runs the tool case end to end.
59//!
60//! Prompts and replies go on the trace only when the agent records them, with
61//! `AgentBuilder::record_content_telemetry(true)`. It is off by default, as in Rig.
62
63#![cfg_attr(docsrs, feature(doc_cfg))]
64#![cfg_attr(
65    test,
66    allow(
67        clippy::expect_used,
68        clippy::panic,
69        clippy::unwrap_used,
70        clippy::unreachable
71    )
72)]
73
74// The A2A server stack (axum, tokio, hyper) runs on native targets only.
75#[cfg(target_family = "wasm")]
76compile_error!(
77    "rig-a2a is native-only: it serves agents over HTTP with the A2A SDK's \
78     axum server. Disable the `a2a` feature for wasm targets."
79);
80
81#[cfg(not(target_family = "wasm"))]
82mod client;
83#[cfg(not(target_family = "wasm"))]
84mod executor;
85#[cfg(not(target_family = "wasm"))]
86mod history;
87#[cfg(all(feature = "otel", not(target_family = "wasm")))]
88mod otel;
89#[cfg(not(target_family = "wasm"))]
90mod parts;
91#[cfg(not(target_family = "wasm"))]
92mod push;
93#[cfg(not(target_family = "wasm"))]
94mod server;
95#[cfg(not(target_family = "wasm"))]
96mod v03;
97
98/// The A2A extension a card declares, for [`A2aServer::extension`].
99#[cfg(not(target_family = "wasm"))]
100pub use a2a::AgentExtension;
101#[cfg(not(target_family = "wasm"))]
102pub use client::{
103    DEFAULT_TIMEOUT, FileContent, RemoteAgent, RemoteAgentBuilder, RemoteError, RemoteFile, Reply,
104    ReplyEvent,
105};
106#[cfg(not(target_family = "wasm"))]
107pub use executor::{
108    A2aExecutor, DEFAULT_MAX_CONCURRENT_TASKS, DEFAULT_MAX_RUN_DURATION, activated_extensions,
109};
110#[cfg(not(target_family = "wasm"))]
111pub use history::{DEFAULT_HISTORY_TOKENS, DEFAULT_MAX_CONTEXTS};
112#[cfg(not(target_family = "wasm"))]
113pub use server::{A2aServer, DEFAULT_MAX_REQUEST_BYTES, JSONRPC_PATH, REST_PATH};
114#[cfg(not(target_family = "wasm"))]
115pub use v03::V03_PATH;
116
117/// The A2A protocol types this crate is built against.
118#[cfg(not(target_family = "wasm"))]
119pub use a2a;
120/// The A2A client this crate is built against.
121#[cfg(not(target_family = "wasm"))]
122pub use a2a_client;
123/// The A2A server framework this crate is built against.
124#[cfg(not(target_family = "wasm"))]
125pub use a2a_server;
126/// The Axum version the router is built with, for `axum::serve`.
127#[cfg(not(target_family = "wasm"))]
128pub use axum;
129/// The session stores and history windows conversations are kept with.
130#[cfg(not(target_family = "wasm"))]
131pub use rig_memory;
132
133#[cfg(all(test, not(target_family = "wasm")))]
134mod tests;
135
136#[cfg(doctest)]
137#[doc = include_str!("../README.md")]
138mod readme_doctests {}