File size: 1,722 Bytes
1110d29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74b842d
1110d29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
LangGraph workflow — the main debate orchestration graph.

Flow:
    START → chef → nutrition → eco → moderator → should_continue?
      ├── decision_reached=True → END
      ├── round >= 4 → END
      └── else → chef (next round)
"""

from langgraph.graph import END, StateGraph

from graph.nodes import chef_node, eco_node, moderator_node, nutrition_node
from graph.state import DebateState


def should_continue(state: DebateState) -> str:
    """
    Conditional edge: decide whether to continue the debate or end it.

    Returns:
        'end' if decision reached or max rounds exceeded, 'continue' otherwise.
    """
    if state.get("decision_reached", False):
        return "end"

    if state.get("current_round", 1) >= 2:
        return "end"

    return "continue"


def build_graph() -> StateGraph:
    """
    Build and compile the multi-agent debate graph.

    Returns:
        A compiled LangGraph StateGraph ready for invocation.
    """
    workflow = StateGraph(DebateState)

    # Add agent nodes
    workflow.add_node("chef", chef_node)
    workflow.add_node("nutrition", nutrition_node)
    workflow.add_node("eco", eco_node)
    workflow.add_node("moderator", moderator_node)

    # Set the entry point
    workflow.set_entry_point("chef")

    # Linear flow within each round
    workflow.add_edge("chef", "nutrition")
    workflow.add_edge("nutrition", "eco")
    workflow.add_edge("eco", "moderator")

    # Conditional edge after moderator: continue or end
    workflow.add_conditional_edges(
        "moderator",
        should_continue,
        {
            "continue": "chef",
            "end": END,
        },
    )

    return workflow.compile()