@librechat/agents 3.0.30 → 3.0.32
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/bedrock/index.cjs +99 -0
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -0
- package/dist/cjs/llm/providers.cjs +15 -15
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +29 -25
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +97 -0
- package/dist/esm/llm/bedrock/index.mjs.map +1 -0
- package/dist/esm/llm/providers.mjs +2 -2
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +30 -26
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/types/llm/bedrock/index.d.ts +36 -0
- package/dist/types/types/llm.d.ts +2 -2
- package/package.json +1 -1
- package/src/llm/bedrock/index.ts +128 -0
- package/src/llm/providers.ts +2 -4
- package/src/tools/handlers.ts +32 -28
- package/src/types/llm.ts +2 -2
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aws = require('@langchain/aws');
|
|
4
|
+
var messages = require('@langchain/core/messages');
|
|
5
|
+
var outputs = require('@langchain/core/outputs');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
|
|
9
|
+
*
|
|
10
|
+
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
11
|
+
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
12
|
+
* errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid
|
|
13
|
+
* the conflict.
|
|
14
|
+
*
|
|
15
|
+
* The contentBlockIndex field is only used internally by Bedrock's streaming protocol
|
|
16
|
+
* and isn't needed by application logic - the index field on tool_call_chunks serves
|
|
17
|
+
* the purpose of tracking tool call ordering.
|
|
18
|
+
*/
|
|
19
|
+
class CustomChatBedrockConverse extends aws.ChatBedrockConverse {
|
|
20
|
+
constructor(fields) {
|
|
21
|
+
super(fields);
|
|
22
|
+
}
|
|
23
|
+
static lc_name() {
|
|
24
|
+
return 'LibreChatBedrockConverse';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Override _streamResponseChunks to strip contentBlockIndex from response_metadata
|
|
28
|
+
* This prevents LangChain's merge conflicts when the same index is used for
|
|
29
|
+
* different content types (text vs tool calls)
|
|
30
|
+
*/
|
|
31
|
+
async *_streamResponseChunks(messages$1, options, runManager) {
|
|
32
|
+
const baseStream = super._streamResponseChunks(messages$1, options, runManager);
|
|
33
|
+
for await (const chunk of baseStream) {
|
|
34
|
+
// Only process if we have response_metadata
|
|
35
|
+
if (chunk.message instanceof messages.AIMessageChunk &&
|
|
36
|
+
chunk.message.response_metadata &&
|
|
37
|
+
typeof chunk.message.response_metadata === 'object') {
|
|
38
|
+
// Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)
|
|
39
|
+
const hasContentBlockIndex = this.hasContentBlockIndex(chunk.message.response_metadata);
|
|
40
|
+
if (hasContentBlockIndex) {
|
|
41
|
+
const cleanedMetadata = this.removeContentBlockIndex(chunk.message.response_metadata);
|
|
42
|
+
yield new outputs.ChatGenerationChunk({
|
|
43
|
+
text: chunk.text,
|
|
44
|
+
message: new messages.AIMessageChunk({
|
|
45
|
+
...chunk.message,
|
|
46
|
+
response_metadata: cleanedMetadata,
|
|
47
|
+
}),
|
|
48
|
+
generationInfo: chunk.generationInfo,
|
|
49
|
+
});
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
yield chunk;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Check if contentBlockIndex exists at any level in the object
|
|
58
|
+
*/
|
|
59
|
+
hasContentBlockIndex(obj) {
|
|
60
|
+
if (obj === null || obj === undefined || typeof obj !== 'object') {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if ('contentBlockIndex' in obj) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
for (const value of Object.values(obj)) {
|
|
67
|
+
if (typeof value === 'object' && value !== null) {
|
|
68
|
+
if (this.hasContentBlockIndex(value)) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Recursively remove contentBlockIndex from all levels of an object
|
|
77
|
+
*/
|
|
78
|
+
removeContentBlockIndex(obj) {
|
|
79
|
+
if (obj === null || obj === undefined) {
|
|
80
|
+
return obj;
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(obj)) {
|
|
83
|
+
return obj.map((item) => this.removeContentBlockIndex(item));
|
|
84
|
+
}
|
|
85
|
+
if (typeof obj === 'object') {
|
|
86
|
+
const cleaned = {};
|
|
87
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
88
|
+
if (key !== 'contentBlockIndex') {
|
|
89
|
+
cleaned[key] = this.removeContentBlockIndex(value);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return cleaned;
|
|
93
|
+
}
|
|
94
|
+
return obj;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
exports.CustomChatBedrockConverse = CustomChatBedrockConverse;
|
|
99
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts\n *\n * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,\n * causing LangChain's merge logic to fail with \"field[contentBlockIndex] already exists\"\n * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid\n * the conflict.\n *\n * The contentBlockIndex field is only used internally by Bedrock's streaming protocol\n * and isn't needed by application logic - the index field on tool_call_chunks serves\n * the purpose of tracking tool call ordering.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n constructor(fields?: ChatBedrockConverseInput) {\n super(fields);\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Override _streamResponseChunks to strip contentBlockIndex from response_metadata\n * This prevents LangChain's merge conflicts when the same index is used for\n * different content types (text vs tool calls)\n */\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const baseStream = super._streamResponseChunks(\n messages,\n options,\n runManager\n );\n\n for await (const chunk of baseStream) {\n // Only process if we have response_metadata\n if (\n chunk.message instanceof AIMessageChunk &&\n (chunk.message as Partial<AIMessageChunk>).response_metadata &&\n typeof chunk.message.response_metadata === 'object'\n ) {\n // Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)\n const hasContentBlockIndex = this.hasContentBlockIndex(\n chunk.message.response_metadata\n );\n\n if (hasContentBlockIndex) {\n const cleanedMetadata = this.removeContentBlockIndex(\n chunk.message.response_metadata\n ) as Record<string, unknown>;\n\n yield new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...chunk.message,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n continue;\n }\n }\n\n yield chunk;\n }\n }\n\n /**\n * Check if contentBlockIndex exists at any level in the object\n */\n private hasContentBlockIndex(obj: unknown): boolean {\n if (obj === null || obj === undefined || typeof obj !== 'object') {\n return false;\n }\n\n if ('contentBlockIndex' in obj) {\n return true;\n }\n\n for (const value of Object.values(obj)) {\n if (typeof value === 'object' && value !== null) {\n if (this.hasContentBlockIndex(value)) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Recursively remove contentBlockIndex from all levels of an object\n */\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"names":["ChatBedrockConverse","messages","AIMessageChunk","ChatGenerationChunk"],"mappings":";;;;;;AAAA;;;;;;;;;;;AAWG;AASG,MAAO,yBAA0B,SAAQA,uBAAmB,CAAA;AAChE,IAAA,WAAA,CAAY,MAAiC,EAAA;QAC3C,KAAK,CAAC,MAAM,CAAC;;AAGf,IAAA,OAAO,OAAO,GAAA;AACZ,QAAA,OAAO,0BAA0B;;AAGnC;;;;AAIG;IACH,OAAO,qBAAqB,CAC1BC,UAAuB,EACvB,OAAkC,EAClC,UAAqC,EAAA;AAErC,QAAA,MAAM,UAAU,GAAG,KAAK,CAAC,qBAAqB,CAC5CA,UAAQ,EACR,OAAO,EACP,UAAU,CACX;AAED,QAAA,WAAW,MAAM,KAAK,IAAI,UAAU,EAAE;;AAEpC,YAAA,IACE,KAAK,CAAC,OAAO,YAAYC,uBAAc;gBACtC,KAAK,CAAC,OAAmC,CAAC,iBAAiB;gBAC5D,OAAO,KAAK,CAAC,OAAO,CAAC,iBAAiB,KAAK,QAAQ,EACnD;;AAEA,gBAAA,MAAM,oBAAoB,GAAG,IAAI,CAAC,oBAAoB,CACpD,KAAK,CAAC,OAAO,CAAC,iBAAiB,CAChC;gBAED,IAAI,oBAAoB,EAAE;AACxB,oBAA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B,MAAM,OAAO,GAA4B,EAAE;AAC3C,YAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AAC9C,gBAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;oBAC/B,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,uBAAuB,CAAC,KAAK,CAAC;;;AAGtD,YAAA,OAAO,OAAO;;AAGhB,QAAA,OAAO,GAAG;;AAEb;;;;"}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var mistralai = require('@langchain/mistralai');
|
|
4
|
-
var
|
|
5
|
-
var index$3 = require('./openai/index.cjs');
|
|
4
|
+
var index$4 = require('./openai/index.cjs');
|
|
6
5
|
var index = require('./google/index.cjs');
|
|
7
|
-
var index$
|
|
8
|
-
var index$
|
|
9
|
-
var index$
|
|
10
|
-
var index$5 = require('./
|
|
6
|
+
var index$1 = require('./bedrock/index.cjs');
|
|
7
|
+
var index$3 = require('./anthropic/index.cjs');
|
|
8
|
+
var index$2 = require('./openrouter/index.cjs');
|
|
9
|
+
var index$5 = require('./vertexai/index.cjs');
|
|
10
|
+
var index$6 = require('./ollama/index.cjs');
|
|
11
11
|
var _enum = require('../common/enum.cjs');
|
|
12
12
|
|
|
13
13
|
// src/llm/providers.ts
|
|
14
14
|
const llmProviders = {
|
|
15
|
-
[_enum.Providers.XAI]: index$
|
|
16
|
-
[_enum.Providers.OPENAI]: index$
|
|
17
|
-
[_enum.Providers.OLLAMA]: index$
|
|
18
|
-
[_enum.Providers.AZURE]: index$
|
|
19
|
-
[_enum.Providers.VERTEXAI]: index$
|
|
20
|
-
[_enum.Providers.DEEPSEEK]: index$
|
|
15
|
+
[_enum.Providers.XAI]: index$4.ChatXAI,
|
|
16
|
+
[_enum.Providers.OPENAI]: index$4.ChatOpenAI,
|
|
17
|
+
[_enum.Providers.OLLAMA]: index$6.ChatOllama,
|
|
18
|
+
[_enum.Providers.AZURE]: index$4.AzureChatOpenAI,
|
|
19
|
+
[_enum.Providers.VERTEXAI]: index$5.ChatVertexAI,
|
|
20
|
+
[_enum.Providers.DEEPSEEK]: index$4.ChatDeepSeek,
|
|
21
21
|
[_enum.Providers.MISTRALAI]: mistralai.ChatMistralAI,
|
|
22
22
|
[_enum.Providers.MISTRAL]: mistralai.ChatMistralAI,
|
|
23
|
-
[_enum.Providers.ANTHROPIC]: index$
|
|
24
|
-
[_enum.Providers.OPENROUTER]: index$
|
|
25
|
-
[_enum.Providers.BEDROCK]:
|
|
23
|
+
[_enum.Providers.ANTHROPIC]: index$3.CustomAnthropic,
|
|
24
|
+
[_enum.Providers.OPENROUTER]: index$2.ChatOpenRouter,
|
|
25
|
+
[_enum.Providers.BEDROCK]: index$1.CustomChatBedrockConverse,
|
|
26
26
|
// [Providers.ANTHROPIC]: ChatAnthropic,
|
|
27
27
|
[_enum.Providers.GOOGLE]: index.CustomChatGoogleGenerativeAI,
|
|
28
28
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providers.cjs","sources":["../../../src/llm/providers.ts"],"sourcesContent":["// src/llm/providers.ts\nimport { ChatMistralAI } from '@langchain/mistralai';\nimport
|
|
1
|
+
{"version":3,"file":"providers.cjs","sources":["../../../src/llm/providers.ts"],"sourcesContent":["// src/llm/providers.ts\nimport { ChatMistralAI } from '@langchain/mistralai';\nimport type {\n ChatModelConstructorMap,\n ProviderOptionsMap,\n ChatModelMap,\n} from '@/types';\nimport {\n AzureChatOpenAI,\n ChatDeepSeek,\n ChatOpenAI,\n ChatXAI,\n} from '@/llm/openai';\nimport { CustomChatGoogleGenerativeAI } from '@/llm/google';\nimport { CustomChatBedrockConverse } from '@/llm/bedrock';\nimport { CustomAnthropic } from '@/llm/anthropic';\nimport { ChatOpenRouter } from '@/llm/openrouter';\nimport { ChatVertexAI } from '@/llm/vertexai';\nimport { ChatOllama } from '@/llm/ollama';\nimport { Providers } from '@/common';\n\nexport const llmProviders: Partial<ChatModelConstructorMap> = {\n [Providers.XAI]: ChatXAI,\n [Providers.OPENAI]: ChatOpenAI,\n [Providers.OLLAMA]: ChatOllama,\n [Providers.AZURE]: AzureChatOpenAI,\n [Providers.VERTEXAI]: ChatVertexAI,\n [Providers.DEEPSEEK]: ChatDeepSeek,\n [Providers.MISTRALAI]: ChatMistralAI,\n [Providers.MISTRAL]: ChatMistralAI,\n [Providers.ANTHROPIC]: CustomAnthropic,\n [Providers.OPENROUTER]: ChatOpenRouter,\n [Providers.BEDROCK]: CustomChatBedrockConverse,\n // [Providers.ANTHROPIC]: ChatAnthropic,\n [Providers.GOOGLE]: CustomChatGoogleGenerativeAI,\n};\n\nexport const manualToolStreamProviders = new Set<Providers | string>([\n Providers.ANTHROPIC,\n Providers.BEDROCK,\n Providers.OLLAMA,\n]);\n\nexport const getChatModelClass = <P extends Providers>(\n provider: P\n): new (config: ProviderOptionsMap[P]) => ChatModelMap[P] => {\n const ChatModelClass = llmProviders[provider];\n if (!ChatModelClass) {\n throw new Error(`Unsupported LLM provider: ${provider}`);\n }\n\n return ChatModelClass;\n};\n"],"names":["Providers","ChatXAI","ChatOpenAI","ChatOllama","AzureChatOpenAI","ChatVertexAI","ChatDeepSeek","ChatMistralAI","CustomAnthropic","ChatOpenRouter","CustomChatBedrockConverse","CustomChatGoogleGenerativeAI"],"mappings":";;;;;;;;;;;;AAAA;AAqBa,MAAA,YAAY,GAAqC;AAC5D,IAAA,CAACA,eAAS,CAAC,GAAG,GAAGC,eAAO;AACxB,IAAA,CAACD,eAAS,CAAC,MAAM,GAAGE,kBAAU;AAC9B,IAAA,CAACF,eAAS,CAAC,MAAM,GAAGG,kBAAU;AAC9B,IAAA,CAACH,eAAS,CAAC,KAAK,GAAGI,uBAAe;AAClC,IAAA,CAACJ,eAAS,CAAC,QAAQ,GAAGK,oBAAY;AAClC,IAAA,CAACL,eAAS,CAAC,QAAQ,GAAGM,oBAAY;AAClC,IAAA,CAACN,eAAS,CAAC,SAAS,GAAGO,uBAAa;AACpC,IAAA,CAACP,eAAS,CAAC,OAAO,GAAGO,uBAAa;AAClC,IAAA,CAACP,eAAS,CAAC,SAAS,GAAGQ,uBAAe;AACtC,IAAA,CAACR,eAAS,CAAC,UAAU,GAAGS,sBAAc;AACtC,IAAA,CAACT,eAAS,CAAC,OAAO,GAAGU,iCAAyB;;AAE9C,IAAA,CAACV,eAAS,CAAC,MAAM,GAAGW,kCAA4B;;AAGrC,MAAA,yBAAyB,GAAG,IAAI,GAAG,CAAqB;AACnE,IAAAX,eAAS,CAAC,SAAS;AACnB,IAAAA,eAAS,CAAC,OAAO;AACjB,IAAAA,eAAS,CAAC,MAAM;AACjB,CAAA;AAEY,MAAA,iBAAiB,GAAG,CAC/B,QAAW,KAC+C;AAC1D,IAAA,MAAM,cAAc,GAAG,YAAY,CAAC,QAAQ,CAAC;IAC7C,IAAI,CAAC,cAAc,EAAE;AACnB,QAAA,MAAM,IAAI,KAAK,CAAC,6BAA6B,QAAQ,CAAA,CAAE,CAAC;;AAG1D,IAAA,OAAO,cAAc;AACvB;;;;;;"}
|
|
@@ -55,20 +55,29 @@ async function handleToolCallChunks({ graph, stepKey, toolCallChunks, metadata,
|
|
|
55
55
|
let stepId = _stepId;
|
|
56
56
|
const alreadyDispatched = prevRunStep?.type === _enum.StepTypes.MESSAGE_CREATION &&
|
|
57
57
|
graph.messageStepHasToolCalls.has(prevStepId);
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
if (prevRunStep?.type === _enum.StepTypes.TOOL_CALLS) {
|
|
59
|
+
/**
|
|
60
|
+
* If previous step is already a tool_calls step, use that step ID
|
|
61
|
+
* This ensures tool call deltas are dispatched to the correct step
|
|
62
|
+
*/
|
|
63
|
+
stepId = prevStepId;
|
|
64
|
+
}
|
|
65
|
+
else if (!alreadyDispatched &&
|
|
66
|
+
prevRunStep?.type === _enum.StepTypes.MESSAGE_CREATION) {
|
|
67
|
+
/**
|
|
68
|
+
* Create tool_calls step as soon as we receive the first tool call chunk
|
|
69
|
+
* This ensures deltas are always associated with the correct step
|
|
70
|
+
*
|
|
71
|
+
* NOTE: We do NOT dispatch an empty text block here because:
|
|
72
|
+
* - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages
|
|
73
|
+
* - The tool_calls themselves are sufficient for the step
|
|
74
|
+
* - Empty content with tool_call_ids gets stored in conversation history
|
|
75
|
+
* and causes "messages must have non-empty content" errors on replay
|
|
76
|
+
*/
|
|
68
77
|
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
69
78
|
stepId = await graph.dispatchRunStep(stepKey, {
|
|
70
79
|
type: _enum.StepTypes.TOOL_CALLS,
|
|
71
|
-
tool_calls,
|
|
80
|
+
tool_calls: tool_calls ?? [],
|
|
72
81
|
}, metadata);
|
|
73
82
|
}
|
|
74
83
|
await graph.dispatchRunStepDelta(stepId, {
|
|
@@ -103,22 +112,18 @@ const handleToolCalls = async (toolCalls, metadata, graph) => {
|
|
|
103
112
|
catch {
|
|
104
113
|
// no previous step
|
|
105
114
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
],
|
|
115
|
-
});
|
|
116
|
-
};
|
|
115
|
+
/**
|
|
116
|
+
* NOTE: We do NOT dispatch empty text blocks with tool_call_ids because:
|
|
117
|
+
* - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages
|
|
118
|
+
* - They get stored in conversation history and cause errors on replay:
|
|
119
|
+
* "messages must have non-empty content" (Anthropic)
|
|
120
|
+
* "The content field in the Message object is empty" (Bedrock)
|
|
121
|
+
* - The tool_calls themselves are sufficient
|
|
122
|
+
*/
|
|
117
123
|
/* If the previous step exists and is a message creation */
|
|
118
124
|
if (prevStepId &&
|
|
119
125
|
prevRunStep &&
|
|
120
126
|
prevRunStep.type === _enum.StepTypes.MESSAGE_CREATION) {
|
|
121
|
-
await dispatchToolCallIds(prevStepId);
|
|
122
127
|
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
123
128
|
/* If the previous step doesn't exist or is not a message creation */
|
|
124
129
|
}
|
|
@@ -131,8 +136,7 @@ const handleToolCalls = async (toolCalls, metadata, graph) => {
|
|
|
131
136
|
message_id: messageId,
|
|
132
137
|
},
|
|
133
138
|
}, metadata);
|
|
134
|
-
|
|
135
|
-
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
139
|
+
graph.messageStepHasToolCalls.set(stepId, true);
|
|
136
140
|
}
|
|
137
141
|
await graph.dispatchRunStep(stepKey, {
|
|
138
142
|
type: _enum.StepTypes.TOOL_CALLS,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlers.cjs","sources":["../../../src/tools/handlers.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/tools/handlers.ts\nimport { nanoid } from 'nanoid';\nimport { ToolMessage } from '@langchain/core/messages';\nimport type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { MultiAgentGraph, StandardGraph } from '@/graphs';\nimport type { AgentContext } from '@/agents/AgentContext';\nimport type * as t from '@/types';\nimport {\n ToolCallTypes,\n ContentTypes,\n GraphEvents,\n StepTypes,\n Providers,\n Constants,\n} from '@/common';\nimport {\n coerceAnthropicSearchResults,\n isAnthropicWebSearchResult,\n} from '@/tools/search/anthropic';\nimport { formatResultsForLLM } from '@/tools/search/format';\nimport { getMessageId } from '@/messages';\n\nexport async function handleToolCallChunks({\n graph,\n stepKey,\n toolCallChunks,\n metadata,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n stepKey: string;\n toolCallChunks: ToolCallChunk[];\n metadata?: Record<string, unknown>;\n}): Promise<void> {\n let prevStepId: string;\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n /** Edge Case: If no previous step exists, create a new message creation step */\n const message_id = getMessageId(stepKey, graph, true) ?? '';\n prevStepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n prevRunStep = graph.getRunStep(prevStepId);\n }\n\n const _stepId = graph.getStepIdByKey(stepKey);\n\n /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */\n const tool_calls: ToolCall[] | undefined =\n prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION\n ? []\n : undefined;\n\n /** Edge Case: `id` and `name` fields cannot be empty strings */\n for (const toolCallChunk of toolCallChunks) {\n if (toolCallChunk.name === '') {\n toolCallChunk.name = undefined;\n }\n if (toolCallChunk.id === '') {\n toolCallChunk.id = undefined;\n } else if (\n tool_calls != null &&\n toolCallChunk.id != null &&\n toolCallChunk.name != null\n ) {\n tool_calls.push({\n args: {},\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n type: ToolCallTypes.TOOL_CALL,\n });\n }\n }\n\n let stepId: string = _stepId;\n const alreadyDispatched =\n prevRunStep?.type === StepTypes.MESSAGE_CREATION &&\n graph.messageStepHasToolCalls.has(prevStepId);\n if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {\n await graph.dispatchMessageDelta(prevStepId, {\n content: [\n {\n type: ContentTypes.TEXT,\n text: '',\n tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),\n },\n ],\n });\n graph.messageStepHasToolCalls.set(prevStepId, true);\n stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls,\n },\n metadata\n );\n }\n await graph.dispatchRunStepDelta(stepId, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: toolCallChunks,\n });\n}\n\nexport const handleToolCalls = async (\n toolCalls?: ToolCall[],\n metadata?: Record<string, unknown>,\n graph?: StandardGraph | MultiAgentGraph\n): Promise<void> => {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n if (!toolCalls) {\n return;\n }\n\n if (toolCalls.length === 0) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n for (const tool_call of toolCalls) {\n const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;\n tool_call.id = toolCallId;\n if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n\n let prevStepId = '';\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n // no previous step\n }\n\n const dispatchToolCallIds = async (\n lastMessageStepId: string\n ): Promise<void> => {\n await graph.dispatchMessageDelta(lastMessageStepId, {\n content: [\n {\n type: 'text',\n text: '',\n tool_call_ids: [toolCallId],\n },\n ],\n });\n };\n /* If the previous step exists and is a message creation */\n if (\n prevStepId &&\n prevRunStep &&\n prevRunStep.type === StepTypes.MESSAGE_CREATION\n ) {\n await dispatchToolCallIds(prevStepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n /* If the previous step doesn't exist or is not a message creation */\n } else if (\n !prevRunStep ||\n prevRunStep.type !== StepTypes.MESSAGE_CREATION\n ) {\n const messageId = getMessageId(stepKey, graph, true) ?? '';\n const stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: messageId,\n },\n },\n metadata\n );\n await dispatchToolCallIds(stepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n }\n\n await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls: [tool_call],\n },\n metadata\n );\n }\n};\n\nexport const toolResultTypes = new Set([\n // 'tool_use',\n // 'server_tool_use',\n // 'input_json_delta',\n 'tool_result',\n 'web_search_result',\n 'web_search_tool_result',\n]);\n\n/**\n * Handles the result of a server tool call; in other words, a provider's built-in tool.\n * As of 2025-07-06, only Anthropic handles server tool calls with this pattern.\n */\nexport async function handleServerToolResult({\n graph,\n content,\n metadata,\n agentContext,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n content?: string | t.MessageContentComplex[];\n metadata?: Record<string, unknown>;\n agentContext?: AgentContext;\n}): Promise<boolean> {\n let skipHandling = false;\n if (agentContext?.provider !== Providers.ANTHROPIC) {\n return skipHandling;\n }\n if (\n typeof content === 'string' ||\n content == null ||\n content.length === 0 ||\n (content.length === 1 &&\n (content[0] as t.ToolResultContent).tool_use_id == null)\n ) {\n return skipHandling;\n }\n\n for (const contentPart of content) {\n const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;\n if (toolUseId == null || toolUseId === '') {\n continue;\n }\n const stepId = graph.toolCallStepIds.get(toolUseId);\n if (stepId == null || stepId === '') {\n console.warn(\n `Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`\n );\n continue;\n }\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n console.warn(\n `Run step for ${stepId} does not exist, cannot dispatch tool result.`\n );\n continue;\n } else if (runStep.type !== StepTypes.TOOL_CALLS) {\n console.warn(\n `Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`\n );\n continue;\n }\n\n const toolCall =\n runStep.stepDetails.type === StepTypes.TOOL_CALLS\n ? (runStep.stepDetails.tool_calls?.find(\n (toolCall) => toolCall.id === toolUseId\n ) as ToolCall)\n : undefined;\n\n if (!toolCall) {\n continue;\n }\n\n if (\n contentPart.type === 'web_search_result' ||\n contentPart.type === 'web_search_tool_result'\n ) {\n await handleAnthropicSearchResults({\n contentPart: contentPart as t.ToolResultContent,\n toolCall,\n metadata,\n graph,\n });\n }\n\n if (!skipHandling) {\n skipHandling = true;\n }\n }\n\n return skipHandling;\n}\n\nasync function handleAnthropicSearchResults({\n contentPart,\n toolCall,\n metadata,\n graph,\n}: {\n contentPart: t.ToolResultContent;\n toolCall: Partial<ToolCall>;\n metadata?: Record<string, unknown>;\n graph: StandardGraph | MultiAgentGraph;\n}): Promise<void> {\n if (!Array.isArray(contentPart.content)) {\n console.warn(\n `Expected content to be an array, got ${typeof contentPart.content}`\n );\n return;\n }\n\n if (!isAnthropicWebSearchResult(contentPart.content[0])) {\n console.warn(\n `Expected content to be an Anthropic web search result, got ${JSON.stringify(\n contentPart.content\n )}`\n );\n return;\n }\n\n const turn = graph.invokedToolIds?.size ?? 0;\n const searchResultData = coerceAnthropicSearchResults({\n turn,\n results: contentPart.content as AnthropicWebSearchResultBlockParam[],\n });\n\n const name = toolCall.name;\n const input = toolCall.args ?? {};\n const artifact = {\n [Constants.WEB_SEARCH]: searchResultData,\n };\n const { output: formattedOutput } = formatResultsForLLM(\n turn,\n searchResultData\n );\n const output = new ToolMessage({\n name,\n artifact,\n content: formattedOutput,\n tool_call_id: toolCall.id!,\n });\n const toolEndData: t.ToolEndData = {\n input,\n output,\n };\n await graph.handlerRegistry\n ?.getHandler(GraphEvents.TOOL_END)\n ?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);\n\n if (graph.invokedToolIds == null) {\n graph.invokedToolIds = new Set<string>();\n }\n\n graph.invokedToolIds.add(toolCall.id!);\n}\n"],"names":["getMessageId","StepTypes","ToolCallTypes","ContentTypes","nanoid","Providers","isAnthropicWebSearchResult","coerceAnthropicSearchResults","Constants","formatResultsForLLM","ToolMessage","GraphEvents"],"mappings":";;;;;;;;;;AAAA;AACA;AAuBO,eAAe,oBAAoB,CAAC,EACzC,KAAK,EACL,OAAO,EACP,cAAc,EACd,QAAQ,GAMT,EAAA;AACC,IAAA,IAAI,UAAkB;AACtB,IAAA,IAAI,WAAkC;AACtC,IAAA,IAAI;AACF,QAAA,UAAU,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC;AAC1C,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC;;AAC1C,IAAA,MAAM;;AAEN,QAAA,MAAM,UAAU,GAAGA,g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A8D,IAAI,CAAC,SAAS,CAC1E,WAAW,CAAC,OAAO,CACpB,CAAA,CAAE,CACJ;QACD;;IAGF,MAAM,IAAI,GAAG,KAAK,CAAC,cAAc,EAAE,IAAI,IAAI,CAAC;IAC5C,MAAM,gBAAgB,GAAGC,sCAA4B,CAAC;QACpD,IAAI;QACJ,OAAO,EAAE,WAAW,CAAC,OAA+C;AACrE,KAAA,CAAC;AAEF,IAAA,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI;AAC1B,IAAA,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,IAAI,EAAE;AACjC,IAAA,MAAM,QAAQ,GAAG;AACf,QAAA,CAACC,eAAS,CAAC,UAAU,GAAG,gBAAgB;KACzC;AACD,IAAA,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,GAAGC,0BAAmB,CACrD,IAAI,EACJ,gBAAgB,CACjB;AACD,IAAA,MAAM,MAAM,GAAG,IAAIC,oBAAW,CAAC;QAC7B,IAAI;QACJ,QAAQ;AACR,QAAA,OAAO,EAAE,eAAe;QACxB,YAAY,EAAE,QAAQ,CAAC,EAAG;AAC3B,KAAA,CAAC;AACF,IAAA,MAAM,WAAW,GAAkB;QACjC,KAAK;QACL,MAAM;KACP;IACD,MAAM,KAAK,CAAC;AACV,UAAE,UAAU,CAACC,iBAAW,CAAC,QAAQ;AACjC,UAAE,MAAM,CAACA,iBAAW,CAAC,QAAQ,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,CAAC;AAE9D,IAAA,IAAI,KAAK,CAAC,cAAc,IAAI,IAAI,EAAE;AAChC,QAAA,KAAK,CAAC,cAAc,GAAG,IAAI,GAAG,EAAU;;IAG1C,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAG,CAAC;AACxC;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"handlers.cjs","sources":["../../../src/tools/handlers.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/tools/handlers.ts\nimport { nanoid } from 'nanoid';\nimport { ToolMessage } from '@langchain/core/messages';\nimport type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { MultiAgentGraph, StandardGraph } from '@/graphs';\nimport type { AgentContext } from '@/agents/AgentContext';\nimport type * as t from '@/types';\nimport {\n ToolCallTypes,\n GraphEvents,\n StepTypes,\n Providers,\n Constants,\n} from '@/common';\nimport {\n coerceAnthropicSearchResults,\n isAnthropicWebSearchResult,\n} from '@/tools/search/anthropic';\nimport { formatResultsForLLM } from '@/tools/search/format';\nimport { getMessageId } from '@/messages';\n\nexport async function handleToolCallChunks({\n graph,\n stepKey,\n toolCallChunks,\n metadata,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n stepKey: string;\n toolCallChunks: ToolCallChunk[];\n metadata?: Record<string, unknown>;\n}): Promise<void> {\n let prevStepId: string;\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n /** Edge Case: If no previous step exists, create a new message creation step */\n const message_id = getMessageId(stepKey, graph, true) ?? '';\n prevStepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n prevRunStep = graph.getRunStep(prevStepId);\n }\n\n const _stepId = graph.getStepIdByKey(stepKey);\n\n /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */\n const tool_calls: ToolCall[] | undefined =\n prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION\n ? []\n : undefined;\n\n /** Edge Case: `id` and `name` fields cannot be empty strings */\n for (const toolCallChunk of toolCallChunks) {\n if (toolCallChunk.name === '') {\n toolCallChunk.name = undefined;\n }\n if (toolCallChunk.id === '') {\n toolCallChunk.id = undefined;\n } else if (\n tool_calls != null &&\n toolCallChunk.id != null &&\n toolCallChunk.name != null\n ) {\n tool_calls.push({\n args: {},\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n type: ToolCallTypes.TOOL_CALL,\n });\n }\n }\n\n let stepId: string = _stepId;\n const alreadyDispatched =\n prevRunStep?.type === StepTypes.MESSAGE_CREATION &&\n graph.messageStepHasToolCalls.has(prevStepId);\n\n if (prevRunStep?.type === StepTypes.TOOL_CALLS) {\n /**\n * If previous step is already a tool_calls step, use that step ID\n * This ensures tool call deltas are dispatched to the correct step\n */\n stepId = prevStepId;\n } else if (\n !alreadyDispatched &&\n prevRunStep?.type === StepTypes.MESSAGE_CREATION\n ) {\n /**\n * Create tool_calls step as soon as we receive the first tool call chunk\n * This ensures deltas are always associated with the correct step\n *\n * NOTE: We do NOT dispatch an empty text block here because:\n * - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages\n * - The tool_calls themselves are sufficient for the step\n * - Empty content with tool_call_ids gets stored in conversation history\n * and causes \"messages must have non-empty content\" errors on replay\n */\n graph.messageStepHasToolCalls.set(prevStepId, true);\n stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls: tool_calls ?? [],\n },\n metadata\n );\n }\n await graph.dispatchRunStepDelta(stepId, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: toolCallChunks,\n });\n}\n\nexport const handleToolCalls = async (\n toolCalls?: ToolCall[],\n metadata?: Record<string, unknown>,\n graph?: StandardGraph | MultiAgentGraph\n): Promise<void> => {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n if (!toolCalls) {\n return;\n }\n\n if (toolCalls.length === 0) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n for (const tool_call of toolCalls) {\n const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;\n tool_call.id = toolCallId;\n if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n\n let prevStepId = '';\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n // no previous step\n }\n\n /**\n * NOTE: We do NOT dispatch empty text blocks with tool_call_ids because:\n * - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages\n * - They get stored in conversation history and cause errors on replay:\n * \"messages must have non-empty content\" (Anthropic)\n * \"The content field in the Message object is empty\" (Bedrock)\n * - The tool_calls themselves are sufficient\n */\n\n /* If the previous step exists and is a message creation */\n if (\n prevStepId &&\n prevRunStep &&\n prevRunStep.type === StepTypes.MESSAGE_CREATION\n ) {\n graph.messageStepHasToolCalls.set(prevStepId, true);\n /* If the previous step doesn't exist or is not a message creation */\n } else if (\n !prevRunStep ||\n prevRunStep.type !== StepTypes.MESSAGE_CREATION\n ) {\n const messageId = getMessageId(stepKey, graph, true) ?? '';\n const stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: messageId,\n },\n },\n metadata\n );\n graph.messageStepHasToolCalls.set(stepId, true);\n }\n\n await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls: [tool_call],\n },\n metadata\n );\n }\n};\n\nexport const toolResultTypes = new Set([\n // 'tool_use',\n // 'server_tool_use',\n // 'input_json_delta',\n 'tool_result',\n 'web_search_result',\n 'web_search_tool_result',\n]);\n\n/**\n * Handles the result of a server tool call; in other words, a provider's built-in tool.\n * As of 2025-07-06, only Anthropic handles server tool calls with this pattern.\n */\nexport async function handleServerToolResult({\n graph,\n content,\n metadata,\n agentContext,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n content?: string | t.MessageContentComplex[];\n metadata?: Record<string, unknown>;\n agentContext?: AgentContext;\n}): Promise<boolean> {\n let skipHandling = false;\n if (agentContext?.provider !== Providers.ANTHROPIC) {\n return skipHandling;\n }\n if (\n typeof content === 'string' ||\n content == null ||\n content.length === 0 ||\n (content.length === 1 &&\n (content[0] as t.ToolResultContent).tool_use_id == null)\n ) {\n return skipHandling;\n }\n\n for (const contentPart of content) {\n const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;\n if (toolUseId == null || toolUseId === '') {\n continue;\n }\n const stepId = graph.toolCallStepIds.get(toolUseId);\n if (stepId == null || stepId === '') {\n console.warn(\n `Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`\n );\n continue;\n }\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n console.warn(\n `Run step for ${stepId} does not exist, cannot dispatch tool result.`\n );\n continue;\n } else if (runStep.type !== StepTypes.TOOL_CALLS) {\n console.warn(\n `Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`\n );\n continue;\n }\n\n const toolCall =\n runStep.stepDetails.type === StepTypes.TOOL_CALLS\n ? (runStep.stepDetails.tool_calls?.find(\n (toolCall) => toolCall.id === toolUseId\n ) as ToolCall)\n : undefined;\n\n if (!toolCall) {\n continue;\n }\n\n if (\n contentPart.type === 'web_search_result' ||\n contentPart.type === 'web_search_tool_result'\n ) {\n await handleAnthropicSearchResults({\n contentPart: contentPart as t.ToolResultContent,\n toolCall,\n metadata,\n graph,\n });\n }\n\n if (!skipHandling) {\n skipHandling = true;\n }\n }\n\n return skipHandling;\n}\n\nasync function handleAnthropicSearchResults({\n contentPart,\n toolCall,\n metadata,\n graph,\n}: {\n contentPart: t.ToolResultContent;\n toolCall: Partial<ToolCall>;\n metadata?: Record<string, unknown>;\n graph: StandardGraph | MultiAgentGraph;\n}): Promise<void> {\n if (!Array.isArray(contentPart.content)) {\n console.warn(\n `Expected content to be an array, got ${typeof contentPart.content}`\n );\n return;\n }\n\n if (!isAnthropicWebSearchResult(contentPart.content[0])) {\n console.warn(\n `Expected content to be an Anthropic web search result, got ${JSON.stringify(\n contentPart.content\n )}`\n );\n return;\n }\n\n const turn = graph.invokedToolIds?.size ?? 0;\n const searchResultData = coerceAnthropicSearchResults({\n turn,\n results: contentPart.content as AnthropicWebSearchResultBlockParam[],\n });\n\n const name = toolCall.name;\n const input = toolCall.args ?? {};\n const artifact = {\n [Constants.WEB_SEARCH]: searchResultData,\n };\n const { output: formattedOutput } = formatResultsForLLM(\n turn,\n searchResultData\n );\n const output = new ToolMessage({\n name,\n artifact,\n content: formattedOutput,\n tool_call_id: toolCall.id!,\n });\n const toolEndData: t.ToolEndData = {\n input,\n output,\n };\n await graph.handlerRegistry\n ?.getHandler(GraphEvents.TOOL_END)\n ?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);\n\n if (graph.invokedToolIds == null) {\n graph.invokedToolIds = new Set<string>();\n }\n\n graph.invokedToolIds.add(toolCall.id!);\n}\n"],"names":["getMessageId","StepTypes","ToolCallTypes","nanoid","Providers","isAnthropicWebSearchResult","coerceAnthropicSearchResults","Constants","formatResultsForLLM","ToolMessage","GraphEvents"],"mappings":";;;;;;;;;;AAAA;AACA;AAsBO,eAAe,oBAAoB,CAAC,EACzC,KAAK,EACL,OAAO,EACP,cAAc,EACd,QAAQ,GAMT,EAAA;AACC,IAAA,IAAI,UAAkB;AACtB,IAAA,IAAI,WAAkC;AACtC,IAAA,IAAI;AACF,QAAA,UAAU,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC;AAC1C,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC;;AAC1C,IAAA,MAAM;;AAEN,QAAA,MAAM,UAAU,GAAGA,gBAAY,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE;AAC3D,QAAA,UAAU,GAAG,MAAM,KAAK,CAAC,eAAe,CACtC,OAAO,EACP;YACE,IAAI,EAAEC,eAAS,CAAC,gBAAgB;AAChC,YAAA,gBAAgB,EAAE;gBAChB,UAAU;AACX,aAAA;SACF,EACD,QAAQ,CACT;AACD,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC;;IAG5C,MAAM,OAAO,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC;;AAG7C,IAAA,MAAM,UAAU,GACd,UAAU,IAAI,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;AAE9D,IAAA,IAAI,KAAK,CAAC,cAAc,IAAI,IAAI,EAAE;AAChC,QAAA,KAAK,CAAC,cAAc,GAAG,IAAI,GAAG,EAAU;;IAG1C,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAG,CAAC;AACxC;;;;;;;"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { ChatBedrockConverse } from '@langchain/aws';
|
|
2
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
3
|
+
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
|
|
7
|
+
*
|
|
8
|
+
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
9
|
+
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
10
|
+
* errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid
|
|
11
|
+
* the conflict.
|
|
12
|
+
*
|
|
13
|
+
* The contentBlockIndex field is only used internally by Bedrock's streaming protocol
|
|
14
|
+
* and isn't needed by application logic - the index field on tool_call_chunks serves
|
|
15
|
+
* the purpose of tracking tool call ordering.
|
|
16
|
+
*/
|
|
17
|
+
class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
18
|
+
constructor(fields) {
|
|
19
|
+
super(fields);
|
|
20
|
+
}
|
|
21
|
+
static lc_name() {
|
|
22
|
+
return 'LibreChatBedrockConverse';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Override _streamResponseChunks to strip contentBlockIndex from response_metadata
|
|
26
|
+
* This prevents LangChain's merge conflicts when the same index is used for
|
|
27
|
+
* different content types (text vs tool calls)
|
|
28
|
+
*/
|
|
29
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
30
|
+
const baseStream = super._streamResponseChunks(messages, options, runManager);
|
|
31
|
+
for await (const chunk of baseStream) {
|
|
32
|
+
// Only process if we have response_metadata
|
|
33
|
+
if (chunk.message instanceof AIMessageChunk &&
|
|
34
|
+
chunk.message.response_metadata &&
|
|
35
|
+
typeof chunk.message.response_metadata === 'object') {
|
|
36
|
+
// Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)
|
|
37
|
+
const hasContentBlockIndex = this.hasContentBlockIndex(chunk.message.response_metadata);
|
|
38
|
+
if (hasContentBlockIndex) {
|
|
39
|
+
const cleanedMetadata = this.removeContentBlockIndex(chunk.message.response_metadata);
|
|
40
|
+
yield new ChatGenerationChunk({
|
|
41
|
+
text: chunk.text,
|
|
42
|
+
message: new AIMessageChunk({
|
|
43
|
+
...chunk.message,
|
|
44
|
+
response_metadata: cleanedMetadata,
|
|
45
|
+
}),
|
|
46
|
+
generationInfo: chunk.generationInfo,
|
|
47
|
+
});
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
yield chunk;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check if contentBlockIndex exists at any level in the object
|
|
56
|
+
*/
|
|
57
|
+
hasContentBlockIndex(obj) {
|
|
58
|
+
if (obj === null || obj === undefined || typeof obj !== 'object') {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if ('contentBlockIndex' in obj) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
for (const value of Object.values(obj)) {
|
|
65
|
+
if (typeof value === 'object' && value !== null) {
|
|
66
|
+
if (this.hasContentBlockIndex(value)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Recursively remove contentBlockIndex from all levels of an object
|
|
75
|
+
*/
|
|
76
|
+
removeContentBlockIndex(obj) {
|
|
77
|
+
if (obj === null || obj === undefined) {
|
|
78
|
+
return obj;
|
|
79
|
+
}
|
|
80
|
+
if (Array.isArray(obj)) {
|
|
81
|
+
return obj.map((item) => this.removeContentBlockIndex(item));
|
|
82
|
+
}
|
|
83
|
+
if (typeof obj === 'object') {
|
|
84
|
+
const cleaned = {};
|
|
85
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
86
|
+
if (key !== 'contentBlockIndex') {
|
|
87
|
+
cleaned[key] = this.removeContentBlockIndex(value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return cleaned;
|
|
91
|
+
}
|
|
92
|
+
return obj;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { CustomChatBedrockConverse };
|
|
97
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts\n *\n * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,\n * causing LangChain's merge logic to fail with \"field[contentBlockIndex] already exists\"\n * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid\n * the conflict.\n *\n * The contentBlockIndex field is only used internally by Bedrock's streaming protocol\n * and isn't needed by application logic - the index field on tool_call_chunks serves\n * the purpose of tracking tool call ordering.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n constructor(fields?: ChatBedrockConverseInput) {\n super(fields);\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Override _streamResponseChunks to strip contentBlockIndex from response_metadata\n * This prevents LangChain's merge conflicts when the same index is used for\n * different content types (text vs tool calls)\n */\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const baseStream = super._streamResponseChunks(\n messages,\n options,\n runManager\n );\n\n for await (const chunk of baseStream) {\n // Only process if we have response_metadata\n if (\n chunk.message instanceof AIMessageChunk &&\n (chunk.message as Partial<AIMessageChunk>).response_metadata &&\n typeof chunk.message.response_metadata === 'object'\n ) {\n // Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)\n const hasContentBlockIndex = this.hasContentBlockIndex(\n chunk.message.response_metadata\n );\n\n if (hasContentBlockIndex) {\n const cleanedMetadata = this.removeContentBlockIndex(\n chunk.message.response_metadata\n ) as Record<string, unknown>;\n\n yield new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...chunk.message,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n continue;\n }\n }\n\n yield chunk;\n }\n }\n\n /**\n * Check if contentBlockIndex exists at any level in the object\n */\n private hasContentBlockIndex(obj: unknown): boolean {\n if (obj === null || obj === undefined || typeof obj !== 'object') {\n return false;\n }\n\n if ('contentBlockIndex' in obj) {\n return true;\n }\n\n for (const value of Object.values(obj)) {\n if (typeof value === 'object' && value !== null) {\n if (this.hasContentBlockIndex(value)) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Recursively remove contentBlockIndex from all levels of an object\n */\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;;;;AAWG;AASG,MAAO,yB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mB,IAAI,GAAG,EAAE;AAC9B,YAAA,OAAO,IAAI;;QAGb,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;YACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE;AAC/C,gBAAA,IAAI,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,EAAE;AACpC,oBAAA,OAAO,IAAI;;;;AAKjB,QAAA,OAAO,KAAK;;AAGd;;AAEG;AACK,IAAA,uBAAuB,CAAC,GAAY,EAAA;QAC1C,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE;AACrC,YAAA,OAAO,GAAG;;AAGZ,QAAA,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AACtB,YAAA,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC;;AAG9D,QAAA,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,MAAM,OAAO,GAA4B,EAAE;AAC3C,YAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AAC9C,gBAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;oBAC/B,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,uBAAuB,CAAC,KAAK,CAAC;;;AAGtD,YAAA,OAAO,OAAO;;AAGhB,QAAA,OAAO,GAAG;;AAEb;;;;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ChatMistralAI } from '@langchain/mistralai';
|
|
2
|
-
import { ChatBedrockConverse } from '@langchain/aws';
|
|
3
2
|
import { ChatDeepSeek, AzureChatOpenAI, ChatOpenAI, ChatXAI } from './openai/index.mjs';
|
|
4
3
|
import { CustomChatGoogleGenerativeAI } from './google/index.mjs';
|
|
4
|
+
import { CustomChatBedrockConverse } from './bedrock/index.mjs';
|
|
5
5
|
import { CustomAnthropic } from './anthropic/index.mjs';
|
|
6
6
|
import { ChatOpenRouter } from './openrouter/index.mjs';
|
|
7
7
|
import { ChatVertexAI } from './vertexai/index.mjs';
|
|
@@ -20,7 +20,7 @@ const llmProviders = {
|
|
|
20
20
|
[Providers.MISTRAL]: ChatMistralAI,
|
|
21
21
|
[Providers.ANTHROPIC]: CustomAnthropic,
|
|
22
22
|
[Providers.OPENROUTER]: ChatOpenRouter,
|
|
23
|
-
[Providers.BEDROCK]:
|
|
23
|
+
[Providers.BEDROCK]: CustomChatBedrockConverse,
|
|
24
24
|
// [Providers.ANTHROPIC]: ChatAnthropic,
|
|
25
25
|
[Providers.GOOGLE]: CustomChatGoogleGenerativeAI,
|
|
26
26
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providers.mjs","sources":["../../../src/llm/providers.ts"],"sourcesContent":["// src/llm/providers.ts\nimport { ChatMistralAI } from '@langchain/mistralai';\nimport
|
|
1
|
+
{"version":3,"file":"providers.mjs","sources":["../../../src/llm/providers.ts"],"sourcesContent":["// src/llm/providers.ts\nimport { ChatMistralAI } from '@langchain/mistralai';\nimport type {\n ChatModelConstructorMap,\n ProviderOptionsMap,\n ChatModelMap,\n} from '@/types';\nimport {\n AzureChatOpenAI,\n ChatDeepSeek,\n ChatOpenAI,\n ChatXAI,\n} from '@/llm/openai';\nimport { CustomChatGoogleGenerativeAI } from '@/llm/google';\nimport { CustomChatBedrockConverse } from '@/llm/bedrock';\nimport { CustomAnthropic } from '@/llm/anthropic';\nimport { ChatOpenRouter } from '@/llm/openrouter';\nimport { ChatVertexAI } from '@/llm/vertexai';\nimport { ChatOllama } from '@/llm/ollama';\nimport { Providers } from '@/common';\n\nexport const llmProviders: Partial<ChatModelConstructorMap> = {\n [Providers.XAI]: ChatXAI,\n [Providers.OPENAI]: ChatOpenAI,\n [Providers.OLLAMA]: ChatOllama,\n [Providers.AZURE]: AzureChatOpenAI,\n [Providers.VERTEXAI]: ChatVertexAI,\n [Providers.DEEPSEEK]: ChatDeepSeek,\n [Providers.MISTRALAI]: ChatMistralAI,\n [Providers.MISTRAL]: ChatMistralAI,\n [Providers.ANTHROPIC]: CustomAnthropic,\n [Providers.OPENROUTER]: ChatOpenRouter,\n [Providers.BEDROCK]: CustomChatBedrockConverse,\n // [Providers.ANTHROPIC]: ChatAnthropic,\n [Providers.GOOGLE]: CustomChatGoogleGenerativeAI,\n};\n\nexport const manualToolStreamProviders = new Set<Providers | string>([\n Providers.ANTHROPIC,\n Providers.BEDROCK,\n Providers.OLLAMA,\n]);\n\nexport const getChatModelClass = <P extends Providers>(\n provider: P\n): new (config: ProviderOptionsMap[P]) => ChatModelMap[P] => {\n const ChatModelClass = llmProviders[provider];\n if (!ChatModelClass) {\n throw new Error(`Unsupported LLM provider: ${provider}`);\n }\n\n return ChatModelClass;\n};\n"],"names":[],"mappings":";;;;;;;;;;AAAA;AAqBa,MAAA,YAAY,GAAqC;AAC5D,IAAA,CAAC,SAAS,CAAC,GAAG,GAAG,OAAO;AACxB,IAAA,CAAC,SAAS,CAAC,MAAM,GAAG,UAAU;AAC9B,IAAA,CAAC,SAAS,CAAC,MAAM,GAAG,UAAU;AAC9B,IAAA,CAAC,SAAS,CAAC,KAAK,GAAG,eAAe;AAClC,IAAA,CAAC,SAAS,CAAC,QAAQ,GAAG,YAAY;AAClC,IAAA,CAAC,SAAS,CAAC,QAAQ,GAAG,YAAY;AAClC,IAAA,CAAC,SAAS,CAAC,SAAS,GAAG,aAAa;AACpC,IAAA,CAAC,SAAS,CAAC,OAAO,GAAG,aAAa;AAClC,IAAA,CAAC,SAAS,CAAC,SAAS,GAAG,eAAe;AACtC,IAAA,CAAC,SAAS,CAAC,UAAU,GAAG,cAAc;AACtC,IAAA,CAAC,SAAS,CAAC,OAAO,GAAG,yBAAyB;;AAE9C,IAAA,CAAC,SAAS,CAAC,MAAM,GAAG,4BAA4B;;AAGrC,MAAA,yBAAyB,GAAG,IAAI,GAAG,CAAqB;AACnE,IAAA,SAAS,CAAC,SAAS;AACnB,IAAA,SAAS,CAAC,OAAO;AACjB,IAAA,SAAS,CAAC,MAAM;AACjB,CAAA;AAEY,MAAA,iBAAiB,GAAG,CAC/B,QAAW,KAC+C;AAC1D,IAAA,MAAM,cAAc,GAAG,YAAY,CAAC,QAAQ,CAAC;IAC7C,IAAI,CAAC,cAAc,EAAE;AACnB,QAAA,MAAM,IAAI,KAAK,CAAC,6BAA6B,QAAQ,CAAA,CAAE,CAAC;;AAG1D,IAAA,OAAO,cAAc;AACvB;;;;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { nanoid } from 'nanoid';
|
|
2
2
|
import { ToolMessage } from '@langchain/core/messages';
|
|
3
|
-
import { StepTypes, ToolCallTypes,
|
|
3
|
+
import { StepTypes, ToolCallTypes, Providers, Constants, GraphEvents } from '../common/enum.mjs';
|
|
4
4
|
import { isAnthropicWebSearchResult, coerceAnthropicSearchResults } from './search/anthropic.mjs';
|
|
5
5
|
import { formatResultsForLLM } from './search/format.mjs';
|
|
6
6
|
import '../messages/core.mjs';
|
|
@@ -53,20 +53,29 @@ async function handleToolCallChunks({ graph, stepKey, toolCallChunks, metadata,
|
|
|
53
53
|
let stepId = _stepId;
|
|
54
54
|
const alreadyDispatched = prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
|
|
55
55
|
graph.messageStepHasToolCalls.has(prevStepId);
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
56
|
+
if (prevRunStep?.type === StepTypes.TOOL_CALLS) {
|
|
57
|
+
/**
|
|
58
|
+
* If previous step is already a tool_calls step, use that step ID
|
|
59
|
+
* This ensures tool call deltas are dispatched to the correct step
|
|
60
|
+
*/
|
|
61
|
+
stepId = prevStepId;
|
|
62
|
+
}
|
|
63
|
+
else if (!alreadyDispatched &&
|
|
64
|
+
prevRunStep?.type === StepTypes.MESSAGE_CREATION) {
|
|
65
|
+
/**
|
|
66
|
+
* Create tool_calls step as soon as we receive the first tool call chunk
|
|
67
|
+
* This ensures deltas are always associated with the correct step
|
|
68
|
+
*
|
|
69
|
+
* NOTE: We do NOT dispatch an empty text block here because:
|
|
70
|
+
* - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages
|
|
71
|
+
* - The tool_calls themselves are sufficient for the step
|
|
72
|
+
* - Empty content with tool_call_ids gets stored in conversation history
|
|
73
|
+
* and causes "messages must have non-empty content" errors on replay
|
|
74
|
+
*/
|
|
66
75
|
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
67
76
|
stepId = await graph.dispatchRunStep(stepKey, {
|
|
68
77
|
type: StepTypes.TOOL_CALLS,
|
|
69
|
-
tool_calls,
|
|
78
|
+
tool_calls: tool_calls ?? [],
|
|
70
79
|
}, metadata);
|
|
71
80
|
}
|
|
72
81
|
await graph.dispatchRunStepDelta(stepId, {
|
|
@@ -101,22 +110,18 @@ const handleToolCalls = async (toolCalls, metadata, graph) => {
|
|
|
101
110
|
catch {
|
|
102
111
|
// no previous step
|
|
103
112
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
],
|
|
113
|
-
});
|
|
114
|
-
};
|
|
113
|
+
/**
|
|
114
|
+
* NOTE: We do NOT dispatch empty text blocks with tool_call_ids because:
|
|
115
|
+
* - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages
|
|
116
|
+
* - They get stored in conversation history and cause errors on replay:
|
|
117
|
+
* "messages must have non-empty content" (Anthropic)
|
|
118
|
+
* "The content field in the Message object is empty" (Bedrock)
|
|
119
|
+
* - The tool_calls themselves are sufficient
|
|
120
|
+
*/
|
|
115
121
|
/* If the previous step exists and is a message creation */
|
|
116
122
|
if (prevStepId &&
|
|
117
123
|
prevRunStep &&
|
|
118
124
|
prevRunStep.type === StepTypes.MESSAGE_CREATION) {
|
|
119
|
-
await dispatchToolCallIds(prevStepId);
|
|
120
125
|
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
121
126
|
/* If the previous step doesn't exist or is not a message creation */
|
|
122
127
|
}
|
|
@@ -129,8 +134,7 @@ const handleToolCalls = async (toolCalls, metadata, graph) => {
|
|
|
129
134
|
message_id: messageId,
|
|
130
135
|
},
|
|
131
136
|
}, metadata);
|
|
132
|
-
|
|
133
|
-
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
137
|
+
graph.messageStepHasToolCalls.set(stepId, true);
|
|
134
138
|
}
|
|
135
139
|
await graph.dispatchRunStep(stepKey, {
|
|
136
140
|
type: StepTypes.TOOL_CALLS,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlers.mjs","sources":["../../../src/tools/handlers.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/tools/handlers.ts\nimport { nanoid } from 'nanoid';\nimport { ToolMessage } from '@langchain/core/messages';\nimport type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { MultiAgentGraph, StandardGraph } from '@/graphs';\nimport type { AgentContext } from '@/agents/AgentContext';\nimport type * as t from '@/types';\nimport {\n ToolCallTypes,\n ContentTypes,\n GraphEvents,\n StepTypes,\n Providers,\n Constants,\n} from '@/common';\nimport {\n coerceAnthropicSearchResults,\n isAnthropicWebSearchResult,\n} from '@/tools/search/anthropic';\nimport { formatResultsForLLM } from '@/tools/search/format';\nimport { getMessageId } from '@/messages';\n\nexport async function handleToolCallChunks({\n graph,\n stepKey,\n toolCallChunks,\n metadata,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n stepKey: string;\n toolCallChunks: ToolCallChunk[];\n metadata?: Record<string, unknown>;\n}): Promise<void> {\n let prevStepId: string;\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n /** Edge Case: If no previous step exists, create a new message creation step */\n const message_id = getMessageId(stepKey, graph, true) ?? '';\n prevStepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n prevRunStep = graph.getRunStep(prevStepId);\n }\n\n const _stepId = graph.getStepIdByKey(stepKey);\n\n /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */\n const tool_calls: ToolCall[] | undefined =\n prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION\n ? []\n : undefined;\n\n /** Edge Case: `id` and `name` fields cannot be empty strings */\n for (const toolCallChunk of toolCallChunks) {\n if (toolCallChunk.name === '') {\n toolCallChunk.name = undefined;\n }\n if (toolCallChunk.id === '') {\n toolCallChunk.id = undefined;\n } else if (\n tool_calls != null &&\n toolCallChunk.id != null &&\n toolCallChunk.name != null\n ) {\n tool_calls.push({\n args: {},\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n type: ToolCallTypes.TOOL_CALL,\n });\n }\n }\n\n let stepId: string = _stepId;\n const alreadyDispatched =\n prevRunStep?.type === StepTypes.MESSAGE_CREATION &&\n graph.messageStepHasToolCalls.has(prevStepId);\n if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {\n await graph.dispatchMessageDelta(prevStepId, {\n content: [\n {\n type: ContentTypes.TEXT,\n text: '',\n tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),\n },\n ],\n });\n graph.messageStepHasToolCalls.set(prevStepId, true);\n stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls,\n },\n metadata\n );\n }\n await graph.dispatchRunStepDelta(stepId, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: toolCallChunks,\n });\n}\n\nexport const handleToolCalls = async (\n toolCalls?: ToolCall[],\n metadata?: Record<string, unknown>,\n graph?: StandardGraph | MultiAgentGraph\n): Promise<void> => {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n if (!toolCalls) {\n return;\n }\n\n if (toolCalls.length === 0) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n for (const tool_call of toolCalls) {\n const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;\n tool_call.id = toolCallId;\n if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n\n let prevStepId = '';\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n // no previous step\n }\n\n const dispatchToolCallIds = async (\n lastMessageStepId: string\n ): Promise<void> => {\n await graph.dispatchMessageDelta(lastMessageStepId, {\n content: [\n {\n type: 'text',\n text: '',\n tool_call_ids: [toolCallId],\n },\n ],\n });\n };\n /* If the previous step exists and is a message creation */\n if (\n prevStepId &&\n prevRunStep &&\n prevRunStep.type === StepTypes.MESSAGE_CREATION\n ) {\n await dispatchToolCallIds(prevStepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n /* If the previous step doesn't exist or is not a message creation */\n } else if (\n !prevRunStep ||\n prevRunStep.type !== StepTypes.MESSAGE_CREATION\n ) {\n const messageId = getMessageId(stepKey, graph, true) ?? '';\n const stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: messageId,\n },\n },\n metadata\n );\n await dispatchToolCallIds(stepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n }\n\n await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls: [tool_call],\n },\n metadata\n );\n }\n};\n\nexport const toolResultTypes = new Set([\n // 'tool_use',\n // 'server_tool_use',\n // 'input_json_delta',\n 'tool_result',\n 'web_search_result',\n 'web_search_tool_result',\n]);\n\n/**\n * Handles the result of a server tool call; in other words, a provider's built-in tool.\n * As of 2025-07-06, only Anthropic handles server tool calls with this pattern.\n */\nexport async function handleServerToolResult({\n graph,\n content,\n metadata,\n agentContext,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n content?: string | t.MessageContentComplex[];\n metadata?: Record<string, unknown>;\n agentContext?: AgentContext;\n}): Promise<boolean> {\n let skipHandling = false;\n if (agentContext?.provider !== Providers.ANTHROPIC) {\n return skipHandling;\n }\n if (\n typeof content === 'string' ||\n content == null ||\n content.length === 0 ||\n (content.length === 1 &&\n (content[0] as t.ToolResultContent).tool_use_id == null)\n ) {\n return skipHandling;\n }\n\n for (const contentPart of content) {\n const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;\n if (toolUseId == null || toolUseId === '') {\n continue;\n }\n const stepId = graph.toolCallStepIds.get(toolUseId);\n if (stepId == null || stepId === '') {\n console.warn(\n `Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`\n );\n continue;\n }\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n console.warn(\n `Run step for ${stepId} does not exist, cannot dispatch tool result.`\n );\n continue;\n } else if (runStep.type !== StepTypes.TOOL_CALLS) {\n console.warn(\n `Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`\n );\n continue;\n }\n\n const toolCall =\n runStep.stepDetails.type === StepTypes.TOOL_CALLS\n ? (runStep.stepDetails.tool_calls?.find(\n (toolCall) => toolCall.id === toolUseId\n ) as ToolCall)\n : undefined;\n\n if (!toolCall) {\n continue;\n }\n\n if (\n contentPart.type === 'web_search_result' ||\n contentPart.type === 'web_search_tool_result'\n ) {\n await handleAnthropicSearchResults({\n contentPart: contentPart as t.ToolResultContent,\n toolCall,\n metadata,\n graph,\n });\n }\n\n if (!skipHandling) {\n skipHandling = true;\n }\n }\n\n return skipHandling;\n}\n\nasync function handleAnthropicSearchResults({\n contentPart,\n toolCall,\n metadata,\n graph,\n}: {\n contentPart: t.ToolResultContent;\n toolCall: Partial<ToolCall>;\n metadata?: Record<string, unknown>;\n graph: StandardGraph | MultiAgentGraph;\n}): Promise<void> {\n if (!Array.isArray(contentPart.content)) {\n console.warn(\n `Expected content to be an array, got ${typeof contentPart.content}`\n );\n return;\n }\n\n if (!isAnthropicWebSearchResult(contentPart.content[0])) {\n console.warn(\n `Expected content to be an Anthropic web search result, got ${JSON.stringify(\n contentPart.content\n )}`\n );\n return;\n }\n\n const turn = graph.invokedToolIds?.size ?? 0;\n const searchResultData = coerceAnthropicSearchResults({\n turn,\n results: contentPart.content as AnthropicWebSearchResultBlockParam[],\n });\n\n const name = toolCall.name;\n const input = toolCall.args ?? {};\n const artifact = {\n [Constants.WEB_SEARCH]: searchResultData,\n };\n const { output: formattedOutput } = formatResultsForLLM(\n turn,\n searchResultData\n );\n const output = new ToolMessage({\n name,\n artifact,\n content: formattedOutput,\n tool_call_id: toolCall.id!,\n });\n const toolEndData: t.ToolEndData = {\n input,\n output,\n };\n await graph.handlerRegistry\n ?.getHandler(GraphEvents.TOOL_END)\n ?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);\n\n if (graph.invokedToolIds == null) {\n graph.invokedToolIds = new Set<string>();\n }\n\n graph.invokedToolIds.add(toolCall.id!);\n}\n"],"names":[],"mappings":";;;;;;;;AAAA;AACA;AAuBO,eAAe,oBAAoB,CAAC,EACzC,KAAK,EACL,OAAO,EACP,cAAc,EACd,QAAQ,GAMT,EAAA;AACC,IAAA,IAAI,UAAkB;AACtB,IAAA,IAAI,WAAkC;AACtC,IAAA,IAAI;AACF,QAAA,UAAU,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC;AAC1C,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC;;AAC1C,IAAA,MAAM;;AAEN,QAAA,MAAM,UAAU,GAAG,YAAY,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE;AAC3D,QAAA,UAAU,GAAG,MAAM,KAAK,CAAC,eAAe,CACtC,OAAO,EACP;Y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MAAM,gBAAgB,GAAG,4BAA4B,CAAC;QACpD,IAAI;QACJ,OAAO,EAAE,WAAW,CAAC,OAA+C;AACrE,KAAA,CAAC;AAEF,IAAA,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI;AAC1B,IAAA,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,IAAI,EAAE;AACjC,IAAA,MAAM,QAAQ,GAAG;AACf,QAAA,CAAC,SAAS,CAAC,UAAU,GAAG,gBAAgB;KACzC;AACD,IAAA,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,GAAG,mBAAmB,CACrD,IAAI,EACJ,gBAAgB,CACjB;AACD,IAAA,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC;QAC7B,IAAI;QACJ,QAAQ;AACR,QAAA,OAAO,EAAE,eAAe;QACxB,YAAY,EAAE,QAAQ,CAAC,EAAG;AAC3B,KAAA,CAAC;AACF,IAAA,MAAM,WAAW,GAAkB;QACjC,KAAK;QACL,MAAM;KACP;IACD,MAAM,KAAK,CAAC;AACV,UAAE,UAAU,CAAC,WAAW,CAAC,QAAQ;AACjC,UAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,CAAC;AAE9D,IAAA,IAAI,KAAK,CAAC,cAAc,IAAI,IAAI,EAAE;AAChC,QAAA,KAAK,CAAC,cAAc,GAAG,IAAI,GAAG,EAAU;;IAG1C,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAG,CAAC;AACxC;;;;"}
|
|
1
|
+
{"version":3,"file":"handlers.mjs","sources":["../../../src/tools/handlers.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/tools/handlers.ts\nimport { nanoid } from 'nanoid';\nimport { ToolMessage } from '@langchain/core/messages';\nimport type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { MultiAgentGraph, StandardGraph } from '@/graphs';\nimport type { AgentContext } from '@/agents/AgentContext';\nimport type * as t from '@/types';\nimport {\n ToolCallTypes,\n GraphEvents,\n StepTypes,\n Providers,\n Constants,\n} from '@/common';\nimport {\n coerceAnthropicSearchResults,\n isAnthropicWebSearchResult,\n} from '@/tools/search/anthropic';\nimport { formatResultsForLLM } from '@/tools/search/format';\nimport { getMessageId } from '@/messages';\n\nexport async function handleToolCallChunks({\n graph,\n stepKey,\n toolCallChunks,\n metadata,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n stepKey: string;\n toolCallChunks: ToolCallChunk[];\n metadata?: Record<string, unknown>;\n}): Promise<void> {\n let prevStepId: string;\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n /** Edge Case: If no previous step exists, create a new message creation step */\n const message_id = getMessageId(stepKey, graph, true) ?? '';\n prevStepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n },\n metadata\n );\n prevRunStep = graph.getRunStep(prevStepId);\n }\n\n const _stepId = graph.getStepIdByKey(stepKey);\n\n /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */\n const tool_calls: ToolCall[] | undefined =\n prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION\n ? []\n : undefined;\n\n /** Edge Case: `id` and `name` fields cannot be empty strings */\n for (const toolCallChunk of toolCallChunks) {\n if (toolCallChunk.name === '') {\n toolCallChunk.name = undefined;\n }\n if (toolCallChunk.id === '') {\n toolCallChunk.id = undefined;\n } else if (\n tool_calls != null &&\n toolCallChunk.id != null &&\n toolCallChunk.name != null\n ) {\n tool_calls.push({\n args: {},\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n type: ToolCallTypes.TOOL_CALL,\n });\n }\n }\n\n let stepId: string = _stepId;\n const alreadyDispatched =\n prevRunStep?.type === StepTypes.MESSAGE_CREATION &&\n graph.messageStepHasToolCalls.has(prevStepId);\n\n if (prevRunStep?.type === StepTypes.TOOL_CALLS) {\n /**\n * If previous step is already a tool_calls step, use that step ID\n * This ensures tool call deltas are dispatched to the correct step\n */\n stepId = prevStepId;\n } else if (\n !alreadyDispatched &&\n prevRunStep?.type === StepTypes.MESSAGE_CREATION\n ) {\n /**\n * Create tool_calls step as soon as we receive the first tool call chunk\n * This ensures deltas are always associated with the correct step\n *\n * NOTE: We do NOT dispatch an empty text block here because:\n * - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages\n * - The tool_calls themselves are sufficient for the step\n * - Empty content with tool_call_ids gets stored in conversation history\n * and causes \"messages must have non-empty content\" errors on replay\n */\n graph.messageStepHasToolCalls.set(prevStepId, true);\n stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls: tool_calls ?? [],\n },\n metadata\n );\n }\n await graph.dispatchRunStepDelta(stepId, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: toolCallChunks,\n });\n}\n\nexport const handleToolCalls = async (\n toolCalls?: ToolCall[],\n metadata?: Record<string, unknown>,\n graph?: StandardGraph | MultiAgentGraph\n): Promise<void> => {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n if (!toolCalls) {\n return;\n }\n\n if (toolCalls.length === 0) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n for (const tool_call of toolCalls) {\n const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;\n tool_call.id = toolCallId;\n if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n\n let prevStepId = '';\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n // no previous step\n }\n\n /**\n * NOTE: We do NOT dispatch empty text blocks with tool_call_ids because:\n * - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages\n * - They get stored in conversation history and cause errors on replay:\n * \"messages must have non-empty content\" (Anthropic)\n * \"The content field in the Message object is empty\" (Bedrock)\n * - The tool_calls themselves are sufficient\n */\n\n /* If the previous step exists and is a message creation */\n if (\n prevStepId &&\n prevRunStep &&\n prevRunStep.type === StepTypes.MESSAGE_CREATION\n ) {\n graph.messageStepHasToolCalls.set(prevStepId, true);\n /* If the previous step doesn't exist or is not a message creation */\n } else if (\n !prevRunStep ||\n prevRunStep.type !== StepTypes.MESSAGE_CREATION\n ) {\n const messageId = getMessageId(stepKey, graph, true) ?? '';\n const stepId = await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: messageId,\n },\n },\n metadata\n );\n graph.messageStepHasToolCalls.set(stepId, true);\n }\n\n await graph.dispatchRunStep(\n stepKey,\n {\n type: StepTypes.TOOL_CALLS,\n tool_calls: [tool_call],\n },\n metadata\n );\n }\n};\n\nexport const toolResultTypes = new Set([\n // 'tool_use',\n // 'server_tool_use',\n // 'input_json_delta',\n 'tool_result',\n 'web_search_result',\n 'web_search_tool_result',\n]);\n\n/**\n * Handles the result of a server tool call; in other words, a provider's built-in tool.\n * As of 2025-07-06, only Anthropic handles server tool calls with this pattern.\n */\nexport async function handleServerToolResult({\n graph,\n content,\n metadata,\n agentContext,\n}: {\n graph: StandardGraph | MultiAgentGraph;\n content?: string | t.MessageContentComplex[];\n metadata?: Record<string, unknown>;\n agentContext?: AgentContext;\n}): Promise<boolean> {\n let skipHandling = false;\n if (agentContext?.provider !== Providers.ANTHROPIC) {\n return skipHandling;\n }\n if (\n typeof content === 'string' ||\n content == null ||\n content.length === 0 ||\n (content.length === 1 &&\n (content[0] as t.ToolResultContent).tool_use_id == null)\n ) {\n return skipHandling;\n }\n\n for (const contentPart of content) {\n const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;\n if (toolUseId == null || toolUseId === '') {\n continue;\n }\n const stepId = graph.toolCallStepIds.get(toolUseId);\n if (stepId == null || stepId === '') {\n console.warn(\n `Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`\n );\n continue;\n }\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n console.warn(\n `Run step for ${stepId} does not exist, cannot dispatch tool result.`\n );\n continue;\n } else if (runStep.type !== StepTypes.TOOL_CALLS) {\n console.warn(\n `Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`\n );\n continue;\n }\n\n const toolCall =\n runStep.stepDetails.type === StepTypes.TOOL_CALLS\n ? (runStep.stepDetails.tool_calls?.find(\n (toolCall) => toolCall.id === toolUseId\n ) as ToolCall)\n : undefined;\n\n if (!toolCall) {\n continue;\n }\n\n if (\n contentPart.type === 'web_search_result' ||\n contentPart.type === 'web_search_tool_result'\n ) {\n await handleAnthropicSearchResults({\n contentPart: contentPart as t.ToolResultContent,\n toolCall,\n metadata,\n graph,\n });\n }\n\n if (!skipHandling) {\n skipHandling = true;\n }\n }\n\n return skipHandling;\n}\n\nasync function handleAnthropicSearchResults({\n contentPart,\n toolCall,\n metadata,\n graph,\n}: {\n contentPart: t.ToolResultContent;\n toolCall: Partial<ToolCall>;\n metadata?: Record<string, unknown>;\n graph: StandardGraph | MultiAgentGraph;\n}): Promise<void> {\n if (!Array.isArray(contentPart.content)) {\n console.warn(\n `Expected content to be an array, got ${typeof contentPart.content}`\n );\n return;\n }\n\n if (!isAnthropicWebSearchResult(contentPart.content[0])) {\n console.warn(\n `Expected content to be an Anthropic web search result, got ${JSON.stringify(\n contentPart.content\n )}`\n );\n return;\n }\n\n const turn = graph.invokedToolIds?.size ?? 0;\n const searchResultData = coerceAnthropicSearchResults({\n turn,\n results: contentPart.content as AnthropicWebSearchResultBlockParam[],\n });\n\n const name = toolCall.name;\n const input = toolCall.args ?? {};\n const artifact = {\n [Constants.WEB_SEARCH]: searchResultData,\n };\n const { output: formattedOutput } = formatResultsForLLM(\n turn,\n searchResultData\n );\n const output = new ToolMessage({\n name,\n artifact,\n content: formattedOutput,\n tool_call_id: toolCall.id!,\n });\n const toolEndData: t.ToolEndData = {\n input,\n output,\n };\n await graph.handlerRegistry\n ?.getHandler(GraphEvents.TOOL_END)\n ?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);\n\n if (graph.invokedToolIds == null) {\n graph.invokedToolIds = new Set<string>();\n }\n\n graph.invokedToolIds.add(toolCall.id!);\n}\n"],"names":[],"mappings":";;;;;;;;AAAA;AACA;AAsBO,eAAe,oBAAoB,CAAC,EACzC,KAAK,EACL,OAAO,EACP,cAAc,EACd,QAAQ,GAMT,EAAA;AACC,IAAA,IAAI,UAAkB;AACtB,IAAA,IAAI,WAAkC;AACtC,IAAA,IAAI;AACF,QAAA,UAAU,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC;AAC1C,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC;;AAC1C,IAAA,MAAM;;AAEN,QAAA,MAAM,UAAU,GAAG,YAAY,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE;AAC3D,QAAA,UAAU,GAAG,MAAM,KAAK,CAAC,eAAe,CACtC,OAAO,EACP;YACE,IAAI,EAAE,SAAS,CAAC,gBAAgB;AAChC,YAAA,gBAAgB,EAAE;gBAChB,UAAU;AACX,aAAA;SACF,EACD,QAAQ,CACT;AACD,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC;;IAG5C,MAAM,OAAO,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC;;AAG7C,IAAA,MAAM,UAAU,GACd,UAAU,IAAI,WAAW,IAAI,WAAW,CAAC,IAAI,KAAK,SAAS,CAAC;AAC1D,UAAE;UACA,SAAS;;AAGf,IAAA,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE;AAC1C,QAAA,IAAI,aAAa,CAAC,IAAI,KAAK,EAAE,EAAE;AAC7B,YAAA,aAAa,CAAC,IAAI,GAAG,SAAS;;AAEhC,QAAA,IAAI,aAAa,CAAC,EAAE,KAAK,EAAE,E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+
*
|
|
4
|
+
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
5
|
+
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
6
|
+
* errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid
|
|
7
|
+
* the conflict.
|
|
8
|
+
*
|
|
9
|
+
* The contentBlockIndex field is only used internally by Bedrock's streaming protocol
|
|
10
|
+
* and isn't needed by application logic - the index field on tool_call_chunks serves
|
|
11
|
+
* the purpose of tracking tool call ordering.
|
|
12
|
+
*/
|
|
13
|
+
import { ChatBedrockConverse } from '@langchain/aws';
|
|
14
|
+
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
|
15
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
16
|
+
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
17
|
+
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
18
|
+
export declare class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
19
|
+
constructor(fields?: ChatBedrockConverseInput);
|
|
20
|
+
static lc_name(): string;
|
|
21
|
+
/**
|
|
22
|
+
* Override _streamResponseChunks to strip contentBlockIndex from response_metadata
|
|
23
|
+
* This prevents LangChain's merge conflicts when the same index is used for
|
|
24
|
+
* different content types (text vs tool calls)
|
|
25
|
+
*/
|
|
26
|
+
_streamResponseChunks(messages: BaseMessage[], options: this['ParsedCallOptions'], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
27
|
+
/**
|
|
28
|
+
* Check if contentBlockIndex exists at any level in the object
|
|
29
|
+
*/
|
|
30
|
+
private hasContentBlockIndex;
|
|
31
|
+
/**
|
|
32
|
+
* Recursively remove contentBlockIndex from all levels of an object
|
|
33
|
+
*/
|
|
34
|
+
private removeContentBlockIndex;
|
|
35
|
+
}
|
|
36
|
+
export type { ChatBedrockConverseInput };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ChatOllama } from '@langchain/ollama';
|
|
2
2
|
import { ChatMistralAI } from '@langchain/mistralai';
|
|
3
|
-
import { ChatBedrockConverse } from '@langchain/aws';
|
|
4
3
|
import type { BindToolsInput, BaseChatModelParams } from '@langchain/core/language_models/chat_models';
|
|
5
4
|
import type { OpenAIChatInput, ChatOpenAIFields, AzureOpenAIInput, ClientOptions as OAIClientOptions } from '@langchain/openai';
|
|
6
5
|
import type { GoogleGenerativeAIChatInput } from '@langchain/google-genai';
|
|
@@ -19,6 +18,7 @@ import type { OpenAI as OpenAIClient } from 'openai';
|
|
|
19
18
|
import type { ChatXAIInput } from '@langchain/xai';
|
|
20
19
|
import { AzureChatOpenAI, ChatDeepSeek, ChatOpenAI, ChatXAI } from '@/llm/openai';
|
|
21
20
|
import { CustomChatGoogleGenerativeAI } from '@/llm/google';
|
|
21
|
+
import { CustomChatBedrockConverse } from '@/llm/bedrock';
|
|
22
22
|
import { CustomAnthropic } from '@/llm/anthropic';
|
|
23
23
|
import { ChatOpenRouter } from '@/llm/openrouter';
|
|
24
24
|
import { ChatVertexAI } from '@/llm/vertexai';
|
|
@@ -94,7 +94,7 @@ export type ChatModelMap = {
|
|
|
94
94
|
[Providers.MISTRALAI]: ChatMistralAI;
|
|
95
95
|
[Providers.MISTRAL]: ChatMistralAI;
|
|
96
96
|
[Providers.OPENROUTER]: ChatOpenRouter;
|
|
97
|
-
[Providers.BEDROCK]:
|
|
97
|
+
[Providers.BEDROCK]: CustomChatBedrockConverse;
|
|
98
98
|
[Providers.GOOGLE]: CustomChatGoogleGenerativeAI;
|
|
99
99
|
};
|
|
100
100
|
export type ChatModelConstructorMap = {
|
package/package.json
CHANGED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
|
|
3
|
+
*
|
|
4
|
+
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
5
|
+
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
6
|
+
* errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid
|
|
7
|
+
* the conflict.
|
|
8
|
+
*
|
|
9
|
+
* The contentBlockIndex field is only used internally by Bedrock's streaming protocol
|
|
10
|
+
* and isn't needed by application logic - the index field on tool_call_chunks serves
|
|
11
|
+
* the purpose of tracking tool call ordering.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { ChatBedrockConverse } from '@langchain/aws';
|
|
15
|
+
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
|
16
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
17
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
18
|
+
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
19
|
+
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
20
|
+
|
|
21
|
+
export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
22
|
+
constructor(fields?: ChatBedrockConverseInput) {
|
|
23
|
+
super(fields);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static lc_name(): string {
|
|
27
|
+
return 'LibreChatBedrockConverse';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Override _streamResponseChunks to strip contentBlockIndex from response_metadata
|
|
32
|
+
* This prevents LangChain's merge conflicts when the same index is used for
|
|
33
|
+
* different content types (text vs tool calls)
|
|
34
|
+
*/
|
|
35
|
+
async *_streamResponseChunks(
|
|
36
|
+
messages: BaseMessage[],
|
|
37
|
+
options: this['ParsedCallOptions'],
|
|
38
|
+
runManager?: CallbackManagerForLLMRun
|
|
39
|
+
): AsyncGenerator<ChatGenerationChunk> {
|
|
40
|
+
const baseStream = super._streamResponseChunks(
|
|
41
|
+
messages,
|
|
42
|
+
options,
|
|
43
|
+
runManager
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
for await (const chunk of baseStream) {
|
|
47
|
+
// Only process if we have response_metadata
|
|
48
|
+
if (
|
|
49
|
+
chunk.message instanceof AIMessageChunk &&
|
|
50
|
+
(chunk.message as Partial<AIMessageChunk>).response_metadata &&
|
|
51
|
+
typeof chunk.message.response_metadata === 'object'
|
|
52
|
+
) {
|
|
53
|
+
// Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)
|
|
54
|
+
const hasContentBlockIndex = this.hasContentBlockIndex(
|
|
55
|
+
chunk.message.response_metadata
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (hasContentBlockIndex) {
|
|
59
|
+
const cleanedMetadata = this.removeContentBlockIndex(
|
|
60
|
+
chunk.message.response_metadata
|
|
61
|
+
) as Record<string, unknown>;
|
|
62
|
+
|
|
63
|
+
yield new ChatGenerationChunk({
|
|
64
|
+
text: chunk.text,
|
|
65
|
+
message: new AIMessageChunk({
|
|
66
|
+
...chunk.message,
|
|
67
|
+
response_metadata: cleanedMetadata,
|
|
68
|
+
}),
|
|
69
|
+
generationInfo: chunk.generationInfo,
|
|
70
|
+
});
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
yield chunk;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Check if contentBlockIndex exists at any level in the object
|
|
81
|
+
*/
|
|
82
|
+
private hasContentBlockIndex(obj: unknown): boolean {
|
|
83
|
+
if (obj === null || obj === undefined || typeof obj !== 'object') {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if ('contentBlockIndex' in obj) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const value of Object.values(obj)) {
|
|
92
|
+
if (typeof value === 'object' && value !== null) {
|
|
93
|
+
if (this.hasContentBlockIndex(value)) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Recursively remove contentBlockIndex from all levels of an object
|
|
104
|
+
*/
|
|
105
|
+
private removeContentBlockIndex(obj: unknown): unknown {
|
|
106
|
+
if (obj === null || obj === undefined) {
|
|
107
|
+
return obj;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (Array.isArray(obj)) {
|
|
111
|
+
return obj.map((item) => this.removeContentBlockIndex(item));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (typeof obj === 'object') {
|
|
115
|
+
const cleaned: Record<string, unknown> = {};
|
|
116
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
117
|
+
if (key !== 'contentBlockIndex') {
|
|
118
|
+
cleaned[key] = this.removeContentBlockIndex(value);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return cleaned;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return obj;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type { ChatBedrockConverseInput };
|
package/src/llm/providers.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
// src/llm/providers.ts
|
|
2
2
|
import { ChatMistralAI } from '@langchain/mistralai';
|
|
3
|
-
import { ChatBedrockConverse } from '@langchain/aws';
|
|
4
|
-
// import { ChatAnthropic } from '@langchain/anthropic';
|
|
5
|
-
// import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
6
3
|
import type {
|
|
7
4
|
ChatModelConstructorMap,
|
|
8
5
|
ProviderOptionsMap,
|
|
@@ -15,6 +12,7 @@ import {
|
|
|
15
12
|
ChatXAI,
|
|
16
13
|
} from '@/llm/openai';
|
|
17
14
|
import { CustomChatGoogleGenerativeAI } from '@/llm/google';
|
|
15
|
+
import { CustomChatBedrockConverse } from '@/llm/bedrock';
|
|
18
16
|
import { CustomAnthropic } from '@/llm/anthropic';
|
|
19
17
|
import { ChatOpenRouter } from '@/llm/openrouter';
|
|
20
18
|
import { ChatVertexAI } from '@/llm/vertexai';
|
|
@@ -32,7 +30,7 @@ export const llmProviders: Partial<ChatModelConstructorMap> = {
|
|
|
32
30
|
[Providers.MISTRAL]: ChatMistralAI,
|
|
33
31
|
[Providers.ANTHROPIC]: CustomAnthropic,
|
|
34
32
|
[Providers.OPENROUTER]: ChatOpenRouter,
|
|
35
|
-
[Providers.BEDROCK]:
|
|
33
|
+
[Providers.BEDROCK]: CustomChatBedrockConverse,
|
|
36
34
|
// [Providers.ANTHROPIC]: ChatAnthropic,
|
|
37
35
|
[Providers.GOOGLE]: CustomChatGoogleGenerativeAI,
|
|
38
36
|
};
|
package/src/tools/handlers.ts
CHANGED
|
@@ -9,7 +9,6 @@ import type { AgentContext } from '@/agents/AgentContext';
|
|
|
9
9
|
import type * as t from '@/types';
|
|
10
10
|
import {
|
|
11
11
|
ToolCallTypes,
|
|
12
|
-
ContentTypes,
|
|
13
12
|
GraphEvents,
|
|
14
13
|
StepTypes,
|
|
15
14
|
Providers,
|
|
@@ -87,22 +86,33 @@ export async function handleToolCallChunks({
|
|
|
87
86
|
const alreadyDispatched =
|
|
88
87
|
prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
|
|
89
88
|
graph.messageStepHasToolCalls.has(prevStepId);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
89
|
+
|
|
90
|
+
if (prevRunStep?.type === StepTypes.TOOL_CALLS) {
|
|
91
|
+
/**
|
|
92
|
+
* If previous step is already a tool_calls step, use that step ID
|
|
93
|
+
* This ensures tool call deltas are dispatched to the correct step
|
|
94
|
+
*/
|
|
95
|
+
stepId = prevStepId;
|
|
96
|
+
} else if (
|
|
97
|
+
!alreadyDispatched &&
|
|
98
|
+
prevRunStep?.type === StepTypes.MESSAGE_CREATION
|
|
99
|
+
) {
|
|
100
|
+
/**
|
|
101
|
+
* Create tool_calls step as soon as we receive the first tool call chunk
|
|
102
|
+
* This ensures deltas are always associated with the correct step
|
|
103
|
+
*
|
|
104
|
+
* NOTE: We do NOT dispatch an empty text block here because:
|
|
105
|
+
* - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages
|
|
106
|
+
* - The tool_calls themselves are sufficient for the step
|
|
107
|
+
* - Empty content with tool_call_ids gets stored in conversation history
|
|
108
|
+
* and causes "messages must have non-empty content" errors on replay
|
|
109
|
+
*/
|
|
100
110
|
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
101
111
|
stepId = await graph.dispatchRunStep(
|
|
102
112
|
stepKey,
|
|
103
113
|
{
|
|
104
114
|
type: StepTypes.TOOL_CALLS,
|
|
105
|
-
tool_calls,
|
|
115
|
+
tool_calls: tool_calls ?? [],
|
|
106
116
|
},
|
|
107
117
|
metadata
|
|
108
118
|
);
|
|
@@ -149,26 +159,21 @@ export const handleToolCalls = async (
|
|
|
149
159
|
// no previous step
|
|
150
160
|
}
|
|
151
161
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
},
|
|
162
|
-
],
|
|
163
|
-
});
|
|
164
|
-
};
|
|
162
|
+
/**
|
|
163
|
+
* NOTE: We do NOT dispatch empty text blocks with tool_call_ids because:
|
|
164
|
+
* - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages
|
|
165
|
+
* - They get stored in conversation history and cause errors on replay:
|
|
166
|
+
* "messages must have non-empty content" (Anthropic)
|
|
167
|
+
* "The content field in the Message object is empty" (Bedrock)
|
|
168
|
+
* - The tool_calls themselves are sufficient
|
|
169
|
+
*/
|
|
170
|
+
|
|
165
171
|
/* If the previous step exists and is a message creation */
|
|
166
172
|
if (
|
|
167
173
|
prevStepId &&
|
|
168
174
|
prevRunStep &&
|
|
169
175
|
prevRunStep.type === StepTypes.MESSAGE_CREATION
|
|
170
176
|
) {
|
|
171
|
-
await dispatchToolCallIds(prevStepId);
|
|
172
177
|
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
173
178
|
/* If the previous step doesn't exist or is not a message creation */
|
|
174
179
|
} else if (
|
|
@@ -186,8 +191,7 @@ export const handleToolCalls = async (
|
|
|
186
191
|
},
|
|
187
192
|
metadata
|
|
188
193
|
);
|
|
189
|
-
|
|
190
|
-
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
194
|
+
graph.messageStepHasToolCalls.set(stepId, true);
|
|
191
195
|
}
|
|
192
196
|
|
|
193
197
|
await graph.dispatchRunStep(
|
package/src/types/llm.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// src/types/llm.ts
|
|
2
2
|
import { ChatOllama } from '@langchain/ollama';
|
|
3
3
|
import { ChatMistralAI } from '@langchain/mistralai';
|
|
4
|
-
import { ChatBedrockConverse } from '@langchain/aws';
|
|
5
4
|
import type {
|
|
6
5
|
BindToolsInput,
|
|
7
6
|
BaseChatModelParams,
|
|
@@ -33,6 +32,7 @@ import {
|
|
|
33
32
|
ChatXAI,
|
|
34
33
|
} from '@/llm/openai';
|
|
35
34
|
import { CustomChatGoogleGenerativeAI } from '@/llm/google';
|
|
35
|
+
import { CustomChatBedrockConverse } from '@/llm/bedrock';
|
|
36
36
|
import { CustomAnthropic } from '@/llm/anthropic';
|
|
37
37
|
import { ChatOpenRouter } from '@/llm/openrouter';
|
|
38
38
|
import { ChatVertexAI } from '@/llm/vertexai';
|
|
@@ -126,7 +126,7 @@ export type ChatModelMap = {
|
|
|
126
126
|
[Providers.MISTRALAI]: ChatMistralAI;
|
|
127
127
|
[Providers.MISTRAL]: ChatMistralAI;
|
|
128
128
|
[Providers.OPENROUTER]: ChatOpenRouter;
|
|
129
|
-
[Providers.BEDROCK]:
|
|
129
|
+
[Providers.BEDROCK]: CustomChatBedrockConverse;
|
|
130
130
|
[Providers.GOOGLE]: CustomChatGoogleGenerativeAI;
|
|
131
131
|
};
|
|
132
132
|
|