@librechat/agents 2.4.320 → 2.4.322
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/dist/cjs/tools/search/firecrawl.cjs +6 -4
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/format.cjs +117 -80
- package/dist/cjs/tools/search/format.cjs.map +1 -1
- package/dist/cjs/tools/search/rerankers.cjs +43 -36
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/schema.cjs +70 -0
- package/dist/cjs/tools/search/schema.cjs.map +1 -0
- package/dist/cjs/tools/search/search.cjs +125 -52
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +162 -47
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +34 -5
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/esm/tools/search/firecrawl.mjs +6 -4
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/format.mjs +118 -81
- package/dist/esm/tools/search/format.mjs.map +1 -1
- package/dist/esm/tools/search/rerankers.mjs +43 -36
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/schema.mjs +61 -0
- package/dist/esm/tools/search/schema.mjs.map +1 -0
- package/dist/esm/tools/search/search.mjs +126 -53
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +161 -46
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +33 -6
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/types/tools/search/firecrawl.d.ts +1 -0
- package/dist/types/tools/search/rerankers.d.ts +8 -4
- package/dist/types/tools/search/schema.d.ts +16 -0
- package/dist/types/tools/search/tool.d.ts +13 -0
- package/dist/types/tools/search/types.d.ts +36 -0
- package/dist/types/tools/search/utils.d.ts +9 -2
- package/package.json +3 -2
- package/src/scripts/search.ts +3 -0
- package/src/tools/search/firecrawl.ts +9 -4
- package/src/tools/search/format.ts +157 -87
- package/src/tools/search/rerankers.ts +57 -36
- package/src/tools/search/schema.ts +63 -0
- package/src/tools/search/search.ts +165 -52
- package/src/tools/search/tool.ts +217 -44
- package/src/tools/search/types.ts +37 -0
- package/src/tools/search/utils.ts +37 -5
- package/src/utils/llmConfig.ts +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
var DATE_RANGE;
|
|
4
|
+
(function (DATE_RANGE) {
|
|
5
|
+
DATE_RANGE["PAST_HOUR"] = "h";
|
|
6
|
+
DATE_RANGE["PAST_24_HOURS"] = "d";
|
|
7
|
+
DATE_RANGE["PAST_WEEK"] = "w";
|
|
8
|
+
DATE_RANGE["PAST_MONTH"] = "m";
|
|
9
|
+
DATE_RANGE["PAST_YEAR"] = "y";
|
|
10
|
+
})(DATE_RANGE || (DATE_RANGE = {}));
|
|
11
|
+
const DEFAULT_QUERY_DESCRIPTION = `
|
|
12
|
+
GUIDELINES:
|
|
13
|
+
- Start broad, then narrow: Begin with key concepts, then refine with specifics
|
|
14
|
+
- Think like sources: Use terminology experts would use in the field
|
|
15
|
+
- Consider perspective: Frame queries from different viewpoints for better results
|
|
16
|
+
- Quality over quantity: A precise 3-4 word query often beats lengthy sentences
|
|
17
|
+
|
|
18
|
+
TECHNIQUES (combine for power searches):
|
|
19
|
+
- EXACT PHRASES: Use quotes ("climate change report")
|
|
20
|
+
- EXCLUDE TERMS: Use minus to remove unwanted results (-wikipedia)
|
|
21
|
+
- SITE-SPECIFIC: Restrict to websites (site:edu research)
|
|
22
|
+
- FILETYPE: Find specific documents (filetype:pdf study)
|
|
23
|
+
- OR OPERATOR: Find alternatives (electric OR hybrid cars)
|
|
24
|
+
- DATE RANGE: Recent information (data after:2020)
|
|
25
|
+
- WILDCARDS: Use * for unknown terms (how to * bread)
|
|
26
|
+
- SPECIFIC QUESTIONS: Use who/what/when/where/why/how
|
|
27
|
+
- DOMAIN TERMS: Include technical terminology for specialized topics
|
|
28
|
+
- CONCISE TERMS: Prioritize keywords over sentences
|
|
29
|
+
`.trim();
|
|
30
|
+
const DEFAULT_COUNTRY_DESCRIPTION = `Country code to localize search results.
|
|
31
|
+
Use standard 2-letter country codes: "us", "uk", "ca", "de", "fr", "jp", "br", etc.
|
|
32
|
+
Provide this when the search should return results specific to a particular country.
|
|
33
|
+
Examples:
|
|
34
|
+
- "us" for United States (default)
|
|
35
|
+
- "de" for Germany
|
|
36
|
+
- "in" for India
|
|
37
|
+
`.trim();
|
|
38
|
+
const querySchema = z.string().describe(DEFAULT_QUERY_DESCRIPTION);
|
|
39
|
+
const dateSchema = z
|
|
40
|
+
.nativeEnum(DATE_RANGE)
|
|
41
|
+
.optional()
|
|
42
|
+
.describe('Date range for search results.');
|
|
43
|
+
const countrySchema = z
|
|
44
|
+
.string()
|
|
45
|
+
.optional()
|
|
46
|
+
.describe(DEFAULT_COUNTRY_DESCRIPTION);
|
|
47
|
+
const imagesSchema = z
|
|
48
|
+
.boolean()
|
|
49
|
+
.optional()
|
|
50
|
+
.describe('Whether to also run an image search.');
|
|
51
|
+
const videosSchema = z
|
|
52
|
+
.boolean()
|
|
53
|
+
.optional()
|
|
54
|
+
.describe('Whether to also run a video search.');
|
|
55
|
+
const newsSchema = z
|
|
56
|
+
.boolean()
|
|
57
|
+
.optional()
|
|
58
|
+
.describe('Whether to also run a news search.');
|
|
59
|
+
|
|
60
|
+
export { DATE_RANGE, DEFAULT_COUNTRY_DESCRIPTION, DEFAULT_QUERY_DESCRIPTION, countrySchema, dateSchema, imagesSchema, newsSchema, querySchema, videosSchema };
|
|
61
|
+
//# sourceMappingURL=schema.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.mjs","sources":["../../../../src/tools/search/schema.ts"],"sourcesContent":["import { z } from 'zod';\n\nexport enum DATE_RANGE {\n PAST_HOUR = 'h',\n PAST_24_HOURS = 'd',\n PAST_WEEK = 'w',\n PAST_MONTH = 'm',\n PAST_YEAR = 'y',\n}\n\nexport const DEFAULT_QUERY_DESCRIPTION = `\nGUIDELINES:\n- Start broad, then narrow: Begin with key concepts, then refine with specifics\n- Think like sources: Use terminology experts would use in the field\n- Consider perspective: Frame queries from different viewpoints for better results\n- Quality over quantity: A precise 3-4 word query often beats lengthy sentences\n\nTECHNIQUES (combine for power searches):\n- EXACT PHRASES: Use quotes (\"climate change report\")\n- EXCLUDE TERMS: Use minus to remove unwanted results (-wikipedia)\n- SITE-SPECIFIC: Restrict to websites (site:edu research)\n- FILETYPE: Find specific documents (filetype:pdf study)\n- OR OPERATOR: Find alternatives (electric OR hybrid cars)\n- DATE RANGE: Recent information (data after:2020)\n- WILDCARDS: Use * for unknown terms (how to * bread)\n- SPECIFIC QUESTIONS: Use who/what/when/where/why/how\n- DOMAIN TERMS: Include technical terminology for specialized topics\n- CONCISE TERMS: Prioritize keywords over sentences\n`.trim();\n\nexport const DEFAULT_COUNTRY_DESCRIPTION =\n `Country code to localize search results.\nUse standard 2-letter country codes: \"us\", \"uk\", \"ca\", \"de\", \"fr\", \"jp\", \"br\", etc.\nProvide this when the search should return results specific to a particular country.\nExamples:\n- \"us\" for United States (default)\n- \"de\" for Germany\n- \"in\" for India\n`.trim();\n\nexport const querySchema = z.string().describe(DEFAULT_QUERY_DESCRIPTION);\nexport const dateSchema = z\n .nativeEnum(DATE_RANGE)\n .optional()\n .describe('Date range for search results.');\nexport const countrySchema = z\n .string()\n .optional()\n .describe(DEFAULT_COUNTRY_DESCRIPTION);\nexport const imagesSchema = z\n .boolean()\n .optional()\n .describe('Whether to also run an image search.');\n\nexport const videosSchema = z\n .boolean()\n .optional()\n .describe('Whether to also run a video search.');\n\nexport const newsSchema = z\n .boolean()\n .optional()\n .describe('Whether to also run a news search.');\n"],"names":[],"mappings":";;IAEY;AAAZ,CAAA,UAAY,UAAU,EAAA;AACpB,IAAA,UAAA,CAAA,WAAA,CAAA,GAAA,GAAe;AACf,IAAA,UAAA,CAAA,eAAA,CAAA,GAAA,GAAmB;AACnB,IAAA,UAAA,CAAA,WAAA,CAAA,GAAA,GAAe;AACf,IAAA,UAAA,CAAA,YAAA,CAAA,GAAA,GAAgB;AAChB,IAAA,UAAA,CAAA,WAAA,CAAA,GAAA,GAAe;AACjB,CAAC,EANW,UAAU,KAAV,UAAU,GAMrB,EAAA,CAAA,CAAA;AAEY,MAAA,yBAAyB,GAAG;;;;;;;;;;;;;;;;;;CAkBxC,CAAC,IAAI;AAEO,MAAA,2BAA2B,GACtC,CAAA;;;;;;;CAOD,CAAC,IAAI;AAEC,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,yBAAyB;AACjE,MAAM,UAAU,GAAG;KACvB,UAAU,CAAC,UAAU;AACrB,KAAA,QAAQ;KACR,QAAQ,CAAC,gCAAgC;AACrC,MAAM,aAAa,GAAG;AAC1B,KAAA,MAAM;AACN,KAAA,QAAQ;KACR,QAAQ,CAAC,2BAA2B;AAChC,MAAM,YAAY,GAAG;AACzB,KAAA,OAAO;AACP,KAAA,QAAQ;KACR,QAAQ,CAAC,sCAAsC;AAE3C,MAAM,YAAY,GAAG;AACzB,KAAA,OAAO;AACP,KAAA,QAAQ;KACR,QAAQ,CAAC,qCAAqC;AAE1C,MAAM,UAAU,GAAG;AACvB,KAAA,OAAO;AACP,KAAA,QAAQ;KACR,QAAQ,CAAC,oCAAoC;;;;"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
|
3
|
-
import { getAttribution } from './utils.mjs';
|
|
3
|
+
import { createDefaultLogger, getAttribution } from './utils.mjs';
|
|
4
4
|
|
|
5
|
-
/* eslint-disable no-console */
|
|
6
5
|
const chunker = {
|
|
7
6
|
cleanText: (text) => {
|
|
8
7
|
if (!text)
|
|
@@ -29,10 +28,11 @@ const chunker = {
|
|
|
29
28
|
});
|
|
30
29
|
return await splitter.splitText(text);
|
|
31
30
|
},
|
|
32
|
-
splitTexts: async (texts, options) => {
|
|
31
|
+
splitTexts: async (texts, options, logger) => {
|
|
33
32
|
// Split multiple texts
|
|
33
|
+
const logger_ = logger || createDefaultLogger();
|
|
34
34
|
const promises = texts.map((text) => chunker.splitText(text, options).catch((error) => {
|
|
35
|
-
|
|
35
|
+
logger_.error('Error splitting text:', error);
|
|
36
36
|
return [text];
|
|
37
37
|
}));
|
|
38
38
|
return Promise.all(promises);
|
|
@@ -49,13 +49,14 @@ function createSourceUpdateCallback(sourceMap) {
|
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
|
-
const getHighlights = async ({ query, content, reranker, topResults = 5, }) => {
|
|
52
|
+
const getHighlights = async ({ query, content, reranker, topResults = 5, logger, }) => {
|
|
53
|
+
const logger_ = logger || createDefaultLogger();
|
|
53
54
|
if (!content) {
|
|
54
|
-
|
|
55
|
+
logger_.warn('No content provided for highlights');
|
|
55
56
|
return;
|
|
56
57
|
}
|
|
57
58
|
if (!reranker) {
|
|
58
|
-
|
|
59
|
+
logger_.warn('No reranker provided for highlights');
|
|
59
60
|
return;
|
|
60
61
|
}
|
|
61
62
|
try {
|
|
@@ -64,12 +65,12 @@ const getHighlights = async ({ query, content, reranker, topResults = 5, }) => {
|
|
|
64
65
|
return await reranker.rerank(query, documents, topResults);
|
|
65
66
|
}
|
|
66
67
|
else {
|
|
67
|
-
|
|
68
|
+
logger_.error('Expected documents to be an array, got:', typeof documents);
|
|
68
69
|
return;
|
|
69
70
|
}
|
|
70
71
|
}
|
|
71
72
|
catch (error) {
|
|
72
|
-
|
|
73
|
+
logger_.error('Error in content processing:', error);
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
75
76
|
};
|
|
@@ -82,19 +83,39 @@ const createSerperAPI = (apiKey) => {
|
|
|
82
83
|
if (config.apiKey == null || config.apiKey === '') {
|
|
83
84
|
throw new Error('SERPER_API_KEY is required for SerperAPI');
|
|
84
85
|
}
|
|
85
|
-
const getSources = async ({ query, country, numResults = 8, }) => {
|
|
86
|
+
const getSources = async ({ query, date, country, safeSearch, numResults = 8, type, }) => {
|
|
86
87
|
if (!query.trim()) {
|
|
87
88
|
return { success: false, error: 'Query cannot be empty' };
|
|
88
89
|
}
|
|
89
90
|
try {
|
|
91
|
+
const safe = ['off', 'moderate', 'active'];
|
|
90
92
|
const payload = {
|
|
91
93
|
q: query,
|
|
94
|
+
safe: safe[safeSearch ?? 1],
|
|
92
95
|
num: Math.min(Math.max(1, numResults), 10),
|
|
93
96
|
};
|
|
97
|
+
// Set the search type if provided
|
|
98
|
+
if (type) {
|
|
99
|
+
payload.type = type;
|
|
100
|
+
}
|
|
101
|
+
if (date != null) {
|
|
102
|
+
payload.tbs = `qdr:${date}`;
|
|
103
|
+
}
|
|
94
104
|
if (country != null && country !== '') {
|
|
95
105
|
payload['gl'] = country.toLowerCase();
|
|
96
106
|
}
|
|
97
|
-
|
|
107
|
+
// Determine the API endpoint based on the search type
|
|
108
|
+
let apiEndpoint = config.apiUrl;
|
|
109
|
+
if (type === 'images') {
|
|
110
|
+
apiEndpoint = 'https://google.serper.dev/images';
|
|
111
|
+
}
|
|
112
|
+
else if (type === 'videos') {
|
|
113
|
+
apiEndpoint = 'https://google.serper.dev/videos';
|
|
114
|
+
}
|
|
115
|
+
else if (type === 'news') {
|
|
116
|
+
apiEndpoint = 'https://google.serper.dev/news';
|
|
117
|
+
}
|
|
118
|
+
const response = await axios.post(apiEndpoint, payload, {
|
|
98
119
|
headers: {
|
|
99
120
|
'X-API-KEY': config.apiKey,
|
|
100
121
|
'Content-Type': 'application/json',
|
|
@@ -110,6 +131,8 @@ const createSerperAPI = (apiKey) => {
|
|
|
110
131
|
peopleAlsoAsk: data.peopleAlsoAsk,
|
|
111
132
|
knowledgeGraph: data.knowledgeGraph,
|
|
112
133
|
relatedSearches: data.relatedSearches,
|
|
134
|
+
videos: data.videos ?? [],
|
|
135
|
+
news: data.news ?? [],
|
|
113
136
|
};
|
|
114
137
|
return { success: true, data: results };
|
|
115
138
|
}
|
|
@@ -129,7 +152,7 @@ const createSearXNGAPI = (instanceUrl, apiKey) => {
|
|
|
129
152
|
if (config.instanceUrl == null || config.instanceUrl === '') {
|
|
130
153
|
throw new Error('SEARXNG_INSTANCE_URL is required for SearXNG API');
|
|
131
154
|
}
|
|
132
|
-
const getSources = async ({ query, numResults = 8, }) => {
|
|
155
|
+
const getSources = async ({ query, numResults = 8, type, }) => {
|
|
133
156
|
if (!query.trim()) {
|
|
134
157
|
return { success: false, error: 'Query cannot be empty' };
|
|
135
158
|
}
|
|
@@ -142,12 +165,23 @@ const createSearXNGAPI = (instanceUrl, apiKey) => {
|
|
|
142
165
|
if (!searchUrl.endsWith('/search')) {
|
|
143
166
|
searchUrl = searchUrl.replace(/\/$/, '') + '/search';
|
|
144
167
|
}
|
|
168
|
+
// Determine the search category based on the type
|
|
169
|
+
let category = 'general';
|
|
170
|
+
if (type === 'images') {
|
|
171
|
+
category = 'images';
|
|
172
|
+
}
|
|
173
|
+
else if (type === 'videos') {
|
|
174
|
+
category = 'videos';
|
|
175
|
+
}
|
|
176
|
+
else if (type === 'news') {
|
|
177
|
+
category = 'news';
|
|
178
|
+
}
|
|
145
179
|
// Prepare parameters for SearXNG
|
|
146
180
|
const params = {
|
|
147
181
|
q: query,
|
|
148
182
|
format: 'json',
|
|
149
183
|
pageno: 1,
|
|
150
|
-
categories:
|
|
184
|
+
categories: category,
|
|
151
185
|
language: 'all',
|
|
152
186
|
safesearch: 0,
|
|
153
187
|
engines: 'google,bing,duckduckgo',
|
|
@@ -188,6 +222,8 @@ const createSearXNGAPI = (instanceUrl, apiKey) => {
|
|
|
188
222
|
topStories: [],
|
|
189
223
|
// Use undefined instead of null for optional properties
|
|
190
224
|
relatedSearches: data.suggestions ?? [],
|
|
225
|
+
videos: [],
|
|
226
|
+
news: [],
|
|
191
227
|
};
|
|
192
228
|
return { success: true, data: results };
|
|
193
229
|
}
|
|
@@ -220,11 +256,12 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
220
256
|
const { topResults = 5,
|
|
221
257
|
// strategies = ['no_extraction'],
|
|
222
258
|
// filterContent = true,
|
|
223
|
-
reranker, } = config;
|
|
259
|
+
reranker, logger, } = config;
|
|
260
|
+
const logger_ = logger || createDefaultLogger();
|
|
224
261
|
const firecrawlScraper = scraperInstance;
|
|
225
262
|
const webScraper = {
|
|
226
263
|
scrapeMany: async ({ query, links, onGetHighlights, }) => {
|
|
227
|
-
|
|
264
|
+
logger_.debug(`Scraping ${links.length} links with Firecrawl`);
|
|
228
265
|
const promises = [];
|
|
229
266
|
try {
|
|
230
267
|
for (let i = 0; i < links.length; i++) {
|
|
@@ -232,7 +269,7 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
232
269
|
const promise = firecrawlScraper
|
|
233
270
|
.scrapeUrl(currentLink, {})
|
|
234
271
|
.then(([url, response]) => {
|
|
235
|
-
const attribution = getAttribution(url, response.data?.metadata);
|
|
272
|
+
const attribution = getAttribution(url, response.data?.metadata, logger_);
|
|
236
273
|
if (response.success && response.data) {
|
|
237
274
|
const [content, references] = firecrawlScraper.extractContent(response);
|
|
238
275
|
return {
|
|
@@ -246,13 +283,13 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
246
283
|
url,
|
|
247
284
|
attribution,
|
|
248
285
|
error: true,
|
|
249
|
-
content:
|
|
286
|
+
content: '',
|
|
250
287
|
};
|
|
251
288
|
})
|
|
252
289
|
.then(async (result) => {
|
|
253
290
|
try {
|
|
254
291
|
if (result.error != null) {
|
|
255
|
-
|
|
292
|
+
logger_.error(`Error scraping ${result.url}: ${result.content}`, result.error);
|
|
256
293
|
return {
|
|
257
294
|
...result,
|
|
258
295
|
};
|
|
@@ -261,6 +298,7 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
261
298
|
query,
|
|
262
299
|
reranker,
|
|
263
300
|
content: result.content,
|
|
301
|
+
logger: logger_,
|
|
264
302
|
});
|
|
265
303
|
if (onGetHighlights) {
|
|
266
304
|
onGetHighlights(result.url);
|
|
@@ -271,18 +309,18 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
271
309
|
};
|
|
272
310
|
}
|
|
273
311
|
catch (error) {
|
|
274
|
-
|
|
312
|
+
logger_.error('Error processing scraped content:', error);
|
|
275
313
|
return {
|
|
276
314
|
...result,
|
|
277
315
|
};
|
|
278
316
|
}
|
|
279
317
|
})
|
|
280
318
|
.catch((error) => {
|
|
281
|
-
|
|
319
|
+
logger_.error(`Error scraping ${currentLink}:`, error);
|
|
282
320
|
return {
|
|
283
321
|
url: currentLink,
|
|
284
322
|
error: true,
|
|
285
|
-
content:
|
|
323
|
+
content: '',
|
|
286
324
|
};
|
|
287
325
|
});
|
|
288
326
|
promises.push(promise);
|
|
@@ -290,7 +328,7 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
290
328
|
return await Promise.all(promises);
|
|
291
329
|
}
|
|
292
330
|
catch (error) {
|
|
293
|
-
|
|
331
|
+
logger_.error('Error in scrapeMany:', error);
|
|
294
332
|
return [];
|
|
295
333
|
}
|
|
296
334
|
},
|
|
@@ -316,7 +354,7 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
316
354
|
});
|
|
317
355
|
}
|
|
318
356
|
};
|
|
319
|
-
const processSources = async ({ result, numElements, query, proMode = true, onGetHighlights, }) => {
|
|
357
|
+
const processSources = async ({ result, numElements, query, news, proMode = true, onGetHighlights, }) => {
|
|
320
358
|
try {
|
|
321
359
|
if (!result.data) {
|
|
322
360
|
return {
|
|
@@ -357,44 +395,48 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
357
395
|
return result.data;
|
|
358
396
|
}
|
|
359
397
|
const sourceMap = new Map();
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (allLinks.length === 0) {
|
|
398
|
+
const organicLinksSet = new Set();
|
|
399
|
+
// Collect organic links
|
|
400
|
+
const organicLinks = collectLinks(result.data.organic, sourceMap, organicLinksSet);
|
|
401
|
+
// Collect top story links, excluding any that are already in organic links
|
|
402
|
+
const topStories = result.data.topStories ?? [];
|
|
403
|
+
const topStoryLinks = collectLinks(topStories, sourceMap, organicLinksSet);
|
|
404
|
+
if (organicLinks.length === 0 && (topStoryLinks.length === 0 || !news)) {
|
|
368
405
|
return result.data;
|
|
369
406
|
}
|
|
370
407
|
const onContentScraped = createSourceUpdateCallback(sourceMap);
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
if (updatedSource) {
|
|
382
|
-
result.data.organic[i] = {
|
|
383
|
-
...source,
|
|
384
|
-
...updatedSource,
|
|
385
|
-
};
|
|
386
|
-
}
|
|
408
|
+
const promises = [];
|
|
409
|
+
// Process organic links
|
|
410
|
+
if (organicLinks.length > 0) {
|
|
411
|
+
promises.push(fetchContents({
|
|
412
|
+
query,
|
|
413
|
+
onGetHighlights,
|
|
414
|
+
onContentScraped,
|
|
415
|
+
links: organicLinks,
|
|
416
|
+
target: numElements,
|
|
417
|
+
}));
|
|
387
418
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
.
|
|
391
|
-
|
|
392
|
-
|
|
419
|
+
// Process top story links
|
|
420
|
+
if (news && topStoryLinks.length > 0) {
|
|
421
|
+
promises.push(fetchContents({
|
|
422
|
+
query,
|
|
423
|
+
onGetHighlights,
|
|
424
|
+
onContentScraped,
|
|
425
|
+
links: topStoryLinks,
|
|
426
|
+
target: numElements,
|
|
427
|
+
}));
|
|
428
|
+
}
|
|
429
|
+
await Promise.all(promises);
|
|
430
|
+
if (result.data.organic.length > 0) {
|
|
431
|
+
updateSourcesWithContent(result.data.organic, sourceMap);
|
|
432
|
+
}
|
|
433
|
+
if (news && topStories.length > 0) {
|
|
434
|
+
updateSourcesWithContent(topStories, sourceMap);
|
|
393
435
|
}
|
|
394
436
|
return result.data;
|
|
395
437
|
}
|
|
396
438
|
catch (error) {
|
|
397
|
-
|
|
439
|
+
logger_.error('Error in processSources:', error);
|
|
398
440
|
return {
|
|
399
441
|
organic: [],
|
|
400
442
|
topStories: [],
|
|
@@ -410,6 +452,37 @@ const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
|
410
452
|
topResults,
|
|
411
453
|
};
|
|
412
454
|
};
|
|
455
|
+
/** Helper function to collect links and update sourceMap */
|
|
456
|
+
function collectLinks(sources, sourceMap, existingLinksSet) {
|
|
457
|
+
const links = [];
|
|
458
|
+
for (const source of sources) {
|
|
459
|
+
if (source.link) {
|
|
460
|
+
// For topStories, only add if not already in organic links
|
|
461
|
+
if (existingLinksSet && existingLinksSet.has(source.link)) {
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
links.push(source.link);
|
|
465
|
+
if (existingLinksSet) {
|
|
466
|
+
existingLinksSet.add(source.link);
|
|
467
|
+
}
|
|
468
|
+
sourceMap.set(source.link, source);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return links;
|
|
472
|
+
}
|
|
473
|
+
/** Helper function to update sources with scraped content */
|
|
474
|
+
function updateSourcesWithContent(sources, sourceMap) {
|
|
475
|
+
for (let i = 0; i < sources.length; i++) {
|
|
476
|
+
const source = sources[i];
|
|
477
|
+
const updatedSource = sourceMap.get(source.link);
|
|
478
|
+
if (updatedSource) {
|
|
479
|
+
sources[i] = {
|
|
480
|
+
...source,
|
|
481
|
+
...updatedSource,
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
413
486
|
|
|
414
487
|
export { createSearchAPI, createSourceProcessor };
|
|
415
488
|
//# sourceMappingURL=search.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.mjs","sources":["../../../../src/tools/search/search.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport axios from 'axios';\nimport { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';\nimport type * as t from './types';\nimport { FirecrawlScraper } from './firecrawl';\nimport { BaseReranker } from './rerankers';\nimport { getAttribution } from './utils';\n\nconst chunker = {\n cleanText: (text: string): string => {\n if (!text) return '';\n\n /** Normalized all line endings to '\\n' */\n const normalizedText = text.replace(/\\r\\n/g, '\\n').replace(/\\r/g, '\\n');\n\n /** Handle multiple backslashes followed by newlines\n * This replaces patterns like '\\\\\\\\\\\\n' with a single newline */\n const fixedBackslashes = normalizedText.replace(/\\\\+\\n/g, '\\n');\n\n /** Cleaned up consecutive newlines, tabs, and spaces around newlines */\n const cleanedNewlines = fixedBackslashes.replace(/[\\t ]*\\n[\\t \\n]*/g, '\\n');\n\n /** Cleaned up excessive spaces and tabs */\n const cleanedSpaces = cleanedNewlines.replace(/[ \\t]+/g, ' ');\n\n return cleanedSpaces.trim();\n },\n splitText: async (\n text: string,\n options?: {\n chunkSize?: number;\n chunkOverlap?: number;\n separators?: string[];\n }\n ): Promise<string[]> => {\n const chunkSize = options?.chunkSize ?? 150;\n const chunkOverlap = options?.chunkOverlap ?? 50;\n const separators = options?.separators || ['\\n\\n', '\\n'];\n\n const splitter = new RecursiveCharacterTextSplitter({\n separators,\n chunkSize,\n chunkOverlap,\n });\n\n return await splitter.splitText(text);\n },\n\n splitTexts: async (\n texts: string[],\n options?: {\n chunkSize?: number;\n chunkOverlap?: number;\n separators?: string[];\n }\n ): Promise<string[][]> => {\n // Split multiple texts\n const promises = texts.map((text) =>\n chunker.splitText(text, options).catch((error) => {\n console.error('Error splitting text:', error);\n return [text];\n })\n );\n return Promise.all(promises);\n },\n};\n\nfunction createSourceUpdateCallback(sourceMap: Map<string, t.ValidSource>) {\n return (link: string, update?: Partial<t.ValidSource>): void => {\n const source = sourceMap.get(link);\n if (source) {\n sourceMap.set(link, {\n ...source,\n ...update,\n });\n }\n };\n}\n\nconst getHighlights = async ({\n query,\n content,\n reranker,\n topResults = 5,\n}: {\n content: string;\n query: string;\n reranker?: BaseReranker;\n topResults?: number;\n}): Promise<t.Highlight[] | undefined> => {\n if (!content) {\n console.warn('No content provided for highlights');\n return;\n }\n if (!reranker) {\n console.warn('No reranker provided for highlights');\n return;\n }\n\n try {\n const documents = await chunker.splitText(content);\n if (Array.isArray(documents)) {\n return await reranker.rerank(query, documents, topResults);\n } else {\n console.error(\n 'Expected documents to be an array, got:',\n typeof documents\n );\n return;\n }\n } catch (error) {\n console.error('Error in content processing:', error);\n return;\n }\n};\n\nconst createSerperAPI = (\n apiKey?: string\n): {\n getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;\n} => {\n const config = {\n apiKey: apiKey ?? process.env.SERPER_API_KEY,\n apiUrl: 'https://google.serper.dev/search',\n timeout: 10000,\n };\n\n if (config.apiKey == null || config.apiKey === '') {\n throw new Error('SERPER_API_KEY is required for SerperAPI');\n }\n\n const getSources = async ({\n query,\n country,\n numResults = 8,\n }: t.GetSourcesParams): Promise<t.SearchResult> => {\n if (!query.trim()) {\n return { success: false, error: 'Query cannot be empty' };\n }\n\n try {\n const payload: t.SerperSearchPayload = {\n q: query,\n num: Math.min(Math.max(1, numResults), 10),\n };\n\n if (country != null && country !== '') {\n payload['gl'] = country.toLowerCase();\n }\n\n const response = await axios.post<t.SerperResultData>(\n config.apiUrl,\n payload,\n {\n headers: {\n 'X-API-KEY': config.apiKey,\n 'Content-Type': 'application/json',\n },\n timeout: config.timeout,\n }\n );\n\n const data = response.data;\n const results: t.SearchResultData = {\n organic: data.organic,\n images: data.images ?? [],\n answerBox: data.answerBox,\n topStories: data.topStories ?? [],\n peopleAlsoAsk: data.peopleAlsoAsk,\n knowledgeGraph: data.knowledgeGraph,\n relatedSearches: data.relatedSearches,\n };\n\n return { success: true, data: results };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return { success: false, error: `API request failed: ${errorMessage}` };\n }\n };\n\n return { getSources };\n};\n\nconst createSearXNGAPI = (\n instanceUrl?: string,\n apiKey?: string\n): {\n getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;\n} => {\n const config = {\n instanceUrl: instanceUrl ?? process.env.SEARXNG_INSTANCE_URL,\n apiKey: apiKey ?? process.env.SEARXNG_API_KEY,\n defaultLocation: 'all',\n timeout: 10000,\n };\n\n if (config.instanceUrl == null || config.instanceUrl === '') {\n throw new Error('SEARXNG_INSTANCE_URL is required for SearXNG API');\n }\n\n const getSources = async ({\n query,\n numResults = 8,\n }: t.GetSourcesParams): Promise<t.SearchResult> => {\n if (!query.trim()) {\n return { success: false, error: 'Query cannot be empty' };\n }\n\n try {\n // Ensure the instance URL ends with /search\n if (config.instanceUrl == null || config.instanceUrl === '') {\n return { success: false, error: 'Instance URL is not defined' };\n }\n\n let searchUrl = config.instanceUrl;\n if (!searchUrl.endsWith('/search')) {\n searchUrl = searchUrl.replace(/\\/$/, '') + '/search';\n }\n\n // Prepare parameters for SearXNG\n const params: t.SearxNGSearchPayload = {\n q: query,\n format: 'json',\n pageno: 1,\n categories: 'general',\n language: 'all',\n safesearch: 0,\n engines: 'google,bing,duckduckgo',\n };\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n if (config.apiKey != null && config.apiKey !== '') {\n headers['X-API-Key'] = config.apiKey;\n }\n\n const response = await axios.get(searchUrl, {\n headers,\n params,\n timeout: config.timeout,\n });\n\n const data = response.data;\n\n // Transform SearXNG results to match SerperAPI format\n const organicResults = (data.results ?? [])\n .slice(0, numResults)\n .map((result: t.SearXNGResult) => ({\n title: result.title ?? '',\n link: result.url ?? '',\n snippet: result.content ?? '',\n date: result.publishedDate ?? '',\n }));\n\n // Extract image results if available\n const imageResults = (data.results ?? [])\n .filter((result: t.SearXNGResult) => result.img_src)\n .slice(0, 6)\n .map((result: t.SearXNGResult) => ({\n title: result.title ?? '',\n imageUrl: result.img_src ?? '',\n }));\n\n // Format results to match SerperAPI structure\n const results: t.SearchResultData = {\n organic: organicResults,\n images: imageResults,\n topStories: [],\n // Use undefined instead of null for optional properties\n relatedSearches: data.suggestions ?? [],\n };\n\n return { success: true, data: results };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return {\n success: false,\n error: `SearXNG API request failed: ${errorMessage}`,\n };\n }\n };\n\n return { getSources };\n};\n\nexport const createSearchAPI = (\n config: t.SearchConfig\n): {\n getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;\n} => {\n const {\n searchProvider = 'serper',\n serperApiKey,\n searxngInstanceUrl,\n searxngApiKey,\n } = config;\n\n if (searchProvider.toLowerCase() === 'serper') {\n return createSerperAPI(serperApiKey);\n } else if (searchProvider.toLowerCase() === 'searxng') {\n return createSearXNGAPI(searxngInstanceUrl, searxngApiKey);\n } else {\n throw new Error(\n `Invalid search provider: ${searchProvider}. Must be 'serper' or 'searxng'`\n );\n }\n};\n\nexport const createSourceProcessor = (\n config: t.ProcessSourcesConfig = {},\n scraperInstance?: FirecrawlScraper\n): {\n processSources: (\n fields: t.ProcessSourcesFields\n ) => Promise<t.SearchResultData>;\n topResults: number;\n} => {\n if (!scraperInstance) {\n throw new Error('Firecrawl scraper instance is required');\n }\n const {\n topResults = 5,\n // strategies = ['no_extraction'],\n // filterContent = true,\n reranker,\n } = config;\n\n const firecrawlScraper = scraperInstance;\n\n const webScraper = {\n scrapeMany: async ({\n query,\n links,\n onGetHighlights,\n }: {\n query: string;\n links: string[];\n onGetHighlights: t.SearchToolConfig['onGetHighlights'];\n }): Promise<Array<t.ScrapeResult>> => {\n console.log(`Scraping ${links.length} links with Firecrawl`);\n const promises: Array<Promise<t.ScrapeResult>> = [];\n try {\n for (let i = 0; i < links.length; i++) {\n const currentLink = links[i];\n const promise: Promise<t.ScrapeResult> = firecrawlScraper\n .scrapeUrl(currentLink, {})\n .then(([url, response]) => {\n const attribution = getAttribution(url, response.data?.metadata);\n if (response.success && response.data) {\n const [content, references] =\n firecrawlScraper.extractContent(response);\n return {\n url,\n references,\n attribution,\n content: chunker.cleanText(content),\n } as t.ScrapeResult;\n }\n\n return {\n url,\n attribution,\n error: true,\n content: `Failed to scrape ${url}: ${response.error ?? 'Unknown error'}`,\n } as t.ScrapeResult;\n })\n .then(async (result) => {\n try {\n if (result.error != null) {\n console.error(\n `Error scraping ${result.url}: ${result.content}`\n );\n return {\n ...result,\n };\n }\n const highlights = await getHighlights({\n query,\n reranker,\n content: result.content,\n });\n if (onGetHighlights) {\n onGetHighlights(result.url);\n }\n return {\n ...result,\n highlights,\n };\n } catch (error) {\n console.error('Error processing scraped content:', error);\n return {\n ...result,\n };\n }\n })\n .catch((error) => {\n console.error(`Error scraping ${currentLink}:`, error);\n return {\n url: currentLink,\n error: true,\n content: `Failed to scrape ${currentLink}: ${error.message ?? 'Unknown error'}`,\n };\n });\n promises.push(promise);\n }\n return await Promise.all(promises);\n } catch (error) {\n console.error('Error in scrapeMany:', error);\n return [];\n }\n },\n };\n\n const fetchContents = async ({\n links,\n query,\n target,\n onGetHighlights,\n onContentScraped,\n }: {\n links: string[];\n query: string;\n target: number;\n onGetHighlights: t.SearchToolConfig['onGetHighlights'];\n onContentScraped?: (link: string, update?: Partial<t.ValidSource>) => void;\n }): Promise<void> => {\n const initialLinks = links.slice(0, target);\n // const remainingLinks = links.slice(target).reverse();\n const results = await webScraper.scrapeMany({\n query,\n links: initialLinks,\n onGetHighlights,\n });\n for (const result of results) {\n if (result.error === true) {\n continue;\n }\n const { url, content, attribution, references, highlights } = result;\n onContentScraped?.(url, {\n content,\n attribution,\n references,\n highlights,\n });\n }\n };\n\n const processSources = async ({\n result,\n numElements,\n query,\n proMode = true,\n onGetHighlights,\n }: t.ProcessSourcesFields): Promise<t.SearchResultData> => {\n try {\n if (!result.data) {\n return {\n organic: [],\n topStories: [],\n images: [],\n relatedSearches: [],\n };\n } else if (!result.data.organic) {\n return result.data;\n }\n\n if (!proMode) {\n const wikiSources = result.data.organic.filter((source) =>\n source.link.includes('wikipedia.org')\n );\n\n if (!wikiSources.length) {\n return result.data;\n }\n\n const wikiSourceMap = new Map<string, t.ValidSource>();\n wikiSourceMap.set(wikiSources[0].link, wikiSources[0]);\n const onContentScraped = createSourceUpdateCallback(wikiSourceMap);\n await fetchContents({\n query,\n target: 1,\n onGetHighlights,\n onContentScraped,\n links: [wikiSources[0].link],\n });\n\n for (let i = 0; i < result.data.organic.length; i++) {\n const source = result.data.organic[i];\n const updatedSource = wikiSourceMap.get(source.link);\n if (updatedSource) {\n result.data.organic[i] = {\n ...source,\n ...updatedSource,\n };\n }\n }\n\n return result.data;\n }\n\n const sourceMap = new Map<string, t.ValidSource>();\n const allLinks: string[] = [];\n\n for (const source of result.data.organic) {\n if (source.link) {\n allLinks.push(source.link);\n sourceMap.set(source.link, source);\n }\n }\n\n if (allLinks.length === 0) {\n return result.data;\n }\n\n const onContentScraped = createSourceUpdateCallback(sourceMap);\n await fetchContents({\n query,\n links: allLinks,\n onGetHighlights,\n onContentScraped,\n target: numElements,\n });\n\n for (let i = 0; i < result.data.organic.length; i++) {\n const source = result.data.organic[i];\n const updatedSource = sourceMap.get(source.link);\n if (updatedSource) {\n result.data.organic[i] = {\n ...source,\n ...updatedSource,\n };\n }\n }\n\n const successfulSources = result.data.organic\n .filter(\n (source) =>\n source.content != null && !source.content.startsWith('Failed')\n )\n .slice(0, numElements);\n\n if (successfulSources.length > 0) {\n result.data.organic = successfulSources;\n }\n return result.data;\n } catch (error) {\n console.error('Error in processSources:', error);\n return {\n organic: [],\n topStories: [],\n images: [],\n relatedSearches: [],\n ...result.data,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n };\n\n return {\n processSources,\n topResults,\n };\n};\n"],"names":[],"mappings":";;;;AAAA;AAQA,MAAM,OAAO,GAAG;AACd,IAAA,SAAS,EAAE,CAAC,IAAY,KAAY;AAClC,QAAA,IAAI,CAAC,IAAI;AAAE,YAAA,OAAO,EAA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,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;AAChC,gBAAA,MAAM,CAAC,IAAI,CAAC,OAAO,GAAG,iBAAiB;;YAEzC,OAAO,MAAM,CAAC,IAAI;;QAClB,OAAO,KAAK,EAAE;AACd,YAAA,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,KAAK,CAAC;YAChD,OAAO;AACL,gBAAA,OAAO,EAAE,EAAE;AACX,gBAAA,UAAU,EAAE,EAAE;AACd,gBAAA,MAAM,EAAE,EAAE;AACV,gBAAA,eAAe,EAAE,EAAE;gBACnB,GAAG,MAAM,CAAC,IAAI;AACd,gBAAA,KAAK,EAAE,KAAK,YAAY,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;aAC9D;;AAEL,KAAC;IAED,OAAO;QACL,cAAc;QACd,UAAU;KACX;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"search.mjs","sources":["../../../../src/tools/search/search.ts"],"sourcesContent":["import axios from 'axios';\nimport { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';\nimport type * as t from './types';\nimport { getAttribution, createDefaultLogger } from './utils';\nimport { FirecrawlScraper } from './firecrawl';\nimport { BaseReranker } from './rerankers';\n\nconst chunker = {\n cleanText: (text: string): string => {\n if (!text) return '';\n\n /** Normalized all line endings to '\\n' */\n const normalizedText = text.replace(/\\r\\n/g, '\\n').replace(/\\r/g, '\\n');\n\n /** Handle multiple backslashes followed by newlines\n * This replaces patterns like '\\\\\\\\\\\\n' with a single newline */\n const fixedBackslashes = normalizedText.replace(/\\\\+\\n/g, '\\n');\n\n /** Cleaned up consecutive newlines, tabs, and spaces around newlines */\n const cleanedNewlines = fixedBackslashes.replace(/[\\t ]*\\n[\\t \\n]*/g, '\\n');\n\n /** Cleaned up excessive spaces and tabs */\n const cleanedSpaces = cleanedNewlines.replace(/[ \\t]+/g, ' ');\n\n return cleanedSpaces.trim();\n },\n splitText: async (\n text: string,\n options?: {\n chunkSize?: number;\n chunkOverlap?: number;\n separators?: string[];\n }\n ): Promise<string[]> => {\n const chunkSize = options?.chunkSize ?? 150;\n const chunkOverlap = options?.chunkOverlap ?? 50;\n const separators = options?.separators || ['\\n\\n', '\\n'];\n\n const splitter = new RecursiveCharacterTextSplitter({\n separators,\n chunkSize,\n chunkOverlap,\n });\n\n return await splitter.splitText(text);\n },\n\n splitTexts: async (\n texts: string[],\n options?: {\n chunkSize?: number;\n chunkOverlap?: number;\n separators?: string[];\n },\n logger?: t.Logger\n ): Promise<string[][]> => {\n // Split multiple texts\n const logger_ = logger || createDefaultLogger();\n const promises = texts.map((text) =>\n chunker.splitText(text, options).catch((error) => {\n logger_.error('Error splitting text:', error);\n return [text];\n })\n );\n return Promise.all(promises);\n },\n};\n\nfunction createSourceUpdateCallback(sourceMap: Map<string, t.ValidSource>) {\n return (link: string, update?: Partial<t.ValidSource>): void => {\n const source = sourceMap.get(link);\n if (source) {\n sourceMap.set(link, {\n ...source,\n ...update,\n });\n }\n };\n}\n\nconst getHighlights = async ({\n query,\n content,\n reranker,\n topResults = 5,\n logger,\n}: {\n content: string;\n query: string;\n reranker?: BaseReranker;\n topResults?: number;\n logger?: t.Logger;\n}): Promise<t.Highlight[] | undefined> => {\n const logger_ = logger || createDefaultLogger();\n\n if (!content) {\n logger_.warn('No content provided for highlights');\n return;\n }\n if (!reranker) {\n logger_.warn('No reranker provided for highlights');\n return;\n }\n\n try {\n const documents = await chunker.splitText(content);\n if (Array.isArray(documents)) {\n return await reranker.rerank(query, documents, topResults);\n } else {\n logger_.error(\n 'Expected documents to be an array, got:',\n typeof documents\n );\n return;\n }\n } catch (error) {\n logger_.error('Error in content processing:', error);\n return;\n }\n};\n\nconst createSerperAPI = (\n apiKey?: string\n): {\n getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;\n} => {\n const config = {\n apiKey: apiKey ?? process.env.SERPER_API_KEY,\n apiUrl: 'https://google.serper.dev/search',\n timeout: 10000,\n };\n\n if (config.apiKey == null || config.apiKey === '') {\n throw new Error('SERPER_API_KEY is required for SerperAPI');\n }\n\n const getSources = async ({\n query,\n date,\n country,\n safeSearch,\n numResults = 8,\n type,\n }: t.GetSourcesParams): Promise<t.SearchResult> => {\n if (!query.trim()) {\n return { success: false, error: 'Query cannot be empty' };\n }\n\n try {\n const safe = ['off', 'moderate', 'active'] as const;\n const payload: t.SerperSearchPayload = {\n q: query,\n safe: safe[safeSearch ?? 1],\n num: Math.min(Math.max(1, numResults), 10),\n };\n\n // Set the search type if provided\n if (type) {\n payload.type = type;\n }\n\n if (date != null) {\n payload.tbs = `qdr:${date}`;\n }\n\n if (country != null && country !== '') {\n payload['gl'] = country.toLowerCase();\n }\n\n // Determine the API endpoint based on the search type\n let apiEndpoint = config.apiUrl;\n if (type === 'images') {\n apiEndpoint = 'https://google.serper.dev/images';\n } else if (type === 'videos') {\n apiEndpoint = 'https://google.serper.dev/videos';\n } else if (type === 'news') {\n apiEndpoint = 'https://google.serper.dev/news';\n }\n\n const response = await axios.post<t.SerperResultData>(\n apiEndpoint,\n payload,\n {\n headers: {\n 'X-API-KEY': config.apiKey,\n 'Content-Type': 'application/json',\n },\n timeout: config.timeout,\n }\n );\n\n const data = response.data;\n const results: t.SearchResultData = {\n organic: data.organic,\n images: data.images ?? [],\n answerBox: data.answerBox,\n topStories: data.topStories ?? [],\n peopleAlsoAsk: data.peopleAlsoAsk,\n knowledgeGraph: data.knowledgeGraph,\n relatedSearches: data.relatedSearches,\n videos: data.videos ?? [],\n news: data.news ?? [],\n };\n\n return { success: true, data: results };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return { success: false, error: `API request failed: ${errorMessage}` };\n }\n };\n\n return { getSources };\n};\n\nconst createSearXNGAPI = (\n instanceUrl?: string,\n apiKey?: string\n): {\n getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;\n} => {\n const config = {\n instanceUrl: instanceUrl ?? process.env.SEARXNG_INSTANCE_URL,\n apiKey: apiKey ?? process.env.SEARXNG_API_KEY,\n defaultLocation: 'all',\n timeout: 10000,\n };\n\n if (config.instanceUrl == null || config.instanceUrl === '') {\n throw new Error('SEARXNG_INSTANCE_URL is required for SearXNG API');\n }\n\n const getSources = async ({\n query,\n numResults = 8,\n type,\n }: t.GetSourcesParams): Promise<t.SearchResult> => {\n if (!query.trim()) {\n return { success: false, error: 'Query cannot be empty' };\n }\n\n try {\n // Ensure the instance URL ends with /search\n if (config.instanceUrl == null || config.instanceUrl === '') {\n return { success: false, error: 'Instance URL is not defined' };\n }\n\n let searchUrl = config.instanceUrl;\n if (!searchUrl.endsWith('/search')) {\n searchUrl = searchUrl.replace(/\\/$/, '') + '/search';\n }\n\n // Determine the search category based on the type\n let category = 'general';\n if (type === 'images') {\n category = 'images';\n } else if (type === 'videos') {\n category = 'videos';\n } else if (type === 'news') {\n category = 'news';\n }\n\n // Prepare parameters for SearXNG\n const params: t.SearxNGSearchPayload = {\n q: query,\n format: 'json',\n pageno: 1,\n categories: category,\n language: 'all',\n safesearch: 0,\n engines: 'google,bing,duckduckgo',\n };\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n if (config.apiKey != null && config.apiKey !== '') {\n headers['X-API-Key'] = config.apiKey;\n }\n\n const response = await axios.get(searchUrl, {\n headers,\n params,\n timeout: config.timeout,\n });\n\n const data = response.data;\n\n // Transform SearXNG results to match SerperAPI format\n const organicResults = (data.results ?? [])\n .slice(0, numResults)\n .map((result: t.SearXNGResult) => ({\n title: result.title ?? '',\n link: result.url ?? '',\n snippet: result.content ?? '',\n date: result.publishedDate ?? '',\n }));\n\n // Extract image results if available\n const imageResults = (data.results ?? [])\n .filter((result: t.SearXNGResult) => result.img_src)\n .slice(0, 6)\n .map((result: t.SearXNGResult) => ({\n title: result.title ?? '',\n imageUrl: result.img_src ?? '',\n }));\n\n // Format results to match SerperAPI structure\n const results: t.SearchResultData = {\n organic: organicResults,\n images: imageResults,\n topStories: [],\n // Use undefined instead of null for optional properties\n relatedSearches: data.suggestions ?? [],\n videos: [],\n news: [],\n };\n\n return { success: true, data: results };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return {\n success: false,\n error: `SearXNG API request failed: ${errorMessage}`,\n };\n }\n };\n\n return { getSources };\n};\n\nexport const createSearchAPI = (\n config: t.SearchConfig\n): {\n getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;\n} => {\n const {\n searchProvider = 'serper',\n serperApiKey,\n searxngInstanceUrl,\n searxngApiKey,\n } = config;\n\n if (searchProvider.toLowerCase() === 'serper') {\n return createSerperAPI(serperApiKey);\n } else if (searchProvider.toLowerCase() === 'searxng') {\n return createSearXNGAPI(searxngInstanceUrl, searxngApiKey);\n } else {\n throw new Error(\n `Invalid search provider: ${searchProvider}. Must be 'serper' or 'searxng'`\n );\n }\n};\n\nexport const createSourceProcessor = (\n config: t.ProcessSourcesConfig = {},\n scraperInstance?: FirecrawlScraper\n): {\n processSources: (\n fields: t.ProcessSourcesFields\n ) => Promise<t.SearchResultData>;\n topResults: number;\n} => {\n if (!scraperInstance) {\n throw new Error('Firecrawl scraper instance is required');\n }\n const {\n topResults = 5,\n // strategies = ['no_extraction'],\n // filterContent = true,\n reranker,\n logger,\n } = config;\n\n const logger_ = logger || createDefaultLogger();\n const firecrawlScraper = scraperInstance;\n\n const webScraper = {\n scrapeMany: async ({\n query,\n links,\n onGetHighlights,\n }: {\n query: string;\n links: string[];\n onGetHighlights: t.SearchToolConfig['onGetHighlights'];\n }): Promise<Array<t.ScrapeResult>> => {\n logger_.debug(`Scraping ${links.length} links with Firecrawl`);\n const promises: Array<Promise<t.ScrapeResult>> = [];\n try {\n for (let i = 0; i < links.length; i++) {\n const currentLink = links[i];\n const promise: Promise<t.ScrapeResult> = firecrawlScraper\n .scrapeUrl(currentLink, {})\n .then(([url, response]) => {\n const attribution = getAttribution(\n url,\n response.data?.metadata,\n logger_\n );\n if (response.success && response.data) {\n const [content, references] =\n firecrawlScraper.extractContent(response);\n return {\n url,\n references,\n attribution,\n content: chunker.cleanText(content),\n } as t.ScrapeResult;\n }\n\n return {\n url,\n attribution,\n error: true,\n content: '',\n } as t.ScrapeResult;\n })\n .then(async (result) => {\n try {\n if (result.error != null) {\n logger_.error(\n `Error scraping ${result.url}: ${result.content}`,\n result.error\n );\n return {\n ...result,\n };\n }\n const highlights = await getHighlights({\n query,\n reranker,\n content: result.content,\n logger: logger_,\n });\n if (onGetHighlights) {\n onGetHighlights(result.url);\n }\n return {\n ...result,\n highlights,\n };\n } catch (error) {\n logger_.error('Error processing scraped content:', error);\n return {\n ...result,\n };\n }\n })\n .catch((error) => {\n logger_.error(`Error scraping ${currentLink}:`, error);\n return {\n url: currentLink,\n error: true,\n content: '',\n };\n });\n promises.push(promise);\n }\n return await Promise.all(promises);\n } catch (error) {\n logger_.error('Error in scrapeMany:', error);\n return [];\n }\n },\n };\n\n const fetchContents = async ({\n links,\n query,\n target,\n onGetHighlights,\n onContentScraped,\n }: {\n links: string[];\n query: string;\n target: number;\n onGetHighlights: t.SearchToolConfig['onGetHighlights'];\n onContentScraped?: (link: string, update?: Partial<t.ValidSource>) => void;\n }): Promise<void> => {\n const initialLinks = links.slice(0, target);\n // const remainingLinks = links.slice(target).reverse();\n const results = await webScraper.scrapeMany({\n query,\n links: initialLinks,\n onGetHighlights,\n });\n for (const result of results) {\n if (result.error === true) {\n continue;\n }\n const { url, content, attribution, references, highlights } = result;\n onContentScraped?.(url, {\n content,\n attribution,\n references,\n highlights,\n });\n }\n };\n\n const processSources = async ({\n result,\n numElements,\n query,\n news,\n proMode = true,\n onGetHighlights,\n }: t.ProcessSourcesFields): Promise<t.SearchResultData> => {\n try {\n if (!result.data) {\n return {\n organic: [],\n topStories: [],\n images: [],\n relatedSearches: [],\n };\n } else if (!result.data.organic) {\n return result.data;\n }\n\n if (!proMode) {\n const wikiSources = result.data.organic.filter((source) =>\n source.link.includes('wikipedia.org')\n );\n\n if (!wikiSources.length) {\n return result.data;\n }\n\n const wikiSourceMap = new Map<string, t.ValidSource>();\n wikiSourceMap.set(wikiSources[0].link, wikiSources[0]);\n const onContentScraped = createSourceUpdateCallback(wikiSourceMap);\n await fetchContents({\n query,\n target: 1,\n onGetHighlights,\n onContentScraped,\n links: [wikiSources[0].link],\n });\n\n for (let i = 0; i < result.data.organic.length; i++) {\n const source = result.data.organic[i];\n const updatedSource = wikiSourceMap.get(source.link);\n if (updatedSource) {\n result.data.organic[i] = {\n ...source,\n ...updatedSource,\n };\n }\n }\n\n return result.data;\n }\n\n const sourceMap = new Map<string, t.ValidSource>();\n const organicLinksSet = new Set<string>();\n\n // Collect organic links\n const organicLinks = collectLinks(\n result.data.organic,\n sourceMap,\n organicLinksSet\n );\n\n // Collect top story links, excluding any that are already in organic links\n const topStories = result.data.topStories ?? [];\n const topStoryLinks = collectLinks(\n topStories,\n sourceMap,\n organicLinksSet\n );\n\n if (organicLinks.length === 0 && (topStoryLinks.length === 0 || !news)) {\n return result.data;\n }\n\n const onContentScraped = createSourceUpdateCallback(sourceMap);\n const promises: Promise<void>[] = [];\n\n // Process organic links\n if (organicLinks.length > 0) {\n promises.push(\n fetchContents({\n query,\n onGetHighlights,\n onContentScraped,\n links: organicLinks,\n target: numElements,\n })\n );\n }\n\n // Process top story links\n if (news && topStoryLinks.length > 0) {\n promises.push(\n fetchContents({\n query,\n onGetHighlights,\n onContentScraped,\n links: topStoryLinks,\n target: numElements,\n })\n );\n }\n\n await Promise.all(promises);\n\n if (result.data.organic.length > 0) {\n updateSourcesWithContent(result.data.organic, sourceMap);\n }\n\n if (news && topStories.length > 0) {\n updateSourcesWithContent(topStories, sourceMap);\n }\n\n return result.data;\n } catch (error) {\n logger_.error('Error in processSources:', error);\n return {\n organic: [],\n topStories: [],\n images: [],\n relatedSearches: [],\n ...result.data,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n };\n\n return {\n processSources,\n topResults,\n };\n};\n\n/** Helper function to collect links and update sourceMap */\nfunction collectLinks(\n sources: Array<t.OrganicResult | t.TopStoryResult>,\n sourceMap: Map<string, t.ValidSource>,\n existingLinksSet?: Set<string>\n): string[] {\n const links: string[] = [];\n\n for (const source of sources) {\n if (source.link) {\n // For topStories, only add if not already in organic links\n if (existingLinksSet && existingLinksSet.has(source.link)) {\n continue;\n }\n\n links.push(source.link);\n if (existingLinksSet) {\n existingLinksSet.add(source.link);\n }\n sourceMap.set(source.link, source as t.ValidSource);\n }\n }\n\n return links;\n}\n\n/** Helper function to update sources with scraped content */\nfunction updateSourcesWithContent<T extends t.ValidSource>(\n sources: T[],\n sourceMap: Map<string, t.ValidSource>\n): void {\n for (let i = 0; i < sources.length; i++) {\n const source = sources[i];\n const updatedSource = sourceMap.get(source.link);\n if (updatedSource) {\n sources[i] = {\n ...source,\n ...updatedSource,\n } as T;\n }\n }\n}\n"],"names":[],"mappings":";;;;AAOA,MAAM,OAAO,GAAG;AACd,IAAA,SAAS,EAAE,CAAC,IAAY,KAAY;AAClC,QAAA,IAAI,CAAC,IAAI;AAAE,YAAA,OAAO,EAAE;;AAGpB,QAAA,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC;AAEvE;AACiE;QACjE,MAAM,gBAAgB,GAAG,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC;;QAG/D,MAAM,eAAe,GAAG,gBAAgB,CAAC,OAAO,CAAC,mBAAmB,EAAE,IAAI,CAAC;;QAG3E,MAAM,aAAa,GAAG,eAAe,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC;AAE7D,QAAA,OAAO,aAAa,CAAC,IAAI,EAAE;KAC5B;AACD,IAAA,SAAS,EAAE,OACT,IAAY,EACZ,OAIC,KACoB;AACrB,QAAA,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,GAAG;AAC3C,QAAA,MAAM,YAAY,GAAG,OAAO,EAAE,YAAY,IAAI,EAAE;QAChD,MAAM,UAAU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