Class: Brute::Providers::ModelsDev

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(provider:, provider_id: "opencode") ⇒ ModelsDev

Returns a new instance of ModelsDev.

Parameters:

  • provider (LLM::Provider)

    the provider instance (for delegating execute/headers)

  • provider_id (String) (defaults to: "opencode")

    the provider key in models.dev (e.g., “opencode”, “opencode-go”)



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_catalogObject

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

#allArray<ModelEntry>

Returns all models for this provider from the models.dev catalog.

Returns:



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