Skip to content

Commit 433d08f

Browse files
committed
added intial files
0 parents  commit 433d08f

26 files changed

+3304
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
node_modules

package-lock.json

Lines changed: 1705 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@codebolt/codeboltjs",
3+
"version": "1.0.0",
4+
"description": "",
5+
"keywords": [],
6+
"author": "",
7+
"main": "src/index.ts",
8+
"license": "ISC",
9+
"scripts": {
10+
"build": "tsc",
11+
"build:docs": "npx typedoc --plugin typedoc-plugin-missing-exports",
12+
"test": "jest fs.test.js"
13+
},
14+
"dependencies": {
15+
"typedoc-plugin-missing-exports": "^2.2.0",
16+
"ws": "^8.16.0"
17+
},
18+
"devDependencies": {
19+
"@types/ws": "^8.5.10",
20+
"ts-loader": "^9.5.1",
21+
"typedoc": "^0.25.13",
22+
"typescript": "^5.4.5",
23+
"webpack": "^5.91.0",
24+
"webpack-cli": "^5.1.4",
25+
"jest": "^29.7.0",
26+
"jest-serial-runner": "^1.2.1"
27+
},
28+
"jest": {
29+
"testTimeout": 50000
30+
}
31+
}

src/index.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import cbws from './modules/websocket';
2+
import cbfs from './modules/fs';
3+
import cbllm from './modules/llm';
4+
import cbterminal from './modules/terminal';
5+
import cbbrowser from './modules/browser';
6+
import cbchat from './modules/chat';
7+
import cbcodeutils from './modules/codeutils';
8+
import cbdocutils from './modules/docutils';
9+
import cbcrawler from './modules/crawler';
10+
import cbsearch from './modules/search';
11+
import cbknowledge from './modules/knowledge';
12+
import cbrag from './modules/rag';
13+
import cbcodeparsers from './modules/codeparsers';
14+
import cboutputparsers from './modules/outputparsers';
15+
import cbproject from './modules/project';
16+
import git from './modules/git';
17+
import dbmemory from './modules/dbmemory';
18+
import WebSocket from 'ws';
19+
20+
/**
21+
* @class Codebolt
22+
* @description This class provides a unified interface to interact with various modules.
23+
*/
24+
class Codebolt {
25+
26+
/**
27+
* @constructor
28+
* @description Initializes the websocket connection.
29+
*/
30+
constructor(){
31+
this.websocket = cbws.getWebsocket;
32+
}
33+
34+
/**
35+
* @method waitForConnection
36+
* @description Waits for the WebSocket connection to open.
37+
* @returns {Promise<void>} A promise that resolves when the WebSocket connection is open.
38+
*/
39+
async waitForConnection() {
40+
return new Promise<void>((resolve, reject) => {
41+
if (!this.websocket) {
42+
reject(new Error('WebSocket is not initialized'));
43+
return;
44+
}
45+
46+
if (this.websocket.readyState === WebSocket.OPEN) {
47+
resolve();
48+
return;
49+
}
50+
51+
this.websocket.addEventListener('open', () => {
52+
resolve();
53+
});
54+
55+
this.websocket.addEventListener('error', (error) => {
56+
reject(error);
57+
});
58+
});
59+
}
60+
61+
/**
62+
* @method start_browser
63+
* @description Starts a new browser page.
64+
* @param {string} objective - The objective of the browser session.
65+
* @param {string} url - The URL to navigate to.
66+
* @param {string} previous_command - The previous command executed.
67+
* @param {string} browser_content - The content of the browser.
68+
*/
69+
start_browser(objective:string, url:string, previous_command:string, browser_content:string) {
70+
cbbrowser.newPage();
71+
}
72+
73+
websocket: WebSocket | null = null;
74+
fs = cbfs;
75+
git=git;
76+
llm = cbllm;
77+
browser = cbbrowser;
78+
chat = cbchat;
79+
terminal = cbterminal;
80+
codeutils = cbcodeutils;
81+
docutils = cbdocutils;
82+
crawler = cbcrawler;
83+
search = cbsearch;
84+
knowledge = cbknowledge;
85+
rag = cbrag;
86+
codeparsers = cbcodeparsers;
87+
outputparsers = cboutputparsers;
88+
project = cbproject;
89+
dbmemory = dbmemory;
90+
}
91+
92+
export default new Codebolt();

