UNPKG

3.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const CLRunner_1 = require("./CLRunner");
5const CLMemory_1 = require("./CLMemory");
6const CLDebug_1 = require("./CLDebug");
7const CLClient_1 = require("./CLClient");
8const router_1 = require("./http/router");
9const Utils_1 = require("./Utils");
10class ConversationLearner {
11 constructor(modelId, maxTimeout) {
12 if (!ConversationLearner.options) {
13 throw new Error("Init() must be called on ConversationLearner before instances are created");
14 }
15 if (typeof maxTimeout !== 'number') {
16 maxTimeout = Utils_1.DEFAULT_MAX_SESSION_LENGTH;
17 }
18 this.clRunner = CLRunner_1.CLRunner.Create(modelId, maxTimeout, ConversationLearner.clClient);
19 }
20 static Init(options, storage = null) {
21 ConversationLearner.options = options;
22 try {
23 this.clClient = new CLClient_1.CLClient(options);
24 CLMemory_1.CLMemory.Init(storage);
25 }
26 catch (error) {
27 CLDebug_1.CLDebug.Error(error, 'Conversation Learner Initialization');
28 }
29 return router_1.default(this.clClient, options);
30 }
31 recognize(turnContext, force) {
32 return tslib_1.__awaiter(this, void 0, void 0, function* () {
33 return yield this.clRunner.recognize(turnContext, force);
34 });
35 }
36 /**
37 * OPTIONAL: Sessions are started automatically, StartSession call is only needed if bot needs
38 * to start Conversation Learner Session with initial entity values.
39 * Results in clearing of existing Entity values, and a call to the OnSessionStartCallback
40 * @param turnContext BotBuilder Context
41 */
42 StartSession(turnContext) {
43 return tslib_1.__awaiter(this, void 0, void 0, function* () {
44 yield this.clRunner.BotStartSession(turnContext);
45 });
46 }
47 /**
48 * Provide an callback that will be invoked whenever a Session is started
49 * @param target Callback of the form (context: BB.TurnContext, memoryManager: ClientMemoryManager) => Promise<void>
50 */
51 OnSessionStartCallback(target) {
52 this.clRunner.onSessionStartCallback = target;
53 }
54 /**
55 * Provide a callback that will be invoked whenever a Session ends. Sessions
56 * can end because of a timeout or the selection of an EndSession activity
57 * @param target Callback of the form (context: BB.TurnContext, memoryManager: ClientMemoryManager, sessionEndState: CLM.SessionEndState, data: string | undefined) => Promise<string[] | undefined>
58 */
59 OnSessionEndCallback(target) {
60 this.clRunner.onSessionEndCallback = target;
61 }
62 SendResult(result) {
63 return tslib_1.__awaiter(this, void 0, void 0, function* () {
64 yield this.clRunner.SendIntent(result);
65 });
66 }
67 /** Returns true is bot is running in the Training UI
68 * @param turnContext BotBuilder Context
69 */
70 InTrainingUI(turnContext) {
71 return tslib_1.__awaiter(this, void 0, void 0, function* () {
72 // TODO: This always returns false for onConversationUpdate as 'from' is not set
73 return yield this.clRunner.InTrainingUI(turnContext);
74 });
75 }
76 /**
77 * Define an API callback that can be used by the Model
78 * @param callback Object with callback name, optional logic function, and optional render function.
79 */
80 AddCallback(callback) {
81 this.clRunner.AddCallback(callback);
82 }
83 /** Define an Callback that will be called after Entity Detection
84 * @param target Callback of the form (text: string, memoryManager: ClientMemoryManager) => Promise<void>
85 */
86 EntityDetectionCallback(target) {
87 this.clRunner.entityDetectionCallback = target;
88 }
89}
90ConversationLearner.options = null;
91exports.ConversationLearner = ConversationLearner;
92//# sourceMappingURL=ConversationLearner.js.map
\No newline at end of file