@gobing-ai/knowledge-kit 0.1.17 → 0.1.20

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.
Files changed (95) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/dist/board/daily-voice/index.js +40 -7
  3. package/dist/index.js +8428 -4448
  4. package/package.json +1 -1
  5. package/plugins/generations/image-gen/README.md +31 -31
  6. package/plugins/generations/image-gen/assets/sample/provenance.json +314 -0
  7. package/plugins/generations/image-gen/config.example.yaml +3 -0
  8. package/plugins/generations/image-gen/dist/index.js +154 -14
  9. package/plugins/generations/image-gen/presets/SOURCE.md +65 -0
  10. package/plugins/generations/image-gen/scripts/eval-style-presets.ts +513 -0
  11. package/plugins/generations/image-gen/scripts/gen-samples.ts +57 -7
  12. package/plugins/generations/image-gen/src/presets.ts +6 -5
  13. package/plugins/generations/image-gen/src/providers/h3-local.ts +196 -0
  14. package/plugins/generations/image-gen/src/providers/types.ts +11 -1
  15. package/plugins/kk/config.example.yaml +7 -2
  16. package/plugins/kk/decisions/extract/ai-news.yaml +5 -2
  17. package/plugins/kk/plugin.json +1 -1
  18. package/plugins/kk/publish-presets/chinese-article.json +8 -2
  19. package/plugins/kk/publish-presets/daily-ai-voice-migration.json +1 -1
  20. package/plugins/kk/publish-presets/daily-ai-voice.json +57 -16
  21. package/plugins/kk/publish-presets/itc-main.json +36 -9
  22. package/plugins/kk/publish-presets/technical-article.json +8 -2
  23. package/plugins/kk/schedules/kk-daily-ai-voice3.cron.tmpl +8 -9
  24. package/plugins/kk/schedules/kk-daily-publish3-cn-peak.cron.tmpl +13 -19
  25. package/plugins/kk/schedules/kk-daily-publish3-overseas.cron.tmpl +11 -9
  26. package/plugins/kk/scripts/daily-stages.ts +238 -14
  27. package/plugins/kk/scripts/daily-triage-core.ts +43 -25
  28. package/plugins/kk/scripts/daily-triage.ts +1 -1
  29. package/plugins/kk/skills/image-authoring/SKILL.md +7 -3
  30. package/plugins/kk/skills/image-authoring/references/providers.md +27 -0
  31. package/plugins/kk/skills/image-authoring/references/style-extraction.md +1 -1
  32. package/plugins/kk/skills/publish/SKILL.md +15 -2
  33. package/plugins/kk/skills/web-automation/SKILL.md +31 -1
  34. package/plugins/kk/workflows/kk-article-generate.yaml +485 -0
  35. package/plugins/kk/workflows/kk-collect.yaml +8 -4
  36. package/plugins/kk/workflows/kk-daily-ai-voice.yaml +2 -1
  37. package/plugins/kk/workflows/kk-daily-ai-voice2.yaml +4 -2
  38. package/plugins/kk/workflows/kk-daily-safety.yaml +5 -7
  39. package/plugins/kk/workflows/kk-news-generate.yaml +410 -0
  40. package/plugins/kk/workflows/kk-publish.yaml +236 -0
  41. package/plugins/publishings/bilibili-pub/dist/profile-check.js +49 -10
  42. package/plugins/publishings/cnblogs-pub/dist/profile-check.js +60 -10
  43. package/plugins/publishings/csdn-pub/dist/profile-check.js +60 -10
  44. package/plugins/publishings/infoq-pub/dist/profile-check.js +60 -10
  45. package/plugins/publishings/jike-pub/dist/profile-check.js +60 -10
  46. package/plugins/publishings/juejin-pub/dist/profile-check.js +60 -10
  47. package/plugins/publishings/linkedin-pub/dist/index.js +43 -6
  48. package/plugins/publishings/linkedin-pub/dist/profile-check.js +92 -15
  49. package/plugins/publishings/linkedin-pub/src/index.ts +52 -6
  50. package/plugins/publishings/shipinhao-pub/dist/index.js +391 -32
  51. package/plugins/publishings/shipinhao-pub/dist/profile-check.js +52 -11
  52. package/plugins/publishings/shipinhao-pub/plugin.json +1 -1
  53. package/plugins/publishings/shipinhao-pub/src/audio-flow.ts +393 -0
  54. package/plugins/publishings/shipinhao-pub/src/flow.ts +243 -14
  55. package/plugins/publishings/shipinhao-pub/src/selectors.ts +13 -0
  56. package/plugins/publishings/sspai-pub/dist/profile-check.js +60 -10
  57. package/plugins/publishings/substack-pub/dist/profile-check.js +60 -10
  58. package/plugins/publishings/surfdash-pub/dist/index.js +50 -8
  59. package/plugins/publishings/surfdash-pub/src/index.ts +59 -8
  60. package/plugins/publishings/surfdash-pub/src/topic-claims.ts +34 -3
  61. package/plugins/publishings/threads-pub/dist/index.js +9196 -296
  62. package/plugins/publishings/threads-pub/dist/profile-check.js +21139 -0
  63. package/plugins/publishings/threads-pub/package.json +3 -1
  64. package/plugins/publishings/threads-pub/plugin.json +6 -1
  65. package/plugins/publishings/threads-pub/src/index.ts +8 -1
  66. package/plugins/publishings/threads-pub/src/post-rail.ts +357 -0
  67. package/plugins/publishings/threads-pub/src/profile-check.ts +17 -0
  68. package/plugins/publishings/wechatmp-pub/dist/index.js +454 -81
  69. package/plugins/publishings/wechatmp-pub/dist/profile-check.js +401 -102
  70. package/plugins/publishings/wechatmp-pub/src/flow.ts +42 -4
  71. package/plugins/publishings/wechatmp-pub/src/index.ts +142 -102
  72. package/plugins/publishings/wechatmp-pub/src/profile-check.ts +36 -58
  73. package/plugins/publishings/weibo-pub/dist/index.js +136 -22
  74. package/plugins/publishings/weibo-pub/dist/profile-check.js +61 -11
  75. package/plugins/publishings/weibo-pub/src/index.ts +208 -22
  76. package/plugins/publishings/x-pub/dist/index.js +263 -43
  77. package/plugins/publishings/x-pub/dist/profile-check.js +63 -11
  78. package/plugins/publishings/x-pub/src/index.ts +15 -1
  79. package/plugins/publishings/x-pub/src/post-rail.ts +334 -0
  80. package/plugins/publishings/xhs-pub/dist/index.js +193 -201
  81. package/plugins/publishings/xhs-pub/dist/profile-check.js +380 -85
  82. package/plugins/publishings/xhs-pub/src/automation-guard.ts +2 -3
  83. package/plugins/publishings/xhs-pub/src/flow.ts +177 -205
  84. package/plugins/publishings/xhs-pub/src/index.ts +65 -59
  85. package/plugins/publishings/xhs-pub/src/modes.ts +1 -1
  86. package/plugins/publishings/xhs-pub/src/page.ts +2 -2
  87. package/plugins/publishings/xhs-pub/src/profile-check.ts +86 -150
  88. package/plugins/publishings/xiaoyuzhou-pub/dist/profile-check.js +60 -10
  89. package/plugins/kk/scripts/daily-scheduled-run.ts +0 -581
  90. package/plugins/kk/scripts/voice3-publish-window.ts +0 -206
  91. package/plugins/kk/workflows/kk-daily-ai-voice3.yaml +0 -903
  92. package/plugins/kk/workflows/kk-daily-publish3.yaml +0 -353
  93. package/plugins/kk/workflows/kk-itc-author.yaml +0 -484
  94. package/plugins/kk/workflows/kk-itc-publish-tail.yaml +0 -112
  95. package/plugins/kk/workflows/kk-itc.yaml +0 -291
