Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | 22x 22x 22x 22x 22x 22x 146x 22x 147x 22x 22x 22x 22x 22x 22x 25x 29x 70x 70x 5x 10x 10x 10x 1x 9x 9x 9x 10x 1x 10x 1x 10x 3x 3x 2x 10x 10x 1x 10x 22x 15x 3x 5x 4x 5x 4x 12x 5x 15x 3x 3x 4x 4x 1x 3x 3x 1x 2x | import type { StatusScale } from "@/components/detail";
import {
WebCrawlScope,
WebCrawlWritten,
WebFeedContent,
WebPageSince,
WebRefreshCadence,
WebPage,
WebSourceConfig,
} from "@/objects/Knowledge";
import { parseServerDate, timeAgo } from "@/util/time";
/**
* A page's standing after its latest crawl. `gone` is muted rather than
* dangerous: the site removed it, and its history is kept.
*/
export const WEB_PAGE_STATUS: StatusScale = {
active: "success",
gone: "muted",
blocked: "warning",
failed: "danger",
};
/** What a crawl stored for a page: its first version, or a changed one. */
export const CRAWL_WRITTEN: StatusScale = {
new: "success",
changed: "done",
} satisfies Record<WebCrawlWritten, StatusScale[string]>;
export const CRAWL_WRITTEN_LABEL: Record<WebCrawlWritten, string> = {
new: "New in this crawl",
changed: "Changed in this crawl",
};
/** How a page has moved since a crawl. `unchanged` is the quiet case. */
export const PAGE_SINCE: StatusScale = {
added: "progress",
changed: "warning",
unchanged: "neutral",
} satisfies Record<WebPageSince, StatusScale[string]>;
export const PAGE_SINCE_LABEL: Record<WebPageSince, string> = {
added: "Added since",
changed: "Changed since",
unchanged: "Unchanged since",
};
/** "3 hours ago" for a server timestamp, or a dash when there is none. */
export const when = (iso?: string | null) =>
iso ? timeAgo(parseServerDate(iso)) : "—";
/** The local date and time of a server timestamp, for a tooltip. */
export const exactly = (iso?: string | null) =>
iso ? parseServerDate(iso).toLocaleString() : undefined;
/**
* A website source's config as the form edits it: lists as one-per-line
* text. Shared by the New and Edit pages so the two cannot drift apart —
* each used to carry its own `urls` + `depth` copy that svc-core never read.
*/
export interface WebSourceForm {
urls: string;
scope: WebCrawlScope;
max_pages: number;
max_depth: number;
include: string;
exclude: string;
refresh: WebRefreshCadence;
use_sitemap: boolean;
skip_low_value: boolean;
follow_external: boolean;
max_external_pages: number;
feed_content: WebFeedContent;
/** Days, as typed; blank keeps every page. */
max_age_days: string;
attribution: string;
licence_url: string;
}
export const WEB_SOURCE_DEFAULTS: WebSourceForm = {
urls: "",
scope: "section",
max_pages: 25,
max_depth: 2,
include: "",
exclude: "",
refresh: "weekly",
use_sitemap: true,
skip_low_value: true,
follow_external: false,
max_external_pages: 20,
feed_content: "auto",
max_age_days: "",
attribution: "",
licence_url: "",
};
/** Mirrors svc-core's web_config ceilings. */
export const WEB_LIMITS = {
maxUrls: 20,
maxPages: 200,
maxDepth: 5,
maxExternalPages: 100,
maxAgeDays: 3650,
maxAttribution: 500,
} as const;
export const WEB_FEED_CONTENT: {
value: WebFeedContent;
label: string;
description: string;
}[] = [
{
value: "auto",
label: "Best available",
description:
"The feed's own text when it carries the whole item; otherwise the page, read as an article — or, if the page has nothing readable, the entry.",
},
{
value: "entry",
label: "Only what the feed says",
description:
"Never fetches the linked pages — for publishers who offer a feed for syndication but not their pages.",
},
{
value: "article",
label: "Always the page",
description: "Fetches every linked page on the feed's own site.",
},
];
export const WEB_SCOPES: {
value: WebCrawlScope;
label: string;
description: string;
}[] = [
{
value: "page",
label: "Only these pages",
description: "Exactly the URLs listed. A listed sitemap is still read.",
},
{
value: "section",
label: "These pages and below",
description:
"Follows links under each URL's path — docs.example.com/guide reaches /guide/…",
},
{
value: "site",
label: "The whole site",
description: "Follows links anywhere on the same host.",
},
];
export const WEB_REFRESH: { value: WebRefreshCadence; label: string }[] = [
{ value: "hourly", label: "Hourly" },
{ value: "daily", label: "Daily" },
{ value: "weekly", label: "Weekly" },
{ value: "manual", label: "Only when I refresh" },
];
const lines = (text: string) =>
text
.split("\n")
.map((s) => s.trim())
.filter(Boolean);
/** Stored config → form. Reads the old urls + depth shape too. */
export function webFormFromConfig(
config: Record<string, unknown> | null | undefined,
): WebSourceForm {
const c = (config ?? {}) as Partial<WebSourceConfig> & { depth?: number };
return {
urls: (c.urls ?? []).join("\n"),
scope: c.scope ?? WEB_SOURCE_DEFAULTS.scope,
max_pages: c.max_pages ?? WEB_SOURCE_DEFAULTS.max_pages,
max_depth: c.max_depth ?? c.depth ?? WEB_SOURCE_DEFAULTS.max_depth,
include: (c.include ?? []).join("\n"),
exclude: (c.exclude ?? []).join("\n"),
refresh: c.refresh ?? WEB_SOURCE_DEFAULTS.refresh,
use_sitemap: c.use_sitemap ?? WEB_SOURCE_DEFAULTS.use_sitemap,
skip_low_value: c.skip_low_value ?? WEB_SOURCE_DEFAULTS.skip_low_value,
follow_external: c.follow_external ?? WEB_SOURCE_DEFAULTS.follow_external,
max_external_pages:
c.max_external_pages ?? WEB_SOURCE_DEFAULTS.max_external_pages,
feed_content: c.feed_content ?? WEB_SOURCE_DEFAULTS.feed_content,
max_age_days: c.max_age_days != null ? String(c.max_age_days) : "",
attribution: c.attribution ?? "",
licence_url: c.licence_url ?? "",
};
}
/** Form → the body svc-core validates (and normalises) on save. */
export function webConfigFromForm(form: WebSourceForm): WebSourceConfig {
return {
urls: lines(form.urls),
scope: form.scope,
max_pages: form.max_pages,
max_depth: form.max_depth,
include: lines(form.include),
exclude: lines(form.exclude),
refresh: form.refresh,
use_sitemap: form.use_sitemap,
skip_low_value: form.skip_low_value,
follow_external: form.follow_external,
max_external_pages: form.max_external_pages,
// Written every time, so an edit never silently drops what it did not
// show — a window or a credit set when the source was created.
feed_content: form.feed_content,
max_age_days: form.max_age_days.trim() ? Number(form.max_age_days) : null,
attribution: form.attribution.trim() || null,
licence_url: form.licence_url.trim() || null,
};
}
/**
* What the form can tell before svc-core does. svc-core re-validates every
* URL; this only catches the mistakes worth flagging while typing.
*/
export function validateWebForm(
form: WebSourceForm,
): Partial<Record<keyof WebSourceForm, string>> {
const errors: Partial<Record<keyof WebSourceForm, string>> = {};
const urls = lines(form.urls);
if (urls.length === 0) {
errors.urls = "At least one URL is required.";
} else Iif (urls.length > WEB_LIMITS.maxUrls) {
errors.urls = `At most ${WEB_LIMITS.maxUrls} URLs.`;
} else {
const plain = urls.find((u) => u.toLowerCase().startsWith("http://"));
if (plain) errors.urls = `Use https:// — ${plain} cannot be crawled.`;
}
if (
!Number.isInteger(form.max_pages) ||
form.max_pages < 1 ||
form.max_pages > WEB_LIMITS.maxPages
) {
errors.max_pages = `Between 1 and ${WEB_LIMITS.maxPages} pages.`;
}
if (
form.follow_external &&
(!Number.isInteger(form.max_external_pages) ||
form.max_external_pages < 1 ||
form.max_external_pages > WEB_LIMITS.maxExternalPages)
) {
errors.max_external_pages = `Between 1 and ${WEB_LIMITS.maxExternalPages} linked pages.`;
}
if (form.max_age_days.trim()) {
const days = Number(form.max_age_days);
if (!Number.isInteger(days) || days < 1 || days > WEB_LIMITS.maxAgeDays) {
errors.max_age_days = `Whole days, 1 to ${WEB_LIMITS.maxAgeDays} — or blank to keep every page.`;
}
}
Iif (form.attribution.length > WEB_LIMITS.maxAttribution) {
errors.attribution = `At most ${WEB_LIMITS.maxAttribution} characters.`;
}
if (form.licence_url.trim() && !form.licence_url.trim().startsWith("https://")) {
errors.licence_url = "An https:// link.";
}
return errors;
}
// ─── What a website source's pages say about it ─────────────────────────────
/** How long ago a page's text last changed, in buckets a reader scans:
* each bucket's upper bound in days (exclusive), in order. */
export const FRESHNESS_BUCKETS: { key: string; label: string; days: number }[] = [
{ key: "day", label: "Changed in the last day", days: 1 },
{ key: "week", label: "In the last week", days: 7 },
{ key: "month", label: "In the last month", days: 30 },
{ key: "quarter", label: "In the last three months", days: 90 },
{ key: "older", label: "Longer ago", days: Infinity },
];
/** How many of `pages` last changed in each bucket, in bucket order; a page
* never seen to change counts from when it was first stored. Pages that are
* gone are left out: they are no longer what the source holds. */
export function freshnessBuckets(
pages: WebPage[],
now: number = Date.now(),
): { key: string; label: string; count: number }[] {
const counts = new Map(FRESHNESS_BUCKETS.map((b) => [b.key, 0]));
for (const page of pages) {
if (page.status === "gone") continue;
const at = page.last_changed_at ?? page.first_seen_at;
Iif (!at) continue;
const days = (now - parseServerDate(at).getTime()) / 86_400_000;
const bucket = FRESHNESS_BUCKETS.find((b) => days < b.days) ?? FRESHNESS_BUCKETS[4];
counts.set(bucket.key, (counts.get(bucket.key) ?? 0) + 1);
}
return FRESHNESS_BUCKETS.map((b) => ({ key: b.key, label: b.label, count: counts.get(b.key) ?? 0 }));
}
/** The sites a source's pages come from, most pages first (`www.` dropped). */
export function pageHosts(pages: WebPage[], top = 8): { host: string; count: number }[] {
const counts = new Map<string, number>();
for (const page of pages) {
let host: string;
try {
host = new URL(page.url).hostname.replace(/^www\./, "");
} catch {
continue;
}
counts.set(host, (counts.get(host) ?? 0) + 1);
}
return [...counts.entries()]
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
.slice(0, top)
.map(([host, count]) => ({ host, count }));
}
|