-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Visualization Extension] Enhance agent graph visualization to prevent infinite recursion, add mermaid graph #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…with recursive handoffs
Hey @rm-openai I noticed the original PR hasn’t had updates, so I’ve created a follow-up here with additional fixes and improvements. Happy to adjust things if needed. |
Addressed #387 (comment) and #387 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, PR is looking great! Some changes requested inline
|
||
@dataclass(frozen=True) | ||
class Edge: | ||
source: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't these be Node
not str
?
def _add_agent_nodes_and_edges( | ||
self, | ||
agent: Agent, | ||
parent: Optional[Agent], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use Agent | None instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment
graph: Graph, | ||
) -> None: | ||
# Add agent node | ||
graph.add_node(Node(agent.name, agent.name, NodeType.AGENT)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using agent.name
as the ID isn't safe unfortunately, since agents can have the same name. You'd need to do an instance equality check everywhere instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works when agent.handoffs have Agent instances. but apparently it's a bit tricky when Handoff objects are passed. It only has the target agent's name (handoff.agent_name), not an Agent instance. The instance itself only gets resolved at runtime with handoff.on_invoke_handoff. so we can't consistently do instance equality checks to link Handoff instances back to the Agent instances at build time if we consider cases with recursive handoffs. The only option I see is to resolve this using agent names but mention the caveat in the docs. any suggestions?
|
||
|
||
def draw_graph( | ||
agent: Agent, filename: Optional[str] = None, renderer: str = "graphviz" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renderer
should be either a Literal or an enum here
Also please add str | None = None
instead of Optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as before
should be looking good now |
response = requests.get(f"https://mermaid.ink/img/{base64_string}") | ||
response.raise_for_status() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this - relying on a 3rd party service that could return anything or leak information.
I'd actually prefer if we got rid of the Renderer base class, and instead had:
class GraphVizRenderer:
@classmethod
def render_to_file(...)
@classmethod
def render_to_object(...)
class MermaidRenderer:
@classmethod
def render_mermaid_ink_link(...)
@classmethod
def render_mermaid_text(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, on it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one more change sorry!
Improves agent graph visualization logic:
Improves on PR #387, addressing pending change requests.