Class: GraphAgent::Graph::MermaidVisualizer

Inherits:
Object
  • Object
show all
Defined in:
lib/graph_agent/graph/mermaid_visualizer.rb

Constant Summary collapse

START_LABEL =
"START"
END_LABEL =
"END"
CONDITION_PREFIX =
"cond_"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_graph, options = {}) ⇒ MermaidVisualizer

Returns a new instance of MermaidVisualizer.



17
18
19
20
# File 'lib/graph_agent/graph/mermaid_visualizer.rb', line 17

def initialize(state_graph, options = {})
  @graph = state_graph
  @options = options
end

Class Method Details

.render(state_graph, options = {}) ⇒ Object

Generate Mermaid diagram for a StateGraph



13
14
15
# File 'lib/graph_agent/graph/mermaid_visualizer.rb', line 13

def self.render(state_graph, options = {})
  new(state_graph, options).render
end

Instance Method Details

#renderObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graph_agent/graph/mermaid_visualizer.rb', line 22

def render
  lines = ["graph TD"]
  lines << _style_definitions
  lines << ""

  # Render entry point (START)
  lines << _node_definition(START.to_s, START_LABEL, :start)

  # Render all graph nodes
  @graph.nodes.each do |name, node|
    lines << _node_definition(name, _node_label(name, node), :node)
  end

  # Render exit point (END)
  lines << _node_definition(END_NODE.to_s, END_LABEL, :end)

  lines << ""

  # Render regular edges
  @graph.edges.each do |edge|
    lines << _edge_definition(edge)
  end

  # Render conditional edges (branches)
  @graph.branches.each do |source, branches|
    branches.each_with_index do |(name, branch), idx|
      cond_id = "#{source}_#{CONDITION_PREFIX}#{idx}"
      lines << _node_definition(cond_id, _condition_label(branch), :condition)

      # Edge from source to condition node
      lines << "  #{_safe_id(source)} --> #{_safe_id(cond_id)}"

      # Edges from condition to targets
      if branch.path_map
        _render_path_map_edges(branch, cond_id, lines)
      else
        # Simple condition - render with note
        lines << "  #{_safe_id(cond_id)} -.->|condition| #{_safe_id(source)}_next"
        lines << "  #{_safe_id(source)}_next[\"?\"]"
      end
    end
  end

  # Render waiting edges (multi-source edges)
  @graph.waiting_edges.each do |(sources, target)|
    sources.each do |source|
      lines << "  #{_safe_id(source)} --> #{_safe_id(target)}"
    end
  end

  lines.join("\n")
end