Class: RubyLLM::Generators::UpgradeToV17Generator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration, GeneratorHelpers
Defined in:
lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb

Overview

Generator to upgrade existing RubyLLM apps to v1.7 with new Rails-like API

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GeneratorHelpers

#acts_as_chat_declaration, #acts_as_message_declaration, #acts_as_model_declaration, #acts_as_tool_call_declaration, #create_namespace_modules, #migration_version, #mysql?, #parse_model_mappings, #postgresql?, #table_exists?

Class Method Details

.next_migration_number(dirname) ⇒ Object



30
31
32
# File 'lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb', line 30

def self.next_migration_number(dirname)
  ::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

.source_pathsObject

Override source_paths to include install templates



18
19
20
21
22
23
# File 'lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb', line 18

def self.source_paths
  [
    File.expand_path('templates', __dir__),
    File.expand_path('../install/templates', __dir__)
  ]
end

Instance Method Details

#create_migration_fileObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb', line 34

def create_migration_file
  @model_table_already_existed = table_exists?(table_name_for(model_model_name))

  # First check if models table exists, if not create it
  unless @model_table_already_existed
    migration_template 'create_models_migration.rb.tt',
                       "db/migrate/create_#{table_name_for(model_model_name)}.rb",
                       migration_version: migration_version,
                       model_model_name: model_model_name

    sleep 1 # Ensure different timestamp
  end

  migration_template 'migration.rb.tt',
                     'db/migrate/migrate_to_ruby_llm_model_references.rb',
                     migration_version: migration_version,
                     chat_model_name: chat_model_name,
                     message_model_name: message_model_name,
                     tool_call_model_name: tool_call_model_name,
                     model_model_name: model_model_name,
                     model_table_already_existed: @model_table_already_existed
end

#create_model_fileObject



57
58
59
60
61
# File 'lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb', line 57

def create_model_file
  create_namespace_modules

  template 'model_model.rb.tt', "app/models/#{model_model_name.underscore}.rb"
end

#show_next_stepsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb', line 90

def show_next_steps
  say_status :success, 'Upgrade prepared!', :green
  say "\n    Next steps:\n    1. Review the generated migrations\n    2. Run: rails db:migrate\n    3. Update your code to use the new API: \#{chat_model_name}.create! now has the same signature as RubyLLM.chat\n\n    \u26A0\uFE0F  If you get \"undefined method 'acts_as_model'\" during migration:\n      Add this to config/application.rb BEFORE your Application class:\n\n      RubyLLM.configure do |config|\n        config.use_new_acts_as = true\n      end\n\n    \u{1F4DA} See the full migration guide: https://rubyllm.com/upgrading-to-1-7/\n\n  INSTRUCTIONS\nend\n"

#update_existing_modelsObject



63
64
65
66
67
# File 'lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb', line 63

def update_existing_models
  update_model_acts_as(chat_model_name, 'acts_as_chat', acts_as_chat_declaration)
  update_model_acts_as(message_model_name, 'acts_as_message', acts_as_message_declaration)
  update_model_acts_as(tool_call_model_name, 'acts_as_tool_call', acts_as_tool_call_declaration)
end

#update_initializerObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb', line 69

def update_initializer
  initializer_path = 'config/initializers/ruby_llm.rb'

  unless File.exist?(initializer_path)
    say_status :warning, 'No initializer found. Creating one...', :yellow
    template 'initializer.rb.tt', initializer_path
    return
  end

  initializer_content = File.read(initializer_path)

  return if initializer_content.include?('config.use_new_acts_as')

  inject_into_file initializer_path, before: /^end/ do
    lines = ["\n  # Enable the new Rails-like API", '  config.use_new_acts_as = true']
    lines << "  config.model_registry_class = \"#{model_model_name}\"" if model_model_name != 'Model'
    lines << "\n"
    lines.join("\n")
  end
end