@codebolt/codeboltjs 2.0.4 → 2.0.5

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.
Files changed (40) hide show
  1. package/dist/core/messageManager.d.ts +47 -0
  2. package/dist/core/messageManager.js +128 -0
  3. package/dist/{modules → core}/websocket.d.ts +5 -0
  4. package/dist/{modules → core}/websocket.js +13 -9
  5. package/dist/index.d.ts +42 -53
  6. package/dist/index.js +49 -4
  7. package/dist/modules/agent.js +26 -58
  8. package/dist/modules/browser.d.ts +7 -7
  9. package/dist/modules/browser.js +75 -195
  10. package/dist/modules/chat.d.ts +8 -20
  11. package/dist/modules/chat.js +60 -123
  12. package/dist/modules/codeutils.d.ts +1 -1
  13. package/dist/modules/codeutils.js +30 -107
  14. package/dist/modules/crawler.d.ts +1 -16
  15. package/dist/modules/crawler.js +13 -72
  16. package/dist/modules/dbmemory.js +12 -28
  17. package/dist/modules/debug.js +17 -33
  18. package/dist/modules/fs.d.ts +1 -1
  19. package/dist/modules/fs.js +82 -162
  20. package/dist/modules/git.js +60 -148
  21. package/dist/modules/history.js +11 -27
  22. package/dist/modules/llm.js +8 -16
  23. package/dist/modules/project.d.ts +1 -1
  24. package/dist/modules/project.js +16 -40
  25. package/dist/modules/state.js +29 -69
  26. package/dist/modules/task.js +19 -43
  27. package/dist/modules/terminal.d.ts +3 -2
  28. package/dist/modules/terminal.js +36 -47
  29. package/dist/modules/tokenizer.js +15 -31
  30. package/dist/modules/tools.d.ts +0 -6
  31. package/dist/modules/tools.js +41 -179
  32. package/dist/modules/utils.js +22 -0
  33. package/dist/modules/vectordb.js +30 -62
  34. package/dist/utils.d.ts +1 -1
  35. package/dist/utils.js +1 -1
  36. package/package.json +1 -1
  37. package/dist/utils/editFile.js +0 -30
  38. /package/dist/{utils/editFile.d.ts → modules/utils.d.ts} +0 -0
  39. /package/dist/{modules → utils}/toolBox.d.ts +0 -0
  40. /package/dist/{modules → utils}/toolBox.js +0 -0
@@ -4,14 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  // chat.ts
7
- const websocket_1 = __importDefault(require("./websocket"));
8
- const events_1 = require("events");
9
- /**
10
- * CustomEventEmitter class that extends the Node.js EventEmitter class.
11
- */
12
- class CustomEventEmitter extends events_1.EventEmitter {
13
- }
14
- let eventEmitter = new CustomEventEmitter();
7
+ const websocket_1 = __importDefault(require("../core/websocket"));
15
8
  /**
16
9
  * Chat module to interact with the WebSocket server.
17
10
  */
@@ -21,17 +14,9 @@ const cbchat = {
21
14
  * @returns {Promise<ChatMessage[]>} A promise that resolves with an array of ChatMessage objects representing the chat history.
22
15
  */
23
16
  getChatHistory: () => {
24
- return new Promise((resolve, reject) => {
25
- websocket_1.default.getWebsocket.send(JSON.stringify({
26
- "type": "getChatHistory"
27
- }));
28
- websocket_1.default.getWebsocket.on('message', (data) => {
29
- const response = JSON.parse(data);
30
- if (response.type === "getChatHistoryResponse") {
31
- resolve(response); // Resolve the Promise with the response data
32
- }
33
- });
34
- });
17
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
18
+ "type": "getChatHistory"
19
+ }, "getChatHistoryResponse");
35
20
  },
