-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
69 lines (65 loc) · 2.47 KB
/
main.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
import assert from "assert";
import dotenv from "dotenv";
import findConfig from "find-config";
import fs from "fs";
import path from "path";
import { createJsonTranslator, createLanguageModel } from "typechat";
import { createTypeScriptJsonValidator } from "typechat/ts";
import { processRequests } from "typechat/interactive";
import { Formula, validFormula } from "./schema";
import { myProcessRequests } from "./myProcessRequests";
import { json } from "stream/consumers";
const dotEnvPath = findConfig(".env");
assert(dotEnvPath, ".env file not found!");
dotenv.config({ path: dotEnvPath });
const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join(__dirname, "schema.ts"), "utf8");
const validator = createTypeScriptJsonValidator<Formula>(schema, "Formula");
const translator = createJsonTranslator(model, validator);
function processFormula(formula: validFormula):string {
// Process the items in the cart
let result:string = "";
if(formula.type === "MathFunction") {
result = (formula.functionName + "(");
for(let i = 0; i < formula.functionArgs.length; i++) {
result += processFormula(formula.functionArgs[i]);
if(i < formula.functionArgs.length - 1) {
result += ",";
}
}
result += ")";
}
else if(formula.type === "CellAddress") {
result = (formula.column + formula.row.toString());
}
else if(formula.type === "CellArea") {
result = processFormula(formula.startCellAddress) + ":" + processFormula(formula.endCellAddress);
}
else if(formula.type === "NumberValue") {
result = formula.value.toString();
}
else if(formula.type === "StringValue") {
result = formula.value;
}
return result;
}
// Process requests interactively or from the input file specified on the command line
myProcessRequests("Discribe the formula: ", process.argv[2], async (request) => {
const response = await translator.translate(request);
if (!response.success) {
console.log(response.message);
return;
}
const formula = response.data;
console.log(JSON.stringify(formula, undefined, 2));
let result = formula.result;
/*
if (formula.result.type !== "validFormula") {
console.log("I didn't understand the following:");
if (result.type === "unknown") console.log(result.text);
return;
}
*/
console.log("=" + processFormula(result));
console.log("Success!");
});