Skip to content

Commit

Permalink
Merge pull request #196 from Cognigy/bug/nokeyphrase-lexicon-issue-re…
Browse files Browse the repository at this point in the history
…store

Bug/nokeyphrase lexicon issue restore
  • Loading branch information
apdemin authored Jan 13, 2025
2 parents 516856b + 27d9d33 commit 626a31f
Showing 1 changed file with 56 additions and 41 deletions.
97 changes: 56 additions & 41 deletions src/lib/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,51 +178,66 @@ export const pushLexicon = async (
): Promise<void> => {
const lexiconDir = CONFIG.agentDir + "/lexicons/" + lexiconName

if (fs.existsSync(lexiconDir + "/config.json")) {
// config exist for this lexicon, proceed
const spinner = new Spinner(
`Uploading lexicon ${lexiconName} to Cognigy.AI... %s`
// early check if the file is missing
if (!fs.existsSync(`${lexiconDir}/config.json`)) {
console.log(`Lexicon ${lexiconName} can't be found in '${lexiconDir}'`)
process.exit(0)
}

const spinner = new Spinner(
`Uploading lexicon ${lexiconName} to Cognigy.AI... %s`
)
spinner.setSpinnerString("|/-\\")

try {
spinner.start()

const lexiconConfig = JSON.parse(
fs.readFileSync(`${lexiconDir}/config.json`).toString()
)
spinner.setSpinnerString("|/-\\")
try {
spinner.start()
// read local lexicon config
const lexiconConfig = JSON.parse(
fs.readFileSync(lexiconDir + "/config.json").toString()
)
const lexiconId = lexiconConfig.lexiconId

const form = new FormData()
form.append("mode", "overwrite")
form.append("lexiconId", lexiconId)
form.append("file", fs.createReadStream(lexiconDir + "/keyphrases.csv"))

// update Lexicon on Cognigy.AI
const result = await makeAxiosRequest({
path: `/new/v2.0/lexicons/${lexiconId}/import`,
method: "POST",
type: "multipart/form-data",
form: form,
})

await checkTask(result?.data?._id, options?.timeout)
spinner.stop()
} catch (err) {
console.error(
`\n${chalk.red(
"error:"
)} Error when updating Lexicon ${lexiconName} on Cognigy.AI: ${
err.message
}.\nAborting...`
)
const lexiconId = lexiconConfig.lexiconId

const keyphrasesPath = `${lexiconDir}/keyphrases.csv`

if (!fs.existsSync(keyphrasesPath)) {
// early check if the file is missing
throw new Error("keyphrases.csv file is missing.")
}

const fileStats = fs.statSync(keyphrasesPath)

// if the file is empty, skip the API call
if (fileStats.size === 0) {
console.log(`The keyphrases.csv file is empty. Skipping the API call.`)
spinner.stop()
process.exit(0)
return Promise.resolve()
}
} else {
// chart or config are missing, skip
console.log(`Lexicon ${lexiconName} can't be found in '${lexiconDir}'`)
process.exit(0)

const form = new FormData()
form.append("mode", "overwrite")
form.append("lexiconId", lexiconId)
form.append("file", fs.createReadStream(keyphrasesPath))

// update Lexicon on Cognigy.AI
const result = await makeAxiosRequest({
path: `/new/v2.0/lexicons/${lexiconId}/import`,
method: "POST",
type: "multipart/form-data",
form: form,
})

await checkTask(result?.data?._id, options?.timeout)

spinner.stop()
console.log(`Successfully uploaded lexicon ${lexiconName}.`)
} catch (err) {
spinner.stop()
console.error(
`\n${chalk.red("Error:")} Failed to upload Lexicon ${lexiconName} to Cognigy.AI: ${err.message}.\n Aborting...`
)
process.exit(1)
}

return Promise.resolve()
}

Expand Down

0 comments on commit 626a31f

Please sign in to comment.