36
21
  /**
37
22
  * Sets a global request handler for all incoming messages
@@ -40,15 +25,14 @@ const cbchat = {
40
25
  setRequestHandler: (handler) => {
41
26
  const waitForConnection = () => {
42
27
  const setupHandler = () => {
43
- if (websocket_1.default.getWebsocket) {
44
- websocket_1.default.getWebsocket.on('message', async (data) => {
28
+ if (websocket_1.default.messageManager) {
29
+ websocket_1.default.messageManager.on('message', async (request) => {
45
30
  try {
46
- const request = JSON.parse(data);
47
31
  await handler(request, (responseData) => {
48
- websocket_1.default.getWebsocket.send(JSON.stringify({
32
+ websocket_1.default.messageManager.send({
49
33
  type: `processStoped`,
50
34
  ...responseData
51
- }));
35
+ });
52
36
  });
53
37
  }
54
38
  catch (error) {
@@ -64,46 +48,16 @@ const cbchat = {
64
48
  };
65
49
  waitForConnection();
66
50
  },
67
- /**
68
- * Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
69
- * @returns {EventEmitter} The event emitter used for emitting custom events.
70
- */
71
- /**
72
- * Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
73
- * @returns {EventEmitter} The event emitter used for emitting custom events.
74
- */
75
- onActionMessage: () => {
76
- const waitForConnection = () => {
77
- if (websocket_1.default.getWebsocket) {
78
- websocket_1.default.getWebsocket.on('message', (data) => {
79
- const response = JSON.parse(data);
80
- if (response.type === "messageResponse") {
81
- eventEmitter.emit("userMessage", response, (message) => {
82
- websocket_1.default.getWebsocket.send(JSON.stringify({
83
- "type": "processStoped",
84
- "message": message
85
- }));
86
- });
87
- }
88
- });
89
- }
90
- else {
91
- setTimeout(waitForConnection, 100);
92
- }
93
- };
94
- waitForConnection();
95
- return eventEmitter;
96
- },
97
51
  /**
98
52
  * Sends a message through the WebSocket connection.
99
53
  * @param {string} message - The message to be sent.
100
54
  */
101
55
  sendMessage: (message, payload) => {
102
- websocket_1.default.getWebsocket.send(JSON.stringify({
56
+ websocket_1.default.messageManager.send({
103
57
  "type": "sendMessage",
104
58
  "message": message,
105
59
  payload
106
- }));
60
+ });
107
61
  },
108
62
  /**
109
63
  * Waits for a reply to a sent message.
@@ -111,44 +65,47 @@ const cbchat = {
111
65
  * @returns {Promise<UserMessage>} A promise that resolves with the reply.
112
66
  */
113
67
  waitforReply: (message) => {
114
- return new Promise((resolve, reject) => {
115
- websocket_1.default.getWebsocket.send(JSON.stringify({
116
- "type": "waitforReply",
117
- "message": message
118
- }));
119
- websocket_1.default.getWebsocket.on('message', (data) => {
120
- const response = JSON.parse(data);
121
- if (response.type === "waitFormessageResponse") {
122
- resolve(response); // Resolve the Promise with the response data
123
- }
124
- });
125
- });
68
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
69
+ "type": "waitforReply",
70
+ "message": message
71
+ }, "waitFormessageResponse");
126
72
  },
127
73
  /**
128
- * Notifies the server that a process has started and sets up an event listener for stopProcessClicked events.
129
- * @returns An object containing the event emitter and a stopProcess method.
74
+ * Notifies the server that a process has started and sets up a listener for stopProcessClicked events.
75
+ * @param {Function} onStopClicked - Callback function to handle stop process events.
76
+ * @returns An object containing a stopProcess method.
130
77
  */
