@ysgao/web-cli 0.3.0 → 0.3.2

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 (2) hide show
  1. package/dist/index.js +59 -14
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -51271,6 +51271,13 @@ async function loadCredentials(site, deps = {}) {
51271
51271
  const { username, password } = candidates[0];
51272
51272
  return { username, password };
51273
51273
  }
51274
+ async function hasStoredCredentials(site, deps = {}) {
51275
+ try {
51276
+ return await loadCredentials(site, deps) !== void 0;
51277
+ } catch {
51278
+ return false;
51279
+ }
51280
+ }
51274
51281
  async function clearCredentials(site, deps = {}) {
51275
51282
  const keychain = resolveKeychain(deps);
51276
51283
  if (keychain) {
@@ -51482,7 +51489,7 @@ async function getLoginPageCookies(session, loginUrl) {
51482
51489
  }
51483
51490
  return session.getCookies(Array.from(urls));
51484
51491
  }
51485
- async function attemptSilentRefresh(opts, deps) {
51492
+ async function attemptSilentRefresh(opts, deps, credentials) {
51486
51493
  const withChrome = deps.withChrome ?? withAutomationChrome;
51487
51494
  const connect = deps.connect ?? connectCdpSession;
51488
51495
  const now = deps.now ?? (() => Date.now());
@@ -51495,7 +51502,6 @@ async function attemptSilentRefresh(opts, deps) {
51495
51502
  await session.navigate(opts.loginUrl);
51496
51503
  } catch {
51497
51504
  }
51498
- const credentials = await loadSavedCredentials(opts.site, deps);
51499
51505
  await attemptAutoFill(opts.site, session, deps, credentials);
51500
51506
  const silentTimeoutMs = deps.silentRefreshTimeoutMs ?? (credentials ? SILENT_REFRESH_TIMEOUT_MS : SILENT_REFRESH_NO_CREDENTIAL_TIMEOUT_MS);
51501
51507
  const deadline = now() + silentTimeoutMs;
@@ -51564,7 +51570,13 @@ async function runVisibleLogin(opts, deps) {
51564
51570
  }
51565
51571
  });
51566
51572
  }
