2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
2024-01-09 17:40:48 -03:00
|
|
|
| █████ █████ ██ █ █████ █████ ████ ██ ████ █████ █████ ███ ® |
|
|
|
|
| ██ █ ███ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| ██ ███ ████ █ ██ █ ████ █████ ██████ ██ ████ █ █ █ ██ |
|
|
|
|
| ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ |
|
2018-04-21 02:59:30 -03:00
|
|
|
| |
|
2024-01-10 15:01:02 -03:00
|
|
|
| General Bots Copyright (c) pragmatismo.com.br. All rights reserved. |
|
2018-04-21 02:59:30 -03:00
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2018-09-11 19:40:53 -03:00
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
2024-01-10 14:52:01 -03:00
|
|
|
| "General Bots" is a registered trademark of pragmatismo.com.br. |
|
2018-04-21 02:59:30 -03:00
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2022-11-18 22:39:14 -03:00
|
|
|
import Path from 'path';
|
|
|
|
import express from 'express';
|
|
|
|
import child_process from 'child_process';
|
|
|
|
import rimraf from 'rimraf';
|
|
|
|
import urlJoin from 'url-join';
|
|
|
|
import Fs from 'fs';
|
2020-12-31 15:36:19 -03:00
|
|
|
import { GBError, GBLog, GBMinInstance, IGBCoreService, IGBDeployer, IGBInstance, IGBPackage } from 'botlib';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { AzureSearch } from 'pragmatismo-io-framework';
|
2020-12-31 15:36:19 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBServer } from '../../../src/app.js';
|
|
|
|
import { GBVMService } from '../../basic.gblib/services/GBVMService.js';
|
|
|
|
import { GuaribasPackage } from '../models/GBModel.js';
|
|
|
|
import { GBAdminService } from './../../admin.gbapp/services/GBAdminService.js';
|
|
|
|
import { AzureDeployerService } from './../../azuredeployer.gbapp/services/AzureDeployerService.js';
|
|
|
|
import { KBService } from './../../kb.gbapp/services/KBService.js';
|
|
|
|
import { GBConfigService } from './GBConfigService.js';
|
|
|
|
import { GBImporter } from './GBImporterService.js';
|
|
|
|
import { TeamsService } from '../../teams.gblib/services/TeamsService.js';
|
|
|
|
import MicrosoftGraph from '@microsoft/microsoft-graph-client';
|
2023-02-26 06:05:57 -03:00
|
|
|
import { GBLogEx } from './GBLogEx.js';
|
2023-03-09 17:46:34 -03:00
|
|
|
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
|
2024-01-10 15:01:02 -03:00
|
|
|
import { GBUtil } from '../../../src/util.js';
|
2024-01-30 19:21:04 -03:00
|
|
|
import { HNSWLib } from 'langchain/vectorstores/hnswlib';
|
2024-01-29 23:03:56 -03:00
|
|
|
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';
|
2020-04-01 15:42:57 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
/**
|
2020-12-31 15:36:19 -03:00
|
|
|
* Deployer service for bots, themes, ai and more.
|
2019-01-31 11:32:33 -02:00
|
|
|
*/
|
2020-04-02 19:03:57 -03:00
|
|
|
export class GBDeployer implements IGBDeployer {
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Where should deployer look into for general packages.
|
|
|
|
*/
|
2018-11-12 12:20:44 -02:00
|
|
|
public static deployFolder = 'packages';
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The work folder used to download artifacts from bot storage.
|
|
|
|
*/
|
2019-08-22 01:54:30 +00:00
|
|
|
public static workFolder = 'work';
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the core service.
|
|
|
|
*/
|
2018-11-12 12:20:44 -02:00
|
|
|
public core: IGBCoreService;
|
2020-12-26 19:47:38 -03:00
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
/**
|
2020-12-26 19:47:38 -03:00
|
|
|
* Reference to the importer service.
|
|
|
|
*/
|
2018-11-12 12:20:44 -02:00
|
|
|
public importer: GBImporter;
|
2018-09-10 12:09:48 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Deployer needs core and importer to be created.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
constructor(core: IGBCoreService, importer: GBImporter) {
|
2018-09-24 11:04:36 -03:00
|
|
|
this.core = core;
|
|
|
|
this.importer = importer;
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Builds a connection string text to be used in direct
|
|
|
|
* use to database like the Indexer (Azure Search).
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public static getConnectionStringFromInstance(instance: IGBInstance) {
|
2023-12-20 18:07:09 -03:00
|
|
|
return `Server=tcp:${GBConfigService.get("STORAGE_SERVER")},1433;Database=${GBConfigService.get("STORAGE_NAME")};User ID=${GBConfigService.get("STORAGE_USERNAME")};Password=${GBConfigService.get("STORAGE_PASSWORD")};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;`;
|
2018-11-12 12:20:44 -02:00
|
|
|
}
|
|
|
|
|
2021-03-29 18:50:27 -03:00
|
|
|
/**
|
|
|
|
* Retrives token and initialize drive client API.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public static async internalGetDriveClient(min: GBMinInstance) {
|
2023-08-25 19:09:03 -03:00
|
|
|
let token;
|
2023-09-10 13:33:20 -03:00
|
|
|
|
2023-09-03 16:22:03 -03:00
|
|
|
// Get token as root only if the bot does not have
|
|
|
|
// an custom tenant for retrieving packages.
|
|
|
|
|
2023-09-03 16:13:28 -03:00
|
|
|
token = await (min.adminService as any)['acquireElevatedToken']
|
2023-12-14 12:34:41 -03:00
|
|
|
(min.instance.instanceId, min.instance.authenticatorTenant ? false : true);
|
2021-03-29 18:50:27 -03:00
|
|
|
|
2023-08-25 19:09:03 -03:00
|
|
|
const siteId = process.env.STORAGE_SITE_ID;
|
|
|
|
const libraryId = process.env.STORAGE_LIBRARY;
|
|
|
|
|
|
|
|
const client = MicrosoftGraph.Client.init({
|
|
|
|
authProvider: done => {
|
|
|
|
done(null, token);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const baseUrl = `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}`;
|
|
|
|
min['cacheToken'] = { baseUrl, client };
|
|
|
|
|
2023-12-19 07:16:20 -03:00
|
|
|
return { baseUrl, client };
|
2023-12-14 12:34:41 -03:00
|
|
|
|
2021-03-29 18:50:27 -03:00
|
|
|
}
|
|
|
|
|
2018-09-11 19:33:58 -03:00
|
|
|
/**
|
|
|
|
* Performs package deployment in all .gbai or default.
|
2018-12-18 13:50:35 -02:00
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async deployPackages(core: IGBCoreService, server: any, appPackages: IGBPackage[]) {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Builds lists of paths to search for packages.
|
2018-12-18 13:50:35 -02:00
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
let paths = [urlJoin(process.env.PWD, GBDeployer.deployFolder), urlJoin(process.env.PWD, GBDeployer.workFolder)];
|
|
|
|
const additionalPath = GBConfigService.get('ADDITIONAL_DEPLOY_PATH');
|
|
|
|
if (additionalPath !== undefined && additionalPath !== '') {
|
2022-01-10 08:45:26 -03:00
|
|
|
paths = paths.concat(additionalPath.split(';'));
|
2020-05-15 14:07:30 -03:00
|
|
|
}
|
|
|
|
const botPackages: string[] = [];
|
|
|
|
const gbappPackages: string[] = [];
|
2020-12-31 15:36:19 -03:00
|
|
|
const generalPackages: string[] = [];
|
2020-05-15 14:07:30 -03:00
|
|
|
|
2023-01-29 12:02:14 -03:00
|
|
|
async function scanPackageDirectory(path) {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Gets all directories.
|
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
const isDirectory = source => Fs.lstatSync(source).isDirectory();
|
|
|
|
const getDirectories = source =>
|
|
|
|
Fs.readdirSync(source)
|
|
|
|
.map(name => Path.join(source, name))
|
|
|
|
.filter(isDirectory);
|
|
|
|
const dirs = getDirectories(path);
|
|
|
|
await CollectionUtil.asyncForEach(dirs, async element => {
|
2020-12-26 19:47:38 -03:00
|
|
|
// For each folder, checks its extensions looking for valid packages.
|
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
if (element === '.') {
|
|
|
|
GBLog.info(`Ignoring ${element}...`);
|
|
|
|
} else {
|
2022-01-10 08:45:26 -03:00
|
|
|
const name = Path.basename(element);
|
2020-08-15 11:39:43 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Skips what does not need to be loaded.
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
if (
|
|
|
|
process.env.GBAPP_SKIP &&
|
|
|
|
(process.env.GBAPP_SKIP.toLowerCase().indexOf(name) !== -1 || process.env.GBAPP_SKIP === 'true')
|
2020-10-18 17:46:16 -03:00
|
|
|
) {
|
2020-08-15 11:39:43 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Put it in corresponding collections.
|
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
if (element.endsWith('.gbot')) {
|
|
|
|
botPackages.push(element);
|
2020-07-18 16:38:58 -03:00
|
|
|
} else if (element.endsWith('.gbapp') || element.endsWith('.gblib')) {
|
2020-05-15 14:07:30 -03:00
|
|
|
gbappPackages.push(element);
|
|
|
|
} else {
|
|
|
|
generalPackages.push(element);
|
|
|
|
}
|
2018-11-26 14:09:09 -02:00
|
|
|
}
|
2020-05-15 14:07:30 -03:00
|
|
|
});
|
|
|
|
}
|
2018-11-26 14:09:09 -02:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Start the process of searching.
|
|
|
|
|
2023-08-13 17:15:22 -03:00
|
|
|
GBLog.info(`Deploying Application packages...`);
|
2020-05-15 14:07:30 -03:00
|
|
|
await CollectionUtil.asyncForEach(paths, async e => {
|
|
|
|
GBLog.info(`Looking in: ${e}...`);
|
|
|
|
await scanPackageDirectory(e);
|
|
|
|
});
|
2018-11-26 14:09:09 -02:00
|
|
|
|
2020-07-18 16:38:58 -03:00
|
|
|
// Deploys all .gblib files first.
|
2018-12-18 13:50:35 -02:00
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
const list = [];
|
2020-07-18 16:38:58 -03:00
|
|
|
for (let index = 0; index < gbappPackages.length; index++) {
|
|
|
|
const element = gbappPackages[index];
|
|
|
|
if (element.endsWith('.gblib')) {
|
|
|
|
list.push(element);
|
|
|
|
gbappPackages.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let index = 0; index < gbappPackages.length; index++) {
|
|
|
|
const element = gbappPackages[index];
|
|
|
|
list.push(element);
|
|
|
|
}
|
|
|
|
await this.deployAppPackages(list, core, appPackages);
|
2018-09-10 12:09:48 -03:00
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
GBLog.info(`App Package deployment done.`);
|
2018-09-10 12:09:48 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Deploys a new blank bot to the database, cognitive services and other services.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async deployBlankBot(botId: string, mobile: string, email: string) {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Creates a new row on the GuaribasInstance table.
|
2020-12-31 15:36:19 -03:00
|
|
|
const instance = await this.importer.createBotInstance(botId);
|
2020-06-14 21:40:41 -03:00
|
|
|
const bootInstance = GBServer.globals.bootInstance;
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
// Gets the access token to perform service operations.
|
2023-07-26 15:05:53 -03:00
|
|
|
|
2023-08-25 19:09:03 -03:00
|
|
|
const accessToken = await (GBServer.globals.minBoot.adminService as any)['acquireElevatedToken'](
|
|
|
|
bootInstance.instanceId,
|
|
|
|
true
|
|
|
|
);
|
2020-02-25 12:37:10 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Creates the MSFT application that will be associated to the bot.
|
|
|
|
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(this);
|
2020-12-31 15:36:19 -03:00
|
|
|
const application = await service.createApplication(accessToken, botId);
|
2020-02-25 12:37:10 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Fills new instance base information and get App secret.
|
|
|
|
|
2020-02-25 12:37:10 -03:00
|
|
|
instance.marketplaceId = (application as any).appId;
|
2020-08-15 11:39:43 -03:00
|
|
|
instance.marketplacePassword = await service.createApplicationSecret(accessToken, (application as any).id);
|
2020-02-25 12:37:10 -03:00
|
|
|
instance.adminPass = GBAdminService.getRndPassword();
|
2020-03-08 09:24:28 -03:00
|
|
|
instance.title = botId;
|
2024-01-03 14:37:28 -03:00
|
|
|
instance.activationCode = instance.botId.substring(0,15);
|
2020-05-27 23:01:44 -03:00
|
|
|
instance.state = 'active';
|
2020-12-26 19:47:38 -03:00
|
|
|
instance.nlpScore = 0.8;
|
2023-02-23 05:47:05 -03:00
|
|
|
instance.searchScore = 0.25;
|
2022-08-28 07:01:23 -03:00
|
|
|
instance.params = JSON.stringify({ 'Can Publish': mobile, 'Admin Notify E-mail': email });
|
2020-03-08 09:24:28 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Saves bot information to the store.
|
|
|
|
|
2020-02-25 12:37:10 -03:00
|
|
|
await this.core.saveInstance(instance);
|
2020-02-25 10:13:38 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Creates remaining objects on the cloud and updates instance information.
|
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
return await this.deployBotFull(instance, GBServer.globals.publicAddress);
|
2020-02-25 10:13:38 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Verifies if bot exists on bot catalog.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async botExists(botId: string): Promise<boolean> {
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(this);
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2020-06-11 09:47:59 -03:00
|
|
|
return await service.botExists(botId);
|
2020-04-15 20:56:28 -03:00
|
|
|
}
|
2020-12-26 19:47:38 -03:00
|
|
|
|
2018-09-11 19:33:58 -03:00
|
|
|
/**
|
2020-12-26 19:47:38 -03:00
|
|
|
* Performs all tasks of deploying a new bot on the cloud.
|
2018-09-09 14:39:37 -03:00
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async deployBotFull(instance: IGBInstance, publicAddress: string): Promise<IGBInstance> {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Reads base configuration from environent file.
|
|
|
|
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(this);
|
2019-05-14 23:02:21 -03:00
|
|
|
const username = GBConfigService.get('CLOUD_USERNAME');
|
|
|
|
const password = GBConfigService.get('CLOUD_PASSWORD');
|
2020-02-25 12:37:10 -03:00
|
|
|
const accessToken = await GBAdminService.getADALTokenFromUsername(username, password);
|
2019-05-14 23:02:21 -03:00
|
|
|
const group = GBConfigService.get('CLOUD_GROUP');
|
|
|
|
const subscriptionId = GBConfigService.get('CLOUD_SUBSCRIPTIONID');
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// If the bot already exists, just update the endpoint.
|
|
|
|
|
2020-06-11 09:47:59 -03:00
|
|
|
if (await service.botExists(instance.botId)) {
|
2019-08-22 01:54:30 +00:00
|
|
|
await service.updateBot(
|
|
|
|
instance.botId,
|
|
|
|
group,
|
2019-05-14 23:02:21 -03:00
|
|
|
instance.title,
|
|
|
|
instance.description,
|
2019-08-22 01:54:30 +00:00
|
|
|
`${publicAddress}/api/messages/${instance.botId}`
|
2019-05-14 23:02:21 -03:00
|
|
|
);
|
2020-12-31 15:36:19 -03:00
|
|
|
} else {
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Internally create resources on cloud provider.
|
|
|
|
|
2019-05-14 23:02:21 -03:00
|
|
|
instance = await service.internalDeployBot(
|
|
|
|
instance,
|
|
|
|
accessToken,
|
|
|
|
instance.botId,
|
|
|
|
instance.title,
|
|
|
|
group,
|
|
|
|
instance.description,
|
2019-08-22 01:54:30 +00:00
|
|
|
`${publicAddress}/api/messages/${instance.botId}`,
|
2019-05-14 23:02:21 -03:00
|
|
|
'global',
|
|
|
|
instance.nlpAppId,
|
|
|
|
instance.nlpKey,
|
|
|
|
instance.marketplaceId,
|
|
|
|
instance.marketplacePassword,
|
|
|
|
subscriptionId
|
|
|
|
);
|
2019-08-22 01:54:30 +00:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Makes available bot to the channels and .gbui interfaces.
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
await GBServer.globals.minService.mountBot(instance);
|
2023-09-16 17:41:36 -03:00
|
|
|
await GBServer.globals.minService.ensureAPI();
|
2019-05-14 23:02:21 -03:00
|
|
|
}
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
// Saves final instance object and returns it.
|
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
return await this.core.saveInstance(instance);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2024-01-29 21:04:53 -03:00
|
|
|
public async loadOrCreateEmptyVectorStore(min: GBMinInstance): Promise<HNSWLib> {
|
|
|
|
let vectorStore: HNSWLib;
|
|
|
|
|
|
|
|
try {
|
|
|
|
vectorStore = await HNSWLib.load(min['vectorStorePath'], new OpenAIEmbeddings({ maxConcurrency: 5 }));
|
|
|
|
} catch {
|
|
|
|
vectorStore = new HNSWLib(new OpenAIEmbeddings({ maxConcurrency: 5 }), {
|
|
|
|
space: 'cosine',
|
|
|
|
numDimensions: 1536,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return vectorStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Performs the NLP publishing process on remote service.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async publishNLP(instance: IGBInstance): Promise<void> {
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(this);
|
2022-11-19 23:34:58 -03:00
|
|
|
const res = await service.publishNLP(instance.cloudLocation, instance.nlpAppId, instance.nlpAuthoringKey);
|
|
|
|
if (res.status !== 200 && res.status !== 201) {
|
|
|
|
throw res.bodyAsText;
|
|
|
|
}
|
2020-10-18 21:28:19 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Trains NLP on the remote service.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async trainNLP(instance: IGBInstance): Promise<void> {
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(this);
|
2020-10-24 18:56:55 -03:00
|
|
|
const res = await service.trainNLP(instance.cloudLocation, instance.nlpAppId, instance.nlpAuthoringKey);
|
2022-11-19 23:34:58 -03:00
|
|
|
if (res.status !== 200 && res.status !== 202) {
|
|
|
|
throw res.bodyAsText;
|
|
|
|
}
|
2024-01-10 15:01:02 -03:00
|
|
|
GBUtil.sleep(5000);
|
2020-10-18 21:28:19 -03:00
|
|
|
}
|
|
|
|
|
2021-09-30 16:03:31 -03:00
|
|
|
/**
|
|
|
|
* Return a zip file for importing bot in apps, currently MS Teams.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async getBotManifest(instance: IGBInstance): Promise<Buffer> {
|
2021-09-30 16:03:31 -03:00
|
|
|
const s = new TeamsService();
|
2022-11-19 23:34:58 -03:00
|
|
|
const manifest = await s.getManifest(
|
|
|
|
instance.marketplaceId,
|
2023-07-31 18:52:11 -03:00
|
|
|
instance.botId,
|
2022-11-19 23:34:58 -03:00
|
|
|
instance.description,
|
|
|
|
GBAdminService.generateUuid(),
|
|
|
|
instance.botId,
|
|
|
|
'General Bots'
|
|
|
|
);
|
2021-11-16 14:53:52 -03:00
|
|
|
|
2021-09-30 16:03:31 -03:00
|
|
|
return await s.getAppFile(manifest);
|
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Refreshes NLP entities on the remote service.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async refreshNLPEntity(instance: IGBInstance, listName, listData): Promise<void> {
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(this);
|
2020-10-24 18:56:55 -03:00
|
|
|
const res = await service.refreshEntityList(
|
2020-10-18 17:46:16 -03:00
|
|
|
instance.cloudLocation,
|
|
|
|
instance.nlpAppId,
|
|
|
|
listName,
|
2020-12-05 09:26:19 -03:00
|
|
|
instance.nlpAuthoringKey,
|
2020-10-18 17:46:16 -03:00
|
|
|
listData
|
|
|
|
);
|
2022-11-19 23:34:58 -03:00
|
|
|
if (res.status !== 200) {
|
|
|
|
throw res.bodyAsText;
|
|
|
|
}
|
2020-10-18 17:46:16 -03:00
|
|
|
}
|
|
|
|
|
2020-02-25 10:13:38 -03:00
|
|
|
/**
|
|
|
|
* Deploys a bot to the storage from a .gbot folder.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async deployBotFromLocalPath(localPath: string, publicAddress: string): Promise<void> {
|
2020-02-25 10:13:38 -03:00
|
|
|
const packageName = Path.basename(localPath);
|
2020-12-31 15:36:19 -03:00
|
|
|
const instance = await this.importer.importIfNotExistsBotPackage(undefined, packageName, localPath);
|
2020-09-19 21:57:00 -03:00
|
|
|
await this.deployBotFull(instance, publicAddress);
|
2020-02-25 10:13:38 -03:00
|
|
|
}
|
2019-08-22 01:54:30 +00:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Loads all para from tabular file Config.xlsx.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async loadParamsFromTabular(min: GBMinInstance): Promise<any> {
|
2021-01-08 12:54:51 -03:00
|
|
|
const siteId = process.env.STORAGE_SITE_ID;
|
|
|
|
const libraryId = process.env.STORAGE_LIBRARY;
|
|
|
|
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.info(min, `Connecting to Config.xslx (siteId: ${siteId}, libraryId: ${libraryId})...`);
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Connects to MSFT storage.
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2023-08-25 19:09:03 -03:00
|
|
|
const token = await (min.adminService as any)['acquireElevatedToken'](min.instance.instanceId, true);
|
2023-07-19 08:03:39 -03:00
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
const client = MicrosoftGraph.Client.init({
|
2020-05-23 17:59:01 -03:00
|
|
|
authProvider: done => {
|
|
|
|
done(null, token);
|
|
|
|
}
|
|
|
|
});
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2021-01-12 18:33:21 -03:00
|
|
|
// Retrieves all files in .bot folder.
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-05-23 17:59:01 -03:00
|
|
|
const botId = min.instance.botId;
|
2023-03-09 17:46:34 -03:00
|
|
|
const path = DialogKeywords.getGBAIPath(botId, 'gbot');
|
2023-03-10 15:49:30 -03:00
|
|
|
let url = `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:/${path}:/children`;
|
2021-01-08 12:54:51 -03:00
|
|
|
|
2023-08-25 19:09:03 -03:00
|
|
|
GBLogEx.info(min, `Loading .gbot from Excel: ${url}`);
|
2022-11-19 23:34:58 -03:00
|
|
|
const res = await client.api(url).get();
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Finds Config.xlsx.
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
const document = res.value.filter(m => {
|
2020-08-15 11:39:43 -03:00
|
|
|
return m.name === 'Config.xlsx';
|
2020-05-23 17:59:01 -03:00
|
|
|
});
|
2020-06-14 21:40:41 -03:00
|
|
|
if (document === undefined || document.length === 0) {
|
2023-08-25 19:09:03 -03:00
|
|
|
GBLogEx.info(min, `Config.xlsx not found on .bot folder, check the package.`);
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2020-06-14 21:40:41 -03:00
|
|
|
return null;
|
2020-05-23 17:59:01 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Reads all rows in Config.xlsx that contains a pair of name/value
|
|
|
|
// and fills an object that is returned to be saved in params instance field.
|
2020-05-23 17:59:01 -03:00
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
const results = await client
|
2020-08-15 11:39:43 -03:00
|
|
|
.api(
|
2023-01-29 12:02:14 -03:00
|
|
|
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('General')/range(address='A7:B100')`
|
2020-08-15 11:39:43 -03:00
|
|
|
)
|
2020-05-23 17:59:01 -03:00
|
|
|
.get();
|
2022-11-19 23:34:58 -03:00
|
|
|
let index = 0,
|
|
|
|
obj = {};
|
2020-05-23 17:59:01 -03:00
|
|
|
for (; index < results.text.length; index++) {
|
2020-08-15 11:39:43 -03:00
|
|
|
if (results.text[index][0] === '') {
|
2020-05-23 17:59:01 -03:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
obj[results.text[index][0]] = results.text[index][1];
|
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2020-05-23 17:59:01 -03:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:50:27 -03:00
|
|
|
/**
|
|
|
|
* Loads all para from tabular file Config.xlsx.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async downloadFolder(
|
2022-11-19 23:34:58 -03:00
|
|
|
min: GBMinInstance,
|
|
|
|
localPath: string,
|
|
|
|
remotePath: string,
|
|
|
|
baseUrl: string = null,
|
|
|
|
client = null
|
|
|
|
): Promise<any> {
|
2023-08-25 19:09:03 -03:00
|
|
|
GBLogEx.info(min, `downloadFolder: localPath=${localPath}, remotePath=${remotePath}, baseUrl=${baseUrl}`);
|
2021-09-13 18:36:26 -03:00
|
|
|
|
2021-03-29 18:50:27 -03:00
|
|
|
if (!baseUrl) {
|
2022-12-22 11:31:37 -03:00
|
|
|
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
2021-03-29 18:50:27 -03:00
|
|
|
|
|
|
|
remotePath = remotePath.replace(/\\/gi, '/');
|
|
|
|
const parts = remotePath.split('/');
|
|
|
|
|
|
|
|
// Creates each subfolder.
|
|
|
|
|
|
|
|
let pathBase = localPath;
|
|
|
|
if (!Fs.existsSync(pathBase)) {
|
|
|
|
Fs.mkdirSync(pathBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
await CollectionUtil.asyncForEach(parts, async item => {
|
|
|
|
pathBase = Path.join(pathBase, item);
|
|
|
|
if (!Fs.existsSync(pathBase)) {
|
|
|
|
Fs.mkdirSync(pathBase);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Retrieves all files in remote folder.
|
|
|
|
|
2023-03-09 17:46:34 -03:00
|
|
|
let path = DialogKeywords.getGBAIPath(min.botId);
|
|
|
|
path = urlJoin(path, remotePath);
|
2023-03-10 15:49:30 -03:00
|
|
|
let url = `${baseUrl}/drive/root:/${path}:/children`;
|
2021-09-30 16:03:31 -03:00
|
|
|
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.info(min, `Download URL: ${url}`);
|
2021-03-29 18:50:27 -03:00
|
|
|
|
2022-12-22 11:31:37 -03:00
|
|
|
const res = await client.api(url).get();
|
2021-03-29 18:50:27 -03:00
|
|
|
const documents = res.value;
|
|
|
|
if (documents === undefined || documents.length === 0) {
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.info(min, `${remotePath} is an empty folder.`);
|
2021-03-29 18:50:27 -03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download files or navigate to directory to recurse.
|
|
|
|
|
|
|
|
await CollectionUtil.asyncForEach(documents, async item => {
|
|
|
|
const itemPath = Path.join(localPath, remotePath, item.name);
|
|
|
|
|
|
|
|
if (item.folder) {
|
|
|
|
if (!Fs.existsSync(itemPath)) {
|
|
|
|
Fs.mkdirSync(itemPath);
|
|
|
|
}
|
|
|
|
const nextFolder = urlJoin(remotePath, item.name);
|
|
|
|
await this.downloadFolder(min, localPath, nextFolder);
|
|
|
|
} else {
|
2021-12-29 07:53:14 -03:00
|
|
|
let download = true;
|
2021-12-26 16:13:33 -03:00
|
|
|
|
|
|
|
if (Fs.existsSync(itemPath)) {
|
|
|
|
const dt = Fs.statSync(itemPath);
|
2023-02-26 06:48:47 -03:00
|
|
|
if (new Date(dt.mtime) >= new Date(item.lastModifiedDateTime)) {
|
2021-12-29 07:53:14 -03:00
|
|
|
download = false;
|
2021-12-26 16:13:33 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (download) {
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.verbose(min, `Downloading ${itemPath}...`);
|
2021-12-26 16:13:33 -03:00
|
|
|
const url = item['@microsoft.graph.downloadUrl'];
|
|
|
|
|
2022-11-30 09:40:09 -03:00
|
|
|
const response = await fetch(url);
|
|
|
|
Fs.writeFileSync(itemPath, Buffer.from(await response.arrayBuffer()), { encoding: null });
|
2022-11-19 23:34:58 -03:00
|
|
|
Fs.utimesSync(itemPath, new Date(), new Date(item.lastModifiedDateTime));
|
|
|
|
} else {
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.info(min, `Local is up to date: ${itemPath}...`);
|
2021-12-26 16:13:33 -03:00
|
|
|
}
|
2021-03-29 18:50:27 -03:00
|
|
|
}
|
2022-08-28 07:01:23 -03:00
|
|
|
});
|
2021-03-29 18:50:27 -03:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 01:54:30 +00:00
|
|
|
/**
|
2019-08-22 17:28:11 -03:00
|
|
|
* UndDeploys a bot to the storage.
|
2019-08-22 01:54:30 +00:00
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async undeployBot(botId: string, packageName: string): Promise<void> {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Deletes Bot registration on cloud.
|
2019-08-22 01:54:30 +00:00
|
|
|
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(this);
|
2020-12-26 19:47:38 -03:00
|
|
|
const group = GBConfigService.get('CLOUD_GROUP');
|
2020-06-11 09:47:59 -03:00
|
|
|
if (await service.botExists(botId)) {
|
2020-08-15 11:39:43 -03:00
|
|
|
await service.deleteBot(botId, group);
|
2019-08-22 01:54:30 +00:00
|
|
|
}
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
// Unbinds resources and listeners.
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
GBServer.globals.minService.unmountBot(botId);
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
// Removes the bot from the storage.
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
await this.core.deleteInstance(botId);
|
2019-08-22 17:28:11 -03:00
|
|
|
}
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deploys a new package to the database storage (just a group).
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async deployPackageToStorage(instanceId: number, packageName: string): Promise<GuaribasPackage> {
|
2022-01-03 13:11:21 -03:00
|
|
|
return await GuaribasPackage.create(<GuaribasPackage>{
|
2018-04-21 02:59:30 -03:00
|
|
|
packageName: packageName,
|
2018-11-27 22:56:11 -02:00
|
|
|
instanceId: instanceId
|
2018-09-24 11:04:36 -03:00
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2023-02-26 15:03:24 -03:00
|
|
|
public async deployPackage(min: GBMinInstance, localPath: string) {
|
2023-09-13 19:42:45 -03:00
|
|
|
// TODO: Adjust interface mismatch.
|
2023-02-26 15:03:24 -03:00
|
|
|
}
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Deploys a folder into the bot storage.
|
|
|
|
*/
|
2023-02-26 15:03:24 -03:00
|
|
|
public async deployPackage2(min: GBMinInstance, user, localPath: string) {
|
2020-12-26 19:47:38 -03:00
|
|
|
const packageType = Path.extname(localPath);
|
2020-07-18 16:38:58 -03:00
|
|
|
let handled = false;
|
|
|
|
let pck = null;
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Asks for each .gbapp if it will handle the package publishing.
|
2020-07-18 16:38:58 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
const _this = this;
|
2020-07-18 16:38:58 -03:00
|
|
|
await CollectionUtil.asyncForEach(min.appPackages, async (e: IGBPackage) => {
|
2023-01-29 12:02:14 -03:00
|
|
|
// If it will be handled, create a temporary service layer to be
|
|
|
|
// called by .gbapp and manage the associated package row.
|
|
|
|
|
|
|
|
if (
|
|
|
|
(pck = await e.onExchangeData(min, 'handlePackage', {
|
|
|
|
name: localPath,
|
|
|
|
createPackage: async packageName => {
|
|
|
|
return await _this.deployPackageToStorage(min.instance.instanceId, packageName);
|
|
|
|
},
|
|
|
|
updatePackage: async (p: GuaribasPackage) => {
|
|
|
|
p.save();
|
|
|
|
},
|
|
|
|
existsPackage: async (packageName: string) => {
|
|
|
|
return await _this.getStoragePackageByName(min.instance.instanceId, packageName);
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
) {
|
|
|
|
handled = true;
|
2020-07-18 16:38:58 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (handled) {
|
|
|
|
return pck;
|
|
|
|
}
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
// Deploy platform packages here accordingly to their extension.
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
switch (packageType) {
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbot':
|
2020-12-26 19:47:38 -03:00
|
|
|
// Extracts configuration information from .gbot files.
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-10-24 18:56:55 -03:00
|
|
|
if (process.env.ENABLE_PARAMS_ONLINE === 'false') {
|
|
|
|
if (Fs.existsSync(localPath)) {
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.info(min, `Loading .gbot from ${localPath}.`);
|
2020-10-24 18:56:55 -03:00
|
|
|
await this.deployBotFromLocalPath(localPath, GBServer.globals.publicAddress);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
min.instance.params = await this.loadParamsFromTabular(min);
|
2020-08-22 18:41:54 -03:00
|
|
|
}
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2023-11-28 14:13:44 -03:00
|
|
|
let connections = [];
|
|
|
|
|
|
|
|
// Find all tokens in .gbot Config.
|
|
|
|
const strFind = ' Driver';
|
2023-11-29 18:46:02 -03:00
|
|
|
const conns = await min.core['findParam'](min.instance, strFind);
|
|
|
|
await CollectionUtil.asyncForEach(conns, async t => {
|
2023-11-28 14:13:44 -03:00
|
|
|
const connectionName = t.replace(strFind, '');
|
|
|
|
let con = {};
|
2023-11-29 18:46:02 -03:00
|
|
|
con['name'] = connectionName;
|
2023-12-14 12:34:41 -03:00
|
|
|
con['storageServer'] = min.core.getParam<string>(min.instance, `${connectionName} Server`, null),
|
|
|
|
con['storageName'] = min.core.getParam<string>(min.instance, `${connectionName} Name`, null),
|
|
|
|
con['storageUsername'] = min.core.getParam<string>(min.instance, `${connectionName} Username`, null),
|
|
|
|
con['storagePort'] = min.core.getParam<string>(min.instance, `${connectionName} Port`, null),
|
|
|
|
con['storagePassword'] = min.core.getParam<string>(min.instance, `${connectionName} Password`, null),
|
|
|
|
con['storageDriver'] = min.core.getParam<string>(min.instance, `${connectionName} Driver`, null)
|
2023-11-28 14:13:44 -03:00
|
|
|
connections.push(con);
|
|
|
|
});
|
|
|
|
|
|
|
|
const path = DialogKeywords.getGBAIPath(min.botId, null);
|
|
|
|
const localFolder = Path.join('work', path, 'connections.json');
|
|
|
|
Fs.writeFileSync(localFolder, JSON.stringify(connections), { encoding: null });
|
2023-12-14 12:34:41 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Updates instance object.
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-05-23 17:59:01 -03:00
|
|
|
await this.core.saveInstance(min.instance);
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
break;
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbkb':
|
2020-12-26 19:47:38 -03:00
|
|
|
// Deploys .gbkb into the storage.
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const service = new KBService(this.core.sequelize);
|
2020-05-12 19:20:59 -03:00
|
|
|
await service.deployKb(this.core, this, localPath, min);
|
2019-08-22 17:28:11 -03:00
|
|
|
break;
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-11-26 14:09:09 -02:00
|
|
|
case '.gbdialog':
|
2020-12-26 19:47:38 -03:00
|
|
|
// Compiles files from .gbdialog into work folder and deploys
|
|
|
|
// it to the VM.
|
|
|
|
|
2018-11-26 15:54:34 -02:00
|
|
|
const vm = new GBVMService();
|
2019-05-15 22:30:14 -03:00
|
|
|
await vm.loadDialogPackage(localPath, min, this.core, this);
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.verbose(min, `Dialogs (.gbdialog) for ${min.botId} loaded.`);
|
2019-08-22 17:28:11 -03:00
|
|
|
break;
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
case '.gbtheme':
|
2020-12-26 19:47:38 -03:00
|
|
|
// Updates server listeners to serve theme files in .gbtheme.
|
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
const packageName = Path.basename(localPath);
|
2020-05-15 14:07:30 -03:00
|
|
|
GBServer.globals.server.use(`/themes/${packageName}`, express.static(localPath));
|
2023-04-20 22:41:58 -03:00
|
|
|
GBLogEx.verbose(min, `Theme (.gbtheme) assets accessible at: /themes/${packageName}.`);
|
2020-05-14 12:46:57 -03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.gbapp':
|
2020-12-26 19:47:38 -03:00
|
|
|
// Dynamically compiles and loads .gbapp packages (Node.js packages).
|
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
await this.callGBAppCompiler(localPath, this.core);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.gblib':
|
2020-12-26 19:47:38 -03:00
|
|
|
// Dynamically compiles and loads .gblib packages (Node.js packages).
|
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
await this.callGBAppCompiler(localPath, this.core);
|
|
|
|
break;
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
default:
|
2023-02-26 06:05:57 -03:00
|
|
|
throw GBError.create(`Unhandled package type: ${packageType}.`);
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
/**
|
|
|
|
* Removes the package local files from cache.
|
|
|
|
*/
|
|
|
|
public async cleanupPackage(instance: IGBInstance, packageName: string) {
|
|
|
|
const path = DialogKeywords.getGBAIPath(instance.botId, null, packageName);
|
|
|
|
const localFolder = Path.join('work', path);
|
|
|
|
rimraf.sync(localFolder);
|
|
|
|
}
|
|
|
|
|
2023-09-13 21:02:33 -03:00
|
|
|
/**
|
|
|
|
* Removes the package from the storage and local work folders.
|
|
|
|
*/
|
|
|
|
public async undeployPackageFromPackageName(instance: IGBInstance, packageName: string) {
|
|
|
|
// Gets information about the package.
|
|
|
|
|
|
|
|
const packageType = Path.extname(packageName);
|
|
|
|
const p = await this.getStoragePackageByName(instance.instanceId, packageName);
|
|
|
|
|
|
|
|
const path = DialogKeywords.getGBAIPath(instance.botId, null, packageName);
|
|
|
|
const localFolder = Path.join('work', path);
|
2023-12-14 12:34:41 -03:00
|
|
|
|
2023-09-13 21:02:33 -03:00
|
|
|
return await this.undeployPackageFromLocalPath(instance, localFolder);
|
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Removes the package from the storage and local work folders.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async undeployPackageFromLocalPath(instance: IGBInstance, localPath: string) {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Gets information about the package.
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const packageType = Path.extname(localPath);
|
|
|
|
const packageName = Path.basename(localPath);
|
2019-08-23 14:36:47 -03:00
|
|
|
const p = await this.getStoragePackageByName(instance.instanceId, packageName);
|
2020-05-12 19:20:59 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Removes objects from storage, cloud resources and local files if any.
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
switch (packageType) {
|
2019-08-22 01:54:30 +00:00
|
|
|
case '.gbot':
|
2020-04-19 13:04:38 -03:00
|
|
|
const packageObject = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'package.json'), 'utf8'));
|
2019-08-22 01:54:30 +00:00
|
|
|
await this.undeployBot(packageObject.botId, packageName);
|
|
|
|
break;
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbkb':
|
|
|
|
const service = new KBService(this.core.sequelize);
|
2020-06-03 21:31:00 -03:00
|
|
|
rimraf.sync(localPath);
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
if (p) {
|
2023-09-14 00:09:31 -03:00
|
|
|
await service.undeployKbFromStorage(instance, this, p.packageId);
|
|
|
|
}
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2023-09-14 00:09:31 -03:00
|
|
|
return;
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbui':
|
2019-08-22 17:28:11 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '.gbtheme':
|
2018-09-24 11:04:36 -03:00
|
|
|
break;
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-11-27 22:56:11 -02:00
|
|
|
case '.gbdialog':
|
|
|
|
break;
|
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
case '.gblib':
|
|
|
|
break;
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
case '.gbapp':
|
|
|
|
break;
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
default:
|
2019-03-09 16:59:31 -03:00
|
|
|
const err = GBError.create(`Unhandled package type: ${packageType}.`);
|
2018-09-24 11:04:36 -03:00
|
|
|
Promise.reject(err);
|
|
|
|
break;
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|
2019-08-23 14:36:47 -03:00
|
|
|
rimraf.sync(localPath);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Performs automation of the Indexer (Azure Search) and rebuild
|
|
|
|
* its index based on .gbkb structure.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async rebuildIndex(instance: IGBInstance, searchSchema: any) {
|
2023-09-10 22:36:39 -03:00
|
|
|
|
2024-02-15 20:50:17 -03:00
|
|
|
const key = instance.searchKey ? instance.searchKey : GBServer.globals.minBoot.instance.searchKey;
|
2023-12-14 12:34:41 -03:00
|
|
|
GBLogEx.info(instance.instanceId, `rebuildIndex running...`);
|
2023-02-27 19:17:52 -03:00
|
|
|
|
2024-02-15 20:50:17 -03:00
|
|
|
if (!key){
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-12-14 12:34:41 -03:00
|
|
|
const searchIndex = instance.searchIndex ? instance.searchIndex : GBServer.globals.minBoot.instance.searchIndex;
|
|
|
|
const searchIndexer = instance.searchIndexer
|
|
|
|
? instance.searchIndexer
|
|
|
|
: GBServer.globals.minBoot.instance.searchIndexer;
|
|
|
|
const host = instance.searchHost ? instance.searchHost : GBServer.globals.minBoot.instance.searchHost;
|
2023-02-27 19:17:52 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
// Prepares search.
|
2023-02-26 15:03:24 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
const search = new AzureSearch(
|
|
|
|
key,
|
|
|
|
host,
|
|
|
|
searchIndex,
|
|
|
|
searchIndexer
|
|
|
|
);
|
|
|
|
const connectionString = GBDeployer.getConnectionStringFromInstance(GBServer.globals.minBoot.instance);
|
|
|
|
const dsName = 'gb';
|
2018-11-04 09:19:03 -02:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
// Removes any previous index.
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
try {
|
|
|
|
await search.deleteDataSource(dsName);
|
|
|
|
} catch (err) {
|
|
|
|
// If it is a 404 there is nothing to delete as it is the first creation.
|
2020-12-26 19:47:38 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
if (err.code !== 404) {
|
|
|
|
throw err;
|
2023-02-26 06:05:57 -03:00
|
|
|
}
|
2023-12-14 12:34:41 -03:00
|
|
|
}
|
2023-02-24 13:31:40 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
// Removes the index.
|
2023-02-27 19:17:52 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
try {
|
|
|
|
await search.deleteIndex();
|
|
|
|
} catch (err) {
|
|
|
|
// If it is a 404 there is nothing to delete as it is the first creation.
|
|
|
|
|
|
|
|
if (err.code !== 404 && err.code !== 'OperationNotAllowed') {
|
2023-02-27 19:17:52 -03:00
|
|
|
throw err;
|
|
|
|
}
|
2023-12-14 12:34:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the data source and index on the cloud.
|
|
|
|
|
|
|
|
try {
|
|
|
|
await search.createDataSource(dsName, dsName, 'GuaribasQuestion', 'azuresql', connectionString);
|
|
|
|
} catch (err) {
|
|
|
|
GBLog.error(err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await search.createIndex(searchSchema, dsName);
|
2023-02-27 19:17:52 -03:00
|
|
|
|
2023-12-14 12:34:41 -03:00
|
|
|
GBLogEx.info(instance.instanceId, `Released rebuildIndex mutex.`);
|
2018-09-24 11:04:36 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Finds a storage package by using package name.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async getStoragePackageByName(instanceId: number, packageName: string): Promise<GuaribasPackage> {
|
2018-11-12 12:20:44 -02:00
|
|
|
const where = { packageName: packageName, instanceId: instanceId };
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
return await GuaribasPackage.findOne({
|
2018-11-27 22:56:11 -02:00
|
|
|
where: where
|
2018-09-24 11:04:36 -03:00
|
|
|
});
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|
2018-11-30 11:55:44 -02:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Prepares the React application inside default.gbui folder and
|
|
|
|
* makes this web application available as default web front-end.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public setupDefaultGBUI() {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Setups paths.
|
|
|
|
|
2018-11-30 11:55:44 -02:00
|
|
|
const root = 'packages/default.gbui';
|
2019-05-27 09:26:40 -03:00
|
|
|
const npm = urlJoin(process.env.PWD, 'node_modules', '.bin', 'npm');
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
// Checks if .gbapp compiliation is enabled.
|
|
|
|
|
|
|
|
if (!Fs.existsSync(`${root}/build`) && process.env.DISABLE_WEB !== 'true') {
|
2021-01-20 18:23:42 -03:00
|
|
|
// Write a .env required to fix some bungs in create-react-app tool.
|
2020-12-26 19:47:38 -03:00
|
|
|
|
2018-11-30 11:55:44 -02:00
|
|
|
Fs.writeFileSync(`${root}/.env`, 'SKIP_PREFLIGHT_CHECK=true');
|
2020-12-26 19:47:38 -03:00
|
|
|
|
|
|
|
// Install modules and compiles the web app.
|
|
|
|
|
2022-01-23 21:27:35 -03:00
|
|
|
GBLog.info(`Installing modules default.gbui (It may take a few minutes)...`);
|
2022-08-28 07:01:23 -03:00
|
|
|
|
2022-01-28 21:59:26 -03:00
|
|
|
child_process.execSync(`${npm} install`, { cwd: root });
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2022-01-23 21:27:35 -03:00
|
|
|
GBLog.info(`Transpiling default.gbui...`);
|
|
|
|
child_process.execSync(`${npm} run build`, { cwd: root });
|
2018-12-18 13:50:35 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Servers bot storage assets to be used by web, WhatsApp and other channels.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public static mountGBKBAssets(packageName: any, botId: string, filename: string) {
|
2023-03-09 17:46:34 -03:00
|
|
|
const gbaiName = DialogKeywords.getGBAIPath(botId);
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Servers menu assets.
|
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
GBServer.globals.server.use(
|
2023-03-09 17:46:34 -03:00
|
|
|
`/kb/${gbaiName}/${packageName}/subjects`,
|
2020-08-15 11:39:43 -03:00
|
|
|
express.static(urlJoin(filename, 'subjects'))
|
|
|
|
);
|
2020-11-09 19:53:42 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// Servers all other assets in .gbkb folders.
|
2020-11-09 19:53:42 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
GBServer.globals.server.use(
|
|
|
|
`/kb/${gbaiName}/${packageName}/assets`,
|
|
|
|
express.static(urlJoin('work', gbaiName, filename, 'assets'))
|
|
|
|
);
|
|
|
|
GBServer.globals.server.use(
|
|
|
|
`/kb/${gbaiName}/${packageName}/images`,
|
|
|
|
express.static(urlJoin('work', gbaiName, filename, 'images'))
|
|
|
|
);
|
|
|
|
GBServer.globals.server.use(
|
|
|
|
`/kb/${gbaiName}/${packageName}/audios`,
|
|
|
|
express.static(urlJoin('work', gbaiName, filename, 'audios'))
|
|
|
|
);
|
|
|
|
GBServer.globals.server.use(
|
|
|
|
`/kb/${gbaiName}/${packageName}/videos`,
|
|
|
|
express.static(urlJoin('work', gbaiName, filename, 'videos'))
|
|
|
|
);
|
|
|
|
GBServer.globals.server.use(`/${botId}/cache`, express.static(urlJoin('work', gbaiName, 'cache')));
|
|
|
|
GBServer.globals.server.use(
|
2023-03-09 09:49:37 -03:00
|
|
|
`/${gbaiName}/${botId}.gbdrive/public`,
|
2022-11-19 23:34:58 -03:00
|
|
|
express.static(urlJoin('work', gbaiName, `${botId}.gbdata`, 'public'))
|
|
|
|
);
|
2021-11-22 19:48:53 -03:00
|
|
|
|
2023-03-09 17:46:34 -03:00
|
|
|
GBLog.verbose(`KB (.gbkb) assets accessible at: /kb/${gbaiName}/${packageName}.`);
|
2019-08-24 18:46:04 -03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
/**
|
|
|
|
* Invokes Type Script compiler for a given .gbapp package (Node.js based).
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
public async callGBAppCompiler(
|
2020-08-15 11:39:43 -03:00
|
|
|
gbappPath: string,
|
|
|
|
core: IGBCoreService,
|
|
|
|
appPackages: any[] = undefined,
|
|
|
|
appPackagesProcessed: number = 0
|
|
|
|
) {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Runs `npm install` for the package.
|
|
|
|
|
2020-05-14 12:48:36 -03:00
|
|
|
GBLog.info(`Deploying General Bots Application (.gbapp) or Library (.gblib): ${Path.basename(gbappPath)}...`);
|
2020-05-14 12:46:57 -03:00
|
|
|
let folder = Path.join(gbappPath, 'node_modules');
|
2020-08-15 11:39:43 -03:00
|
|
|
if (process.env.GBAPP_DISABLE_COMPILE !== 'true') {
|
2020-05-14 12:46:57 -03:00
|
|
|
if (!Fs.existsSync(folder)) {
|
|
|
|
GBLog.info(`Installing modules for ${gbappPath}...`);
|
|
|
|
child_process.execSync('npm install', { cwd: gbappPath });
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
folder = Path.join(gbappPath, 'dist');
|
|
|
|
try {
|
2020-12-26 19:47:38 -03:00
|
|
|
// Runs TSC in .gbapp folder.
|
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
if (process.env.GBAPP_DISABLE_COMPILE !== 'true') {
|
2020-05-14 12:46:57 -03:00
|
|
|
GBLog.info(`Compiling: ${gbappPath}.`);
|
|
|
|
child_process.execSync(Path.join(process.env.PWD, 'node_modules/.bin/tsc'), { cwd: gbappPath });
|
|
|
|
}
|
2020-07-03 14:35:42 -03:00
|
|
|
|
2020-12-26 19:47:38 -03:00
|
|
|
// After compiled, adds the .gbapp to the current server VM context.
|
|
|
|
|
2021-05-18 10:56:04 -03:00
|
|
|
if (gbappPath.endsWith('.gbapp') || gbappPath.endsWith('.gblib')) {
|
2023-04-20 22:41:58 -03:00
|
|
|
const m = await import(`file://${gbappPath}/dist/index.js`);
|
2021-05-18 10:56:04 -03:00
|
|
|
if (m.Package) {
|
|
|
|
const p = new m.Package();
|
2022-12-22 11:31:37 -03:00
|
|
|
|
|
|
|
// Adds a name property to the list of loaded .gbapp packages.
|
|
|
|
|
|
|
|
p['name'] = gbappPath;
|
2021-05-18 10:56:04 -03:00
|
|
|
await p.loadPackage(core, core.sequelize);
|
|
|
|
if (appPackages !== undefined) {
|
|
|
|
appPackages.push(p);
|
|
|
|
}
|
2020-07-03 14:35:42 -03:00
|
|
|
}
|
2020-05-14 12:46:57 -03:00
|
|
|
}
|
|
|
|
GBLog.info(`.gbapp or .gblib deployed: ${gbappPath}.`);
|
|
|
|
appPackagesProcessed++;
|
2020-08-15 11:39:43 -03:00
|
|
|
} catch (error) {
|
2020-07-18 16:38:58 -03:00
|
|
|
GBLog.error(`Error compiling package, message: ${error.message}\n${error.stack}`);
|
|
|
|
if (error.stdout) {
|
2020-12-26 19:47:38 -03:00
|
|
|
GBLog.error(`.gbapp stdout: ${gbappPath}:\n${error.stdout.toString()}`);
|
2020-07-18 16:38:58 -03:00
|
|
|
}
|
2020-05-14 12:46:57 -03:00
|
|
|
appPackagesProcessed++;
|
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
|
|
|
return appPackagesProcessed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a given package is of system kind.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
private isSystemPackage(name: string): Boolean {
|
2020-12-31 15:36:19 -03:00
|
|
|
const names = [
|
|
|
|
'analytics.gblib',
|
|
|
|
'console.gblib',
|
|
|
|
'security.gbapp',
|
|
|
|
'whatsapp.gblib',
|
|
|
|
'sharepoint.gblib',
|
|
|
|
'core.gbapp',
|
2021-01-03 18:29:41 -03:00
|
|
|
'basic.gblib',
|
2020-12-31 15:36:19 -03:00
|
|
|
'admin.gbapp',
|
|
|
|
'azuredeployer.gbapp',
|
|
|
|
'customer-satisfaction.gbapp',
|
2021-06-11 09:50:40 -03:00
|
|
|
'kb.gbapp',
|
2021-09-30 16:03:31 -03:00
|
|
|
'google-chat.gblib',
|
2021-12-19 16:39:50 -03:00
|
|
|
'teams.gblib',
|
2023-07-23 15:39:05 -03:00
|
|
|
'hubspot.gblib',
|
|
|
|
'gpt.gblib'
|
2020-12-31 15:36:19 -03:00
|
|
|
];
|
|
|
|
|
|
|
|
return names.indexOf(name) > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the process of compiling all .gbapp folders.
|
|
|
|
*/
|
2023-01-29 12:02:14 -03:00
|
|
|
private async deployAppPackages(gbappPackages: string[], core: any, appPackages: any[]) {
|
2020-12-31 15:36:19 -03:00
|
|
|
// Loops through all ready to load .gbapp packages.
|
|
|
|
|
|
|
|
let appPackagesProcessed = 0;
|
|
|
|
await CollectionUtil.asyncForEach(gbappPackages, async e => {
|
|
|
|
const filenameOnly = Path.basename(e);
|
|
|
|
|
|
|
|
// Skips .gbapp inside deploy folder.
|
|
|
|
|
|
|
|
if (this.isSystemPackage(filenameOnly) === false) {
|
|
|
|
appPackagesProcessed = await this.callGBAppCompiler(e, core, appPackages, appPackagesProcessed);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
return appPackagesProcessed;
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|