-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathaltimate-core-fix.ts
More file actions
57 lines (55 loc) · 2.03 KB
/
altimate-core-fix.ts
File metadata and controls
57 lines (55 loc) · 2.03 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
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
export const AltimateCoreFixTool = Tool.define("altimate_core_fix", {
description:
"Auto-fix SQL errors using the Rust-based altimate-core engine. Uses fuzzy matching and iterative re-validation to correct syntax errors, typos, and schema reference issues.",
parameters: z.object({
sql: z.string().describe("SQL query to fix"),
schema_path: z.string().optional().describe("Path to YAML/JSON schema file"),
schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"),
max_iterations: z.number().optional().describe("Maximum fix iterations (default: 5)"),
}),
async execute(args, ctx) {
try {
const result = await Dispatcher.call("altimate_core.fix", {
sql: args.sql,
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
max_iterations: args.max_iterations ?? 5,
})
const data = result.data as Record<string, any>
return {
title: `Fix: ${data.success ? "FIXED" : "COULD NOT FIX"}`,
metadata: { success: result.success, fixed: !!data.fixed_sql },
output: formatFix(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Fix: ERROR", metadata: { success: false, fixed: false }, output: `Failed: ${msg}` }
}
},
})
function formatFix(data: Record<string, any>): string {
if (data.error) return `Error: ${data.error}`
const lines: string[] = []
if (data.fixed_sql) {
lines.push("Fixed SQL:")
lines.push(data.fixed_sql)
if (data.changes?.length) {
lines.push("\nChanges applied:")
for (const c of data.changes) {
lines.push(` - ${c.description ?? c}`)
}
}
} else {
lines.push("Could not auto-fix the SQL.")
if (data.errors?.length) {
lines.push("\nErrors found:")
for (const e of data.errors) {
lines.push(` - ${e.message ?? e}`)
}
}
}
return lines.join("\n")
}