Class: Genie::SessionConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/genie/session_config.rb

Overview

Handles loading of session configuration such as run_tests_cmd

Constant Summary collapse

DEFAULT_INSTRUCTIONS =
<<~INSTRUCTIONS
  # Genie Instructions
  You are a Genie coding assistant. You help me write code using Test Driven Development
  (Genie) principles. You have some tools available to you, such as listing files, reading files, and writing files,
  and you can write code in Ruby. You will always write tests first, and then implement
  the code to pass those tests. You will not write any code that does not have a test.

  # Rules
  1. We do not have access to any files outside of the base_path.
  2. We do not have access to the internet.
  3. You will always write tests first, and then implement the code to pass those tests.
INSTRUCTIONS
DEFAULTS =
{
  base_path: ".",
  run_tests_cmd: 'rake test',
  model: 'gpt-4o-mini',
  first_question: nil,
  instructions: DEFAULT_INSTRUCTIONS,
  ignore_paths: ['tmp', '/tmp'],
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path:, run_tests_cmd:, model:, first_question:, instructions:, ignore_paths:) ⇒ SessionConfig

Returns a new instance of SessionConfig.



106
107
108
109
110
111
112
113
# File 'lib/genie/session_config.rb', line 106

def initialize(base_path:, run_tests_cmd:, model:, first_question:, instructions:, ignore_paths:)  
  @base_path = File.expand_path(base_path)
  @run_tests_cmd = run_tests_cmd
  @model = model
  @first_question = first_question
  @instructions = instructions
  @ignore_paths = ignore_paths
end

Instance Attribute Details

#base_pathObject (readonly)

Read-only attributes



5
6
7
# File 'lib/genie/session_config.rb', line 5

def base_path
  @base_path
end

#first_questionObject (readonly)

Read-only attributes



5
6
7
# File 'lib/genie/session_config.rb', line 5

def first_question
  @first_question
end

#ignore_pathsObject (readonly)

Read-only attributes



5
6
7
# File 'lib/genie/session_config.rb', line 5

def ignore_paths
  @ignore_paths
end

#instructionsObject (readonly)

Read-only attributes



5
6
7
# File 'lib/genie/session_config.rb', line 5

def instructions
  @instructions
end

#modelObject (readonly)

Read-only attributes



5
6
7
# File 'lib/genie/session_config.rb', line 5

def model
  @model
end

#run_tests_cmdObject (readonly)

Read-only attributes



5
6
7
# File 'lib/genie/session_config.rb', line 5

def run_tests_cmd
  @run_tests_cmd
end

Class Method Details

.defaultObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/genie/session_config.rb', line 95

def self.default
  new(
    base_path: DEFAULTS[:base_path],
    run_tests_cmd: DEFAULTS[:run_tests_cmd],
    model: DEFAULTS[:model],
    first_question: DEFAULTS[:first_question],
    instructions: DEFAULTS[:instructions],
    ignore_paths: DEFAULTS[:ignore_paths],
  )
end

.from_argv(argv) ⇒ Object



29
30
31
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
89
90
91
92
93
# File 'lib/genie/session_config.rb', line 29

def self.from_argv(argv)
  cli_options = {}
  config_file = File.expand_path "./genie.yml"

  OptionParser.new do |opts|
    opts.banner = "Usage: genie [options]"

    opts.on("-c", "--config FILE", "Path to config file") do |file|
      config_file = File.expand_path(file)
    end

    opts.on("-b", "--base-path PATH", "Base path for the session") do |path|
      cli_options[:base_path] = path
    end

    opts.on("-r", "--run-tests CMD", "Command to run tests") do |cmd|
      cli_options[:run_tests_cmd] = cmd
    end

    opts.on("-m", "--model NAME", "Name of model to use") do |name|
      cli_options[:model] = name
    end

    opts.on("-i", "--instructions TEXT", "Instructions for the session") do |text|
      cli_options[:instructions] = text
    end

    opts.on("--ignore-paths LIST", "Comma-separated paths to ignore") do |list|
      cli_options[:ignore_paths] = list.split(',').map(&:strip)
    end

    opts.on("-v", "--[no-]verbose", "Enable verbose mode") do |v|
      cli_options['verbose'] = v
    end
  end.parse!(argv)

  # Last remaining argument is the first question
  cli_options[:first_question] = argv.last if argv.any?

  file_config = {}
  if config_file && File.exist?(config_file)
    file_config = YAML.load_file(config_file, symbolize_names: true)
  end

  # 3️⃣ Merge: DEFAULT < FILE < CLI
  final_config = DEFAULTS.merge(file_config).merge(cli_options)

  # We always preface the instructions with context
  final_config[:instructions] = <<~PREFACE
  # Context
  Current Date and Time: \\#{Time.now.iso8601}
  We are working in a codebase located at '\\#{final_config[:base_path]}'.

  \\#{final_config[:instructions]}
  PREFACE

  new(
    base_path: final_config[:base_path],
    run_tests_cmd: final_config[:run_tests_cmd],
    model: final_config[:model],
    first_question: final_config[:first_question],
    instructions: final_config[:instructions],
    ignore_paths: final_config[:ignore_paths]
  )
end