Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 5 additions & 64 deletions a2a-slimrpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ pub type SlimApp = slim_service::app::App<
>;

use crate::common::{
A2A_COLLABORATIVE_CHANNEL_SERVICE, A2A_SLIMRPC_SERVICE, METHOD_CANCEL_TASK, METHOD_COLLABORATE,
METHOD_CREATE_PUSH_CONFIG, METHOD_DELETE_PUSH_CONFIG, METHOD_GET_EXTENDED_AGENT_CARD,
METHOD_GET_PUSH_CONFIG, METHOD_GET_TASK, METHOD_LIST_PUSH_CONFIGS, METHOD_LIST_TASKS,
METHOD_SEND_MESSAGE, METHOD_SEND_STREAMING_MESSAGE, METHOD_SUBSCRIBE_TO_TASK,
SLIM_SRC_METADATA_KEY, decode_proto_response, encode_proto_message,
service_params_to_metadata_opt,
A2A_SLIMRPC_SERVICE, METHOD_CANCEL_TASK, METHOD_CREATE_PUSH_CONFIG, METHOD_DELETE_PUSH_CONFIG,
METHOD_GET_EXTENDED_AGENT_CARD, METHOD_GET_PUSH_CONFIG, METHOD_GET_TASK,
METHOD_LIST_PUSH_CONFIGS, METHOD_LIST_TASKS, METHOD_SEND_MESSAGE,
METHOD_SEND_STREAMING_MESSAGE, METHOD_SUBSCRIBE_TO_TASK, decode_proto_response,
encode_proto_message, service_params_to_metadata_opt,
};
use crate::errors::rpc_error_to_a2a_error;

Expand Down Expand Up @@ -52,64 +51,6 @@ impl SlimRpcTransport {
Self { channel }
}

/// Build a transport backed by a SLIM **group** channel spanning `members`,
/// for the `Collaborate` many-to-many operation (see
/// [`Self::collaborate`]). Unlike the point-to-point [`Self::new`], this does
/// not correspond to a single `Transport` peer.
pub fn new_group(
app: Arc<SlimApp>,
members: Vec<Arc<ProtoName>>,
) -> Result<Self, slim_rpc::RpcError> {
Self::new_group_with_connection(app, members, None)
}

pub fn new_group_with_connection(
app: Arc<SlimApp>,
members: Vec<Arc<ProtoName>>,
connection_id: Option<u64>,
) -> Result<Self, slim_rpc::RpcError> {
Ok(Self {
channel: slim_rpc::Channel::new_group_with_connection(app, members, connection_id)?,
})
}

/// Open a `Collaborate` session on this (group) channel: broadcast every
/// `Message` produced by `outbound` to the group, and yield every `Message`
/// broadcast by other members — each attributed via
/// `metadata["slim-src"]` (the sender's SLIM name), per the SLIMRPC
/// collaborative channel extension spec. Additive: not part of the
/// point-to-point [`Transport`] trait, whose methods assume exactly one
/// response per call.
pub fn collaborate(
&self,
outbound: impl futures::Stream<Item = Message> + Send + 'static,
timeout: Option<std::time::Duration>,
) -> impl futures::Stream<Item = Result<Message, A2AError>> {
let request_stream =
outbound.map(|message| encode_proto_message(&pbconv::to_proto_message(&message)));
let stream = self.channel.multicast_stream_stream::<Vec<u8>, Vec<u8>>(
A2A_COLLABORATIVE_CHANNEL_SERVICE,
METHOD_COLLABORATE,
request_stream,
timeout,
None,
);
stream.map(|item| {
let item = item.map_err(|error| rpc_error_to_a2a_error(&error))?;
let proto_message =
decode_proto_response::<a2a_pb::proto::Message>(item.message, "Message")?;
let mut message = pbconv::from_proto_message(&proto_message);
message
.metadata
.get_or_insert_with(std::collections::HashMap::new)
.insert(
SLIM_SRC_METADATA_KEY.to_string(),
serde_json::Value::String(item.context.source.to_string()),
);
Ok(message)
})
}