@@ -20355,6 +20355,16 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20355
20355
  return 1;
20356
20356
  }
20357
20357
  const page = context.pages()[0] ?? await context.newPage();
20358
+ const onOpen = config.sessionState?.onOpen;
20359
+ if (onOpen !== undefined) {
20360
+ try {
20361
+ await onOpen({ context, page, profileDir: request.profileDir });
20362
+ } catch (error) {
20363
+ await context.close().catch(() => {});
20364
+ stdout(`${config.channel} profile open failed: ${errorMessage2(error)}`);
20365
+ return 1;
20366
+ }
20367
+ }
20358
20368
  try {
20359
20369
  await page.goto(request.url, { waitUntil: "commit", timeout: DEFAULT_OPEN_NAV_TIMEOUT_MS });
20360
20370
  } catch (error) {
@@ -20366,6 +20376,36 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20366
20376
  stdout(`${config.channel} profile open closed: ${request.profileDir}`);
20367
20377
  return 0;
20368
20378
  }
20379
+ async function loginWithAuthenticatedPage(config, opts) {
20380
+ const context = await opts.launcher.launch({ profileDir: opts.profileDir, headless: false });
20381
+ const sessionCaptured = config.sessionState?.onAuthenticated !== undefined;
20382
+ let captured = false;
20383
+ let facts = {};
20384
+ try {
20385
+ const page = context.pages()[0] ?? await context.newPage();
20386
+ await page.goto(opts.url);
20387
+ if (!await waitUntil(() => Promise.resolve(config.isAuthenticated(page)), opts.timeoutMs)) {
20388
+ throw new Error(`Authentication was not confirmed within ${opts.timeoutMs}ms \u2014 the profile's site session has likely ` + "expired or the sign-in was not completed: run the login command again to sign in again.");
20389
+ }
20390
+ await config.sessionState?.onAuthenticated?.({ context, page, profileDir: opts.profileDir });
20391
+ captured = sessionCaptured;
20392
+ if (opts.captureFacts) {
20393
+ facts = {
20394
+ ...await captureSessionFacts(context, config.homeUrl),
20395
+ ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20396
+ };
20397
+ }
20398
+ } finally {
20399
+ try {
20400
+ await context.close();
20401
+ } catch {
20402
+ if (captured) {
20403
+ echoError(`${config.channel} browser context close failed after the authenticated session capture; saved state preserved`);
20404
+ }
20405
+ }
20406
+ }
20407
+ return facts;
20408
+ }
20369
20409
  async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20370