131
- processStarted: () => {
78
+ processStarted: (onStopClicked) => {
132
79
  // Send the process started message
133
- websocket_1.default.getWebsocket.send(JSON.stringify({
80
+ websocket_1.default.messageManager.send({
134
81
  "type": "processStarted"
135
- }));
136
- // Register event listener for WebSocket messages
137
- websocket_1.default.getWebsocket.on('message', (data) => {
138
- const message = JSON.parse(data);
139
- if (message.type === 'stopProcessClicked')
140
- // Emit a custom event based on the message type
141
- eventEmitter.emit("stopProcessClicked", message);
142
82
  });
143
- // Return an object that includes the event emitter and the stopProcess method
83
+ // Register event listener for WebSocket messages if callback provided
84
+ if (onStopClicked) {
85
+ const handleStopMessage = (message) => {
86
+ if (message.type === 'stopProcessClicked') {
87
+ onStopClicked(message);
88
+ }
89
+ };
90
+ websocket_1.default.messageManager.on('message', handleStopMessage);
91
+ // Return an object that includes the stopProcess method and cleanup
92
+ return {
93
+ stopProcess: () => {
94
+ websocket_1.default.messageManager.send({
95
+ "type": "processStoped"
96
+ });
97
+ },
98
+ cleanup: () => {
99
+ websocket_1.default.messageManager.removeListener('message', handleStopMessage);
100
+ }
101
+ };
102
+ }
103
+ // Return an object that includes the stopProcess method
144
104
  return {
145
- event: eventEmitter,
146
105
  stopProcess: () => {
147
- // Implement the logic to stop the process here
148
- // For example, you might want to send a specific message to the server to stop the process
149
- websocket_1.default.getWebsocket.send(JSON.stringify({
106
+ websocket_1.default.messageManager.send({
150
107
  "type": "processStoped"
151
- }));
108
+ });
152
109
  }
153
110
  };
154
111
  },
@@ -157,69 +114,49 @@ const cbchat = {
157
114
  * Sends a specific message to the server to stop the process.
158
115
  */
159
116
  stopProcess: () => {
160
- // Implement the logic to stop the process here
161
- // For example, you might want to send a specific message to the server to stop the process
162
- websocket_1.default.getWebsocket.send(JSON.stringify({
117
+ websocket_1.default.messageManager.send({
163
118
  "type": "processStoped"
164
- }));
119
+ });
165
120
  },
166
121
  /**
167
122
  * Stops the ongoing process.
168
123
  * Sends a specific message to the server to stop the process.
169
124
  */
170
125
  processFinished: () => {
171
- // Implement the logic to stop the process here
172
- // For example, you might want to send a specific message to the server to stop the process
173
- websocket_1.default.getWebsocket.send(JSON.stringify({
126
+ websocket_1.default.messageManager.send({
174
127
  "type": "processFinished"
175
- }));
128
+ });
176
129
  },
177
130
  /**
178
131
  * Sends a confirmation request to the server with two options: Yes or No.
179
132
  * @returns {Promise<string>} A promise that resolves with the server's response.
180
133
  */
181
134
  sendConfirmationRequest: (confirmationMessage, buttons = [], withFeedback = false) => {
182
- return new Promise((resolve, reject) => {
183
- websocket_1.default.getWebsocket.send(JSON.stringify({
184
- "type": "confirmationRequest",
185
- "message": confirmationMessage,
186
- buttons: buttons,
187
- withFeedback
188
- }));
189
- websocket_1.default.getWebsocket.on('message', (data) => {
190
- const response = JSON.parse(data);
191
- if (response.type === "confirmationResponse" || response.type === "feedbackResponse") {
192
- resolve(response); // Resolve the Promise with the server's response
193
- }
194
- });
195
- });
135
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
136
+ "type": "confirmationRequest",
137
+ "message": confirmationMessage,
138
+ buttons: buttons,
139
+ withFeedback
140
+ }, "confirmationResponse|feedbackResponse");
196
141
  },
197
142
  askQuestion: (question, buttons = [], withFeedback = false) => {
198
- return new Promise((resolve, reject) => {
199
- websocket_1.default.getWebsocket.send(JSON.stringify({
200
- "type": "confirmationRequest",
201
- "message": question,
202
- buttons: buttons,
203
- withFeedback
204
- }));
205
- websocket_1.default.getWebsocket.on('message', (data) => {
206
- const response = JSON.parse(data);
207
- if (response.type === "confirmationResponse" || response.type === "feedbackResponse") {
208
- resolve(response); // Resolve the Promise with the server's response
209
- }
210
- });
211
- });
143
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
144
+ "type": "confirmationRequest",
145
+ "message": question,
146
+ buttons: buttons,
147
+ withFeedback
148
+ }, "confirmationResponse|feedbackResponse");
212
149
  },
213
150
  /**
214
151
  * Sends a notification event to the server.
215
152
  * @param {string} notificationMessage - The message to be sent in the notification.
216
153
  */
217
154
  sendNotificationEvent: (notificationMessage, type) => {
218
- websocket_1.default.getWebsocket.send(JSON.stringify({
155
+ websocket_1.default.messageManager.send({
219
156
  "type": "notificationEvent",
220
157
  "message": notificationMessage,
221
158
  "eventType": type
222
- }));
159
+ });
223
160
  },
224
161
  };
225
162
  exports.default = cbchat;
