@librechat/agents 2.1.3 → 2.1.5
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/common/enum.cjs +1 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +12 -0
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/llm.cjs +46 -16
- package/dist/cjs/llm/anthropic/llm.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +41 -3
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/stream.cjs +13 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +1 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +12 -0
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/anthropic/llm.mjs +46 -16
- package/dist/esm/llm/anthropic/llm.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +41 -3
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/stream.mjs +13 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +1 -0
- package/dist/types/llm/anthropic/llm.d.ts +3 -2
- package/dist/types/llm/anthropic/types.d.ts +7 -0
- package/dist/types/scripts/thinking.d.ts +1 -0
- package/dist/types/types/stream.d.ts +9 -2
- package/package.json +2 -1
- package/src/common/enum.ts +1 -0
- package/src/graphs/Graph.ts +12 -0
- package/src/llm/anthropic/llm.spec.ts +1069 -0
- package/src/llm/anthropic/llm.ts +65 -22
- package/src/llm/anthropic/types.ts +7 -2
- package/src/llm/anthropic/utils/message_outputs.ts +53 -8
- package/src/scripts/thinking.ts +155 -0
- package/src/stream.ts +11 -2
- package/src/types/stream.ts +10 -2
|
@@ -11,16 +11,27 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
11
11
|
filteredAdditionalKwargs[key] = value;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
const { input_tokens, output_tokens, ...rest } = usage ?? {};
|
|
14
16
|
const usageMetadata = {
|
|
15
|
-
input_tokens
|
|
16
|
-
output_tokens
|
|
17
|
-
total_tokens:
|
|
17
|
+
input_tokens,
|
|
18
|
+
output_tokens,
|
|
19
|
+
total_tokens: input_tokens + output_tokens,
|
|
20
|
+
input_token_details: {
|
|
21
|
+
cache_creation: rest.cache_creation_input_tokens,
|
|
22
|
+
cache_read: rest.cache_read_input_tokens,
|
|
23
|
+
},
|
|
18
24
|
};
|
|
19
25
|
return {
|
|
20
26
|
chunk: new AIMessageChunk({
|
|
21
27
|
content: fields.coerceContentToString ? '' : [],
|
|
22
28
|
additional_kwargs: filteredAdditionalKwargs,
|
|
23
29
|
usage_metadata: fields.streamUsage ? usageMetadata : undefined,
|
|
30
|
+
response_metadata: {
|
|
31
|
+
usage: {
|
|
32
|
+
...rest,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
24
35
|
id: data.message.id,
|
|
25
36
|
}),
|
|
26
37
|
};
|
|
@@ -30,6 +41,12 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
30
41
|
input_tokens: 0,
|
|
31
42
|
output_tokens: data.usage.output_tokens,
|
|
32
43
|
total_tokens: data.usage.output_tokens,
|
|
44
|
+
input_token_details: {
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
+
cache_creation: data.usage.cache_creation_input_tokens,
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
cache_read: data.usage.cache_read_input_tokens,
|
|
49
|
+
},
|
|
33
50
|
};
|
|
34
51
|
return {
|
|
35
52
|
chunk: new AIMessageChunk({
|
|
@@ -150,6 +167,27 @@ function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
|
150
167
|
};
|
|
151
168
|
}
|
|
152
169
|
}
|
|
170
|
+
else if (data.type === "content_block_start" &&
|
|
171
|
+
data.content_block.type === "redacted_thinking") {
|
|
172
|
+
return {
|
|
173
|
+
chunk: new AIMessageChunk({
|
|
174
|
+
content: fields.coerceContentToString
|
|
175
|
+
? ""
|
|
176
|
+
: [{ index: data.index, ...data.content_block }],
|
|
177
|
+
}),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
else if (data.type === "content_block_start" &&
|
|
181
|
+
data.content_block.type === "thinking") {
|
|
182
|
+
const content = data.content_block.thinking;
|
|
183
|
+
return {
|
|
184
|
+
chunk: new AIMessageChunk({
|
|
185
|
+
content: fields.coerceContentToString
|
|
186
|
+
? content
|
|
187
|
+
: [{ index: data.index, ...data.content_block }],
|
|
188
|
+
}),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
153
191
|
return null;
|
|
154
192
|
}
|
|
155
193
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message_outputs.mjs","sources":["../../../../../src/llm/anthropic/utils/message_outputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting Anthropic messages to LangChain messages.\n */\nimport Anthropic from '@anthropic-ai/sdk';\nimport {\n AIMessage,\n UsageMetadata,\n AIMessageChunk,\n} from '@langchain/core/messages';\nimport type { ToolCallChunk } from \"@langchain/core/messages/tool\";\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport { ChatGeneration } from '@langchain/core/outputs';\nimport { AnthropicMessageResponse } from '../types.js';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function extractToolCalls(content: Record<string, any>[]): ToolCall[] {\n const toolCalls: ToolCall[] = [];\n for (const block of content) {\n if (block.type === 'tool_use') {\n toolCalls.push({\n name: block.name,\n args: block.input,\n id: block.id,\n type: 'tool_call',\n });\n }\n }\n return toolCalls;\n}\n\nexport function _makeMessageChunkFromAnthropicEvent(\n data: Anthropic.Messages.RawMessageStreamEvent,\n fields: {\n streamUsage: boolean;\n coerceContentToString: boolean;\n }\n): {\n chunk: AIMessageChunk;\n} | null {\n if (data.type === 'message_start') {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { content, usage, ...additionalKwargs } = data.message;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const filteredAdditionalKwargs: Record<string, any> = {};\n for (const [key, value] of Object.entries(additionalKwargs)) {\n if (value !== undefined && value !== null) {\n filteredAdditionalKwargs[key] = value;\n }\n }\n const usageMetadata: UsageMetadata = {\n input_tokens: usage.input_tokens,\n output_tokens: usage.output_tokens,\n total_tokens: usage.input_tokens + usage.output_tokens,\n };\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString ? '' : [],\n additional_kwargs: filteredAdditionalKwargs,\n usage_metadata: fields.streamUsage ? usageMetadata : undefined,\n id: data.message.id,\n }),\n };\n } else if (data.type === 'message_delta') {\n const usageMetadata: UsageMetadata = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString ? '' : [],\n additional_kwargs: { ...data.delta },\n usage_metadata: fields.streamUsage ? usageMetadata : undefined,\n }),\n };\n } else if (\n data.type === \"content_block_start\" &&\n [\"tool_use\", \"document\"].includes(data.content_block.type)\n ) {\n const contentBlock = data.content_block;\n let toolCallChunks: ToolCallChunk[];\n if (contentBlock.type === \"tool_use\") {\n toolCallChunks = [\n {\n id: contentBlock.id,\n index: data.index,\n name: contentBlock.name,\n args: \"\",\n },\n ];\n } else {\n toolCallChunks = [];\n }\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? \"\"\n : [\n {\n index: data.index,\n ...data.content_block,\n input: \"\",\n },\n ],\n additional_kwargs: {},\n tool_call_chunks: toolCallChunks,\n }),\n };\n } else if (\n data.type === \"content_block_delta\" &&\n [\n \"text_delta\",\n \"citations_delta\",\n \"thinking_delta\",\n \"signature_delta\",\n ].includes(data.delta.type)\n ) {\n if (fields.coerceContentToString && \"text\" in data.delta) {\n return {\n chunk: new AIMessageChunk({\n content: data.delta.text,\n }),\n };\n } else {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const contentBlock: Record<string, any> = data.delta;\n if (\"citation\" in contentBlock) {\n contentBlock.citations = [contentBlock.citation];\n delete contentBlock.citation;\n }\n if (\n contentBlock.type === \"thinking_delta\" ||\n contentBlock.type === \"signature_delta\"\n ) {\n return {\n chunk: new AIMessageChunk({\n content: [{ index: data.index, ...contentBlock, type: \"thinking\" }],\n }),\n };\n }\n\n return {\n chunk: new AIMessageChunk({\n content: [{ index: data.index, ...contentBlock, type: \"text\" }],\n }),\n };\n }\n } else if (\n data.type === \"content_block_delta\" &&\n data.delta.type === \"input_json_delta\"\n ) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? ''\n : [\n {\n index: data.index,\n input: data.delta.partial_json,\n type: data.delta.type,\n },\n ],\n additional_kwargs: {},\n tool_call_chunks: [\n {\n index: data.index,\n args: data.delta.partial_json,\n },\n ],\n }),\n };\n } else if (\n data.type === 'content_block_start' &&\n data.content_block.type === 'text'\n ) {\n const content = data.content_block.text;\n if (content !== undefined) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? content\n : [\n {\n index: data.index,\n ...data.content_block,\n },\n ],\n additional_kwargs: {},\n }),\n };\n }\n }\n\n return null;\n}\n\nexport function anthropicResponseToChatMessages(\n messages: AnthropicMessageResponse[],\n additionalKwargs: Record<string, unknown>\n): ChatGeneration[] {\n const usage: Record<string, number> | null | undefined =\n additionalKwargs.usage as Record<string, number> | null | undefined;\n const usageMetadata =\n usage != null\n ? {\n input_tokens: usage.input_tokens ?? 0,\n output_tokens: usage.output_tokens ?? 0,\n total_tokens: (usage.input_tokens ?? 0) + (usage.output_tokens ?? 0),\n }\n : undefined;\n if (messages.length === 1 && messages[0].type === 'text') {\n return [\n {\n text: messages[0].text,\n message: new AIMessage({\n content: messages[0].text,\n additional_kwargs: additionalKwargs,\n usage_metadata: usageMetadata,\n response_metadata: additionalKwargs,\n id: additionalKwargs.id as string,\n }),\n },\n ];\n } else {\n const toolCalls = extractToolCalls(messages);\n const generations: ChatGeneration[] = [\n {\n text: '',\n message: new AIMessage({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n content: messages as any,\n additional_kwargs: additionalKwargs,\n tool_calls: toolCalls,\n usage_metadata: usageMetadata,\n response_metadata: additionalKwargs,\n id: additionalKwargs.id as string,\n }),\n },\n ];\n return generations;\n }\n}"],"names":[],"mappings":";;AA8BgB,SAAA,mCAAmC,CACjD,IAA8C,EAC9C,MAGC,EAAA;AAID,IAAA,IAAI,IAAI,CAAC,IAAI,KAAK,eAAe,EAAE;;AAEjC,QAAA,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,gBAAgB,EAAE,GAAG,IAAI,CAAC,OAAO;;QAE5D,MAAM,wBAAwB,GAAwB,EAAE;AACxD,QAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE;YAC3D,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE;AACzC,gBAAA,wBAAwB,CAAC,GAAG,CAAC,GAAG,KAAK;;;AAGzC,QAAA,MAAM,aAAa,GAAkB;YACnC,YAAY,EAAE,KAAK,CAAC,YAAY;YAChC,aAAa,EAAE,KAAK,CAAC,aAAa;AAClC,YAAA,YAAY,EAAE,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,aAAa;SACvD;QACD,OAAO;YACL,KAAK,EAAE,IAAI,cAAc,CAAC;gBACxB,OAAO,EAAE,MAAM,CAAC,qBAAqB,GAAG,EAAE,GAAG,EAAE;AAC/C,gBAAA,iBAAiB,EAAE,wBAAwB;gBAC3C,cAAc,EAAE,MAAM,CAAC,WAAW,GAAG,aAAa,GAAG,SAAS;AAC9D,gBAAA,EA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BAAqB;AACnC,QAAA,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,kBAAkB,EACtC;QACA,OAAO;YACL,KAAK,EAAE,IAAI,cAAc,CAAC;gBACxB,OAAO,EAAE,MAAM,CAAC;AACd,sBAAE;AACF,sBAAE;AACA,wBAAA;4BACE,KAAK,EAAE,IAAI,CAAC,KAAK;AACjB,4BAAA,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY;AAC9B,4BAAA,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI;AACtB,yBAAA;AACF,qBAAA;AACH,gBAAA,iBAAiB,EAAE,EAAE;AACrB,gBAAA,gBAAgB,EAAE;AAChB,oBAAA;wBACE,KAAK,EAAE,IAAI,CAAC,KAAK;AACjB,wBAAA,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY;AAC9B,qBAAA;AACF,iBAAA;aACF,CAAC;SACH;;AACI,SAAA,IACL,IAAI,CAAC,IAAI,KAAK,qBAAqB;AACnC,QAAA,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,MAAM,EAClC;AACA,QAAA,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI;AACvC,QAAA,IAAI,OAAO,KAAK,SAAS,EAAE;YACzB,OAAO;gBACL,KAAK,EAAE,IAAI,cAAc,CAAC;oBACxB,OAAO,EAAE,MAAM,CAAC;AACd,0BAAE;AACF,0BAAE;AACA,4BAAA;gCACE,KAAK,EAAE,IAAI,CAAC,KAAK;gCACjB,GAAG,IAAI,CAAC,aAAa;AACtB,6BAAA;AACF,yBAAA;AACH,oBAAA,iBAAiB,EAAE,EAAE;iBACtB,CAAC;aACH;;;AAIL,IAAA,OAAO,IAAI;AACb;;;;"}
|
|
1
|
+
{"version":3,"file":"message_outputs.mjs","sources":["../../../../../src/llm/anthropic/utils/message_outputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting Anthropic messages to LangChain messages.\n */\nimport Anthropic from '@anthropic-ai/sdk';\nimport {\n AIMessage,\n UsageMetadata,\n AIMessageChunk,\n} from '@langchain/core/messages';\nimport type { ToolCallChunk } from \"@langchain/core/messages/tool\";\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport { ChatGeneration } from '@langchain/core/outputs';\nimport { AnthropicMessageResponse } from '../types.js';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function extractToolCalls(content: Record<string, any>[]): ToolCall[] {\n const toolCalls: ToolCall[] = [];\n for (const block of content) {\n if (block.type === 'tool_use') {\n toolCalls.push({\n name: block.name,\n args: block.input,\n id: block.id,\n type: 'tool_call',\n });\n }\n }\n return toolCalls;\n}\n\nexport function _makeMessageChunkFromAnthropicEvent(\n data: Anthropic.Messages.RawMessageStreamEvent,\n fields: {\n streamUsage: boolean;\n coerceContentToString: boolean;\n }\n): {\n chunk: AIMessageChunk;\n} | null {\n if (data.type === 'message_start') {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { content, usage, ...additionalKwargs } = data.message;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const filteredAdditionalKwargs: Record<string, any> = {};\n for (const [key, value] of Object.entries(additionalKwargs)) {\n if (value !== undefined && value !== null) {\n filteredAdditionalKwargs[key] = value;\n }\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const { input_tokens, output_tokens, ...rest }: Record<string, any> =\n usage ?? {};\n const usageMetadata: UsageMetadata = {\n input_tokens,\n output_tokens,\n total_tokens: input_tokens + output_tokens,\n input_token_details: {\n cache_creation: rest.cache_creation_input_tokens,\n cache_read: rest.cache_read_input_tokens,\n },\n };\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString ? '' : [],\n additional_kwargs: filteredAdditionalKwargs,\n usage_metadata: fields.streamUsage ? usageMetadata : undefined,\n response_metadata: {\n usage: {\n ...rest,\n },\n },\n id: data.message.id,\n }),\n };\n } else if (data.type === 'message_delta') {\n const usageMetadata: UsageMetadata = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n input_token_details: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n cache_creation: (data.usage as any).cache_creation_input_tokens,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n cache_read: (data.usage as any).cache_read_input_tokens,\n },\n };\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString ? '' : [],\n additional_kwargs: { ...data.delta },\n usage_metadata: fields.streamUsage ? usageMetadata : undefined,\n }),\n };\n } else if (\n data.type === \"content_block_start\" &&\n [\"tool_use\", \"document\"].includes(data.content_block.type)\n ) {\n const contentBlock = data.content_block;\n let toolCallChunks: ToolCallChunk[];\n if (contentBlock.type === \"tool_use\") {\n toolCallChunks = [\n {\n id: contentBlock.id,\n index: data.index,\n name: contentBlock.name,\n args: \"\",\n },\n ];\n } else {\n toolCallChunks = [];\n }\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? \"\"\n : [\n {\n index: data.index,\n ...data.content_block,\n input: \"\",\n },\n ],\n additional_kwargs: {},\n tool_call_chunks: toolCallChunks,\n }),\n };\n } else if (\n data.type === \"content_block_delta\" &&\n [\n \"text_delta\",\n \"citations_delta\",\n \"thinking_delta\",\n \"signature_delta\",\n ].includes(data.delta.type)\n ) {\n if (fields.coerceContentToString && \"text\" in data.delta) {\n return {\n chunk: new AIMessageChunk({\n content: data.delta.text,\n }),\n };\n } else {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const contentBlock: Record<string, any> = data.delta;\n if (\"citation\" in contentBlock) {\n contentBlock.citations = [contentBlock.citation];\n delete contentBlock.citation;\n }\n if (\n contentBlock.type === \"thinking_delta\" ||\n contentBlock.type === \"signature_delta\"\n ) {\n return {\n chunk: new AIMessageChunk({\n content: [{ index: data.index, ...contentBlock, type: \"thinking\" }],\n }),\n };\n }\n\n return {\n chunk: new AIMessageChunk({\n content: [{ index: data.index, ...contentBlock, type: \"text\" }],\n }),\n };\n }\n } else if (\n data.type === \"content_block_delta\" &&\n data.delta.type === \"input_json_delta\"\n ) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? ''\n : [\n {\n index: data.index,\n input: data.delta.partial_json,\n type: data.delta.type,\n },\n ],\n additional_kwargs: {},\n tool_call_chunks: [\n {\n index: data.index,\n args: data.delta.partial_json,\n },\n ],\n }),\n };\n } else if (\n data.type === 'content_block_start' &&\n data.content_block.type === 'text'\n ) {\n const content = data.content_block.text;\n if (content !== undefined) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? content\n : [\n {\n index: data.index,\n ...data.content_block,\n },\n ],\n additional_kwargs: {},\n }),\n };\n }\n } else if (\n data.type === \"content_block_start\" &&\n data.content_block.type === \"redacted_thinking\"\n ) {\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? \"\"\n : [{ index: data.index, ...data.content_block }],\n }),\n };\n } else if (\n data.type === \"content_block_start\" &&\n data.content_block.type === \"thinking\"\n ) {\n const content = data.content_block.thinking;\n return {\n chunk: new AIMessageChunk({\n content: fields.coerceContentToString\n ? content\n : [{ index: data.index, ...data.content_block }],\n }),\n };\n }\n\n return null;\n}\n\nexport function anthropicResponseToChatMessages(\n messages: AnthropicMessageResponse[],\n additionalKwargs: Record<string, unknown>\n): ChatGeneration[] {\n const usage: Record<string, number> | null | undefined =\n additionalKwargs.usage as Record<string, number> | null | undefined;\n const usageMetadata =\n usage != null\n ? {\n input_tokens: usage.input_tokens ?? 0,\n output_tokens: usage.output_tokens ?? 0,\n total_tokens: (usage.input_tokens ?? 0) + (usage.output_tokens ?? 0),\n input_token_details: {\n cache_creation: usage.cache_creation_input_tokens,\n cache_read: usage.cache_read_input_tokens,\n },\n }\n : undefined;\n if (messages.length === 1 && messages[0].type === 'text') {\n return [\n {\n text: messages[0].text,\n message: new AIMessage({\n content: messages[0].text,\n additional_kwargs: additionalKwargs,\n usage_metadata: usageMetadata,\n response_metadata: additionalKwargs,\n id: additionalKwargs.id as string,\n }),\n },\n ];\n } else {\n const toolCalls = extractToolCalls(messages);\n const generations: ChatGeneration[] = [\n {\n text: '',\n message: new AIMessage({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n content: messages as any,\n additional_kwargs: additionalKwargs,\n tool_calls: toolCalls,\n usage_metadata: usageMetadata,\n response_metadata: additionalKwargs,\n id: additionalKwargs.id as string,\n }),\n },\n ];\n return generations;\n }\n}"],"names":[],"mappings":";;AA8BgB,SAAA,mCAAmC,CACjD,IAA8C,EAC9C,MAGC,EAAA;AAID,IAAA,IAAI,IAAI,CAAC,IAAI,KAAK,eAAe,EAAE;;AAEjC,QAAA,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,gBAAgB,EAAE,GAAG,IAAI,CAAC,OAAO;;QAE5D,MAAM,wBAAwB,GAAwB,EAAE;AACxD,QAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE;YAC3D,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE;AACzC,gBAAA,wBAAwB,CAAC,GAAG,CAAC,GAAG,KAAK;;;;AAIzC,QAAA,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,GAAG,IAAI,EAAE,GAC5C,KAAK,IAAI,EAAE;AACb,QAAA,MAAM,aAAa,GAAkB;YACnC,YAAY;YACZ,aAAa;YACb,YAAY,EAAE,YAAY,GAAG,aAAa;AAC1C,YAAA,mBAAmB,EAAE;gBACnB,cAAc,EAAE,IAAI,CAAC,2BAA2B;gBAChD,UAAU,EAAE,IAAI,CAAC,uBAAuB;AACzC,aAAA;SACF;QACD,OAAO;YACL,KAAK,EAAE,IAAI,cAAc,CAAC;gBACxB,OAAO,EAAE,MAAM,CAAC,qBAAqB,GAAG,EAAE,GAAG,EAAE;AAC/C,gBAAA,iBAAiB,EAAE,wBAAwB;gBAC3C,cAAc,EAAE,MAAM,CAAC,WAAW,GAAG,aAAa,GAAG,SAAS;AAC9D,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AAA;AACF,yBAAA;AACH,oBAAA,iBAAiB,EAAE,EAAE;iBACtB,CAAC;aACH;;;AAEE,SAAA,IACL,IAAI,CAAC,IAAI,KAAK,qBAAqB;AACnC,QAAA,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,mBAAmB,EAC/C;QACA,OAAO;YACL,KAAK,EAAE,IAAI,cAAc,CAAC;gBACxB,OAAO,EAAE,MAAM,CAAC;AACd,sBAAE;AACF,sBAAE,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;aACnD,CAAC;SACH;;AACI,SAAA,IACL,IAAI,CAAC,IAAI,KAAK,qBAAqB;AACnC,QAAA,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,UAAU,EACtC;AACA,QAAA,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ;QAC3C,OAAO;YACL,KAAK,EAAE,IAAI,cAAc,CAAC;gBACxB,OAAO,EAAE,MAAM,CAAC;AACd,sBAAE;AACF,sBAAE,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;aACnD,CAAC;SACH;;AAGH,IAAA,OAAO,IAAI;AACb;;;;"}
|
package/dist/esm/stream.mjs
CHANGED
|
@@ -196,6 +196,14 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
196
196
|
content,
|
|
197
197
|
});
|
|
198
198
|
}
|
|
199
|
+
else if (content.every((c) => c.type?.startsWith(ContentTypes.THINKING))) {
|
|
200
|
+
graph.dispatchReasoningDelta(stepId, {
|
|
201
|
+
content: content.map((c) => ({
|
|
202
|
+
type: ContentTypes.THINK,
|
|
203
|
+
think: c.thinking,
|
|
204
|
+
}))
|
|
205
|
+
});
|
|
206
|
+
}
|
|
199
207
|
}
|
|
200
208
|
handleToolCallChunks = ({ graph, stepKey, toolCallChunks, }) => {
|
|
201
209
|
const prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
|
|
@@ -244,8 +252,11 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
244
252
|
});
|
|
245
253
|
};
|
|
246
254
|
handleReasoning(chunk, graph) {
|
|
247
|
-
|
|
248
|
-
if (
|
|
255
|
+
let reasoning_content = chunk.additional_kwargs?.[graph.reasoningKey];
|
|
256
|
+
if (Array.isArray(chunk.content) && chunk.content[0]?.type === 'thinking') {
|
|
257
|
+
reasoning_content = 'valid';
|
|
258
|
+
}
|
|
259
|
+
if (reasoning_content != null && reasoning_content && (chunk.content == null || chunk.content === '' || reasoning_content === 'valid')) {
|
|
249
260
|
graph.currentTokenType = ContentTypes.THINK;
|
|
250
261
|
graph.tokenTypeSwitch = 'reasoning';
|
|
251
262
|
return;
|
package/dist/esm/stream.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.mjs","sources":["../../src/stream.ts"],"sourcesContent":["// src/stream.ts\nimport { nanoid } from 'nanoid';\nimport type { AIMessageChunk } from '@langchain/core/messages';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { Graph } from '@/graphs';\nimport type * as t from '@/types';\nimport { StepTypes, ContentTypes, GraphEvents, ToolCallTypes } from '@/common';\n\nfunction getNonEmptyValue(possibleValues: string[]): string | undefined {\n for (const value of possibleValues) {\n if (value && value.trim() !== '') {\n return value;\n }\n }\n return undefined;\n}\n\nexport const getMessageId = (stepKey: string, graph: Graph<t.BaseGraphState>, returnExistingId = false): string | undefined => {\n const messageId = graph.messageIdsByStepKey.get(stepKey);\n if (messageId != null && messageId) {\n return returnExistingId ? messageId : undefined;\n }\n\n const prelimMessageId = graph.prelimMessageIdsByStepKey.get(stepKey);\n if (prelimMessageId != null && prelimMessageId) {\n graph.prelimMessageIdsByStepKey.delete(stepKey);\n graph.messageIdsByStepKey.set(stepKey, prelimMessageId);\n return prelimMessageId;\n }\n\n const message_id = `msg_${nanoid()}`;\n graph.messageIdsByStepKey.set(stepKey, message_id);\n return message_id;\n};\n\nexport const handleToolCalls = (toolCalls?: ToolCall[], metadata?: Record<string, unknown>, graph?: Graph): 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 tool_calls: ToolCall[] = [];\n const tool_call_ids: string[] = [];\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 tool_calls.push(tool_call);\n tool_call_ids.push(toolCallId);\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n let prevStepId = '';\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch (e) {\n // no previous step\n }\n\n const dispatchToolCallIds = (lastMessageStepId: string): void => {\n graph.dispatchMessageDelta(lastMessageStepId, {\n content: [{\n type: 'text',\n text: '',\n tool_call_ids,\n }],\n });\n };\n /* If the previous step exists and is a message creation */\n if (prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION) {\n 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 (!prevRunStep || prevRunStep.type !== StepTypes.MESSAGE_CREATION) {\n const messageId = getMessageId(stepKey, graph, true) ?? '';\n const stepId = graph.dispatchRunStep(stepKey, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: messageId,\n },\n });\n dispatchToolCallIds(stepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n }\n graph.dispatchRunStep(stepKey, {\n type: StepTypes.TOOL_CALLS,\n tool_calls,\n });\n};\n\nexport class ChatModelStreamHandler implements t.EventHandler {\n handle(event: string, data: t.StreamEventData, metadata?: Record<string, unknown>, graph?: Graph): void {\n if (!graph) {\n throw new Error('Graph not found');\n }\n if (!graph.config) {\n throw new Error('Config not found in graph');\n }\n if (!data.chunk) {\n console.warn(`No chunk found in ${event} event`);\n return;\n }\n\n const chunk = data.chunk as Partial<AIMessageChunk>;\n const content = (chunk.additional_kwargs?.[graph.reasoningKey] as string | undefined) ?? chunk.content;\n this.handleReasoning(chunk, graph);\n\n let hasToolCalls = false;\n if (chunk.tool_calls && chunk.tool_calls.length > 0 && chunk.tool_calls.every((tc) => tc.id)) {\n hasToolCalls = true;\n handleToolCalls(chunk.tool_calls, metadata, graph);\n }\n\n const hasToolCallChunks = (chunk.tool_call_chunks && chunk.tool_call_chunks.length > 0) ?? false;\n const isEmptyContent = typeof content === 'undefined' || !content.length || typeof content === 'string' && !content;\n const isEmptyChunk = isEmptyContent && !hasToolCallChunks;\n const chunkId = chunk.id ?? '';\n if (isEmptyChunk && chunkId && chunkId.startsWith('msg')) {\n if (graph.messageIdsByStepKey.has(chunkId)) {\n return;\n } else if (graph.prelimMessageIdsByStepKey.has(chunkId)) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n graph.prelimMessageIdsByStepKey.set(stepKey, chunkId);\n return;\n } else if (isEmptyChunk) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n if (hasToolCallChunks\n && chunk.tool_call_chunks\n && chunk.tool_call_chunks.length\n && typeof chunk.tool_call_chunks[0]?.index === 'number') {\n this.handleToolCallChunks({ graph, stepKey, toolCallChunks: chunk.tool_call_chunks });\n }\n\n if (isEmptyContent) {\n return;\n }\n\n const message_id = getMessageId(stepKey, graph) ?? '';\n if (message_id) {\n graph.dispatchRunStep(stepKey, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n });\n }\n\n const stepId = graph.getStepIdByKey(stepKey);\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n // eslint-disable-next-line no-console\n console.warn(`\\n\n==============================================================\n\n\nRun step for ${stepId} does not exist, cannot dispatch delta event.\n\nevent: ${event}\nstepId: ${stepId}\nstepKey: ${stepKey}\nmessage_id: ${message_id}\nhasToolCalls: ${hasToolCalls}\nhasToolCallChunks: ${hasToolCallChunks}\n\n==============================================================\n\\n`);\n return;\n }\n\n /* Note: tool call chunks may have non-empty content that matches the current tool chunk generation */\n if (typeof content === 'string' && runStep.type === StepTypes.TOOL_CALLS) {\n return;\n } else if (hasToolCallChunks && (chunk.tool_call_chunks?.some((tc) => tc.args === content) ?? false)) {\n return;\n } else if (typeof content === 'string') {\n if (graph.currentTokenType === ContentTypes.TEXT) {\n graph.dispatchMessageDelta(stepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: content,\n }],\n });\n } else {\n graph.dispatchReasoningDelta(stepId, {\n content: [{\n type: ContentTypes.THINK,\n think: content,\n }],\n });\n }\n } else if (content.every((c) => c.type?.startsWith(ContentTypes.TEXT))) {\n graph.dispatchMessageDelta(stepId, {\n content,\n });\n }\n }\n handleToolCallChunks = ({\n graph,\n stepKey,\n toolCallChunks,\n }: {\n graph: Graph;\n stepKey: string;\n toolCallChunks: ToolCallChunk[],\n }): void => {\n const prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);\n const prevRunStep = graph.getRunStep(prevStepId);\n const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);\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 /** 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 (tool_calls != null && toolCallChunk.id != null && toolCallChunk.name != null) {\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 = prevRunStep?.type === StepTypes.MESSAGE_CREATION && graph.messageStepHasToolCalls.has(prevStepId);\n if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {\n graph.dispatchMessageDelta(prevStepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: '',\n tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),\n }],\n });\n graph.messageStepHasToolCalls.set(prevStepId, true);\n stepId = graph.dispatchRunStep(stepKey, {\n type: StepTypes.TOOL_CALLS,\n tool_calls,\n });\n }\n graph.dispatchRunStepDelta(stepId, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: toolCallChunks,\n });\n };\n handleReasoning(chunk: Partial<AIMessageChunk>, graph: Graph): void {\n const reasoning_content = chunk.additional_kwargs?.[graph.reasoningKey] as string | undefined;\n if (reasoning_content != null && reasoning_content && (chunk.content == null || chunk.content === '')) {\n graph.currentTokenType = ContentTypes.THINK;\n graph.tokenTypeSwitch = 'reasoning';\n return;\n } else if (graph.tokenTypeSwitch === 'reasoning' && graph.currentTokenType !== ContentTypes.TEXT && chunk.content != null && chunk.content !== '') {\n graph.currentTokenType = ContentTypes.TEXT;\n graph.tokenTypeSwitch = 'content';\n } else if (chunk.content != null && typeof chunk.content === 'string' && chunk.content.includes('<think>')) {\n graph.currentTokenType = ContentTypes.THINK;\n graph.tokenTypeSwitch = 'content';\n } else if (graph.lastToken != null && graph.lastToken.includes('</think>')) {\n graph.currentTokenType = ContentTypes.TEXT;\n graph.tokenTypeSwitch = 'content';\n }\n if (typeof chunk.content !== 'string') {\n return;\n }\n graph.lastToken = chunk.content;\n }\n}\n\nexport function createContentAggregator(): t.ContentAggregatorResult {\n const contentParts: Array<t.MessageContentComplex | undefined> = [];\n const stepMap = new Map<string, t.RunStep>();\n const toolCallIdMap = new Map<string, string>();\n\n const updateContent = (\n index: number,\n contentPart: t.MessageContentComplex,\n finalUpdate = false,\n ): void => {\n const partType = contentPart.type ?? '';\n if (!partType) {\n console.warn('No content type found in content part');\n return;\n }\n\n if (!contentParts[index]) {\n contentParts[index] = { type: partType };\n }\n\n if (!partType.startsWith(contentParts[index]?.type ?? '')) {\n console.warn('Content type mismatch');\n return;\n }\n\n if (\n partType.startsWith(ContentTypes.TEXT) &&\n ContentTypes.TEXT in contentPart &&\n typeof contentPart.text === 'string'\n ) {\n // TODO: update this!!\n const currentContent = contentParts[index] as t.MessageDeltaUpdate;\n const update: t.MessageDeltaUpdate = {\n type: ContentTypes.TEXT,\n text: (currentContent.text || '') + contentPart.text,\n };\n\n if (contentPart.tool_call_ids) {\n update.tool_call_ids = contentPart.tool_call_ids;\n }\n contentParts[index] = update;\n } else if (\n partType.startsWith(ContentTypes.THINK) &&\n ContentTypes.THINK in contentPart &&\n typeof contentPart.think === 'string'\n ) {\n const currentContent = contentParts[index] as t.ReasoningDeltaUpdate;\n const update: t.ReasoningDeltaUpdate = {\n type: ContentTypes.THINK,\n think: (currentContent.think || '') + contentPart.think,\n };\n contentParts[index] = update;\n } else if (partType === ContentTypes.IMAGE_URL && 'image_url' in contentPart) {\n const currentContent = contentParts[index] as { type: 'image_url'; image_url: string };\n contentParts[index] = {\n ...currentContent,\n };\n } else if (partType === ContentTypes.TOOL_CALL && 'tool_call' in contentPart) {\n const existingContent = contentParts[index] as Omit<t.ToolCallContent, 'tool_call'> & { tool_call?: ToolCall } | undefined;\n\n const args = finalUpdate\n ? contentPart.tool_call.args\n : (existingContent?.tool_call?.args || '') + (contentPart.tool_call.args ?? '');\n\n const id = getNonEmptyValue([contentPart.tool_call.id, existingContent?.tool_call?.id]) ?? '';\n const name =\n getNonEmptyValue([contentPart.tool_call.name, existingContent?.tool_call?.name]) ?? '';\n\n const newToolCall: ToolCall & t.PartMetadata = {\n id,\n name,\n args,\n type: ToolCallTypes.TOOL_CALL,\n };\n\n if (finalUpdate) {\n newToolCall.progress = 1;\n newToolCall.output = contentPart.tool_call.output;\n }\n\n contentParts[index] = {\n type: ContentTypes.TOOL_CALL,\n tool_call: newToolCall,\n };\n }\n };\n\n const aggregateContent = ({ event, data }: {\n event: GraphEvents;\n data: t.RunStep | t.MessageDeltaEvent | t.RunStepDeltaEvent | { result: t.ToolEndEvent };\n }): void => {\n\n if (event === GraphEvents.ON_RUN_STEP) {\n const runStep = data as t.RunStep;\n stepMap.set(runStep.id, runStep);\n\n // Store tool call IDs if present\n if (runStep.stepDetails.type === StepTypes.TOOL_CALLS && runStep.stepDetails.tool_calls) {\n runStep.stepDetails.tool_calls.forEach((toolCall) => {\n const toolCallId = toolCall.id ?? '';\n if ('id' in toolCall && toolCallId) {\n toolCallIdMap.set(runStep.id, toolCallId);\n }\n });\n }\n } else if (event === GraphEvents.ON_MESSAGE_DELTA) {\n const messageDelta = data as t.MessageDeltaEvent;\n const runStep = stepMap.get(messageDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for message delta event');\n return;\n }\n\n if (messageDelta.delta.content) {\n const contentPart = Array.isArray(messageDelta.delta.content)\n ? messageDelta.delta.content[0]\n : messageDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (event === GraphEvents.ON_REASONING_DELTA) {\n const reasoningDelta = data as t.ReasoningDeltaEvent;\n const runStep = stepMap.get(reasoningDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for reasoning delta event');\n return;\n }\n\n if (reasoningDelta.delta.content) {\n const contentPart = Array.isArray(reasoningDelta.delta.content)\n ? reasoningDelta.delta.content[0]\n : reasoningDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (event === GraphEvents.ON_RUN_STEP_DELTA) {\n const runStepDelta = data as t.RunStepDeltaEvent;\n const runStep = stepMap.get(runStepDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for run step delta event');\n return;\n }\n\n if (\n runStepDelta.delta.type === StepTypes.TOOL_CALLS &&\n runStepDelta.delta.tool_calls\n ) {\n\n runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {\n const toolCallId = toolCallIdMap.get(runStepDelta.id);\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: toolCallDelta.args ?? '',\n name: toolCallDelta.name,\n id: toolCallId,\n },\n };\n\n updateContent(runStep.index, contentPart);\n });\n }\n } else if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {\n const { result } = data as unknown as { result: t.ToolEndEvent };\n\n const { id: stepId } = result;\n\n const runStep = stepMap.get(stepId);\n if (!runStep) {\n console.warn('No run step or runId found for completed tool call event');\n return;\n }\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: result.tool_call,\n };\n\n updateContent(runStep.index, contentPart, true);\n }\n\n };\n\n return { contentParts, aggregateContent, stepMap };\n}\n"],"names":[],"mappings":";;;AAAA;AAQA,SAAS,gBAAgB,CAAC,cAAwB,EAAA;AAChD,IAAA,KAAK,MAAM,KAAK,IAAI,cAAc,EAAE;QAClC,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;AAChC,YAAA,OAAO,KAAK;;;AAGhB,IAAA,OAAO,SAAS;AAClB;AAEO,MAAM,YAAY,GAAG,CAAC,OAAe,EAAE,KAA8B,EAAE,gBAAgB,GAAG,KAAK,KAAwB;IAC5H,MAAM,SAAS,GAAG,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC;AACxD,IAAA,IAAI,SAAS,IAAI,IAAI,IAAI,SAAS,EAAE;QAClC,OAAO,gBAAgB,GAAG,SAAS,GAAG,SAAS;;IAGjD,MAAM,eAAe,GAAG,KAAK,CAAC,yBAAyB,CAAC,GAAG,CAAC,OAAO,CAAC;AACpE,IAAA,IAAI,eAAe,IAAI,IAAI,IAAI,eAAe,EAAE;AAC9C,QAAA,KAAK,CAAC,yBAAyB,CAAC,MAAM,CAAC,OAAO,CAAC;QAC/C,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,eAAe,CAAC;AACvD,QAAA,OAAO,eAAe;;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AAC;AAED,IAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAE;AACpD;;;;"}
|
|
1
|
+
{"version":3,"file":"stream.mjs","sources":["../../src/stream.ts"],"sourcesContent":["// src/stream.ts\nimport { nanoid } from 'nanoid';\nimport type { AIMessageChunk } from '@langchain/core/messages';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { Graph } from '@/graphs';\nimport type * as t from '@/types';\nimport { StepTypes, ContentTypes, GraphEvents, ToolCallTypes } from '@/common';\n\nfunction getNonEmptyValue(possibleValues: string[]): string | undefined {\n for (const value of possibleValues) {\n if (value && value.trim() !== '') {\n return value;\n }\n }\n return undefined;\n}\n\nexport const getMessageId = (stepKey: string, graph: Graph<t.BaseGraphState>, returnExistingId = false): string | undefined => {\n const messageId = graph.messageIdsByStepKey.get(stepKey);\n if (messageId != null && messageId) {\n return returnExistingId ? messageId : undefined;\n }\n\n const prelimMessageId = graph.prelimMessageIdsByStepKey.get(stepKey);\n if (prelimMessageId != null && prelimMessageId) {\n graph.prelimMessageIdsByStepKey.delete(stepKey);\n graph.messageIdsByStepKey.set(stepKey, prelimMessageId);\n return prelimMessageId;\n }\n\n const message_id = `msg_${nanoid()}`;\n graph.messageIdsByStepKey.set(stepKey, message_id);\n return message_id;\n};\n\nexport const handleToolCalls = (toolCalls?: ToolCall[], metadata?: Record<string, unknown>, graph?: Graph): 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 tool_calls: ToolCall[] = [];\n const tool_call_ids: string[] = [];\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 tool_calls.push(tool_call);\n tool_call_ids.push(toolCallId);\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n let prevStepId = '';\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch (e) {\n // no previous step\n }\n\n const dispatchToolCallIds = (lastMessageStepId: string): void => {\n graph.dispatchMessageDelta(lastMessageStepId, {\n content: [{\n type: 'text',\n text: '',\n tool_call_ids,\n }],\n });\n };\n /* If the previous step exists and is a message creation */\n if (prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION) {\n 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 (!prevRunStep || prevRunStep.type !== StepTypes.MESSAGE_CREATION) {\n const messageId = getMessageId(stepKey, graph, true) ?? '';\n const stepId = graph.dispatchRunStep(stepKey, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: messageId,\n },\n });\n dispatchToolCallIds(stepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n }\n graph.dispatchRunStep(stepKey, {\n type: StepTypes.TOOL_CALLS,\n tool_calls,\n });\n};\n\nexport class ChatModelStreamHandler implements t.EventHandler {\n handle(event: string, data: t.StreamEventData, metadata?: Record<string, unknown>, graph?: Graph): void {\n if (!graph) {\n throw new Error('Graph not found');\n }\n if (!graph.config) {\n throw new Error('Config not found in graph');\n }\n if (!data.chunk) {\n console.warn(`No chunk found in ${event} event`);\n return;\n }\n\n const chunk = data.chunk as Partial<AIMessageChunk>;\n const content = (chunk.additional_kwargs?.[graph.reasoningKey] as string | undefined) ?? chunk.content;\n this.handleReasoning(chunk, graph);\n\n let hasToolCalls = false;\n if (chunk.tool_calls && chunk.tool_calls.length > 0 && chunk.tool_calls.every((tc) => tc.id)) {\n hasToolCalls = true;\n handleToolCalls(chunk.tool_calls, metadata, graph);\n }\n\n const hasToolCallChunks = (chunk.tool_call_chunks && chunk.tool_call_chunks.length > 0) ?? false;\n const isEmptyContent = typeof content === 'undefined' || !content.length || typeof content === 'string' && !content;\n const isEmptyChunk = isEmptyContent && !hasToolCallChunks;\n const chunkId = chunk.id ?? '';\n if (isEmptyChunk && chunkId && chunkId.startsWith('msg')) {\n if (graph.messageIdsByStepKey.has(chunkId)) {\n return;\n } else if (graph.prelimMessageIdsByStepKey.has(chunkId)) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n graph.prelimMessageIdsByStepKey.set(stepKey, chunkId);\n return;\n } else if (isEmptyChunk) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n if (hasToolCallChunks\n && chunk.tool_call_chunks\n && chunk.tool_call_chunks.length\n && typeof chunk.tool_call_chunks[0]?.index === 'number') {\n this.handleToolCallChunks({ graph, stepKey, toolCallChunks: chunk.tool_call_chunks });\n }\n\n if (isEmptyContent) {\n return;\n }\n\n const message_id = getMessageId(stepKey, graph) ?? '';\n if (message_id) {\n graph.dispatchRunStep(stepKey, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n });\n }\n\n const stepId = graph.getStepIdByKey(stepKey);\n const runStep = graph.getRunStep(stepId);\n if (!runStep) {\n // eslint-disable-next-line no-console\n console.warn(`\\n\n==============================================================\n\n\nRun step for ${stepId} does not exist, cannot dispatch delta event.\n\nevent: ${event}\nstepId: ${stepId}\nstepKey: ${stepKey}\nmessage_id: ${message_id}\nhasToolCalls: ${hasToolCalls}\nhasToolCallChunks: ${hasToolCallChunks}\n\n==============================================================\n\\n`);\n return;\n }\n\n /* Note: tool call chunks may have non-empty content that matches the current tool chunk generation */\n if (typeof content === 'string' && runStep.type === StepTypes.TOOL_CALLS) {\n return;\n } else if (hasToolCallChunks && (chunk.tool_call_chunks?.some((tc) => tc.args === content) ?? false)) {\n return;\n } else if (typeof content === 'string') {\n if (graph.currentTokenType === ContentTypes.TEXT) {\n graph.dispatchMessageDelta(stepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: content,\n }],\n });\n } else {\n graph.dispatchReasoningDelta(stepId, {\n content: [{\n type: ContentTypes.THINK,\n think: content,\n }],\n });\n }\n } else if (content.every((c) => c.type?.startsWith(ContentTypes.TEXT))) {\n graph.dispatchMessageDelta(stepId, {\n content,\n });\n } else if (content.every((c) => c.type?.startsWith(ContentTypes.THINKING))) {\n graph.dispatchReasoningDelta(stepId, {\n content: content.map((c) => ({\n type: ContentTypes.THINK,\n think: (c as t.ThinkingContentText).thinking,\n }))});\n }\n }\n handleToolCallChunks = ({\n graph,\n stepKey,\n toolCallChunks,\n }: {\n graph: Graph;\n stepKey: string;\n toolCallChunks: ToolCallChunk[],\n }): void => {\n const prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);\n const prevRunStep = graph.getRunStep(prevStepId);\n const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);\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 /** 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 (tool_calls != null && toolCallChunk.id != null && toolCallChunk.name != null) {\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 = prevRunStep?.type === StepTypes.MESSAGE_CREATION && graph.messageStepHasToolCalls.has(prevStepId);\n if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {\n graph.dispatchMessageDelta(prevStepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: '',\n tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),\n }],\n });\n graph.messageStepHasToolCalls.set(prevStepId, true);\n stepId = graph.dispatchRunStep(stepKey, {\n type: StepTypes.TOOL_CALLS,\n tool_calls,\n });\n }\n graph.dispatchRunStepDelta(stepId, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: toolCallChunks,\n });\n };\n handleReasoning(chunk: Partial<AIMessageChunk>, graph: Graph): void {\n let reasoning_content = chunk.additional_kwargs?.[graph.reasoningKey] as string | undefined;\n if (Array.isArray(chunk.content) && chunk.content[0]?.type === 'thinking') {\n reasoning_content = 'valid';\n }\n if (reasoning_content != null && reasoning_content && (chunk.content == null || chunk.content === '' || reasoning_content === 'valid')) {\n graph.currentTokenType = ContentTypes.THINK;\n graph.tokenTypeSwitch = 'reasoning';\n return;\n } else if (graph.tokenTypeSwitch === 'reasoning' && graph.currentTokenType !== ContentTypes.TEXT && chunk.content != null && chunk.content !== '') {\n graph.currentTokenType = ContentTypes.TEXT;\n graph.tokenTypeSwitch = 'content';\n } else if (chunk.content != null && typeof chunk.content === 'string' && chunk.content.includes('<think>')) {\n graph.currentTokenType = ContentTypes.THINK;\n graph.tokenTypeSwitch = 'content';\n } else if (graph.lastToken != null && graph.lastToken.includes('</think>')) {\n graph.currentTokenType = ContentTypes.TEXT;\n graph.tokenTypeSwitch = 'content';\n }\n if (typeof chunk.content !== 'string') {\n return;\n }\n graph.lastToken = chunk.content;\n }\n}\n\nexport function createContentAggregator(): t.ContentAggregatorResult {\n const contentParts: Array<t.MessageContentComplex | undefined> = [];\n const stepMap = new Map<string, t.RunStep>();\n const toolCallIdMap = new Map<string, string>();\n\n const updateContent = (\n index: number,\n contentPart: t.MessageContentComplex,\n finalUpdate = false,\n ): void => {\n const partType = contentPart.type ?? '';\n if (!partType) {\n console.warn('No content type found in content part');\n return;\n }\n\n if (!contentParts[index]) {\n contentParts[index] = { type: partType };\n }\n\n if (!partType.startsWith(contentParts[index]?.type ?? '')) {\n console.warn('Content type mismatch');\n return;\n }\n\n if (\n partType.startsWith(ContentTypes.TEXT) &&\n ContentTypes.TEXT in contentPart &&\n typeof contentPart.text === 'string'\n ) {\n // TODO: update this!!\n const currentContent = contentParts[index] as t.MessageDeltaUpdate;\n const update: t.MessageDeltaUpdate = {\n type: ContentTypes.TEXT,\n text: (currentContent.text || '') + contentPart.text,\n };\n\n if (contentPart.tool_call_ids) {\n update.tool_call_ids = contentPart.tool_call_ids;\n }\n contentParts[index] = update;\n } else if (\n partType.startsWith(ContentTypes.THINK) &&\n ContentTypes.THINK in contentPart &&\n typeof contentPart.think === 'string'\n ) {\n const currentContent = contentParts[index] as t.ReasoningDeltaUpdate;\n const update: t.ReasoningDeltaUpdate = {\n type: ContentTypes.THINK,\n think: (currentContent.think || '') + contentPart.think,\n };\n contentParts[index] = update;\n } else if (partType === ContentTypes.IMAGE_URL && 'image_url' in contentPart) {\n const currentContent = contentParts[index] as { type: 'image_url'; image_url: string };\n contentParts[index] = {\n ...currentContent,\n };\n } else if (partType === ContentTypes.TOOL_CALL && 'tool_call' in contentPart) {\n const existingContent = contentParts[index] as Omit<t.ToolCallContent, 'tool_call'> & { tool_call?: ToolCall } | undefined;\n\n const args = finalUpdate\n ? contentPart.tool_call.args\n : (existingContent?.tool_call?.args || '') + (contentPart.tool_call.args ?? '');\n\n const id = getNonEmptyValue([contentPart.tool_call.id, existingContent?.tool_call?.id]) ?? '';\n const name =\n getNonEmptyValue([contentPart.tool_call.name, existingContent?.tool_call?.name]) ?? '';\n\n const newToolCall: ToolCall & t.PartMetadata = {\n id,\n name,\n args,\n type: ToolCallTypes.TOOL_CALL,\n };\n\n if (finalUpdate) {\n newToolCall.progress = 1;\n newToolCall.output = contentPart.tool_call.output;\n }\n\n contentParts[index] = {\n type: ContentTypes.TOOL_CALL,\n tool_call: newToolCall,\n };\n }\n };\n\n const aggregateContent = ({ event, data }: {\n event: GraphEvents;\n data: t.RunStep | t.MessageDeltaEvent | t.RunStepDeltaEvent | { result: t.ToolEndEvent };\n }): void => {\n\n if (event === GraphEvents.ON_RUN_STEP) {\n const runStep = data as t.RunStep;\n stepMap.set(runStep.id, runStep);\n\n // Store tool call IDs if present\n if (runStep.stepDetails.type === StepTypes.TOOL_CALLS && runStep.stepDetails.tool_calls) {\n runStep.stepDetails.tool_calls.forEach((toolCall) => {\n const toolCallId = toolCall.id ?? '';\n if ('id' in toolCall && toolCallId) {\n toolCallIdMap.set(runStep.id, toolCallId);\n }\n });\n }\n } else if (event === GraphEvents.ON_MESSAGE_DELTA) {\n const messageDelta = data as t.MessageDeltaEvent;\n const runStep = stepMap.get(messageDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for message delta event');\n return;\n }\n\n if (messageDelta.delta.content) {\n const contentPart = Array.isArray(messageDelta.delta.content)\n ? messageDelta.delta.content[0]\n : messageDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (event === GraphEvents.ON_REASONING_DELTA) {\n const reasoningDelta = data as t.ReasoningDeltaEvent;\n const runStep = stepMap.get(reasoningDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for reasoning delta event');\n return;\n }\n\n if (reasoningDelta.delta.content) {\n const contentPart = Array.isArray(reasoningDelta.delta.content)\n ? reasoningDelta.delta.content[0]\n : reasoningDelta.delta.content;\n\n updateContent(runStep.index, contentPart);\n }\n } else if (event === GraphEvents.ON_RUN_STEP_DELTA) {\n const runStepDelta = data as t.RunStepDeltaEvent;\n const runStep = stepMap.get(runStepDelta.id);\n if (!runStep) {\n console.warn('No run step or runId found for run step delta event');\n return;\n }\n\n if (\n runStepDelta.delta.type === StepTypes.TOOL_CALLS &&\n runStepDelta.delta.tool_calls\n ) {\n\n runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {\n const toolCallId = toolCallIdMap.get(runStepDelta.id);\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: {\n args: toolCallDelta.args ?? '',\n name: toolCallDelta.name,\n id: toolCallId,\n },\n };\n\n updateContent(runStep.index, contentPart);\n });\n }\n } else if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {\n const { result } = data as unknown as { result: t.ToolEndEvent };\n\n const { id: stepId } = result;\n\n const runStep = stepMap.get(stepId);\n if (!runStep) {\n console.warn('No run step or runId found for completed tool call event');\n return;\n }\n\n const contentPart: t.MessageContentComplex = {\n type: ContentTypes.TOOL_CALL,\n tool_call: result.tool_call,\n };\n\n updateContent(runStep.index, contentPart, true);\n }\n\n };\n\n return { contentParts, aggregateContent, stepMap };\n}\n"],"names":[],"mappings":";;;AAAA;AAQA,SAAS,gBAAgB,CAAC,cAAwB,EAAA;AAChD,IAAA,KAAK,MAAM,KAAK,IAAI,cAAc,EAAE;QAClC,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;AAChC,YAAA,OAAO,KAAK;;;AAGhB,IAAA,OAAO,SAAS;AAClB;AAEO,MAAM,YAAY,GAAG,CAAC,OAAe,EAAE,KAA8B,EAAE,gBAAgB,GAAG,KAAK,KAAwB;IAC5H,MAAM,SAAS,GAAG,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC;AACxD,IAAA,IAAI,SAAS,IAAI,IAAI,IAAI,SAAS,EAAE;QAClC,OAAO,gBAAgB,GAAG,SAAS,GAAG,SAAS;;IAGjD,MAAM,eAAe,GAAG,KAAK,CAAC,yBAAyB,CAAC,GAAG,CAAC,OAAO,CAAC;AACpE,IAAA,IAAI,eAAe,IAAI,IAAI,IAAI,eAAe,EAAE;AAC9C,QAAA,KAAK,CAAC,yBAAyB,CAAC,MAAM,CAAC,OAAO,CAAC;QAC/C,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,eAAe,CAAC;AACvD,QAAA,OAAO,eAAe;;AAGxB,IAAA,MAAM,UAAU,GAAG,CAAA,IAAA,EAAO,MAAM,EAAE,EAAE;IACpC,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,CAAC;AAClD,IAAA,OAAO,UAAU;AACnB;AAEa,MAAA,eAAe,GAAG,CAAC,SAAsB,EAAE,QAAkC,EAAE,KAAa,KAAU;A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AAC,GAAG,CAAC,MAAM,CAAC;YACnC,IAAI,CAAC,OAAO,EAAE;AACZ,gBAAA,OAAO,CAAC,IAAI,CAAC,0DAA0D,CAAC;gBACxE;;AAGF,YAAA,MAAM,WAAW,GAA4B;gBAC3C,IAAI,EAAE,YAAY,CAAC,SAAS;gBAC5B,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B;YAED,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC;;AAGnD,KAAC;AAED,IAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAE;AACpD;;;;"}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { ChatAnthropicMessages } from '@langchain/anthropic';
|
|
2
2
|
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
3
|
+
import type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';
|
|
3
4
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
4
5
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
5
6
|
import type { AnthropicInput } from '@langchain/anthropic';
|
|
6
7
|
import type { AnthropicStreamUsage } from '@/llm/anthropic/types';
|
|
7
8
|
export type CustomAnthropicInput = AnthropicInput & {
|
|
8
9
|
_lc_stream_delay?: number;
|
|
9
|
-
};
|
|
10
|
+
} & BaseChatModelParams;
|
|
10
11
|
export declare class CustomAnthropic extends ChatAnthropicMessages {
|
|
11
12
|
_lc_stream_delay: number;
|
|
12
13
|
private message_start;
|
|
13
14
|
private message_delta;
|
|
14
15
|
private tools_in_params?;
|
|
15
16
|
private emitted_usage?;
|
|
16
|
-
constructor(fields
|
|
17
|
+
constructor(fields?: CustomAnthropicInput);
|
|
17
18
|
/**
|
|
18
19
|
* Get stream usage as returned by this client's API response.
|
|
19
20
|
* @returns {AnthropicStreamUsage} The stream usage object.
|
|
@@ -52,4 +52,11 @@ export interface AnthropicStreamUsage {
|
|
|
52
52
|
* The total number of tokens generated in the response
|
|
53
53
|
*/
|
|
54
54
|
total_tokens: number;
|
|
55
|
+
/**
|
|
56
|
+
* Details about input token usage
|
|
57
|
+
*/
|
|
58
|
+
input_token_details?: {
|
|
59
|
+
cache_creation: number;
|
|
60
|
+
cache_read: number;
|
|
61
|
+
};
|
|
55
62
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -178,8 +178,15 @@ export type ReasoningContentText = {
|
|
|
178
178
|
type: ContentTypes.THINK;
|
|
179
179
|
think: string;
|
|
180
180
|
};
|
|
181
|
-
|
|
182
|
-
|
|
181
|
+
/** Anthropic's Reasoning Content Block Format */
|
|
182
|
+
export type ThinkingContentText = {
|
|
183
|
+
type: ContentTypes.THINKING;
|
|
184
|
+
index?: number;
|
|
185
|
+
signature?: string;
|
|
186
|
+
thinking: string;
|
|
187
|
+
};
|
|
188
|
+
export type MessageContentComplex = (ThinkingContentText | ReasoningContentText | MessageContentText | MessageContentImageUrl | (Record<string, any> & {
|
|
189
|
+
type?: 'text' | 'image_url' | 'think' | 'thinking' | string;
|
|
183
190
|
}) | (Record<string, any> & {
|
|
184
191
|
type?: never;
|
|
185
192
|
})) & {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.5",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'vertexai' --name 'Jo' --location 'New York, NY'",
|
|
48
48
|
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'openrouter' --name 'Jo' --location 'New York, NY'",
|
|
49
49
|
"caching": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/caching.ts --name 'Jo' --location 'New York, NY'",
|
|
50
|
+
"thinking": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking.ts --name 'Jo' --location 'New York, NY'",
|
|
50
51
|
"memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
51
52
|
"tool-test": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider 'alibaba' --name 'Jo' --location 'New York, NY'",
|
|
52
53
|
"abort": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/abort.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
package/src/common/enum.ts
CHANGED
package/src/graphs/Graph.ts
CHANGED
|
@@ -385,6 +385,18 @@ export class StandardGraph extends Graph<
|
|
|
385
385
|
}
|
|
386
386
|
|
|
387
387
|
finalChunk = modifyDeltaProperties(finalChunk);
|
|
388
|
+
if (this.provider === Providers.ANTHROPIC && Array.isArray(finalChunk?.content)) {
|
|
389
|
+
finalChunk.content = finalChunk.content.map((block) => {
|
|
390
|
+
if ((block as t.ThinkingContentText | undefined)?.['thinking'] && block.type !== ContentTypes.THINKING) {
|
|
391
|
+
return {
|
|
392
|
+
...block,
|
|
393
|
+
type: ContentTypes.THINKING,
|
|
394
|
+
thinking: (block as t.ThinkingContentText).thinking ?? '',
|
|
395
|
+
} as t.ThinkingContentText;
|
|
396
|
+
}
|
|
397
|
+
return block;
|
|
398
|
+
})
|
|
399
|
+
}
|
|
388
400
|
return { messages: [finalChunk as AIMessageChunk] };
|
|
389
401
|
}
|
|
390
402
|
|