Skip to content

Commit

Permalink
fix(all): Fixes in production.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Mar 19, 2023
1 parent 3e68858 commit 56b6877
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 160 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"args": [
"--no-deprecation",
"--loader ts-node/esm",
"--openssl-legacy-provider",
"--require ${workspaceRoot}/suppress-node-warnings.cjs",
],
"skipFiles": [
Expand Down
Empty file modified gbot.sh
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"punycode": "2.1.1",
"puppeteer": "19.6.3",
"puppeteer-extra": "3.3.4",
"puppeteer-extra-plugin-minmax": "1.1.2",
"puppeteer-extra-plugin-stealth": "2.11.1",
"qr-scanner": "1.4.2",
"qrcode": "1.5.1",
Expand Down
45 changes: 1 addition & 44 deletions packages/basic.gblib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,50 +109,7 @@ export class GBBasicPackage implements IGBPackage {
}
public async loadBot(min: GBMinInstance): Promise<void> {
const botId = min.botId;

const opts = {
pingSendTimeout: null,
keepAliveTimeout: null,
listeners: {
unsubscribed(subscriptions: number): void {},
subscribed(subscriptions: number): void {},
disconnected(remoteId: string, connections: number): void {},
connected(remoteId: string, connections: number): void {},
messageIn(...params): void {
GBLogEx.info(min, '[IN] ' + params);
},
messageOut(...params): void {
GBLogEx.info(min, '[OUT] ' + params);
}
}
};

function getRemoteId(ctx: Koa.Context) {
return '1'; // share a single session for now, real impl could use cookies or some other meaning for HTTP sessions
}
let instances: IGBInstance[];
instances = await min.core.loadInstances();
let proxies = {};
await CollectionUtil.asyncForEach(instances, async instance => {
const proxy = {
dk: new DialogKeywords(),
wa: new WebAutomationServices(),
sys: new SystemKeywords(),
dbg: new DebuggerService(),
img: new ImageProcessingServices()
};
proxies[instance.botId] = proxy;
});

GBServer.globals.server.dk = createRpcServer(
proxies,
createKoaHttpServer(GBVMService.API_PORT, getRemoteId, { prefix: `api/v3` }),
opts
);

GBLogEx.info(min, 'API RPC HTTP Server started at http://localhost:' + GBVMService.API_PORT);

GBServer.globals.debuggers[botId] = {};
GBServer.globals.debuggers[botId] = {};
GBServer.globals.debuggers[botId].state = 0;
GBServer.globals.debuggers[botId].breaks = [];
GBServer.globals.debuggers[botId].stateInfo = 'Stopped';
Expand Down
6 changes: 3 additions & 3 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,9 @@ export class DialogKeywords {
}

private async setOption({ pid, name, value }) {
if (this.isUserSystemParam(name)) {
throw new Error(`Not possible to define ${name} as it is a reserved system param name.`);
}
// if (this.isUserSystemParam(name)) {
// throw new Error(`Not possible to define ${name} as it is a reserved system param name.`);
// }
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
const sec = new SecService();
await sec.setParam(user.userId, name, value);
Expand Down
11 changes: 6 additions & 5 deletions packages/basic.gblib/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,12 @@ export class GBVMService extends GBService {
}

public static normalizeQuotes(text: any) {
text = text.replace('¨', '"');
text = text.replace('“', '"');
text = text.replace('”', '"');
text = text.replace('‘', "'");
text = text.replace('’', "'");
text = text.replace(//gm, '"');
text = text.replace(/\“/gm, '"');
text = text.replace(/\”/gm, '"');
text = text.replace(/\‘/gm, "'");
text = text.replace(/\’/gm, "'");

return text;
}

Expand Down
27 changes: 19 additions & 8 deletions packages/basic.gblib/services/KeywordsExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

'use strict';

import { GBVMService } from "./GBVMService.js";

/**
* Image processing services of conversation to be called by BASIC.
*/
Expand Down Expand Up @@ -292,9 +294,9 @@ export class KeywordsExpressions {
keywords[i++] = [
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)\s*or talk\s*(.*)/gim,
($0, $1, $2, $3) => {
return `${$1} = await sys.find({pid: pid, handle: page, args:[${$2}])\n
if (!${$1}) {s
await dk.talk ({pid: pid, ${$3}})\n;
return `${$1} = await sys.find({pid: pid, handle: page, args:[${$2}]})\n
if (!${$1}) {
await dk.talk ({pid: pid, text: ${$3}})\n;
return -1;
}
`;
Expand Down Expand Up @@ -601,7 +603,12 @@ export class KeywordsExpressions {
keywords[i++] = [
/^\s*(talk)(\s*)(.*)/gim,
($0, $1, $2, $3) => {
if ($3.substr(0, 1) !== '"') {
$3 = GBVMService.normalizeQuotes($3);

// Uses auto quote if this is a frase with more then one word.

if (/\s/.test($3) && $3.substr(0, 1) !== '"') {

$3 = `"${$3}"`;
}
return `await dk.talk ({pid: pid, text: ${$3}})`;
Expand Down Expand Up @@ -755,15 +762,19 @@ export class KeywordsExpressions {
];

keywords[i++] = [
/^\s*save\s*(.*)\s*as\s*(.*)/gim,
/^\s*save\s*(\w+)\s*as\s*(.*)/gim,
($0, $1, $2, $3) => {
return `await sys.saveFile({pid: pid, file: ${$2}, data: ${$1}})`;
}
];

keywords[i++] = [
/^\s*(save)(\s*)(.*)/gim,
($0, $1, $2, $3) => {
return `await sys.save({pid: pid, args: [${$3}]})`;
/^\s*(save)(\s*)(.*\.xlsx)(.*)/gim,
($0, $1, $2, $3, $4) => {
$3 = $3.replace (/\'/g, "")
$3 = $3.replace (/\"/g, "")
$4 = $4.substr(2)
return `await sys.save({pid: pid,file: "${$3}" , args: [${$4}]})`;
}
];

Expand Down
28 changes: 14 additions & 14 deletions packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class SystemKeywords {
const gbaiName = DialogKeywords.getGBAIPath(min.botId);
const browser = await GBSSR.createBrowser(null);
const page = await browser.newPage();
await page.minimize();

// Includes the associated CSS related to current theme.

Expand Down Expand Up @@ -544,7 +545,7 @@ export class SystemKeywords {
}

try {
data = GBServer.globals.files[data].data;
data = GBServer.globals.files[data].data; // TODO
await client.api(`${baseUrl}/drive/root:/${path}/${file}:/content`).put(data);
} catch (error) {
if (error.code === 'itemNotFound') {
Expand All @@ -562,9 +563,8 @@ export class SystemKeywords {
* @exaple SAVE "customers.xlsx", name, email, phone, address, city, state, country
*
*/
public async save({ pid, args }): Promise<any> {
public async save({ pid,file, args }): Promise<any> {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const file = args[0];
args.shift();
GBLog.info(`BASIC: Saving '${file}' (SAVE). Args: ${args.join(',')}.`);
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
Expand All @@ -589,7 +589,7 @@ export class SystemKeywords {
const address = `A2:${this.numberToLetters(args.length - 1)}2`;
for (let index = 0; index < args.length; index++) {
let value = args[index];
if (value && this.isValidDate(value)) {
if (value && await this.isValidDate({pid, dt:value})) {
value = `'${value}`;
}
body.values[0][index] = value;
Expand Down Expand Up @@ -699,8 +699,8 @@ export class SystemKeywords {

// MAX LINES property.

let maxLines;
if (user && params && params.maxLines) {
let maxLines = 5000;
if (params && params.maxLines) {
if (params.maxLines.toString().toLowerCase() !== 'default') {
maxLines = Number.parseInt(params.maxLines).valueOf();
}
Expand All @@ -718,7 +718,7 @@ export class SystemKeywords {
page = WebAutomationServices.getPageByHandle(handle);
}

if (page['$eval'] && WebAutomationServices.isSelector(file)) {
if (handle &&page['$eval'] && WebAutomationServices.isSelector(file)) {
const container = page['frame'] ? page['frame'] : page;
const originalSelector = file;

Expand Down Expand Up @@ -857,10 +857,10 @@ export class SystemKeywords {

if (this.isValidHour(filter.value)) {
filter.dataType = fixed ? fixed : 'hourInterval';
} else if (this.isValidDate(filter.value)) {
} else if (await this.isValidDate({pid, dt: filter.value})) {
filter.value = SystemKeywords.getDateFromLocaleString(pid, filter.value, contentLocale);
filter.dataType = fixed ? fixed : 'date';
} else if (this.isValidNumber(filter.value)) {
} else if (await this.isValidNumber({pid, number: filter.value})) {
filter.value = Number.parseInt(filter.value);
filter.dataType = fixed ? fixed : 'number';
} else {
Expand Down Expand Up @@ -893,7 +893,7 @@ export class SystemKeywords {
switch (filter.dataType) {
case 'string':
const v1 = GBConversationalService.removeDiacritics(result.toLowerCase().trim());
const v2 = GBConversationalService.removeDiacritics(filter.toLowerCase().trim());
const v2 = GBConversationalService.removeDiacritics(filter.value.toLowerCase().trim());

switch (filter.operator) {
case '=':
Expand Down Expand Up @@ -1003,7 +1003,7 @@ export class SystemKeywords {
const propertyName = header[colIndex];
let value = xlRow[colIndex];
if (value && value.charAt(0) === "'") {
if (this.isValidDate(value.substr(1))) {
if (await this.isValidDate({pid, dt:value.substr(1)})) {
value = value.substr(1);
}
}
Expand Down Expand Up @@ -1650,7 +1650,7 @@ export class SystemKeywords {
const propertyName = header[colIndex];
let value = xlRow[colIndex];
if (value && value.charAt(0) === "'") {
if (this.isValidDate(value.substr(1))) {
if (await this.isValidDate({pid, dt:value.substr(1)})) {
value = value.substr(1);
}
}
Expand Down Expand Up @@ -1702,13 +1702,13 @@ export class SystemKeywords {
}
}
} else {
let args = [file];
let args = [];
let keys = Object.keys(row);
for (let j = 0; j < keys.length; j++) {
args.push(row[keys[j]]);
}

await this.save({ pid, args });
await this.save({ pid,file, args });
adds++;
}
}
Expand Down
13 changes: 8 additions & 5 deletions packages/core.gbapp/services/GBConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ import * as en from 'dotenv-extended';
* Base configuration for the server like storage.
*/
export class GBConfigService {
public static getBoolean (value: string): boolean {
public static getBoolean(value: string): boolean {
return (this.get(value) as unknown) as boolean;
}
public static getServerPort (): string {
public static getServerPort(): string {
if (process.env.PORT) {
return process.env.PORT;
}
Expand All @@ -57,7 +57,7 @@ export class GBConfigService {
return '4242';
}

public static init (): any {
public static init(): any {
try {
en.load({
encoding: 'utf8',
Expand All @@ -78,7 +78,7 @@ export class GBConfigService {
}
}

public static get (key: string): string | undefined {
public static get(key: string): string | undefined {
let value = GBConfigService.tryGet(key);

if (value === undefined) {
Expand Down Expand Up @@ -155,6 +155,9 @@ export class GBConfigService {
case 'ENABLE_SPELLING_CHECKER':
value = false;
break;
case 'DEV_GBAI':
value = undefined;
break;
default:
GBLog.warn(`Invalid key on .env file: '${key}'`);
break;
Expand All @@ -164,7 +167,7 @@ export class GBConfigService {
return value;
}

public static tryGet (key: string): any {
public static tryGet(key: string): any {
let value = process.env[`container:${key}`];
if (value === undefined) {
value = process.env[key];
Expand Down
1 change: 1 addition & 0 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ export class GBConversationalService {
}

public async translate(min: GBMinInstance, text: string, language: string): Promise<string> {

const translatorEnabled = () => {
if (min.instance.params) {
const params = JSON.parse(min.instance.params);
Expand Down
11 changes: 6 additions & 5 deletions packages/core.gbapp/services/GBLogEx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ export class GBLogEx {
*/
public static async log(instance: IGBInstance, kind: string, message: string): Promise<GuaribasLog> {
message = message ? message.substring(0, 1023) : null;
return await GuaribasLog.create(<GuaribasLog>{
instanceId: instance ? instance.instanceId : 1,
message: message,
kind: kind
});
// return await GuaribasLog.create(<GuaribasLog>{
// instanceId: instance ? instance.instanceId : 1,
// message: message,
// kind: kind
// });
return null;
}
}

0 comments on commit 56b6877

Please sign in to comment.