@@ -8,7 +8,7 @@ declare const cbcodeutils: {
8
8
  * @param {string} filePath - The path of the file to retrieve the JS tree for.
9
9
  * @returns {Promise<GetJsTreeResponse>} A promise that resolves with the JS tree response.
10
10
  */
11
- getJsTree: (filePath?: string) => Promise<unknown>;
11
+ getJsTree: (filePath?: string) => Promise<any>;
12
12
  /**
13
13
  * Retrieves all files as Markdown.
14
14
  * @returns {Promise<string>} A promise that resolves with the Markdown content of all files.
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const websocket_1 = __importDefault(require("./websocket"));
6
+ const websocket_1 = __importDefault(require("../core/websocket"));
7
7
  /**
8
8
  * A utility module for working with code.
9
9
  */
@@ -14,63 +14,18 @@ const cbcodeutils = {
14
14
  * @returns {Promise<GetJsTreeResponse>} A promise that resolves with the JS tree response.
15
15
  */
16
16
  getJsTree: (filePath) => {
17
- return new Promise(async (resolve, reject) => {
18
- websocket_1.default.getWebsocket.send(JSON.stringify({
19
- "type": "settingEvent",
20
- "action": "getProjectPath"
21
- }));
22
- websocket_1.default.getWebsocket.on('message', (data) => {
23
- const response = JSON.parse(data);
24
- if (response.type === "getProjectPathResponse") {
25
- // resolve(response);
26
- // try {
27
- // let pathInput = filePath || response.projectPath;
28
- // let parser = new Parser();
29
- // // Initialize the parser with the JavaScript language
30
- // parser.setLanguage(JavaScript);
31
- // const trees = [];
32
- // const functionNodes = [];
33
- // const processDirectory = (directory: any) => {
34
- // // Read all files in the directory
35
- // const files = fs.readdirSync(directory, { withFileTypes: true });
36
- // files.forEach(file => {
37
- // if (file.isDirectory()) {
38
- // if (file.name !== 'node_modules') { // Ignore node_modules directory
39
- // processDirectory(path.join(directory, file.name)); // Recursive call for subdirectories
40
- // }
41
- // } else if (path.extname(file.name) === '.js') {
42
- // const code = fs.readFileSync(path.join(directory, file.name), 'utf-8');
43
- // let tree: any = parser.parse(code);
44
- // tree.rootNode.path = path.join(directory, file.name); // Set file path for t
45
- // trees.push(tree);
46
- // }
47
- // });
48
- // };
49
- // if (fs.lstatSync(pathInput).isDirectory()) {
50
- // processDirectory(pathInput);
51
- // } else if (path.extname(pathInput) === '.js') {
52
- // // Read a single JavaScript file
53
- // const code = fs.readFileSync(pathInput, 'utf-8');
54
- // let tree: any = parser.parse(code);
55
- // tree.rootNode.path = pathInput; // Set file path for t
56
- // trees.push(tree);
57
- // }
58
- // resolve({ event: 'GetJsTreeResponse', payload: trees }); // Return an array of abstract syntax trees (ASTs)
59
- // } catch (error) {
60
- // console.error('An error occurred:', error);
61
- // return { event: 'GetJsTreeResponse', payload: null }; // Return null in case of error
62
- // }
63
- }
64
- });
65
- });
17
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
18
+ "type": "settingEvent",
19
+ "action": "getProjectPath"
20
+ }, "getProjectPathResponse");
66
21
  // return new Promise(async (resolve, reject) => {
67
- // cbws.getWebsocket.send(JSON.stringify({
22
+ // cbws.messageManager.send({
68
23
  // "type": "codeEvent",
69
24
  // "action": "getJsTree",
70
25
  // payload: {
71
26
  // filePath
72
27
  // }
73
- // }));
28
+ // });
74
29
  // cbws.getWebsocket.on('message', (data: string) => {
75
30
  // const response = JSON.parse(data);
76
31
  // if (response.type === "getJsTreeResponse") {
@@ -84,18 +39,10 @@ const cbcodeutils = {
84
39
  * @returns {Promise<string>} A promise that resolves with the Markdown content of all files.
85
40
  */
86
41
  getAllFilesAsMarkDown: () => {
87
- return new Promise((resolve, reject) => {
88
- websocket_1.default.getWebsocket.send(JSON.stringify({
89
- "type": "codeEvent",
90
- "action": "getAllFilesMarkdown"
91
- }));
92
- websocket_1.default.getWebsocket.on('message', (data) => {
93
- const response = JSON.parse(data);
94
- if (response.type === "getAllFilesMarkdownResponse") {
95
- resolve(response); // Resolve the Promise with the response data
96
- }
97
- });
98
- });
42
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
43
+ "type": "codeEvent",
44
+ "action": "getAllFilesMarkdown"
45
+ }, "getAllFilesMarkdownResponse");
99
46
  },
