@codebolt/codeboltjs 2.0.4 → 2.0.6
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.
- package/dist/core/messageManager.d.ts +47 -0
- package/dist/core/messageManager.js +128 -0
- package/dist/{modules → core}/websocket.d.ts +5 -0
- package/dist/{modules → core}/websocket.js +15 -10
- package/dist/index.d.ts +98 -68
- package/dist/index.js +51 -4
- package/dist/modules/agent.js +26 -58
- package/dist/modules/browser.d.ts +7 -7
- package/dist/modules/browser.js +75 -195
- package/dist/modules/chat.d.ts +8 -20
- package/dist/modules/chat.js +60 -123
- package/dist/modules/codeparsers.d.ts +32 -3
- package/dist/modules/codeparsers.js +295 -3
- package/dist/modules/codeutils.d.ts +24 -7
- package/dist/modules/codeutils.js +113 -126
- package/dist/modules/crawler.d.ts +1 -16
- package/dist/modules/crawler.js +13 -72
- package/dist/modules/dbmemory.js +12 -28
- package/dist/modules/debug.js +17 -33
- package/dist/modules/docutils.d.ts +1 -4
- package/dist/modules/docutils.js +52 -2
- package/dist/modules/fs.d.ts +47 -1
- package/dist/modules/fs.js +151 -162
- package/dist/modules/git.d.ts +7 -14
- package/dist/modules/git.js +54 -163
- package/dist/modules/history.js +11 -27
- package/dist/modules/llm.js +8 -16
- package/dist/modules/outputparsers.d.ts +36 -4
- package/dist/modules/outputparsers.js +56 -5
- package/dist/modules/project.d.ts +4 -5
- package/dist/modules/project.js +23 -45
- package/dist/modules/state.js +29 -69
- package/dist/modules/task.js +19 -43
- package/dist/modules/terminal.d.ts +3 -2
- package/dist/modules/terminal.js +36 -47
- package/dist/modules/tokenizer.js +15 -31
- package/dist/modules/tools.d.ts +0 -6
- package/dist/modules/tools.js +41 -179
- package/dist/modules/utils.js +22 -0
- package/dist/modules/vectordb.js +30 -62
- package/dist/utils/parse-source-code/index.d.ts +9 -0
- package/dist/utils/parse-source-code/index.js +233 -0
- package/dist/utils/parse-source-code/languageParser.d.ts +8 -0
- package/dist/utils/parse-source-code/languageParser.js +137 -0
- package/dist/utils/parse-source-code/queries/c-sharp.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/c-sharp.js +25 -0
- package/dist/utils/parse-source-code/queries/c.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/c.js +17 -0
- package/dist/utils/parse-source-code/queries/cpp.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/cpp.js +25 -0
- package/dist/utils/parse-source-code/queries/go.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/go.js +29 -0
- package/dist/utils/parse-source-code/queries/index.d.ts +12 -0
- package/dist/utils/parse-source-code/queries/index.js +30 -0
- package/dist/utils/parse-source-code/queries/java.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/java.js +17 -0
- package/dist/utils/parse-source-code/queries/javascript.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/javascript.js +67 -0
- package/dist/utils/parse-source-code/queries/php.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/php.js +17 -0
- package/dist/utils/parse-source-code/queries/python.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/python.js +13 -0
- package/dist/utils/parse-source-code/queries/ruby.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/ruby.js +54 -0
- package/dist/utils/parse-source-code/queries/rust.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/rust.js +18 -0
- package/dist/utils/parse-source-code/queries/swift.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/swift.js +47 -0
- package/dist/utils/parse-source-code/queries/typescript.d.ts +2 -0
- package/dist/utils/parse-source-code/queries/typescript.js +34 -0
- package/dist/utils/parse-source-code/tree-sitter-c.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-c_sharp.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-cpp.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-go.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-java.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-javascript.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-php.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-python.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-ruby.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-rust.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-swift.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-tsx.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter-typescript.wasm +0 -0
- package/dist/utils/parse-source-code/tree-sitter.wasm +0 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +6 -2
- package/dist/utils/editFile.js +0 -30
- /package/dist/{utils/editFile.d.ts → modules/utils.d.ts} +0 -0
- /package/dist/{modules → utils}/toolBox.d.ts +0 -0
- /package/dist/{modules → utils}/toolBox.js +0 -0
package/dist/modules/chat.js
CHANGED
|
@@ -4,14 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
// chat.ts
|
|
7
|
-
const websocket_1 = __importDefault(require("
|
|
8
|
-
const events_1 = require("events");
|
|
9
|
-
/**
|
|
10
|
-
* CustomEventEmitter class that extends the Node.js EventEmitter class.
|
|
11
|
-
*/
|
|
12
|
-
class CustomEventEmitter extends events_1.EventEmitter {
|
|
13
|
-
}
|
|
14
|
-
let eventEmitter = new CustomEventEmitter();
|
|
7
|
+
const websocket_1 = __importDefault(require("../core/websocket"));
|
|
15
8
|
/**
|
|
16
9
|
* Chat module to interact with the WebSocket server.
|
|
17
10
|
*/
|
|
@@ -21,17 +14,9 @@ const cbchat = {
|
|
|
21
14
|
* @returns {Promise<ChatMessage[]>} A promise that resolves with an array of ChatMessage objects representing the chat history.
|
|
22
15
|
*/
|
|
23
16
|
getChatHistory: () => {
|
|
24
|
-
return
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}));
|
|
28
|
-
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
29
|
-
const response = JSON.parse(data);
|
|
30
|
-
if (response.type === "getChatHistoryResponse") {
|
|
31
|
-
resolve(response); // Resolve the Promise with the response data
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
});
|
|
17
|
+
return websocket_1.default.messageManager.sendAndWaitForResponse({
|
|
18
|
+
"type": "getChatHistory"
|
|
19
|
+
}, "getChatHistoryResponse");
|
|
35
20
|
},
|
|
36
21
|
/**
|
|
37
22
|
* Sets a global request handler for all incoming messages
|
|
@@ -40,15 +25,14 @@ const cbchat = {
|
|
|
40
25
|
setRequestHandler: (handler) => {
|
|
41
26
|
const waitForConnection = () => {
|
|
42
27
|
const setupHandler = () => {
|
|
43
|
-
if (websocket_1.default.
|
|
44
|
-
websocket_1.default.
|
|
28
|
+
if (websocket_1.default.messageManager) {
|
|
29
|
+
websocket_1.default.messageManager.on('message', async (request) => {
|
|
45
30
|
try {
|
|
46
|
-
const request = JSON.parse(data);
|
|
47
31
|
await handler(request, (responseData) => {
|
|
48
|
-
websocket_1.default.
|
|
32
|
+
websocket_1.default.messageManager.send({
|
|
49
33
|
type: `processStoped`,
|
|
50
34
|
...responseData
|
|
51
|
-
})
|
|
35
|
+
});
|
|
52
36
|
});
|
|
53
37
|
}
|
|
54
38
|
catch (error) {
|
|
@@ -64,46 +48,16 @@ const cbchat = {
|
|
|
64
48
|
};
|
|
65
49
|
waitForConnection();
|
|
66
50
|
},
|
|
67
|
-
/**
|
|
68
|
-
* Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
|
|
69
|
-
* @returns {EventEmitter} The event emitter used for emitting custom events.
|
|
70
|
-
*/
|
|
71
|
-
/**
|
|
72
|
-
* Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
|
|
73
|
-
* @returns {EventEmitter} The event emitter used for emitting custom events.
|
|
74
|
-
*/
|
|
75
|
-
onActionMessage: () => {
|
|
76
|
-
const waitForConnection = () => {
|
|
77
|
-
if (websocket_1.default.getWebsocket) {
|
|
78
|
-
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
79
|
-
const response = JSON.parse(data);
|
|
80
|
-
if (response.type === "messageResponse") {
|
|
81
|
-
eventEmitter.emit("userMessage", response, (message) => {
|
|
82
|
-
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
83
|
-
"type": "processStoped",
|
|
84
|
-
"message": message
|
|
85
|
-
}));
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
setTimeout(waitForConnection, 100);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
waitForConnection();
|
|
95
|
-
return eventEmitter;
|
|
96
|
-
},
|
|
97
51
|
/**
|
|
98
52
|
* Sends a message through the WebSocket connection.
|
|
99
53
|
* @param {string} message - The message to be sent.
|
|
100
54
|
*/
|
|
101
55
|
sendMessage: (message, payload) => {
|
|
102
|
-
websocket_1.default.
|
|
56
|
+
websocket_1.default.messageManager.send({
|
|
103
57
|
"type": "sendMessage",
|
|
104
58
|
"message": message,
|
|
105
59
|
payload
|
|
106
|
-
})
|
|
60
|
+
});
|
|
107
61
|
},
|
|
108
62
|
/**
|
|
109
63
|
* Waits for a reply to a sent message.
|
|
@@ -111,44 +65,47 @@ const cbchat = {
|
|
|
111
65
|
* @returns {Promise<UserMessage>} A promise that resolves with the reply.
|
|
112
66
|
*/
|
|
113
67
|
waitforReply: (message) => {
|
|
114
|
-
return
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}));
|
|
119
|
-
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
120
|
-
const response = JSON.parse(data);
|
|
121
|
-
if (response.type === "waitFormessageResponse") {
|
|
122
|
-
resolve(response); // Resolve the Promise with the response data
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
});
|
|
68
|
+
return websocket_1.default.messageManager.sendAndWaitForResponse({
|
|
69
|
+
"type": "waitforReply",
|
|
70
|
+
"message": message
|
|
71
|
+
}, "waitFormessageResponse");
|
|
126
72
|
},
|
|
127
73
|
/**
|
|
128
|
-
* Notifies the server that a process has started and sets up
|
|
129
|
-
* @
|
|
74
|
+
* Notifies the server that a process has started and sets up a listener for stopProcessClicked events.
|
|
75
|
+
* @param {Function} onStopClicked - Callback function to handle stop process events.
|
|
76
|
+
* @returns An object containing a stopProcess method.
|
|
130
77
|
*/
|
|
131
|
-
processStarted: () => {
|
|
78
|
+
processStarted: (onStopClicked) => {
|
|
132
79
|
// Send the process started message
|
|
133
|
-
websocket_1.default.
|
|
80
|
+
websocket_1.default.messageManager.send({
|
|
134
81
|
"type": "processStarted"
|
|
135
|
-
}));
|
|
136
|
-
// Register event listener for WebSocket messages
|
|
137
|
-
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
138
|
-
const message = JSON.parse(data);
|
|
139
|
-
if (message.type === 'stopProcessClicked')
|
|
140
|
-
// Emit a custom event based on the message type
|
|
141
|
-
eventEmitter.emit("stopProcessClicked", message);
|
|
142
82
|
});
|
|
143
|
-
//
|
|
83
|
+
// Register event listener for WebSocket messages if callback provided
|
|
84
|
+
if (onStopClicked) {
|
|
85
|
+
const handleStopMessage = (message) => {
|
|
86
|
+
if (message.type === 'stopProcessClicked') {
|
|
87
|
+
onStopClicked(message);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
websocket_1.default.messageManager.on('message', handleStopMessage);
|
|
91
|
+
// Return an object that includes the stopProcess method and cleanup
|
|
92
|
+
return {
|
|
93
|
+
stopProcess: () => {
|
|
94
|
+
websocket_1.default.messageManager.send({
|
|
95
|
+
"type": "processStoped"
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
cleanup: () => {
|
|
99
|
+
websocket_1.default.messageManager.removeListener('message', handleStopMessage);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// Return an object that includes the stopProcess method
|
|
144
104
|
return {
|
|
145
|
-
event: eventEmitter,
|
|
146
105
|
stopProcess: () => {
|
|
147
|
-
|
|
148
|
-
// For example, you might want to send a specific message to the server to stop the process
|
|
149
|
-
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
106
|
+
websocket_1.default.messageManager.send({
|
|
150
107
|
"type": "processStoped"
|
|
151
|
-
})
|
|
108
|
+
});
|
|
152
109
|
}
|
|
153
110
|
};
|
|
154
111
|
},
|
|
@@ -157,69 +114,49 @@ const cbchat = {
|
|
|
157
114
|
* Sends a specific message to the server to stop the process.
|
|
158
115
|
*/
|
|
159
116
|
stopProcess: () => {
|
|
160
|
-
|
|
161
|
-
// For example, you might want to send a specific message to the server to stop the process
|
|
162
|
-
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
117
|
+
websocket_1.default.messageManager.send({
|
|
163
118
|
"type": "processStoped"
|
|
164
|
-
})
|
|
119
|
+
});
|
|
165
120
|
},
|
|
166
121
|
/**
|
|
167
122
|
* Stops the ongoing process.
|
|
168
123
|
* Sends a specific message to the server to stop the process.
|
|
169
124
|
*/
|
|
170
125
|
processFinished: () => {
|
|
171
|
-
|
|
172
|
-
// For example, you might want to send a specific message to the server to stop the process
|
|
173
|
-
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
126
|
+
websocket_1.default.messageManager.send({
|
|
174
127
|
"type": "processFinished"
|
|
175
|
-
})
|
|
128
|
+
});
|
|
176
129
|
},
|
|
177
130
|
/**
|
|
178
131
|
* Sends a confirmation request to the server with two options: Yes or No.
|
|
179
132
|
* @returns {Promise<string>} A promise that resolves with the server's response.
|
|
180
133
|
*/
|
|
181
134
|
sendConfirmationRequest: (confirmationMessage, buttons = [], withFeedback = false) => {
|
|
182
|
-
return
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}));
|
|
189
|
-
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
190
|
-
const response = JSON.parse(data);
|
|
191
|
-
if (response.type === "confirmationResponse" || response.type === "feedbackResponse") {
|
|
192
|
-
resolve(response); // Resolve the Promise with the server's response
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
});
|
|
135
|
+
return websocket_1.default.messageManager.sendAndWaitForResponse({
|
|
136
|
+
"type": "confirmationRequest",
|
|
137
|
+
"message": confirmationMessage,
|
|
138
|
+
buttons: buttons,
|
|
139
|
+
withFeedback
|
|
140
|
+
}, "confirmationResponse|feedbackResponse");
|
|
196
141
|
},
|
|
197
142
|
askQuestion: (question, buttons = [], withFeedback = false) => {
|
|
198
|
-
return
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}));
|
|
205
|
-
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
206
|
-
const response = JSON.parse(data);
|
|
207
|
-
if (response.type === "confirmationResponse" || response.type === "feedbackResponse") {
|
|
208
|
-
resolve(response); // Resolve the Promise with the server's response
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
});
|
|
143
|
+
return websocket_1.default.messageManager.sendAndWaitForResponse({
|
|
144
|
+
"type": "confirmationRequest",
|
|
145
|
+
"message": question,
|
|
146
|
+
buttons: buttons,
|
|
147
|
+
withFeedback
|
|
148
|
+
}, "confirmationResponse|feedbackResponse");
|
|
212
149
|
},
|
|
213
150
|
/**
|
|
214
151
|
* Sends a notification event to the server.
|
|
215
152
|
* @param {string} notificationMessage - The message to be sent in the notification.
|
|
216
153
|
*/
|
|
217
154
|
sendNotificationEvent: (notificationMessage, type) => {
|
|
218
|
-
websocket_1.default.
|
|
155
|
+
websocket_1.default.messageManager.send({
|
|
219
156
|
"type": "notificationEvent",
|
|
220
157
|
"message": notificationMessage,
|
|
221
158
|
"eventType": type
|
|
222
|
-
})
|
|
159
|
+
});
|
|
223
160
|
},
|
|
224
161
|
};
|
|
225
162
|
exports.default = cbchat;
|
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definition for an AST node.
|
|
3
|
+
*/
|
|
4
|
+
export interface ASTNode {
|
|
5
|
+
type: string;
|
|
6
|
+
text: string;
|
|
7
|
+
startPosition: {
|
|
8
|
+
row: number;
|
|
9
|
+
column: number;
|
|
10
|
+
};
|
|
11
|
+
endPosition: {
|
|
12
|
+
row: number;
|
|
13
|
+
column: number;
|
|
14
|
+
};
|
|
15
|
+
children: ASTNode[];
|
|
16
|
+
}
|
|
1
17
|
/**
|
|
2
18
|
* A collection of code parser functions.
|
|
3
19
|
*/
|
|
@@ -6,18 +22,31 @@ declare const cbcodeparsers: {
|
|
|
6
22
|
* Retrieves the classes in a given file.
|
|
7
23
|
* @param file The file to parse for classes.
|
|
8
24
|
*/
|
|
9
|
-
getClassesInFile: (file:
|
|
25
|
+
getClassesInFile: (file: string) => Promise<{
|
|
26
|
+
error: string;
|
|
27
|
+
} | {
|
|
28
|
+
name: any;
|
|
29
|
+
location: string;
|
|
30
|
+
}[]>;
|
|
10
31
|
/**
|
|
11
32
|
* Retrieves the functions in a given class within a file.
|
|
12
33
|
* @param file The file containing the class.
|
|
13
34
|
* @param className The name of the class to parse for functions.
|
|
14
35
|
*/
|
|
15
|
-
getFunctionsinClass: (file:
|
|
36
|
+
getFunctionsinClass: (file: string, className: string) => Promise<{
|
|
37
|
+
error: string;
|
|
38
|
+
} | {
|
|
39
|
+
name: string;
|
|
40
|
+
class: string;
|
|
41
|
+
location: string;
|
|
42
|
+
}[]>;
|
|
16
43
|
/**
|
|
17
44
|
* Generates an Abstract Syntax Tree (AST) for a given file.
|
|
18
45
|
* @param file The file to generate an AST for.
|
|
19
46
|
* @param className The name of the class to focus the AST generation on.
|
|
20
47
|
*/
|
|
21
|
-
getAstTreeInFile: (file:
|
|
48
|
+
getAstTreeInFile: (file: string, className?: string) => Promise<ASTNode | {
|
|
49
|
+
error: string;
|
|
50
|
+
}>;
|
|
22
51
|
};
|
|
23
52
|
export default cbcodeparsers;
|
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const parse_source_code_1 = require("../utils/parse-source-code");
|
|
27
|
+
const languageParser_1 = require("../utils/parse-source-code/languageParser");
|
|
28
|
+
const fs = __importStar(require("fs/promises"));
|
|
29
|
+
const path = __importStar(require("path"));
|
|
3
30
|
/**
|
|
4
31
|
* A collection of code parser functions.
|
|
5
32
|
*/
|
|
@@ -8,21 +35,286 @@ const cbcodeparsers = {
|
|
|
8
35
|
* Retrieves the classes in a given file.
|
|
9
36
|
* @param file The file to parse for classes.
|
|
10
37
|
*/
|
|
11
|
-
getClassesInFile: (file) => {
|
|
38
|
+
getClassesInFile: async (file) => {
|
|
39
|
+
// Check if file exists
|
|
40
|
+
try {
|
|
41
|
+
await fs.access(file);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return { error: `File does not exist or is not accessible: ${file}` };
|
|
45
|
+
}
|
|
46
|
+
// Get file extension and determine if we support it
|
|
47
|
+
const ext = path.extname(file).toLowerCase().slice(1);
|
|
48
|
+
const supportedExtensions = ['js', 'jsx', 'ts', 'tsx', 'py', 'rs', 'go', 'c', 'h', 'cpp', 'hpp', 'cs', 'rb', 'java', 'php', 'swift'];
|
|
49
|
+
if (!supportedExtensions.includes(ext)) {
|
|
50
|
+
return { error: `Unsupported file type: ${ext}` };
|
|
51
|
+
}
|
|
52
|
+
// Load language parser for this file
|
|
53
|
+
const languageParsers = await (0, languageParser_1.loadRequiredLanguageParsers)([file]);
|
|
54
|
+
// Parse the file
|
|
55
|
+
const result = await (0, parse_source_code_1.parseFile)(file, languageParsers);
|
|
56
|
+
// Filter for class definitions only
|
|
57
|
+
const lines = (result === null || result === void 0 ? void 0 : result.split('\n')) || [];
|
|
58
|
+
const classes = [];
|
|
59
|
+
let currentClass = null;
|
|
60
|
+
for (const line of lines) {
|
|
61
|
+
// Match line patterns from the parseFile result format
|
|
62
|
+
if (line.includes('@definition.class')) {
|
|
63
|
+
// Extract the class name from the line
|
|
64
|
+
const className = line.split('@')[0].trim();
|
|
65
|
+
currentClass = { name: className, location: file };
|
|
66
|
+
classes.push(currentClass);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// If no classes found, try to use AST approach
|
|
70
|
+
if (classes.length === 0) {
|
|
71
|
+
try {
|
|
72
|
+
// Get the file extension
|
|
73
|
+
const ext = path.extname(file).toLowerCase().slice(1);
|
|
74
|
+
// Load language parser
|
|
75
|
+
const languageParsers = await (0, languageParser_1.loadRequiredLanguageParsers)([file]);
|
|
76
|
+
// Get the parser for this file type
|
|
77
|
+
const { parser } = languageParsers[ext] || {};
|
|
78
|
+
if (parser) {
|
|
79
|
+
// Read the file content
|
|
80
|
+
const fileContent = await fs.readFile(file, 'utf8');
|
|
81
|
+
// Parse the file to get AST
|
|
82
|
+
const tree = parser.parse(fileContent);
|
|
83
|
+
// Function to find class nodes in the AST
|
|
84
|
+
const findClassNodes = (node) => {
|
|
85
|
+
const classNodes = [];
|
|
86
|
+
if (node.type === 'class_declaration' ||
|
|
87
|
+
node.type === 'class' ||
|
|
88
|
+
node.type === 'class_definition') {
|
|
89
|
+
classNodes.push(node);
|
|
90
|
+
}
|
|
91
|
+
// Recursively search children
|
|
92
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
93
|
+
const childResults = findClassNodes(node.child(i));
|
|
94
|
+
classNodes.push(...childResults);
|
|
95
|
+
}
|
|
96
|
+
return classNodes;
|
|
97
|
+
};
|
|
98
|
+
const classNodes = findClassNodes(tree.rootNode);
|
|
99
|
+
// Extract class names from the nodes
|
|
100
|
+
for (const node of classNodes) {
|
|
101
|
+
// For most languages, the class name is in a child node
|
|
102
|
+
// Let's attempt to extract it from the text
|
|
103
|
+
const text = node.text;
|
|
104
|
+
const classMatch = text.match(/class\s+([a-zA-Z_$][0-9a-zA-Z_$]*)/);
|
|
105
|
+
if (classMatch && classMatch[1]) {
|
|
106
|
+
classes.push({
|
|
107
|
+
name: classMatch[1],
|
|
108
|
+
location: file
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error('Error trying AST approach:', error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return classes;
|
|
12
119
|
},
|
|
13
120
|
/**
|
|
14
121
|
* Retrieves the functions in a given class within a file.
|
|
15
122
|
* @param file The file containing the class.
|
|
16
123
|
* @param className The name of the class to parse for functions.
|
|
17
124
|
*/
|
|
18
|
-
getFunctionsinClass: (file, className) => {
|
|
125
|
+
getFunctionsinClass: async (file, className) => {
|
|
126
|
+
// Check if file exists
|
|
127
|
+
try {
|
|
128
|
+
await fs.access(file);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
return { error: `File does not exist or is not accessible: ${file}` };
|
|
132
|
+
}
|
|
133
|
+
// Load language parser for this file
|
|
134
|
+
const languageParsers = await (0, languageParser_1.loadRequiredLanguageParsers)([file]);
|
|
135
|
+
// Parse the file
|
|
136
|
+
const result = await (0, parse_source_code_1.parseFile)(file, languageParsers);
|
|
137
|
+
// Extract content
|
|
138
|
+
const fileContent = await fs.readFile(file, 'utf8');
|
|
139
|
+
// Parse the file content to find the class and its methods
|
|
140
|
+
const lines = (result === null || result === void 0 ? void 0 : result.split('\n')) || [];
|
|
141
|
+
const functions = [];
|
|
142
|
+
// Use AST approach to find methods within the class
|
|
143
|
+
try {
|
|
144
|
+
// Get the file extension
|
|
145
|
+
const ext = path.extname(file).toLowerCase().slice(1);
|
|
146
|
+
// Get the parser for this file type
|
|
147
|
+
const { parser } = languageParsers[ext] || {};
|
|
148
|
+
if (parser) {
|
|
149
|
+
// Parse the file to get AST
|
|
150
|
+
const tree = parser.parse(fileContent);
|
|
151
|
+
// Find the class node
|
|
152
|
+
const findClassNode = (node) => {
|
|
153
|
+
if ((node.type === 'class_declaration' ||
|
|
154
|
+
node.type === 'class' ||
|
|
155
|
+
node.type === 'class_definition') &&
|
|
156
|
+
node.text.includes(className)) {
|
|
157
|
+
return node;
|
|
158
|
+
}
|
|
159
|
+
// Recursively search children
|
|
160
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
161
|
+
const child = node.child(i);
|
|
162
|
+
const result = findClassNode(child);
|
|
163
|
+
if (result)
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
};
|
|
168
|
+
const classNode = findClassNode(tree.rootNode);
|
|
169
|
+
if (classNode) {
|
|
170
|
+
// Function to find method nodes within the class
|
|
171
|
+
const findMethodNodes = (node) => {
|
|
172
|
+
const methodNodes = [];
|
|
173
|
+
// Different languages use different node types for methods
|
|
174
|
+
if (node.type === 'method_definition' ||
|
|
175
|
+
node.type === 'method' ||
|
|
176
|
+
node.type === 'method_declaration' ||
|
|
177
|
+
node.type === 'function' ||
|
|
178
|
+
node.type === 'function_definition') {
|
|
179
|
+
methodNodes.push(node);
|
|
180
|
+
}
|
|
181
|
+
// Recursively search children
|
|
182
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
183
|
+
const childResults = findMethodNodes(node.child(i));
|
|
184
|
+
methodNodes.push(...childResults);
|
|
185
|
+
}
|
|
186
|
+
return methodNodes;
|
|
187
|
+
};
|
|
188
|
+
const methodNodes = findMethodNodes(classNode);
|
|
189
|
+
// Extract method names from the nodes
|
|
190
|
+
for (const node of methodNodes) {
|
|
191
|
+
// Try to find method name from node text
|
|
192
|
+
let methodName = '';
|
|
193
|
+
// Different pattern matching based on language type
|
|
194
|
+
if (ext === 'py') {
|
|
195
|
+
// Python methods: def method_name(
|
|
196
|
+
const methodMatch = node.text.match(/def\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/);
|
|
197
|
+
if (methodMatch && methodMatch[1]) {
|
|
198
|
+
methodName = methodMatch[1];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else if (['js', 'jsx', 'ts', 'tsx'].includes(ext)) {
|
|
202
|
+
// JavaScript/TypeScript methods: methodName( or methodName =
|
|
203
|
+
const methodMatch = node.text.match(/([a-zA-Z_$][a-zA-Z0-9_$]*)\s*[\(=]/);
|
|
204
|
+
if (methodMatch && methodMatch[1]) {
|
|
205
|
+
methodName = methodMatch[1];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
// Generic approach for other languages
|
|
210
|
+
// Look for identifiers followed by parentheses
|
|
211
|
+
const methodMatch = node.text.match(/([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/);
|
|
212
|
+
if (methodMatch && methodMatch[1]) {
|
|
213
|
+
methodName = methodMatch[1];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (methodName && !['constructor', '__init__'].includes(methodName)) {
|
|
217
|
+
functions.push({
|
|
218
|
+
name: methodName,
|
|
219
|
+
class: className,
|
|
220
|
+
location: file
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
console.error('Error parsing methods:', error);
|
|
229
|
+
}
|
|
230
|
+
return functions;
|
|
19
231
|
},
|
|
20
232
|
/**
|
|
21
233
|
* Generates an Abstract Syntax Tree (AST) for a given file.
|
|
22
234
|
* @param file The file to generate an AST for.
|
|
23
235
|
* @param className The name of the class to focus the AST generation on.
|
|
24
236
|
*/
|
|
25
|
-
getAstTreeInFile: (file, className) => {
|
|
237
|
+
getAstTreeInFile: async (file, className) => {
|
|
238
|
+
try {
|
|
239
|
+
await fs.access(file);
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
return { error: `File does not exist or is not accessible: ${file}` };
|
|
243
|
+
}
|
|
244
|
+
// Get file extension and determine if we support it
|
|
245
|
+
const ext = path.extname(file).toLowerCase().slice(1);
|
|
246
|
+
const supportedExtensions = ['js', 'jsx', 'ts', 'tsx', 'py', 'rs', 'go', 'c', 'h', 'cpp', 'hpp', 'cs', 'rb', 'java', 'php', 'swift'];
|
|
247
|
+
if (!supportedExtensions.includes(ext)) {
|
|
248
|
+
return { error: `Unsupported file type: ${ext}` };
|
|
249
|
+
}
|
|
250
|
+
// Load language parser for this file
|
|
251
|
+
const languageParsers = await (0, languageParser_1.loadRequiredLanguageParsers)([file]);
|
|
252
|
+
// Get the parser for this file type
|
|
253
|
+
const { parser } = languageParsers[ext] || {};
|
|
254
|
+
if (!parser) {
|
|
255
|
+
return { error: `No parser available for file type: ${ext}` };
|
|
256
|
+
}
|
|
257
|
+
// Read the file content
|
|
258
|
+
const fileContent = await fs.readFile(file, 'utf8');
|
|
259
|
+
// Parse the file to get AST
|
|
260
|
+
const tree = parser.parse(fileContent);
|
|
261
|
+
// If className is provided, find and return only that class's AST
|
|
262
|
+
if (className) {
|
|
263
|
+
// Function to recursively search for the class node
|
|
264
|
+
const findClassNode = (node) => {
|
|
265
|
+
if ((node.type === 'class_declaration' ||
|
|
266
|
+
node.type === 'abstract_class_declaration' ||
|
|
267
|
+
node.type === 'class_definition') &&
|
|
268
|
+
node.text.includes(className)) {
|
|
269
|
+
return node;
|
|
270
|
+
}
|
|
271
|
+
// Recursively search children
|
|
272
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
273
|
+
const child = node.child(i);
|
|
274
|
+
const result = findClassNode(child);
|
|
275
|
+
if (result)
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
};
|
|
280
|
+
const classNode = findClassNode(tree.rootNode);
|
|
281
|
+
if (classNode) {
|
|
282
|
+
// Convert the class node to a JSON-serializable object
|
|
283
|
+
const serializeNode = (node) => {
|
|
284
|
+
const result = {
|
|
285
|
+
type: node.type,
|
|
286
|
+
text: node.text,
|
|
287
|
+
startPosition: { row: node.startPosition.row, column: node.startPosition.column },
|
|
288
|
+
endPosition: { row: node.endPosition.row, column: node.endPosition.column },
|
|
289
|
+
children: []
|
|
290
|
+
};
|
|
291
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
292
|
+
result.children.push(serializeNode(node.child(i)));
|
|
293
|
+
}
|
|
294
|
+
return result;
|
|
295
|
+
};
|
|
296
|
+
return serializeNode(classNode);
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
return { error: `Class '${className}' not found in file: ${file}` };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// Otherwise return the full AST
|
|
303
|
+
// Convert the full tree to a JSON-serializable object
|
|
304
|
+
const serializeNode = (node) => {
|
|
305
|
+
const result = {
|
|
306
|
+
type: node.type,
|
|
307
|
+
text: node.text,
|
|
308
|
+
startPosition: { row: node.startPosition.row, column: node.startPosition.column },
|
|
309
|
+
endPosition: { row: node.endPosition.row, column: node.endPosition.column },
|
|
310
|
+
children: []
|
|
311
|
+
};
|
|
312
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
313
|
+
result.children.push(serializeNode(node.child(i)));
|
|
314
|
+
}
|
|
315
|
+
return result;
|
|
316
|
+
};
|
|
317
|
+
return serializeNode(tree.rootNode);
|
|
26
318
|
}
|
|
27
319
|
};
|
|
28
320
|
exports.default = cbcodeparsers;
|