@librechat/agents 2.4.30 → 2.4.311
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/common/enum.cjs +1 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/main.cjs +2 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/search/firecrawl.cjs +149 -0
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
- package/dist/cjs/tools/search/format.cjs +116 -0
- package/dist/cjs/tools/search/format.cjs.map +1 -0
- package/dist/cjs/tools/search/highlights.cjs +194 -0
- package/dist/cjs/tools/search/highlights.cjs.map +1 -0
- package/dist/cjs/tools/search/rerankers.cjs +187 -0
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
- package/dist/cjs/tools/search/search.cjs +410 -0
- package/dist/cjs/tools/search/search.cjs.map +1 -0
- package/dist/cjs/tools/search/tool.cjs +103 -0
- package/dist/cjs/tools/search/tool.cjs.map +1 -0
- package/dist/esm/common/enum.mjs +1 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/tools/search/firecrawl.mjs +145 -0
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
- package/dist/esm/tools/search/format.mjs +114 -0
- package/dist/esm/tools/search/format.mjs.map +1 -0
- package/dist/esm/tools/search/highlights.mjs +192 -0
- package/dist/esm/tools/search/highlights.mjs.map +1 -0
- package/dist/esm/tools/search/rerankers.mjs +181 -0
- package/dist/esm/tools/search/rerankers.mjs.map +1 -0
- package/dist/esm/tools/search/search.mjs +407 -0
- package/dist/esm/tools/search/search.mjs.map +1 -0
- package/dist/esm/tools/search/tool.mjs +101 -0
- package/dist/esm/tools/search/tool.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/scripts/search.d.ts +1 -0
- package/dist/types/tools/search/firecrawl.d.ts +117 -0
- package/dist/types/tools/search/format.d.ts +2 -0
- package/dist/types/tools/search/highlights.d.ts +13 -0
- package/dist/types/tools/search/index.d.ts +2 -0
- package/dist/types/tools/search/rerankers.d.ts +32 -0
- package/dist/types/tools/search/search.d.ts +9 -0
- package/dist/types/tools/search/tool.d.ts +12 -0
- package/dist/types/tools/search/types.d.ts +150 -0
- package/package.json +2 -1
- package/src/common/enum.ts +1 -0
- package/src/index.ts +1 -0
- package/src/scripts/search.ts +141 -0
- package/src/tools/search/firecrawl.ts +270 -0
- package/src/tools/search/format.ts +121 -0
- package/src/tools/search/highlights.ts +238 -0
- package/src/tools/search/index.ts +2 -0
- package/src/tools/search/rerankers.ts +248 -0
- package/src/tools/search/search.ts +567 -0
- package/src/tools/search/tool.ts +151 -0
- package/src/tools/search/types.ts +179 -0
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
|
4
|
+
import type * as t from './types';
|
|
5
|
+
import { getAttribution, 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
|
+
): Promise<string[][]> => {
|
|
56
|
+
// Split multiple texts
|
|
57
|
+
const promises = texts.map((text) =>
|
|
58
|
+
chunker.splitText(text, options).catch((error) => {
|
|
59
|
+
console.error('Error splitting text:', error);
|
|
60
|
+
return [text];
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
return Promise.all(promises);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const createSourceUpdateCallback = (sourceMap: Map<string, t.ValidSource>) => {
|
|
68
|
+
return (link: string, update?: Partial<t.ValidSource>): void => {
|
|
69
|
+
const source = sourceMap.get(link);
|
|
70
|
+
if (source) {
|
|
71
|
+
sourceMap.set(link, {
|
|
72
|
+
...source,
|
|
73
|
+
...update,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const getHighlights = async ({
|
|
80
|
+
query,
|
|
81
|
+
content,
|
|
82
|
+
reranker,
|
|
83
|
+
topResults = 5,
|
|
84
|
+
}: {
|
|
85
|
+
content: string;
|
|
86
|
+
query: string;
|
|
87
|
+
reranker?: BaseReranker;
|
|
88
|
+
topResults?: number;
|
|
89
|
+
}): Promise<t.Highlight[] | undefined> => {
|
|
90
|
+
if (!content) {
|
|
91
|
+
console.warn('No content provided for highlights');
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (!reranker) {
|
|
95
|
+
console.warn('No reranker provided for highlights');
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
const documents = await chunker.splitText(content);
|
|
101
|
+
if (Array.isArray(documents)) {
|
|
102
|
+
return await reranker.rerank(query, documents, topResults);
|
|
103
|
+
} else {
|
|
104
|
+
console.error(
|
|
105
|
+
'Expected documents to be an array, got:',
|
|
106
|
+
typeof documents
|
|
107
|
+
);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error('Error in content processing:', error);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const createSerperAPI = (
|
|
117
|
+
apiKey?: string
|
|
118
|
+
): {
|
|
119
|
+
getSources: (
|
|
120
|
+
query: string,
|
|
121
|
+
numResults?: number,
|
|
122
|
+
storedLocation?: string
|
|
123
|
+
) => Promise<t.SearchResult>;
|
|
124
|
+
} => {
|
|
125
|
+
const config = {
|
|
126
|
+
apiKey: apiKey ?? process.env.SERPER_API_KEY,
|
|
127
|
+
apiUrl: 'https://google.serper.dev/search',
|
|
128
|
+
defaultLocation: 'us',
|
|
129
|
+
timeout: 10000,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
if (config.apiKey == null || config.apiKey === '') {
|
|
133
|
+
throw new Error('SERPER_API_KEY is required for SerperAPI');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const getSources = async (
|
|
137
|
+
query: string,
|
|
138
|
+
numResults: number = 8,
|
|
139
|
+
storedLocation?: string
|
|
140
|
+
): Promise<t.SearchResult> => {
|
|
141
|
+
if (!query.trim()) {
|
|
142
|
+
return { success: false, error: 'Query cannot be empty' };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
const searchLocation = (
|
|
147
|
+
storedLocation ?? config.defaultLocation
|
|
148
|
+
).toLowerCase();
|
|
149
|
+
|
|
150
|
+
const payload = {
|
|
151
|
+
q: query,
|
|
152
|
+
num: Math.min(Math.max(1, numResults), 10),
|
|
153
|
+
gl: searchLocation,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const response = await axios.post(config.apiUrl, payload, {
|
|
157
|
+
headers: {
|
|
158
|
+
'X-API-KEY': config.apiKey,
|
|
159
|
+
'Content-Type': 'application/json',
|
|
160
|
+
},
|
|
161
|
+
timeout: config.timeout,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const data = response.data;
|
|
165
|
+
const results: t.SearchResultData = {
|
|
166
|
+
organic: data.organic,
|
|
167
|
+
images: data.images ?? [],
|
|
168
|
+
topStories: data.topStories ?? [],
|
|
169
|
+
knowledgeGraph: data.knowledgeGraph as t.KnowledgeGraphResult,
|
|
170
|
+
answerBox: data.answerBox as t.AnswerBoxResult,
|
|
171
|
+
peopleAlsoAsk: data.peopleAlsoAsk as t.PeopleAlsoAskResult[],
|
|
172
|
+
relatedSearches: data.relatedSearches as string[],
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
return { success: true, data: results };
|
|
176
|
+
} catch (error) {
|
|
177
|
+
const errorMessage =
|
|
178
|
+
error instanceof Error ? error.message : String(error);
|
|
179
|
+
return { success: false, error: `API request failed: ${errorMessage}` };
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
return { getSources };
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const createSearXNGAPI = (
|
|
187
|
+
instanceUrl?: string,
|
|
188
|
+
apiKey?: string
|
|
189
|
+
): {
|
|
190
|
+
getSources: (
|
|
191
|
+
query: string,
|
|
192
|
+
numResults?: number,
|
|
193
|
+
storedLocation?: string
|
|
194
|
+
) => Promise<t.SearchResult>;
|
|
195
|
+
} => {
|
|
196
|
+
const config = {
|
|
197
|
+
instanceUrl: instanceUrl ?? process.env.SEARXNG_INSTANCE_URL,
|
|
198
|
+
apiKey: apiKey ?? process.env.SEARXNG_API_KEY,
|
|
199
|
+
defaultLocation: 'all',
|
|
200
|
+
timeout: 10000,
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
if (config.instanceUrl == null || config.instanceUrl === '') {
|
|
204
|
+
throw new Error('SEARXNG_INSTANCE_URL is required for SearXNG API');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const getSources = async (
|
|
208
|
+
query: string,
|
|
209
|
+
numResults: number = 8,
|
|
210
|
+
storedLocation?: string
|
|
211
|
+
): Promise<t.SearchResult> => {
|
|
212
|
+
if (!query.trim()) {
|
|
213
|
+
return { success: false, error: 'Query cannot be empty' };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
// Ensure the instance URL ends with /search
|
|
218
|
+
if (config.instanceUrl == null || config.instanceUrl === '') {
|
|
219
|
+
return { success: false, error: 'Instance URL is not defined' };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
let searchUrl = config.instanceUrl;
|
|
223
|
+
if (!searchUrl.endsWith('/search')) {
|
|
224
|
+
searchUrl = searchUrl.replace(/\/$/, '') + '/search';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Prepare parameters for SearXNG
|
|
228
|
+
const params: Record<string, string | number> = {
|
|
229
|
+
q: query,
|
|
230
|
+
format: 'json',
|
|
231
|
+
pageno: 1,
|
|
232
|
+
categories: 'general',
|
|
233
|
+
language: 'all',
|
|
234
|
+
safesearch: 0,
|
|
235
|
+
engines: 'google,bing,duckduckgo',
|
|
236
|
+
max_results: Math.min(Math.max(1, numResults), 20),
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
if (storedLocation != null && storedLocation !== 'all') {
|
|
240
|
+
params.language = storedLocation;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const headers: Record<string, string> = {
|
|
244
|
+
'Content-Type': 'application/json',
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
if (config.apiKey != null && config.apiKey !== '') {
|
|
248
|
+
headers['X-API-Key'] = config.apiKey;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const response = await axios.get(searchUrl, {
|
|
252
|
+
headers,
|
|
253
|
+
params,
|
|
254
|
+
timeout: config.timeout,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const data = response.data;
|
|
258
|
+
|
|
259
|
+
// Transform SearXNG results to match SerperAPI format
|
|
260
|
+
const organicResults = (data.results ?? [])
|
|
261
|
+
.slice(0, numResults)
|
|
262
|
+
.map((result: t.SearXNGResult) => ({
|
|
263
|
+
title: result.title ?? '',
|
|
264
|
+
link: result.url ?? '',
|
|
265
|
+
snippet: result.content ?? '',
|
|
266
|
+
date: result.publishedDate ?? '',
|
|
267
|
+
}));
|
|
268
|
+
|
|
269
|
+
// Extract image results if available
|
|
270
|
+
const imageResults = (data.results ?? [])
|
|
271
|
+
.filter((result: t.SearXNGResult) => result.img_src)
|
|
272
|
+
.slice(0, 6)
|
|
273
|
+
.map((result: t.SearXNGResult) => ({
|
|
274
|
+
title: result.title ?? '',
|
|
275
|
+
imageUrl: result.img_src ?? '',
|
|
276
|
+
}));
|
|
277
|
+
|
|
278
|
+
// Format results to match SerperAPI structure
|
|
279
|
+
const results: t.SearchResultData = {
|
|
280
|
+
organic: organicResults,
|
|
281
|
+
images: imageResults,
|
|
282
|
+
topStories: [],
|
|
283
|
+
// Use undefined instead of null for optional properties
|
|
284
|
+
relatedSearches: data.suggestions ?? [],
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
return { success: true, data: results };
|
|
288
|
+
} catch (error) {
|
|
289
|
+
const errorMessage =
|
|
290
|
+
error instanceof Error ? error.message : String(error);
|
|
291
|
+
return {
|
|
292
|
+
success: false,
|
|
293
|
+
error: `SearXNG API request failed: ${errorMessage}`,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
return { getSources };
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
export const createSearchAPI = (
|
|
302
|
+
config: t.SearchConfig
|
|
303
|
+
): {
|
|
304
|
+
getSources: (
|
|
305
|
+
query: string,
|
|
306
|
+
numResults?: number,
|
|
307
|
+
storedLocation?: string
|
|
308
|
+
) => Promise<t.SearchResult>;
|
|
309
|
+
} => {
|
|
310
|
+
const {
|
|
311
|
+
searchProvider = 'serper',
|
|
312
|
+
serperApiKey,
|
|
313
|
+
searxngInstanceUrl,
|
|
314
|
+
searxngApiKey,
|
|
315
|
+
} = config;
|
|
316
|
+
|
|
317
|
+
if (searchProvider.toLowerCase() === 'serper') {
|
|
318
|
+
return createSerperAPI(serperApiKey);
|
|
319
|
+
} else if (searchProvider.toLowerCase() === 'searxng') {
|
|
320
|
+
return createSearXNGAPI(searxngInstanceUrl, searxngApiKey);
|
|
321
|
+
} else {
|
|
322
|
+
throw new Error(
|
|
323
|
+
`Invalid search provider: ${searchProvider}. Must be 'serper' or 'searxng'`
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export const createSourceProcessor = (
|
|
329
|
+
config: t.ProcessSourcesConfig = {},
|
|
330
|
+
scraperInstance?: FirecrawlScraper
|
|
331
|
+
): {
|
|
332
|
+
processSources: (
|
|
333
|
+
result: t.SearchResult,
|
|
334
|
+
numElements: number,
|
|
335
|
+
query: string,
|
|
336
|
+
proMode?: boolean
|
|
337
|
+
) => Promise<t.SearchResultData>;
|
|
338
|
+
topResults: number;
|
|
339
|
+
} => {
|
|
340
|
+
if (!scraperInstance) {
|
|
341
|
+
throw new Error('Firecrawl scraper instance is required');
|
|
342
|
+
}
|
|
343
|
+
const {
|
|
344
|
+
topResults = 5,
|
|
345
|
+
// strategies = ['no_extraction'],
|
|
346
|
+
// filterContent = true,
|
|
347
|
+
reranker,
|
|
348
|
+
} = config;
|
|
349
|
+
|
|
350
|
+
const firecrawlScraper = scraperInstance;
|
|
351
|
+
|
|
352
|
+
const webScraper = {
|
|
353
|
+
scrapeMany: async ({
|
|
354
|
+
query,
|
|
355
|
+
links,
|
|
356
|
+
}: {
|
|
357
|
+
query: string;
|
|
358
|
+
links: string[];
|
|
359
|
+
}): Promise<Array<t.ScrapeResult>> => {
|
|
360
|
+
console.log(`Scraping ${links.length} links with Firecrawl`);
|
|
361
|
+
const promises: Array<Promise<t.ScrapeResult>> = [];
|
|
362
|
+
try {
|
|
363
|
+
for (const currentLink of links) {
|
|
364
|
+
const promise: Promise<t.ScrapeResult> = firecrawlScraper
|
|
365
|
+
.scrapeUrl(currentLink, {})
|
|
366
|
+
.then(([url, response]) => {
|
|
367
|
+
const attribution = getAttribution(url, response.data?.metadata);
|
|
368
|
+
if (response.success && response.data) {
|
|
369
|
+
const content = firecrawlScraper.extractContent(response);
|
|
370
|
+
return {
|
|
371
|
+
url,
|
|
372
|
+
attribution,
|
|
373
|
+
content: chunker.cleanText(content),
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return {
|
|
378
|
+
url,
|
|
379
|
+
attribution,
|
|
380
|
+
error: true,
|
|
381
|
+
content: `Failed to scrape ${url}: ${response.error ?? 'Unknown error'}`,
|
|
382
|
+
};
|
|
383
|
+
})
|
|
384
|
+
.then(async (result) => {
|
|
385
|
+
try {
|
|
386
|
+
if (result.error != null) {
|
|
387
|
+
console.error(
|
|
388
|
+
`Error scraping ${result.url}: ${result.content}`
|
|
389
|
+
);
|
|
390
|
+
return {
|
|
391
|
+
...result,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
const highlights = await getHighlights({
|
|
395
|
+
query,
|
|
396
|
+
reranker,
|
|
397
|
+
content: result.content,
|
|
398
|
+
});
|
|
399
|
+
return {
|
|
400
|
+
...result,
|
|
401
|
+
highlights,
|
|
402
|
+
};
|
|
403
|
+
} catch (error) {
|
|
404
|
+
console.error('Error processing scraped content:', error);
|
|
405
|
+
return {
|
|
406
|
+
...result,
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
})
|
|
410
|
+
.catch((error) => {
|
|
411
|
+
console.error(`Error scraping ${currentLink}:`, error);
|
|
412
|
+
return {
|
|
413
|
+
url: currentLink,
|
|
414
|
+
error: true,
|
|
415
|
+
content: `Failed to scrape ${currentLink}: ${error.message ?? 'Unknown error'}`,
|
|
416
|
+
};
|
|
417
|
+
});
|
|
418
|
+
promises.push(promise);
|
|
419
|
+
}
|
|
420
|
+
return await Promise.all(promises);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
console.error('Error in scrapeMany:', error);
|
|
423
|
+
return [];
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
const fetchContents = async ({
|
|
429
|
+
links,
|
|
430
|
+
query,
|
|
431
|
+
target,
|
|
432
|
+
onContentScraped,
|
|
433
|
+
}: {
|
|
434
|
+
links: string[];
|
|
435
|
+
query: string;
|
|
436
|
+
target: number;
|
|
437
|
+
onContentScraped?: (link: string, update?: Partial<t.ValidSource>) => void;
|
|
438
|
+
}): Promise<void> => {
|
|
439
|
+
const initialLinks = links.slice(0, target);
|
|
440
|
+
// const remainingLinks = links.slice(target).reverse();
|
|
441
|
+
const results = await webScraper.scrapeMany({ query, links: initialLinks });
|
|
442
|
+
for (const result of results) {
|
|
443
|
+
if (result.error === true) {
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
const { url, content, attribution, highlights } = result;
|
|
447
|
+
onContentScraped?.(url, {
|
|
448
|
+
content,
|
|
449
|
+
attribution,
|
|
450
|
+
highlights,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
const processSources = async (
|
|
456
|
+
result: t.SearchResult,
|
|
457
|
+
numElements: number,
|
|
458
|
+
query: string,
|
|
459
|
+
proMode: boolean = false
|
|
460
|
+
): Promise<t.SearchResultData> => {
|
|
461
|
+
try {
|
|
462
|
+
if (!result.data) {
|
|
463
|
+
return {
|
|
464
|
+
organic: [],
|
|
465
|
+
topStories: [],
|
|
466
|
+
images: [],
|
|
467
|
+
relatedSearches: [],
|
|
468
|
+
};
|
|
469
|
+
} else if (!result.data.organic) {
|
|
470
|
+
return result.data;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (!proMode) {
|
|
474
|
+
const wikiSources = result.data.organic.filter((source) =>
|
|
475
|
+
source.link.includes('wikipedia.org')
|
|
476
|
+
);
|
|
477
|
+
|
|
478
|
+
if (!wikiSources.length) {
|
|
479
|
+
return result.data;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const wikiSourceMap = new Map<string, t.ValidSource>();
|
|
483
|
+
wikiSourceMap.set(wikiSources[0].link, wikiSources[0]);
|
|
484
|
+
const onContentScraped = createSourceUpdateCallback(wikiSourceMap);
|
|
485
|
+
await fetchContents({
|
|
486
|
+
query,
|
|
487
|
+
target: 1,
|
|
488
|
+
onContentScraped,
|
|
489
|
+
links: [wikiSources[0].link],
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
for (let i = 0; i < result.data.organic.length; i++) {
|
|
493
|
+
const source = result.data.organic[i];
|
|
494
|
+
const updatedSource = wikiSourceMap.get(source.link);
|
|
495
|
+
if (updatedSource) {
|
|
496
|
+
result.data.organic[i] = {
|
|
497
|
+
...source,
|
|
498
|
+
...updatedSource,
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return result.data;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const sourceMap = new Map<string, t.ValidSource>();
|
|
507
|
+
const allLinks: string[] = [];
|
|
508
|
+
|
|
509
|
+
for (const source of result.data.organic) {
|
|
510
|
+
if (source.link) {
|
|
511
|
+
allLinks.push(source.link);
|
|
512
|
+
sourceMap.set(source.link, source);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (allLinks.length === 0) {
|
|
517
|
+
return result.data;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const onContentScraped = createSourceUpdateCallback(sourceMap);
|
|
521
|
+
await fetchContents({
|
|
522
|
+
links: allLinks,
|
|
523
|
+
query,
|
|
524
|
+
onContentScraped,
|
|
525
|
+
target: numElements,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
for (let i = 0; i < result.data.organic.length; i++) {
|
|
529
|
+
const source = result.data.organic[i];
|
|
530
|
+
const updatedSource = sourceMap.get(source.link);
|
|
531
|
+
if (updatedSource) {
|
|
532
|
+
result.data.organic[i] = {
|
|
533
|
+
...source,
|
|
534
|
+
...updatedSource,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const successfulSources = result.data.organic
|
|
540
|
+
.filter(
|
|
541
|
+
(source) =>
|
|
542
|
+
source.content != null && !source.content.startsWith('Failed')
|
|
543
|
+
)
|
|
544
|
+
.slice(0, numElements);
|
|
545
|
+
|
|
546
|
+
if (successfulSources.length > 0) {
|
|
547
|
+
result.data.organic = successfulSources;
|
|
548
|
+
}
|
|
549
|
+
return result.data;
|
|
550
|
+
} catch (error) {
|
|
551
|
+
console.error('Error in processSources:', error);
|
|
552
|
+
return {
|
|
553
|
+
organic: [],
|
|
554
|
+
topStories: [],
|
|
555
|
+
images: [],
|
|
556
|
+
relatedSearches: [],
|
|
557
|
+
...result.data,
|
|
558
|
+
error: error instanceof Error ? error.message : String(error),
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
return {
|
|
564
|
+
processSources,
|
|
565
|
+
topResults,
|
|
566
|
+
};
|
|
567
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { tool, DynamicStructuredTool } from '@langchain/core/tools';
|
|
4
|
+
import type * as t from './types';
|
|
5
|
+
import { createSearchAPI, createSourceProcessor } from './search';
|
|
6
|
+
import { createFirecrawlScraper } from './firecrawl';
|
|
7
|
+
import { expandHighlights } from './highlights';
|
|
8
|
+
import { formatResultsForLLM } from './format';
|
|
9
|
+
import { createReranker } from './rerankers';
|
|
10
|
+
import { Constants } from '@/common';
|
|
11
|
+
|
|
12
|
+
const SearchToolSchema = z.object({
|
|
13
|
+
query: z
|
|
14
|
+
.string()
|
|
15
|
+
.describe(
|
|
16
|
+
'The search query string that specifies what should be searched for.'
|
|
17
|
+
),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const createSearchTool = (
|
|
21
|
+
config: t.SearchToolConfig = {}
|
|
22
|
+
): DynamicStructuredTool<typeof SearchToolSchema> => {
|
|
23
|
+
const {
|
|
24
|
+
searchProvider = 'serper',
|
|
25
|
+
serperApiKey,
|
|
26
|
+
searxngInstanceUrl,
|
|
27
|
+
searxngApiKey,
|
|
28
|
+
rerankerType = 'cohere',
|
|
29
|
+
topResults = 5,
|
|
30
|
+
strategies = ['no_extraction'],
|
|
31
|
+
filterContent = true,
|
|
32
|
+
firecrawlApiKey,
|
|
33
|
+
firecrawlApiUrl,
|
|
34
|
+
firecrawlFormats = ['markdown', 'html'],
|
|
35
|
+
jinaApiKey,
|
|
36
|
+
cohereApiKey,
|
|
37
|
+
onSearchResults: _onSearchResults,
|
|
38
|
+
} = config;
|
|
39
|
+
|
|
40
|
+
const searchAPI = createSearchAPI({
|
|
41
|
+
searchProvider,
|
|
42
|
+
serperApiKey,
|
|
43
|
+
searxngInstanceUrl,
|
|
44
|
+
searxngApiKey,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const firecrawlScraper = createFirecrawlScraper({
|
|
48
|
+
apiKey: firecrawlApiKey ?? process.env.FIRECRAWL_API_KEY,
|
|
49
|
+
apiUrl: firecrawlApiUrl,
|
|
50
|
+
formats: firecrawlFormats,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const selectedReranker = createReranker({
|
|
54
|
+
rerankerType,
|
|
55
|
+
jinaApiKey,
|
|
56
|
+
cohereApiKey,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (!selectedReranker) {
|
|
60
|
+
console.warn('No reranker selected. Using default ranking.');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const sourceProcessor = createSourceProcessor(
|
|
64
|
+
{
|
|
65
|
+
reranker: selectedReranker,
|
|
66
|
+
topResults,
|
|
67
|
+
strategies,
|
|
68
|
+
filterContent,
|
|
69
|
+
},
|
|
70
|
+
firecrawlScraper
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const search = async ({
|
|
74
|
+
query,
|
|
75
|
+
proMode = true,
|
|
76
|
+
maxSources = 5,
|
|
77
|
+
onSearchResults,
|
|
78
|
+
}: {
|
|
79
|
+
query: string;
|
|
80
|
+
proMode?: boolean;
|
|
81
|
+
maxSources?: number;
|
|
82
|
+
onSearchResults?: (sources: t.SearchResult) => void;
|
|
83
|
+
}): Promise<t.SearchResultData> => {
|
|
84
|
+
try {
|
|
85
|
+
const sources = await searchAPI.getSources(query);
|
|
86
|
+
onSearchResults?.(sources);
|
|
87
|
+
|
|
88
|
+
if (!sources.success) {
|
|
89
|
+
throw new Error(sources.error ?? 'Search failed');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const processedSources = await sourceProcessor.processSources(
|
|
93
|
+
sources,
|
|
94
|
+
maxSources,
|
|
95
|
+
query,
|
|
96
|
+
proMode
|
|
97
|
+
);
|
|
98
|
+
return expandHighlights(processedSources);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error('Error in search:', error);
|
|
101
|
+
return {
|
|
102
|
+
organic: [],
|
|
103
|
+
topStories: [],
|
|
104
|
+
images: [],
|
|
105
|
+
relatedSearches: [],
|
|
106
|
+
error: error instanceof Error ? error.message : String(error),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return tool<typeof SearchToolSchema>(
|
|
112
|
+
async ({ query }, runnableConfig) => {
|
|
113
|
+
const searchResult = await search({
|
|
114
|
+
query,
|
|
115
|
+
onSearchResults: _onSearchResults
|
|
116
|
+
? (result): void => {
|
|
117
|
+
_onSearchResults(result, runnableConfig);
|
|
118
|
+
}
|
|
119
|
+
: undefined,
|
|
120
|
+
});
|
|
121
|
+
const output = formatResultsForLLM(searchResult);
|
|
122
|
+
return [output, { [Constants.WEB_SEARCH]: { ...searchResult } }];
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: Constants.WEB_SEARCH,
|
|
126
|
+
description: `
|
|
127
|
+
Real-time search. Results have required unique citation anchors.
|
|
128
|
+
|
|
129
|
+
Anchors:
|
|
130
|
+
- \\ue202turn0searchN (web), \\ue202turn0newsN (news), \\ue202turn0imageN (image)
|
|
131
|
+
|
|
132
|
+
Special Markers:
|
|
133
|
+
- \\ue203...\\ue204 — mark start/end of cited span
|
|
134
|
+
- \\ue200...\\ue201 — composite/group block (e.g. \\ue200cite\\ue202turn0search1\\ue202turn0news2\\ue201)
|
|
135
|
+
- \\ue206 — marks grouped/summary citation areas
|
|
136
|
+
|
|
137
|
+
**CITE EVERY NON-OBVIOUS FACT/QUOTE:**
|
|
138
|
+
Insert the anchor marker(s) immediately after the statement:
|
|
139
|
+
- "Pure functions produce same output \\ue202turn0search0."
|
|
140
|
+
- Multiple: "Benefits \\ue202turn0search0\\ue202turn0news0."
|
|
141
|
+
- Span: \\ue203Key: first-class functions\\ue204\\ue202turn0news1
|
|
142
|
+
- Group: "Functional languages."\\ue206 or \\ue200cite\\ue202turn0search0\\ue202turn0news1\\ue201
|
|
143
|
+
- Image: "See photo \\ue202turn0image0."
|
|
144
|
+
|
|
145
|
+
**NEVER use markdown links, [1], or footnotes. CITE ONLY with anchors provided.**
|
|
146
|
+
`.trim(),
|
|
147
|
+
schema: SearchToolSchema,
|
|
148
|
+
responseFormat: Constants.CONTENT_AND_ARTIFACT,
|
|
149
|
+
}
|
|
150
|
+
);
|
|
151
|
+
};
|