Skip to content

Commit 519d6be

Browse files
authored
Merge pull request #64 from yeuai/dev-response
Dev response
2 parents 5e82d71 + ae4cffd commit 519d6be

16 files changed

Lines changed: 471 additions & 158 deletions

File tree

.mocharc.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require: ts-node/register
22
reporter: spec
33
delay: true
4-
retries: 3
4+
retries: 1
55
exit: true
66
spec:
7+
- test/e2e/delay.spec.ts
78
- test/**/*.spec.ts
9+
# - test/engine/conditions.spec.ts
10+
# - test/engine/conditions.spec.ts
11+
# - test/dialogue/sorted.spec.ts

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yeuai/botscript",
3-
"version": "1.7.0",
3+
"version": "1.7.1",
44
"description": "A text-based scripting language and bot engine for Conversational User Interfaces (CUI)",
55
"main": "dist/engine",
66
"scripts": {

src/engine/botscript.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,6 @@ export class BotScript extends EventEmitter {
9595
return vResult;
9696
}
9797

98-
/**
99-
* Get struct type
100-
* @param type type
101-
*/
102-
private type(type: string): Map<string, any> {
103-
switch (type) {
104-
case 'definition':
105-
return this.context.definitions;
106-
case 'dialogue':
107-
return this.context.dialogues;
108-
case 'flows':
109-
return this.context.flows;
110-
case 'command':
111-
return this.context.commands;
112-
case 'plugin':
113-
return this.context.plugins;
114-
case 'directive':
115-
return this.context.directives;
116-
default:
117-
throw new Error('Not found type: ' + type);
118-
}
119-
}
120-
12198
/**
12299
* Script structure parser
123100
* @param content
@@ -162,8 +139,8 @@ export class BotScript extends EventEmitter {
162139

163140
scripts.forEach(data => {
164141
const struct = Struct.parse(data);
165-
// append bot data struct
166-
this.type(struct.type).set(struct.name, struct);
142+
// add bot context
143+
this.context.add(struct);
167144
});
168145

169146
return this;
@@ -222,6 +199,9 @@ export class BotScript extends EventEmitter {
222199
});
223200
}
224201
}
202+
203+
// sort triggers
204+
this.context.sortTriggers();
225205
this.logger.info('Ready!');
226206
this.emit('ready');
227207
return this;

src/engine/context.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Request } from './request';
44
import { Struct } from './struct';
55
import { IActivator } from '../interfaces/activator';
66
import { Logger } from '../lib/logger';
7+
import { Trigger } from './trigger';
78

89
const logger = new Logger('Context');
910

@@ -28,6 +29,8 @@ export class Context {
2829
*/
2930
idctx: string;
3031

32+
private _sorted_triggers: Trigger[];
33+
3134
constructor() {
3235
this.definitions = new Map();
3336
this.dialogues = new Map();
@@ -37,6 +40,7 @@ export class Context {
3740
this.plugins = new Map();
3841
this.directives = new Map();
3942
this.idctx = newid();
43+
this._sorted_triggers = [];
4044
}
4145

4246
/**
@@ -48,6 +52,64 @@ export class Context {
4852
: this.idctx;
4953
}
5054

55+
/**
56+
* Get all of context triggers
57+
*/
58+
get triggers(): Trigger[] {
59+
if (this._sorted_triggers?.length > 0) {
60+
return this._sorted_triggers;
61+
} else {
62+
this.sortTriggers();
63+
return this._sorted_triggers;
64+
}
65+
}
66+
67+
/**
68+
* Get struct type
69+
* @param type type
70+
*/
71+
private type(type: string): Map<string, Struct> {
72+
switch (type) {
73+
case 'definition':
74+
return this.definitions;
75+
case 'dialogue':
76+
return this.dialogues;
77+
case 'flows':
78+
return this.flows;
79+
case 'command':
80+
return this.commands;
81+
case 'plugin':
82+
return this.plugins;
83+
case 'directive':
84+
return this.directives;
85+
default:
86+
throw new Error('Not found type: ' + type);
87+
}
88+
}
89+
90+
/**
91+
* Add context struct
92+
* @param struct
93+
*/
94+
add(struct: Struct) {
95+
this.type(struct.type).set(struct.name, struct);
96+
return this;
97+
}
98+
99+
/**
100+
* sort trigger
101+
*/
102+
sortTriggers(): void {
103+
const vTriggers: Trigger[] = [];
104+
this._sorted_triggers = [];
105+
Array.from(this.dialogues.values())
106+
.forEach(x => {
107+
vTriggers.push(...x.triggers.map(t => new Trigger(t, x.name)));
108+
});
109+
// sort & cache triggers.
110+
this._sorted_triggers = vTriggers.sort(Trigger.sorter);
111+
}
112+
51113
/**
52114
* Get dialogue by name
53115
* Notice: a flow is a dialogue

0 commit comments

Comments
 (0)