Class: LlmGateway::Agents::Harness

Inherits:
Prompt show all
Defined in:
lib/llm_gateway/agents/harness.rb

Constant Summary collapse

COMPACTION_TOKEN_THRESHOLD =
180_000
COMPACTION_IDLE_THRESHOLD_SECONDS =
60 * 60

Instance Attribute Summary collapse

Attributes inherited from Prompt

#cache_key, #cache_retention

Instance Method Summary collapse

Methods inherited from Prompt

after_execute, before_execute, find_tool, #stream, #system_prompt, tools, #tools

Constructor Details

#initialize(session_manager, provider:, model: nil, reasoning: "high") ⇒ Harness

Returns a new instance of Harness.



15
16
17
18
19
20
21
22
# File 'lib/llm_gateway/agents/harness.rb', line 15

def initialize(session_manager, provider:, model: nil, reasoning: "high")
  @provider = provider
  super(provider: provider, model: model, reasoning: reasoning)
  @session_manager = session_manager
  sync_initial_configuration_events
  self.default_queue_mode = :next_turn
  self.queue_drain_mode = :all
end

Instance Attribute Details

#default_queue_modeObject

Returns the value of attribute default_queue_mode.



12
13
14
# File 'lib/llm_gateway/agents/harness.rb', line 12

def default_queue_mode
  @default_queue_mode
end

#modelObject

Returns the value of attribute model.



12
13
14
# File 'lib/llm_gateway/agents/harness.rb', line 12

def model
  @model
end

#providerObject

Returns the value of attribute provider.



11
12
13
# File 'lib/llm_gateway/agents/harness.rb', line 11

def provider
  @provider
end

#queue_drain_modeObject

Returns the value of attribute queue_drain_mode.



12
13
14
# File 'lib/llm_gateway/agents/harness.rb', line 12

def queue_drain_mode
  @queue_drain_mode
end

#reasoningObject

Returns the value of attribute reasoning.



12
13
14
# File 'lib/llm_gateway/agents/harness.rb', line 12

def reasoning
  @reasoning
end

#session_managerObject (readonly)

Returns the value of attribute session_manager.



12
13
14
# File 'lib/llm_gateway/agents/harness.rb', line 12

def session_manager
  @session_manager
end

Instance Method Details

#compactObject



69
70
71
# File 'lib/llm_gateway/agents/harness.rb', line 69

def compact
  session_manager.compaction(provider)
end

#follow_up_message(message, &block) ⇒ Object



37
38
39
# File 'lib/llm_gateway/agents/harness.rb', line 37

def follow_up_message(message, &block)
  enqueue_or_run_message(message, :follow_up, &block)
end

#next_turn_message(message, &block) ⇒ Object



41
42
43
# File 'lib/llm_gateway/agents/harness.rb', line 41

def next_turn_message(message, &block)
  enqueue_or_run_message(message, :next_turn, &block)
end

#prompt_message(message, &block) ⇒ Object



29
30
31
# File 'lib/llm_gateway/agents/harness.rb', line 29

def prompt_message(message, &block)
  enqueue_or_run_message(message, default_queue_mode, &block)
end

#run(&block) ⇒ Object Also known as: continue



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/llm_gateway/agents/harness.rb', line 73

def run(&block)
  emit(Event::AgentStart.new, &block)
  drain_queue(:steer)
  emit(Event::TurnStart.new, &block)
  emit(Event::MessageStart.new, &block)

  assistant_message = stream do |event|
    emit(Event::MessageUpdate.new(stream_event: event), &block)
  end

  session_manager.push_message(assistant_message.to_h)
  emit(Event::MessageEnd.new(message: assistant_message), &block)

  tool_results = tool_requests(assistant_message).map do |message|
    parameters = message.to_h
    emit(Event::ToolExecutionStart.new(parameters: parameters), &block)
    tool_result = find_and_execute_tool(message)
    emit(Event::ToolExecutionEnd.new(parameters: parameters, result: tool_result), &block)
    tool_result
  end

  tool_result_content = tool_results.map(&:to_h)
  session_manager.push_message(
    role: "user",
    content: tool_result_content,
  ) unless tool_result_content.empty?

  turn_end_event = Event::TurnEnd.new(message: assistant_message, tool_results: tool_results)
  emit(turn_end_event, &block)

  if tool_results.length.positive?
    return run(&block)
  end

  if session_manager.queued_messages?(:follow_up)
    compact_if_needed
    return run(&block) if drain_queue(:follow_up).any?
  end

  emit(Event::AgentEnd.new(messages: []), &block)
  assistant_message
end

#steer_message(message, &block) ⇒ Object



33
34
35
# File 'lib/llm_gateway/agents/harness.rb', line 33

def steer_message(message, &block)
  enqueue_or_run_message(message, :steer, &block)
end

#transcriptObject Also known as: prompt



24
25
26
# File 'lib/llm_gateway/agents/harness.rb', line 24

def transcript
  session_manager.build_model_input_messages
end