Skip to content

Commit

Permalink
Merge pull request #69 from yeuai/dev-plugin
Browse files Browse the repository at this point in the history
Dev plugin, support post-processing
  • Loading branch information
vunb authored Aug 2, 2021
2 parents 926d1c6 + a5bf0fc commit 7654e30
Showing 5 changed files with 64 additions and 7 deletions.
1 change: 1 addition & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -9,3 +9,4 @@ spec:
# - test/engine/conditions.spec.ts
# - test/engine/conditions.spec.ts
# - test/dialogue/sorted.spec.ts
# - test/directives/plugin.spec.ts
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -572,6 +572,13 @@ Syntax:
# normalize message or what you need
console.log('Human say: ', req.message)
return (req, ctx) => {
// do post-processing.
if (req.isNotResponse) {
req.speechResponse = 'I dont know!';
}
}
```
~~~
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yeuai/botscript",
"version": "1.7.1",
"version": "1.7.2",
"description": "A text-based scripting language and bot engine for Conversational User Interfaces (CUI)",
"main": "dist/engine",
"scripts": {
14 changes: 9 additions & 5 deletions src/engine/botscript.ts
Original file line number Diff line number Diff line change
@@ -176,7 +176,7 @@ export class BotScript extends EventEmitter {
}
} else if (/^plugin/.test(item)) {
const vPlugin = this.context.directives.get(item) as Struct;
const vCode = vPlugin.value.replace(/```js([\s\S]*)```/, (m: string, code: string) => code);
const vCode = vPlugin.value.replace(/```js([\s\S]*)```/, (m: string, code: string) => code) as string;
const vName = vPlugin.name.replace(/^plugin:/, '');
this.logger.debug(`javascript code: /plugin: ${vName} => ${vCode}`);
// add custom plugin
@@ -186,13 +186,17 @@ export class BotScript extends EventEmitter {
if (typeof window === 'undefined') {
this.logger.debug('Execute plugin in node!');
const { VmRunner } = await import('../lib/vm2');
// TODO: support post-processing
await VmRunner.run(vCode, { req, ctx });
// support post-processing
const vPostProcessingCallback = await VmRunner.run(vCode, { req, ctx });
this.logger.debug(`Plugin [${vName}] has post-processing function!`);
return vPostProcessingCallback;
} else {
this.logger.debug('Execute plugin in browser!');
const { VmRunner } = await import('../lib/vm');
// TODO: support post-processing
await VmRunner.run(vCode, { req, ctx });
// support post-processing
const vPostProcessingCallback = await VmRunner.run(vCode, { req, ctx });
this.logger.debug(`Plugin [${vName}] has post-processing function!`);
return vPostProcessingCallback;
}

this.logger.debug(`Execute plugin: ${vName} => done!`);
47 changes: 46 additions & 1 deletion test/directives/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ describe('Directive: /plugin', () => {
`);
await bot.init();

it('should load directive /plugin', async () => {
it('should load directive /plugin: test', async () => {
// console.log(bot.context.directives);
// console.log(bot.context.plugins);
assert.isTrue(bot.context.directives.has('plugin:test'), 'contains directive /plugin');
@@ -39,4 +39,49 @@ describe('Directive: /plugin', () => {
});
});

describe('Support post-processing', async () => {
const bot = new BotScript();

bot.parse(`
/plugin: testNoReply
\`\`\`js
req.variables.today = new Date().getDate();
return (req, ctx) => {
// do post-processing.
if (req.speechResponse === 'Hello') {
req.speechResponse = 'Hello Human!';
}
if (req.isNotResponse) {
req.speechResponse = 'I dont know!';
}
}
\`\`\`
> testNoReply
+ hello bot
- Hello
`);
await bot.init();

it('should load directive /plugin: testNoReply', async () => {
assert.isTrue(bot.context.directives.has('plugin:testNoReply'), 'contains directive /plugin');
});

it('should execute plugin and get value', async () => {
const today = new Date().getDate();
const req = new Request();
// ask bot with data output format
const res1 = await bot.handleAsync(req.enter('hello bot'));
assert.match(res1.speechResponse, /Hello Human!/i, 'bot response');
assert.equal(res1.variables.today, today, 'today value number');
// no reply
const res2 = await bot.handleAsync(req.enter('something great'));
assert.match(res2.speechResponse, /I dont know!/i, 'bot response');
assert.equal(res2.variables.today, today, 'today value number');

});
});

});

0 comments on commit 7654e30

Please sign in to comment.