Class: Luo::AgentRunnerBase

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/luo/agent_runner_base.rb

Direct Known Subclasses

OpenAIAgentRunner, XinghuoAgentRunner

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#config, included

Constructor Details

#initialize(histories: nil) ⇒ AgentRunnerBase

Returns a new instance of AgentRunnerBase.



12
13
14
15
# File 'lib/luo/agent_runner_base.rb', line 12

def initialize(histories: nil)
  context.histories = histories unless histories.nil?
  on_init
end

Class Method Details

.agentsObject



81
82
83
# File 'lib/luo/agent_runner_base.rb', line 81

def agents
  @agents ||= {}
end

.disable_historyObject



105
106
107
108
109
# File 'lib/luo/agent_runner_base.rb', line 105

def disable_history
  define_method(:save_history?) do
    false
  end
end

.language_infoObject



95
96
97
98
99
100
101
102
103
# File 'lib/luo/agent_runner_base.rb', line 95

def language_info
  if self.config.language == "en"
    ""
  elsif self.config.language == "zh"
    "(must be a Chinese string)"
  else
    ""
  end
end

.register(agent) ⇒ Object



91
92
93
# File 'lib/luo/agent_runner_base.rb', line 91

def register(agent)
  agents[agent.agent_name] = agent
end

Instance Method Details

#add_agent(agent) ⇒ Object



65
66
67
# File 'lib/luo/agent_runner_base.rb', line 65

def add_agent(agent)
  context.have_running_agents << agent
end

#after_runObject



55
56
# File 'lib/luo/agent_runner_base.rb', line 55

def after_run
end

#call(user_input) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/luo/agent_runner_base.rb', line 36

def call(user_input)
  context.user_input = user_input
  on_request
  on_result
  on_run
  after_run
  rt = Marshal.load(Marshal.dump(context))
  reset_context
  rt
end

#clientObject



20
21
22
23
# File 'lib/luo/agent_runner_base.rb', line 20

def client
  raise Luo::ClientNotSetError, "client not set" if self.class.config.client.nil?
  self.class.config.client
end

#contextObject



25
26
27
# File 'lib/luo/agent_runner_base.rb', line 25

def context
  @context ||= config.context_adapter.call
end

#on_initObject



17
18
# File 'lib/luo/agent_runner_base.rb', line 17

def on_init
end

#on_requestObject

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/luo/agent_runner_base.rb', line 47

def on_request
  raise NotImplementedError, "call method must be implemented in subclass"
end

#on_resultObject

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/luo/agent_runner_base.rb', line 51

def on_result
  raise NotImplementedError, "call method must be implemented in subclass"
end

#on_runObject



58
59
60
61
62
63
# File 'lib/luo/agent_runner_base.rb', line 58

def on_run
  context.have_running_agents.each do |agent|
    context.agent_results << { agent_name: agent.class.agent_name, agent_response: agent.call }
  end
  context.have_running_agents.clear
end

#reset_contextObject



29
30
31
32
33
34
# File 'lib/luo/agent_runner_base.rb', line 29

def reset_context
  histories = context.histories
  @context = config.context_adapter.call
  @context.histories = histories
  @context
end

#save_historyObject



69
70
71
# File 'lib/luo/agent_runner_base.rb', line 69

def save_history
  context.histories.save(context.user_input, context.final_result) if save_history?
end