100
47
  /**
101
48
  * Performs a matching operation based on the provided matcher definition and problem patterns.
@@ -105,40 +52,24 @@ const cbcodeutils = {
105
52
  * @returns {Promise<MatchProblemResponse>} A promise that resolves with the matching problem response.
106
53
  */
107
54
  performMatch: (matcherDefinition, problemPatterns, problems) => {
108
- return new Promise((resolve, reject) => {
109
- websocket_1.default.getWebsocket.send(JSON.stringify({
110
- "type": "codeEvent",
111
- "action": "performMatch",
112
- payload: {
113
- matcherDefinition,
114
- problemPatterns,
115
- }
116
- }));
117
- websocket_1.default.getWebsocket.on('message', (data) => {
118
- const response = JSON.parse(data);
119
- if (response.type === "getgetJsTreeResponse") {
120
- resolve(response); // Resolve the Promise with the response data
121
- }
122
- });
123
- });
55
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
56
+ "type": "codeEvent",
57
+ "action": "performMatch",
58
+ payload: {
59
+ matcherDefinition,
60
+ problemPatterns,
61
+ }
62
+ }, "getgetJsTreeResponse");
124
63
  },
125
64
  /**
126
65
  * Retrieves the list of matchers.
127
66
  * @returns {Promise<GetMatcherListTreeResponse>} A promise that resolves with the list of matchers response.
128
67
  */
129
68
  getMatcherList: () => {
130
- return new Promise((resolve, reject) => {
131
- websocket_1.default.getWebsocket.send(JSON.stringify({
132
- "type": "codeEvent",
133
- "action": "getMatcherList",
134
- }));
135
- websocket_1.default.getWebsocket.on('message', (data) => {
136
- const response = JSON.parse(data);
137
- if (response.type === "getMatcherListTreeResponse") {
138
- resolve(response); // Resolve the Promise with the response data
139
- }
140
- });
141
- });
69
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
70
+ "type": "codeEvent",
71
+ "action": "getMatcherList",
72
+ }, "getMatcherListTreeResponse");
142
73
  },
143
74
  /**
144
75
  * Retrieves details of a match.
@@ -146,21 +77,13 @@ const cbcodeutils = {
146
77
  * @returns {Promise<getMatchDetail>} A promise that resolves with the match detail response.
147
78
  */
148
79
  matchDetail: (matcher) => {
149
- return new Promise((resolve, reject) => {
150
- websocket_1.default.getWebsocket.send(JSON.stringify({
151
- "type": "codeEvent",
152
- "action": "getMatchDetail",
153
- payload: {
154
- match: matcher
155
- }
156
- }));
157
- websocket_1.default.getWebsocket.on('message', (data) => {
158
- const response = JSON.parse(data);
159
- if (response.type === "matchDetailTreeResponse") {
160
- resolve(response); // Resolve the Promise with the response data
161
- }
162
- });
163
- });
80
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
81
+ "type": "codeEvent",
82
+ "action": "getMatchDetail",
83
+ payload: {
84
+ match: matcher
85
+ }
86
+ }, "matchDetailTreeResponse");
164
87
  }
165
88
  };
166
89
  exports.default = cbcodeutils;
@@ -25,21 +25,6 @@ declare const cbcrawler: {
25
25
  * @param id - The ID of the element to be clicked.
26
26
  * @returns {Promise<any>} A promise that resolves when the click action is complete.
27
27
  */
28
- click: (id: string) => Promise<unknown>;
29
- /**
30
- * Types the provided text into an element with the specified ID.
31
- * @param id - The ID of the element where text will be typed.
32
- * @param text - The text to type into the element.
33
- * @returns {Promise<any>} A promise that resolves when the type action is complete.
34
- */
35
- type: (id: string, text: string) => Promise<unknown>;
36
- /**
37
- * Simulates the Enter key press using the crawler.
38
- */
39
- enter: () => void;
40
- /**
41
- * Initiates a crawl process.
42
- */
43
- crawl: (query: string) => Promise<unknown>;
28
+ click: (id: string) => Promise<any>;
44
29
  };
