Class: Chorus::Orchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/chorus/orchestrator.rb

Overview

Main entry point of Chorus. Ties together the Router, Context and Agents: for every incoming user message it picks an agent, builds that agent's context slice, calls it, and records the response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: Chorus::Client.new, router: Chorus::Router.new, context: Chorus::Context.new, agents: nil) ⇒ Orchestrator

Returns a new instance of Orchestrator.

Parameters:

  • client (Chorus::Client) (defaults to: Chorus::Client.new)

    API client shared by all agents

  • router (Chorus::Router) (defaults to: Chorus::Router.new)

    decides which agent handles a message

  • context (Chorus::Context) (defaults to: Chorus::Context.new)

    shared conversation state

  • agents (Hash{Symbol => Chorus::Agent}) (defaults to: nil)

    agent name => agent instance



20
21
22
23
24
25
# File 'lib/chorus/orchestrator.rb', line 20

def initialize(client: Chorus::Client.new, router: Chorus::Router.new, context: Chorus::Context.new, agents: nil)
  @client = client
  @router = router
  @context = context
  @agents = agents || default_agents
end

Instance Attribute Details

#contextChorus::Context (readonly)

Returns the shared conversation state.

Returns:



14
15
16
# File 'lib/chorus/orchestrator.rb', line 14

def context
  @context
end

Instance Method Details

#handle(user_message) ⇒ Hash

Returns {agent: Symbol, response: String}.

Parameters:

  • user_message (String)

    the incoming message to handle

Returns:

  • (Hash)

    {agent: Symbol, response: String}



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chorus/orchestrator.rb', line 29

def handle(user_message)
  agent_name = @router.route(user_message)
  agent = @agents.fetch(agent_name) { raise ArgumentError, "Unknown agent: #{agent_name}" }

  @context.add_message(role: :user, content: user_message)

  context_slice = @context.slice_for(agent_name, user_message)
  response = agent.call(context_slice)

  @context.add_message(role: :assistant, content: response, agent: agent_name)

  { agent: agent_name, response: response }
end