/
typechat.mjs
48 lines (42 loc) · 1.76 KB
/
typechat.mjs
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
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { createLanguageModel, createJsonTranslator, createProgramTranslator } from 'typechat'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const useProgram = process.argv[2].toLowerCase() === "program"
const model = createLanguageModel(process.env)
const schema = await fs.readFile(path.join(__dirname, process.argv[3]), "utf8")
let schemaName = ""
if (!useProgram) {
const firstInterfacePos = schema.indexOf('export interface ')
const firstTypePos = schema.indexOf('export type ')
const endInterfacePos = schema.indexOf('{', firstInterfacePos)
const endTypePos = schema.indexOf(' = {', firstTypePos)
if (firstInterfacePos === -1 && firstTypePos === -1) {
console.log(JSON.stringify({
responseStatus: {
errorCode: 'Error',
message: "No interface or type found in schema"
}
}, undefined, 2))
}
schemaName = firstInterfacePos !== -1 && (firstTypePos === -1 || firstInterfacePos < firstTypePos)
? schema.substring(firstInterfacePos + 'export interface '.length, endInterfacePos).trim()
: schema.substring(firstTypePos + 'export type '.length, endTypePos).trim()
}
const translator = useProgram
? createProgramTranslator(model, schema)
: createJsonTranslator(model, schema, schemaName)
const response = await translator.translate(process.argv[4])
if (!response.success) {
console.log(JSON.stringify({
responseStatus: {
errorCode: 'Error',
message: response.message
}
}, undefined, 2))
} else {
const cart = response.data
console.log(JSON.stringify(cart, undefined, 2))
}