UNPKG

4.25 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4/**
5 * Copyright (c) Microsoft Corporation. All rights reserved.
6 * Licensed under the MIT License.
7 */
8const Utils_1 = require("./Utils");
9const CLM = require("@conversationlearner/models");
10var DebugType;
11(function (DebugType) {
12 DebugType[DebugType["Client"] = 1] = "Client";
13 DebugType[DebugType["ClientBody"] = 2] = "ClientBody";
14 DebugType[DebugType["MessageQueue"] = 4] = "MessageQueue";
15 DebugType[DebugType["Memory"] = 8] = "Memory";
16 DebugType[DebugType["MemVerbose"] = 16] = "MemVerbose";
17})(DebugType = exports.DebugType || (exports.DebugType = {}));
18var LogType;
19(function (LogType) {
20 LogType[LogType["Log"] = 0] = "Log";
21 LogType[LogType["Error"] = 1] = "Error";
22})(LogType || (LogType = {}));
23class CLDebug {
24 static InitLogger(adapter, conversationReference) {
25 CLDebug.adapter = adapter;
26 CLDebug.conversationReference = conversationReference;
27 }
28 static HasDebugType(debugType) {
29 return (debugType & this.debugType) === debugType;
30 }
31 static SendCache() {
32 return tslib_1.__awaiter(this, void 0, void 0, function* () {
33 if (CLDebug.adapter && CLDebug.cachedMessages.length > 0) {
34 //TODO: Only send when running in UI
35 yield CLDebug.adapter.continueConversation(CLDebug.conversationReference, (context) => tslib_1.__awaiter(this, void 0, void 0, function* () {
36 let cachedMessages = [...this.cachedMessages];
37 this.cachedMessages = [];
38 for (let logMessage of cachedMessages) {
39 if (logMessage.logType === LogType.Error) {
40 // TODO: Create CLM.SenderType.Error to special handle this when clicked
41 const clData = { replayError: new CLM.ReplayErrorException(), senderType: CLM.SenderType.Bot, roundIndex: null, scoreIndex: null };
42 yield context.sendActivity({ text: logMessage.message, channelData: { clData: clData } });
43 }
44 else {
45 yield context.sendActivity(logMessage.message);
46 }
47 }
48 }));
49 }
50 });
51 }
52 static Log(text, filter) {
53 if (!filter || this.HasDebugType(filter)) {
54 console.log(text);
55 if (CLDebug.logToUI) {
56 CLDebug.cachedMessages.push({ message: text, logType: LogType.Log });
57 }
58 CLDebug.SendCache();
59 }
60 }
61 static LogRequest(method, path, payload) {
62 if (this.HasDebugType(DebugType.Client)) {
63 // Ignore training status messages
64 if (path.indexOf('trainingstatus') > 0) {
65 return;
66 }
67 let message = `${method} ${path}`;
68 if (this.HasDebugType(DebugType.ClientBody)) {
69 const formattedBody = payload.body ? JSON.stringify(payload.body, null, ' ') : '';
70 if (formattedBody.length > 0) {
71 message = `${message}\n\n${formattedBody}`;
72 }
73 }
74 console.log(message);
75 if (CLDebug.logToUI) {
76 CLDebug.cachedMessages.push({ message: message, logType: LogType.Log });
77 }
78 CLDebug.SendCache();
79 }
80 }
81 static Error(error, context = "", sendAsChat = true) {
82 let text = `ERROR: ${error ? Utils_1.Utils.ErrorString(error, context) : 'No details'}`;
83 console.log(text);
84 if (sendAsChat) {
85 CLDebug.cachedMessages.push({ message: text, logType: LogType.Error });
86 CLDebug.SendCache();
87 }
88 return text;
89 }
90 static Verbose(text) {
91 if (CLDebug.verbose) {
92 CLDebug.Log(`${text}`);
93 }
94 }
95 static LogObject(obj) {
96 CLDebug.Log(JSON.stringify(obj));
97 }
98}
99CLDebug.cachedMessages = [];
100CLDebug.logToUI = false; // If set all log messages displayed in chat UI, if false only error messages
101CLDebug.verbose = true;
102CLDebug.debugType = 0;
103exports.CLDebug = CLDebug;
104//# sourceMappingURL=CLDebug.js.map
\No newline at end of file