@codebolt/codeboltjs 1.1.88 → 1.1.90

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.
Files changed (38) hide show
  1. package/.github/workflows/publish-to-npm.yml +1 -1
  2. package/index.d.ts +6 -3
  3. package/index.js +1 -1
  4. package/modules/agent.d.ts +26 -1
  5. package/modules/agent.js +65 -4
  6. package/modules/agentlib/agent.d.ts +23 -0
  7. package/modules/agentlib/agent.js +175 -0
  8. package/modules/agentlib/systemprompt.d.ts +20 -0
  9. package/modules/agentlib/systemprompt.js +48 -0
  10. package/modules/agentlib/taskInstruction.d.ts +22 -0
  11. package/modules/agentlib/taskInstruction.js +37 -0
  12. package/modules/agentlib/usermessage.d.ts +22 -0
  13. package/modules/agentlib/usermessage.js +70 -0
  14. package/modules/chat.d.ts +11 -1
  15. package/modules/chat.js +52 -12
  16. package/modules/fs.d.ts +1 -1
  17. package/modules/mcp.js +0 -15
  18. package/modules/toolBox.d.ts +262 -0
  19. package/modules/toolBox.js +720 -0
  20. package/modules/websocket.d.ts +1 -1
  21. package/modules/websocket.js +13 -11
  22. package/package.json +28 -4
  23. package/src/index.ts +6 -2
  24. package/src/modules/agent.ts +70 -4
  25. package/src/modules/agentlib/agent.ts +226 -0
  26. package/src/modules/agentlib/package-lock.json +282 -0
  27. package/src/modules/agentlib/package.json +6 -0
  28. package/src/modules/agentlib/systemprompt.ts +55 -0
  29. package/src/modules/agentlib/taskInstruction.ts +66 -0
  30. package/src/modules/agentlib/usermessage.ts +97 -0
  31. package/src/modules/chat.ts +56 -15
  32. package/src/modules/fs.ts +1 -1
  33. package/src/modules/mcp.ts +5 -15
  34. package/src/modules/toolBox.ts +1164 -0
  35. package/src/modules/websocket.ts +20 -13
  36. package/src/utils.ts +5 -0
  37. package/utils.d.ts +5 -0
  38. package/utils.js +13 -0
