rapitapir 0.1.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -7
- data/.rubocop_todo.yml +88 -0
- data/CHANGELOG.md +29 -1
- data/README.md +1339 -236
- data/RUBY_WEEKLY_LAUNCH_POST.md +200 -0
- data/docs/RAILS_INTEGRATION_IMPLEMENTATION.md +209 -0
- data/docs/SINATRA_EXTENSION.md +399 -348
- data/docs/STRICT_VALIDATION.md +229 -0
- data/docs/VALIDATION_IMPROVEMENTS.md +218 -0
- data/docs/adr/0001-base-class-strategy.md +33 -0
- data/docs/adr/0002-type-shortcut-and-type-system.md +30 -0
- data/docs/adr/0003-openapi-source-of-truth.md +32 -0
- data/docs/adr/0004-client-generation-typescript-first.md +34 -0
- data/docs/adr/0005-auth-architecture.md +32 -0
- data/docs/adr/0006-observability-defaults.md +30 -0
- data/docs/adr/0007-ai-scope-and-boundaries.md +30 -0
- data/docs/adr/README.md +46 -0
- data/docs/adr/template.md +24 -0
- data/docs/ai-integration-plan.md +112 -0
- data/docs/auto-derivation.md +505 -92
- data/docs/blueprint.md +92 -4
- data/docs/endpoint-definition.md +536 -129
- data/docs/implementation-status.md +1 -1
- data/docs/n8n-integration.md +212 -0
- data/docs/observability.md +810 -500
- data/docs/using-mcp.md +93 -0
- data/examples/ai/knowledge_base_rag.rb +83 -0
- data/examples/ai/user_management_mcp.rb +92 -0
- data/examples/ai/user_validation_llm.rb +187 -0
- data/examples/auto_derivation_ruby_friendly.rb +44 -113
- data/examples/cli/user_api_endpoints.rb +63 -36
- data/examples/demo_api.rb +57 -18
- data/examples/enterprise_extension_demo.rb +13 -13
- data/examples/enterprise_rapitapir_api.rb +81 -93
- data/examples/nocodb/nocodb_scaffold/Gemfile +22 -0
- data/examples/nocodb/nocodb_scaffold/README.md +10 -0
- data/examples/nocodb/nocodb_scaffold/Rakefile +7 -0
- data/examples/nocodb/nocodb_scaffold/app/api/app.rb +1139 -0
- data/examples/nocodb/nocodb_scaffold/app/models/email_template.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/faq.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/feature.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/idea.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/itinerary_day.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/route.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/subscriber.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/tour.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/webhook.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models_loader.rb +3 -0
- data/examples/nocodb/nocodb_scaffold/config/database.yml +11 -0
- data/examples/nocodb/nocodb_scaffold/config.ru +9 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152012_create_features.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152013_create_tours.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152014_create_routes.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152015_create_itinerary_days.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152016_create_subscribers.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152017_create_email_templates.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152018_create_faqs.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152019_create_webhooks.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152020_create_ideas.rb +10 -0
- data/examples/nocodb/swagger.json +4378 -0
- data/examples/production_ready_example.rb +78 -58
- data/examples/rails/RAILS_8_GUIDE.md +165 -0
- data/examples/rails/RAILS_LOADING_FIX.rb +35 -0
- data/examples/rails/README.md +497 -0
- data/examples/rails/comprehensive_test.rb +91 -0
- data/examples/rails/config/routes.rb +48 -0
- data/examples/rails/debug_controller.rb +63 -0
- data/examples/rails/detailed_test.rb +46 -0
- data/examples/rails/enhanced_users_controller.rb +278 -0
- data/examples/rails/final_server_test.rb +50 -0
- data/examples/rails/hello_world_app.rb +116 -0
- data/examples/rails/hello_world_controller.rb +186 -0
- data/examples/rails/hello_world_routes.rb +28 -0
- data/examples/rails/rails8_minimal_demo.rb +132 -0
- data/examples/rails/rails8_simple_demo.rb +140 -0
- data/examples/rails/rails8_working_demo.rb +255 -0
- data/examples/rails/real_world_blog_api.rb +510 -0
- data/examples/rails/server_test.rb +46 -0
- data/examples/rails/test_direct_processing.rb +41 -0
- data/examples/rails/test_hello_world.rb +80 -0
- data/examples/rails/test_rails_integration.rb +54 -0
- data/examples/rails/traditional_app/Gemfile +37 -0
- data/examples/rails/traditional_app/README.md +265 -0
- data/examples/rails/traditional_app/app/controllers/api/v1/posts_controller.rb +254 -0
- data/examples/rails/traditional_app/app/controllers/api/v1/users_controller.rb +220 -0
- data/examples/rails/traditional_app/app/controllers/application_controller.rb +86 -0
- data/examples/rails/traditional_app/app/controllers/application_controller_simplified.rb +87 -0
- data/examples/rails/traditional_app/app/controllers/documentation_controller.rb +149 -0
- data/examples/rails/traditional_app/app/controllers/health_controller.rb +42 -0
- data/examples/rails/traditional_app/config/routes.rb +25 -0
- data/examples/rails/traditional_app/config/routes_best_practice.rb +25 -0
- data/examples/rails/traditional_app/config/routes_simplified.rb +36 -0
- data/examples/rails/traditional_app_runnable.rb +406 -0
- data/examples/rails/users_controller.rb +4 -1
- data/examples/serverless/Gemfile +43 -0
- data/examples/serverless/QUICKSTART.md +331 -0
- data/examples/serverless/README.md +520 -0
- data/examples/serverless/aws_lambda_example.rb +307 -0
- data/examples/serverless/aws_sam_template.yaml +215 -0
- data/examples/serverless/azure_functions_example.rb +407 -0
- data/examples/serverless/deploy.rb +204 -0
- data/examples/serverless/gcp_cloud_functions_example.rb +367 -0
- data/examples/serverless/gcp_function.yaml +23 -0
- data/examples/serverless/host.json +24 -0
- data/examples/serverless/package.json +32 -0
- data/examples/serverless/spec/aws_lambda_spec.rb +196 -0
- data/examples/serverless/spec/spec_helper.rb +89 -0
- data/examples/serverless/vercel.json +31 -0
- data/examples/serverless/vercel_example.rb +404 -0
- data/examples/simple_demo_api.rb +40 -7
- data/examples/strict_validation_examples.rb +104 -0
- data/examples/user_api.rb +130 -132
- data/examples/validation_error_examples.rb +173 -0
- data/lib/rapitapir/ai/llm_instruction.rb +456 -0
- data/lib/rapitapir/ai/mcp.rb +134 -0
- data/lib/rapitapir/ai/rag.rb +287 -0
- data/lib/rapitapir/ai/rag_middleware.rb +147 -0
- data/lib/rapitapir/auth/oauth2.rb +44 -58
- data/lib/rapitapir/cli/command.rb +392 -6
- data/lib/rapitapir/cli/mcp_export.rb +18 -0
- data/lib/rapitapir/cli/scaffold_generator.rb +639 -0
- data/lib/rapitapir/cli/validator.rb +3 -7
- data/lib/rapitapir/core/endpoint.rb +59 -6
- data/lib/rapitapir/core/enhanced_endpoint.rb +3 -7
- data/lib/rapitapir/dsl/enhanced_structures.rb +1 -1
- data/lib/rapitapir/dsl/fluent_endpoint_builder.rb +53 -0
- data/lib/rapitapir/endpoint_registry.rb +47 -0
- data/lib/rapitapir/observability/health_check.rb +4 -4
- data/lib/rapitapir/observability/logging.rb +10 -10
- data/lib/rapitapir/openapi/schema_generator.rb +46 -2
- data/lib/rapitapir/schema.rb +2 -2
- data/lib/rapitapir/server/enhanced_rack_adapter.rb +11 -8
- data/lib/rapitapir/server/middleware.rb +2 -2
- data/lib/rapitapir/server/rack_adapter.rb +1 -3
- data/lib/rapitapir/server/rails/configuration.rb +77 -0
- data/lib/rapitapir/server/rails/controller_base.rb +185 -0
- data/lib/rapitapir/server/rails/documentation_helpers.rb +76 -0
- data/lib/rapitapir/server/rails/resource_builder.rb +181 -0
- data/lib/rapitapir/server/rails/routes.rb +114 -0
- data/lib/rapitapir/server/rails_adapter.rb +10 -3
- data/lib/rapitapir/server/rails_adapter_class.rb +1 -3
- data/lib/rapitapir/server/rails_controller.rb +1 -3
- data/lib/rapitapir/server/rails_integration.rb +67 -0
- data/lib/rapitapir/server/rails_response_handler.rb +16 -3
- data/lib/rapitapir/server/sinatra_adapter.rb +29 -5
- data/lib/rapitapir/server/sinatra_integration.rb +4 -4
- data/lib/rapitapir/sinatra/extension.rb +2 -2
- data/lib/rapitapir/sinatra/oauth2_helpers.rb +34 -40
- data/lib/rapitapir/types/array.rb +4 -0
- data/lib/rapitapir/types/auto_derivation.rb +4 -20
- data/lib/rapitapir/types/datetime.rb +1 -3
- data/lib/rapitapir/types/float.rb +2 -6
- data/lib/rapitapir/types/hash.rb +40 -2
- data/lib/rapitapir/types/integer.rb +4 -12
- data/lib/rapitapir/types/object.rb +6 -2
- data/lib/rapitapir/types.rb +6 -2
- data/lib/rapitapir/version.rb +1 -1
- data/lib/rapitapir.rb +5 -3
- data/rapitapir.gemspec +3 -2
- metadata +142 -8
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'optparse'
|
|
4
4
|
require 'fileutils'
|
|
5
|
+
require 'json'
|
|
5
6
|
|
|
6
7
|
module RapiTapir
|
|
7
8
|
module CLI
|
|
@@ -38,10 +39,14 @@ module RapiTapir
|
|
|
38
39
|
case command
|
|
39
40
|
when 'generate'
|
|
40
41
|
run_generate(@args)
|
|
42
|
+
when 'export'
|
|
43
|
+
run_export(@args)
|
|
41
44
|
when 'serve'
|
|
42
45
|
run_serve(@args)
|
|
43
46
|
when 'validate'
|
|
44
47
|
run_validate(@args)
|
|
48
|
+
when 'ai'
|
|
49
|
+
run_ai(@args)
|
|
45
50
|
when 'version'
|
|
46
51
|
puts "RapiTapir version #{RapiTapir::VERSION}"
|
|
47
52
|
when 'help', nil
|
|
@@ -153,7 +158,7 @@ module RapiTapir
|
|
|
153
158
|
type = args.shift
|
|
154
159
|
unless type
|
|
155
160
|
puts 'Error: Generate command requires a type'
|
|
156
|
-
puts 'Available types: openapi, client, docs'
|
|
161
|
+
puts 'Available types: openapi, client, docs, mcp'
|
|
157
162
|
exit 1
|
|
158
163
|
end
|
|
159
164
|
|
|
@@ -166,9 +171,14 @@ module RapiTapir
|
|
|
166
171
|
when 'docs'
|
|
167
172
|
docs_type = args.shift || 'html'
|
|
168
173
|
generate_docs(docs_type)
|
|
174
|
+
when 'scaffold'
|
|
175
|
+
generate_scaffold(args)
|
|
176
|
+
when 'mcp'
|
|
177
|
+
generate_mcp
|
|
169
178
|
else
|
|
170
179
|
puts "Error: Unknown generation type: #{type}"
|
|
171
|
-
|
|
180
|
+
# Keep this list stable for backward compatibility with existing tests
|
|
181
|
+
puts 'Available types: openapi, client, docs, mcp'
|
|
172
182
|
exit 1
|
|
173
183
|
end
|
|
174
184
|
end
|
|
@@ -206,6 +216,24 @@ module RapiTapir
|
|
|
206
216
|
end
|
|
207
217
|
end
|
|
208
218
|
|
|
219
|
+
def run_export(args)
|
|
220
|
+
type = args.shift
|
|
221
|
+
unless type
|
|
222
|
+
puts 'Error: Export command requires a type'
|
|
223
|
+
puts 'Available types: mcp'
|
|
224
|
+
exit 1
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
case type
|
|
228
|
+
when 'mcp'
|
|
229
|
+
export_mcp
|
|
230
|
+
else
|
|
231
|
+
puts "Error: Unknown export type: #{type}"
|
|
232
|
+
puts 'Available types: mcp'
|
|
233
|
+
exit 1
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
209
237
|
def generate_openapi
|
|
210
238
|
validate_openapi_options
|
|
211
239
|
|
|
@@ -379,6 +407,31 @@ module RapiTapir
|
|
|
379
407
|
end
|
|
380
408
|
end
|
|
381
409
|
|
|
410
|
+
def generate_scaffold(_args)
|
|
411
|
+
unless @options[:input]
|
|
412
|
+
puts 'Error: --input must point to an OpenAPI/Swagger JSON file'
|
|
413
|
+
exit 1
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
output_dir = @options[:output] || 'rapitapir_sinatra_app'
|
|
417
|
+
|
|
418
|
+
begin
|
|
419
|
+
require_relative 'scaffold_generator'
|
|
420
|
+
generator = RapiTapir::CLI::ScaffoldGenerator.new(
|
|
421
|
+
openapi_path: @options[:input],
|
|
422
|
+
output_dir: output_dir,
|
|
423
|
+
config: @options[:config]
|
|
424
|
+
)
|
|
425
|
+
generator.generate!
|
|
426
|
+
puts "Scaffold generated at #{output_dir}"
|
|
427
|
+
puts "Next steps:\n 1) cd #{output_dir}\n 2) bundle install\n 3) bundle exec rake db:create db:migrate\n 4) bundle exec rackup"
|
|
428
|
+
rescue StandardError => e
|
|
429
|
+
puts "Error generating scaffold: #{e.message}"
|
|
430
|
+
puts e.backtrace.first(5).join("\n")
|
|
431
|
+
exit 1
|
|
432
|
+
end
|
|
433
|
+
end
|
|
434
|
+
|
|
382
435
|
def create_markdown_generator(endpoints)
|
|
383
436
|
require_relative '../docs/markdown_generator'
|
|
384
437
|
RapiTapir::Docs::MarkdownGenerator.new(
|
|
@@ -412,6 +465,33 @@ module RapiTapir
|
|
|
412
465
|
end
|
|
413
466
|
end
|
|
414
467
|
|
|
468
|
+
def generate_mcp
|
|
469
|
+
validate_mcp_options
|
|
470
|
+
|
|
471
|
+
endpoints = load_endpoints(@options[:input])
|
|
472
|
+
require_relative '../ai/mcp'
|
|
473
|
+
exporter = RapiTapir::AI::MCP::Exporter.new(endpoints)
|
|
474
|
+
content = JSON.pretty_generate(exporter.as_mcp_context)
|
|
475
|
+
save_mcp_output(content)
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
def export_mcp
|
|
479
|
+
generate_mcp
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
def validate_mcp_options
|
|
483
|
+
return if @options[:input]
|
|
484
|
+
|
|
485
|
+
puts 'Error: --endpoints is required'
|
|
486
|
+
exit 1
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
def save_mcp_output(content)
|
|
490
|
+
output_file = @options[:output] || 'mcp-context.json'
|
|
491
|
+
File.write(output_file, content)
|
|
492
|
+
puts "MCP context exported to #{output_file}"
|
|
493
|
+
end
|
|
494
|
+
|
|
415
495
|
def load_endpoints(file_path)
|
|
416
496
|
raise "Input file not found: #{file_path}" unless File.exist?(file_path)
|
|
417
497
|
|
|
@@ -448,9 +528,8 @@ module RapiTapir
|
|
|
448
528
|
file_dir = File.dirname(File.expand_path(file_path))
|
|
449
529
|
Dir.chdir(file_dir) do
|
|
450
530
|
# Execute the file content using load instead of eval for safety
|
|
451
|
-
# rubocop:disable Security/Eval
|
|
531
|
+
# rubocop:disable-next Security/Eval
|
|
452
532
|
eval(content, TOPLEVEL_BINDING, file_path)
|
|
453
|
-
# rubocop:enable Security/Eval
|
|
454
533
|
end
|
|
455
534
|
|
|
456
535
|
find_endpoints_in_variables(content)
|
|
@@ -488,9 +567,8 @@ module RapiTapir
|
|
|
488
567
|
|
|
489
568
|
return nil unless global_endpoints_var
|
|
490
569
|
|
|
491
|
-
# rubocop:disable Security/Eval
|
|
570
|
+
# rubocop:disable-next Security/Eval
|
|
492
571
|
endpoints = eval(global_endpoints_var.to_s)
|
|
493
|
-
# rubocop:enable Security/Eval
|
|
494
572
|
normalize_endpoints_array(endpoints)
|
|
495
573
|
end
|
|
496
574
|
|
|
@@ -530,6 +608,314 @@ module RapiTapir
|
|
|
530
608
|
|
|
531
609
|
endpoints
|
|
532
610
|
end
|
|
611
|
+
|
|
612
|
+
def run_ai(args)
|
|
613
|
+
subcommand = args.shift
|
|
614
|
+
|
|
615
|
+
case subcommand
|
|
616
|
+
when 'rag'
|
|
617
|
+
run_ai_rag(args)
|
|
618
|
+
when 'query'
|
|
619
|
+
run_ai_query(args)
|
|
620
|
+
when 'llm'
|
|
621
|
+
run_ai_llm(args)
|
|
622
|
+
when 'help', nil
|
|
623
|
+
puts ai_help
|
|
624
|
+
else
|
|
625
|
+
puts "Unknown AI subcommand: #{subcommand}"
|
|
626
|
+
puts ai_help
|
|
627
|
+
exit 1
|
|
628
|
+
end
|
|
629
|
+
end
|
|
630
|
+
|
|
631
|
+
def run_ai_rag(args)
|
|
632
|
+
command = args.shift
|
|
633
|
+
|
|
634
|
+
case command
|
|
635
|
+
when 'test'
|
|
636
|
+
run_rag_test(args)
|
|
637
|
+
when 'setup'
|
|
638
|
+
run_rag_setup(args)
|
|
639
|
+
else
|
|
640
|
+
puts 'RAG subcommands: test, setup'
|
|
641
|
+
exit 1
|
|
642
|
+
end
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
def run_rag_test(args)
|
|
646
|
+
unless @options[:input]
|
|
647
|
+
puts 'Error: --endpoints is required'
|
|
648
|
+
exit 1
|
|
649
|
+
end
|
|
650
|
+
|
|
651
|
+
endpoints = load_endpoints(@options[:input])
|
|
652
|
+
|
|
653
|
+
# Find RAG-enabled endpoints
|
|
654
|
+
rag_endpoints = endpoints.select(&:rag_inference?)
|
|
655
|
+
|
|
656
|
+
if rag_endpoints.empty?
|
|
657
|
+
puts "No RAG-enabled endpoints found in #{@options[:input]}"
|
|
658
|
+
puts 'Add .rag_inference to your endpoints to enable RAG capabilities'
|
|
659
|
+
exit 1
|
|
660
|
+
end
|
|
661
|
+
|
|
662
|
+
require_relative '../ai/rag'
|
|
663
|
+
|
|
664
|
+
puts 'Testing RAG functionality...'
|
|
665
|
+
puts "Found #{rag_endpoints.size} RAG-enabled endpoint(s):"
|
|
666
|
+
|
|
667
|
+
rag_endpoints.each do |endpoint|
|
|
668
|
+
puts " #{endpoint.method} #{endpoint.path}"
|
|
669
|
+
config = endpoint.rag_config
|
|
670
|
+
|
|
671
|
+
# Create pipeline and test
|
|
672
|
+
pipeline = RapiTapir::AI::RAG::Pipeline.new(
|
|
673
|
+
llm: config[:llm] || :openai,
|
|
674
|
+
retrieval: config[:retrieval] || :memory,
|
|
675
|
+
config: config[:config] || {}
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
test_query = args.first || 'What can this API do?'
|
|
679
|
+
result = pipeline.process(test_query)
|
|
680
|
+
|
|
681
|
+
puts " Query: #{test_query}"
|
|
682
|
+
puts " Answer: #{result[:answer][0..100]}#{'...' if result[:answer].length > 100}"
|
|
683
|
+
puts " Sources: #{result[:sources].size} document(s)"
|
|
684
|
+
puts
|
|
685
|
+
end
|
|
686
|
+
end
|
|
687
|
+
|
|
688
|
+
def run_rag_setup(_args)
|
|
689
|
+
puts 'Setting up RAG configuration...'
|
|
690
|
+
|
|
691
|
+
config_template = {
|
|
692
|
+
rag: {
|
|
693
|
+
llm: {
|
|
694
|
+
provider: 'openai',
|
|
695
|
+
api_key: 'your-openai-api-key'
|
|
696
|
+
},
|
|
697
|
+
retrieval: {
|
|
698
|
+
backend: 'memory',
|
|
699
|
+
documents_path: './docs/**/*.md'
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
config_file = 'rapitapir_ai.json'
|
|
705
|
+
|
|
706
|
+
if File.exist?(config_file)
|
|
707
|
+
puts "Configuration file #{config_file} already exists"
|
|
708
|
+
exit 1
|
|
709
|
+
end
|
|
710
|
+
|
|
711
|
+
File.write(config_file, JSON.pretty_generate(config_template))
|
|
712
|
+
puts "Created #{config_file}"
|
|
713
|
+
puts 'Please update the configuration with your API keys and document paths'
|
|
714
|
+
end
|
|
715
|
+
|
|
716
|
+
def run_ai_query(args)
|
|
717
|
+
query = args.join(' ')
|
|
718
|
+
|
|
719
|
+
if query.empty?
|
|
720
|
+
puts 'Usage: rapitapir ai query <your question>'
|
|
721
|
+
exit 1
|
|
722
|
+
end
|
|
723
|
+
|
|
724
|
+
unless @options[:input]
|
|
725
|
+
puts 'Error: --endpoints is required'
|
|
726
|
+
exit 1
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
endpoints = load_endpoints(@options[:input])
|
|
730
|
+
|
|
731
|
+
# Export MCP context for the query
|
|
732
|
+
require_relative '../ai/mcp'
|
|
733
|
+
exporter = RapiTapir::AI::MCP::Exporter.new(endpoints)
|
|
734
|
+
context = exporter.as_mcp_context
|
|
735
|
+
|
|
736
|
+
puts 'API Context for AI Agents:'
|
|
737
|
+
puts '========================='
|
|
738
|
+
puts
|
|
739
|
+
puts "Query: #{query}"
|
|
740
|
+
puts
|
|
741
|
+
puts 'Available Endpoints:'
|
|
742
|
+
if context && context[:endpoints]
|
|
743
|
+
context[:endpoints].each do |ep|
|
|
744
|
+
puts " #{ep[:method]} #{ep[:path]} - #{ep[:summary]}"
|
|
745
|
+
end
|
|
746
|
+
else
|
|
747
|
+
puts ' No endpoints found'
|
|
748
|
+
end
|
|
749
|
+
puts
|
|
750
|
+
puts 'This context can be used by AI agents to understand your API structure.'
|
|
751
|
+
end
|
|
752
|
+
|
|
753
|
+
def run_ai_llm(args)
|
|
754
|
+
command = args.shift
|
|
755
|
+
|
|
756
|
+
case command
|
|
757
|
+
when 'generate'
|
|
758
|
+
run_llm_generate(args)
|
|
759
|
+
when 'export'
|
|
760
|
+
run_llm_export(args)
|
|
761
|
+
when 'test'
|
|
762
|
+
run_llm_test(args)
|
|
763
|
+
else
|
|
764
|
+
puts 'LLM subcommands: generate, export, test'
|
|
765
|
+
exit 1
|
|
766
|
+
end
|
|
767
|
+
end
|
|
768
|
+
|
|
769
|
+
def run_llm_generate(_args)
|
|
770
|
+
unless @options[:input]
|
|
771
|
+
puts 'Error: --endpoints is required'
|
|
772
|
+
exit 1
|
|
773
|
+
end
|
|
774
|
+
|
|
775
|
+
endpoints = load_endpoints(@options[:input])
|
|
776
|
+
|
|
777
|
+
# Find LLM instruction-enabled endpoints
|
|
778
|
+
llm_endpoints = endpoints.select(&:llm_instruction?)
|
|
779
|
+
|
|
780
|
+
if llm_endpoints.empty?
|
|
781
|
+
puts "No LLM instruction-enabled endpoints found in #{@options[:input]}"
|
|
782
|
+
puts 'Add .llm_instruction(purpose: :validation) to your endpoints to enable LLM instruction generation'
|
|
783
|
+
exit 1
|
|
784
|
+
end
|
|
785
|
+
|
|
786
|
+
require_relative '../ai/llm_instruction'
|
|
787
|
+
|
|
788
|
+
puts 'Generating LLM instructions...'
|
|
789
|
+
puts "Found #{llm_endpoints.size} LLM instruction-enabled endpoint(s):"
|
|
790
|
+
|
|
791
|
+
generator = RapiTapir::AI::LLMInstruction::Generator.new(llm_endpoints)
|
|
792
|
+
instructions = generator.generate_all_instructions
|
|
793
|
+
|
|
794
|
+
instructions[:instructions].each do |instruction|
|
|
795
|
+
puts "\n#{'=' * 60}"
|
|
796
|
+
puts "#{instruction[:method]} #{instruction[:path]} (#{instruction[:purpose]})"
|
|
797
|
+
puts '=' * 60
|
|
798
|
+
puts instruction[:instruction]
|
|
799
|
+
end
|
|
800
|
+
|
|
801
|
+
puts "\n#{'=' * 60}"
|
|
802
|
+
puts "Generated #{instructions[:instructions].size} LLM instructions"
|
|
803
|
+
end
|
|
804
|
+
|
|
805
|
+
def run_llm_export(args)
|
|
806
|
+
format = args.shift || 'json'
|
|
807
|
+
output_file = @options[:output]
|
|
808
|
+
|
|
809
|
+
unless @options[:input]
|
|
810
|
+
puts 'Error: --endpoints is required'
|
|
811
|
+
exit 1
|
|
812
|
+
end
|
|
813
|
+
|
|
814
|
+
endpoints = load_endpoints(@options[:input])
|
|
815
|
+
llm_endpoints = endpoints.select(&:llm_instruction?)
|
|
816
|
+
|
|
817
|
+
if llm_endpoints.empty?
|
|
818
|
+
puts 'No LLM instruction-enabled endpoints found'
|
|
819
|
+
exit 1
|
|
820
|
+
end
|
|
821
|
+
|
|
822
|
+
require_relative '../ai/llm_instruction'
|
|
823
|
+
|
|
824
|
+
generator = RapiTapir::AI::LLMInstruction::Generator.new(llm_endpoints)
|
|
825
|
+
instructions = generator.generate_all_instructions
|
|
826
|
+
exporter = RapiTapir::AI::LLMInstruction::Exporter.new(instructions)
|
|
827
|
+
|
|
828
|
+
case format.downcase
|
|
829
|
+
when 'json'
|
|
830
|
+
output = exporter.to_json
|
|
831
|
+
extension = '.json'
|
|
832
|
+
when 'yaml', 'yml'
|
|
833
|
+
output = exporter.to_yaml
|
|
834
|
+
extension = '.yml'
|
|
835
|
+
when 'markdown', 'md'
|
|
836
|
+
output = exporter.to_markdown
|
|
837
|
+
extension = '.md'
|
|
838
|
+
when 'prompts'
|
|
839
|
+
if output_file
|
|
840
|
+
puts exporter.to_prompt_files(output_file)
|
|
841
|
+
return
|
|
842
|
+
else
|
|
843
|
+
puts 'Error: --output directory is required for prompts format'
|
|
844
|
+
exit 1
|
|
845
|
+
end
|
|
846
|
+
else
|
|
847
|
+
puts "Error: Unsupported format '#{format}'. Supported: json, yaml, markdown, prompts"
|
|
848
|
+
exit 1
|
|
849
|
+
end
|
|
850
|
+
|
|
851
|
+
if output_file
|
|
852
|
+
output_file += extension unless output_file.end_with?(extension)
|
|
853
|
+
File.write(output_file, output)
|
|
854
|
+
puts "LLM instructions exported to #{output_file}"
|
|
855
|
+
else
|
|
856
|
+
puts output
|
|
857
|
+
end
|
|
858
|
+
end
|
|
859
|
+
|
|
860
|
+
def run_llm_test(args)
|
|
861
|
+
purpose = args.shift
|
|
862
|
+
|
|
863
|
+
unless purpose
|
|
864
|
+
puts 'Usage: rapitapir ai llm test <purpose>'
|
|
865
|
+
puts 'Available purposes: validation, transformation, analysis, documentation, testing, completion'
|
|
866
|
+
exit 1
|
|
867
|
+
end
|
|
868
|
+
|
|
869
|
+
unless @options[:input]
|
|
870
|
+
puts 'Error: --endpoints is required'
|
|
871
|
+
exit 1
|
|
872
|
+
end
|
|
873
|
+
|
|
874
|
+
endpoints = load_endpoints(@options[:input])
|
|
875
|
+
llm_endpoints = endpoints.select(&:llm_instruction?)
|
|
876
|
+
|
|
877
|
+
if llm_endpoints.empty?
|
|
878
|
+
puts 'No LLM instruction-enabled endpoints found'
|
|
879
|
+
exit 1
|
|
880
|
+
end
|
|
881
|
+
|
|
882
|
+
require_relative '../ai/llm_instruction'
|
|
883
|
+
|
|
884
|
+
puts "Testing LLM instruction generation for purpose: #{purpose}"
|
|
885
|
+
puts
|
|
886
|
+
|
|
887
|
+
generator = RapiTapir::AI::LLMInstruction::Generator.new(llm_endpoints)
|
|
888
|
+
|
|
889
|
+
llm_endpoints.each do |endpoint|
|
|
890
|
+
config = endpoint.llm_instruction_config.dup
|
|
891
|
+
config[:purpose] = purpose.to_sym
|
|
892
|
+
|
|
893
|
+
begin
|
|
894
|
+
instruction = generator.generate_instruction(endpoint, config)
|
|
895
|
+
puts "✅ #{endpoint.method&.upcase} #{endpoint.path}"
|
|
896
|
+
puts " Purpose: #{instruction[:purpose]}"
|
|
897
|
+
puts " Length: #{instruction[:instruction].length} characters"
|
|
898
|
+
puts
|
|
899
|
+
rescue StandardError => e
|
|
900
|
+
puts "❌ #{endpoint.method&.upcase} #{endpoint.path}"
|
|
901
|
+
puts " Error: #{e.message}"
|
|
902
|
+
puts
|
|
903
|
+
end
|
|
904
|
+
end
|
|
905
|
+
end
|
|
906
|
+
|
|
907
|
+
def ai_help
|
|
908
|
+
<<~HELP
|
|
909
|
+
AI Commands:
|
|
910
|
+
rapitapir ai rag test [query] - Test RAG functionality on your API
|
|
911
|
+
rapitapir ai rag setup - Create RAG configuration template
|
|
912
|
+
rapitapir ai query <question> - Get AI-ready context for a query
|
|
913
|
+
rapitapir ai llm generate - Generate LLM instructions from endpoints
|
|
914
|
+
rapitapir ai llm export [format] - Export LLM instructions (json, yaml, markdown, prompts)
|
|
915
|
+
rapitapir ai llm test <purpose> - Test LLM instruction generation for a purpose
|
|
916
|
+
rapitapir ai help - Show this help
|
|
917
|
+
HELP
|
|
918
|
+
end
|
|
533
919
|
end
|
|
534
920
|
end
|
|
535
921
|
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../core/endpoint'
|
|
4
|
+
require_relative '../ai/mcp'
|
|
5
|
+
require 'json'
|
|
6
|
+
|
|
7
|
+
module RapiTapir
|
|
8
|
+
module CLI
|
|
9
|
+
class MCPExport
|
|
10
|
+
# Exports all endpoints marked for MCP as a JSON file
|
|
11
|
+
def self.run(endpoints, output_path = 'mcp-context.json')
|
|
12
|
+
exporter = RapiTapir::AI::MCP::Exporter.new(endpoints)
|
|
13
|
+
File.write(output_path, JSON.pretty_generate(exporter.as_mcp_context))
|
|
14
|
+
puts "MCP context exported to #{output_path}"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|