@librechat/agents 2.4.30 → 2.4.33

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.
Files changed (124) hide show
  1. package/dist/cjs/common/enum.cjs +1 -0
  2. package/dist/cjs/common/enum.cjs.map +1 -1
  3. package/dist/cjs/events.cjs +3 -3
  4. package/dist/cjs/events.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +2 -1
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/main.cjs +7 -2
  8. package/dist/cjs/main.cjs.map +1 -1
  9. package/dist/cjs/messages/ids.cjs +23 -0
  10. package/dist/cjs/messages/ids.cjs.map +1 -0
  11. package/dist/cjs/splitStream.cjs +2 -1
  12. package/dist/cjs/splitStream.cjs.map +1 -1
  13. package/dist/cjs/stream.cjs +87 -154
  14. package/dist/cjs/stream.cjs.map +1 -1
  15. package/dist/cjs/tools/ToolNode.cjs +14 -3
  16. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  17. package/dist/cjs/tools/handlers.cjs +144 -0
  18. package/dist/cjs/tools/handlers.cjs.map +1 -0
  19. package/dist/cjs/tools/search/content.cjs +140 -0
  20. package/dist/cjs/tools/search/content.cjs.map +1 -0
  21. package/dist/cjs/tools/search/firecrawl.cjs +131 -0
  22. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
  23. package/dist/cjs/tools/search/format.cjs +203 -0
  24. package/dist/cjs/tools/search/format.cjs.map +1 -0
  25. package/dist/cjs/tools/search/highlights.cjs +245 -0
  26. package/dist/cjs/tools/search/highlights.cjs.map +1 -0
  27. package/dist/cjs/tools/search/rerankers.cjs +194 -0
  28. package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
  29. package/dist/cjs/tools/search/schema.cjs +70 -0
  30. package/dist/cjs/tools/search/schema.cjs.map +1 -0
  31. package/dist/cjs/tools/search/search.cjs +491 -0
  32. package/dist/cjs/tools/search/search.cjs.map +1 -0
  33. package/dist/cjs/tools/search/tool.cjs +292 -0
  34. package/dist/cjs/tools/search/tool.cjs.map +1 -0
  35. package/dist/cjs/tools/search/utils.cjs +66 -0
  36. package/dist/cjs/tools/search/utils.cjs.map +1 -0
  37. package/dist/esm/common/enum.mjs +1 -0
  38. package/dist/esm/common/enum.mjs.map +1 -1
  39. package/dist/esm/events.mjs +1 -1
  40. package/dist/esm/events.mjs.map +1 -1
  41. package/dist/esm/graphs/Graph.mjs +2 -1
  42. package/dist/esm/graphs/Graph.mjs.map +1 -1
  43. package/dist/esm/main.mjs +4 -1
  44. package/dist/esm/main.mjs.map +1 -1
  45. package/dist/esm/messages/ids.mjs +21 -0
  46. package/dist/esm/messages/ids.mjs.map +1 -0
  47. package/dist/esm/splitStream.mjs +2 -1
  48. package/dist/esm/splitStream.mjs.map +1 -1
  49. package/dist/esm/stream.mjs +87 -152
  50. package/dist/esm/stream.mjs.map +1 -1
  51. package/dist/esm/tools/ToolNode.mjs +14 -3
  52. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  53. package/dist/esm/tools/handlers.mjs +141 -0
  54. package/dist/esm/tools/handlers.mjs.map +1 -0
  55. package/dist/esm/tools/search/content.mjs +119 -0
  56. package/dist/esm/tools/search/content.mjs.map +1 -0
  57. package/dist/esm/tools/search/firecrawl.mjs +128 -0
  58. package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
  59. package/dist/esm/tools/search/format.mjs +201 -0
  60. package/dist/esm/tools/search/format.mjs.map +1 -0
  61. package/dist/esm/tools/search/highlights.mjs +243 -0
  62. package/dist/esm/tools/search/highlights.mjs.map +1 -0
  63. package/dist/esm/tools/search/rerankers.mjs +188 -0
  64. package/dist/esm/tools/search/rerankers.mjs.map +1 -0
  65. package/dist/esm/tools/search/schema.mjs +61 -0
  66. package/dist/esm/tools/search/schema.mjs.map +1 -0
  67. package/dist/esm/tools/search/search.mjs +488 -0
  68. package/dist/esm/tools/search/search.mjs.map +1 -0
  69. package/dist/esm/tools/search/tool.mjs +290 -0
  70. package/dist/esm/tools/search/tool.mjs.map +1 -0
  71. package/dist/esm/tools/search/utils.mjs +61 -0
  72. package/dist/esm/tools/search/utils.mjs.map +1 -0
  73. package/dist/types/common/enum.d.ts +1 -0
  74. package/dist/types/graphs/Graph.d.ts +1 -1
  75. package/dist/types/index.d.ts +2 -0
  76. package/dist/types/messages/ids.d.ts +3 -0
  77. package/dist/types/messages/index.d.ts +1 -0
  78. package/dist/types/scripts/search.d.ts +1 -0
  79. package/dist/types/stream.d.ts +0 -8
  80. package/dist/types/tools/ToolNode.d.ts +6 -0
  81. package/dist/types/tools/example.d.ts +23 -3
  82. package/dist/types/tools/handlers.d.ts +8 -0
  83. package/dist/types/tools/search/content.d.ts +4 -0
  84. package/dist/types/tools/search/firecrawl.d.ts +38 -0
  85. package/dist/types/tools/search/format.d.ts +5 -0
  86. package/dist/types/tools/search/highlights.d.ts +13 -0
  87. package/dist/types/tools/search/index.d.ts +2 -0
  88. package/dist/types/tools/search/rerankers.d.ts +36 -0
  89. package/dist/types/tools/search/schema.d.ts +16 -0
  90. package/dist/types/tools/search/search.d.ts +9 -0
  91. package/dist/types/tools/search/test.d.ts +1 -0
  92. package/dist/types/tools/search/tool.d.ts +33 -0
  93. package/dist/types/tools/search/types.d.ts +540 -0
  94. package/dist/types/tools/search/utils.d.ts +10 -0
  95. package/package.json +10 -7
  96. package/src/common/enum.ts +1 -0
  97. package/src/events.ts +49 -15
  98. package/src/graphs/Graph.ts +6 -2
  99. package/src/index.ts +2 -0
  100. package/src/messages/ids.ts +26 -0
  101. package/src/messages/index.ts +1 -0
  102. package/src/scripts/search.ts +146 -0
  103. package/src/splitStream.test.ts +132 -71
  104. package/src/splitStream.ts +2 -1
  105. package/src/stream.ts +94 -183
  106. package/src/tools/ToolNode.ts +37 -14
  107. package/src/tools/handlers.ts +167 -0
  108. package/src/tools/search/content.test.ts +173 -0
  109. package/src/tools/search/content.ts +147 -0
  110. package/src/tools/search/firecrawl.ts +158 -0
  111. package/src/tools/search/format.ts +252 -0
  112. package/src/tools/search/highlights.ts +320 -0
  113. package/src/tools/search/index.ts +2 -0
  114. package/src/tools/search/output.md +2775 -0
  115. package/src/tools/search/rerankers.ts +269 -0
  116. package/src/tools/search/schema.ts +63 -0
  117. package/src/tools/search/search.ts +680 -0
  118. package/src/tools/search/test.html +884 -0
  119. package/src/tools/search/test.md +643 -0
  120. package/src/tools/search/test.ts +159 -0
  121. package/src/tools/search/tool.ts +427 -0
  122. package/src/tools/search/types.ts +621 -0
  123. package/src/tools/search/utils.ts +79 -0
  124. package/src/utils/llmConfig.ts +1 -1
