"""LangGraph workflow compilation — two stateless graphs.""" from langgraph.graph import StateGraph, START, END from app.agent.state import AgentState from app.agent.nodes import ( check_round, generate_initial_questions, generate_followup_questions, analyze_round, check_complete, generate_next_round_questions, generate_plan, generate_markdown, ) def build_generate_questions_graph(): """Build the graph for generating questions. Flow: START → check_round (conditional) → generate_initial_questions (round == 1) → END → generate_followup_questions (round > 1) → END """ builder = StateGraph(AgentState) # Add nodes builder.add_node("generate_initial_questions", generate_initial_questions) builder.add_node("generate_followup_questions", generate_followup_questions) # START → conditional routing builder.add_conditional_edges( START, check_round, { "generate_initial_questions": "generate_initial_questions", "generate_followup_questions": "generate_followup_questions", }, ) # Both question nodes → END builder.add_edge("generate_initial_questions", END) builder.add_edge("generate_followup_questions", END) return builder.compile() def _route_process_start(state: AgentState) -> str: """Route at START: final round skips analyze_round, goes straight to plan.""" if state["current_round"] >= state["max_rounds"]: return "generate_plan" return "analyze_round" def build_process_round_graph(): """Build the graph for processing a round of answers. Flow (non-final round): START → analyze_round → check_complete → generate_next_round_questions → END Flow (final round — single LLM call): START → generate_plan → generate_markdown → END """ builder = StateGraph(AgentState) # Add nodes builder.add_node("analyze_round", analyze_round) builder.add_node("generate_next_round_questions", generate_next_round_questions) builder.add_node("generate_plan", generate_plan) builder.add_node("generate_markdown", generate_markdown) # START → conditional: final round skips analyze_round builder.add_conditional_edges( START, _route_process_start, { "analyze_round": "analyze_round", "generate_plan": "generate_plan", }, ) # analyze → conditional routing (only for non-final rounds) builder.add_conditional_edges( "analyze_round", check_complete, { "generate_next_round_questions": "generate_next_round_questions", "generate_plan": "generate_plan", }, ) # Next round questions → END builder.add_edge("generate_next_round_questions", END) # Plan → markdown → END builder.add_edge("generate_plan", "generate_markdown") builder.add_edge("generate_markdown", END) return builder.compile() # Pre-compiled graph instances generate_questions_graph = build_generate_questions_graph() process_round_graph = build_process_round_graph()