Module: RubyLLM::Agents::Routing::ClassMethods

Defined in:
lib/ruby_llm/agents/routing/class_methods.rb

Overview

Class-level DSL for defining routes and classification categories.

Extended into any BaseAgent subclass that includes the Routing concern. Provides ‘route`, `default_route`, and accessor methods for route definitions.

Examples:

class SupportRouter < RubyLLM::Agents::BaseAgent
  include RubyLLM::Agents::Routing

  route :billing,  "Billing, charges, refunds"
  route :technical, "Bugs, errors, crashes"
  default_route :general
end

Instance Method Summary collapse

Instance Method Details

#agent_typeSymbol

Returns :router for instrumentation/tracking.

Returns:

  • (Symbol)


73
74
75
# File 'lib/ruby_llm/agents/routing/class_methods.rb', line 73

def agent_type
  :router
end

#call(message: nil, **kwargs, &block) ⇒ RoutingResult

Override call to accept message: as a named param.

Parameters:

  • message (String, nil) (defaults to: nil)

    The message to classify

  • kwargs (Hash)

    Additional options

Returns:



82
83
84
85
86
87
88
# File 'lib/ruby_llm/agents/routing/class_methods.rb', line 82

def call(message: nil, **kwargs, &block)
  if message
    super(**kwargs.merge(message: message), &block)
  else
    super(**kwargs, &block)
  end
end

#default_route(name, agent: nil) ⇒ Object

Set the default route for unmatched classifications.

Parameters:

  • name (Symbol)

    Default route name

  • agent (Class, nil) (defaults to: nil)

    Optional default agent class



46
47
48
49
50
51
52
53
# File 'lib/ruby_llm/agents/routing/class_methods.rb', line 46

def default_route(name, agent: nil)
  @default_route_name = name.to_sym
  @routes ||= {}
  @routes[name.to_sym] ||= {
    description: "Default / general category",
    agent: agent
  }
end

#default_route_nameSymbol

Returns the default route name (including inherited).

Returns:

  • (Symbol)

    The default route name



66
67
68
# File 'lib/ruby_llm/agents/routing/class_methods.rb', line 66

def default_route_name
  @default_route_name || (superclass.respond_to?(:default_route_name) ? superclass.default_route_name : :general)
end

#route(name, description, agent: nil) ⇒ Object

Define a classification route.

Examples:

Simple route

route :billing, "Billing, charges, refunds"

Route with agent mapping

route :billing, "Billing questions", agent: BillingAgent

Parameters:

  • name (Symbol)

    Route identifier

  • description (String)

    What messages belong to this route

  • agent (Class, nil) (defaults to: nil)

    Optional agent class to map to this route



33
34
35
36
37
38
39
# File 'lib/ruby_llm/agents/routing/class_methods.rb', line 33

def route(name, description, agent: nil)
  @routes ||= {}
  @routes[name.to_sym] = {
    description: description,
    agent: agent
  }
end

#routesHash{Symbol => Hash}

Returns all defined routes (including inherited).

Returns:

  • (Hash{Symbol => Hash})

    Route definitions



58
59
60
61
# File 'lib/ruby_llm/agents/routing/class_methods.rb', line 58

def routes
  parent = superclass.respond_to?(:routes) ? superclass.routes : {}
  parent.merge(@routes || {})
end