-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathsql-execute.ts
More file actions
81 lines (75 loc) · 3.09 KB
/
Copy pathsql-execute.ts
File metadata and controls
81 lines (75 loc) · 3.09 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
import type { SqlExecuteResult } from "../native/types"
// altimate_change start - SQL write access control
import { classifyAndCheck } from "./sql-classify"
// altimate_change end
// altimate_change start — progressive disclosure suggestions
import { PostConnectSuggestions } from "./post-connect-suggestions"
// altimate_change end
export const SqlExecuteTool = Tool.define("sql_execute", {
description: "Execute SQL against a connected data warehouse. Returns results as a formatted table.",
parameters: z.object({
query: z.string().describe("SQL query to execute"),
warehouse: z.string().optional().describe("Warehouse connection name"),
limit: z.number().optional().default(100).describe("Max rows to return"),
}),
async execute(args, ctx) {
// altimate_change start - SQL write access control
// Permission checks OUTSIDE try/catch so denial errors propagate to the framework
const { queryType, blocked } = classifyAndCheck(args.query)
if (blocked) {
throw new Error("DROP DATABASE, DROP SCHEMA, and TRUNCATE are blocked for safety. This cannot be overridden.")
}
if (queryType === "write") {
await ctx.ask({
permission: "sql_execute_write",
patterns: [args.query.slice(0, 200)],
always: ["*"],
metadata: { queryType },
})
}
// altimate_change end
try {
const result = await Dispatcher.call("sql.execute", {
sql: args.query,
warehouse: args.warehouse,
limit: args.limit,
})
let output = formatResult(result)
// altimate_change start — progressive disclosure suggestions
const suggestion = PostConnectSuggestions.getProgressiveSuggestion("sql_execute")
if (suggestion) {
output += "\n\n" + suggestion
PostConnectSuggestions.trackSuggestions({
suggestionType: "progressive_disclosure",
suggestionsShown: ["sql_analyze"],
warehouseType: args.warehouse ?? "default",
})
}
// altimate_change end
return {
title: `SQL: ${args.query.slice(0, 60)}${args.query.length > 60 ? "..." : ""}`,
metadata: { rowCount: result.row_count, truncated: result.truncated },
output,
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return {
title: "SQL: ERROR",
metadata: { rowCount: 0, truncated: false },
output: `Failed to execute SQL: ${msg}\n\nEnsure the dispatcher is running and a warehouse connection is configured.`,
}
}
},
})
function formatResult(result: SqlExecuteResult): string {
if (result.row_count === 0) return "(0 rows)"
const header = result.columns.join(" | ")
const separator = result.columns.map((c) => "-".repeat(Math.max(c.length, 4))).join("-+-")
const rows = result.rows.map((r) => r.map((v) => (v === null ? "NULL" : String(v))).join(" | ")).join("\n")
let output = `${header}\n${separator}\n${rows}\n\n(${result.row_count} rows)`
if (result.truncated) output += " [truncated]"
return output
}