@gobing-ai/knowledge-kit 0.1.16 → 0.1.17
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/.claude-plugin/marketplace.json +1 -1
- package/dist/index.js +162 -39
- package/package.json +1 -1
- package/plugins/kk/plugin.json +1 -1
- package/plugins/kk/publish-presets/daily-ai-voice.json +8 -8
- package/plugins/kk/publish-presets/itc-main.json +42 -0
- package/plugins/kk/scripts/cover-compositor.ts +54 -8
- package/plugins/kk/scripts/itc-stages.ts +3 -2
- package/plugins/kk/scripts/voice3-publish-window.ts +5 -1
- package/plugins/kk/workflows/kk-daily-publish3.yaml +10 -3
- package/plugins/publishings/bilibili-pub/dist/index.js +89 -16
- package/plugins/publishings/bilibili-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/cnblogs-pub/dist/index.js +89 -16
- package/plugins/publishings/cnblogs-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/csdn-pub/dist/index.js +89 -16
- package/plugins/publishings/csdn-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/infoq-pub/dist/index.js +119 -18
- package/plugins/publishings/infoq-pub/dist/profile-check.js +94 -17
- package/plugins/publishings/infoq-pub/src/index.ts +40 -1
- package/plugins/publishings/jike-pub/dist/index.js +89 -16
- package/plugins/publishings/jike-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/juejin-pub/dist/index.js +116 -19
- package/plugins/publishings/juejin-pub/dist/profile-check.js +93 -17
- package/plugins/publishings/juejin-pub/src/index.ts +41 -2
- package/plugins/publishings/linkedin-pub/dist/index.js +89 -16
- package/plugins/publishings/linkedin-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/shipinhao-pub/dist/index.js +89 -16
- package/plugins/publishings/shipinhao-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/sspai-pub/dist/index.js +89 -16
- package/plugins/publishings/sspai-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/substack-pub/dist/index.js +89 -16
- package/plugins/publishings/substack-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/surfdash-pub/dist/index.js +9 -1
- package/plugins/publishings/surfdash-pub/src/index.ts +17 -2
- package/plugins/publishings/wechatmp-pub/dist/index.js +176 -32
- package/plugins/publishings/wechatmp-pub/dist/profile-check.js +95 -18
- package/plugins/publishings/wechatmp-pub/src/cgi-draft.ts +14 -2
- package/plugins/publishings/wechatmp-pub/src/flow.ts +155 -21
- package/plugins/publishings/wechatmp-pub/src/profile-check.ts +8 -1
- package/plugins/publishings/weibo-pub/dist/index.js +89 -16
- package/plugins/publishings/weibo-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/x-pub/dist/index.js +89 -16
- package/plugins/publishings/x-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/xhs-pub/dist/index.js +89 -16
- package/plugins/publishings/xhs-pub/dist/profile-check.js +89 -16
- package/plugins/publishings/xiaoyuzhou-pub/dist/index.js +89 -16
- package/plugins/publishings/xiaoyuzhou-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/zenn-pub/dist/index.js +93 -1
- package/plugins/publishings/zenn-pub/src/index.ts +134 -1
|
@@ -31,8 +31,9 @@
|
|
|
31
31
|
* the source-locale article profile share content.json (adapt:false); every other profile
|
|
32
32
|
* gets content.<variant>.json (adapt:true) and each variant carries the union of its
|
|
33
33
|
* targets' draft/live switches (V3). Exits 1, naming the target, for an unknown or
|
|
34
|
-
* non-article target
|
|
35
|
-
* wechatmp-pub live)
|
|
34
|
+
* non-article target or a target with no switch for the run's mode (x-pub in draft,
|
|
35
|
+
* wechatmp-pub live). A target that needs a cover when 4-illustrations/cover.* is absent is
|
|
36
|
+
* SKIPPED instead: the plan records a warning and every other target still plans.
|
|
36
37
|
*
|
|
37
38
|
* itc-adapt-verify <work_dir>
|
|
38
39
|
* R3 follow-up check for the adapt state: every plan variant with adapt:true must have its
|
|
@@ -26,7 +26,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
26
26
|
|
|
27
27
|
const USAGE =
|
|
28
28
|
'usage: voice3-publish-window.ts --window <after-generate|cn-peak|overseas-rss> [--run-date <YYYY-MM-DD|YYYYMMDD>] ' +
|
|
29
|
-
'[--preset <slug>] [--live-channels a,b] [--work-dir <dir>] [--dummy] [--dry-run]';
|
|
29
|
+
'[--preset <slug>] [--live-channels a,b] [--live-confirm true] [--work-dir <dir>] [--dummy] [--dry-run]';
|
|
30
30
|
|
|
31
31
|
const WINDOWS = new Set(['after-generate', 'cn-peak', 'overseas-rss']);
|
|
32
32
|
|
|
@@ -96,6 +96,9 @@ function main(): void {
|
|
|
96
96
|
if (window === undefined || !WINDOWS.has(window))
|
|
97
97
|
failWith(1, `${USAGE}\nvoice3-publish-window: --window must be one of ${[...WINDOWS].join('|')}`);
|
|
98
98
|
const dummy = args.includes('--dummy');
|
|
99
|
+
// ADR-022 amendment 2026-11-12: the preset's own rows carry live consent, so the send-confirm
|
|
100
|
+
// pause is opt-in. `--live-confirm true` restores it for a supervised one-off window.
|
|
101
|
+
const liveConfirm = (flagValue(args, '--live-confirm') ?? 'false').trim() === 'true';
|
|
99
102
|
const dryRun = args.includes('--dry-run');
|
|
100
103
|
const preset = flagValue(args, '--preset');
|
|
101
104
|
const liveChannelsRaw = flagValue(args, '--live-channels');
|
|
@@ -182,6 +185,7 @@ function main(): void {
|
|
|
182
185
|
if (preset !== undefined && preset.trim() !== '') vars.preset = preset;
|
|
183
186
|
if (liveChannels.length > 0) vars.live_channels = liveChannels.join(',');
|
|
184
187
|
if (dummy) vars.dummy = 'true';
|
|
188
|
+
if (liveConfirm) vars.live_confirm = 'true';
|
|
185
189
|
|
|
186
190
|
if (dryRun) {
|
|
187
191
|
console.log(JSON.stringify(vars, null, 2));
|
|
@@ -19,6 +19,10 @@ vars:
|
|
|
19
19
|
window: after-generate
|
|
20
20
|
# Empty = the operator's `workflows.kk-daily-ai-voice3.publish_preset` selector decides.
|
|
21
21
|
preset: ""
|
|
22
|
+
# 2026-11-12 (ADR-022 amendment): the preset owns the live/draft decision, so the unattended
|
|
23
|
+
# daily posture runs without a human at the pause. `live_confirm: "true"` restores the
|
|
24
|
+
# send-confirm pause for an interactive, one-off window.
|
|
25
|
+
live_confirm: "false"
|
|
22
26
|
channels: ""
|
|
23
27
|
live_channels: ""
|
|
24
28
|
dummy: "false"
|
|
@@ -114,15 +118,18 @@ nodes:
|
|
|
114
118
|
- id: send-confirm
|
|
115
119
|
description: >-
|
|
116
120
|
Operator approval of a LIVE send: `pause: true` stops the run and RESUME is the approval;
|
|
117
|
-
cancelling is the rejection. Skipped for a draft-only window, so
|
|
118
|
-
|
|
121
|
+
cancelling is the rejection. Skipped for a draft-only window, so a rehearsal proceeds without
|
|
122
|
+
a human. Since 2026-11-12 (ADR-022 amendment) the operator's consent for the scheduled daily
|
|
123
|
+
posture lives in the preset's own modes, so the pause now requires `live_confirm=true` —
|
|
124
|
+
a scheduled run with live rows sends without stopping here.
|
|
119
125
|
dependsOn: [check]
|
|
120
126
|
pause: true
|
|
121
127
|
condition:
|
|
122
128
|
kind: shell
|
|
123
129
|
options:
|
|
124
130
|
command: >-
|
|
125
|
-
|
|
131
|
+
test "${vars.live_confirm}" = "true"
|
|
132
|
+
&& jq -e '[.rows[] | select(.mode == "live")] | length > 0'
|
|
126
133
|
"${vars.work_dir}/4-publish/plan.${vars.window}.json" >/dev/null 2>&1
|
|
127
134
|
|
|
128
135
|
- id: send
|
|
@@ -20446,8 +20446,11 @@ import {
|
|
|
20446
20446
|
mkdirSync as mkdirSync2,
|
|
20447
20447
|
openSync,
|
|
20448
20448
|
readdirSync as readdirSync2,
|
|
20449
|
+
readlinkSync,
|
|
20449
20450
|
realpathSync as realpathSync2,
|
|
20450
|
-
|
|
20451
|
+
renameSync as renameSync2,
|
|
20452
|
+
rmdirSync,
|
|
20453
|
+
unlinkSync
|
|
20451
20454
|
} from "fs";
|
|
20452
20455
|
function lstat(path) {
|
|
20453
20456
|
try {
|
|
@@ -20482,6 +20485,33 @@ function rmdir(path) {
|
|
|
20482
20485
|
throw error;
|
|
20483
20486
|
}
|
|
20484
20487
|
}
|
|
20488
|
+
function readlink(path) {
|
|
20489
|
+
try {
|
|
20490
|
+
return readlinkSync(path);
|
|
20491
|
+
} catch {
|
|
20492
|
+
return null;
|
|
20493
|
+
}
|
|
20494
|
+
}
|
|
20495
|
+
function unlink(path) {
|
|
20496
|
+
try {
|
|
20497
|
+
unlinkSync(path);
|
|
20498
|
+
return true;
|
|
20499
|
+
} catch (error) {
|
|
20500
|
+
if (error.code === "ENOENT")
|
|
20501
|
+
return false;
|
|
20502
|
+
throw error;
|
|
20503
|
+
}
|
|
20504
|
+
}
|
|
20505
|
+
function rename(from, to) {
|
|
20506
|
+
try {
|
|
20507
|
+
renameSync2(from, to);
|
|
20508
|
+
return true;
|
|
20509
|
+
} catch (error) {
|
|
20510
|
+
if (error.code === "ENOENT")
|
|
20511
|
+
return false;
|
|
20512
|
+
throw error;
|
|
20513
|
+
}
|
|
20514
|
+
}
|
|
20485
20515
|
function chmod(path, mode) {
|
|
20486
20516
|
chmodSync(path, mode);
|
|
20487
20517
|
}
|
|
@@ -20493,14 +20523,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join2(homedir2(), ".config", "kk", "browser-t
|
|
|
20493
20523
|
|
|
20494
20524
|
// ../../packages/publish-harness/src/profile-lease.ts
|
|
20495
20525
|
import { createHash, randomBytes } from "crypto";
|
|
20496
|
-
import { homedir as homedir3 } from "os";
|
|
20526
|
+
import { homedir as homedir3, hostname } from "os";
|
|
20497
20527
|
import { basename, dirname as dirname3, join as join3, resolve as resolve3 } from "path";
|
|
20498
20528
|
var DEFAULT_BOT_LEASE_ROOT = join3(homedir3(), ".config", "kk", "browser-leases");
|
|
20499
20529
|
var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
|
|
20500
20530
|
var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
|
|
20501
20531
|
var OWNER_FILE = "owner.json";
|
|
20502
20532
|
var HOLDER_PREFIX = "holder-";
|
|
20503
|
-
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
|
|
20533
|
+
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
|
|
20504
20534
|
async function acquireProfileLease(profileDir, options = {}) {
|
|
20505
20535
|
const fileSystem = options.fileSystem ?? createNodeFileSystem();
|
|
20506
20536
|
const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
|
|
@@ -20668,6 +20698,8 @@ function isProcessAlive(pid) {
|
|
|
20668
20698
|
}
|
|
20669
20699
|
}
|
|
20670
20700
|
function refuseNativeLock(profileDir) {
|
|
20701
|
+
if (reclaimStaleNativeLock(profileDir))
|
|
20702
|
+
return;
|
|
20671
20703
|
for (const name of NATIVE_LOCK_FILES) {
|
|
20672
20704
|
const path = join3(profileDir, name);
|
|
20673
20705
|
if (lstat(path) !== null) {
|
|
@@ -20675,6 +20707,24 @@ function refuseNativeLock(profileDir) {
|
|
|
20675
20707
|
}
|
|
20676
20708
|
}
|
|
20677
20709
|
}
|
|
20710
|
+
function reclaimStaleNativeLock(profileDir) {
|
|
20711
|
+
const lockPath = join3(profileDir, "SingletonLock");
|
|
20712
|
+
const target = readlink(lockPath);
|
|
20713
|
+
if (target === null)
|
|
20714
|
+
return false;
|
|
20715
|
+
const match = /^(.*)-(\d+)$/.exec(target);
|
|
20716
|
+
if (match === null)
|
|
20717
|
+
return false;
|
|
20718
|
+
const [, host, pidText] = match;
|
|
20719
|
+
if (host !== hostname())
|
|
20720
|
+
return false;
|
|
20721
|
+
const pid = Number(pidText);
|
|
20722
|
+
if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
|
|
20723
|
+
return false;
|
|
20724
|
+
for (const name of NATIVE_LOCK_FILES)
|
|
20725
|
+
unlink(join3(profileDir, name));
|
|
20726
|
+
return true;
|
|
20727
|
+
}
|
|
20678
20728
|
function sha256Hex(value) {
|
|
20679
20729
|
return createHash("sha256").update(value).digest("hex");
|
|
20680
20730
|
}
|
|
@@ -20709,6 +20759,27 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
|
|
|
20709
20759
|
// ../../packages/publish-harness/src/browser.ts
|
|
20710
20760
|
var DEFAULT_CHECK_TIMEOUT_MS = 30000;
|
|
20711
20761
|
var DEFAULT_POLL_MS = 500;
|
|
20762
|
+
function isInstantBrowserDeath(message) {
|
|
20763
|
+
return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
|
|
20764
|
+
}
|
|
20765
|
+
function classifyLaunchError(error, profileDir, channel) {
|
|
20766
|
+
const message = errorMessage2(error);
|
|
20767
|
+
if (/singleton|already in use|in use by another/i.test(message))
|
|
20768
|
+
return new BrowserProfileBusyError(profileDir, error);
|
|
20769
|
+
if (/playwright install|executable doesn't exist/i.test(message)) {
|
|
20770
|
+
return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
|
|
20771
|
+
}
|
|
20772
|
+
if (isInstantBrowserDeath(message)) {
|
|
20773
|
+
return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
|
|
20774
|
+
}
|
|
20775
|
+
return error instanceof Error ? error : new Error(message);
|
|
20776
|
+
}
|
|
20777
|
+
function quarantineProfileSyncData(profileDir) {
|
|
20778
|
+
const syncData = join5(profileDir, "Default", "Sync Data");
|
|
20779
|
+
if (lstat(syncData) === null)
|
|
20780
|
+
return false;
|
|
20781
|
+
return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
|
|
20782
|
+
}
|
|
20712
20783
|
function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
|
|
20713
20784
|
return {
|
|
20714
20785
|
async launch(request) {
|
|
@@ -20737,23 +20808,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
|
|
|
20737
20808
|
throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
|
|
20738
20809
|
}
|
|
20739
20810
|
const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
|
|
20811
|
+
const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
|
|
20812
|
+
headless,
|
|
20813
|
+
channel,
|
|
20814
|
+
args: ["--disable-blink-features=AutomationControlled"],
|
|
20815
|
+
ignoreDefaultArgs: ["--enable-automation"]
|
|
20816
|
+
});
|
|
20740
20817
|
let context;
|
|
20741
20818
|
try {
|
|
20742
|
-
context = await
|
|
20743
|
-
headless,
|
|
20744
|
-
channel,
|
|
20745
|
-
args: ["--disable-blink-features=AutomationControlled"],
|
|
20746
|
-
ignoreDefaultArgs: ["--enable-automation"]
|
|
20747
|
-
});
|
|
20819
|
+
context = await launch();
|
|
20748
20820
|
} catch (error) {
|
|
20749
|
-
|
|
20750
|
-
|
|
20751
|
-
|
|
20752
|
-
|
|
20753
|
-
|
|
20754
|
-
|
|
20821
|
+
if (!isInstantBrowserDeath(errorMessage2(error)))
|
|
20822
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20823
|
+
if (!quarantineProfileSyncData(profileDir))
|
|
20824
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20825
|
+
try {
|
|
20826
|
+
context = await launch();
|
|
20827
|
+
} catch (retryError) {
|
|
20828
|
+
throw classifyLaunchError(retryError, profileDir, channel);
|
|
20755
20829
|
}
|
|
20756
|
-
throw error;
|
|
20757
20830
|
}
|
|
20758
20831
|
try {
|
|
20759
20832
|
await context.addInitScript(() => {
|
|
@@ -19853,8 +19853,11 @@ import {
|
|
|
19853
19853
|
mkdirSync as mkdirSync2,
|
|
19854
19854
|
openSync,
|
|
19855
19855
|
readdirSync as readdirSync2,
|
|
19856
|
+
readlinkSync,
|
|
19856
19857
|
realpathSync as realpathSync2,
|
|
19857
|
-
|
|
19858
|
+
renameSync as renameSync2,
|
|
19859
|
+
rmdirSync,
|
|
19860
|
+
unlinkSync
|
|
19858
19861
|
} from "fs";
|
|
19859
19862
|
function lstat(path) {
|
|
19860
19863
|
try {
|
|
@@ -19889,6 +19892,33 @@ function rmdir(path) {
|
|
|
19889
19892
|
throw error;
|
|
19890
19893
|
}
|
|
19891
19894
|
}
|
|
19895
|
+
function readlink(path) {
|
|
19896
|
+
try {
|
|
19897
|
+
return readlinkSync(path);
|
|
19898
|
+
} catch {
|
|
19899
|
+
return null;
|
|
19900
|
+
}
|
|
19901
|
+
}
|
|
19902
|
+
function unlink(path) {
|
|
19903
|
+
try {
|
|
19904
|
+
unlinkSync(path);
|
|
19905
|
+
return true;
|
|
19906
|
+
} catch (error) {
|
|
19907
|
+
if (error.code === "ENOENT")
|
|
19908
|
+
return false;
|
|
19909
|
+
throw error;
|
|
19910
|
+
}
|
|
19911
|
+
}
|
|
19912
|
+
function rename(from, to) {
|
|
19913
|
+
try {
|
|
19914
|
+
renameSync2(from, to);
|
|
19915
|
+
return true;
|
|
19916
|
+
} catch (error) {
|
|
19917
|
+
if (error.code === "ENOENT")
|
|
19918
|
+
return false;
|
|
19919
|
+
throw error;
|
|
19920
|
+
}
|
|
19921
|
+
}
|
|
19892
19922
|
function chmod(path, mode) {
|
|
19893
19923
|
chmodSync(path, mode);
|
|
19894
19924
|
}
|
|
@@ -19900,14 +19930,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join(homedir(), ".config", "kk", "browser-tra
|
|
|
19900
19930
|
|
|
19901
19931
|
// ../../packages/publish-harness/src/profile-lease.ts
|
|
19902
19932
|
import { createHash, randomBytes } from "crypto";
|
|
19903
|
-
import { homedir as homedir2 } from "os";
|
|
19933
|
+
import { homedir as homedir2, hostname } from "os";
|
|
19904
19934
|
import { basename, dirname as dirname3, join as join2, resolve as resolve2 } from "path";
|
|
19905
19935
|
var DEFAULT_BOT_LEASE_ROOT = join2(homedir2(), ".config", "kk", "browser-leases");
|
|
19906
19936
|
var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
|
|
19907
19937
|
var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
|
|
19908
19938
|
var OWNER_FILE = "owner.json";
|
|
19909
19939
|
var HOLDER_PREFIX = "holder-";
|
|
19910
|
-
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
|
|
19940
|
+
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
|
|
19911
19941
|
async function acquireProfileLease(profileDir, options = {}) {
|
|
19912
19942
|
const fileSystem = options.fileSystem ?? createNodeFileSystem();
|
|
19913
19943
|
const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
|
|
@@ -20075,6 +20105,8 @@ function isProcessAlive(pid) {
|
|
|
20075
20105
|
}
|
|
20076
20106
|
}
|
|
20077
20107
|
function refuseNativeLock(profileDir) {
|
|
20108
|
+
if (reclaimStaleNativeLock(profileDir))
|
|
20109
|
+
return;
|
|
20078
20110
|
for (const name of NATIVE_LOCK_FILES) {
|
|
20079
20111
|
const path = join2(profileDir, name);
|
|
20080
20112
|
if (lstat(path) !== null) {
|
|
@@ -20082,6 +20114,24 @@ function refuseNativeLock(profileDir) {
|
|
|
20082
20114
|
}
|
|
20083
20115
|
}
|
|
20084
20116
|
}
|
|
20117
|
+
function reclaimStaleNativeLock(profileDir) {
|
|
20118
|
+
const lockPath = join2(profileDir, "SingletonLock");
|
|
20119
|
+
const target = readlink(lockPath);
|
|
20120
|
+
if (target === null)
|
|
20121
|
+
return false;
|
|
20122
|
+
const match = /^(.*)-(\d+)$/.exec(target);
|
|
20123
|
+
if (match === null)
|
|
20124
|
+
return false;
|
|
20125
|
+
const [, host, pidText] = match;
|
|
20126
|
+
if (host !== hostname())
|
|
20127
|
+
return false;
|
|
20128
|
+
const pid = Number(pidText);
|
|
20129
|
+
if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
|
|
20130
|
+
return false;
|
|
20131
|
+
for (const name of NATIVE_LOCK_FILES)
|
|
20132
|
+
unlink(join2(profileDir, name));
|
|
20133
|
+
return true;
|
|
20134
|
+
}
|
|
20085
20135
|
function sha256Hex(value) {
|
|
20086
20136
|
return createHash("sha256").update(value).digest("hex");
|
|
20087
20137
|
}
|
|
@@ -20140,6 +20190,27 @@ var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
|
|
|
20140
20190
|
var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
|
|
20141
20191
|
var DEFAULT_CHECK_TIMEOUT_MS = 30000;
|
|
20142
20192
|
var DEFAULT_POLL_MS = 500;
|
|
20193
|
+
function isInstantBrowserDeath(message) {
|
|
20194
|
+
return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
|
|
20195
|
+
}
|
|
20196
|
+
function classifyLaunchError(error, profileDir, channel) {
|
|
20197
|
+
const message = errorMessage2(error);
|
|
20198
|
+
if (/singleton|already in use|in use by another/i.test(message))
|
|
20199
|
+
return new BrowserProfileBusyError(profileDir, error);
|
|
20200
|
+
if (/playwright install|executable doesn't exist/i.test(message)) {
|
|
20201
|
+
return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
|
|
20202
|
+
}
|
|
20203
|
+
if (isInstantBrowserDeath(message)) {
|
|
20204
|
+
return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
|
|
20205
|
+
}
|
|
20206
|
+
return error instanceof Error ? error : new Error(message);
|
|
20207
|
+
}
|
|
20208
|
+
function quarantineProfileSyncData(profileDir) {
|
|
20209
|
+
const syncData = join5(profileDir, "Default", "Sync Data");
|
|
20210
|
+
if (lstat(syncData) === null)
|
|
20211
|
+
return false;
|
|
20212
|
+
return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
|
|
20213
|
+
}
|
|
20143
20214
|
function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
|
|
20144
20215
|
return {
|
|
20145
20216
|
async launch(request) {
|
|
@@ -20168,23 +20239,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
|
|
|
20168
20239
|
throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
|
|
20169
20240
|
}
|
|
20170
20241
|
const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
|
|
20242
|
+
const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
|
|
20243
|
+
headless,
|
|
20244
|
+
channel,
|
|
20245
|
+
args: ["--disable-blink-features=AutomationControlled"],
|
|
20246
|
+
ignoreDefaultArgs: ["--enable-automation"]
|
|
20247
|
+
});
|
|
20171
20248
|
let context;
|
|
20172
20249
|
try {
|
|
20173
|
-
context = await
|
|
20174
|
-
headless,
|
|
20175
|
-
channel,
|
|
20176
|
-
args: ["--disable-blink-features=AutomationControlled"],
|
|
20177
|
-
ignoreDefaultArgs: ["--enable-automation"]
|
|
20178
|
-
});
|
|
20250
|
+
context = await launch();
|
|
20179
20251
|
} catch (error) {
|
|
20180
|
-
|
|
20181
|
-
|
|
20182
|
-
|
|
20183
|
-
|
|
20184
|
-
|
|
20185
|
-
|
|
20252
|
+
if (!isInstantBrowserDeath(errorMessage2(error)))
|
|
20253
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20254
|
+
if (!quarantineProfileSyncData(profileDir))
|
|
20255
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20256
|
+
try {
|
|
20257
|
+
context = await launch();
|
|
20258
|
+
} catch (retryError) {
|
|
20259
|
+
throw classifyLaunchError(retryError, profileDir, channel);
|
|
20186
20260
|
}
|
|
20187
|
-
throw error;
|
|
20188
20261
|
}
|
|
20189
20262
|
try {
|
|
20190
20263
|
await context.addInitScript(() => {
|
|
@@ -20341,6 +20414,7 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
|
|
|
20341
20414
|
if (command === "login") {
|
|
20342
20415
|
const loginTimeoutMs = timeoutMs ?? config.loginTimeoutMs ?? DEFAULT_LOGIN_TIMEOUT_MS;
|
|
20343
20416
|
const launcher = deps.launcher ?? kkBrowserLauncher;
|
|
20417
|
+
stdout(`${config.channel} profile login: a browser window is opening \u2014 sign in there; waiting up to ${Math.round(loginTimeoutMs / 1000)}s for the signed-in state`);
|
|
20344
20418
|
try {
|
|
20345
20419
|
if (url === undefined) {
|
|
20346
20420
|
await loginWithProfile({
|
|
@@ -20467,8 +20467,11 @@ import {
|
|
|
20467
20467
|
mkdirSync as mkdirSync2,
|
|
20468
20468
|
openSync,
|
|
20469
20469
|
readdirSync as readdirSync2,
|
|
20470
|
+
readlinkSync,
|
|
20470
20471
|
realpathSync as realpathSync2,
|
|
20471
|
-
|
|
20472
|
+
renameSync as renameSync2,
|
|
20473
|
+
rmdirSync,
|
|
20474
|
+
unlinkSync
|
|
20472
20475
|
} from "fs";
|
|
20473
20476
|
function lstat(path) {
|
|
20474
20477
|
try {
|
|
@@ -20503,6 +20506,33 @@ function rmdir(path) {
|
|
|
20503
20506
|
throw error;
|
|
20504
20507
|
}
|
|
20505
20508
|
}
|
|
20509
|
+
function readlink(path) {
|
|
20510
|
+
try {
|
|
20511
|
+
return readlinkSync(path);
|
|
20512
|
+
} catch {
|
|
20513
|
+
return null;
|
|
20514
|
+
}
|
|
20515
|
+
}
|
|
20516
|
+
function unlink(path) {
|
|
20517
|
+
try {
|
|
20518
|
+
unlinkSync(path);
|
|
20519
|
+
return true;
|
|
20520
|
+
} catch (error) {
|
|
20521
|
+
if (error.code === "ENOENT")
|
|
20522
|
+
return false;
|
|
20523
|
+
throw error;
|
|
20524
|
+
}
|
|
20525
|
+
}
|
|
20526
|
+
function rename(from, to) {
|
|
20527
|
+
try {
|
|
20528
|
+
renameSync2(from, to);
|
|
20529
|
+
return true;
|
|
20530
|
+
} catch (error) {
|
|
20531
|
+
if (error.code === "ENOENT")
|
|
20532
|
+
return false;
|
|
20533
|
+
throw error;
|
|
20534
|
+
}
|
|
20535
|
+
}
|
|
20506
20536
|
function chmod(path, mode) {
|
|
20507
20537
|
chmodSync(path, mode);
|
|
20508
20538
|
}
|
|
@@ -20514,14 +20544,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join2(homedir2(), ".config", "kk", "browser-t
|
|
|
20514
20544
|
|
|
20515
20545
|
// ../../packages/publish-harness/src/profile-lease.ts
|
|
20516
20546
|
import { createHash, randomBytes } from "crypto";
|
|
20517
|
-
import { homedir as homedir3 } from "os";
|
|
20547
|
+
import { homedir as homedir3, hostname } from "os";
|
|
20518
20548
|
import { basename, dirname as dirname3, join as join3, resolve as resolve3 } from "path";
|
|
20519
20549
|
var DEFAULT_BOT_LEASE_ROOT = join3(homedir3(), ".config", "kk", "browser-leases");
|
|
20520
20550
|
var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
|
|
20521
20551
|
var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
|
|
20522
20552
|
var OWNER_FILE = "owner.json";
|
|
20523
20553
|
var HOLDER_PREFIX = "holder-";
|
|
20524
|
-
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
|
|
20554
|
+
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
|
|
20525
20555
|
async function acquireProfileLease(profileDir, options = {}) {
|
|
20526
20556
|
const fileSystem = options.fileSystem ?? createNodeFileSystem();
|
|
20527
20557
|
const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
|
|
@@ -20689,6 +20719,8 @@ function isProcessAlive(pid) {
|
|
|
20689
20719
|
}
|
|
20690
20720
|
}
|
|
20691
20721
|
function refuseNativeLock(profileDir) {
|
|
20722
|
+
if (reclaimStaleNativeLock(profileDir))
|
|
20723
|
+
return;
|
|
20692
20724
|
for (const name of NATIVE_LOCK_FILES) {
|
|
20693
20725
|
const path = join3(profileDir, name);
|
|
20694
20726
|
if (lstat(path) !== null) {
|
|
@@ -20696,6 +20728,24 @@ function refuseNativeLock(profileDir) {
|
|
|
20696
20728
|
}
|
|
20697
20729
|
}
|
|
20698
20730
|
}
|
|
20731
|
+
function reclaimStaleNativeLock(profileDir) {
|
|
20732
|
+
const lockPath = join3(profileDir, "SingletonLock");
|
|
20733
|
+
const target = readlink(lockPath);
|
|
20734
|
+
if (target === null)
|
|
20735
|
+
return false;
|
|
20736
|
+
const match = /^(.*)-(\d+)$/.exec(target);
|
|
20737
|
+
if (match === null)
|
|
20738
|
+
return false;
|
|
20739
|
+
const [, host, pidText] = match;
|
|
20740
|
+
if (host !== hostname())
|
|
20741
|
+
return false;
|
|
20742
|
+
const pid = Number(pidText);
|
|
20743
|
+
if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
|
|
20744
|
+
return false;
|
|
20745
|
+
for (const name of NATIVE_LOCK_FILES)
|
|
20746
|
+
unlink(join3(profileDir, name));
|
|
20747
|
+
return true;
|
|
20748
|
+
}
|
|
20699
20749
|
function sha256Hex(value) {
|
|
20700
20750
|
return createHash("sha256").update(value).digest("hex");
|
|
20701
20751
|
}
|
|
@@ -20732,6 +20782,27 @@ var DEFAULT_CHECK_TIMEOUT_MS = 30000;
|
|
|
20732
20782
|
var DEFAULT_STEP_TIMEOUT_MS = 30000;
|
|
20733
20783
|
var DEFAULT_POLL_MS = 500;
|
|
20734
20784
|
var ATTACH_POLL_MS = 100;
|
|
20785
|
+
function isInstantBrowserDeath(message) {
|
|
20786
|
+
return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
|
|
20787
|
+
}
|
|
20788
|
+
function classifyLaunchError(error, profileDir, channel) {
|
|
20789
|
+
const message = errorMessage2(error);
|
|
20790
|
+
if (/singleton|already in use|in use by another/i.test(message))
|
|
20791
|
+
return new BrowserProfileBusyError(profileDir, error);
|
|
20792
|
+
if (/playwright install|executable doesn't exist/i.test(message)) {
|
|
20793
|
+
return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
|
|
20794
|
+
}
|
|
20795
|
+
if (isInstantBrowserDeath(message)) {
|
|
20796
|
+
return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
|
|
20797
|
+
}
|
|
20798
|
+
return error instanceof Error ? error : new Error(message);
|
|
20799
|
+
}
|
|
20800
|
+
function quarantineProfileSyncData(profileDir) {
|
|
20801
|
+
const syncData = join5(profileDir, "Default", "Sync Data");
|
|
20802
|
+
if (lstat(syncData) === null)
|
|
20803
|
+
return false;
|
|
20804
|
+
return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
|
|
20805
|
+
}
|
|
20735
20806
|
function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
|
|
20736
20807
|
return {
|
|
20737
20808
|
async launch(request) {
|
|
@@ -20760,23 +20831,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
|
|
|
20760
20831
|
throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
|
|
20761
20832
|
}
|
|
20762
20833
|
const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
|
|
20834
|
+
const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
|
|
20835
|
+
headless,
|
|
20836
|
+
channel,
|
|
20837
|
+
args: ["--disable-blink-features=AutomationControlled"],
|
|
20838
|
+
ignoreDefaultArgs: ["--enable-automation"]
|
|
20839
|
+
});
|
|
20763
20840
|
let context;
|
|
20764
20841
|
try {
|
|
20765
|
-
context = await
|
|
20766
|
-
headless,
|
|
20767
|
-
channel,
|
|
20768
|
-
args: ["--disable-blink-features=AutomationControlled"],
|
|
20769
|
-
ignoreDefaultArgs: ["--enable-automation"]
|
|
20770
|
-
});
|
|
20842
|
+
context = await launch();
|
|
20771
20843
|
} catch (error) {
|
|
20772
|
-
|
|
20773
|
-
|
|
20774
|
-
|
|
20775
|
-
|
|
20776
|
-
|
|
20777
|
-
|
|
20844
|
+
if (!isInstantBrowserDeath(errorMessage2(error)))
|
|
20845
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20846
|
+
if (!quarantineProfileSyncData(profileDir))
|
|
20847
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20848
|
+
try {
|
|
20849
|
+
context = await launch();
|
|
20850
|
+
} catch (retryError) {
|
|
20851
|
+
throw classifyLaunchError(retryError, profileDir, channel);
|
|
20778
20852
|
}
|
|
20779
|
-
throw error;
|
|
20780
20853
|
}
|
|
20781
20854
|
try {
|
|
20782
20855
|
await context.addInitScript(() => {
|