Module: AIA::Adapter::ChatExecution

Included in:
RubyLLMAdapter
Defined in:
lib/aia/adapter/chat_execution.rb

Instance Method Summary collapse

Instance Method Details

#chat(prompt) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/aia/adapter/chat_execution.rb', line 7

def chat(prompt)
  if @models.size == 1
    single_model_chat(prompt, @models.first)
  else
    multi_model_chat(prompt)
  end
end

#clear_contextObject

Clear the chat context/history Needed for the /clear and /restore directives Simplified with ADR-002: Each model has isolated context, no global state to manage



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aia/adapter/chat_execution.rb', line 39

def clear_context
  old_chats = @chats.dup
  new_chats = {}

  @models.each do |model_name|
    begin
      # Get the isolated context for this model
      context = @contexts[model_name]
      actual_model, provider = extract_model_and_provider(model_name)

      # Create a fresh chat instance from the same isolated context
      chat = if provider
               context.chat(model: actual_model, provider: provider, assume_model_exists: true)
             else
               context.chat(model: actual_model)
             end

      # Re-add tools if they were previously loaded
      if @tools && !@tools.empty? && chat.model&.supports_functions?
        chat.with_tools(*@tools)
      end

      new_chats[model_name] = chat
    rescue StandardError => e
      # If recreation fails, keep the old chat but clear its messages
      warn "Warning: Could not recreate chat for #{model_name}: #{e.message}. Clearing existing chat."
      chat = old_chats[model_name]
      if chat&.instance_variable_defined?(:@messages)
        chat.instance_variable_set(:@messages, [])
      end
      chat.clear_history if chat&.respond_to?(:clear_history)
      new_chats[model_name] = chat
    end
  end

  @chats = new_chats
  'Chat context successfully cleared.'
rescue StandardError => e
  "Error clearing chat context: #{e.message}"
end

#create_isolated_context_for_model(model_name) ⇒ Object

Create an isolated RubyLLM::Context for a model to prevent cross-talk (ADR-002) Each model gets its own context with provider-specific configuration



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/aia/adapter/chat_execution.rb', line 156

def create_isolated_context_for_model(model_name)
  config = RubyLLM.config.dup

  # Apply provider-specific configuration
  if model_name.start_with?('lms/')
    config.openai_api_base = ENV.fetch('LMS_API_BASE', 'http://localhost:1234/v1')
    config.openai_api_key = 'dummy' # Local servers don't need a real API key
  elsif model_name.start_with?('osaurus/')
    config.openai_api_base = ENV.fetch('OSAURUS_API_BASE', 'http://localhost:11434/v1')
    config.openai_api_key = 'dummy' # Local servers don't need a real API key
  end

  RubyLLM::Context.new(config)
end

#extract_model_and_provider(model_name) ⇒ Object

Extract the actual model name and provider from the prefixed model_name Returns: [actual_model, provider] where provider may be nil for auto-detection



173
174
175
176
177
178
179
180
181
# File 'lib/aia/adapter/chat_execution.rb', line 173

def extract_model_and_provider(model_name)
  if model_name.start_with?('ollama/')
    [model_name.sub('ollama/', ''), 'ollama']
  elsif model_name.start_with?('lms/') || model_name.start_with?('osaurus/')
    [model_name.sub(%r{^(lms|osaurus)/}, ''), 'openai']
  else
    [model_name, nil] # Let RubyLLM auto-detect provider
  end
end

#setup_chats_with_toolsObject



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/aia/adapter/chat_execution.rb', line 80

