Class: Brute::Providers::ModelsDev
- Inherits:
-
Object
- Object
- Brute::Providers::ModelsDev
- Defined in:
- lib/brute/providers/models_dev.rb
Overview
Fetches and caches model metadata from the models.dev catalog.
Quacks like llm.rb’s provider.models so that the REPL’s model picker can call:
provider.models.all.select(&:chat?)
Models are fetched from models.dev/api.json and cached in-memory for the lifetime of the process (with a TTL).
Defined Under Namespace
Classes: ModelEntry
Constant Summary collapse
- CATALOG_URL =
"https://models.dev/api.json"- CACHE_TTL =
1 hour
3600
Class Method Summary collapse
-
.fetch_catalog ⇒ Object
Fetch the models.dev catalog, with in-memory caching.
-
.invalidate_cache! ⇒ Object
Force a cache refresh on next access.
Instance Method Summary collapse
-
#all ⇒ Array<ModelEntry>
Returns all models for this provider from the models.dev catalog.
-
#initialize(provider:, provider_id: "opencode") ⇒ ModelsDev
constructor
A new instance of ModelsDev.
Constructor Details
#initialize(provider:, provider_id: "opencode") ⇒ ModelsDev
Returns a new instance of ModelsDev.
26 27 28 29 |
# File 'lib/brute/providers/models_dev.rb', line 26 def initialize(provider:, provider_id: "opencode") @provider = provider @provider_id = provider_id end |
Class Method Details
.fetch_catalog ⇒ Object
Fetch the models.dev catalog, with in-memory caching. Thread-safe via a simple mutex.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/brute/providers/models_dev.rb', line 61 def fetch_catalog @mutex ||= Mutex.new @mutex.synchronize do if @catalog && @fetched_at && (Time.now - @fetched_at < CACHE_TTL) return @catalog end @catalog = download_catalog @fetched_at = Time.now @catalog end end |
.invalidate_cache! ⇒ Object
Force a cache refresh on next access.
75 76 77 78 79 80 |
# File 'lib/brute/providers/models_dev.rb', line 75 def invalidate_cache! @mutex&.synchronize do @catalog = nil @fetched_at = nil end end |
Instance Method Details
#all ⇒ Array<ModelEntry>
Returns all models for this provider from the models.dev catalog.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/brute/providers/models_dev.rb', line 33 def all entries = fetch_provider_models entries.map do |id, model| ModelEntry.new( id: id, name: model["name"] || id, chat?: true, cost: model["cost"], limit: model["limit"], reasoning: model["reasoning"] || false, tool_call: model["tool_call"] || false ) end.sort_by(&:id) end |