Class: Rubino::Agent::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/agent/router.rb

Overview

Routes user input to the appropriate agent. Handles @mention syntax for subagent invocation and agent switching.

Constant Summary collapse

MENTION_REGEX =
/\A@(\w+)\s+(.+)/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry:, ui:) ⇒ Router

Returns a new instance of Router.



10
11
12
13
14
# File 'lib/rubino/agent/router.rb', line 10

def initialize(registry:, ui:)
  @registry = registry
  @ui = ui
  @current_agent = registry.default
end

Instance Attribute Details

#current_agentObject (readonly)

Returns the value of attribute current_agent.



16
17
18
# File 'lib/rubino/agent/router.rb', line 16

def current_agent
  @current_agent
end

Instance Method Details

#available_mentionsObject

Returns available agent names for autocomplete



55
56
57
# File 'lib/rubino/agent/router.rb', line 55

def available_mentions
  @registry.subagents.map { |a| "@#{a.name}" }
end

#route(input) ⇒ Object

Routes input, returning [agent_definition, cleaned_input]



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubino/agent/router.rb', line 37

def route(input)
  # Check for @mention
  if input.match?(MENTION_REGEX)
    match = input.match(MENTION_REGEX)
    agent_name = match[1]
    actual_input = match[2]

    agent = @registry.find(agent_name)
    return [agent, actual_input] if agent && (agent.subagent? || agent.primary?)

    @ui.warning("Unknown agent '#{agent_name}', using current agent")

  end

  [@current_agent, input]
end

#switch_to(agent_name) ⇒ Object

Switches to a different primary agent



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubino/agent/router.rb', line 19

def switch_to(agent_name)
  agent = @registry.find(agent_name)
  unless agent
    @ui.error("unknown agent: #{agent_name}")
    return false
  end

  unless agent.primary?
    @ui.error("cannot switch to subagent '#{agent_name}'. Use @#{agent_name} to invoke it.")
    return false
  end

  @current_agent = agent
  @ui.info("Switched to agent: #{agent.name}")
  true
end

#switchable_agentsObject

Returns primary agent names for switching



60
61
62
# File 'lib/rubino/agent/router.rb', line 60

def switchable_agents
  @registry.primary_agents.map(&:name)
end