diff --git a/lib/llm/config.rb b/lib/llm/config.rb index 7e144e482..da5f5f69e 100644 --- a/lib/llm/config.rb +++ b/lib/llm/config.rb @@ -2,8 +2,7 @@ require 'ruby_llm' module Llm::Config DEFAULT_MODEL = 'gpt-4.1-mini'.freeze - REGISTRY_TTL = 1.hour - REGISTRY_PATH = Rails.root.join('tmp/ruby_llm_models.json').to_s + INIT_MUTEX = Mutex.new class << self def initialized? @@ -13,9 +12,13 @@ module Llm::Config def initialize! return if @initialized - configure_ruby_llm - refresh_model_registry - @initialized = true + INIT_MUTEX.synchronize do + return if @initialized + + configure_ruby_llm + refresh_model_registry + @initialized = true + end end def reset! @@ -34,18 +37,12 @@ module Llm::Config private def refresh_model_registry - if fresh_registry_file? - RubyLLM.models.load_from_json!(REGISTRY_PATH) - else + Thread.new do RubyLLM.models.refresh! - RubyLLM.models.save_to_json(REGISTRY_PATH) + RubyLLM.models.save_to_json + rescue StandardError => e + Rails.logger.warn "Failed to refresh RubyLLM model registry: #{e.message}" end - rescue StandardError => e - Rails.logger.warn "Failed to refresh RubyLLM model registry: #{e.message}" - end - - def fresh_registry_file? - File.exist?(REGISTRY_PATH) && File.mtime(REGISTRY_PATH) > REGISTRY_TTL.ago end def configure_ruby_llm diff --git a/spec/support/ruby_llm_stubs.rb b/spec/support/ruby_llm_stubs.rb new file mode 100644 index 000000000..1adc898b4 --- /dev/null +++ b/spec/support/ruby_llm_stubs.rb @@ -0,0 +1,7 @@ +RSpec.configure do |config| + config.before do + models_instance = RubyLLM.models + allow(models_instance).to receive(:refresh!) + allow(models_instance).to receive(:save_to_json) + end +end