-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexplore_schema.py
More file actions
47 lines (37 loc) · 1.7 KB
/
explore_schema.py
File metadata and controls
47 lines (37 loc) · 1.7 KB
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
"""explore_schema — let the agent discover tables/columns before writing SQL.
Read-only introspection through the :class:`ExplorerPort`. With no ``table``
arg it lists tables; with one it returns full column detail.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from ..core.types import ToolResult, ToolSpec
if TYPE_CHECKING:
from ..harness.context import HarnessContext
class ExploreSchema:
@property
def spec(self) -> ToolSpec:
return ToolSpec(
name="explore_schema",
description="List tables, or describe one table's columns. Call before writing SQL.",
parameters={
"type": "object",
"properties": {
"table": {"type": "string", "description": "table name to describe; omit to list all tables"},
},
},
)
async def run(self, args: dict[str, Any], ctx: "HarnessContext") -> ToolResult:
if ctx.explorer is None:
return ToolResult(call_id="", content="no DB connected (use /connect)", is_error=True)
table = (args.get("table") or "").strip()
if not table:
tables = await ctx.explorer.list_tables()
names = "\n".join(f"- {t.qualified}" for t in tables) or "(no tables)"
return ToolResult(call_id="", content="Tables:\n" + names)
t = await ctx.explorer.describe_table(table)
cols = "\n".join(
f"- {c.name}: {c.type}{'' if c.nullable else ' NOT NULL'}"
f"{(' — ' + c.description) if c.description else ''}"
for c in t.columns
) or "(no columns)"
return ToolResult(call_id="", content=f"{t.qualified}\n{cols}")