Class: LangsmithrbRails::Generators::BufferGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/langsmithrb_rails/buffer/buffer_generator.rb

Overview

Generator for adding LangSmith buffer to Rails applications

Instance Method Summary collapse

Instance Method Details

#create_directoriesObject



70
71
72
# File 'lib/generators/langsmithrb_rails/buffer/buffer_generator.rb', line 70

def create_directories
  empty_directory "app/jobs/langsmith"
end

#create_jobObject



20
21
22
# File 'lib/generators/langsmithrb_rails/buffer/buffer_generator.rb', line 20

def create_job
  template "flush_buffer_job.rb", "app/jobs/langsmith/flush_buffer_job.rb"
end

#create_migrationObject



11
12
13
14
# File 'lib/generators/langsmithrb_rails/buffer/buffer_generator.rb', line 11

def create_migration
  timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
  template "migration.rb", "db/migrate/#{timestamp}_create_langsmith_run_buffers.rb"
end

#create_modelObject



16
17
18
# File 'lib/generators/langsmithrb_rails/buffer/buffer_generator.rb', line 16

def create_model
  template "langsmith_run_buffer.rb", "app/models/langsmith_run_buffer.rb"
end

#create_rake_taskObject



24
25
26
# File 'lib/generators/langsmithrb_rails/buffer/buffer_generator.rb', line 24

def create_rake_task
  template "langsmith.rake", "lib/tasks/langsmith.rake"
end

#display_post_install_messageObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/langsmithrb_rails/buffer/buffer_generator.rb', line 74

def display_post_install_message
  say "\n"
  say "LangSmith buffer has been added to your Rails application! 🎉", :green
  say "\n"
  say "Next steps:", :yellow
  say "  1. Run the migration to create the buffer table:", :yellow
  say "     bin/rails db:migrate", :yellow
  say "  2. Set up a job to flush the buffer periodically:", :yellow
  say "     # In config/sidekiq.yml or equivalent", :yellow
  say "     :schedule:", :yellow
  say "       langsmith_flush_buffer:", :yellow
  say "         cron: '*/5 * * * *'  # Every 5 minutes", :yellow
  say "         class: LangsmithFlushBufferJob", :yellow
  say "\n"
  say "You can also flush the buffer manually:", :yellow
  say "  bin/rails langsmith:flush", :yellow
  say "\n"
end

#update_configObject



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generators/langsmithrb_rails/buffer/buffer_generator.rb', line 28

def update_config
  initializer_path = "config/initializers/langsmith.rb"
  
  if File.exist?(initializer_path)
    buffer_config = "\n# Enable buffer for LangSmith traces\nLangsmithrbRails::Config.set(:use_buffer, true)\n"
    
    # Check if buffer config is already present
    if File.read(initializer_path).include?("use_buffer")
      say_status :skip, "Buffer config already present in initializer", :yellow
    else
      append_to_file initializer_path, buffer_config
      say_status :append, "Added buffer config to initializer", :green
    end
  else
    say_status :error, "Could not find langsmith initializer", :red
    say_status :create, "Creating initializer with buffer config", :green
    
    create_file initializer_path do
      "        # frozen_string_literal: true\n        \n        # LangSmith Rails Initializer\n        \n        # Configure LangSmith Rails\n        LangsmithrbRails.configure do |config|\n          # Whether LangSmith tracing is enabled\n          config.enabled = ENV[\"LANGSMITH_API_KEY\"].present?\n          \n          # LangSmith API key\n          config.api_key = ENV[\"LANGSMITH_API_KEY\"]\n          \n          # LangSmith project name\n          config.project_name = ENV[\"LANGSMITH_PROJECT\"]\n        end\n        \n        # Enable buffer for LangSmith traces\n        LangsmithrbRails::Config.set(:use_buffer, true)\n      RUBY\n    end\n  end\nend\n"