Class: RubyCoded::Chat::LLMBridge

Inherits:
Object
  • Object
show all
Includes:
PlanMode, StreamingRetries, ToolCallHandling
Defined in:
lib/ruby_coded/chat/llm_bridge.rb,
lib/ruby_coded/chat/llm_bridge/plan_mode.rb,
lib/ruby_coded/chat/llm_bridge/streaming_retries.rb,
lib/ruby_coded/chat/llm_bridge/tool_call_handling.rb

Overview

Sends prompts to RubyLLM and streams assistant output into State.

Defined Under Namespace

Modules: PlanMode, StreamingRetries, ToolCallHandling

Constant Summary collapse

MAX_RATE_LIMIT_RETRIES =
2
RATE_LIMIT_BASE_DELAY =
2
MAX_WRITE_TOOL_ROUNDS =
50
MAX_TOTAL_TOOL_ROUNDS =
200
TOOL_ROUNDS_WARNING_THRESHOLD =
0.8
MAX_TOOL_RESULT_CHARS =
10_000

Constants included from PlanMode

PlanMode::IMPLEMENTATION_PATTERNS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, project_root: Dir.pwd) ⇒ LLMBridge

Returns a new instance of LLMBridge.



31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 31

def initialize(state, project_root: Dir.pwd)
  @state = state
  @chat_mutex = Mutex.new
  @cancel_requested = false
  @project_root = project_root
  @agentic_mode = false
  @plan_mode = false
  @tool_registry = Tools::Registry.new(project_root: @project_root)
  reset_chat!(@state.model)
end

Instance Attribute Details

#agentic_modeObject (readonly)

Returns the value of attribute agentic_mode.



29
30
31
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 29

def agentic_mode
  @agentic_mode
end

#plan_modeObject (readonly)

Returns the value of attribute plan_mode.



29
30
31
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 29

def plan_mode
  @plan_mode
end

#project_rootObject (readonly)

Returns the value of attribute project_root.



29
30
31
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 29

def project_root
  @project_root
end

Instance Method Details

#approve_all_tools!Object



98
99
100
101
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 98

def approve_all_tools!
  @state.enable_auto_approve!
  @state.tool_confirmation_response = :approved
end

#approve_tool!Object



94
95
96
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 94

def approve_tool!
  @state.tool_confirmation_response = :approved
end

#cancel!Object



89
90
91
92
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 89

def cancel!
  @cancel_requested = true
  @state.mutex.synchronize { @state.tool_cv.signal }
end

#reject_tool!Object



103
104
105
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 103

def reject_tool!
  @state.tool_confirmation_response = :rejected
end

#reset_agent_session!Object



60
61
62
63
64
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 60

def reset_agent_session!
  @tool_call_count = 0
  @write_tool_call_count = 0
  reset_chat!(@state.model)
end

#reset_chat!(model_name) ⇒ Object



42
43
44
45
46
47
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 42

def reset_chat!(model_name)
  @chat_mutex.synchronize do
    @chat = RubyLLM.chat(model: model_name)
    apply_mode_config!(@chat)
  end
end

#send_async(input) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 76

def send_async(input)
  auto_switch_to_agent! if should_auto_switch_to_agent?(input)
  reset_call_counts
  chat = prepare_streaming
  Thread.new do
    response = attempt_with_retries(chat, input)
    update_response_tokens(response)
    post_process_plan_response if @plan_mode && !@cancel_requested
  ensure
    @state.streaming = false
  end
end

#toggle_agentic_mode!(enabled) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 49

def toggle_agentic_mode!(enabled)
  @agentic_mode = enabled
  @state.agentic_mode = enabled
  if enabled && @plan_mode
    @plan_mode = false
    @state.deactivate_plan_mode!
  end
  @state.disable_auto_approve! unless enabled
  reconfigure_chat!
end

#toggle_plan_mode!(enabled) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 66

def toggle_plan_mode!(enabled)
  @plan_mode = enabled
  if enabled && @agentic_mode
    @agentic_mode = false
    @state.agentic_mode = false
    @state.disable_auto_approve!
  end
  reconfigure_chat!
end