Class: A2A::Agent

Inherits:
Object
  • Object
show all
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 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
    respond_with -> (env) {
      A2A::Schema["Send Message Response"].new({})
    }
  end

  on "GetTask" do
    respond_with -> (env) {
      task = Task.find_by(id: env["a2a.request"].id)
      raise A2A::TaskNotFoundError.new(env["a2a.request"].id) unless task
      task.to_a2a
    }
  end
end

server.register(agent)

Defined Under Namespace

Classes: Handler, StackBuilder, Terminal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Agent

Returns a new instance of Agent.



34
35
36
37
# File 'lib/a2a/agent.rb', line 34

def initialize(&block)
  @handlers = []
  instance_eval(&block) if block
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



32
33
34
# File 'lib/a2a/agent.rb', line 32

def handlers
  @handlers
end

Instance Method Details

#on(*operations, &block) ⇒ Object

Define a handler stack for one or more A2A operations.

The block is evaluated at definition time to build the middleware stack. Use ‘use` to add middleware, `respond_with` to set the terminal handler.

on "SendMessage" do
  use SomeMiddleware
  respond_with -> (env) { ... }
end

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/a2a/agent.rb', line 50

def on(*operations, &block)
  raise ArgumentError, "on requires at least one operation" if operations.empty?
  raise ArgumentError, "on requires a block" unless block

  builder = StackBuilder.new
  builder.instance_eval(&block)

  handler = Handler.new(
    operations: operations.flatten,
    app:        builder.to_app
  )

  @handlers << handler
  handler
end