@fetchbrain.com/sdk 0.8.0 → 0.9.0

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.mjs CHANGED
@@ -1159,7 +1159,12 @@ function safeChangeStats(client) {
1159
1159
  }
1160
1160
  }
1161
1161
  var STRIP_HEADERS = /* @__PURE__ */ new Set(["authorization", "cookie", "set-cookie"]);
1162
- var SDK_USERDATA_KEYS = /* @__PURE__ */ new Set(["fetchBrainRecalled", "fetchBrainData", "__fetchBrainResult"]);
1162
+ var SDK_USERDATA_KEYS = /* @__PURE__ */ new Set([
1163
+ "fetchBrainRecalled",
1164
+ "fetchBrainData",
1165
+ "__fetchBrainResult",
1166
+ "__fetchBrainCounted"
1167
+ ]);
1163
1168
  var MAX_STASH_ENTRIES = 5e3;
1164
1169
  var MAX_STASH_BYTES = 256 * 1024 * 1024;
1165
1170
  var stashBytesCap = MAX_STASH_BYTES;
@@ -1278,6 +1283,10 @@ var FetchBrain = class {
1278
1283
  recalled: 0,
1279
1284
  aiSkipped: 0,
1280
1285
  // brain knew + skipped scraping
1286
+ requestsSkipped: 0,
1287
+ // recalled without a fetch (pre-recalled at enqueue + skipNavigation)
1288
+ lateRecalls: 0,
1289
+ // recalled after the page was already fetched
1281
1290
  learned: 0,
1282
1291
  scraped: 0,
1283
1292
  // Actually ran handler
@@ -1432,20 +1441,31 @@ var FetchBrain = class {
1432
1441
  const handlerLabel = request.label;
1433
1442
  const runHandler = shouldRunHandler(config.alwaysRun, handlerLabel);
1434
1443
  if (aiResult.known && aiResult.data && !runHandler) {
1435
- runStats.recalled++;
1436
- runStats.aiSkipped++;
1437
1444
  logger.info(`Recalled: ${url} [${handlerLabel || "default"}]`);
1438
1445
  if (originalPushData) {
1439
1446
  await originalPushData.call(context, aiResult.data);
1440
1447
  }
1448
+ try {
1449
+ if (!request.userData?.__fetchBrainCounted) {
1450
+ request.userData = { ...request.userData, __fetchBrainCounted: true };
1451
+ runStats.recalled++;
1452
+ runStats.aiSkipped++;
1453
+ const skippedNav = request.skipNavigation === true;
1454
+ if (preRecalled?.known && skippedNav) {
1455
+ runStats.requestsSkipped++;
1456
+ } else if (!skippedNav) {
1457
+ runStats.lateRecalls++;
1458
+ }
1459
+ }
1460
+ } catch (countErr) {
1461
+ logger.debug(`Run stats skipped: ${countErr}`);
1462
+ }
1441
1463
  await fireOnRecalled(aiResult.data);
1442
1464
  return;
1443
1465
  }
1444
- runStats.scraped++;
1445
1466
  const counts = labelCounts(request.label);
1446
1467
  if (counts) counts.scraped++;
1447
1468
  if (aiResult.known) {
1448
- runStats.recalled++;
1449
1469
  logger.info(
1450
1470
  `Running handler [${handlerLabel || "default"}] with recalled data available: ${url}`
1451
1471
  );
@@ -1477,6 +1497,15 @@ var FetchBrain = class {
1477
1497
  if (counts) counts.failed++;
1478
1498
  throw handlerErr;
1479
1499
  }
1500
+ try {
1501
+ if (!request.userData?.__fetchBrainCounted) {
1502
+ request.userData = { ...request.userData, __fetchBrainCounted: true };
1503
+ runStats.scraped++;
1504
+ if (aiResult.known) runStats.recalled++;
1505
+ }
1506
+ } catch (countErr) {
1507
+ logger.debug(`Run stats skipped: ${countErr}`);
1508
+ }
1480
1509
  if (counts && learnItems.length === 0 && originalPushData && config.learning !== false && !usedAIData && (!aiResult.known || relearn)) {
1481
1510
  counts.empty++;
1482
1511
  }
@@ -1574,7 +1603,17 @@ var FetchBrain = class {
1574
1603
  runStats.startTime = Date.now();
1575
1604
  const runStartedAt = new Date(runStats.startTime).toISOString();
1576
1605
  labelStats.clear();
1577
- const runBase = { scraped: runStats.scraped, learned: runStats.learned, recalled: runStats.recalled };
1606
+ const runBase = {
1607
+ totalRequests: runStats.totalRequests,
1608
+ bypassed: runStats.bypassed,
1609
+ dropped: runStats.dropped,
1610
+ scraped: runStats.scraped,
1611
+ learned: runStats.learned,
1612
+ recalled: runStats.recalled,
1613
+ aiSkipped: runStats.aiSkipped,
1614
+ requestsSkipped: runStats.requestsSkipped,
1615
+ lateRecalls: runStats.lateRecalls
1616
+ };
1578
1617
  const changeBase = safeChangeStats(client);
1579
1618
  const runId = nextRunId();
1580
1619
  try {
@@ -1591,8 +1630,19 @@ var FetchBrain = class {
1591
1630
  const duration = ((Date.now() - runStats.startTime) / 1e3).toFixed(
1592
1631
  1
1593
1632
  );
1594
- const managedRequests = runStats.totalRequests - runStats.bypassed;
1595
- const savingsPercent = managedRequests > 0 ? (runStats.aiSkipped / managedRequests * 100).toFixed(1) : "0";
1633
+ const run = {
1634
+ totalRequests: runStats.totalRequests - runBase.totalRequests,
1635
+ bypassed: runStats.bypassed - runBase.bypassed,
1636
+ dropped: runStats.dropped - runBase.dropped,
1637
+ scraped: runStats.scraped - runBase.scraped,
1638
+ learned: runStats.learned - runBase.learned,
1639
+ recalled: runStats.recalled - runBase.recalled,
1640
+ aiSkipped: runStats.aiSkipped - runBase.aiSkipped,
1641
+ requestsSkipped: runStats.requestsSkipped - runBase.requestsSkipped,
1642
+ lateRecalls: runStats.lateRecalls - runBase.lateRecalls
1643
+ };
1644
+ const managedRequests = run.totalRequests - run.bypassed;
1645
+ const savingsPercent = managedRequests > 0 ? (run.aiSkipped / managedRequests * 100).toFixed(1) : "0";
1596
1646
  let changes = { changed: 0, drift: [] };
1597
1647
  try {
1598
1648
  const now = safeChangeStats(client);
@@ -1607,8 +1657,13 @@ var FetchBrain = class {
1607
1657
  }
1608
1658
  const showChanged = config.changeDetection?.enabled === true || changes.changed > 0;
1609
1659
  logger.info(
1610
- `\u{1F9E0} Finished! recalled: ${runStats.recalled}/${managedRequests} (${savingsPercent}%), learned: ${runStats.learned}, scraped: ${runStats.scraped}` + (skipLabels ? `, bypassed: ${runStats.bypassed}` : "") + (runStats.dropped ? `, dropped: ${runStats.dropped}` : "") + (showChanged ? `, changed: ${changes.changed}` : "") + `, duration: ${duration}s`
1660
+ `\u{1F9E0} Finished! recalled: ${run.recalled}/${managedRequests} (${savingsPercent}%), requests skipped: ${run.requestsSkipped}, learned: ${run.learned}, scraped: ${run.scraped}` + (skipLabels ? `, bypassed: ${run.bypassed}` : "") + (run.dropped ? `, dropped: ${run.dropped}` : "") + (showChanged ? `, changed: ${changes.changed}` : "") + `, duration: ${duration}s`
1611
1661
  );
1662
+ if (run.lateRecalls > 0 && !crawlerAny.retryOnBlocked && config.preRecall !== false) {
1663
+ logger.info(
1664
+ `${run.lateRecalls} ${run.lateRecalls === 1 ? "page was" : "pages were"} recalled after ${run.lateRecalls === 1 ? "it was" : "they were"} fetched; enqueue them with context.crawler.addRequests([...]) to skip the fetch (non-GET requests also need an explicit uniqueKey).`
1665
+ );
1666
+ }
1612
1667
  for (const item of changes.drift) {
1613
1668
  try {
1614
1669
  logger.warn(describeDrift(item));
@@ -1624,9 +1679,12 @@ var FetchBrain = class {
1624
1679
  build: config.refreshOnRebuild ? getPlatformBuildId() : void 0,
1625
1680
  startedAt: runStartedAt,
1626
1681
  endedAt: (/* @__PURE__ */ new Date()).toISOString(),
1627
- scraped: runStats.scraped - runBase.scraped,
1628
- learned: runStats.learned - runBase.learned,
1629
- recalled: runStats.recalled - runBase.recalled,
1682
+ scraped: run.scraped,
1683
+ learned: run.learned,
1684
+ recalled: run.recalled,
1685
+ requestsSkipped: run.requestsSkipped,
1686
+ lateRecalls: run.lateRecalls,
1687
+ bypassed: run.bypassed,
1630
1688
  labels
1631
1689
  });
1632
1690
  for (const flag of Array.isArray(flags) ? flags : []) {
@@ -1659,10 +1717,11 @@ var FetchBrain = class {
1659
1717
  const r = slice[i];
1660
1718
  if (!r || typeof r.url !== "string") continue;
1661
1719
  const prior = r.userData?.__fetchBrainResult;
1662
- if (prior) {
1663
- if (prior.known && r.skipNavigation) r.skipNavigation = false;
1720
+ if (prior || r.userData?.__fetchBrainCounted) {
1721
+ if (prior?.known && r.skipNavigation) r.skipNavigation = false;
1664
1722
  const rest = { ...r.userData };
1665
1723
  delete rest.__fetchBrainResult;
1724
+ delete rest.__fetchBrainCounted;
1666
1725
  r.userData = rest;
1667
1726
  }
1668
1727
  if (r.skipNavigation) continue;