Class: Kward::OpenRouterModelCache
- Inherits:
-
Object
- Object
- Kward::OpenRouterModelCache
- Defined in:
- lib/kward/openrouter_model_cache.rb
Overview
Fetches and stores the OpenRouter model list available to the configured API key.
Constant Summary collapse
- MODELS_URL =
URI("https://openrouter.ai/api/v1/models/user")
- VERSION =
1
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(api_key:, path: ConfigFiles.openrouter_models_cache_path) ⇒ OpenRouterModelCache
constructor
A new instance of OpenRouterModelCache.
- #matching_key? ⇒ Boolean
- #models ⇒ Object
- #read ⇒ Object
- #refresh ⇒ Object
Constructor Details
#initialize(api_key:, path: ConfigFiles.openrouter_models_cache_path) ⇒ OpenRouterModelCache
Returns a new instance of OpenRouterModelCache.
19 20 21 22 |
# File 'lib/kward/openrouter_model_cache.rb', line 19 def initialize(api_key:, path: ConfigFiles.openrouter_models_cache_path) @api_key = api_key.to_s @path = File.(path) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/kward/openrouter_model_cache.rb', line 17 def path @path end |
Class Method Details
.model_entry?(entry) ⇒ Boolean
24 25 26 27 28 29 30 31 |
# File 'lib/kward/openrouter_model_cache.rb', line 24 def self.model_entry?(entry) return false unless entry.is_a?(Hash) architecture = entry["architecture"].is_a?(Hash) ? entry["architecture"] : {} input_modalities = Array(architecture["input_modalities"]).map(&:to_s) output_modalities = Array(architecture["output_modalities"]).map(&:to_s) input_modalities.include?("text") && output_modalities.include?("text") end |
Instance Method Details
#matching_key? ⇒ Boolean
65 66 67 68 69 70 |
# File 'lib/kward/openrouter_model_cache.rb', line 65 def matching_key? data = read return false unless data data["api_key_sha256"] == api_key_sha256 end |
#models ⇒ Object
61 62 63 |
# File 'lib/kward/openrouter_model_cache.rb', line 61 def models Array(read&.fetch("models", [])) end |
#read ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kward/openrouter_model_cache.rb', line 50 def read return nil unless File.exist?(@path) data = JSON.parse(File.read(@path)) return nil unless data.is_a?(Hash) && data["version"] == VERSION data rescue JSON::ParserError nil end |
#refresh ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kward/openrouter_model_cache.rb', line 33 def refresh raise "No OpenRouter API key found. Set OPENROUTER_API_KEY or add openrouter_api_key to your Kward config." if @api_key.empty? response = Net::HTTP.start(MODELS_URL.hostname, MODELS_URL.port, use_ssl: true) do |http| http.request(refresh_request) end unless response.is_a?(Net::HTTPSuccess) raise "OpenRouter model refresh failed: #{response.code} #{redact(response.body)}" end entries = model_entries(response.body) models = entries.select { |entry| self.class.model_entry?(entry) }.map { |entry| normalize_model(entry) }.uniq { |model| model["id"] }.sort_by { |model| model["id"] } data = cache_data(models) PrivateFile.write_json(@path, data) data end |