1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const CLDebug_1 = require("./CLDebug");
|
5 | const Request = require("request");
|
6 | const constants = require("./constants");
|
7 | const requestMethodMap = new Map([
|
8 | ['GET', Request.get],
|
9 | ['PUT', Request.put],
|
10 | ['POST', Request.post],
|
11 | ['DELETE', Request.delete]
|
12 | ]);
|
13 | class CLClient {
|
14 | constructor(options) {
|
15 | this.options = options;
|
16 | if (options.APIM_SUBSCRIPTION_KEY === undefined) {
|
17 | options.APIM_SUBSCRIPTION_KEY = options.LUIS_AUTHORING_KEY;
|
18 | }
|
19 | }
|
20 | ValidationError() {
|
21 | if (typeof this.options.CONVERSATION_LEARNER_SERVICE_URI !== 'string' || this.options.CONVERSATION_LEARNER_SERVICE_URI.length === 0) {
|
22 | return `CONVERSATION_LEARNER_SERVICE_URI must be a non-empty string. You passed: ${this.options.CONVERSATION_LEARNER_SERVICE_URI}`;
|
23 | }
|
24 | if (typeof this.options.LUIS_AUTHORING_KEY !== 'string' || this.options.LUIS_AUTHORING_KEY.length === 0) {
|
25 | return `LUIS_AUTHORING_KEY must be a non-empty string. You passed: ${this.options.LUIS_AUTHORING_KEY}`;
|
26 | }
|
27 | return null;
|
28 | }
|
29 | LuisAuthoringKey() {
|
30 | return this.options.LUIS_AUTHORING_KEY;
|
31 | }
|
32 | BuildURL(baseUri, apiPath, query) {
|
33 | let uri = baseUri + (!baseUri.endsWith('/') ? '/' : '') + apiPath;
|
34 | if (query) {
|
35 | uri += `?${query}`;
|
36 | }
|
37 | return uri;
|
38 | }
|
39 | MakeURL(apiPath, query) {
|
40 | return this.BuildURL(this.options.CONVERSATION_LEARNER_SERVICE_URI, apiPath, query);
|
41 | }
|
42 | MakeSessionURL(apiPath, query) {
|
43 |
|
44 | if (!this.options.CONVERSATION_LEARNER_SERVICE_URI.includes('api.cognitive.microsoft.com')) {
|
45 |
|
46 |
|
47 | return this.MakeURL(apiPath, query);
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | let baseUri = this.options.CONVERSATION_LEARNER_SERVICE_URI.endsWith('/') ?
|
56 | this.options.CONVERSATION_LEARNER_SERVICE_URI :
|
57 | `${this.options.CONVERSATION_LEARNER_SERVICE_URI}/`;
|
58 | const apimVersionSuffix = '/v1.0/';
|
59 | if (baseUri.endsWith(apimVersionSuffix)) {
|
60 |
|
61 |
|
62 |
|
63 | baseUri = `${baseUri.substring(0, baseUri.lastIndexOf(apimVersionSuffix))}/session${apimVersionSuffix}`;
|
64 | }
|
65 | else {
|
66 |
|
67 |
|
68 | baseUri += 'session/';
|
69 | }
|
70 | return this.BuildURL(baseUri, apiPath, query);
|
71 | }
|
72 | send(method, url, body) {
|
73 | return new Promise((resolve, reject) => {
|
74 | const requestData = {
|
75 | url,
|
76 | headers: {
|
77 | [constants.luisAuthoringKeyHeader]: this.options.LUIS_AUTHORING_KEY,
|
78 | [constants.luisSubscriptionKeyHeader]: this.options.LUIS_SUBSCRIPTION_KEY,
|
79 |
|
80 | [constants.apimSubscriptionIdHeader]: this.options.LUIS_AUTHORING_KEY,
|
81 | [constants.apimSubscriptionKeyHeader]: this.options.APIM_SUBSCRIPTION_KEY
|
82 | },
|
83 | json: true,
|
84 | body
|
85 | };
|
86 | CLDebug_1.CLDebug.LogRequest(method, url, requestData);
|
87 | const requestMethod = requestMethodMap.get(method);
|
88 | if (!requestMethod) {
|
89 | throw new Error(`Request method not found for http verb: ${method}`);
|
90 | }
|
91 | requestMethod(requestData, (error, response, responseBody) => {
|
92 | if (error) {
|
93 | reject(error);
|
94 | }
|
95 | else if (response.statusCode && response.statusCode >= 300) {
|
96 | reject(response);
|
97 | }
|
98 | else {
|
99 | resolve(responseBody);
|
100 | }
|
101 | });
|
102 | });
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 | GetApp(appId) {
|
113 | let apiPath = `app/${appId}`;
|
114 | return this.send('GET', this.MakeURL(apiPath));
|
115 | }
|
116 | GetAppSource(appId, packageId) {
|
117 | let apiPath = `app/${appId}/source?package=${packageId}`;
|
118 | return this.send('GET', this.MakeURL(apiPath));
|
119 | }
|
120 | PostAppSource(appId, appDefinition) {
|
121 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
122 | let apiPath = `app/${appId}/source`;
|
123 | yield this.send('POST', this.MakeURL(apiPath), appDefinition);
|
124 | });
|
125 | }
|
126 |
|
127 | GetApps(query) {
|
128 | let apiPath = `apps`;
|
129 | return this.send('GET', this.MakeURL(apiPath, query));
|
130 | }
|
131 |
|
132 | CopyApps(srcUserId, destUserId, appId, luisSubscriptionKey) {
|
133 | const apiPath = `apps/copy?srcUserId=${srcUserId}&destUserId=${destUserId}&appId=${appId}&luisSubscriptionKey=${luisSubscriptionKey}`;
|
134 | return this.send('POST', this.MakeURL(apiPath));
|
135 | }
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | ArchiveApp(appId) {
|
144 | let apiPath = `app/${appId}`;
|
145 | return this.send('DELETE', this.MakeURL(apiPath));
|
146 | }
|
147 | |
148 |
|
149 |
|
150 |
|
151 | AddApp(app, query) {
|
152 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
153 | const apiPath = `app`;
|
154 |
|
155 | const appResponse = yield this.send('POST', this.MakeURL(apiPath, query), app);
|
156 | return appResponse.appId;
|
157 | });
|
158 | }
|
159 |
|
160 | PublishApp(appId, tagName) {
|
161 | let apiPath = `app/${appId}/publish?version=${tagName}`;
|
162 | return this.send('PUT', this.MakeURL(apiPath));
|
163 | }
|
164 |
|
165 | PublishProdPackage(appId, packageId) {
|
166 | let apiPath = `app/${appId}/publish/${packageId}`;
|
167 | return this.send('POST', this.MakeURL(apiPath));
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 | GetEntities(appId, query) {
|
178 | let apiPath = `app/${appId}/entities`;
|
179 | return this.send('GET', this.MakeURL(apiPath, query));
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 | GetLogDialogs(appId, packageIds) {
|
190 | const packages = packageIds.map(p => `package=${p}`).join("&");
|
191 | const apiPath = `app/${appId}/logdialogs?includeDefinitions=false&${packages}`;
|
192 | return this.send('GET', this.MakeURL(apiPath));
|
193 | }
|
194 |
|
195 | LogDialogExtract(appId, logDialogId, turnIndex, userInput) {
|
196 | let apiPath = `app/${appId}/logdialog/${logDialogId}/extractor/${turnIndex}`;
|
197 |
|
198 | let query = 'includeDefinitions=true';
|
199 | return this.send('PUT', this.MakeURL(apiPath, query), userInput);
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 | |
205 |
|
206 |
|
207 |
|
208 | GetTrainDialog(appId, trainDialogId, includeDefinitions = false) {
|
209 | let query = `includeDefinitions=${includeDefinitions}`;
|
210 | let apiPath = `app/${appId}/traindialog/${trainDialogId}`;
|
211 | return this.send('GET', this.MakeURL(apiPath, query));
|
212 | }
|
213 |
|
214 | TrainDialogExtract(appId, trainDialogId, turnIndex, userInput) {
|
215 | let apiPath = `app/${appId}/traindialog/${trainDialogId}/extractor/${turnIndex}`;
|
216 |
|
217 | let query = 'includeDefinitions=true';
|
218 | return this.send('PUT', this.MakeURL(apiPath, query), userInput);
|
219 | }
|
220 | |
221 |
|
222 |
|
223 |
|
224 | TrainDialogValidateTextVariation(appId, trainDialogId, textVariation, excludeConflictCheckId) {
|
225 | let apiPath = `app/${appId}/traindialog/${trainDialogId}/extractor/textvariation`;
|
226 |
|
227 | let query = excludeConflictCheckId ? `filteredDialogs=${excludeConflictCheckId}` : undefined;
|
228 | return this.send('POST', this.MakeURL(apiPath, query), textVariation);
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | StartSession(appId, sessionCreateParams) {
|
235 | let apiPath = `app/${appId}/session`;
|
236 | return this.send('POST', this.MakeSessionURL(apiPath), sessionCreateParams);
|
237 | }
|
238 |
|
239 |
|
240 | GetSession(appId, sessionId) {
|
241 | let apiPath = `app/${appId}/session/${sessionId}`;
|
242 | return this.send('GET', this.MakeURL(apiPath));
|
243 | }
|
244 |
|
245 | SessionExtract(appId, sessionId, userInput) {
|
246 | let apiPath = `app/${appId}/session/${sessionId}/extractor`;
|
247 |
|
248 | let query = 'includeDefinitions=true';
|
249 | return this.send('PUT', this.MakeSessionURL(apiPath, query), userInput);
|
250 | }
|
251 |
|
252 | SessionScore(appId, sessionId, scorerInput) {
|
253 | let apiPath = `app/${appId}/session/${sessionId}/scorer`;
|
254 | return this.send('PUT', this.MakeSessionURL(apiPath), scorerInput);
|
255 | }
|
256 | SessionLogicResult(appId, sessionId, actionId, actionResult) {
|
257 | let apiPath = `app/${appId}/session/${sessionId}/scorerSteps/action/${actionId}/logicResult`;
|
258 | return this.send('PUT', this.MakeSessionURL(apiPath), { logicResult: actionResult.logicResult });
|
259 | }
|
260 |
|
261 | EndSession(appId, sessionId) {
|
262 | let apiPath = `app/${appId}/session/${sessionId}`;
|
263 |
|
264 | let query = 'saveDialog=false';
|
265 | return this.send('DELETE', this.MakeSessionURL(apiPath, query));
|
266 | }
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | StartTeach(appId, createTeachParams) {
|
272 | let apiPath = `app/${appId}/teach`;
|
273 | return this.send('POST', this.MakeURL(apiPath), createTeachParams);
|
274 | }
|
275 | |
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | TeachExtract(appId, teachId, userInput, excludeConflictCheckId) {
|
282 | let apiPath = `app/${appId}/teach/${teachId}/extractor`;
|
283 |
|
284 | let query = `includeDefinitions=true`;
|
285 | if (excludeConflictCheckId) {
|
286 | query += `&filteredDialogs=${excludeConflictCheckId}`;
|
287 | }
|
288 | return this.send('PUT', this.MakeURL(apiPath, query), { text: userInput.text });
|
289 | }
|
290 | |
291 |
|
292 |
|
293 |
|
294 |
|
295 | TeachExtractFeedback(appId, teachId, extractorStep) {
|
296 | let apiPath = `app/${appId}/teach/${teachId}/extractor`;
|
297 | return this.send('POST', this.MakeURL(apiPath), extractorStep);
|
298 | }
|
299 | |
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | TeachScore(appId, teachId, scorerInput) {
|
306 | let apiPath = `app/${appId}/teach/${teachId}/scorer`;
|
307 | return this.send('PUT', this.MakeURL(apiPath), scorerInput);
|
308 | }
|
309 | |
310 |
|
311 |
|
312 |
|
313 |
|
314 | TeachScoreFeedback(appId, teachId, scorerResponse) {
|
315 | let apiPath = `app/${appId}/teach/${teachId}/scorer`;
|
316 | return this.send('POST', this.MakeURL(apiPath), scorerResponse);
|
317 | }
|
318 | |
319 |
|
320 |
|
321 |
|
322 |
|
323 | EndTeach(appId, teachId, save) {
|
324 | const query = `saveDialog=${save}`;
|
325 | let apiPath = `app/${appId}/teach/${teachId}`;
|
326 | return this.send('DELETE', this.MakeURL(apiPath, query));
|
327 | }
|
328 | }
|
329 | exports.CLClient = CLClient;
|
330 |
|
\ | No newline at end of file |