Class: A2A::Agent
- Inherits:
-
Object
- Object
- A2A::Agent
- Defined in:
- lib/a2a/agent.rb
Overview
DSL wrapper that collects operation handlers for an A2A agent.
An Agent produces handler objects that conform to the Dispatcher’s duck-type contract (#operations, #call). Register an agent on a Server the same way you would register a plain handler.
agent = A2A::Agent.new do
on "SendMessage" do |request|
respond A2A::Schema["Send Message Response"].new({})
end
on "GetTask" do |request|
task = store.get(request.id)
respond A2A::Schema["Task"].new(task.to_h)
end
end
server.register(agent)
Defined Under Namespace
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
Instance Method Summary collapse
-
#initialize(&block) ⇒ Agent
constructor
A new instance of Agent.
-
#on(*operations, &block) ⇒ Object
Register a handler block for one or more A2A operations.
Constructor Details
#initialize(&block) ⇒ Agent
Returns a new instance of Agent.
29 30 31 32 33 |
# File 'lib/a2a/agent.rb', line 29 def initialize(&block) @handlers = [] instance_eval(&block) if block end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
27 28 29 |
# File 'lib/a2a/agent.rb', line 27 def handlers @handlers end |
Instance Method Details
#on(*operations, &block) ⇒ Object
Register a handler block for one or more A2A operations.
Operations are identified by their proto name (e.g. “SendMessage”, “GetTask”, “CancelTask”). See A2A::Proto.operations for the full list.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/a2a/agent.rb', line 40 def on(*operations, &block) raise ArgumentError, "on requires at least one operation" if operations.empty? raise ArgumentError, "on requires a block" unless block handler = Handler.new( agent: self, operations: operations.flatten, block: block ) @handlers << handler handler end |