Ragex.NodeFormatter (Ragex v0.38.0)

View Source

Renders knowledge-graph node identifiers as human-readable strings.

Graph nodes appear in several shapes depending on where they came from:

  • a bare atom — a module name
  • {module, name, arity} — a function reference
  • {:module, id} / {:function, module, name, arity} — tagged variants produced by some graph queries

This module is the single place that knows how to turn any of those into a string. Previously three near-identical private copies existed (in the MCP Resources and Tools handlers and in Ragex.RAG.ContextBuilder) and had already drifted apart, so the same node could render differently depending on which code path reached it.

Styles

Two rendering styles are supported, because the two audiences have different expectations:

  • :mcp (default) — the form MCP clients see. Modules are rendered with Atom.to_string/1, so MyApp.Foo becomes "Elixir.MyApp.Foo", matching the identifiers clients send back in requests. nil is inspected like any other unexpected value.

  • :prompt — the form embedded in LLM prompts by the RAG pipeline. Modules are rendered with inspect/1, so MyApp.Foo becomes "MyApp.Foo" without the Elixir. prefix, which reads better in prose. nil stays nil so callers can use || "Unknown" to substitute a default.

Examples

iex> Ragex.NodeFormatter.format_node_id(MyApp.Foo)
"Elixir.MyApp.Foo"

iex> Ragex.NodeFormatter.format_node_id({MyApp.Foo, :bar, 2})
"Elixir.MyApp.Foo.bar/2"

iex> Ragex.NodeFormatter.format_node_id({:function, MyApp.Foo, :bar, 2})
"Elixir.MyApp.Foo.bar/2"

iex> Ragex.NodeFormatter.format_node_id({MyApp.Foo, :bar, 2}, :prompt)
"MyApp.Foo.bar/2"

iex> Ragex.NodeFormatter.format_node_id(nil, :prompt)
nil

Summary

Functions

Formats a graph node identifier.

Types

style()

@type style() :: :mcp | :prompt

Functions

format_node_id(node, style \\ :mcp)

@spec format_node_id(term(), style()) :: String.t() | nil

Formats a graph node identifier.

Parameters

  • node — the node identifier in any supported shape
  • style — :mcp (default) or :prompt, see the module docs

Returns

A string, or nil when node is nil and style is :prompt.