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|
+
"""
|
| 4 |
+
|
| 5 |
+
import time
|
| 6 |
+
from typing import Dict, Any, Callable
|
| 7 |
+
|
| 8 |
+
def tool_list_tables(db):
|
| 9 |
+
"""Tool to list available tables in the database."""
|
| 10 |
+
def _list_tables(args):
|
| 11 |
+
return db.get_tables()
|
| 12 |
+
|
| 13 |
+
return {
|
| 14 |
+
"name": "list_tables",
|
| 15 |
+
"description": "List available tables in the database, categorized by pipeline stage",
|
| 16 |
+
"parameters": {
|
| 17 |
+
"type": "object",
|
| 18 |
+
"properties": {},
|
| 19 |
+
"required": []
|
| 20 |
+
},
|
| 21 |
+
"function": _list_tables
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def tool_describe_table(db):
|
| 25 |
+
"""Tool to describe a table schema."""
|
| 26 |
+
def _describe_table(args):
|
| 27 |
+
table_name = args.get("table_name")
|
| 28 |
+
if not table_name:
|
| 29 |
+
return {"error": "Missing table_name parameter"}
|
| 30 |
+
|
| 31 |
+
query = f"DESCRIBE {table_name}"
|
| 32 |
+
return db.execute_query(query)
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"name": "describe_table",
|
| 36 |
+
"description": "Get the schema of a specific table",
|
| 37 |
+
"parameters": {
|
| 38 |
+
"type": "object",
|
| 39 |
+
"properties": {
|
| 40 |
+
"table_name": {
|
| 41 |
+
"type": "string",
|
| 42 |
+
"description": "Name of the table to describe"
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
"required": ["table_name"]
|
| 46 |
+
},
|
| 47 |
+
"function": _describe_table
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
def tool_sample_table(db):
|
| 51 |
+
"""Tool to get a sample of data from a table."""
|
| 52 |
+
def _sample_table(args):
|
| 53 |
+
table_name = args.get("table_name")
|
| 54 |
+
rows = args.get("rows", 5)
|
| 55 |
+
|
| 56 |
+
if not table_name:
|
| 57 |
+
return {"error": "Missing table_name parameter"}
|
| 58 |
+
|
| 59 |
+
return db.get_table_sample(table_name, rows)
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
"name": "sample_table",
|
| 63 |
+
"description": "Get a sample of rows from a specific table",
|
| 64 |
+
"parameters": {
|
| 65 |
+
"type": "object",
|
| 66 |
+
"properties": {
|
| 67 |
+
"table_name": {
|
| 68 |
+
"type": "string",
|
| 69 |
+
"description": "Name of the table to sample"
|
| 70 |
+
},
|
| 71 |
+
"rows": {
|
| 72 |
+
"type": "integer",
|
| 73 |
+
"description": "Number of rows to return (default: 5)"
|
| 74 |
+
}
|
| 75 |
+
},
|
| 76 |
+
"required": ["table_name"]
|
| 77 |
+
},
|
| 78 |
+
"function": _sample_table
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
def tool_execute_query(db):
|
| 82 |
+
"""Tool to execute a SQL query."""
|
| 83 |
+
def _execute_query(args):
|
| 84 |
+
query = args.get("query")
|
| 85 |
+
|
| 86 |
+
if not query:
|
| 87 |
+
return {"error": "Missing query parameter"}
|
| 88 |
+
|
| 89 |
+
return db.execute_query(query)
|
| 90 |
+
|
| 91 |
+
return {
|
| 92 |
+
"name": "execute_query",
|
| 93 |
+
"description": "Execute a SQL query on the database",
|
| 94 |
+
"parameters": {
|
| 95 |
+
"type": "object",
|
| 96 |
+
"properties": {
|
| 97 |
+
"query": {
|
| 98 |
+
"type": "string",
|
| 99 |
+
"description": "SQL query to execute"
|
| 100 |
+
}
|
| 101 |
+
},
|
| 102 |
+
"required": ["query"]
|
| 103 |
+
},
|
| 104 |
+
"function": _execute_query
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
def tool_get_confidence(state_provider: Callable[[], Dict[str, Any]]):
|
| 108 |
+
"""
|
| 109 |
+
Tool to calculate confidence score for the current plan.
|
| 110 |
+
Takes a function that returns the current state to ensure we have the latest.
|
| 111 |
+
"""
|
| 112 |
+
def _get_confidence(args):
|
| 113 |
+
area = args.get("area", "overall")
|
| 114 |
+
state = state_provider()
|
| 115 |
+
user_intent = state.get("user_intent", {})
|
| 116 |
+
pipeline_plan = state.get("pipeline_plan", {})
|
| 117 |
+
|
| 118 |
+
# This would implement a real confidence scoring system
|
| 119 |
+
# For demo, we'll return simulated confidence scores
|
| 120 |
+
|
| 121 |
+
completeness = len(user_intent) / 5 # Simulate based on intent completeness
|
| 122 |
+
clarity = 0.7 if "target_tables" in pipeline_plan else 0.3
|
| 123 |
+
feasibility = 0.85 # High by default for demo
|
| 124 |
+
|
| 125 |
+
if area == "intent":
|
| 126 |
+
return {"confidence": round(completeness * 100, 1), "area": "intent"}
|
| 127 |
+
elif area == "plan":
|
| 128 |
+
return {"confidence": round(clarity * 100, 1), "area": "plan"}
|
| 129 |
+
elif area == "feasibility":
|
| 130 |
+
return {"confidence": round(feasibility * 100, 1), "area": "feasibility"}
|
| 131 |
+
else:
|
| 132 |
+
overall = (completeness + clarity + feasibility) / 3
|
| 133 |
+
return {"confidence": round(overall * 100, 1), "area": "overall"}
|
| 134 |
+
|
| 135 |
+
return {
|
| 136 |
+
"name": "get_confidence",
|
| 137 |
+
"description": "Calculate confidence score for the current plan or specific area",
|
| 138 |
+
"parameters": {
|
| 139 |
+
"type": "object",
|
| 140 |
+
"properties": {
|
| 141 |
+
"area": {
|
| 142 |
+
"type": "string",
|
| 143 |
+
"description": "Area to get confidence for (intent, plan, feasibility, or overall)",
|
| 144 |
+
"enum": ["intent", "plan", "feasibility", "overall"]
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
},
|
| 148 |
+
"function": _get_confidence
|
| 149 |
+
}
|