@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.
Files changed (55) 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/main.cjs +2 -0
  4. package/dist/cjs/main.cjs.map +1 -1
  5. package/dist/cjs/tools/search/firecrawl.cjs +149 -0
  6. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
  7. package/dist/cjs/tools/search/format.cjs +116 -0
  8. package/dist/cjs/tools/search/format.cjs.map +1 -0
  9. package/dist/cjs/tools/search/highlights.cjs +194 -0
  10. package/dist/cjs/tools/search/highlights.cjs.map +1 -0
  11. package/dist/cjs/tools/search/rerankers.cjs +187 -0
  12. package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
  13. package/dist/cjs/tools/search/search.cjs +410 -0
  14. package/dist/cjs/tools/search/search.cjs.map +1 -0
  15. package/dist/cjs/tools/search/tool.cjs +103 -0
  16. package/dist/cjs/tools/search/tool.cjs.map +1 -0
  17. package/dist/esm/common/enum.mjs +1 -0
  18. package/dist/esm/common/enum.mjs.map +1 -1
  19. package/dist/esm/main.mjs +1 -0
  20. package/dist/esm/main.mjs.map +1 -1
  21. package/dist/esm/tools/search/firecrawl.mjs +145 -0
  22. package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
  23. package/dist/esm/tools/search/format.mjs +114 -0
  24. package/dist/esm/tools/search/format.mjs.map +1 -0
  25. package/dist/esm/tools/search/highlights.mjs +192 -0
  26. package/dist/esm/tools/search/highlights.mjs.map +1 -0
  27. package/dist/esm/tools/search/rerankers.mjs +181 -0
  28. package/dist/esm/tools/search/rerankers.mjs.map +1 -0
  29. package/dist/esm/tools/search/search.mjs +407 -0
  30. package/dist/esm/tools/search/search.mjs.map +1 -0
  31. package/dist/esm/tools/search/tool.mjs +101 -0
  32. package/dist/esm/tools/search/tool.mjs.map +1 -0
  33. package/dist/types/common/enum.d.ts +1 -0
  34. package/dist/types/index.d.ts +1 -0
  35. package/dist/types/scripts/search.d.ts +1 -0
  36. package/dist/types/tools/search/firecrawl.d.ts +117 -0
  37. package/dist/types/tools/search/format.d.ts +2 -0
  38. package/dist/types/tools/search/highlights.d.ts +13 -0
  39. package/dist/types/tools/search/index.d.ts +2 -0
  40. package/dist/types/tools/search/rerankers.d.ts +32 -0
  41. package/dist/types/tools/search/search.d.ts +9 -0
  42. package/dist/types/tools/search/tool.d.ts +12 -0
  43. package/dist/types/tools/search/types.d.ts +150 -0
  44. package/package.json +2 -1
  45. package/src/common/enum.ts +1 -0
  46. package/src/index.ts +1 -0
  47. package/src/scripts/search.ts +141 -0
  48. package/src/tools/search/firecrawl.ts +270 -0
  49. package/src/tools/search/format.ts +121 -0
  50. package/src/tools/search/highlights.ts +238 -0
  51. package/src/tools/search/index.ts +2 -0
  52. package/src/tools/search/rerankers.ts +248 -0
  53. package/src/tools/search/search.ts +567 -0
  54. package/src/tools/search/tool.ts +151 -0
  55. package/src/tools/search/types.ts +179 -0
