Refactor code formatting and adjust minor syntax errors
Some checks are pending
GBCI / build (push) Waiting to run

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-08-25 19:32:31 -03:00
parent 27dde0f4f0
commit 2695b54460

View file

@ -177,22 +177,22 @@ export class SystemKeywords {
if (date) { if (date) {
return array return array
? array.sort((a, b) => { ? array.sort((a, b) => {
const c = new Date(a[memberName]); const c = new Date(a[memberName]);
const d = new Date(b[memberName]); const d = new Date(b[memberName]);
return c.getTime() - d.getTime(); return c.getTime() - d.getTime();
}) })
: null; : null;
} else { } else {
return array return array
? array.sort((a, b) => { ? array.sort((a, b) => {
if (a[memberName] < b[memberName]) { if (a[memberName] < b[memberName]) {
return -1; return -1;
} }
if (a[memberName] > b[memberName]) { if (a[memberName] > b[memberName]) {
return 1; return 1;
} }
return 0; return 0;
}) })
: array; : array;
} }
} }
@ -386,23 +386,19 @@ export class SystemKeywords {
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName)); const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
return { localName, url}; return { localName, url };
} catch (error) { } catch (error) {
GBLogEx.error(min, `Error converting file to PDF: ${error}`); GBLogEx.error(min, `Error converting file to PDF: ${error}`);
throw new Error('PDF conversion failed'); throw new Error('PDF conversion failed');
} }
} }
public async asPdf({ pid, data }) { public async asPdf({ pid, data }) {
let file; let file;
if (data.url) { if (data.url) {
file = await this.convertWithLibreOffice(pid, data.localName); file = await this.convertWithLibreOffice(pid, data.localName);
} } else {
else {
file = await this.renderTable(pid, data, true, false); file = await this.renderTable(pid, data, true, false);
} }
@ -827,7 +823,7 @@ export class SystemKeywords {
const minRef = min; const minRef = min;
await retry( await retry(
async (bail) => { async bail => {
const t = this.getTableFromName(tableName, minRef); const t = this.getTableFromName(tableName, minRef);
try { try {
await t.bulkCreate(rowsDest); await t.bulkCreate(rowsDest);
@ -908,7 +904,7 @@ export class SystemKeywords {
port: parseInt(process.env.DRIVE_PORT || '9000', 10), port: parseInt(process.env.DRIVE_PORT || '9000', 10),
useSSL: process.env.DRIVE_USE_SSL === 'true', useSSL: process.env.DRIVE_USE_SSL === 'true',
accessKey: process.env.DRIVE_ACCESSKEY, accessKey: process.env.DRIVE_ACCESSKEY,
secretKey: process.env.DRIVE_SECRET, secretKey: process.env.DRIVE_SECRET
}); });
const gbaiName = GBUtil.getGBAIPath(min.botId); const gbaiName = GBUtil.getGBAIPath(min.botId);
@ -1035,7 +1031,9 @@ export class SystemKeywords {
// If no row was updated, add a new row // If no row was updated, add a new row
if (!rowUpdated) { if (!rowUpdated) {
await client await client
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='A2:DX2')/insert`) .api(
`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='A2:DX2')/insert`
)
.post({}); .post({});
address = `A2:${this.numberToLetters(args.length - 1)}2`; address = `A2:${this.numberToLetters(args.length - 1)}2`;
for (let j = 0; j < args.length; j++) { for (let j = 0; j < args.length; j++) {
@ -1046,7 +1044,9 @@ export class SystemKeywords {
await retry( await retry(
async bail => { async bail => {
const result = await client const result = await client
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='${address}')`) .api(
`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='${address}')`
)
.patch(body); .patch(body);
if (result.status != 200) { if (result.status != 200) {
@ -1318,8 +1318,8 @@ export class SystemKeywords {
let res; let res;
let packagePath = GBUtil.getGBAIPath(min.botId, `gbdata`); let packagePath = GBUtil.getGBAIPath(min.botId, `gbdata`);
let csvFile = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file);
if (GBConfigService.get('GB_MODE') === 'gbcluster') { if (GBConfigService.get('GB_MODE') === 'gbcluster') {
const fileUrl = urlJoin('/', `${min.botId}.gbdata`, file); const fileUrl = urlJoin('/', `${min.botId}.gbdata`, file);
GBLogEx.info(min, `Direct data from .csv: ${fileUrl}.`); GBLogEx.info(min, `Direct data from .csv: ${fileUrl}.`);
@ -1330,7 +1330,7 @@ export class SystemKeywords {
port: parseInt(process.env.DRIVE_PORT || '9000', 10), port: parseInt(process.env.DRIVE_PORT || '9000', 10),
useSSL: process.env.DRIVE_USE_SSL === 'true', useSSL: process.env.DRIVE_USE_SSL === 'true',
accessKey: process.env.DRIVE_ACCESSKEY, accessKey: process.env.DRIVE_ACCESSKEY,
secretKey: process.env.DRIVE_SECRET, secretKey: process.env.DRIVE_SECRET
}); });
const gbaiName = GBUtil.getGBAIPath(min.botId); const gbaiName = GBUtil.getGBAIPath(min.botId);
@ -1343,9 +1343,10 @@ export class SystemKeywords {
); );
await minioClient.fGetObject(bucketName, fileUrl, localName); await minioClient.fGetObject(bucketName, fileUrl, localName);
csvFile = localName;
} }
const csvFile = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file);
const data = await fs.readFile(csvFile, 'utf8'); const data = await fs.readFile(csvFile, 'utf8');
const firstLine = data.split('\n')[0]; const firstLine = data.split('\n')[0];
@ -1869,13 +1870,9 @@ export class SystemKeywords {
const srcPath = urlJoin(root, src); const srcPath = urlJoin(root, src);
const dstPath = urlJoin(packagePath, dest); const dstPath = urlJoin(packagePath, dest);
if (path.extname(srcPath) === 'ai') { if (path.extname(srcPath) === 'ai') {
// TODO: To be done. // TODO: To be done.
} else { } else {
// Checks if the destination contains subfolders that // Checks if the destination contains subfolders that
// need to be created. // need to be created.
@ -2170,8 +2167,7 @@ export class SystemKeywords {
const url = template['@microsoft.graph.downloadUrl']; const url = template['@microsoft.graph.downloadUrl'];
const res = await fetch(url); const res = await fetch(url);
return Buffer.from(await res.arrayBuffer()); return Buffer.from(await res.arrayBuffer());
} } else if (GBConfigService.get('GB_MODE') === 'gbcluster') {
else if (GBConfigService.get('GB_MODE') === 'gbcluster') {
// GBCluster mode - using MinIO // GBCluster mode - using MinIO
const minioClient = this.createMinioClient(); const minioClient = this.createMinioClient();
const bucketName = (process.env.DRIVE_ORG_PREFIX + botId + '.gbai').toLowerCase(); const bucketName = (process.env.DRIVE_ORG_PREFIX + botId + '.gbai').toLowerCase();
@ -2179,14 +2175,16 @@ export class SystemKeywords {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const chunks: Uint8Array[] = []; const chunks: Uint8Array[] = [];
minioClient.getObject(bucketName, filePath).then(stream => { minioClient
stream.on('data', chunk => chunks.push(new Uint8Array(chunk))); .getObject(bucketName, filePath)
stream.on('end', () => resolve(Buffer.concat(chunks))); .then(stream => {
stream.on('error', reject); stream.on('data', chunk => chunks.push(new Uint8Array(chunk)));
}).catch(reject); stream.on('end', () => resolve(Buffer.concat(chunks)));
stream.on('error', reject);
})
.catch(reject);
}); });
} } else {
else {
// Default mode - direct filesystem access // Default mode - direct filesystem access
const gbdriveName = GBUtil.getGBAIPath(botId, 'gbdrive'); const gbdriveName = GBUtil.getGBAIPath(botId, 'gbdrive');
const templatePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, templateName); const templatePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, templateName);
@ -2208,8 +2206,7 @@ export class SystemKeywords {
const url = ref['@microsoft.graph.downloadUrl']; const url = ref['@microsoft.graph.downloadUrl'];
const response = await fetch(url); const response = await fetch(url);
return Buffer.from(await response.arrayBuffer()); return Buffer.from(await response.arrayBuffer());
} } else if (GBConfigService.get('GB_MODE') === 'gbcluster') {
else if (GBConfigService.get('GB_MODE') === 'gbcluster') {
const minioClient = this.createMinioClient(); const minioClient = this.createMinioClient();
const bucketName = (process.env.DRIVE_ORG_PREFIX + botId + '.gbai').toLowerCase(); const bucketName = (process.env.DRIVE_ORG_PREFIX + botId + '.gbai').toLowerCase();
const filePath = urlJoin(gbaiName, `${botId}.gbdrive`, imagePath); const filePath = urlJoin(gbaiName, `${botId}.gbdrive`, imagePath);
@ -2225,8 +2222,7 @@ export class SystemKeywords {
reject(err); reject(err);
} }
}); });
} } else {
else {
const gbdriveName = GBUtil.getGBAIPath(botId, 'gbdrive'); const gbdriveName = GBUtil.getGBAIPath(botId, 'gbdrive');
const fullPath = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, imagePath); const fullPath = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbdriveName, imagePath);
return fs.readFile(fullPath); return fs.readFile(fullPath);
@ -2239,7 +2235,7 @@ export class SystemKeywords {
port: parseInt(process.env.DRIVE_PORT || '9000', 10), port: parseInt(process.env.DRIVE_PORT || '9000', 10),
useSSL: process.env.DRIVE_USE_SSL === 'true', useSSL: process.env.DRIVE_USE_SSL === 'true',
accessKey: process.env.DRIVE_ACCESSKEY, accessKey: process.env.DRIVE_ACCESSKEY,
secretKey: process.env.DRIVE_SECRET, secretKey: process.env.DRIVE_SECRET
}); });
} }
@ -2334,10 +2330,7 @@ export class SystemKeywords {
return { localName, url, data: outputBuffer }; return { localName, url, data: outputBuffer };
} }
public screenCapture(pid) {}
public screenCapture(pid) {
}
private numberToLetters(num) { private numberToLetters(num) {
let letters = ''; let letters = '';
@ -2688,18 +2681,15 @@ export class SystemKeywords {
GBLogEx.info(min, `BlueSky Automation: ${text}.`); GBLogEx.info(min, `BlueSky Automation: ${text}.`);
} }
/** /**
*/ */
public async answer({ pid, text }) { public async answer({ pid, text }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid); const { min, user } = await DialogKeywords.getProcessInfo(pid);
const answer = await ChatServices.answerByLLM(pid, min, user, text) const answer = await ChatServices.answerByLLM(pid, min, user, text);
GBLogEx.info(min, `ANSWER ${text} TO ${answer}`); GBLogEx.info(min, `ANSWER ${text} TO ${answer}`);
return answer.answer; return answer.answer;
} }
/** /**
* HEAR description * HEAR description
* text = REWRITE description * text = REWRITE description
@ -2859,7 +2849,6 @@ export class SystemKeywords {
} }
public async getExtensionInfo(ext: any): Promise<any> { public async getExtensionInfo(ext: any): Promise<any> {
// TODO: Load exts. // TODO: Load exts.
let array = []; // exts.filter((v, i, a) => a[i]['extension'] === ext); let array = []; // exts.filter((v, i, a) => a[i]['extension'] === ext);
@ -3120,17 +3109,16 @@ export class SystemKeywords {
} }
public async convertAI2HTML(aiFilePath) { public async convertAI2HTML(aiFilePath) {
// Convert the AI file to HTML and assets // Convert the AI file to HTML and assets
const result = await ai2html.convertFile(aiFilePath, { const result = await ai2html.convertFile(aiFilePath, {
outputFormat: 'html', outputFormat: 'html',
outputWriteMethod: 'write-file', outputWriteMethod: 'write-file',
outputPath: path.dirname(aiFilePath), outputPath: path.dirname(aiFilePath),
useDocumentSettings: true, useDocumentSettings: true
}); });
// Get the generated HTML file path // Get the generated HTML file path
const htmlFilePath = result.outputFiles.find((file) => file.endsWith('.html')).filePath; const htmlFilePath = result.outputFiles.find(file => file.endsWith('.html')).filePath;
// Read the HTML content // Read the HTML content
const htmlContent = await fs.readFile(htmlFilePath, 'utf8'); const htmlContent = await fs.readFile(htmlFilePath, 'utf8');
@ -3142,10 +3130,8 @@ export class SystemKeywords {
await fs.writeFile(cacheFilePath, htmlContent); await fs.writeFile(cacheFilePath, htmlContent);
return cacheFilePath; return cacheFilePath;
} }
public async refreshDataSourceCache({ pid, connectionName }) { public async refreshDataSourceCache({ pid, connectionName }) {
const { min, user, params, step } = await DialogKeywords.getProcessInfo(pid); const { min, user, params, step } = await DialogKeywords.getProcessInfo(pid);
@ -3171,25 +3157,25 @@ export class SystemKeywords {
const tables = await GBUtil.listTables(dialect, con); const tables = await GBUtil.listTables(dialect, con);
// Function to map source database datatypes to SQLite-compatible datatypes // Function to map source database datatypes to SQLite-compatible datatypes
const mapToSQLiteType = (columnType) => { const mapToSQLiteType = columnType => {
const typeMapping = { const typeMapping = {
'VARCHAR': DataTypes.STRING, VARCHAR: DataTypes.STRING,
'CHAR': DataTypes.STRING, CHAR: DataTypes.STRING,
'TEXT': DataTypes.TEXT, TEXT: DataTypes.TEXT,
'TINYINT': DataTypes.INTEGER, TINYINT: DataTypes.INTEGER,
'SMALLINT': DataTypes.INTEGER, SMALLINT: DataTypes.INTEGER,
'MEDIUMINT': DataTypes.INTEGER, MEDIUMINT: DataTypes.INTEGER,
'INT': DataTypes.INTEGER, INT: DataTypes.INTEGER,
'INTEGER': DataTypes.INTEGER, INTEGER: DataTypes.INTEGER,
'BIGINT': DataTypes.BIGINT, BIGINT: DataTypes.BIGINT,
'FLOAT': DataTypes.FLOAT, FLOAT: DataTypes.FLOAT,
'DOUBLE': DataTypes.DOUBLE, DOUBLE: DataTypes.DOUBLE,
'DECIMAL': DataTypes.DECIMAL, DECIMAL: DataTypes.DECIMAL,
'DATE': DataTypes.DATE, DATE: DataTypes.DATE,
'DATETIME': DataTypes.DATE, DATETIME: DataTypes.DATE,
'TIMESTAMP': DataTypes.DATE, TIMESTAMP: DataTypes.DATE,
'BLOB': DataTypes.BLOB, BLOB: DataTypes.BLOB,
'BOOLEAN': DataTypes.BOOLEAN, BOOLEAN: DataTypes.BOOLEAN
// Add more mappings as needed // Add more mappings as needed
}; };