@@ -20,47 +20,62 @@ import task from './modules/task';
2020import vectorDB from './modules/vectordb' ;
2121import debug from './modules/debug'
2222import tokenizer from './modules/tokenizer'
23- import WebSocket from 'ws' ;
23+ import WebSocket , { EventEmitter } from 'ws' ;
2424
2525
2626/**
2727 * @class Codebolt
2828 * @description This class provides a unified interface to interact with various modules.
2929 */
30- class Codebolt {
30+ class Codebolt extends EventEmitter { // Extend EventEmitter
3131
3232 /**
3333 * @constructor
3434 * @description Initializes the websocket connection.
3535 */
36- constructor ( ) {
36+ constructor ( ) {
37+ super ( )
3738 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+ } ) ;
3853 }
39-
4054 /**
4155 * @method waitForConnection
4256 * @description Waits for the WebSocket connection to open.
4357 * @returns {Promise<void> } A promise that resolves when the WebSocket connection is open.
4458 */
45- async waitForConnection ( ) {
59+ async waitForConnection ( ) {
4660 return new Promise < void > ( ( resolve , reject ) => {
4761 if ( ! this . websocket ) {
4862 reject ( new Error ( 'WebSocket is not initialized' ) ) ;
4963 return ;
5064 }
51-
65+
5266 if ( this . websocket . readyState === WebSocket . OPEN ) {
5367 resolve ( ) ;
5468 return ;
5569 }
56-
70+
5771 this . websocket . addEventListener ( 'open' , ( ) => {
5872 resolve ( ) ;
5973 } ) ;
60-
74+
6175 this . websocket . addEventListener ( 'error' , ( error ) => {
6276 reject ( error ) ;
6377 } ) ;
78+
6479 } ) ;
6580 }
6681
@@ -72,12 +87,12 @@ class Codebolt {
7287 * @param {string } previous_command - The previous command executed.
7388 * @param {string } browser_content - The content of the browser.
7489 */
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 ) {
7691 cbbrowser . newPage ( ) ;
7792 }
7893 websocket : WebSocket | null = null ;
7994 fs = cbfs ;
80- git = git ;
95+ git = git ;
8196 llm = cbllm ;
8297 browser = cbbrowser ;
8398 chat = cbchat ;
@@ -92,11 +107,11 @@ class Codebolt {
92107 outputparsers = cboutputparsers ;
93108 project = cbproject ;
94109 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 ;
100115}
101116
102117export default new Codebolt ( ) ;
0 commit comments