tdfs4ds.agent.query_optimizer_graph¶
tdfs4ds.agent.query_optimizer_graph
¶
LangGraph-based orchestration for the tdfs4ds query optimizer agent.
Flow::
START -> node_classify -> [conditional on intent] -> node_skill_* -> node_synthesize -> END
_QO_INTENT_NODE_MAP
module-attribute
¶
_QO_INTENT_NODE_MAP = {'EXPLAIN': node_skill_explain, 'SIMPLIFY': node_skill_simplify, 'OPTIMIZE': node_skill_optimize}
QOState
¶
Bases: TypedDict
_persist_inputs
¶
Build state update that remembers sql_query and process_id for next turns.
query_optimizer_report_root
¶
Directory under which OPTIMIZE reports are written (QUERY_OPTIMIZER_REPORT_DIR).
_save_optimization_report
¶
Write the full Markdown report to disk and return its path relative to
:func:query_optimizer_report_root ({domain}/{file}.md), or None on
failure / when there is nothing to save.
Mirrors the file-naming convention used by upload_features in
tdfs4ds/execution.py so reports from both paths land together.
node_skill_simplify
¶
Execute the SQL simplification skill.
node_skill_optimize
¶
Execute the full optimization pipeline skill.
_render_optimize_summary
¶
Compact OPTIMIZE answer: score delta + best SQL + a link to the report.
The full Markdown report is written to disk by node_skill_optimize and
referenced here by report_path (relative to the report root). The serve
layer rewrites that path into a clickable download URL; in-process callers
still get the full report under skill_result['report_full'].
node_synthesize
¶
Format the skill result as a human-readable answer.
build_qo_graph
¶
Build and compile the LangGraph StateGraph for the query optimizer agent.
The skill set is loaded from the qo-skill-* SKILL.md files bundled with
the package (via :func:tdfs4ds.query_optimizer_skill_catalog). Only skills
whose intent is in _QO_INTENT_NODE_MAP are registered as graph nodes.
Returns:
| Type | Description |
|---|---|
|
A compiled LangGraph with MemorySaver checkpointer for multi-turn support. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When no query-optimizer skills are found in the package. |
reset_query_optimizer_agent
¶
Clear the graph singleton cache.
Call this after adding or removing qo-skill-* SKILL.md files so that
the next :func:query_optimizer_agent call picks up the new skill set.
query_optimizer_agent
¶
query_optimizer_agent(question: str, sql_query: Optional[str] = None, process_id: Optional[str] = None, thread_id: str = 'default', return_trace: bool = False) -> Union[str, tuple]
Run the LangGraph query optimizer agent for a single conversational turn.
The agent classifies the question into one of three intents (EXPLAIN /
SIMPLIFY / OPTIMIZE) and routes to the corresponding skill. Multi-turn
context (sql_query, process_id) is persisted across calls that
share the same thread_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
Natural language description of the desired action, e.g. 'Analyze this query for performance issues' or 'Optimize this SQL'. |
required |
sql_query
|
Optional[str]
|
Optional SQL string. When provided it is remembered for
follow-up turns on the same |
None
|
process_id
|
Optional[str]
|
Optional process UUID (looked up in the process catalog to retrieve the SQL automatically). Remembered across turns. |
None
|
thread_id
|
str
|
Conversation thread ID for multi-turn memory. |
'default'
|
return_trace
|
bool
|
When True, returns |
False
|
Returns:
| Type | Description |
|---|---|
Union[str, tuple]
|
Answer string, or |
Union[str, tuple]
|
|