src/modules/browser.ts

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
import cbws from './websocket';
2+
3+
/**
4+
* A module for interacting with a browser through WebSockets.
5+
*/
6+
const cbbrowser = {
7+
8+
/**
9+
* Opens a new page in the browser.
10+
*/
11+
newPage: () => {
12+
cbws.getWebsocket.send(JSON.stringify({
13+
"type": "browserEvent",
14+
action: 'newPage'
15+
}));
16+
},
17+
18+
/**
19+
* Retrieves the current URL of the browser's active page.
20+
* @returns {Promise<any>} A promise that resolves with the URL.
21+
*/
22+
getUrl: () => {
23+
return new Promise((resolve, reject) => {
24+
cbws.getWebsocket.send(JSON.stringify({
25+
"type": "browserEvent",
26+
action: 'getUrl'
27+
}));
28+
cbws.getWebsocket.on('message', (data: string) => {
29+
const response = JSON.parse(data);
30+
if (response.event === "getUrlResponse") {
31+
resolve(response);
32+
}
33+
});
34+
});
35+
},
36+
37+
/**
38+
* Navigates to a specified URL.
39+
* @param {string} url - The URL to navigate to.
40+
* @returns {Promise<any>} A promise that resolves when navigation is complete.
41+
*/
42+
goToPage: (url: string) => {
43+
return new Promise((resolve, reject) => {
44+
cbws.getWebsocket.send(JSON.stringify({
45+
"type": "browserEvent",
46+
action: 'goToPage',
47+
url
48+
}));
49+
cbws.getWebsocket.on('message', (data: string) => {
50+
const response = JSON.parse(data);
51+
if (response.event === "goToPageResponse") {
52+
resolve(response);
53+
}
54+
});
55+
});
56+
},
57+
58+
/**
59+
* Takes a screenshot of the current page.
60+
*/
61+
screenshot: () => {
62+
cbws.getWebsocket.send(JSON.stringify({
63+
"type": "browserEvent",
64+
action: 'screenshot'
65+
}));
66+
},
67+
68+
/**
69+
* Retrieves the HTML content of the current page.
70+
* @returns {Promise<string>} A promise that resolves with the HTML content.
71+
*/
72+
getHTML: () => {
73+
return new Promise((resolve, reject) => {
74+
cbws.getWebsocket.send(JSON.stringify({
75+
"type": "browserEvent",
76+
action: 'getHTML'
77+
}));
78+
cbws.getWebsocket.on('message', (data: string) => {
79+
const response = JSON.parse(data);
80+
if (response.event === "htmlReceived") {
81+
resolve(response.htmlResponse);
82+
}
83+
});
84+
});
85+
},
86+
87+
/**
88+
* Retrieves the Markdown content of the current page.
89+
* @returns {Promise<any>} A promise that resolves with the Markdown content.
90+
*/
91+
getMarkdown: () => {
92+
return new Promise((resolve, reject) => {
93+
cbws.getWebsocket.send(JSON.stringify({
94+
"type": "browserEvent",
95+
action: 'getMarkdown'
96+
}));
97+
cbws.getWebsocket.on('message', (data: string) => {
98+
const response = JSON.parse(data);
99+
if (response.event === "getMarkdownResponse") {
100+
resolve(response);
101+
}
102+
});
103+
});
104+
},
105+
106+
/**
107+
* Retrieves the PDF content of the current page.
108+
*/
109+
getPDF: () => {
110+
cbws.getWebsocket.send(JSON.stringify({
111+
"type": "browserEvent",
112+
action: 'getPDF'
113+
}));
114+
},
115+
116+
/**
117+
* Converts the PDF content of the current page to text.
118+
*/
119+
pdfToText: () => {
120+
cbws.getWebsocket.send(JSON.stringify({
121+
"type": "browserEvent",
122+
action: 'pdfToText'
123+
}));
124+
},
125+
126+
/**
127+
* Retrieves the content of the current page.
128+
*/
129+
getContent: () => {
130+
cbws.getWebsocket.send(JSON.stringify({
131+
"type": "browserEvent",
132+
action: 'getContent'
133+
}));
134+
},
135+
136+
/**
137+
* Extracts text from the current page.
138+
*/
139+
extractText: () => {
140+
cbws.getWebsocket.send(JSON.stringify({
141+
"type": "browserEvent",
142+
action: 'extractText'
143+
}));
144+
},
145+
146+
/**
147+
* Closes the current page.
148+
*/
149+
close: () => {
150+
cbws.getWebsocket.send(JSON.stringify({
151+
"type": "browserEvent",
152+
action: 'close'
153+
}));
154+
},
155+
156+
/**
157+
* Scrolls the current page in a specified direction by a specified number of pixels.
158+
* @param {string} direction - The direction to scroll.
159+
* @param {string} pixels - The number of pixels to scroll.
160+
* @returns {Promise<any>} A promise that resolves when the scroll action is complete.
161+
*/
162+
scroll: (direction: string, pixels: string) => {
163+
return new Promise((resolve, reject) => {
164+
cbws.getWebsocket.send(JSON.stringify({
165+
"type": "browserEvent",
166+
action: 'scroll',
167+
direction,
168+
pixels
169+
}));
170+
cbws.getWebsocket.on('message', (data: string) => {
171+
const response = JSON.parse(data);
172+
if (response.event === "scrollResponse") {
173+
resolve(response);
174+
}
175+
});
176+
});
177+
},
178+
179+
/**
180+
* Types text into a specified element on the page.
181+
* @param {string} elementid - The ID of the element to type into.
182+
* @param {string} text - The text to type.
183+
* @returns {Promise<any>} A promise that resolves when the typing action is complete.
184+
*/
185+
type: (elementid: string, text: string) => {
186+
return new Promise((resolve, reject) => {
187+
cbws.getWebsocket.send(JSON.stringify({
188+
"type": "browserEvent",
189+
action: 'type',
190+
text,
191+
elementid
192+
}));
193+
cbws.getWebsocket.on('message', (data: string) => {
194+
const response = JSON.parse(data);
195+
if (response.event === "typeResponse") {
196+
resolve(response);
197+
}
198+
});
199+
});
200+
},
201+
202+
/**
203+
* Clicks on a specified element on the page.
204+
* @param {string} elementid - The ID of the element to click.
205+
* @returns {Promise<any>} A promise that resolves when the click action is complete.
206+
*/
207+
click: (elementid: string) => {
208+
return new Promise((resolve, reject) => {
209+
cbws.getWebsocket.send(JSON.stringify({
210+
"type": "browserEvent",
211+
action: 'click',
212+
elementid
213+
}));
214+
cbws.getWebsocket.on('message', (data: string) => {
215+
const response = JSON.parse(data);
216+
if (response.event === "clickResponse") {
217+
resolve(response);
218+
}
219+
});
220+
});
221+
},
222+
223+
/**
224+
* Simulates the Enter key press on the current page.
225+
* @returns {Promise<any>} A promise that resolves when the Enter action is complete.
226+
*/
227+
enter: () => {
228+
return new Promise((resolve, reject) => {
229+
cbws.getWebsocket.send(JSON.stringify({
230+
"type": "browserEvent",
231+
action: 'enter'
232+
}));
233+
cbws.getWebsocket.on('message', (data: string) => {
234+
const response = JSON.parse(data);
235+
if (response.event === "EnterResponse") {
236+
resolve(response);
237+
}
238+
});
239+
});
240+
},
241+
242+
/**
243+
* Performs a search on the current page using a specified query.
244+
* @param {string} elementid - The ID of the element to perform the search in.
245+
* @param {string} query - The search query.
246+
* @returns {Promise<any>} A promise that resolves with the search results.
247+
*/
248+
search: (elementid: string, query: string) => {
249+
return new Promise((resolve, reject) => {
250+
cbws.getWebsocket.send(JSON.stringify({
251+
"type": "browserEvent",
252+
action: 'search',
253+
elementid,
254+
query
255+
}));
256+
cbws.getWebsocket.on('message', (data: string) => {
257+
const response = JSON.parse(data);
258+
if (response.event === "searchResponse") {
259+
resolve(response);
260+
}
261+
});
262+
});
263+
}
264+
}
265+
266+
export default cbbrowser;

0 commit comments

Comments
 (0)