@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,187 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var axios = require('axios');
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-console */
|
|
6
|
+
class BaseReranker {
|
|
7
|
+
apiKey;
|
|
8
|
+
constructor() {
|
|
9
|
+
// Each specific reranker will set its API key
|
|
10
|
+
}
|
|
11
|
+
getDefaultRanking(documents, topK) {
|
|
12
|
+
return documents
|
|
13
|
+
.slice(0, Math.min(topK, documents.length))
|
|
14
|
+
.map((doc) => ({ text: doc, score: 0 }));
|
|
15
|
+
}
|
|
16
|
+
logDocumentSamples(documents) {
|
|
17
|
+
console.log('Sample documents being sent to API:');
|
|
18
|
+
for (let i = 0; i < Math.min(3, documents.length); i++) {
|
|
19
|
+
console.log(`Document ${i}: ${documents[i].substring(0, 100)}...`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
class JinaReranker extends BaseReranker {
|
|
24
|
+
constructor({ apiKey = process.env.JINA_API_KEY }) {
|
|
25
|
+
super();
|
|
26
|
+
this.apiKey = apiKey;
|
|
27
|
+
}
|
|
28
|
+
async rerank(query, documents, topK = 5) {
|
|
29
|
+
console.log(`Reranking ${documents.length} documents with Jina`);
|
|
30
|
+
try {
|
|
31
|
+
if (this.apiKey == null || this.apiKey === '') {
|
|
32
|
+
console.warn('JINA_API_KEY is not set. Using default ranking.');
|
|
33
|
+
return this.getDefaultRanking(documents, topK);
|
|
34
|
+
}
|
|
35
|
+
this.logDocumentSamples(documents);
|
|
36
|
+
const requestData = {
|
|
37
|
+
model: 'jina-reranker-v2-base-multilingual',
|
|
38
|
+
query: query,
|
|
39
|
+
top_n: topK,
|
|
40
|
+
documents: documents,
|
|
41
|
+
return_documents: true,
|
|
42
|
+
};
|
|
43
|
+
const response = await axios.post('https://api.jina.ai/v1/rerank', requestData, {
|
|
44
|
+
headers: {
|
|
45
|
+
'Content-Type': 'application/json',
|
|
46
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
// Log the response data structure
|
|
50
|
+
console.log('Jina API response structure:');
|
|
51
|
+
console.log('Model:', response.data?.model);
|
|
52
|
+
console.log('Usage:', response.data?.usage);
|
|
53
|
+
console.log('Results count:', response.data?.results.length);
|
|
54
|
+
// Log a sample of the results
|
|
55
|
+
if ((response.data?.results.length ?? 0) > 0) {
|
|
56
|
+
console.log('Sample result:', JSON.stringify(response.data?.results[0], null, 2));
|
|
57
|
+
}
|
|
58
|
+
if (response.data && response.data.results.length) {
|
|
59
|
+
return response.data.results.map((result) => {
|
|
60
|
+
const docIndex = result.index;
|
|
61
|
+
const score = result.relevance_score;
|
|
62
|
+
let text = '';
|
|
63
|
+
// If return_documents is true, the document field will be present
|
|
64
|
+
if (result.document != null) {
|
|
65
|
+
const doc = result.document;
|
|
66
|
+
if (typeof doc === 'object' && 'text' in doc) {
|
|
67
|
+
text = doc.text;
|
|
68
|
+
}
|
|
69
|
+
else if (typeof doc === 'string') {
|
|
70
|
+
text = doc;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// Otherwise, use the index to get the document
|
|
75
|
+
text = documents[docIndex];
|
|
76
|
+
}
|
|
77
|
+
return { text, score };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
console.warn('Unexpected response format from Jina API. Using default ranking.');
|
|
82
|
+
return this.getDefaultRanking(documents, topK);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
console.error('Error using Jina reranker:', error);
|
|
87
|
+
// Fallback to default ranking on error
|
|
88
|
+
return this.getDefaultRanking(documents, topK);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
class CohereReranker extends BaseReranker {
|
|
93
|
+
constructor({ apiKey = process.env.COHERE_API_KEY }) {
|
|
94
|
+
super();
|
|
95
|
+
this.apiKey = apiKey;
|
|
96
|
+
}
|
|
97
|
+
async rerank(query, documents, topK = 5) {
|
|
98
|
+
console.log(`Reranking ${documents.length} documents with Cohere`);
|
|
99
|
+
try {
|
|
100
|
+
if (this.apiKey == null || this.apiKey === '') {
|
|
101
|
+
console.warn('COHERE_API_KEY is not set. Using default ranking.');
|
|
102
|
+
return this.getDefaultRanking(documents, topK);
|
|
103
|
+
}
|
|
104
|
+
this.logDocumentSamples(documents);
|
|
105
|
+
const requestData = {
|
|
106
|
+
model: 'rerank-v3.5',
|
|
107
|
+
query: query,
|
|
108
|
+
top_n: topK,
|
|
109
|
+
documents: documents,
|
|
110
|
+
};
|
|
111
|
+
const response = await axios.post('https://api.cohere.com/v2/rerank', requestData, {
|
|
112
|
+
headers: {
|
|
113
|
+
'Content-Type': 'application/json',
|
|
114
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
// Log the response data structure
|
|
118
|
+
console.log('Cohere API response structure:');
|
|
119
|
+
console.log('ID:', response.data?.id);
|
|
120
|
+
console.log('Meta:', response.data?.meta);
|
|
121
|
+
console.log('Results count:', response.data?.results.length);
|
|
122
|
+
// Log a sample of the results
|
|
123
|
+
if ((response.data?.results.length ?? 0) > 0) {
|
|
124
|
+
console.log('Sample result:', JSON.stringify(response.data?.results[0], null, 2));
|
|
125
|
+
}
|
|
126
|
+
if (response.data && response.data.results.length) {
|
|
127
|
+
return response.data.results.map((result) => {
|
|
128
|
+
const docIndex = result.index;
|
|
129
|
+
const score = result.relevance_score;
|
|
130
|
+
const text = documents[docIndex];
|
|
131
|
+
return { text, score };
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
console.warn('Unexpected response format from Cohere API. Using default ranking.');
|
|
136
|
+
return this.getDefaultRanking(documents, topK);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
console.error('Error using Cohere reranker:', error);
|
|
141
|
+
// Fallback to default ranking on error
|
|
142
|
+
return this.getDefaultRanking(documents, topK);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
class InfinityReranker extends BaseReranker {
|
|
147
|
+
constructor() {
|
|
148
|
+
super();
|
|
149
|
+
// No API key needed for the placeholder implementation
|
|
150
|
+
}
|
|
151
|
+
async rerank(query, documents, topK = 5) {
|
|
152
|
+
console.log(`Reranking ${documents.length} documents with Infinity (placeholder)`);
|
|
153
|
+
// This would be replaced with actual Infinity reranker implementation
|
|
154
|
+
return this.getDefaultRanking(documents, topK);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Creates the appropriate reranker based on type and configuration
|
|
159
|
+
*/
|
|
160
|
+
const createReranker = (config) => {
|
|
161
|
+
const { rerankerType, jinaApiKey, cohereApiKey } = config;
|
|
162
|
+
switch (rerankerType.toLowerCase()) {
|
|
163
|
+
case 'jina':
|
|
164
|
+
return new JinaReranker({ apiKey: jinaApiKey });
|
|
165
|
+
case 'cohere':
|
|
166
|
+
return new CohereReranker({ apiKey: cohereApiKey });
|
|
167
|
+
case 'infinity':
|
|
168
|
+
return new InfinityReranker();
|
|
169
|
+
case 'none':
|
|
170
|
+
console.log('Skipping reranking as reranker is set to "none"');
|
|
171
|
+
return undefined;
|
|
172
|
+
default:
|
|
173
|
+
console.warn(`Unknown reranker type: ${rerankerType}. Defaulting to InfinityReranker.`);
|
|
174
|
+
return new JinaReranker({ apiKey: jinaApiKey });
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
// Example usage:
|
|
178
|
+
// const jinaReranker = new JinaReranker();
|
|
179
|
+
// const cohereReranker = new CohereReranker();
|
|
180
|
+
// const infinityReranker = new InfinityReranker();
|
|
181
|
+
|
|
182
|
+
exports.BaseReranker = BaseReranker;
|
|
183
|
+
exports.CohereReranker = CohereReranker;
|
|
184
|
+
exports.InfinityReranker = InfinityReranker;
|
|
185
|
+
exports.JinaReranker = JinaReranker;
|
|
186
|
+
exports.createReranker = createReranker;
|
|
187
|
+
//# sourceMappingURL=rerankers.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rerankers.cjs","sources":["../../../../src/tools/search/rerankers.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport axios from 'axios';\nimport type * as t from './types';\n\nexport abstract class BaseReranker {\n protected apiKey: string | undefined;\n\n constructor() {\n // Each specific reranker will set its API key\n }\n\n abstract rerank(\n query: string,\n documents: string[],\n topK?: number\n ): Promise<t.Highlight[]>;\n\n protected getDefaultRanking(\n documents: string[],\n topK: number\n ): t.Highlight[] {\n return documents\n .slice(0, Math.min(topK, documents.length))\n .map((doc) => ({ text: doc, score: 0 }));\n }\n\n protected logDocumentSamples(documents: string[]): void {\n console.log('Sample documents being sent to API:');\n for (let i = 0; i < Math.min(3, documents.length); i++) {\n console.log(`Document ${i}: ${documents[i].substring(0, 100)}...`);\n }\n }\n}\n\nexport class JinaReranker extends BaseReranker {\n constructor({ apiKey = process.env.JINA_API_KEY }: { apiKey?: string }) {\n super();\n this.apiKey = apiKey;\n }\n\n async rerank(\n query: string,\n documents: string[],\n topK: number = 5\n ): Promise<t.Highlight[]> {\n console.log(`Reranking ${documents.length} documents with Jina`);\n\n try {\n if (this.apiKey == null || this.apiKey === '') {\n console.warn('JINA_API_KEY is not set. Using default ranking.');\n return this.getDefaultRanking(documents, topK);\n }\n\n this.logDocumentSamples(documents);\n\n const requestData = {\n model: 'jina-reranker-v2-base-multilingual',\n query: query,\n top_n: topK,\n documents: documents,\n return_documents: true,\n };\n\n const response = await axios.post<t.JinaRerankerResponse | undefined>(\n 'https://api.jina.ai/v1/rerank',\n requestData,\n {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.apiKey}`,\n },\n }\n );\n\n // Log the response data structure\n console.log('Jina API response structure:');\n console.log('Model:', response.data?.model);\n console.log('Usage:', response.data?.usage);\n console.log('Results count:', response.data?.results.length);\n\n // Log a sample of the results\n if ((response.data?.results.length ?? 0) > 0) {\n console.log(\n 'Sample result:',\n JSON.stringify(response.data?.results[0], null, 2)\n );\n }\n\n if (response.data && response.data.results.length) {\n return response.data.results.map((result) => {\n const docIndex = result.index;\n const score = result.relevance_score;\n let text = '';\n\n // If return_documents is true, the document field will be present\n if (result.document != null) {\n const doc = result.document;\n if (typeof doc === 'object' && 'text' in doc) {\n text = doc.text;\n } else if (typeof doc === 'string') {\n text = doc;\n }\n } else {\n // Otherwise, use the index to get the document\n text = documents[docIndex];\n }\n\n return { text, score };\n });\n } else {\n console.warn(\n 'Unexpected response format from Jina API. Using default ranking.'\n );\n return this.getDefaultRanking(documents, topK);\n }\n } catch (error) {\n console.error('Error using Jina reranker:', error);\n // Fallback to default ranking on error\n return this.getDefaultRanking(documents, topK);\n }\n }\n}\n\nexport class CohereReranker extends BaseReranker {\n constructor({ apiKey = process.env.COHERE_API_KEY }: { apiKey?: string }) {\n super();\n this.apiKey = apiKey;\n }\n\n async rerank(\n query: string,\n documents: string[],\n topK: number = 5\n ): Promise<t.Highlight[]> {\n console.log(`Reranking ${documents.length} documents with Cohere`);\n\n try {\n if (this.apiKey == null || this.apiKey === '') {\n console.warn('COHERE_API_KEY is not set. Using default ranking.');\n return this.getDefaultRanking(documents, topK);\n }\n\n this.logDocumentSamples(documents);\n\n const requestData = {\n model: 'rerank-v3.5',\n query: query,\n top_n: topK,\n documents: documents,\n };\n\n const response = await axios.post<t.CohereRerankerResponse | undefined>(\n 'https://api.cohere.com/v2/rerank',\n requestData,\n {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.apiKey}`,\n },\n }\n );\n\n // Log the response data structure\n console.log('Cohere API response structure:');\n console.log('ID:', response.data?.id);\n console.log('Meta:', response.data?.meta);\n console.log('Results count:', response.data?.results.length);\n\n // Log a sample of the results\n if ((response.data?.results.length ?? 0) > 0) {\n console.log(\n 'Sample result:',\n JSON.stringify(response.data?.results[0], null, 2)\n );\n }\n\n if (response.data && response.data.results.length) {\n return response.data.results.map((result) => {\n const docIndex = result.index;\n const score = result.relevance_score;\n const text = documents[docIndex];\n return { text, score };\n });\n } else {\n console.warn(\n 'Unexpected response format from Cohere API. Using default ranking.'\n );\n return this.getDefaultRanking(documents, topK);\n }\n } catch (error) {\n console.error('Error using Cohere reranker:', error);\n // Fallback to default ranking on error\n return this.getDefaultRanking(documents, topK);\n }\n }\n}\n\nexport class InfinityReranker extends BaseReranker {\n constructor() {\n super();\n // No API key needed for the placeholder implementation\n }\n\n async rerank(\n query: string,\n documents: string[],\n topK: number = 5\n ): Promise<t.Highlight[]> {\n console.log(\n `Reranking ${documents.length} documents with Infinity (placeholder)`\n );\n // This would be replaced with actual Infinity reranker implementation\n return this.getDefaultRanking(documents, topK);\n }\n}\n\n/**\n * Creates the appropriate reranker based on type and configuration\n */\nexport const createReranker = (config: {\n rerankerType: t.RerankerType;\n jinaApiKey?: string;\n cohereApiKey?: string;\n}): BaseReranker | undefined => {\n const { rerankerType, jinaApiKey, cohereApiKey } = config;\n\n switch (rerankerType.toLowerCase()) {\n case 'jina':\n return new JinaReranker({ apiKey: jinaApiKey });\n case 'cohere':\n return new CohereReranker({ apiKey: cohereApiKey });\n case 'infinity':\n return new InfinityReranker();\n case 'none':\n console.log('Skipping reranking as reranker is set to \"none\"');\n return undefined;\n default:\n console.warn(\n `Unknown reranker type: ${rerankerType}. Defaulting to InfinityReranker.`\n );\n return new JinaReranker({ apiKey: jinaApiKey });\n }\n};\n\n// Example usage:\n// const jinaReranker = new JinaReranker();\n// const cohereReranker = new CohereReranker();\n// const infinityReranker = new InfinityReranker();\n"],"names":[],"mappings":";;;;AAAA;MAIsB,YAAY,CAAA;AACtB,IAAA,MAAM;AAEhB,IAAA,WAAA,GAAA;;;IAUU,iBAAiB,CACzB,SAAmB,EACnB,IAAY,EAAA;AAEZ,QAAA,OAAO;AACJ,aAAA,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC;AACzC,aAAA,GAAG,CAAC,CAAC,GAAG,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;;AAGlC,IAAA,kBAAkB,CAAC,SAAmB,EAAA;AAC9C,QAAA,OAAO,CAAC,GAAG,CAAC,qCAAqC,CAAC;QAClD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE;AACtD,YAAA,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA,EAAA,EAAK,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA,GAAA,CAAK,CAAC;;;AAGvE;AAEK,MAAO,YAAa,SAAQ,YAAY,CAAA;IAC5C,WAAY,CAAA,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,EAAuB,EAAA;AACpE,QAAA,KAAK,EAAE;AACP,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM;;IAGtB,MAAM,M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,SAAS,CAAC,MAAM,CAAwC,sCAAA,CAAA,CACtE;;QAED,OAAO,IAAI,CAAC,iBAAiB,CAAC,SAAS,EAAE,IAAI,CAAC;;AAEjD;AAED;;AAEG;AACU,MAAA,cAAc,GAAG,CAAC,MAI9B,KAA8B;IAC7B,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,YAAY,EAAE,GAAG,MAAM;AAEzD,IAAA,QAAQ,YAAY,CAAC,WAAW,EAAE;AAClC,QAAA,KAAK,MAAM;YACT,OAAO,IAAI,YAAY,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;AACjD,QAAA,KAAK,QAAQ;YACX,OAAO,IAAI,cAAc,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;AACrD,QAAA,KAAK,UAAU;YACb,OAAO,IAAI,gBAAgB,EAAE;AAC/B,QAAA,KAAK,MAAM;AACT,YAAA,OAAO,CAAC,GAAG,CAAC,iDAAiD,CAAC;AAC9D,YAAA,OAAO,SAAS;AAClB,QAAA;AACE,YAAA,OAAO,CAAC,IAAI,CACV,0BAA0B,YAAY,CAAA,iCAAA,CAAmC,CAC1E;YACD,OAAO,IAAI,YAAY,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;;AAEnD;AAEA;AACA;AACA;AACA;;;;;;;;"}
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var axios = require('axios');
|
|
4
|
+
var textsplitters = require('@langchain/textsplitters');
|
|
5
|
+
var firecrawl = require('./firecrawl.cjs');
|
|
6
|
+
|
|
7
|
+
/* eslint-disable no-console */
|
|
8
|
+
const chunker = {
|
|
9
|
+
cleanText: (text) => {
|
|
10
|
+
if (!text)
|
|
11
|
+
return '';
|
|
12
|
+
/** Normalized all line endings to '\n' */
|
|
13
|
+
const normalizedText = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
14
|
+
/** Handle multiple backslashes followed by newlines
|
|
15
|
+
* This replaces patterns like '\\\\\\n' with a single newline */
|
|
16
|
+
const fixedBackslashes = normalizedText.replace(/\\+\n/g, '\n');
|
|
17
|
+
/** Cleaned up consecutive newlines, tabs, and spaces around newlines */
|
|
18
|
+
const cleanedNewlines = fixedBackslashes.replace(/[\t ]*\n[\t \n]*/g, '\n');
|
|
19
|
+
/** Cleaned up excessive spaces and tabs */
|
|
20
|
+
const cleanedSpaces = cleanedNewlines.replace(/[ \t]+/g, ' ');
|
|
21
|
+
return cleanedSpaces.trim();
|
|
22
|
+
},
|
|
23
|
+
splitText: async (text, options) => {
|
|
24
|
+
const chunkSize = options?.chunkSize ?? 150;
|
|
25
|
+
const chunkOverlap = options?.chunkOverlap ?? 50;
|
|
26
|
+
const separators = options?.separators || ['\n\n', '\n'];
|
|
27
|
+
const splitter = new textsplitters.RecursiveCharacterTextSplitter({
|
|
28
|
+
separators,
|
|
29
|
+
chunkSize,
|
|
30
|
+
chunkOverlap,
|
|
31
|
+
});
|
|
32
|
+
return await splitter.splitText(text);
|
|
33
|
+
},
|
|
34
|
+
splitTexts: async (texts, options) => {
|
|
35
|
+
// Split multiple texts
|
|
36
|
+
const promises = texts.map((text) => chunker.splitText(text, options).catch((error) => {
|
|
37
|
+
console.error('Error splitting text:', error);
|
|
38
|
+
return [text];
|
|
39
|
+
}));
|
|
40
|
+
return Promise.all(promises);
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
const createSourceUpdateCallback = (sourceMap) => {
|
|
44
|
+
return (link, update) => {
|
|
45
|
+
const source = sourceMap.get(link);
|
|
46
|
+
if (source) {
|
|
47
|
+
sourceMap.set(link, {
|
|
48
|
+
...source,
|
|
49
|
+
...update,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const getHighlights = async ({ query, content, reranker, topResults = 5, }) => {
|
|
55
|
+
if (!content) {
|
|
56
|
+
console.warn('No content provided for highlights');
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!reranker) {
|
|
60
|
+
console.warn('No reranker provided for highlights');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const documents = await chunker.splitText(content);
|
|
65
|
+
if (Array.isArray(documents)) {
|
|
66
|
+
return await reranker.rerank(query, documents, topResults);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
console.error('Expected documents to be an array, got:', typeof documents);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
console.error('Error in content processing:', error);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const createSerperAPI = (apiKey) => {
|
|
79
|
+
const config = {
|
|
80
|
+
apiKey: apiKey ?? process.env.SERPER_API_KEY,
|
|
81
|
+
apiUrl: 'https://google.serper.dev/search',
|
|
82
|
+
defaultLocation: 'us',
|
|
83
|
+
timeout: 10000,
|
|
84
|
+
};
|
|
85
|
+
if (config.apiKey == null || config.apiKey === '') {
|
|
86
|
+
throw new Error('SERPER_API_KEY is required for SerperAPI');
|
|
87
|
+
}
|
|
88
|
+
const getSources = async (query, numResults = 8, storedLocation) => {
|
|
89
|
+
if (!query.trim()) {
|
|
90
|
+
return { success: false, error: 'Query cannot be empty' };
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
const searchLocation = (storedLocation ?? config.defaultLocation).toLowerCase();
|
|
94
|
+
const payload = {
|
|
95
|
+
q: query,
|
|
96
|
+
num: Math.min(Math.max(1, numResults), 10),
|
|
97
|
+
gl: searchLocation,
|
|
98
|
+
};
|
|
99
|
+
const response = await axios.post(config.apiUrl, payload, {
|
|
100
|
+
headers: {
|
|
101
|
+
'X-API-KEY': config.apiKey,
|
|
102
|
+
'Content-Type': 'application/json',
|
|
103
|
+
},
|
|
104
|
+
timeout: config.timeout,
|
|
105
|
+
});
|
|
106
|
+
const data = response.data;
|
|
107
|
+
const results = {
|
|
108
|
+
organic: data.organic,
|
|
109
|
+
images: data.images ?? [],
|
|
110
|
+
topStories: data.topStories ?? [],
|
|
111
|
+
knowledgeGraph: data.knowledgeGraph,
|
|
112
|
+
answerBox: data.answerBox,
|
|
113
|
+
peopleAlsoAsk: data.peopleAlsoAsk,
|
|
114
|
+
relatedSearches: data.relatedSearches,
|
|
115
|
+
};
|
|
116
|
+
return { success: true, data: results };
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
120
|
+
return { success: false, error: `API request failed: ${errorMessage}` };
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
return { getSources };
|
|
124
|
+
};
|
|
125
|
+
const createSearXNGAPI = (instanceUrl, apiKey) => {
|
|
126
|
+
const config = {
|
|
127
|
+
instanceUrl: instanceUrl ?? process.env.SEARXNG_INSTANCE_URL,
|
|
128
|
+
apiKey: apiKey ?? process.env.SEARXNG_API_KEY,
|
|
129
|
+
timeout: 10000,
|
|
130
|
+
};
|
|
131
|
+
if (config.instanceUrl == null || config.instanceUrl === '') {
|
|
132
|
+
throw new Error('SEARXNG_INSTANCE_URL is required for SearXNG API');
|
|
133
|
+
}
|
|
134
|
+
const getSources = async (query, numResults = 8, storedLocation) => {
|
|
135
|
+
if (!query.trim()) {
|
|
136
|
+
return { success: false, error: 'Query cannot be empty' };
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
// Ensure the instance URL ends with /search
|
|
140
|
+
if (config.instanceUrl == null || config.instanceUrl === '') {
|
|
141
|
+
return { success: false, error: 'Instance URL is not defined' };
|
|
142
|
+
}
|
|
143
|
+
let searchUrl = config.instanceUrl;
|
|
144
|
+
if (!searchUrl.endsWith('/search')) {
|
|
145
|
+
searchUrl = searchUrl.replace(/\/$/, '') + '/search';
|
|
146
|
+
}
|
|
147
|
+
// Prepare parameters for SearXNG
|
|
148
|
+
const params = {
|
|
149
|
+
q: query,
|
|
150
|
+
format: 'json',
|
|
151
|
+
pageno: 1,
|
|
152
|
+
categories: 'general',
|
|
153
|
+
language: 'all',
|
|
154
|
+
safesearch: 0,
|
|
155
|
+
engines: 'google,bing,duckduckgo',
|
|
156
|
+
max_results: Math.min(Math.max(1, numResults), 20),
|
|
157
|
+
};
|
|
158
|
+
if (storedLocation != null && storedLocation !== 'all') {
|
|
159
|
+
params.language = storedLocation;
|
|
160
|
+
}
|
|
161
|
+
const headers = {
|
|
162
|
+
'Content-Type': 'application/json',
|
|
163
|
+
};
|
|
164
|
+
if (config.apiKey != null && config.apiKey !== '') {
|
|
165
|
+
headers['X-API-Key'] = config.apiKey;
|
|
166
|
+
}
|
|
167
|
+
const response = await axios.get(searchUrl, {
|
|
168
|
+
headers,
|
|
169
|
+
params,
|
|
170
|
+
timeout: config.timeout,
|
|
171
|
+
});
|
|
172
|
+
const data = response.data;
|
|
173
|
+
// Transform SearXNG results to match SerperAPI format
|
|
174
|
+
const organicResults = (data.results ?? [])
|
|
175
|
+
.slice(0, numResults)
|
|
176
|
+
.map((result) => ({
|
|
177
|
+
title: result.title ?? '',
|
|
178
|
+
link: result.url ?? '',
|
|
179
|
+
snippet: result.content ?? '',
|
|
180
|
+
date: result.publishedDate ?? '',
|
|
181
|
+
}));
|
|
182
|
+
// Extract image results if available
|
|
183
|
+
const imageResults = (data.results ?? [])
|
|
184
|
+
.filter((result) => result.img_src)
|
|
185
|
+
.slice(0, 6)
|
|
186
|
+
.map((result) => ({
|
|
187
|
+
title: result.title ?? '',
|
|
188
|
+
imageUrl: result.img_src ?? '',
|
|
189
|
+
}));
|
|
190
|
+
// Format results to match SerperAPI structure
|
|
191
|
+
const results = {
|
|
192
|
+
organic: organicResults,
|
|
193
|
+
images: imageResults,
|
|
194
|
+
topStories: [],
|
|
195
|
+
// Use undefined instead of null for optional properties
|
|
196
|
+
relatedSearches: data.suggestions ?? [],
|
|
197
|
+
};
|
|
198
|
+
return { success: true, data: results };
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
202
|
+
return {
|
|
203
|
+
success: false,
|
|
204
|
+
error: `SearXNG API request failed: ${errorMessage}`,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
return { getSources };
|
|
209
|
+
};
|
|
210
|
+
const createSearchAPI = (config) => {
|
|
211
|
+
const { searchProvider = 'serper', serperApiKey, searxngInstanceUrl, searxngApiKey, } = config;
|
|
212
|
+
if (searchProvider.toLowerCase() === 'serper') {
|
|
213
|
+
return createSerperAPI(serperApiKey);
|
|
214
|
+
}
|
|
215
|
+
else if (searchProvider.toLowerCase() === 'searxng') {
|
|
216
|
+
return createSearXNGAPI(searxngInstanceUrl, searxngApiKey);
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
throw new Error(`Invalid search provider: ${searchProvider}. Must be 'serper' or 'searxng'`);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const createSourceProcessor = (config = {}, scraperInstance) => {
|
|
223
|
+
if (!scraperInstance) {
|
|
224
|
+
throw new Error('Firecrawl scraper instance is required');
|
|
225
|
+
}
|
|
226
|
+
const { topResults = 5,
|
|
227
|
+
// strategies = ['no_extraction'],
|
|
228
|
+
// filterContent = true,
|
|
229
|
+
reranker, } = config;
|
|
230
|
+
const firecrawlScraper = scraperInstance;
|
|
231
|
+
const webScraper = {
|
|
232
|
+
scrapeMany: async ({ query, links, }) => {
|
|
233
|
+
console.log(`Scraping ${links.length} links with Firecrawl`);
|
|
234
|
+
const promises = [];
|
|
235
|
+
try {
|
|
236
|
+
for (const currentLink of links) {
|
|
237
|
+
const promise = firecrawlScraper
|
|
238
|
+
.scrapeUrl(currentLink, {})
|
|
239
|
+
.then(([url, response]) => {
|
|
240
|
+
const attribution = firecrawl.getAttribution(url, response.data?.metadata);
|
|
241
|
+
if (response.success && response.data) {
|
|
242
|
+
const content = firecrawlScraper.extractContent(response);
|
|
243
|
+
return {
|
|
244
|
+
url,
|
|
245
|
+
attribution,
|
|
246
|
+
content: chunker.cleanText(content),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
url,
|
|
251
|
+
attribution,
|
|
252
|
+
error: true,
|
|
253
|
+
content: `Failed to scrape ${url}: ${response.error ?? 'Unknown error'}`,
|
|
254
|
+
};
|
|
255
|
+
})
|
|
256
|
+
.then(async (result) => {
|
|
257
|
+
try {
|
|
258
|
+
if (result.error != null) {
|
|
259
|
+
console.error(`Error scraping ${result.url}: ${result.content}`);
|
|
260
|
+
return {
|
|
261
|
+
...result,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
const highlights = await getHighlights({
|
|
265
|
+
query,
|
|
266
|
+
reranker,
|
|
267
|
+
content: result.content,
|
|
268
|
+
});
|
|
269
|
+
return {
|
|
270
|
+
...result,
|
|
271
|
+
highlights,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
console.error('Error processing scraped content:', error);
|
|
276
|
+
return {
|
|
277
|
+
...result,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
})
|
|
281
|
+
.catch((error) => {
|
|
282
|
+
console.error(`Error scraping ${currentLink}:`, error);
|
|
283
|
+
return {
|
|
284
|
+
url: currentLink,
|
|
285
|
+
error: true,
|
|
286
|
+
content: `Failed to scrape ${currentLink}: ${error.message ?? 'Unknown error'}`,
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
promises.push(promise);
|
|
290
|
+
}
|
|
291
|
+
return await Promise.all(promises);
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
console.error('Error in scrapeMany:', error);
|
|
295
|
+
return [];
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
const fetchContents = async ({ links, query, target, onContentScraped, }) => {
|
|
300
|
+
const initialLinks = links.slice(0, target);
|
|
301
|
+
// const remainingLinks = links.slice(target).reverse();
|
|
302
|
+
const results = await webScraper.scrapeMany({ query, links: initialLinks });
|
|
303
|
+
for (const result of results) {
|
|
304
|
+
if (result.error === true) {
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const { url, content, attribution, highlights } = result;
|
|
308
|
+
onContentScraped?.(url, {
|
|
309
|
+
content,
|
|
310
|
+
attribution,
|
|
311
|
+
highlights,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
const processSources = async (result, numElements, query, proMode = false) => {
|
|
316
|
+
try {
|
|
317
|
+
if (!result.data) {
|
|
318
|
+
return {
|
|
319
|
+
organic: [],
|
|
320
|
+
topStories: [],
|
|
321
|
+
images: [],
|
|
322
|
+
relatedSearches: [],
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
else if (!result.data.organic) {
|
|
326
|
+
return result.data;
|
|
327
|
+
}
|
|
328
|
+
if (!proMode) {
|
|
329
|
+
const wikiSources = result.data.organic.filter((source) => source.link.includes('wikipedia.org'));
|
|
330
|
+
if (!wikiSources.length) {
|
|
331
|
+
return result.data;
|
|
332
|
+
}
|
|
333
|
+
const wikiSourceMap = new Map();
|
|
334
|
+
wikiSourceMap.set(wikiSources[0].link, wikiSources[0]);
|
|
335
|
+
const onContentScraped = createSourceUpdateCallback(wikiSourceMap);
|
|
336
|
+
await fetchContents({
|
|
337
|
+
query,
|
|
338
|
+
target: 1,
|
|
339
|
+
onContentScraped,
|
|
340
|
+
links: [wikiSources[0].link],
|
|
341
|
+
});
|
|
342
|
+
for (let i = 0; i < result.data.organic.length; i++) {
|
|
343
|
+
const source = result.data.organic[i];
|
|
344
|
+
const updatedSource = wikiSourceMap.get(source.link);
|
|
345
|
+
if (updatedSource) {
|
|
346
|
+
result.data.organic[i] = {
|
|
347
|
+
...source,
|
|
348
|
+
...updatedSource,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return result.data;
|
|
353
|
+
}
|
|
354
|
+
const sourceMap = new Map();
|
|
355
|
+
const allLinks = [];
|
|
356
|
+
for (const source of result.data.organic) {
|
|
357
|
+
if (source.link) {
|
|
358
|
+
allLinks.push(source.link);
|
|
359
|
+
sourceMap.set(source.link, source);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (allLinks.length === 0) {
|
|
363
|
+
return result.data;
|
|
364
|
+
}
|
|
365
|
+
const onContentScraped = createSourceUpdateCallback(sourceMap);
|
|
366
|
+
await fetchContents({
|
|
367
|
+
links: allLinks,
|
|
368
|
+
query,
|
|
369
|
+
onContentScraped,
|
|
370
|
+
target: numElements,
|
|
371
|
+
});
|
|
372
|
+
for (let i = 0; i < result.data.organic.length; i++) {
|
|
373
|
+
const source = result.data.organic[i];
|
|
374
|
+
const updatedSource = sourceMap.get(source.link);
|
|
375
|
+
if (updatedSource) {
|
|
376
|
+
result.data.organic[i] = {
|
|
377
|
+
...source,
|
|
378
|
+
...updatedSource,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const successfulSources = result.data.organic
|
|
383
|
+
.filter((source) => source.content != null && !source.content.startsWith('Failed'))
|
|
384
|
+
.slice(0, numElements);
|
|
385
|
+
if (successfulSources.length > 0) {
|
|
386
|
+
result.data.organic = successfulSources;
|
|
387
|
+
}
|
|
388
|
+
return result.data;
|
|
389
|
+
}
|
|
390
|
+
catch (error) {
|
|
391
|
+
console.error('Error in processSources:', error);
|
|
392
|
+
return {
|
|
393
|
+
organic: [],
|
|
394
|
+
topStories: [],
|
|
395
|
+
images: [],
|
|
396
|
+
relatedSearches: [],
|
|
397
|
+
...result.data,
|
|
398
|
+
error: error instanceof Error ? error.message : String(error),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
return {
|
|
403
|
+
processSources,
|
|
404
|
+
topResults,
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
exports.createSearchAPI = createSearchAPI;
|
|
409
|
+
exports.createSourceProcessor = createSourceProcessor;
|
|
410
|
+
//# sourceMappingURL=search.cjs.map
|