File size: 1,688 Bytes
0c591a7
 
53fe655
0c591a7
 
 
 
 
 
 
 
 
 
 
 
 
 
53fe655
0c591a7
 
 
 
 
 
 
 
 
 
53fe655
0c591a7
 
 
 
 
53fe655
0c591a7
 
 
 
 
 
 
 
 
 
 
53fe655
0c591a7
53fe655
0c591a7
 
 
 
 
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
"""
LangGraph workflow definition for self-correcting SWOT analysis.
Defines the cyclic workflow: Researcher -> Analyzer -> Critic -> Analyzer (revision loop)
"""

from langgraph.graph import StateGraph
from langchain_core.runnables import RunnableLambda

from src.state import AgentState
from src.nodes.researcher import researcher_node
from src.nodes.analyzer import analyzer_node
from src.nodes.critic import critic_node
from src.utils.conditions import should_continue

# Create the cyclic workflow
workflow = StateGraph(AgentState)

# Add nodes to the workflow (Analyzer handles both initial generation and revisions)
workflow.add_node("Researcher", RunnableLambda(researcher_node))
workflow.add_node("Analyzer", RunnableLambda(analyzer_node))
workflow.add_node("Critic", RunnableLambda(critic_node))

# Define the workflow edges
workflow.set_entry_point("Researcher")
workflow.add_edge("Researcher", "Analyzer")
workflow.add_edge("Analyzer", "Critic")

# Add conditional edges for the self-correcting loop
# Analyzer now handles revisions directly (no separate Editor node)
workflow.add_conditional_edges(
    "Critic",
    should_continue,
    {
        "exit": "__end__",
        "retry": "Analyzer"  # Route back to Analyzer for revisions
    }
)

# Set the finish point
workflow.set_finish_point("Critic")

# Enhanced configuration for better tracing
workflow.config = {
    "project_name": "AI-strategy-agent-cyclic",
    "tags": ["self-correcting", "quality-loop", "swot-analysis"],
    "metadata": {
        "version": "2.0",
        "environment": "development",
        "workflow_type": "researcher-analyzer-critic"
    }
}

# Compile the workflow
app = workflow.compile()