@optima-chat/browser-cli 0.3.0 → 0.5.0
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/index.js +58 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ var ENV_CONFIG = {
|
|
|
16
16
|
apiUrl: "https://browser-api.stage.optima.onl"
|
|
17
17
|
},
|
|
18
18
|
development: {
|
|
19
|
-
apiUrl: "http://
|
|
19
|
+
apiUrl: "http://127.0.0.1:7292"
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
22
|
var OPTIMA_TOKEN_FILE = join(homedir(), ".optima", "token.json");
|
|
@@ -155,13 +155,23 @@ async function requestBinary(path) {
|
|
|
155
155
|
return Buffer.from(arrayBuffer);
|
|
156
156
|
}
|
|
157
157
|
var BrowserAPI = class {
|
|
158
|
-
async createSession(url) {
|
|
159
|
-
const body =
|
|
158
|
+
async createSession(url, profileId) {
|
|
159
|
+
const body = {};
|
|
160
|
+
if (url) body.url = url;
|
|
161
|
+
if (profileId) body.profile_id = profileId;
|
|
160
162
|
return request("/api/sessions", {
|
|
161
163
|
method: "POST",
|
|
162
164
|
body: JSON.stringify(body)
|
|
163
165
|
});
|
|
164
166
|
}
|
|
167
|
+
async listProfiles() {
|
|
168
|
+
return request("/api/profiles");
|
|
169
|
+
}
|
|
170
|
+
async deleteProfile(profileId) {
|
|
171
|
+
return request("/api/profiles/" + encodeURIComponent(profileId), {
|
|
172
|
+
method: "DELETE"
|
|
173
|
+
});
|
|
174
|
+
}
|
|
165
175
|
async closeSession(sessionId) {
|
|
166
176
|
await request(`/api/sessions/${sessionId}`, { method: "DELETE" });
|
|
167
177
|
}
|
|
@@ -321,10 +331,12 @@ var BrowserAPI = class {
|
|
|
321
331
|
async deleteScript(name) {
|
|
322
332
|
await request(`/api/scripts/${encodeURIComponent(name)}`, { method: "DELETE" });
|
|
323
333
|
}
|
|
324
|
-
async runScript(scriptName, params = {}) {
|
|
334
|
+
async runScript(scriptName, params = {}, profileId) {
|
|
335
|
+
const payload = { params };
|
|
336
|
+
if (profileId) payload.profile_id = profileId;
|
|
325
337
|
return request(`/api/scripts/${encodeURIComponent(scriptName)}/run`, {
|
|
326
338
|
method: "POST",
|
|
327
|
-
body: JSON.stringify(
|
|
339
|
+
body: JSON.stringify(payload)
|
|
328
340
|
});
|
|
329
341
|
}
|
|
330
342
|
async getScriptTask(taskId) {
|
|
@@ -493,10 +505,24 @@ function formatWorkflowTask(task) {
|
|
|
493
505
|
function formatWorkflowDeleted(name) {
|
|
494
506
|
return `\u2713 Workflow '${name}' deleted`;
|
|
495
507
|
}
|
|
508
|
+
function formatProfiles(result) {
|
|
509
|
+
if (result.profiles.length === 0) {
|
|
510
|
+
return "No browser profiles found.";
|
|
511
|
+
}
|
|
512
|
+
const lines = result.profiles.map((p) => {
|
|
513
|
+
const sizeMb = (p.size_bytes / (1024 * 1024)).toFixed(1);
|
|
514
|
+
return ` ${p.profile_id} (${sizeMb} MB)`;
|
|
515
|
+
});
|
|
516
|
+
return `Browser profiles:
|
|
517
|
+
${lines.join("\n")}`;
|
|
518
|
+
}
|
|
519
|
+
function formatProfileDelete(profileId) {
|
|
520
|
+
return `Profile '${profileId}' deleted.`;
|
|
521
|
+
}
|
|
496
522
|
|
|
497
523
|
// src/commands/lifecycle.ts
|
|
498
524
|
function registerLifecycleCommands(program2) {
|
|
499
|
-
program2.command("launch").description("Launch a browser session").option("--url <url>", "Navigate to URL after launch").option("--force", "Close existing session before launching").action(async (opts) => {
|
|
525
|
+
program2.command("launch").description("Launch a browser session").option("--url <url>", "Navigate to URL after launch").option("--profile <id>", "Use a named browser profile").option("--force", "Close existing session before launching").action(async (opts) => {
|
|
500
526
|
try {
|
|
501
527
|
if (opts.force) {
|
|
502
528
|
try {
|
|
@@ -507,7 +533,7 @@ function registerLifecycleCommands(program2) {
|
|
|
507
533
|
clearSessionId();
|
|
508
534
|
}
|
|
509
535
|
}
|
|
510
|
-
const state = await api.createSession(opts.url);
|
|
536
|
+
const state = await api.createSession(opts.url, opts.profile);
|
|
511
537
|
saveSessionId(state.session_id);
|
|
512
538
|
console.log(formatLaunch(state));
|
|
513
539
|
} catch (err) {
|
|
@@ -1108,7 +1134,7 @@ function registerScriptCommands(program2) {
|
|
|
1108
1134
|
process.exit(1);
|
|
1109
1135
|
}
|
|
1110
1136
|
});
|
|
1111
|
-
program2.command("runscript").argument("<name>", "Script name").description("Run a script (polls until complete)").option("--param <params...>", "Parameters as key=value").option("--no-poll", "Return immediately without polling").action(async (name, opts) => {
|
|
1137
|
+
program2.command("runscript").argument("<name>", "Script name").description("Run a script (polls until complete)").option("--param <params...>", "Parameters as key=value").option("--profile <id>", "Use a named browser profile").option("--no-poll", "Return immediately without polling").action(async (name, opts) => {
|
|
1112
1138
|
try {
|
|
1113
1139
|
const params = {};
|
|
1114
1140
|
if (opts.param) {
|
|
@@ -1118,7 +1144,7 @@ function registerScriptCommands(program2) {
|
|
|
1118
1144
|
}
|
|
1119
1145
|
}
|
|
1120
1146
|
clearSessionId();
|
|
1121
|
-
const task = await api.runScript(name, params);
|
|
1147
|
+
const task = await api.runScript(name, params, opts.profile);
|
|
1122
1148
|
console.log(`Script started (task: ${task.id})`);
|
|
1123
1149
|
if (!opts.poll) {
|
|
1124
1150
|
console.log(`Poll with: browser-cli script-task ${task.id}`);
|
|
@@ -1158,6 +1184,28 @@ function sleep(ms) {
|
|
|
1158
1184
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1159
1185
|
}
|
|
1160
1186
|
|
|
1187
|
+
// src/commands/profile.ts
|
|
1188
|
+
function registerProfileCommands(program2) {
|
|
1189
|
+
program2.command("profiles").description("List browser profiles").action(async () => {
|
|
1190
|
+
try {
|
|
1191
|
+
const result = await api.listProfiles();
|
|
1192
|
+
console.log(formatProfiles(result));
|
|
1193
|
+
} catch (err) {
|
|
1194
|
+
console.error(formatError(err));
|
|
1195
|
+
process.exit(1);
|
|
1196
|
+
}
|
|
1197
|
+
});
|
|
1198
|
+
program2.command("delete-profile").description("Delete a named browser profile").argument("<profile-id>", "Profile ID to delete").action(async (profileId) => {
|
|
1199
|
+
try {
|
|
1200
|
+
const result = await api.deleteProfile(profileId);
|
|
1201
|
+
console.log(formatProfileDelete(result.profile_id));
|
|
1202
|
+
} catch (err) {
|
|
1203
|
+
console.error(formatError(err));
|
|
1204
|
+
process.exit(1);
|
|
1205
|
+
}
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1161
1209
|
// src/index.ts
|
|
1162
1210
|
var require2 = createRequire(import.meta.url);
|
|
1163
1211
|
var { version } = require2("../package.json");
|
|
@@ -1176,4 +1224,5 @@ registerInfoCommands(program);
|
|
|
1176
1224
|
registerTabCommands(program);
|
|
1177
1225
|
registerWorkflowCommands(program);
|
|
1178
1226
|
registerScriptCommands(program);
|
|
1227
|
+
registerProfileCommands(program);
|
|
1179
1228
|
program.parse();
|