consolidate: HQ LangGraph StateGraph orchestration
Browse files- hq/langgraph_graph.py +63 -0
hq/langgraph_graph.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Agent Q3 [HQ] — LangGraph StateGraph
|
| 3 |
+
Multi-agent routing: classify → reasoner | coder → tandem (optional) → output
|
| 4 |
+
"""
|
| 5 |
+
from langgraph.graph import StateGraph, END
|
| 6 |
+
from typing import TypedDict, Literal
|
| 7 |
+
import asyncio
|
| 8 |
+
|
| 9 |
+
class AgentState(TypedDict):
|
| 10 |
+
messages: list
|
| 11 |
+
target: str
|
| 12 |
+
plan: str
|
| 13 |
+
output: str
|
| 14 |
+
route: str
|
| 15 |
+
|
| 16 |
+
def classify_node(state: AgentState) -> AgentState:
|
| 17 |
+
content = " ".join(m.get("content","") for m in state["messages"]).lower()
|
| 18 |
+
code_kw = ["fix","debug","implement","write","function","class","solidity","contract"]
|
| 19 |
+
state["route"] = "coder" if any(k in content for k in code_kw) else "reasoner"
|
| 20 |
+
return state
|
| 21 |
+
|
| 22 |
+
async def reasoner_node(state: AgentState) -> AgentState:
|
| 23 |
+
from compute_router import ComputeRouter
|
| 24 |
+
router = ComputeRouter()
|
| 25 |
+
result = await router.route(state["messages"], target="reasoner")
|
| 26 |
+
state["output"] = result["choices"][0]["message"]["content"]
|
| 27 |
+
return state
|
| 28 |
+
|
| 29 |
+
async def coder_node(state: AgentState) -> AgentState:
|
| 30 |
+
from compute_router import ComputeRouter
|
| 31 |
+
router = ComputeRouter()
|
| 32 |
+
result = await router.route(state["messages"], target="coder")
|
| 33 |
+
state["output"] = result["choices"][0]["message"]["content"]
|
| 34 |
+
return state
|
| 35 |
+
|
| 36 |
+
async def tandem_node(state: AgentState) -> AgentState:
|
| 37 |
+
from tandem_core import TandemCore
|
| 38 |
+
result = await TandemCore().run(state["messages"])
|
| 39 |
+
state["plan"] = result["plan"]
|
| 40 |
+
state["output"] = result["implementation"]
|
| 41 |
+
return state
|
| 42 |
+
|
| 43 |
+
def route_decision(state: AgentState) -> Literal["reasoner", "coder", "tandem"]:
|
| 44 |
+
return state.get("route", "reasoner")
|
| 45 |
+
|
| 46 |
+
def build_graph() -> StateGraph:
|
| 47 |
+
g = StateGraph(AgentState)
|
| 48 |
+
g.add_node("classify", classify_node)
|
| 49 |
+
g.add_node("reasoner", reasoner_node)
|
| 50 |
+
g.add_node("coder", coder_node)
|
| 51 |
+
g.add_node("tandem", tandem_node)
|
| 52 |
+
g.set_entry_point("classify")
|
| 53 |
+
g.add_conditional_edges("classify", route_decision, {
|
| 54 |
+
"reasoner": "reasoner",
|
| 55 |
+
"coder": "coder",
|
| 56 |
+
"tandem": "tandem",
|
| 57 |
+
})
|
| 58 |
+
g.add_edge("reasoner", END)
|
| 59 |
+
g.add_edge("coder", END)
|
| 60 |
+
g.add_edge("tandem", END)
|
| 61 |
+
return g.compile()
|
| 62 |
+
|
| 63 |
+
graph = build_graph()
|