20410
  const stdout = deps.stdout ?? ((line) => {
20371
20411
  echo(line);
@@ -20416,7 +20456,8 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20416
20456
  const launcher = deps.launcher ?? kkBrowserLauncher;
20417
20457
  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`);
20418
20458
  try {
20419
- if (url === undefined) {
20459
+ const captureFacts = url !== undefined;
20460
+ if (!captureFacts && config.sessionState?.onAuthenticated === undefined) {
20420
20461
  await loginWithProfile({
20421
20462
  profileDir,
20422
20463
  url: config.homeUrl,
@@ -20427,18 +20468,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20427
20468
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20428
20469
  return 0;
20429
20470
  }
20430
- const facts = await withProfilePage({
20471
+ const facts = await loginWithAuthenticatedPage(config, {
20431
20472
  profileDir,
20432
- headless: false,
20433
- url,
20434
- isAuthenticated: config.isAuthenticated,
20473
+ url: url ?? config.homeUrl,
20435
20474
  timeoutMs: loginTimeoutMs,
20436
- launcher
20437
- }, async ({ context, page }) => ({
20438
- ...await captureSessionFacts(context, config.homeUrl),
20439
- ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20440
- }));
20475
+ launcher,
20476
+ captureFacts
20477
+ });
20441
20478
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20479
+ if (url === undefined)
20480
+ return 0;
20442
20481
  stdout(JSON.stringify({
20443
20482
  status: "verified",
20444
20483
  profileDir,
@@ -20210,6 +20210,7 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
20210
20210
  var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
20211
20211
  var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
20212
20212
  var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20213
+ var DEFAULT_POLL_MS = 500;
20213
20214
  function isInstantBrowserDeath(message) {
20214
20215
  return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20215
20216
  }
@@ -20375,6 +20376,16 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20375
20376
  return 1;
20376
20377
  }
20377
20378
  const page = context.pages()[0] ?? await context.newPage();
20379
+ const onOpen = config.sessionState?.onOpen;
20380
+ if (onOpen !== undefined) {
20381
+ try {
20382
+ await onOpen({ context, page, profileDir: request.profileDir });
20383
+ } catch (error) {
20384
+ await context.close().catch(() => {});
20385
+ stdout(`${config.channel} profile open failed: ${errorMessage2(error)}`);
20386
+ return 1;
20387
+ }
20388
+ }
20378
20389
  try {
20379
20390
  await page.goto(request.url, { waitUntil: "commit", timeout: DEFAULT_OPEN_NAV_TIMEOUT_MS });
20380
20391
  } catch (error) {
@@ -20386,6 +20397,36 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20386
20397
  stdout(`${config.channel} profile open closed: ${request.profileDir}`);
20387
20398
  return 0;
20388
20399
  }
20400
+ async function loginWithAuthenticatedPage(config, opts) {
20401
+ const context = await opts.launcher.launch({ profileDir: opts.profileDir, headless: false });
20402
+ const sessionCaptured = config.sessionState?.onAuthenticated !== undefined;
20403
+ let captured = false;
20404
+ let facts = {};
20405
+ try {
20406
+ const page = context.pages()[0] ?? await context.newPage();
20407
+ await page.goto(opts.url);
20408
+ if (!await waitUntil(() => Promise.resolve(config.isAuthenticated(page)), opts.timeoutMs)) {
20409
+ throw new Error(`Authentication was not confirmed within ${opts.timeoutMs}ms \u2014 the profile's site session has likely ` + "expired or the sign-in was not completed: run the login command again to sign in again.");
20410
+ }
20411
+ await config.sessionState?.onAuthenticated?.({ context, page, profileDir: opts.profileDir });
20412
+ captured = sessionCaptured;
20413
+ if (opts.captureFacts) {
20414
+ facts = {
20415
+ ...await captureSessionFacts(context, config.homeUrl),
20416
+ ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20417
+ };
20418
+ }
20419
+ } finally {
20420
+ try {
20421
+ await context.close();
20422
+ } catch {
20423
+ if (captured) {
20424
+ echoError(`${config.channel} browser context close failed after the authenticated session capture; saved state preserved`);
20425
+ }
20426
+ }
20427
+ }
20428
+ return facts;
20429
+ }
20389
20430
  async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20390
