Class: A2A::Client::Base
- Inherits:
-
Object
- Object
- A2A::Client::Base
- Defined in:
- lib/a2a/client/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#consumers ⇒ Object
readonly
Returns the value of attribute consumers.
-
#middleware ⇒ Object
readonly
Returns the value of attribute middleware.
Instance Method Summary collapse
-
#add_consumer(consumer) ⇒ void
Add an event consumer.
-
#add_middleware(interceptor) ⇒ void
Add middleware to the client.
-
#agent_supports_transport?(agent_card, transport) ⇒ Boolean
private
Check if an agent supports a specific transport.
-
#cancel_task(task_id, context: nil) ⇒ Task
Cancel a task.
-
#delete_task_callback(task_id, push_notification_config_id, context: nil) ⇒ void
Delete a callback configuration for a task.
-
#ensure_agent_card(agent_card) ⇒ AgentCard
protected
Convert an agent card hash or object to an AgentCard instance.
-
#ensure_message(message) ⇒ Message
protected
Convert a message hash or object to a Message instance.
-
#ensure_task(task) ⇒ Task
protected
Convert a task hash or object to a Task instance.
-
#execute_with_middleware(request, context = {}) {|request, context| ... } ⇒ Object
protected
Execute middleware chain for a request.
-
#get_card(context: nil, authenticated: false) ⇒ AgentCard
Get the agent card.
-
#get_endpoint_url(agent_card, transport) ⇒ String
Get the endpoint URL for a specific transport.
-
#get_task(task_id, context: nil, history_length: nil) ⇒ Task
Get a task by ID.
-
#get_task_callback(task_id, push_notification_config_id, context: nil) ⇒ TaskPushNotificationConfig
Get the callback configuration for a task.
-
#initialize(config: nil, middleware: [], consumers: []) ⇒ Base
constructor
Initialize a new client.
-
#list_task_callbacks(task_id, context: nil) ⇒ Array<TaskPushNotificationConfig>
List all callback configurations for a task.
-
#negotiate_transport(agent_card) ⇒ String
Negotiate transport with agent card.
-
#polling? ⇒ Boolean
Check if the client supports polling.
-
#process_event(event) ⇒ void
protected
Process events with registered consumers.
-
#remove_consumer(consumer) ⇒ void
Remove an event consumer.
-
#remove_middleware(interceptor) ⇒ void
Remove middleware from the client.
-
#resubscribe(task_id, context: nil) ⇒ Enumerator
Resubscribe to a task for streaming updates.
-
#send_message(message, context: nil) ⇒ Enumerator, Message
Send a message to the agent.
-
#set_task_callback(task_id, push_notification_config, context: nil) ⇒ void
Set a callback for task updates.
-
#streaming? ⇒ Boolean
Check if the client supports streaming.
-
#supported_transports ⇒ Array<String>
Get the supported transports.
Constructor Details
#initialize(config: nil, middleware: [], consumers: []) ⇒ Base
Initialize a new client
22 23 24 25 26 27 |
# File 'lib/a2a/client/base.rb', line 22 def initialize(config: nil, middleware: [], consumers: []) @config = config || Config.new @middleware = middleware.dup @consumers = consumers.dup @task_callbacks = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/a2a/client/base.rb', line 14 def config @config end |
#consumers ⇒ Object (readonly)
Returns the value of attribute consumers.
14 15 16 |
# File 'lib/a2a/client/base.rb', line 14 def consumers @consumers end |
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
14 15 16 |
# File 'lib/a2a/client/base.rb', line 14 def middleware @middleware end |
Instance Method Details
#add_consumer(consumer) ⇒ void
This method returns an undefined value.
Add an event consumer
155 156 157 |
# File 'lib/a2a/client/base.rb', line 155 def add_consumer(consumer) @consumers << consumer end |
#add_middleware(interceptor) ⇒ void
This method returns an undefined value.
Add middleware to the client
137 138 139 |
# File 'lib/a2a/client/base.rb', line 137 def add_middleware(interceptor) @middleware << interceptor end |
#agent_supports_transport?(agent_card, transport) ⇒ Boolean (private)
Check if an agent supports a specific transport
309 310 311 312 313 |
# File 'lib/a2a/client/base.rb', line 309 def agent_supports_transport?(agent_card, transport) return true if agent_card.preferred_transport == transport agent_card.additional_interfaces&.any? { |iface| iface.transport == transport } end |
#cancel_task(task_id, context: nil) ⇒ Task
Cancel a task
59 60 61 |
# File 'lib/a2a/client/base.rb', line 59 def cancel_task(task_id, context: nil) raise NotImplementedError, "#{self.class}#cancel_task must be implemented" end |
#delete_task_callback(task_id, push_notification_config_id, context: nil) ⇒ void
This method returns an undefined value.
Delete a callback configuration for a task
128 129 130 |
# File 'lib/a2a/client/base.rb', line 128 def delete_task_callback(task_id, push_notification_config_id, context: nil) raise NotImplementedError, "#{self.class}#delete_task_callback must be implemented" end |
#ensure_agent_card(agent_card) ⇒ AgentCard (protected)
Convert an agent card hash or object to an AgentCard instance
295 296 297 298 299 |
# File 'lib/a2a/client/base.rb', line 295 def ensure_agent_card(agent_card) return agent_card if agent_card.is_a?(A2A::Types::AgentCard) A2A::Types::AgentCard.from_h(agent_card) end |
#ensure_message(message) ⇒ Message (protected)
Convert a message hash or object to a Message instance
273 274 275 276 277 |
# File 'lib/a2a/client/base.rb', line 273 def () return if .is_a?(A2A::Types::Message) A2A::Types::Message.from_h() end |
#ensure_task(task) ⇒ Task (protected)
Convert a task hash or object to a Task instance
284 285 286 287 288 |
# File 'lib/a2a/client/base.rb', line 284 def ensure_task(task) return task if task.is_a?(A2A::Types::Task) A2A::Types::Task.from_h(task) end |
#execute_with_middleware(request, context = {}) {|request, context| ... } ⇒ Object (protected)
Execute middleware chain for a request
244 245 246 247 248 249 250 251 252 |
# File 'lib/a2a/client/base.rb', line 244 def execute_with_middleware(request, context = {}, &block) # Create a chain of middleware calls chain = @middleware.reverse.reduce(proc(&block)) do |next_call, middleware| proc { |req, ctx| middleware.call(req, ctx, next_call) } end # Execute the chain chain.call(request, context) end |
#get_card(context: nil, authenticated: false) ⇒ AgentCard
Get the agent card
70 71 72 |
# File 'lib/a2a/client/base.rb', line 70 def get_card(context: nil, authenticated: false) raise NotImplementedError, "#{self.class}#get_card must be implemented" end |
#get_endpoint_url(agent_card, transport) ⇒ String
Get the endpoint URL for a specific transport
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/a2a/client/base.rb', line 223 def get_endpoint_url(agent_card, transport) # Check if the transport matches the preferred transport return agent_card.url if agent_card.preferred_transport == transport # Look for the transport in additional interfaces interface = agent_card.additional_interfaces&.find { |iface| iface.transport == transport } return interface.url if interface # Fallback to main URL if no specific interface found agent_card.url end |
#get_task(task_id, context: nil, history_length: nil) ⇒ Task
Get a task by ID
48 49 50 |
# File 'lib/a2a/client/base.rb', line 48 def get_task(task_id, context: nil, history_length: nil) raise NotImplementedError, "#{self.class}#get_task must be implemented" end |
#get_task_callback(task_id, push_notification_config_id, context: nil) ⇒ TaskPushNotificationConfig
Get the callback configuration for a task
105 106 107 |
# File 'lib/a2a/client/base.rb', line 105 def get_task_callback(task_id, push_notification_config_id, context: nil) raise NotImplementedError, "#{self.class}#get_task_callback must be implemented" end |
#list_task_callbacks(task_id, context: nil) ⇒ Array<TaskPushNotificationConfig>
List all callback configurations for a task
116 117 118 |
# File 'lib/a2a/client/base.rb', line 116 def list_task_callbacks(task_id, context: nil) raise NotImplementedError, "#{self.class}#list_task_callbacks must be implemented" end |
#negotiate_transport(agent_card) ⇒ String
Negotiate transport with agent card
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/a2a/client/base.rb', line 197 def negotiate_transport(agent_card) # Use client preference if enabled if @config.use_client_preference? preferred = @config.preferred_transport return preferred if agent_supports_transport?(agent_card, preferred) end # Find first mutually supported transport @config.supported_transports.each do |transport| return transport if agent_supports_transport?(agent_card, transport) end # Fallback to agent's preferred transport if we support it agent_preferred = agent_card.preferred_transport return agent_preferred if @config.supports_transport?(agent_preferred) # No compatible transport found raise A2A::Errors::ClientError, "No compatible transport protocol found" end |
#polling? ⇒ Boolean
Check if the client supports polling
180 181 182 |
# File 'lib/a2a/client/base.rb', line 180 def polling? @config.polling? end |
#process_event(event) ⇒ void (protected)
This method returns an undefined value.
Process events with registered consumers
259 260 261 262 263 264 265 266 |
# File 'lib/a2a/client/base.rb', line 259 def process_event(event) @consumers.each do |consumer| consumer.call(event) rescue StandardError => e # Log error but don't fail the entire processing warn "Error in event consumer: #{e.message}" end end |
#remove_consumer(consumer) ⇒ void
This method returns an undefined value.
Remove an event consumer
164 165 166 |
# File 'lib/a2a/client/base.rb', line 164 def remove_consumer(consumer) @consumers.delete(consumer) end |
#remove_middleware(interceptor) ⇒ void
This method returns an undefined value.
Remove middleware from the client
146 147 148 |
# File 'lib/a2a/client/base.rb', line 146 def remove_middleware(interceptor) @middleware.delete(interceptor) end |
#resubscribe(task_id, context: nil) ⇒ Enumerator
Resubscribe to a task for streaming updates
81 82 83 |
# File 'lib/a2a/client/base.rb', line 81 def resubscribe(task_id, context: nil) raise NotImplementedError, "#{self.class}#resubscribe must be implemented" end |
#send_message(message, context: nil) ⇒ Enumerator, Message
Send a message to the agent
36 37 38 |
# File 'lib/a2a/client/base.rb', line 36 def (, context: nil) raise NotImplementedError, "#{self.class}#send_message must be implemented" end |
#set_task_callback(task_id, push_notification_config, context: nil) ⇒ void
This method returns an undefined value.
Set a callback for task updates
93 94 95 |
# File 'lib/a2a/client/base.rb', line 93 def set_task_callback(task_id, push_notification_config, context: nil) raise NotImplementedError, "#{self.class}#set_task_callback must be implemented" end |
#streaming? ⇒ Boolean
Check if the client supports streaming
172 173 174 |
# File 'lib/a2a/client/base.rb', line 172 def streaming? @config.streaming? end |
#supported_transports ⇒ Array<String>
Get the supported transports
188 189 190 |
# File 'lib/a2a/client/base.rb', line 188 def supported_transports @config.supported_transports end |