UNPKG

node-nlp

Version:

Library for NLU (Natural Language Understanding) done in Node.js

149 lines 5.51 kB
"use strict"; /** * @module botbuilder */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const botframework_schema_1 = require("botframework-schema"); const turnContext_1 = require("./turnContext"); /** * Logs incoming and outgoing activities to a TranscriptStore. */ class TranscriptLoggerMiddleware { /** * Middleware for logging incoming and outgoing activities to a transcript store. * @param logger Transcript logger */ constructor(logger) { this.transcript = []; if (!logger) { throw new Error('TranscriptLoggerMiddleware requires a TranscriptLogger instance.'); } this.logger = logger; } /** * Initialization for middleware turn. * @param context Context for the current turn of conversation with the user. * @param next Function to call at the end of the middleware chain. */ onTurn(context, next) { return __awaiter(this, void 0, void 0, function* () { // log incoming activity at beginning of turn if (context.activity) { if (!context.activity.from.role) { context.activity.from.role = 'user'; } this.logActivity(this.cloneActivity(context.activity)); } // hook up onSend pipeline context.onSendActivities((ctx, activities, next2) => __awaiter(this, void 0, void 0, function* () { // run full pipeline const responses = yield next2(); activities.forEach((a) => this.logActivity(this.cloneActivity(a))); return responses; })); // hook up update activity pipeline context.onUpdateActivity((ctx, activity, next3) => __awaiter(this, void 0, void 0, function* () { // run full pipeline const response = yield next3(); // add Message Update activity const updateActivity = this.cloneActivity(activity); updateActivity.type = botframework_schema_1.ActivityTypes.MessageUpdate; this.logActivity(updateActivity); return response; })); // hook up delete activity pipeline context.onDeleteActivity((ctx, reference, next4) => __awaiter(this, void 0, void 0, function* () { // run full pipeline yield next4(); // add MessageDelete activity // log as MessageDelete activity const deleteActivity = turnContext_1.TurnContext.applyConversationReference({ type: botframework_schema_1.ActivityTypes.MessageDelete, id: reference.activityId }, reference, false); this.logActivity(deleteActivity); })); // process bot logic yield next(); // flush transcript at end of turn while (this.transcript.length > 0) { try { const activity = this.transcript.shift(); this.logger.logActivity(activity); } catch (err) { // tslint:disable-next-line:no-console console.error('TranscriptLoggerMiddleware logActivity failed', err); } } }); } /** * Logs the Activity. * @param activity Activity to log. */ logActivity(activity) { if (!activity.timestamp) { activity.timestamp = new Date(); } this.transcript.push(activity); } /** * Clones the Activity entity. * @param activity Activity to clone. */ cloneActivity(activity) { return Object.assign({}, activity); } } exports.TranscriptLoggerMiddleware = TranscriptLoggerMiddleware; /** * ConsoleTranscriptLogger , writes activities to Console output. */ class ConsoleTranscriptLogger { /** * Log an activity to the transcript. * @param activity Activity being logged. */ logActivity(activity) { if (!activity) { throw new Error('Activity is required.'); } // tslint:disable-next-line:no-console console.log('Activity Log:', activity); } } exports.ConsoleTranscriptLogger = ConsoleTranscriptLogger; /** * Metadata for a stored transcript. */ class TranscriptInfo { } exports.TranscriptInfo = TranscriptInfo; /** * Page of results. * @param T type of items being paged in. */ // tslint:disable-next-line:max-classes-per-file class PagedResult { constructor() { /** * Page of items. */ this.items = []; } } exports.PagedResult = PagedResult; //# sourceMappingURL=transcriptLogger.js.map