2024-01-10 15:01:02 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| █████ █████ ██ █ █████ █████ ████ ██ ████ █████ █████ ███ ® |
|
|
|
|
| ██ █ ███ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| ██ ███ ████ █ ██ █ ████ █████ ██████ ██ ████ █ █ █ ██ |
|
|
|
|
| ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ |
|
|
|
|
| |
|
2024-05-26 20:13:56 -03:00
|
|
|
| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. |
|
2024-01-10 15:01:02 -03:00
|
|
|
| Licensed under the AGPL-3.0. |
|
|
|
|
| |
|
|
|
|
| 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, |
|
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
2024-05-26 20:13:56 -03:00
|
|
|
| "General Bots" is a registered trademark of pragmatismo.cloud. |
|
2024-01-10 15:01:02 -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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
2024-01-09 17:40:48 -03:00
|
|
|
|
2024-01-10 15:01:02 -03:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots local utility.
|
|
|
|
*/
|
2024-01-09 17:40:48 -03:00
|
|
|
|
2024-01-10 15:01:02 -03:00
|
|
|
'use strict';
|
2024-05-26 20:13:56 -03:00
|
|
|
import * as YAML from 'yaml';
|
2024-08-19 16:12:23 -03:00
|
|
|
import SwaggerClient from 'swagger-client';
|
2024-09-11 14:41:24 -03:00
|
|
|
import fs from 'fs/promises';
|
2024-08-19 23:03:58 -03:00
|
|
|
import { GBConfigService } from '../packages/core.gbapp/services/GBConfigService.js';
|
2024-08-21 13:26:40 -03:00
|
|
|
import path from 'path';
|
2024-09-07 18:13:36 -03:00
|
|
|
import { VerbosityLevel, getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs';
|
2024-09-11 14:41:24 -03:00
|
|
|
VerbosityLevel.ERRORS = 0;
|
|
|
|
VerbosityLevel.WARNINGS = 0;
|
|
|
|
VerbosityLevel.INFOS = 0;
|
2024-09-06 15:15:42 -03:00
|
|
|
import { Page } from 'puppeteer';
|
|
|
|
import urljoin from 'url-join';
|
2024-01-10 15:01:02 -03:00
|
|
|
|
|
|
|
export class GBUtil {
|
2024-05-26 20:13:56 -03:00
|
|
|
public static repeat(chr, count) {
|
|
|
|
let str = '';
|
|
|
|
for (let x = 0; x < count; x++) {
|
|
|
|
str += chr;
|
|
|
|
}
|
2024-01-13 14:23:04 -03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
public static padL(value, width, pad) {
|
|
|
|
if (!width || width < 1) return value;
|
2024-01-13 14:23:04 -03:00
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
if (!pad) pad = ' ';
|
|
|
|
const length = width - value.length;
|
2024-01-13 14:23:04 -03:00
|
|
|
if (length < 1) return value.substr(0, width);
|
|
|
|
|
|
|
|
return (GBUtil.repeat(pad, length) + value).substr(0, width);
|
|
|
|
}
|
2024-08-19 16:12:23 -03:00
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
public static padR(value, width, pad) {
|
|
|
|
if (!width || width < 1) return value;
|
2024-01-13 14:23:04 -03:00
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
if (!pad) pad = ' ';
|
|
|
|
const length = width - value.length;
|
2024-01-13 14:23:04 -03:00
|
|
|
if (length < 1) value.substr(0, width);
|
|
|
|
|
|
|
|
return (value + GBUtil.repeat(pad, length)).substr(0, width);
|
|
|
|
}
|
|
|
|
|
2024-08-19 16:12:23 -03:00
|
|
|
public static async getDirectLineClient(min) {
|
|
|
|
let config = {
|
2024-09-07 18:13:36 -03:00
|
|
|
spec: JSON.parse(await fs.readFile('directline-3.0.json', 'utf8')),
|
2024-08-19 16:12:23 -03:00
|
|
|
requestInterceptor: req => {
|
|
|
|
req.headers['Authorization'] = `Bearer ${min.instance.webchatKey}`;
|
|
|
|
}
|
2024-08-20 15:26:07 -03:00
|
|
|
};
|
2024-08-20 15:35:03 -03:00
|
|
|
if (!GBConfigService.get('STORAGE_NAME')) {
|
2024-08-24 00:12:50 -03:00
|
|
|
(config['spec'].url = `http://127.0.0.1:${GBConfigService.getServerPort()}/api/messages/${min.botId}`),
|
|
|
|
(config['spec'].servers = [
|
|
|
|
{ url: `http://127.0.0.1:${GBConfigService.getServerPort()}/api/messages/${min.botId}` }
|
|
|
|
]);
|
2024-08-20 16:12:30 -03:00
|
|
|
config['spec'].openapi = '3.0.0';
|
2024-08-20 15:49:28 -03:00
|
|
|
delete config['spec'].host;
|
2024-08-20 16:18:01 -03:00
|
|
|
delete config['spec'].swagger;
|
2024-08-19 16:12:23 -03:00
|
|
|
}
|
2024-08-20 15:49:28 -03:00
|
|
|
|
2024-08-20 15:32:18 -03:00
|
|
|
return await new SwaggerClient(config);
|
2024-08-19 16:12:23 -03:00
|
|
|
}
|
|
|
|
|
2024-08-20 15:26:07 -03:00
|
|
|
public static toYAML(data) {
|
2024-08-20 15:30:47 -03:00
|
|
|
const extractProps = obj => {
|
|
|
|
return Object.getOwnPropertyNames(obj).reduce((acc, key) => {
|
|
|
|
const value = obj[key];
|
|
|
|
acc[key] = value && typeof value === 'object' && !Array.isArray(value) ? extractProps(value) : value;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2024-08-20 15:26:07 -03:00
|
|
|
};
|
2024-09-11 14:41:24 -03:00
|
|
|
|
2024-08-20 15:26:07 -03:00
|
|
|
const extractedError = extractProps(data);
|
2024-09-11 14:41:24 -03:00
|
|
|
|
2024-09-08 16:48:26 -03:00
|
|
|
// Inline formatting for logs
|
|
|
|
return YAML.stringify(extractedError, {
|
2024-09-11 14:41:24 -03:00
|
|
|
indent: 2, // Defines the indentation
|
|
|
|
flowLevel: -1, // Forces inline formatting
|
|
|
|
styles: { '!!null': 'canonical' } // Optional: Customize null display
|
2024-09-08 16:48:26 -03:00
|
|
|
} as any);
|
2024-08-20 15:30:47 -03:00
|
|
|
}
|
2024-09-11 14:41:24 -03:00
|
|
|
|
2024-01-10 15:01:02 -03:00
|
|
|
public static sleep(ms) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
2024-05-26 20:13:56 -03:00
|
|
|
}
|
2024-01-10 15:01:02 -03:00
|
|
|
|
|
|
|
public static caseInsensitive(listOrRow) {
|
2024-09-11 18:39:37 -03:00
|
|
|
// If the input is not an object or array, return it as is
|
2024-05-26 20:13:56 -03:00
|
|
|
if (!listOrRow || typeof listOrRow !== 'object') {
|
2024-01-09 17:40:48 -03:00
|
|
|
return listOrRow;
|
2024-05-26 20:13:56 -03:00
|
|
|
}
|
2024-09-11 18:39:37 -03:00
|
|
|
|
|
|
|
// Helper function to convert property names to lowercase
|
|
|
|
const lowercase = key => typeof key === 'string' ? key.toLowerCase() : key;
|
|
|
|
|
|
|
|
// Create a proxy that maps property accesses to lowercase property names
|
2024-05-26 20:13:56 -03:00
|
|
|
const createCaseInsensitiveProxy = obj => {
|
2024-09-11 18:39:37 -03:00
|
|
|
const propertiesMap = new Map(
|
|
|
|
Object.keys(obj).map(propKey => [lowercase(propKey), obj[propKey]])
|
|
|
|
);
|
|
|
|
|
2024-01-09 17:40:48 -03:00
|
|
|
const caseInsensitiveGetHandler = {
|
|
|
|
get: (target, property) => propertiesMap.get(lowercase(property))
|
|
|
|
};
|
2024-09-11 18:39:37 -03:00
|
|
|
|
2024-01-09 17:40:48 -03:00
|
|
|
return new Proxy(obj, caseInsensitiveGetHandler);
|
|
|
|
};
|
2024-09-11 18:39:37 -03:00
|
|
|
|
|
|
|
// Handle arrays by mapping each element to a case-insensitive proxy
|
2024-05-26 20:13:56 -03:00
|
|
|
if (Array.isArray(listOrRow)) {
|
2024-09-11 18:39:37 -03:00
|
|
|
return listOrRow.map(row =>
|
|
|
|
typeof row === 'object' && row !== null ? createCaseInsensitiveProxy(row) : row
|
|
|
|
);
|
2024-01-09 17:40:48 -03:00
|
|
|
} else {
|
|
|
|
return createCaseInsensitiveProxy(listOrRow);
|
|
|
|
}
|
2024-05-26 20:13:56 -03:00
|
|
|
}
|
2024-09-11 18:39:37 -03:00
|
|
|
|
2024-09-07 18:13:36 -03:00
|
|
|
public static async exists(filePath: string): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
await fs.access(filePath);
|
|
|
|
return true; // File exists
|
|
|
|
} catch (error) {
|
|
|
|
return false; // File does not exist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async copyIfNewerRecursive(src, dest) {
|
2024-09-11 21:02:19 -03:00
|
|
|
// Check if the source exists
|
2024-09-11 14:41:24 -03:00
|
|
|
if (!(await GBUtil.exists(src))) {
|
2024-09-11 21:02:19 -03:00
|
|
|
return;
|
2024-08-21 13:26:40 -03:00
|
|
|
}
|
2024-09-11 21:02:19 -03:00
|
|
|
|
|
|
|
// Check if the source is a directory
|
|
|
|
if ((await fs.stat(src)).isDirectory()) {
|
|
|
|
// Create the destination directory if it doesn't exist
|
|
|
|
if (!(await GBUtil.exists(dest))) {
|
|
|
|
await fs.mkdir(dest, { recursive: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all files and directories in the source directory
|
|
|
|
const entries = await fs.readdir(src);
|
|
|
|
|
|
|
|
for (let entry of entries) {
|
|
|
|
const srcEntry = path.join(src, entry);
|
|
|
|
const destEntry = path.join(dest, entry);
|
|
|
|
|
|
|
|
// Recursively copy each entry
|
|
|
|
await this.copyIfNewerRecursive(srcEntry, destEntry);
|
2024-08-21 13:26:40 -03:00
|
|
|
}
|
2024-08-24 00:12:50 -03:00
|
|
|
} else {
|
2024-09-11 21:02:19 -03:00
|
|
|
// Source is a file, check if we need to copy it
|
|
|
|
if (await GBUtil.exists(dest)) {
|
|
|
|
const srcStat = await fs.stat(src);
|
|
|
|
const destStat = await fs.stat(dest);
|
|
|
|
|
|
|
|
// Copy only if the source file is newer than the destination file
|
|
|
|
if (srcStat.mtime > destStat.mtime) {
|
|
|
|
await fs.cp(src, dest, { force: true });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Destination file doesn't exist, so copy it
|
|
|
|
await fs.cp(src, dest, { force: true });
|
|
|
|
}
|
2024-08-24 00:12:50 -03:00
|
|
|
}
|
|
|
|
}
|
2024-09-11 21:02:19 -03:00
|
|
|
// Check if is a tree or flat object.
|
2024-08-21 13:26:40 -03:00
|
|
|
|
2024-08-24 00:12:50 -03:00
|
|
|
public static hasSubObject(t) {
|
|
|
|
for (var key in t) {
|
|
|
|
if (!t.hasOwnProperty(key)) continue;
|
|
|
|
if (typeof t[key] === 'object') return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-09-06 15:15:42 -03:00
|
|
|
|
2024-09-07 18:13:36 -03:00
|
|
|
public static async getPdfText(data): Promise<string> {
|
2024-09-11 14:41:24 -03:00
|
|
|
const pdf = await getDocument({ data }).promise;
|
2024-09-06 15:15:42 -03:00
|
|
|
let pages = [];
|
|
|
|
|
|
|
|
for (let i = 1; i <= pdf.numPages; i++) {
|
|
|
|
const page = await pdf.getPage(i);
|
|
|
|
const textContent = await page.getTextContent();
|
|
|
|
const text = textContent.items
|
|
|
|
.map(item => item['str'])
|
2024-09-07 18:13:36 -03:00
|
|
|
.join(' ')
|
|
|
|
.replace(/\s+/g, ' '); // Optionally remove extra spaces
|
2024-09-06 15:15:42 -03:00
|
|
|
pages.push(text);
|
|
|
|
}
|
|
|
|
|
2024-09-07 18:13:36 -03:00
|
|
|
return pages.join(' ');
|
2024-09-06 15:15:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static getGBAIPath(botId, packageType = null, packageName = null) {
|
|
|
|
let gbai = `${botId}.gbai`;
|
|
|
|
if (!packageType && !packageName) {
|
|
|
|
return GBConfigService.get('DEV_GBAI') ? GBConfigService.get('DEV_GBAI') : gbai;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GBConfigService.get('DEV_GBAI')) {
|
|
|
|
gbai = GBConfigService.get('DEV_GBAI');
|
|
|
|
botId = gbai.replace(/\.[^/.]+$/, '');
|
|
|
|
return urljoin(GBConfigService.get('DEV_GBAI'), packageName ? packageName : `${botId}.${packageType}`);
|
|
|
|
} else {
|
|
|
|
return urljoin(gbai, packageName ? packageName : `${botId}.${packageType}`);
|
|
|
|
}
|
|
|
|
}
|
2024-05-26 20:13:56 -03:00
|
|
|
}
|