Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Add validation for .lu and .dispatch file (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Favretto authored and darrenj committed May 9, 2019
1 parent 5700ddb commit ffc9344
Showing 1 changed file with 97 additions and 70 deletions.
167 changes: 97 additions & 70 deletions lib/typescript/botskills/src/functionality/connectSkill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,77 +63,104 @@ function validateManifestSchema(skillManifest: ISkillManifest): void {
}
}

// tslint:disable-next-line:max-func-body-length
async function updateDispatch(configuration: IConnectConfiguration, manifest: ISkillManifest): Promise<void> {
// Initializing variables for the updateDispatch scope
const dispatchFile: string = `${configuration.dispatchName}.dispatch`;
const dispatchJsonFile: string = `${configuration.dispatchName}.json`;
const intentName: string = manifest.id;
let luisDictionary: Map<string, string>;

logger.message('Getting intents for dispatch...');

luisDictionary = manifest.actions.filter((action: IAction) => action.definition.triggers.utteranceSources)
.reduce((acc: IUtteranceSource[], val: IAction) => acc.concat(val.definition.triggers.utteranceSources), [])
.reduce((acc: string[], val: IUtteranceSource) => acc.concat(val.source), [])
.reduce(
(acc: Map<string, string>, val: string) => {
const luis: string[] = val.split('#');
if (acc.has(luis[0])) {
const previous: string | undefined = acc.get(luis[0]);
acc.set(luis[0], previous + luis[1]);
} else {
acc.set(luis[0], luis[1]);
}

return acc;
},
new Map());

logger.message('Adding skill to Dispatch');

await Promise.all(
Array.from(luisDictionary.entries())
.map(async(item: [string, string]) => {
const luisApp: string = item[0];
const luFile: string = `${luisApp}.lu`;
const luisFile: string = `${luisApp}.luis`;

// Parse LU file
logger.message(`Parsing ${luisApp} LU file...`);
const ludownParseCommand: string[] = ['ludown', 'parse', 'toluis'];
ludownParseCommand.push(...['--in', join(configuration.luisFolder, luFile)]);
ludownParseCommand.push(...['--luis_culture', configuration.language]);
ludownParseCommand.push(...['--out_folder', configuration.luisFolder]); //luisFolder should point to 'en' folder inside LUIS folder
ludownParseCommand.push(...['--out', `"${luisApp}.luis"`]);

const dispatchAddCommand: string[] = ['dispatch', 'add'];
dispatchAddCommand.push(...['--type', 'file']);
dispatchAddCommand.push(...['--name', manifest.id]);
dispatchAddCommand.push(...['--filePath', join(configuration.luisFolder, luisFile)]);
dispatchAddCommand.push(...['--intentName', intentName]);
dispatchAddCommand.push(...['--dataFolder', configuration.dispatchFolder]);
dispatchAddCommand.push(...['--dispatch', join(configuration.dispatchFolder, dispatchFile)]);

logger.message(await runCommand(ludownParseCommand, `Parsing ${luisApp} LU file`));
logger.message(await runCommand(dispatchAddCommand, `Executing dispatch add for the ${luisApp} LU file`));
}));

logger.message('Running dispatch refresh...');

const dispatchRefreshCommand: string[] = ['dispatch', 'refresh'];
dispatchRefreshCommand.push(...['--dispatch', join(configuration.dispatchFolder, dispatchFile)]);
dispatchRefreshCommand.push(...['--dataFolder', configuration.dispatchFolder]);

await runCommand(dispatchRefreshCommand, `Executing dispatch refresh for the ${configuration.dispatchName} file`);

logger.message('Running LuisGen...');

const luisgenCommand: string[] = ['luisgen'];
luisgenCommand.push(join(configuration.dispatchFolder, dispatchJsonFile));
luisgenCommand.push(...[`-${configuration.lgLanguage}`, `"DispatchLuis"`]);
luisgenCommand.push(...['-o', configuration.lgOutFolder]);

await runCommand(luisgenCommand, `Executing luisgen for the ${configuration.dispatchName} file`);
try {
// Initializing variables for the updateDispatch scope
const dispatchFile: string = `${configuration.dispatchName}.dispatch`;
const dispatchJsonFile: string = `${configuration.dispatchName}.json`;
const dispatchFilePath: string = join(configuration.dispatchFolder, dispatchFile);
const intentName: string = manifest.id;
let luisDictionary: Map<string, string>;

logger.message('Getting intents for dispatch...');
luisDictionary = manifest.actions.filter((action: IAction) => action.definition.triggers.utteranceSources)
.reduce((acc: IUtteranceSource[], val: IAction) => acc.concat(val.definition.triggers.utteranceSources), [])
.reduce((acc: string[], val: IUtteranceSource) => acc.concat(val.source), [])
.reduce(
(acc: Map<string, string>, val: string) => {
const luis: string[] = val.split('#');
if (acc.has(luis[0])) {
const previous: string | undefined = acc.get(luis[0]);
acc.set(luis[0], previous + luis[1]);
} else {
acc.set(luis[0], luis[1]);
}

return acc;
},
new Map());

logger.message('Adding skill to Dispatch');
await Promise.all(
Array.from(luisDictionary.entries())
.map(async(item: [string, string]) => {
const luisApp: string = item[0];
const luFile: string = `${luisApp}.lu`;
const luisFile: string = `${luisApp}.luis`;
const luFilePath: string = join(configuration.luisFolder, luFile);
const luisFilePath: string = join(configuration.luisFolder, luisFile);

// Validate 'ludown' arguments
if (!existsSync(configuration.luisFolder)) {
throw(new Error(`Path to the LUIS folder (${configuration.luisFolder}) leads to a nonexistent folder.`));
} else if (!existsSync(luFilePath)) {
throw(new Error(`Path to the ${luisApp}.lu file leads to a nonexistent file.`));
}

// Validate 'dispatch add' arguments
if (!existsSync(configuration.dispatchFolder)) {
throw(new Error(`Path to the Dispatch folder (${configuration.dispatchFolder}) leads to a nonexistent folder.`));
} else if (!existsSync(dispatchFilePath)) {
throw(new Error(`Path to the ${dispatchFile} file leads to a nonexistent file.`));
}

// Parse LU file
logger.message(`Parsing ${luisApp} LU file...`);
const ludownParseCommand: string[] = ['ludown', 'parse', 'toluis'];
ludownParseCommand.push(...['--in', luFilePath]);
ludownParseCommand.push(...['--luis_culture', configuration.language]);
ludownParseCommand.push(...['--out_folder', configuration.luisFolder]);
ludownParseCommand.push(...['--out', `"${luisApp}.luis"`]);

await runCommand(ludownParseCommand, `Parsing ${luisApp} LU file`);

if (!existsSync(luisFilePath)) {
// tslint:disable-next-line: max-line-length
throw(new Error(`Path to the ${luisFile} leads to a nonexistent file. Make sure the ludown command is being executed successfully`));
}
// Update Dispatch file
const dispatchAddCommand: string[] = ['dispatch', 'add'];
dispatchAddCommand.push(...['--type', 'file']);
dispatchAddCommand.push(...['--name', manifest.id]);
dispatchAddCommand.push(...['--filePath', luisFilePath]);
dispatchAddCommand.push(...['--intentName', intentName]);
dispatchAddCommand.push(...['--dataFolder', configuration.dispatchFolder]);
dispatchAddCommand.push(...['--dispatch', dispatchFilePath]);

await runCommand(dispatchAddCommand, `Executing dispatch add for the ${luisApp} LU file`);
}));

logger.message('Running dispatch refresh...');
const dispatchRefreshCommand: string[] = ['dispatch', 'refresh'];
dispatchRefreshCommand.push(...['--dispatch', dispatchFilePath]);
dispatchRefreshCommand.push(...['--dataFolder', configuration.dispatchFolder]);

await runCommand(dispatchRefreshCommand, `Executing dispatch refresh for the ${configuration.dispatchName} file`);

logger.message('Running LuisGen...');
const luisgenCommand: string[] = ['luisgen'];
luisgenCommand.push(join(configuration.dispatchFolder, dispatchJsonFile));
luisgenCommand.push(...[`-${configuration.lgLanguage}`, `"DispatchLuis"`]);
luisgenCommand.push(...['-o', configuration.lgOutFolder]);

await runCommand(luisgenCommand, `Executing luisgen for the ${configuration.dispatchName} file`);

logger.success('Successfully updated Dispatch model');
} catch (err) {
logger.error(`An error ocurred while updating the Dispatch model: ${err}`);
process.exit(1);
}
}

export async function connectSkill(configuration: IConnectConfiguration): Promise<void> {
Expand Down

0 comments on commit ffc9344

Please sign in to comment.