Module: A2A::Server::A2AMethods
- Included in:
- ExampleAgent
- Defined in:
- lib/a2a/server/a2a_methods.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#client_connected?(_yielder) ⇒ Boolean
private
Check if client is still connected (implementation specific).
-
#generate_agent_card(context) ⇒ A2A::Types::AgentCard
protected
Generate agent card (to be implemented by subclasses).
-
#generate_extended_agent_card(context) ⇒ A2A::Types::AgentCard
protected
Generate extended agent card (to be implemented by subclasses).
-
#handle_agent_get_authenticated_extended_card(_params, context) ⇒ Hash
Handle agent/getAuthenticatedExtendedCard method.
-
#handle_agent_get_card(_params, context) ⇒ Hash
Handle agent/getCard method.
-
#handle_message_send(params, context) ⇒ Hash
Handle message/send method.
-
#handle_message_stream(params, context) ⇒ Enumerator
Handle message/stream method.
-
#handle_push_notification_config_delete(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/delete method.
-
#handle_push_notification_config_get(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/get method.
-
#handle_push_notification_config_list(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/list method.
-
#handle_push_notification_config_set(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/set method.
-
#handle_tasks_cancel(params, _context) ⇒ Hash
Handle tasks/cancel method.
-
#handle_tasks_get(params, _context) ⇒ Hash
Handle tasks/get method.
-
#handle_tasks_resubscribe(params, _context) ⇒ Enumerator
Handle tasks/resubscribe method.
-
#process_message_async(message, task, context) ⇒ void
protected
Process message asynchronously (to be implemented by subclasses).
-
#process_message_stream(message, task, context) {|response| ... } ⇒ void
protected
Process message stream (to be implemented by subclasses).
-
#process_message_sync(message, task, context) ⇒ Object
protected
Process message synchronously (to be implemented by subclasses).
-
#push_notification_manager ⇒ A2A::Server::PushNotificationManager
Get push notification manager instance.
-
#task_manager ⇒ A2A::Server::TaskManager
Get task manager instance.
-
#validate_required_params(params, required) ⇒ Object
private
Validate required parameters.
Class Method Details
.included(base) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/a2a/server/a2a_methods.rb', line 17 def self.included(base) base.extend(ClassMethods) # Register all standard A2A methods when included base.register_a2a_methods end |
Instance Method Details
#client_connected?(_yielder) ⇒ Boolean (private)
Check if client is still connected (implementation specific)
513 514 515 516 517 |
# File 'lib/a2a/server/a2a_methods.rb', line 513 def client_connected?(_yielder) # This is a placeholder - actual implementation depends on the server framework # For now, assume client is always connected true end |
#generate_agent_card(context) ⇒ A2A::Types::AgentCard (protected)
Generate agent card (to be implemented by subclasses)
478 479 480 |
# File 'lib/a2a/server/a2a_methods.rb', line 478 def generate_agent_card(context) raise NotImplementedError, "Subclasses must implement generate_agent_card" end |
#generate_extended_agent_card(context) ⇒ A2A::Types::AgentCard (protected)
Generate extended agent card (to be implemented by subclasses)
487 488 489 490 |
# File 'lib/a2a/server/a2a_methods.rb', line 487 def generate_extended_agent_card(context) # Default implementation returns the same as regular card generate_agent_card(context) end |
#handle_agent_get_authenticated_extended_card(_params, context) ⇒ Hash
Handle agent/getAuthenticatedExtendedCard method
409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/a2a/server/a2a_methods.rb', line 409 def handle_agent_get_authenticated_extended_card(_params, context) # Verify authentication raise A2A::Errors::AuthenticationRequired, "Authentication required for extended agent card" unless context.authenticated? # Generate extended agent card with authentication context card = generate_extended_agent_card(context) { agent_card: card.to_h } end |
#handle_agent_get_card(_params, context) ⇒ Hash
Handle agent/getCard method
394 395 396 397 398 399 400 401 |
# File 'lib/a2a/server/a2a_methods.rb', line 394 def handle_agent_get_card(_params, context) # Generate agent card from registered capabilities card = generate_agent_card(context) { agent_card: card.to_h } end |
#handle_message_send(params, context) ⇒ Hash
Handle message/send method
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/a2a/server/a2a_methods.rb', line 84 def (params, context) validate_required_params(params, %w[message]) = params["message"] blocking = params.fetch("blocking", true) # Parse message = A2A::Types::Message.from_h() # Create task for message processing task = task_manager.create_task( type: "message_processing", params: { message: .to_h, blocking: blocking }, context_id: .context_id, metadata: { message_id: ., role: .role } ) # Process message (delegate to subclass implementation) if blocking # Update task to working state first task_manager.update_task_status( task.id, A2A::Types::TaskStatus.new( state: A2A::Types::TASK_STATE_WORKING, message: "Processing message", updated_at: Time.now.utc.iso8601 ) ) # Synchronous processing result = (, task, context) # Update task with result task_manager.update_task_status( task.id, A2A::Types::TaskStatus.new( state: A2A::Types::TASK_STATE_COMPLETED, result: result, updated_at: Time.now.utc.iso8601 ) ) { task_id: task.id, context_id: task.context_id, result: result } else # Asynchronous processing (, task, context) { task_id: task.id, context_id: task.context_id, status: task.status.to_h } end end |
#handle_message_stream(params, context) ⇒ Enumerator
Handle message/stream method
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/a2a/server/a2a_methods.rb', line 152 def (params, context) validate_required_params(params, %w[message]) = params["message"] = A2A::Types::Message.from_h() # Create task for streaming message processing task = task_manager.create_task( type: "message_streaming", params: { message: .to_h }, context_id: .context_id, metadata: { message_id: ., role: .role, streaming: true } ) # Return enumerator for streaming responses Enumerator.new do |yielder| # Update task to working state task_manager.update_task_status( task.id, A2A::Types::TaskStatus.new( state: A2A::Types::TASK_STATE_WORKING, message: "Processing message stream", updated_at: Time.now.utc.iso8601 ) ) # Process message stream (delegate to subclass implementation) (, task, context) do |response| yielder << { task_id: task.id, context_id: task.context_id, response: response } end # Mark task as completed task_manager.update_task_status( task.id, A2A::Types::TaskStatus.new( state: A2A::Types::TASK_STATE_COMPLETED, message: "Stream completed", updated_at: Time.now.utc.iso8601 ) ) rescue StandardError => e # Mark task as failed task_manager.update_task_status( task.id, A2A::Types::TaskStatus.new( state: A2A::Types::TASK_STATE_FAILED, error: { message: e., type: e.class.name }, updated_at: Time.now.utc.iso8601 ) ) raise end end |
#handle_push_notification_config_delete(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/delete method
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/a2a/server/a2a_methods.rb', line 371 def handle_push_notification_config_delete(params, _context) validate_required_params(params, %w[taskId configId]) task_id = params["taskId"] config_id = params["configId"] deleted = push_notification_manager.delete_push_notification_config(task_id, config_id) raise A2A::Errors::NotFound, "Push notification config not found" unless deleted { task_id: task_id, config_id: config_id, deleted: true } end |
#handle_push_notification_config_get(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/get method
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/a2a/server/a2a_methods.rb', line 327 def handle_push_notification_config_get(params, _context) validate_required_params(params, %w[taskId]) task_id = params["taskId"] config_id = params["configId"] config = push_notification_manager.get_push_notification_config( task_id, config_id: config_id ) raise A2A::Errors::NotFound, "Push notification config not found" unless config { task_id: task_id, config: config.push_notification_config.to_h } end |
#handle_push_notification_config_list(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/list method
352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/a2a/server/a2a_methods.rb', line 352 def handle_push_notification_config_list(params, _context) validate_required_params(params, %w[taskId]) task_id = params["taskId"] configs = push_notification_manager.list_push_notification_configs(task_id) { task_id: task_id, configs: configs.map { |config| config.push_notification_config.to_h } } end |
#handle_push_notification_config_set(params, _context) ⇒ Hash
Handle tasks/pushNotificationConfig/set method
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/a2a/server/a2a_methods.rb', line 303 def handle_push_notification_config_set(params, _context) validate_required_params(params, %w[taskId config]) task_id = params["taskId"] config_data = params["config"] # Verify task exists task_manager.get_task(task_id) # Create push notification config config = push_notification_manager.set_push_notification_config(task_id, config_data) { task_id: task_id, config: config.push_notification_config.to_h } end |
#handle_tasks_cancel(params, _context) ⇒ Hash
Handle tasks/cancel method
239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/a2a/server/a2a_methods.rb', line 239 def handle_tasks_cancel(params, _context) validate_required_params(params, %w[id]) task_id = params["id"] reason = params["reason"] task = task_manager.cancel_task(task_id, reason: reason) { task_id: task.id, status: task.status.to_h } end |
#handle_tasks_get(params, _context) ⇒ Hash
Handle tasks/get method
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/a2a/server/a2a_methods.rb', line 220 def handle_tasks_get(params, _context) validate_required_params(params, %w[id]) task_id = params["id"] history_length = params["historyLength"] task = task_manager.get_task(task_id, history_length: history_length) { task: task.to_h } end |
#handle_tasks_resubscribe(params, _context) ⇒ Enumerator
Handle tasks/resubscribe method
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/a2a/server/a2a_methods.rb', line 259 def handle_tasks_resubscribe(params, _context) validate_required_params(params, %w[id]) task_id = params["id"] # Verify task exists task = task_manager.get_task(task_id) # Return enumerator for task update stream Enumerator.new do |yielder| # Register for task updates client_id = push_notification_manager.register_sse_client(task_id, yielder) begin # Send current task state immediately yielder << { event_type: "task_status_update", event_data: { task_id: task.id, context_id: task.context_id, status: task.status.to_h } } # Keep connection alive until client disconnects # The actual updates will be sent via the push notification manager loop do sleep 1 # Check if client is still connected (implementation specific) break unless client_connected?(yielder) end ensure # Unregister client when done push_notification_manager.unregister_sse_client(task_id, client_id) end end end |
#process_message_async(message, task, context) ⇒ void (protected)
This method returns an undefined value.
Process message asynchronously (to be implemented by subclasses)
457 458 459 |
# File 'lib/a2a/server/a2a_methods.rb', line 457 def (, task, context) raise NotImplementedError, "Subclasses must implement process_message_async" end |
#process_message_stream(message, task, context) {|response| ... } ⇒ void (protected)
This method returns an undefined value.
Process message stream (to be implemented by subclasses)
469 470 471 |
# File 'lib/a2a/server/a2a_methods.rb', line 469 def (, task, context) raise NotImplementedError, "Subclasses must implement process_message_stream" end |
#process_message_sync(message, task, context) ⇒ Object (protected)
Process message synchronously (to be implemented by subclasses)
446 447 448 |
# File 'lib/a2a/server/a2a_methods.rb', line 446 def (, task, context) raise NotImplementedError, "Subclasses must implement process_message_sync" end |
#push_notification_manager ⇒ A2A::Server::PushNotificationManager
Get push notification manager instance
433 434 435 |
# File 'lib/a2a/server/a2a_methods.rb', line 433 def push_notification_manager @push_notification_manager ||= A2A::Server::PushNotificationManager.new end |
#task_manager ⇒ A2A::Server::TaskManager
Get task manager instance
425 426 427 |
# File 'lib/a2a/server/a2a_methods.rb', line 425 def task_manager @task_manager ||= A2A::Server::TaskManager.new end |
#validate_required_params(params, required) ⇒ Object (private)
Validate required parameters
500 501 502 503 504 505 506 |
# File 'lib/a2a/server/a2a_methods.rb', line 500 def validate_required_params(params, required) missing = required.reject { |param| params.key?(param) } return if missing.empty? raise A2A::Errors::InvalidParams, "Missing required parameters: #{missing.join(', ')}" end |