UNPKG

@microsoft/botbuilder-m365

Version:

M365 extensions for Microsoft BotBuilder, Alpha release.

119 lines 5.87 kB
"use strict"; /** * @module botbuilder-m365 */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromptParser = void 0; const promises_1 = require("fs/promises"); const ConversationHistory_1 = require("./ConversationHistory"); var PromptParseState; (function (PromptParseState) { PromptParseState[PromptParseState["inText"] = 0] = "inText"; PromptParseState[PromptParseState["inVariable"] = 1] = "inVariable"; })(PromptParseState || (PromptParseState = {})); class PromptParser { static expandPromptTemplate(context, state, prompt, options) { return __awaiter(this, void 0, void 0, function* () { // Get template let promptTemplate; if (typeof prompt == 'function') { promptTemplate = yield prompt(context, state); } else if (promptFileCache.has(prompt)) { promptTemplate = promptFileCache.get(prompt); } else { promptTemplate = yield (0, promises_1.readFile)(prompt, { encoding: 'utf8' }); promptFileCache.set(prompt, promptTemplate); } // Expand template let variableName; let parseState = PromptParseState.inText; let outputPrompt = ''; for (let i = 0; i < promptTemplate.length; i++) { const ch = promptTemplate[i]; switch (parseState) { case PromptParseState.inText: default: if (ch == '{' && i + 1 < promptTemplate.length && promptTemplate[i + 1] == '{') { // Skip next character and change parse state i++; variableName = ''; parseState = PromptParseState.inVariable; } else { // Append character to output outputPrompt += ch; } break; case PromptParseState.inVariable: if (ch == '}') { // Skip next character and change state if (i + 1 < promptTemplate.length && promptTemplate[i + 1] == '}') { i++; parseState = PromptParseState.inText; } // Append variable contents to output outputPrompt += PromptParser.lookupPromptVariable(context, state, variableName, options); } else { // Append character to variable name variableName += ch; } break; } } return outputPrompt; }); } static lookupPromptVariable(context, state, variableName, options) { var _a, _b, _c, _d; // Split variable name into parts and validate // TODO: Add support for longer dotted path variable names const parts = variableName.trim().split('.'); if (parts.length != 2) { throw new Error(`PromptParser: invalid variable name of "${variableName}" specified`); } // Check for special cased variables first let value; switch (parts[0]) { case 'activity': // Return activity field value = (_a = context.activity[parts[1]]) !== null && _a !== void 0 ? _a : ''; break; default: // Find referenced state entry const entry = state[parts[0]]; if (!entry) { throw new Error(`PromptParser: invalid variable name of "${variableName}" specified. Couldn't find a state named "${parts[0]}".`); } // Special case `conversation.history` reference if (parts[0] == 'conversation' && parts[1] == 'history') { value = ConversationHistory_1.ConversationHistory.toString(state, (_b = options === null || options === void 0 ? void 0 : options.conversationHistory) === null || _b === void 0 ? void 0 : _b.maxCharacterLength, (_c = options === null || options === void 0 ? void 0 : options.conversationHistory) === null || _c === void 0 ? void 0 : _c.lineSeparator); } else { // Return state field value = (_d = entry.value[parts[1]]) !== null && _d !== void 0 ? _d : ''; } break; } // Return value return typeof value == 'object' || Array.isArray(value) ? JSON.stringify(value) : value.toString(); } } exports.PromptParser = PromptParser; const promptFileCache = new Map(); //# sourceMappingURL=PromptParser.js.map