@librechat/agents 2.3.1 → 2.3.3
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/graphs/Graph.cjs +6 -6
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/llm.cjs +7 -7
- package/dist/cjs/llm/anthropic/llm.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +6 -6
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +24 -24
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/fake.cjs.map +1 -1
- package/dist/cjs/llm/text.cjs.map +1 -1
- package/dist/cjs/main.cjs +3 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +6 -6
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +11 -9
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/prune.cjs +155 -205
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +7 -7
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +1 -1
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +3 -3
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +6 -6
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/anthropic/llm.mjs +7 -7
- package/dist/esm/llm/anthropic/llm.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +6 -6
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +24 -24
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/fake.mjs.map +1 -1
- package/dist/esm/llm/text.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -1
- package/dist/esm/messages/core.mjs +6 -6
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +11 -9
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/prune.mjs +153 -206
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +7 -7
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +1 -1
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +3 -3
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/messages/format.d.ts +1 -2
- package/dist/types/messages/prune.d.ts +31 -2
- package/dist/types/types/stream.d.ts +2 -2
- package/dist/types/utils/tokens.d.ts +1 -1
- package/package.json +4 -3
- package/src/graphs/Graph.ts +8 -8
- package/src/llm/anthropic/llm.ts +7 -8
- package/src/llm/anthropic/types.ts +4 -4
- package/src/llm/anthropic/utils/message_inputs.ts +6 -6
- package/src/llm/anthropic/utils/message_outputs.ts +39 -39
- package/src/llm/fake.ts +2 -2
- package/src/llm/text.ts +1 -1
- package/src/messages/core.ts +9 -9
- package/src/messages/format.ts +43 -42
- package/src/messages/formatAgentMessages.test.ts +35 -35
- package/src/messages/formatAgentMessages.tools.test.ts +30 -30
- package/src/messages/prune.ts +182 -255
- package/src/messages/shiftIndexTokenCountMap.test.ts +18 -18
- package/src/mockStream.ts +1 -1
- package/src/run.ts +2 -2
- package/src/specs/prune.test.ts +89 -89
- package/src/specs/reasoning.test.ts +1 -1
- package/src/specs/thinking-prune.test.ts +265 -261
- package/src/specs/tool-error.test.ts +16 -17
- package/src/stream.ts +21 -18
- package/src/tools/ToolNode.ts +1 -1
- package/src/types/stream.ts +4 -3
- package/src/utils/tokens.ts +12 -12
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1
|
+
|
|
3
2
|
import { z } from 'zod';
|
|
4
3
|
import { config } from 'dotenv';
|
|
5
4
|
config();
|
|
@@ -71,7 +70,7 @@ describe('Tool Error Handling Tests', () => {
|
|
|
71
70
|
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
72
71
|
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
73
72
|
handle: (event: GraphEvents.ON_RUN_STEP_COMPLETED, data: t.StreamEventData): void => {
|
|
74
|
-
if ((data.result as t.MessageContentComplex)
|
|
73
|
+
if ((data.result as t.MessageContentComplex)['type'] === 'tool_call') {
|
|
75
74
|
run.Graph?.overrideTestModel(['Looks like there was an error calling the tool.'], 5);
|
|
76
75
|
}
|
|
77
76
|
onRunStepCompletedSpy(event, data);
|
|
@@ -118,16 +117,16 @@ describe('Tool Error Handling Tests', () => {
|
|
|
118
117
|
});
|
|
119
118
|
|
|
120
119
|
const toolCalls: ToolCall[] = [
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
120
|
+
{
|
|
121
|
+
name: 'errorTool',
|
|
122
|
+
args: {
|
|
123
|
+
input: 'test input',
|
|
124
|
+
},
|
|
125
|
+
id: 'call_test123',
|
|
126
|
+
type: 'tool_call',
|
|
127
|
+
}
|
|
128
|
+
];
|
|
129
|
+
|
|
131
130
|
const firstResponse = 'Let me try calling the tool';
|
|
132
131
|
run.Graph?.overrideTestModel([firstResponse], 5, toolCalls);
|
|
133
132
|
|
|
@@ -142,12 +141,12 @@ describe('Tool Error Handling Tests', () => {
|
|
|
142
141
|
|
|
143
142
|
// Verify handleToolCallError was called
|
|
144
143
|
expect(handleToolCallErrorSpy).toHaveBeenCalled();
|
|
145
|
-
|
|
144
|
+
|
|
146
145
|
// Find the tool call content part
|
|
147
|
-
const toolCallPart = contentParts.find(part =>
|
|
148
|
-
part
|
|
146
|
+
const toolCallPart = contentParts.find(part =>
|
|
147
|
+
part.type === 'tool_call'
|
|
149
148
|
) as t.ToolCallContent | undefined;
|
|
150
|
-
|
|
149
|
+
|
|
151
150
|
// Verify the error message in contentParts
|
|
152
151
|
expect(toolCallPart).toBeDefined();
|
|
153
152
|
expect(toolCallPart?.tool_call?.args).toEqual(JSON.stringify(toolCalls[0].args));
|
package/src/stream.ts
CHANGED
|
@@ -47,22 +47,21 @@ export const handleToolCalls = (toolCalls?: ToolCall[], metadata?: Record<string
|
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
51
50
|
const stepKey = graph.getStepKey(metadata);
|
|
52
|
-
|
|
51
|
+
|
|
53
52
|
for (const tool_call of toolCalls) {
|
|
54
53
|
const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;
|
|
55
54
|
tool_call.id = toolCallId;
|
|
56
55
|
if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
|
|
57
56
|
continue;
|
|
58
57
|
}
|
|
59
|
-
|
|
58
|
+
|
|
60
59
|
let prevStepId = '';
|
|
61
60
|
let prevRunStep: t.RunStep | undefined;
|
|
62
61
|
try {
|
|
63
62
|
prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
|
|
64
63
|
prevRunStep = graph.getRunStep(prevStepId);
|
|
65
|
-
} catch
|
|
64
|
+
} catch {
|
|
66
65
|
// no previous step
|
|
67
66
|
}
|
|
68
67
|
|
|
@@ -92,10 +91,10 @@ export const handleToolCalls = (toolCalls?: ToolCall[], metadata?: Record<string
|
|
|
92
91
|
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
93
92
|
}
|
|
94
93
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
graph.dispatchRunStep(stepKey, {
|
|
95
|
+
type: StepTypes.TOOL_CALLS,
|
|
96
|
+
tool_calls: [tool_call],
|
|
97
|
+
});
|
|
99
98
|
}
|
|
100
99
|
};
|
|
101
100
|
|
|
@@ -117,7 +116,7 @@ export class ChatModelStreamHandler implements t.EventHandler {
|
|
|
117
116
|
this.handleReasoning(chunk, graph);
|
|
118
117
|
|
|
119
118
|
let hasToolCalls = false;
|
|
120
|
-
if (chunk.tool_calls && chunk.tool_calls.length > 0 && chunk.tool_calls.every((tc) => tc.id)) {
|
|
119
|
+
if (chunk.tool_calls && chunk.tool_calls.length > 0 && chunk.tool_calls.every((tc) => tc.id != null && tc.id !== '')) {
|
|
121
120
|
hasToolCalls = true;
|
|
122
121
|
handleToolCalls(chunk.tool_calls, metadata, graph);
|
|
123
122
|
}
|
|
@@ -166,7 +165,7 @@ export class ChatModelStreamHandler implements t.EventHandler {
|
|
|
166
165
|
const stepId = graph.getStepIdByKey(stepKey);
|
|
167
166
|
const runStep = graph.getRunStep(stepId);
|
|
168
167
|
if (!runStep) {
|
|
169
|
-
|
|
168
|
+
|
|
170
169
|
console.warn(`\n
|
|
171
170
|
==============================================================
|
|
172
171
|
|
|
@@ -206,16 +205,20 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
206
205
|
}],
|
|
207
206
|
});
|
|
208
207
|
}
|
|
209
|
-
} else if (content.every((c) => c.type?.startsWith(ContentTypes.TEXT))) {
|
|
208
|
+
} else if (content.every((c) => c.type?.startsWith(ContentTypes.TEXT) ?? false)) {
|
|
210
209
|
graph.dispatchMessageDelta(stepId, {
|
|
211
210
|
content,
|
|
212
211
|
});
|
|
213
|
-
} else if (content.every(
|
|
212
|
+
} else if (content.every(
|
|
213
|
+
(c) =>
|
|
214
|
+
(c.type?.startsWith(ContentTypes.THINKING) ?? false) ||
|
|
215
|
+
(c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false)
|
|
216
|
+
)) {
|
|
214
217
|
graph.dispatchReasoningDelta(stepId, {
|
|
215
218
|
content: content.map((c) => ({
|
|
216
219
|
type: ContentTypes.THINK,
|
|
217
|
-
think: (c as t.ThinkingContentText).thinking ?? (c as t.BedrockReasoningContentText).reasoningText?.text ?? '',
|
|
218
|
-
|
|
220
|
+
think: (c as t.ThinkingContentText).thinking ?? (c as Partial<t.BedrockReasoningContentText>).reasoningText?.text ?? '',
|
|
221
|
+
}))});
|
|
219
222
|
}
|
|
220
223
|
}
|
|
221
224
|
handleToolCallChunks = ({
|
|
@@ -232,7 +235,7 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
232
235
|
try {
|
|
233
236
|
prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
|
|
234
237
|
prevRunStep = graph.getRunStep(prevStepId);
|
|
235
|
-
} catch
|
|
238
|
+
} catch {
|
|
236
239
|
/** Edge Case: If no previous step exists, create a new message creation step */
|
|
237
240
|
const message_id = getMessageId(stepKey, graph, true) ?? '';
|
|
238
241
|
prevStepId = graph.dispatchRunStep(stepKey, {
|
|
@@ -245,13 +248,13 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
245
248
|
}
|
|
246
249
|
|
|
247
250
|
const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);
|
|
248
|
-
|
|
251
|
+
|
|
249
252
|
/** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
|
|
250
253
|
const tool_calls: ToolCall[] | undefined =
|
|
251
254
|
prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION
|
|
252
255
|
? []
|
|
253
256
|
: undefined;
|
|
254
|
-
|
|
257
|
+
|
|
255
258
|
/** Edge Case: `id` and `name` fields cannot be empty strings */
|
|
256
259
|
for (const toolCallChunk of toolCallChunks) {
|
|
257
260
|
if (toolCallChunk.name === '') {
|
|
@@ -371,7 +374,7 @@ export function createContentAggregator(): t.ContentAggregatorResult {
|
|
|
371
374
|
} else if (
|
|
372
375
|
partType.startsWith(ContentTypes.AGENT_UPDATE) &&
|
|
373
376
|
ContentTypes.AGENT_UPDATE in contentPart &&
|
|
374
|
-
contentPart.agent_update
|
|
377
|
+
contentPart.agent_update != null
|
|
375
378
|
) {
|
|
376
379
|
const update: t.AgentUpdate = {
|
|
377
380
|
type: ContentTypes.AGENT_UPDATE,
|
package/src/tools/ToolNode.ts
CHANGED
|
@@ -91,7 +91,7 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
91
91
|
id: call.id!,
|
|
92
92
|
name: call.name,
|
|
93
93
|
input: call.args,
|
|
94
|
-
}, config
|
|
94
|
+
}, config.metadata);
|
|
95
95
|
return new ToolMessage({
|
|
96
96
|
content: `Error: ${e.message}\n Please fix your mistakes.`,
|
|
97
97
|
name: call.name,
|
package/src/types/stream.ts
CHANGED
|
@@ -56,12 +56,13 @@ export type RunStep = {
|
|
|
56
56
|
index: number; // #new
|
|
57
57
|
stepIndex?: number; // #new
|
|
58
58
|
stepDetails: StepDetails;
|
|
59
|
-
usage?: null |
|
|
59
|
+
usage?: null | object;
|
|
60
|
+
// {
|
|
60
61
|
// Define usage structure if it's ever non-null
|
|
61
62
|
// prompt_tokens: number; // #new
|
|
62
63
|
// completion_tokens: number; // #new
|
|
63
64
|
// total_tokens: number; // #new
|
|
64
|
-
};
|
|
65
|
+
// };
|
|
65
66
|
};
|
|
66
67
|
|
|
67
68
|
/**
|
|
@@ -285,7 +286,7 @@ export type MessageContentComplex = (ThinkingContentText | AgentUpdate | ToolCal
|
|
|
285
286
|
export interface TMessage {
|
|
286
287
|
role?: string;
|
|
287
288
|
content?: MessageContentComplex[] | string;
|
|
288
|
-
[key: string]:
|
|
289
|
+
[key: string]: unknown;
|
|
289
290
|
}
|
|
290
291
|
|
|
291
292
|
export type TPayload = Array<Partial<TMessage>>;
|
package/src/utils/tokens.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Tiktoken } from
|
|
2
|
-
import type { BaseMessage } from
|
|
3
|
-
import { ContentTypes } from
|
|
1
|
+
import { Tiktoken } from 'js-tiktoken/lite';
|
|
2
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
|
+
import { ContentTypes } from '@/common/enum';
|
|
4
4
|
|
|
5
5
|
export function getTokenCountForMessage(message: BaseMessage, getTokenCount: (text: string) => number): number {
|
|
6
|
-
|
|
6
|
+
const tokensPerMessage = 3;
|
|
7
7
|
|
|
8
|
-
const processValue = (value: unknown) => {
|
|
8
|
+
const processValue = (value: unknown): void => {
|
|
9
9
|
if (Array.isArray(value)) {
|
|
10
|
-
for (
|
|
10
|
+
for (const item of value) {
|
|
11
11
|
if (
|
|
12
12
|
!item ||
|
|
13
13
|
!item.type ||
|
|
@@ -58,13 +58,13 @@ export function getTokenCountForMessage(message: BaseMessage, getTokenCount: (te
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
export const createTokenCounter = async () => {
|
|
61
|
-
const res = await fetch(
|
|
61
|
+
const res = await fetch('https://tiktoken.pages.dev/js/o200k_base.json');
|
|
62
62
|
const o200k_base = await res.json();
|
|
63
63
|
|
|
64
|
-
const countTokens = (text: string) => {
|
|
64
|
+
const countTokens = (text: string): number => {
|
|
65
65
|
const enc = new Tiktoken(o200k_base);
|
|
66
66
|
return enc.encode(text).length;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return (message: BaseMessage) => getTokenCountForMessage(message, countTokens);
|
|
70
|
-
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return (message: BaseMessage): number => getTokenCountForMessage(message, countTokens);
|
|
70
|
+
};
|