@optima-chat/browser-cli 0.8.0 → 0.9.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 +43 -52
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -186,14 +186,6 @@ var BrowserAPI = class {
|
|
|
186
186
|
body: JSON.stringify(body)
|
|
187
187
|
});
|
|
188
188
|
}
|
|
189
|
-
async listProfiles() {
|
|
190
|
-
return request("/api/profiles");
|
|
191
|
-
}
|
|
192
|
-
async deleteProfile(profileId) {
|
|
193
|
-
return request("/api/profiles/" + encodeURIComponent(profileId), {
|
|
194
|
-
method: "DELETE"
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
189
|
async setZiNiaoConfig(company, username, password) {
|
|
198
190
|
return request("/api/user/ziniao-config", {
|
|
199
191
|
method: "PUT",
|
|
@@ -452,20 +444,6 @@ function formatError(err) {
|
|
|
452
444
|
function formatTabsInline(tabs) {
|
|
453
445
|
return tabs.map((t) => `[${t.tab_id}] ${t.title}`).join(", ");
|
|
454
446
|
}
|
|
455
|
-
function formatProfiles(result) {
|
|
456
|
-
if (result.profiles.length === 0) {
|
|
457
|
-
return "No browser profiles found.";
|
|
458
|
-
}
|
|
459
|
-
const lines = result.profiles.map((p) => {
|
|
460
|
-
const sizeMb = (p.size_bytes / (1024 * 1024)).toFixed(1);
|
|
461
|
-
return ` ${p.profile_id} (${sizeMb} MB)`;
|
|
462
|
-
});
|
|
463
|
-
return `Browser profiles:
|
|
464
|
-
${lines.join("\n")}`;
|
|
465
|
-
}
|
|
466
|
-
function formatProfileDelete(profileId) {
|
|
467
|
-
return `Profile '${profileId}' deleted.`;
|
|
468
|
-
}
|
|
469
447
|
|
|
470
448
|
// src/commands/lifecycle.ts
|
|
471
449
|
function registerLifecycleCommands(program2) {
|
|
@@ -688,15 +666,38 @@ function registerTabCommands(program2) {
|
|
|
688
666
|
}
|
|
689
667
|
|
|
690
668
|
// src/commands/script.ts
|
|
691
|
-
import { readFileSync as readFileSync3 } from "fs";
|
|
669
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync3 } from "fs";
|
|
670
|
+
import { basename, dirname, resolve } from "path";
|
|
692
671
|
var POLL_INTERVAL_MS = 2e3;
|
|
693
672
|
var POLL_TIMEOUT_MS = 18e4;
|
|
673
|
+
var REQUIRED_ARG_FLAG = {
|
|
674
|
+
ziniao_profile_id: "--ziniao-profile <id>",
|
|
675
|
+
bitbrowser_profile_id: "--bitbrowser-profile <id>"
|
|
676
|
+
};
|
|
677
|
+
function formatRequiredArgs(s) {
|
|
678
|
+
if (!s.required_args?.length) return null;
|
|
679
|
+
const flags = s.required_args.map((a) => REQUIRED_ARG_FLAG[a] ?? `--param ${a}=<value>`).join(" ");
|
|
680
|
+
return ` requires: ${flags}`;
|
|
681
|
+
}
|
|
682
|
+
async function deliverArtifact(task, out) {
|
|
683
|
+
const output = task.output;
|
|
684
|
+
if (!output) return null;
|
|
685
|
+
if (output.path) return output.path;
|
|
686
|
+
if (!output.artifact_path) return null;
|
|
687
|
+
const target = resolve(out ?? `./${basename(output.filename ?? "artifact")}`);
|
|
688
|
+
const content = await api.fetchArtifact(output.artifact_path);
|
|
689
|
+
mkdirSync2(dirname(target), { recursive: true });
|
|
690
|
+
writeFileSync3(target, content);
|
|
691
|
+
return target;
|
|
692
|
+
}
|
|
694
693
|
function formatScriptList(scripts) {
|
|
695
694
|
if (scripts.length === 0) return "No scripts registered.";
|
|
696
695
|
const lines = [];
|
|
697
696
|
for (const s of scripts) {
|
|
698
697
|
const tag = s.source ? ` [${s.source}]` : "";
|
|
699
698
|
lines.push(`${s.name} \u2014 ${s.description}${tag}`);
|
|
699
|
+
const requires = formatRequiredArgs(s);
|
|
700
|
+
if (requires) lines.push(requires);
|
|
700
701
|
for (let i = 0; i < s.steps.length; i++) {
|
|
701
702
|
lines.push(` [${i + 1}] ${s.steps[i].id} \u2014 ${s.steps[i].description}`);
|
|
702
703
|
}
|
|
@@ -787,7 +788,7 @@ function registerScriptCommands(program2) {
|
|
|
787
788
|
process.exit(1);
|
|
788
789
|
}
|
|
789
790
|
});
|
|
790
|
-
program2.command("runscript").argument("<name>", "Script name").description("Run a script (polls until complete)").option("--param <params...>", "Parameters as key=value").option("--connector <type>", "Browser connector: chrome, ziniao, or bitbrowser").option("--ziniao-profile <id>", "ZiNiao browser profile ID (with --connector ziniao)").option("--bitbrowser-profile <id>", "BitBrowser profile ID (with --connector bitbrowser)").option("--no-poll", "Return immediately without polling").action(async (name, opts) => {
|
|
791
|
+
program2.command("runscript").argument("<name>", "Script name").description("Run a script (polls until complete)").option("--param <params...>", "Parameters as key=value").option("--connector <type>", "Browser connector: chrome, ziniao, or bitbrowser").option("--ziniao-profile <id>", "ZiNiao browser profile ID (with --connector ziniao)").option("--bitbrowser-profile <id>", "BitBrowser profile ID (with --connector bitbrowser)").option("--out <path>", "Where to write the script's exported file (default: ./<filename>)").option("--no-poll", "Return immediately without polling").action(async (name, opts) => {
|
|
791
792
|
try {
|
|
792
793
|
const params = {};
|
|
793
794
|
if (opts.param) {
|
|
@@ -796,6 +797,7 @@ function registerScriptCommands(program2) {
|
|
|
796
797
|
params[k] = rest.join("=");
|
|
797
798
|
}
|
|
798
799
|
}
|
|
800
|
+
if (opts.out) params.out_path = resolve(opts.out);
|
|
799
801
|
clearSessionId();
|
|
800
802
|
const task = await api.runScript(name, params, opts.connector, opts.ziniaoProfile, opts.bitbrowserProfile);
|
|
801
803
|
console.log(`Script started (task: ${task.id})`);
|
|
@@ -818,6 +820,18 @@ function registerScriptCommands(program2) {
|
|
|
818
820
|
saveSessionId(result.session_id);
|
|
819
821
|
}
|
|
820
822
|
console.log(formatScriptTask(result));
|
|
823
|
+
if (result.status === "completed") {
|
|
824
|
+
try {
|
|
825
|
+
const saved = await deliverArtifact(result, opts.out);
|
|
826
|
+
if (saved) console.log(`Saved -> ${saved}`);
|
|
827
|
+
} catch (err) {
|
|
828
|
+
const staged = result.output?.artifact_path;
|
|
829
|
+
console.error(`${formatError(err)}
|
|
830
|
+
Script completed; only saving the file failed.`);
|
|
831
|
+
if (staged) console.error(`The artifact is still at ${staged}.`);
|
|
832
|
+
process.exit(1);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
821
835
|
} catch (err) {
|
|
822
836
|
console.error(formatError(err));
|
|
823
837
|
process.exit(1);
|
|
@@ -834,29 +848,7 @@ function registerScriptCommands(program2) {
|
|
|
834
848
|
});
|
|
835
849
|
}
|
|
836
850
|
function sleep(ms) {
|
|
837
|
-
return new Promise((
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
// src/commands/profile.ts
|
|
841
|
-
function registerProfileCommands(program2) {
|
|
842
|
-
program2.command("profiles").description("List browser profiles").action(async () => {
|
|
843
|
-
try {
|
|
844
|
-
const result = await api.listProfiles();
|
|
845
|
-
console.log(formatProfiles(result));
|
|
846
|
-
} catch (err) {
|
|
847
|
-
console.error(formatError(err));
|
|
848
|
-
process.exit(1);
|
|
849
|
-
}
|
|
850
|
-
});
|
|
851
|
-
program2.command("delete-profile").description("Delete a named browser profile").argument("<profile-id>", "Profile ID to delete").action(async (profileId) => {
|
|
852
|
-
try {
|
|
853
|
-
const result = await api.deleteProfile(profileId);
|
|
854
|
-
console.log(formatProfileDelete(result.profile_id));
|
|
855
|
-
} catch (err) {
|
|
856
|
-
console.error(formatError(err));
|
|
857
|
-
process.exit(1);
|
|
858
|
-
}
|
|
859
|
-
});
|
|
851
|
+
return new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
860
852
|
}
|
|
861
853
|
|
|
862
854
|
// src/ziniao-doctor.ts
|
|
@@ -1033,13 +1025,13 @@ function registerZiNiaoCommands(program2) {
|
|
|
1033
1025
|
}
|
|
1034
1026
|
|
|
1035
1027
|
// src/commands/recipes.ts
|
|
1036
|
-
import { writeFileSync as
|
|
1037
|
-
import { resolve } from "path";
|
|
1028
|
+
import { writeFileSync as writeFileSync4 } from "fs";
|
|
1029
|
+
import { resolve as resolve2 } from "path";
|
|
1038
1030
|
async function runExport(script, params, out, timeoutMs) {
|
|
1039
|
-
const outPath =
|
|
1031
|
+
const outPath = resolve2(out);
|
|
1040
1032
|
const result = await api.runRecipeScript(script, { ...params, out_path: outPath }, 2e3, timeoutMs);
|
|
1041
1033
|
if (!result.path) {
|
|
1042
|
-
|
|
1034
|
+
writeFileSync4(outPath, await api.fetchArtifact(result.artifact_path));
|
|
1043
1035
|
}
|
|
1044
1036
|
console.log(`Saved ${result.filename} -> ${outPath} (${result.size} bytes)`);
|
|
1045
1037
|
}
|
|
@@ -1111,7 +1103,6 @@ registerInteractionCommands(program);
|
|
|
1111
1103
|
registerInfoCommands(program);
|
|
1112
1104
|
registerTabCommands(program);
|
|
1113
1105
|
registerScriptCommands(program);
|
|
1114
|
-
registerProfileCommands(program);
|
|
1115
1106
|
registerZiNiaoCommands(program);
|
|
1116
1107
|
registerRecipeCommands(program);
|
|
1117
1108
|
registerBitBrowserCommands(program);
|