51567
- function noVisibleWindowError(site) {
51573
+ function noVisibleWindowError(site, hasCredential) {
51574
+ if (hasCredential) {
51575
+ return new CliError(
51576
+ "SESSION_EXPIRED",
51577
+ `No valid session for "${site}" \u2014 a credential IS already saved for this site (from an earlier "web-cli login ${site} --save-credentials"), and the silent refresh already tried it, but a working session still could not be re-established this run. Re-running "--save-credentials" again will not help unless the saved password is actually out of date \u2014 a likely cause here is something the automatic form-fill can't get past on its own (a CAPTCHA, a 2FA prompt, or a changed login page), or a transient site issue. A visible sign-in window is not allowed for this run (pass --allow-visible-login to permit one and see, in person, what is actually blocking it), or run "web-cli login ${site}" directly to sign in by hand right now.`
51578
+ );
51579
+ }
51568
51580
  return new CliError(
51569
51581
  "SESSION_EXPIRED",
51570
51582
  `No valid session for "${site}" \u2014 the silent, credential-based refresh did not recover it, and a visible sign-in window is not allowed for this run (pass --allow-visible-login to permit one). Run "web-cli login ${site}" to sign in manually, or "web-cli login ${site} --save-credentials" so a future run can recover silently without ever needing a window.`
@@ -51572,14 +51584,15 @@ function noVisibleWindowError(site) {
51572
51584
  }
51573
51585
  async function runInteractiveLogin(opts, deps) {
51574
51586
  const allowVisible = opts.allowVisible ?? true;
51575
- const silentJar = await attemptSilentRefresh(opts, deps);
51587
+ const credentials = await loadSavedCredentials(opts.site, deps);
51588
+ const silentJar = await attemptSilentRefresh(opts, deps, credentials);
51576
51589
  if (silentJar) {
51577
51590
  establishedJars.set(opts.site, silentJar);
51578
51591
  await (deps.saveSession ?? saveSessionJar)(opts.site, silentJar);
51579
51592
  return silentJar;
51580
51593
  }
51581
51594
  if (!allowVisible) {
51582
- throw noVisibleWindowError(opts.site);
51595
+ throw noVisibleWindowError(opts.site, credentials !== null);
51583
51596
  }
51584
51597
  return runVisibleLogin(opts, deps);
51585
51598
  }
@@ -51791,8 +51804,9 @@ function callOptionsFor(intent, signal) {
51791
51804
  }
51792
51805
  async function attemptReplay(site, intent, param, jarOverride, signal) {
51793
51806
  const config2 = getSiteConfig(site);
51807
+ const siteSupportsLogin = config2.requiresLogin !== false;
51794
51808
  const cookieJar = jarOverride ?? getEstablishedSession(site) ?? mergeStoredSession(await getLiveCookieJar(config2.host), await loadSessionJar(site));
51795
- if (Object.keys(cookieJar).length === 0) {
51809
+ if (Object.keys(cookieJar).length === 0 && siteSupportsLogin) {
51796
51810
  throw new AuthRequiredError(
51797
51811
  `No live session cookies found for ${config2.host} in web-cli's Chrome profile`,
51798
51812
  site
@@ -51810,7 +51824,7 @@ async function attemptReplay(site, intent, param, jarOverride, signal) {
51810
51824
  if (isAbortError(err)) {
51811
51825
  throw err;
51812
51826
  }
51813
- if (isAuthFailure(err)) {
51827
+ if (isAuthFailure(err) && siteSupportsLogin) {
51814
51828
  throw err;
51815
51829
  }
51816
51830
  }
@@ -51823,7 +51837,10 @@ async function attemptReplay(site, intent, param, jarOverride, signal) {
51823
51837
  persistTemplateBestEffort(site, intent, preseed);
51824
51838
  return { data, discovered: false };
51825
51839
  } catch (err) {
51826
- if (isAuthFailure(err) || isAbortError(err)) {
51840
+ if (isAbortError(err)) {
51841
+ throw err;
51842
+ }
51843
+ if (isAuthFailure(err) && siteSupportsLogin) {
51827
51844
  throw err;
51828
51845
  }
51829
51846
  }
@@ -52414,10 +52431,17 @@ function describeError(err) {
52414
52431
  code: err instanceof CliError ? err.code : "INTERNAL_ERROR"
52415
52432
  };
52416
52433
  }
52417
- function recommendedActionFor(code, site) {
52434
+ function recommendedActionFor(code, site, sessionExpiredContext) {
52418
52435
  switch (code) {
52419
- case "SESSION_EXPIRED":
52436
+ case "SESSION_EXPIRED": {
52437
+ if (sessionExpiredContext && !sessionExpiredContext.siteSupportsLogin) {
52438
+ return `"${site}" has no login/account concept at all (requiresLogin: false), so web-cli never attempted a credential-based refresh here \u2014 there is nothing to sign into, and telling the user to save credentials or sign in would be wrong. This SESSION_EXPIRED means the site's own short-lived per-request token went stale; simply retrying re-discovers a fresh one automatically. If it keeps failing, run "web-cli observe --site ${site} --intent search --param \\"<a real query>\\"" once to re-learn the endpoint by hand. Never substitute another site's results in place of this one without saying so.`;
52439
+ }
52440
+ if (sessionExpiredContext?.hasCredential) {
52441
+ return `A credential is ALREADY saved for "${site}" \u2014 web-cli's own silent, credential-based refresh already tried it before returning this. Do NOT tell the user to run "web-cli login ${site} --save-credentials" again as if nothing were saved; it is, and re-saving an unchanged password will not fix this. The likely cause is something the automatic form-fill cannot get past on its own (a CAPTCHA, a 2FA prompt, a changed login page) or a now-incorrect saved password. Tell the user to re-run with --allow-visible-login to sign in by hand and see what is actually blocking it, or run "web-cli login ${site}" directly \u2014 and to re-save credentials only if the password genuinely changed. Never substitute another site's results, a previously-saved file, or any other data in place of this one without saying so.`;
52442
+ }
52420
52443
  return `The silent, credential-based session refresh for "${site}" already ran and did not recover \u2014 do not retry the same call expecting a different result. Tell the user to run "web-cli login ${site} --save-credentials" once so every future run recovers silently with no human needed, or re-run with --allow-visible-login if a human can sign in right now. Never substitute another site's results, a previously-saved file, or any other data in place of this one without saying so.`;
52444
+ }
52421
52445
  case "DISCOVERY_FAILED":
52422
52446
  return `This site's endpoint needs re-learning: run "web-cli observe --site ${site} --intent <search|getArticle> --param \\"<a real query/article for ${site}>\\"" once, then retry.`;
52423
52447
  case "ARTICLE_MISMATCH":
@@ -52428,7 +52452,27 @@ function recommendedActionFor(code, site) {
52428
52452
  return `No specific automatic recovery is known for this code ("${code}") \u2014 do not silently drop or paper over it; state the failure and its message/code plainly in your answer.`;
52429
52453
  }
52430
52454
  }
52431
- function buildAgentTask(query, keyReferences, sites2) {
52455
+ async function buildAgentTask(query, keyReferences, sites2) {
52456
+ const sessionExpiredSites = /* @__PURE__ */ new Set();
52457
+ for (const [site, outcome] of Object.entries(sites2)) {
52458
+ if (outcome.error !== void 0 && (outcome.code ?? "INTERNAL_ERROR") === "SESSION_EXPIRED") {
52459
+ sessionExpiredSites.add(site);
52460
+ }
52461
+ }
52462
+ for (const ref of keyReferences) {
52463
+ if (ref.fetchError !== void 0 && (ref.fetchError.code ?? "INTERNAL_ERROR") === "SESSION_EXPIRED") {
52464
+ sessionExpiredSites.add(ref.site);
52465
+ }
52466
+ }
52467
+ const sessionExpiredContext = new Map(
52468
+ await Promise.all(
52469
+ Array.from(sessionExpiredSites).map(async (site) => {
52470
+ const siteSupportsLogin = getSiteConfig(site).requiresLogin !== false;
52471
+ const hasCredential = siteSupportsLogin ? await hasStoredCredentials(site) : false;
52472
+ return [site, { siteSupportsLogin, hasCredential }];
52473
+ })
52474
+ )
52475
+ );
52432
52476
  const unrecoveredFailures = [];
52433
52477
  for (const [site, outcome] of Object.entries(sites2)) {
52434
52478
  if (outcome.error !== void 0) {
@@ -52438,7 +52482,7 @@ function buildAgentTask(query, keyReferences, sites2) {
52438
52482
  site,
52439
52483
  code,
52440
52484
  error: outcome.error,
52441
- recommendedAction: recommendedActionFor(code, site)
52485
+ recommendedAction: recommendedActionFor(code, site, sessionExpiredContext.get(site))
52442
52486
  });
52443
52487
  }
52444
52488
  }
@@ -52452,7 +52496,7 @@ function buildAgentTask(query, keyReferences, sites2) {
52452
52496
  url: ref.url,
52453
52497
  code,
52454
52498
  error: ref.fetchError.error,
52455
- recommendedAction: recommendedActionFor(code, ref.site)
52499
+ recommendedAction: recommendedActionFor(code, ref.site, sessionExpiredContext.get(ref.site))
52456
52500
  });
52457
52501
  }
52458
52502
  }
@@ -52742,10 +52786,11 @@ async function runSearch(query, extraSites = [], opts = {}) {
52742
52786
  markdownPath = void 0;
52743
52787
  }
52744
52788
  }
52789
+ const agentTask = await buildAgentTask(query, keyReferences, sites2);
52745
52790
  const result = {
52746
52791
  ...document2,
52747
52792
  summary,
52748
- agentTask: buildAgentTask(query, keyReferences, sites2),
52793
+ agentTask,
52749
52794
  ...markdownPath !== void 0 ? { markdownPath } : {},
52750
52795
  // Present only when something actually went wrong, so its mere presence is the signal.
52751
52796
  ...markdownError !== void 0 ? { markdownError } : {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ysgao/web-cli",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Self-discovering REST API CLI for subscription medical-knowledge sites, driven by an AI agent",
5
5
  "license": "Apache-2.0",
6
6
  "bin": {