From f4098d63166c7cd2b30dd3a362516695e7ecae2a Mon Sep 17 00:00:00 2001 From: Utkarsh Shukla Date: Fri, 20 Sep 2024 16:08:49 +0530 Subject: [PATCH 1/6] updated --- src/index.ts | 98 +++------------------------------------- src/modules/websocket.ts | 2 +- 2 files changed, 7 insertions(+), 93 deletions(-) diff --git a/src/index.ts b/src/index.ts index d5407a6..d9f072c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,49 +1,16 @@ -import cbws from './modules/websocket'; -import cbfs from './modules/fs'; -import cbllm from './modules/llm'; -import cbterminal from './modules/terminal'; -import cbbrowser from './modules/browser'; -import cbchat from './modules/chat'; -import cbcodeutils from './modules/codeutils'; -import cbdocutils from './modules/docutils'; -import cbcrawler from './modules/crawler'; -import cbsearch from './modules/search'; -import cbknowledge from './modules/knowledge'; -import cbrag from './modules/rag'; -import cbcodeparsers from './modules/codeparsers'; -import cboutputparsers from './modules/outputparsers'; -import cbproject from './modules/project'; -import git from './modules/git'; -import dbmemory from './modules/dbmemory'; -import cbstate from './modules/state'; -import task from './modules/task'; -import vectorDB from './modules/vectordb'; -import debug from './modules/debug' -import tokenizer from './modules/tokenizer' +import CbWS from './modules/websocket'; import WebSocket from 'ws'; import { EventEmitter } from 'events'; -import {chatSummary} from './modules/history' +import Chat from './modules/chat'; - -/** - * @class Codebolt - * @description This class provides a unified interface to interact with various modules. - */ class Codebolt { // Extend EventEmitter private static instance: Codebolt | null = null; private wsManager: cbws; - private vectorDB: vectorDB; - + chat: Chat; - /** - * @constructor - * @description Initializes the websocket connection. - */ constructor() { - this.wsManager = new cbws(); - this.vectordb = new vectorDB(this.wsManager); - // this.websocket = cbws.getWebsocket; - // ... initialize other modules ... + this.wsManager = new CbWS(); + this.chat = new Chat(this.wsManager); } public static getInstance(): Codebolt { @@ -53,68 +20,15 @@ class Codebolt { // Extend EventEmitter return Codebolt.instance; } - /** - * @method waitForConnection - * @description Waits for the WebSocket connection to open. - * @returns {Promise} A promise that resolves when the WebSocket connection is open. - */ - // async waitForConnection() { - // return new Promise((resolve, reject) => { - // if (!this.websocket) { - // reject(new Error('WebSocket is not initialized')); - // return; - // } - - // if (this.websocket.readyState === WebSocket.OPEN) { - // resolve(); - // return; - // } - - // this.websocket.addEventListener('open', () => { - // resolve(); - // }); - - // this.websocket.addEventListener('error', (error) => { - // reject(error); - // }); - - // }); - // } - async connect() { await this.wsManager.connect(); } async disconnect() { await this.wsManager.disconnect(); - this.wsManager = null; } websocket: WebSocket | null = null; - fs = cbfs; - git = git; - llm = cbllm; - browser = cbbrowser; - chat = cbchat; - terminal = cbterminal; - codeutils = cbcodeutils; - docutils = cbdocutils; - crawler = cbcrawler; - search = cbsearch; - knowledge = cbknowledge; - rag = cbrag; - codeparsers = cbcodeparsers; - outputparsers = cboutputparsers; - project = cbproject; - dbmemory = dbmemory; - cbstate = cbstate; - taskplaner = task; - vectordb = vectorDB; - debug = debug; - tokenizer = tokenizer; - chatSummary=chatSummary } -export default Codebolt.getInstance(); - -// module.exports = new Codebolt(); \ No newline at end of file +export default Codebolt.getInstance(); \ No newline at end of file diff --git a/src/modules/websocket.ts b/src/modules/websocket.ts index 899e154..3c3b954 100644 --- a/src/modules/websocket.ts +++ b/src/modules/websocket.ts @@ -5,7 +5,7 @@ import yaml from 'js-yaml'; /** * Class representing a WebSocket connection. */ -class cbws { +class CbWS { private websocket: WebSocket | null = null; /** From ae767297829cb2f182d0e585bc909d68aaacfb6f Mon Sep 17 00:00:00 2001 From: Utkarsh Shukla Date: Fri, 20 Sep 2024 16:09:12 +0530 Subject: [PATCH 2/6] updated --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index d9f072c..6b72eb6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import Chat from './modules/chat'; class Codebolt { // Extend EventEmitter private static instance: Codebolt | null = null; - private wsManager: cbws; + private wsManager: CbWS; chat: Chat; constructor() { From d17c50a0c43481e9591fa5a489131606a8b2c923 Mon Sep 17 00:00:00 2001 From: Utkarsh Shukla Date: Fri, 20 Sep 2024 16:15:08 +0530 Subject: [PATCH 3/6] updated --- src/modules/chat.ts | 6 +++--- src/modules/websocket.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/modules/chat.ts b/src/modules/chat.ts index 86df70f..fc43713 100644 --- a/src/modules/chat.ts +++ b/src/modules/chat.ts @@ -1,15 +1,15 @@ -import cbws from './websocket'; +import CbWS from './websocket'; import { EventEmitter } from 'events'; import { ChatMessage, UserMessage } from '@codebolt/types'; class CustomEventEmitter extends EventEmitter {} class Chat { - private wsManager: cbws; + private wsManager: CbWS; private ws: any; private eventEmitter: CustomEventEmitter; - constructor(wsManager: cbws) { + constructor(wsManager: CbWS) { this.wsManager = wsManager; this.ws = this.wsManager.getWebsocket() as any; this.eventEmitter = new CustomEventEmitter(); diff --git a/src/modules/websocket.ts b/src/modules/websocket.ts index 3c3b954..454636b 100644 --- a/src/modules/websocket.ts +++ b/src/modules/websocket.ts @@ -18,7 +18,7 @@ class CbWS { const uniqueConnectionId = this.getUniqueConnectionId(); const initialMessage = this.getInitialMessage(); - this.websocket = new WebSocket(`ws://localhost:${process.env.SOCKET_PORT}/codebolt?id=${uniqueConnectionId}`); + this.websocket = new WebSocket(`ws://localhost:12345/codebolt?id=${uniqueConnectionId}`); return await this.initializeWebSocket(initialMessage); } @@ -91,4 +91,4 @@ class CbWS { } } -export default cbws; +export default CbWS; From ea8faac97e2ada2e2aa408947e5f85ae6996f934 Mon Sep 17 00:00:00 2001 From: Utkarsh Shukla Date: Fri, 20 Sep 2024 16:16:25 +0530 Subject: [PATCH 4/6] updated --- {src/modules => bkp}/browser.ts | 2 +- {src/modules => bkp}/codeparsers.ts | 0 {src/modules => bkp}/codeutils.ts | 0 {src/modules => bkp}/crawler.ts | 0 {src/modules => bkp}/dbmemory.ts | 0 {src/modules => bkp}/debug.ts | 0 {src/modules => bkp}/docutils.ts | 0 {src/modules => bkp}/fs.ts | 0 {src/modules => bkp}/git.ts | 0 {src/modules => bkp}/history.ts | 0 {src/modules => bkp}/knowledge.ts | 0 {src/modules => bkp}/llm.ts | 0 {src/modules => bkp}/outputparsers.ts | 0 {src/modules => bkp}/project.ts | 0 {src/modules => bkp}/rag.ts | 0 {src/modules => bkp}/search.ts | 0 {src/modules => bkp}/state.ts | 0 {src/modules => bkp}/task.ts | 0 {src/modules => bkp}/terminal.ts | 0 {src/modules => bkp}/tokenizer.ts | 0 {src/modules => bkp}/vectordb.ts | 0 tsconfig.json | 2 +- 22 files changed, 2 insertions(+), 2 deletions(-) rename {src/modules => bkp}/browser.ts (99%) rename {src/modules => bkp}/codeparsers.ts (100%) rename {src/modules => bkp}/codeutils.ts (100%) rename {src/modules => bkp}/crawler.ts (100%) rename {src/modules => bkp}/dbmemory.ts (100%) rename {src/modules => bkp}/debug.ts (100%) rename {src/modules => bkp}/docutils.ts (100%) rename {src/modules => bkp}/fs.ts (100%) rename {src/modules => bkp}/git.ts (100%) rename {src/modules => bkp}/history.ts (100%) rename {src/modules => bkp}/knowledge.ts (100%) rename {src/modules => bkp}/llm.ts (100%) rename {src/modules => bkp}/outputparsers.ts (100%) rename {src/modules => bkp}/project.ts (100%) rename {src/modules => bkp}/rag.ts (100%) rename {src/modules => bkp}/search.ts (100%) rename {src/modules => bkp}/state.ts (100%) rename {src/modules => bkp}/task.ts (100%) rename {src/modules => bkp}/terminal.ts (100%) rename {src/modules => bkp}/tokenizer.ts (100%) rename {src/modules => bkp}/vectordb.ts (100%) diff --git a/src/modules/browser.ts b/bkp/browser.ts similarity index 99% rename from src/modules/browser.ts rename to bkp/browser.ts index b14792c..0725d23 100644 --- a/src/modules/browser.ts +++ b/bkp/browser.ts @@ -1,4 +1,4 @@ -import cbws from './websocket'; +import cbws from '../src/modules/websocket'; import {GoToPageResponse,UrlResponse,GetMarkdownResponse,HtmlReceived,ExtractTextResponse,GetContentResponse} from '@codebolt/types' /** * A module for interacting with a browser through WebSockets. diff --git a/src/modules/codeparsers.ts b/bkp/codeparsers.ts similarity index 100% rename from src/modules/codeparsers.ts rename to bkp/codeparsers.ts diff --git a/src/modules/codeutils.ts b/bkp/codeutils.ts similarity index 100% rename from src/modules/codeutils.ts rename to bkp/codeutils.ts diff --git a/src/modules/crawler.ts b/bkp/crawler.ts similarity index 100% rename from src/modules/crawler.ts rename to bkp/crawler.ts diff --git a/src/modules/dbmemory.ts b/bkp/dbmemory.ts similarity index 100% rename from src/modules/dbmemory.ts rename to bkp/dbmemory.ts diff --git a/src/modules/debug.ts b/bkp/debug.ts similarity index 100% rename from src/modules/debug.ts rename to bkp/debug.ts diff --git a/src/modules/docutils.ts b/bkp/docutils.ts similarity index 100% rename from src/modules/docutils.ts rename to bkp/docutils.ts diff --git a/src/modules/fs.ts b/bkp/fs.ts similarity index 100% rename from src/modules/fs.ts rename to bkp/fs.ts diff --git a/src/modules/git.ts b/bkp/git.ts similarity index 100% rename from src/modules/git.ts rename to bkp/git.ts diff --git a/src/modules/history.ts b/bkp/history.ts similarity index 100% rename from src/modules/history.ts rename to bkp/history.ts diff --git a/src/modules/knowledge.ts b/bkp/knowledge.ts similarity index 100% rename from src/modules/knowledge.ts rename to bkp/knowledge.ts diff --git a/src/modules/llm.ts b/bkp/llm.ts similarity index 100% rename from src/modules/llm.ts rename to bkp/llm.ts diff --git a/src/modules/outputparsers.ts b/bkp/outputparsers.ts similarity index 100% rename from src/modules/outputparsers.ts rename to bkp/outputparsers.ts diff --git a/src/modules/project.ts b/bkp/project.ts similarity index 100% rename from src/modules/project.ts rename to bkp/project.ts diff --git a/src/modules/rag.ts b/bkp/rag.ts similarity index 100% rename from src/modules/rag.ts rename to bkp/rag.ts diff --git a/src/modules/search.ts b/bkp/search.ts similarity index 100% rename from src/modules/search.ts rename to bkp/search.ts diff --git a/src/modules/state.ts b/bkp/state.ts similarity index 100% rename from src/modules/state.ts rename to bkp/state.ts diff --git a/src/modules/task.ts b/bkp/task.ts similarity index 100% rename from src/modules/task.ts rename to bkp/task.ts diff --git a/src/modules/terminal.ts b/bkp/terminal.ts similarity index 100% rename from src/modules/terminal.ts rename to bkp/terminal.ts diff --git a/src/modules/tokenizer.ts b/bkp/tokenizer.ts similarity index 100% rename from src/modules/tokenizer.ts rename to bkp/tokenizer.ts diff --git a/src/modules/vectordb.ts b/bkp/vectordb.ts similarity index 100% rename from src/modules/vectordb.ts rename to bkp/vectordb.ts diff --git a/tsconfig.json b/tsconfig.json index 61b4b22..20fa1a1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -108,7 +108,7 @@ }, "include": [ "src/**/*" - ], +, "bkp/browser.ts", "bkp/codeparsers.ts" ], "exclude": [ "node_modules", "**/*.spec.ts" From b13069c7de3745beeb0b40e8ea2c6b27760470e0 Mon Sep 17 00:00:00 2001 From: Utkarsh Shukla Date: Fri, 20 Sep 2024 16:17:22 +0530 Subject: [PATCH 5/6] updated tsc --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 20fa1a1..831b7d9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -108,7 +108,7 @@ }, "include": [ "src/**/*" -, "bkp/browser.ts", "bkp/codeparsers.ts" ], + ], "exclude": [ "node_modules", "**/*.spec.ts" From 6342abcf9799a357c12c742ea2e78655116a0a02 Mon Sep 17 00:00:00 2001 From: ravi18 Date: Fri, 20 Sep 2024 16:46:48 +0530 Subject: [PATCH 6/6] working code --- index.d.ts | 14 +++++ index.js | 28 ++++++++++ modules/chat.d.ts | 24 +++++++++ modules/chat.js | 113 +++++++++++++++++++++++++++++++++++++++ modules/websocket.d.ts | 29 ++++++++++ modules/websocket.js | 107 ++++++++++++++++++++++++++++++++++++ src/modules/chat.ts | 30 +++++------ src/modules/websocket.ts | 18 ++++++- 8 files changed, 347 insertions(+), 16 deletions(-) create mode 100644 index.d.ts create mode 100644 index.js create mode 100644 modules/chat.d.ts create mode 100644 modules/chat.js create mode 100644 modules/websocket.d.ts create mode 100644 modules/websocket.js diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..07bce39 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,14 @@ +import WebSocket from 'ws'; +import Chat from './modules/chat'; +declare class Codebolt { + private static instance; + private wsManager; + chat: Chat; + constructor(); + static getInstance(): Codebolt; + connect(): Promise; + disconnect(): Promise; + websocket: WebSocket | null; +} +declare const _default: Codebolt; +export default _default; diff --git a/index.js b/index.js new file mode 100644 index 0000000..7851633 --- /dev/null +++ b/index.js @@ -0,0 +1,28 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const websocket_1 = __importDefault(require("./modules/websocket")); +const chat_1 = __importDefault(require("./modules/chat")); +class Codebolt { + constructor() { + this.websocket = null; + this.wsManager = new websocket_1.default(); + this.chat = new chat_1.default(this.wsManager); + } + static getInstance() { + if (!Codebolt.instance) { + Codebolt.instance = new Codebolt(); + } + return Codebolt.instance; + } + async connect() { + await this.wsManager.connect(); + } + async disconnect() { + await this.wsManager.disconnect(); + } +} +Codebolt.instance = null; +exports.default = Codebolt.getInstance(); diff --git a/modules/chat.d.ts b/modules/chat.d.ts new file mode 100644 index 0000000..7843b89 --- /dev/null +++ b/modules/chat.d.ts @@ -0,0 +1,24 @@ +import CbWS from './websocket'; +import { EventEmitter } from 'events'; +import { ChatMessage, UserMessage } from '@codebolt/types'; +declare class CustomEventEmitter extends EventEmitter { +} +declare class Chat { + private wsManager; + private ws; + private eventEmitter; + constructor(wsManager: CbWS); + getChatHistory: () => Promise; + onActionMessage: () => CustomEventEmitter | undefined; + sendMessage: (message: string) => void; + waitforReply: (message: string) => Promise; + processStarted: () => { + event: CustomEventEmitter; + stopProcess: () => void; + }; + stopProcess: () => void; + processFinished: () => void; + sendConfirmationRequest: (confirmationMessage: string, buttons?: string[]) => Promise; + sendNotificationEvent: (notificationMessage: string, type: "debug" | "git" | "planner" | "browser" | "editor" | "terminal" | "preview") => void; +} +export default Chat; diff --git a/modules/chat.js b/modules/chat.js new file mode 100644 index 0000000..eded7b4 --- /dev/null +++ b/modules/chat.js @@ -0,0 +1,113 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const events_1 = require("events"); +class CustomEventEmitter extends events_1.EventEmitter { +} +class Chat { + constructor(wsManager) { + this.getChatHistory = () => { + return new Promise((resolve, reject) => { + this.wsManager.send(JSON.stringify({ + "type": "getChatHistory" + })); + this.wsManager.on((data) => { + const response = JSON.parse(data); + if (response.type === "getChatHistoryResponse") { + resolve(response); + } + }); + }); + }; + this.onActionMessage = () => { + if (!this.ws) + return; + this.wsManager.on((data) => { + const response = JSON.parse(data); + if (response.type === "messageResponse") { + this.eventEmitter.emit("userMessage", response, (message) => { + console.log("Callback function invoked with message:", message); + this.wsManager.send(JSON.stringify({ + "type": "processStoped" + })); + }); + } + }); + return this.eventEmitter; + }; + this.sendMessage = (message) => { + console.log(message); + this.wsManager.send(JSON.stringify({ + "type": "sendMessage", + "message": message + })); + }; + this.waitforReply = (message) => { + return new Promise((resolve, reject) => { + this.wsManager.send(JSON.stringify({ + "type": "waitforReply", + "message": message + })); + this.wsManager.on((data) => { + const response = JSON.parse(data); + if (response.type === "waitFormessageResponse") { + resolve(response); + } + }); + }); + }; + this.processStarted = () => { + this.wsManager.send(JSON.stringify({ + "type": "processStarted" + })); + this.wsManager.on((data) => { + const message = JSON.parse(data); + console.log("Received message:", message); + if (message.type === 'stopProcessClicked') { + this.eventEmitter.emit("stopProcessClicked", message); + } + }); + return { + event: this.eventEmitter, + stopProcess: this.stopProcess + }; + }; + this.stopProcess = () => { + console.log("Stopping process..."); + this.wsManager.send(JSON.stringify({ + "type": "processStoped" + })); + }; + this.processFinished = () => { + console.log("Process Finished ..."); + this.wsManager.send(JSON.stringify({ + "type": "processFinished" + })); + }; + this.sendConfirmationRequest = (confirmationMessage, buttons = []) => { + return new Promise((resolve, reject) => { + this.wsManager.send(JSON.stringify({ + "type": "confirmationRequest", + "message": confirmationMessage, + buttons: buttons + })); + this.wsManager.on((data) => { + const response = JSON.parse(data); + if (response.type === "confirmationResponse") { + resolve(response); + } + }); + }); + }; + this.sendNotificationEvent = (notificationMessage, type) => { + this.wsManager.send(JSON.stringify({ + "type": "notificationEvent", + "message": notificationMessage, + "eventType": type + })); + }; + this.wsManager = wsManager; + // this.ws = this.wsManager.getWebsocket(); + this.eventEmitter = new CustomEventEmitter(); + } +} +exports.default = Chat; diff --git a/modules/websocket.d.ts b/modules/websocket.d.ts new file mode 100644 index 0000000..1a46514 --- /dev/null +++ b/modules/websocket.d.ts @@ -0,0 +1,29 @@ +import WebSocket from 'ws'; +/** + * Class representing a WebSocket connection. + */ +declare class CbWS { + websocket: WebSocket | null; + /** + * Constructs a new cbws instance and initializes the WebSocket connection. + */ + connect(): Promise; + disconnect(): Promise; + private getUniqueConnectionId; + private getInitialMessage; + /** + * Initializes the WebSocket by setting up event listeners and returning a promise that resolves + * when the WebSocket connection is successfully opened. + * @returns {Promise} A promise that resolves with the WebSocket instance. + */ + private initializeWebSocket; + /** + * Getter for the WebSocket instance. Throws an error if the WebSocket is not open. + * @returns {WebSocket} The WebSocket instance. + * @throws {Error} If the WebSocket is not open. + */ + getWebsocket(): WebSocket; + send(data: any): void; + on(callback: (data: string) => void): void; +} +export default CbWS; diff --git a/modules/websocket.js b/modules/websocket.js new file mode 100644 index 0000000..e9b6e16 --- /dev/null +++ b/modules/websocket.js @@ -0,0 +1,107 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const ws_1 = __importDefault(require("ws")); +const fs_1 = __importDefault(require("fs")); +const js_yaml_1 = __importDefault(require("js-yaml")); +/** + * Class representing a WebSocket connection. + */ +class CbWS { + constructor() { + this.websocket = null; + } + /** + * Constructs a new cbws instance and initializes the WebSocket connection. + */ + async connect() { + if (this.websocket && this.websocket.readyState === ws_1.default.OPEN) { + return this.websocket; + } + const uniqueConnectionId = this.getUniqueConnectionId(); + const initialMessage = this.getInitialMessage(); + this.websocket = new ws_1.default(`ws://localhost:12345/codebolt?id=${uniqueConnectionId}`); + return await this.initializeWebSocket(initialMessage); + } + async disconnect() { + if (this.websocket) { + this.websocket.close(); + this.websocket = null; + } + } + getUniqueConnectionId() { + try { + let fileContents = fs_1.default.readFileSync('./codeboltagent.yaml', 'utf8'); + let data = js_yaml_1.default.load(fileContents); + return data.unique_connectionid; + } + catch (e) { + console.error('Unable to locate codeboltagent.yaml file.'); + return ''; + } + } + getInitialMessage() { + try { + let fileContents = fs_1.default.readFileSync('./codeboltagent.yaml', 'utf8'); + let data = js_yaml_1.default.load(fileContents); + return data.initial_message; + } + catch (e) { + console.error('Unable to locate codeboltagent.yaml file.'); + return ''; + } + } + /** + * Initializes the WebSocket by setting up event listeners and returning a promise that resolves + * when the WebSocket connection is successfully opened. + * @returns {Promise} A promise that resolves with the WebSocket instance. + */ + async initializeWebSocket(initialMessage) { + return new Promise((resolve, reject) => { + if (!this.websocket) { + reject(new Error('WebSocket is not initialized')); + return; + } + this.websocket.on('error', (error) => { + console.log('WebSocket error:', error); + reject(error); + }); + this.websocket.on('open', () => { + console.log('WebSocket connected'); + resolve(this.websocket); + }); + this.websocket.on('message', (data) => { + // Handle incoming WebSocket messages here. + // console.log('WebSocket message received:', data); + }); + }); + } + /** + * Getter for the WebSocket instance. Throws an error if the WebSocket is not open. + * @returns {WebSocket} The WebSocket instance. + * @throws {Error} If the WebSocket is not open. + */ + getWebsocket() { + if (!this.websocket || this.websocket.readyState !== ws_1.default.OPEN) { + throw new Error('WebSocket is not open'); + } + return this.websocket; + } + send(data) { + if (!this.websocket || this.websocket.readyState !== ws_1.default.OPEN) { + console.error('WebSocket is not open. Unable to send data.'); + return; + } + this.websocket.send(JSON.stringify(data)); + } + on(callback) { + if (!this.websocket) { + console.error('WebSocket is not initialized. Unable to set up message listener.'); + return; + } + this.websocket.on('message', callback); + } +} +exports.default = CbWS; diff --git a/src/modules/chat.ts b/src/modules/chat.ts index fc43713..972ecf3 100644 --- a/src/modules/chat.ts +++ b/src/modules/chat.ts @@ -11,16 +11,16 @@ class Chat { constructor(wsManager: CbWS) { this.wsManager = wsManager; - this.ws = this.wsManager.getWebsocket() as any; + // this.ws = this.wsManager.getWebsocket(); this.eventEmitter = new CustomEventEmitter(); } getChatHistory = (): Promise => { return new Promise((resolve, reject) => { - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "getChatHistory" })); - this.ws.on('message', (data: string) => { + this.wsManager.on((data) => { const response = JSON.parse(data); if (response.type === "getChatHistoryResponse") { resolve(response); @@ -31,12 +31,12 @@ class Chat { onActionMessage = () => { if (!this.ws) return; - this.ws.on('message', (data: string) => { + this.wsManager.on((data: string) => { const response = JSON.parse(data); if (response.type === "messageResponse") { this.eventEmitter.emit("userMessage", response, (message: string) => { console.log("Callback function invoked with message:", message); - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "processStoped" })); }); @@ -47,7 +47,7 @@ class Chat { sendMessage = (message: string) => { console.log(message); - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "sendMessage", "message": message })); @@ -55,11 +55,11 @@ class Chat { waitforReply = (message: string): Promise => { return new Promise((resolve, reject) => { - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "waitforReply", "message": message })); - this.ws.on('message', (data: string) => { + this.wsManager.on((data: string) => { const response = JSON.parse(data); if (response.type === "waitFormessageResponse") { resolve(response); @@ -69,10 +69,10 @@ class Chat { } processStarted = () => { - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "processStarted" })); - this.ws.on('message', (data: string) => { + this.wsManager.on((data: string) => { const message = JSON.parse(data); console.log("Received message:", message); if (message.type === 'stopProcessClicked') { @@ -88,26 +88,26 @@ class Chat { stopProcess = () => { console.log("Stopping process..."); - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "processStoped" })); } processFinished = () => { console.log("Process Finished ..."); - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "processFinished" })); } sendConfirmationRequest = (confirmationMessage: string, buttons: string[] = []): Promise => { return new Promise((resolve, reject) => { - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "confirmationRequest", "message": confirmationMessage, buttons: buttons })); - this.ws.on('message', (data: string) => { + this.wsManager.on((data: string) => { const response = JSON.parse(data); if (response.type === "confirmationResponse") { resolve(response); @@ -117,7 +117,7 @@ class Chat { } sendNotificationEvent = (notificationMessage: string, type: 'debug' | 'git' | 'planner' | 'browser' | 'editor' | 'terminal' | 'preview'): void => { - this.ws.send(JSON.stringify({ + this.wsManager.send(JSON.stringify({ "type": "notificationEvent", "message": notificationMessage, "eventType": type diff --git a/src/modules/websocket.ts b/src/modules/websocket.ts index 454636b..0261187 100644 --- a/src/modules/websocket.ts +++ b/src/modules/websocket.ts @@ -6,7 +6,7 @@ import yaml from 'js-yaml'; * Class representing a WebSocket connection. */ class CbWS { - private websocket: WebSocket | null = null; + public websocket: WebSocket | null = null; /** * Constructs a new cbws instance and initializes the WebSocket connection. @@ -89,6 +89,22 @@ class CbWS { } return this.websocket; } + + send(data: any): void { + if (!this.websocket || this.websocket.readyState !== WebSocket.OPEN) { + console.error('WebSocket is not open. Unable to send data.'); + return; + } + this.websocket.send(JSON.stringify(data)); + } + + on(callback: (data: string) => void): void { + if (!this.websocket) { + console.error('WebSocket is not initialized. Unable to set up message listener.'); + return; + } + this.websocket.on('message', callback); + } } export default CbWS;