-
Notifications
You must be signed in to change notification settings - Fork 3
feat(a2a-nats): add server push_list handler #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| use tracing::{instrument, warn}; | ||
|
|
||
| use crate::jsonrpc::extract_request_id; | ||
| use crate::server::handler::{A2aError, A2aExecutor}; | ||
| use crate::server::wire::{JsonRpcErrorResponse, JsonRpcResponse, is_notification, parse_request}; | ||
|
|
||
| #[instrument(name = "a2a.server.push_list", skip(handler, payload, reply_subject, nats))] | ||
| pub async fn handle<H, N>(handler: &H, payload: &[u8], reply_subject: Option<String>, nats: &N) | ||
| where | ||
| H: A2aExecutor, | ||
| N: trogon_nats::PublishClient, | ||
| { | ||
| let Some(reply) = reply_subject else { | ||
| warn!("tasks/pushNotificationConfig/list received without reply subject; dropping"); | ||
| return; | ||
| }; | ||
|
|
||
| let id = extract_request_id(payload); | ||
| if id.is_none() && is_notification(payload) { | ||
| return; | ||
| } | ||
|
|
||
| let result = match parse_request::<serde_json::Value>(payload) { | ||
| Err(_) => Err(A2aError::new(-32700, "Parse error")), | ||
| Ok(envelope) => match envelope.params { | ||
| None => Err(A2aError::new(-32602, "Invalid params: missing params")), | ||
| Some(raw) => match serde_json::from_value::<a2a::types::ListTaskPushNotificationConfigsRequest>(raw) { | ||
| Err(e) => Err(A2aError::new(-32602, format!("Invalid params: {e}"))), | ||
| Ok(params) => handler.push_notification_list(params).await, | ||
| }, | ||
| }, | ||
| }; | ||
| let bytes = match result { | ||
| Ok(resp) => JsonRpcResponse::new(id, resp).to_bytes(), | ||
| Err(e) => JsonRpcErrorResponse::new(id, e.code, e.message).to_bytes(), | ||
| }; | ||
| match bytes { | ||
| Ok(b) => { | ||
| let headers = async_nats::HeaderMap::new(); | ||
| if let Err(e) = nats | ||
| .publish_with_headers(async_nats::Subject::from(reply.as_str()), headers, b) | ||
| .await | ||
| { | ||
| warn!(error = %e, "failed to publish push_list reply"); | ||
| } | ||
| } | ||
| Err(e) => warn!(error = %e, "failed to serialize push_list response"), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use trogon_nats::AdvancedMockNatsClient; | ||
|
|
||
| use super::*; | ||
| use crate::server::test_support::{parse_response, stub}; | ||
|
|
||
| fn list_request() -> a2a::types::ListTaskPushNotificationConfigsRequest { | ||
| a2a::types::ListTaskPushNotificationConfigsRequest { | ||
| task_id: "task-1".to_string(), | ||
| page_size: None, | ||
| page_token: None, | ||
| tenant: None, | ||
| } | ||
| } | ||
|
|
||
| fn empty_response() -> a2a::types::ListTaskPushNotificationConfigsResponse { | ||
| a2a::types::ListTaskPushNotificationConfigsResponse { | ||
| configs: vec![], | ||
| next_page_token: None, | ||
| } | ||
| } | ||
|
|
||
| fn list_payload(req_id: i64) -> Vec<u8> { | ||
| serde_json::to_vec(&serde_json::json!({ | ||
| "jsonrpc": "2.0", | ||
| "id": req_id, | ||
| "method": "tasks/pushNotificationConfig/list", | ||
| "params": list_request() | ||
| })) | ||
| .unwrap() | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn success_publishes_list() { | ||
| let nats = AdvancedMockNatsClient::new(); | ||
| let handler = stub(); | ||
| handler.lock().unwrap().push_list_result = Some(Ok(empty_response())); | ||
| handle(&handler, &list_payload(1), Some("r".into()), &nats).await; | ||
| let body = parse_response(&nats.published_payloads()[0]); | ||
| // configs is skip_serializing_if=Vec::is_empty, so an empty list omits the field. | ||
| assert!(body.get("result").is_some()); | ||
| assert!(body["error"].is_null()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn push_not_supported_error_uses_typed_code() { | ||
| let nats = AdvancedMockNatsClient::new(); | ||
| let handler = stub(); | ||
| handler.lock().unwrap().push_list_result = Some(Err(A2aError::push_notification_not_supported("no push"))); | ||
| handle(&handler, &list_payload(2), Some("r".into()), &nats).await; | ||
| let body = parse_response(&nats.published_payloads()[0]); | ||
| assert_eq!( | ||
| body["error"]["code"].as_i64(), | ||
| Some(i64::from(crate::error::PUSH_NOTIFICATION_NOT_SUPPORTED)) | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn no_reply_drops_request() { | ||
| let nats = AdvancedMockNatsClient::new(); | ||
| let handler = stub(); | ||
| handle(&handler, &list_payload(3), None, &nats).await; | ||
| assert!(nats.published_messages().is_empty()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn missing_params_returns_invalid_params_error() { | ||
| let nats = AdvancedMockNatsClient::new(); | ||
| let handler = stub(); | ||
| let payload = serde_json::to_vec(&serde_json::json!({ | ||
| "jsonrpc": "2.0", | ||
| "id": 5, | ||
| "method": "tasks/pushNotificationConfig/list" | ||
| })) | ||
| .unwrap(); | ||
| handle(&handler, &payload, Some("r".into()), &nats).await; | ||
| let body = parse_response(&nats.published_payloads()[0]); | ||
| assert_eq!(body["error"]["code"], -32602); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn invalid_params_shape_returns_invalid_params_code() { | ||
| let nats = AdvancedMockNatsClient::new(); | ||
| let handler = stub(); | ||
| let payload = serde_json::to_vec(&serde_json::json!({ | ||
| "jsonrpc": "2.0", | ||
| "id": 6, | ||
| "method": "tasks/pushNotificationConfig/list", | ||
| "params": { "taskId": 42 } | ||
| })) | ||
| .unwrap(); | ||
| handle(&handler, &payload, Some("r".into()), &nats).await; | ||
| let body = parse_response(&nats.published_payloads()[0]); | ||
| assert_eq!(body["error"]["code"], -32602); | ||
| assert_eq!(body["id"], 6); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn malformed_json_still_publishes_parse_error_with_null_id() { | ||
| let nats = AdvancedMockNatsClient::new(); | ||
| let handler = stub(); | ||
| handle(&handler, b"not json", Some("r".into()), &nats).await; | ||
| let body = parse_response(&nats.published_payloads()[0]); | ||
| assert_eq!(body["error"]["code"], -32700); | ||
| assert!(body["id"].is_null()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn notification_without_id_is_dropped() { | ||
| let nats = AdvancedMockNatsClient::new(); | ||
| let handler = stub(); | ||
| let payload = serde_json::to_vec(&serde_json::json!({ | ||
| "jsonrpc": "2.0", | ||
| "method": "tasks/pushNotificationConfig/list", | ||
| "params": list_request() | ||
| })) | ||
| .unwrap(); | ||
| handle(&handler, &payload, Some("r".into()), &nats).await; | ||
| assert!(nats.published_messages().is_empty()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.