Ragex.AI.Features.Context
(Ragex v0.10.0)
View Source
Context building helpers for AI-enhanced features.
Provides utilities to build rich context from the knowledge graph and embeddings for AI prompts. This ensures AI responses are grounded in actual codebase data.
Usage
alias Ragex.AI.Features.Context
# Build context for validation error
context = Context.for_validation_error(error, file_path, surrounding_code)
# Build context for refactoring preview
context = Context.for_refactor_preview(operation, params, affected_files)
# Build context for dead code analysis
context = Context.for_dead_code_analysis(function_ref, callers, callees)
Summary
Functions
Build context for complexity explanation.
Build context for dead code confidence refinement.
Build context for dependency insights.
Build context for duplication semantic analysis.
Build context for refactoring preview commentary.
Build context for validation error explanation.
Convert context map to string format for AI prompt.
Types
Functions
@spec for_complexity_explanation(tuple(), map(), keyword()) :: context_map()
Build context for complexity explanation.
Includes:
- Function AST or code
- Complexity metrics breakdown
- Similar functions with lower complexity
- Common patterns
Parameters
function_ref- {:function, module, name, arity} tuplecomplexity_metrics- Map of complexity metricsopts- Additional options
Returns
- Context map suitable for AI prompt
@spec for_dead_code_analysis( tuple(), keyword() ) :: context_map()
Build context for dead code confidence refinement.
Includes:
- Function reference and basic info
- Call graph (who calls it, if anyone)
- Module behavior declarations
- Function documentation and naming patterns
- Similar functions in codebase
Parameters
function_ref- {:function, module, name, arity} tupleopts- Additional options
Returns
- Context map suitable for AI prompt
@spec for_dependency_insights(module(), map(), keyword()) :: context_map()
Build context for dependency insights.
Includes:
- Module coupling metrics
- Dependency graph
- Similar modules in codebase
- Architectural patterns
Parameters
module- Module atommetrics- Coupling metrics mapopts- Additional options
Returns
- Context map suitable for AI prompt
@spec for_duplication_analysis(String.t(), String.t(), float(), keyword()) :: context_map()
Build context for duplication semantic analysis.
Includes:
- Both code snippets
- AST similarity metrics
- Embedding similarity
- Call patterns
- Usage context
Parameters
code1- First code snippetcode2- Second code snippetsimilarity_score- Pre-calculated similarity scoreopts- Additional options
Returns
- Context map suitable for AI prompt
@spec for_refactor_preview(atom(), map(), [String.t()], keyword()) :: context_map()
Build context for refactoring preview commentary.
Includes:
- Operation type and parameters
- Affected files and their relationships
- Call graph context (who calls these functions)
- Similar refactorings in the codebase
- Complexity metrics
Parameters
operation- Refactoring operation atomparams- Operation parametersaffected_files- List of file paths that will be modifiedopts- Additional options
Returns
- Context map suitable for AI prompt
@spec for_validation_error(map(), String.t(), String.t() | nil, keyword()) :: context_map()
Build context for validation error explanation.
Includes:
- The error message and location
- Surrounding code context
- Similar code patterns from the codebase
- Common fixes for this error type
Parameters
error- Validation error map with message, line, columnfile_path- Path to file with errorsurrounding_code- Code around the error (optional)opts- Additional options
Returns
- Context map suitable for AI prompt
@spec to_prompt_string(context_map()) :: String.t()
Convert context map to string format for AI prompt.
Formats the context in a human-readable way suitable for inclusion in AI prompts.
Parameters
context- Context map from any of the builder functions
Returns
- Formatted string