Class: Crimson::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/crimson/setup.rb

Overview

First-run setup wizard that guides users through provider selection and configuration.

Class Method Summary collapse

Class Method Details

.ask_for_api_key(prompt, provider) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
# File 'lib/crimson/setup.rb', line 56

def self.ask_for_api_key(prompt, provider)
  prompt.mask("Enter your #{PROVIDERS[provider][:name]} API key:")
end

.ask_for_base_url(prompt) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
# File 'lib/crimson/setup.rb', line 61

def self.ask_for_base_url(prompt)
  prompt.ask("Enter the base URL for the provider:")
end

.copy_default_skillsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/crimson/setup.rb', line 120

def self.copy_default_skills
  FileUtils.mkdir_p(Crimson::SKILLS_DIR)

  gem_root = File.expand_path("../..", __dir__)
  bundled_skills_dir = File.join(gem_root, "skills")

  return unless Dir.exist?(bundled_skills_dir)

  Dir.glob(File.join(bundled_skills_dir, "*.md")).each do |file|
    dest = File.join(Crimson::SKILLS_DIR, File.basename(file))
    FileUtils.cp(file, dest) unless File.exist?(dest)
  end
end

.fetch_models(provider, api_key, base_url = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/crimson/setup.rb', line 71

def self.fetch_models(provider, api_key, base_url = nil)
  spinner = TTY::Spinner.new("[:spinner] Fetching models...", format: :dots)
  spinner.auto_spin

  url_str = base_url || PROVIDERS[provider][:base_url]
  url_str += MODELS_ENDPOINT
  uri = URI(url_str)

  headers = PROVIDERS[provider][:auth_headers].call(api_key)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = 10
  http.read_timeout = 30

  request = Net::HTTP::Get.new(uri.request_uri, headers)

  begin
    response = http.request(request)

    unless response.is_a?(Net::HTTPSuccess)
      spinner.error("Failed!")
      return []
    end

    data = JSON.parse(response.body)
    models = data["data"].map { |model| model["id"] }

    spinner.success("Done!")
    models
  rescue => e
    spinner.error("Error: #{e.message}")
    []
  end
end

.first_runvoid

This method returns an undefined value.

Run the full first-time setup including copying default skills.



16
17
18
19
# File 'lib/crimson/setup.rb', line 16

def self.first_run
  copy_default_skills
  run
end

.runvoid

This method returns an undefined value.

Run the interactive configuration wizard.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/crimson/setup.rb', line 23

def self.run
  prompt = TTY::Prompt.new
  puts "Crimson Setup"
  puts "============="
  puts

  provider = select_provider(prompt)
  api_key = ask_for_api_key(prompt, provider)
  base_url = ask_for_base_url(prompt) if provider == :custom
  models = fetch_models(provider, api_key, base_url)

  if models.empty?
    puts "No models found for the provided API key."
    return
  end

  model = select_model(prompt, models)
  save_config(provider, api_key, base_url, model)

  puts
  puts "Configuration saved to #{Crimson::CONFIG_FILE}"
end

.save_config(provider, api_key, base_url, model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
111
112
113
114
115
116
117
# File 'lib/crimson/setup.rb', line 108

def self.save_config(provider, api_key, base_url, model)
  config = Crimson::Config.new(
    provider: provider.to_s,
    model: model,
    api_key: api_key,
    base_url: base_url,
    max_tokens: 1000
  )
  config.save
end

.select_model(prompt, models) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
# File 'lib/crimson/setup.rb', line 66

def self.select_model(prompt, models)
  prompt.select("Select a model:", models)
end

.select_provider(prompt) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
# File 'lib/crimson/setup.rb', line 49

def self.select_provider(prompt)
  prompt.select("Select a provider:",
    PROVIDERS.map { |key, data| { name: data[:name], value: key } }
  )
end