File size: 5,337 Bytes
3cb23da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate a visual image of the LangGraph pipeline."""
from cybersecurityAgents import (
    build_llm,
    build_embeddings,
    build_cybersec_graph,
    VectorKnowledgeBase,
)

print("Initializing pipeline to generate graph image...")
llm = build_llm()
embeddings = build_embeddings()
kb = VectorKnowledgeBase(embeddings)
app = build_cybersec_graph(llm, kb)

# Method 1: LangGraph built-in (requires pygraphviz or grandalf)
try:
    png_data = app.get_graph().draw_png()
    with open("langgraph_pipeline.png", "wb") as f:
        f.write(png_data)
    print("βœ… Graph saved as langgraph_pipeline.png (using LangGraph draw_png)")
except Exception as e:
    print(f"draw_png not available ({e}), falling back to matplotlib...")

    # Method 2: Matplotlib fallback with code_fixer loop
    import matplotlib.pyplot as plt
    import matplotlib.patches as FancyArrowPatch

    nodes = [
        ("classify_input",    "🏷️  Classify Input"),
        ("rag_retrieve",      "πŸ“š  RAG Retrieve"),
        ("log_monitor",       "πŸ“‘  Log Monitor Agent"),
        ("threat_intel",      "πŸ•΅οΈ  Threat Intel Agent"),
        ("vuln_scanner",      "πŸ”  Vuln Scanner Agent"),
        ("code_fixer",        "πŸ”§  Code Fixer Agent (iterative)"),
        ("incident_response", "🚨  Incident Response Agent"),
        ("policy_checker",    "πŸ“‹  Policy Checker Agent"),
        ("synthesize_report", "πŸ“  Synthesize Report"),
    ]

    fig, ax = plt.subplots(1, 1, figsize=(8, 14))
    ax.set_xlim(-2, 12)
    ax.set_ylim(-0.5, len(nodes) + 2.5)
    ax.axis("off")
    ax.set_title("CyberSecurity Multi-Agent LangGraph Pipeline\n(with iterative Code Fixer loop)",
                 fontsize=13, fontweight="bold", pad=20)

    center_x = 5
    node_positions = {}

    # Draw START
    start_y = len(nodes) + 1.5
    ax.annotate("START", xy=(center_x, start_y), fontsize=11, ha="center", va="center",
                bbox=dict(boxstyle="round,pad=0.3", facecolor="#4CAF50", edgecolor="black"),
                color="white", fontweight="bold")

    # Draw nodes
    for i, (key, label) in enumerate(nodes):
        y = len(nodes) - i
        node_positions[key] = (center_x, y)

        # Highlight code_fixer differently
        if key == "code_fixer":
            facecolor = "#FF9800"
            edgecolor = "#E65100"
        else:
            facecolor = "#2196F3"
            edgecolor = "#1565C0"

        ax.annotate(label, xy=(center_x, y), fontsize=9.5, ha="center", va="center",
                    bbox=dict(boxstyle="round,pad=0.4", facecolor=facecolor, edgecolor=edgecolor, alpha=0.9),
                    color="white", fontweight="bold")

    # Draw END
    end_y = 0
    ax.annotate("END", xy=(center_x, end_y), fontsize=11, ha="center", va="center",
                bbox=dict(boxstyle="round,pad=0.3", facecolor="#f44336", edgecolor="black"),
                color="white", fontweight="bold")

    # Draw arrows between sequential nodes
    # START -> first node
    ax.annotate("", xy=(center_x, node_positions["classify_input"][1] + 0.3),
                xytext=(center_x, start_y - 0.25),
                arrowprops=dict(arrowstyle="->", color="black", lw=1.5))

    # Sequential edges
    sequential = [
        ("classify_input", "rag_retrieve"),
        ("rag_retrieve", "log_monitor"),
        ("log_monitor", "threat_intel"),
        ("threat_intel", "vuln_scanner"),
        ("vuln_scanner", "code_fixer"),
        # code_fixer -> incident_response is conditional (drawn separately)
        ("incident_response", "policy_checker"),
        ("policy_checker", "synthesize_report"),
    ]

    for src, dst in sequential:
        src_y = node_positions[src][1]
        dst_y = node_positions[dst][1]
        ax.annotate("", xy=(center_x, dst_y + 0.3),
                    xytext=(center_x, src_y - 0.3),
                    arrowprops=dict(arrowstyle="->", color="black", lw=1.5))

    # code_fixer -> incident_response (conditional: when fixed)
    cf_y = node_positions["code_fixer"][1]
    ir_y = node_positions["incident_response"][1]
    ax.annotate("", xy=(center_x, ir_y + 0.3),
                xytext=(center_x, cf_y - 0.3),
                arrowprops=dict(arrowstyle="->", color="green", lw=2))
    ax.text(center_x + 1.8, (cf_y + ir_y) / 2, "fixed βœ…", fontsize=8, color="green", fontweight="bold")

    # code_fixer LOOP (conditional: when not fixed)
    loop_x = center_x + 4.2
    ax.annotate("",
                xy=(center_x + 2.5, cf_y + 0.15),
                xytext=(center_x + 2.5, cf_y - 0.15),
                arrowprops=dict(arrowstyle="->", color="#FF5722", lw=2,
                                connectionstyle="arc3,rad=-1.8"))
    # Loop label
    ax.text(loop_x - 0.5, cf_y, "loop\n(max 3x)", fontsize=8, color="#FF5722",
            fontweight="bold", ha="center", va="center",
            bbox=dict(boxstyle="round,pad=0.2", facecolor="#FFF3E0", edgecolor="#FF5722", alpha=0.8))

    # Last node -> END
    ax.annotate("", xy=(center_x, end_y + 0.25),
                xytext=(center_x, node_positions["synthesize_report"][1] - 0.3),
                arrowprops=dict(arrowstyle="->", color="black", lw=1.5))

    plt.tight_layout()
    plt.savefig("langgraph_pipeline.png", dpi=150, bbox_inches="tight")
    print("βœ… Graph saved as langgraph_pipeline.png (using matplotlib)")