Class: RubyCoded::Chat::LLMBridge

Inherits:
Object
  • Object
show all
Includes:
BridgeCommon, ChatConfiguration, 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/chat_configuration.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: ChatConfiguration, PlanMode, StreamingRetries, ToolCallHandling

Constant Summary collapse

MAX_RATE_LIMIT_RETRIES =
2
RATE_LIMIT_BASE_DELAY =
2
MAX_WRITE_TOOL_ROUNDS =
Tools::ExecutionPolicy::MAX_WRITE_TOOL_ROUNDS
MAX_TOTAL_TOOL_ROUNDS =
Tools::ExecutionPolicy::MAX_TOTAL_TOOL_ROUNDS
MAX_TOOL_RESULT_CHARS =
10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BridgeCommon

included

Constructor Details

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

Returns a new instance of LLMBridge.



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

def initialize(state, project_root: Dir.pwd, skill_catalog: nil)
  @state = state
  @chat_mutex = Mutex.new
  @cancel_requested = false
  @project_root = project_root
  @skill_catalog = skill_catalog || RubyCoded::Skills::Catalog.new(project_root: @project_root)
  @mode = RuntimeMode.chat
  setup_agent_pipeline!
  reset_chat!(@state.model)
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



36
37
38
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 36

def mode
  @mode
end

#project_rootObject (readonly)

Returns the value of attribute project_root.



36
37
38
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 36

def project_root
  @project_root
end

Instance Method Details

#reset_agent_session!Object



56
57
58
59
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 56

def reset_agent_session!
  @policy.reset_counters!
  reset_chat!(@state.model)
end

#reset_chat!(model_name) ⇒ Object



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

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



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_coded/chat/llm_bridge.rb', line 61

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 @mode.plan? && !@cancel_requested
  ensure
    @state.streaming = false
  end
end