Spaces:
Runtime error
Runtime error
File size: 3,872 Bytes
6086e71 | 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 68 69 70 71 72 73 74 | """
Agent State β Shared TypedDict for the LangGraph multi-agent pipeline.
Every agent reads from and writes to this state object.
"""
from typing import TypedDict, Optional, Literal
class AgentState(TypedDict, total=False):
"""
Shared state flowing through the LangGraph agent pipeline.
Each agent enriches specific fields and passes state forward.
"""
# ββ Input ββββββββββββββββββββββββββββββββββββββββββββ
user_query: str
conversation_history: list[dict]
tenant_id: str
user_role: str
trace_id: str
# ββ Query Understanding Agent Output βββββββββββββββββ
intent: Literal[
"chat", # Natural conversation
"sql", # Database query that should continue to SQL generation
]
route_intent: Literal[
"chat", # Hi, hello, thanks, capabilities, etc.
"data_query", # SELECT with filters
"aggregation", # COUNT, SUM, AVG, GROUP BY
"comparison", # Compare datasets
"explanation", # Explain a concept or previous query
"meta_query", # Questions about the schema itself
]
entities: list[str] # Extracted table/column names
complexity: Literal["simple", "moderate", "complex"]
retrieval_top_k: int # How many schema docs to fetch (3=simple, 5=default, 8=complex)
# ββ Schema Retrieval Agent Output ββββββββββββββββββββ
relevant_schema: str # Formatted schema context for LLM
relevant_tables: list[str] # Table names retrieved
retrieval_source: str # "rag_top_k:{n}" | "full_schema_fallback" | "meta_query" β for tracing
# ββ SQL Generation Agent Output ββββββββββββββββββββββ
generated_sql: str # The SQL query
sql_explanation: str # Technical explanation
friendly_message: str # User-friendly message
# ββ SQL Validation Agent Output ββββββββββββββββββββββ
is_valid: bool # Safety check passed
validation_errors: list[str] # List of issues found
sanitized_sql: str # SQL after safety modifications (LIMIT injection, etc.)
retry_count: int # Number of regeneration attempts
# ββ Output Guardrail Results βββββββββββββββββββββββββ
guardrail_warnings: list[str] # Hallucinated reference warnings
guardrail_confidence: float # LLM output confidence score (0.0-1.0)
# ββ Execution Agent Output βββββββββββββββββββββββββββ
query_results: list[dict] # Raw query results
execution_time_ms: float # Query execution duration
row_count: int # Number of rows returned
column_names: list[str] # Column names in result set
# ββ Visualization Agent Output βββββββββββββββββββββββ
chart_config: Optional[dict] # Chart.js configuration
chart_type: Optional[str] # bar, line, pie, doughnut, scatter
insights: list[str] # Auto-generated insights
follow_up_questions: list[str] # Suggested follow-up queries
# ββ Error Handling βββββββββββββββββββββββββββββββββββ
error: Optional[str] # Error message if any step failed
error_agent: Optional[str] # Which agent produced the error
|