Class: A2A::Rails::Generators::AgentGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/a2a/rails/generators/agent_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_routesObject



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_pathObject (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_descriptionObject (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_tagsObject (private)



304
305
306
307
# File 'lib/a2a/rails/generators/agent_generator.rb', line 304

def agent_tags
  base_tags = [class_name.underscore, "generated"]
  (base_tags + skills).uniq
end

#authentication_configObject (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_methodsObject (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_nameObject (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_pathObject (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_classObject (private)



144
145
146
147
148
149
150
# File 'lib/a2a/rails/generators/agent_generator.rb', line 144

def controller_parent_class
  if options[:api_only]
    "ActionController::API"
  else
    "ApplicationController"
  end
end

#create_controllerObject



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_documentationObject



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_testsObject



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 options[: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_pathObject (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_contentObject (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_definitionsObject (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_methodsObject (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"

#namespaceObject (private)



152
153
154
# File 'lib/a2a/rails/generators/agent_generator.rb', line 152

def namespace
  options[:namespace]
end

#rpc_pathObject (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)

Returns:

  • (Boolean)


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_instructionsObject



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 options[: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

#skillsObject (private)



156
157
158
# File 'lib/a2a/rails/generators/agent_generator.rb', line 156

def skills
  @skills ||= options[:skills].map(&:strip).reject(&:empty?)
end

#spec_file_pathObject (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_pathObject (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)

Returns:

  • (Boolean)


160
161
162
# File 'lib/a2a/rails/generators/agent_generator.rb', line 160

def with_authentication?
  options[:with_authentication]
end