20431
  const stdout = deps.stdout ?? ((line) => {
20391
20432
  echo(line);
@@ -20436,7 +20477,8 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20436
20477
  const launcher = deps.launcher ?? kkBrowserLauncher;
20437
20478
  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`);
20438
20479
  try {
20439
- if (url === undefined) {
20480
+ const captureFacts = url !== undefined;
20481
+ if (!captureFacts && config.sessionState?.onAuthenticated === undefined) {
20440
20482
  await loginWithProfile({
20441
20483
  profileDir,
20442
20484
  url: config.homeUrl,
@@ -20447,18 +20489,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20447
20489
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20448
20490
  return 0;
20449
20491
  }
20450
- const facts = await withProfilePage({
20492
+ const facts = await loginWithAuthenticatedPage(config, {
20451
20493
  profileDir,
20452
- headless: false,
20453
- url,
20454
- isAuthenticated: config.isAuthenticated,
20494
+ url: url ?? config.homeUrl,
20455
20495
  timeoutMs: loginTimeoutMs,
20456
- launcher
20457
- }, async ({ context, page }) => ({
20458
- ...await captureSessionFacts(context, config.homeUrl),
20459
- ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20460
- }));
20496
+ launcher,
20497
+ captureFacts
20498
+ });
20461
20499
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20500
+ if (url === undefined)
20501
+ return 0;
20462
20502
  stdout(JSON.stringify({
20463
20503
  status: "verified",
20464
20504
  profileDir,
@@ -20481,6 +20521,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20481
20521
  stdout(JSON.stringify(result));
20482
20522
  return result.status === "verified" ? 0 : 1;
20483
20523
  }
20524
+ async function waitUntil(predicate, timeoutMs, pollMs = DEFAULT_POLL_MS) {
20525
+ const deadline = Date.now() + timeoutMs;
20526
+ while (Date.now() < deadline) {
20527
+ if (await predicate()) {
20528
+ return true;
20529
+ }
20530
+ await new Promise((resolve) => setTimeout(resolve, pollMs));
20531
+ }
20532
+ return predicate();
20533
+ }
20484
20534
  async function captureSessionFacts(context, url) {
20485
20535
  let cookies;
20486
20536
  try {
@@ -20482,6 +20482,7 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
20482
20482
  var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
20483
20483
  var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
20484
20484
  var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20485
+ var DEFAULT_POLL_MS = 500;
20485
20486
  function isInstantBrowserDeath(message) {
20486
20487
  return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20487
20488
  }
@@ -20647,6 +20648,16 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20647
20648
  return 1;
20648
20649
  }
20649
20650
  const page = context.pages()[0] ?? await context.newPage();
20651
+ const onOpen = config.sessionState?.onOpen;
20652
+ if (onOpen !== undefined) {
20653
+ try {
20654
+ await onOpen({ context, page, profileDir: request.profileDir });
20655
+ } catch (error) {
20656
+ await context.close().catch(() => {});
20657
+ stdout(`${config.channel} profile open failed: ${errorMessage2(error)}`);
20658
+ return 1;
20659
+ }
20660
+ }
20650
20661
  try {
20651
20662
  await page.goto(request.url, { waitUntil: "commit", timeout: DEFAULT_OPEN_NAV_TIMEOUT_MS });
20652
20663
  } catch (error) {
@@ -20658,6 +20669,36 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20658
20669
  stdout(`${config.channel} profile open closed: ${request.profileDir}`);
20659
20670
  return 0;
20660
20671
  }
20672
+ async function loginWithAuthenticatedPage(config, opts) {
20673
+ const context = await opts.launcher.launch({ profileDir: opts.profileDir, headless: false });
20674
+ const sessionCaptured = config.sessionState?.onAuthenticated !== undefined;
20675
+ let captured = false;
20676
+ let facts = {};
20677
+ try {
20678
+ const page = context.pages()[0] ?? await context.newPage();
20679
+ await page.goto(opts.url);
20680
+ if (!await waitUntil(() => Promise.resolve(config.isAuthenticated(page)), opts.timeoutMs)) {
20681
+ throw new Error(`Authentication was not confirmed within ${opts.timeoutMs}ms \u2014 the profile's site session has likely ` + "expired or the sign-in was not completed: run the login command again to sign in again.");
20682
+ }
20683
+ await config.sessionState?.onAuthenticated?.({ context, page, profileDir: opts.profileDir });
20684
+ captured = sessionCaptured;
20685
+ if (opts.captureFacts) {
20686
+ facts = {
20687
+ ...await captureSessionFacts(context, config.homeUrl),
20688
+ ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20689
+ };
20690
+ }
20691
+ } finally {
20692
+ try {
20693
+ await context.close();
20694
+ } catch {
20695
+ if (captured) {
20696
+ echoError(`${config.channel} browser context close failed after the authenticated session capture; saved state preserved`);
20697
+ }
20698
+ }
20699
+ }
20700
+ return facts;
20701
+ }
20661
20702
  async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20662
