Spaces:
Sleeping
Sleeping
File size: 19,733 Bytes
8edee29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | """
agents/flowchart_agent.py
--------------------------
Flowchart Agent for AutoDevAgent.
Generates a Mermaid.js flowchart diagram describing the logic flow
of the final working code. Only triggered when:
- The code is longer than settings.flowchart_min_lines (default 20), OR
- The user explicitly requested a flowchart
Mermaid.js diagrams are plain text that render as visual flowcharts
in the Gradio UI via gr.HTML() with the Mermaid CDN library.
Design:
- Uses the fast model (8B) β flowchart generation is a simpler task
than code writing and 8B is sufficient for diagram syntax.
- Instructs the LLM to output only valid Mermaid flowchart syntax.
- Validates that the output starts with "flowchart" or "graph" to
catch cases where the LLM returned prose instead of a diagram.
- Flowchart quality degrades for complex nested logic β documented
in README as a known limitation. Best for functions and short scripts.
Usage:
from agents.flowchart_agent import FlowchartAgent, should_generate_flowchart
from pipeline.state import PipelineState
if should_generate_flowchart(state, user_requested=False):
agent = FlowchartAgent()
updated = agent.run(state)
print(updated["flowchart_mermaid"])
"""
import logging
from typing import Any
from langchain_groq import ChatGroq
from langchain_core.messages import SystemMessage, HumanMessage
from config import settings
from pipeline.state import (
PipelineState,
PipelineStatus,
Language,
)
logger = logging.getLogger(__name__)
# ------------------------------------------------------------------ #
# Prompt #
# ------------------------------------------------------------------ #
FLOWCHART_SYSTEM = """
You are a diagram generator. Describe the logical steps of the given code as a simple Mermaid flowchart.
Output ONLY raw Mermaid syntax β no explanation, no code fences, no markdown.
STRICT RULES β follow every rule exactly or the diagram will fail to render:
1. First line: graph TD (exactly this)
2. Node IDs: single capital letters only β A, B, C, D ... (max 10 nodes)
3. Node shape: RECTANGLES ONLY β A["label text"]
- NEVER use diamonds {} or round nodes ()
4. Labels: 2 to 8 plain English words only
- FORBIDDEN characters: " ' ; : & < > | ( ) , . / * % = [ ] { }
- No SQL keywords, no code syntax, no punctuation
- Use words like "Check condition" or "Repeat for each item" to imply logic
5. Edges: plain arrows only β A --> B
- NEVER add edge labels (no -->|Yes|)
- NEVER loop back to an earlier node
- Use forward arrows only (top to bottom)
6. Max 10 nodes total
7. If the code contains branches or loops, describe them as sequential steps (e.g., "Check base case", "Handle base case", "Recursive case", "Combine results")
8. CONNECTIVITY β the graph must be a single connected component:
- Every node except A must have at least one incoming arrow
- Every node except the final node must have at least one outgoing arrow
- Every node must be reachable by following arrows from A
- NEVER define a node that is not connected to the main flow from A
CORRECT example for "fibonacci with memoisation":
graph TD
A["Start"]
B["Base case check"]
C["Return base value"]
D["Check memo cache"]
E["Return cached value"]
F["Compute recursively"]
G["Store in memo"]
H["Return result"]
A --> B
B --> C
B --> D
D --> E
D --> F
F --> G
G --> H
""".strip()
SQL_FLOWCHART_SYSTEM = """
You are a diagram generator. Describe the logical steps of the given SQL query as a simple Mermaid flowchart.
Output ONLY raw Mermaid syntax β no explanation, no code fences, no markdown.
STRICT RULES β follow every rule exactly or the diagram will fail to render:
1. First line: graph TD (exactly this)
2. Node IDs: single capital letters only β A, B, C, D ... (max 10 nodes)
3. Node shape: RECTANGLES ONLY β A["label text"]
- NEVER use diamonds {} or round nodes ()
4. Labels: 2 to 8 plain English words only
- FORBIDDEN characters: " ' ; : & < > | ( ) , . / * % = [ ] { }
- FORBIDDEN SQL keywords: SELECT FROM WHERE JOIN GROUP ORDER COUNT SUM AVG MAX MIN
- Describe what the step DOES in plain English (e.g. "Filter recent orders", "Calculate running total")
- For subqueries, use a step like "Prepare subquery results"
5. Edges: plain arrows only β A --> B
- NEVER add edge labels
- No duplicate edges, no loops
6. Max 10 nodes total
7. CONNECTIVITY β the graph must be a single connected component:
- Every node except A must have at least one incoming arrow
- Every node except the final node must have at least one outgoing arrow
- Every node must be reachable by following arrows from A
- NEVER define a node that is not connected to the main flow from A
CORRECT example for "top 3 products by sales":
graph TD
A["Start"]
B["Load products table"]
C["Load sales table"]
D["Join tables together"]
E["Sum sales per product"]
F["Sort by total descending"]
G["Keep top three rows"]
H["Return results"]
A --> B
B --> C
C --> D
D --> E
E --> F
F --> G
G --> H
""".strip()
FLOWCHART_FIX_SYSTEM = """
You are fixing broken Mermaid.js syntax. Rewrite the diagram following these exact rules.
RULES (all mandatory β no exceptions):
1. First line must be exactly: graph TD
2. Node IDs: single capital letters A, B, C, D ...
3. RECTANGLES ONLY: A["plain label"]
- Remove ALL diamond nodes {} β replace with rectangles
- Remove ALL round nodes () β replace with rectangles
4. Labels: 2 to 6 plain English words. No special characters at all.
- Remove: " ' ; : & < > | ( ) , . / * % = [ ] { }
5. Edges: A --> B only β no edge labels, no loops, no duplicate edges
6. Max 10 nodes
7. CONNECTIVITY β single connected component from A:
- Every node except A must have at least one incoming arrow
- Every node except the final node must have at least one outgoing arrow
- Remove or reconnect any node that is not reachable from A
Output ONLY the corrected raw Mermaid syntax. No explanation. No code fences.
""".strip()
# ------------------------------------------------------------------ #
# Gate function #
# ------------------------------------------------------------------ #
def should_generate_flowchart(
state: PipelineState,
user_requested: bool = False,
) -> bool:
"""
Decide whether to generate a flowchart for the current code.
SQL tasks: always generate β even a short query has a meaningful
logical flow (filter β aggregate β sort) that benefits from a diagram.
Python tasks: only generate when the code exceeds the configured
minimum line threshold, or when the user explicitly requested one.
Args:
state: Current PipelineState.
user_requested: True if the user clicked "Generate flowchart".
Returns:
True if a flowchart should be generated, False otherwise.
"""
if user_requested:
return True
if state.language == Language.SQL:
logger.info("FlowchartAgent: triggering β SQL task always gets a flowchart")
return True
code = state.final_code()
line_count = len(code.strip().splitlines())
if line_count >= settings.flowchart_min_lines:
logger.info(
"FlowchartAgent: triggering β code is %d lines (threshold: %d)",
line_count,
settings.flowchart_min_lines,
)
return True
logger.debug(
"FlowchartAgent: skipping β code is %d lines (threshold: %d)",
line_count,
settings.flowchart_min_lines,
)
return False
# ------------------------------------------------------------------ #
# Agent #
# ------------------------------------------------------------------ #
class FlowchartAgent:
"""
Generates Mermaid.js flowchart syntax for the final code.
Uses the fast 8B model since flowchart generation is a simpler
structured output task than code generation or debugging.
Attributes:
llm: ChatGroq instance using the fast model (8B).
"""
def __init__(self) -> None:
"""LLM client is built lazily in run() once model assignments are known."""
self.llm = None
def _build_llm(self, model: str) -> "ChatGroq":
return ChatGroq(
api_key=settings.groq_api_key,
model=model,
temperature=0.1,
max_tokens=600,
request_timeout=settings.groq_request_timeout,
)
def run(self, state: PipelineState) -> dict[str, Any]:
"""
Generate a Mermaid.js flowchart for the code in state.
Args:
state: Current PipelineState. Reads: task, language,
final_code().
Returns:
Partial state dict with keys:
- "flowchart_mermaid": str β Mermaid diagram syntax
- "status": PipelineStatus.EXPLAINING
"""
ma = state.model_assignments or {}
model = ma.get("flowchart", settings.groq_model_fast)
self.llm = self._build_llm(model)
logger.info("FlowchartAgent using model: %s", model)
code = state.final_code()
if not code.strip():
logger.warning("FlowchartAgent: no code to diagram")
return {
"flowchart_mermaid": "",
"status": PipelineStatus.EXPLAINING,
}
logger.info(
"FlowchartAgent generating diagram for %d lines of %s code",
len(code.splitlines()),
state.language.value,
)
system_prompt = (
SQL_FLOWCHART_SYSTEM
if state.language == Language.SQL
else FLOWCHART_SYSTEM
)
messages = [
SystemMessage(content=system_prompt),
HumanMessage(content=(
f"Task: {state.task}\n\n"
f"Code:\n{code}"
)),
]
# ββ LLM call ββββββββββββββββββββββββββββββββββββββββββββββββ #
try:
response = self.llm.invoke(messages)
raw = response.content.strip()
except Exception as e:
logger.error("FlowchartAgent LLM call failed: %s", e)
raise RuntimeError(f"Flowchart agent LLM call failed: {e}") from e
# ββ Layer 2: Strip fences + validate βββββββββββββββββββββββ #
mermaid = _clean_mermaid(raw)
# ββ Track token usage βββββββββββββββββββββββββββββββββββββββ #
from observability.langsmith_tracer import extract_token_usage_from_response
prompt_t, completion_t = extract_token_usage_from_response(response)
updated_token_usage = state.token_usage.model_copy()
updated_token_usage.add(prompt_t, completion_t)
if not _is_valid_mermaid(mermaid):
logger.warning(
"FlowchartAgent: output does not look like valid Mermaid syntax β discarding"
)
return {
"flowchart_mermaid": "",
"status": PipelineStatus.EXPLAINING,
"token_usage": updated_token_usage,
}
is_valid, issues = _validate_mermaid_syntax(mermaid)
# ββ Layer 3: LLM retry with error feedback ββββββββββββββββββ #
if not is_valid:
error_summary = "; ".join(issues)
logger.warning(
"FlowchartAgent: Layer 2 detected %d issue(s): %s β retrying with fix prompt",
len(issues), error_summary,
)
try:
fix_messages = [
SystemMessage(content=FLOWCHART_FIX_SYSTEM),
HumanMessage(content=(
f"Broken diagram (errors: {error_summary}):\n\n{mermaid}"
)),
]
fix_response = self.llm.invoke(fix_messages)
fixed_raw = fix_response.content.strip()
fixed_mermaid = _clean_mermaid(fixed_raw)
# Count fix attempt tokens
fix_pt, fix_ct = extract_token_usage_from_response(fix_response)
updated_token_usage.add(fix_pt, fix_ct)
fixed_valid, fixed_issues = _validate_mermaid_syntax(fixed_mermaid)
if fixed_valid and _is_valid_mermaid(fixed_mermaid):
logger.info("FlowchartAgent: Layer 3 fix succeeded")
mermaid = fixed_mermaid
else:
logger.warning(
"FlowchartAgent: Layer 3 fix still invalid (%s) β passing to Layer 4 rebuild",
"; ".join(fixed_issues),
)
mermaid = fixed_mermaid # Layer 4 (parseβrebuild in app.py) handles it
except Exception as fix_err:
logger.warning("FlowchartAgent: Layer 3 retry failed: %s β passing to Layer 4", fix_err)
logger.info(
"FlowchartAgent produced %d line diagram", len(mermaid.splitlines())
)
return {
"flowchart_mermaid": mermaid,
"status": PipelineStatus.EXPLAINING,
"token_usage": updated_token_usage,
}
# ------------------------------------------------------------------ #
# Helpers #
# ------------------------------------------------------------------ #
def _validate_mermaid_syntax(text: str) -> tuple[bool, list[str]]:
"""
Layer 2 β Syntax validation.
Enforces the strict simple-flowchart rules:
1. Must start with 'graph' or 'flowchart'
2. No diamond {} nodes (replaced by strict prompt, but catch LLM drift)
3. No edge labels (|...|) β pipe chars inside edges are a common error
4. No unbalanced brackets on node-definition lines
5. Forbidden characters inside quoted labels
6. At least one edge (-->) present
7. No more than 12 nodes (safety cap β prompt says max 10)
"""
import re
issues: list[str] = []
if not text or not text.strip():
return False, ["Empty output"]
lines = [l.strip() for l in text.splitlines() if l.strip()]
# 1. Must start with graph / flowchart
if not lines[0].lower().startswith(("graph", "flowchart")):
issues.append(f"Missing graph header β first line is: '{lines[0]}'")
# 2. Diamond nodes {} are forbidden β they cause Mermaid v11 syntax errors
diamond_re = re.compile(r'\b[A-Za-z][A-Za-z0-9_]*\s*\{')
for line in lines:
if "-->" in line:
continue
if diamond_re.search(line):
issues.append(f"Diamond node detected (forbidden): {line}")
break
# 3. Edge labels with pipes e.g. A -->|Yes| B β forbidden in strict mode
edge_label_re = re.compile(r'--+>\s*\|')
for line in lines:
if edge_label_re.search(line):
issues.append(f"Edge label with pipe detected (forbidden): {line}")
break
# 4. Unbalanced brackets on node-definition lines
for line in lines:
if "-->" in line or "---" in line:
continue
opens = line.count("[") + line.count("{") + line.count("(")
closes = line.count("]") + line.count("}") + line.count(")")
if opens != closes:
issues.append(f"Unbalanced brackets: {line}")
break
# 5. Forbidden chars inside quoted labels
forbidden_re = re.compile(r'"[^"]*[;:&<>|][^"]*"')
for line in lines:
if forbidden_re.search(line):
issues.append(f"Forbidden characters in label: {line}")
break
# 6. Must have at least one edge
has_edge = any("-->" in l for l in lines)
if not has_edge:
issues.append("No edges found β diagram has no connections")
# 7. Node count sanity check (safety cap)
node_def_re = re.compile(r'^\s*[A-Za-z][A-Za-z0-9_]*\s*[\[({]')
node_count = sum(1 for l in lines if node_def_re.match(l) and "-->" not in l)
if node_count > 12:
issues.append(f"Too many nodes ({node_count}) β max 12 allowed")
# 8. Connectivity β every non-root node must have an incoming edge;
# every non-terminal node must have an outgoing edge; graph must
# be a single connected component reachable from A.
edge_re = re.compile(r'\b([A-Za-z][A-Za-z0-9_]*)\s*--+>\s*([A-Za-z][A-Za-z0-9_]*)\b')
defined_nodes: set[str] = set()
for l in lines:
if "-->" not in l:
m = re.match(r'^\s*([A-Za-z][A-Za-z0-9_]*)\s*[\[({]', l)
if m:
defined_nodes.add(m.group(1))
parsed_edges: list[tuple[str, str]] = []
for l in lines:
m = edge_re.search(l)
if m:
parsed_edges.append((m.group(1), m.group(2)))
if defined_nodes and parsed_edges:
sources = {s for s, _ in parsed_edges}
targets = {d for _, d in parsed_edges}
# Nodes with no incoming edge (except A which is the root)
no_incoming = defined_nodes - targets - {"A"}
if no_incoming:
issues.append(f"Orphaned nodes with no incoming edge: {sorted(no_incoming)}")
# BFS reachability from A
adj: dict[str, list[str]] = {}
for s, d in parsed_edges:
adj.setdefault(s, []).append(d)
reachable: set[str] = set()
queue = ["A"]
while queue:
cur = queue.pop(0)
if cur in reachable:
continue
reachable.add(cur)
for nb in adj.get(cur, []):
queue.append(nb)
unreachable = defined_nodes - reachable
if unreachable:
issues.append(f"Nodes not reachable from A: {sorted(unreachable)}")
return len(issues) == 0, issues
def _clean_mermaid(raw: str) -> str:
"""
Strip markdown fences from Mermaid output.
LLMs frequently wrap Mermaid diagrams in ```mermaid fences even
when instructed not to. The Gradio renderer needs raw syntax.
Args:
raw: Raw LLM response string.
Returns:
Clean Mermaid syntax string.
"""
stripped = raw.strip()
if stripped.startswith("```"):
lines = stripped.splitlines()
end = len(lines) - 1 if lines[-1].strip() == "```" else len(lines)
return "\n".join(lines[1:end]).strip()
return stripped
def _is_valid_mermaid(text: str) -> bool:
"""
Basic validation that the output looks like Mermaid flowchart syntax.
Checks that the text starts with a known Mermaid diagram keyword.
This catches cases where the LLM returned a prose explanation
instead of diagram syntax.
Args:
text: Cleaned Mermaid output string.
Returns:
True if it looks like valid Mermaid syntax, False otherwise.
"""
if not text:
return False
first_line = text.splitlines()[0].strip().lower()
valid_starters = (
"flowchart",
"graph td",
"graph lr",
"graph tb",
"graph rl",
"graph bt",
"sequencediagram",
"classDiagram",
)
return any(first_line.startswith(s) for s in valid_starters)
|