rails-ai-context 5.2.0 → 5.3.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/CHANGELOG.md +29 -0
- data/README.md +2 -2
- data/docs/GUIDE.md +5 -3
- data/lib/rails_ai_context/configuration.rb +7 -0
- data/lib/rails_ai_context/hydration_result.rb +14 -0
- data/lib/rails_ai_context/hydrators/controller_hydrator.rb +51 -0
- data/lib/rails_ai_context/hydrators/hydration_formatter.rb +56 -0
- data/lib/rails_ai_context/hydrators/schema_hint_builder.rb +73 -0
- data/lib/rails_ai_context/hydrators/view_hydrator.rb +56 -0
- data/lib/rails_ai_context/introspectors/listeners/model_reference_listener.rb +106 -0
- data/lib/rails_ai_context/schema_hint.rb +28 -0
- data/lib/rails_ai_context/tools/get_context.rb +11 -0
- data/lib/rails_ai_context/tools/get_controllers.rb +15 -0
- data/lib/rails_ai_context/tools/get_view.rb +14 -0
- data/lib/rails_ai_context/version.rb +1 -1
- data/server.json +3 -3
- metadata +8 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a9d802d024a373d0552ee2b70cf3d5d755fd79f951b8335da948563a7f488af4
|
|
4
|
+
data.tar.gz: fabf3246fb0df9a97294bca58dac86d4bccc7ffd881fb1da69dc262d03e43fba
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 40fbc9d7455f857de366de800f91ddfa19b9b752cfced2905c2bedc0576a3d54ee4a4c89eccb701904b0eb6cb8babd106cb699182a5eab98981a02d60ff14d6f
|
|
7
|
+
data.tar.gz: e640c259017a222d43a32d15a8c7e70f9888eb3a8df3ad172159efa17371003196ade1f2bf561683a319fa4cebc6b16262f72aee56dea636d2be5fa0a5c7dba7
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,35 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [5.3.0] — 2026-04-07
|
|
9
|
+
|
|
10
|
+
### Added — Phase 2: Cross-Tool Semantic Hydration (Ground Truth Engine Blueprint #38)
|
|
11
|
+
|
|
12
|
+
Controller and view tools now automatically inject schema hints for referenced models, eliminating the need for follow-up tool calls.
|
|
13
|
+
|
|
14
|
+
- **SchemaHint** (`lib/rails_ai_context/schema_hint.rb`) — Immutable `Data.define` value object carrying model ground truth: table, columns, associations, validations, primary key, and `[VERIFIED]`/`[INFERRED]` confidence tag.
|
|
15
|
+
|
|
16
|
+
- **HydrationResult** — Wraps hints + warnings for downstream formatting.
|
|
17
|
+
|
|
18
|
+
- **SchemaHintBuilder** (`lib/rails_ai_context/hydrators/schema_hint_builder.rb`) — Resolves model names to `SchemaHint` objects from cached introspection context. Case-insensitive lookup, batch builder with configurable cap.
|
|
19
|
+
|
|
20
|
+
- **HydrationFormatter** (`lib/rails_ai_context/hydrators/hydration_formatter.rb`) — Renders `SchemaHint` objects as compact Markdown `## Schema Hints` sections with columns (capped at 10), associations, and validations.
|
|
21
|
+
|
|
22
|
+
- **ControllerHydrator** (`lib/rails_ai_context/hydrators/controller_hydrator.rb`) — Parses controller source via Prism AST to detect model references (constant receivers, `params.require` keys, ivar writes), then builds schema hints.
|
|
23
|
+
|
|
24
|
+
- **ViewHydrator** (`lib/rails_ai_context/hydrators/view_hydrator.rb`) — Maps instance variable names to models by convention (`@post` → `Post`, `@posts` → `Post`). Filters framework ivars (page, query, flash, etc.).
|
|
25
|
+
|
|
26
|
+
- **ModelReferenceListener** (`lib/rails_ai_context/introspectors/listeners/model_reference_listener.rb`) — Prism Dispatcher listener for controller-specific model detection. Not registered in `LISTENER_MAP` — used standalone by `ControllerHydrator`.
|
|
27
|
+
|
|
28
|
+
- **Tool integrations:**
|
|
29
|
+
- `GetControllers` — schema hints injected into both action source and controller overview
|
|
30
|
+
- `GetContext` — hydrates combined controller+view ivars in action context mode
|
|
31
|
+
- `GetView` — hydrates instance variables from view templates in standard detail
|
|
32
|
+
|
|
33
|
+
- **Configuration:** `hydration_enabled` (default: true), `hydration_max_hints` (default: 5). Both YAML-configurable.
|
|
34
|
+
|
|
35
|
+
- **65 new specs** covering SchemaHint, HydrationResult, SchemaHintBuilder, HydrationFormatter, ModelReferenceListener, ControllerHydrator, ViewHydrator, tool-level hydration integration (GetControllers, GetView), and configuration (defaults, YAML loading, max_hints propagation).
|
|
36
|
+
|
|
8
37
|
## [5.2.0] — 2026-04-07
|
|
9
38
|
|
|
10
39
|
### Added — Phase 1: Prism AST Foundation (Ground Truth Engine Blueprint #36)
|
data/README.md
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
[](https://registry.modelcontextprotocol.io)
|
|
20
20
|
[](https://github.com/crisnahine/rails-ai-context)
|
|
21
21
|
[](https://github.com/crisnahine/rails-ai-context)
|
|
22
|
-
[](https://github.com/crisnahine/rails-ai-context/actions)
|
|
23
23
|
[](LICENSE)
|
|
24
24
|
|
|
25
25
|
</div>
|
|
@@ -472,7 +472,7 @@ end
|
|
|
472
472
|
## About
|
|
473
473
|
|
|
474
474
|
Built by a Rails developer with 10+ years of production experience.<br>
|
|
475
|
-
|
|
475
|
+
1731 tests. 38 tools. 31 introspectors. Standalone or in-Gemfile.<br>
|
|
476
476
|
MIT licensed. [Contributions welcome.](CONTRIBUTING.md)
|
|
477
477
|
|
|
478
478
|
<br>
|
data/docs/GUIDE.md
CHANGED
|
@@ -431,7 +431,7 @@ rails_get_routes(detail: "standard", limit: 20, offset: 100)
|
|
|
431
431
|
|
|
432
432
|
### rails_get_controllers
|
|
433
433
|
|
|
434
|
-
Returns controller details: actions, filters, strong params, concerns.
|
|
434
|
+
Returns controller details: actions, filters, strong params, concerns. Automatically includes **Schema Hints** for models referenced in the controller (via Prism AST detection).
|
|
435
435
|
|
|
436
436
|
**Parameters:**
|
|
437
437
|
|
|
@@ -549,7 +549,7 @@ rails_get_stimulus(detail: "full")
|
|
|
549
549
|
|
|
550
550
|
### rails_get_view
|
|
551
551
|
|
|
552
|
-
Returns view template contents, partials, and Stimulus controller references.
|
|
552
|
+
Returns view template contents, partials, and Stimulus controller references. In standard detail, includes **Schema Hints** for models inferred from instance variables.
|
|
553
553
|
|
|
554
554
|
**Parameters:**
|
|
555
555
|
|
|
@@ -946,7 +946,7 @@ rails_get_turbo_map(controller: "messages", detail: "full")
|
|
|
946
946
|
|
|
947
947
|
### rails_get_context
|
|
948
948
|
|
|
949
|
-
Get cross-layer context in a single call — combines schema, model, controller, routes, views, stimulus, and tests. Use when you need full context for implementing a feature or modifying an action.
|
|
949
|
+
Get cross-layer context in a single call — combines schema, model, controller, routes, views, stimulus, and tests. Automatically includes **Schema Hints** for models referenced in controller/view code. Use when you need full context for implementing a feature or modifying an action.
|
|
950
950
|
|
|
951
951
|
**Parameters:**
|
|
952
952
|
|
|
@@ -1275,6 +1275,8 @@ end
|
|
|
1275
1275
|
| `server_version` | String | gem version | MCP server version |
|
|
1276
1276
|
| `generate_root_files` | Boolean | `true` | Generate root files (CLAUDE.md, etc.) — set `false` for split rules only |
|
|
1277
1277
|
| `anti_hallucination_rules` | Boolean | `true` | Embed 6-rule Anti-Hallucination Protocol in generated context files — set `false` to skip |
|
|
1278
|
+
| `hydration_enabled` | Boolean | `true` | Inject schema hints into controller/view tool responses |
|
|
1279
|
+
| `hydration_max_hints` | Integer | `5` | Max schema hints per tool response |
|
|
1278
1280
|
| `max_file_size` | Integer | `5_000_000` | Per-file read limit for tools (5MB) |
|
|
1279
1281
|
| `max_test_file_size` | Integer | `1_000_000` | Test file read limit (1MB) |
|
|
1280
1282
|
| `max_schema_file_size` | Integer | `10_000_000` | schema.rb / structure.sql parse limit (10MB) |
|
|
@@ -23,6 +23,7 @@ module RailsAiContext
|
|
|
23
23
|
max_view_file_size max_search_results max_validate_files
|
|
24
24
|
query_timeout query_row_limit query_redacted_columns allow_query_in_production
|
|
25
25
|
log_lines introspectors
|
|
26
|
+
hydration_enabled hydration_max_hints
|
|
26
27
|
].freeze
|
|
27
28
|
|
|
28
29
|
# Load configuration from a YAML file, applying values to the current config instance.
|
|
@@ -211,6 +212,10 @@ module RailsAiContext
|
|
|
211
212
|
# Log reading settings (rails_read_logs)
|
|
212
213
|
attr_accessor :log_lines # Default lines to tail (default: 50)
|
|
213
214
|
|
|
215
|
+
# Hydration: inject schema hints into controller/view tool responses
|
|
216
|
+
attr_accessor :hydration_enabled # Enable/disable hydration (default: true)
|
|
217
|
+
attr_accessor :hydration_max_hints # Max schema hints per response (default: 5)
|
|
218
|
+
|
|
214
219
|
def initialize
|
|
215
220
|
@server_name = "rails-ai-context"
|
|
216
221
|
@introspectors = PRESETS[:full].dup
|
|
@@ -267,6 +272,8 @@ module RailsAiContext
|
|
|
267
272
|
]
|
|
268
273
|
@allow_query_in_production = false
|
|
269
274
|
@log_lines = 50
|
|
275
|
+
@hydration_enabled = true
|
|
276
|
+
@hydration_max_hints = 5
|
|
270
277
|
end
|
|
271
278
|
|
|
272
279
|
def preset=(name)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
# Wraps hydration output: hints for detected models + any warnings.
|
|
5
|
+
HydrationResult = Data.define(:hints, :warnings) do
|
|
6
|
+
def initialize(hints: [], warnings: [])
|
|
7
|
+
super
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def any?
|
|
11
|
+
hints.any?
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Hydrators
|
|
5
|
+
# Parses a controller source file via Prism AST, detects model
|
|
6
|
+
# references, and builds SchemaHint objects for each detected model.
|
|
7
|
+
class ControllerHydrator
|
|
8
|
+
# Hydrate a controller file with schema hints.
|
|
9
|
+
# Returns a HydrationResult with hints for all detected models.
|
|
10
|
+
def self.call(source_path, context:)
|
|
11
|
+
return HydrationResult.new unless source_path && File.exist?(source_path)
|
|
12
|
+
return HydrationResult.new if File.size(source_path) > RailsAiContext.configuration.max_file_size
|
|
13
|
+
|
|
14
|
+
model_names = detect_model_references(source_path)
|
|
15
|
+
return HydrationResult.new if model_names.empty?
|
|
16
|
+
|
|
17
|
+
hints = SchemaHintBuilder.build_many(model_names, context: context, max: RailsAiContext.configuration.hydration_max_hints)
|
|
18
|
+
|
|
19
|
+
warnings = []
|
|
20
|
+
unresolved = model_names - hints.map(&:model_name)
|
|
21
|
+
unresolved.each do |name|
|
|
22
|
+
warnings << "Model '#{name}' referenced but not found in introspection data"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
HydrationResult.new(hints: hints, warnings: warnings)
|
|
26
|
+
rescue => e
|
|
27
|
+
$stderr.puts "[rails-ai-context] ControllerHydrator failed: #{e.message}" if ENV["DEBUG"]
|
|
28
|
+
HydrationResult.new
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Detect model names referenced in a controller source file using Prism AST.
|
|
32
|
+
def self.detect_model_references(source_path)
|
|
33
|
+
parse_result = AstCache.parse(source_path)
|
|
34
|
+
dispatcher = Prism::Dispatcher.new
|
|
35
|
+
listener = Introspectors::Listeners::ModelReferenceListener.new
|
|
36
|
+
|
|
37
|
+
events = []
|
|
38
|
+
events << :on_call_node_enter if listener.respond_to?(:on_call_node_enter)
|
|
39
|
+
events << :on_instance_variable_write_node_enter if listener.respond_to?(:on_instance_variable_write_node_enter)
|
|
40
|
+
dispatcher.register(listener, *events)
|
|
41
|
+
|
|
42
|
+
dispatcher.dispatch(parse_result.value)
|
|
43
|
+
listener.results
|
|
44
|
+
rescue => e
|
|
45
|
+
$stderr.puts "[rails-ai-context] detect_model_references failed: #{e.message}" if ENV["DEBUG"]
|
|
46
|
+
[]
|
|
47
|
+
end
|
|
48
|
+
private_class_method :detect_model_references
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Hydrators
|
|
5
|
+
# Formats SchemaHint objects into Markdown for tool output.
|
|
6
|
+
class HydrationFormatter
|
|
7
|
+
# Format a HydrationResult into a Markdown section.
|
|
8
|
+
def self.format(hydration_result)
|
|
9
|
+
return "" unless hydration_result&.any?
|
|
10
|
+
|
|
11
|
+
lines = [ "## Schema Hints", "" ]
|
|
12
|
+
hydration_result.hints.each do |hint|
|
|
13
|
+
lines << format_hint(hint)
|
|
14
|
+
lines << ""
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
if hydration_result.warnings.any?
|
|
18
|
+
hydration_result.warnings.each { |w| lines << "_#{w}_" }
|
|
19
|
+
lines << ""
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
lines.join("\n")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Format a single SchemaHint as a compact Markdown block.
|
|
26
|
+
def self.format_hint(hint)
|
|
27
|
+
lines = []
|
|
28
|
+
lines << "### #{hint.model_name} #{hint.confidence}"
|
|
29
|
+
lines << "**Table:** `#{hint.table_name}` (pk: `#{hint.primary_key}`)"
|
|
30
|
+
|
|
31
|
+
if hint.columns.any?
|
|
32
|
+
col_summary = hint.columns.first(10).map { |c|
|
|
33
|
+
"`#{c[:name]}` #{c[:type]}#{c[:null] == false ? ' NOT NULL' : ''}"
|
|
34
|
+
}
|
|
35
|
+
col_summary << "... #{hint.columns.size - 10} more" if hint.columns.size > 10
|
|
36
|
+
lines << "**Columns:** #{col_summary.join(', ')}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
if hint.associations.any?
|
|
40
|
+
assoc_list = hint.associations.map { |a| "`#{a[:type]}` :#{a[:name]}" }
|
|
41
|
+
lines << "**Associations:** #{assoc_list.join(', ')}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
if hint.validations.any?
|
|
45
|
+
val_list = hint.validations.map { |v|
|
|
46
|
+
attrs = v[:attributes]&.join(", ") || ""
|
|
47
|
+
"#{v[:kind]}(#{attrs})"
|
|
48
|
+
}
|
|
49
|
+
lines << "**Validations:** #{val_list.join(', ')}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
lines.join("\n")
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Hydrators
|
|
5
|
+
# Builds SchemaHint objects from cached introspection context.
|
|
6
|
+
# Single point of truth for resolving a model name into a
|
|
7
|
+
# structured hydration payload.
|
|
8
|
+
class SchemaHintBuilder
|
|
9
|
+
# Build a SchemaHint for a single model name.
|
|
10
|
+
# Returns nil if the model is not found in context.
|
|
11
|
+
def self.build(model_name, context:)
|
|
12
|
+
models_data = context[:models]
|
|
13
|
+
schema_data = context[:schema]
|
|
14
|
+
return nil unless models_data.is_a?(Hash) && schema_data.is_a?(Hash)
|
|
15
|
+
|
|
16
|
+
# Find model in models context (case-insensitive)
|
|
17
|
+
model_key = models_data.keys.find { |k| k.to_s.casecmp?(model_name) }
|
|
18
|
+
return nil unless model_key
|
|
19
|
+
|
|
20
|
+
model_info = models_data[model_key]
|
|
21
|
+
return nil unless model_info.is_a?(Hash)
|
|
22
|
+
|
|
23
|
+
table_name = model_info[:table_name]
|
|
24
|
+
table_data = schema_data.dig(:tables, table_name) if table_name
|
|
25
|
+
|
|
26
|
+
columns = if table_data
|
|
27
|
+
(table_data[:columns] || []).map do |col|
|
|
28
|
+
{ name: col[:name], type: col[:type], null: col[:null] }.compact
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
[]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
associations = (model_info[:associations] || []).map do |assoc|
|
|
35
|
+
{
|
|
36
|
+
name: assoc[:name],
|
|
37
|
+
type: assoc[:type],
|
|
38
|
+
class_name: assoc[:class_name],
|
|
39
|
+
foreign_key: assoc[:foreign_key]
|
|
40
|
+
}.compact
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
validations = (model_info[:validations] || []).map do |val|
|
|
44
|
+
{
|
|
45
|
+
kind: val[:kind],
|
|
46
|
+
attributes: val[:attributes]
|
|
47
|
+
}.compact
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
primary_key = table_data&.dig(:primary_key) || "id"
|
|
51
|
+
|
|
52
|
+
# Confidence: verified if we have both model data and schema table
|
|
53
|
+
confidence = table_data ? "[VERIFIED]" : "[INFERRED]"
|
|
54
|
+
|
|
55
|
+
SchemaHint.new(
|
|
56
|
+
model_name: model_key.to_s,
|
|
57
|
+
table_name: table_name.to_s,
|
|
58
|
+
columns: columns,
|
|
59
|
+
associations: associations,
|
|
60
|
+
validations: validations,
|
|
61
|
+
primary_key: primary_key.to_s,
|
|
62
|
+
confidence: confidence
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Build SchemaHints for multiple model names.
|
|
67
|
+
# Returns only the ones that resolved successfully.
|
|
68
|
+
def self.build_many(model_names, context:, max: 5)
|
|
69
|
+
model_names.first(max).filter_map { |name| build(name, context: context) }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Hydrators
|
|
5
|
+
# Resolves instance variable references in views to model schema hints.
|
|
6
|
+
# Maps @post → Post, @posts → Post (singularized), etc.
|
|
7
|
+
class ViewHydrator
|
|
8
|
+
# Hydrate view instance variables with schema hints.
|
|
9
|
+
# ivar_names: array of instance variable names (without @) used in a view.
|
|
10
|
+
# Returns a HydrationResult with hints for resolved models.
|
|
11
|
+
def self.call(ivar_names, context:)
|
|
12
|
+
return HydrationResult.new if ivar_names.nil? || ivar_names.empty?
|
|
13
|
+
|
|
14
|
+
model_names = ivar_names.filter_map { |ivar| ivar_to_model_name(ivar) }.uniq
|
|
15
|
+
return HydrationResult.new if model_names.empty?
|
|
16
|
+
|
|
17
|
+
hints = SchemaHintBuilder.build_many(model_names, context: context, max: RailsAiContext.configuration.hydration_max_hints)
|
|
18
|
+
|
|
19
|
+
warnings = []
|
|
20
|
+
unresolved = model_names - hints.map(&:model_name)
|
|
21
|
+
unresolved.each do |name|
|
|
22
|
+
warnings << "@#{name.underscore} used in view but '#{name}' model not found"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
HydrationResult.new(hints: hints, warnings: warnings)
|
|
26
|
+
rescue => e
|
|
27
|
+
$stderr.puts "[rails-ai-context] ViewHydrator failed: #{e.message}" if ENV["DEBUG"]
|
|
28
|
+
HydrationResult.new
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Convert an instance variable name to a model name by convention.
|
|
32
|
+
# @post → "Post", @posts → "Post", @current_user → "User"
|
|
33
|
+
def self.ivar_to_model_name(ivar_name)
|
|
34
|
+
name = ivar_name.to_s
|
|
35
|
+
# Skip framework/common non-model ivars
|
|
36
|
+
return nil if SKIP_IVARS.include?(name)
|
|
37
|
+
|
|
38
|
+
# Singularize and classify: "posts" → "Post", "order_items" → "OrderItem"
|
|
39
|
+
name.singularize.camelize
|
|
40
|
+
rescue StandardError
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
private_class_method :ivar_to_model_name
|
|
44
|
+
|
|
45
|
+
SKIP_IVARS = %w[
|
|
46
|
+
page per_page total_count total_pages
|
|
47
|
+
query search filter sort order
|
|
48
|
+
flash notice alert errors
|
|
49
|
+
breadcrumbs tabs menu
|
|
50
|
+
title description meta
|
|
51
|
+
current_page pagy
|
|
52
|
+
output_buffer virtual_path _request
|
|
53
|
+
].to_set.freeze
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
module RailsAiContext
|
|
6
|
+
module Introspectors
|
|
7
|
+
module Listeners
|
|
8
|
+
# Prism Dispatcher listener that detects model references in controller source.
|
|
9
|
+
# Extracts constant names used as method receivers (Post.find, Post.new),
|
|
10
|
+
# params.require(:post) keys, and instance variable write targets.
|
|
11
|
+
#
|
|
12
|
+
# Not registered in SourceIntrospector::LISTENER_MAP — used standalone
|
|
13
|
+
# by ControllerHydrator since model references are controller-specific.
|
|
14
|
+
class ModelReferenceListener < BaseListener
|
|
15
|
+
attr_reader :constant_references, :require_keys, :ivar_models
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
super
|
|
19
|
+
@constant_references = []
|
|
20
|
+
@require_keys = []
|
|
21
|
+
@ivar_models = {}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Detect: Post.find, Post.where, Post.new, Post.create, etc.
|
|
25
|
+
# Detect: params.require(:post)
|
|
26
|
+
def on_call_node_enter(node)
|
|
27
|
+
extract_constant_receiver(node)
|
|
28
|
+
extract_params_require(node)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Detect: @post = Post.new(...), @post = Post.find(...)
|
|
32
|
+
def on_instance_variable_write_node_enter(node)
|
|
33
|
+
extract_ivar_model(node)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def results
|
|
37
|
+
model_names = Set.new
|
|
38
|
+
|
|
39
|
+
# Constant receivers: Post.find → "Post"
|
|
40
|
+
@constant_references.each { |name| model_names << name }
|
|
41
|
+
|
|
42
|
+
# params.require(:post) → "Post"
|
|
43
|
+
@require_keys.each { |key| model_names << key.to_s.classify }
|
|
44
|
+
|
|
45
|
+
# @post = Post.new → "Post"
|
|
46
|
+
@ivar_models.each_value { |name| model_names << name }
|
|
47
|
+
|
|
48
|
+
model_names.reject { |n| framework_constant?(n) }.sort
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def extract_constant_receiver(node)
|
|
54
|
+
receiver = node.receiver
|
|
55
|
+
return unless receiver
|
|
56
|
+
|
|
57
|
+
case receiver
|
|
58
|
+
when Prism::ConstantReadNode
|
|
59
|
+
@constant_references << receiver.name.to_s
|
|
60
|
+
when Prism::ConstantPathNode
|
|
61
|
+
# Handles Namespaced::Model.find
|
|
62
|
+
@constant_references << constant_path_string(receiver)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def extract_params_require(node)
|
|
67
|
+
return unless node.name == :require
|
|
68
|
+
receiver = node.receiver
|
|
69
|
+
return unless receiver.is_a?(Prism::CallNode) && receiver.name == :params && receiver.receiver.nil?
|
|
70
|
+
|
|
71
|
+
arg = node.arguments&.arguments&.first
|
|
72
|
+
case arg
|
|
73
|
+
when Prism::SymbolNode then @require_keys << arg.value
|
|
74
|
+
when Prism::StringNode then @require_keys << arg.unescaped
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def extract_ivar_model(node)
|
|
79
|
+
value = node.value
|
|
80
|
+
return unless value.is_a?(Prism::CallNode)
|
|
81
|
+
|
|
82
|
+
receiver = value.receiver
|
|
83
|
+
name = case receiver
|
|
84
|
+
when Prism::ConstantReadNode then receiver.name.to_s
|
|
85
|
+
when Prism::ConstantPathNode then constant_path_string(receiver)
|
|
86
|
+
end
|
|
87
|
+
return unless name
|
|
88
|
+
|
|
89
|
+
@ivar_models[node.name.to_s] = name
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
FRAMEWORK_CONSTANTS = %w[
|
|
93
|
+
ApplicationController ActionController ActionDispatch
|
|
94
|
+
ActiveRecord ActiveSupport Rails ApplicationRecord
|
|
95
|
+
ActionView ActionMailer ActiveJob ActiveStorage
|
|
96
|
+
ActionCable ActionMailbox ActionText Turbo
|
|
97
|
+
].to_set.freeze
|
|
98
|
+
|
|
99
|
+
def framework_constant?(name)
|
|
100
|
+
root = name.split("::").first
|
|
101
|
+
FRAMEWORK_CONSTANTS.include?(root)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
# Structured hydration payload representing a model's ground truth.
|
|
5
|
+
# Used by hydrators to inject cross-tool context into controller
|
|
6
|
+
# and view tool responses. Immutable value object via Data.define.
|
|
7
|
+
SchemaHint = Data.define(
|
|
8
|
+
:model_name, # "Post"
|
|
9
|
+
:table_name, # "posts"
|
|
10
|
+
:columns, # [{name: "title", type: "string", null: false}, ...]
|
|
11
|
+
:associations, # [{name: "comments", type: "has_many", class_name: "Comment"}, ...]
|
|
12
|
+
:validations, # [{kind: "presence", attributes: ["title"]}, ...]
|
|
13
|
+
:primary_key, # "id"
|
|
14
|
+
:confidence # "[VERIFIED]" or "[INFERRED]"
|
|
15
|
+
) do
|
|
16
|
+
def verified?
|
|
17
|
+
confidence == "[VERIFIED]"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def column_names
|
|
21
|
+
columns.map { |c| c[:name] }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def association_names
|
|
25
|
+
associations.map { |a| a[:name] }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -109,6 +109,17 @@ module RailsAiContext
|
|
|
109
109
|
ivar_check = cross_reference_ivars(ctrl_ivars, view_ivars, rendered_templates: other_templates)
|
|
110
110
|
lines << "" << ivar_check if ivar_check
|
|
111
111
|
|
|
112
|
+
# Hydrate: inject schema hints for models referenced in controller + view ivars
|
|
113
|
+
if RailsAiContext.configuration.hydration_enabled
|
|
114
|
+
all_ivars = (ctrl_ivars | view_ivars).to_a
|
|
115
|
+
hydration = Hydrators::ViewHydrator.call(all_ivars, context: cached_context)
|
|
116
|
+
hydration_text = Hydrators::HydrationFormatter.format(hydration)
|
|
117
|
+
# Skip if get_controllers already injected schema hints
|
|
118
|
+
unless hydration_text.empty? || lines.any? { |l| l.include?("## Schema Hints") }
|
|
119
|
+
lines << "" << hydration_text
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
112
123
|
text_response(lines.join("\n"))
|
|
113
124
|
rescue => e
|
|
114
125
|
text_response("Error assembling context: #{e.message}")
|
|
@@ -285,6 +285,13 @@ module RailsAiContext
|
|
|
285
285
|
end
|
|
286
286
|
end
|
|
287
287
|
|
|
288
|
+
# Hydrate with schema hints for models referenced in this action
|
|
289
|
+
if RailsAiContext.configuration.hydration_enabled
|
|
290
|
+
hydration = Hydrators::ControllerHydrator.call(source_path.to_s, context: cached_context)
|
|
291
|
+
hydration_text = Hydrators::HydrationFormatter.format(hydration)
|
|
292
|
+
lines << "" << hydration_text unless hydration_text.empty?
|
|
293
|
+
end
|
|
294
|
+
|
|
288
295
|
lines.join("\n")
|
|
289
296
|
end
|
|
290
297
|
|
|
@@ -519,6 +526,14 @@ module RailsAiContext
|
|
|
519
526
|
info[:turbo_stream_actions].each { |a| lines << "- `#{a}`" }
|
|
520
527
|
end
|
|
521
528
|
|
|
529
|
+
# Hydrate with schema hints for models referenced in this controller
|
|
530
|
+
if RailsAiContext.configuration.hydration_enabled
|
|
531
|
+
source_path = Rails.root.join("app", "controllers", "#{name.underscore}.rb")
|
|
532
|
+
hydration = Hydrators::ControllerHydrator.call(source_path.to_s, context: cached_context)
|
|
533
|
+
hydration_text = Hydrators::HydrationFormatter.format(hydration)
|
|
534
|
+
lines << "" << hydration_text unless hydration_text.empty?
|
|
535
|
+
end
|
|
536
|
+
|
|
522
537
|
# Cross-reference hints
|
|
523
538
|
ctrl_path = name.underscore.delete_suffix("_controller")
|
|
524
539
|
model_name = ctrl_path.split("/").last.singularize.camelize
|
|
@@ -153,6 +153,20 @@ module RailsAiContext
|
|
|
153
153
|
end
|
|
154
154
|
lines << ""
|
|
155
155
|
end
|
|
156
|
+
|
|
157
|
+
# Hydrate: inject schema hints for models inferred from view instance variables
|
|
158
|
+
if RailsAiContext.configuration.hydration_enabled && controller
|
|
159
|
+
all_ivars = []
|
|
160
|
+
templates.each do |path, _meta|
|
|
161
|
+
content = read_view_content(path)
|
|
162
|
+
content.scan(/@(\w+)/).flatten.each { |v| all_ivars << v }
|
|
163
|
+
end
|
|
164
|
+
all_ivars.uniq!
|
|
165
|
+
hydration = Hydrators::ViewHydrator.call(all_ivars, context: cached_context)
|
|
166
|
+
hydration_text = Hydrators::HydrationFormatter.format(hydration)
|
|
167
|
+
lines << hydration_text << "" unless hydration_text.empty?
|
|
168
|
+
end
|
|
169
|
+
|
|
156
170
|
text_response(lines.join("\n"))
|
|
157
171
|
|
|
158
172
|
when "full"
|
data/server.json
CHANGED
|
@@ -7,12 +7,12 @@
|
|
|
7
7
|
"url": "https://github.com/crisnahine/rails-ai-context",
|
|
8
8
|
"source": "github"
|
|
9
9
|
},
|
|
10
|
-
"version": "5.
|
|
10
|
+
"version": "5.3.0",
|
|
11
11
|
"packages": [
|
|
12
12
|
{
|
|
13
13
|
"registryType": "mcpb",
|
|
14
|
-
"identifier": "https://github.com/crisnahine/rails-ai-context/releases/download/v5.
|
|
15
|
-
"fileSha256": "
|
|
14
|
+
"identifier": "https://github.com/crisnahine/rails-ai-context/releases/download/v5.3.0/rails-ai-context-mcp.mcpb",
|
|
15
|
+
"fileSha256": "0000000000000000000000000000000000000000000000000000000000000000",
|
|
16
16
|
"transport": {
|
|
17
17
|
"type": "stdio"
|
|
18
18
|
}
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails-ai-context
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.
|
|
4
|
+
version: 5.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- crisnahine
|
|
@@ -224,6 +224,11 @@ files:
|
|
|
224
224
|
- lib/rails_ai_context/doctor.rb
|
|
225
225
|
- lib/rails_ai_context/engine.rb
|
|
226
226
|
- lib/rails_ai_context/fingerprinter.rb
|
|
227
|
+
- lib/rails_ai_context/hydration_result.rb
|
|
228
|
+
- lib/rails_ai_context/hydrators/controller_hydrator.rb
|
|
229
|
+
- lib/rails_ai_context/hydrators/hydration_formatter.rb
|
|
230
|
+
- lib/rails_ai_context/hydrators/schema_hint_builder.rb
|
|
231
|
+
- lib/rails_ai_context/hydrators/view_hydrator.rb
|
|
227
232
|
- lib/rails_ai_context/introspector.rb
|
|
228
233
|
- lib/rails_ai_context/introspectors/action_mailbox_introspector.rb
|
|
229
234
|
- lib/rails_ai_context/introspectors/action_text_introspector.rb
|
|
@@ -248,6 +253,7 @@ files:
|
|
|
248
253
|
- lib/rails_ai_context/introspectors/listeners/enums_listener.rb
|
|
249
254
|
- lib/rails_ai_context/introspectors/listeners/macros_listener.rb
|
|
250
255
|
- lib/rails_ai_context/introspectors/listeners/methods_listener.rb
|
|
256
|
+
- lib/rails_ai_context/introspectors/listeners/model_reference_listener.rb
|
|
251
257
|
- lib/rails_ai_context/introspectors/listeners/scopes_listener.rb
|
|
252
258
|
- lib/rails_ai_context/introspectors/listeners/validations_listener.rb
|
|
253
259
|
- lib/rails_ai_context/introspectors/middleware_introspector.rb
|
|
@@ -270,6 +276,7 @@ files:
|
|
|
270
276
|
- lib/rails_ai_context/middleware.rb
|
|
271
277
|
- lib/rails_ai_context/resources.rb
|
|
272
278
|
- lib/rails_ai_context/safe_file.rb
|
|
279
|
+
- lib/rails_ai_context/schema_hint.rb
|
|
273
280
|
- lib/rails_ai_context/serializers/claude_rules_serializer.rb
|
|
274
281
|
- lib/rails_ai_context/serializers/claude_serializer.rb
|
|
275
282
|
- lib/rails_ai_context/serializers/compact_serializer_helper.rb
|