Class: AppchatGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActionView::Helpers::FormHelper, ActionView::Helpers::FormTagHelper, ActionView::Helpers::TagHelper, Turbo::FramesHelper
Defined in:
lib/generators/appchat/appchat_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/appchat/appchat_generator.rb', line 11

def add_gems
  gems = %w(
    ollama-ai
    tailwindcss-rails
  )

  gems.each do |gem|
    unless gem_exists?(gem)
      append_to_file 'Gemfile', "\ngem '#{gem}'\n"
    end
  end

  Bundler.with_unbundled_env do
    run 'bundle install'
  end
end

#copy_modelsObject



71
72
73
74
75
# File 'lib/generators/appchat/appchat_generator.rb', line 71

def copy_models
  copy_file "models/message.rb", "app/models/message.rb", force: true
  copy_file "models/chat.rb", "app/models/chat.rb", force: true
  copy_file "models/appchat_function.rb", "app/models/appchat_function.rb", force: true
end

#copy_rake_tasksObject



104
105
106
# File 'lib/generators/appchat/appchat_generator.rb', line 104

def copy_rake_tasks
  copy_file "tasks/create_appchat_function.rake", "lib/tasks/create_appchat_function.rake"
end

#copy_servicesObject



77
78
79
80
# File 'lib/generators/appchat/appchat_generator.rb', line 77

def copy_services
  copy_file "services/appchat_function_service.rb", "app/services/appchat_function_service.rb", force: true
  copy_file "services/web_search_service.rb", "app/services/web_search_service.rb", force: true
end

#create_background_jobObject



90
91
92
# File 'lib/generators/appchat/appchat_generator.rb', line 90

def create_background_job
  copy_file "get_ai_response_job.rb", "app/jobs/get_ai_response_job.rb"
end

#create_controllersObject



45
46
47
48
# File 'lib/generators/appchat/appchat_generator.rb', line 45

def create_controllers
  copy_file "chats_controller.rb", "app/controllers/chats_controller.rb"
  copy_file "messages_controller.rb", "app/controllers/messages_controller.rb", force: true
end

#create_functionsObject



108
109
110
# File 'lib/generators/appchat/appchat_generator.rb', line 108

def create_functions
  rake "appchat_function:create_web_search"
end

#create_stimulus_controllersObject



65
66
67
68
69
# File 'lib/generators/appchat/appchat_generator.rb', line 65

def create_stimulus_controllers
  copy_file "javascript/chat_message_controller.js", "app/javascript/controllers/chat_message_controller.js"
  copy_file "javascript/speech_to_text_controller.js", "app/javascript/controllers/speech_to_text_controller.js"
  copy_file "javascript/toggle_controller.js", "app/javascript/controllers/toggle_controller.js"
end

#create_stylesheetsObject



61
62
63
# File 'lib/generators/appchat/appchat_generator.rb', line 61

def create_stylesheets
  copy_file "assets/appchat.tailwind.css", "app/assets/stylesheets/appchat.tailwind.css", force: true
end

#create_viewsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/appchat/appchat_generator.rb', line 50

def create_views
  copy_file "chats/chat.html.erb", "app/views/chats/_chat.html.erb", force: true
  copy_file "chats/index.html.erb", "app/views/chats/index.html.erb", force: true
  copy_file "chats/show.html.erb", "app/views/chats/show.html.erb", force: true
  copy_file "messages/index.html.erb", "app/views/messages/index.html.erb", force: true
  copy_file "messages/new.html.erb", "app/views/messages/new.html.erb", force: true
  copy_file "messages/message.html.erb", "app/views/messages/_message.html.erb", force: true
  copy_file "messages/_typing_bubbles.html.erb", "app/views/messages/_typing_bubbles.html.erb", force: true
  copy_file "messages/_function_logs.html.erb", "app/views/messages/_function_logs.html.erb", force: true
end

#generate_modelsObject



37
38
39
40
41
42
43
# File 'lib/generators/appchat/appchat_generator.rb', line 37

def generate_models
  generate "model", "Chat context:text"
  generate "model", "Message chat:references content:text role:integer status:string"
  generate "model", "AppchatFunction name:string description:text class_name:string"
  generate "model", "FunctionParameter appchat_function:references name:string example_value:string"
  generate "model", "FunctionLog message:references name:string prompt:text results:text"
end

#get_startedObject



127
128
129
# File 'lib/generators/appchat/appchat_generator.rb', line 127

def get_started
  puts "Congratulations on setting up appchat! \n run bin/dev to spin up your app, and visit localhost:3000/chats to start chatting"
end

#install_tailwindObject



28
29
30
# File 'lib/generators/appchat/appchat_generator.rb', line 28

def install_tailwind
  run 'rails tailwindcss:install'
end

#run_migrationsObject



86
87
88
# File 'lib/generators/appchat/appchat_generator.rb', line 86

def run_migrations
  rake "db:migrate"
end

#serialize_contextObject



82
83
84
# File 'lib/generators/appchat/appchat_generator.rb', line 82

def serialize_context
  inject_into_class 'app/models/chat.rb', 'Chat', "serialize :context, coder:JSON, type: Array\n"
end

#set_routesObject



32
33
34
35
# File 'lib/generators/appchat/appchat_generator.rb', line 32

def set_routes
  route "resources :chats"
  route "resources :messages"
end

#show_artObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/generators/appchat/appchat_generator.rb', line 112

def show_art
  puts <<-ART

                          _             _   
                         | |           | |  
__ _  _ __   _ __    ___ | |__    __ _ | |_ 
 / _` || '_ \ | '_ \  / __|| '_ \  / _` || __|
| (_| || |_) || |_) || (__ | | | || (_| || |_ 
 \__,_|| .__/ | .__/  \___||_| |_| \__,_| \__|
     | |    | |                             
     |_|    |_|                             

  ART
end

#swap_class_in_layoutObject



94
95
96
97
98
99
100
101
102
# File 'lib/generators/appchat/appchat_generator.rb', line 94

def swap_class_in_layout
  layout_file = "app/views/layouts/application.html.erb"

  if File.exist?(layout_file)
    gsub_file layout_file, /\bmt-28\b/, "mt-10"
  else
    say "Layout file not found. No changes were made.", :red
  end
end