-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathschema-inspect.ts
More file actions
67 lines (64 loc) · 2.54 KB
/
schema-inspect.ts
File metadata and controls
67 lines (64 loc) · 2.54 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
import type { SchemaInspectResult } from "../native/types"
// altimate_change start — progressive disclosure suggestions
import { PostConnectSuggestions } from "./post-connect-suggestions"
// altimate_change end
export const SchemaInspectTool = Tool.define("schema_inspect", {
description: "Inspect database schema — list columns, types, and constraints for a table.",
parameters: z.object({
table: z.string().describe("Table name (optionally schema-qualified, e.g. public.orders)"),
schema_name: z.string().optional().describe("Schema name if not in table string"),
warehouse: z.string().optional().describe("Warehouse connection name"),
}),
async execute(args, ctx) {
try {
const result = await Dispatcher.call("schema.inspect", {
table: args.table,
schema_name: args.schema_name,
warehouse: args.warehouse,
})
// altimate_change start — progressive disclosure suggestions
let output = formatSchema(result)
const suggestion = PostConnectSuggestions.getProgressiveSuggestion("schema_inspect")
if (suggestion) {
output += "\n\n" + suggestion
PostConnectSuggestions.trackSuggestions({
suggestionType: "progressive_disclosure",
suggestionsShown: ["lineage_check"],
})
}
// altimate_change end
return {
title: `Schema: ${result.table}`,
metadata: { columnCount: result.columns.length, rowCount: result.row_count },
output,
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return {
title: "Schema: ERROR",
metadata: { columnCount: 0, rowCount: undefined },
output: `Failed to inspect schema: ${msg}\n\nEnsure the dispatcher is running and a warehouse connection is configured.`,
}
}
},
})
function formatSchema(result: SchemaInspectResult): string {
const lines: string[] = []
const qualified = result.schema_name ? `${result.schema_name}.${result.table}` : result.table
lines.push(`Table: ${qualified}`)
if (result.row_count !== null && result.row_count !== undefined) {
lines.push(`Rows: ${result.row_count.toLocaleString()}`)
}
lines.push("")
lines.push("Column | Type | Nullable | PK")
lines.push("-------|------|----------|---")
for (const col of result.columns) {
lines.push(
`${col.name} | ${col.data_type} | ${col.nullable ? "YES" : "NO"} | ${col.primary_key ? "YES" : ""}`,
)
}
return lines.join("\n")
}