oshul's picture
Single request for Plan & Insights
716ca58
Raw History Blame Contribute Delete
3.19 kB
"""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()