Class: A2A::Rails::Generators::AgentGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- A2A::Rails::Generators::AgentGenerator
- Defined in:
- lib/a2a/rails/generators/agent_generator.rb
Instance Method Summary collapse
- #add_routes ⇒ Object
- #agent_card_path ⇒ Object private
- #agent_description ⇒ Object private
- #agent_tags ⇒ Object private
- #authentication_config ⇒ Object private
- #authentication_methods ⇒ Object private
- #controller_class_name ⇒ Object private
- #controller_file_path ⇒ Object private
- #controller_parent_class ⇒ Object private
- #create_controller ⇒ Object
- #create_documentation ⇒ Object
- #create_tests ⇒ Object
- #documentation_file_path ⇒ Object private
- #generate_route_content ⇒ Object private
- #generate_skill_definitions ⇒ Object private
- #generate_skill_methods ⇒ Object private
- #namespace ⇒ Object private
- #rpc_path ⇒ Object private
- #rspec_available? ⇒ Boolean private
- #show_post_generation_instructions ⇒ Object
- #skills ⇒ Object private
- #spec_file_path ⇒ Object private
- #test_file_path ⇒ Object private
- #with_authentication? ⇒ Boolean private
Instance Method Details
#add_routes ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 62 def add_routes route_content = generate_route_content return unless File.exist?("config/routes.rb") inject_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do route_content end say "Added routes for #{controller_class_name}", :green end |
#agent_card_path ⇒ Object (private)
209 210 211 212 213 214 215 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 209 def agent_card_path if namespace.present? "/#{namespace}/#{file_name.pluralize}/agent_card" else "/#{file_name.pluralize}/agent_card" end end |
#agent_description ⇒ Object (private)
296 297 298 299 300 301 302 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 296 def agent_description if skills.any? "A2A agent that provides #{skills.map(&:humanize).join(', ')} functionality" else "A2A agent for #{class_name.humanize.downcase} operations" end end |
#agent_tags ⇒ Object (private)
304 305 306 307 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 304 def = [class_name.underscore, "generated"] ( + skills).uniq end |
#authentication_config ⇒ Object (private)
283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 283 def authentication_config if with_authentication? auth_methods = authentication_methods.map { |m| "\"#{m}\"" }.join(", ") "\n # Configure authentication for specific methods\n a2a_authenticate :devise, methods: [\#{auth_methods}]\n RUBY\n else\n \"\"\n end\nend\n" |
#authentication_methods ⇒ Object (private)
225 226 227 228 229 230 231 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 225 def authentication_methods if with_authentication? skills.map { |skill| "#{skill}_secure" } else [] end end |
#controller_class_name ⇒ Object (private)
136 137 138 139 140 141 142 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 136 def controller_class_name if namespace.present? "#{namespace.camelize}::#{class_name}Controller" else "#{class_name}Controller" end end |
#controller_file_path ⇒ Object (private)
104 105 106 107 108 109 110 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 104 def controller_file_path if namespace.present? "app/controllers/#{namespace}/#{file_name}_controller.rb" else "app/controllers/#{file_name}_controller.rb" end end |
#controller_parent_class ⇒ Object (private)
144 145 146 147 148 149 150 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 144 def controller_parent_class if [:api_only] "ActionController::API" else "ApplicationController" end end |
#create_controller ⇒ Object
40 41 42 43 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 40 def create_controller template "agent_controller.rb", controller_file_path say "Created A2A agent controller: #{controller_class_name}", :green end |
#create_documentation ⇒ Object
57 58 59 60 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 57 def create_documentation template "agent_readme.md", documentation_file_path say "Created documentation: #{documentation_file_path}", :green end |
#create_tests ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 45 def create_tests return if [:skip_tests] if rspec_available? template "agent_controller_spec.rb", spec_file_path say "Created RSpec test: #{spec_file_path}", :green else template "agent_controller_test.rb", test_file_path say "Created test: #{test_file_path}", :green end end |
#documentation_file_path ⇒ Object (private)
128 129 130 131 132 133 134 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 128 def documentation_file_path if namespace.present? "docs/agents/#{namespace}/#{file_name}.md" else "docs/agents/#{file_name}.md" end end |
#generate_route_content ⇒ Object (private)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 164 def generate_route_content if namespace.present? namespace_parts = namespace.split("/") indent = " " * namespace_parts.length route_lines = [] # Build nested namespace structure namespace_parts.each_with_index do |ns, index| route_lines << ((" " * index) + "namespace :#{ns} do") end # Add the actual route route_lines << "#{indent} resources :#{file_name.pluralize}, only: [] do" route_lines << "#{indent} collection do" route_lines << "#{indent} get :agent_card" route_lines << "#{indent} post :rpc" route_lines << "#{indent} end" route_lines << "#{indent} end" # Close namespace blocks namespace_parts.length.times do |index| route_lines << "#{' ' * (namespace_parts.length - index - 1)}end" end "\n#{route_lines.join("\n")}\n" else "\n # \#{class_name} A2A Agent routes\n resources :\#{file_name.pluralize}, only: [] do\n collection do\n get :agent_card\n post :rpc\n end\n end\n\n RUBY\n end\nend\n" |
#generate_skill_definitions ⇒ Object (private)
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 266 def generate_skill_definitions skills.map do |skill| " a2a_skill \"\#{skill}\" do |skill|\n skill.description = \"\#{skill.humanize} functionality\"\n skill.tags = [\"\#{skill}\", \"generated\"]\n skill.examples = [\n {\n input: { action: \"\#{skill}\" },\n output: { result: \"\#{skill} completed\" }\n }\n ]\n end\n RUBY\n end.join(\"\\n\\n\")\nend\n" |
#generate_skill_methods ⇒ Object (private)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 233 def generate_skill_methods skills.map do |skill| method_name = skill.underscore if with_authentication? && authentication_methods.include?("#{skill}_secure") " # \#{skill.humanize} skill method (authenticated)\n a2a_method \"\#{method_name}\" do |params|\n # TODO: Implement \#{skill.humanize.downcase} functionality\n {\n skill: \"\#{skill}\",\n message: \"\#{skill.humanize} functionality not yet implemented\",\n params: params,\n user: current_user_info\n }\n end\n RUBY\n else\n <<~RUBY\n # \#{skill.humanize} skill method\n a2a_method \"\#{method_name}\" do |params|\n # TODO: Implement \#{skill.humanize.downcase} functionality\n {\n skill: \"\#{skill}\",\n message: \"\#{skill.humanize} functionality not yet implemented\",\n params: params\n }\n end\n RUBY\n end\n end.join(\"\\n\\n\")\nend\n" |
#namespace ⇒ Object (private)
152 153 154 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 152 def namespace [:namespace] end |
#rpc_path ⇒ Object (private)
217 218 219 220 221 222 223 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 217 def rpc_path if namespace.present? "/#{namespace}/#{file_name.pluralize}/rpc" else "/#{file_name.pluralize}/rpc" end end |
#rspec_available? ⇒ Boolean (private)
205 206 207 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 205 def rspec_available? File.exist?("spec/spec_helper.rb") || File.exist?("spec/rails_helper.rb") end |
#show_post_generation_instructions ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 73 def show_post_generation_instructions say "\n#{'=' * 60}", :green say "A2A Agent '#{class_name}' generated successfully!", :green say "=" * 60, :green say "\nGenerated files:", :yellow say " #{controller_file_path}" say " #{documentation_file_path}" say " #{test_file_path}" unless [:skip_tests] say "\nNext steps:", :yellow say "1. Customize the agent skills and methods in #{controller_file_path}" say "2. Implement the A2A method handlers" say "3. Test your agent using the generated test file" say "\nAgent endpoints:", :yellow say " GET #{agent_card_path} (Agent Card)" say " POST #{rpc_path} (JSON-RPC Methods)" if skills.any? say "\nGenerated skills:", :yellow skills.each do |skill| say " - #{skill.humanize}" end end say "\nFor more information, visit: https://a2a-protocol.org/sdk/ruby/agents/", :blue end |
#skills ⇒ Object (private)
156 157 158 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 156 def skills @skills ||= [:skills].map(&:strip).reject(&:empty?) end |
#spec_file_path ⇒ Object (private)
112 113 114 115 116 117 118 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 112 def spec_file_path if namespace.present? "spec/controllers/#{namespace}/#{file_name}_controller_spec.rb" else "spec/controllers/#{file_name}_controller_spec.rb" end end |
#test_file_path ⇒ Object (private)
120 121 122 123 124 125 126 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 120 def test_file_path if namespace.present? "test/controllers/#{namespace}/#{file_name}_controller_test.rb" else "test/controllers/#{file_name}_controller_test.rb" end end |
#with_authentication? ⇒ Boolean (private)
160 161 162 |
# File 'lib/a2a/rails/generators/agent_generator.rb', line 160 def with_authentication? [:with_authentication] end |