Class: Ace::Support::Models::CLI::Commands::ModelsSubcommands::Search

Inherits:
Cli::Command
  • Object
show all
Includes:
Cli::Base
Defined in:
lib/ace/support/models/cli/commands/models/search.rb

Overview

Search for models

Instance Method Summary collapse

Instance Method Details

#call(query: nil, **options) ⇒ Object



32
33
34
35
36
37
38
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
79
80
81
82
83
84
85
86
87
88
# File 'lib/ace/support/models/cli/commands/models/search.rb', line 32

def call(query: nil, **options)
  # Validate filters before searching
  filter_errors = Atoms::ModelFilter.validate(options[:filter])
  unless filter_errors.empty?
    filter_errors.each { |e| warn "Error: #{e}" }
    raise Ace::Support::Cli::Error.new("Invalid model search filters")
  end

  searcher = Molecules::ModelSearcher.new
  filters = parse_filters(options[:filter])
  limit = options[:limit] || 20

  # Single search with total count for efficient pagination
  result = searcher.search(
    query,
    provider: options[:provider],
    limit: limit,
    filters: filters.empty? ? nil : filters,
    with_total: true
  )

  models = result[:models]
  total_models_count = result[:total]

  if models.empty?
    if options[:json]
      puts "[]"
    else
      message = query ? "No models found matching '#{query}'" : "No models found"
      message += " with filters: #{options[:filter].join(", ")}" if options[:filter]&.any?
      puts message
    end
    return
  end

  if options[:json]
    json_result = {
      models: models.map(&:to_h),
      showing: models.size,
      total: total_models_count
    }
    puts JSON.pretty_generate(json_result)
  else
    if models.size < total_models_count
      puts "Showing #{models.size} of #{total_models_count} results:"
    else
      puts "Found #{models.size} model(s):"
    end
    models.each do |model|
      status = model.deprecated? ? " (deprecated)" : ""
      puts "  #{model.full_id}#{status}"
      puts "    #{model.name}"
    end
  end
rescue CacheError => e
  raise Ace::Support::Cli::Error.new(e.message)
end