@microsoft/botbuilder-m365
Version:
M365 extensions for Microsoft BotBuilder, Alpha release.
96 lines • 4.16 kB
JavaScript
;
/**
* @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.CodeBlock = void 0;
const Block_1 = require("./Block");
const VarBlock_1 = require("./VarBlock");
/**
* @private
*/
class CodeBlock extends Block_1.Block {
constructor(content) {
super();
this._validated = false;
this.content = content;
}
get type() {
return Block_1.BlockTypes.Code;
}
isValid() {
let valid = true;
let errorMessage;
const partsToValidate = this.content.split(/[ \t\r\n]+/).filter((x) => x.trim() !== '');
for (let index = 0; index < partsToValidate.length; index++) {
// TODO:
// eslint-disable-next-line security/detect-object-injection
const part = partsToValidate[index];
if (index === 0) {
// There is only a function name
if (VarBlock_1.VarBlock.hasVarPrefix(part)) {
errorMessage = `Variables cannot be used as function names [\`${part}\`]`;
valid = false;
}
if (!/^[a-zA-Z0-9_.]*$/.test(part)) {
errorMessage = `The function name \`${part}\` contains invalid characters`;
valid = false;
}
}
else {
// The function has parameters
if (!VarBlock_1.VarBlock.hasVarPrefix(part)) {
errorMessage = `\`${part}\` is not a valid function parameter: parameters must be variables.`;
valid = false;
}
if (part.length < 2) {
errorMessage = `\`${part}\` is not a valid variable.`;
valid = false;
}
if (!VarBlock_1.VarBlock.isValidVarName(part.substring(1))) {
errorMessage = `\`${part}\` variable name is not valid.`;
valid = false;
}
}
}
this._validated = true;
return { valid, error: errorMessage };
}
render(context, state) {
throw new Error('Code blocks rendering requires IFunctionRegistryReader. Incorrect method call.');
}
renderCode(context, state, promptManager) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
if (!this._validated) {
const { valid, error } = this.isValid();
if (!valid) {
throw new Error(error);
}
}
const parts = this.content.split(/[ \t\r\n]+/).filter((x) => x.trim() !== '');
const functionName = parts[0];
const result = yield promptManager.invokeFunction(context, state, functionName);
const output = VarBlock_1.VarBlock.formatValue(result);
// Save output to $temp.output and then return
const temp = (_c = (_b = (_a = state) === null || _a === void 0 ? void 0 : _a.temp) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : {};
temp.output = output;
return output;
});
}
}
exports.CodeBlock = CodeBlock;
//# sourceMappingURL=CodeBlock.js.map