-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
76 lines (68 loc) · 1.71 KB
/
cli.ts
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
import type { DatabaseConfig } from "./src/db/Database";
import { parseArgs } from "util";
import { DatabaseFactory } from "./src/db/DatabaseFactory";
import { translateToSQL } from "./src/llm";
interface Args {
query: string;
model: string;
db: string;
dataFilename?: string;
schemaFilename?: string;
exec?: boolean;
}
export function getArgs(): Args {
const { values, positionals } = parseArgs({
args: Bun.argv,
options: {
model: {
type: "string",
default: "llama3",
},
db: {
type: "string",
default: "pocketbase",
},
dataFilename: {
type: "string",
},
schemaFilename: {
type: "string",
},
exec: {
type: "boolean",
},
},
strict: true,
allowPositionals: true,
});
const [, , ...rest] = positionals;
const { model, db, dataFilename, schemaFilename, exec } = values;
return {
query: rest.join(" "),
model: model!!,
db: db!!,
dataFilename,
schemaFilename,
exec,
};
}
async function run() {
const args = getArgs();
const { query, model, db: dbType, dataFilename, schemaFilename, exec } = args;
const dataFilePath = `${import.meta.dir}/${dataFilename}` || "";
const schemaFilePath = `${import.meta.dir}/${schemaFilename}` || "";
const conf: DatabaseConfig = {
type: dbType || "",
filename: dataFilePath,
schemaFilename: schemaFilePath,
};
const db = DatabaseFactory.createDatabase(conf);
const schema = db.getSchemaForLLM();
const sql = await translateToSQL(query, schema, model!!);
console.log(`\n### Generated SQL: <${sql}>`);
if (exec) {
const result = db.query(sql);
console.log("*** Results: ", result);
}
}
run();