@eko-ai/eko-nodejs 3.0.0-alpha.3 → 3.0.0-alpha.5
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/README.md +1 -1
- package/dist/browser.d.ts +1 -0
- package/dist/browser.d.ts.map +1 -1
- package/dist/index.cjs.js +144 -13
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +143 -13
- package/dist/index.esm.js.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.esm.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { realpathSync as realpathSync$1, readlinkSync, readdirSync, readdir as readdir$1, lstatSync } from 'fs';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import * as path$1 from 'path';
|
|
1
5
|
import { Log, BaseBrowserLabelsAgent, BaseFileAgent, uuidv4 } from '@eko-ai/eko';
|
|
2
6
|
import { chromium } from 'playwright';
|
|
3
|
-
import * as fs from 'fs/promises';
|
|
4
|
-
import * as path$1 from 'path';
|
|
7
|
+
import * as fs$1 from 'fs/promises';
|
|
5
8
|
import { fileURLToPath } from 'node:url';
|
|
6
9
|
import { win32, posix } from 'node:path';
|
|
7
|
-
import { realpathSync as realpathSync$1, readlinkSync, readdirSync, readdir as readdir$1, lstatSync } from 'fs';
|
|
8
10
|
import * as actualFS from 'node:fs';
|
|
9
11
|
import { realpath, readlink, readdir, lstat } from 'node:fs/promises';
|
|
10
12
|
import { EventEmitter } from 'node:events';
|
|
@@ -19,6 +21,104 @@ async function getCdpWsEndpoint(port) {
|
|
|
19
21
|
Log.info("browserInfo: ", browserInfo);
|
|
20
22
|
return browserInfo.webSocketDebuggerUrl;
|
|
21
23
|
}
|
|
24
|
+
function getDefaultChromeUserDataDir(copyToTempDir = false) {
|
|
25
|
+
const platform = os.platform();
|
|
26
|
+
const homeDir = os.homedir();
|
|
27
|
+
let defaultPath;
|
|
28
|
+
switch (platform) {
|
|
29
|
+
case "win32":
|
|
30
|
+
// Windows: %LOCALAPPDATA%\Google\Chrome\User Data
|
|
31
|
+
const localAppData = process.env.LOCALAPPDATA || path$1.join(homeDir, "AppData", "Local");
|
|
32
|
+
defaultPath = path$1.join(localAppData, "Google", "Chrome", "User Data");
|
|
33
|
+
break;
|
|
34
|
+
case "darwin":
|
|
35
|
+
// macOS: ~/Library/Application Support/Google/Chrome
|
|
36
|
+
defaultPath = path$1.join(homeDir, "Library", "Application Support", "Google", "Chrome");
|
|
37
|
+
break;
|
|
38
|
+
case "linux":
|
|
39
|
+
// Linux: ~/.config/google-chrome
|
|
40
|
+
defaultPath = path$1.join(homeDir, ".config", "google-chrome");
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
if (defaultPath && fs.existsSync(defaultPath)) {
|
|
44
|
+
if (copyToTempDir) {
|
|
45
|
+
const tempDir = os.tmpdir();
|
|
46
|
+
const tempPath = path$1.join(tempDir, "chrome-user-data");
|
|
47
|
+
if (fs.existsSync(tempPath)) {
|
|
48
|
+
Log.info(`Removing existing temp directory: ${tempPath}`);
|
|
49
|
+
fs.rmSync(tempPath, { recursive: true, force: true });
|
|
50
|
+
}
|
|
51
|
+
fs.cpSync(defaultPath, tempPath, { recursive: true });
|
|
52
|
+
// Delete all Chrome locked files and directories to prevent startup conflicts.
|
|
53
|
+
removeLockFiles(tempPath);
|
|
54
|
+
const defaultProfilePath = path$1.join(tempPath, "Default");
|
|
55
|
+
if (fs.existsSync(defaultProfilePath)) {
|
|
56
|
+
removeLockFiles(defaultProfilePath);
|
|
57
|
+
}
|
|
58
|
+
Log.info(`Created clean Chrome user data directory: ${tempPath}`);
|
|
59
|
+
return tempPath;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
return defaultPath;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
function removeLockFiles(dirPath) {
|
|
68
|
+
try {
|
|
69
|
+
const items = fs.readdirSync(dirPath);
|
|
70
|
+
items.forEach((item) => {
|
|
71
|
+
const itemPath = path$1.join(dirPath, item);
|
|
72
|
+
try {
|
|
73
|
+
const stat = fs.statSync(itemPath);
|
|
74
|
+
if (stat.isDirectory()) {
|
|
75
|
+
removeLockFiles(itemPath);
|
|
76
|
+
}
|
|
77
|
+
const shouldDelete = item === "SingletonLock" ||
|
|
78
|
+
item === "lockfile" ||
|
|
79
|
+
item === "RunningChromeVersion" ||
|
|
80
|
+
item === "SingletonCookie" ||
|
|
81
|
+
item === "SingletonSocket" ||
|
|
82
|
+
item === "chrome_debug.log" ||
|
|
83
|
+
item === "LOCK" ||
|
|
84
|
+
item === "LOG" ||
|
|
85
|
+
item === "LOG.old" ||
|
|
86
|
+
item.includes(".lock") ||
|
|
87
|
+
item.includes("Lock") ||
|
|
88
|
+
item.includes("LOCK") ||
|
|
89
|
+
item.includes(".tmp") ||
|
|
90
|
+
item.includes("Temp") ||
|
|
91
|
+
item.endsWith(".pid") ||
|
|
92
|
+
item.endsWith(".log") ||
|
|
93
|
+
item.includes("chrome_shutdown_ms.txt") ||
|
|
94
|
+
item.includes("Crashpad") ||
|
|
95
|
+
(stat.isDirectory() &&
|
|
96
|
+
(item.includes("CrashReports") ||
|
|
97
|
+
item.includes("ShaderCache") ||
|
|
98
|
+
item.includes("crashpad_database")));
|
|
99
|
+
if (shouldDelete) {
|
|
100
|
+
fs.rmSync(itemPath, { recursive: true, force: true });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch (statError) {
|
|
104
|
+
if (item.includes("Lock") ||
|
|
105
|
+
item.includes("lock") ||
|
|
106
|
+
item.includes("LOCK")) {
|
|
107
|
+
try {
|
|
108
|
+
Log.info(`Force deleting suspected lock file: ${itemPath}`);
|
|
109
|
+
fs.rmSync(itemPath, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
catch (deleteError) {
|
|
112
|
+
Log.warn(`Failed to force delete ${itemPath}:`, deleteError);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
Log.warn(`Error while removing lock files from ${dirPath}:`, error);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
22
122
|
|
|
23
123
|
class BrowserAgent extends BaseBrowserLabelsAgent {
|
|
24
124
|
constructor() {
|
|
@@ -34,6 +134,15 @@ class BrowserAgent extends BaseBrowserLabelsAgent {
|
|
|
34
134
|
setCdpWsEndpoint(cdpWsEndpoint) {
|
|
35
135
|
this.cdpWsEndpoint = cdpWsEndpoint;
|
|
36
136
|
}
|
|
137
|
+
initUserDataDir(userDataDir) {
|
|
138
|
+
if (userDataDir) {
|
|
139
|
+
this.userDataDir = userDataDir;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.userDataDir = getDefaultChromeUserDataDir(true);
|
|
143
|
+
}
|
|
144
|
+
return this.userDataDir;
|
|
145
|
+
}
|
|
37
146
|
setOptions(options) {
|
|
38
147
|
this.options = options;
|
|
39
148
|
}
|
|
@@ -193,13 +302,34 @@ class BrowserAgent extends BaseBrowserLabelsAgent {
|
|
|
193
302
|
this.browser_context = await chromium.launchPersistentContext(this.userDataDir, {
|
|
194
303
|
headless: this.headless,
|
|
195
304
|
// channel: 'chrome',
|
|
305
|
+
args: [
|
|
306
|
+
"--no-sandbox",
|
|
307
|
+
"--remote-allow-origins=*",
|
|
308
|
+
"--disable-dev-shm-usage",
|
|
309
|
+
"--disable-popup-blocking",
|
|
310
|
+
"--enable-automation",
|
|
311
|
+
"--ignore-ssl-errors",
|
|
312
|
+
"--ignore-certificate-errors",
|
|
313
|
+
"--ignore-certificate-errors-spki-list",
|
|
314
|
+
"--disable-blink-features=AutomationControlled",
|
|
315
|
+
],
|
|
196
316
|
...this.options,
|
|
197
317
|
});
|
|
198
318
|
}
|
|
199
319
|
else {
|
|
200
320
|
this.browser = await chromium.launch({
|
|
201
321
|
headless: this.headless,
|
|
202
|
-
args: [
|
|
322
|
+
args: [
|
|
323
|
+
"--no-sandbox",
|
|
324
|
+
"--remote-allow-origins=*",
|
|
325
|
+
"--disable-dev-shm-usage",
|
|
326
|
+
"--disable-popup-blocking",
|
|
327
|
+
"--enable-automation",
|
|
328
|
+
"--ignore-ssl-errors",
|
|
329
|
+
"--ignore-certificate-errors",
|
|
330
|
+
"--ignore-certificate-errors-spki-list",
|
|
331
|
+
"--disable-blink-features=AutomationControlled",
|
|
332
|
+
],
|
|
203
333
|
...this.options,
|
|
204
334
|
});
|
|
205
335
|
this.browser_context = await this.browser.newContext();
|
|
@@ -8166,10 +8296,10 @@ glob.glob = glob;
|
|
|
8166
8296
|
|
|
8167
8297
|
class FileAgent extends BaseFileAgent {
|
|
8168
8298
|
async file_list(agentContext, directoryPath) {
|
|
8169
|
-
const files = await fs.readdir(directoryPath);
|
|
8299
|
+
const files = await fs$1.readdir(directoryPath);
|
|
8170
8300
|
const fileDetails = await Promise.all(files.map(async (file) => {
|
|
8171
8301
|
const filePath = path$1.join(directoryPath, file);
|
|
8172
|
-
const stats = await fs.stat(filePath);
|
|
8302
|
+
const stats = await fs$1.stat(filePath);
|
|
8173
8303
|
return {
|
|
8174
8304
|
name: file,
|
|
8175
8305
|
path: filePath,
|
|
@@ -8181,32 +8311,32 @@ class FileAgent extends BaseFileAgent {
|
|
|
8181
8311
|
return fileDetails;
|
|
8182
8312
|
}
|
|
8183
8313
|
async file_read(agentContext, filePath) {
|
|
8184
|
-
return await fs.readFile(filePath, "utf-8");
|
|
8314
|
+
return await fs$1.readFile(filePath, "utf-8");
|
|
8185
8315
|
}
|
|
8186
8316
|
async file_write(agentContext, filePath, content, append) {
|
|
8187
8317
|
const directory = path$1.dirname(filePath);
|
|
8188
|
-
await fs.mkdir(directory, { recursive: true });
|
|
8318
|
+
await fs$1.mkdir(directory, { recursive: true });
|
|
8189
8319
|
if (append) {
|
|
8190
|
-
await fs.appendFile(filePath, content, "utf-8");
|
|
8320
|
+
await fs$1.appendFile(filePath, content, "utf-8");
|
|
8191
8321
|
}
|
|
8192
8322
|
else {
|
|
8193
|
-
await fs.writeFile(filePath, content, "utf-8");
|
|
8323
|
+
await fs$1.writeFile(filePath, content, "utf-8");
|
|
8194
8324
|
}
|
|
8195
8325
|
}
|
|
8196
8326
|
async file_str_replace(agentContext, filePath, oldStr, newStr) {
|
|
8197
|
-
let content = await fs.readFile(filePath, "utf-8");
|
|
8327
|
+
let content = await fs$1.readFile(filePath, "utf-8");
|
|
8198
8328
|
const originalContent = content;
|
|
8199
8329
|
content = content.replace(new RegExp(oldStr, "g"), newStr);
|
|
8200
8330
|
if (content === originalContent) {
|
|
8201
8331
|
return;
|
|
8202
8332
|
}
|
|
8203
|
-
await fs.writeFile(filePath, content, "utf-8");
|
|
8333
|
+
await fs$1.writeFile(filePath, content, "utf-8");
|
|
8204
8334
|
}
|
|
8205
8335
|
async file_find_by_name(agentContext, directoryPath, globPattern) {
|
|
8206
8336
|
const pattern = path$1.join(directoryPath, globPattern);
|
|
8207
8337
|
const files = await glob.glob(pattern);
|
|
8208
8338
|
const fileDetails = await Promise.all(files.map(async (file) => {
|
|
8209
|
-
const stats = await fs.stat(file);
|
|
8339
|
+
const stats = await fs$1.stat(file);
|
|
8210
8340
|
return {
|
|
8211
8341
|
name: path$1.basename(file),
|
|
8212
8342
|
path: file,
|