@codebolt/codeboltjs 5.0.0 → 5.0.2

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.
@@ -13,6 +13,7 @@ declare class Codebolt {
13
13
  private readyHandlers;
14
14
  private messageQueue;
15
15
  private messageResolvers;
16
+ private lastUserMessage;
16
17
  /**
17
18
  * @constructor
18
19
  * @description Initializes the websocket connection.
@@ -380,10 +381,16 @@ declare class Codebolt {
380
381
  */
381
382
  onReady(handler: () => void | Promise<void>): void;
382
383
  /**
383
- * Waits for and returns the next incoming message.
384
- * If a message is already in the queue, returns it immediately.
385
- * Otherwise, waits for the next message to arrive.
386
- * @returns {Promise<FlatUserMessage>} A promise that resolves with the next message
384
+ * Gets the current or next incoming message.
385
+ * Priority order:
386
+ * 1. Returns the current message being processed (if called during message handling)
387
+ * 2. Returns a queued message (if any are waiting)
388
+ * 3. Waits for the next message to arrive
389
+ *
390
+ * This allows getMessage() to work both during active message processing
391
+ * and when waiting for new messages in a loop.
392
+ *
393
+ * @returns {Promise<FlatUserMessage>} A promise that resolves with the message
387
394
  */
388
395
  getMessage(): Promise<FlatUserMessage>;
389
396
  /**
@@ -392,8 +399,16 @@ declare class Codebolt {
392
399
  * @private
393
400
  */
394
401
  private handleIncomingMessage;
402
+ /**
403
+ * Sets up a background listener for all messageResponse messages from the socket.
404
+ * This ensures that getMessage() promises are always resolved even if onMessage() is not called.
405
+ * @private
406
+ */
407
+ private setupMessageListener;
395
408
  /**
396
409
  * Sets up a listener for incoming messages with a direct handler function.
410
+ * Note: Message extraction and resolver handling is done by setupMessageListener.
411
+ * This method only adds the custom handler logic and sends processStoped response.
397
412
  * @param {Function} handler - The handler function to call when a message is received.
398
413
  * @returns {void}
399
414
  */
@@ -79,6 +79,8 @@ class Codebolt {
79
79
  this.userMessage = user_message_utilities_1.userMessageUtilities;
80
80
  console.log("Codebolt Agent initialized");
81
81
  this.readyPromise = this.initializeConnection();
82
+ this.setupMessageListener();
83
+ this.lastUserMessage = undefined;
82
84
  }
83
85
  /**
84
86
  * @method initializeConnection
@@ -149,18 +151,36 @@ class Codebolt {
149
151
  }
150
152
  }
151
153
  /**
152
- * Waits for and returns the next incoming message.
153
- * If a message is already in the queue, returns it immediately.
154
- * Otherwise, waits for the next message to arrive.
155
- * @returns {Promise<FlatUserMessage>} A promise that resolves with the next message
154
+ * Gets the current or next incoming message.
155
+ * Priority order:
156
+ * 1. Returns the current message being processed (if called during message handling)
157
+ * 2. Returns a queued message (if any are waiting)
158
+ * 3. Waits for the next message to arrive
159
+ *
160
+ * This allows getMessage() to work both during active message processing
161
+ * and when waiting for new messages in a loop.
162
+ *
163
+ * @returns {Promise<FlatUserMessage>} A promise that resolves with the message
156
164
  */
157
- getMessage() {
165
+ async getMessage() {
166
+ // Wait for WebSocket to be ready first
167
+ console.log('[Codebolt] getMessage called');
168
+ // await this.waitForReady();
169
+ // First, check if there's a current message being processed
170
+ const currentMessage = user_message_manager_1.userMessageManager.getMessage();
171
+ if (currentMessage) {
172
+ console.log('[Codebolt] Returning current message');
173
+ return Promise.resolve(currentMessage);
174
+ }
158
175
  // If there are queued messages, return the first one
159
- if (this.messageQueue.length > 0) {
160
- const message = this.messageQueue.shift();
161
- return Promise.resolve(message);
176
+ if (this.messageQueue.length > 0 || this.lastUserMessage) {
177
+ console.log('[Codebolt] Returning queued message');
178
+ const message = this.messageQueue.shift() || this.lastUserMessage;
179
+ if (message)
180
+ return Promise.resolve(message);
162
181
  }
163
182
  // Otherwise, create a new promise that will be resolved when a message arrives
183
+ console.log('[Codebolt] Waiting for next message');
164
184
  return new Promise((resolve) => {
165
185
  this.messageResolvers.push(resolve);
166
186
  });
@@ -181,8 +201,55 @@ class Codebolt {
181
201
  this.messageQueue.push(message);
182
202
  }
183
203
  }
204
+ /**
205
+ * Sets up a background listener for all messageResponse messages from the socket.
206
+ * This ensures that getMessage() promises are always resolved even if onMessage() is not called.
207
+ * @private
208
+ */
209
+ setupMessageListener() {
210
+ console.log("listener setup");
211
+ this.waitForReady().then(() => {
212
+ const handleSocketMessage = async (response) => {
213
+ var _a, _b;
214
+ console.log(response);
215
+ if (response.type === "messageResponse") {
216
+ // Extract user-facing message from internal socket message
217
+ const userMessage = {
218
+ userMessage: response.message.userMessage,
219
+ currentFile: response.message.currentFile,
220
+ mentionedFiles: response.message.mentionedFiles || [],
221
+ mentionedFullPaths: response.message.mentionedFullPaths || [],
222
+ mentionedFolders: response.message.mentionedFolders || [],
223
+ uploadedImages: response.message.uploadedImages || [],
224
+ mentionedMCPs: response.message.mentionedMCPs || [],
225
+ selectedAgent: {
226
+ id: ((_a = response.message.selectedAgent) === null || _a === void 0 ? void 0 : _a.id) || '',
227
+ name: ((_b = response.message.selectedAgent) === null || _b === void 0 ? void 0 : _b.name) || ''
228
+ },
229
+ messageId: response.message.messageId,
230
+ threadId: response.message.threadId,
231
+ selection: response.message.selection,
232
+ remixPrompt: response.message.remixPrompt,
233
+ mentionedAgents: response.message.mentionedAgents || [],
234
+ activeFile: response.message.activeFile,
235
+ openedFiles: response.message.activeFile
236
+ };
237
+ // Automatically save the user message globally
238
+ user_message_manager_1.userMessageManager.saveMessage(userMessage);
239
+ this.lastUserMessage = userMessage;
240
+ // Handle the message in the queue system for getMessage() resolvers
241
+ this.handleIncomingMessage(userMessage);
242
+ }
243
+ };
244
+ websocket_1.default.messageManager.on('message', handleSocketMessage);
245
+ }).catch(error => {
246
+ console.error('Failed to set up background message listener:', error);
247
+ });
248
+ }
184
249
  /**
185
250
  * Sets up a listener for incoming messages with a direct handler function.
251
+ * Note: Message extraction and resolver handling is done by setupMessageListener.
252
+ * This method only adds the custom handler logic and sends processStoped response.
186
253
  * @param {Function} handler - The handler function to call when a message is received.
187
254
  * @returns {void}
188
255
  */
@@ -215,11 +282,7 @@ class Codebolt {
215
282
  activeFile: response.message.activeFile,
216
283
  openedFiles: response.message.activeFile
217
284
  };
218
- // Automatically save the user message globally
219
- user_message_manager_1.userMessageManager.saveMessage(userMessage);
220
- // Handle the message in the queue system
221
- this.handleIncomingMessage(userMessage);
222
- // Call the original handler
285
+ // Call the custom handler
223
286
  const result = await handler(userMessage);
224
287
  // Send processStoped with optional message
225
288
  const message = {
@@ -65,21 +65,21 @@ class cbws {
65
65
  const socketPort = process.env.SOCKET_PORT || '12345';
66
66
  const serverUrl = process.env.CODEBOLT_SERVER_URL || 'localhost';
67
67
  const threadToken = process.env.threadToken || null;
68
- console.log('[WebSocket] Logging all relevant variables:');
69
- console.log('uniqueConnectionId:', uniqueConnectionId);
68
+ // console.log('[WebSocket] Logging all relevant variables:');
69
+ // console.log('uniqueConnectionId:', uniqueConnectionId);
70
70
  // console.log('initialMessage:', initialMessage);
71
- console.log('agentIdParam:', agentIdParam);
72
- console.log('parentIdParam:', parentIdParam);
73
- console.log('parentAgentInstanceIdParam:', parentAgentInstanceIdParam);
74
- console.log('agentTask:', agentTask);
75
- console.log('socketPort:', socketPort);
76
- console.log('serverUrl:', serverUrl);
77
- console.log('threadToken:', threadToken);
78
- console.log('[WebSocket] Environment variables check:');
79
- console.log('process.env.agentId:', process.env.agentId);
80
- console.log('process.env.threadToken:', process.env.threadToken);
81
- console.log('process.env.parentId:', process.env.parentId);
82
- console.log('process.env.agentTask:', process.env.agentTask);
71
+ // console.log('agentIdParam:', agentIdParam);
72
+ // console.log('parentIdParam:', parentIdParam);
73
+ // console.log('parentAgentInstanceIdParam:', parentAgentInstanceIdParam);
74
+ // console.log('agentTask:', agentTask);
75
+ // console.log('socketPort:', socketPort);
76
+ // console.log('serverUrl:', serverUrl);
77
+ // console.log('threadToken:', threadToken);
78
+ // console.log('[WebSocket] Environment variables check:');
79
+ // console.log('process.env.agentId:', process.env.agentId);
80
+ // console.log('process.env.threadToken:', process.env.threadToken);
81
+ // console.log('process.env.parentId:', process.env.parentId);
82
+ // console.log('process.env.agentTask:', process.env.agentTask);
83
83
  const threadTokenParam = threadToken ? `&threadToken=${encodeURIComponent(threadToken)}` : '';
84
84
  // Add all custom environment variables as URL parameters
85
85
  const knownEnvVars = ['SOCKET_PORT', 'CODEBOLT_SERVER_URL', 'agentId', 'parentId', 'parentAgentInstanceId', 'agentTask', 'threadToken', 'Is_Dev', 'PATH', 'NODE_ENV', 'HOME', 'USER', 'SHELL'];
@@ -90,7 +90,7 @@ class cbws {
90
90
  }
91
91
  }
92
92
  const wsUrl = `ws://${serverUrl}:${socketPort}/codebolt?id=${uniqueConnectionId}${agentIdParam}${parentIdParam}${parentAgentInstanceIdParam}${agentTask}${threadTokenParam}${customParams}${process.env.Is_Dev ? '&dev=true' : ''}`;
93
- console.log('[WebSocket] Connecting to:', wsUrl);
93
+ console.log('[WebSocket] Connecting to:', serverUrl);
94
94
  this.websocket = new ws_1.default(wsUrl);
95
95
  return new Promise((resolve, reject) => {
96
96
  // Set a timeout for the connection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebolt/codeboltjs",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "",
@@ -23,6 +23,7 @@
23
23
  "access": "public"
24
24
  },
25
25
  "dependencies": {
26
+ "@codebolt/types": "latest",
26
27
  "@types/uuid": "^10.0.0",
27
28
  "buffer": "^6.0.3",
28
29
  "execa": "^9.5.2",
@@ -34,8 +35,7 @@
34
35
  "util": "^0.12.5",
35
36
  "uuid": "^11.1.0",
36
37
  "ws": "^8.18.3",
37
- "yargs": "^17.7.2",
38
- "@codebolt/types": "1.0.22"
38
+ "yargs": "^17.7.2"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/events": "^3.0.3",