-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathaltimate-core-correct.ts
More file actions
64 lines (61 loc) · 2.58 KB
/
altimate-core-correct.ts
File metadata and controls
64 lines (61 loc) · 2.58 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
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
export const AltimateCoreCorrectTool = Tool.define("altimate_core_correct", {
description:
"Iteratively correct SQL using a propose-verify-refine loop. More thorough than fix — applies multiple correction rounds to produce valid SQL. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
sql: z.string().describe("SQL query to correct"),
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"),
}),
async execute(args, ctx) {
try {
const result = await Dispatcher.call("altimate_core.correct", {
sql: args.sql,
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = (result.data ?? {}) as Record<string, any>
const error = result.error ?? data.error ?? extractCorrectErrors(data)
return {
title: `Correct: ${data.success ? "CORRECTED" : "COULD NOT CORRECT"}`,
metadata: { success: result.success, iterations: data.iterations, ...(error && { error }) },
output: formatCorrect(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Correct: ERROR", metadata: { success: false, iterations: 0, error: msg }, output: `Failed: ${msg}` }
}
},
})
function extractCorrectErrors(data: Record<string, any>): string | undefined {
if (data.final_validation?.errors?.length > 0) {
const msgs = data.final_validation.errors.map((e: any) => e.message ?? String(e)).filter(Boolean)
if (msgs.length > 0) return msgs.join("; ")
}
if (Array.isArray(data.errors) && data.errors.length > 0) {
const msgs = data.errors.map((e: any) => e.message ?? String(e)).filter(Boolean)
if (msgs.length > 0) return msgs.join("; ")
}
return undefined
}
function formatCorrect(data: Record<string, any>): string {
if (data.error) return `Error: ${data.error}`
const lines: string[] = []
if (data.corrected_sql) {
lines.push("Corrected SQL:")
lines.push(data.corrected_sql)
}
if (data.iterations) lines.push(`\nIterations: ${data.iterations}`)
if (data.changes?.length) {
lines.push("\nCorrections applied:")
for (const c of data.changes) {
lines.push(` - ${c.description ?? c}`)
}
}
if (!data.corrected_sql && !data.changes?.length) {
lines.push("Could not correct the SQL after maximum iterations.")
}
return lines.join("\n")
}