@librechat/agents 3.1.32 → 3.1.34
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 +14 -5
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +35 -8
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +3 -4
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/index.cjs +43 -11
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +10 -7
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +32 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +34 -13
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +14 -5
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +35 -8
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +3 -4
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +43 -11
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +10 -7
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +32 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +34 -13
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +2 -0
- package/dist/types/llm/anthropic/index.d.ts +7 -1
- package/dist/types/llm/anthropic/types.d.ts +5 -2
- package/dist/types/llm/anthropic/utils/message_outputs.d.ts +1 -1
- package/dist/types/tools/ToolNode.d.ts +8 -3
- package/dist/types/types/tools.d.ts +2 -0
- package/package.json +4 -1
- package/src/agents/AgentContext.ts +18 -5
- package/src/graphs/Graph.ts +45 -9
- package/src/graphs/MultiAgentGraph.ts +3 -4
- package/src/llm/anthropic/index.ts +68 -15
- package/src/llm/anthropic/llm.spec.ts +402 -0
- package/src/llm/anthropic/types.ts +8 -2
- package/src/llm/anthropic/utils/message_inputs.ts +16 -33
- package/src/llm/anthropic/utils/message_outputs.ts +40 -1
- package/src/scripts/test-thinking-to-thinking-handoff-bedrock.ts +165 -0
- package/src/specs/agent-handoffs.test.ts +16 -18
- package/src/specs/thinking-handoff.test.ts +2 -2
- package/src/tools/ToolNode.ts +52 -18
- package/src/types/tools.ts +2 -0
- package/src/utils/llmConfig.ts +2 -1
|
@@ -4,9 +4,12 @@ import type { BaseChatModelParams } from '@langchain/core/language_models/chat_m
|
|
|
4
4
|
import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
5
5
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
6
6
|
import type { AnthropicInput } from '@langchain/anthropic';
|
|
7
|
-
import type { AnthropicMessageCreateParams, AnthropicStreamingMessageCreateParams } from '@/llm/anthropic/types';
|
|
7
|
+
import type { AnthropicMessageCreateParams, AnthropicStreamingMessageCreateParams, AnthropicOutputConfig } from '@/llm/anthropic/types';
|
|
8
8
|
export type CustomAnthropicInput = AnthropicInput & {
|
|
9
9
|
_lc_stream_delay?: number;
|
|
10
|
+
outputConfig?: AnthropicOutputConfig;
|
|
11
|
+
inferenceGeo?: string;
|
|
12
|
+
contextManagement?: any;
|
|
10
13
|
} & BaseChatModelParams;
|
|
11
14
|
/**
|
|
12
15
|
* A type representing additional parameters that can be passed to the
|
|
@@ -20,6 +23,9 @@ export declare class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
20
23
|
private tools_in_params?;
|
|
21
24
|
private emitted_usage?;
|
|
22
25
|
top_k: number | undefined;
|
|
26
|
+
outputConfig?: AnthropicOutputConfig;
|
|
27
|
+
inferenceGeo?: string;
|
|
28
|
+
contextManagement?: any;
|
|
23
29
|
constructor(fields?: CustomAnthropicInput);
|
|
24
30
|
static lc_name(): 'LibreChatAnthropic';
|
|
25
31
|
/**
|
|
@@ -32,6 +32,9 @@ export type AnthropicServerToolUseBlockParam = Anthropic.Messages.ServerToolUseB
|
|
|
32
32
|
export type AnthropicWebSearchToolResultBlockParam = Anthropic.Messages.WebSearchToolResultBlockParam;
|
|
33
33
|
export type AnthropicWebSearchResultBlockParam = Anthropic.Messages.WebSearchResultBlockParam;
|
|
34
34
|
export type AnthropicSearchResultBlockParam = Anthropic.Beta.BetaSearchResultBlockParam;
|
|
35
|
-
export type
|
|
36
|
-
export type
|
|
35
|
+
export type AnthropicCompactionBlockParam = Anthropic.Beta.BetaCompactionBlockParam;
|
|
36
|
+
export type AnthropicOutputConfig = Anthropic.Messages.OutputConfig;
|
|
37
|
+
export type ChatAnthropicOutputFormat = Anthropic.Messages.JSONOutputFormat;
|
|
38
|
+
export type AnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam | AnthropicCompactionBlockParam;
|
|
39
|
+
export type ChatAnthropicContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam | AnthropicDocumentBlockParam | AnthropicThinkingBlockParam | AnthropicRedactedThinkingBlockParam | AnthropicServerToolUseBlockParam | AnthropicWebSearchToolResultBlockParam | AnthropicWebSearchResultBlockParam | AnthropicSearchResultBlockParam | AnthropicCompactionBlockParam;
|
|
37
40
|
export declare function isAnthropicImageBlockParam(block: unknown): block is AnthropicImageBlockParam;
|
|
@@ -5,7 +5,7 @@ import Anthropic from '@anthropic-ai/sdk';
|
|
|
5
5
|
import { AIMessageChunk } from '@langchain/core/messages';
|
|
6
6
|
import { ChatGeneration } from '@langchain/core/outputs';
|
|
7
7
|
import { AnthropicMessageResponse } from '../types';
|
|
8
|
-
export declare function _makeMessageChunkFromAnthropicEvent(data: Anthropic.Messages.
|
|
8
|
+
export declare function _makeMessageChunkFromAnthropicEvent(data: Anthropic.Beta.Messages.BetaRawMessageStreamEvent, fields: {
|
|
9
9
|
streamUsage: boolean;
|
|
10
10
|
coerceContentToString: boolean;
|
|
11
11
|
}): {
|
|
@@ -20,11 +20,11 @@ export declare class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
20
20
|
private sessions?;
|
|
21
21
|
/** When true, dispatches ON_TOOL_EXECUTE events instead of invoking tools directly */
|
|
22
22
|
private eventDrivenMode;
|
|
23
|
-
/** Tool definitions for event-driven mode */
|
|
24
|
-
private toolDefinitions?;
|
|
25
23
|
/** Agent ID for event-driven mode */
|
|
26
24
|
private agentId?;
|
|
27
|
-
|
|
25
|
+
/** Tool names that bypass event dispatch and execute directly (e.g., graph-managed handoff tools) */
|
|
26
|
+
private directToolNames?;
|
|
27
|
+
constructor({ tools, toolMap, name, tags, errorHandler, toolCallStepIds, handleToolErrors, loadRuntimeTools, toolRegistry, sessions, eventDrivenMode, agentId, directToolNames, }: t.ToolNodeConstructorParams);
|
|
28
28
|
/**
|
|
29
29
|
* Returns cached programmatic tools, computing once on first access.
|
|
30
30
|
* Single iteration builds both toolMap and toolDefs simultaneously.
|
|
@@ -39,6 +39,11 @@ export declare class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
39
39
|
* Runs a single tool call with error handling
|
|
40
40
|
*/
|
|
41
41
|
protected runTool(call: ToolCall, config: RunnableConfig): Promise<BaseMessage | Command>;
|
|
42
|
+
/**
|
|
43
|
+
* Dispatches tool calls to the host via ON_TOOL_EXECUTE event and returns raw ToolMessages.
|
|
44
|
+
* Core logic for event-driven execution, separated from output shaping.
|
|
45
|
+
*/
|
|
46
|
+
private dispatchToolEvents;
|
|
42
47
|
/**
|
|
43
48
|
* Execute all tool calls via ON_TOOL_EXECUTE event dispatch.
|
|
44
49
|
* Used in event-driven mode where the host handles actual tool execution.
|
|
@@ -37,6 +37,8 @@ export type ToolNodeOptions = {
|
|
|
37
37
|
toolDefinitions?: Map<string, LCTool>;
|
|
38
38
|
/** Agent ID for event-driven mode (used to identify which agent's context to use) */
|
|
39
39
|
agentId?: string;
|
|
40
|
+
/** Tool names that must be executed directly (via runTool) even in event-driven mode (e.g., graph-managed handoff tools) */
|
|
41
|
+
directToolNames?: Set<string>;
|
|
40
42
|
};
|
|
41
43
|
export type ToolNodeConstructorParams = ToolRefs & ToolNodeOptions;
|
|
42
44
|
export type ToolEndEvent = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.34",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -83,6 +83,7 @@
|
|
|
83
83
|
"test-parallel-agent-labeling": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-parallel-agent-labeling.ts",
|
|
84
84
|
"test-thinking-handoff": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-thinking-handoff.ts",
|
|
85
85
|
"test-thinking-handoff-bedrock": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-thinking-handoff-bedrock.ts",
|
|
86
|
+
"test-thinking-to-thinking-handoff-bedrock": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-thinking-to-thinking-handoff-bedrock.ts",
|
|
86
87
|
"multi-agent-hybrid-flow": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-hybrid-flow.ts",
|
|
87
88
|
"test-handoff-input": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-handoff-input.ts",
|
|
88
89
|
"test-custom-prompt-key": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-custom-prompt-key.ts",
|
|
@@ -107,12 +108,14 @@
|
|
|
107
108
|
},
|
|
108
109
|
"overrides": {
|
|
109
110
|
"@langchain/openai": "0.5.18",
|
|
111
|
+
"@anthropic-ai/sdk": "$@anthropic-ai/sdk",
|
|
110
112
|
"@browserbasehq/stagehand": {
|
|
111
113
|
"openai": "$openai"
|
|
112
114
|
},
|
|
113
115
|
"fast-xml-parser": "5.3.4"
|
|
114
116
|
},
|
|
115
117
|
"dependencies": {
|
|
118
|
+
"@anthropic-ai/sdk": "^0.73.0",
|
|
116
119
|
"@aws-sdk/client-bedrock-runtime": "^3.980.0",
|
|
117
120
|
"@langchain/anthropic": "^0.3.26",
|
|
118
121
|
"@langchain/aws": "^0.1.15",
|
|
@@ -116,6 +116,8 @@ export class AgentContext {
|
|
|
116
116
|
lastStreamCall?: number;
|
|
117
117
|
/** Tools available to this agent */
|
|
118
118
|
tools?: t.GraphTools;
|
|
119
|
+
/** Graph-managed tools (e.g., handoff tools created by MultiAgentGraph) that bypass event-driven dispatch */
|
|
120
|
+
graphTools?: t.GraphTools;
|
|
119
121
|
/** Tool map for this agent */
|
|
120
122
|
toolMap?: t.ToolMap;
|
|
121
123
|
/**
|
|
@@ -595,17 +597,22 @@ export class AgentContext {
|
|
|
595
597
|
}
|
|
596
598
|
|
|
597
599
|
/** Traditional mode: filter actual tool instances */
|
|
598
|
-
|
|
599
|
-
|
|
600
|
+
const filtered =
|
|
601
|
+
!this.tools || !this.toolRegistry
|
|
602
|
+
? this.tools
|
|
603
|
+
: this.filterToolsForBinding(this.tools);
|
|
604
|
+
|
|
605
|
+
if (this.graphTools && this.graphTools.length > 0) {
|
|
606
|
+
return [...(filtered ?? []), ...this.graphTools];
|
|
600
607
|
}
|
|
601
608
|
|
|
602
|
-
return
|
|
609
|
+
return filtered;
|
|
603
610
|
}
|
|
604
611
|
|
|
605
612
|
/** Creates schema-only tools from toolDefinitions for event-driven mode */
|
|
606
613
|
private getEventDrivenToolsForBinding(): t.GraphTools {
|
|
607
614
|
if (!this.toolDefinitions) {
|
|
608
|
-
return [];
|
|
615
|
+
return this.graphTools ?? [];
|
|
609
616
|
}
|
|
610
617
|
|
|
611
618
|
const defsToInclude = this.toolDefinitions.filter((def) => {
|
|
@@ -622,7 +629,13 @@ export class AgentContext {
|
|
|
622
629
|
return true;
|
|
623
630
|
});
|
|
624
631
|
|
|
625
|
-
|
|
632
|
+
const schemaTools = createSchemaOnlyTools(defsToInclude) as t.GraphTools;
|
|
633
|
+
|
|
634
|
+
if (this.graphTools && this.graphTools.length > 0) {
|
|
635
|
+
return [...schemaTools, ...this.graphTools];
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return schemaTools;
|
|
626
639
|
}
|
|
627
640
|
|
|
628
641
|
/** Filters tool instances for binding based on registry config */
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -461,24 +461,60 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
461
461
|
if (eventDrivenMode) {
|
|
462
462
|
const schemaTools = createSchemaOnlyTools(toolDefinitions);
|
|
463
463
|
const toolDefMap = new Map(toolDefinitions.map((def) => [def.name, def]));
|
|
464
|
+
const graphTools = agentContext?.graphTools as
|
|
465
|
+
| t.GenericTool[]
|
|
466
|
+
| undefined;
|
|
467
|
+
|
|
468
|
+
const directToolNames = new Set<string>();
|
|
469
|
+
const allTools = [...schemaTools] as t.GenericTool[];
|
|
470
|
+
const allToolMap: t.ToolMap = new Map(
|
|
471
|
+
schemaTools.map((tool) => [tool.name, tool])
|
|
472
|
+
);
|
|
473
|
+
|
|
474
|
+
if (graphTools && graphTools.length > 0) {
|
|
475
|
+
for (const tool of graphTools) {
|
|
476
|
+
if ('name' in tool) {
|
|
477
|
+
allTools.push(tool);
|
|
478
|
+
allToolMap.set(tool.name, tool);
|
|
479
|
+
directToolNames.add(tool.name);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
464
483
|
|
|
465
484
|
return new CustomToolNode<t.BaseGraphState>({
|
|
466
|
-
tools:
|
|
467
|
-
toolMap:
|
|
468
|
-
toolCallStepIds: this.toolCallStepIds,
|
|
469
|
-
errorHandler: (data, metadata) =>
|
|
470
|
-
StandardGraph.handleToolCallErrorStatic(this, data, metadata),
|
|
471
|
-
toolRegistry: agentContext?.toolRegistry,
|
|
472
|
-
sessions: this.sessions,
|
|
485
|
+
tools: allTools,
|
|
486
|
+
toolMap: allToolMap,
|
|
473
487
|
eventDrivenMode: true,
|
|
488
|
+
sessions: this.sessions,
|
|
474
489
|
toolDefinitions: toolDefMap,
|
|
475
490
|
agentId: agentContext?.agentId,
|
|
491
|
+
toolCallStepIds: this.toolCallStepIds,
|
|
492
|
+
toolRegistry: agentContext?.toolRegistry,
|
|
493
|
+
directToolNames: directToolNames.size > 0 ? directToolNames : undefined,
|
|
494
|
+
errorHandler: (data, metadata) =>
|
|
495
|
+
StandardGraph.handleToolCallErrorStatic(this, data, metadata),
|
|
476
496
|
});
|
|
477
497
|
}
|
|
478
498
|
|
|
499
|
+
const graphTools = agentContext?.graphTools as t.GenericTool[] | undefined;
|
|
500
|
+
const baseTools = (currentTools as t.GenericTool[] | undefined) ?? [];
|
|
501
|
+
const allTraditionalTools =
|
|
502
|
+
graphTools && graphTools.length > 0
|
|
503
|
+
? [...baseTools, ...graphTools]
|
|
504
|
+
: baseTools;
|
|
505
|
+
const traditionalToolMap =
|
|
506
|
+
graphTools && graphTools.length > 0
|
|
507
|
+
? new Map([
|
|
508
|
+
...(currentToolMap ?? new Map()),
|
|
509
|
+
...graphTools
|
|
510
|
+
.filter((t): t is t.GenericTool & { name: string } => 'name' in t)
|
|
511
|
+
.map((t) => [t.name, t] as [string, t.GenericTool]),
|
|
512
|
+
])
|
|
513
|
+
: currentToolMap;
|
|
514
|
+
|
|
479
515
|
return new CustomToolNode<t.BaseGraphState>({
|
|
480
|
-
tools:
|
|
481
|
-
toolMap:
|
|
516
|
+
tools: allTraditionalTools,
|
|
517
|
+
toolMap: traditionalToolMap,
|
|
482
518
|
toolCallStepIds: this.toolCallStepIds,
|
|
483
519
|
errorHandler: (data, metadata) =>
|
|
484
520
|
StandardGraph.handleToolCallErrorStatic(this, data, metadata),
|
|
@@ -255,11 +255,10 @@ export class MultiAgentGraph extends StandardGraph {
|
|
|
255
255
|
);
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
agentContext.tools = [];
|
|
258
|
+
if (!agentContext.graphTools) {
|
|
259
|
+
agentContext.graphTools = [];
|
|
261
260
|
}
|
|
262
|
-
agentContext.
|
|
261
|
+
agentContext.graphTools.push(...handoffTools);
|
|
263
262
|
}
|
|
264
263
|
}
|
|
265
264
|
|
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
AnthropicStreamUsage,
|
|
17
17
|
AnthropicMessageStartEvent,
|
|
18
18
|
AnthropicMessageDeltaEvent,
|
|
19
|
+
AnthropicOutputConfig,
|
|
19
20
|
} from '@/llm/anthropic/types';
|
|
20
21
|
import { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';
|
|
21
22
|
import { _convertMessagesToAnthropicPayload } from './utils/message_inputs';
|
|
@@ -39,6 +40,7 @@ function _documentsInParams(
|
|
|
39
40
|
typeof block === 'object' &&
|
|
40
41
|
block != null &&
|
|
41
42
|
block.type === 'document' &&
|
|
43
|
+
block.citations != null &&
|
|
42
44
|
typeof block.citations === 'object' &&
|
|
43
45
|
block.citations.enabled
|
|
44
46
|
) {
|
|
@@ -52,7 +54,20 @@ function _documentsInParams(
|
|
|
52
54
|
function _thinkingInParams(
|
|
53
55
|
params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams
|
|
54
56
|
): boolean {
|
|
55
|
-
return !!(
|
|
57
|
+
return !!(
|
|
58
|
+
params.thinking &&
|
|
59
|
+
(params.thinking.type === 'enabled' || params.thinking.type === 'adaptive')
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function _compactionInParams(
|
|
64
|
+
params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams
|
|
65
|
+
): boolean {
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
67
|
+
const cm = (params as any).context_management;
|
|
68
|
+
return !!cm?.edits?.some(
|
|
69
|
+
(e: { type: string }) => e.type === 'compact_20260112'
|
|
70
|
+
);
|
|
56
71
|
}
|
|
57
72
|
|
|
58
73
|
function extractToken(
|
|
@@ -120,6 +135,10 @@ function cloneChunk(
|
|
|
120
135
|
|
|
121
136
|
export type CustomAnthropicInput = AnthropicInput & {
|
|
122
137
|
_lc_stream_delay?: number;
|
|
138
|
+
outputConfig?: AnthropicOutputConfig;
|
|
139
|
+
inferenceGeo?: string;
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
141
|
+
contextManagement?: any;
|
|
123
142
|
} & BaseChatModelParams;
|
|
124
143
|
|
|
125
144
|
/**
|
|
@@ -136,11 +155,18 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
136
155
|
private tools_in_params?: boolean;
|
|
137
156
|
private emitted_usage?: boolean;
|
|
138
157
|
top_k: number | undefined;
|
|
158
|
+
outputConfig?: AnthropicOutputConfig;
|
|
159
|
+
inferenceGeo?: string;
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
+
contextManagement?: any;
|
|
139
162
|
constructor(fields?: CustomAnthropicInput) {
|
|
140
163
|
super(fields);
|
|
141
164
|
this.resetTokenEvents();
|
|
142
165
|
this.setDirectFields(fields);
|
|
143
166
|
this._lc_stream_delay = fields?._lc_stream_delay ?? 25;
|
|
167
|
+
this.outputConfig = fields?.outputConfig;
|
|
168
|
+
this.inferenceGeo = fields?.inferenceGeo;
|
|
169
|
+
this.contextManagement = fields?.contextManagement;
|
|
144
170
|
}
|
|
145
171
|
|
|
146
172
|
static lc_name(): 'LibreChatAnthropic' {
|
|
@@ -163,7 +189,36 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
163
189
|
| Anthropic.Messages.ToolChoiceTool
|
|
164
190
|
| undefined = handleToolChoice(options?.tool_choice);
|
|
165
191
|
|
|
166
|
-
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
193
|
+
const callOptions = options as Record<string, any> | undefined;
|
|
194
|
+
const mergedOutputConfig: AnthropicOutputConfig | undefined = (():
|
|
195
|
+
| AnthropicOutputConfig
|
|
196
|
+
| undefined => {
|
|
197
|
+
const base = {
|
|
198
|
+
...this.outputConfig,
|
|
199
|
+
...callOptions?.outputConfig,
|
|
200
|
+
};
|
|
201
|
+
if (callOptions?.outputFormat && !base.format) {
|
|
202
|
+
base.format = callOptions.outputFormat;
|
|
203
|
+
}
|
|
204
|
+
return Object.keys(base).length > 0 ? base : undefined;
|
|
205
|
+
})();
|
|
206
|
+
|
|
207
|
+
const inferenceGeo = callOptions?.inferenceGeo ?? this.inferenceGeo;
|
|
208
|
+
|
|
209
|
+
const contextManagement = this.contextManagement;
|
|
210
|
+
|
|
211
|
+
const sharedParams = {
|
|
212
|
+
tools: this.formatStructuredToolToAnthropic(options?.tools),
|
|
213
|
+
tool_choice,
|
|
214
|
+
thinking: this.thinking,
|
|
215
|
+
...(mergedOutputConfig ? { output_config: mergedOutputConfig } : {}),
|
|
216
|
+
...(inferenceGeo ? { inference_geo: inferenceGeo } : {}),
|
|
217
|
+
...(contextManagement ? { context_management: contextManagement } : {}),
|
|
218
|
+
...this.invocationKwargs,
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
if (this.thinking.type === 'enabled' || this.thinking.type === 'adaptive') {
|
|
167
222
|
if (this.top_k !== -1 && (this.top_k as number | undefined) != null) {
|
|
168
223
|
throw new Error('topK is not supported when thinking is enabled');
|
|
169
224
|
}
|
|
@@ -184,10 +239,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
184
239
|
stop_sequences: options?.stop ?? this.stopSequences,
|
|
185
240
|
stream: this.streaming,
|
|
186
241
|
max_tokens: this.maxTokens,
|
|
187
|
-
|
|
188
|
-
tool_choice,
|
|
189
|
-
thinking: this.thinking,
|
|
190
|
-
...this.invocationKwargs,
|
|
242
|
+
...sharedParams,
|
|
191
243
|
};
|
|
192
244
|
}
|
|
193
245
|
return {
|
|
@@ -198,10 +250,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
198
250
|
stop_sequences: options?.stop ?? this.stopSequences,
|
|
199
251
|
stream: this.streaming,
|
|
200
252
|
max_tokens: this.maxTokens,
|
|
201
|
-
|
|
202
|
-
tool_choice,
|
|
203
|
-
thinking: this.thinking,
|
|
204
|
-
...this.invocationKwargs,
|
|
253
|
+
...sharedParams,
|
|
205
254
|
};
|
|
206
255
|
}
|
|
207
256
|
|
|
@@ -309,7 +358,8 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
309
358
|
const coerceContentToString =
|
|
310
359
|
!_toolsInParams(payload) &&
|
|
311
360
|
!_documentsInParams(payload) &&
|
|
312
|
-
!_thinkingInParams(payload)
|
|
361
|
+
!_thinkingInParams(payload) &&
|
|
362
|
+
!_compactionInParams(payload);
|
|
313
363
|
|
|
314
364
|
const stream = await this.createStreamWithRetry(payload, {
|
|
315
365
|
headers: options.headers,
|
|
@@ -334,10 +384,13 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
334
384
|
usageMetadata = this.getStreamUsage();
|
|
335
385
|
}
|
|
336
386
|
|
|
337
|
-
const result = _makeMessageChunkFromAnthropicEvent(
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
387
|
+
const result = _makeMessageChunkFromAnthropicEvent(
|
|
388
|
+
data as Anthropic.Beta.Messages.BetaRawMessageStreamEvent,
|
|
389
|
+
{
|
|
390
|
+
streamUsage: shouldStreamUsage,
|
|
391
|
+
coerceContentToString,
|
|
392
|
+
}
|
|
393
|
+
);
|
|
341
394
|
if (!result) continue;
|
|
342
395
|
|
|
343
396
|
const { chunk } = result;
|