From 3e68858bb09d7d5b09a2f7cfbfe6e6c964950fd1 Mon Sep 17 00:00:00 2001 From: rodrigorodriguez Date: Mon, 13 Mar 2023 14:20:18 -0300 Subject: [PATCH] fix(basic.gblib): Debugger improvements. --- boot.mjs | 4 +- package.json | 1 + packages/basic.gblib/index.ts | 8 +- .../basic.gblib/services/DebuggerService.ts | 77 +++++++++++++------ packages/basic.gblib/services/GBVMService.ts | 8 +- 5 files changed, 65 insertions(+), 33 deletions(-) diff --git a/boot.mjs b/boot.mjs index 64130510..74c14c04 100644 --- a/boot.mjs +++ b/boot.mjs @@ -13,7 +13,7 @@ console.log(`██ █ ███ █ █ ██ ██ ██ console.log(`██ ███ ████ █ ██ █ ████ █████ ██████ ██ ████ █ █ █ ██ `); console.log(`██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ `); console.log(` █████ █████ █ ███ █████ ██ ██ ██ ██ ██████ ████ █████ █ ███ 3.0`); -console.log(` botserver@${pjson.version}, botlib@${pjson.dependencies.botlib}, botbuilder@${pjson.dependencies.botbuilder}, node@${process.version.replace('v', '')}, ${process.platform} ${process.arch}`); +process.stdout.write(` botserver@${pjson.version}, botlib@${pjson.dependencies.botlib}, botbuilder@${pjson.dependencies.botbuilder}, node@${process.version.replace('v', '')}, ${process.platform} ${process.arch}`); var now = () => { return new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + ' UTC'; @@ -22,7 +22,7 @@ var __dirname = process.env.PWD || process.cwd(); try { var run = () => { import('./dist/src/app.js').then((gb)=> { - console.log(``); + console.log(`\n`); gb.GBServer.run() }); }; diff --git a/package.json b/package.json index 9b7a9ea5..ca81449c 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "@google-cloud/pubsub": "3.2.1", "@google-cloud/translate": "7.0.4", "@hubspot/api-client": "7.1.2", + "@koa/cors": "4.0.0", "@microsoft/microsoft-graph-client": "3.0.4", "@nlpjs/basic": "4.26.1", "@nosferatu500/textract": "3.1.2", diff --git a/packages/basic.gblib/index.ts b/packages/basic.gblib/index.ts index ba5f791f..95612c7d 100644 --- a/packages/basic.gblib/index.ts +++ b/packages/basic.gblib/index.ts @@ -45,6 +45,7 @@ import { WebAutomationServices } from './services/WebAutomationServices.js'; import { ImageProcessingServices } from './services/ImageProcessingServices.js'; import { DebuggerService } from './services/DebuggerService.js'; import Koa from 'koa'; +import cors from '@koa/cors'; import { createRpcServer } from '@push-rpc/core'; import { createHttpKoaMiddleware } from '@push-rpc/http'; import { HttpServerOptions } from '@push-rpc/http/dist/server.js'; @@ -63,6 +64,7 @@ export function createKoaHttpServer( const { onError, onConnection, middleware } = createHttpKoaMiddleware(getRemoteId, opts); const app = new Koa(); + app.use(cors({ origin: '*' })); app.use(koaBody.koaBody({ multipart: true })); app.use(middleware); const server = app.listen(port); @@ -117,10 +119,10 @@ export class GBBasicPackage implements IGBPackage { disconnected(remoteId: string, connections: number): void {}, connected(remoteId: string, connections: number): void {}, messageIn(...params): void { - GBLogEx.info(min, 'API IN' + params); + GBLogEx.info(min, '[IN] ' + params); }, messageOut(...params): void { - GBLogEx.info(min, 'API OUT ' + params); + GBLogEx.info(min, '[OUT] ' + params); } } }; @@ -156,7 +158,7 @@ export class GBBasicPackage implements IGBPackage { GBServer.globals.debuggers[botId].stateInfo = 'Stopped'; GBServer.globals.debuggers[botId].childProcess = null; GBServer.globals.debuggers[botId].client = null; - GBServer.globals.debuggers[botId].conversationsMap = {}; + GBServer.globals.debuggers[botId].conversationId = null; GBServer.globals.debuggers[botId].watermarkMap = {}; } } diff --git a/packages/basic.gblib/services/DebuggerService.ts b/packages/basic.gblib/services/DebuggerService.ts index 2603d486..fcccb01e 100644 --- a/packages/basic.gblib/services/DebuggerService.ts +++ b/packages/basic.gblib/services/DebuggerService.ts @@ -34,10 +34,8 @@ import { GBLog, GBMinInstance } from 'botlib'; import { GBServer } from '../../../src/app.js'; -import { GuaribasUser } from '../../security.gbapp/models/index.js'; -import { DialogKeywords } from './DialogKeywords.js'; import Fs from 'fs'; -import Swagger from 'swagger-client'; +import SwaggerClient from 'swagger-client'; import { spawn } from 'child_process'; /** @@ -135,12 +133,12 @@ export class DebuggerService { 'valueOf' ]; - public async breakpoint ({ botId, line }) { + public async setBreakpoint({ botId, line }) { GBLog.info(`BASIC: Enabled breakpoint for ${botId} on ${line}.`); GBServer.globals.debuggers[botId].breaks.push(Number.parseInt(line)); } - public async resume ({ botId }) { + public async resume({ botId }) { if (GBServer.globals.debuggers[botId].state === 2) { const client = GBServer.globals.debuggers[botId].client; await client.Debugger.resume(); @@ -153,7 +151,7 @@ export class DebuggerService { } } - public async stop ({ botId }) { + public async stop({ botId }) { GBServer.globals.debuggers[botId].state = 0; GBServer.globals.debuggers[botId].stateInfo = 'Stopped'; @@ -166,7 +164,7 @@ export class DebuggerService { return { status: 'OK' }; } - public async step ({ botId }) { + public async step({ botId }) { if (GBServer.globals.debuggers[botId].state === 2) { GBServer.globals.debuggers[botId].stateInfo = 'Break'; const client = GBServer.globals.debuggers[botId].client; @@ -178,7 +176,7 @@ export class DebuggerService { } } - public async context ({ botId }) { + public async getContext({ botId }) { const conversationsMap = GBServer.globals.debuggers[botId].conversationsMap; const watermarkMap = GBServer.globals.debuggers[botId].watermarkMap; @@ -186,7 +184,7 @@ export class DebuggerService { let messages = []; const client = GBServer.globals.debuggers[botId].client; if (client) { - const response = await client.Conversations.Conversations_GetActivities({ + const response = await client.apis.Conversations.Conversations_GetActivities({ conversationId: conversationId, watermark: watermarkMap[botId] }); @@ -215,11 +213,10 @@ export class DebuggerService { }; } - public async getRunning ({ botId, botApiKey, scriptName }) { + public async start({ botId, botApiKey, scriptName }) { const conversationsMap = GBServer.globals.debuggers[botId].conversationsMap; let error; - botId = botId[0]; if (!GBServer.globals.debuggers[botId]) { GBServer.globals.debuggers[botId] = {}; } @@ -242,29 +239,26 @@ export class DebuggerService { let min: GBMinInstance = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0]; - GBServer.globals.debuggers[botId].client = await new Swagger({ + const client = await new SwaggerClient({ spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')), - usePromise: true + requestInterceptor: req => { + req.headers['Authorization'] = `Bearer ${min.instance.webchatKey}`; + } }); - const client = GBServer.globals.debuggers[botId].client; - client.clientAuthorizations.add( - 'AuthorizationBotConnector', - new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${min.instance.webchatKey}`, 'header') - ); - const response = await client.Conversations.Conversations_StartConversation(); + GBServer.globals.debuggers[botId].client = client; + const response = await client.apis.Conversations.Conversations_StartConversation(); const conversationId = response.obj.conversationId; - conversationsMap[botId] = conversationId; - GBServer.globals.debugConversationId = conversationId; + GBServer.globals.debuggers[botId].conversationId = conversationId; - client.Conversations.Conversations_PostActivity({ + client.apis.Conversations.Conversations_PostActivity({ conversationId: conversationId, activity: { textFormat: 'plain', text: `/calldbg ${scriptName}`, type: 'message', from: { - id: 'test', - name: 'test' + id: 'word', + name: 'word' } } }); @@ -272,4 +266,39 @@ export class DebuggerService { return { status: 'OK' }; } } + + 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' }; + } + } diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index e07599f6..0816fcd3 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -212,13 +212,13 @@ export class GBVMService extends GBService { let url; url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/dk'; - const dk = (await createRpcClient(0, async () => createHttpClient(url))).remote; + const dk = (await createRpcClient(0, () => createHttpClient(url))).remote; url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/sys'; - const sys = (await createRpcClient(0, async () => createHttpClient(url))).remote; + const sys = (await createRpcClient(0, () => createHttpClient(url))).remote; url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/wa'; - const wa = (await createRpcClient(0, async () => createHttpClient(url))).remote; + const wa = (await createRpcClient(0, () => createHttpClient(url))).remote; url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/img'; - const img = (await createRpcClient(0, async () => createHttpClient(url))).remote; + const img = (await createRpcClient(0, () => createHttpClient(url))).remote; // Unmarshalls Local variables from server VM.