20703
  const stdout = deps.stdout ?? ((line) => {
20663
20704
  echo(line);
@@ -20708,7 +20749,8 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20708
20749
  const launcher = deps.launcher ?? kkBrowserLauncher;
20709
20750
  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`);
20710
20751
  try {
20711
- if (url === undefined) {
20752
+ const captureFacts = url !== undefined;
20753
+ if (!captureFacts && config.sessionState?.onAuthenticated === undefined) {
20712
20754
  await loginWithProfile({
20713
20755
  profileDir,
20714
20756
  url: config.homeUrl,
@@ -20719,18 +20761,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20719
20761
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20720
20762
  return 0;
20721
20763
  }
20722
- const facts = await withProfilePage({
20764
+ const facts = await loginWithAuthenticatedPage(config, {
20723
20765
  profileDir,
20724
- headless: false,
20725
- url,
20726
- isAuthenticated: config.isAuthenticated,
20766
+ url: url ?? config.homeUrl,
20727
20767
  timeoutMs: loginTimeoutMs,
20728
- launcher
20729
- }, async ({ context, page }) => ({
20730
- ...await captureSessionFacts(context, config.homeUrl),
20731
- ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20732
- }));
20768
+ launcher,
20769
+ captureFacts
20770
+ });
20733
20771
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20772
+ if (url === undefined)
20773
+ return 0;
20734
20774
  stdout(JSON.stringify({
20735
20775
  status: "verified",
20736
20776
  profileDir,
@@ -20753,6 +20793,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20753
20793
  stdout(JSON.stringify(result));
20754
20794
  return result.status === "verified" ? 0 : 1;
20755
20795
  }
20796
+ async function waitUntil(predicate, timeoutMs, pollMs = DEFAULT_POLL_MS) {
20797
+ const deadline = Date.now() + timeoutMs;
20798
+ while (Date.now() < deadline) {
20799
+ if (await predicate()) {
20800
+ return true;
20801
+ }
20802
+ await new Promise((resolve) => setTimeout(resolve, pollMs));
20803
+ }
20804
+ return predicate();
20805
+ }
20756
20806
  async function captureSessionFacts(context, url) {
20757
20807
  let cookies;
20758
20808
  try {
@@ -20482,6 +20482,7 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
20482
20482
  var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
20483
20483
  var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
20484
20484
  var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20485
+ var DEFAULT_POLL_MS = 500;
20485
20486
  function isInstantBrowserDeath(message) {
20486
20487
  return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20487
20488
  }
@@ -20647,6 +20648,16 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20647
20648
  return 1;
20648
20649
  }
20649
20650
  const page = context.pages()[0] ?? await context.newPage();
20651
+ const onOpen = config.sessionState?.onOpen;
20652
+ if (onOpen !== undefined) {
20653
+ try {
20654
+ await onOpen({ context, page, profileDir: request.profileDir });
20655
+ } catch (error) {
20656
+ await context.close().catch(() => {});
20657
+ stdout(`${config.channel} profile open failed: ${errorMessage2(error)}`);
20658
+ return 1;
20659
+ }
20660
+ }
20650
20661
  try {
20651
20662
  await page.goto(request.url, { waitUntil: "commit", timeout: DEFAULT_OPEN_NAV_TIMEOUT_MS });
20652
20663
  } catch (error) {
@@ -20658,6 +20669,36 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20658
20669
  stdout(`${config.channel} profile open closed: ${request.profileDir}`);
20659
20670
  return 0;
20660
20671
  }
20672
+ async function loginWithAuthenticatedPage(config, opts) {
20673
+ const context = await opts.launcher.launch({ profileDir: opts.profileDir, headless: false });
20674
+ const sessionCaptured = config.sessionState?.onAuthenticated !== undefined;
20675
+ let captured = false;
20676
+ let facts = {};
20677
+ try {
20678
+ const page = context.pages()[0] ?? await context.newPage();
20679
+ await page.goto(opts.url);
20680
+ if (!await waitUntil(() => Promise.resolve(config.isAuthenticated(page)), opts.timeoutMs)) {
20681
+ throw new Error(`Authentication was not confirmed within ${opts.timeoutMs}ms \u2014 the profile's site session has likely ` + "expired or the sign-in was not completed: run the login command again to sign in again.");
20682
+ }
20683
+ await config.sessionState?.onAuthenticated?.({ context, page, profileDir: opts.profileDir });
20684
+ captured = sessionCaptured;
20685
+ if (opts.captureFacts) {
20686
+ facts = {
20687
+ ...await captureSessionFacts(context, config.homeUrl),
20688
+ ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20689
+ };
20690
+ }
20691
+ } finally {
20692
+ try {
20693
+ await context.close();
20694
+ } catch {
20695
+ if (captured) {
20696
+ echoError(`${config.channel} browser context close failed after the authenticated session capture; saved state preserved`);
20697
+ }
20698
+ }
20699
+ }
20700
+ return facts;
20701
+ }
20661
20702
  async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20662
