@blazediff/agent 0.4.0 → 0.6.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.d.mts CHANGED
@@ -151,7 +151,7 @@ interface CaptureOptions {
151
151
  }
152
152
  interface DiscoveredRoute {
153
153
  url: string;
154
- source: "next-manifest" | "sitemap" | "crawl";
154
+ source: "crawl";
155
155
  auth?: string;
156
156
  }
157
157
  interface CheckReport {
@@ -271,11 +271,16 @@ interface DiffOutcome {
271
271
  }
272
272
  declare function diffEntry(id: string, baselinePath: string, actualPath: string, opts?: DiffOptions, cwd?: string): Promise<DiffOutcome>;
273
273
 
274
+ interface TemplateSampling {
275
+ threshold: number;
276
+ samples: number;
277
+ }
278
+
274
279
  interface DiscoverOptions {
275
280
  baseUrl: string;
276
281
  cwd?: string;
277
282
  maxRoutes?: number;
278
- skipCrawl?: boolean;
283
+ sampleTemplates?: boolean | TemplateSampling;
279
284
  }
280
285
  declare function discover(opts: DiscoverOptions): Promise<DiscoveredRoute[]>;
281
286
 
package/dist/index.d.ts CHANGED
@@ -151,7 +151,7 @@ interface CaptureOptions {
151
151
  }
152
152
  interface DiscoveredRoute {
153
153
  url: string;
154
- source: "next-manifest" | "sitemap" | "crawl";
154
+ source: "crawl";
155
155
  auth?: string;
156
156
  }
157
157
  interface CheckReport {
@@ -271,11 +271,16 @@ interface DiffOutcome {
271
271
  }
272
272
  declare function diffEntry(id: string, baselinePath: string, actualPath: string, opts?: DiffOptions, cwd?: string): Promise<DiffOutcome>;
273
273
 
274
+ interface TemplateSampling {
275
+ threshold: number;
276
+ samples: number;
277
+ }
278
+
274
279
  interface DiscoverOptions {
275
280
  baseUrl: string;
276
281
  cwd?: string;
277
282
  maxRoutes?: number;
278
- skipCrawl?: boolean;
283
+ sampleTemplates?: boolean | TemplateSampling;
279
284
  }
280
285
  declare function discover(opts: DiscoverOptions): Promise<DiscoveredRoute[]>;
281
286
 
package/dist/index.js CHANGED
@@ -513,7 +513,9 @@ function resolveBaseUrl(config, override) {
513
513
  if (override) return override;
514
514
  if (config?.baseUrl) return config.baseUrl;
515
515
  if (config?.devServer) return `http://127.0.0.1:${config.devServer.port}`;
516
- throw new Error("no baseUrl: pass --base-url or run `blazediff-agent init`");
516
+ throw new Error(
517
+ "no baseUrl: pass --base-url or run `blazediff-agent onboard`"
518
+ );
517
519
  }
518
520
 
519
521
  // src/graph/semaphore.ts
@@ -876,9 +878,9 @@ function extractInternalLinks(base, target, hrefs) {
876
878
  try {
877
879
  const u = new URL(href, target);
878
880
  if (u.origin !== base.origin) continue;
879
- const path23 = u.pathname + u.search;
880
- if (path23.startsWith("/api/")) continue;
881
- out.push(path23);
881
+ const path22 = u.pathname + u.search;
882
+ if (path22.startsWith("/api/")) continue;
883
+ out.push(path22);
882
884
  } catch {
883
885
  }
884
886
  }
@@ -908,6 +910,8 @@ async function crawlRoutes(opts) {
908
910
  waitUntil: "domcontentloaded",
909
911
  timeout: 15e3
910
912
  });
913
+ await page.waitForLoadState("networkidle", { timeout: 5e3 }).catch(() => {
914
+ });
911
915
  const hrefs = await page.evaluate(
912
916
  () => Array.from(
913
917
  document.querySelectorAll("a[href]")
@@ -932,74 +936,38 @@ async function crawlRoutes(opts) {
932
936
  }
933
937
  return discovered.slice(0, maxRoutes);
934
938
  }
935
- async function fileExists2(p) {
936
- try {
937
- await promises.access(p);
938
- return true;
939
- } catch {
940
- return false;
941
- }
942
- }
943
- async function readJsonOrNull(file) {
944
- try {
945
- return JSON.parse(await promises.readFile(file, "utf8"));
946
- } catch {
947
- return null;
948
- }
949
- }
950
-
951
- // src/discover/next.ts
952
- var DYNAMIC_SEGMENT = /\[[^\]]+\]/;
953
- function isPublicRoute(route) {
954
- if (DYNAMIC_SEGMENT.test(route)) return false;
955
- if (route === "/api" || route.startsWith("/api/")) return false;
956
- return true;
957
- }
958
- async function discoverFromNextManifest(cwd = process.cwd()) {
959
- const nextDir = path__default.default.join(cwd, ".next");
960
- if (!fs.existsSync(nextDir)) return [];
961
- const seen = /* @__PURE__ */ new Set();
962
- const out = [];
963
- const add = (url) => {
964
- if (seen.has(url)) return;
965
- seen.add(url);
966
- out.push({ url, source: "next-manifest" });
967
- };
968
- const routes = await readJsonOrNull(
969
- path__default.default.join(nextDir, "routes-manifest.json")
970
- );
971
- for (const r of routes?.staticRoutes ?? []) {
972
- if (isPublicRoute(r.page)) add(r.page);
973
- }
974
- const appPaths = await readJsonOrNull(
975
- path__default.default.join(nextDir, "server", "app-paths-manifest.json")
976
- );
977
- for (const route of Object.keys(appPaths ?? {})) {
978
- const normalized = route.replace(/\/page$/, "") || "/";
979
- if (isPublicRoute(normalized)) add(normalized);
980
- }
981
- return out;
982
- }
983
939
 
984
- // src/discover/sitemap.ts
985
- var CANDIDATES = ["/sitemap.xml", "/sitemap_index.xml"];
986
- var LOC_RE = /<loc>([^<]+)<\/loc>/g;
987
- async function discoverFromSitemap(baseUrl) {
988
- for (const candidate of CANDIDATES) {
989
- try {
990
- const res = await fetch(new URL(candidate, baseUrl));
991
- if (!res.ok) continue;
992
- const text = await res.text();
993
- const urls = Array.from(text.matchAll(LOC_RE)).map((m) => m[1]);
994
- if (!urls.length) continue;
995
- return urls.map((u) => {
996
- const url = new URL(u);
997
- return { url: url.pathname + url.search, source: "sitemap" };
998
- });
999
- } catch {
1000
- }
940
+ // src/discover/templates.ts
941
+ var DEFAULT_SAMPLE_THRESHOLD = 5;
942
+ var DEFAULT_SAMPLES_PER_TEMPLATE = 2;
943
+ var DEFAULT_SAMPLING = {
944
+ threshold: DEFAULT_SAMPLE_THRESHOLD,
945
+ samples: DEFAULT_SAMPLES_PER_TEMPLATE
946
+ };
947
+ function parentPath(path22) {
948
+ const [pathPart] = path22.split("?", 2);
949
+ const idx = pathPart.lastIndexOf("/");
950
+ return idx <= 0 ? "/" : pathPart.slice(0, idx);
951
+ }
952
+ function collapseTemplates(routes, { threshold, samples } = DEFAULT_SAMPLING) {
953
+ const groups = /* @__PURE__ */ new Map();
954
+ for (const route of routes) {
955
+ const parent = parentPath(route.url);
956
+ const group = groups.get(parent);
957
+ if (group) group.push(route);
958
+ else groups.set(parent, [route]);
959
+ }
960
+ const drop = /* @__PURE__ */ new Set();
961
+ const collapsed = [];
962
+ for (const [parent, group] of groups) {
963
+ if (parent === "/" || group.length < threshold) continue;
964
+ for (const route of group.slice(samples)) drop.add(route.url);
965
+ collapsed.push(`${parent}/* (${group.length} \u2192 ${samples})`);
1001
966
  }
1002
- return [];
967
+ return {
968
+ routes: drop.size ? routes.filter((r) => !drop.has(r.url)) : routes,
969
+ collapsed
970
+ };
1003
971
  }
1004
972
 
1005
973
  // src/discover/index.ts
@@ -1009,27 +977,23 @@ function normalizePath(url) {
1009
977
  const normalizedPath = trimmed === "" ? "/" : trimmed;
1010
978
  return query ? `${normalizedPath}?${query}` : normalizedPath;
1011
979
  }
1012
- function mergeBy(routes, into) {
980
+ function dedupe(routes) {
981
+ const merged = /* @__PURE__ */ new Map();
1013
982
  for (const r of routes) {
1014
983
  const key = normalizePath(r.url);
1015
- if (!into.has(key)) into.set(key, { ...r, url: key });
984
+ if (!merged.has(key)) merged.set(key, { ...r, url: key });
1016
985
  }
986
+ return Array.from(merged.values());
1017
987
  }
1018
988
  async function discover(opts) {
1019
- const cwd = opts.cwd ?? process.cwd();
1020
- const merged = /* @__PURE__ */ new Map();
1021
- mergeBy(await discoverFromNextManifest(cwd), merged);
1022
- mergeBy(await discoverFromSitemap(opts.baseUrl), merged);
1023
- if (!opts.skipCrawl) {
1024
- const crawlMax = Math.max(0, (opts.maxRoutes ?? 50) - merged.size);
1025
- if (crawlMax > 0) {
1026
- mergeBy(
1027
- await crawlRoutes({ baseUrl: opts.baseUrl, maxRoutes: crawlMax }),
1028
- merged
1029
- );
1030
- }
1031
- }
1032
- return Array.from(merged.values()).sort((a, b) => a.url.localeCompare(b.url));
989
+ const crawled = await crawlRoutes({
990
+ baseUrl: opts.baseUrl,
991
+ maxRoutes: opts.maxRoutes ?? 50
992
+ });
993
+ const deduped = dedupe(crawled);
994
+ const sampling = opts.sampleTemplates ?? true;
995
+ const routes = sampling === false ? deduped : collapseTemplates(deduped, sampling === true ? void 0 : sampling).routes;
996
+ return routes.sort((a, b) => a.url.localeCompare(b.url));
1033
997
  }
1034
998
  function envFiles(cwd) {
1035
999
  const root = paths(cwd).root;
@@ -1907,6 +1871,23 @@ var noneJudge = {
1907
1871
  return { kind: "judged", verdict: input.heuristicVerdict };
1908
1872
  }
1909
1873
  };
1874
+ async function fileExists2(p) {
1875
+ try {
1876
+ await promises.access(p);
1877
+ return true;
1878
+ } catch {
1879
+ return false;
1880
+ }
1881
+ }
1882
+ async function readJsonOrNull(file) {
1883
+ try {
1884
+ return JSON.parse(await promises.readFile(file, "utf8"));
1885
+ } catch {
1886
+ return null;
1887
+ }
1888
+ }
1889
+
1890
+ // src/report/json.ts
1910
1891
  async function writeReport(report, cwd = process.cwd()) {
1911
1892
  const file = paths(cwd).report;
1912
1893
  await promises.mkdir(path__default.default.dirname(file), { recursive: true });
@@ -2024,7 +2005,7 @@ async function reconstructFromDisk(cwd, dirs) {
2024
2005
  const manifest = await loadManifest(cwd);
2025
2006
  if (!manifest) {
2026
2007
  throw new Error(
2027
- `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent init\` first.`
2008
+ `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent onboard\` first.`
2028
2009
  );
2029
2010
  }
2030
2011
  const dirById = new Map(dirs.map((d) => [d.id, d]));
@@ -2068,7 +2049,7 @@ async function applyJudgments(opts = {}) {
2068
2049
  const manifest = await loadManifest(cwd);
2069
2050
  if (!manifest) {
2070
2051
  throw new Error(
2071
- `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent init\` first.`
2052
+ `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent onboard\` first.`
2072
2053
  );
2073
2054
  }
2074
2055
  const dirs = await readJudgmentDirs(paths(cwd).judgments);
@@ -3044,7 +3025,7 @@ async function loadNode(state) {
3044
3025
  const manifest = await loadManifest(state.options.cwd);
3045
3026
  if (!manifest) {
3046
3027
  throw new Error(
3047
- `no manifest found at ${paths(state.options.cwd).manifest}. Run \`blazediff init\` then \`/blazediff\` (or capture manually) first.`
3028
+ `no manifest found at ${paths(state.options.cwd).manifest}. Run \`blazediff-agent onboard\` then \`/blazediff\` (or capture manually) first.`
3048
3029
  );
3049
3030
  }
3050
3031
  return { entries: manifest.entries, manifest };
package/dist/index.mjs CHANGED
@@ -485,7 +485,9 @@ function resolveBaseUrl(config, override) {
485
485
  if (override) return override;
486
486
  if (config?.baseUrl) return config.baseUrl;
487
487
  if (config?.devServer) return `http://127.0.0.1:${config.devServer.port}`;
488
- throw new Error("no baseUrl: pass --base-url or run `blazediff-agent init`");
488
+ throw new Error(
489
+ "no baseUrl: pass --base-url or run `blazediff-agent onboard`"
490
+ );
489
491
  }
490
492
 
491
493
  // src/graph/semaphore.ts
@@ -848,9 +850,9 @@ function extractInternalLinks(base, target, hrefs) {
848
850
  try {
849
851
  const u = new URL(href, target);
850
852
  if (u.origin !== base.origin) continue;
851
- const path23 = u.pathname + u.search;
852
- if (path23.startsWith("/api/")) continue;
853
- out.push(path23);
853
+ const path22 = u.pathname + u.search;
854
+ if (path22.startsWith("/api/")) continue;
855
+ out.push(path22);
854
856
  } catch {
855
857
  }
856
858
  }
@@ -880,6 +882,8 @@ async function crawlRoutes(opts) {
880
882
  waitUntil: "domcontentloaded",
881
883
  timeout: 15e3
882
884
  });
885
+ await page.waitForLoadState("networkidle", { timeout: 5e3 }).catch(() => {
886
+ });
883
887
  const hrefs = await page.evaluate(
884
888
  () => Array.from(
885
889
  document.querySelectorAll("a[href]")
@@ -904,74 +908,38 @@ async function crawlRoutes(opts) {
904
908
  }
905
909
  return discovered.slice(0, maxRoutes);
906
910
  }
907
- async function fileExists2(p) {
908
- try {
909
- await access(p);
910
- return true;
911
- } catch {
912
- return false;
913
- }
914
- }
915
- async function readJsonOrNull(file) {
916
- try {
917
- return JSON.parse(await readFile(file, "utf8"));
918
- } catch {
919
- return null;
920
- }
921
- }
922
-
923
- // src/discover/next.ts
924
- var DYNAMIC_SEGMENT = /\[[^\]]+\]/;
925
- function isPublicRoute(route) {
926
- if (DYNAMIC_SEGMENT.test(route)) return false;
927
- if (route === "/api" || route.startsWith("/api/")) return false;
928
- return true;
929
- }
930
- async function discoverFromNextManifest(cwd = process.cwd()) {
931
- const nextDir = path.join(cwd, ".next");
932
- if (!existsSync(nextDir)) return [];
933
- const seen = /* @__PURE__ */ new Set();
934
- const out = [];
935
- const add = (url) => {
936
- if (seen.has(url)) return;
937
- seen.add(url);
938
- out.push({ url, source: "next-manifest" });
939
- };
940
- const routes = await readJsonOrNull(
941
- path.join(nextDir, "routes-manifest.json")
942
- );
943
- for (const r of routes?.staticRoutes ?? []) {
944
- if (isPublicRoute(r.page)) add(r.page);
945
- }
946
- const appPaths = await readJsonOrNull(
947
- path.join(nextDir, "server", "app-paths-manifest.json")
948
- );
949
- for (const route of Object.keys(appPaths ?? {})) {
950
- const normalized = route.replace(/\/page$/, "") || "/";
951
- if (isPublicRoute(normalized)) add(normalized);
952
- }
953
- return out;
954
- }
955
911
 
956
- // src/discover/sitemap.ts
957
- var CANDIDATES = ["/sitemap.xml", "/sitemap_index.xml"];
958
- var LOC_RE = /<loc>([^<]+)<\/loc>/g;
959
- async function discoverFromSitemap(baseUrl) {
960
- for (const candidate of CANDIDATES) {
961
- try {
962
- const res = await fetch(new URL(candidate, baseUrl));
963
- if (!res.ok) continue;
964
- const text = await res.text();
965
- const urls = Array.from(text.matchAll(LOC_RE)).map((m) => m[1]);
966
- if (!urls.length) continue;
967
- return urls.map((u) => {
968
- const url = new URL(u);
969
- return { url: url.pathname + url.search, source: "sitemap" };
970
- });
971
- } catch {
972
- }
912
+ // src/discover/templates.ts
913
+ var DEFAULT_SAMPLE_THRESHOLD = 5;
914
+ var DEFAULT_SAMPLES_PER_TEMPLATE = 2;
915
+ var DEFAULT_SAMPLING = {
916
+ threshold: DEFAULT_SAMPLE_THRESHOLD,
917
+ samples: DEFAULT_SAMPLES_PER_TEMPLATE
918
+ };
919
+ function parentPath(path22) {
920
+ const [pathPart] = path22.split("?", 2);
921
+ const idx = pathPart.lastIndexOf("/");
922
+ return idx <= 0 ? "/" : pathPart.slice(0, idx);
923
+ }
924
+ function collapseTemplates(routes, { threshold, samples } = DEFAULT_SAMPLING) {
925
+ const groups = /* @__PURE__ */ new Map();
926
+ for (const route of routes) {
927
+ const parent = parentPath(route.url);
928
+ const group = groups.get(parent);
929
+ if (group) group.push(route);
930
+ else groups.set(parent, [route]);
931
+ }
932
+ const drop = /* @__PURE__ */ new Set();
933
+ const collapsed = [];
934
+ for (const [parent, group] of groups) {
935
+ if (parent === "/" || group.length < threshold) continue;
936
+ for (const route of group.slice(samples)) drop.add(route.url);
937
+ collapsed.push(`${parent}/* (${group.length} \u2192 ${samples})`);
973
938
  }
974
- return [];
939
+ return {
940
+ routes: drop.size ? routes.filter((r) => !drop.has(r.url)) : routes,
941
+ collapsed
942
+ };
975
943
  }
976
944
 
977
945
  // src/discover/index.ts
@@ -981,27 +949,23 @@ function normalizePath(url) {
981
949
  const normalizedPath = trimmed === "" ? "/" : trimmed;
982
950
  return query ? `${normalizedPath}?${query}` : normalizedPath;
983
951
  }
984
- function mergeBy(routes, into) {
952
+ function dedupe(routes) {
953
+ const merged = /* @__PURE__ */ new Map();
985
954
  for (const r of routes) {
986
955
  const key = normalizePath(r.url);
987
- if (!into.has(key)) into.set(key, { ...r, url: key });
956
+ if (!merged.has(key)) merged.set(key, { ...r, url: key });
988
957
  }
958
+ return Array.from(merged.values());
989
959
  }
990
960
  async function discover(opts) {
991
- const cwd = opts.cwd ?? process.cwd();
992
- const merged = /* @__PURE__ */ new Map();
993
- mergeBy(await discoverFromNextManifest(cwd), merged);
994
- mergeBy(await discoverFromSitemap(opts.baseUrl), merged);
995
- if (!opts.skipCrawl) {
996
- const crawlMax = Math.max(0, (opts.maxRoutes ?? 50) - merged.size);
997
- if (crawlMax > 0) {
998
- mergeBy(
999
- await crawlRoutes({ baseUrl: opts.baseUrl, maxRoutes: crawlMax }),
1000
- merged
1001
- );
1002
- }
1003
- }
1004
- return Array.from(merged.values()).sort((a, b) => a.url.localeCompare(b.url));
961
+ const crawled = await crawlRoutes({
962
+ baseUrl: opts.baseUrl,
963
+ maxRoutes: opts.maxRoutes ?? 50
964
+ });
965
+ const deduped = dedupe(crawled);
966
+ const sampling = opts.sampleTemplates ?? true;
967
+ const routes = sampling === false ? deduped : collapseTemplates(deduped, sampling === true ? void 0 : sampling).routes;
968
+ return routes.sort((a, b) => a.url.localeCompare(b.url));
1005
969
  }
1006
970
  function envFiles(cwd) {
1007
971
  const root = paths(cwd).root;
@@ -1879,6 +1843,23 @@ var noneJudge = {
1879
1843
  return { kind: "judged", verdict: input.heuristicVerdict };
1880
1844
  }
1881
1845
  };
1846
+ async function fileExists2(p) {
1847
+ try {
1848
+ await access(p);
1849
+ return true;
1850
+ } catch {
1851
+ return false;
1852
+ }
1853
+ }
1854
+ async function readJsonOrNull(file) {
1855
+ try {
1856
+ return JSON.parse(await readFile(file, "utf8"));
1857
+ } catch {
1858
+ return null;
1859
+ }
1860
+ }
1861
+
1862
+ // src/report/json.ts
1882
1863
  async function writeReport(report, cwd = process.cwd()) {
1883
1864
  const file = paths(cwd).report;
1884
1865
  await mkdir(path.dirname(file), { recursive: true });
@@ -1996,7 +1977,7 @@ async function reconstructFromDisk(cwd, dirs) {
1996
1977
  const manifest = await loadManifest(cwd);
1997
1978
  if (!manifest) {
1998
1979
  throw new Error(
1999
- `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent init\` first.`
1980
+ `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent onboard\` first.`
2000
1981
  );
2001
1982
  }
2002
1983
  const dirById = new Map(dirs.map((d) => [d.id, d]));
@@ -2040,7 +2021,7 @@ async function applyJudgments(opts = {}) {
2040
2021
  const manifest = await loadManifest(cwd);
2041
2022
  if (!manifest) {
2042
2023
  throw new Error(
2043
- `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent init\` first.`
2024
+ `no manifest at ${paths(cwd).manifest}. Run \`blazediff-agent onboard\` first.`
2044
2025
  );
2045
2026
  }
2046
2027
  const dirs = await readJudgmentDirs(paths(cwd).judgments);
@@ -3016,7 +2997,7 @@ async function loadNode(state) {
3016
2997
  const manifest = await loadManifest(state.options.cwd);
3017
2998
  if (!manifest) {
3018
2999
  throw new Error(
3019
- `no manifest found at ${paths(state.options.cwd).manifest}. Run \`blazediff init\` then \`/blazediff\` (or capture manually) first.`
3000
+ `no manifest found at ${paths(state.options.cwd).manifest}. Run \`blazediff-agent onboard\` then \`/blazediff\` (or capture manually) first.`
3020
3001
  );
3021
3002
  }
3022
3003
  return { entries: manifest.entries, manifest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blazediff/agent",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Agentic visual regression for BlazeDiff. Auto-discovers routes, captures deterministic screenshots, runs CI checks.",
5
5
  "private": false,
6
6
  "publishConfig": {