@botmock/export
Advanced tools
Comparing version 0.16.0 to 0.17.0
@@ -33,14 +33,29 @@ "use strict"; | ||
_generateStories.set(this, (intents, blocks) => { | ||
var _a, _b, _c, _d; | ||
const storyName = `path_${uuid_1.v4()}`; | ||
const storyStack = []; | ||
let currentNodeId = (_a = blocks.find(block => block.is_root)) === null || _a === void 0 ? void 0 : _a.message_id; | ||
while (currentNodeId) { | ||
const currentNode = blocks.find(block => block.message_id === currentNodeId); | ||
const nextMessagesEmanatingIntent = (_b = currentNode === null || currentNode === void 0 ? void 0 : currentNode.next_message_ids.filter(next => !!next.intent.value)) !== null && _b !== void 0 ? _b : []; | ||
switch (nextMessagesEmanatingIntent.length) { | ||
case 1: | ||
const block = (_c = blocks.find(block => block.message_id === nextMessagesEmanatingIntent[0].message_id)) === null || _c === void 0 ? void 0 : _c.message_id; | ||
const responseName = `utter_${block}`; | ||
const intentName = (_d = intents.find(intent => intent.id === nextMessagesEmanatingIntent[0].intent.value)) === null || _d === void 0 ? void 0 : _d.name; | ||
storyStack.push(new Set([intentName, responseName])); | ||
currentNodeId = nextMessagesEmanatingIntent[0].message_id; | ||
break; | ||
case 0: | ||
currentNodeId = undefined; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
return [ | ||
{ | ||
[`path_${uuid_1.v4()}`]: intents.reduce((intentList, intent) => { | ||
var _a, _b; | ||
const idOfBlockConnectedToIntent = (_a = blocks | ||
.flatMap(block => block.next_message_ids) | ||
.find(next => next.intent.value === intent.id)) === null || _a === void 0 ? void 0 : _a.message_id; | ||
if (!idOfBlockConnectedToIntent) { | ||
return intentList; | ||
} | ||
const nodeName = (_b = blocks.find(block => block.message_id === idOfBlockConnectedToIntent)) === null || _b === void 0 ? void 0 : _b.message_id; | ||
return Object.assign(Object.assign({}, intentList), { [intent.name]: [`utter_${nodeName === null || nodeName === void 0 ? void 0 : nodeName.toLowerCase().replace(/\s/g, "_")}`] }); | ||
[storyName]: Array.from(storyStack).reduce((acc, value) => { | ||
const [intentName, responseName] = value; | ||
return Object.assign(Object.assign({}, acc), { [intentName]: responseName.toLowerCase().replace(/\s/g, "_") }); | ||
}, {}) | ||
@@ -50,3 +65,3 @@ } | ||
}); | ||
_generateIntentGroupings.set(this, (intents) => { | ||
_generateIntentGroupings.set(this, (intents, entities) => { | ||
return intents.reduce((structure, intent) => { | ||
@@ -57,3 +72,12 @@ const intentName = `intent:${intent.name}`; | ||
.reduce((utterances, value) => { | ||
return [...utterances, ...value.map(v => v.text)]; | ||
return [...utterances, ...value.map((v) => { | ||
var _a, _b, _c; | ||
const [firstVariable] = (_a = v.variables) !== null && _a !== void 0 ? _a : []; | ||
let entityName = ""; | ||
if (firstVariable === null || firstVariable === void 0 ? void 0 : firstVariable.entity) { | ||
entityName = (_c = (_b = entities.find(entity => entity.id === firstVariable.entity)) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : ""; | ||
} | ||
console.log(entityName); | ||
return this.replaceEntityCharacters(v.text, "[", `](${entityName})`); | ||
})]; | ||
}, []); | ||
@@ -109,3 +133,3 @@ return Object.assign(Object.assign({}, structure), { [intentName]: utterancesUnderIntent }); | ||
data: [ | ||
__classPrivateFieldGet(this, _generateIntentGroupings).call(this, resources.intents), | ||
__classPrivateFieldGet(this, _generateIntentGroupings).call(this, resources.intents, resources.entities), | ||
__classPrivateFieldGet(this, _generateStories).call(this, resources.intents, resources.board.board.messages), | ||
@@ -116,3 +140,3 @@ ], | ||
_rootTransformation.set(this, (resources) => { | ||
const templates = resources.board.board.messages | ||
const responses = resources.board.board.messages | ||
.reduce((templatesForActions, block) => { | ||
@@ -135,6 +159,8 @@ if (block.is_root) { | ||
intents: resources.intents.map(intent => intent.name), | ||
actions: Object.keys(templates), | ||
entities: resources.variables.map(variable => variable.name.replace(/\s/, "")), | ||
slots: this.representRequiredSlots(resources.intents, resources.variables), | ||
templates, | ||
responses, | ||
session_config: { | ||
session_expiration_time: 60, | ||
carry_over_slots_to_new_session: true, | ||
} | ||
}, | ||
@@ -141,0 +167,0 @@ }; |
{ | ||
"name": "@botmock/export", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
@@ -20,19 +20,37 @@ import Exporter from '../base-exporter'; | ||
* Generate structure of path name -> list of intents -> list of connected actions. | ||
* @param intents {@link Botmock.Intent[]} | ||
* @param blocks {@link Botmock.Message[]} | ||
* @param {Botmock.Intent[]} intents Intent objects | ||
* @param {Botmock.Message[]} blocks Message objects | ||
*/ | ||
#generateStories = (intents: Botmock.Intent[], blocks: Botmock.Message[]): [Rasa.Story<unknown>] => { | ||
const storyName = `path_${v4()}`; | ||
const storyStack: Set<string>[] = []; | ||
let currentNodeId: string | void = blocks.find(block => block.is_root)?.message_id; | ||
while (currentNodeId) { | ||
const currentNode = blocks.find(block => block.message_id === currentNodeId); | ||
const nextMessagesEmanatingIntent = currentNode?.next_message_ids.filter(next => !!next.intent.value) ?? []; | ||
switch (nextMessagesEmanatingIntent.length) { | ||
case 1: | ||
const block = blocks.find(block => block.message_id === nextMessagesEmanatingIntent[0].message_id)?.message_id; | ||
const responseName = `utter_${block}`; | ||
const intentName = intents.find(intent => intent.id === nextMessagesEmanatingIntent[0].intent.value)?.name as string; | ||
storyStack.push(new Set([intentName, responseName])); | ||
currentNodeId = nextMessagesEmanatingIntent[0].message_id; | ||
break; | ||
case 0: | ||
currentNodeId = undefined; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
return [ | ||
{ | ||
[`path_${v4()}`]: intents.reduce((intentList, intent) => { | ||
const idOfBlockConnectedToIntent = blocks | ||
.flatMap(block => block.next_message_ids) | ||
.find(next => next.intent.value === intent.id)?.message_id; | ||
if (!idOfBlockConnectedToIntent) { | ||
return intentList; | ||
} | ||
const nodeName = blocks.find(block => block.message_id === idOfBlockConnectedToIntent)?.message_id; | ||
[storyName]: Array.from(storyStack).reduce((acc, value) => { | ||
const [intentName, responseName] = value; | ||
return { | ||
...intentList, | ||
[intent.name]: [`utter_${nodeName?.toLowerCase().replace(/\s/g, "_")}`], | ||
...acc, | ||
[intentName]: responseName.toLowerCase().replace(/\s/g, "_") | ||
}; | ||
@@ -47,3 +65,3 @@ }, {}) | ||
*/ | ||
#generateIntentGroupings = (intents: Botmock.Intent[]): Rasa.NLU => { | ||
#generateIntentGroupings = (intents: Botmock.Intent[], entities: Botmock.Entity[]): Rasa.NLU => { | ||
return intents.reduce((structure, intent) => { | ||
@@ -53,4 +71,14 @@ const intentName = `intent:${intent.name}`; | ||
.values(intent.utterances) | ||
.reduce((utterances: string[], value) => { | ||
return [...utterances, ...value.map(v => v.text)]; | ||
.reduce((utterances: string[], value: any) => { | ||
return [...utterances, ...value.map((v: Botmock.Utterance) => { | ||
const [firstVariable] = v.variables ?? []; | ||
let entityName: string = ""; | ||
if (firstVariable?.entity) { | ||
entityName = entities.find(entity => entity.id === firstVariable.entity)?.name ?? ""; | ||
} | ||
console.log(entityName); | ||
return this.replaceEntityCharacters(v.text, "[", `](${entityName})`); | ||
})]; | ||
}, []); | ||
@@ -127,2 +155,3 @@ return { | ||
*/ | ||
// @ts-ignore | ||
private representRequiredSlots(intents: Botmock.Intent[], variables: Botmock.Variable[]): any[] { | ||
@@ -153,3 +182,3 @@ const uniqueNamesOfRequiredSlots = Array.from(this.representRequirementsForIntents(intents)) | ||
data: [ | ||
this.#generateIntentGroupings(resources.intents), | ||
this.#generateIntentGroupings(resources.intents, resources.entities), | ||
this.#generateStories(resources.intents, resources.board.board.messages), | ||
@@ -160,3 +189,3 @@ ], | ||
#rootTransformation = (resources: Resources): DataTransformation => { | ||
const templates = (resources.board.board.messages as Botmock.Message[]) | ||
const responses = (resources.board.board.messages as Botmock.Message[]) | ||
.reduce((templatesForActions, block) => { | ||
@@ -169,2 +198,3 @@ if (block.is_root) { | ||
const templates: void | Rasa.Template<unknown> = this.#unpackTemplatesInBlock(block); | ||
if (Object.keys(templates).length <= 0) { | ||
@@ -182,6 +212,9 @@ return templatesForActions; | ||
intents: (resources.intents as Botmock.Intent[]).map(intent => intent.name), | ||
actions: Object.keys(templates), | ||
entities: (resources.variables as Botmock.Variable[]).map(variable => variable.name.replace(/\s/, "")), | ||
slots: this.representRequiredSlots(resources.intents, resources.variables), | ||
templates, | ||
// slots: this.representRequiredSlots(resources.intents, resources.variables), | ||
responses, | ||
session_config: { | ||
session_expiration_time: 60, | ||
carry_over_slots_to_new_session: true, | ||
} | ||
}, | ||
@@ -188,0 +221,0 @@ }; |
Sorry, the diff of this file is not supported yet
349826
7027