ruby_llm 1.8.1 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -4
  3. data/lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb +117 -69
  4. data/lib/generators/ruby_llm/chat_ui/templates/controllers/chats_controller.rb.tt +12 -12
  5. data/lib/generators/ruby_llm/chat_ui/templates/controllers/messages_controller.rb.tt +7 -7
  6. data/lib/generators/ruby_llm/chat_ui/templates/controllers/models_controller.rb.tt +4 -4
  7. data/lib/generators/ruby_llm/chat_ui/templates/jobs/chat_response_job.rb.tt +6 -6
  8. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_chat.html.erb.tt +4 -4
  9. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_form.html.erb.tt +5 -5
  10. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/index.html.erb.tt +5 -5
  11. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/new.html.erb.tt +4 -4
  12. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/show.html.erb.tt +8 -8
  13. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_form.html.erb.tt +5 -5
  14. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_message.html.erb.tt +9 -6
  15. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_tool_calls.html.erb.tt +7 -0
  16. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/create.turbo_stream.erb.tt +5 -5
  17. data/lib/generators/ruby_llm/chat_ui/templates/views/models/_model.html.erb.tt +9 -9
  18. data/lib/generators/ruby_llm/chat_ui/templates/views/models/index.html.erb.tt +4 -6
  19. data/lib/generators/ruby_llm/chat_ui/templates/views/models/show.html.erb.tt +11 -11
  20. data/lib/generators/ruby_llm/generator_helpers.rb +131 -87
  21. data/lib/generators/ruby_llm/install/install_generator.rb +75 -79
  22. data/lib/generators/ruby_llm/install/templates/initializer.rb.tt +1 -1
  23. data/lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb +88 -85
  24. data/lib/ruby_llm/active_record/acts_as.rb +11 -2
  25. data/lib/ruby_llm/connection.rb +1 -1
  26. data/lib/ruby_llm/version.rb +1 -1
  27. metadata +2 -1
@@ -5,117 +5,120 @@ require 'rails/generators/active_record'
5
5
  require_relative '../generator_helpers'
6
6
 
7
7
  module RubyLLM
8
- class UpgradeToV17Generator < Rails::Generators::Base # rubocop:disable Style/Documentation
9
- include Rails::Generators::Migration
10
- include RubyLLM::GeneratorHelpers
11
-
12
- namespace 'ruby_llm:upgrade_to_v1_7'
13
- source_root File.expand_path('templates', __dir__)
14
-
15
- # Override source_paths to include install templates
16
- def self.source_paths
17
- [
18
- File.expand_path('templates', __dir__),
19
- File.expand_path('../install/templates', __dir__)
20
- ]
21
- end
8
+ module Generators
9
+ # Generator to upgrade existing RubyLLM apps to v1.7 with new Rails-like API
10
+ class UpgradeToV17Generator < Rails::Generators::Base
11
+ include Rails::Generators::Migration
12
+ include RubyLLM::GeneratorHelpers
13
+
14
+ namespace 'ruby_llm:upgrade_to_v1_7'
15
+ source_root File.expand_path('templates', __dir__)
16
+
17
+ # Override source_paths to include install templates
18
+ def self.source_paths
19
+ [
20
+ File.expand_path('templates', __dir__),
21
+ File.expand_path('../install/templates', __dir__)
22
+ ]
23
+ end
22
24
 
23
- argument :model_mappings, type: :array, default: [], banner: 'chat:ChatName message:MessageName ...'
25
+ argument :model_mappings, type: :array, default: [], banner: 'chat:ChatName message:MessageName ...'
24
26
 
25
- desc 'Upgrades existing RubyLLM apps to v1.7 with new Rails-like API\n' \
26
- 'Usage: rails g ruby_llm:upgrade_to_v1_7 [chat:ChatName] [message:MessageName] ...'
27
+ desc 'Upgrades existing RubyLLM apps to v1.7 with new Rails-like API\n' \
28
+ 'Usage: rails g ruby_llm:upgrade_to_v1_7 [chat:ChatName] [message:MessageName] ...'
27
29
 
28
- def self.next_migration_number(dirname)
29
- ::ActiveRecord::Generators::Base.next_migration_number(dirname)
30
- end
30
+ def self.next_migration_number(dirname)
31
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
32
+ end
31
33
 
32
- def create_migration_file
33
- @model_table_already_existed = table_exists?(table_name_for(model_model_name))
34
+ def create_migration_file
35
+ @model_table_already_existed = table_exists?(table_name_for(model_model_name))
34
36
 
35
- # First check if models table exists, if not create it
36
- unless @model_table_already_existed
37
- migration_template 'create_models_migration.rb.tt',
38
- "db/migrate/create_#{table_name_for(model_model_name)}.rb",
39
- migration_version: migration_version,
40
- model_model_name: model_model_name
37
+ # First check if models table exists, if not create it
38
+ unless @model_table_already_existed
39
+ migration_template 'create_models_migration.rb.tt',
40
+ "db/migrate/create_#{table_name_for(model_model_name)}.rb",
41
+ migration_version: migration_version,
42
+ model_model_name: model_model_name
41
43
 