@@ -0,0 +1,282 @@
1
+ {
2
+ "name": "lib",
3
+ "lockfileVersion": 3,
4
+ "requires": true,
5
+ "packages": {
6
+ "": {
7
+ "dependencies": {
8
+ "@codebolt/codeboltjs": "^1.1.89",
9
+ "js-yaml": "^4.1.0"
10
+ }
11
+ },
12
+ "node_modules/@codebolt/codeboltjs": {
13
+ "version": "1.1.89",
14
+ "resolved": "https://registry.npmjs.org/@codebolt/codeboltjs/-/codeboltjs-1.1.89.tgz",
15
+ "integrity": "sha512-hnYgk2U+jSCQUmfUqqqIZ6NDgY+dQ77scUZkKAD1364+khtxE0iQx3PmMKolLZt4Z9VEWFzgVX37S/9683OV1w==",
16
+ "license": "MIT",
17
+ "dependencies": {
18
+ "@codebolt/types": "^1.0.10",
19
+ "js-yaml": "^4.1.0",
20
+ "tree-sitter": "^0.21.1",
21
+ "tree-sitter-javascript": "^0.23.1",
22
+ "tree-sitter-typescript": "^0.23.2",
23
+ "typedoc-plugin-missing-exports": "^2.2.0",
24
+ "ws": "^8.17.0"
25
+ }
26
+ },
27
+ "node_modules/@codebolt/types": {
28
+ "version": "1.0.10",
29
+ "resolved": "https://registry.npmjs.org/@codebolt/types/-/types-1.0.10.tgz",
30
+ "integrity": "sha512-tQgFq9Lo9oJNIZHOvvyVjcsE+1Tk515VhMel2YyoDZvKFbfQauNripAfaKDOxBO4naZ8davqmq3pXsDTX8vb8w==",
31
+ "license": "ISC"
32
+ },
33
+ "node_modules/ansi-sequence-parser": {
34
+ "version": "1.1.3",
35
+ "resolved": "https://registry.npmjs.org/ansi-sequence-parser/-/ansi-sequence-parser-1.1.3.tgz",
36
+ "integrity": "sha512-+fksAx9eG3Ab6LDnLs3ZqZa8KVJ/jYnX+D4Qe1azX+LFGFAXqynCQLOdLpNYN/l9e7l6hMWwZbrnctqr6eSQSw==",
37
+ "license": "MIT",
38
+ "peer": true
39
+ },
40
+ "node_modules/argparse": {
41
+ "version": "2.0.1",
42
+ "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
43
+ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
44
+ "license": "Python-2.0"
45
+ },
46
+ "node_modules/balanced-match": {
47
+ "version": "1.0.2",
48
+ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
49
+ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
50
+ "license": "MIT",
51
+ "peer": true
52
+ },
53
+ "node_modules/brace-expansion": {
54
+ "version": "2.0.1",
55
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
56
+ "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
57
+ "license": "MIT",
58
+ "peer": true,
59
+ "dependencies": {
60
+ "balanced-match": "^1.0.0"
61
+ }
62
+ },
63
+ "node_modules/js-yaml": {
64
+ "version": "4.1.0",
65
+ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
66
+ "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
67
+ "license": "MIT",
68
+ "dependencies": {
69
+ "argparse": "^2.0.1"
70
+ },
71
+ "bin": {
72
+ "js-yaml": "bin/js-yaml.js"
73
+ }
74
+ },
75
+ "node_modules/jsonc-parser": {
76
+ "version": "3.3.1",
77
+ "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.3.1.tgz",
78
+ "integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==",
79
+ "license": "MIT",
80
+ "peer": true
81
+ },
82
+ "node_modules/lunr": {
83
+ "version": "2.3.9",
84
+ "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz",
85
+ "integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==",
86
+ "license": "MIT",
87
+ "peer": true
88
+ },
89
+ "node_modules/marked": {
90
+ "version": "4.3.0",
91
+ "resolved": "https://registry.npmjs.org/marked/-/marked-4.3.0.tgz",
92
+ "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==",
93
+ "license": "MIT",
94
+ "peer": true,
95
+ "bin": {
96
+ "marked": "bin/marked.js"
97
+ },
98
+ "engines": {
99
+ "node": ">= 12"
100
+ }
101
+ },
102
+ "node_modules/minimatch": {
103
+ "version": "9.0.5",
104
+ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
105
+ "integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==",
106
+ "license": "ISC",
107
+ "peer": true,
108
+ "dependencies": {
109
+ "brace-expansion": "^2.0.1"
110
+ },
111
+ "engines": {
112
+ "node": ">=16 || 14 >=14.17"
113
+ },
114
+ "funding": {
115
+ "url": "https://github.com/sponsors/isaacs"
116
+ }
117
+ },
118
+ "node_modules/node-addon-api": {
119
+ "version": "8.3.0",
120
+ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-8.3.0.tgz",
121
+ "integrity": "sha512-8VOpLHFrOQlAH+qA0ZzuGRlALRA6/LVh8QJldbrC4DY0hXoMP0l4Acq8TzFC018HztWiRqyCEj2aTWY2UvnJUg==",
122
+ "license": "MIT",
123
+ "engines": {
124
+ "node": "^18 || ^20 || >= 21"
125
+ }
126
+ },
127
+ "node_modules/node-gyp-build": {
128
+ "version": "4.8.4",
129
+ "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
130
+ "integrity": "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ==",
131
+ "license": "MIT",
132
+ "bin": {
133
+ "node-gyp-build": "bin.js",
134
+ "node-gyp-build-optional": "optional.js",
135
+ "node-gyp-build-test": "build-test.js"
136
+ }
137
+ },
138
+ "node_modules/shiki": {
139
+ "version": "0.14.7",
140
+ "resolved": "https://registry.npmjs.org/shiki/-/shiki-0.14.7.tgz",
141
+ "integrity": "sha512-dNPAPrxSc87ua2sKJ3H5dQ/6ZaY8RNnaAqK+t0eG7p0Soi2ydiqbGOTaZCqaYvA/uZYfS1LJnemt3Q+mSfcPCg==",
142
+ "license": "MIT",
143
+ "peer": true,
144
+ "dependencies": {
145
+ "ansi-sequence-parser": "^1.1.0",
146
+ "jsonc-parser": "^3.2.0",
147
+ "vscode-oniguruma": "^1.7.0",
148
+ "vscode-textmate": "^8.0.0"
149
+ }
150
+ },
151
+ "node_modules/tree-sitter": {
152
+ "version": "0.21.1",
153
+ "resolved": "https://registry.npmjs.org/tree-sitter/-/tree-sitter-0.21.1.tgz",
154
+ "integrity": "sha512-7dxoA6kYvtgWw80265MyqJlkRl4yawIjO7S5MigytjELkX43fV2WsAXzsNfO7sBpPPCF5Gp0+XzHk0DwLCq3xQ==",
155
+ "hasInstallScript": true,
156
+ "license": "MIT",
157
+ "dependencies": {
158
+ "node-addon-api": "^8.0.0",
159
+ "node-gyp-build": "^4.8.0"
160
+ }
161
+ },
162
+ "node_modules/tree-sitter-javascript": {
163
+ "version": "0.23.1",
164
+ "resolved": "https://registry.npmjs.org/tree-sitter-javascript/-/tree-sitter-javascript-0.23.1.tgz",
165
+ "integrity": "sha512-/bnhbrTD9frUYHQTiYnPcxyHORIw157ERBa6dqzaKxvR/x3PC4Yzd+D1pZIMS6zNg2v3a8BZ0oK7jHqsQo9fWA==",
166
+ "hasInstallScript": true,
167
+ "license": "MIT",
168
+ "dependencies": {
169
+ "node-addon-api": "^8.2.2",
170
+ "node-gyp-build": "^4.8.2"
171
+ },
172
+ "peerDependencies": {
173
+ "tree-sitter": "^0.21.1"
174
+ },
175
+ "peerDependenciesMeta": {
176
+ "tree-sitter": {
177
+ "optional": true
178
+ }
179
+ }
180
+ },
181
+ "node_modules/tree-sitter-typescript": {
182
+ "version": "0.23.2",
183
+ "resolved": "https://registry.npmjs.org/tree-sitter-typescript/-/tree-sitter-typescript-0.23.2.tgz",
184
+ "integrity": "sha512-e04JUUKxTT53/x3Uq1zIL45DoYKVfHH4CZqwgZhPg5qYROl5nQjV+85ruFzFGZxu+QeFVbRTPDRnqL9UbU4VeA==",
185
+ "hasInstallScript": true,
186
+ "license": "MIT",
187
+ "dependencies": {
188
+ "node-addon-api": "^8.2.2",
189
+ "node-gyp-build": "^4.8.2",
190
+ "tree-sitter-javascript": "^0.23.1"
191
+ },
192
+ "peerDependencies": {
193
+ "tree-sitter": "^0.21.0"
194
+ },
195
+ "peerDependenciesMeta": {
196
+ "tree-sitter": {
197
+ "optional": true
198
+ }
199
+ }
200
+ },
201
+ "node_modules/typedoc": {
202
+ "version": "0.25.13",
203
+ "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.25.13.tgz",
204
+ "integrity": "sha512-pQqiwiJ+Z4pigfOnnysObszLiU3mVLWAExSPf+Mu06G/qsc3wzbuM56SZQvONhHLncLUhYzOVkjFFpFfL5AzhQ==",
205
+ "license": "Apache-2.0",
206
+ "peer": true,
207
+ "dependencies": {
208
+ "lunr": "^2.3.9",
209
+ "marked": "^4.3.0",
210
+ "minimatch": "^9.0.3",
211
+ "shiki": "^0.14.7"
212
+ },
213
+ "bin": {
214
+ "typedoc": "bin/typedoc"
215
+ },
216
+ "engines": {
217
+ "node": ">= 16"
218
+ },
219
+ "peerDependencies": {
220
+ "typescript": "4.6.x || 4.7.x || 4.8.x || 4.9.x || 5.0.x || 5.1.x || 5.2.x || 5.3.x || 5.4.x"
221
+ }
222
+ },
223
+ "node_modules/typedoc-plugin-missing-exports": {
224
+ "version": "2.3.0",
225
+ "resolved": "https://registry.npmjs.org/typedoc-plugin-missing-exports/-/typedoc-plugin-missing-exports-2.3.0.tgz",
226
+ "integrity": "sha512-iI9ITNNLlbsLCBBeYDyu0Qqp3GN/9AGyWNKg8bctRXuZEPT7G1L+0+MNWG9MsHcf/BFmNbXL0nQ8mC/tXRicog==",
227
+ "license": "MIT",
228
+ "peerDependencies": {
229
+ "typedoc": "0.24.x || 0.25.x"
230
+ }
231
+ },
232
+ "node_modules/typescript": {
233
+ "version": "5.4.5",
234
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz",
235
+ "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==",
236
+ "license": "Apache-2.0",
237
+ "peer": true,
238
+ "bin": {
239
+ "tsc": "bin/tsc",
240
+ "tsserver": "bin/tsserver"
241
+ },
242
+ "engines": {
243
+ "node": ">=14.17"
244
+ }
245
+ },
246
+ "node_modules/vscode-oniguruma": {
247
+ "version": "1.7.0",
248
+ "resolved": "https://registry.npmjs.org/vscode-oniguruma/-/vscode-oniguruma-1.7.0.tgz",
249
+ "integrity": "sha512-L9WMGRfrjOhgHSdOYgCt/yRMsXzLDJSL7BPrOZt73gU0iWO4mpqzqQzOz5srxqTvMBaR0XZTSrVWo4j55Rc6cA==",
250
+ "license": "MIT",
251
+ "peer": true
252
+ },
253
+ "node_modules/vscode-textmate": {
254
+ "version": "8.0.0",
255
+ "resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-8.0.0.tgz",
256
+ "integrity": "sha512-AFbieoL7a5LMqcnOF04ji+rpXadgOXnZsxQr//r83kLPr7biP7am3g9zbaZIaBGwBRWeSvoMD4mgPdX3e4NWBg==",
257
+ "license": "MIT",
258
+ "peer": true
259
+ },
260
+ "node_modules/ws": {
261
+ "version": "8.18.0",
262
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",
263
+ "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==",
264
+ "license": "MIT",
265
+ "engines": {
266
+ "node": ">=10.0.0"
267
+ },
268
+ "peerDependencies": {
269
+ "bufferutil": "^4.0.1",
270
+ "utf-8-validate": ">=5.0.2"
271
+ },
272
+ "peerDependenciesMeta": {
273
+ "bufferutil": {
274
+ "optional": true
275
+ },
276
+ "utf-8-validate": {
277
+ "optional": true
278
+ }
279
+ }
280
+ }
281
+ }
282
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "dependencies": {
3
+ "js-yaml": "^4.1.0",
4
+ "@codebolt/codeboltjs": "^1.1.89"
5
+ }
6
+ }
@@ -0,0 +1,55 @@
1
+ import yaml from 'js-yaml';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+
5
+ interface PromptData {
6
+ [key: string]: {
7
+ prompt: string;
8
+ };
9
+ }
10
+
11
+ /**
12
+ * SystemPrompt class for loading and managing system prompts from YAML files
13
+ */
14
+ class SystemPrompt {
15
+ private filepath: string;
16
+ private key: string;
17
+
18
+ /**
19
+ * Creates a SystemPrompt instance
20
+ * @param {string} filepath - Path to the YAML file containing prompts
21
+ * @param {string} key - Key identifier for the specific prompt
22
+ */
23
+ constructor(filepath: string = "", key: string = "") {
24
+ this.filepath = filepath;
25
+ this.key = key;
26
+ }
27
+
28
+ /**
29
+ * Loads and returns the prompt text
30
+ * @returns {string} The prompt text
31
+ * @throws {Error} If file cannot be read or parsed
32
+ */
33
+ toPromptText(): string {
34
+ try {
35
+ const absolutePath = path.resolve(this.filepath);
36
+ const fileContents = fs.readFileSync(absolutePath, 'utf8');
37
+ const data = yaml.load(fileContents) as PromptData;
38
+
39
+ if (!data || typeof data !== 'object') {
40
+ throw new Error('Invalid YAML structure');
41
+ }
42
+
43
+ if (!data[this.key]?.prompt) {
44
+ throw new Error(`Prompt not found for key: ${this.key}`);
45
+ }
46
+
47
+ return data[this.key].prompt;
48
+ } catch (error) {
49
+ console.error(`SystemPrompt Error: ${error instanceof Error ? error.message : 'Unknown error'}`);
50
+ throw error; // Re-throw to allow caller handling
51
+ }
52
+ }
53
+ }
54
+
55
+ export { SystemPrompt };
@@ -0,0 +1,66 @@
1
+ import { UserMessage, UserMessageContent } from "./usermessage";
2
+
3
+ /**
4
+ * Encapsulates task instructions and their related metadata.
5
+ * Handles loading and processing of task instructions from YAML files.
6
+ */
7
+ const yaml = require('js-yaml');
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+
11
+ // ... existing imports ...
12
+
13
+ interface Tools {
14
+ [key: string]: {
15
+ description: string;
16
+ usage: string;
17
+ example?: string;
18
+ };
19
+ }
20
+
21
+
22
+
23
+ interface TaskData {
24
+ [key: string]: {
25
+ description: string;
26
+ expected_output: string;
27
+ };
28
+ }
29
+
30
+ interface UserMessages {
31
+ type: string;
32
+ text: string;
33
+ }
34
+
35
+ class TaskInstruction {
36
+ tools: Tools;
37
+ userMessages: UserMessageContent[]=[];
38
+ userMessage: UserMessage
39
+ filepath: string;
40
+ refsection: string;
41
+
42
+ constructor(tools: Tools = {}, userMessage: UserMessage , filepath: string = "", refsection: string = "") {
43
+ this.tools = tools;
44
+ this.userMessage = userMessage;
45
+ this.filepath = filepath;
46
+ this.refsection = refsection;
47
+ }
48
+
49
+ async toPrompt(): Promise<UserMessages[]> {
50
+ try {
51
+ this.userMessages = await this.userMessage.toPrompt();
52
+ const fileContents = fs.readFileSync(path.resolve(this.filepath), 'utf8');
53
+ const data = yaml.load(fileContents) as TaskData;
54
+ const task = data[this.refsection];
55
+ this.userMessages.push({
56
+ type: "text",
57
+ text: `Task Description: ${task.description}\nExpected Output: ${task.expected_output}`
58
+ });
59
+ return this.userMessages;
60
+ } catch (error) {
61
+ console.error(`Error processing task instruction: ${error}`);
62
+ throw error;
63
+ }
64
+ }
65
+ }
66
+ export { TaskInstruction };
@@ -0,0 +1,97 @@
1
+ import cbfs from "./../fs";
2
+ import project from "./../project";
3
+ import mcp from "./../mcp";
4
+ import { escape } from "querystring";
5
+
6
+
7
+ interface Message {
8
+ userMessage: string;
9
+ mentionedFiles?: string[];
10
+ mentionedMCPs: string[];
11
+ }
12
+
13
+ export interface UserMessageContent {
14
+ type: string;
15
+ text: string;
16
+ }
17
+
18
+ interface FileListResult {
19
+ success: boolean;
20
+ result: string;
21
+ }
22
+
23
+ class UserMessage {
24
+ message: Message;
25
+ promptOverride: boolean;
26
+ userMessages: UserMessageContent[];
27
+ mentaionedMCPS: string[];
28
+
29
+ constructor(message: Message, promptOverride: boolean = false) {
30
+ this.message = message;
31
+ this.promptOverride = promptOverride;
32
+ this.userMessages = [];
33
+ this.mentaionedMCPS = message.mentionedMCPs;
34
+ }
35
+
36
+ getFiles(): void {
37
+ // Implementation to be added
38
+ }
39
+
40
+ async toPrompt(
41
+ bAttachFiles: boolean = true,
42
+ bAttachImages: boolean = true,
43
+ bAttachEnvironment: boolean = true
44
+ ): Promise<UserMessageContent[]> {
45
+ if (bAttachFiles) {
46
+ if (this.promptOverride) {
47
+ // Use a rendering engine
48
+ } else {
49
+ let finalPrompt = `
50
+ The user has sent the following query:
51
+ ${this.message.userMessage}.
52
+ `;
53
+ if (this.message.mentionedFiles?.length) {
54
+ finalPrompt += `The Attached files are:`;
55
+ for (const file of this.message.mentionedFiles) {
56
+ let filedata = await cbfs.readFile(file);
57
+ finalPrompt += `File Name: ${file}, File Path: ${file}, Filedata: ${filedata}`;
58
+ }
59
+ }
60
+ this.userMessages.push({ type: "text", text: finalPrompt });
61
+ }
62
+ }
63
+
64
+ if (bAttachEnvironment) {
65
+ let { projectPath } = await project.getProjectPath();
66
+ const environmentDetail = await this.getEnvironmentDetail(projectPath);
67
+ this.userMessages.push({ type: "text", text: environmentDetail });
68
+ }
69
+
70
+ return this.userMessages;
71
+ }
72
+
73
+ getMentionedMcps(): string[] {
74
+ return this.message.mentionedMCPs || [];
75
+ }
76
+ async getMentionedMcpsTools() {
77
+ if (this.mentaionedMCPS.length > 0) {
78
+ let tools = await mcp.getMcpTools(this.mentaionedMCPS)
79
+ return tools
80
+ }
81
+ else {
82
+ return []
83
+ }
84
+ }
85
+
86
+ private getEnvironmentDetail = async (cwd: string): Promise<string> => {
87
+ let details = "";
88
+ const { success, result }: FileListResult = await cbfs.listFile(cwd, true);
89
+ details += `\n\n# Current Working Directory (${cwd}) Files\n${result}
90
+ ? "\n(Note: Only top-level contents shown for Desktop by default. Use list_files to explore further if necessary.)"
91
+ : ""
92
+ }`;
93
+ return `<environment_details>\n${details.trim()}\n</environment_details>`;
94
+ }
95
+ }
96
+
97
+ export { UserMessage };
@@ -3,6 +3,7 @@ import cbws from './websocket';
3
3
  import { EventEmitter } from 'events';
