Copilot commented on code in PR #861:
URL: https://github.com/apache/burr/pull/861#discussion_r4433732582
##########
burr/core/graph.py:
##########
@@ -110,6 +110,63 @@ def _render_graphviz(
pathlib.Path(f"{path_without_suffix}.{fmt}").write_bytes(graphviz_obj.pipe(format=fmt))
+def _escape_mermaid_label(value: str) -> str:
+ """Escape text embedded in a Mermaid quoted node or edge label."""
+ return (
+ value.replace("&", "&")
+ .replace('"', """)
+ .replace("<", "<")
+ .replace(">", ">")
+ .replace("\n", "<br/>")
+ )
+
+
+def _render_mermaid(
+ actions: List[Action],
+ transitions: List[Transition],
+ include_conditions: bool,
+ include_state: bool,
+ direction: str = "TD",
+) -> str:
+ """Render a Burr graph as dependency-free Mermaid flowchart text."""
+ valid_directions = {"TB", "TD", "BT", "RL", "LR"}
+ if direction not in valid_directions:
+ raise ValueError(
+ f"Invalid Mermaid direction {direction!r}. Expected one of
{sorted(valid_directions)}"
+ )
+
+ action_ids = {action.name: f"action_{index}" for index, action in
enumerate(actions)}
+ input_ids = {}
+ lines = [f"flowchart {direction}"]
+ for action in actions:
+ label = (
+ action.name
+ if not include_state
+ else f"{action.name}({', '.join(action.reads)}): {',
'.join(action.writes)}"
+ )
+ lines.append(f'
{action_ids[action.name]}["{_escape_mermaid_label(label)}"]')
+ required_inputs, optional_inputs = action.optional_and_required_inputs
+ for input_ in sorted(required_inputs | optional_inputs):
+ if input_.startswith("__"):
+ continue
+ if input_ not in input_ids:
+ input_ids[input_] = f"input_{len(input_ids)}"
+ lines.append(f' {input_ids[input_]}["input:
{_escape_mermaid_label(input_)}"]')
+ lines.append(f" {input_ids[input_]} -.->
{action_ids[action.name]}")
+
+ for transition in transitions:
+ source = action_ids[transition.from_.name]
+ target = action_ids[transition.to.name]
+ if include_conditions and transition.condition is not default:
+ label = _escape_mermaid_label(transition.condition.name)
Review Comment:
`Condition` allows omitting `name` (it defaults to `None`), and
`GraphBuilder` accepts such transitions. For a graph using
`Condition(["count"], resolver)`, enabling `include_conditions=True` raises
`AttributeError` when `_escape_mermaid_label` calls `.replace()` on `None`.
Only emit a labeled edge when the condition has a name; otherwise retain the
unlabeled dotted edge.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]