Class: FlexiAdmin::Services::CodeGen::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/flexi_admin/services/code_gen/runner.rb

Constant Summary collapse

SYSTEM_PROMPT_FILENAME =
'prompts/codegen-system-prompt.md'
OUTPUT_PROMPT_FILE =
'tmp/code_gen_prompt.md'
OUTPUT_CODE_FILE =
'tmp/code_gen_parsed_response.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: :gemini, model: :gemini_flash) ⇒ Runner

Returns a new instance of Runner.



13
14
15
16
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 13

def initialize(client: :gemini, model: :gemini_flash)
  @client = resolve_client(client)
  @model = model
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 11

def client
  @client
end

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 11

def model
  @model
end

Instance Method Details

#bulk_action_view_filesObject



167
168
169
170
171
172
173
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 167

def bulk_action_view_files
  description = <<~DESCRIPTION
    These are files that comprise components to build bulk actions.
  DESCRIPTION

  OpenStruct.new(description:, files: resource_files[:bulk_actions])
end

#config_filesObject



191
192
193
194
195
196
197
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 191

def config_files
  description = <<~DESCRIPTION
    Various files describing the system setup. Current time is #{Time.now.strftime('%Y%m%d%H%M%S')}.
  DESCRIPTION

  OpenStruct.new(description:, files: resource_files[:config])
end

#controller_filesObject



183
184
185
186
187
188
189
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 183

def controller_files
  description = <<~DESCRIPTION
    These are files that comprise controllers of an application resource.
  DESCRIPTION

  OpenStruct.new(description:, files: resource_files[:controllers])
end

#execute(text) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 18

def execute(text)
  prompt = prompt(text)
  File.write OUTPUT_PROMPT_FILE, prompt

  result = client.new.chat prompt, format: :json, model: model
  File.write OUTPUT_CODE_FILE, result.as_json.to_json
  execute_response(result.as_json.dig('files'))

  result
rescue StandardError => e
  binding.pry if Rails.env.development?
  raise e
end

#execute_response(array_of_hashes) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 32

def execute_response(array_of_hashes)
  raise "Array of hashes, expected, got #{array_of_hashes.inspect}" if !array_of_hashes.is_a?(Array)

  array_of_hashes.each do |hash|
    FileUtils.mkdir_p File.dirname(hash['filename'])
    File.write hash['filename'], hash['code']
  end
end

#export_codebase(output_file = 'codebase_export.txt', exclude_patterns = []) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 41

def export_codebase(output_file = 'codebase_export.txt', exclude_patterns = [])
  exclude_patterns = ['tmp/**', 'templates/**', 'examples/**', 'storage/**', 'bin/**', 'db/**', 'vendor/**', 'node_modules/**', 'public/**',
                      '*.png', '*.jpg', '*.jpeg', '*.gif', '*.lock', '*.log', '*.pid', '*.pid.lock',
                      '*.map', '*.gz', '*.swp', '*.swo', '*.DS_Store']

  File.open(output_file, 'w') do |file|
    Dir.glob('**/*').each do |path|
      next if exclude_patterns.any? { |pattern| File.fnmatch?(pattern, path) }
      next if File.directory?(path)
      next if File.size(path) > 100 * 1024 # Skip files larger than 100KB

      file.puts "# #{path}"
      file.puts File.read(path)
      file.puts "\n\n"
    end
  end
end

#gem_infoObject



63
64
65
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 63

def gem_info
  @gem_info ||= Gem::Specification.find_by_name('flexi_admin')
end

#index_view_filesObject



159
160
161
162
163
164
165
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 159

def index_view_files
  description = <<~DESCRIPTION
    These are files that comprise index views of an application resource.
  DESCRIPTION

  OpenStruct.new(description:, files: resource_files[:index_views])
end

#inject_all_filesObject



86
87
88
89
90
91
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 86

def inject_all_files
  inject_files_and_descriptions(show_view_files, index_view_files, bulk_action_view_files,
                                model_files,
                                controller_files,
                                config_files)
