@ysgao/web-cli 0.3.0 → 0.3.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 +52 -11
- 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
|
|
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
|
}
|
|
@@ -52414,10 +52427,17 @@ function describeError(err) {
|
|
|
52414
52427
|
code: err instanceof CliError ? err.code : "INTERNAL_ERROR"
|
|
52415
52428
|
};
|
|
52416
52429
|
}
|
|
52417
|
-
function recommendedActionFor(code, site) {
|
|
52430
|
+
function recommendedActionFor(code, site, sessionExpiredContext) {
|
|
52418
52431
|
switch (code) {
|
|
52419
|
-
case "SESSION_EXPIRED":
|
|
52432
|
+
case "SESSION_EXPIRED": {
|
|
52433
|
+
if (sessionExpiredContext && !sessionExpiredContext.siteSupportsLogin) {
|
|
52434
|
+
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.`;
|
|
52435
|
+
}
|
|
52436
|
+
if (sessionExpiredContext?.hasCredential) {
|
|
52437
|
+
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.`;
|
|
52438
|
+
}
|
|
52420
52439
|
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.`;
|
|
52440
|
+
}
|
|
52421
52441
|
case "DISCOVERY_FAILED":
|
|
52422
52442
|
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
52443
|
case "ARTICLE_MISMATCH":
|
|
@@ -52428,7 +52448,27 @@ function recommendedActionFor(code, site) {
|
|
|
52428
52448
|
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
52449
|
}
|
|
52430
52450
|
}
|
|
52431
|
-
function buildAgentTask(query, keyReferences, sites2) {
|
|
52451
|
+
async function buildAgentTask(query, keyReferences, sites2) {
|
|
52452
|
+
const sessionExpiredSites = /* @__PURE__ */ new Set();
|
|
52453
|
+
for (const [site, outcome] of Object.entries(sites2)) {
|
|
52454
|
+
if (outcome.error !== void 0 && (outcome.code ?? "INTERNAL_ERROR") === "SESSION_EXPIRED") {
|
|
52455
|
+
sessionExpiredSites.add(site);
|
|
52456
|
+
}
|
|
52457
|
+
}
|
|
52458
|
+
for (const ref of keyReferences) {
|
|
52459
|
+
if (ref.fetchError !== void 0 && (ref.fetchError.code ?? "INTERNAL_ERROR") === "SESSION_EXPIRED") {
|
|
52460
|
+
sessionExpiredSites.add(ref.site);
|
|
52461
|
+
}
|
|
52462
|
+
}
|
|
52463
|
+
const sessionExpiredContext = new Map(
|
|
52464
|
+
await Promise.all(
|
|
52465
|
+
Array.from(sessionExpiredSites).map(async (site) => {
|
|
52466
|
+
const siteSupportsLogin = getSiteConfig(site).requiresLogin !== false;
|
|
52467
|
+
const hasCredential = siteSupportsLogin ? await hasStoredCredentials(site) : false;
|
|
52468
|
+
return [site, { siteSupportsLogin, hasCredential }];
|
|
52469
|
+
})
|
|
52470
|
+
)
|
|
52471
|
+
);
|
|
52432
52472
|
const unrecoveredFailures = [];
|
|
52433
52473
|
for (const [site, outcome] of Object.entries(sites2)) {
|
|
52434
52474
|
if (outcome.error !== void 0) {
|
|
@@ -52438,7 +52478,7 @@ function buildAgentTask(query, keyReferences, sites2) {
|
|
|
52438
52478
|
site,
|
|
52439
52479
|
code,
|
|
52440
52480
|
error: outcome.error,
|
|
52441
|
-
recommendedAction: recommendedActionFor(code, site)
|
|
52481
|
+
recommendedAction: recommendedActionFor(code, site, sessionExpiredContext.get(site))
|
|
52442
52482
|
});
|
|
52443
52483
|
}
|
|
52444
52484
|
}
|
|
@@ -52452,7 +52492,7 @@ function buildAgentTask(query, keyReferences, sites2) {
|
|
|
52452
52492
|
url: ref.url,
|
|
52453
52493
|
code,
|
|
52454
52494
|
error: ref.fetchError.error,
|
|
52455
|
-
recommendedAction: recommendedActionFor(code, ref.site)
|
|
52495
|
+
recommendedAction: recommendedActionFor(code, ref.site, sessionExpiredContext.get(ref.site))
|
|
52456
52496
|
});
|
|
52457
52497
|
}
|
|
52458
52498
|
}
|
|
@@ -52742,10 +52782,11 @@ async function runSearch(query, extraSites = [], opts = {}) {
|
|
|
52742
52782
|
markdownPath = void 0;
|
|
52743
52783
|
}
|
|
52744
52784
|
}
|
|
52785
|
+
const agentTask = await buildAgentTask(query, keyReferences, sites2);
|
|
52745
52786
|
const result = {
|
|
52746
52787
|
...document2,
|
|
52747
52788
|
summary,
|
|
52748
|
-
agentTask
|
|
52789
|
+
agentTask,
|
|
52749
52790
|
...markdownPath !== void 0 ? { markdownPath } : {},
|
|
52750
52791
|
// Present only when something actually went wrong, so its mere presence is the signal.
|
|
52751
52792
|
...markdownError !== void 0 ? { markdownError } : {},
|