botserver/packages/basic.gblib/services/DebuggerService.ts

221 lines
8.4 KiB
TypeScript
Raw Normal View History

2022-11-11 21:35:05 -03:00
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__,\| (˅) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
2024-04-20 17:24:00 -03:00
| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. |
2022-11-11 21:35:05 -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-04-20 17:24:00 -03:00
| "General Bots" is a registered trademark of pragmatismo.cloud. |
2022-11-11 21:35:05 -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. |
| |
\*****************************************************************************/
'use strict';
2022-11-14 16:09:05 -03:00
import { GBLog, GBMinInstance } from 'botlib';
import { GBServer } from '../../../src/app.js';
import Fs from 'fs';
import SwaggerClient from 'swagger-client';
import { spawn } from 'child_process';
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 {
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
}
public async refactor({ botId, code, change }) {
const service = new CodeServices();
return await service.refactor(code, change);
}
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
}
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
}
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
}
public async getContext({ botId }) {
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 = [];
const client = GBServer.globals.debuggers[botId].client;
if (client) {
const response = await client.apis.Conversations.Conversations_GetActivities({
2022-11-13 22:56:09 -03:00
conversationId: conversationId,
watermark: watermarkMap[botId]
2022-11-13 22:56:09 -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
public async start({ botId, botApiKey, scriptName }) {
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];
const client = await new SwaggerClient({
spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')),
requestInterceptor: req => {
req.headers['Authorization'] = `Bearer ${min.instance.webchatKey}`;
}
2022-11-13 22:56:09 -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;
GBServer.globals.debuggers[botId].conversationId = conversationId;
2022-11-13 22:56:09 -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: {
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
}
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
}