Nasty.Interop.RagexBridge (Nasty v0.3.0)

View Source

Optional integration with Ragex knowledge graph for context-aware code generation.

This module provides utilities to query the Ragex knowledge graph for:

  • Available functions in the codebase
  • Function signatures and documentation
  • Semantic similarity search for function suggestions

The bridge gracefully degrades if Ragex is not available or not running.

Usage

# Check if Ragex is available
if RagexBridge.available?() do
  # Query for function suggestions
  {:ok, functions} = RagexBridge.suggest_functions("sort a list")
end

Configuration

The bridge can be configured via application environment:

config :nasty, :ragex,
  enabled: true,
  path: "/path/to/ragex"

Summary

Functions

Checks if Ragex integration is available and enabled.

Enhances an intent with context from the knowledge graph.

Queries the knowledge graph for modules matching a pattern.

Gets function signature and documentation from the knowledge graph.

Suggests functions from the codebase based on natural language query.

Functions

available?()

@spec available?() :: boolean()

Checks if Ragex integration is available and enabled.

Examples

iex> RagexBridge.available?()
false  # Unless Ragex is configured and running

enhance_intent(intent)

@spec enhance_intent(Nasty.AST.Intent.t()) ::
  {:ok, Nasty.AST.Intent.t()} | {:error, term()}

Enhances an intent with context from the knowledge graph.

Adds suggestions for available functions that match the intent's action.

Examples

intent = %Intent{type: :action, action: "sort", target: "list"}
{:ok, enhanced} = RagexBridge.enhance_intent(intent)
# intent.metadata will include :ragex_suggestions

find_modules(pattern)

@spec find_modules(String.t()) :: {:ok, [String.t()]} | {:error, term()}

Queries the knowledge graph for modules matching a pattern.

Examples

{:ok, modules} = RagexBridge.find_modules("Enum")
# => ["Enum", "Enumerable"]

get_function_info(module, function, arity \\ nil)

@spec get_function_info(String.t(), String.t(), non_neg_integer() | nil) ::
  {:ok, map()} | {:error, term()}

Gets function signature and documentation from the knowledge graph.

Parameters

  • module - Module name (e.g., "Enum")
  • function - Function name (e.g., "sort")
  • arity - Function arity (optional)

Returns

  • {:ok, %{signature: String.t(), doc: String.t(), examples: [String.t()]}}
  • {:error, :not_found} - Function not in knowledge graph
  • {:error, :ragex_unavailable} - If Ragex is not available

Examples

{:ok, info} = RagexBridge.get_function_info("Enum", "sort", 1)
# => %{signature: "sort(enumerable)", doc: "Sorts...", ...}

suggest_functions(query, opts \\ [])

@spec suggest_functions(
  String.t(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}

Suggests functions from the codebase based on natural language query.

Uses semantic search via Ragex's vector embeddings to find relevant functions.

Parameters

  • query - Natural language description of desired functionality
  • opts - Options:
    • :limit - Maximum number of suggestions (default: 5)
    • :threshold - Minimum similarity score 0.0-1.0 (default: 0.7)
    • :module - Filter by module name (optional)

Returns

  • {:ok, [%{name: String.t(), module: String.t(), doc: String.t(), score: float()}]}
  • {:error, :ragex_unavailable} - If Ragex is not available
  • {:error, reason} - Other errors

Examples

{:ok, suggestions} = RagexBridge.suggest_functions("sort a list")
# => [{%{name: "sort", module: "Enum", doc: "Sorts...", score: 0.95}, ...}]