async fn call_unary<Req, Res>(
&self,
params: &ServiceParams,
Expand Down
23 changes: 6 additions & 17 deletions a2a-slimrpc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,12 @@ pub const METHOD_LIST_PUSH_CONFIGS: &str = "ListTaskPushNotificationConfigs";
pub const METHOD_DELETE_PUSH_CONFIG: &str = "DeleteTaskPushNotificationConfig";
pub const METHOD_GET_EXTENDED_AGENT_CARD: &str = "GetExtendedAgentCard";

/// The `Collaborate` operation (many-to-many messaging on a SLIM group channel;
/// see the SLIMRPC collaborative channel extension spec) lives on its own service,
/// separate from the point-to-point `A2AService`.
pub const A2A_COLLABORATIVE_CHANNEL_SERVICE: &str =
"experimental.slimrpc.collaborative_channel.v1.CollaborativeChannelService";
pub const METHOD_COLLABORATE: &str = "Collaborate";

/// Key for the sender's SLIM name.
///
/// - On every inbound `Collaborate` message, the SLIMRPC layer sets it in
/// `Message.metadata`, per the collaborative channel spec's
/// message-attribution requirement.
/// - On every request a [`SlimRpcHandler`](crate::SlimRpcHandler) serves, it is
/// a reserved service-params key holding the caller's SLIM name as the
/// session reports it. Caller-supplied values are dropped. The name is the
/// one the caller claimed when it set up the session; for authorization, use
/// [`SLIM_PEER_SUBJECT_METADATA_KEY`], which is verified.
/// Reserved service-params key holding the caller's SLIM name as the session
/// reports it. Set by the SLIMRPC layer on every request a
/// [`SlimRpcHandler`](crate::SlimRpcHandler) serves; caller-supplied values
/// are dropped. The name is the one the caller claimed when setting up the
/// session; for authorization, use [`SLIM_PEER_SUBJECT_METADATA_KEY`], which
/// is cryptographically verified.
pub const SLIM_SRC_METADATA_KEY: &str = "slim-src";

/// Reserved service-params key holding the verified subject (`sub`) of the
Expand Down
4 changes: 1 addition & 3 deletions a2a-slimrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
//! `slim://` or `slimrpc://`; see [`parse_slimrpc_target`].
//! - **Server:** [`SlimRpcHandler`] registers the methods of an
//! [`a2a_server::RequestHandler`] on a `slim_rpc::Server`.
//! [`register_collaborate`] adds a listen-only participant for the
//! collaborative channel, which is not a request/response operation.
//! - **Errors:** [`errors`] converts between [`a2a::A2AError`] and SLIM RPC
//! errors.
//!
Expand All @@ -37,7 +35,7 @@ pub use client::{SlimApp, SlimRpcTransport, SlimRpcTransportFactory, parse_slimr
pub use common::{
SLIM_PEER_ISSUER_METADATA_KEY, SLIM_PEER_SUBJECT_METADATA_KEY, SLIM_SRC_METADATA_KEY,
};
pub use server::{SlimRpcHandler, register_collaborate};
pub use server::SlimRpcHandler;

// Wire internals reached by the fuzz target in fuzz/ — see #238. Thin
// wrappers, not re-exports: the originals stay crate-private.
Expand Down
50 changes: 5 additions & 45 deletions a2a-slimrpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use a2a::{A2AError, Message, StreamResponse};
use a2a::{A2AError, StreamResponse};
use a2a_pb::pbconv;
use a2a_pb::proto;
use a2a_server::RequestHandler;
use futures::StreamExt;

use crate::common::{
A2A_COLLABORATIVE_CHANNEL_SERVICE, A2A_SLIMRPC_SERVICE, METHOD_CANCEL_TASK, METHOD_COLLABORATE,
METHOD_CREATE_PUSH_CONFIG, METHOD_DELETE_PUSH_CONFIG, METHOD_GET_EXTENDED_AGENT_CARD,
METHOD_GET_PUSH_CONFIG, METHOD_GET_TASK, METHOD_LIST_PUSH_CONFIGS, METHOD_LIST_TASKS,
METHOD_SEND_MESSAGE, METHOD_SEND_STREAMING_MESSAGE, METHOD_SUBSCRIBE_TO_TASK, ServiceParamsMap,
A2A_SLIMRPC_SERVICE, METHOD_CANCEL_TASK, METHOD_CREATE_PUSH_CONFIG, METHOD_DELETE_PUSH_CONFIG,
METHOD_GET_EXTENDED_AGENT_CARD, METHOD_GET_PUSH_CONFIG, METHOD_GET_TASK,
METHOD_LIST_PUSH_CONFIGS, METHOD_LIST_TASKS, METHOD_SEND_MESSAGE,
METHOD_SEND_STREAMING_MESSAGE, METHOD_SUBSCRIBE_TO_TASK, ServiceParamsMap,
context_to_service_params, decode_proto_request, encode_proto_message,
};
use crate::errors::a2a_error_to_rpc_error;
Expand Down Expand Up @@ -158,46 +158,6 @@ impl<H: RequestHandler> SlimRpcHandler<H> {
}
}

/// Register `server` as a **listen-only** `Collaborate` participant (see the
/// SLIMRPC collaborative channel extension spec): `on_message` is invoked for
/// every inbound `Message` on any `Collaborate` session this app is a member of.
/// This participant sends nothing of its own — its reply stream is empty, which
/// per the spec signals non-participation on the send side without affecting its
/// ability to receive.
///
/// Independent of [`SlimRpcHandler`]/`RequestHandler` — `Collaborate` is a
/// broadcast channel rather than a request/response operation, so this takes a
/// plain callback instead.
pub fn register_collaborate<F>(server: &slim_rpc::Server, on_message: F)
where
F: Fn(Message) + Send + Sync + 'static,
{
let on_message = Arc::new(on_message);
server.register_stream_stream::<_, Vec<u8>, Vec<u8>, _, _>(
A2A_COLLABORATIVE_CHANNEL_SERVICE,
METHOD_COLLABORATE,
move |mut stream: slim_rpc::DecodedStream<Vec<u8>>, _context: slim_rpc::Context| {
let on_message = on_message.clone();
async move {
// Drain the broadcast concurrently with returning the (empty)
// reply stream below, so this member's non-participation is
// visible immediately rather than only after the session ends.
tokio::spawn(async move {
while let Some(item) = stream.next().await {
if let Ok(bytes) = item
&& let Ok(proto_message) =
decode_proto_request::<a2a_pb::proto::Message>(bytes, "Message")
{
on_message(pbconv::from_proto_message(&proto_message));
}
}
});
Ok(futures::stream::empty::<Result<Vec<u8>, slim_rpc::RpcError>>())
}
},
);
}

fn register_unary_unary<H, ReqProto, ReqNative, ResNative, DecodeReq, EncodeRes>(
server: &slim_rpc::Server,
handler: Arc<H>,
Expand Down
103 changes: 1 addition & 102 deletions a2a-slimrpc/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,105 +782,4 @@ async fn slimrpc_transport_reports_malformed_payloads() {
env.shutdown().await;
}

/// Group `Collaborate`: every member independently broadcasts one intro `Message`
/// to the others over its own group channel, and every other member's listen-only
/// `register_collaborate` handler receives it — proving the SLIMRPC collaborative
/// channel extension's many-to-many delivery end to end (not just moderator-to-
/// members fan-out).
#[tokio::test]
async fn slimrpc_collaborate_broadcasts_to_every_other_member() {
use std::sync::Mutex;

const MEMBERS: [&str; 3] = ["avatar", "agent-a", "agent-b"];

let service = Arc::new(Service::new(
ID::new_with_name(Kind::new("slim").unwrap(), "collab-group").unwrap(),
));

struct Member {
name: Arc<Name>,
app: Arc<SlimApp>,
received: Arc<Mutex<Vec<Message>>>,
}

let mut members = Vec::new();
for suffix in MEMBERS {
let name = Arc::new(Name::from_strings([
"org",
"test",
&format!("collab-{suffix}"),
]));
let (provider, verifier) = auth(&format!("collab-provider-{suffix}"));
let (app, notifications) = service.create_app(&name, provider, verifier).unwrap();
let app = Arc::new(app);

let server = Arc::new(Server::new(
app.clone(),
app.app_name().clone(),
notifications,
));
let received = Arc::new(Mutex::new(Vec::new()));
let received_for_handler = received.clone();
a2a_slimrpc::register_collaborate(&server, move |message| {
received_for_handler.lock().unwrap().push(message);
});
tokio::spawn(async move {
let _ = server.serve().await;
});

members.push(Member {
name,
app,
received,
});
}
tokio::time::sleep(Duration::from_millis(200)).await;

// Every member independently broadcasts its own intro to the other two.
let mut senders = Vec::new();
for (i, member) in members.iter().enumerate() {
let others: Vec<Arc<Name>> = members
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
.map(|(_, m)| m.name.clone())
.collect();
let transport = SlimRpcTransport::new_group(member.app.clone(), others).unwrap();
let intro = sample_message(
Role::Agent,
&format!("hi, I am {}", MEMBERS[i]),
"collaborate",
);
let outbound = stream::once(async move { intro });
senders.push(tokio::spawn(async move {
let replies = transport.collaborate(outbound, Some(Duration::from_secs(5)));
futures::pin_mut!(replies);
// Everyone else is listen-only, so this drains to nothing; the point
// is proving the broadcast send doesn't error.
while let Some(reply) = replies.next().await {
reply.expect("collaborate reply stream should not error");
}
}));
}
for sender in senders {
sender.await.expect("collaborate sender task panicked");
}

tokio::time::sleep(Duration::from_millis(300)).await;

for (i, member) in members.iter().enumerate() {
let received = member.received.lock().unwrap();
for (j, other) in MEMBERS.iter().enumerate() {
if i == j {
continue;
}
let expected = format!("hi, I am {other}");
assert!(
received.iter().any(|m| m.text() == Some(expected.as_str())),
"{} did not receive {other}'s intro; got {:?}",
MEMBERS[i],
received.iter().map(|m| m.text()).collect::<Vec<_>>()
);
}
}
}
// TODO(#578): re-add slimrpc_collaborate_broadcasts_to_every_other_member when SLIM shared-responses is ready for SLIMRPC native-mode server
2 changes: 1 addition & 1 deletion a2acli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ the provenance do that.

Commands are namespaced by the resource they act on (`card get`, `task get`,
`task push-config create`, …), matching the command taxonomy in the
[a2a-cli specification](https://github.com/a2aproject/a2a-cli/blob/main/specification/SPEC.md#7-command-surface--global-options):
[a2a-cli](https://github.com/a2aproject/a2a-cli):

```sh
cargo run --bin a2acli -- card get
Expand Down
17 changes: 17 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,26 @@ path = "src/helloworld_tls/server.rs"
name = "helloworld-tls-client"
path = "src/helloworld_tls/client.rs"

[[bin]]
name = "collaborate-orders"
path = "src/collaborate/orders.rs"

[[bin]]
name = "collaborate-inventory"
path = "src/collaborate/inventory.rs"

[[bin]]
name = "collaborate-shipping"
path = "src/collaborate/shipping.rs"

[[bin]]
name = "collaborate-client"
path = "src/collaborate/client.rs"

[dependencies]
a2a = { workspace = true }
a2a-client = { workspace = true, features = ["rustls-tls"] }
a2a-collab = { workspace = true }
a2a-grpc = { workspace = true, features = ["rustls-tls"] }
a2a-pb = { workspace = true }
a2a-server = { workspace = true, features = ["rustls-tls"] }
Expand Down
Loading