Module: Legion::CLI::Chat::AgentDelegator

Defined in:
lib/legion/cli/chat/agent_delegator.rb

Class Method Summary collapse

Class Method Details

.build_agent_prompt(agent, task) ⇒ Object



66
67
68
69
70
71
# File 'lib/legion/cli/chat/agent_delegator.rb', line 66

def build_agent_prompt(agent, task)
  parts = []
  parts << agent[:system_prompt] if agent[:system_prompt]
  parts << "Task: #{task}"
  parts.join("\n\n")
end

.delegate?(input) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/legion/cli/chat/agent_delegator.rb', line 11

def delegate?(input)
  return :at_mention if input.match?(/\A@\w+\s/)
  return :slash if input.match?(%r{\A/agent\s+\w+\s})

  false
end

.dispatch(agent_name:, task:, session:, out:, chat_log: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/cli/chat/agent_delegator.rb', line 33

def dispatch(agent_name:, task:, session:, out:, chat_log: nil)
  require 'legion/cli/chat/agent_registry'
  agent = AgentRegistry.find(agent_name)
  unless agent
    out.error("Unknown agent: @#{agent_name}. Available: #{AgentRegistry.names.join(', ')}")
    return
  end

  chat_log&.info("agent_delegate name=#{agent_name} task_length=#{task.length}")

  require 'legion/cli/chat/subagent'
  prompt = build_agent_prompt(agent, task)

  result = Subagent.spawn(
    task:        prompt,
    model:       agent[:model],
    on_complete: lambda { |_id, res|
      output = res[:output] || res[:error] || 'No output'
      session.chat.add_message(
        role:    :user,
        content: "@#{agent_name} result:\n\n#{output}"
      )
      puts out.dim("\n  [@#{agent_name}] Complete. Results added to context.")
    }
  )

  if result[:error]
    out.error(result[:error])
  else
    out.success("Delegated to @#{agent_name} (#{result[:id]})")
  end
end

.parse(input) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/cli/chat/agent_delegator.rb', line 18

def parse(input)
  case delegate?(input)
  when :at_mention
    match = input.match(/\A@(\w+)\s+(.+)/m)
    return nil unless match

    { agent_name: match[1], task: match[2].strip }
  when :slash
    match = input.match(%r{\A/agent\s+(\w+)\s+(.+)}m)
    return nil unless match

    { agent_name: match[1], task: match[2].strip }
  end
end