core[patch]: wrap mermaid node names w/ markdown in <p> tag (#26235)

This fixes the issue where `__start__` and `__end__` node labels are
being interpreted as markdown, as of the most recent Mermaid update
This commit is contained in:
Vadym Barda
2024-09-09 20:11:00 -04:00
committed by GitHub
parent 3e48c728d5
commit bab9de581c
2 changed files with 16 additions and 8 deletions

View File

@@ -11,6 +11,8 @@ from langchain_core.runnables.graph import (
NodeStyles,
)
MARKDOWN_SPECIAL_CHARS = "*_`"
def draw_mermaid(
nodes: Dict[str, Node],
@@ -58,13 +60,19 @@ def draw_mermaid(
default_class_label = "default"
format_dict = {default_class_label: "{0}({1})"}
if first_node is not None:
format_dict[first_node] = "{0}([{0}]):::first"
format_dict[first_node] = "{0}([{1}]):::first"
if last_node is not None:
format_dict[last_node] = "{0}([{0}]):::last"
format_dict[last_node] = "{0}([{1}]):::last"
# Add nodes to the graph
for key, node in nodes.items():
label = node.name.split(":")[-1]
node_name = node.name.split(":")[-1]
label = (
f"<p>{node_name}</p>"
if node_name.startswith(tuple(MARKDOWN_SPECIAL_CHARS))
and node_name.endswith(tuple(MARKDOWN_SPECIAL_CHARS))
else node_name
)
if node.metadata:
label = (
f"{label}<hr/><small><em>"