Skip to content

Commit

Permalink
apply pending changes (NLU sync missing)
Browse files Browse the repository at this point in the history
  • Loading branch information
emirotin committed Oct 11, 2019
1 parent b5acb7f commit ea4b8f4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
2 changes: 1 addition & 1 deletion modules/misunderstood/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- [x] Intent picker
- [x] Pending list
- [x] Apply all button
- [ ] API to apply the changes
- [x] API to apply the changes
- [x] Deleted list
- [x] Return action
- [x] Applied list
Expand Down
4 changes: 3 additions & 1 deletion modules/misunderstood/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"module-builder": "../../build/module-builder",
"tslint": "^5.20.0"
},
"dependencies": {},
"dependencies": {
"bluebird": "^3.7.0"
},
"webpack": {
"externals": {
"react": "React",
Expand Down
68 changes: 67 additions & 1 deletion modules/misunderstood/src/backend/applyChanges.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
import Bluebird from 'bluebird'
import * as sdk from 'botpress/sdk'
import memoize from 'lodash/memoize'
import uniq from 'lodash/uniq'

const applyChanges = (knex: sdk.KnexExtended, botId: string) => { }
import { QnaEntry } from '../../../qna/src/backend/qna'
import { DbFlaggedEvent, FLAGGED_MESSAGE_STATUS, RESOLUTION_TYPE } from '../types'

const applyChanges = (bp: typeof sdk, botId: string, tableName: string) => {
const knex = bp.database

return knex.transaction(async trx => {
const events: DbFlaggedEvent[] = await trx(tableName)
.select('id', 'language', 'preview', 'resolutionType', 'resolution', 'resolutionParams')
.where({ botId, status: FLAGGED_MESSAGE_STATUS.pending })

const qnaEvents = events.filter(({ resolutionType }) => resolutionType === RESOLUTION_TYPE.qna)
const nluEvents = events.filter(({ resolutionType }) => resolutionType === RESOLUTION_TYPE.intent)

const botGhost = bp.ghost.forBot(botId)

const addQnA = async (event: DbFlaggedEvent) => {
const qnaId = event.resolution
const qnaEntry: { data: QnaEntry } = await botGhost.readFileAsObject('qna', `${qnaId}.json`)
qnaEntry.data.questions[event.language] = uniq([...(qnaEntry.data.questions[event.language] || []), event.preview])
await botGhost.upsertFile('qna', `${qnaId}.json`, JSON.stringify(qnaEntry, null, 2))
}

const normalizeUtterance = memoize((utterance: string) => utterance.replace(/\[([^\]]+)\]\([^\)]+\)/g, '$1'))

const removeNLU = (language: string, utterance: string) => async (fileName: string) => {
const nluEntry: sdk.NLU.IntentDefinition = await botGhost.readFileAsObject('intents', fileName)
if (nluEntry.utterances[language]) {
nluEntry.utterances[language] = nluEntry.utterances[language].filter(u => normalizeUtterance(u) !== normalizeUtterance(utterance))
}
await botGhost.upsertFile('intents', fileName, JSON.stringify(nluEntry, null, 2))
}

const addNLU = async (event: DbFlaggedEvent) => {
const intentName = event.resolution

const nluFiles = (await botGhost.directoryListing('intents', '*.json', '__qna__*'))
.filter(fileName => fileName !== `${intentName}.json`)
await Bluebird.mapSeries(nluFiles, removeNLU(event.language, event.preview))

const nluEntry: sdk.NLU.IntentDefinition = await botGhost.readFileAsObject('intents', `${intentName}.json`)
nluEntry.utterances[event.language] = uniq([...(nluEntry.utterances[event.language] || []), event.preview])

const params = event.resolutionParams && typeof event.resolutionParams !== 'object'
? JSON.parse(event.resolutionParams) : {}
if (params.contexts) {
nluEntry.contexts = uniq([...(nluEntry.contexts || []), ...params.contexts])
}

await botGhost.upsertFile('intents', `${intentName}.json`, JSON.stringify(nluEntry, null, 2))
}

await Bluebird.mapSeries(qnaEvents, addQnA)

await Bluebird.mapSeries(nluEvents, addNLU)

// TODO: sync NLU

await trx(tableName).update({
status: FLAGGED_MESSAGE_STATUS.applied,
updatedAt: knex.fn.now()
}).whereIn('id', events.map(({ id }) => id))
})
}

export default applyChanges
4 changes: 2 additions & 2 deletions modules/misunderstood/src/backend/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const EVENTS_TABLE_NAME = 'events'
export default class Db {
knex: Knex & sdk.KnexExtension

constructor(bp: typeof sdk) {
constructor(private bp: typeof sdk) {
this.knex = bp.database
}

Expand Down Expand Up @@ -128,6 +128,6 @@ export default class Db {
}

applyChanges(botId: string) {
return applyChanges(this.knex, botId)
return applyChanges(this.bp, botId, TABLE_NAME)
}
}
2 changes: 1 addition & 1 deletion modules/misunderstood/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type FlaggedEvent = {
status: FLAGGED_MESSAGE_STATUS
resolutionType: RESOLUTION_TYPE
resolution: string | null
resolutionParams: object | null
resolutionParams: string | object | null
}

export type DbFlaggedEvent = FlaggedEvent & {
Expand Down
5 changes: 5 additions & 0 deletions modules/misunderstood/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,11 @@ bluebird@^3.5.1, bluebird@^3.5.5:
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.5.tgz#a8d0afd73251effbbd5fe384a77d73003c17a71f"
integrity sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w==

bluebird@^3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.0.tgz#56a6a886e03f6ae577cffedeb524f8f2450293cf"
integrity sha512-aBQ1FxIa7kSWCcmKHlcHFlT2jt6J/l4FzC7KcPELkOJOsPOb/bccdhmIrKDfXhwFrmc7vDoDrrepFvGqjyXGJg==

bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
version "4.11.8"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
Expand Down

0 comments on commit ea4b8f4

Please sign in to comment.