Ragex.Editor.ValidationAI
(Ragex v0.11.0)
View Source
AI-enhanced validation error explanation and fix suggestions.
Wraps the standard Validator with optional AI explanations that provide:
- Human-readable error explanations
- Specific fix suggestions
- Similar patterns from the codebase
- Learning from past fixes
Usage
alias Ragex.Editor.ValidationAI
# Validate with AI explanations
case ValidationAI.validate_with_explanation(content, path: "lib/file.ex") do
{:ok, :valid} ->
:ok
{:error, errors} ->
# Errors enriched with ai_explanation and ai_suggestion fields
Enum.each(errors, fn error ->
IO.puts("Error: #{error.message}")
if error[:ai_explanation] do
IO.puts("Why: #{error.ai_explanation}")
IO.puts("Fix: #{error.ai_suggestion}")
end
end)
end
# Or enrich existing validation errors
{:error, errors} = Validator.validate(content, opts)
enriched = ValidationAI.explain_errors(errors, content, opts)Configuration
# config/runtime.exs
config :ragex, :ai_features,
validation_error_explanation: true
# Disable for specific call
ValidationAI.validate_with_explanation(content,
path: "test.ex",
ai_explain: false
)
Summary
Functions
Clear the explanation cache.
Check if AI explanations are currently enabled.
Enrich existing validation errors with AI explanations.
Explain a single validation error.
Validate code with AI-enhanced error explanations.
Types
@type enriched_error() :: Ragex.Editor.Types.validation_error()
@type validation_result() :: {:ok, :valid | :no_validator} | {:error, [Ragex.Editor.Types.validation_error()]}
Functions
@spec clear_cache() :: :ok
Clear the explanation cache.
Useful for testing or when you want fresh explanations.
Check if AI explanations are currently enabled.
Takes into account config and optional overrides.
@spec explain_errors([Ragex.Editor.Types.validation_error()], String.t(), keyword()) :: [ enriched_error() ]
Enrich existing validation errors with AI explanations.
Takes errors from standard validation and adds AI-generated explanations and fix suggestions.
Parameters
errors- List of validation errors from Validatorcontent- Original code contentopts- Options (same as validate_with_explanation/2)
Returns
- List of enriched errors with
:ai_explanationand:ai_suggestionfields
Examples
{:error, errors} = Validator.validate(content, path: "lib/file.ex")
enriched = ValidationAI.explain_errors(errors, content,
path: "lib/file.ex"
)
@spec explain_single_error( Ragex.Editor.Types.validation_error(), String.t(), String.t(), non_neg_integer(), keyword() ) :: enriched_error()
Explain a single validation error.
Lower-level function for explaining one error at a time.
Parameters
error- Single validation errorcontent- Code contentfile_path- Path to filesurrounding_lines- Lines of context (default: 3)opts- Additional options
Returns
- Enriched error with AI fields
@spec validate_with_explanation( String.t(), keyword() ) :: validation_result()
Validate code with AI-enhanced error explanations.
This is the main entry point for AI-enhanced validation. It performs standard validation and optionally enriches errors with AI explanations.
Parameters
content- Code content to validateopts- Options::path- File path (for language detection):language- Explicit language override:ai_explain- Enable/disable AI (default: from config):surrounding_lines- Lines of context around error (default: 3):timeout- AI timeout in ms (default: from feature config)
Returns
{:ok, :valid}if validation passes{:ok, :no_validator}if no validator available{:error, [enriched_error]}if validation fails (with AI explanations)
Examples
# Basic usage
ValidationAI.validate_with_explanation(code, path: "lib/module.ex")
# Disable AI for this call
ValidationAI.validate_with_explanation(code,
path: "lib/module.ex",
ai_explain: false
)
# Custom timeout
ValidationAI.validate_with_explanation(code,
path: "lib/module.ex",
timeout: 10_000
)