feat: allow non-ast plot routes

This commit is contained in:
Greyson LaLonde
2025-11-06 21:17:29 -05:00
committed by GitHub
parent 40a2d387a1
commit f6aed9798b
9 changed files with 1746 additions and 424 deletions

View File

@@ -428,6 +428,8 @@ class FlowMeta(type):
possible_returns = get_possible_return_constants(attr_value)
if possible_returns:
router_paths[attr_name] = possible_returns
else:
router_paths[attr_name] = []
cls._start_methods = start_methods # type: ignore[attr-defined]
cls._listeners = listeners # type: ignore[attr-defined]

View File

@@ -21,6 +21,7 @@ P = ParamSpec("P")
R = TypeVar("R", covariant=True)
FlowMethodName = NewType("FlowMethodName", str)
FlowRouteName = NewType("FlowRouteName", str)
PendingListenerKey = NewType(
"PendingListenerKey",
Annotated[str, "nested flow conditions use 'listener_name:object_id'"],

View File

@@ -19,11 +19,11 @@ import ast
from collections import defaultdict, deque
import inspect
import textwrap
from typing import Any, TYPE_CHECKING
from typing import TYPE_CHECKING, Any
from typing_extensions import TypeIs
from crewai.flow.constants import OR_CONDITION, AND_CONDITION
from crewai.flow.constants import AND_CONDITION, OR_CONDITION
from crewai.flow.flow_wrappers import (
FlowCondition,
FlowConditions,
@@ -33,6 +33,7 @@ from crewai.flow.flow_wrappers import (
from crewai.flow.types import FlowMethodCallable, FlowMethodName
from crewai.utilities.printer import Printer
if TYPE_CHECKING:
from crewai.flow.flow import Flow
@@ -40,6 +41,22 @@ _printer = Printer()
def get_possible_return_constants(function: Any) -> list[str] | None:
"""Extract possible string return values from a function using AST parsing.
This function analyzes the source code of a router method to identify
all possible string values it might return. It handles:
- Direct string literals: return "value"
- Variable assignments: x = "value"; return x
- Dictionary lookups: d = {"k": "v"}; return d[key]
- Conditional returns: return "a" if cond else "b"
- State attributes: return self.state.attr (infers from class context)
Args:
function: The function to analyze.
Returns:
List of possible string return values, or None if analysis fails.
"""
try:
source = inspect.getsource(function)
except OSError:
@@ -82,6 +99,7 @@ def get_possible_return_constants(function: Any) -> list[str] | None:
return_values: set[str] = set()
dict_definitions: dict[str, list[str]] = {}
variable_values: dict[str, list[str]] = {}
state_attribute_values: dict[str, list[str]] = {}
def extract_string_constants(node: ast.expr) -> list[str]:
"""Recursively extract all string constants from an AST node."""
@@ -91,6 +109,17 @@ def get_possible_return_constants(function: Any) -> list[str] | None:
elif isinstance(node, ast.IfExp):
strings.extend(extract_string_constants(node.body))
strings.extend(extract_string_constants(node.orelse))
elif isinstance(node, ast.Call):
if (
isinstance(node.func, ast.Attribute)
and node.func.attr == "get"
and len(node.args) >= 2
):
default_arg = node.args[1]
if isinstance(default_arg, ast.Constant) and isinstance(
default_arg.value, str
):
strings.append(default_arg.value)
return strings
class VariableAssignmentVisitor(ast.NodeVisitor):
@@ -124,6 +153,22 @@ def get_possible_return_constants(function: Any) -> list[str] | None:
self.generic_visit(node)
def get_attribute_chain(node: ast.expr) -> str | None:
"""Extract the full attribute chain from an AST node.
Examples:
self.state.run_type -> "self.state.run_type"
x.y.z -> "x.y.z"
simple_var -> "simple_var"
"""
if isinstance(node, ast.Name):
return node.id
if isinstance(node, ast.Attribute):
base = get_attribute_chain(node.value)
if base:
return f"{base}.{node.attr}"
return None
class ReturnVisitor(ast.NodeVisitor):
def visit_Return(self, node: ast.Return) -> None:
if (
@@ -139,21 +184,94 @@ def get_possible_return_constants(function: Any) -> list[str] | None:
for v in dict_definitions[var_name_dict]:
return_values.add(v)
elif node.value:
var_name_ret: str | None = None
if isinstance(node.value, ast.Name):
var_name_ret = node.value.id
elif isinstance(node.value, ast.Attribute):
var_name_ret = f"{node.value.value.id if isinstance(node.value.value, ast.Name) else '_'}.{node.value.attr}"
var_name_ret = get_attribute_chain(node.value)
if var_name_ret and var_name_ret in variable_values:
for v in variable_values[var_name_ret]:
return_values.add(v)
elif var_name_ret and var_name_ret in state_attribute_values:
for v in state_attribute_values[var_name_ret]:
return_values.add(v)
self.generic_visit(node)
def visit_If(self, node: ast.If) -> None:
self.generic_visit(node)
# Try to get the class context to infer state attribute values
try:
if hasattr(function, "__self__"):
# Method is bound, get the class
class_obj = function.__self__.__class__
elif hasattr(function, "__qualname__") and "." in function.__qualname__:
# Method is unbound but we can try to get class from module
class_name = function.__qualname__.rsplit(".", 1)[0]
if hasattr(function, "__globals__"):
class_obj = function.__globals__.get(class_name)
else:
class_obj = None
else:
class_obj = None
if class_obj is not None:
try:
class_source = inspect.getsource(class_obj)
class_source = textwrap.dedent(class_source)
class_ast = ast.parse(class_source)
# Look for comparisons and assignments involving state attributes
class StateAttributeVisitor(ast.NodeVisitor):
def visit_Compare(self, node: ast.Compare) -> None:
"""Find comparisons like: self.state.attr == "value" """
left_attr = get_attribute_chain(node.left)
if left_attr:
for comparator in node.comparators:
if isinstance(comparator, ast.Constant) and isinstance(
comparator.value, str
):
if left_attr not in state_attribute_values:
state_attribute_values[left_attr] = []
if (
comparator.value
not in state_attribute_values[left_attr]
):
state_attribute_values[left_attr].append(
comparator.value
)
# Also check right side
for comparator in node.comparators:
right_attr = get_attribute_chain(comparator)
if (
right_attr
and isinstance(node.left, ast.Constant)
and isinstance(node.left.value, str)
):
if right_attr not in state_attribute_values:
state_attribute_values[right_attr] = []
if (
node.left.value
not in state_attribute_values[right_attr]
):
state_attribute_values[right_attr].append(
node.left.value
)
self.generic_visit(node)
StateAttributeVisitor().visit(class_ast)
except Exception as e:
_printer.print(
f"Could not analyze class context for {function.__name__}: {e}",
color="yellow",
)
except Exception as e:
_printer.print(
f"Could not introspect class for {function.__name__}: {e}",
color="yellow",
)
VariableAssignmentVisitor().visit(code_ast)
ReturnVisitor().visit(code_ast)

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,7 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="'{{ css_path }}'" />
<script src="https://unpkg.com/lucide@latest"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
<script src="'{{ js_path }}'"></script>
@@ -23,93 +24,129 @@
<div class="drawer-title" id="drawer-node-name">Node Details</div>
<div style="display: flex; align-items: center;">
<button class="drawer-open-ide" id="drawer-open-ide" style="display: none;">
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2">
<path d="M4 2 L12 2 L12 14 L4 14 Z" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M6 5 L10 5 M6 8 L10 8 M6 11 L10 11" stroke-linecap="round"/>
</svg>
<i data-lucide="file-code" style="width: 16px; height: 16px;"></i>
Open in IDE
</button>
<button class="drawer-close" id="drawer-close">×</button>
<button class="drawer-close" id="drawer-close">
<i data-lucide="x" style="width: 20px; height: 20px;"></i>
</button>
</div>
</div>
<div class="drawer-content" id="drawer-content"></div>
</div>
<div id="info">
<div style="text-align: center; margin-bottom: 20px;">
<div style="text-align: center;">
<img src="https://cdn.prod.website-files.com/68de1ee6d7c127849807d7a6/68de1ee6d7c127849807d7ef_Logo.svg"
alt="CrewAI Logo"
style="width: 120px; height: auto;">
</div>
<h3>Flow Execution</h3>
<div class="stats">
<p><strong>Nodes:</strong> '{{ dag_nodes_count }}'</p>
<p><strong>Edges:</strong> '{{ dag_edges_count }}'</p>
<p><strong>Topological Paths:</strong> '{{ execution_paths }}'</p>
</div>
<div class="legend">
<div class="legend-title">Node Types</div>
<div class="legend-item">
<div class="legend-color" style="background: '{{ CREWAI_ORANGE }}';"></div>
<span>Start Methods</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: '{{ DARK_GRAY }}'; border: 3px solid '{{ CREWAI_ORANGE }}';"></div>
<span>Router Methods</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: '{{ DARK_GRAY }}';"></div>
<span>Listen Methods</span>
</div>
</div>
<div class="legend">
<div class="legend-title">Edge Types</div>
<div class="legend-item">
<svg width="24" height="12" style="margin-right: 12px;">
<line x1="0" y1="6" x2="24" y2="6" stroke="'{{ CREWAI_ORANGE }}'" stroke-width="2" stroke-dasharray="5,5"/>
</svg>
<span>Router Paths</span>
</div>
<div class="legend-item">
<svg width="24" height="12" style="margin-right: 12px;" class="legend-or-line">
<line x1="0" y1="6" x2="24" y2="6" stroke="var(--edge-or-color)" stroke-width="2"/>
</svg>
<span>OR Conditions</span>
</div>
<div class="legend-item">
<svg width="24" height="12" style="margin-right: 12px;">
<line x1="0" y1="6" x2="24" y2="6" stroke="'{{ CREWAI_ORANGE }}'" stroke-width="2"/>
</svg>
<span>AND Conditions</span>
</div>
</div>
<div class="instructions">
<strong>Interactions:</strong><br>
• Drag to pan<br>
• Scroll to zoom<br><br>
<strong>IDE:</strong>
<select id="ide-selector" style="width: 100%; padding: 4px; margin-top: 4px; border-radius: 3px; border: 1px solid #e0e0e0; background: white; font-size: 12px; cursor: pointer; pointer-events: auto; position: relative; z-index: 10;">
<option value="auto">Auto-detect</option>
<option value="pycharm">PyCharm</option>
<option value="vscode">VS Code</option>
<option value="jetbrains">JetBrains (Toolbox)</option>
</select>
style="width: 144px; height: auto;">
</div>
</div>
<!-- Custom navigation controls -->
<div class="nav-controls">
<div class="nav-button" id="theme-toggle" title="Toggle Dark Mode">🌙</div>
<div class="nav-button" id="zoom-in" title="Zoom In">+</div>
<div class="nav-button" id="zoom-out" title="Zoom Out"></div>
<div class="nav-button" id="fit" title="Fit to Screen">⊡</div>
<div class="nav-button" id="export-png" title="Export to PNG">🖼</div>
<div class="nav-button" id="export-pdf" title="Export to PDF">📄</div>
<div class="nav-button" id="export-json" title="Export to JSON">{}</div>
<div class="nav-button" id="theme-toggle" title="Toggle Dark Mode">
<i data-lucide="moon" style="width: 18px; height: 18px;"></i>
</div>
<div class="nav-button" id="zoom-in" title="Zoom In">
<i data-lucide="zoom-in" style="width: 18px; height: 18px;"></i>
</div>
<div class="nav-button" id="zoom-out" title="Zoom Out">
<i data-lucide="zoom-out" style="width: 18px; height: 18px;"></i>
</div>
<div class="nav-button" id="fit" title="Fit to Screen">
<i data-lucide="maximize-2" style="width: 18px; height: 18px;"></i>
</div>
<div class="nav-button" id="export-png" title="Export to PNG">
<i data-lucide="image" style="width: 18px; height: 18px;"></i>
</div>
<div class="nav-button" id="export-pdf" title="Export to PDF">
<i data-lucide="file-text" style="width: 18px; height: 18px;"></i>
</div>
<!-- <div class="nav-button" id="export-json" title="Export to JSON">
<i data-lucide="braces" style="width: 18px; height: 18px;"></i>
</div> -->
</div>
<div id="network-container">
<div id="network"></div>
</div>
<!-- Info panel at bottom -->
<div id="legend-panel">
<!-- Stats Section -->
<div class="legend-section">
<div class="legend-stats-row">
<div class="legend-stat-item">
<span class="stat-value">'{{ dag_nodes_count }}'</span>
<span class="stat-label">Nodes</span>
</div>
<div class="legend-stat-item">
<span class="stat-value">'{{ dag_edges_count }}'</span>
<span class="stat-label">Edges</span>
</div>
<div class="legend-stat-item">
<span class="stat-value">'{{ execution_paths }}'</span>
<span class="stat-label">Paths</span>
</div>
</div>
</div>
<!-- Node Types Section -->
<div class="legend-section">
<div class="legend-group">
<div class="legend-item-compact">
<div class="legend-color-small" style="background: var(--node-bg-start);"></div>
<span>Start</span>
</div>
<div class="legend-item-compact">
<div class="legend-color-small" style="background: var(--node-bg-router); border: 2px solid var(--node-border-start);"></div>
<span>Router</span>
</div>
<div class="legend-item-compact">
<div class="legend-color-small" style="background: var(--node-bg-listen); border: 2px solid var(--node-border-listen);"></div>
<span>Listen</span>
</div>
</div>
</div>
<!-- Edge Types Section -->
<div class="legend-section">
<div class="legend-group">
<div class="legend-item-compact">
<svg>
<line x1="0" y1="7" x2="29" y2="7" stroke="var(--edge-router-color)" stroke-width="2" stroke-dasharray="4,4"/>
</svg>
<span>Router</span>
</div>
<div class="legend-item-compact">
<svg class="legend-or-line">
<line x1="0" y1="7" x2="29" y2="7" stroke="var(--edge-or-color)" stroke-width="2"/>
</svg>
<span>OR</span>
</div>
<div class="legend-item-compact">
<svg>
<line x1="0" y1="7" x2="29" y2="7" stroke="var(--edge-router-color)" stroke-width="2"/>
</svg>
<span>AND</span>
</div>
</div>
</div>
<!-- IDE Selector Section -->
<div class="legend-section">
<div class="legend-ide-column">
<label class="legend-ide-label">IDE</label>
<select id="ide-selector" class="legend-ide-select">
<option value="auto">Auto-detect</option>
<option value="pycharm">PyCharm</option>
<option value="vscode">VS Code</option>
<option value="jetbrains">JetBrains</option>
</select>
</div>
</div>
</div>
</body>
</html>

View File

@@ -13,6 +13,14 @@
--edge-label-text: '{{ GRAY }}';
--edge-label-bg: rgba(255, 255, 255, 0.8);
--edge-or-color: #000000;
--edge-router-color: '{{ CREWAI_ORANGE }}';
--node-border-start: #C94238;
--node-border-listen: #3D3D3D;
--node-bg-start: #FF7066;
--node-bg-router: #FFFFFF;
--node-bg-listen: #FFFFFF;
--node-text-color: #FFFFFF;
--nav-button-hover: #f5f5f5;
}
[data-theme="dark"] {
@@ -30,6 +38,14 @@
--edge-label-text: #c9d1d9;
--edge-label-bg: rgba(22, 27, 34, 0.9);
--edge-or-color: #ffffff;
--edge-router-color: '{{ CREWAI_ORANGE }}';
--node-border-start: #FF5A50;
--node-border-listen: #666666;
--node-bg-start: #B33830;
--node-bg-router: #3D3D3D;
--node-bg-listen: #3D3D3D;
--node-text-color: #FFFFFF;
--nav-button-hover: #30363d;
}
@keyframes dash {
@@ -72,12 +88,10 @@ body {
position: absolute;
top: 20px;
left: 20px;
background: var(--bg-secondary);
background: transparent;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 12px var(--shadow-strong);
max-width: 320px;
border: 1px solid var(--border-color);
z-index: 10000;
pointer-events: auto;
transition: background 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease;
@@ -125,12 +139,16 @@ h3 {
margin-right: 12px;
border-radius: 3px;
box-sizing: border-box;
transition: background 0.3s ease, border-color 0.3s ease;
}
.legend-item span {
color: var(--text-secondary);
font-size: 13px;
transition: color 0.3s ease;
}
.legend-item svg line {
transition: stroke 0.3s ease;
}
.instructions {
margin-top: 15px;
padding-top: 15px;
@@ -155,7 +173,7 @@ h3 {
bottom: 20px;
right: auto;
display: grid;
grid-template-columns: repeat(4, 40px);
grid-template-columns: repeat(3, 40px);
gap: 8px;
z-index: 10002;
pointer-events: auto;
@@ -165,10 +183,187 @@ h3 {
.nav-controls.drawer-open {
}
#legend-panel {
position: fixed;
left: 164px;
bottom: 20px;
right: 20px;
height: 92px;
background: var(--bg-secondary);
backdrop-filter: blur(12px) saturate(180%);
-webkit-backdrop-filter: blur(12px) saturate(180%);
border: 1px solid var(--border-subtle);
border-radius: 6px;
box-shadow: 0 2px 8px var(--shadow-color);
display: grid;
grid-template-columns: repeat(4, 1fr);
align-items: center;
gap: 0;
padding: 0 24px;
box-sizing: border-box;
z-index: 10001;
pointer-events: auto;
transition: background 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease, right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
#legend-panel.drawer-open {
right: 405px;
}
.legend-section {
display: flex;
align-items: center;
justify-content: center;
min-width: 0;
width: -webkit-fill-available;
width: -moz-available;
width: stretch;
position: relative;
}
.legend-section:not(:last-child)::after {
content: '';
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
width: 1px;
height: 48px;
background: var(--border-color);
transition: background 0.3s ease;
}
.legend-stats-row {
display: flex;
gap: 32px;
justify-content: center;
align-items: center;
min-width: 0;
}
.legend-stat-item {
display: flex;
flex-direction: column;
align-items: center;
gap: 4px;
}
.stat-value {
font-size: 19px;
font-weight: 700;
color: var(--text-primary);
line-height: 1;
transition: color 0.3s ease;
}
.stat-label {
font-size: 8px;
font-weight: 500;
text-transform: uppercase;
color: var(--text-secondary);
letter-spacing: 0.5px;
transition: color 0.3s ease;
}
.legend-items-row {
display: flex;
gap: 16px;
align-items: center;
justify-content: center;
min-width: 0;
}
.legend-group {
display: flex;
gap: 16px;
align-items: center;
}
.legend-item-compact {
display: flex;
align-items: center;
gap: 6px;
}
.legend-item-compact span {
font-size: 12px;
font-weight: 500;
text-transform: uppercase;
color: var(--text-secondary);
letter-spacing: 0.5px;
white-space: nowrap;
font-family: inherit;
line-height: 1;
transition: color 0.3s ease;
}
.legend-color-small {
width: 17px;
height: 17px;
border-radius: 2px;
box-sizing: border-box;
flex-shrink: 0;
transition: background 0.3s ease, border-color 0.3s ease;
}
.legend-item-compact svg {
display: block;
flex-shrink: 0;
width: 29px;
height: 14px;
}
.legend-item-compact svg line {
transition: stroke 0.3s ease;
}
.legend-ide-column {
display: flex;
flex-direction: row;
gap: 8px;
align-items: center;
justify-content: center;
min-width: 0;
width: 100%;
}
.legend-ide-label {
font-size: 12px;
font-weight: 500;
text-transform: uppercase;
color: var(--text-secondary);
letter-spacing: 0.5px;
transition: color 0.3s ease;
white-space: nowrap;
}
.legend-ide-select {
width: 120px;
padding: 6px 10px;
border-radius: 4px;
border: 1px solid var(--border-subtle);
background: var(--bg-primary);
color: var(--text-primary);
font-size: 11px;
cursor: pointer;
transition: all 0.3s ease;
}
.legend-ide-select:hover {
border-color: var(--text-secondary);
}
.legend-ide-select:focus {
outline: none;
border-color: '{{ CREWAI_ORANGE }}';
}
.nav-button {
width: 40px;
height: 40px;
background: var(--bg-secondary);
backdrop-filter: blur(12px) saturate(180%);
-webkit-backdrop-filter: blur(12px) saturate(180%);
border: 1px solid var(--border-subtle);
border-radius: 6px;
display: flex;
@@ -181,12 +376,12 @@ h3 {
user-select: none;
pointer-events: auto;
position: relative;
z-index: 10001;
z-index: 10002;
transition: background 0.3s ease, border-color 0.3s ease, color 0.3s ease, box-shadow 0.3s ease;
}
.nav-button:hover {
background: var(--border-subtle);
background: var(--nav-button-hover);
}
#drawer {
@@ -198,9 +393,10 @@ h3 {
background: var(--bg-drawer);
box-shadow: -4px 0 12px var(--shadow-strong);
transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s ease, box-shadow 0.3s ease;
z-index: 2000;
overflow-y: auto;
padding: 24px;
z-index: 10003;
overflow: hidden;
transform: translateZ(0);
isolation: isolate;
}
#drawer.open {
@@ -247,17 +443,22 @@ h3 {
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding-bottom: 16px;
padding: 24px 24px 16px 24px;
border-bottom: 2px solid '{{ CREWAI_ORANGE }}';
position: relative;
z-index: 2001;
}
.drawer-title {
font-size: 20px;
font-size: 15px;
font-weight: 700;
color: var(--text-primary);
transition: color 0.3s ease;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: 1;
min-width: 0;
}
.drawer-close {
@@ -269,12 +470,19 @@ h3 {
padding: 4px 8px;
line-height: 1;
transition: color 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
}
.drawer-close:hover {
color: '{{ CREWAI_ORANGE }}';
}
.drawer-close i {
display: block;
}
.drawer-open-ide {
background: '{{ CREWAI_ORANGE }}';
border: none;
@@ -292,6 +500,9 @@ h3 {
position: relative;
z-index: 9999;
pointer-events: auto;
white-space: nowrap;
flex-shrink: 0;
min-width: fit-content;
}
.drawer-open-ide:hover {
@@ -305,14 +516,19 @@ h3 {
box-shadow: 0 1px 4px rgba(255, 90, 80, 0.2);
}
.drawer-open-ide svg {
.drawer-open-ide svg,
.drawer-open-ide i {
width: 14px;
height: 14px;
display: block;
}
.drawer-content {
color: '{{ DARK_GRAY }}';
line-height: 1.6;
padding: 0 24px 24px 24px;
overflow-y: auto;
height: calc(100vh - 95px);
}
.drawer-section {
@@ -328,6 +544,10 @@ h3 {
position: relative;
}
.drawer-metadata-grid:has(.drawer-section:nth-child(3):nth-last-child(1)) {
grid-template-columns: 1fr 2fr;
}
.drawer-metadata-grid::before {
content: '';
position: absolute;
@@ -419,20 +639,35 @@ h3 {
grid-column: 2;
display: flex;
flex-direction: column;
justify-content: center;
justify-content: flex-start;
align-items: flex-start;
}
.drawer-metadata-grid:has(.drawer-section:nth-child(3):nth-last-child(1))::after {
right: 50%;
right: 66.666%;
}
.drawer-metadata-grid:has(.drawer-section:nth-child(3):nth-last-child(1))::before {
left: 33.333%;
}
.drawer-metadata-grid .drawer-section:nth-child(3):nth-last-child(1) .drawer-section-title {
align-self: flex-start;
}
.drawer-metadata-grid .drawer-section:nth-child(3):nth-last-child(1) > *:not(.drawer-section-title) {
width: 100%;
align-self: stretch;
}
.drawer-section-title {
font-size: 12px;
text-transform: uppercase;
color: '{{ GRAY }}';
color: var(--text-secondary);
letter-spacing: 0.5px;
margin-bottom: 8px;
font-weight: 600;
transition: color 0.3s ease;
}
.drawer-badge {
@@ -465,9 +700,44 @@ h3 {
padding: 3px 0;
}
.drawer-metadata-grid .drawer-section .drawer-list {
display: flex;
flex-direction: column;
gap: 6px;
}
.drawer-metadata-grid .drawer-section .drawer-list li {
border-bottom: none;
padding: 0;
}
.drawer-metadata-grid .drawer-section:nth-child(3) .drawer-list li {
border-bottom: none;
padding: 3px 0;
padding: 0;
}
.drawer-metadata-grid .drawer-section {
overflow: visible;
}
.drawer-metadata-grid .drawer-section .condition-group,
.drawer-metadata-grid .drawer-section .trigger-group {
width: 100%;
box-sizing: border-box;
}
.drawer-metadata-grid .drawer-section .condition-children {
width: 100%;
}
.drawer-metadata-grid .drawer-section .trigger-group-items {
width: 100%;
}
.drawer-metadata-grid .drawer-section .drawer-code-link {
word-break: break-word;
overflow-wrap: break-word;
max-width: 100%;
}
.drawer-code {
@@ -491,6 +761,7 @@ h3 {
cursor: pointer;
transition: all 0.2s;
display: inline-block;
margin: 3px 2px;
}
.drawer-code-link:hover {

View File

@@ -3,12 +3,13 @@
from __future__ import annotations
from collections import defaultdict
from collections.abc import Iterable
import inspect
from typing import TYPE_CHECKING, Any
from crewai.flow.constants import AND_CONDITION, OR_CONDITION
from crewai.flow.flow_wrappers import FlowCondition
from crewai.flow.types import FlowMethodName
from crewai.flow.types import FlowMethodName, FlowRouteName
from crewai.flow.utils import (
is_flow_condition_dict,
is_simple_flow_condition,
@@ -197,8 +198,6 @@ def build_flow_structure(flow: Flow[Any]) -> FlowStructure:
node_metadata["type"] = "router"
router_methods.append(method_name)
node_metadata["condition_type"] = "IF"
if method_name in flow._router_paths:
node_metadata["router_paths"] = [
str(p) for p in flow._router_paths[method_name]
@@ -210,9 +209,13 @@ def build_flow_structure(flow: Flow[Any]) -> FlowStructure:
]
if hasattr(method, "__condition_type__") and method.__condition_type__:
node_metadata["trigger_condition_type"] = method.__condition_type__
if "condition_type" not in node_metadata:
node_metadata["condition_type"] = method.__condition_type__
if node_metadata.get("is_router") and "condition_type" not in node_metadata:
node_metadata["condition_type"] = "IF"
if (
hasattr(method, "__trigger_condition__")
and method.__trigger_condition__ is not None
@@ -298,6 +301,9 @@ def build_flow_structure(flow: Flow[Any]) -> FlowStructure:
nodes[method_name] = node_metadata
for listener_name, condition_data in flow._listeners.items():
if listener_name in router_methods:
continue
if is_simple_flow_condition(condition_data):
cond_type, methods = condition_data
edges.extend(
@@ -315,6 +321,60 @@ def build_flow_structure(flow: Flow[Any]) -> FlowStructure:
_create_edges_from_condition(condition_data, str(listener_name), nodes)
)
for method_name, node_metadata in nodes.items(): # type: ignore[assignment]
if node_metadata.get("is_router") and "trigger_methods" in node_metadata:
trigger_methods = node_metadata["trigger_methods"]
condition_type = node_metadata.get("trigger_condition_type", OR_CONDITION)
if "trigger_condition" in node_metadata:
edges.extend(
_create_edges_from_condition(
node_metadata["trigger_condition"], # type: ignore[arg-type]
method_name,
nodes,
)
)
else:
edges.extend(
StructureEdge(
source=trigger_method,
target=method_name,
condition_type=condition_type,
is_router_path=False,
)
for trigger_method in trigger_methods
if trigger_method in nodes
)
for router_method_name in router_methods:
if router_method_name not in flow._router_paths:
flow._router_paths[FlowMethodName(router_method_name)] = []
inferred_paths: Iterable[FlowMethodName | FlowRouteName] = set(
flow._router_paths.get(FlowMethodName(router_method_name), [])
)
for condition_data in flow._listeners.values():
trigger_strings: list[str] = []
if is_simple_flow_condition(condition_data):
_, methods = condition_data
trigger_strings = [str(m) for m in methods]
elif is_flow_condition_dict(condition_data):
trigger_strings = _extract_direct_or_triggers(condition_data)
for trigger_str in trigger_strings:
if trigger_str not in nodes:
# This is likely a router path output
inferred_paths.add(trigger_str) # type: ignore[attr-defined]
if inferred_paths:
flow._router_paths[FlowMethodName(router_method_name)] = list(
inferred_paths # type: ignore[arg-type]
)
if router_method_name in nodes:
nodes[router_method_name]["router_paths"] = list(inferred_paths)
for router_method_name in router_methods:
if router_method_name not in flow._router_paths:
continue
@@ -340,6 +400,7 @@ def build_flow_structure(flow: Flow[Any]) -> FlowStructure:
target=str(listener_name),
condition_type=None,
is_router_path=True,
router_path_label=str(path),
)
)

View File

@@ -20,7 +20,7 @@ class CSSExtension(Extension):
Provides {% css 'path/to/file.css' %} tag syntax.
"""
tags: ClassVar[set[str]] = {"css"} # type: ignore[assignment]
tags: ClassVar[set[str]] = {"css"} # type: ignore[misc]
def parse(self, parser: Parser) -> nodes.Node:
"""Parse {% css 'styles.css' %} tag.
@@ -53,7 +53,7 @@ class JSExtension(Extension):
Provides {% js 'path/to/file.js' %} tag syntax.
"""
tags: ClassVar[set[str]] = {"js"} # type: ignore[assignment]
tags: ClassVar[set[str]] = {"js"} # type: ignore[misc]
def parse(self, parser: Parser) -> nodes.Node:
"""Parse {% js 'script.js' %} tag.
@@ -91,6 +91,116 @@ TEXT_PRIMARY = "#e6edf3"
TEXT_SECONDARY = "#7d8590"
def calculate_node_positions(
dag: FlowStructure,
) -> dict[str, dict[str, int | float]]:
"""Calculate hierarchical positions (level, x, y) for each node.
Args:
dag: FlowStructure containing nodes and edges.
Returns:
Dictionary mapping node names to their position data (level, x, y).
"""
children: dict[str, list[str]] = {name: [] for name in dag["nodes"]}
parents: dict[str, list[str]] = {name: [] for name in dag["nodes"]}
for edge in dag["edges"]:
source = edge["source"]
target = edge["target"]
if source in children and target in children:
children[source].append(target)
parents[target].append(source)
levels: dict[str, int] = {}
queue: list[tuple[str, int]] = []
for start_method in dag["start_methods"]:
if start_method in dag["nodes"]:
levels[start_method] = 0
queue.append((start_method, 0))
visited: set[str] = set()
while queue:
node, level = queue.pop(0)
if node in visited:
continue
visited.add(node)
if node not in levels or levels[node] < level:
levels[node] = level
for child in children.get(node, []):
if child not in visited:
child_level = level + 1
if child not in levels or levels[child] < child_level:
levels[child] = child_level
queue.append((child, child_level))
for name in dag["nodes"]:
if name not in levels:
levels[name] = 0
nodes_by_level: dict[int, list[str]] = {}
for node, level in levels.items():
if level not in nodes_by_level:
nodes_by_level[level] = []
nodes_by_level[level].append(node)
positions: dict[str, dict[str, int | float]] = {}
level_separation = 300 # Vertical spacing between levels
node_spacing = 400 # Horizontal spacing between nodes
parent_count: dict[str, int] = {}
for node, parent_list in parents.items():
parent_count[node] = len(parent_list)
for level, nodes_at_level in sorted(nodes_by_level.items()):
y = level * level_separation
if level == 0:
num_nodes = len(nodes_at_level)
for i, node in enumerate(nodes_at_level):
x = (i - (num_nodes - 1) / 2) * node_spacing
positions[node] = {"level": level, "x": x, "y": y}
else:
for i, node in enumerate(nodes_at_level):
parent_list = parents.get(node, [])
parent_positions: list[float] = [
positions[parent]["x"]
for parent in parent_list
if parent in positions
]
if parent_positions:
if len(parent_positions) > 1 and len(set(parent_positions)) == 1:
base_x = parent_positions[0]
avg_x = base_x + node_spacing * 0.4
else:
avg_x = sum(parent_positions) / len(parent_positions)
else:
avg_x = i * node_spacing * 0.5
positions[node] = {"level": level, "x": avg_x, "y": y}
nodes_at_level_sorted = sorted(
nodes_at_level, key=lambda n: positions[n]["x"]
)
min_spacing = node_spacing * 0.6 # Minimum horizontal distance
for i in range(len(nodes_at_level_sorted) - 1):
current_node = nodes_at_level_sorted[i]
next_node = nodes_at_level_sorted[i + 1]
current_x = positions[current_node]["x"]
next_x = positions[next_node]["x"]
if next_x - current_x < min_spacing:
positions[next_node]["x"] = current_x + min_spacing
return positions
def render_interactive(
dag: FlowStructure,
filename: str = "flow_dag.html",
@@ -110,6 +220,8 @@ def render_interactive(
Returns:
Absolute path to generated HTML file in temporary directory.
"""
node_positions = calculate_node_positions(dag)
nodes_list: list[dict[str, Any]] = []
for name, metadata in dag["nodes"].items():
node_type: str = metadata.get("type", "listen")
@@ -120,37 +232,37 @@ def render_interactive(
if node_type == "start":
color_config = {
"background": CREWAI_ORANGE,
"border": CREWAI_ORANGE,
"background": "var(--node-bg-start)",
"border": "var(--node-border-start)",
"highlight": {
"background": CREWAI_ORANGE,
"border": CREWAI_ORANGE,
"background": "var(--node-bg-start)",
"border": "var(--node-border-start)",
},
}
font_color = WHITE
border_width = 2
font_color = "var(--node-text-color)"
border_width = 3
elif node_type == "router":
color_config = {
"background": DARK_GRAY,
"background": "var(--node-bg-router)",
"border": CREWAI_ORANGE,
"highlight": {
"background": DARK_GRAY,
"background": "var(--node-bg-router)",
"border": CREWAI_ORANGE,
},
}
font_color = WHITE
font_color = "var(--node-text-color)"
border_width = 3
else:
color_config = {
"background": DARK_GRAY,
"border": DARK_GRAY,
"background": "var(--node-bg-listen)",
"border": "var(--node-border-listen)",
"highlight": {
"background": DARK_GRAY,
"border": DARK_GRAY,
"background": "var(--node-bg-listen)",
"border": "var(--node-border-listen)",
},
}
font_color = WHITE
border_width = 2
font_color = "var(--node-text-color)"
border_width = 3
title_parts: list[str] = []
@@ -215,25 +327,34 @@ def render_interactive(
bg_color = color_config["background"]
border_color = color_config["border"]
nodes_list.append(
{
"id": name,
"label": name,
"title": "".join(title_parts),
"shape": "custom",
"size": 30,
"nodeStyle": {
"name": name,
"bgColor": bg_color,
"borderColor": border_color,
"borderWidth": border_width,
"fontColor": font_color,
},
"opacity": 1.0,
"glowSize": 0,
"glowColor": None,
}
)
position_data = node_positions.get(name, {"level": 0, "x": 0, "y": 0})
node_data: dict[str, Any] = {
"id": name,
"label": name,
"title": "".join(title_parts),
"shape": "custom",
"size": 30,
"level": position_data["level"],
"nodeStyle": {
"name": name,
"bgColor": bg_color,
"borderColor": border_color,
"borderWidth": border_width,
"fontColor": font_color,
},
"opacity": 1.0,
"glowSize": 0,
"glowColor": None,
}
# Add x,y only for graphs with 3-4 nodes
total_nodes = len(dag["nodes"])
if 3 <= total_nodes <= 4:
node_data["x"] = position_data["x"]
node_data["y"] = position_data["y"]
nodes_list.append(node_data)
execution_paths: int = calculate_execution_paths(dag)
@@ -246,6 +367,8 @@ def render_interactive(
if edge["is_router_path"]:
edge_color = CREWAI_ORANGE
edge_dashes = [15, 10]
if "router_path_label" in edge:
edge_label = edge["router_path_label"]
elif edge["condition_type"] == "AND":
edge_label = "AND"
edge_color = CREWAI_ORANGE

View File

@@ -10,6 +10,7 @@ class NodeMetadata(TypedDict, total=False):
is_router: bool
router_paths: list[str]
condition_type: str | None
trigger_condition_type: str | None
trigger_methods: list[str]
trigger_condition: dict[str, Any] | None
method_signature: dict[str, Any]
@@ -22,13 +23,14 @@ class NodeMetadata(TypedDict, total=False):
class_line_number: int
class StructureEdge(TypedDict):
class StructureEdge(TypedDict, total=False):
"""Represents a connection in the flow structure."""
source: str
target: str
condition_type: str | None
is_router_path: bool
router_path_label: str
class FlowStructure(TypedDict):