o
    h                     @  sn   d dl mZ d dlZd dlmZ d dlmZ d dlmZ dd
dZ		ddddZ
	ddddZddddZdS )    )annotationsN)Agent)Handoff)Toolagentr   returnstrc                 C  s6   dg}| t|  | t|  | d d|S )z
    Generates the main graph structure in DOT format for the given agent.

    Args:
        agent (Agent): The agent for which the graph is to be generated.

    Returns:
        str: The DOT format string representing the graph.
    zq
    digraph G {
        graph [splines=true];
        node [fontname="Arial"];
        edge [penwidth=1.5];
    } )appendget_all_nodesget_all_edgesjoin)r   parts r   `/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/extensions/visualization.pyget_main_graph
   s   

r   parentAgent | Nonevisitedset[str] | Nonec                 C  s$  |du rt  }| j|v rdS || j g }|s+|d |d| j d| j d | jD ]}|d|j d|j d q.| jD ]}|d|j d|j d qB| jD ]6}t|trk|d|j	 d|j	 d	 t|t
r|j|vr|d|j d|j d	 |t|| | qVd|S )
z
    Recursively generates the nodes for the given agent and its handoffs in DOT format.

    Args:
        agent (Agent): The agent for which the nodes are to be generated.

    Returns:
        str: The DOT format string representing the nodes.
    Nr
   z"__start__" [label="__start__", shape=ellipse, style=filled, fillcolor=lightblue, width=0.5, height=0.3];"__end__" [label="__end__", shape=ellipse, style=filled, fillcolor=lightblue, width=0.5, height=0.3];"z
" [label="zJ", shape=box, style=filled, fillcolor=lightyellow, width=1.5, height=0.8];zM", shape=ellipse, style=filled, fillcolor=lightgreen, width=0.5, height=0.3];zF", shape=box, style=filled, fillcolor=lightgrey, width=1, height=0.5];zY", shape=box, style=filled, style=rounded, fillcolor=lightyellow, width=1.5, height=0.8];)setnameaddr   toolsmcp_servershandoffs
isinstancer   
agent_namer   r   r   r   r   r   r   tool
mcp_serverhandoffr   r   r   r   "   sD   







r   c                 C  sL  |du rt  }| j|v rdS || j g }|s"|d| j d | jD ]}|d| j d|j d|j d| j d	 q%| jD ]}|d| j d|j d	|j d| j d
	 qA| jD ]1}t|trr|d| j d|j	 d t|t
r|d| j d|j d |t|| | q]| jst| ts|d| j d d|S )aA  
    Recursively generates the edges for the given agent and its handoffs in DOT format.

    Args:
        agent (Agent): The agent for which the edges are to be generated.
        parent (Agent, optional): The parent agent. Defaults to None.

    Returns:
        str: The DOT format string representing the edges.
    Nr
   z"__start__" -> "z";z

        "z" -> "z)" [style=dotted, penwidth=1.5];
        "z" [style=dotted, penwidth=1.5];z)" [style=dashed, penwidth=1.5];
        "z" [style=dashed, penwidth=1.5];z
            "r   z" -> "__end__";)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   c   s\   








r   filename
str | Nonegraphviz.Sourcec                 C  s*   t | }t|}|r|j|ddd |S )aG  
    Draws the graph for the given agent and optionally saves it as a PNG file.

    Args:
        agent (Agent): The agent for which the graph is to be drawn.
        filename (str): The name of the file to save the graph as a PNG.

    Returns:
        graphviz.Source: The graphviz Source object representing the graph.
    pngT)formatcleanup)r   graphvizSourcerender)r   r$   dot_codegraphr   r   r   
draw_graph   s
   
r/   )r   r   r   r   )NN)r   r   r   r   r   r   r   r   )N)r   r   r$   r%   r   r&   )
__future__r   r*   agentsr   agents.handoffsr   agents.toolr   r   r   r   r/   r   r   r   r   <module>   s    
B1