Class: A2A::Server::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/server/dispatcher.rb

Overview

Routes incoming A2A operations to their registered handler stacks.

Each operation maps to exactly one Rack app (a compiled middleware stack built by the Agent DSL). The Dispatcher is a terminal Rack app that reads env set by Triage, looks up the matching route, and calls it.

Returns domain objects (A2A::Schema::Definition or A2A::Error) —the binding layer is responsible for formatting these into HTTP.

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



20
21
22
# File 'lib/a2a/server/dispatcher.rb', line 20

def initialize
  @routes = {}
end

Instance Method Details

#call(env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/a2a/server/dispatcher.rb', line 37

def call(env)
  operation = env["a2a.operation"]
  app = @routes[operation]

  unless app
    raise A2A::UnsupportedOperationError.new(
      message: "Operation not supported: #{operation}"
    )
  end

  app.call(env)
rescue A2A::Error => e
  e
rescue => e
  Console.error(self, "Handler raised #{e.class}: #{e.message}", e)
  A2A::Error.new("Internal error", code: -32603, http_status: 500)
end

#handler_countObject



55
56
57
# File 'lib/a2a/server/dispatcher.rb', line 55

def handler_count
  @routes.size
end

#register(handler) ⇒ Object

Register a handler object.

The handler must respond to:

#operations -> Array(String)  (e.g. ["SendMessage", "GetTask"])
#call(env)  -> A2A::Schema::Definition | A2A::Error


30
31
32
33
34
35
# File 'lib/a2a/server/dispatcher.rb', line 30

def register(handler)
  handler.operations.each do |op|
    @routes[op] = handler
    Console.info(self) { "Registered #{handler.class.name} for #{op}" }
  end
end