20703
  const stdout = deps.stdout ?? ((line) => {
20663
20704
  echo(line);
@@ -20708,7 +20749,8 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20708
20749
  const launcher = deps.launcher ?? kkBrowserLauncher;
20709
20750
  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`);
20710
20751
  try {
20711
- if (url === undefined) {
20752
+ const captureFacts = url !== undefined;
20753
+ if (!captureFacts && config.sessionState?.onAuthenticated === undefined) {
20712
20754
  await loginWithProfile({
20713
20755
  profileDir,
20714
20756
  url: config.homeUrl,
@@ -20719,18 +20761,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20719
20761
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20720
20762
  return 0;
20721
20763
  }
20722
- const facts = await withProfilePage({
20764
+ const facts = await loginWithAuthenticatedPage(config, {
20723
20765
  profileDir,
20724
- headless: false,
20725
- url,
20726
- isAuthenticated: config.isAuthenticated,
20766
+ url: url ?? config.homeUrl,
20727
20767
  timeoutMs: loginTimeoutMs,
20728
- launcher
20729
- }, async ({ context, page }) => ({
20730
- ...await captureSessionFacts(context, config.homeUrl),
20731
- ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20732
- }));
20768
+ launcher,
20769
+ captureFacts
20770
+ });
20733
20771
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20772
+ if (url === undefined)
20773
+ return 0;
20734
20774
  stdout(JSON.stringify({
20735
20775
  status: "verified",
20736
20776
  profileDir,
@@ -20753,6 +20793,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20753
20793
  stdout(JSON.stringify(result));
20754
20794
  return result.status === "verified" ? 0 : 1;
20755
20795
  }
20796
+ async function waitUntil(predicate, timeoutMs, pollMs = DEFAULT_POLL_MS) {
20797
+ const deadline = Date.now() + timeoutMs;
20798
+ while (Date.now() < deadline) {
20799
+ if (await predicate()) {
20800
+ return true;
20801
+ }
20802
+ await new Promise((resolve) => setTimeout(resolve, pollMs));
20803
+ }
20804
+ return predicate();
20805
+ }
20756
20806
  var DEFAULT_SIGNAL_CONTAINERS = [
20757
20807
  '[class*="toast" i]',
20758
20808
  '[class*="notification" i]',
@@ -20482,6 +20482,7 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
20482
20482
  var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
20483
20483
  var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
20484
20484
  var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20485
+ var DEFAULT_POLL_MS = 500;
20485
20486
  function isInstantBrowserDeath(message) {
20486
20487
  return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20487
20488
  }
@@ -20647,6 +20648,16 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20647
20648
  return 1;
20648
20649
  }
20649
20650
  const page = context.pages()[0] ?? await context.newPage();
20651
+ const onOpen = config.sessionState?.onOpen;
20652
+ if (onOpen !== undefined) {
20653
+ try {
20654
+ await onOpen({ context, page, profileDir: request.profileDir });
20655
+ } catch (error) {
20656
+ await context.close().catch(() => {});
20657
+ stdout(`${config.channel} profile open failed: ${errorMessage2(error)}`);
20658
+ return 1;
20659
+ }
20660
+ }
20650
20661
  try {
20651
20662
  await page.goto(request.url, { waitUntil: "commit", timeout: DEFAULT_OPEN_NAV_TIMEOUT_MS });
20652
20663
  } catch (error) {
@@ -20658,6 +20669,36 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20658
20669
  stdout(`${config.channel} profile open closed: ${request.profileDir}`);
20659
20670
  return 0;
20660
20671
  }
20672
+ async function loginWithAuthenticatedPage(config, opts) {
20673
+ const context = await opts.launcher.launch({ profileDir: opts.profileDir, headless: false });
20674
+ const sessionCaptured = config.sessionState?.onAuthenticated !== undefined;
20675
+ let captured = false;
20676
+ let facts = {};
20677
+ try {
20678
+ const page = context.pages()[0] ?? await context.newPage();
20679
+ await page.goto(opts.url);
20680
+ if (!await waitUntil(() => Promise.resolve(config.isAuthenticated(page)), opts.timeoutMs)) {
20681
+ throw new Error(`Authentication was not confirmed within ${opts.timeoutMs}ms \u2014 the profile's site session has likely ` + "expired or the sign-in was not completed: run the login command again to sign in again.");
20682
+ }
20683
+ await config.sessionState?.onAuthenticated?.({ context, page, profileDir: opts.profileDir });
20684
+ captured = sessionCaptured;
20685
+ if (opts.captureFacts) {
20686
+ facts = {
20687
+ ...await captureSessionFacts(context, config.homeUrl),
20688
+ ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20689
+ };
20690
+ }
20691
+ } finally {
20692
+ try {
20693
+ await context.close();
20694
+ } catch {
20695
+ if (captured) {
20696
+ echoError(`${config.channel} browser context close failed after the authenticated session capture; saved state preserved`);
20697
+ }
20698
+ }
20699
+ }
20700
+ return facts;
20701
+ }
20661
20702
  async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20662