4
4
  import { ChatMessage, UserMessage } from '@codebolt/types'
5
5
 
6
+ type RequestHandler = (request: any, response: (data: any) => void) => Promise<void> | void;
6
7
 
7
8
 
8
9
  /**
@@ -31,24 +32,64 @@ const cbchat = {
31
32
  })
32
33
  })
33
34
  },
35
+ /**
36
+ * Sets a global request handler for all incoming messages
37
+ * @param handler The async handler function
38
+ */
39
+ setRequestHandler: (handler: RequestHandler) => {
40
+ const waitForConnection = () => {
41
+ const setupHandler = () => {
42
+ if (cbws.getWebsocket) {
43
+ cbws.getWebsocket.on('message', async (data: string) => {
44
+ try {
45
+ const request = JSON.parse(data);
46
+ await handler(request, (responseData: any) => {
47
+ cbws.getWebsocket.send(JSON.stringify({
48
+ type: `processStoped`,
49
+ ...responseData
50
+ }));
51
+ });
52
+ } catch (error) {
53
+ console.error('Error handling request:', error);
54
+ }
55
+ });
56
+ } else {
57
+ setTimeout(setupHandler, 100);
58
+ }
59
+ };
60
+
61
+ setupHandler();
62
+ }
63
+ waitForConnection();
64
+ },
34
65
  /**
35
66
  * Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
36
67
  * @returns {EventEmitter} The event emitter used for emitting custom events.
37
68
  */
