agentgraphgenerator.go

Overview

The agentgraphgenerator.go file is responsible for generating a visual graph representation of agents and their relationships within the system. It uses the gographviz library to construct directed graphs depicting agents, their sub-agents, tools they incorporate, and their interaction structure. The generated graph is designed to highlight agent clusters (composite agents composed of sub-agents), indicate agent and tool types through node shapes and captions, and visually emphasize specified nodes or edges.

This visualization supports understanding the hierarchical and interaction structure of AI agents, including special cluster agents such as sequential, loop, and parallel agents that orchestrate complex workflows. The primary output is a graph description in DOT language format, suitable for rendering with Graphviz-compatible tools.


Key Concepts and Components

Supported Cluster Agents

The file defines a list of supported cluster agent types:

These special agent types are responsible for complex orchestration patterns such as looping through sub-agents, sequential execution, or parallel execution of sub-agents.

Named Instances

The interface namedInstance requires a single method:

type namedInstance interface {
    Name() string
}

This abstraction is used to generically handle agents and tools by their names.


Functions and Methods

nodeName(instance any) string

Returns the name of the given instance, which may be either an agent.Agent or a tool.Tool. If the instance type is unknown, it returns "Unknown instance type".

Parameters:

Returns:


nodeCaption(instance any) string

Generates a caption label for graph nodes representing the instance. It prepends an emoji based on the type (robot for agent, wrench for tool). For supported cluster agents, it appends the agent type in parentheses.

Parameters:

Returns:


nodeShape(instance any) string

Determines the Graphviz shape attribute for nodes, based on instance type:

Parameters:

Returns:


shouldBuildAgentCluster(instance any) bool

Checks if the given instance is a cluster agent type that should be drawn as a subgraph cluster.

Parameters:

Returns:


highlighted(nodeName string, highlightedPairs [][]string) bool

Determines whether a node should be highlighted based on its presence in any of the provided pairs.

Parameters:

Returns:


edgeHighlighted(from, to string, highlightedPairs [][]string) *bool

Checks if an edge between two nodes should be highlighted, considering directionality. Returns a pointer to a boolean indicating:

Parameters:

Returns:


drawCluster(parentGraph, cluster *gographviz.Graph, agent agent.Agent, highlightedPairs [][]string, visitedNodes map[string]bool) error

Builds a subgraph cluster for a composite agent (cluster agent). It recursively processes sub-agents, draws their nodes and edges according to agent type:

Parameters:

Returns:


drawNode(graph, parentGraph *gographviz.Graph, instance any, highlightedPairs [][]string, visitedNodes map[string]bool) error

Draws a node in the graph for the given instance. If the instance is a cluster agent, it creates a subgraph cluster and delegates drawing to drawCluster. Otherwise, it adds a single node with appropriate attributes including highlighting.

Parameters:

Returns:


drawEdge(graph *gographviz.Graph, from, to string, highlightedPairs [][]string) error

Adds a directed edge from from node to to node with attributes indicating highlight status and arrow style.

Parameters:

Returns:


buildGraph(graph, parentGraph *gographviz.Graph, instance any, highlightedPairs [][]string, visitedNodes map[string]bool) error

Recursively constructs the graph starting from the root instance. It draws the node or cluster, adds nodes for tools (for LLM agents), connects agent to tools via edges, and recursively processes sub-agents.

Parameters:

Returns:


GetAgentGraph(ctx context.Context, agent agent.Agent, highlightedPairs [][]string) (string, error)

Entry point to generate the graph of an agent. It initializes the graph with attributes (name, direction, background color), invokes buildGraph to recursively construct the agent graph, and returns the graph in DOT format as a string.

Parameters:

Returns:

Example Usage:

dotGraph, err := GetAgentGraph(ctx, myAgent, [][]string{{"AgentA", "ToolX"}})
if err != nil {
    log.Fatal(err)
}
// dotGraph now contains the DOT format of the agent graph.

Implementation Details and Algorithms


Interaction with Other System Components


Mermaid Diagram: Structure of agentgraphgenerator.go

classDiagram
class agentgraphgenerator {
+GetAgentGraph()
-buildGraph()
-drawNode()
-drawCluster()
-drawEdge()
-nodeName()
-nodeCaption()
-nodeShape()
-shouldBuildAgentCluster()
-highlighted()
-edgeHighlighted()
-boolPtr()
}
agentgraphgenerator ..> gographviz.Graph : uses
agentgraphgenerator ..> agent.Agent : uses
agentgraphgenerator ..> tool.Tool : uses

This documentation details the functional components and usage of the agentgraphgenerator.go file, focusing on generating hierarchical visual graphs of agents and their subcomponents with contextual highlights. For further context on agent types and workflows, see [Agent Workflow Management](80558) and [LLM Integration and Agents](80562).