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 | 2x 9x 2x 9x 9x 9x 9x 9x 9x 1x 9x 9x 9x 9x 9x 9x 9x 9x 1x 13x 9x 1x 1x 9x 5x 2x 2x 1x 1x 1x 1x 1x 1x 9x 1x 1x 9x 1x 1x 1x 1x 1x 1x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 1x 9x 9x 1x 1x | import DiscoveryProgress from "@/apps/user/pages/knowledge/DiscoveryProgress";
import {
clearGeneration,
loadGeneration,
saveGeneration,
} from "@/apps/user/pages/knowledge/generationStore";
import {
DEFAULT_REFRESH,
DEFAULT_SCOPE,
LinkDraft,
PublisherTermsNote,
SourceLinksEditor,
draftLinks,
keptLinks,
linkError,
runningSearch,
toggledLink,
withLink,
} from "@/apps/user/pages/knowledge/sourceLinks";
import { SectionCard } from "@/components/detail";
import { useWorkspace } from "@/context/WorkspaceContext";
import {
Knowledge,
WebCrawlScope,
WebRefreshCadence,
} from "@/objects/Knowledge";
import {
ApplyFoundSourcesResponse,
FindSourcesStatus,
FoundSource,
} from "@/objects/KnowledgeSetup";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { AlertCircle, Check, Loader2, Search } from "lucide-react";
import React, { useEffect, useState } from "react";
// "Find sources" on an empty website or file source's page: the same
// background search the agent wizard runs for its proposed sources (svc-core
// `source_discovery`), for this one source, from its name and description.
// What it finds is the person's to review; what they keep is added
// (`POST /knowledge/{id}/discover/{run}/apply`) and crawled or imported.
// The run is remembered in this tab (generationStore), so leaving the page
// comes back to it.
/** How often a running search is asked where it is. */
const POLL_MS = 2000;
/** The generationStore entry point for a source's own search. */
const storeMode = (knowledgeId: string) => `find-sources:${knowledgeId}`;
const FindSourcesPanel: React.FC<{ knowledge: Knowledge }> = ({ knowledge }) => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const { key: workspaceKey } = useWorkspace();
const mode = storeMode(knowledge.id);
const isWeb = knowledge.source_type === "web";
const [runId, setRunId] = useState<string | null>(
() => loadGeneration(workspaceKey, mode)?.generationId ?? null,
);
const [starting, setStarting] = useState(false);
const [error, setError] = useState<string | null>(null);
const [links, setLinks] = useState<LinkDraft[] | null>(null);
const [scope, setScope] = useState<WebCrawlScope>(DEFAULT_SCOPE);
const [refresh, setRefresh] = useState<WebRefreshCadence>(DEFAULT_REFRESH);
const [adding, setAdding] = useState(false);
const [added, setAdded] = useState<ApplyFoundSourcesResponse | null>(null);
const { data: run, error: runError } = useQuery<FindSourcesStatus>({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE_SOURCE_SEARCH, runId],
queryFn: () =>
getApiHandler().knowledge_router.sourcesStatus(runId as string),
enabled: !!runId && links === null,
refetchInterval: (query) =>
query.state.data?.status === "running" || !query.state.data
? POLL_MS
: false,
retry: false,
});
const forget = () => {
clearGeneration(workspaceKey, mode);
setRunId(null);
};
useEffect(() => {
if (!runId || links !== null) return;
Iif (runError) {
forget();
setError(linkError(runError, "The search could not be found."));
return;
}
if (!run) return;
if (run.status === "proposed" && run.proposal) {
const mine = run.proposal.sources.find(
(s: FoundSource) => s.key === knowledge.id,
);
setLinks(draftLinks(mine ? [...mine.pages, ...mine.documents] : []));
setScope(mine?.scope ?? DEFAULT_SCOPE);
setRefresh(mine?.refresh ?? DEFAULT_REFRESH);
} else Eif (run.status === "failed" || run.status === "applied") {
forget();
setError(
run.status === "failed"
? (run.error ?? "No sources could be found.")
: null,
);
}
// `forget` only sets state.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [run, runError, runId, links, knowledge.id]);
const follow = (id: string) => {
saveGeneration(workspaceKey, mode, {
generationId: id,
description: knowledge.name,
modelId: "",
});
setRunId(id);
};
const start = async () => {
setStarting(true);
setError(null);
setAdded(null);
try {
follow(
(await getApiHandler().knowledge_router.findSourcesFor(knowledge.id))
.generation_id,
);
} catch (e) {
const running = runningSearch(e);
if (running) follow(running);
else setError(linkError(e, "Could not start the search. Please try again."));
} finally {
setStarting(false);
}
};
const discard = () => {
forget();
setLinks(null);
};
const kept = (links ?? []).filter((l) => l.included).length;
const add = async () => {
Iif (!runId || !links) return;
setAdding(true);
setError(null);
try {
const result = await getApiHandler().knowledge_router.applyFoundSources(
knowledge.id,
runId,
keptLinks(knowledge.source_type, links, scope, refresh),
);
forget();
setLinks(null);
setAdded(result);
// The source (its config), its documents, its crawls and its history.
await Promise.all([
queryClient.invalidateQueries({
predicate: (q) =>
String(q.queryKey[0] ?? "").startsWith(
QUERY_KEYS.GET.KNOWLEDGE_BY_ID,
),
}),
queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE_WEB_CRAWLS, knowledge.id],
}),
queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE_HISTORY],
}),
]);
} catch (e) {
setError(linkError(e, "Could not add those links. Please try again."));
} finally {
setAdding(false);
}
};
const searching = !!runId && links === null;
return (
<SectionCard
icon={Search}
title="Find sources"
description={
isWeb
? "Search the web for the pages this source should crawl, from its name and description."
: "Search the web for the documents this source should hold, from its name and description."
}
>
{error && (
<div className="mb-3 flex items-start gap-2 px-3 py-2 rounded-lg bg-red-50 dark:bg-red-950/40 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-300 text-xs">
<AlertCircle className="size-3.5 shrink-0 mt-0.5" />
{error}
</div>
)}
{added && (
<p className="mb-3 flex items-center gap-1.5 text-xs text-emerald-700 dark:text-emerald-400">
<Check className="size-3.5" />
{isWeb
? added.crawl_started
? "Added — the crawl has started."
: "Added. Refresh the source to crawl them."
: `${added.imports_queued} document${
added.imports_queued === 1 ? " is" : "s are"
} being imported.`}
</p>
)}
{searching ? (
<div className="space-y-3">
<DiscoveryProgress
stage={run?.stage ?? "queued"}
progress={run?.progress ?? {}}
lastStep="Matching them to this source"
/>
<p className="text-xs text-gray-400 dark:text-gray-500">
Usually a minute or two. You can leave this page — the search
carries on, and coming back picks it up.
</p>
</div>
) : links !== null ? (
<div className="space-y-3">
{(run?.proposal?.warnings ?? []).map((warning: string) => (
<p
key={warning}
className="flex items-start gap-1.5 text-xs text-amber-700 dark:text-amber-400"
>
<AlertCircle className="size-3.5 shrink-0 mt-0.5" />
{warning}
</p>
))}
<SourceLinksEditor
sourceType={knowledge.source_type}
links={links}
scope={scope}
refresh={refresh}
onToggle={(url) => setLinks((prev) => toggledLink(prev ?? [], url))}
onAdd={(link) => setLinks((prev) => withLink(prev ?? [], link))}
onScope={setScope}
onRefresh={setRefresh}
/>
<PublisherTermsNote />
<div className="flex justify-end gap-2">
<button
type="button"
onClick={discard}
disabled={adding}
className="px-3 py-1.5 text-xs text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 disabled:opacity-50"
>
Discard
</button>
<button
type="button"
onClick={() => void add()}
disabled={adding || kept === 0}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-xs font-medium hover:opacity-80 disabled:opacity-50"
>
{adding && <Loader2 className="size-3.5 animate-spin" />}
{`Add ${kept} ${isWeb ? "page" : "document"}${kept === 1 ? "" : "s"}`}
</button>
</div>
</div>
) : (
<button
type="button"
onClick={() => void start()}
disabled={starting}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-xs font-medium text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 disabled:opacity-50"
>
{starting ? (
<Loader2 className="size-3.5 animate-spin" />
) : (
<Search className="size-3.5" />
)}
Find sources
</button>
)}
</SectionCard>
);
};
export default FindSourcesPanel;
|