20703
  const stdout = deps.stdout ?? ((line) => {
20663
20704
  echo(line);
@@ -20708,7 +20749,8 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20708
20749
  const launcher = deps.launcher ?? kkBrowserLauncher;
20709
20750
  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`);
20710
20751
  try {
20711
- if (url === undefined) {
20752
+ const captureFacts = url !== undefined;
20753
+ if (!captureFacts && config.sessionState?.onAuthenticated === undefined) {
20712
20754
  await loginWithProfile({
20713
20755
  profileDir,
20714
20756
  url: config.homeUrl,
@@ -20719,18 +20761,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20719
20761
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20720
20762
  return 0;
20721
20763
  }
20722
- const facts = await withProfilePage({
20764
+ const facts = await loginWithAuthenticatedPage(config, {
20723
20765
  profileDir,
20724
- headless: false,
20725
- url,
20726
- isAuthenticated: config.isAuthenticated,
20766
+ url: url ?? config.homeUrl,
20727
20767
  timeoutMs: loginTimeoutMs,
20728
- launcher
20729
- }, async ({ context, page }) => ({
20730
- ...await captureSessionFacts(context, config.homeUrl),
20731
- ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20732
- }));
20768
+ launcher,
20769
+ captureFacts
20770
+ });
20733
20771
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20772
+ if (url === undefined)
20773
+ return 0;
20734
20774
  stdout(JSON.stringify({
20735
20775
  status: "verified",
20736
20776
  profileDir,
@@ -20753,6 +20793,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20753
20793
  stdout(JSON.stringify(result));
20754
20794
  return result.status === "verified" ? 0 : 1;
20755
20795
  }
20796
+ async function waitUntil(predicate, timeoutMs, pollMs = DEFAULT_POLL_MS) {
20797
+ const deadline = Date.now() + timeoutMs;
20798
+ while (Date.now() < deadline) {
20799
+ if (await predicate()) {
20800
+ return true;
20801
+ }
20802
+ await new Promise((resolve) => setTimeout(resolve, pollMs));
20803
+ }
20804
+ return predicate();
20805
+ }
20756
20806
  async function captureSessionFacts(context, url) {
20757
20807
  let cookies;
20758
20808
  try {
@@ -20482,6 +20482,7 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
20482
20482
  var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
20483
20483
  var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
20484
20484
  var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20485
+ var DEFAULT_POLL_MS = 500;
20485
20486
  function isInstantBrowserDeath(message) {
20486
20487
  return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20487
20488
  }
@@ -20647,6 +20648,16 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20647
20648
  return 1;
20648
20649
  }
20649
20650
  const page = context.pages()[0] ?? await context.newPage();
20651
+ const onOpen = config.sessionState?.onOpen;
20652
+ if (onOpen !== undefined) {
20653
+ try {
20654
+ await onOpen({ context, page, profileDir: request.profileDir });
20655
+ } catch (error) {
20656
+ await context.close().catch(() => {});
20657
+ stdout(`${config.channel} profile open failed: ${errorMessage2(error)}`);
20658
+ return 1;
20659
+ }
20660
+ }
20650
20661
  try {
20651
20662
  await page.goto(request.url, { waitUntil: "commit", timeout: DEFAULT_OPEN_NAV_TIMEOUT_MS });
20652
20663
  } catch (error) {
@@ -20658,6 +20669,36 @@ async function openChannelSession(config, request, deps = {}, stdout = echo) {
20658
20669
  stdout(`${config.channel} profile open closed: ${request.profileDir}`);
20659
20670
  return 0;
20660
20671
  }
20672
+ async function loginWithAuthenticatedPage(config, opts) {
20673
+ const context = await opts.launcher.launch({ profileDir: opts.profileDir, headless: false });
20674
+ const sessionCaptured = config.sessionState?.onAuthenticated !== undefined;
20675
+ let captured = false;
20676
+ let facts = {};
20677
+ try {
20678
+ const page = context.pages()[0] ?? await context.newPage();
20679
+ await page.goto(opts.url);
20680
+ if (!await waitUntil(() => Promise.resolve(config.isAuthenticated(page)), opts.timeoutMs)) {
20681
+ throw new Error(`Authentication was not confirmed within ${opts.timeoutMs}ms \u2014 the profile's site session has likely ` + "expired or the sign-in was not completed: run the login command again to sign in again.");
20682
+ }
20683
+ await config.sessionState?.onAuthenticated?.({ context, page, profileDir: opts.profileDir });
20684
+ captured = sessionCaptured;
20685
+ if (opts.captureFacts) {
20686
+ facts = {
20687
+ ...await captureSessionFacts(context, config.homeUrl),
20688
+ ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20689
+ };
20690
+ }
20691
+ } finally {
20692
+ try {
20693
+ await context.close();
20694
+ } catch {
20695
+ if (captured) {
20696
+ echoError(`${config.channel} browser context close failed after the authenticated session capture; saved state preserved`);
20697
+ }
20698
+ }
20699
+ }
20700
+ return facts;
20701
+ }
20661
20702
  async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20662