69
+ /**
70
+ * Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
71
+ * @returns {EventEmitter} The event emitter used for emitting custom events.
72
+ */
38
73
  onActionMessage: () => {
39
- if (!cbws.getWebsocket) return;
40
- cbws.getWebsocket.on('message', (data: string) => {
41
- const response = JSON.parse(data);
42
- if (response.type === "messageResponse") {
43
- // Pass a callback function as an argument to the emit method
44
- eventEmitter.emit("userMessage", response, (message: string) => {
45
- console.log("Callback function invoked with message:", message);
46
- cbws.getWebsocket.send(JSON.stringify({
47
- "type": "processStoped"
48
- }));
74
+ const waitForConnection = () => {
75
+ if (cbws.getWebsocket) {
76
+ cbws.getWebsocket.on('message', (data: string) => {
77
+ const response = JSON.parse(data);
78
+ if (response.type === "messageResponse") {
79
+ eventEmitter.emit("userMessage", response, (message: string) => {
80
+ console.log("Callback function invoked with message:", message);
81
+ cbws.getWebsocket.send(JSON.stringify({
82
+ "type": "processStoped"
83
+ }));
84
+ });
85
+ }
49
86
  });
87
+ } else {
88
+ setTimeout(waitForConnection, 100);
50
89
  }
51
- });
90
+ };
91
+
92
+ waitForConnection();
52
93
  return eventEmitter;
53
94
  },
54
95
  /**
@@ -142,7 +183,7 @@ const cbchat = {
142
183
  * Sends a confirmation request to the server with two options: Yes or No.
143
184
  * @returns {Promise<string>} A promise that resolves with the server's response.
144
185
  */
145
- sendConfirmationRequest: (confirmationMessage: string, buttons: string[] = [],withFeedback:boolean=false): Promise<string> => {
186
+ sendConfirmationRequest: (confirmationMessage: string, buttons: string[] = [], withFeedback: boolean = false): Promise<string> => {
146
187
  return new Promise((resolve, reject) => {
147
188
  cbws.getWebsocket.send(JSON.stringify({
148
189
  "type": "confirmationRequest",
@@ -153,13 +194,13 @@ const cbchat = {
153
194
  }));
154
195
  cbws.getWebsocket.on('message', (data: string) => {
155
196
  const response = JSON.parse(data);
156
- if (response.type === "confirmationResponse" || response.type === "feedbackResponse" ) {
197
+ if (response.type === "confirmationResponse" || response.type === "feedbackResponse") {
157
198
  resolve(response); // Resolve the Promise with the server's response
158
199
  }
159
200
  });
160
201
  });
161
202
  },
162
- askQuestion: (question: string, buttons: string[] = [],withFeedback:boolean=false): Promise<string> => {
203
+ askQuestion: (question: string, buttons: string[] = [], withFeedback: boolean = false): Promise<string> => {
163
204
  return new Promise((resolve, reject) => {
164
205
  cbws.getWebsocket.send(JSON.stringify({
165
206
  "type": "confirmationRequest",
@@ -170,7 +211,7 @@ const cbchat = {
170
211
  }));
171
212
  cbws.getWebsocket.on('message', (data: string) => {
172
213
  const response = JSON.parse(data);
173
- if (response.type === "confirmationResponse" || response.type === "feedbackResponse" ) {
214
+ if (response.type === "confirmationResponse" || response.type === "feedbackResponse") {
174
215
  resolve(response); // Resolve the Promise with the server's response
175
216
  }
176
217
  });
package/src/modules/fs.ts CHANGED
@@ -163,7 +163,7 @@ const cbfs = {
163
163
  * @description Lists all files.
164
164
  * @returns {Promise<FileListResponse>} A promise that resolves with the list of files.
165
165
  */
166
- listFile: (folderPath:string,isRecursive=false) => {
166
+ listFile: (folderPath: string, isRecursive: boolean = false): Promise<any> => {
167
167
  return new Promise((resolve, reject) => {
168
168
  cbws.getWebsocket.send(JSON.stringify({
169
169
  "type": "fsEvent",