Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active September 28, 2023 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/49d4f35e6cb243ffefb83c7ee471a2d8 to your computer and use it in GitHub Desktop.
Save gistlyn/49d4f35e6cb243ffefb83c7ee471a2d8 to your computer and use it in GitHub Desktop.
typechat.mjs
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment