2022-11-11 21:35:05 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__,\| (˅) |( (_) ) |
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| 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. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| 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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2022-11-14 16:09:05 -03:00
|
|
|
import { GBLog, GBMinInstance } from 'botlib';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBServer } from '../../../src/app.js';
|
|
|
|
import Fs from 'fs';
|
2023-03-13 14:20:18 -03:00
|
|
|
import SwaggerClient from 'swagger-client';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { spawn } from 'child_process';
|
2023-07-25 08:00:43 -03:00
|
|
|
import { CodeServices } from '../../gpt.gblib/services/CodeServices.js';
|
2022-11-11 21:35:05 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Web Automation services of conversation to be called by BASIC.
|
|
|
|
*/
|
|
|
|
export class DebuggerService {
|
2022-11-13 22:56:09 -03:00
|
|
|
static systemVariables = [
|
|
|
|
'AggregateError',
|
|
|
|
'Array',
|
|
|
|
'ArrayBuffer',
|
|
|
|
'Atomics',
|
|
|
|
'BigInt',
|
|
|
|
'BigInt64Array',
|
|
|
|
'BigUint64Array',
|
|
|
|
'Boolean',
|
|
|
|
'DataView',
|
|
|
|
'Date',
|
|
|
|
'Error',
|
|
|
|
'EvalError',
|
|
|
|
'FinalizationRegistry',
|
|
|
|
'Float32Array',
|
|
|
|
'Float64Array',
|
|
|
|
'Function',
|
|
|
|
'Headers',
|
|
|
|
'Infinity',
|
|
|
|
'Int16Array',
|
|
|
|
'Int32Array',
|
|
|
|
'Int8Array',
|
|
|
|
'Intl',
|
|
|
|
'JSON',
|
|
|
|
'Map',
|
|
|
|
'Math',
|
|
|
|
'NaN',
|
|
|
|
'Number',
|
|
|
|
'Object',
|
|
|
|
'Promise',
|
|
|
|
'Proxy',
|
|
|
|
'RangeError',
|
|
|
|
'ReferenceError',
|
|
|
|
'Reflect',
|
|
|
|
'RegExp',
|
|
|
|
'Request',
|
|
|
|
'Response',
|
|
|
|
'Set',
|
|
|
|
'SharedArrayBuffer',
|
|
|
|
'String',
|
|
|
|
'Symbol',
|
|
|
|
'SyntaxError',
|
|
|
|
'TypeError',
|
|
|
|
'URIError',
|
|
|
|
'Uint16Array',
|
|
|
|
'Uint32Array',
|
|
|
|
'Uint8Array',
|
|
|
|
'Uint8ClampedArray',
|
|
|
|
'VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL',
|
|
|
|
'WeakMap',
|
|
|
|
'WeakRef',
|
|
|
|
'WeakSet',
|
|
|
|
'WebAssembly',
|
|
|
|
'__defineGetter__',
|
|
|
|
'__defineSetter__',
|
|
|
|
'__lookupGetter__',
|
|
|
|
'__lookupSetter__',
|
|
|
|
'__proto__',
|
|
|
|
'clearImmediate',
|
|
|
|
'clearInterval',
|
|
|
|
'clearTimeout',
|
|
|
|
'console',
|
|
|
|
'constructor',
|
|
|
|
'decodeURI',
|
|
|
|
'decodeURIComponent',
|
|
|
|
'dss',
|
|
|
|
'encodeURI',
|
|
|
|
'encodeURIComponent',
|
|
|
|
'escape',
|
|
|
|
'eval',
|
|
|
|
'fetch',
|
|
|
|
'global',
|
|
|
|
'globalThis',
|
|
|
|
'hasOwnProperty',
|
|
|
|
'isFinite',
|
|
|
|
'isNaN',
|
|
|
|
'isPrototypeOf',
|
|
|
|
'parseFloat',
|
|
|
|
'parseInt',
|
|
|
|
'process',
|
|
|
|
'propertyIsEnumerable',
|
|
|
|
'setImmediate',
|
|
|
|
'setInterval',
|
|
|
|
'setTimeout',
|
|
|
|
'toLocaleString',
|
|
|
|
'toString',
|
|
|
|
'undefined',
|
|
|
|
'unescape',
|
|
|
|
'valueOf'
|
|
|
|
];
|
2022-11-11 21:35:05 -03:00
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
public async setBreakpoint({ botId, line }) {
|
2022-11-13 23:38:04 -03:00
|
|
|
GBLog.info(`BASIC: Enabled breakpoint for ${botId} on ${line}.`);
|
2022-11-12 21:33:45 -03:00
|
|
|
GBServer.globals.debuggers[botId].breaks.push(Number.parseInt(line));
|
2022-11-11 21:35:05 -03:00
|
|
|
}
|
|
|
|
|
2023-07-25 08:00:43 -03:00
|
|
|
public async refactor({ botId, code, change }) {
|
|
|
|
const service = new CodeServices();
|
|
|
|
return await service.refactor(code, change);
|
|
|
|
}
|
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
public async resume({ botId }) {
|
2022-11-13 23:11:52 -03:00
|
|
|
if (GBServer.globals.debuggers[botId].state === 2) {
|
|
|
|
const client = GBServer.globals.debuggers[botId].client;
|
|
|
|
await client.Debugger.resume();
|
|
|
|
GBServer.globals.debuggers[botId].state = 1;
|
2022-11-19 23:34:58 -03:00
|
|
|
GBServer.globals.debuggers[botId].stateInfo = 'Running (Debug)';
|
|
|
|
return { status: 'OK' };
|
2022-11-13 23:11:52 -03:00
|
|
|
} else {
|
|
|
|
const error = 'Invalid call to resume and state not being debug(2).';
|
2022-11-19 23:34:58 -03:00
|
|
|
return { error: error };
|
2022-11-13 23:11:52 -03:00
|
|
|
}
|
2022-11-11 21:35:05 -03:00
|
|
|
}
|
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
public async stop({ botId }) {
|
2022-11-13 22:56:09 -03:00
|
|
|
GBServer.globals.debuggers[botId].state = 0;
|
2022-11-19 23:34:58 -03:00
|
|
|
GBServer.globals.debuggers[botId].stateInfo = 'Stopped';
|
2022-11-14 16:09:05 -03:00
|
|
|
|
|
|
|
const kill = ref => {
|
|
|
|
spawn('sh', ['-c', `pkill -9 -f ${ref}`]);
|
|
|
|
};
|
|
|
|
|
|
|
|
kill(GBServer.globals.debuggers[botId].childProcess);
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
return { status: 'OK' };
|
2022-11-11 21:35:05 -03:00
|
|
|
}
|
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
public async step({ botId }) {
|
2022-11-13 22:56:09 -03:00
|
|
|
if (GBServer.globals.debuggers[botId].state === 2) {
|
2022-11-19 23:34:58 -03:00
|
|
|
GBServer.globals.debuggers[botId].stateInfo = 'Break';
|
2022-11-13 22:56:09 -03:00
|
|
|
const client = GBServer.globals.debuggers[botId].client;
|
2022-11-13 23:11:52 -03:00
|
|
|
await client.Debugger.stepOver();
|
2022-11-19 23:34:58 -03:00
|
|
|
return { status: 'OK' };
|
2022-11-13 22:56:09 -03:00
|
|
|
} else {
|
2022-11-13 23:11:52 -03:00
|
|
|
const error = 'Invalid call to stepOver and state not being debug(2).';
|
2022-11-19 23:34:58 -03:00
|
|
|
return { error: error };
|
2022-11-13 22:56:09 -03:00
|
|
|
}
|
2022-11-11 21:35:05 -03:00
|
|
|
}
|
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
public async getContext({ botId }) {
|
2023-03-04 16:27:25 -03:00
|
|
|
const conversationsMap = GBServer.globals.debuggers[botId].conversationsMap;
|
|
|
|
const watermarkMap = GBServer.globals.debuggers[botId].watermarkMap;
|
|
|
|
|
|
|
|
const conversationId = conversationsMap[botId];
|
2022-11-12 17:17:14 -03:00
|
|
|
let messages = [];
|
2023-03-04 16:27:25 -03:00
|
|
|
const client = GBServer.globals.debuggers[botId].client;
|
|
|
|
if (client) {
|
2023-03-13 14:20:18 -03:00
|
|
|
const response = await client.apis.Conversations.Conversations_GetActivities({
|
2022-11-13 22:56:09 -03:00
|
|
|
conversationId: conversationId,
|
2023-03-04 16:27:25 -03:00
|
|
|
watermark: watermarkMap[botId]
|
2022-11-13 22:56:09 -03:00
|
|
|
});
|
2023-03-04 16:27:25 -03:00
|
|
|
watermarkMap[botId] = response.obj.watermark;
|
2022-11-13 22:56:09 -03:00
|
|
|
let activities = response.obj.activites;
|
|
|
|
|
|
|
|
if (activities && activities.length) {
|
|
|
|
activities = activities.filter(m => m.from.id === botId && m.type === 'message');
|
|
|
|
if (activities.length) {
|
|
|
|
activities.forEach(activity => {
|
|
|
|
messages.push({ text: activity.text });
|
|
|
|
GBLog.info(`Debugger sending text to API: ${activity.text}`);
|
|
|
|
});
|
|
|
|
}
|
2022-11-12 17:17:14 -03:00
|
|
|
}
|
|
|
|
}
|
2022-11-12 21:33:45 -03:00
|
|
|
|
2022-11-13 22:56:09 -03:00
|
|
|
let messagesText = messages.join('\n');
|
|
|
|
|
|
|
|
return {
|
2022-11-13 23:11:52 -03:00
|
|
|
status: 'OK',
|
2022-11-13 22:56:09 -03:00
|
|
|
state: GBServer.globals.debuggers[botId].state,
|
2022-11-19 23:34:58 -03:00
|
|
|
messages: messagesText,
|
2022-11-13 23:38:04 -03:00
|
|
|
scope: GBServer.globals.debuggers[botId].scope,
|
|
|
|
scopeInfo: GBServer.globals.debuggers[botId].stateInfo
|
2022-11-13 22:56:09 -03:00
|
|
|
};
|
2022-11-12 17:17:14 -03:00
|
|
|
}
|
2022-11-11 21:35:05 -03:00
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
public async start({ botId, botApiKey, scriptName }) {
|
2023-03-04 16:27:25 -03:00
|
|
|
const conversationsMap = GBServer.globals.debuggers[botId].conversationsMap;
|
|
|
|
|
2022-11-13 23:11:52 -03:00
|
|
|
let error;
|
2022-11-19 23:34:58 -03:00
|
|
|
if (!GBServer.globals.debuggers[botId]) {
|
|
|
|
GBServer.globals.debuggers[botId] = {};
|
2022-11-14 16:09:05 -03:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
if (!scriptName) {
|
2022-11-14 16:09:05 -03:00
|
|
|
scriptName = 'start';
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:56:09 -03:00
|
|
|
if (GBServer.globals.debuggers[botId].state === 1) {
|
2022-11-19 23:34:58 -03:00
|
|
|
error = `Cannot DEBUG an already running process. ${botId}`;
|
|
|
|
return { error: error };
|
2022-11-13 22:56:09 -03:00
|
|
|
} else if (GBServer.globals.debuggers[botId].state === 2) {
|
|
|
|
GBLog.info(`BASIC: Releasing execution ${botId} in DEBUG mode.`);
|
2022-11-19 23:34:58 -03:00
|
|
|
await this.resume({ botId });
|
|
|
|
return { status: 'OK' };
|
2022-11-13 22:56:09 -03:00
|
|
|
} else {
|
|
|
|
GBLog.info(`BASIC: Running ${botId} in DEBUG mode.`);
|
|
|
|
GBServer.globals.debuggers[botId].state = 1;
|
2022-11-19 23:34:58 -03:00
|
|
|
GBServer.globals.debuggers[botId].stateInfo = 'Running (Debug)';
|
2022-11-13 22:56:09 -03:00
|
|
|
|
|
|
|
let min: GBMinInstance = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0];
|
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
const client = await new SwaggerClient({
|
2022-11-18 22:39:14 -03:00
|
|
|
spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')),
|
2023-03-13 14:20:18 -03:00
|
|
|
requestInterceptor: req => {
|
|
|
|
req.headers['Authorization'] = `Bearer ${min.instance.webchatKey}`;
|
|
|
|
}
|
2022-11-13 22:56:09 -03:00
|
|
|
});
|
2023-03-13 14:20:18 -03:00
|
|
|
GBServer.globals.debuggers[botId].client = client;
|
|
|
|
const response = await client.apis.Conversations.Conversations_StartConversation();
|
2022-11-13 22:56:09 -03:00
|
|
|
const conversationId = response.obj.conversationId;
|
2023-03-13 14:20:18 -03:00
|
|
|
GBServer.globals.debuggers[botId].conversationId = conversationId;
|
2022-11-13 22:56:09 -03:00
|
|
|
|
2023-03-13 14:20:18 -03:00
|
|
|
client.apis.Conversations.Conversations_PostActivity({
|
2022-11-13 22:56:09 -03:00
|
|
|
conversationId: conversationId,
|
|
|
|
activity: {
|
|
|
|
textFormat: 'plain',
|
|
|
|
text: `/calldbg ${scriptName}`,
|
|
|
|
type: 'message',
|
|
|
|
from: {
|
2023-03-13 14:20:18 -03:00
|
|
|
id: 'word',
|
|
|
|
name: 'word'
|
2022-11-13 22:56:09 -03:00
|
|
|
}
|
2022-11-11 21:35:05 -03:00
|
|
|
}
|
2022-11-13 22:56:09 -03:00
|
|
|
});
|
2022-11-13 23:11:52 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
return { status: 'OK' };
|
2022-11-13 22:56:09 -03:00
|
|
|
}
|
2022-11-11 21:35:05 -03:00
|
|
|
}
|
2023-03-13 14:20:18 -03:00
|
|
|
|
|
|
|
public async sendMessage({ botId, botApiKey, text }) {
|
|
|
|
const conversationsMap = GBServer.globals.debuggers[botId].conversationsMap;
|
|
|
|
|
|
|
|
let error;
|
|
|
|
if (!GBServer.globals.debuggers[botId]) {
|
|
|
|
GBServer.globals.debuggers[botId] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GBServer.globals.debuggers[botId].state != 1) {
|
|
|
|
error = `Cannot sendMessage to an stopped process. ${botId}`;
|
|
|
|
return { error: error };
|
|
|
|
}
|
|
|
|
|
|
|
|
let min: GBMinInstance = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0];
|
|
|
|
|
|
|
|
const client = GBServer.globals.debuggers[botId].client;
|
|
|
|
const conversationId = GBServer.globals.debuggers[botId].conversationId;
|
|
|
|
|
|
|
|
client.apis.Conversations.Conversations_PostActivity({
|
|
|
|
conversationId: conversationId,
|
|
|
|
activity: {
|
|
|
|
textFormat: 'plain',
|
|
|
|
text: text,
|
|
|
|
type: 'message',
|
|
|
|
from: {
|
|
|
|
id: 'word',
|
|
|
|
name: 'word'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return { status: 'OK' };
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:56:09 -03:00
|
|
|
}
|