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 = :follow_up
  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



65
66
67
# File 'lib/llm_gateway/agents/harness.rb', line 65

def compact
  session_manager.compaction(provider)
end

#continue(&block) ⇒ Object

Raises:

  • (RuntimeError)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/llm_gateway/agents/harness.rb', line 110

def continue(&block)
  raise RuntimeError, "Cannot continue a busy agent" if session_manager.busy?

  session_manager.busy!
  begin
    drain_queue(:steer)
    drain_queue(:follow_up)
    run(&block)
  ensure
    session_manager.idle!
  end
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_and_continue_if_idle(message, :follow_up, &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_and_continue_if_idle(message, default_queue_mode, &block)
end

#run(&block) ⇒ Object



69
70
71
72
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
# File 'lib/llm_gateway/agents/harness.rb', line 69

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

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

  tool_request_blocks = tool_requests(assistant_message)
  tool_result_message = execute_tool_requests(
    requests: tool_request_blocks,
    assistant_message: assistant_message,
    session_event: persisted_message
  ) do |requests_to_execute|
    run_tool_requests(requests_to_execute, session_event: persisted_message, &block)
  end
  tool_results = tool_result_message.tool_results

  session_manager.push_message(tool_result_message.to_h) if tool_result_message.any?

  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_and_continue_if_idle(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