end

#inject_files_and_descriptions(*objects) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 72

def inject_files_and_descriptions(*objects)
  result = String.new
  objects.each do |object|
    result << "# Description\n"
    result << "#{object.description}\n\n# Files\n"
    object.files.each do |example_file|
      real_path = example_file.gsub(EXAMPLES_DIR + '/', '')
      result << "/# #{real_path}\n"
      result << "#{File.read(example_file)}\n\n" # Fetch from examples/ directory
    end
  end
  result
end

#model_filesObject



175
176
177
178
179
180
181
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 175

def model_files
  description = <<~DESCRIPTION
    These are files that comprise models of an application resource.
  DESCRIPTION

  OpenStruct.new(description:, files: resource_files[:models])
end

#prompt(text) ⇒ Object



67
68
69
70
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 67

def prompt(text)
  prompt = system_prompt.gsub('{{task}}', text)
  prompt.gsub('{{sample_files}}', inject_all_files)
end

#resolve_client(client) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 199

def resolve_client(client)
  case client
  when :gpt
    FlexiAdmin::Services::CodeGen::Gpt
  when :gemini
    FlexiAdmin::Services::CodeGen::Gemini
  else
    raise "Unknown client: #{client}, expected :gpt or :gemini"
  end
end

#resolve_file_path(file) ⇒ Object



93
94
95
96
97
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 93

def resolve_file_path(file)
  local_path = gem_info.full_gem_path
  full_path = gem_info.files.find { |f| f.include?(file) }
  File.expand_path(full_path, local_path)
end

#resource_filesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 99

def resource_files
  {
    show_views: [
      "#{EXAMPLES_DIR}/app/components/admin/observation/show/edit_form_component.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/observation/show/edit_form_component.rb",
      "#{EXAMPLES_DIR}/app/components/admin/observation/show/page_component.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/observation/show/page_component.rb",
      "#{EXAMPLES_DIR}/app/views/admin/observations/show.html.slim"

    ],
    index_views: [
      "#{EXAMPLES_DIR}/app/components/admin/observation/index_page_component.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/observation/index_page_component.rb",
      "#{EXAMPLES_DIR}/app/components/admin/observation/resources_component.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/observation/resources_component.rb",
      "#{EXAMPLES_DIR}/app/components/admin/observation/view/grid_view_component.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/observation/view/grid_view_component.rb",
      "#{EXAMPLES_DIR}/app/components/admin/observation/view/list_view_component.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/observation/view/list_view_component.rb",
      "#{EXAMPLES_DIR}/app/views/admin/observations/index.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/nav/navbar_component.rb"
    ],
    bulk_actions: [
      "#{EXAMPLES_DIR}/app/components/admin/observation_image/action/delete_image.html.slim",
      "#{EXAMPLES_DIR}/app/components/admin/observation_image/action/delete_image.rb"
    ],
    models: [
      "#{EXAMPLES_DIR}/app/models/observation.rb"
    ],
    controllers: [
      "#{EXAMPLES_DIR}/app/controllers/admin/observations_controller.rb"
    ],
    config: [
      "#{EXAMPLES_DIR}/db/schema.rb",
      "#{EXAMPLES_DIR}/config/routes.rb"
    ]
  }
end

#show_view_filesObject



151
152
153
154
155
156
157
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 151

def show_view_files
  description = <<~DESCRIPTION
    These are files that comprise show views of an application resource.
  DESCRIPTION

  OpenStruct.new(description:, files: resource_files[:show_views])
end

#system_promptObject



59
60
61
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 59

def system_prompt
  File.read(resolve_file_path(SYSTEM_PROMPT_FILENAME))
end

#update_resource_examplesObject



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/flexi_admin/services/code_gen/runner.rb', line 138

def update_resource_examples
  resource_files.each do |_type, files|
    files.each do |file|
      source = file.gsub(EXAMPLES_DIR + '/', '')
      destination = file
      dirname = File.dirname(destination)

      FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
      FileUtils.cp(source, destination)
    end
  end
end