Class: Bristow::Functions::Delegate

Inherits:
Bristow::Function show all
Defined in:
lib/bristow/functions/delegate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Bristow::Function

#call, call, to_schema

Methods included from Delegate

included

Methods included from Sgetter

included

Constructor Details

#initialize(agent, agency) ⇒ Delegate

Returns a new instance of Delegate.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/bristow/functions/delegate.rb', line 37

def initialize(agent, agency)
  raise ArgumentError, "Agent must not be nil" if agent.nil?

  @agent = agent
  @agency = agency
  super()
end

Class Method Details

.descriptionObject



8
9
10
# File 'lib/bristow/functions/delegate.rb', line 8

def self.description
  "Delegate a task to a specialized agent"
end

.parametersObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bristow/functions/delegate.rb', line 12

def self.parameters
  {
    type: "object",
    properties: {
      agent_name: {
        type: "string",
        description: "The name of the agent to delegate to"
      },
      message: {
        type: "string",
        description: "The instructions for the agent being delegated to"
      }
    },
    required: ["agent_name", "message"]
  }
end

Instance Method Details

#agency=(agency) ⇒ Object



45
46
47
# File 'lib/bristow/functions/delegate.rb', line 45

def agency=(agency)
  @agency = agency
end

#descriptionObject



29
30
31
# File 'lib/bristow/functions/delegate.rb', line 29

def description
  self.class.description
end

#parametersObject



33
34
35
# File 'lib/bristow/functions/delegate.rb', line 33

def parameters
  self.class.parameters
end

#perform(agent_name:, message:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bristow/functions/delegate.rb', line 49

def perform(agent_name:, message:)
  raise "Agency not set" if @agency.nil?

  if agent_name == @agent.agent_name
    { error: "Cannot delegate to self" }
  else
    agent = @agency.find_agent(agent_name)
    raise ArgumentError, "Agent #{agent_name} not found" unless agent

    Bristow.configuration.logger.info("Delegating to #{agent_name}: #{message}")
    response = agent.chat([{ "role" => "user", "content" => message }])
    last_message = response&.last
    { response: last_message&.[]("content") }
  end
end