20703
  const stdout = deps.stdout ?? ((line) => {
20663
20704
  echo(line);
@@ -20708,7 +20749,8 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20708
20749
  const launcher = deps.launcher ?? kkBrowserLauncher;
20709
20750
  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`);
20710
20751
  try {
20711
- if (url === undefined) {
20752
+ const captureFacts = url !== undefined;
20753
+ if (!captureFacts && config.sessionState?.onAuthenticated === undefined) {
20712
20754
  await loginWithProfile({
20713
20755
  profileDir,
20714
20756
  url: config.homeUrl,
@@ -20719,18 +20761,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20719
20761
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20720
20762
  return 0;
20721
20763
  }
20722
- const facts = await withProfilePage({
20764
+ const facts = await loginWithAuthenticatedPage(config, {
20723
20765
  profileDir,
20724
- headless: false,
20725
- url,
20726
- isAuthenticated: config.isAuthenticated,
20766
+ url: url ?? config.homeUrl,
20727
20767
  timeoutMs: loginTimeoutMs,
20728
- launcher
20729
- }, async ({ context, page }) => ({
20730
- ...await captureSessionFacts(context, config.homeUrl),
20731
- ...config.readIdentity === undefined ? {} : { identity: await readIdentityOrUndefined(config.readIdentity, page) }
20732
- }));
20768
+ launcher,
20769
+ captureFacts
20770
+ });
20733
20771
  stdout(`${config.channel} profile login verified: ${profileDir}`);
20772
+ if (url === undefined)
20773
+ return 0;
20734
20774
  stdout(JSON.stringify({
20735
20775
  status: "verified",
20736
20776
  profileDir,
@@ -20753,6 +20793,16 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20753
20793
  stdout(JSON.stringify(result));
20754
20794
  return result.status === "verified" ? 0 : 1;
20755
20795
  }
20796
+ async function waitUntil(predicate, timeoutMs, pollMs = DEFAULT_POLL_MS) {
20797
+ const deadline = Date.now() + timeoutMs;
20798
+ while (Date.now() < deadline) {
20799
+ if (await predicate()) {
20800
+ return true;
20801
+ }
20802
+ await new Promise((resolve) => setTimeout(resolve, pollMs));
20803
+ }
20804
+ return predicate();
20805
+ }
20756
20806
  async function captureSessionFacts(context, url) {
20757
20807
  let cookies;
20758
20808
  try {