feat: complete phase 2 -- multi-agent routing, interrupt TTL, escalation, templates
- Intent classification with LLM structured output (single/multi/ambiguous) - Discount agent with apply_discount and generate_coupon tools - Interrupt manager with 30-min TTL auto-expiration and retry prompts - Webhook escalation module with exponential backoff retry (max 3) - Three vertical industry templates (e-commerce, SaaS, fintech) - Template loading in AgentRegistry - Enhanced supervisor prompt with dynamic agent descriptions - 153 tests passing, 90.18% coverage
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from langgraph.prebuilt import create_react_agent
|
||||
from langgraph_supervisor import create_supervisor
|
||||
|
||||
from app.agents import get_tools_by_names
|
||||
from app.intent import ClassificationResult, IntentClassifier
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from langchain_core.language_models import BaseChatModel
|
||||
@@ -16,15 +18,31 @@ if TYPE_CHECKING:
|
||||
|
||||
from app.registry import AgentRegistry
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SUPERVISOR_PROMPT = (
|
||||
"You are a customer support supervisor. "
|
||||
"Route customer requests to the appropriate agent based on their description. "
|
||||
"For order status and tracking queries, use the order_lookup agent. "
|
||||
"For order modifications like cancellations, use the order_actions agent. "
|
||||
"For anything else, use the fallback agent."
|
||||
"Route customer requests to the appropriate agent based on their description.\n\n"
|
||||
"Available agents and their roles:\n"
|
||||
"{agent_descriptions}\n\n"
|
||||
"Routing rules:\n"
|
||||
"- For order status and tracking queries, use the order_lookup agent.\n"
|
||||
"- For order modifications like cancellations, use the order_actions agent.\n"
|
||||
"- For discounts, promotions, or coupon codes, use the discount agent.\n"
|
||||
"- For anything else or when uncertain, use the fallback agent.\n"
|
||||
"- If the user's request involves multiple actions, execute them in order.\n"
|
||||
"- If a previous intent classification is provided, follow it.\n"
|
||||
)
|
||||
|
||||
|
||||
def _format_agent_descriptions(registry: AgentRegistry) -> str:
|
||||
"""Build agent description text for the supervisor prompt."""
|
||||
lines = []
|
||||
for agent in registry.list_agents():
|
||||
lines.append(f"- {agent.name}: {agent.description}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def build_agent_nodes(
|
||||
registry: AgentRegistry,
|
||||
llm: BaseChatModel,
|
||||
@@ -56,15 +74,48 @@ def build_graph(
|
||||
registry: AgentRegistry,
|
||||
llm: BaseChatModel,
|
||||
checkpointer: AsyncPostgresSaver,
|
||||
intent_classifier: IntentClassifier | None = None,
|
||||
) -> CompiledStateGraph:
|
||||
"""Build and compile the LangGraph supervisor graph."""
|
||||
"""Build and compile the LangGraph supervisor graph.
|
||||
|
||||
If an intent_classifier is provided, the supervisor prompt is enhanced
|
||||
with agent descriptions for better routing. The classifier is stored
|
||||
for use by the routing layer (ws_handler).
|
||||
"""
|
||||
agent_nodes = build_agent_nodes(registry, llm)
|
||||
agent_descriptions = _format_agent_descriptions(registry)
|
||||
|
||||
prompt = SUPERVISOR_PROMPT.format(agent_descriptions=agent_descriptions)
|
||||
|
||||
workflow = create_supervisor(
|
||||
agent_nodes,
|
||||
model=llm,
|
||||
prompt=SUPERVISOR_PROMPT,
|
||||
prompt=prompt,
|
||||
output_mode="full_history",
|
||||
)
|
||||
|
||||
return workflow.compile(checkpointer=checkpointer)
|
||||
graph = workflow.compile(checkpointer=checkpointer)
|
||||
|
||||
# Attach classifier and registry to graph for use by ws_handler
|
||||
graph.intent_classifier = intent_classifier # type: ignore[attr-defined]
|
||||
graph.agent_registry = registry # type: ignore[attr-defined]
|
||||
|
||||
return graph
|
||||
|
||||
|
||||
async def classify_intent(
|
||||
graph: CompiledStateGraph,
|
||||
message: str,
|
||||
) -> ClassificationResult | None:
|
||||
"""Classify user intent using the graph's attached classifier.
|
||||
|
||||
Returns None if no classifier is configured.
|
||||
"""
|
||||
classifier = getattr(graph, "intent_classifier", None)
|
||||
registry = getattr(graph, "agent_registry", None)
|
||||
|
||||
if classifier is None or registry is None:
|
||||
return None
|
||||
|
||||
agents = registry.list_agents()
|
||||
return await classifier.classify(message, agents)
|
||||
|
||||
Reference in New Issue
Block a user