@abhishek944/pi-web-access 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ASTACK.md +23 -0
- package/CHANGELOG.md +1289 -0
- package/LICENSE +21 -0
- package/README.md +1215 -2
- package/SECURITY.md +5 -0
- package/abortable.ts +17 -0
- package/activity.ts +101 -0
- package/anysearch.ts +188 -0
- package/auth-fetch.ts +152 -0
- package/baizhi.ts +124 -0
- package/banner.png +0 -0
- package/bocha.ts +203 -0
- package/brave-rate-limit.ts +176 -0
- package/brave.ts +247 -0
- package/brightdata-unlocker.ts +272 -0
- package/ceramic.ts +220 -0
- package/chrome-cookies.ts +752 -0
- package/cloudflare-search.ts +237 -0
- package/content-find.ts +255 -0
- package/crawl4ai.ts +196 -0
- package/credential-source.ts +242 -0
- package/curator-page.ts +3607 -0
- package/curator-run.ts +45 -0
- package/curator-server.ts +761 -0
- package/custom-search.ts +226 -0
- package/data-uri-sanitize.ts +406 -0
- package/datalab-pdf-extract.ts +568 -0
- package/declared-web-links.ts +173 -0
- package/deepseek.ts +377 -0
- package/degoog.ts +323 -0
- package/dist/index.js +28286 -0
- package/dist/mcp-cli.js +29186 -0
- package/dist/pi-mcp.js +20855 -0
- package/dist/types/abortable.d.ts +2 -0
- package/dist/types/activity.d.ts +33 -0
- package/dist/types/anysearch.d.ts +7 -0
- package/dist/types/auth-fetch.d.ts +14 -0
- package/dist/types/baizhi.d.ts +3 -0
- package/dist/types/bocha.d.ts +3 -0
- package/dist/types/brave-rate-limit.d.ts +35 -0
- package/dist/types/brave.d.ts +3 -0
- package/dist/types/brightdata-unlocker.d.ts +11 -0
- package/dist/types/ceramic.d.ts +3 -0
- package/dist/types/chrome-cookies.d.ts +44 -0
- package/dist/types/cloudflare-search.d.ts +4 -0
- package/dist/types/content-find.d.ts +10 -0
- package/dist/types/crawl4ai.d.ts +8 -0
- package/dist/types/credential-source.d.ts +29 -0
- package/dist/types/curator-page.d.ts +5 -0
- package/dist/types/curator-run.d.ts +10 -0
- package/dist/types/curator-server.d.ts +72 -0
- package/dist/types/custom-search.d.ts +22 -0
- package/dist/types/data-uri-sanitize.d.ts +20 -0
- package/dist/types/datalab-pdf-extract.d.ts +59 -0
- package/dist/types/declared-web-links.d.ts +7 -0
- package/dist/types/deepseek.d.ts +13 -0
- package/dist/types/degoog.d.ts +3 -0
- package/dist/types/domain-filter-normalization.d.ts +9 -0
- package/dist/types/duckduckgo-rate-limit.d.ts +52 -0
- package/dist/types/duckduckgo.d.ts +6 -0
- package/dist/types/endpoint-pdf-extract.d.ts +22 -0
- package/dist/types/exa.d.ts +17 -0
- package/dist/types/extract.d.ts +70 -0
- package/dist/types/feature-config.d.ts +2 -0
- package/dist/types/fetch-params.d.ts +39 -0
- package/dist/types/field-extract.d.ts +37 -0
- package/dist/types/firecrawl.d.ts +18 -0
- package/dist/types/gemini-adc.d.ts +9 -0
- package/dist/types/gemini-api.d.ts +37 -0
- package/dist/types/gemini-pdf-extract.d.ts +8 -0
- package/dist/types/gemini-search.d.ts +76 -0
- package/dist/types/gemini-url-context.d.ts +3 -0
- package/dist/types/gemini-web-config.d.ts +25 -0
- package/dist/types/gemini-web.d.ts +31 -0
- package/dist/types/github-api.d.ts +6 -0
- package/dist/types/github-extract.d.ts +19 -0
- package/dist/types/github-issue-pr.d.ts +28 -0
- package/dist/types/index.d.ts +53 -0
- package/dist/types/jina-search.d.ts +7 -0
- package/dist/types/kagi.d.ts +14 -0
- package/dist/types/keenable.d.ts +3 -0
- package/dist/types/kimi-search.d.ts +4 -0
- package/dist/types/linkup.d.ts +5 -0
- package/dist/types/mcp-cli.d.ts +2 -0
- package/dist/types/mcp-server.d.ts +10 -0
- package/dist/types/mcp-tool-definitions.d.ts +16 -0
- package/dist/types/mistral-search.d.ts +3 -0
- package/dist/types/ninerouter.d.ts +3 -0
- package/dist/types/offtopic-judge.d.ts +26 -0
- package/dist/types/ollama.d.ts +14 -0
- package/dist/types/openai-search.d.ts +23 -0
- package/dist/types/page-query.d.ts +22 -0
- package/dist/types/parallel-mcp.d.ts +10 -0
- package/dist/types/parallel.d.ts +13 -0
- package/dist/types/pdf-extract.d.ts +44 -0
- package/dist/types/perplexity.d.ts +26 -0
- package/dist/types/pi-mcp.d.ts +56 -0
- package/dist/types/pi-session-results.d.ts +8 -0
- package/dist/types/querit.d.ts +9 -0
- package/dist/types/query-rewrite.d.ts +2 -0
- package/dist/types/render-search-error.d.ts +56 -0
- package/dist/types/retrieval-only.d.ts +78 -0
- package/dist/types/rsc-extract.d.ts +11 -0
- package/dist/types/search-answer-formatting.d.ts +7 -0
- package/dist/types/search-result-count-normalization.d.ts +1 -0
- package/dist/types/searchinfinity.d.ts +7 -0
- package/dist/types/searxng.d.ts +3 -0
- package/dist/types/source-check.d.ts +102 -0
- package/dist/types/ssrf-protection.d.ts +66 -0
- package/dist/types/storage.d.ts +69 -0
- package/dist/types/summary-model-scope.d.ts +33 -0
- package/dist/types/summary-review.d.ts +30 -0
- package/dist/types/tabstack.d.ts +5 -0
- package/dist/types/tavily.d.ts +7 -0
- package/dist/types/tinyfish.d.ts +10 -0
- package/dist/types/tool-activation.d.ts +7 -0
- package/dist/types/tool-arguments.d.ts +1 -0
- package/dist/types/upgrade-notice.d.ts +6 -0
- package/dist/types/utils.d.ts +55 -0
- package/dist/types/valyu.d.ts +5 -0
- package/dist/types/video-extract.d.ts +15 -0
- package/dist/types/web-tool-contract.d.ts +98 -0
- package/dist/types/web-tool-core.d.ts +227 -0
- package/dist/types/xai-search.d.ts +12 -0
- package/dist/types/you.d.ts +3 -0
- package/dist/types/youtube-extract.d.ts +29 -0
- package/dist/types/zai.d.ts +3 -0
- package/docs/pi-mcp.md +41 -0
- package/domain-filter-normalization.ts +44 -0
- package/duckduckgo-rate-limit.ts +205 -0
- package/duckduckgo.ts +123 -0
- package/endpoint-pdf-extract.ts +225 -0
- package/exa.ts +620 -0
- package/extract.ts +1721 -0
- package/feature-config.ts +29 -0
- package/fetch-params.ts +147 -0
- package/field-extract.ts +347 -0
- package/firecrawl.ts +476 -0
- package/gemini-adc.ts +345 -0
- package/gemini-api.ts +368 -0
- package/gemini-pdf-extract.ts +108 -0
- package/gemini-search.ts +1162 -0
- package/gemini-url-context.ts +128 -0
- package/gemini-web-config.ts +131 -0
- package/gemini-web.ts +527 -0
- package/github-api.ts +197 -0
- package/github-extract.ts +1064 -0
- package/github-issue-pr.ts +717 -0
- package/index.ts +2819 -0
- package/jina-search.ts +275 -0
- package/kagi.ts +257 -0
- package/keenable.ts +222 -0
- package/kimi-search.ts +209 -0
- package/linkup.ts +170 -0
- package/mcp-cli.ts +40 -0
- package/mcp-server.ts +84 -0
- package/mcp-tool-definitions.ts +143 -0
- package/mistral-search.ts +268 -0
- package/ninerouter.ts +92 -0
- package/offtopic-judge.ts +72 -0
- package/ollama.ts +237 -0
- package/openai-search.ts +855 -0
- package/package.json +108 -4
- package/page-query.ts +185 -0
- package/parallel-mcp.ts +255 -0
- package/parallel.ts +373 -0
- package/pdf-extract.ts +447 -0
- package/perplexity.ts +245 -0
- package/pi-mcp.ts +139 -0
- package/pi-session-results.ts +40 -0
- package/pi-web-fetch-demo.mp4 +0 -0
- package/promise-try.d.ts +7 -0
- package/querit.ts +394 -0
- package/query-rewrite.ts +84 -0
- package/render-search-error.ts +170 -0
- package/retrieval-only.ts +102 -0
- package/rsc-extract.ts +338 -0
- package/search-answer-formatting.ts +11 -0
- package/search-result-count-normalization.ts +4 -0
- package/searchinfinity.ts +246 -0
- package/searxng.ts +236 -0
- package/source-check.ts +269 -0
- package/ssrf-protection.ts +602 -0
- package/storage.ts +547 -0
- package/summary-model-scope.ts +72 -0
- package/summary-review.ts +476 -0
- package/tabstack.ts +289 -0
- package/tavily.ts +257 -0
- package/tinyfish.ts +373 -0
- package/tool-activation.ts +169 -0
- package/tool-arguments.ts +23 -0
- package/upgrade-notice.ts +47 -0
- package/utils.ts +693 -0
- package/valyu.ts +177 -0
- package/video-extract.ts +398 -0
- package/web-tool-contract.ts +93 -0
- package/web-tool-core.ts +1810 -0
- package/xai-search.ts +443 -0
- package/you.ts +165 -0
- package/youtube-extract.ts +336 -0
- package/zai.ts +161 -0
package/source-check.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Structured source evidence and machine-readable research artifacts.
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { generateId, getResult, storeResult } from "./storage.ts";
|
|
4
|
+
import type { SearchResult } from "./perplexity.ts";
|
|
5
|
+
import type { ExtractedContent } from "./extract.ts";
|
|
6
|
+
|
|
7
|
+
export type SourceQuality = "official_docs" | "vendor_docs" | "repo_issue" | "blog" | "forum" | "news" | "unknown";
|
|
8
|
+
export type ClaimStatus = "supported" | "contradicted" | "unclear" | "missing-evidence";
|
|
9
|
+
export type RecencyFilter = "day" | "week" | "month" | "year";
|
|
10
|
+
|
|
11
|
+
export interface ResearchSource {
|
|
12
|
+
rank: number;
|
|
13
|
+
url: string;
|
|
14
|
+
title: string;
|
|
15
|
+
snippet?: string;
|
|
16
|
+
/** "generated" when the snippet is the search provider's own answer text around a citation, not page text. */
|
|
17
|
+
snippet_source?: "generated";
|
|
18
|
+
fetch_timestamp?: number;
|
|
19
|
+
content_hash?: string;
|
|
20
|
+
quality: SourceQuality;
|
|
21
|
+
fetched?: boolean;
|
|
22
|
+
fetch_error?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ResearchPassage {
|
|
26
|
+
passage_id: string;
|
|
27
|
+
source_url: string;
|
|
28
|
+
source_rank: number;
|
|
29
|
+
text: string;
|
|
30
|
+
extraction_span?: { start: number; end: number };
|
|
31
|
+
content_hash?: string;
|
|
32
|
+
/** "provider_summary" marks text written by the search provider (for example OpenAI or xAI answer text), not quoted from the page. */
|
|
33
|
+
origin?: "provider_summary";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ClaimAssessment {
|
|
37
|
+
claim: string;
|
|
38
|
+
status: ClaimStatus;
|
|
39
|
+
supporting_passages: string[];
|
|
40
|
+
contradicting_passages: string[];
|
|
41
|
+
rationale: string;
|
|
42
|
+
confidence: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ResearchArtifact {
|
|
46
|
+
id: string;
|
|
47
|
+
type: "research";
|
|
48
|
+
timestamp: number;
|
|
49
|
+
query: string;
|
|
50
|
+
sources: ResearchSource[];
|
|
51
|
+
passages: ResearchPassage[];
|
|
52
|
+
claims?: ClaimAssessment[];
|
|
53
|
+
provider?: string;
|
|
54
|
+
summary?: string;
|
|
55
|
+
content_hash?: string;
|
|
56
|
+
filters?: { recency?: RecencyFilter; domain_include?: string[]; domain_exclude?: string[] };
|
|
57
|
+
errors?: Array<{ query: string; error: string }>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ResearchSearchRequest {
|
|
61
|
+
query: string;
|
|
62
|
+
numResults: number;
|
|
63
|
+
recencyFilter?: RecencyFilter;
|
|
64
|
+
domainFilter?: string[];
|
|
65
|
+
}
|
|
66
|
+
export interface ResearchSearchResult { url: string; title: string; snippet: string; rank: number }
|
|
67
|
+
export interface ResearchSearchResponse { provider: string; results: ResearchSearchResult[]; summary?: string }
|
|
68
|
+
export interface ResearchProvider { name: string; search(req: ResearchSearchRequest): Promise<ResearchSearchResponse> }
|
|
69
|
+
|
|
70
|
+
const OFFICIAL_DOCS_HOSTS = /^(developers\.|docs\.|learn\.|reference\.)|\.github\.io$/i;
|
|
71
|
+
const OFFICIAL_DOCS_PATHS = /\/(docs?|reference)(\/|\b)/i;
|
|
72
|
+
const VENDOR_DOCS_PATHS = /\/(documentation|docs?)\//i;
|
|
73
|
+
const REPO_ISSUE_PATHS = /\/(issues|pull|pulls)\//i;
|
|
74
|
+
const BLOG_HOSTS = /(medium\.com|substack\.com|dev\.to|hashnode\.)/i;
|
|
75
|
+
const BLOG_PATHS = /\/blogs?\//i;
|
|
76
|
+
const FORUM_HOSTS = /(stackoverflow\.com|serverfault\.com|superuser\.com|discourse\.|community\.)/i;
|
|
77
|
+
const FORUM_PATHS = /\/(forum|forums|threads)\//i;
|
|
78
|
+
const NEWS_HOSTS = /(reuters\.com|bloomberg\.com|techcrunch\.com|theverge\.com|arstechnica\.com|wired\.com|cnet\.com|zdnet\.com)/i;
|
|
79
|
+
const NEWS_PATHS = /\/news(\/|$)/i;
|
|
80
|
+
|
|
81
|
+
export function classifySource(url: string): SourceQuality {
|
|
82
|
+
let host = "";
|
|
83
|
+
let path = "";
|
|
84
|
+
try {
|
|
85
|
+
const parsed = new URL(url);
|
|
86
|
+
host = parsed.hostname;
|
|
87
|
+
path = parsed.pathname;
|
|
88
|
+
} catch {
|
|
89
|
+
return "unknown";
|
|
90
|
+
}
|
|
91
|
+
if (REPO_ISSUE_PATHS.test(path)) return "repo_issue";
|
|
92
|
+
if (OFFICIAL_DOCS_HOSTS.test(host) || OFFICIAL_DOCS_PATHS.test(path)) return "official_docs";
|
|
93
|
+
if (VENDOR_DOCS_PATHS.test(path)) return "vendor_docs";
|
|
94
|
+
if (NEWS_HOSTS.test(host) || NEWS_PATHS.test(path)) return "news";
|
|
95
|
+
if (FORUM_HOSTS.test(host) || FORUM_PATHS.test(path)) return "forum";
|
|
96
|
+
if (BLOG_HOSTS.test(host) || BLOG_PATHS.test(path)) return "blog";
|
|
97
|
+
return "unknown";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function hashContent(text: string): string {
|
|
101
|
+
return `sha256:${createHash("sha256").update(text, "utf8").digest("hex")}`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface Span { text: string; start: number; end: number }
|
|
105
|
+
|
|
106
|
+
function tokenize(value: string): string[] {
|
|
107
|
+
// Short words are mostly noise ("the", "for"), but short acronyms and numbers ("API", "TLS", "429") carry meaning.
|
|
108
|
+
return [...new Set(value.split(/[^A-Za-z0-9]+/)
|
|
109
|
+
.filter((term) => term.length > 3 || (term.length > 1 && /^[A-Z0-9]+$/.test(term)))
|
|
110
|
+
.map((term) => term.length > 3 ? term.toLowerCase() : term))];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Long terms match inside words in any case ("stream" in "Streaming"); short acronyms only as whole words in the same case, so "API" skips "rapid" and "US" skips "us". */
|
|
114
|
+
function countMatches(terms: string[], text: string): number {
|
|
115
|
+
const lower = text.toLowerCase();
|
|
116
|
+
const words = new Set(text.split(/[^A-Za-z0-9]+/));
|
|
117
|
+
return terms.filter((term) => term.length > 3 ? lower.includes(term) : words.has(term)).length;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function extractRelevantSpans(content: string, claim: string, snippet: string): Span[] {
|
|
121
|
+
const sentences: Span[] = [];
|
|
122
|
+
// A sentence ends at punctuation (plus closing quotes, brackets, emphasis or a [n] citation) followed by
|
|
123
|
+
// whitespace or the end, so "example.com" stays inside its sentence. The lookbehind starts each attempt only at
|
|
124
|
+
// the beginning of a punctuation run, which keeps the scan linear on long runs such as "!!!…x".
|
|
125
|
+
const boundary = /(?<![.!?])[.!?]+(?:["'”’)\]*_]|\[\d+\])*(?=\s|$)/g;
|
|
126
|
+
let segmentStart = 0;
|
|
127
|
+
const addSentence = (end: number) => {
|
|
128
|
+
const raw = content.slice(segmentStart, end);
|
|
129
|
+
const text = raw.trim();
|
|
130
|
+
if (text.length > 0 && text.length <= 400) {
|
|
131
|
+
const start = segmentStart + raw.indexOf(text);
|
|
132
|
+
sentences.push({ text, start, end: start + text.length });
|
|
133
|
+
}
|
|
134
|
+
segmentStart = end;
|
|
135
|
+
};
|
|
136
|
+
for (const match of content.matchAll(boundary)) addSentence(match.index + match[0].length);
|
|
137
|
+
addSentence(content.length);
|
|
138
|
+
const claimTerms = tokenize(claim);
|
|
139
|
+
const snippetTerms = tokenize(snippet);
|
|
140
|
+
return sentences
|
|
141
|
+
.map((sentence, index) => ({ sentence, index, claim: countMatches(claimTerms, sentence.text), snippet: countMatches(snippetTerms, sentence.text) }))
|
|
142
|
+
.filter((item) => item.claim + item.snippet > 0)
|
|
143
|
+
.sort((a, b) => b.claim - a.claim || b.snippet - a.snippet || a.index - b.index)
|
|
144
|
+
.slice(0, 3)
|
|
145
|
+
.map(({ sentence }) => sentence);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function passageId(sourceRank: number, index: number): string {
|
|
149
|
+
return `p-${sourceRank}-${index}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function buildPassages(sources: ResearchSource[], fetched: ExtractedContent[] = [], claim = ""): ResearchPassage[] {
|
|
153
|
+
const passages: ResearchPassage[] = [];
|
|
154
|
+
const fetchedByUrl = new Map(fetched.map((item) => [item.url, item]));
|
|
155
|
+
for (const source of sources) {
|
|
156
|
+
if (source.snippet) {
|
|
157
|
+
passages.push({
|
|
158
|
+
passage_id: passageId(source.rank, 0),
|
|
159
|
+
source_url: source.url,
|
|
160
|
+
source_rank: source.rank,
|
|
161
|
+
text: source.snippet,
|
|
162
|
+
content_hash: hashContent(source.snippet),
|
|
163
|
+
// The provider's paraphrase around a citation, not a quote, so it is never presented as page text.
|
|
164
|
+
...(source.snippet_source === "generated" ? { origin: "provider_summary" as const } : {}),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const page = fetchedByUrl.get(source.url);
|
|
168
|
+
if (page && !page.error && page.content) {
|
|
169
|
+
for (const [index, span] of extractRelevantSpans(page.content, claim, source.snippet ?? "").entries()) {
|
|
170
|
+
passages.push({
|
|
171
|
+
passage_id: passageId(source.rank, index + 1),
|
|
172
|
+
source_url: source.url,
|
|
173
|
+
source_rank: source.rank,
|
|
174
|
+
text: span.text,
|
|
175
|
+
extraction_span: { start: span.start, end: span.end },
|
|
176
|
+
content_hash: hashContent(span.text),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return passages;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function assessClaim(claim: string, passages: ResearchPassage[]): ClaimAssessment {
|
|
185
|
+
if (passages.length === 0) {
|
|
186
|
+
return { claim, status: "missing-evidence", supporting_passages: [], contradicting_passages: [], rationale: "No passages were retrieved for the claim.", confidence: 0.2 };
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
claim,
|
|
190
|
+
status: "unclear",
|
|
191
|
+
supporting_passages: [],
|
|
192
|
+
contradicting_passages: [],
|
|
193
|
+
rationale: "Passages were retrieved, but automated semantic support or contradiction assessment is unavailable; review the cited passages manually.",
|
|
194
|
+
confidence: 0.3,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface RankedSearchResult extends SearchResult {
|
|
199
|
+
rank?: number;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface BuildArtifactInput {
|
|
203
|
+
query: string;
|
|
204
|
+
provider?: string;
|
|
205
|
+
summary?: string;
|
|
206
|
+
results: RankedSearchResult[];
|
|
207
|
+
fetched?: ExtractedContent[];
|
|
208
|
+
recency?: RecencyFilter;
|
|
209
|
+
domainFilter?: string[];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function buildResearchArtifact(input: BuildArtifactInput): ResearchArtifact {
|
|
213
|
+
const filters = input.domainFilter ?? [];
|
|
214
|
+
const fetchedByUrl = new Map((input.fetched ?? []).map((page) => [page.url, page]));
|
|
215
|
+
const sources: ResearchSource[] = [];
|
|
216
|
+
const seen = new Set<string>();
|
|
217
|
+
for (const [index, result] of input.results.entries()) {
|
|
218
|
+
if (seen.has(result.url)) continue;
|
|
219
|
+
seen.add(result.url);
|
|
220
|
+
const page = fetchedByUrl.get(result.url);
|
|
221
|
+
const fetched = Boolean(page && !page.error);
|
|
222
|
+
sources.push({
|
|
223
|
+
rank: result.rank ?? index + 1,
|
|
224
|
+
url: result.url,
|
|
225
|
+
title: result.title,
|
|
226
|
+
snippet: result.snippet,
|
|
227
|
+
...(result.snippetSource === "generated" && result.snippet ? { snippet_source: "generated" as const } : {}),
|
|
228
|
+
quality: classifySource(result.url),
|
|
229
|
+
fetched,
|
|
230
|
+
...(page ? { fetch_timestamp: Date.now() } : {}),
|
|
231
|
+
...(page && !page.error ? { content_hash: hashContent(page.content) } : {}),
|
|
232
|
+
...(page?.error ? { fetch_error: page.error } : {}),
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
const passages = buildPassages(sources, input.fetched, input.query);
|
|
236
|
+
const domainInclude = filters.filter((domain) => !domain.startsWith("-"));
|
|
237
|
+
const domainExclude = filters.filter((domain) => domain.startsWith("-")).map((domain) => domain.slice(1));
|
|
238
|
+
return {
|
|
239
|
+
id: generateId(),
|
|
240
|
+
type: "research",
|
|
241
|
+
timestamp: Date.now(),
|
|
242
|
+
query: input.query,
|
|
243
|
+
sources,
|
|
244
|
+
passages,
|
|
245
|
+
...(input.provider !== undefined ? { provider: input.provider } : {}),
|
|
246
|
+
...(input.summary !== undefined ? { summary: input.summary } : {}),
|
|
247
|
+
...(passages.length > 0 ? { content_hash: hashContent(passages.map((passage) => passage.text).join("\n")) } : {}),
|
|
248
|
+
filters: {
|
|
249
|
+
...(input.recency !== undefined ? { recency: input.recency } : {}),
|
|
250
|
+
domain_include: domainInclude,
|
|
251
|
+
domain_exclude: domainExclude,
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function withClaimAssessment(artifact: ResearchArtifact, claims: string[]): ResearchArtifact {
|
|
257
|
+
return { ...artifact, claims: claims.map((claim) => assessClaim(claim, artifact.passages)) };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function storeResearchArtifact(artifact: ResearchArtifact): void {
|
|
261
|
+
if (!artifact.id) throw new Error("Research artifact id must not be empty");
|
|
262
|
+
storeResult(artifact.id, { id: artifact.id, type: "research", timestamp: artifact.timestamp, artifact });
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function getResearchArtifact(id: string): ResearchArtifact | null {
|
|
266
|
+
const data = getResult(id);
|
|
267
|
+
if (!data || data.type !== "research" || !data.artifact || typeof data.artifact !== "object") return null;
|
|
268
|
+
return data.artifact as ResearchArtifact;
|
|
269
|
+
}
|