Skip to content

Add active_agent:json_schema generator for stub tool‑schema templates #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PATH
activejob (>= 7.2, < 9.0)
activemodel (>= 7.2, < 9.0)
activesupport (>= 7.2, < 9.0)
jbuilder
rails (>= 7.2, < 9.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion activeagent.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ Gem::Specification.new do |spec|
spec.add_dependency "activejob", ">= 7.2", "< 9.0"

spec.add_dependency "rails", ">= 7.2", "< 9.0"
spec.add_development_dependency "jbuilder"
spec.add_dependency "jbuilder"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not everyone uses jbuilder, so I'd prefer keeping it a dev dependency for testing jbuilder without forcing it on everyone as a dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thoughtful feedback! I’ll move jbuilder back to a dev dependency and experiment with a fastMCP-style schema defined directly in the agent class. I’ll update the PR shortly

end
5 changes: 5 additions & 0 deletions lib/generators/active_agent/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ Examples:
`bin/rails generate active_agent:agent inventory search update report`

creates an inventory agent with search, update, and report actions.

`bin/rails generate active_agent:json_schema inventory search`

creates a JSON schema template for the `search` action:
Schema: app/views/inventory_agent/search.json.jbuilder
49 changes: 49 additions & 0 deletions lib/generators/active_agent/json_schema/json_schema_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "rails/generators"

module ActiveAgent
module Generators
class JsonSchemaGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path("templates", __dir__)
argument :actions, type: :array, default: [], banner: "method method"

check_class_collision suffix: "Agent"

desc <<~DESC
Generates stub *.json.jbuilder tool‑schema templates
for the specified agent actions.

Example:
rails g active_agent:json_schema Report generate summarize

This will create:

app/views/report_agent/generate.json.jbuilder
app/views/report_agent/summarize.json.jbuilder
DESC

def create_schema_templates
view_base = File.join("app/views", class_path, file_name + "_agent")
empty_directory view_base

actions.each do |action|
@action = action
dest = File.join(view_base, "#{action}.json.jbuilder")

if behavior == :invoke && File.exist?(dest)
say_status :skip, dest, :yellow
next
end

@path = dest.sub(%r{\Aapp/views/}, "")
template "view.json.jbuilder.tt", dest
end
end

private

def file_name
@_file_name ||= super.sub(/_agent\z/i, "")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
json.type :function
json.function do
json.name "<%= class_name %>Agent#<%= @action %>"
json.description ""

json.parameters do
json.type :object
json.properties do
json.message do
json.type :string
json.description "Find me in <%= @path %>"
end
end
end
end
16 changes: 16 additions & 0 deletions test/generators/json_schema_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "test_helper"
require "generators/active_agent/json_schema/json_schema_generator"

class JsonSchemaGeneratorTest < Rails::Generators::TestCase
tests ActiveAgent::Generators::JsonSchemaGenerator
destination File.expand_path("../../tmp/json_schema_generator", __dir__)
setup :prepare_destination

def test_creates_json_view_template
run_generator %w[Report analyze]

assert_file "app/views/report_agent/analyze.json.jbuilder" do |content|
assert_match(/json\.name .*ReportAgent#analyze/, content)
end
end
end
Loading