Skip to content

Commit 0de700f

Browse files
committed
2 parents a5649e5 + b6b32fd commit 0de700f

35 files changed

+394
-277
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codebolt/codeboltjs",
3-
"version": "2.0.13",
3+
"version": "2.0.15",
44
"description": "",
55
"keywords": [],
66
"author": "",

src/core/Codebolt.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Codebolt {
3535
websocket: WebSocket | null = null;
3636
private isReady: boolean = false;
3737
private readyPromise: Promise<void>;
38+
private readyHandlers: Array<() => void | Promise<void>> = [];
3839

3940
/**
4041
* @constructor
@@ -56,6 +57,15 @@ class Codebolt {
5657
this.websocket = cbws.getWebsocket;
5758
this.isReady = true;
5859
console.log("Codebolt WebSocket connection established");
60+
61+
// Execute all registered ready handlers
62+
for (const handler of this.readyHandlers) {
63+
try {
64+
await handler();
65+
} catch (error) {
66+
console.error('Error executing ready handler:', error);
67+
}
68+
}
5969
} catch (error) {
6070
console.error('Failed to initialize WebSocket connection:', error);
6171
throw error;
@@ -106,6 +116,31 @@ class Codebolt {
106116
utils = cbutils;
107117
notify = notificationFunctions;
108118

119+
/**
120+
* Sets up a handler function to be executed when the WebSocket connection is established.
121+
* If the connection is already established, the handler will be executed immediately.
122+
* @param {Function} handler - The handler function to call when the connection is ready.
123+
* @returns {void}
124+
*/
125+
onReady(handler: () => void | Promise<void>) {
126+
if (this.isReady) {
127+
// If already ready, execute the handler immediately
128+
try {
129+
const result = handler();
130+
if (result instanceof Promise) {
131+
result.catch(error => {
132+
console.error('Error in ready handler:', error);
133+
});
134+
}
135+
} catch (error) {
136+
console.error('Error in ready handler:', error);
137+
}
138+
} else {
139+
// If not ready yet, add to the list of handlers to execute when ready
140+
this.readyHandlers.push(handler);
141+
}
142+
}
143+
109144
/**
110145
* Sets up a listener for incoming messages with a direct handler function.
111146
* @param {Function} handler - The handler function to call when a message is received.

src/core/websocket.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,9 @@ class cbws {
6666
const parentAgentInstanceIdParam = process.env.parentAgentInstanceId ? `&parentAgentInstanceId=${process.env.parentAgentInstanceId}` : '';
6767
const agentTask = process.env.agentTask ? `&agentTask=${process.env.agentTask}` : '';
6868
const socketPort = process.env.SOCKET_PORT || '12345';
69+
const serverUrl = process.env.CODEBOLT_SERVER_URL || 'localhost';
6970

70-
// const wsUrl = `ws://100.126.109.83:${socketPort}/codebolt?id=${uniqueConnectionId}${agentIdParam}${parentIdParam}${parentAgentInstanceIdParam}${agentTask}${process.env.Is_Dev ? '&dev=true' : ''}`;
71-
72-
const wsUrl = `ws://localhost:${socketPort}/codebolt?id=${uniqueConnectionId}${agentIdParam}${parentIdParam}${parentAgentInstanceIdParam}${agentTask}${process.env.Is_Dev ? '&dev=true' : ''}`;
73-
74-
71+
const wsUrl = `ws://${serverUrl}:${socketPort}/codebolt?id=${uniqueConnectionId}${agentIdParam}${parentIdParam}${parentAgentInstanceIdParam}${agentTask}${process.env.Is_Dev ? '&dev=true' : ''}`;
7572
console.log('[WebSocket] Connecting to:', wsUrl);
7673

7774
this.websocket = new WebSocket(wsUrl);

src/notificationfunctions/agent.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,14 @@ import {
1111
SubagentTaskCompletedNotification
1212
} from '../types/notifications/agent';
1313

14+
import { AgentNotifications } from '../types/notificationFunctions/agent';
15+
1416
import {
1517
sendNotification,
1618
generateToolUseId,
17-
validateRequiredFields,
18-
createErrorResponse,
19-
createSuccessResponse
19+
validateRequiredFields
2020
} from './utils';
2121

22-
/**
23-
* Interface for agent notification functions
24-
*/
25-
export interface AgentNotifications {
26-
StartSubagentTaskRequestNotify(parentAgentId: string, subagentId: string, task: string, priority?: string, dependencies?: string[], toolUseId?: string): void;
27-
StartSubagentTaskResponseNotify(content: string | any, isError?: boolean, toolUseId?: string): void;
28-
SubagentTaskCompletedNotify(parentAgentId: string, subagentId: string, taskId: string, result: any, status: string, toolUseId?: string): void;
29-
}
30-
3122
/**
3223
* Sends a request to start a subagent task
3324
*

src/notificationfunctions/browser.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,14 @@ import {
1212
WebSearchResponseNotification
1313
} from '../types/notifications/browser';
1414

15+
import { BrowserNotifications } from '../types/notificationFunctions/browser';
16+
1517
import {
1618
sendNotification,
1719
generateToolUseId,
1820
validateRequiredFields
1921
} from './utils';
2022

21-
/**
22-
* Interface for browser notification functions
23-
*/
24-
export interface BrowserNotifications {
25-
WebFetchRequestNotify(url: string, method?: string, headers?: Record<string, string>, body?: any, timeout?: number, toolUseId?: string): void;
26-
WebFetchResponseNotify(content: string | any, isError?: boolean, toolUseId?: string, data?: WebFetchResponseNotification['data']): void;
27-
WebSearchRequestNotify(query: string, maxResults?: number, searchEngine?: string, filters?: any, toolUseId?: string): void;
28-
WebSearchResponseNotify(content: string | any, isError?: boolean, toolUseId?: string, data?: WebSearchResponseNotification['data']): void;
29-
}
3023

3124
/**
3225
* Sends a web fetch request notification

src/notificationfunctions/chat.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
GetChatHistoryResultNotification
1313
} from '../types/notifications/chat';
1414

15+
import { ChatNotifications } from '../types/notificationFunctions/chat';
16+
1517
import {
1618
sendNotification,
1719
generateToolUseId,
@@ -20,15 +22,6 @@ import {
2022
createSuccessResponse
2123
} from './utils';
2224

23-
/**
24-
* Interface for chat notification functions
25-
*/
26-
export interface ChatNotifications {
27-
UserMessageRequestNotify(message: string, payload?: any, toolUseId?: string): void;
28-
AgentTextResponseNotify(content: string | any, isError?: boolean, toolUseId?: string, data?: AgentTextResponseNotification['data']): void;
29-
GetChatHistoryRequestNotify(data?: GetChatHistoryRequestNotification['data'], toolUseId?: string): void;
30-
GetChatHistoryResultNotify(content: string | any, isError?: boolean, toolUseId?: string): void;
31-
}
3225

3326
/**
3427
* Sends a user message request

src/notificationfunctions/codeutils.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,14 @@ import {
1212
GlobSearchResponseNotification
1313
} from '../types/notifications/codeutils';
1414

15+
import { CodeutilsNotifications } from '../types/notificationFunctions/codeutils';
16+
1517
import {
1618
sendNotification,
1719
generateToolUseId,
1820
validateRequiredFields
1921
} from './utils';
2022

21-
/**
22-
* Interface for code utils notification functions
23-
*/
24-
export interface CodeutilsNotifications {
25-
GrepSearchRequestNotify(pattern: string, filePath?: string, recursive?: boolean, ignoreCase?: boolean, maxResults?: number, toolUseId?: string): void;
26-
GrepSearchResponseNotify(content: string | any, isError?: boolean, toolUseId?: string, data?: GrepSearchResponseNotification['data']): void;
27-
GlobSearchRequestNotify(pattern: string, basePath?: string, maxDepth?: number, includeDirectories?: boolean, toolUseId?: string): void;
28-
GlobSearchResponseNotify(content: string | any, isError?: boolean, toolUseId?: string, data?: GlobSearchResponseNotification['data']): void;
29-
}
3023

3124
/**
3225
* Sends a grep search request

src/notificationfunctions/crawler.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,14 @@ import {
1212
CrawlerStartResponseNotification
1313
} from '../types/notifications/crawler';
1414

15+
import { CrawlerNotifications } from '../types/notificationFunctions/crawler';
16+
1517
import {
1618
sendNotification,
1719
generateToolUseId,
18-
validateRequiredFields,
19-
createErrorResponse,
20-
createSuccessResponse
20+
validateRequiredFields
2121
} from './utils';
2222

23-
/**
24-
* Interface for crawler notification functions
25-
*/
26-
export interface CrawlerNotifications {
27-
CrawlerSearchRequestNotify(url: string, searchQuery?: string, maxDepth?: number, maxPages?: number, includeSubdomains?: boolean, followRedirects?: boolean, toolUseId?: string): void;
28-
CrawlerSearchResponseNotify(content: string | any, isError?: boolean, toolUseId?: string, data?: CrawlerSearchResponseNotification['data']): void;
29-
CrawlerStartRequestNotify(startUrl: string, options?: any, toolUseId?: string): void;
30-
CrawlerStartResponseNotify(content: string | any, isError?: boolean, toolUseId?: string, data?: CrawlerStartResponseNotification['data']): void;
31-
}
3223

3324
/**
3425
* Sends a request to perform a crawler search

src/notificationfunctions/dbmemory.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,14 @@ import {
1212
GetMemoryResultNotification
1313
} from '../types/notifications/dbmemory';
1414

15+
import { DbmemoryNotifications } from '../types/notificationFunctions/dbmemory';
16+
1517
import {
1618
sendNotification,
1719
generateToolUseId,
18-
validateRequiredFields,
19-
createErrorResponse,
20-
createSuccessResponse
20+
validateRequiredFields
2121
} from './utils';
2222

23-
/**
24-
* Interface for database memory notification functions
25-
*/
26-
export interface DbmemoryNotifications {
27-
AddMemoryRequestNotify(key: string, value: any, toolUseId?: string): void;
28-
AddMemoryResultNotify(content: string | any, isError?: boolean, toolUseId?: string): void;
29-
GetMemoryRequestNotify(key: string, toolUseId?: string): void;
30-
GetMemoryResultNotify(content: string | any, isError?: boolean, toolUseId?: string): void;
31-
}
3223

3324
/**
3425
* Sends a request to add knowledge to memory

0 commit comments

Comments
 (0)