@@ -20,47 +20,62 @@ import task from './modules/task';
20
20
import vectorDB from './modules/vectordb' ;
21
21
import debug from './modules/debug'
22
22
import tokenizer from './modules/tokenizer'
23
- import WebSocket from 'ws' ;
23
+ import WebSocket , { EventEmitter } from 'ws' ;
24
24
25
25
26
26
/**
27
27
* @class Codebolt
28
28
* @description This class provides a unified interface to interact with various modules.
29
29
*/
30
- class Codebolt {
30
+ class Codebolt extends EventEmitter { // Extend EventEmitter
31
31
32
32
/**
33
33
* @constructor
34
34
* @description Initializes the websocket connection.
35
35
*/
36
- constructor ( ) {
36
+ constructor ( ) {
37
+ super ( )
37
38
this . websocket = cbws . getWebsocket ;
39
+ this . userMessageListener ( ) ; // Call setupMessageListener() to subscribe to WebSocket messages
40
+ }
41
+ /**
42
+ * @method setupMessageListener
43
+ * @description Sets up a listener for incoming WebSocket messages.
44
+ */
45
+ userMessageListener ( ) {
46
+ if ( ! this . websocket ) return ;
47
+ this . websocket . on ( 'message' , ( data : string ) => {
48
+ const response = JSON . parse ( data ) ;
49
+ if ( response . type === "messageResponse" ) {
50
+ this . emit ( response . message )
51
+ }
52
+ } ) ;
38
53
}
39
-
40
54
/**
41
55
* @method waitForConnection
42
56
* @description Waits for the WebSocket connection to open.
43
57
* @returns {Promise<void> } A promise that resolves when the WebSocket connection is open.
44
58
*/
45
- async waitForConnection ( ) {
59
+ async waitForConnection ( ) {
46
60
return new Promise < void > ( ( resolve , reject ) => {
47
61
if ( ! this . websocket ) {
48
62
reject ( new Error ( 'WebSocket is not initialized' ) ) ;
49
63
return ;
50
64
}
51
-
65
+
52
66
if ( this . websocket . readyState === WebSocket . OPEN ) {
53
67
resolve ( ) ;
54
68
return ;
55
69
}
56
-
70
+
57
71
this . websocket . addEventListener ( 'open' , ( ) => {
58
72
resolve ( ) ;
59
73
} ) ;
60
-
74
+
61
75
this . websocket . addEventListener ( 'error' , ( error ) => {
62
76
reject ( error ) ;
63
77
} ) ;
78
+
64
79
} ) ;
65
80
}
66
81
@@ -72,12 +87,12 @@ class Codebolt {
72
87
* @param {string } previous_command - The previous command executed.
73
88
* @param {string } browser_content - The content of the browser.
74
89
*/
75
- start_browser ( objective :string , url :string , previous_command :string , browser_content :string ) {
90
+ start_browser ( objective : string , url : string , previous_command : string , browser_content : string ) {
76
91
cbbrowser . newPage ( ) ;
77
92
}
78
93
websocket : WebSocket | null = null ;
79
94
fs = cbfs ;
80
- git = git ;
95
+ git = git ;
81
96
llm = cbllm ;
82
97
browser = cbbrowser ;
83
98
chat = cbchat ;
@@ -92,11 +107,11 @@ class Codebolt {
92
107
outputparsers = cboutputparsers ;
93
108
project = cbproject ;
94
109
dbmemory = dbmemory ;
95
- cbstate = cbstate ;
96
- taskplaner = task ;
97
- vectordb = vectorDB ;
98
- debug = debug ;
99
- tokenizer = tokenizer ;
110
+ cbstate = cbstate ;
111
+ taskplaner = task ;
112
+ vectordb = vectorDB ;
113
+ debug = debug ;
114
+ tokenizer = tokenizer ;
100
115
}
101
116
102
117
export default new Codebolt ( ) ;
0 commit comments