lex-llm 0.1.1

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 (135) hide show
  1. checksums.yaml +7 -0
  2. data/.github/CODEOWNERS +7 -0
  3. data/.github/dependabot.yml +18 -0
  4. data/.github/workflows/ci.yml +16 -0
  5. data/.gitignore +19 -0
  6. data/.rubocop.yml +42 -0
  7. data/CHANGELOG.md +15 -0
  8. data/Gemfile +50 -0
  9. data/LICENSE +21 -0
  10. data/README.md +279 -0
  11. data/lex-llm.gemspec +43 -0
  12. data/lib/generators/lex_llm/agent/agent_generator.rb +36 -0
  13. data/lib/generators/lex_llm/agent/templates/agent.rb.tt +6 -0
  14. data/lib/generators/lex_llm/agent/templates/instructions.txt.erb.tt +0 -0
  15. data/lib/generators/lex_llm/chat_ui/chat_ui_generator.rb +256 -0
  16. data/lib/generators/lex_llm/chat_ui/templates/controllers/chats_controller.rb.tt +38 -0
  17. data/lib/generators/lex_llm/chat_ui/templates/controllers/messages_controller.rb.tt +21 -0
  18. data/lib/generators/lex_llm/chat_ui/templates/controllers/models_controller.rb.tt +14 -0
  19. data/lib/generators/lex_llm/chat_ui/templates/helpers/messages_helper.rb.tt +25 -0
  20. data/lib/generators/lex_llm/chat_ui/templates/jobs/chat_response_job.rb.tt +12 -0
  21. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/chats/_chat.html.erb.tt +16 -0
  22. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/chats/_form.html.erb.tt +31 -0
  23. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/chats/index.html.erb.tt +31 -0
  24. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/chats/new.html.erb.tt +9 -0
  25. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/chats/show.html.erb.tt +27 -0
  26. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_assistant.html.erb.tt +14 -0
  27. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_content.html.erb.tt +1 -0
  28. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_error.html.erb.tt +13 -0
  29. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_form.html.erb.tt +23 -0
  30. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_system.html.erb.tt +10 -0
  31. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_tool.html.erb.tt +2 -0
  32. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_tool_calls.html.erb.tt +4 -0
  33. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/_user.html.erb.tt +14 -0
  34. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/tool_calls/_default.html.erb.tt +13 -0
  35. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/messages/tool_results/_default.html.erb.tt +21 -0
  36. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/models/_model.html.erb.tt +17 -0
  37. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/models/index.html.erb.tt +40 -0
  38. data/lib/generators/lex_llm/chat_ui/templates/tailwind/views/models/show.html.erb.tt +27 -0
  39. data/lib/generators/lex_llm/chat_ui/templates/views/chats/_chat.html.erb.tt +16 -0
  40. data/lib/generators/lex_llm/chat_ui/templates/views/chats/_form.html.erb.tt +29 -0
  41. data/lib/generators/lex_llm/chat_ui/templates/views/chats/index.html.erb.tt +28 -0
  42. data/lib/generators/lex_llm/chat_ui/templates/views/chats/new.html.erb.tt +11 -0
  43. data/lib/generators/lex_llm/chat_ui/templates/views/chats/show.html.erb.tt +25 -0
  44. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_assistant.html.erb.tt +9 -0
  45. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_content.html.erb.tt +1 -0
  46. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_error.html.erb.tt +8 -0
  47. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_form.html.erb.tt +21 -0
  48. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_system.html.erb.tt +6 -0
  49. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_tool.html.erb.tt +2 -0
  50. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_tool_calls.html.erb.tt +4 -0
  51. data/lib/generators/lex_llm/chat_ui/templates/views/messages/_user.html.erb.tt +9 -0
  52. data/lib/generators/lex_llm/chat_ui/templates/views/messages/create.turbo_stream.erb.tt +7 -0
  53. data/lib/generators/lex_llm/chat_ui/templates/views/messages/tool_calls/_default.html.erb.tt +8 -0
  54. data/lib/generators/lex_llm/chat_ui/templates/views/messages/tool_results/_default.html.erb.tt +16 -0
  55. data/lib/generators/lex_llm/chat_ui/templates/views/models/_model.html.erb.tt +15 -0
  56. data/lib/generators/lex_llm/chat_ui/templates/views/models/index.html.erb.tt +38 -0
  57. data/lib/generators/lex_llm/chat_ui/templates/views/models/show.html.erb.tt +17 -0
  58. data/lib/generators/lex_llm/generator_helpers.rb +214 -0
  59. data/lib/generators/lex_llm/install/install_generator.rb +109 -0
  60. data/lib/generators/lex_llm/install/templates/add_references_to_chats_tool_calls_and_messages_migration.rb.tt +9 -0
  61. data/lib/generators/lex_llm/install/templates/chat_model.rb.tt +3 -0
  62. data/lib/generators/lex_llm/install/templates/create_chats_migration.rb.tt +7 -0
  63. data/lib/generators/lex_llm/install/templates/create_messages_migration.rb.tt +19 -0
  64. data/lib/generators/lex_llm/install/templates/create_models_migration.rb.tt +39 -0
  65. data/lib/generators/lex_llm/install/templates/create_tool_calls_migration.rb.tt +21 -0
  66. data/lib/generators/lex_llm/install/templates/initializer.rb.tt +20 -0
  67. data/lib/generators/lex_llm/install/templates/message_model.rb.tt +4 -0
  68. data/lib/generators/lex_llm/install/templates/model_model.rb.tt +3 -0
  69. data/lib/generators/lex_llm/install/templates/tool_call_model.rb.tt +3 -0
  70. data/lib/generators/lex_llm/schema/schema_generator.rb +26 -0
  71. data/lib/generators/lex_llm/schema/templates/schema.rb.tt +2 -0
  72. data/lib/generators/lex_llm/tool/templates/tool.rb.tt +9 -0
  73. data/lib/generators/lex_llm/tool/templates/tool_call.html.erb.tt +13 -0
  74. data/lib/generators/lex_llm/tool/templates/tool_result.html.erb.tt +13 -0
  75. data/lib/generators/lex_llm/tool/tool_generator.rb +96 -0
  76. data/lib/generators/lex_llm/upgrade_to_v1_10/templates/add_v1_10_message_columns.rb.tt +19 -0
  77. data/lib/generators/lex_llm/upgrade_to_v1_10/upgrade_to_v1_10_generator.rb +50 -0
  78. data/lib/generators/lex_llm/upgrade_to_v1_14/templates/add_v1_14_tool_call_columns.rb.tt +7 -0
  79. data/lib/generators/lex_llm/upgrade_to_v1_14/upgrade_to_v1_14_generator.rb +49 -0
  80. data/lib/generators/lex_llm/upgrade_to_v1_7/templates/migration.rb.tt +145 -0
  81. data/lib/generators/lex_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb +122 -0
  82. data/lib/generators/lex_llm/upgrade_to_v1_9/templates/add_v1_9_message_columns.rb.tt +15 -0
  83. data/lib/generators/lex_llm/upgrade_to_v1_9/upgrade_to_v1_9_generator.rb +49 -0
  84. data/lib/legion/extensions/llm/provider_settings.rb +49 -0
  85. data/lib/legion/extensions/llm/transport/fleet_lane.rb +70 -0
  86. data/lib/legion/extensions/llm.rb +50 -0
  87. data/lib/lex_llm/active_record/acts_as.rb +180 -0
  88. data/lib/lex_llm/active_record/acts_as_legacy.rb +503 -0
  89. data/lib/lex_llm/active_record/chat_methods.rb +468 -0
  90. data/lib/lex_llm/active_record/message_methods.rb +131 -0
  91. data/lib/lex_llm/active_record/model_methods.rb +76 -0
  92. data/lib/lex_llm/active_record/payload_helpers.rb +26 -0
  93. data/lib/lex_llm/active_record/tool_call_methods.rb +15 -0
  94. data/lib/lex_llm/agent.rb +365 -0
  95. data/lib/lex_llm/aliases.json +436 -0
  96. data/lib/lex_llm/aliases.rb +38 -0
  97. data/lib/lex_llm/attachment.rb +223 -0
  98. data/lib/lex_llm/chat.rb +351 -0
  99. data/lib/lex_llm/chunk.rb +6 -0
  100. data/lib/lex_llm/configuration.rb +81 -0
  101. data/lib/lex_llm/connection.rb +130 -0
  102. data/lib/lex_llm/content.rb +77 -0
  103. data/lib/lex_llm/context.rb +29 -0
  104. data/lib/lex_llm/embedding.rb +29 -0
  105. data/lib/lex_llm/error.rb +112 -0
  106. data/lib/lex_llm/image.rb +105 -0
  107. data/lib/lex_llm/message.rb +107 -0
  108. data/lib/lex_llm/mime_type.rb +71 -0
  109. data/lib/lex_llm/model/info.rb +113 -0
  110. data/lib/lex_llm/model/modalities.rb +22 -0
  111. data/lib/lex_llm/model/pricing.rb +48 -0
  112. data/lib/lex_llm/model/pricing_category.rb +46 -0
  113. data/lib/lex_llm/model/pricing_tier.rb +33 -0
  114. data/lib/lex_llm/model.rb +7 -0
  115. data/lib/lex_llm/models.json +57241 -0
  116. data/lib/lex_llm/models.rb +506 -0
  117. data/lib/lex_llm/models_schema.json +168 -0
  118. data/lib/lex_llm/moderation.rb +56 -0
  119. data/lib/lex_llm/provider.rb +278 -0
  120. data/lib/lex_llm/railtie.rb +35 -0
  121. data/lib/lex_llm/routing/lane_key.rb +51 -0
  122. data/lib/lex_llm/routing/model_offering.rb +169 -0
  123. data/lib/lex_llm/routing.rb +7 -0
  124. data/lib/lex_llm/stream_accumulator.rb +203 -0
  125. data/lib/lex_llm/streaming.rb +175 -0
  126. data/lib/lex_llm/thinking.rb +49 -0
  127. data/lib/lex_llm/tokens.rb +47 -0
  128. data/lib/lex_llm/tool.rb +254 -0
  129. data/lib/lex_llm/tool_call.rb +25 -0
  130. data/lib/lex_llm/transcription.rb +35 -0
  131. data/lib/lex_llm/utils.rb +91 -0
  132. data/lib/lex_llm/version.rb +5 -0
  133. data/lib/lex_llm.rb +95 -0
  134. data/lib/tasks/lex_llm.rake +23 -0
  135. metadata +349 -0
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
5
+ require_relative '../generator_helpers'
6
+
7
+ module LexLLM
8
+ module Generators
9
+ # Generator for LexLLM Rails models and migrations
10
+ class InstallGenerator < Rails::Generators::Base
11
+ include Rails::Generators::Migration
12
+ include LexLLM::Generators::GeneratorHelpers
13
+
14
+ namespace 'lex_llm:install'
15
+
16
+ source_root File.expand_path('templates', __dir__)
17
+
18
+ argument :model_mappings, type: :array, default: [], banner: 'chat:ChatName message:MessageName ...'
19
+
20
+ class_option :skip_active_storage, type: :boolean, default: false,
21
+ desc: 'Skip ActiveStorage installation and attachment setup'
22
+
23
+ desc 'Creates models and migrations for LexLLM Rails integration\n' \
24
+ 'Usage: bin/rails g lex_llm:install [chat:ChatName] [message:MessageName] ...'
25
+
26
+ def self.next_migration_number(dirname)
27
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
28
+ end
29
+
30
+ def create_migration_files
31
+ migration_template 'create_chats_migration.rb.tt',
32
+ "db/migrate/create_#{chat_table_name}.rb"
33
+ migration_template 'create_messages_migration.rb.tt',
34
+ "db/migrate/create_#{message_table_name}.rb"
35
+ migration_template 'create_tool_calls_migration.rb.tt',
36
+ "db/migrate/create_#{tool_call_table_name}.rb"
37
+ migration_template 'create_models_migration.rb.tt',
38
+ "db/migrate/create_#{model_table_name}.rb"
39
+ migration_template 'add_references_to_chats_tool_calls_and_messages_migration.rb.tt',
40
+ 'db/migrate/add_references_to_' \
41
+ "#{chat_table_name}_#{tool_call_table_name}_and_#{message_table_name}.rb"
42
+ end
43
+
44
+ def create_model_files
45
+ create_namespace_modules
46
+
47
+ template 'chat_model.rb.tt', "app/models/#{chat_model_name.underscore}.rb"
48
+ template 'message_model.rb.tt', "app/models/#{message_model_name.underscore}.rb"
49
+ template 'tool_call_model.rb.tt', "app/models/#{tool_call_model_name.underscore}.rb"
50
+
51
+ template 'model_model.rb.tt', "app/models/#{model_model_name.underscore}.rb"
52
+ end
53
+
54
+ def create_initializer
55
+ template 'initializer.rb.tt', 'config/initializers/lex_llm.rb'
56
+ end
57
+
58
+ def create_convention_directories
59
+ %w[agents tools schemas prompts].each do |name|
60
+ empty_directory "app/#{name}"
61
+ create_file "app/#{name}/.gitkeep" unless File.exist?(Rails.root.join("app/#{name}/.gitkeep"))
62
+ end
63
+ end
64
+
65
+ def install_active_storage
66
+ return if options[:skip_active_storage]
67
+
68
+ say ' Installing ActiveStorage for file attachments...', :cyan
69
+ rails_command 'active_storage:install'
70
+ end
71
+
72
+ def show_install_info
73
+ say "\n ✅ LexLLM installed!", :green
74
+
75
+ say ' ✅ ActiveStorage configured for file attachments support', :green unless options[:skip_active_storage]
76
+
77
+ say "\n Next steps:", :yellow
78
+ say ' 1. Run: bin/rails db:migrate'
79
+ say ' 2. Run: bin/rails lex_llm:load_models'
80
+ say ' 3. Install a lex-llm-* provider gem and configure its settings'
81
+
82
+ say " 4. Start chatting: #{chat_model_name}.create!(model: 'your-provider-model').ask('Hello!')"
83
+ say " 5. Optional UI: #{chat_ui_generator_command}"
84
+
85
+ if options[:skip_active_storage]
86
+ say "\n 📎 Note: ActiveStorage was skipped", :yellow
87
+ say ' File attachments won\'t work without ActiveStorage.'
88
+ say ' To enable later:'
89
+ say ' 1. Run: bin/rails active_storage:install && bin/rails db:migrate'
90
+ say " 2. Add to your #{message_model_name} model: has_many_attached :attachments"
91
+ end
92
+
93
+ say "\n 📚 Documentation: https://github.com/LegionIO/lex-llm", :cyan
94
+
95
+ say "\n ❤️ Love LexLLM?", :magenta
96
+ say ' • ⭐ Star on GitHub: https://github.com/LegionIO/lex-llm'
97
+ say "\n"
98
+ end
99
+
100
+ private
101
+
102
+ def chat_ui_generator_command
103
+ mappings = model_mappings.join(' ')
104
+ mappings = " #{mappings}" unless mappings.empty?
105
+ "bin/rails generate lex_llm:chat_ui#{mappings}"
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,9 @@
1
+ class AddReferencesTo<%= "#{chat_model_name.gsub('::', '').pluralize}#{tool_call_model_name.gsub('::', '').pluralize}And#{message_model_name.gsub('::', '').pluralize}" %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ add_reference :<%= chat_table_name %>, :<%= model_table_name.singularize %>, foreign_key: true
4
+ add_reference :<%= tool_call_table_name %>, :<%= message_table_name.singularize %>, null: false, foreign_key: true
5
+ add_reference :<%= message_table_name %>, :<%= chat_table_name.singularize %>, null: false, foreign_key: true
6
+ add_reference :<%= message_table_name %>, :<%= model_table_name.singularize %>, foreign_key: true
7
+ add_reference :<%= message_table_name %>, :<%= tool_call_table_name.singularize %>, foreign_key: true
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class <%= chat_model_name %> < ApplicationRecord
2
+ <%= acts_as_chat_declaration %>
3
+ end
@@ -0,0 +1,7 @@
1
+ class <%= create_migration_class_name(chat_table_name) %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :<%= chat_table_name %> do |t|
4
+ t.timestamps
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ class <%= create_migration_class_name(message_table_name) %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :<%= message_table_name %> do |t|
4
+ t.string :role, null: false
5
+ t.text :content
6
+ t.json :content_raw
7
+ t.text :thinking_text
8
+ t.text :thinking_signature
9
+ t.integer :thinking_tokens
10
+ t.integer :input_tokens
11
+ t.integer :output_tokens
12
+ t.integer :cached_tokens
13
+ t.integer :cache_creation_tokens
14
+ t.timestamps
15
+ end
16
+
17
+ add_index :<%= message_table_name %>, :role
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ class <%= create_migration_class_name(model_table_name) %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :<%= model_table_name %> do |t|
4
+ t.string :model_id, null: false
5
+ t.string :name, null: false
6
+ t.string :provider, null: false
7
+ t.string :family
8
+ t.datetime :model_created_at
9
+ t.integer :context_window
10
+ t.integer :max_output_tokens
11
+ t.date :knowledge_cutoff
12
+ <% if postgresql? %>
13
+ t.jsonb :modalities, default: {}
14
+ t.jsonb :capabilities, default: []
15
+ t.jsonb :pricing, default: {}
16
+ t.jsonb :metadata, default: {}
17
+ <% elsif mysql? %>
18
+ t.json :modalities
19
+ t.json :capabilities
20
+ t.json :pricing
21
+ t.json :metadata
22
+ <% else %>
23
+ t.json :modalities, default: {}
24
+ t.json :capabilities, default: []
25
+ t.json :pricing, default: {}
26
+ t.json :metadata, default: {}
27
+ <% end %>
28
+ t.timestamps
29
+
30
+ t.index [ :provider, :model_id ], unique: true
31
+ t.index :provider
32
+ t.index :family
33
+ <% if postgresql? -%>
34
+ t.index :capabilities, using: :gin
35
+ t.index :modalities, using: :gin
36
+ <% end -%>
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ <%#- # Migration for creating tool_calls table with database-specific JSON handling -%>
2
+ class <%= create_migration_class_name(tool_call_table_name) %> < ActiveRecord::Migration<%= migration_version %>
3
+ def change
4
+ create_table :<%= tool_call_table_name %> do |t|
5
+ t.string :tool_call_id, null: false
6
+ t.string :name, null: false
7
+ t.text :thought_signature
8
+ <% if postgresql? %>
9
+ t.jsonb :arguments, default: {}
10
+ <% elsif mysql? %>
11
+ t.json :arguments
12
+ <% else %>
13
+ t.json :arguments, default: {}
14
+ <% end %>
15
+ t.timestamps
16
+ end
17
+
18
+ add_index :<%= tool_call_table_name %>, :tool_call_id, unique: true
19
+ add_index :<%= tool_call_table_name %>, :name
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require "lex_llm"
2
+ require "lex_llm/active_record/acts_as"
3
+
4
+ LexLLM.configure do |config|
5
+ # Provider gems register their own settings.
6
+ # Example after installing a provider extension:
7
+ # config.default_model = "your-provider-model"
8
+
9
+ # Use the new association-based acts_as API (recommended)
10
+ config.use_new_acts_as = true
11
+ <% if model_model_name != 'Model' -%>
12
+
13
+ # Custom model registry class name
14
+ config.model_registry_class = "<%= model_model_name %>"
15
+ <% end -%>
16
+ end
17
+
18
+ ActiveSupport.on_load :active_record do
19
+ ::ActiveRecord::Base.include LexLLM::ActiveRecord::ActsAs
20
+ end
@@ -0,0 +1,4 @@
1
+ class <%= message_model_name %> < ApplicationRecord
2
+ <%= acts_as_message_declaration %><% unless options[:skip_active_storage] %>
3
+ has_many_attached :attachments<% end %>
4
+ end
@@ -0,0 +1,3 @@
1
+ class <%= model_model_name %> < ApplicationRecord
2
+ <%= acts_as_model_declaration %>
3
+ end
@@ -0,0 +1,3 @@
1
+ class <%= tool_call_model_name %> < ApplicationRecord
2
+ <%= acts_as_tool_call_declaration %>
3
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module LexLLM
6
+ module Generators
7
+ # Generator for LexLLM schema classes.
8
+ class SchemaGenerator < Rails::Generators::NamedBase
9
+ source_root File.expand_path('templates', __dir__)
10
+
11
+ namespace 'lex_llm:schema'
12
+
13
+ desc 'Creates a LexLLM schema class'
14
+
15
+ def create_schema_file
16
+ template 'schema.rb.tt', File.join('app/schemas', class_path, "#{file_name}.rb")
17
+ end
18
+
19
+ private
20
+
21
+ def schema_class_name
22
+ class_name.end_with?('Schema') ? class_name : "#{class_name}Schema"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,2 @@
1
+ class <%= schema_class_name %> < LexLLM::Schema
2
+ end
@@ -0,0 +1,9 @@
1
+ class <%= class_name %>Tool < LexLLM::Tool
2
+ description "TODO: describe what this tool does"
3
+
4
+ def execute
5
+ # TODO: return something to the LLM.
6
+ # Anything you return here gets converted to a string.
7
+ "TODO: implement <%= class_name %>Tool"
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ <%% tool_call_error = tool_call.tool_error_message %>
2
+ <%% if tool_call_error.present? %>
3
+ <%%= render "messages/error", message: tool_calls, title: "Tool Call Error", error_message: tool_call_error %>
4
+ <%% else %>
5
+ <div id="message_<%%= tool_calls.id %>" class="message"
6
+ style="margin-bottom: 20px; padding: 10px; border-left: 3px solid #6b7280; background: #f9fafb;">
7
+ <div style="font-weight: bold; margin-bottom: 5px;"><%= tool_display_name %> Call</div>
8
+ <pre style="white-space: pre-wrap; margin: 0;"><%%= tool_call.name %>(<%%= tool_call.arguments.map { |k, v| "#{k}: #{v.inspect}" }.join(", ") %>)</pre>
9
+ <div style="font-size: 0.85em; color: #666; margin-top: 5px;">
10
+ <%%= tool_calls.created_at&.strftime("%I:%M %p") %>
11
+ </div>
12
+ </div>
13
+ <%% end %>
@@ -0,0 +1,13 @@
1
+ <%% error_message = tool.tool_error_message %>
2
+ <%% if error_message.present? %>
3
+ <%%= render "messages/error", message: tool, title: "Tool Result Error", error_message: error_message %>
4
+ <%% else %>
5
+ <div id="message_<%%= tool.id %>" class="message"
6
+ style="margin-bottom: 20px; padding: 10px; border-left: 3px solid #6b7280; background: #f9fafb;">
7
+ <div style="font-weight: bold; margin-bottom: 5px;"><%= tool_display_name %> Result</div>
8
+ <pre style="white-space: pre-wrap; margin: 0;"><%%= tool.content.presence || "(no output)" %></pre>
9
+ <div style="font-size: 0.85em; color: #666; margin-top: 5px;">
10
+ <%%= tool.created_at&.strftime("%I:%M %p") %>
11
+ </div>
12
+ </div>
13
+ <%% end %>
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module LexLLM
6
+ module Generators
7
+ # Generator for LexLLM tool classes and related message partials.
8
+ class ToolGenerator < Rails::Generators::NamedBase
9
+ source_root File.expand_path('templates', __dir__)
10
+
11
+ namespace 'lex_llm:tool'
12
+
13
+ check_class_collision suffix: 'Tool'
14
+
15
+ desc 'Creates a LexLLM tool class and matching tool call/result view partials'
16
+
17
+ def create_tool_file
18
+ template 'tool.rb.tt', File.join('app/tools', class_path, "#{file_name}_tool.rb")
19
+ end
20
+
21
+ def create_tool_view_partials
22
+ empty_directory 'app/views/messages/tool_calls'
23
+ empty_directory 'app/views/messages/tool_results'
24
+
25
+ create_tool_call_partial
26
+ create_tool_result_partial
27
+ end
28
+
29
+ private
30
+
31
+ def create_tool_call_partial
32
+ destination_path = File.join('app/views/messages/tool_calls', "_#{tool_partial_name}.html.erb")
33
+ default_partial_path = File.join(destination_root, 'app/views/messages/tool_calls/_default.html.erb')
34
+
35
+ if File.exist?(default_partial_path)
36
+ default_markup = tool_named_call_markup(File.read(default_partial_path))
37
+ indented_markup = indent_non_empty_lines(default_markup, 2)
38
+ create_file destination_path, <<~ERB
39
+ <% tool_call_error = tool_call.tool_error_message %>
40
+ <% if tool_call_error.present? %>
41
+ <%= render "messages/error", message: tool_calls, title: "Tool Call Error", error_message: tool_call_error %>
42
+ <% else %>
43
+ #{indented_markup}<% end %>
44
+ ERB
45
+ else
46
+ template 'tool_call.html.erb.tt', destination_path
47
+ end
48
+
49
+ strip_trailing_whitespace(destination_path)
50
+ end
51
+
52
+ def create_tool_result_partial
53
+ destination_path = File.join('app/views/messages/tool_results', "_#{tool_partial_name}.html.erb")
54
+ default_partial_path = File.join(destination_root, 'app/views/messages/tool_results/_default.html.erb')
55
+
56
+ if File.exist?(default_partial_path)
57
+ create_file destination_path, tool_named_result_markup(File.read(default_partial_path))
58
+ else
59
+ template 'tool_result.html.erb.tt', destination_path
60
+ end
61
+
62
+ strip_trailing_whitespace(destination_path)
63
+ end
64
+
65
+ def tool_named_call_markup(markup)
66
+ markup.sub('Tool Call', "#{tool_display_name} Call")
67
+ end
68
+
69
+ def tool_named_result_markup(markup)
70
+ markup.sub(/\bTool\b(?!\s*Result)/, "#{tool_display_name} Result")
71
+ end
72
+
73
+ def tool_display_name
74
+ class_name.demodulize
75
+ end
76
+
77
+ def tool_partial_name
78
+ file_name.delete_suffix('_tool')
79
+ end
80
+
81
+ def indent_non_empty_lines(markup, spaces)
82
+ indentation = ' ' * spaces
83
+ markup.lines.map { |line| line.strip.empty? ? line : "#{indentation}#{line}" }.join
84
+ end
85
+
86
+ def strip_trailing_whitespace(path)
87
+ content = File.read(path)
88
+ stripped_content = content.lines.map(&:rstrip).join("\n")
89
+ stripped_content = "#{stripped_content}\n" unless stripped_content.end_with?("\n")
90
+ return if content == stripped_content
91
+
92
+ File.write(path, stripped_content)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,19 @@
1
+ class AddLexLlmV110Columns < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ unless column_exists?(:<%= message_table_name %>, :thinking_text)
4
+ add_column :<%= message_table_name %>, :thinking_text, :text
5
+ end
6
+
7
+ unless column_exists?(:<%= message_table_name %>, :thinking_signature)
8
+ add_column :<%= message_table_name %>, :thinking_signature, :text
9
+ end
10
+
11
+ unless column_exists?(:<%= message_table_name %>, :thinking_tokens)
12
+ add_column :<%= message_table_name %>, :thinking_tokens, :integer
13
+ end
14
+
15
+ unless column_exists?(:<%= tool_call_table_name %>, :thought_signature)
16
+ add_column :<%= tool_call_table_name %>, :thought_signature, :string
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
5
+ require_relative '../generator_helpers'
6
+
7
+ module LexLLM
8
+ module Generators
9
+ # Generator to add v1.10 columns (thinking output + thinking tokens) to existing apps.
10
+ class UpgradeToV110Generator < Rails::Generators::Base
11
+ include Rails::Generators::Migration
12
+ include LexLLM::Generators::GeneratorHelpers
13
+
14
+ namespace 'lex_llm:upgrade_to_v1_10'
15
+ source_root File.expand_path('templates', __dir__)
16
+
17
+ argument :model_mappings, type: :array, default: [], banner: 'message:MessageName'
18
+
19
+ desc 'Adds thinking output columns and thinking token tracking introduced in v1.10.0'
20
+
21
+ def self.next_migration_number(dirname)
22
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
23
+ end
24
+
25
+ def create_migration_file
26
+ parse_model_mappings
27
+
28
+ migration_template 'add_v1_10_message_columns.rb.tt',
29
+ 'db/migrate/add_lex_llm_v1_10_columns.rb',
30
+ migration_version: migration_version,
31
+ message_table_name: message_table_name,
32
+ tool_call_table_name: tool_call_table_name
33
+ end
34
+
35
+ def show_next_steps
36
+ say_status :success, 'Upgrade prepared!', :green
37
+ say <<~INSTRUCTIONS
38
+
39
+ Next steps:
40
+ 1. Review the generated migration
41
+ 2. Run: bin/rails db:migrate
42
+ 3. Restart your application server
43
+
44
+ 📚 See the v1.10.0 release notes for details on extended thinking support.
45
+
46
+ INSTRUCTIONS
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ class AddLexLlmV114Columns < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ if column_exists?(:<%= tool_call_table_name %>, :thought_signature, :string)
4
+ change_column :<%= tool_call_table_name %>, :thought_signature, :text
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
5
+ require_relative '../generator_helpers'
6
+
7
+ module LexLLM
8
+ module Generators
9
+ # Generator to fix tool call thought signature column type for MySQL safety.
10
+ class UpgradeToV114Generator < Rails::Generators::Base
11
+ include Rails::Generators::Migration
12
+ include LexLLM::Generators::GeneratorHelpers
13
+
14
+ namespace 'lex_llm:upgrade_to_v1_14'
15
+ source_root File.expand_path('templates', __dir__)
16
+
17
+ argument :model_mappings, type: :array, default: [], banner: 'tool_call:ToolCallName'
18
+
19
+ desc 'Updates tool call thought_signature column to text introduced in v1.14.0'
20
+
21
+ def self.next_migration_number(dirname)
22
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
23
+ end
24
+
25
+ def create_migration_file
26
+ parse_model_mappings
27
+
28
+ migration_template 'add_v1_14_tool_call_columns.rb.tt',
29
+ 'db/migrate/add_lex_llm_v1_14_columns.rb',
30
+ migration_version: migration_version,
31
+ tool_call_table_name: tool_call_table_name
32
+ end
33
+
34
+ def show_next_steps
35
+ say_status :success, 'Upgrade prepared!', :green
36
+ say <<~INSTRUCTIONS
37
+
38
+ Next steps:
39
+ 1. Review the generated migration
40
+ 2. Run: bin/rails db:migrate
41
+ 3. Restart your application server
42
+
43
+ 📚 See the v1.14.0 release notes for details on thought signature persistence.
44
+
45
+ INSTRUCTIONS
46
+ end
47
+ end
48
+ end
49
+ end