@librechat/agents 4.2.19 → 4.2.20
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/agents/AgentContext.cjs +2 -6
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +14 -1
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +89 -38
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +67 -6
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +88 -12
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/cjs/tools/search/keenable-scraper.cjs +27 -7
- package/dist/cjs/tools/search/keenable-scraper.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +2 -6
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +14 -1
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +89 -38
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +67 -6
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +88 -12
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/esm/tools/search/keenable-scraper.mjs +27 -7
- package/dist/esm/tools/search/keenable-scraper.mjs.map +1 -1
- package/dist/types/tools/ToolNode.d.ts +7 -0
- package/dist/types/tools/search/keenable-scraper.d.ts +6 -0
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +7 -11
- package/src/graphs/Graph.ts +19 -1
- package/src/llm/vertexai/index.ts +193 -67
- package/src/tools/ToolNode.ts +133 -11
- package/src/tools/ToolSearch.ts +161 -12
- package/src/tools/search/keenable-scraper.ts +62 -7
package/src/tools/ToolSearch.ts
CHANGED
|
@@ -26,6 +26,7 @@ import { tool, DynamicStructuredTool } from '@langchain/core/tools';
|
|
|
26
26
|
import type * as t from '@/types';
|
|
27
27
|
import { INTENT_PROPERTY } from '@/tools/intentArg';
|
|
28
28
|
import { buildCodeApiEndpoint, getCodeBaseURL } from './CodeExecutor';
|
|
29
|
+
import { allowsToolCaller } from './CallerCapabilities';
|
|
29
30
|
import { Constants } from '@/common';
|
|
30
31
|
|
|
31
32
|
config();
|
|
@@ -821,6 +822,56 @@ function parseSearchResults(stdout: string): t.ToolSearchResponse {
|
|
|
821
822
|
return parsed;
|
|
822
823
|
}
|
|
823
824
|
|
|
825
|
+
const TOOL_SEARCH_NEXT_GUIDANCE =
|
|
826
|
+
'call these directly; they stay loaded for the rest of the conversation';
|
|
827
|
+
/** For a result that includes tools only code execution may call. */
|
|
828
|
+
const TOOL_SEARCH_MIXED_GUIDANCE =
|
|
829
|
+
'call these directly, except tools marked callable_from: code_execution, which only run from code execution; they stay loaded for the rest of the conversation';
|
|
830
|
+
|
|
831
|
+
type ToolSearchRef = t.ToolSearchResponse['tool_references'][number];
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Loaded tools whose full or base name equals the whole query or one of its words, so a model
|
|
835
|
+
* searching for a tool it already has is told so instead of reading an empty result.
|
|
836
|
+
*/
|
|
837
|
+
function findLoadedNameHits(
|
|
838
|
+
loadedTools: readonly t.LCTool[],
|
|
839
|
+
query: string,
|
|
840
|
+
maxResults: number
|
|
841
|
+
): ToolSearchRef[] {
|
|
842
|
+
const trimmed = query.trim().toLowerCase();
|
|
843
|
+
if (trimmed === '' || loadedTools.length === 0) {
|
|
844
|
+
return [];
|
|
845
|
+
}
|
|
846
|
+
const tokens = new Set<string>([trimmed]);
|
|
847
|
+
for (const word of trimmed.split(/\s+/)) {
|
|
848
|
+
if (word !== '') {
|
|
849
|
+
tokens.add(word);
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
const hits: ToolSearchRef[] = [];
|
|
854
|
+
for (const lcTool of loadedTools) {
|
|
855
|
+
const fullName = lcTool.name.toLowerCase();
|
|
856
|
+
const baseName = getBaseToolName(lcTool.name).toLowerCase();
|
|
857
|
+
if (tokens.has(fullName) || tokens.has(baseName)) {
|
|
858
|
+
hits.push({
|
|
859
|
+
tool_name: lcTool.name,
|
|
860
|
+
match_score: 1,
|
|
861
|
+
matched_field: 'name',
|
|
862
|
+
snippet:
|
|
863
|
+
lcTool.description != null && lcTool.description !== ''
|
|
864
|
+
? lcTool.description.slice(0, 120)
|
|
865
|
+
: lcTool.name,
|
|
866
|
+
});
|
|
867
|
+
if (hits.length >= maxResults) {
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
return hits;
|
|
873
|
+
}
|
|
874
|
+
|
|
824
875
|
/**
|
|
825
876
|
* Formats search results as structured JSON for efficient parsing.
|
|
826
877
|
* @param searchResponse - The parsed search response
|
|
@@ -832,19 +883,30 @@ function parseSearchResults(stdout: string): t.ToolSearchResponse {
|
|
|
832
883
|
function formatSearchResults(
|
|
833
884
|
searchResponse: t.ToolSearchResponse,
|
|
834
885
|
nameFormat: t.McpNameFormat = 'full',
|
|
835
|
-
notes: string[] = []
|
|
886
|
+
notes: string[] = [],
|
|
887
|
+
loadedStatusByTool?: ReadonlyMap<string, 'loaded' | 'already_loaded'>,
|
|
888
|
+
codeOnlyTools?: ReadonlySet<string>
|
|
836
889
|
): string {
|
|
837
890
|
const { tool_references, total_tools_searched, pattern_used } =
|
|
838
891
|
searchResponse;
|
|
839
892
|
const useFullName = nameFormat === 'full';
|
|
893
|
+
const isCodeOnly = (name: string): boolean => codeOnlyTools?.has(name) === true;
|
|
894
|
+
const next = tool_references.some((ref) => isCodeOnly(ref.tool_name))
|
|
895
|
+
? TOOL_SEARCH_MIXED_GUIDANCE
|
|
896
|
+
: TOOL_SEARCH_NEXT_GUIDANCE;
|
|
840
897
|
|
|
841
898
|
const output = {
|
|
842
899
|
found: tool_references.length,
|
|
900
|
+
...(tool_references.length > 0 ? { next } : {}),
|
|
843
901
|
tools: tool_references.map((ref) => ({
|
|
844
902
|
name: useFullName ? ref.tool_name : getBaseToolName(ref.tool_name),
|
|
845
903
|
score: Number(ref.match_score.toFixed(2)),
|
|
846
904
|
matched_in: ref.matched_field,
|
|
847
905
|
snippet: ref.snippet,
|
|
906
|
+
status: loadedStatusByTool?.get(ref.tool_name) ?? 'loaded',
|
|
907
|
+
...(isCodeOnly(ref.tool_name)
|
|
908
|
+
? { callable_from: 'code_execution' as const }
|
|
909
|
+
: {}),
|
|
848
910
|
})),
|
|
849
911
|
total_searched: total_tools_searched,
|
|
850
912
|
query: pattern_used,
|
|
@@ -1121,13 +1183,21 @@ ${mcpNote}${toolsListSection}
|
|
|
1121
1183
|
* single traversal, with no staging array and no copy of the registry. */
|
|
1122
1184
|
const staged: t.LCTool[] | undefined = hasServerFilter ? [] : undefined;
|
|
1123
1185
|
const deferredTools: t.ToolMetadata[] = [];
|
|
1186
|
+
const loadedCandidates: t.LCTool[] = [];
|
|
1124
1187
|
let searchableCount = 0;
|
|
1125
1188
|
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1189
|
+
/** Deferred tools the model can reach only through code execution. */
|
|
1190
|
+
const codeOnlyTools = new Set<string>();
|
|
1191
|
+
const describeTool = (lcTool: t.LCTool): t.ToolMetadata => {
|
|
1192
|
+
if (!allowsToolCaller(lcTool, 'direct')) {
|
|
1193
|
+
codeOnlyTools.add(lcTool.name);
|
|
1194
|
+
}
|
|
1195
|
+
return {
|
|
1196
|
+
name: lcTool.name,
|
|
1197
|
+
description: lcTool.description ?? '',
|
|
1198
|
+
parameters: simplifyParametersForSearch(lcTool.parameters),
|
|
1199
|
+
};
|
|
1200
|
+
};
|
|
1131
1201
|
|
|
1132
1202
|
for (const lcTool of toolRegistry.values()) {
|
|
1133
1203
|
let server: string | undefined;
|
|
@@ -1140,6 +1210,11 @@ ${mcpNote}${toolsListSection}
|
|
|
1140
1210
|
}
|
|
1141
1211
|
}
|
|
1142
1212
|
if (onlyDeferred === true && lcTool.defer_loading !== true) {
|
|
1213
|
+
/** Only a tool bound for direct calls is one the model can "call directly"; a
|
|
1214
|
+
* programmatic-only tool is loaded but never offered to it. */
|
|
1215
|
+
if (allowsToolCaller(lcTool, 'direct')) {
|
|
1216
|
+
loadedCandidates.push(lcTool);
|
|
1217
|
+
}
|
|
1143
1218
|
continue;
|
|
1144
1219
|
}
|
|
1145
1220
|
searchableCount += 1;
|
|
@@ -1209,7 +1284,49 @@ ${mcpNote}${toolsListSection}
|
|
|
1209
1284
|
}
|
|
1210
1285
|
}
|
|
1211
1286
|
|
|
1287
|
+
const loadedTools = hasServerFilter
|
|
1288
|
+
? loadedCandidates.filter((lcTool) =>
|
|
1289
|
+
isFromAnyMcpServer(lcTool.name, matchedServers)
|
|
1290
|
+
)
|
|
1291
|
+
: loadedCandidates;
|
|
1292
|
+
|
|
1293
|
+
const isServerListing = hasServerFilter && query === '';
|
|
1294
|
+
const loadedHits =
|
|
1295
|
+
mode === 'local' && !isServerListing
|
|
1296
|
+
? findLoadedNameHits(loadedTools, query, max_results)
|
|
1297
|
+
: [];
|
|
1298
|
+
|
|
1212
1299
|
if (deferredTools.length === 0) {
|
|
1300
|
+
if (loadedHits.length > 0) {
|
|
1301
|
+
const loadedStatusByTool = new Map<
|
|
1302
|
+
string,
|
|
1303
|
+
'loaded' | 'already_loaded'
|
|
1304
|
+
>(loadedHits.map((hit) => [hit.tool_name, 'already_loaded']));
|
|
1305
|
+
const formattedOutput = formatSearchResults(
|
|
1306
|
+
{
|
|
1307
|
+
tool_references: loadedHits,
|
|
1308
|
+
total_tools_searched: 0,
|
|
1309
|
+
pattern_used: query,
|
|
1310
|
+
},
|
|
1311
|
+
mcpNameFormat,
|
|
1312
|
+
filterNotes,
|
|
1313
|
+
loadedStatusByTool,
|
|
1314
|
+
codeOnlyTools
|
|
1315
|
+
);
|
|
1316
|
+
return [
|
|
1317
|
+
formattedOutput,
|
|
1318
|
+
{
|
|
1319
|
+
tool_references: [],
|
|
1320
|
+
metadata: {
|
|
1321
|
+
total_searched: 0,
|
|
1322
|
+
pattern: query,
|
|
1323
|
+
mcp_server:
|
|
1324
|
+
serverFilters.length > 0 ? serverFilters : undefined,
|
|
1325
|
+
...filterMetadata,
|
|
1326
|
+
},
|
|
1327
|
+
},
|
|
1328
|
+
];
|
|
1329
|
+
}
|
|
1213
1330
|
/** Say which of the three it is, so an empty result is not read as an
|
|
1214
1331
|
* outage: nothing is registered, the name did not match anything, or
|
|
1215
1332
|
* the named server genuinely has no tools. */
|
|
@@ -1264,8 +1381,6 @@ ${mcpNote}${toolsListSection}
|
|
|
1264
1381
|
];
|
|
1265
1382
|
}
|
|
1266
1383
|
|
|
1267
|
-
const isServerListing = hasServerFilter && query === '';
|
|
1268
|
-
|
|
1269
1384
|
if (isServerListing) {
|
|
1270
1385
|
/** The listing's contract is parseable JSON, so the diagnostics go
|
|
1271
1386
|
* inside the payload rather than in front of it. */
|
|
@@ -1297,17 +1412,51 @@ ${mcpNote}${toolsListSection}
|
|
|
1297
1412
|
fields,
|
|
1298
1413
|
max_results
|
|
1299
1414
|
);
|
|
1415
|
+
const loadedStatusByTool = new Map<
|
|
1416
|
+
string,
|
|
1417
|
+
'loaded' | 'already_loaded'
|
|
1418
|
+
>();
|
|
1419
|
+
const displayReferences: ToolSearchRef[] = [];
|
|
1420
|
+
const seenNames = new Set<string>();
|
|
1421
|
+
for (const hit of loadedHits) {
|
|
1422
|
+
if (displayReferences.length >= max_results) {
|
|
1423
|
+
break;
|
|
1424
|
+
}
|
|
1425
|
+
seenNames.add(hit.tool_name);
|
|
1426
|
+
loadedStatusByTool.set(hit.tool_name, 'already_loaded');
|
|
1427
|
+
displayReferences.push(hit);
|
|
1428
|
+
}
|
|
1429
|
+
for (const ref of searchResponse.tool_references) {
|
|
1430
|
+
if (displayReferences.length >= max_results) {
|
|
1431
|
+
break;
|
|
1432
|
+
}
|
|
1433
|
+
if (!seenNames.has(ref.tool_name)) {
|
|
1434
|
+
seenNames.add(ref.tool_name);
|
|
1435
|
+
loadedStatusByTool.set(ref.tool_name, 'loaded');
|
|
1436
|
+
displayReferences.push(ref);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1300
1439
|
/** Diagnostics ride inside the payload; this output is parsed. */
|
|
1301
1440
|
const formattedOutput = formatSearchResults(
|
|
1302
|
-
|
|
1441
|
+
{
|
|
1442
|
+
...searchResponse,
|
|
1443
|
+
tool_references: displayReferences,
|
|
1444
|
+
},
|
|
1303
1445
|
mcpNameFormat,
|
|
1304
|
-
filterNotes
|
|
1446
|
+
filterNotes,
|
|
1447
|
+
loadedStatusByTool,
|
|
1448
|
+
codeOnlyTools
|
|
1449
|
+
);
|
|
1450
|
+
/** Load only the deferred tools the model was shown: loaded hits take display slots, and
|
|
1451
|
+
* a tool loaded without being listed would be one the model never hears about. */
|
|
1452
|
+
const shownDeferred = displayReferences.filter(
|
|
1453
|
+
(ref) => loadedStatusByTool.get(ref.tool_name) === 'loaded'
|
|
1305
1454
|
);
|
|
1306
1455
|
|
|
1307
1456
|
return [
|
|
1308
1457
|
formattedOutput,
|
|
1309
1458
|
{
|
|
1310
|
-
tool_references:
|
|
1459
|
+
tool_references: shownDeferred,
|
|
1311
1460
|
metadata: {
|
|
1312
1461
|
total_searched: searchResponse.total_tools_searched,
|
|
1313
1462
|
pattern: searchResponse.pattern_used,
|
|
@@ -1380,7 +1529,7 @@ ${mcpNote}${toolsListSection}
|
|
|
1380
1529
|
}
|
|
1381
1530
|
|
|
1382
1531
|
const searchResponse = parseSearchResults(result.stdout);
|
|
1383
|
-
const formattedOutput = `${warningMessage}${formatSearchResults(searchResponse, mcpNameFormat, filterNotes)}`;
|
|
1532
|
+
const formattedOutput = `${warningMessage}${formatSearchResults(searchResponse, mcpNameFormat, filterNotes, undefined, codeOnlyTools)}`;
|
|
1384
1533
|
|
|
1385
1534
|
return [
|
|
1386
1535
|
formattedOutput,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
+
import type { AxiosResponse } from 'axios';
|
|
2
3
|
import type * as t from './types';
|
|
3
4
|
import { createDefaultLogger } from './utils';
|
|
4
5
|
|
|
@@ -9,6 +10,26 @@ const DEFAULT_KEENABLE_SCRAPE_TIMEOUT = 15000;
|
|
|
9
10
|
const KEENABLE_FETCH_API_URL = 'https://api.keenable.ai/v1/fetch';
|
|
10
11
|
const KEENABLE_FETCH_PUBLIC_URL = 'https://api.keenable.ai/v1/fetch/public';
|
|
11
12
|
|
|
13
|
+
/** Keenable enforces a per-second rate limit and answers bursts with 429 and
|
|
14
|
+
* `Retry-After`; such requests are retried a bounded number of times. */
|
|
15
|
+
const KEENABLE_MAX_RATE_LIMIT_RETRIES = 2;
|
|
16
|
+
const KEENABLE_DEFAULT_RETRY_DELAY_MS = 1000;
|
|
17
|
+
const KEENABLE_MAX_RETRY_DELAY_MS = 3000;
|
|
18
|
+
const KEENABLE_RETRY_JITTER_MS = 500;
|
|
19
|
+
|
|
20
|
+
/** Delay before retrying a 429 response, or undefined for any other failure. */
|
|
21
|
+
const getRateLimitRetryDelayMs = (error: unknown): number | undefined => {
|
|
22
|
+
if (!axios.isAxiosError(error) || error.response?.status !== 429) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const retryAfterSeconds = Number(error.response.headers['retry-after']);
|
|
26
|
+
const baseDelayMs =
|
|
27
|
+
Number.isFinite(retryAfterSeconds) && retryAfterSeconds >= 0
|
|
28
|
+
? Math.min(retryAfterSeconds * 1000, KEENABLE_MAX_RETRY_DELAY_MS)
|
|
29
|
+
: KEENABLE_DEFAULT_RETRY_DELAY_MS;
|
|
30
|
+
return baseDelayMs + Math.random() * KEENABLE_RETRY_JITTER_MS;
|
|
31
|
+
};
|
|
32
|
+
|
|
12
33
|
export class KeenableScraper implements t.BaseScraper {
|
|
13
34
|
private apiKey: string | undefined;
|
|
14
35
|
private apiUrl: string;
|
|
@@ -48,6 +69,43 @@ export class KeenableScraper implements t.BaseScraper {
|
|
|
48
69
|
return headers;
|
|
49
70
|
}
|
|
50
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Fetches a page, retrying a 429 within the request's own timeout: every attempt and wait share
|
|
74
|
+
* one deadline, so retries never make a fetch outlast the timeout a caller already accepted. A
|
|
75
|
+
* timeout of 0 keeps meaning "no timeout" to axios, and then no deadline applies.
|
|
76
|
+
*/
|
|
77
|
+
private async fetchPage(
|
|
78
|
+
url: string,
|
|
79
|
+
timeout: number
|
|
80
|
+
): Promise<AxiosResponse<t.KeenableFetchResult>> {
|
|
81
|
+
const deadline = timeout > 0 ? Date.now() + timeout : Infinity;
|
|
82
|
+
for (let attempt = 0; ; attempt++) {
|
|
83
|
+
try {
|
|
84
|
+
return await axios.get<t.KeenableFetchResult>(this.apiUrl, {
|
|
85
|
+
params: { url },
|
|
86
|
+
headers: this.buildHeaders(),
|
|
87
|
+
timeout:
|
|
88
|
+
deadline === Infinity ? 0 : Math.max(1, deadline - Date.now()),
|
|
89
|
+
httpAgent: this.httpAgent,
|
|
90
|
+
httpsAgent: this.httpsAgent,
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
const delayMs = getRateLimitRetryDelayMs(error);
|
|
94
|
+
if (
|
|
95
|
+
delayMs == null ||
|
|
96
|
+
attempt >= KEENABLE_MAX_RATE_LIMIT_RETRIES ||
|
|
97
|
+
Date.now() + delayMs >= deadline
|
|
98
|
+
) {
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
this.logger.debug(
|
|
102
|
+
`Keenable Fetch rate limited (429), retrying in ${Math.round(delayMs)} ms`
|
|
103
|
+
);
|
|
104
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
51
109
|
async scrapeUrl(
|
|
52
110
|
url: string,
|
|
53
111
|
options: t.KeenableScrapeOptions = {}
|
|
@@ -57,13 +115,10 @@ export class KeenableScraper implements t.BaseScraper {
|
|
|
57
115
|
}
|
|
58
116
|
|
|
59
117
|
try {
|
|
60
|
-
const response = await
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
httpAgent: this.httpAgent,
|
|
65
|
-
httpsAgent: this.httpsAgent,
|
|
66
|
-
});
|
|
118
|
+
const response = await this.fetchPage(
|
|
119
|
+
url,
|
|
120
|
+
options.timeout ?? this.timeout
|
|
121
|
+
);
|
|
67
122
|
|
|
68
123
|
const data = response.data;
|
|
69
124
|
const content = data.content ?? '';
|