42
- sleep 1 # Ensure different timestamp
43
- end
44
+ sleep 1 # Ensure different timestamp
45
+ end
44
46
 
45
- migration_template 'migration.rb.tt',
46
- 'db/migrate/migrate_to_ruby_llm_model_references.rb',
47
- migration_version: migration_version,
48
- chat_model_name: chat_model_name,
49
- message_model_name: message_model_name,
50
- tool_call_model_name: tool_call_model_name,
51
- model_model_name: model_model_name,
52
- model_table_already_existed: @model_table_already_existed
53
- end
47
+ migration_template 'migration.rb.tt',
48
+ 'db/migrate/migrate_to_ruby_llm_model_references.rb',
49
+ migration_version: migration_version,
50
+ chat_model_name: chat_model_name,
51
+ message_model_name: message_model_name,
52
+ tool_call_model_name: tool_call_model_name,
53
+ model_model_name: model_model_name,
54
+ model_table_already_existed: @model_table_already_existed
55
+ end
54
56
 
55
- def create_model_file
56
- create_namespace_modules
57
+ def create_model_file
58
+ create_namespace_modules
57
59
 
58
- template 'model_model.rb.tt', "app/models/#{model_model_name.underscore}.rb"
59
- end
60
+ template 'model_model.rb.tt', "app/models/#{model_model_name.underscore}.rb"
61
+ end
60
62
 
61
- def update_existing_models
62
- update_model_acts_as(chat_model_name, 'acts_as_chat', acts_as_chat_declaration)
63
- update_model_acts_as(message_model_name, 'acts_as_message', acts_as_message_declaration)
64
- update_model_acts_as(tool_call_model_name, 'acts_as_tool_call', acts_as_tool_call_declaration)
65
- end
63
+ def update_existing_models
64
+ update_model_acts_as(chat_model_name, 'acts_as_chat', acts_as_chat_declaration)
65
+ update_model_acts_as(message_model_name, 'acts_as_message', acts_as_message_declaration)
66
+ update_model_acts_as(tool_call_model_name, 'acts_as_tool_call', acts_as_tool_call_declaration)
67
+ end
66
68
 
67
- def update_initializer
68
- initializer_path = 'config/initializers/ruby_llm.rb'
69
+ def update_initializer
70
+ initializer_path = 'config/initializers/ruby_llm.rb'
69
71
 
70
- unless File.exist?(initializer_path)
71
- say_status :warning, 'No initializer found. Creating one...', :yellow
72
- template 'initializer.rb.tt', initializer_path
73
- return
74
- end
72
+ unless File.exist?(initializer_path)
73
+ say_status :warning, 'No initializer found. Creating one...', :yellow
74
+ template 'initializer.rb.tt', initializer_path
75
+ return
76
+ end
75
77
 
76
- initializer_content = File.read(initializer_path)
78
+ initializer_content = File.read(initializer_path)
77
79
 
78
- return if initializer_content.include?('config.use_new_acts_as')
80
+ return if initializer_content.include?('config.use_new_acts_as')
79
81
 
80
- inject_into_file initializer_path, before: /^end/ do
81
- lines = ["\n # Enable the new Rails-like API", ' config.use_new_acts_as = true']
82
- lines << " config.model_registry_class = \"#{model_model_name}\"" if model_model_name != 'Model'
83
- lines << "\n"
84
- lines.join("\n")
82
+ inject_into_file initializer_path, before: /^end/ do
83
+ lines = ["\n # Enable the new Rails-like API", ' config.use_new_acts_as = true']
84
+ lines << " config.model_registry_class = \"#{model_model_name}\"" if model_model_name != 'Model'
85
+ lines << "\n"
86
+ lines.join("\n")
87
+ end
85
88
  end
86
- end
87
89
 
88
- def show_next_steps
89
- say_status :success, 'Upgrade prepared!', :green
90
- say <<~INSTRUCTIONS
90
+ def show_next_steps
91
+ say_status :success, 'Upgrade prepared!', :green
92
+ say <<~INSTRUCTIONS
91
93
 
92
- Next steps:
93
- 1. Review the generated migrations
94
- 2. Run: rails db:migrate
95
- 3. Update your code to use the new API: #{chat_model_name}.create! now has the same signature as RubyLLM.chat
94
+ Next steps:
95
+ 1. Review the generated migrations
96
+ 2. Run: rails db:migrate
97
+ 3. Update your code to use the new API: #{chat_model_name}.create! now has the same signature as RubyLLM.chat
96
98
 
97
- ⚠️ If you get "undefined method 'acts_as_model'" during migration:
98
- Add this to config/application.rb BEFORE your Application class:
99
+ ⚠️ If you get "undefined method 'acts_as_model'" during migration:
100
+ Add this to config/application.rb BEFORE your Application class:
99
101
 
100
- RubyLLM.configure do |config|
101
- config.use_new_acts_as = true
102
- end
102
+ RubyLLM.configure do |config|
103
+ config.use_new_acts_as = true
104
+ end
103
105
 
104
- 📚 See the full migration guide: https://rubyllm.com/upgrading-to-1-7/
106
+ 📚 See the full migration guide: https://rubyllm.com/upgrading-to-1-7/
105
107
 
