codex-browser-use 0.0.0-stage → 4.8.1
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/LICENSE +21 -0
- package/README.md +312 -2
- package/bin/browser-use.js +200 -0
- package/bin/chrome-native-host.js +29 -0
- package/extension/background.js +1384 -0
- package/extension/content-script.js +3 -0
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/manifest.json +33 -0
- package/package.json +44 -4
- package/rules/browser_use.md +26 -0
- package/skill/SKILL.md +72 -0
- package/skill/references/cookbook.md +281 -0
- package/skill/references/tools.md +18 -0
- package/src/ax-tree.js +889 -0
- package/src/backend-discovery.js +123 -0
- package/src/backend-native-driver.js +223 -0
- package/src/backend-page-commands.js +91 -0
- package/src/backend-tabs.js +490 -0
- package/src/backend-transport.js +165 -0
- package/src/bridge-browser.js +3787 -0
- package/src/bridge-client.js +181 -0
- package/src/config.js +166 -0
- package/src/content-extractor.js +576 -0
- package/src/engine-discovery.js +90 -0
- package/src/extension.js +26 -0
- package/src/frame-reader.js +160 -0
- package/src/install.js +303 -0
- package/src/locators.js +1497 -0
- package/src/official-runtime.js +359 -0
- package/src/portable-server.js +231 -0
- package/src/read-only.js +333 -0
- package/src/repl-session.js +180 -0
- package/src/repl-worker.js +190 -0
- package/src/script-runner.js +1546 -0
- package/src/server.js +1121 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single-allocation JSON frame encoder and O(n) chunked stream reader
|
|
3
|
+
* for 4-byte uint32 length-prefixed framing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Serialize and frame a JSON message in a single Buffer allocation.
|
|
8
|
+
* @param {unknown} message
|
|
9
|
+
* @param {{littleEndian:boolean, maxFrameBytes:number, overflowError?:string}} options
|
|
10
|
+
* @returns {Buffer}
|
|
11
|
+
*/
|
|
12
|
+
export function encodeFrame(message, {
|
|
13
|
+
littleEndian,
|
|
14
|
+
maxFrameBytes,
|
|
15
|
+
overflowError = "Message exceeds frame limit",
|
|
16
|
+
}) {
|
|
17
|
+
const json = JSON.stringify(message);
|
|
18
|
+
const byteLen = Buffer.byteLength(json, "utf8");
|
|
19
|
+
if (byteLen > maxFrameBytes) {
|
|
20
|
+
throw new Error(overflowError);
|
|
21
|
+
}
|
|
22
|
+
const packet = Buffer.allocUnsafe(4 + byteLen);
|
|
23
|
+
if (littleEndian) packet.writeUInt32LE(byteLen, 0);
|
|
24
|
+
else packet.writeUInt32BE(byteLen, 0);
|
|
25
|
+
packet.write(json, 4, byteLen, "utf8");
|
|
26
|
+
return packet;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Stateful O(n) reader for 4-byte length-prefixed streams without repeated Buffer.concat copies.
|
|
31
|
+
*/
|
|
32
|
+
export class FrameReader {
|
|
33
|
+
/**
|
|
34
|
+
* @param {{littleEndian:boolean, maxFrameBytes:number, maxBufferedBytes?:number, overflowError?:string, lengthError?:string}} options
|
|
35
|
+
*/
|
|
36
|
+
constructor({
|
|
37
|
+
littleEndian,
|
|
38
|
+
maxFrameBytes,
|
|
39
|
+
maxBufferedBytes = 2 * (maxFrameBytes + 4),
|
|
40
|
+
overflowError = "Input frame buffer exceeds limit",
|
|
41
|
+
lengthError = "Invalid frame length",
|
|
42
|
+
}) {
|
|
43
|
+
this.littleEndian = Boolean(littleEndian);
|
|
44
|
+
this.maxFrameBytes = maxFrameBytes;
|
|
45
|
+
this.maxBufferedBytes = maxBufferedBytes;
|
|
46
|
+
this.overflowError = overflowError;
|
|
47
|
+
this.lengthError = lengthError;
|
|
48
|
+
/** @type {Buffer[]} */
|
|
49
|
+
this.chunks = [];
|
|
50
|
+
this.headIndex = 0;
|
|
51
|
+
this.headOffset = 0;
|
|
52
|
+
this.bufferedBytes = 0;
|
|
53
|
+
this.headerScratch = Buffer.allocUnsafe(4);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @param {Buffer} chunk @returns {void} */
|
|
57
|
+
push(chunk) {
|
|
58
|
+
if (!chunk || chunk.length === 0) return;
|
|
59
|
+
if (this.bufferedBytes + chunk.length > this.maxBufferedBytes) {
|
|
60
|
+
throw new Error(this.overflowError);
|
|
61
|
+
}
|
|
62
|
+
this.chunks.push(chunk);
|
|
63
|
+
this.bufferedBytes += chunk.length;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @returns {Buffer | null} */
|
|
67
|
+
nextFrame() {
|
|
68
|
+
if (this.bufferedBytes < 4) return null;
|
|
69
|
+
const length = this.#peekLength();
|
|
70
|
+
if (length < 1 || length > this.maxFrameBytes) {
|
|
71
|
+
throw new Error(this.lengthError);
|
|
72
|
+
}
|
|
73
|
+
if (this.bufferedBytes < 4 + length) return null;
|
|
74
|
+
|
|
75
|
+
this.#advance(4);
|
|
76
|
+
|
|
77
|
+
const head = this.chunks[this.headIndex];
|
|
78
|
+
if (head.length - this.headOffset >= length) {
|
|
79
|
+
const frame = head.subarray(this.headOffset, this.headOffset + length);
|
|
80
|
+
this.#advance(length);
|
|
81
|
+
this.#compact();
|
|
82
|
+
return frame;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const frame = Buffer.allocUnsafe(length);
|
|
86
|
+
let written = 0;
|
|
87
|
+
this.bufferedBytes -= length;
|
|
88
|
+
while (written < length) {
|
|
89
|
+
const current = this.chunks[this.headIndex];
|
|
90
|
+
const avail = current.length - this.headOffset;
|
|
91
|
+
const take = Math.min(length - written, avail);
|
|
92
|
+
current.copy(frame, written, this.headOffset, this.headOffset + take);
|
|
93
|
+
written += take;
|
|
94
|
+
this.headOffset += take;
|
|
95
|
+
if (this.headOffset >= current.length) {
|
|
96
|
+
this.headIndex += 1;
|
|
97
|
+
this.headOffset = 0;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
this.#compact();
|
|
101
|
+
return frame;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @returns {number} */
|
|
105
|
+
#peekLength() {
|
|
106
|
+
const head = this.chunks[this.headIndex];
|
|
107
|
+
if (head.length - this.headOffset >= 4) {
|
|
108
|
+
return this.littleEndian
|
|
109
|
+
? head.readUInt32LE(this.headOffset)
|
|
110
|
+
: head.readUInt32BE(this.headOffset);
|
|
111
|
+
}
|
|
112
|
+
let idx = this.headIndex;
|
|
113
|
+
let off = this.headOffset;
|
|
114
|
+
let filled = 0;
|
|
115
|
+
while (filled < 4) {
|
|
116
|
+
const current = this.chunks[idx];
|
|
117
|
+
const take = Math.min(4 - filled, current.length - off);
|
|
118
|
+
current.copy(this.headerScratch, filled, off, off + take);
|
|
119
|
+
filled += take;
|
|
120
|
+
off += take;
|
|
121
|
+
if (off >= current.length) {
|
|
122
|
+
idx += 1;
|
|
123
|
+
off = 0;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return this.littleEndian
|
|
127
|
+
? this.headerScratch.readUInt32LE(0)
|
|
128
|
+
: this.headerScratch.readUInt32BE(0);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** @param {number} count @returns {void} */
|
|
132
|
+
#advance(count) {
|
|
133
|
+
let rem = count;
|
|
134
|
+
this.bufferedBytes -= rem;
|
|
135
|
+
while (rem > 0) {
|
|
136
|
+
const current = this.chunks[this.headIndex];
|
|
137
|
+
const avail = current.length - this.headOffset;
|
|
138
|
+
if (rem < avail) {
|
|
139
|
+
this.headOffset += rem;
|
|
140
|
+
rem = 0;
|
|
141
|
+
} else {
|
|
142
|
+
rem -= avail;
|
|
143
|
+
this.headIndex += 1;
|
|
144
|
+
this.headOffset = 0;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** @returns {void} */
|
|
150
|
+
#compact() {
|
|
151
|
+
if (this.headIndex >= this.chunks.length) {
|
|
152
|
+
this.chunks.length = 0;
|
|
153
|
+
this.headIndex = 0;
|
|
154
|
+
this.headOffset = 0;
|
|
155
|
+
} else if (this.headIndex >= 64) {
|
|
156
|
+
this.chunks = this.chunks.slice(this.headIndex);
|
|
157
|
+
this.headIndex = 0;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
package/src/install.js
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { cp, copyFile, mkdir, readdir, readFile, rename, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import {
|
|
7
|
+
EXTENSION_ID,
|
|
8
|
+
EXTENSION_VERSION,
|
|
9
|
+
HOST_NAME,
|
|
10
|
+
defaultBridgeFile,
|
|
11
|
+
extensionSource,
|
|
12
|
+
installedExtensionDir,
|
|
13
|
+
nativeHostLauncher,
|
|
14
|
+
} from "./extension.js";
|
|
15
|
+
import { buildConfig, clientRuleTarget, clientSkillTarget, clientTarget, normalizeClient, stableNodePath } from "./config.js";
|
|
16
|
+
|
|
17
|
+
const nativeHostEntry = fileURLToPath(new URL("../bin/chrome-native-host.js", import.meta.url));
|
|
18
|
+
const repoSkillSource = fileURLToPath(new URL("../../skill/SKILL.md", import.meta.url));
|
|
19
|
+
const localSkillSource = fileURLToPath(new URL("../skill/SKILL.md", import.meta.url));
|
|
20
|
+
const repoRuleSource = fileURLToPath(new URL("../../rules/browser_use.md", import.meta.url));
|
|
21
|
+
const localRuleSource = fileURLToPath(new URL("../rules/browser_use.md", import.meta.url));
|
|
22
|
+
|
|
23
|
+
/** @returns {Promise<string>} */
|
|
24
|
+
async function resolveDefaultSkillSource() {
|
|
25
|
+
for (const candidate of [repoSkillSource, localSkillSource]) {
|
|
26
|
+
try {
|
|
27
|
+
const info = await stat(candidate);
|
|
28
|
+
if (info.isFile()) return candidate;
|
|
29
|
+
} catch {
|
|
30
|
+
// Try next location.
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
throw new Error("Bundled Browser Use skill (skill/SKILL.md) was not found");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @returns {Promise<string>} */
|
|
37
|
+
async function resolveDefaultRuleSource() {
|
|
38
|
+
for (const candidate of [repoRuleSource, localRuleSource]) {
|
|
39
|
+
try {
|
|
40
|
+
const info = await stat(candidate);
|
|
41
|
+
if (info.isFile()) return candidate;
|
|
42
|
+
} catch {
|
|
43
|
+
// Try next location.
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
throw new Error("Bundled Browser Use policy rule (rules/browser_use.md) was not found");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @param {string} value */
|
|
50
|
+
function shellQuote(value) {
|
|
51
|
+
return `'${value.replaceAll("'", "'\\''")}'`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @param {string} file @param {string|Buffer} content @param {number} mode */
|
|
55
|
+
async function writeAtomic(file, content, mode) {
|
|
56
|
+
await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
57
|
+
const temporary = path.join(path.dirname(file), `.tmp-${randomUUID()}`);
|
|
58
|
+
await writeFile(temporary, content, { mode });
|
|
59
|
+
try {
|
|
60
|
+
await rename(temporary, file);
|
|
61
|
+
} finally {
|
|
62
|
+
await unlink(temporary).catch(() => {});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @param {string} file @returns {Promise<string|null>} */
|
|
67
|
+
async function backupOnce(file) {
|
|
68
|
+
let info;
|
|
69
|
+
try {
|
|
70
|
+
info = await stat(file);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (error.code === "ENOENT") return null;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
if (!info.isFile()) return null;
|
|
76
|
+
const backup = `${file}.bak`;
|
|
77
|
+
try {
|
|
78
|
+
await stat(backup);
|
|
79
|
+
return backup;
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (error.code !== "ENOENT") throw error;
|
|
82
|
+
}
|
|
83
|
+
await copyFile(file, backup);
|
|
84
|
+
return backup;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Copy the bundled extension to a stable path and report its pinned ID.
|
|
89
|
+
* @param {{source?:string, destination?:string}} [options]
|
|
90
|
+
*/
|
|
91
|
+
export async function installExtension({ source = extensionSource, destination = installedExtensionDir() } = {}) {
|
|
92
|
+
const manifest = JSON.parse(await readFile(path.join(source, "manifest.json"), "utf8"));
|
|
93
|
+
if (manifest.version !== EXTENSION_VERSION) {
|
|
94
|
+
throw new Error(`Bundled extension is ${manifest.version}, expected ${EXTENSION_VERSION}`);
|
|
95
|
+
}
|
|
96
|
+
let changed = false;
|
|
97
|
+
try {
|
|
98
|
+
const existing = JSON.parse(await readFile(path.join(destination, "manifest.json"), "utf8"));
|
|
99
|
+
changed = existing.version !== manifest.version;
|
|
100
|
+
} catch {
|
|
101
|
+
changed = true;
|
|
102
|
+
}
|
|
103
|
+
if (changed) {
|
|
104
|
+
const temporary = `${destination}.tmp-${randomUUID()}`;
|
|
105
|
+
await cp(source, temporary, { recursive: true });
|
|
106
|
+
const backup = await backupDirectory(destination);
|
|
107
|
+
await rename(temporary, destination);
|
|
108
|
+
return { path: destination, id: EXTENSION_ID, version: manifest.version, changed: true, backup };
|
|
109
|
+
}
|
|
110
|
+
return { path: destination, id: EXTENSION_ID, version: manifest.version, changed: false, backup: null };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** @param {string} destination @returns {Promise<string|null>} */
|
|
114
|
+
async function backupDirectory(destination) {
|
|
115
|
+
try {
|
|
116
|
+
await stat(destination);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (error.code === "ENOENT") return null;
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
const backup = `${destination}.bak`;
|
|
122
|
+
await rm(backup, { recursive: true, force: true });
|
|
123
|
+
await rename(destination, backup);
|
|
124
|
+
return backup;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @returns {string[]} */
|
|
128
|
+
export function nativeMessagingDirs() {
|
|
129
|
+
const home = homedir();
|
|
130
|
+
if (process.platform === "darwin") {
|
|
131
|
+
const support = path.join(home, "Library", "Application Support");
|
|
132
|
+
return [
|
|
133
|
+
path.join(support, "Google", "Chrome", "NativeMessagingHosts"),
|
|
134
|
+
path.join(support, "Chromium", "NativeMessagingHosts"),
|
|
135
|
+
path.join(support, "Microsoft Edge", "NativeMessagingHosts"),
|
|
136
|
+
path.join(support, "BraveSoftware", "Brave-Browser", "NativeMessagingHosts"),
|
|
137
|
+
];
|
|
138
|
+
}
|
|
139
|
+
if (process.platform === "linux") {
|
|
140
|
+
return [
|
|
141
|
+
path.join(home, ".config", "google-chrome", "NativeMessagingHosts"),
|
|
142
|
+
path.join(home, ".config", "chromium", "NativeMessagingHosts"),
|
|
143
|
+
path.join(home, ".config", "microsoft-edge", "NativeMessagingHosts"),
|
|
144
|
+
path.join(home, ".config", "BraveSoftware", "Brave-Browser", "NativeMessagingHosts"),
|
|
145
|
+
];
|
|
146
|
+
}
|
|
147
|
+
throw new Error("Native host installation supports macOS and Linux; on Windows, register the host with the Chrome registry key manually");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Write the launcher and native messaging manifests for every installed browser.
|
|
152
|
+
* @param {{extensionId?:string, bridgeFile?:string}} [options]
|
|
153
|
+
*/
|
|
154
|
+
export async function installNativeHost({ extensionId = EXTENSION_ID, bridgeFile = defaultBridgeFile() } = {}) {
|
|
155
|
+
const launcher = nativeHostLauncher();
|
|
156
|
+
const script = `#!/bin/sh\nexec ${shellQuote(stableNodePath())} ${shellQuote(nativeHostEntry)} ${shellQuote(path.resolve(bridgeFile))}\n`;
|
|
157
|
+
await writeAtomic(launcher, script, 0o700);
|
|
158
|
+
const manifest = {
|
|
159
|
+
name: HOST_NAME,
|
|
160
|
+
description: "Browser Use bridge native host",
|
|
161
|
+
path: launcher,
|
|
162
|
+
type: "stdio",
|
|
163
|
+
allowed_origins: [`chrome-extension://${extensionId}/`],
|
|
164
|
+
};
|
|
165
|
+
const written = [];
|
|
166
|
+
let chromeWritten = false;
|
|
167
|
+
for (const directory of nativeMessagingDirs()) {
|
|
168
|
+
const browserRoot = path.dirname(directory);
|
|
169
|
+
let exists = true;
|
|
170
|
+
try {
|
|
171
|
+
await stat(browserRoot);
|
|
172
|
+
} catch {
|
|
173
|
+
exists = false;
|
|
174
|
+
}
|
|
175
|
+
const isChrome = directory.includes(`${path.sep}Google${path.sep}Chrome${path.sep}`);
|
|
176
|
+
if (!exists && !isChrome) continue;
|
|
177
|
+
const file = path.join(directory, `${HOST_NAME}.json`);
|
|
178
|
+
const backup = await backupOnce(file);
|
|
179
|
+
await writeAtomic(file, `${JSON.stringify(manifest, null, 2)}\n`, 0o600);
|
|
180
|
+
written.push({ file, backup });
|
|
181
|
+
if (isChrome) chromeWritten = true;
|
|
182
|
+
}
|
|
183
|
+
if (!chromeWritten) throw new Error("Could not locate a Chrome native messaging directory");
|
|
184
|
+
return { launcher, manifests: written };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Merge the browser-use entry into a client's MCP configuration.
|
|
189
|
+
* @param {{client:string, bridgeFile?:string, profile?:string, write?:boolean, home?:string}} options
|
|
190
|
+
*/
|
|
191
|
+
export async function configureClient({ client, bridgeFile = defaultBridgeFile(), profile, write = false, home = homedir() }) {
|
|
192
|
+
const name = normalizeClient(client);
|
|
193
|
+
const target = clientTarget(name, home);
|
|
194
|
+
const config = buildConfig(name, { bridgeFile, profile });
|
|
195
|
+
if (!write || target.file === null) {
|
|
196
|
+
return { written: false, file: target.file, config };
|
|
197
|
+
}
|
|
198
|
+
let existing = {};
|
|
199
|
+
try {
|
|
200
|
+
existing = JSON.parse(await readFile(target.file, "utf8"));
|
|
201
|
+
} catch (error) {
|
|
202
|
+
if (error.code !== "ENOENT") throw new Error(`${target.file} is not valid JSON: ${error.message}`);
|
|
203
|
+
}
|
|
204
|
+
const merged = target.key === "mcp.servers"
|
|
205
|
+
? { ...existing, mcp: { ...existing.mcp, servers: { ...existing.mcp?.servers, ...config.mcp.servers } } }
|
|
206
|
+
: { ...existing, mcpServers: { ...existing.mcpServers, ...config.mcpServers } };
|
|
207
|
+
const backup = await backupOnce(target.file);
|
|
208
|
+
await writeAtomic(target.file, `${JSON.stringify(merged, null, 2)}\n`, 0o600);
|
|
209
|
+
return { written: true, file: target.file, backup, config };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const IGNORED_SKILL_FILES = new Set(["SKILL.md", ".DS_Store"]);
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Resolve source SKILL.md + directory even if caller passes either a file or a directory.
|
|
216
|
+
* @param {string|undefined} source
|
|
217
|
+
* @returns {Promise<{skillFile:string, skillDir:string}>}
|
|
218
|
+
*/
|
|
219
|
+
async function resolveSkillPaths(source) {
|
|
220
|
+
if (!source) {
|
|
221
|
+
const skillFile = await resolveDefaultSkillSource();
|
|
222
|
+
return { skillFile, skillDir: path.dirname(skillFile) };
|
|
223
|
+
}
|
|
224
|
+
const resolved = path.resolve(source);
|
|
225
|
+
const info = await stat(resolved);
|
|
226
|
+
if (info.isDirectory()) {
|
|
227
|
+
return { skillFile: path.join(resolved, "SKILL.md"), skillDir: resolved };
|
|
228
|
+
}
|
|
229
|
+
return { skillFile: resolved, skillDir: path.dirname(resolved) };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Recursively copy companion files inside a skill bundle (`references/`, etc.).
|
|
234
|
+
* @param {string} srcDir
|
|
235
|
+
* @param {string} dstDir
|
|
236
|
+
* @param {string} [prefix]
|
|
237
|
+
* @returns {Promise<string[]>}
|
|
238
|
+
*/
|
|
239
|
+
async function copyCompanionSkillFiles(srcDir, dstDir, prefix = "") {
|
|
240
|
+
const entries = await readdir(srcDir, { withFileTypes: true });
|
|
241
|
+
const copied = [];
|
|
242
|
+
for (const entry of entries) {
|
|
243
|
+
if (IGNORED_SKILL_FILES.has(entry.name) && prefix === "") continue;
|
|
244
|
+
if (entry.name === ".DS_Store" || entry.name.endsWith(".bak") || entry.name.startsWith(".tmp-")) continue;
|
|
245
|
+
const rel = prefix ? path.posix.join(prefix, entry.name) : entry.name;
|
|
246
|
+
const srcPath = path.join(srcDir, entry.name);
|
|
247
|
+
const dstPath = path.join(dstDir, entry.name);
|
|
248
|
+
if (entry.isDirectory()) {
|
|
249
|
+
copied.push(...await copyCompanionSkillFiles(srcPath, dstPath, rel));
|
|
250
|
+
} else if (entry.isFile()) {
|
|
251
|
+
await writeAtomic(dstPath, await readFile(srcPath), 0o644);
|
|
252
|
+
copied.push(rel);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return copied;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Install the Browser Use skill bundle (`SKILL.md` + companion `references/` files)
|
|
260
|
+
* into a client's standard skills directory.
|
|
261
|
+
* @param {{client?:string, home?:string, source?:string}} [options]
|
|
262
|
+
* @returns {Promise<{written:boolean, client:string, file:string, dir:string, files:string[], backup:string|null}>}
|
|
263
|
+
*/
|
|
264
|
+
export async function installSkill({ client = "generic", home = homedir(), source } = {}) {
|
|
265
|
+
const name = normalizeClient(client);
|
|
266
|
+
const target = clientSkillTarget(name, home);
|
|
267
|
+
const targetDir = path.dirname(target);
|
|
268
|
+
const { skillFile, skillDir } = await resolveSkillPaths(source);
|
|
269
|
+
const content = await readFile(skillFile, "utf8");
|
|
270
|
+
const backup = await backupOnce(target);
|
|
271
|
+
await writeAtomic(target, content, 0o644);
|
|
272
|
+
const companions = await copyCompanionSkillFiles(skillDir, targetDir);
|
|
273
|
+
return {
|
|
274
|
+
written: true,
|
|
275
|
+
client: name,
|
|
276
|
+
file: target,
|
|
277
|
+
dir: targetDir,
|
|
278
|
+
files: ["SKILL.md", ...companions.sort()],
|
|
279
|
+
backup,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Install the always-on Browser Use policy rules file for clients that use standalone rule files.
|
|
285
|
+
* @param {{client?:string, home?:string, source?:string}} [options]
|
|
286
|
+
* @returns {Promise<{written:boolean, client:string, file:string|null, backup:string|null, skipped?:boolean}>}
|
|
287
|
+
*/
|
|
288
|
+
export async function installRules({ client = "generic", home = homedir(), source } = {}) {
|
|
289
|
+
const name = normalizeClient(client);
|
|
290
|
+
const target = clientRuleTarget(name, home);
|
|
291
|
+
if (!target.file) {
|
|
292
|
+
return { written: false, client: name, file: null, backup: null, skipped: true };
|
|
293
|
+
}
|
|
294
|
+
const src = source ? path.resolve(source) : await resolveDefaultRuleSource();
|
|
295
|
+
let content = await readFile(src, "utf8");
|
|
296
|
+
if (target.format === "mdc") {
|
|
297
|
+
const body = content.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n*/u, "");
|
|
298
|
+
content = `---\ndescription: Browser Use MCP automation policy\nalwaysApply: true\n---\n\n${body}`;
|
|
299
|
+
}
|
|
300
|
+
const backup = await backupOnce(target.file);
|
|
301
|
+
await writeAtomic(target.file, content, 0o644);
|
|
302
|
+
return { written: true, client: name, file: target.file, backup };
|
|
303
|
+
}
|