Skip to content

Commit

Permalink
fix(basic.gblib): Debugger improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Mar 13, 2023
1 parent 2de41ee commit 3e68858
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 33 deletions.
4 changes: 2 additions & 2 deletions boot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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()
});
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions packages/basic.gblib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
};
Expand Down Expand Up @@ -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 = {};
}
}
77 changes: 53 additions & 24 deletions packages/basic.gblib/services/DebuggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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';

Expand All @@ -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;
Expand All @@ -178,15 +176,15 @@ 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;

const conversationId = conversationsMap[botId];
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]
});
Expand Down Expand Up @@ -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] = {};
}
Expand All @@ -242,34 +239,66 @@ 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'
}
}
});

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' };
}

}
8 changes: 4 additions & 4 deletions packages/basic.gblib/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3e68858

Please sign in to comment.