@@ -0,0 +1,248 @@
1
+ /* eslint-disable no-console */
2
+ import axios from 'axios';
3
+ import type * as t from './types';
4
+
5
+ export abstract class BaseReranker {
6
+ protected apiKey: string | undefined;
7
+
8
+ constructor() {
9
+ // Each specific reranker will set its API key
10
+ }
11
+
12
+ abstract rerank(
13
+ query: string,
14
+ documents: string[],
15
+ topK?: number
16
+ ): Promise<t.Highlight[]>;
17
+
18
+ protected getDefaultRanking(
19
+ documents: string[],
20
+ topK: number
21
+ ): t.Highlight[] {
22
+ return documents
23
+ .slice(0, Math.min(topK, documents.length))
24
+ .map((doc) => ({ text: doc, score: 0 }));
25
+ }
26
+
27
+ protected logDocumentSamples(documents: string[]): void {
28
+ console.log('Sample documents being sent to API:');
29
+ for (let i = 0; i < Math.min(3, documents.length); i++) {
30
+ console.log(`Document ${i}: ${documents[i].substring(0, 100)}...`);
31
+ }
32
+ }
33
+ }
34
+
35
+ export class JinaReranker extends BaseReranker {
36
+ constructor({ apiKey = process.env.JINA_API_KEY }: { apiKey?: string }) {
37
+ super();
38
+ this.apiKey = apiKey;
39
+ }
40
+
41
+ async rerank(
42
+ query: string,
43
+ documents: string[],
44
+ topK: number = 5
45
+ ): Promise<t.Highlight[]> {
46
+ console.log(`Reranking ${documents.length} documents with Jina`);
47
+
48
+ try {
49
+ if (this.apiKey == null || this.apiKey === '') {
50
+ console.warn('JINA_API_KEY is not set. Using default ranking.');
51
+ return this.getDefaultRanking(documents, topK);
52
+ }
53
+
54
+ this.logDocumentSamples(documents);
55
+
56
+ const requestData = {
57
+ model: 'jina-reranker-v2-base-multilingual',
58
+ query: query,
59
+ top_n: topK,
60
+ documents: documents,
61
+ return_documents: true,
62
+ };
63
+
64
+ const response = await axios.post<t.JinaRerankerResponse | undefined>(
65
+ 'https://api.jina.ai/v1/rerank',
66
+ requestData,
67
+ {
68
+ headers: {
69
+ 'Content-Type': 'application/json',
70
+ Authorization: `Bearer ${this.apiKey}`,
71
+ },
72
+ }
73
+ );
74
+
75
+ // Log the response data structure
76
+ console.log('Jina API response structure:');
77
+ console.log('Model:', response.data?.model);
78
+ console.log('Usage:', response.data?.usage);
79
+ console.log('Results count:', response.data?.results.length);
80
+
81
+ // Log a sample of the results
82
+ if ((response.data?.results.length ?? 0) > 0) {
83
+ console.log(
84
+ 'Sample result:',
85
+ JSON.stringify(response.data?.results[0], null, 2)
86
+ );
87
+ }
88
+
89
+ if (response.data && response.data.results.length) {
90
+ return response.data.results.map((result) => {
91
+ const docIndex = result.index;
92
+ const score = result.relevance_score;
93
+ let text = '';
94
+
95
+ // If return_documents is true, the document field will be present
96
+ if (result.document != null) {
97
+ const doc = result.document;
98
+ if (typeof doc === 'object' && 'text' in doc) {
99
+ text = doc.text;
100
+ } else if (typeof doc === 'string') {
101
+ text = doc;
102
+ }
103
+ } else {
104
+ // Otherwise, use the index to get the document
105
+ text = documents[docIndex];
106
+ }
107
+
108
+ return { text, score };
109
+ });
110
+ } else {
111
+ console.warn(
112
+ 'Unexpected response format from Jina API. Using default ranking.'
113
+ );
114
+ return this.getDefaultRanking(documents, topK);
115
+ }
116
+ } catch (error) {
117
+ console.error('Error using Jina reranker:', error);
118
+ // Fallback to default ranking on error
119
+ return this.getDefaultRanking(documents, topK);
120
+ }
121
+ }
122
+ }
123
+
124
+ export class CohereReranker extends BaseReranker {
125
+ constructor({ apiKey = process.env.COHERE_API_KEY }: { apiKey?: string }) {
126
+ super();
127
+ this.apiKey = apiKey;
128
+ }
129
+
130
+ async rerank(
131
+ query: string,
132
+ documents: string[],
133
+ topK: number = 5
134
+ ): Promise<t.Highlight[]> {
135
+ console.log(`Reranking ${documents.length} documents with Cohere`);
136
+
137
+ try {
138
+ if (this.apiKey == null || this.apiKey === '') {
139
+ console.warn('COHERE_API_KEY is not set. Using default ranking.');
140
+ return this.getDefaultRanking(documents, topK);
141
+ }
142
+
143
+ this.logDocumentSamples(documents);
144
+
145
+ const requestData = {
146
+ model: 'rerank-v3.5',
147
+ query: query,
148
+ top_n: topK,
149
+ documents: documents,
150
+ };
151
+
152
+ const response = await axios.post<t.CohereRerankerResponse | undefined>(
153
+ 'https://api.cohere.com/v2/rerank',
154
+ requestData,
155
+ {
156
+ headers: {
157
+ 'Content-Type': 'application/json',
158
+ Authorization: `Bearer ${this.apiKey}`,
159
+ },
160
+ }
161
+ );
162
+
163
+ // Log the response data structure
164
+ console.log('Cohere API response structure:');
165
+ console.log('ID:', response.data?.id);
166
+ console.log('Meta:', response.data?.meta);
167
+ console.log('Results count:', response.data?.results.length);
168
+
169
+ // Log a sample of the results
170
+ if ((response.data?.results.length ?? 0) > 0) {
171
+ console.log(
172
+ 'Sample result:',
173
+ JSON.stringify(response.data?.results[0], null, 2)
174
+ );
175
+ }
176
+
177
+ if (response.data && response.data.results.length) {
178
+ return response.data.results.map((result) => {
179
+ const docIndex = result.index;
180
+ const score = result.relevance_score;
181
+ const text = documents[docIndex];
182
+ return { text, score };
183
+ });
184
+ } else {
185
+ console.warn(
186
+ 'Unexpected response format from Cohere API. Using default ranking.'
187
+ );
188
+ return this.getDefaultRanking(documents, topK);
189
+ }
190
+ } catch (error) {
191
+ console.error('Error using Cohere reranker:', error);
192
+ // Fallback to default ranking on error
193
+ return this.getDefaultRanking(documents, topK);
194
+ }
195
+ }
196
+ }
197
+
198
+ export class InfinityReranker extends BaseReranker {
199
+ constructor() {
200
+ super();
201
+ // No API key needed for the placeholder implementation
202
+ }
203
+
204
+ async rerank(
205
+ query: string,
206
+ documents: string[],
207
+ topK: number = 5
208
+ ): Promise<t.Highlight[]> {
209
+ console.log(
210
+ `Reranking ${documents.length} documents with Infinity (placeholder)`
211
+ );
212
+ // This would be replaced with actual Infinity reranker implementation
213
+ return this.getDefaultRanking(documents, topK);
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Creates the appropriate reranker based on type and configuration
219
+ */
220
+ export const createReranker = (config: {
221
+ rerankerType: t.RerankerType;
222
+ jinaApiKey?: string;
223
+ cohereApiKey?: string;
224
+ }): BaseReranker | undefined => {
225
+ const { rerankerType, jinaApiKey, cohereApiKey } = config;
226
+
227
+ switch (rerankerType.toLowerCase()) {
228
+ case 'jina':
229
+ return new JinaReranker({ apiKey: jinaApiKey });
230
+ case 'cohere':
231
+ return new CohereReranker({ apiKey: cohereApiKey });
232
+ case 'infinity':
233
+ return new InfinityReranker();
234
+ case 'none':
235
+ console.log('Skipping reranking as reranker is set to "none"');
236
+ return undefined;
237
+ default:
238
+ console.warn(
239
+ `Unknown reranker type: ${rerankerType}. Defaulting to InfinityReranker.`
240
+ );
241
+ return new JinaReranker({ apiKey: jinaApiKey });
242
+ }
243
+ };
244
+
245
+ // Example usage:
246
+ // const jinaReranker = new JinaReranker();
247
+ // const cohereReranker = new CohereReranker();
248
+ // const infinityReranker = new InfinityReranker();