45
30
  export default cbcrawler;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const websocket_1 = __importDefault(require("./websocket"));
6
+ const websocket_1 = __importDefault(require("../core/websocket"));
7
7
  /**
8
8
  * A module for controlling a web crawler through WebSocket messages.
9
9
  */
@@ -12,41 +12,41 @@ const cbcrawler = {
12
12
  * Starts the crawler.
13
13
  */
14
14
  start: () => {
15
- websocket_1.default.getWebsocket.send(JSON.stringify({
15
+ websocket_1.default.messageManager.send({
16
16
  "type": "crawlerEvent",
17
17
  action: 'start'
18
- }));
18
+ });
19
19
  },
20
20
  /**
21
21
  * Takes a screenshot using the crawler.
22
22
  */
23
23
  screenshot: () => {
24
- websocket_1.default.getWebsocket.send(JSON.stringify({
24
+ websocket_1.default.messageManager.send({
25
25
  "type": "crawlerEvent",
26
26
  action: 'screenshot'
27
- }));
27
+ });
28
28
  },
29
29
  /**
30
30
  * Directs the crawler to navigate to a specified URL.
31
31
  * @param url - The URL for the crawler to navigate to.
32
32
  */
33
33
  goToPage: (url) => {
34
- websocket_1.default.getWebsocket.send(JSON.stringify({
34
+ websocket_1.default.messageManager.send({
35
35
  "type": "crawlerEvent",
36
36
  action: 'goToPage',
37
37
  url
38
- }));
38
+ });
39
39
  },
40
40
  /**
41
41
  * Scrolls the crawler in a specified direction.
42
42
  * @param direction - The direction to scroll ('up', 'down', 'left', 'right').
43
43
  */
44
44
  scroll: (direction) => {
45
- websocket_1.default.getWebsocket.send(JSON.stringify({
45
+ websocket_1.default.messageManager.send({
46
46
  "type": "crawlerEvent",
47
47
  action: 'scroll',
48
48
  direction
49
- }));
49
+ });
50
50
  },
51
51
  /**
52
52
  * Simulates a click event on an element with the specified ID.
@@ -54,70 +54,11 @@ const cbcrawler = {
54
54
  * @returns {Promise<any>} A promise that resolves when the click action is complete.
55
55
  */
56
56
  click: (id) => {
57
- return new Promise((resolve, reject) => {
58
- websocket_1.default.getWebsocket.send(JSON.stringify({
59
- "type": "crawlerEvent",
60
- action: 'click',
61
- id
62
- }));
63
- websocket_1.default.getWebsocket.on('message', (data) => {
64
- const response = JSON.parse(data);
65
- if (response.event === "clickFinished") {
66
- resolve(response);
67
- }
68
- });
69
- });
70
- },
71
- /**
72
- * Types the provided text into an element with the specified ID.
73
- * @param id - The ID of the element where text will be typed.
74
- * @param text - The text to type into the element.
75
- * @returns {Promise<any>} A promise that resolves when the type action is complete.
76
- */
77
- type: (id, text) => {
78
- return new Promise((resolve, reject) => {
79
- websocket_1.default.getWebsocket.send(JSON.stringify({
80
- "type": "crawlerEvent",
81
- action: 'type',
82
- id,
83
- text
84
- }));
85
- websocket_1.default.getWebsocket.on('message', (data) => {
86
- const response = JSON.parse(data);
87
- if (response.event === "typeFinished") {
88
- resolve(response);
89
- }
90
- });
91
- });
92
- },
93
- /**
94
- * Simulates the Enter key press using the crawler.
95
- */
96
- enter: () => {
97
- websocket_1.default.getWebsocket.send(JSON.stringify({
57
+ return websocket_1.default.messageManager.sendAndWaitForResponse({
98
58
  "type": "crawlerEvent",
99
- action: 'enter'
100
- }));
101
- },
102
- /**
103
- * Initiates a crawl process.
104
- */
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
- });
59
+ action: 'click',
60
+ id
61
+ }, "crawlResponse");
121
62
  }
122
63
  };
123
64
  exports.default = cbcrawler;