@librechat/agents 3.1.78 → 3.1.79
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/llm/anthropic/index.cjs +44 -55
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +33 -21
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +0 -4
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/messages/anthropicToolCache.cjs +48 -15
- package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +97 -14
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +14 -16
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +43 -54
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +33 -21
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +0 -4
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/messages/anthropicToolCache.mjs +48 -15
- package/dist/esm/messages/anthropicToolCache.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +97 -14
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +14 -16
- package/dist/esm/tools/local/LocalExecutionEngine.mjs.map +1 -1
- package/dist/types/llm/anthropic/index.d.ts +1 -9
- package/dist/types/messages/anthropicToolCache.d.ts +5 -5
- package/package.json +1 -1
- package/src/llm/anthropic/index.ts +55 -64
- package/src/llm/anthropic/llm.spec.ts +585 -0
- package/src/llm/anthropic/utils/message_inputs.ts +36 -21
- package/src/llm/anthropic/utils/message_outputs.ts +0 -4
- package/src/llm/anthropic/utils/server-tool-inputs.test.ts +95 -13
- package/src/messages/__tests__/anthropicToolCache.test.ts +46 -0
- package/src/messages/anthropicToolCache.ts +70 -25
- package/src/messages/format.ts +117 -18
- package/src/messages/formatAgentMessages.test.ts +202 -1
- package/src/specs/summarization.test.ts +3 -3
- package/src/tools/__tests__/LocalExecutionRoots.test.ts +8 -0
- package/src/tools/local/LocalExecutionEngine.ts +55 -54
- package/src/types/diff.d.ts +15 -0
|
@@ -4,7 +4,6 @@ import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
|
4
4
|
import type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';
|
|
5
5
|
import type {
|
|
6
6
|
BaseMessage,
|
|
7
|
-
UsageMetadata,
|
|
8
7
|
MessageContentComplex,
|
|
9
8
|
} from '@langchain/core/messages';
|
|
10
9
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
@@ -13,18 +12,13 @@ import type { Anthropic } from '@anthropic-ai/sdk';
|
|
|
13
12
|
import type {
|
|
14
13
|
AnthropicMessageCreateParams,
|
|
15
14
|
AnthropicStreamingMessageCreateParams,
|
|
16
|
-
AnthropicMessageStartEvent,
|
|
17
|
-
AnthropicMessageDeltaEvent,
|
|
18
15
|
AnthropicOutputConfig,
|
|
19
16
|
AnthropicBeta,
|
|
20
17
|
ChatAnthropicToolType,
|
|
21
18
|
AnthropicMCPServerURLDefinition,
|
|
22
19
|
AnthropicContextManagementConfigParam,
|
|
23
20
|
} from '@/llm/anthropic/types';
|
|
24
|
-
import {
|
|
25
|
-
_makeMessageChunkFromAnthropicEvent,
|
|
26
|
-
getAnthropicUsageMetadata,
|
|
27
|
-
} from './utils/message_outputs';
|
|
21
|
+
import { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';
|
|
28
22
|
import { _convertMessagesToAnthropicPayload } from './utils/message_inputs';
|
|
29
23
|
import { handleToolChoice } from './utils/tools';
|
|
30
24
|
import { TextStream } from '@/llm/text';
|
|
@@ -53,13 +47,17 @@ export function _documentsInParams(
|
|
|
53
47
|
continue;
|
|
54
48
|
}
|
|
55
49
|
for (const block of message.content) {
|
|
50
|
+
const maybeBlock: unknown = block;
|
|
56
51
|
if (
|
|
57
|
-
typeof
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
typeof maybeBlock === 'object' &&
|
|
53
|
+
maybeBlock !== null &&
|
|
54
|
+
'type' in maybeBlock &&
|
|
55
|
+
maybeBlock.type === 'document' &&
|
|
56
|
+
'citations' in maybeBlock &&
|
|
57
|
+
maybeBlock.citations != null &&
|
|
58
|
+
typeof maybeBlock.citations === 'object' &&
|
|
59
|
+
'enabled' in maybeBlock.citations &&
|
|
60
|
+
maybeBlock.citations.enabled === true
|
|
63
61
|
) {
|
|
64
62
|
return true;
|
|
65
63
|
}
|
|
@@ -307,6 +305,30 @@ function cloneChunk(
|
|
|
307
305
|
return chunk;
|
|
308
306
|
}
|
|
309
307
|
|
|
308
|
+
function withIncrementalMessageDeltaUsage(
|
|
309
|
+
chunk: AIMessageChunk,
|
|
310
|
+
previousOutputTokens: number
|
|
311
|
+
): { chunk: AIMessageChunk; outputTokens: number } {
|
|
312
|
+
const usage = chunk.usage_metadata;
|
|
313
|
+
if (usage == null) {
|
|
314
|
+
return { chunk, outputTokens: previousOutputTokens };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const outputTokens = Math.max(0, usage.output_tokens - previousOutputTokens);
|
|
318
|
+
return {
|
|
319
|
+
chunk: new AIMessageChunk(
|
|
320
|
+
Object.assign({}, chunk, {
|
|
321
|
+
usage_metadata: {
|
|
322
|
+
...usage,
|
|
323
|
+
output_tokens: outputTokens,
|
|
324
|
+
total_tokens: usage.input_tokens + outputTokens,
|
|
325
|
+
},
|
|
326
|
+
})
|
|
327
|
+
),
|
|
328
|
+
outputTokens: usage.output_tokens,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
310
332
|
export type CustomAnthropicInput = AnthropicInput & {
|
|
311
333
|
_lc_stream_delay?: number;
|
|
312
334
|
outputConfig?: AnthropicOutputConfig;
|
|
@@ -334,10 +356,7 @@ type CustomAnthropicInvocationParams = {
|
|
|
334
356
|
|
|
335
357
|
export class CustomAnthropic extends ChatAnthropicMessages {
|
|
336
358
|
_lc_stream_delay: number;
|
|
337
|
-
private message_start: AnthropicMessageStartEvent | undefined;
|
|
338
|
-
private message_delta: AnthropicMessageDeltaEvent | undefined;
|
|
339
359
|
private tools_in_params?: boolean;
|
|
340
|
-
private emitted_usage?: boolean;
|
|
341
360
|
top_k: number | undefined;
|
|
342
361
|
outputConfig?: AnthropicOutputConfig;
|
|
343
362
|
inferenceGeo?: string;
|
|
@@ -436,33 +455,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
436
455
|
};
|
|
437
456
|
}
|
|
438
457
|
|
|
439
|
-
/**
|
|
440
|
-
* Get stream usage as returned by this client's API response.
|
|
441
|
-
* @returns The stream usage object.
|
|
442
|
-
*/
|
|
443
|
-
getStreamUsage(): UsageMetadata | undefined {
|
|
444
|
-
if (this.emitted_usage === true) {
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
const inputUsage = this.message_start?.message.usage;
|
|
448
|
-
const outputUsage = this.message_delta?.usage;
|
|
449
|
-
if (!outputUsage) {
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
this.emitted_usage = true;
|
|
454
|
-
return getAnthropicUsageMetadata({
|
|
455
|
-
input_tokens: inputUsage?.input_tokens,
|
|
456
|
-
output_tokens: outputUsage.output_tokens,
|
|
457
|
-
cache_creation_input_tokens: inputUsage?.cache_creation_input_tokens,
|
|
458
|
-
cache_read_input_tokens: inputUsage?.cache_read_input_tokens,
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
|
|
462
458
|
resetTokenEvents(): void {
|
|
463
|
-
this.message_start = undefined;
|
|
464
|
-
this.message_delta = undefined;
|
|
465
|
-
this.emitted_usage = undefined;
|
|
466
459
|
this.tools_in_params = undefined;
|
|
467
460
|
}
|
|
468
461
|
|
|
@@ -484,17 +477,13 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
484
477
|
private createGenerationChunk({
|
|
485
478
|
token,
|
|
486
479
|
chunk,
|
|
487
|
-
usageMetadata,
|
|
488
480
|
shouldStreamUsage,
|
|
489
481
|
}: {
|
|
490
482
|
token?: string;
|
|
491
483
|
chunk: AIMessageChunk;
|
|
492
484
|
shouldStreamUsage: boolean;
|
|
493
|
-
usageMetadata?: UsageMetadata;
|
|
494
485
|
}): ChatGenerationChunk {
|
|
495
|
-
const usage_metadata = shouldStreamUsage
|
|
496
|
-
? (usageMetadata ?? chunk.usage_metadata)
|
|
497
|
-
: undefined;
|
|
486
|
+
const usage_metadata = shouldStreamUsage ? chunk.usage_metadata : undefined;
|
|
498
487
|
return new ChatGenerationChunk({
|
|
499
488
|
message: new AIMessageChunk({
|
|
500
489
|
// Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
|
|
@@ -534,6 +523,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
534
523
|
});
|
|
535
524
|
|
|
536
525
|
const shouldStreamUsage = options.streamUsage ?? this.streamUsage;
|
|
526
|
+
let messageDeltaOutputTokens = 0;
|
|
537
527
|
|
|
538
528
|
for await (const data of stream) {
|
|
539
529
|
if (options.signal?.aborted === true) {
|
|
@@ -541,17 +531,6 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
541
531
|
throw new Error('AbortError: User aborted the request.');
|
|
542
532
|
}
|
|
543
533
|
|
|
544
|
-
if (data.type === 'message_start') {
|
|
545
|
-
this.message_start = data as AnthropicMessageStartEvent;
|
|
546
|
-
} else if (data.type === 'message_delta') {
|
|
547
|
-
this.message_delta = data as AnthropicMessageDeltaEvent;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
let usageMetadata: UsageMetadata | undefined;
|
|
551
|
-
if (this.tools_in_params !== true && this.emitted_usage !== true) {
|
|
552
|
-
usageMetadata = this.getStreamUsage();
|
|
553
|
-
}
|
|
554
|
-
|
|
555
534
|
const result = _makeMessageChunkFromAnthropicEvent(
|
|
556
535
|
data as Anthropic.Beta.Messages.BetaRawMessageStreamEvent,
|
|
557
536
|
{
|
|
@@ -561,18 +540,25 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
561
540
|
);
|
|
562
541
|
if (!result) continue;
|
|
563
542
|
|
|
564
|
-
|
|
543
|
+
let { chunk } = result;
|
|
544
|
+
if (data.type === 'message_delta') {
|
|
545
|
+
const incremental = withIncrementalMessageDeltaUsage(
|
|
546
|
+
chunk,
|
|
547
|
+
messageDeltaOutputTokens
|
|
548
|
+
);
|
|
549
|
+
chunk = incremental.chunk;
|
|
550
|
+
messageDeltaOutputTokens = incremental.outputTokens;
|
|
551
|
+
}
|
|
565
552
|
const [token = '', tokenType] = extractToken(chunk);
|
|
566
553
|
|
|
567
554
|
if (
|
|
568
555
|
!tokenType ||
|
|
569
556
|
tokenType === 'input' ||
|
|
570
|
-
(token === '' && (
|
|
557
|
+
(token === '' && (chunk.usage_metadata != null || chunk.id != null))
|
|
571
558
|
) {
|
|
572
559
|
const generationChunk = this.createGenerationChunk({
|
|
573
560
|
token,
|
|
574
561
|
chunk,
|
|
575
|
-
usageMetadata,
|
|
576
562
|
shouldStreamUsage,
|
|
577
563
|
});
|
|
578
564
|
yield generationChunk;
|
|
@@ -602,15 +588,20 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
602
588
|
break;
|
|
603
589
|
}
|
|
604
590
|
const newChunk = cloneChunk(currentToken, tokenType, chunk);
|
|
591
|
+
const chunkForToken =
|
|
592
|
+
emittedUsage && newChunk.usage_metadata != null
|
|
593
|
+
? new AIMessageChunk(
|
|
594
|
+
Object.assign({}, newChunk, { usage_metadata: undefined })
|
|
595
|
+
)
|
|
596
|
+
: newChunk;
|
|
605
597
|
|
|
606
598
|
const generationChunk = this.createGenerationChunk({
|
|
607
599
|
token: currentToken,
|
|
608
|
-
chunk:
|
|
609
|
-
usageMetadata: emittedUsage ? undefined : usageMetadata,
|
|
600
|
+
chunk: chunkForToken,
|
|
610
601
|
shouldStreamUsage,
|
|
611
602
|
});
|
|
612
603
|
|
|
613
|
-
if (
|
|
604
|
+
if (newChunk.usage_metadata != null && !emittedUsage) {
|
|
614
605
|
emittedUsage = true;
|
|
615
606
|
}
|
|
616
607
|
yield generationChunk;
|