66
77namespace A2A . AspNetCore ;
88
9+ /// <summary>
10+ /// Static processor class for handling A2A HTTP requests in ASP.NET Core applications.
11+ /// </summary>
12+ /// <remarks>
13+ /// Provides methods for processing agent card queries, task operations, message sending,
14+ /// and push notification configuration through HTTP endpoints.
15+ /// </remarks>
916public static class A2AHttpProcessor
1017{
18+ /// <summary>
19+ /// OpenTelemetry ActivitySource for tracing HTTP processor operations.
20+ /// </summary>
1121 public static readonly ActivitySource ActivitySource = new ( "A2A.HttpProcessor" , "1.0.0" ) ;
1222
23+ /// <summary>
24+ /// Processes a request to retrieve the agent card containing agent capabilities and metadata.
25+ /// </summary>
26+ /// <remarks>
27+ /// Invokes the task manager's agent card query handler to get current agent information.
28+ /// </remarks>
29+ /// <param name="taskManager">The task manager instance containing the agent card query handler.</param>
30+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
31+ /// <param name="agentUrl">The URL of the agent to retrieve the card for.</param>
32+ /// <returns>An HTTP result containing the agent card JSON or an error response.</returns>
1333 internal static Task < IResult > GetAgentCard ( TaskManager taskManager , ILogger logger , string agentUrl )
1434 {
1535 using var activity = ActivitySource . StartActivity ( "GetAgentCard" , ActivityKind . Server ) ;
@@ -27,6 +47,18 @@ internal static Task<IResult> GetAgentCard(TaskManager taskManager, ILogger logg
2747 }
2848 }
2949
50+ /// <summary>
51+ /// Processes a request to retrieve a specific task by its ID.
52+ /// </summary>
53+ /// <remarks>
54+ /// Returns the task's current state, history, and metadata with optional history length limiting.
55+ /// </remarks>
56+ /// <param name="taskManager">The task manager instance for accessing task storage.</param>
57+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
58+ /// <param name="id">The unique identifier of the task to retrieve.</param>
59+ /// <param name="historyLength">Optional limit on the number of history items to return.</param>
60+ /// <param name="metadata">Optional JSON metadata filter for the task query.</param>
61+ /// <returns>An HTTP result containing the task JSON or a not found/error response.</returns>
3062 internal static async Task < IResult > GetTask ( TaskManager taskManager , ILogger logger , string id , int ? historyLength , string ? metadata )
3163 {
3264 using var activity = ActivitySource . StartActivity ( "GetTask" , ActivityKind . Server ) ;
@@ -50,6 +82,16 @@ internal static async Task<IResult> GetTask(TaskManager taskManager, ILogger log
5082 }
5183 }
5284
85+ /// <summary>
86+ /// Processes a request to cancel a specific task by setting its status to Canceled.
87+ /// </summary>
88+ /// <remarks>
89+ /// Invokes the task manager's cancellation logic and returns the updated task state.
90+ /// </remarks>
91+ /// <param name="taskManager">The task manager instance for handling task cancellation.</param>
92+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
93+ /// <param name="id">The unique identifier of the task to cancel.</param>
94+ /// <returns>An HTTP result containing the canceled task JSON or a not found/error response.</returns>
5395 internal static async Task < IResult > CancelTask ( TaskManager taskManager , ILogger logger , string id )
5496 {
5597 using var activity = ActivitySource . StartActivity ( "CancelTask" , ActivityKind . Server ) ;
@@ -72,6 +114,20 @@ internal static async Task<IResult> CancelTask(TaskManager taskManager, ILogger
72114 }
73115 }
74116
117+ /// <summary>
118+ /// Processes a request to send a message to a task and return a single response.
119+ /// </summary>
120+ /// <remarks>
121+ /// Creates a new task if no task ID is provided, or updates an existing task's history.
122+ /// Configures message sending parameters including history length and metadata.
123+ /// </remarks>
124+ /// <param name="taskManager">The task manager instance for handling message processing.</param>
125+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
126+ /// <param name="taskId">Optional task ID to send the message to. If null, a new task may be created.</param>
127+ /// <param name="sendParams">The message parameters containing the message content and configuration.</param>
128+ /// <param name="historyLength">Optional limit on the number of history items to include in processing.</param>
129+ /// <param name="metadata">Optional JSON metadata to include with the message request.</param>
130+ /// <returns>An HTTP result containing the agent's response (Task or Message) or an error response.</returns>
75131 internal static async Task < IResult > SendTaskMessage ( TaskManager taskManager , ILogger logger , string ? taskId , MessageSendParams sendParams , int ? historyLength , string ? metadata )
76132 {
77133 using var activity = ActivitySource . StartActivity ( "SendTaskMessage" , ActivityKind . Server ) ;
@@ -107,6 +163,20 @@ internal static async Task<IResult> SendTaskMessage(TaskManager taskManager, ILo
107163 }
108164 }
109165
166+ /// <summary>
167+ /// Processes a request to send a message to a task and return a stream of events.
168+ /// </summary>
169+ /// <remarks>
170+ /// Creates or updates a task and establishes a Server-Sent Events stream that yields
171+ /// Task, Message, TaskStatusUpdateEvent, and TaskArtifactUpdateEvent objects as they occur.
172+ /// </remarks>
173+ /// <param name="taskManager">The task manager instance for handling streaming message processing.</param>
174+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
175+ /// <param name="id">The unique identifier of the task to send the message to.</param>
176+ /// <param name="sendParams">The message parameters containing the message content and configuration.</param>
177+ /// <param name="historyLength">Optional limit on the number of history items to include in processing.</param>
178+ /// <param name="metadata">Optional JSON metadata to include with the message request.</param>
179+ /// <returns>An HTTP result that streams events as Server-Sent Events or an error response.</returns>
110180 internal static async Task < IResult > SendSubscribeTaskMessage ( TaskManager taskManager , ILogger logger , string id , MessageSendParams sendParams , int ? historyLength , string ? metadata )
111181 {
112182 using var activity = ActivitySource . StartActivity ( "SendSubscribeTaskMessage" , ActivityKind . Server ) ;
@@ -132,6 +202,17 @@ internal static async Task<IResult> SendSubscribeTaskMessage(TaskManager taskMan
132202 }
133203 }
134204
205+ /// <summary>
206+ /// Processes a request to resubscribe to an existing task's event stream.
207+ /// </summary>
208+ /// <remarks>
209+ /// Returns the active event enumerator for the specified task, allowing clients
210+ /// to reconnect to ongoing task updates via Server-Sent Events.
211+ /// </remarks>
212+ /// <param name="taskManager">The task manager instance containing active task event streams.</param>
213+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
214+ /// <param name="id">The unique identifier of the task to resubscribe to.</param>
215+ /// <returns>An HTTP result that streams existing task events or an error response.</returns>
135216 internal static IResult ResubscribeTask ( TaskManager taskManager , ILogger logger , string id )
136217 {
137218 using var activity = ActivitySource . StartActivity ( "ResubscribeTask" , ActivityKind . Server ) ;
@@ -150,6 +231,17 @@ internal static IResult ResubscribeTask(TaskManager taskManager, ILogger logger,
150231 }
151232 }
152233
234+ /// <summary>
235+ /// Processes a request to set or update push notification configuration for a specific task.
236+ /// </summary>
237+ /// <remarks>
238+ /// Configures callback URLs and authentication settings for receiving task update notifications via HTTP.
239+ /// </remarks>
240+ /// <param name="taskManager">The task manager instance for handling push notification configuration.</param>
241+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
242+ /// <param name="id">The unique identifier of the task to configure push notifications for.</param>
243+ /// <param name="pushNotificationConfig">The push notification configuration containing callback URL and authentication details.</param>
244+ /// <returns>An HTTP result containing the configured settings or an error response.</returns>
153245 internal static async Task < IResult > SetPushNotification ( TaskManager taskManager , ILogger logger , string id , PushNotificationConfig pushNotificationConfig )
154246 {
155247 using var activity = ActivitySource . StartActivity ( "ConfigurePushNotification" , ActivityKind . Server ) ;
@@ -178,6 +270,16 @@ internal static async Task<IResult> SetPushNotification(TaskManager taskManager,
178270 }
179271 }
180272
273+ /// <summary>
274+ /// Processes a request to retrieve the push notification configuration for a specific task.
275+ /// </summary>
276+ /// <remarks>
277+ /// Returns the callback URL and authentication settings configured for receiving task update notifications.
278+ /// </remarks>
279+ /// <param name="taskManager">The task manager instance for accessing push notification configurations.</param>
280+ /// <param name="logger">Logger instance for recording operation details and errors.</param>
281+ /// <param name="id">The unique identifier of the task to get push notification configuration for.</param>
282+ /// <returns>An HTTP result containing the push notification configuration or a not found/error response.</returns>
181283 internal static async Task < IResult > GetPushNotification ( TaskManager taskManager , ILogger logger , string id )
182284 {
183285 using var activity = ActivitySource . StartActivity ( "GetPushNotification" , ActivityKind . Server ) ;
@@ -203,14 +305,34 @@ internal static async Task<IResult> GetPushNotification(TaskManager taskManager,
203305 }
204306}
205307
308+ /// <summary>
309+ /// Result type for returning A2A responses as JSON in HTTP responses.
310+ /// </summary>
311+ /// <remarks>
312+ /// Implements IResult to provide custom serialization of A2AResponse objects
313+ /// using the configured JSON serialization options.
314+ /// </remarks>
206315public class A2AResponseResult : IResult
207316{
208317 private readonly A2AResponse a2aResponse ;
209318
319+ /// <summary>
320+ /// Initializes a new instance of the A2AResponseResult class.
321+ /// </summary>
322+ /// <param name="a2aResponse">The A2A response object to serialize and return in the HTTP response.</param>
210323 public A2AResponseResult ( A2AResponse a2aResponse )
211324 {
212325 this . a2aResponse = a2aResponse ;
213326 }
327+
328+ /// <summary>
329+ /// Executes the result by serializing the A2A response as JSON to the HTTP response body.
330+ /// </summary>
331+ /// <remarks>
332+ /// Sets the appropriate content type and uses the default A2A JSON serialization options.
333+ /// </remarks>
334+ /// <param name="httpContext">The HTTP context to write the response to.</param>
335+ /// <returns>A task representing the asynchronous serialization operation.</returns>
214336 public async Task ExecuteAsync ( HttpContext httpContext )
215337 {
216338 httpContext . Response . ContentType = "application/json" ;
@@ -219,15 +341,35 @@ public async Task ExecuteAsync(HttpContext httpContext)
219341 }
220342}
221343
344+ /// <summary>
345+ /// Result type for streaming A2A events as Server-Sent Events (SSE) in HTTP responses.
346+ /// </summary>
347+ /// <remarks>
348+ /// Implements IResult to provide real-time streaming of task events including Task objects,
349+ /// TaskStatusUpdateEvent, and TaskArtifactUpdateEvent objects.
350+ /// </remarks>
222351public class A2AEventStreamResult : IResult
223352{
224353 private readonly IAsyncEnumerable < A2AEvent > taskEvents ;
225354
355+ /// <summary>
356+ /// Initializes a new instance of the A2AEventStreamResult class.
357+ /// </summary>
358+ /// <param name="taskEvents">The async enumerable stream of A2A events to send as Server-Sent Events.</param>
226359 public A2AEventStreamResult ( IAsyncEnumerable < A2AEvent > taskEvents )
227360 {
228361 this . taskEvents = taskEvents ;
229362 }
230363
364+ /// <summary>
365+ /// Executes the result by streaming A2A events as Server-Sent Events to the HTTP response.
366+ /// </summary>
367+ /// <remarks>
368+ /// Sets the appropriate SSE content type and formats each event according to the SSE specification.
369+ /// Each event is serialized as JSON and sent with the "data:" prefix followed by double newlines.
370+ /// </remarks>
371+ /// <param name="httpContext">The HTTP context to stream the events to.</param>
372+ /// <returns>A task representing the asynchronous streaming operation.</returns>
231373 public async Task ExecuteAsync ( HttpContext httpContext )
232374 {
233375 httpContext . Response . ContentType = "text/event-stream" ;
0 commit comments