@@ -0,0 +1,680 @@
1
+ import axios from 'axios';
2
+ import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
3
+ import type * as t from './types';
4
+ import { getAttribution, createDefaultLogger } from './utils';
5
+ import { FirecrawlScraper } from './firecrawl';
6
+ import { BaseReranker } from './rerankers';
7
+
8
+ const chunker = {
9
+ cleanText: (text: string): string => {
10
+ if (!text) return '';
11
+
12
+ /** Normalized all line endings to '\n' */
13
+ const normalizedText = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
14
+
15
+ /** Handle multiple backslashes followed by newlines
16
+ * This replaces patterns like '\\\\\\n' with a single newline */
17
+ const fixedBackslashes = normalizedText.replace(/\\+\n/g, '\n');
18
+
19
+ /** Cleaned up consecutive newlines, tabs, and spaces around newlines */
20
+ const cleanedNewlines = fixedBackslashes.replace(/[\t ]*\n[\t \n]*/g, '\n');
21
+
22
+ /** Cleaned up excessive spaces and tabs */
23
+ const cleanedSpaces = cleanedNewlines.replace(/[ \t]+/g, ' ');
24
+
25
+ return cleanedSpaces.trim();
26
+ },
27
+ splitText: async (
28
+ text: string,
29
+ options?: {
30
+ chunkSize?: number;
31
+ chunkOverlap?: number;
32
+ separators?: string[];
33
+ }
34
+ ): Promise<string[]> => {
35
+ const chunkSize = options?.chunkSize ?? 150;
36
+ const chunkOverlap = options?.chunkOverlap ?? 50;
37
+ const separators = options?.separators || ['\n\n', '\n'];
38
+
39
+ const splitter = new RecursiveCharacterTextSplitter({
40
+ separators,
41
+ chunkSize,
42
+ chunkOverlap,
43
+ });
44
+
45
+ return await splitter.splitText(text);
46
+ },
47
+
48
+ splitTexts: async (
49
+ texts: string[],
50
+ options?: {
51
+ chunkSize?: number;
52
+ chunkOverlap?: number;
53
+ separators?: string[];
54
+ },
55
+ logger?: t.Logger
56
+ ): Promise<string[][]> => {
57
+ // Split multiple texts
58
+ const logger_ = logger || createDefaultLogger();
59
+ const promises = texts.map((text) =>
60
+ chunker.splitText(text, options).catch((error) => {
61
+ logger_.error('Error splitting text:', error);
62
+ return [text];
63
+ })
64
+ );
65
+ return Promise.all(promises);
66
+ },
67
+ };
68
+
69
+ function createSourceUpdateCallback(sourceMap: Map<string, t.ValidSource>) {
70
+ return (link: string, update?: Partial<t.ValidSource>): void => {
71
+ const source = sourceMap.get(link);
72
+ if (source) {
73
+ sourceMap.set(link, {
74
+ ...source,
75
+ ...update,
76
+ });
77
+ }
78
+ };
79
+ }
80
+
81
+ const getHighlights = async ({
82
+ query,
83
+ content,
84
+ reranker,
85
+ topResults = 5,
86
+ logger,
87
+ }: {
88
+ content: string;
89
+ query: string;
90
+ reranker?: BaseReranker;
91
+ topResults?: number;
92
+ logger?: t.Logger;
93
+ }): Promise<t.Highlight[] | undefined> => {
94
+ const logger_ = logger || createDefaultLogger();
95
+
96
+ if (!content) {
97
+ logger_.warn('No content provided for highlights');
98
+ return;
99
+ }
100
+ if (!reranker) {
101
+ logger_.warn('No reranker provided for highlights');
102
+ return;
103
+ }
104
+
105
+ try {
106
+ const documents = await chunker.splitText(content);
107
+ if (Array.isArray(documents)) {
108
+ return await reranker.rerank(query, documents, topResults);
109
+ } else {
110
+ logger_.error(
111
+ 'Expected documents to be an array, got:',
112
+ typeof documents
113
+ );
114
+ return;
115
+ }
116
+ } catch (error) {
117
+ logger_.error('Error in content processing:', error);
118
+ return;
119
+ }
120
+ };
121
+
122
+ const createSerperAPI = (
123
+ apiKey?: string
124
+ ): {
125
+ getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;
126
+ } => {
127
+ const config = {
128
+ apiKey: apiKey ?? process.env.SERPER_API_KEY,
129
+ apiUrl: 'https://google.serper.dev/search',
130
+ timeout: 10000,
131
+ };
132
+
133
+ if (config.apiKey == null || config.apiKey === '') {
134
+ throw new Error('SERPER_API_KEY is required for SerperAPI');
135
+ }
136
+
137
+ const getSources = async ({
138
+ query,
139
+ date,
140
+ country,
141
+ safeSearch,
142
+ numResults = 8,
143
+ type,
144
+ }: t.GetSourcesParams): Promise<t.SearchResult> => {
145
+ if (!query.trim()) {
146
+ return { success: false, error: 'Query cannot be empty' };
147
+ }
148
+
149
+ try {
150
+ const safe = ['off', 'moderate', 'active'] as const;
151
+ const payload: t.SerperSearchPayload = {
152
+ q: query,
153
+ safe: safe[safeSearch ?? 1],
154
+ num: Math.min(Math.max(1, numResults), 10),
155
+ };
156
+
157
+ // Set the search type if provided
158
+ if (type) {
159
+ payload.type = type;
160
+ }
161
+
162
+ if (date != null) {
163
+ payload.tbs = `qdr:${date}`;
164
+ }
165
+
166
+ if (country != null && country !== '') {
167
+ payload['gl'] = country.toLowerCase();
168
+ }
169
+
170
+ // Determine the API endpoint based on the search type
171
+ let apiEndpoint = config.apiUrl;
172
+ if (type === 'images') {
173
+ apiEndpoint = 'https://google.serper.dev/images';
174
+ } else if (type === 'videos') {
175
+ apiEndpoint = 'https://google.serper.dev/videos';
176
+ } else if (type === 'news') {
177
+ apiEndpoint = 'https://google.serper.dev/news';
178
+ }
179
+
180
+ const response = await axios.post<t.SerperResultData>(
181
+ apiEndpoint,
182
+ payload,
183
+ {
184
+ headers: {
185
+ 'X-API-KEY': config.apiKey,
186
+ 'Content-Type': 'application/json',
187
+ },
188
+ timeout: config.timeout,
189
+ }
190
+ );
191
+
192
+ const data = response.data;
193
+ const results: t.SearchResultData = {
194
+ organic: data.organic,
195
+ images: data.images ?? [],
196
+ answerBox: data.answerBox,
197
+ topStories: data.topStories ?? [],
198
+ peopleAlsoAsk: data.peopleAlsoAsk,
199
+ knowledgeGraph: data.knowledgeGraph,
200
+ relatedSearches: data.relatedSearches,
201
+ videos: data.videos ?? [],
202
+ news: data.news ?? [],
203
+ };
204
+
205
+ return { success: true, data: results };
206
+ } catch (error) {
207
+ const errorMessage =
208
+ error instanceof Error ? error.message : String(error);
209
+ return { success: false, error: `API request failed: ${errorMessage}` };
210
+ }
211
+ };
212
+
213
+ return { getSources };
214
+ };
215
+
216
+ const createSearXNGAPI = (
217
+ instanceUrl?: string,
218
+ apiKey?: string
219
+ ): {
220
+ getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;
221
+ } => {
222
+ const config = {
223
+ instanceUrl: instanceUrl ?? process.env.SEARXNG_INSTANCE_URL,
224
+ apiKey: apiKey ?? process.env.SEARXNG_API_KEY,
225
+ defaultLocation: 'all',
226
+ timeout: 10000,
227
+ };
228
+
229
+ if (config.instanceUrl == null || config.instanceUrl === '') {
230
+ throw new Error('SEARXNG_INSTANCE_URL is required for SearXNG API');
231
+ }
232
+
233
+ const getSources = async ({
234
+ query,
235
+ numResults = 8,
236
+ type,
237
+ }: t.GetSourcesParams): Promise<t.SearchResult> => {
238
+ if (!query.trim()) {
239
+ return { success: false, error: 'Query cannot be empty' };
240
+ }
241
+
242
+ try {
243
+ // Ensure the instance URL ends with /search
244
+ if (config.instanceUrl == null || config.instanceUrl === '') {
245
+ return { success: false, error: 'Instance URL is not defined' };
246
+ }
247
+
248
+ let searchUrl = config.instanceUrl;
249
+ if (!searchUrl.endsWith('/search')) {
250
+ searchUrl = searchUrl.replace(/\/$/, '') + '/search';
251
+ }
252
+
253
+ // Determine the search category based on the type
254
+ let category = 'general';
255
+ if (type === 'images') {
256
+ category = 'images';
257
+ } else if (type === 'videos') {
258
+ category = 'videos';
259
+ } else if (type === 'news') {
260
+ category = 'news';
261
+ }
262
+
263
+ // Prepare parameters for SearXNG
264
+ const params: t.SearxNGSearchPayload = {
265
+ q: query,
266
+ format: 'json',
267
+ pageno: 1,
268
+ categories: category,
269
+ language: 'all',
270
+ safesearch: 0,
271
+ engines: 'google,bing,duckduckgo',
272
+ };
273
+
274
+ const headers: Record<string, string> = {
275
+ 'Content-Type': 'application/json',
276
+ };
277
+
278
+ if (config.apiKey != null && config.apiKey !== '') {
279
+ headers['X-API-Key'] = config.apiKey;
280
+ }
281
+
282
+ const response = await axios.get(searchUrl, {
283
+ headers,
284
+ params,
285
+ timeout: config.timeout,
286
+ });
287
+
288
+ const data = response.data;
289
+
290
+ // Transform SearXNG results to match SerperAPI format
291
+ const organicResults = (data.results ?? [])
292
+ .slice(0, numResults)
293
+ .map((result: t.SearXNGResult) => ({
294
+ title: result.title ?? '',
295
+ link: result.url ?? '',
296
+ snippet: result.content ?? '',
297
+ date: result.publishedDate ?? '',
298
+ }));
299
+
300
+ // Extract image results if available
301
+ const imageResults = (data.results ?? [])
302
+ .filter((result: t.SearXNGResult) => result.img_src)
303
+ .slice(0, 6)
304
+ .map((result: t.SearXNGResult) => ({
305
+ title: result.title ?? '',
306
+ imageUrl: result.img_src ?? '',
307
+ }));
308
+
309
+ // Format results to match SerperAPI structure
310
+ const results: t.SearchResultData = {
311
+ organic: organicResults,
312
+ images: imageResults,
313
+ topStories: [],
314
+ // Use undefined instead of null for optional properties
315
+ relatedSearches: data.suggestions ?? [],
316
+ videos: [],
317
+ news: [],
318
+ };
319
+
320
+ return { success: true, data: results };
321
+ } catch (error) {
322
+ const errorMessage =
323
+ error instanceof Error ? error.message : String(error);
324
+ return {
325
+ success: false,
326
+ error: `SearXNG API request failed: ${errorMessage}`,
327
+ };
328
+ }
329
+ };
330
+
331
+ return { getSources };
332
+ };
333
+
334
+ export const createSearchAPI = (
335
+ config: t.SearchConfig
336
+ ): {
337
+ getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;
338
+ } => {
339
+ const {
340
+ searchProvider = 'serper',
341
+ serperApiKey,
342
+ searxngInstanceUrl,
343
+ searxngApiKey,
344
+ } = config;
345
+
346
+ if (searchProvider.toLowerCase() === 'serper') {
347
+ return createSerperAPI(serperApiKey);
348
+ } else if (searchProvider.toLowerCase() === 'searxng') {
349
+ return createSearXNGAPI(searxngInstanceUrl, searxngApiKey);
350
+ } else {
351
+ throw new Error(
352
+ `Invalid search provider: ${searchProvider}. Must be 'serper' or 'searxng'`
353
+ );
354
+ }
355
+ };
356
+
357
+ export const createSourceProcessor = (
358
+ config: t.ProcessSourcesConfig = {},
359
+ scraperInstance?: FirecrawlScraper
360
+ ): {
361
+ processSources: (
362
+ fields: t.ProcessSourcesFields
363
+ ) => Promise<t.SearchResultData>;
364
+ topResults: number;
365
+ } => {
366
+ if (!scraperInstance) {
367
+ throw new Error('Firecrawl scraper instance is required');
368
+ }
369
+ const {
370
+ topResults = 5,
371
+ // strategies = ['no_extraction'],
372
+ // filterContent = true,
373
+ reranker,
374
+ logger,
375
+ } = config;
376
+
377
+ const logger_ = logger || createDefaultLogger();
378
+ const firecrawlScraper = scraperInstance;
379
+
380
+ const webScraper = {
381
+ scrapeMany: async ({
382
+ query,
383
+ links,
384
+ onGetHighlights,
385
+ }: {
386
+ query: string;
387
+ links: string[];
388
+ onGetHighlights: t.SearchToolConfig['onGetHighlights'];
389
+ }): Promise<Array<t.ScrapeResult>> => {
390
+ logger_.debug(`Scraping ${links.length} links with Firecrawl`);
391
+ const promises: Array<Promise<t.ScrapeResult>> = [];
392
+ try {
393
+ for (let i = 0; i < links.length; i++) {
394
+ const currentLink = links[i];
395
+ const promise: Promise<t.ScrapeResult> = firecrawlScraper
396
+ .scrapeUrl(currentLink, {})
397
+ .then(([url, response]) => {
398
+ const attribution = getAttribution(
399
+ url,
400
+ response.data?.metadata,
401
+ logger_
402
+ );
403
+ if (response.success && response.data) {
404
+ const [content, references] =
405
+ firecrawlScraper.extractContent(response);
406
+ return {
407
+ url,
408
+ references,
409
+ attribution,
410
+ content: chunker.cleanText(content),
411
+ } as t.ScrapeResult;
412
+ }
413
+
414
+ return {
415
+ url,
416
+ attribution,
417
+ error: true,
418
+ content: '',
419
+ } as t.ScrapeResult;
420
+ })
421
+ .then(async (result) => {
422
+ try {
423
+ if (result.error != null) {
424
+ logger_.error(
425
+ `Error scraping ${result.url}: ${result.content}`,
426
+ result.error
427
+ );
428
+ return {
429
+ ...result,
430
+ };
431
+ }
432
+ const highlights = await getHighlights({
433
+ query,
434
+ reranker,
435
+ content: result.content,
436
+ logger: logger_,
437
+ });
438
+ if (onGetHighlights) {
439
+ onGetHighlights(result.url);
440
+ }
441
+ return {
442
+ ...result,
443
+ highlights,
444
+ };
445
+ } catch (error) {
446
+ logger_.error('Error processing scraped content:', error);
447
+ return {
448
+ ...result,
449
+ };
450
+ }
451
+ })
452
+ .catch((error) => {
453
+ logger_.error(`Error scraping ${currentLink}:`, error);
454
+ return {
455
+ url: currentLink,
456
+ error: true,
457
+ content: '',
458
+ };
459
+ });
460
+ promises.push(promise);
461
+ }
462
+ return await Promise.all(promises);
463
+ } catch (error) {
464
+ logger_.error('Error in scrapeMany:', error);
465
+ return [];
466
+ }
467
+ },
468
+ };
469
+
470
+ const fetchContents = async ({
471
+ links,
472
+ query,
473
+ target,
474
+ onGetHighlights,
475
+ onContentScraped,
476
+ }: {
477
+ links: string[];
478
+ query: string;
479
+ target: number;
480
+ onGetHighlights: t.SearchToolConfig['onGetHighlights'];
481
+ onContentScraped?: (link: string, update?: Partial<t.ValidSource>) => void;
482
+ }): Promise<void> => {
483
+ const initialLinks = links.slice(0, target);
484
+ // const remainingLinks = links.slice(target).reverse();
485
+ const results = await webScraper.scrapeMany({
486
+ query,
487
+ links: initialLinks,
488
+ onGetHighlights,
489
+ });
490
+ for (const result of results) {
491
+ if (result.error === true) {
492
+ continue;
493
+ }
494
+ const { url, content, attribution, references, highlights } = result;
495
+ onContentScraped?.(url, {
496
+ content,
497
+ attribution,
498
+ references,
499
+ highlights,
500
+ });
501
+ }
502
+ };
503
+
504
+ const processSources = async ({
505
+ result,
506
+ numElements,
507
+ query,
508
+ news,
509
+ proMode = true,
510
+ onGetHighlights,
511
+ }: t.ProcessSourcesFields): Promise<t.SearchResultData> => {
512
+ try {
513
+ if (!result.data) {
514
+ return {
515
+ organic: [],
516
+ topStories: [],
517
+ images: [],
518
+ relatedSearches: [],
519
+ };
520
+ } else if (!result.data.organic) {
521
+ return result.data;
522
+ }
523
+
524
+ if (!proMode) {
525
+ const wikiSources = result.data.organic.filter((source) =>
526
+ source.link.includes('wikipedia.org')
527
+ );
528
+
529
+ if (!wikiSources.length) {
530
+ return result.data;
531
+ }
532
+
533
+ const wikiSourceMap = new Map<string, t.ValidSource>();
534
+ wikiSourceMap.set(wikiSources[0].link, wikiSources[0]);
535
+ const onContentScraped = createSourceUpdateCallback(wikiSourceMap);
536
+ await fetchContents({
537
+ query,
538
+ target: 1,
539
+ onGetHighlights,
540
+ onContentScraped,
541
+ links: [wikiSources[0].link],
542
+ });
543
+
544
+ for (let i = 0; i < result.data.organic.length; i++) {
545
+ const source = result.data.organic[i];
546
+ const updatedSource = wikiSourceMap.get(source.link);
547
+ if (updatedSource) {
548
+ result.data.organic[i] = {
549
+ ...source,
550
+ ...updatedSource,
551
+ };
552
+ }
553
+ }
554
+
555
+ return result.data;
556
+ }
557
+
558
+ const sourceMap = new Map<string, t.ValidSource>();
559
+ const organicLinksSet = new Set<string>();
560
+
561
+ // Collect organic links
562
+ const organicLinks = collectLinks(
563
+ result.data.organic,
564
+ sourceMap,
565
+ organicLinksSet
566
+ );
567
+
568
+ // Collect top story links, excluding any that are already in organic links
569
+ const topStories = result.data.topStories ?? [];
570
+ const topStoryLinks = collectLinks(
571
+ topStories,
572
+ sourceMap,
573
+ organicLinksSet
574
+ );
575
+
576
+ if (organicLinks.length === 0 && (topStoryLinks.length === 0 || !news)) {
577
+ return result.data;
578
+ }
579
+
580
+ const onContentScraped = createSourceUpdateCallback(sourceMap);
581
+ const promises: Promise<void>[] = [];
582
+
583
+ // Process organic links
584
+ if (organicLinks.length > 0) {
585
+ promises.push(
586
+ fetchContents({
587
+ query,
588
+ onGetHighlights,
589
+ onContentScraped,
590
+ links: organicLinks,
591
+ target: numElements,
592
+ })
593
+ );
594
+ }
595
+
596
+ // Process top story links
597
+ if (news && topStoryLinks.length > 0) {
598
+ promises.push(
599
+ fetchContents({
600
+ query,
601
+ onGetHighlights,
602
+ onContentScraped,
603
+ links: topStoryLinks,
604
+ target: numElements,
605
+ })
606
+ );
607
+ }
608
+
609
+ await Promise.all(promises);
610
+
611
+ if (result.data.organic.length > 0) {
612
+ updateSourcesWithContent(result.data.organic, sourceMap);
613
+ }
614
+
615
+ if (news && topStories.length > 0) {
616
+ updateSourcesWithContent(topStories, sourceMap);
617
+ }
618
+
619
+ return result.data;
620
+ } catch (error) {
621
+ logger_.error('Error in processSources:', error);
622
+ return {
623
+ organic: [],
624
+ topStories: [],
625
+ images: [],
626
+ relatedSearches: [],
627
+ ...result.data,
628
+ error: error instanceof Error ? error.message : String(error),
629
+ };
630
+ }
631
+ };
632
+
633
+ return {
634
+ processSources,
635
+ topResults,
636
+ };
637
+ };
638
+
639
+ /** Helper function to collect links and update sourceMap */
640
+ function collectLinks(
641
+ sources: Array<t.OrganicResult | t.TopStoryResult>,
642
+ sourceMap: Map<string, t.ValidSource>,
643
+ existingLinksSet?: Set<string>
644
+ ): string[] {
645
+ const links: string[] = [];
646
+
647
+ for (const source of sources) {
648
+ if (source.link) {
649
+ // For topStories, only add if not already in organic links
650
+ if (existingLinksSet && existingLinksSet.has(source.link)) {
651
+ continue;
652
+ }
653
+
654
+ links.push(source.link);
655
+ if (existingLinksSet) {
656
+ existingLinksSet.add(source.link);
657
+ }
658
+ sourceMap.set(source.link, source as t.ValidSource);
659
+ }
660
+ }
661
+
662
+ return links;
663
+ }
664
+
665
+ /** Helper function to update sources with scraped content */
666
+ function updateSourcesWithContent<T extends t.ValidSource>(
667
+ sources: T[],
668
+ sourceMap: Map<string, t.ValidSource>
669
+ ): void {
670
+ for (let i = 0; i < sources.length; i++) {
671
+ const source = sources[i];
672
+ const updatedSource = sourceMap.get(source.link);
673
+ if (updatedSource) {
674
+ sources[i] = {
675
+ ...source,
676
+ ...updatedSource,
677
+ } as T;
678
+ }
679
+ }
680
+ }