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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | 11x 11x 167x 11x 11x 11x 66x 66x 6x 60x 11x 144x 68x 39x 144x 11x 1x 1x 11x 2x 2x 11x 1x 1x 1x 1x 1x 1x 11x 1x 1x 1x 11x 50x 11x 12x 11x 2x 11x 38x 29x 38x 14x 26x 16x 13x 10x 11x 117x 117x 11x 141x 141x 141x 114x 117x 114x 2x 2x 1x 1x 1x 2x 2x 114x 117x 2x 42x 42x 42x 1x 1x 222x 296x 11x 40x | import {
FieldLabel,
inputBase,
} from "@/apps/user/pages/agents/setup/primitives";
import { knowledgeCategory } from "@/apps/user/pages/knowledge/categories";
import { formatBytes } from "@/apps/user/pages/knowledge/util";
import { WEB_REFRESH, WEB_SCOPES } from "@/apps/user/pages/knowledge/webSource";
import { WebCrawlScope, WebRefreshCadence } from "@/objects/Knowledge";
import { errorDetail } from "@/util/apiError";
import {
ApplyFoundSourcesRequest,
Candidate,
} from "@/objects/KnowledgeSetup";
import { ExternalLink, Plus } from "lucide-react";
import React, { useState } from "react";
// A source's links as a review edits them — the pages a website source
// crawls and the documents a file source imports, each one a search found
// and svc-core checked, or one the person typed. Shared by every place a
// person reviews found links: knowledge generation, the agent wizard's
// Knowledge step, and "Find sources" on a source's page. svc-core
// re-validates whatever is sent (`knowledge/source_links.py`).
/** A link, and whether the person kept it. */
export interface LinkDraft extends Candidate {
included: boolean;
}
/** Which source types take which kinds of link — svc-core's `ACCEPTS`. */
export const ACCEPTS: Record<string, Candidate["kind"][]> = {
web: ["page", "feed"],
file: ["document"],
};
/** Whether a source of this type takes links at all. */
export const takesLinks = (sourceType: string): boolean =>
sourceType in ACCEPTS;
export const DEFAULT_SCOPE: WebCrawlScope = "section";
export const DEFAULT_REFRESH: WebRefreshCadence = "weekly";
/** How often a new website source of `category` is read: the cadence
* chosen, else its kind's — dated knowledge never less often than daily.
* svc-core holds a created source to the same (`web_config.refresh_for`). */
export const refreshFor = (
category: string | null | undefined,
chosen?: WebRefreshCadence | null,
): WebRefreshCadence => {
const kind = knowledgeCategory(category);
if (kind.key === "temporal")
return chosen === "hourly" || chosen === "daily" ? chosen : kind.refresh;
return chosen ?? kind.refresh;
};
export const kindLabel = (link: Candidate): string => {
if (link.kind === "page") return "Page";
if (link.kind === "feed") return "Feed";
const type = (link.content_type ?? "").toLowerCase();
if (type.includes("pdf")) return "PDF";
if (type.includes("word")) return "Word";
if (type.includes("sheet") || type.includes("excel")) return "Spreadsheet";
if (type.includes("presentation")) return "Slides";
return "Document";
};
export const hostOf = (url: string): string => {
try {
return new URL(url).hostname.replace(/^www\./, "");
} catch {
return url;
}
};
export const isHttpsUrl = (value: string): boolean => {
try {
return new URL(value).protocol === "https:";
} catch {
return false;
}
};
/** A failed request's reason, a link's validation message included: a
* refused link comes back in FastAPI's list shape, naming what is wrong
* with it, which `errorDetail` leaves alone. */
export const linkError = (e: unknown, fallback: string): string => {
const detail = (e as { response?: { data?: { detail?: unknown } } })?.response
?.data?.detail;
Eif (Array.isArray(detail)) {
const messages = detail
.map((d) => (d as { msg?: unknown })?.msg)
.filter((m): m is string => typeof m === "string");
Eif (messages.length) return messages.join("; ");
}
return errorDetail(e, fallback);
};
/** The run a 409 names: one is already going for the same thing. */
export const runningSearch = (e: unknown): string | null => {
const response = (
e as { response?: { status?: number; data?: { detail?: unknown } } }
)?.response;
const id = (response?.data?.detail as { generation_id?: unknown })
?.generation_id;
return response?.status === 409 && typeof id === "string" ? id : null;
};
/** Found links, every one kept until the person unticks it. */
export const draftLinks = (found: Candidate[]): LinkDraft[] =>
found.map((link) => ({ ...link, included: true }));
/** `links` with `link` added (kept), unless it is there already. */
export const withLink = (links: LinkDraft[], link: Candidate): LinkDraft[] =>
links.some((l) => l.url === link.url)
? links
: [...links, { ...link, included: true }];
/** `links` with the one at `url` kept or dropped. */
export const toggledLink = (links: LinkDraft[], url: string): LinkDraft[] =>
links.map((l) => (l.url === url ? { ...l, included: !l.included } : l));
/** What a request carries of a source's kept links: a website source's
* URLs, scope and cadence, or a file source's documents — only links of
* the kinds its type takes. */
export const keptLinks = (
sourceType: string,
links: LinkDraft[],
scope: WebCrawlScope,
refresh: WebRefreshCadence,
): ApplyFoundSourcesRequest => {
const kept = links.filter(
(l) => l.included && (ACCEPTS[sourceType] ?? []).includes(l.kind),
);
if (sourceType === "web") {
return { urls: kept.map((l) => l.url), scope, refresh };
}
if (sourceType === "file") {
return {
documents: kept.map((l) => ({
url: l.url,
title: l.title,
publisher: l.publisher ?? "",
})),
};
}
return {};
};
/** One link a source would crawl or import. */
export const LinkRow: React.FC<{
link: LinkDraft;
onToggle: () => void;
}> = ({ link, onToggle }) => {
const label = link.title || link.url;
return (
<div className="flex items-start gap-2.5 py-1.5">
<input
type="checkbox"
checked={link.included}
onChange={onToggle}
aria-label={`Keep ${label}`}
className="mt-0.5 size-3.5 shrink-0 accent-gray-800 dark:accent-gray-200"
/>
<div className="min-w-0 flex-1">
<div
className={
link.included
? "text-xs text-gray-700 dark:text-gray-300 truncate"
: "text-xs text-gray-400 dark:text-gray-500 truncate line-through"
}
>
{label}
</div>
<div className="text-[11px] text-gray-400 dark:text-gray-500 truncate">
{link.host || hostOf(link.url)} · {kindLabel(link)}
{link.size_bytes != null && ` · ${formatBytes(link.size_bytes)}`}
</div>
</div>
<a
href={link.url}
target="_blank"
rel="noopener noreferrer"
aria-label={`Open ${label}`}
className="mt-0.5 shrink-0 text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"
>
<ExternalLink className="size-3.5" />
</a>
</div>
);
};
/** A website or file source's links, with a box to add one and — for a
* website source — how far to crawl and how often. Renders nothing for a
* type that takes no links. */
export const SourceLinksEditor: React.FC<{
sourceType: string;
links: LinkDraft[];
scope: WebCrawlScope;
refresh: WebRefreshCadence;
onToggle: (url: string) => void;
onAdd: (link: Candidate) => void;
onScope: (scope: WebCrawlScope) => void;
onRefresh: (refresh: WebRefreshCadence) => void;
/** Said when there are no links. */
emptyNote?: string;
}> = ({
sourceType,
links,
scope,
refresh,
onToggle,
onAdd,
onScope,
onRefresh,
emptyNote = "Nothing the search found fitted this source.",
}) => {
const [adding, setAdding] = useState("");
const [addError, setAddError] = useState<string | null>(null);
if (!takesLinks(sourceType)) return null;
const isWeb = sourceType === "web";
// Only what this type takes: a source whose type was changed keeps the
// rest, unshown, in case it is changed back.
const shown = links.filter((l) => ACCEPTS[sourceType].includes(l.kind));
const add = () => {
const url = adding.trim();
if (!isHttpsUrl(url)) {
setAddError("Use a full https:// address.");
return;
}
onAdd({
kind: isWeb ? "page" : "document",
url,
title: "",
host: hostOf(url),
snippet: "",
});
setAdding("");
setAddError(null);
};
return (
<>
<div>
<FieldLabel
label={isWeb ? "Pages to crawl" : "Documents to import"}
description={
isWeb
? "The crawl starts from these."
: "Downloaded from these links and indexed."
}
/>
{shown.length > 0 ? (
<div className="divide-y divide-gray-100 dark:divide-gray-700">
{shown.map((link) => (
<LinkRow
key={link.url}
link={link}
onToggle={() => onToggle(link.url)}
/>
))}
</div>
) : (
<p className="text-xs text-gray-400 dark:text-gray-500">{emptyNote}</p>
)}
<div className="mt-2 flex items-center gap-2">
<input
type="url"
value={adding}
onChange={(e) => {
setAdding(e.target.value);
setAddError(null);
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
add();
}
}}
placeholder={
isWeb
? "https://example.gov.uk/guidance"
: "https://example.gov.uk/policy.pdf"
}
aria-label={isWeb ? "Add a URL" : "Add a document link"}
className={inputBase + " flex-1"}
/>
<button
type="button"
onClick={add}
disabled={!adding.trim()}
className="flex items-center gap-1 px-3 py-2 rounded-lg border border-gray-200 dark:border-gray-600 text-xs text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 disabled:opacity-50"
>
<Plus className="size-3.5" />
Add
</button>
</div>
{addError && (
<p className="mt-1 text-xs text-red-600 dark:text-red-400">
{addError}
</p>
)}
</div>
{isWeb && (
<div className="grid grid-cols-2 gap-3">
<div>
<FieldLabel label="How far to crawl" />
<select
value={scope}
onChange={(e) => onScope(e.target.value as WebCrawlScope)}
className={inputBase}
>
{WEB_SCOPES.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</div>
<div>
<FieldLabel label="Keep current" />
<select
value={refresh}
onChange={(e) => onRefresh(e.target.value as WebRefreshCadence)}
className={inputBase}
>
{WEB_REFRESH.map((cadence) => (
<option key={cadence.value} value={cadence.value}>
{cadence.label}
</option>
))}
</select>
</div>
</div>
)}
</>
);
};
/** The publisher-terms note shown wherever found links are reviewed. */
export const PublisherTermsNote: React.FC = () => (
<p className="text-xs text-gray-400 dark:text-gray-500 leading-relaxed">
Pages and documents come from their publishers, under their terms. Check
that each one may be used as you intend before your agents rely on it.
</p>
);
|