@optima-chat/browser-cli 0.13.0 → 0.13.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/dist/index.js +42 -18
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -219,8 +219,20 @@ var BrowserAPI = class {
|
|
|
219
219
|
async getFeikuaProfiles() {
|
|
220
220
|
return request("/api/user/feikua-profiles");
|
|
221
221
|
}
|
|
222
|
+
/** true = 后端确实关了它;false = 它本来就没了(idle reaper 已经收走)。
|
|
223
|
+
*
|
|
224
|
+
* 「已经没了」不是错误。抛出去的话调用方连本地记录都不敢清,下一次 close 再撞
|
|
225
|
+
* 同一堵墙 —— 而 close 是 SKILL.md 写给 agent 的标准恢复动作,必须幂等(#225)。 */
|
|
222
226
|
async closeSession(sessionId) {
|
|
223
|
-
|
|
227
|
+
try {
|
|
228
|
+
await request(`/api/sessions/${sessionId}`, { method: "DELETE" });
|
|
229
|
+
return true;
|
|
230
|
+
} catch (err) {
|
|
231
|
+
if (err instanceof BrowserAPIError && err.errorCode === "session_not_found") {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
throw err;
|
|
235
|
+
}
|
|
224
236
|
}
|
|
225
237
|
async getStatus(sessionId) {
|
|
226
238
|
return request(`/api/sessions/${sessionId}/status`);
|
|
@@ -404,15 +416,17 @@ function loadSessionId() {
|
|
|
404
416
|
}
|
|
405
417
|
if (!existsSync2(SESSION_FILE)) {
|
|
406
418
|
throw new Error(
|
|
407
|
-
"No active browser session. Run 'browser-cli launch' first."
|
|
419
|
+
"No active browser session. Run 'browser-cli launch' first. (If the backend says you already have one, close it by id: browser-cli --session <id> close)"
|
|
408
420
|
);
|
|
409
421
|
}
|
|
410
422
|
return readFileSync2(SESSION_FILE, "utf-8").trim();
|
|
411
423
|
}
|
|
412
|
-
function clearSessionId() {
|
|
413
|
-
if (existsSync2(SESSION_FILE))
|
|
414
|
-
|
|
424
|
+
function clearSessionId(onlyIfEquals) {
|
|
425
|
+
if (!existsSync2(SESSION_FILE)) return;
|
|
426
|
+
if (onlyIfEquals !== void 0 && readFileSync2(SESSION_FILE, "utf-8").trim() !== onlyIfEquals) {
|
|
427
|
+
return;
|
|
415
428
|
}
|
|
429
|
+
unlinkSync(SESSION_FILE);
|
|
416
430
|
}
|
|
417
431
|
|
|
418
432
|
// src/formatter.ts
|
|
@@ -435,6 +449,9 @@ function formatLaunch(state) {
|
|
|
435
449
|
function formatClose(sessionId) {
|
|
436
450
|
return `\u2713 Browser closed (session: ${sessionId})`;
|
|
437
451
|
}
|
|
452
|
+
function formatAlreadyClosed(sessionId) {
|
|
453
|
+
return `\u2713 Session ${sessionId} was already closed (local record cleared)`;
|
|
454
|
+
}
|
|
438
455
|
function formatStatus(status) {
|
|
439
456
|
const lines = [];
|
|
440
457
|
lines.push(`Session: ${status.session_id}`);
|
|
@@ -507,9 +524,9 @@ function registerLifecycleCommands(program2) {
|
|
|
507
524
|
program2.command("close").description("Close the active browser session").action(async () => {
|
|
508
525
|
try {
|
|
509
526
|
const sessionId = loadSessionId();
|
|
510
|
-
await api.closeSession(sessionId);
|
|
511
|
-
clearSessionId();
|
|
512
|
-
console.log(formatClose(sessionId));
|
|
527
|
+
const closed = await api.closeSession(sessionId);
|
|
528
|
+
clearSessionId(sessionId);
|
|
529
|
+
console.log(closed ? formatClose(sessionId) : formatAlreadyClosed(sessionId));
|
|
513
530
|
} catch (err) {
|
|
514
531
|
console.error(formatError(err));
|
|
515
532
|
process.exit(1);
|
|
@@ -870,7 +887,6 @@ function registerScriptCommands(program2) {
|
|
|
870
887
|
}
|
|
871
888
|
}
|
|
872
889
|
if (opts.out) params.out_path = resolve(opts.out);
|
|
873
|
-
clearSessionId();
|
|
874
890
|
const task = await api.runScript(name, params, opts.connector, opts.ziniaoProfile, opts.bitbrowserProfile, opts.feikuaProfile);
|
|
875
891
|
console.log(`Script started (task: ${task.id})`);
|
|
876
892
|
if (!opts.poll) {
|
|
@@ -1100,6 +1116,7 @@ function registerZiNiaoCommands(program2) {
|
|
|
1100
1116
|
import { writeFileSync as writeFileSync4 } from "fs";
|
|
1101
1117
|
import { resolve as resolve2 } from "path";
|
|
1102
1118
|
var PUBLISH_TIMEOUT_MS = 18e5;
|
|
1119
|
+
var AD_REPORT_TIMEOUT_MS = 132e4;
|
|
1103
1120
|
var PUBLISH_TIMEOUT_HINT = "\u4EFB\u52A1\u4ECD\u5728\u540E\u7AEF\u8FD0\u884C,\u89C6\u9891\u53EF\u80FD\u5DF2\u7ECF\u53D1\u51FA\u53BB\u4E86\u3002\u53D1\u5E03\u72B6\u6001\u672A\u77E5 \u2014\u2014 \u8BF7\u5230\u5356\u5BB6\u540E\u53F0\u786E\u8BA4,\u4E0D\u8981\u91CD\u8DD1\u672C\u547D\u4EE4(\u4F1A\u53D1\u51FA\u91CD\u590D\u89C6\u9891)\u3002";
|
|
1104
1121
|
async function runExport(script, params, out, timeoutMs) {
|
|
1105
1122
|
const outPath = resolve2(out);
|
|
@@ -1115,16 +1132,23 @@ async function runExport(script, params, out, timeoutMs) {
|
|
|
1115
1132
|
console.log(`Saved ${result.filename} -> ${outPath} (${result.size} bytes)`);
|
|
1116
1133
|
}
|
|
1117
1134
|
function registerRecipeCommands(program2) {
|
|
1118
|
-
program2.command("export-ad-report").description("Export TikTok Shop
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1135
|
+
program2.command("export-ad-report").description("Export TikTok Shop ad data (per-creative by default) into the session workspace").requiredOption("--ziniao-profile <id>", "ZiNiao browser profile ID").option("--report-url <url>", "Ad dashboard URL (default: TikTok Shop MY local site)").option("--days <n>", "Date range: 7 / 30 / 90 / 180 / 365 (default: 7)").option(
|
|
1136
|
+
"--scope <scope>",
|
|
1137
|
+
"creative = per-creative rows (default) | daily = account-level daily totals"
|
|
1138
|
+
).option("--out <path>", "Output CSV path", "./tt_ad_report.csv").action(
|
|
1139
|
+
async (opts) => {
|
|
1140
|
+
try {
|
|
1141
|
+
const params = { ziniao_profile_id: opts.ziniaoProfile };
|
|
1142
|
+
if (opts.reportUrl) params.report_url = opts.reportUrl;
|
|
1143
|
+
if (opts.days) params.days = opts.days;
|
|
1144
|
+
if (opts.scope) params.scope = opts.scope;
|
|
1145
|
+
await runExport("tiktok_video_ad_export", params, opts.out, AD_REPORT_TIMEOUT_MS);
|
|
1146
|
+
} catch (err) {
|
|
1147
|
+
console.error(formatError(err));
|
|
1148
|
+
process.exit(1);
|
|
1149
|
+
}
|
|
1126
1150
|
}
|
|
1127
|
-
|
|
1151
|
+
);
|
|
1128
1152
|
program2.command("export-order-report").description("Export TikTok Shop order data into the session workspace").requiredOption("--ziniao-profile <id>", "ZiNiao browser profile ID").option("--report-url <url>", "Order list URL (default: TikTok Shop MY local site)").option("--days <n>", "Export the last N days (default: 7, max 365)").option("--out <path>", "Output CSV path", "./tt_orders.csv").action(async (opts) => {
|
|
1129
1153
|
try {
|
|
1130
1154
|
const params = { ziniao_profile_id: opts.ziniaoProfile };
|