Class: LangsmithrbRails::Generators::TracingGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- LangsmithrbRails::Generators::TracingGenerator
- Defined in:
- lib/generators/langsmithrb_rails/tracing/tracing_generator.rb
Overview
Generator for adding LangSmith tracing to Rails applications
Instance Method Summary collapse
- #create_directories ⇒ Object
- #create_job_concern ⇒ Object
- #create_middleware ⇒ Object
- #create_service_concern ⇒ Object
- #display_post_install_message ⇒ Object
- #update_application_config ⇒ Object
Instance Method Details
#create_directories ⇒ Object
55 56 57 58 59 |
# File 'lib/generators/langsmithrb_rails/tracing/tracing_generator.rb', line 55 def create_directories empty_directory "app/middleware/langsmithrb_rails" empty_directory "app/services/concerns" empty_directory "app/jobs/concerns" end |
#create_job_concern ⇒ Object
19 20 21 |
# File 'lib/generators/langsmithrb_rails/tracing/tracing_generator.rb', line 19 def create_job_concern template "langsmith_traced_job.rb", "app/jobs/concerns/langsmith_traced_job.rb" end |
#create_middleware ⇒ Object
11 12 13 |
# File 'lib/generators/langsmithrb_rails/tracing/tracing_generator.rb', line 11 def create_middleware template "request_tracing.rb", "app/middleware/langsmithrb_rails/request_tracing.rb" end |
#create_service_concern ⇒ Object
15 16 17 |
# File 'lib/generators/langsmithrb_rails/tracing/tracing_generator.rb', line 15 def create_service_concern template "langsmith_traced.rb", "app/services/concerns/langsmith_traced.rb" end |
#display_post_install_message ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/generators/langsmithrb_rails/tracing/tracing_generator.rb', line 61 def say "\n" say "LangSmith tracing has been added to your Rails application! 🎉", :green say "\n" say "Usage:", :yellow say " 1. Use the LangsmithTraced concern in your services:", :yellow say " include LangsmithTraced", :yellow say " LangsmithTraced.trace(name: 'my_operation', type: 'llm') { ... }", :yellow say " 2. Use the LangsmithTracedJob concern in your jobs:", :yellow say " include LangsmithTracedJob", :yellow say "\n" say "To add a local buffer for traces, run:", :yellow say " bin/rails g langsmithrb_rails:buffer", :yellow say "\n" end |
#update_application_config ⇒ Object
23 24 25 26 27 28 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 |
# File 'lib/generators/langsmithrb_rails/tracing/tracing_generator.rb', line 23 def update_application_config application_rb_path = "config/application.rb" if File.exist?(application_rb_path) middleware_line = " config.middleware.use LangsmithrbRails::RequestTracing" # Check if middleware is already configured if File.read(application_rb_path).include?(middleware_line) say_status :skip, "Middleware already configured in application.rb", :yellow else # Find the class Application < Rails::Application line application_content = File.read(application_rb_path) if application_content =~ /class Application < Rails::Application/ inject_into_file application_rb_path, after: "class Application < Rails::Application\n" do " # Use LangSmith request tracing middleware\n \#{middleware_line}\n \n RUBY\n end\n \n say_status :insert, \"Added middleware to application.rb\", :green\n else\n say_status :error, \"Could not find the Application class in application.rb\", :red\n end\n end\n else\n say_status :error, \"Could not find application.rb\", :red\n end\nend\n" |