-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource-intake-service.ts
More file actions
70 lines (62 loc) · 2.72 KB
/
Copy pathsource-intake-service.ts
File metadata and controls
70 lines (62 loc) · 2.72 KB
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
import type { SourceCandidate, SourceIntakeResult } from "@/types/source-intake";
import { resolveSourceCacheForUrl } from "@/server/analysis/source-cache-service";
import { extractUrls } from "@/server/analysis/source-intake/extract-urls";
import { resolveSourceDiscoveryProvider } from "@/server/analysis/source-discovery/resolve-source-discovery-provider";
import { DEFAULT_DISCOVERY_MAX_RESULTS, DEFAULT_SOURCE_CANDIDATE_MAX_RESULTS } from "@/server/analysis/source-discovery/source-discovery-service";
export async function buildSourceIntakeFromQuestion(question: string): Promise<SourceIntakeResult> {
const manualUrls = extractUrls(question);
const discoveryProvider = resolveSourceDiscoveryProvider();
const ignoredUrls: SourceIntakeResult["ignoredUrls"] = [];
let discoveredUrls: string[] = [];
if (discoveryProvider.id !== "disabled") {
const discovery = await discoveryProvider.discoverSources({
researchTopic: question,
maxResults: DEFAULT_DISCOVERY_MAX_RESULTS,
});
if (discovery.kind === "failure") {
ignoredUrls.push({ url: "[source_discovery]", reason: discovery.errorMessage });
} else {
discoveredUrls = discovery.candidates.map((candidate) => candidate.url);
}
}
const merged = [
...manualUrls.map((url) => ({ url, origin: "manual_url" as const })),
...discoveredUrls.map((url) => ({ url, origin: "discovered" as const })),
];
const candidates: SourceCandidate[] = [];
const seen = new Set<string>();
for (const item of merged) {
let result;
try {
result = await resolveSourceCacheForUrl(item.url);
} catch (error) {
ignoredUrls.push({
url: item.url,
reason: error instanceof Error ? error.message : String(error),
});
continue;
}
if (result.kind === "invalid") {
ignoredUrls.push({ url: item.url, reason: result.errorMessage });
continue;
}
if (seen.has(result.normalizedUrl)) continue;
seen.add(result.normalizedUrl);
candidates.push({
normalizedUrl: result.normalizedUrl,
originalUrl: result.originalUrl,
finalUrl: result.finalUrl,
label: result.finalUrl ? new URL(result.finalUrl).hostname : new URL(result.normalizedUrl).hostname,
excerpt: result.excerpt,
contentType: result.contentType,
httpStatus: result.httpStatus,
fetchedAt: result.checkedAt,
sourceCacheEntryId: result.sourceCacheEntryId,
sourceFetchSnapshotId: result.sourceFetchSnapshotId,
fetchErrorMessage: result.verificationStatus !== "verified" ? `verification_status:${result.verificationStatus}` : null,
origin: item.origin,
});
if (candidates.length >= DEFAULT_SOURCE_CANDIDATE_MAX_RESULTS) break;
}
return { candidates, ignoredUrls };
}