@librechat/agents 2.1.3 → 2.1.4
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/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/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 +8 -2
- package/package.json +2 -1
- package/src/common/enum.ts +1 -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 +152 -0
- package/src/stream.ts +11 -2
- package/src/types/stream.ts +9 -2
|
@@ -8,6 +8,26 @@ import { TextStream } from '../text.mjs';
|
|
|
8
8
|
function _toolsInParams(params) {
|
|
9
9
|
return !!(params.tools && params.tools.length > 0);
|
|
10
10
|
}
|
|
11
|
+
function _documentsInParams(params) {
|
|
12
|
+
for (const message of params.messages ?? []) {
|
|
13
|
+
if (typeof message.content === "string") {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
for (const block of message.content ?? []) {
|
|
17
|
+
if (typeof block === "object" &&
|
|
18
|
+
block != null &&
|
|
19
|
+
block.type === "document" &&
|
|
20
|
+
typeof block.citations === "object" &&
|
|
21
|
+
block.citations.enabled) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
function _thinkingInParams(params) {
|
|
29
|
+
return !!(params.thinking && params.thinking.type === "enabled");
|
|
30
|
+
}
|
|
11
31
|
function extractToken(chunk) {
|
|
12
32
|
if (typeof chunk.content === 'string') {
|
|
13
33
|
return [chunk.content, 'string'];
|
|
@@ -24,6 +44,11 @@ function extractToken(chunk) {
|
|
|
24
44
|
'text' in chunk.content[0]) {
|
|
25
45
|
return [chunk.content[0].text, 'content'];
|
|
26
46
|
}
|
|
47
|
+
else if (Array.isArray(chunk.content) &&
|
|
48
|
+
chunk.content.length >= 1 &&
|
|
49
|
+
'thinking' in chunk.content[0]) {
|
|
50
|
+
return [chunk.content[0].thinking, 'content'];
|
|
51
|
+
}
|
|
27
52
|
return [undefined];
|
|
28
53
|
}
|
|
29
54
|
function cloneChunk(text, tokenType, chunk) {
|
|
@@ -40,6 +65,9 @@ function cloneChunk(text, tokenType, chunk) {
|
|
|
40
65
|
else if (tokenType === 'content' && content.type === 'text_delta') {
|
|
41
66
|
return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));
|
|
42
67
|
}
|
|
68
|
+
else if (tokenType === 'content' && content.type?.startsWith('thinking')) {
|
|
69
|
+
return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { thinking: text })] }));
|
|
70
|
+
}
|
|
43
71
|
return chunk;
|
|
44
72
|
}
|
|
45
73
|
class CustomAnthropic extends ChatAnthropicMessages {
|
|
@@ -50,7 +78,7 @@ class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
50
78
|
emitted_usage;
|
|
51
79
|
constructor(fields) {
|
|
52
80
|
super(fields);
|
|
53
|
-
this._lc_stream_delay = fields
|
|
81
|
+
this._lc_stream_delay = fields?._lc_stream_delay ?? 25;
|
|
54
82
|
}
|
|
55
83
|
/**
|
|
56
84
|
* Get stream usage as returned by this client's API response.
|
|
@@ -66,17 +94,18 @@ class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
66
94
|
return;
|
|
67
95
|
}
|
|
68
96
|
const totalUsage = {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
+ (inputUsage?.cache_read_input_tokens ?? 0)
|
|
73
|
-
+ (outputUsage.input_tokens ?? 0)
|
|
74
|
-
+ (outputUsage.output_tokens ?? 0)
|
|
75
|
-
+ (outputUsage.cache_creation_input_tokens ?? 0)
|
|
76
|
-
+ (outputUsage.cache_read_input_tokens ?? 0),
|
|
97
|
+
input_tokens: inputUsage?.input_tokens ?? 0,
|
|
98
|
+
output_tokens: outputUsage?.output_tokens ?? 0,
|
|
99
|
+
total_tokens: (inputUsage?.input_tokens ?? 0) + (outputUsage?.output_tokens ?? 0),
|
|
77
100
|
};
|
|
101
|
+
if (inputUsage?.cache_creation_input_tokens != null || inputUsage?.cache_read_input_tokens != null) {
|
|
102
|
+
totalUsage.input_token_details = {
|
|
103
|
+
cache_creation: inputUsage.cache_creation_input_tokens ?? 0,
|
|
104
|
+
cache_read: inputUsage.cache_read_input_tokens ?? 0,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
78
107
|
this.emitted_usage = true;
|
|
79
|
-
return
|
|
108
|
+
return totalUsage;
|
|
80
109
|
}
|
|
81
110
|
resetTokenEvents() {
|
|
82
111
|
this.message_start = undefined;
|
|
@@ -102,10 +131,12 @@ class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
102
131
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
103
132
|
const params = this.invocationParams(options);
|
|
104
133
|
const formattedMessages = _convertMessagesToAnthropicPayload(messages);
|
|
105
|
-
|
|
134
|
+
const payload = {
|
|
106
135
|
...params,
|
|
107
|
-
...formattedMessages}
|
|
108
|
-
const coerceContentToString = !
|
|
136
|
+
...formattedMessages};
|
|
137
|
+
const coerceContentToString = !_toolsInParams(payload) &&
|
|
138
|
+
!_documentsInParams(payload) &&
|
|
139
|
+
!_thinkingInParams(payload);
|
|
109
140
|
const stream = await this.createStreamWithRetry({
|
|
110
141
|
...params,
|
|
111
142
|
...formattedMessages,
|
|
@@ -119,11 +150,10 @@ class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
119
150
|
stream.controller.abort();
|
|
120
151
|
throw new Error('AbortError: User aborted the request.');
|
|
121
152
|
}
|
|
122
|
-
|
|
123
|
-
if (type === 'message_start') {
|
|
153
|
+
if (data.type === 'message_start') {
|
|
124
154
|
this.message_start = data;
|
|
125
155
|
}
|
|
126
|
-
else if (type === 'message_delta') {
|
|
156
|
+
else if (data.type === 'message_delta') {
|
|
127
157
|
this.message_delta = data;
|
|
128
158
|
}
|
|
129
159
|
let usageMetadata;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.mjs","sources":["../../../../src/llm/anthropic/llm.ts"],"sourcesContent":["import { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatAnthropicMessages } from '@langchain/anthropic';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { AnthropicInput } from '@langchain/anthropic';\nimport type { AnthropicMessageCreateParams, AnthropicStreamUsage, AnthropicMessageStartEvent, AnthropicMessageDeltaEvent } from '@/llm/anthropic/types';\nimport { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';\nimport { _convertMessagesToAnthropicPayload } from './utils/message_inputs';\nimport { TextStream } from '@/llm/text';\n\nfunction _toolsInParams(params: AnthropicMessageCreateParams): boolean {\n return !!(params.tools && params.tools.length > 0);\n}\n\nfunction extractToken(chunk: AIMessageChunk): [string, 'string' | 'input' | 'content'] | [undefined] {\n if (typeof chunk.content === 'string') {\n return [chunk.content, 'string'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'input' in chunk.content[0]\n ) {\n return typeof chunk.content[0].input === 'string'\n ? [chunk.content[0].input, 'input']\n : [JSON.stringify(chunk.content[0].input), 'input'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'text' in chunk.content[0]\n ) {\n return [chunk.content[0].text, 'content'];\n }\n return [undefined];\n}\n\nfunction cloneChunk(text: string, tokenType: string, chunk: AIMessageChunk): AIMessageChunk {\n if (tokenType === 'string') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: text }));\n } else if (tokenType === 'input') {\n return chunk;\n }\n const content = chunk.content[0] as MessageContentComplex;\n if (tokenType === 'content' && content.type === 'text') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));\n } else if (tokenType === 'content' && content.type === 'text_delta') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));\n }\n\n return chunk;\n}\n\nexport type CustomAnthropicInput = AnthropicInput & { _lc_stream_delay?: number };\n\nexport class CustomAnthropic extends ChatAnthropicMessages {\n _lc_stream_delay: number;\n private message_start: AnthropicMessageStartEvent | undefined;\n private message_delta: AnthropicMessageDeltaEvent | undefined;\n private tools_in_params?: boolean;\n private emitted_usage?: boolean;\n constructor(fields: CustomAnthropicInput) {\n super(fields);\n this._lc_stream_delay = fields._lc_stream_delay ?? 25;\n }\n\n /**\n * Get stream usage as returned by this client's API response.\n * @returns {AnthropicStreamUsage} The stream usage object.\n */\n getStreamUsage(): AnthropicStreamUsage | undefined {\n if (this.emitted_usage === true) {\n return;\n }\n const inputUsage = (this.message_start?.message)?.usage as undefined | AnthropicStreamUsage;\n const outputUsage = this.message_delta?.usage as undefined | Partial<AnthropicStreamUsage>;\n if (!outputUsage) {\n return;\n }\n const totalUsage = {\n total_tokens: (inputUsage?.input_tokens ?? 0)\n + (inputUsage?.output_tokens ?? 0)\n + (inputUsage?.cache_creation_input_tokens ?? 0)\n + (inputUsage?.cache_read_input_tokens ?? 0)\n + (outputUsage.input_tokens ?? 0)\n + (outputUsage.output_tokens ?? 0)\n + (outputUsage.cache_creation_input_tokens ?? 0)\n + (outputUsage.cache_read_input_tokens ?? 0),\n };\n\n this.emitted_usage = true;\n return Object.assign(totalUsage, inputUsage, outputUsage);\n }\n\n resetTokenEvents(): void {\n this.message_start = undefined;\n this.message_delta = undefined;\n this.emitted_usage = undefined;\n this.tools_in_params = undefined;\n }\n\n private createGenerationChunk({\n token,\n chunk,\n usageMetadata,\n shouldStreamUsage,\n }: {\n token?: string,\n chunk: AIMessageChunk,\n shouldStreamUsage: boolean\n usageMetadata?: AnthropicStreamUsage,\n }): ChatGenerationChunk {\n const usage_metadata = shouldStreamUsage ? usageMetadata ?? chunk.usage_metadata : undefined;\n return new ChatGenerationChunk({\n message: new AIMessageChunk({\n // Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().\n content: chunk.content,\n additional_kwargs: chunk.additional_kwargs,\n tool_call_chunks: chunk.tool_call_chunks,\n response_metadata: chunk.response_metadata,\n usage_metadata,\n id: chunk.id,\n }),\n text: token ?? '',\n });\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const params = this.invocationParams(options);\n const formattedMessages = _convertMessagesToAnthropicPayload(messages);\n this.tools_in_params = _toolsInParams({\n ...params,\n ...formattedMessages,\n stream: false,\n });\n const coerceContentToString = !this.tools_in_params;\n\n const stream = await this.createStreamWithRetry(\n {\n ...params,\n ...formattedMessages,\n stream: true,\n },\n {\n headers: options.headers,\n }\n );\n\n const shouldStreamUsage = this.streamUsage ?? options.streamUsage;\n\n for await (const data of stream) {\n if (options.signal?.aborted === true) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n\n const type = data.type ?? '';\n if (type === 'message_start') {\n this.message_start = data as AnthropicMessageStartEvent;\n } else if (type === 'message_delta') {\n this.message_delta = data as AnthropicMessageDeltaEvent;\n }\n\n let usageMetadata: AnthropicStreamUsage | undefined;\n if (this.tools_in_params !== true && this.emitted_usage !== true) {\n usageMetadata = this.getStreamUsage();\n }\n\n const result = _makeMessageChunkFromAnthropicEvent(data, {\n streamUsage: shouldStreamUsage,\n coerceContentToString,\n });\n if (!result) continue;\n\n const { chunk } = result;\n const [token = '', tokenType] = extractToken(chunk);\n\n if (!tokenType || tokenType === 'input' || (token === '' && usageMetadata)) {\n const generationChunk = this.createGenerationChunk({\n token,\n chunk,\n usageMetadata,\n shouldStreamUsage,\n });\n yield generationChunk;\n await runManager?.handleLLMNewToken(\n token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: generationChunk }\n );\n continue;\n }\n\n const textStream = new TextStream(token, {\n delay: this._lc_stream_delay,\n firstWordChunk: true,\n minChunkSize: 4,\n maxChunkSize: 8,\n });\n\n const generator = textStream.generateText();\n try {\n let emittedUsage = false;\n for await (const currentToken of generator) {\n const newChunk = cloneChunk(currentToken, tokenType, chunk);\n\n const generationChunk = this.createGenerationChunk({\n token: currentToken,\n chunk: newChunk,\n usageMetadata: emittedUsage ? undefined : usageMetadata,\n shouldStreamUsage,\n });\n\n if (usageMetadata && !emittedUsage) {\n emittedUsage = true;\n }\n yield generationChunk;\n\n await runManager?.handleLLMNewToken(\n token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: generationChunk }\n );\n }\n } finally {\n await generator.return();\n }\n }\n\n this.resetTokenEvents();\n }\n}"],"names":[],"mappings":";;;;;;;AAWA,SAAS,cAAc,CAAC,MAAoC,EAAA;AAC1D,IAAA,OAAO,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;A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AAQ;wBACf,aAAa,EAAE,YAAY,GAAG,SAAS,GAAG,aAAa;wBACvD,iBAAiB;AAClB,qBAAA,CAAC;AAEF,oBAAA,IAAI,aAAa,IAAI,CAAC,YAAY,EAAE;wBAClC,YAAY,GAAG,IAAI;;AAErB,oBAAA,MAAM,eAAe;oBAErB,MAAM,UAAU,EAAE,iBAAiB,CACjC,KAAK,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,EAAE,KAAK,EAAE,eAAe,EAAE,CAC3B;;;oBAEK;AACR,gBAAA,MAAM,SAAS,CAAC,MAAM,EAAE;;;QAI5B,IAAI,CAAC,gBAAgB,EAAE;;AAE1B;;;;"}
|
|
1
|
+
{"version":3,"file":"llm.mjs","sources":["../../../../src/llm/anthropic/llm.ts"],"sourcesContent":["import { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatAnthropicMessages } from '@langchain/anthropic';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';\nimport type { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { AnthropicInput } from '@langchain/anthropic';\nimport type { AnthropicMessageCreateParams, AnthropicStreamingMessageCreateParams, AnthropicStreamUsage, AnthropicMessageStartEvent, AnthropicMessageDeltaEvent } from '@/llm/anthropic/types';\nimport { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';\nimport { _convertMessagesToAnthropicPayload } from './utils/message_inputs';\nimport { TextStream } from '@/llm/text';\n\nfunction _toolsInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n return !!(params.tools && params.tools.length > 0);\n}\nfunction _documentsInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n for (const message of params.messages ?? []) {\n if (typeof message.content === \"string\") {\n continue;\n }\n for (const block of message.content ?? []) {\n if (\n typeof block === \"object\" &&\n block != null &&\n block.type === \"document\" &&\n typeof block.citations === \"object\" &&\n block.citations.enabled\n ) {\n return true;\n }\n }\n }\n return false;\n}\n\nfunction _thinkingInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n return !!(params.thinking && params.thinking.type === \"enabled\");\n}\n\n\nfunction extractToken(chunk: AIMessageChunk): [string, 'string' | 'input' | 'content'] | [undefined] {\n if (typeof chunk.content === 'string') {\n return [chunk.content, 'string'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'input' in chunk.content[0]\n ) {\n return typeof chunk.content[0].input === 'string'\n ? [chunk.content[0].input, 'input']\n : [JSON.stringify(chunk.content[0].input), 'input'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'text' in chunk.content[0]\n ) {\n return [chunk.content[0].text, 'content'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'thinking' in chunk.content[0]\n ) {\n return [chunk.content[0].thinking, 'content'];\n }\n return [undefined];\n}\n\nfunction cloneChunk(text: string, tokenType: string, chunk: AIMessageChunk): AIMessageChunk {\n if (tokenType === 'string') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: text }));\n } else if (tokenType === 'input') {\n return chunk;\n }\n const content = chunk.content[0] as MessageContentComplex;\n if (tokenType === 'content' && content.type === 'text') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));\n } else if (tokenType === 'content' && content.type === 'text_delta') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));\n } else if (tokenType === 'content' && content.type?.startsWith('thinking')) {\n return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { thinking: text })] }));\n }\n\n return chunk;\n}\n\nexport type CustomAnthropicInput = AnthropicInput & { _lc_stream_delay?: number } & BaseChatModelParams;\n\nexport class CustomAnthropic extends ChatAnthropicMessages {\n _lc_stream_delay: number;\n private message_start: AnthropicMessageStartEvent | undefined;\n private message_delta: AnthropicMessageDeltaEvent | undefined;\n private tools_in_params?: boolean;\n private emitted_usage?: boolean;\n constructor(fields?: CustomAnthropicInput) {\n super(fields);\n this._lc_stream_delay = fields?._lc_stream_delay ?? 25;\n }\n\n /**\n * Get stream usage as returned by this client's API response.\n * @returns {AnthropicStreamUsage} The stream usage object.\n */\n getStreamUsage(): AnthropicStreamUsage | undefined {\n if (this.emitted_usage === true) {\n return;\n }\n const inputUsage = (this.message_start?.message)?.usage as undefined | AnthropicStreamUsage;\n const outputUsage = this.message_delta?.usage as undefined | Partial<AnthropicStreamUsage>;\n if (!outputUsage) {\n return;\n }\n const totalUsage: AnthropicStreamUsage = {\n input_tokens: inputUsage?.input_tokens ?? 0,\n output_tokens: outputUsage?.output_tokens ?? 0,\n total_tokens: (inputUsage?.input_tokens ?? 0) + (outputUsage?.output_tokens ?? 0),\n };\n\n if (inputUsage?.cache_creation_input_tokens != null || inputUsage?.cache_read_input_tokens != null) {\n totalUsage.input_token_details = {\n cache_creation: inputUsage.cache_creation_input_tokens ?? 0,\n cache_read: inputUsage.cache_read_input_tokens ?? 0,\n };\n }\n\n this.emitted_usage = true;\n return totalUsage;\n }\n\n resetTokenEvents(): void {\n this.message_start = undefined;\n this.message_delta = undefined;\n this.emitted_usage = undefined;\n this.tools_in_params = undefined;\n }\n\n private createGenerationChunk({\n token,\n chunk,\n usageMetadata,\n shouldStreamUsage,\n }: {\n token?: string,\n chunk: AIMessageChunk,\n shouldStreamUsage: boolean\n usageMetadata?: AnthropicStreamUsage,\n }): ChatGenerationChunk {\n const usage_metadata = shouldStreamUsage ? usageMetadata ?? chunk.usage_metadata : undefined;\n return new ChatGenerationChunk({\n message: new AIMessageChunk({\n // Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().\n content: chunk.content,\n additional_kwargs: chunk.additional_kwargs,\n tool_call_chunks: chunk.tool_call_chunks,\n response_metadata: chunk.response_metadata,\n usage_metadata,\n id: chunk.id,\n }),\n text: token ?? '',\n });\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const params = this.invocationParams(options);\n const formattedMessages = _convertMessagesToAnthropicPayload(messages);\n const payload = {\n ...params,\n ...formattedMessages,\n stream: true,\n } as const;\n const coerceContentToString =\n !_toolsInParams(payload) &&\n !_documentsInParams(payload) &&\n !_thinkingInParams(payload);\n\n const stream = await this.createStreamWithRetry(\n {\n ...params,\n ...formattedMessages,\n stream: true,\n },\n {\n headers: options.headers,\n }\n );\n\n const shouldStreamUsage = this.streamUsage ?? options.streamUsage;\n\n for await (const data of stream) {\n if (options.signal?.aborted === true) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n\n if (data.type === 'message_start') {\n this.message_start = data as AnthropicMessageStartEvent;\n } else if (data.type === 'message_delta') {\n this.message_delta = data as AnthropicMessageDeltaEvent;\n }\n\n let usageMetadata: AnthropicStreamUsage | undefined;\n if (this.tools_in_params !== true && this.emitted_usage !== true) {\n usageMetadata = this.getStreamUsage();\n }\n\n const result = _makeMessageChunkFromAnthropicEvent(data, {\n streamUsage: shouldStreamUsage,\n coerceContentToString,\n });\n if (!result) continue;\n\n const { chunk } = result;\n const [token = '', tokenType] = extractToken(chunk);\n\n if (!tokenType || tokenType === 'input' || (token === '' && usageMetadata)) {\n const generationChunk = this.createGenerationChunk({\n token,\n chunk,\n usageMetadata,\n shouldStreamUsage,\n });\n yield generationChunk;\n await runManager?.handleLLMNewToken(\n token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: generationChunk }\n );\n continue;\n }\n\n const textStream = new TextStream(token, {\n delay: this._lc_stream_delay,\n firstWordChunk: true,\n minChunkSize: 4,\n maxChunkSize: 8,\n });\n\n const generator = textStream.generateText();\n try {\n let emittedUsage = false;\n for await (const currentToken of generator) {\n const newChunk = cloneChunk(currentToken, tokenType, chunk);\n\n const generationChunk = this.createGenerationChunk({\n token: currentToken,\n chunk: newChunk,\n usageMetadata: emittedUsage ? undefined : usageMetadata,\n shouldStreamUsage,\n });\n\n if (usageMetadata && !emittedUsage) {\n emittedUsage = true;\n }\n yield generationChunk;\n\n await runManager?.handleLLMNewToken(\n token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: generationChunk }\n );\n }\n } finally {\n await generator.return();\n }\n }\n\n this.resetTokenEvents();\n }\n}"],"names":[],"mappings":";;;;;;;AAYA,SAAS,cAAc,CACrB,MAA4E,EAAA;AAE5E,IAAA,OAAO,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AACpD;AACA,SAAS,kBAAkB,CACzB,MAA4E,EAAA;IAE5E,KAAK,MAAM,OAAO,IAAI,MAAM,CAAC,QAAQ,IAAI,EAAE,EAAE;AAC3C,QAAA,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,EAAE;YACvC;;QAEF,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,OAAO,IAAI,EAAE,EAAE;YACzC,IACE,OAAO,KAAK,KAAK,QAAQ;AACzB,gBAAA,KAAK,IAAI,IAAI;gBACb,KAAK,CAAC,IAAI,KAAK,UAAU;AACzB,gBAAA,OAAO,KAAK,CAAC,SAAS,KAAK,QAAQ;AACnC,gBAAA,KAAK,CAAC,SAAS,CAAC,OAAO,EACvB;AACA,gBAAA,OAAO,IAAI;;;;AAIjB,IAAA,OAAO,KAAK;AACd;AAEA,SAAS,iBAAiB,CACxB,MAA4E,EAAA;AAE5E,IAAA,OAAO,CAAC,EAAE,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,SAAS,CAAC;AAClE;AAGA,SAAS,YAAY,CAAC,KAAqB,EAAA;AACzC,IAAA,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE;AACrC,QAAA,OAAO,CAAC,KAAK,CA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,EAAE;gBACvC,KAAK,EAAE,IAAI,CAAC,gBAAgB;AAC5B,gBAAA,cAAc,EAAE,IAAI;AACpB,gBAAA,YAAY,EAAE,CAAC;AACf,gBAAA,YAAY,EAAE,CAAC;AAChB,aAAA,CAAC;AAEF,YAAA,MAAM,SAAS,GAAG,UAAU,CAAC,YAAY,EAAE;AAC3C,YAAA,IAAI;gBACF,IAAI,YAAY,GAAG,KAAK;AACxB,gBAAA,WAAW,MAAM,YAAY,IAAI,SAAS,EAAE;oBAC1C,MAAM,QAAQ,GAAG,UAAU,CAAC,YAAY,EAAE,SAAS,EAAE,KAAK,CAAC;AAE3D,oBAAA,MAAM,eAAe,GAAG,IAAI,CAAC,qBAAqB,CAAC;AACjD,wBAAA,KAAK,EAAE,YAAY;AACnB,wBAAA,KAAK,EAAE,QAAQ;wBACf,aAAa,EAAE,YAAY,GAAG,SAAS,GAAG,aAAa;wBACvD,iBAAiB;AAClB,qBAAA,CAAC;AAEF,oBAAA,IAAI,aAAa,IAAI,CAAC,YAAY,EAAE;wBAClC,YAAY,GAAG,IAAI;;AAErB,oBAAA,MAAM,eAAe;oBAErB,MAAM,UAAU,EAAE,iBAAiB,CACjC,KAAK,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,EAAE,KAAK,EAAE,eAAe,EAAE,CAC3B;;;oBAEK;AACR,gBAAA,MAAM,SAAS,CAAC,MAAM,EAAE;;;QAI5B,IAAI,CAAC,gBAAgB,EAAE;;AAE1B;;;;"}
|
|
@@ -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,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE;aACpB,CAAC;SACH;;AACI,SAAA,IAAI,IAAI,CAAC,IAAI,KAAK,eAAe,EAAE;AACxC,QAAA,MAAM,aAAa,GAAkB;AACnC,YAAA,YAAY,EAAE,CAAC;AACf,YAAA,aAAa,EAAE,IAAI,CAAC,KAAK,CAAC,aAAa;AACvC,YAAA,YAAY,EAAE,IAAI,CAAC,KAAK,C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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,mCAA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AE;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;;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;AACjH,IAAA,IAAI,CAAC,KAAK,IAA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AAC,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,aAAa,KAAI;oBACtD,MAAM,UAAU,GAAG,aAAa,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;AAErD,oBAAA,MAAM,WAAW,GAA4B;wBAC3C,IAAI,EAAE,YAAY,CAAC,SAAS;AAC5B,wBAAA,SAAS,EAAE;AACT,4BAAA,IAAI,EAAE,aAAa,CAAC,IAAI,IAAI,EAAE;4BAC9B,IAAI,EAAE,aAAa,CAAC,IAAI;AACxB,4BAAA,EAAE,EAAE,UAAU;AACf,yBAAA;qBACF;AAED,oBAAA,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,CAAC;AAC3C,iBAAC,CAAC;;;AAEC,aAAA,IAAI,KAAK,KAAK,WAAW,CAAC,qBAAqB,EAAE;AACtD,YAAA,MAAM,EAAE,MAAM,EAAE,GAAG,IAA6C;AAEhE,YAAA,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,MAAM;YAE7B,MAAM,OAAO,GAAG,OAAO,CAAC,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
|
+
{"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,cAAw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,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,14 @@ 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
|
+
thinking: string;
|
|
186
|
+
};
|
|
187
|
+
export type MessageContentComplex = (ThinkingContentText | ReasoningContentText | MessageContentText | MessageContentImageUrl | (Record<string, any> & {
|
|
188
|
+
type?: 'text' | 'image_url' | 'think' | 'thinking' | string;
|
|
183
189
|
}) | (Record<string, any> & {
|
|
184
190
|
type?: never;
|
|
185
191
|
})) & {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.4",
|
|
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'",
|