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 | 3x 3x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 3x 30x 18x 21x 21x 3x 3x 7x 3x 9x 9x 9x 9x 4x 25x 9x 9x 36x 9x 23x 9x 9x 24x 2x 7x 1x | import {
MetaGrid,
MetaItem,
Modal,
RUN_STATUS,
SkeletonLines,
StatusBadge,
} from "@/components/detail";
import {
KnowledgeCrawl,
WebCrawlPage,
WebCrawlPages,
WebPageVersion,
} from "@/objects/Knowledge";
import { formatUsd } from "@/components/cost/format";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { parseServerDate } from "@/util/time";
import { useQuery } from "@tanstack/react-query";
import { FileClock, GitCompare, History } from "lucide-react";
import React, { useState } from "react";
import {
CRAWL_WRITTEN,
CRAWL_WRITTEN_LABEL,
PAGE_SINCE,
PAGE_SINCE_LABEL,
WEB_PAGE_STATUS,
exactly,
when,
} from "./webSource";
export const TRIGGER_LABEL: Record<string, string> = {
created: "First crawl",
updated: "Settings changed",
manual: "Refreshed by hand",
scheduled: "Scheduled",
granted: "Granted to an agent",
};
/** A crawl's outcome in one line: what it found, and what indexing cost. */
export const CrawlCounts: React.FC<{ crawl: KnowledgeCrawl }> = ({ crawl }) => {
const parts: string[] = [];
Iif (crawl.pages_new) parts.push(`${crawl.pages_new} new`);
Eif (crawl.pages_changed) parts.push(`${crawl.pages_changed} changed`);
Eif (crawl.pages_unchanged) parts.push(`${crawl.pages_unchanged} unchanged`);
Iif (crawl.pages_gone) parts.push(`${crawl.pages_gone} gone`);
Iif (crawl.pages_expired) parts.push(`${crawl.pages_expired} aged out`);
Iif (crawl.pages_failed) parts.push(`${crawl.pages_failed} not read`);
const cost = crawl.usage?.estimated_cost_usd;
Iif (cost != null && cost > 0) parts.push(`${formatUsd(cost)} to index`);
return (
<span className="text-xs text-gray-500 dark:text-gray-400">
{crawl.status === "running"
? `${crawl.pages_fetched} page(s) read so far`
: parts.join(" · ") || "No pages read"}
</span>
);
};
/** Where the page history opens: which snapshot, and what to diff it with. */
export interface PageHistoryTarget {
page: { document_id: string; url: string; title?: string | null };
/** The snapshot selected on opening; the newest when absent. */
versionId?: string;
/** A version number to compare the selected snapshot with, instead of the
* one before it. */
compareWith?: number;
}
type Filter = "stored" | "changed" | "added" | "then";
const FILTERS: {
value: Filter;
label: string;
matches: (page: WebCrawlPage) => boolean;
}[] = [
{ value: "stored", label: "Stored by this crawl", matches: (p) => !!p.written },
{ value: "changed", label: "Changed since", matches: (p) => p.since === "changed" },
{ value: "added", label: "Added since", matches: (p) => p.since === "added" },
{ value: "then", label: "All pages then", matches: (p) => !!p.then },
];
const linkButton =
"flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors";
const versionLabel = (v?: WebPageVersion | null) =>
v ? `Version ${v.version_number}` : "Not stored yet";
/**
* A website source as one crawl left it, beside how it is now.
*
* A page gets a new version only when its text changes, so the pages list
* (each page's newest version) cannot say what the source held at an earlier
* crawl. Here every page shows the version in force after the crawl and its
* newest, and opens its history on either, compared with the other.
*/
const WebCrawlModal: React.FC<{
knowledgeId: string;
crawl: KnowledgeCrawl;
onOpenPage: (target: PageHistoryTarget) => void;
onClose: () => void;
}> = ({ knowledgeId, crawl, onOpenPage, onClose }) => {
const { getApiHandler } = useApiHandler();
const api = getApiHandler().knowledge_router;
const [filter, setFilter] = useState<Filter | null>(null);
const { data, isLoading, isError } = useQuery<WebCrawlPages | null>({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE_WEB_CRAWL_PAGES, knowledgeId, crawl.id],
queryFn: () => api.getWebCrawlPages(knowledgeId, crawl.id),
// A crawl still running keeps storing pages; follow it until it ends.
refetchInterval: (query) =>
(query.state.data as WebCrawlPages | null | undefined)?.crawl.status ===
"running"
? 3000
: false,
});
const pages: WebCrawlPage[] = data?.pages ?? [];
const counts = Object.fromEntries(
FILTERS.map((f) => [f.value, pages.filter(f.matches).length]),
) as Record<Filter, number>;
// Open on what the crawl stored; a crawl that stored nothing on the pages
// it held.
const active: Filter = filter ?? (counts.stored ? "stored" : "then");
const shown = pages.filter(FILTERS.find((f) => f.value === active)!.matches);
const started = parseServerDate(crawl.started_at).toLocaleString();
return (
<Modal
title={`Crawl of ${started}`}
icon={History}
size="xl"
onClose={onClose}
description={
<span className="flex flex-wrap items-center gap-2">
<StatusBadge status={crawl.status} scale={RUN_STATUS} size="xs" />
<span>{TRIGGER_LABEL[crawl.trigger] ?? crawl.trigger}</span>
<CrawlCounts crawl={crawl} />
</span>
}
>
{isLoading ? (
<SkeletonLines count={4} />
) : isError || !data ? (
<p className="text-sm text-red-500">Could not load this crawl's pages.</p>
) : (
<div className="space-y-4">
<p className="text-xs text-gray-500 dark:text-gray-400">
Each page as this crawl left it, beside its version now. A page
gets a new version only when its text changes, so a page this
crawl did not store is shown at the version before it.
</p>
<MetaGrid>
<MetaItem label="Pages then" value={String(counts.then)} />
<MetaItem label="Stored by this crawl" value={String(counts.stored)} />
<MetaItem label="Changed since" value={String(counts.changed)} />
<MetaItem label="Added since" value={String(counts.added)} />
</MetaGrid>
{data.expired_since > 0 && (
<p className="text-xs text-gray-500 dark:text-gray-400">
{data.expired_since} page(s) have aged out of the source's window
since, and were removed with their versions.
</p>
)}
<div
role="tablist"
aria-label="Pages to show"
className="inline-flex flex-wrap rounded-lg border border-gray-200 dark:border-gray-600 p-0.5"
>
{FILTERS.map((f) => (
<button
key={f.value}
role="tab"
type="button"
aria-selected={active === f.value}
onClick={() => setFilter(f.value)}
className={`px-2.5 py-1 text-xs rounded-md transition-colors ${
active === f.value
? "bg-gray-900 text-white dark:bg-gray-100 dark:text-gray-900"
: "text-gray-600 dark:text-gray-300"
}`}
>
{f.label} ({counts[f.value]})
</button>
))}
</div>
{shown.length === 0 ? (
<p className="text-sm text-gray-400 dark:text-gray-500 italic">
No pages here.
</p>
) : (
<ul className="divide-y divide-gray-100 dark:divide-gray-700 rounded-lg border border-gray-200 dark:border-gray-700">
{shown.map((page) => (
<li key={page.document_id} className="space-y-1.5 px-3 py-2.5">
<div className="min-w-0">
<div className="truncate text-sm font-medium text-gray-800 dark:text-gray-200">
{page.title || page.url}
</div>
<div className="truncate text-xs text-gray-400 dark:text-gray-500">
{page.url}
</div>
<div
className="text-xs text-gray-500 dark:text-gray-400"
title={exactly(page.then?.fetched_at ?? page.now.fetched_at)}
>
{page.then
? `${versionLabel(page.then)} then, captured ${when(page.then.fetched_at)}`
: versionLabel(page.then)}
{page.since !== "unchanged" &&
` · Version ${page.now.version_number} now`}
</div>
</div>
<div className="flex flex-wrap items-center gap-1.5">
{page.written && (
<StatusBadge
status={page.written}
scale={CRAWL_WRITTEN}
label={CRAWL_WRITTEN_LABEL[page.written]}
/>
)}
{page.since !== "unchanged" && (
<StatusBadge
status={page.since}
scale={PAGE_SINCE}
label={PAGE_SINCE_LABEL[page.since]}
/>
)}
{page.status !== "active" && (
<StatusBadge status={page.status} scale={WEB_PAGE_STATUS} />
)}
<div className="ml-auto flex flex-wrap gap-1.5">
<button
type="button"
className={linkButton}
aria-label={`View ${page.url} as this crawl left it`}
onClick={() =>
onOpenPage({
page,
versionId: (page.then ?? page.now).id,
})
}
>
<FileClock className="size-3.5" />
{page.then ? "View then" : "View"}
</button>
{page.since === "changed" && page.then && (
<button
type="button"
className={linkButton}
aria-label={`Compare ${page.url} with its current version`}
onClick={() =>
onOpenPage({
page,
versionId: page.then!.id,
compareWith: page.now.version_number,
})
}
>
<GitCompare className="size-3.5" />
Compare with current
</button>
)}
</div>
</div>
</li>
))}
</ul>
)}
</div>
)}
</Modal>
);
};
export default WebCrawlModal;
|