def setup_chats_with_tools
  valid_chats = {}
  valid_contexts = {}
  valid_specs = []
  failed_models = []

  @model_specs.each do |spec|
    model_name = spec[:model]          # Actual model name (e.g., "gpt-4o")
    internal_id = spec[:internal_id]   # Key for storage (e.g., "gpt-4o#1", "gpt-4o#2")

    begin
      # Create isolated context for this model to prevent cross-talk (ADR-002)
      context = create_isolated_context_for_model(model_name)

      # Determine provider and actual model name
      actual_model, provider = extract_model_and_provider(model_name)

      # Validate LM Studio models
      if model_name.start_with?('lms/')
        lms_api_base = ENV.fetch('LMS_API_BASE', 'http://localhost:1234/v1')
        validate_lms_model!(actual_model, lms_api_base)
      end

      # Create chat using isolated context
      chat = if provider
               context.chat(model: actual_model, provider: provider, assume_model_exists: true)
             else
               context.chat(model: actual_model)
             end

      valid_chats[internal_id] = chat
      valid_contexts[internal_id] = context
      valid_specs << spec
    rescue StandardError => e
      failed_models << "#{internal_id}: #{e.message}"
    end
  end

  # Report failed models but continue with valid ones
  unless failed_models.empty?
    warn "\nFailed to initialize the following models:"
    failed_models.each { |failure| warn "   - #{failure}" }
    logger = LoggerManager.aia_logger
    failed_models.each { |failure| logger.error("Model initialization failed: #{failure}") }
  end

  # If no models initialized successfully, exit
  if valid_chats.empty?
    warn "\nNo valid models could be initialized. Exiting."
    warn "\nAvailable models can be listed with: bin/aia --help models"
    exit 1
  end

  @chats = valid_chats
  @contexts = valid_contexts
  @model_specs = valid_specs
  @models = valid_chats.keys

  # Report successful models
  if failed_models.any?
    warn "\nSuccessfully initialized: #{@models.join(', ')}"
  end

  # Use the first chat to determine tool support (assuming all models have similar tool support)
  first_chat = @chats.values.first
  return unless first_chat&.model&.supports_functions?

  load_tools_lazy_mcp_support_only_when_needed

  @chats.each_value do |chat|
    chat.with_tools(*tools) unless tools.empty?
  end
end

#single_model_chat(prompt, internal_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aia/adapter/chat_execution.rb', line 15

def single_model_chat(prompt, internal_id)
  chat_instance = @chats[internal_id]
  modes = chat_instance.model.modalities

  # TODO: Need to consider how to handle multi-mode models
  if modes.text_to_text?
    text_to_text_single(prompt, internal_id)
  elsif modes.image_to_text?
    image_to_text_single(prompt, internal_id)
  elsif modes.text_to_image?
    text_to_image_single(prompt, internal_id)
  elsif modes.text_to_audio?
    text_to_audio_single(prompt, internal_id)
  elsif modes.audio_to_text?
    audio_to_text_single(prompt, internal_id)
  else
    # TODO: what else can be done?
    "Error: No matching modality for model #{internal_id}"
  end
end

#validate_lms_model!(model_name, api_base) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/aia/adapter/chat_execution.rb', line 183

def validate_lms_model!(model_name, api_base)
  require 'net/http'
  require 'json'

  # Build the /v1/models endpoint URL
  uri = URI("#{api_base.gsub(%r{/v1/?$}, '')}/v1/models")

  begin
    response = Net::HTTP.get_response(uri)

    unless response.is_a?(Net::HTTPSuccess)
      raise "Cannot connect to LM Studio at #{api_base}. Is LM Studio running?"
    end

    data = JSON.parse(response.body)
    available_models = data['data']&.map { |m| m['id'] } || []

    unless available_models.include?(model_name)
      error_msg = "'#{model_name}' is not a valid LM Studio model.\n\n"
      if available_models.empty?
        error_msg += "No models are currently loaded in LM Studio.\n"
        error_msg += "Please load a model in LM Studio first."
      else
        error_msg += "Available LM Studio models:\n"
        available_models.each { |m| error_msg += "  - lms/#{m}\n" }
      end
      raise error_msg
    end
  rescue JSON::ParserError => e
    raise "Invalid response from LM Studio at #{api_base}: #{e.message}"
  rescue StandardError => e
    # Re-raise our custom error messages, wrap others
    raise if e.message.include?("not a valid LM Studio") || e.message.include?("Cannot connect")
    raise "Error connecting to LM Studio: #{e.message}"
  end
end