-
Notifications
You must be signed in to change notification settings - Fork 44
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
otrub
wants to merge
2
commits into
activeagents:main
Choose a base branch
from
otrub:improve-agent-generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/generators/active_agent/json_schema/json_schema_generator.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
lib/generators/active_agent/json_schema/templates/view.json.jbuilder.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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