@codebolt/codeboltjs 1.1.40 → 1.1.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -65,7 +65,7 @@ declare class Codebolt {
65
65
  };
66
66
  chat: {
67
67
  getChatHistory: () => Promise<import("@codebolt/types").ChatMessage[]>;
68
- userMessageListener: () => {
68
+ onActionMessage: () => {
69
69
  [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: string | symbol, ...args: any[]): void;
70
70
  addListener<K_1>(eventName: string | symbol, listener: (...args: any[]) => void): any;
71
71
  on<K_2>(eventName: string | symbol, listener: (...args: any[]) => void): any;
@@ -150,7 +150,7 @@ declare class Codebolt {
150
150
  click: (id: string) => Promise<unknown>;
151
151
  type: (id: string, text: string) => Promise<unknown>;
152
152
  enter: () => void;
153
- crawl: () => void;
153
+ crawl: (query: string) => Promise<unknown>;
154
154
  };
155
155
  search: {
156
156
  init: (engine?: string) => void;
package/modules/chat.d.ts CHANGED
@@ -19,7 +19,7 @@ declare const cbchat: {
19
19
  * Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
20
20
  * @returns {EventEmitter} The event emitter used for emitting custom events.
21
21
  */
22
- userMessageListener: () => CustomEventEmitter | undefined;
22
+ onActionMessage: () => CustomEventEmitter | undefined;
23
23
  /**
24
24
  * Sends a message through the WebSocket connection.
25
25
  * @param {string} message - The message to be sent.
package/modules/chat.js CHANGED
@@ -37,13 +37,19 @@ const cbchat = {
37
37
  * Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
38
38
  * @returns {EventEmitter} The event emitter used for emitting custom events.
39
39
  */
40
- userMessageListener: () => {
40
+ onActionMessage: () => {
41
41
  if (!websocket_1.default.getWebsocket)
42
42
  return;
43
43
  websocket_1.default.getWebsocket.on('message', (data) => {
44
44
  const response = JSON.parse(data);
45
45
  if (response.type === "messageResponse") {
46
- eventEmitter.emit("userMessage", response.response);
46
+ // Pass a callback function as an argument to the emit method
47
+ eventEmitter.emit("userMessage", response, (message) => {
48
+ console.log("Callback function invoked with message:", message);
49
+ websocket_1.default.getWebsocket.send(JSON.stringify({
50
+ "type": "processStoped"
51
+ }));
52
+ });
47
53
  }
48
54
  });
49
55
  return eventEmitter;
@@ -40,6 +40,6 @@ declare const cbcrawler: {
40
40
  /**
41
41
  * Initiates a crawl process.
42
42
  */
43
- crawl: () => void;
43
+ crawl: (query: string) => Promise<unknown>;
44
44
  };
45
45
  export default cbcrawler;
@@ -102,11 +102,22 @@ const cbcrawler = {
102
102
  /**
103
103
  * Initiates a crawl process.
104
104
  */
105
- crawl: () => {
106
- websocket_1.default.getWebsocket.send(JSON.stringify({
107
- "type": "crawlerEvent",
108
- action: 'crawl'
109
- }));
105
+ crawl: (query) => {
106
+ return new Promise((resolve, reject) => {
107
+ websocket_1.default.getWebsocket.send(JSON.stringify({
108
+ "type": "crawlerEvent",
109
+ "action": 'crawl',
110
+ "message": {
111
+ query
112
+ }
113
+ }));
114
+ websocket_1.default.getWebsocket.on('message', (data) => {
115
+ const response = JSON.parse(data);
116
+ if (response.type === "crawlResponse") {
117
+ resolve(response); // Resolve the Promise with the response data
118
+ }
119
+ });
120
+ });
110
121
  }
111
122
  };
112
123
  exports.default = cbcrawler;
@@ -8,6 +8,7 @@ declare class cbws {
8
8
  * Constructs a new cbws instance and initializes the WebSocket connection.
9
9
  */
10
10
  constructor();
11
+ private getUniqueConnectionId;
11
12
  /**
12
13
  * Initializes the WebSocket by setting up event listeners and returning a promise that resolves
13
14
  * when the WebSocket connection is successfully opened.
@@ -4,6 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const ws_1 = __importDefault(require("ws"));
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const js_yaml_1 = __importDefault(require("js-yaml"));
7
9
  /**
8
10
  * Class representing a WebSocket connection.
9
11
  */
@@ -12,11 +14,24 @@ class cbws {
12
14
  * Constructs a new cbws instance and initializes the WebSocket connection.
13
15
  */
14
16
  constructor() {
15
- this.websocket = new ws_1.default("ws://localhost:12345/codebolt");
17
+ const uniqueConnectionId = this.getUniqueConnectionId();
18
+ console.log(uniqueConnectionId);
19
+ this.websocket = new ws_1.default(`ws://localhost:12345/codebolt?id=${uniqueConnectionId}`);
16
20
  this.initializeWebSocket().catch(error => {
17
21
  console.error("WebSocket connection failed:", error);
18
22
  });
19
23
  }
24
+ getUniqueConnectionId() {
25
+ try {
26
+ let fileContents = fs_1.default.readFileSync('./codebotagent.yml', 'utf8');
27
+ let data = js_yaml_1.default.load(fileContents);
28
+ return data.uniqueConnectionId;
29
+ }
30
+ catch (e) {
31
+ console.error('Unable to locate codebotagent.yml file.');
32
+ return '';
33
+ }
34
+ }
20
35
  /**
21
36
  * Initializes the WebSocket by setting up event listeners and returning a promise that resolves
22
37
  * when the WebSocket connection is successfully opened.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebolt/codeboltjs",
3
- "version": "1.1.40",
3
+ "version": "1.1.41",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "",
@@ -26,6 +26,7 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/events": "^3.0.3",
29
+ "@types/js-yaml": "^4.0.9",
29
30
  "@types/ws": "^8.5.10",
30
31
  "jest": "^29.7.0",
31
32
  "jest-serial-runner": "^1.2.1",
@@ -37,12 +37,18 @@ const cbchat = {
37
37
  * Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
38
38
  * @returns {EventEmitter} The event emitter used for emitting custom events.
39
39
  */
40
- userMessageListener: () => {
40
+ onActionMessage: () => {
41
41
  if (!cbws.getWebsocket) return;
42
42
  cbws.getWebsocket.on('message', (data: string) => {
43
43
  const response = JSON.parse(data);
44
44
  if (response.type === "messageResponse") {
45
- eventEmitter.emit("userMessage", response.response);
45
+ // Pass a callback function as an argument to the emit method
46
+ eventEmitter.emit("userMessage", response, (message: string) => {
47
+ console.log("Callback function invoked with message:", message);
48
+ cbws.getWebsocket.send(JSON.stringify({
49
+ "type": "processStoped"
50
+ }));
51
+ });
46
52
  }
47
53
  });
48
54
  return eventEmitter;
@@ -98,11 +98,23 @@ const cbcrawler = {
98
98
  /**
99
99
  * Initiates a crawl process.
100
100
  */
101
- crawl: () => {
102
- cbws.getWebsocket.send(JSON.stringify({
103
- "type": "crawlerEvent",
104
- action: 'crawl'
105
- }));
101
+ crawl: (query:string) => {
102
+ return new Promise((resolve, reject) => {
103
+ cbws.getWebsocket.send(JSON.stringify({
104
+ "type": "crawlerEvent",
105
+ "action": 'crawl',
106
+ "message":{
107
+ query
108
+ }
109
+ }));
110
+ cbws.getWebsocket.on('message', (data: string) => {
111
+ const response = JSON.parse(data);
112
+ if (response.type === "crawlResponse") {
113
+ resolve(response); // Resolve the Promise with the response data
114
+ }
115
+ });
116
+ });
117
+
106
118
  }
107
119
  };
108
120
 
@@ -1,4 +1,6 @@
1
1
  import WebSocket from 'ws';
2
+ import fs from 'fs';
3
+ import yaml from 'js-yaml';
2
4
 
3
5
  /**
4
6
  * Class representing a WebSocket connection.
@@ -10,11 +12,23 @@ class cbws {
10
12
  * Constructs a new cbws instance and initializes the WebSocket connection.
11
13
  */
12
14
  constructor() {
13
- this.websocket = new WebSocket("ws://localhost:12345/codebolt");
15
+ const uniqueConnectionId = this.getUniqueConnectionId();
16
+ console.log(uniqueConnectionId)
17
+ this.websocket = new WebSocket(`ws://localhost:12345/codebolt?id=${uniqueConnectionId}`);
14
18
  this.initializeWebSocket().catch(error => {
15
19
  console.error("WebSocket connection failed:", error);
16
20
  });
17
21
  }
22
+ private getUniqueConnectionId(): string {
23
+ try {
24
+ let fileContents = fs.readFileSync('./codebotagent.yml', 'utf8');
25
+ let data:any = yaml.load(fileContents);
26
+ return data.uniqueConnectionId;
27
+ } catch (e) {
28
+ console.error('Unable to locate codebotagent.yml file.');
29
+ return '';
30
+ }
31
+ }
18
32
 
19
33
  /**
20
34
  * Initializes the WebSocket by setting up event listeners and returning a promise that resolves