106
- INSTRUCTIONS
107
- end
108
+ INSTRUCTIONS
109
+ end
108
110
 
109
- private
111
+ private
110
112
 
111
- def update_model_acts_as(model_name, old_acts_as, new_acts_as)
112
- model_path = "app/models/#{model_name.underscore}.rb"
113
- return unless File.exist?(Rails.root.join(model_path))
113
+ def update_model_acts_as(model_name, old_acts_as, new_acts_as)
114
+ model_path = "app/models/#{model_name.underscore}.rb"
115
+ return unless File.exist?(Rails.root.join(model_path))
114
116
 
115
- content = File.read(Rails.root.join(model_path))
116
- return unless content.match?(/^\s*#{old_acts_as}/)
117
+ content = File.read(Rails.root.join(model_path))
118
+ return unless content.match?(/^\s*#{old_acts_as}/)
117
119
 
118
- gsub_file model_path, /^\s*#{old_acts_as}.*$/, " #{new_acts_as}"
120
+ gsub_file model_path, /^\s*#{old_acts_as}.*$/, " #{new_acts_as}"
121
+ end
119
122
  end
120
123
  end
121
124
  end
@@ -45,10 +45,12 @@ module RubyLLM
45
45
  has_many messages,
46
46
  -> { order(created_at: :asc) },
47
47
  class_name: self.message_class,
48
+ foreign_key: ActiveSupport::Inflector.foreign_key(table_name.singularize),
48
49
  dependent: :destroy
49
50
 
50
51
  belongs_to model,
51
52
  class_name: self.model_class,
53
+ foreign_key: ActiveSupport::Inflector.foreign_key(model.to_s.singularize),
52
54
  optional: true
53
55
 
54
56
  delegate :add_message, to: :to_llm
@@ -78,7 +80,9 @@ module RubyLLM
78
80
  validates :provider, presence: true
79
81
  validates :name, presence: true
80
82
 
81
- has_many chats, class_name: self.chat_class
83
+ has_many chats,
84
+ class_name: self.chat_class,
85
+ foreign_key: ActiveSupport::Inflector.foreign_key(table_name.singularize)
82
86
 
83
87
  define_method :chats_association do
84
88
  send(chats_association_name)
@@ -102,10 +106,12 @@ module RubyLLM
102
106
 
103
107
  belongs_to chat,
104
108
  class_name: self.chat_class,
109
+ foreign_key: ActiveSupport::Inflector.foreign_key(chat.to_s.singularize),
105
110
  touch: touch_chat
106
111
 
107
112
  has_many tool_calls,
108
113
  class_name: self.tool_call_class,
114
+ foreign_key: ActiveSupport::Inflector.foreign_key(table_name.singularize),
109
115
  dependent: :destroy
110
116
 
111
117
  belongs_to :parent_tool_call,
@@ -120,6 +126,7 @@ module RubyLLM
120
126
 
121
127
  belongs_to model,
122
128
  class_name: self.model_class,
129
+ foreign_key: ActiveSupport::Inflector.foreign_key(model.to_s.singularize),
123
130
  optional: true
124
131
 
125
132
  delegate :tool_call?, :tool_result?, to: :to_llm
@@ -147,10 +154,12 @@ module RubyLLM
147
154
  self.result_class = (result_class || self.message_class).to_s
148
155
 
149
156
  belongs_to message,
150
- class_name: self.message_class
157
+ class_name: self.message_class,
158
+ foreign_key: ActiveSupport::Inflector.foreign_key(message.to_s.singularize)
151
159
 
152
160
  has_one result,
153
161
  class_name: self.result_class,
162
+ foreign_key: ActiveSupport::Inflector.foreign_key(table_name.singularize),
154
163
  dependent: :nullify
155
164
 
156
165
  define_method :message_association do
@@ -85,7 +85,7 @@ module RubyLLM
85
85
  def setup_middleware(faraday)
86
86
  faraday.request :json
87
87
  faraday.response :json
88
- faraday.adapter Faraday.default_adapter
88
+ faraday.adapter :net_http
89
89
  faraday.use :llm_errors, provider: @provider
90
90
  end
91
91
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLLM
4
- VERSION = '1.8.1'
4
+ VERSION = '1.8.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.1
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carmine Paolino
@@ -149,6 +149,7 @@ files:
149
149
  - lib/generators/ruby_llm/chat_ui/templates/views/messages/_content.html.erb.tt
150
150
  - lib/generators/ruby_llm/chat_ui/templates/views/messages/_form.html.erb.tt
151
151
  - lib/generators/ruby_llm/chat_ui/templates/views/messages/_message.html.erb.tt
152
+ - lib/generators/ruby_llm/chat_ui/templates/views/messages/_tool_calls.html.erb.tt
152
153
  - lib/generators/ruby_llm/chat_ui/templates/views/messages/create.turbo_stream.erb.tt
153
154
  - lib/generators/ruby_llm/chat_ui/templates/views/models/_model.html.erb.tt
154
155
  - lib/generators/ruby_llm/chat_ui/templates/views/models/index.html.erb.tt