@librechat/agents 1.7.8 → 1.7.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/llm/anthropic/llm.cjs +117 -0
- package/dist/cjs/llm/anthropic/llm.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +251 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +135 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -0
- package/dist/cjs/llm/providers.cjs +3 -2
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/text.cjs +73 -0
- package/dist/cjs/llm/text.cjs.map +1 -0
- package/dist/esm/llm/anthropic/llm.mjs +115 -0
- package/dist/esm/llm/anthropic/llm.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +248 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +133 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -0
- package/dist/esm/llm/providers.mjs +3 -2
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/text.mjs +71 -0
- package/dist/esm/llm/text.mjs.map +1 -0
- package/dist/types/llm/anthropic/llm.d.ts +13 -0
- package/dist/types/llm/anthropic/types.d.ts +20 -0
- package/dist/types/llm/anthropic/utils/message_inputs.d.ts +14 -0
- package/dist/types/llm/anthropic/utils/message_outputs.d.ts +16 -0
- package/dist/types/llm/text.d.ts +21 -0
- package/package.json +3 -3
- package/src/llm/anthropic/llm.ts +151 -0
- package/src/llm/anthropic/types.ts +32 -0
- package/src/llm/anthropic/utils/message_inputs.ts +279 -0
- package/src/llm/anthropic/utils/message_outputs.ts +217 -0
- package/src/llm/providers.ts +4 -2
- package/src/llm/text.ts +90 -0
- package/src/scripts/code_exec.ts +1 -1
- package/src/scripts/code_exec_simple.ts +1 -1
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var stream = require('stream');
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-console */
|
|
6
|
+
class TextStream extends stream.Readable {
|
|
7
|
+
text;
|
|
8
|
+
currentIndex;
|
|
9
|
+
minChunkSize;
|
|
10
|
+
maxChunkSize;
|
|
11
|
+
delay;
|
|
12
|
+
constructor(text, options = {}) {
|
|
13
|
+
super(options);
|
|
14
|
+
this.text = text;
|
|
15
|
+
this.currentIndex = 0;
|
|
16
|
+
this.minChunkSize = options.minChunkSize ?? 2;
|
|
17
|
+
this.maxChunkSize = options.maxChunkSize ?? 4;
|
|
18
|
+
this.delay = options.delay ?? 20; // Time in milliseconds
|
|
19
|
+
}
|
|
20
|
+
_read() {
|
|
21
|
+
const { delay, minChunkSize, maxChunkSize } = this;
|
|
22
|
+
if (this.currentIndex < this.text.length) {
|
|
23
|
+
setTimeout(() => {
|
|
24
|
+
const remainingChars = this.text.length - this.currentIndex;
|
|
25
|
+
const chunkSize = Math.min(this.randomInt(minChunkSize, maxChunkSize + 1), remainingChars);
|
|
26
|
+
const chunk = this.text.slice(this.currentIndex, this.currentIndex + chunkSize);
|
|
27
|
+
this.push(chunk);
|
|
28
|
+
this.currentIndex += chunkSize;
|
|
29
|
+
}, delay);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.push(null); // signal end of data
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
randomInt(min, max) {
|
|
36
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
37
|
+
}
|
|
38
|
+
async processTextStream(progressCallback) {
|
|
39
|
+
const streamPromise = new Promise((resolve, reject) => {
|
|
40
|
+
this.on('data', (chunk) => {
|
|
41
|
+
progressCallback(chunk.toString());
|
|
42
|
+
});
|
|
43
|
+
this.on('end', () => {
|
|
44
|
+
resolve();
|
|
45
|
+
});
|
|
46
|
+
this.on('error', (err) => {
|
|
47
|
+
reject(err);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
try {
|
|
51
|
+
await streamPromise;
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
console.error('[processTextStream] Error in text stream:', err);
|
|
55
|
+
// Handle the error appropriately, e.g., return an error message or throw an error
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async *generateText(progressCallback) {
|
|
59
|
+
const { delay, minChunkSize, maxChunkSize } = this;
|
|
60
|
+
while (this.currentIndex < this.text.length) {
|
|
61
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
62
|
+
const remainingChars = this.text.length - this.currentIndex;
|
|
63
|
+
const chunkSize = Math.min(this.randomInt(minChunkSize, maxChunkSize + 1), remainingChars);
|
|
64
|
+
const chunk = this.text.slice(this.currentIndex, this.currentIndex + chunkSize);
|
|
65
|
+
progressCallback?.(chunk);
|
|
66
|
+
yield chunk;
|
|
67
|
+
this.currentIndex += chunkSize;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
exports.TextStream = TextStream;
|
|
73
|
+
//# sourceMappingURL=text.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.cjs","sources":["../../../src/llm/text.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { Readable } from 'stream';\nimport type { ReadableOptions } from 'stream';\nexport interface TextStreamOptions extends ReadableOptions {\n minChunkSize?: number;\n maxChunkSize?: number;\n delay?: number;\n}\n\nexport type ProgressCallback = (chunk: string) => void;\nexport type PostChunkCallback = (chunk: string) => void;\n\nexport class TextStream extends Readable {\n private text: string;\n private currentIndex: number;\n private minChunkSize: number;\n private maxChunkSize: number;\n private delay: number;\n\n constructor(text: string, options: TextStreamOptions = {}) {\n super(options);\n this.text = text;\n this.currentIndex = 0;\n this.minChunkSize = options.minChunkSize ?? 2;\n this.maxChunkSize = options.maxChunkSize ?? 4;\n this.delay = options.delay ?? 20; // Time in milliseconds\n }\n\n _read(): void {\n const { delay, minChunkSize, maxChunkSize } = this;\n\n if (this.currentIndex < this.text.length) {\n setTimeout(() => {\n const remainingChars = this.text.length - this.currentIndex;\n const chunkSize = Math.min(this.randomInt(minChunkSize, maxChunkSize + 1), remainingChars);\n\n const chunk = this.text.slice(this.currentIndex, this.currentIndex + chunkSize);\n this.push(chunk);\n this.currentIndex += chunkSize;\n }, delay);\n } else {\n this.push(null); // signal end of data\n }\n }\n\n private randomInt(min: number, max: number): number {\n return Math.floor(Math.random() * (max - min)) + min;\n }\n\n async processTextStream(progressCallback: ProgressCallback): Promise<void> {\n const streamPromise = new Promise<void>((resolve, reject) => {\n this.on('data', (chunk) => {\n progressCallback(chunk.toString());\n });\n\n this.on('end', () => {\n resolve();\n });\n\n this.on('error', (err) => {\n reject(err);\n });\n });\n\n try {\n await streamPromise;\n } catch (err) {\n console.error('[processTextStream] Error in text stream:', err);\n // Handle the error appropriately, e.g., return an error message or throw an error\n }\n }\n\n async *generateText(progressCallback?: ProgressCallback): AsyncGenerator<string, void, unknown> {\n const { delay, minChunkSize, maxChunkSize } = this;\n\n while (this.currentIndex < this.text.length) {\n await new Promise(resolve => setTimeout(resolve, delay));\n\n const remainingChars = this.text.length - this.currentIndex;\n const chunkSize = Math.min(this.randomInt(minChunkSize, maxChunkSize + 1), remainingChars);\n\n const chunk = this.text.slice(this.currentIndex, this.currentIndex + chunkSize);\n\n progressCallback?.(chunk);\n\n yield chunk;\n this.currentIndex += chunkSize;\n }\n }\n}"],"names":["Readable"],"mappings":";;;;AAAA;AAYM,MAAO,UAAW,SAAQA,eAAQ,CAAA;AAC9B,IAAA,IAAI,CAAS;AACb,IAAA,YAAY,CAAS;AACrB,IAAA,YAAY,CAAS;AACrB,IAAA,YAAY,CAAS;AACrB,IAAA,KAAK,CAAS;IAEtB,WAAY,CAAA,IAAY,EAAE,OAAA,GAA6B,EAAE,EAAA;QACvD,KAAK,CAAC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I,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,YAAY,GAAG,SAAS,CAAC,CAAC;AAEhF,YAAA,gBAAgB,GAAG,KAAK,CAAC,CAAC;AAE1B,YAAA,MAAM,KAAK,CAAC;AACZ,YAAA,IAAI,CAAC,YAAY,IAAI,SAAS,CAAC;SAChC;KACF;AACF;;;;"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
2
|
+
import { ChatAnthropicMessages } from '@langchain/anthropic';
|
|
3
|
+
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
4
|
+
import { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs.mjs';
|
|
5
|
+
import { _convertMessagesToAnthropicPayload } from './utils/message_inputs.mjs';
|
|
6
|
+
import { TextStream } from '../text.mjs';
|
|
7
|
+
|
|
8
|
+
function _toolsInParams(params) {
|
|
9
|
+
return !!(params.tools && params.tools.length > 0);
|
|
10
|
+
}
|
|
11
|
+
function extractToken(chunk) {
|
|
12
|
+
if (typeof chunk.content === 'string') {
|
|
13
|
+
return [chunk.content, 'string'];
|
|
14
|
+
}
|
|
15
|
+
else if (Array.isArray(chunk.content) &&
|
|
16
|
+
chunk.content.length >= 1 &&
|
|
17
|
+
'input' in chunk.content[0]) {
|
|
18
|
+
return typeof chunk.content[0].input === 'string'
|
|
19
|
+
? [chunk.content[0].input, 'input']
|
|
20
|
+
: [JSON.stringify(chunk.content[0].input), 'input'];
|
|
21
|
+
}
|
|
22
|
+
else if (Array.isArray(chunk.content) &&
|
|
23
|
+
chunk.content.length >= 1 &&
|
|
24
|
+
'text' in chunk.content[0]) {
|
|
25
|
+
return [chunk.content[0].text, 'content'];
|
|
26
|
+
}
|
|
27
|
+
return [undefined];
|
|
28
|
+
}
|
|
29
|
+
function cloneChunk(text, tokenType, chunk) {
|
|
30
|
+
if (tokenType === 'string') {
|
|
31
|
+
return new AIMessageChunk(Object.assign({}, chunk, { content: text }));
|
|
32
|
+
}
|
|
33
|
+
else if (tokenType === 'input') {
|
|
34
|
+
return chunk;
|
|
35
|
+
}
|
|
36
|
+
const content = chunk.content[0];
|
|
37
|
+
if (tokenType === 'content' && content.type === 'text') {
|
|
38
|
+
return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));
|
|
39
|
+
}
|
|
40
|
+
else if (tokenType === 'content' && content.type === 'text_delta') {
|
|
41
|
+
return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));
|
|
42
|
+
}
|
|
43
|
+
return chunk;
|
|
44
|
+
}
|
|
45
|
+
class CustomAnthropic extends ChatAnthropicMessages {
|
|
46
|
+
_lc_stream_delay;
|
|
47
|
+
constructor(fields) {
|
|
48
|
+
super(fields);
|
|
49
|
+
this._lc_stream_delay = fields._lc_stream_delay ?? 25;
|
|
50
|
+
}
|
|
51
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
52
|
+
const params = this.invocationParams(options);
|
|
53
|
+
const formattedMessages = _convertMessagesToAnthropicPayload(messages);
|
|
54
|
+
const coerceContentToString = !_toolsInParams({
|
|
55
|
+
...params,
|
|
56
|
+
...formattedMessages,
|
|
57
|
+
stream: false,
|
|
58
|
+
});
|
|
59
|
+
const stream = await this.createStreamWithRetry({
|
|
60
|
+
...params,
|
|
61
|
+
...formattedMessages,
|
|
62
|
+
stream: true,
|
|
63
|
+
}, {
|
|
64
|
+
headers: options.headers,
|
|
65
|
+
});
|
|
66
|
+
for await (const data of stream) {
|
|
67
|
+
if (options.signal?.aborted) {
|
|
68
|
+
stream.controller.abort();
|
|
69
|
+
throw new Error('AbortError: User aborted the request.');
|
|
70
|
+
}
|
|
71
|
+
const shouldStreamUsage = this.streamUsage ?? options.streamUsage;
|
|
72
|
+
const result = _makeMessageChunkFromAnthropicEvent(data, {
|
|
73
|
+
streamUsage: shouldStreamUsage,
|
|
74
|
+
coerceContentToString,
|
|
75
|
+
});
|
|
76
|
+
if (!result)
|
|
77
|
+
continue;
|
|
78
|
+
const { chunk } = result;
|
|
79
|
+
// Extract the text content token for text field and runManager.
|
|
80
|
+
const [token = '', tokenType] = extractToken(chunk);
|
|
81
|
+
const createGenerationChunk = (text, incomingChunk) => {
|
|
82
|
+
return new ChatGenerationChunk({
|
|
83
|
+
message: new AIMessageChunk({
|
|
84
|
+
// Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
|
|
85
|
+
content: incomingChunk.content,
|
|
86
|
+
additional_kwargs: incomingChunk.additional_kwargs,
|
|
87
|
+
tool_call_chunks: incomingChunk.tool_call_chunks,
|
|
88
|
+
usage_metadata: shouldStreamUsage ? incomingChunk.usage_metadata : undefined,
|
|
89
|
+
response_metadata: incomingChunk.response_metadata,
|
|
90
|
+
id: incomingChunk.id,
|
|
91
|
+
}),
|
|
92
|
+
text,
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
if (!tokenType || tokenType === 'input') {
|
|
96
|
+
const generationChunk = createGenerationChunk(token, chunk);
|
|
97
|
+
yield generationChunk;
|
|
98
|
+
await runManager?.handleLLMNewToken(token, undefined, undefined, undefined, undefined, { chunk: generationChunk });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const textStream = new TextStream(token, {
|
|
102
|
+
delay: this._lc_stream_delay,
|
|
103
|
+
});
|
|
104
|
+
for await (const currentToken of textStream.generateText()) {
|
|
105
|
+
const newChunk = cloneChunk(currentToken, tokenType, chunk);
|
|
106
|
+
const generationChunk = createGenerationChunk(currentToken, newChunk);
|
|
107
|
+
yield generationChunk;
|
|
108
|
+
await runManager?.handleLLMNewToken(token, undefined, undefined, undefined, undefined, { chunk: generationChunk });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { CustomAnthropic };
|
|
115
|
+
//# sourceMappingURL=llm.mjs.map
|
|
@@ -0,0 +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 } 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 constructor(fields: CustomAnthropicInput) {\n super(fields);\n this._lc_stream_delay = fields._lc_stream_delay ?? 25;\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 coerceContentToString = !_toolsInParams({\n ...params,\n ...formattedMessages,\n stream: false,\n });\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 for await (const data of stream) {\n if (options.signal?.aborted) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n const shouldStreamUsage = this.streamUsage ?? options.streamUsage;\n const result = _makeMessageChunkFromAnthropicEvent(data, {\n streamUsage: shouldStreamUsage,\n coerceContentToString,\n });\n if (!result) continue;\n\n const { chunk } = result;\n\n // Extract the text content token for text field and runManager.\n const [token = '', tokenType] = extractToken(chunk);\n const createGenerationChunk = (text: string, incomingChunk: AIMessageChunk): ChatGenerationChunk => {\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: incomingChunk.content,\n additional_kwargs: incomingChunk.additional_kwargs,\n tool_call_chunks: incomingChunk.tool_call_chunks,\n usage_metadata: shouldStreamUsage ? incomingChunk.usage_metadata : undefined,\n response_metadata: incomingChunk.response_metadata,\n id: incomingChunk.id,\n }),\n text,\n });\n };\n\n if (!tokenType || tokenType === 'input') {\n const generationChunk = createGenerationChunk(token, chunk);\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 });\n for await (const currentToken of textStream.generateText()) {\n const newChunk = cloneChunk(currentToken, tokenType, chunk);\n const generationChunk = createGenerationChunk(currentToken, newChunk);\n yield generationChunk;\n\n await runManager?.handleLLMNewToken(\n token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: generationChunk }\n );\n }\n }\n }\n\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,CAAC;AACrD,CAAC;AAED,SAAS,YAAY,CAAC,KAAqB,EAAA;AACzC,IAAA,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE;AACrC,QAAA,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;KAClC;AAAM,SAAA,IACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;AAC5B,QAAA,KAAK,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC;QACzB,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAC3B;QACA,OAAO,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,QAAQ;AAC/C,cAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,OAAO,CAAC;AACnC,cAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;KACvD;AAAM,SAAA,IACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;AAC5B,QAAA,KAAK,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAC1B;AACA,QAAA,OAAO,CAAC,KAAK,C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AEtB,MAAM,UAAU,EAAE,iBAAiB,CACjC,KAAK,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,EAAE,KAAK,EAAE,eAAe,EAAE,CAC3B,CAAC;aACH;SACF;KACF;AAEF;;;;"}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { isAIMessage, HumanMessage } from '@langchain/core/messages';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This util file contains functions for converting LangChain messages to Anthropic messages.
|
|
5
|
+
*/
|
|
6
|
+
function _formatImage(imageUrl) {
|
|
7
|
+
const regex = /^data:(image\/.+);base64,(.+)$/;
|
|
8
|
+
const match = imageUrl.match(regex);
|
|
9
|
+
if (match === null) {
|
|
10
|
+
throw new Error([
|
|
11
|
+
'Anthropic only supports base64-encoded images currently.',
|
|
12
|
+
'Example: data:image/png;base64,/9j/4AAQSk...',
|
|
13
|
+
].join('\n\n'));
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
type: 'base64',
|
|
17
|
+
media_type: match[1] ?? '',
|
|
18
|
+
data: match[2] ?? '',
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function _mergeMessages(messages) {
|
|
23
|
+
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
24
|
+
const merged = [];
|
|
25
|
+
for (const message of messages) {
|
|
26
|
+
if (message._getType() === 'tool') {
|
|
27
|
+
if (typeof message.content === 'string') {
|
|
28
|
+
const previousMessage = merged[merged.length - 1];
|
|
29
|
+
if (previousMessage &&
|
|
30
|
+
previousMessage._getType() === 'human' &&
|
|
31
|
+
Array.isArray(previousMessage.content) &&
|
|
32
|
+
'type' in previousMessage.content[0] &&
|
|
33
|
+
previousMessage.content[0].type === 'tool_result') {
|
|
34
|
+
// If the previous message was a tool result, we merge this tool message into it.
|
|
35
|
+
previousMessage.content.push({
|
|
36
|
+
type: 'tool_result',
|
|
37
|
+
content: message.content,
|
|
38
|
+
tool_use_id: message.tool_call_id,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// If not, we create a new human message with the tool result.
|
|
43
|
+
merged.push(new HumanMessage({
|
|
44
|
+
content: [
|
|
45
|
+
{
|
|
46
|
+
type: 'tool_result',
|
|
47
|
+
content: message.content,
|
|
48
|
+
tool_use_id: message.tool_call_id,
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
merged.push(new HumanMessage({
|
|
56
|
+
content: [
|
|
57
|
+
{
|
|
58
|
+
type: 'tool_result',
|
|
59
|
+
content: _formatContent(message.content),
|
|
60
|
+
tool_use_id: message.tool_call_id,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const previousMessage = merged[merged.length - 1];
|
|
68
|
+
if (previousMessage &&
|
|
69
|
+
previousMessage._getType() === 'human' &&
|
|
70
|
+
message._getType() === 'human') {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
let combinedContent;
|
|
73
|
+
if (typeof previousMessage.content === 'string') {
|
|
74
|
+
combinedContent = [{ type: 'text', text: previousMessage.content }];
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
combinedContent = previousMessage.content;
|
|
78
|
+
}
|
|
79
|
+
if (typeof message.content === 'string') {
|
|
80
|
+
combinedContent.push({ type: 'text', text: message.content });
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
combinedContent = combinedContent.concat(message.content);
|
|
84
|
+
}
|
|
85
|
+
previousMessage.content = combinedContent;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
merged.push(message);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return merged;
|
|
93
|
+
}
|
|
94
|
+
function _convertLangChainToolCallToAnthropic(toolCall) {
|
|
95
|
+
if (toolCall.id === undefined) {
|
|
96
|
+
throw new Error('Anthropic requires all tool calls to have an "id".');
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
type: 'tool_use',
|
|
100
|
+
id: toolCall.id,
|
|
101
|
+
name: toolCall.name,
|
|
102
|
+
input: toolCall.args,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
+
function _formatContent(content) {
|
|
107
|
+
const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];
|
|
108
|
+
const textTypes = ['text', 'text_delta'];
|
|
109
|
+
if (typeof content === 'string') {
|
|
110
|
+
return content;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const contentBlocks = content.map((contentPart) => {
|
|
114
|
+
const cacheControl = 'cache_control' in contentPart ? contentPart.cache_control : undefined;
|
|
115
|
+
if (contentPart.type === 'image_url') {
|
|
116
|
+
let source;
|
|
117
|
+
if (typeof contentPart.image_url === 'string') {
|
|
118
|
+
source = _formatImage(contentPart.image_url);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
source = _formatImage(contentPart.image_url.url);
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
type: 'image', // Explicitly setting the type as "image"
|
|
125
|
+
source,
|
|
126
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
else if (textTypes.find((t) => t === contentPart.type) &&
|
|
130
|
+
'text' in contentPart) {
|
|
131
|
+
// Assuming contentPart is of type MessageContentText here
|
|
132
|
+
return {
|
|
133
|
+
type: 'text', // Explicitly setting the type as "text"
|
|
134
|
+
text: contentPart.text,
|
|
135
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
else if (toolTypes.find((t) => t === contentPart.type)) {
|
|
139
|
+
const contentPartCopy = { ...contentPart };
|
|
140
|
+
if ('index' in contentPartCopy) {
|
|
141
|
+
// Anthropic does not support passing the index field here, so we remove it.
|
|
142
|
+
delete contentPartCopy.index;
|
|
143
|
+
}
|
|
144
|
+
if (contentPartCopy.type === 'input_json_delta') {
|
|
145
|
+
// `input_json_delta` type only represents yielding partial tool inputs
|
|
146
|
+
// and is not a valid type for Anthropic messages.
|
|
147
|
+
contentPartCopy.type = 'tool_use';
|
|
148
|
+
}
|
|
149
|
+
if ('input' in contentPartCopy) {
|
|
150
|
+
// Anthropic tool use inputs should be valid objects, when applicable.
|
|
151
|
+
try {
|
|
152
|
+
contentPartCopy.input = JSON.parse(contentPartCopy.input);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// no-op
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// TODO: Fix when SDK types are fixed
|
|
159
|
+
return {
|
|
160
|
+
...contentPartCopy,
|
|
161
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
162
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
throw new Error('Unsupported message content format');
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
return contentBlocks;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Formats messages as a prompt for the model.
|
|
174
|
+
* Used in LangSmith, export is important here.
|
|
175
|
+
* @param messages The base messages to format as a prompt.
|
|
176
|
+
* @returns The formatted prompt.
|
|
177
|
+
*/
|
|
178
|
+
function _convertMessagesToAnthropicPayload(messages) {
|
|
179
|
+
const mergedMessages = _mergeMessages(messages);
|
|
180
|
+
let system;
|
|
181
|
+
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
182
|
+
system = messages[0].content;
|
|
183
|
+
}
|
|
184
|
+
const conversationMessages = system !== undefined ? mergedMessages.slice(1) : mergedMessages;
|
|
185
|
+
const formattedMessages = conversationMessages.map((message) => {
|
|
186
|
+
let role;
|
|
187
|
+
if (message._getType() === 'human') {
|
|
188
|
+
role = 'user';
|
|
189
|
+
}
|
|
190
|
+
else if (message._getType() === 'ai') {
|
|
191
|
+
role = 'assistant';
|
|
192
|
+
}
|
|
193
|
+
else if (message._getType() === 'tool') {
|
|
194
|
+
role = 'user';
|
|
195
|
+
}
|
|
196
|
+
else if (message._getType() === 'system') {
|
|
197
|
+
throw new Error('System messages are only permitted as the first passed message.');
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
throw new Error(`Message type "${message._getType()}" is not supported.`);
|
|
201
|
+
}
|
|
202
|
+
if (isAIMessage(message) && !!message.tool_calls?.length) {
|
|
203
|
+
if (typeof message.content === 'string') {
|
|
204
|
+
if (message.content === '') {
|
|
205
|
+
return {
|
|
206
|
+
role,
|
|
207
|
+
content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
return {
|
|
212
|
+
role,
|
|
213
|
+
content: [
|
|
214
|
+
{ type: 'text', text: message.content },
|
|
215
|
+
...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
|
|
216
|
+
],
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
const { content } = message;
|
|
222
|
+
const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) => content.find((contentPart) => (contentPart.type === 'tool_use' ||
|
|
223
|
+
contentPart.type === 'input_json_delta') &&
|
|
224
|
+
contentPart.id === toolCall.id));
|
|
225
|
+
if (hasMismatchedToolCalls) {
|
|
226
|
+
console.warn('The "tool_calls" field on a message is only respected if content is a string.');
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
role,
|
|
230
|
+
content: _formatContent(message.content),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
return {
|
|
236
|
+
role,
|
|
237
|
+
content: _formatContent(message.content),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
return {
|
|
242
|
+
messages: formattedMessages,
|
|
243
|
+
system,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export { _convertLangChainToolCallToAnthropic, _convertMessagesToAnthropicPayload };
|
|
248
|
+
//# sourceMappingURL=message_inputs.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message_inputs.mjs","sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport {\n BaseMessage,\n SystemMessage,\n HumanMessage,\n AIMessage,\n ToolMessage,\n MessageContent,\n isAIMessage,\n} from '@langchain/core/messages';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicToolResponse,\n} from '@/llm/anthropic/types';\n\nfunction _formatImage(imageUrl: string): { type: string; media_type: string; data: string } {\n const regex = /^data:(image\\/.+);base64,(.+)$/;\n const match = imageUrl.match(regex);\n if (match === null) {\n throw new Error(\n [\n 'Anthropic only supports base64-encoded images currently.',\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n ].join('\\n\\n')\n );\n }\n return {\n type: 'base64',\n media_type: match[1] ?? '',\n data: match[2] ?? '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n}\n\nfunction _mergeMessages(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const merged = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n previousMessage.content.push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n });\n } else {\n // If not, we create a new human message with the tool result.\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: _formatContent(message.content),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n message._getType() === 'human'\n ) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let combinedContent: Record<string, any>[];\n if (typeof previousMessage.content === 'string') {\n combinedContent = [{ type: 'text', text: previousMessage.content }];\n } else {\n combinedContent = previousMessage.content;\n }\n if (typeof message.content === 'string') {\n combinedContent.push({ type: 'text', text: message.content });\n } else {\n combinedContent = combinedContent.concat(message.content);\n }\n previousMessage.content = combinedContent;\n } else {\n merged.push(message);\n }\n }\n }\n return merged;\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n return {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _formatContent(content: MessageContent): string | Record<string, any>[] {\n const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];\n const textTypes = ['text', 'text_delta'];\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentBlocks = content.map((contentPart) => {\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n if (typeof contentPart.image_url === 'string') {\n source = _formatImage(contentPart.image_url);\n } else {\n source = _formatImage(contentPart.image_url.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n contentPartCopy.type = 'tool_use';\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n // no-op\n }\n }\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else {\n throw new Error('Unsupported message content format');\n }\n });\n return contentBlocks;\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _mergeMessages(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n if (typeof message.content === 'string') {\n if (message.content === '') {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: 'text', text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>\n content.find(\n (contentPart) =>\n (contentPart.type === 'tool_use' ||\n contentPart.type === 'input_json_delta') &&\n contentPart.id === toolCall.id\n )\n );\n if (hasMismatchedToolCalls) {\n console.warn(\n 'The \"tool_calls\" field on a message is only respected if content is a string.'\n );\n }\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n });\n return {\n messages: formattedMessages,\n system,\n } as AnthropicMessageCreateParams;\n}"],"names":[],"mappings":";;AAAA;;AAEG;AAgBH,SAAS,YAAY,CAAC,QAAgB,EAAA;IACpC,MAAM,KAAK,GAAG,gCAAgC,CAAC;IAC/C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACpC,IAAA,IAAI,KAAK,KAAK,IAAI,EAAE;QAClB,MAAM,IAAI,KAAK,CACb;YACE,0DAA0D;YAC1D,8CAA8C;AAC/C,SAAA,CAAC,IAAI,CAAC,MAAM,CAAC,CACf,CAAC;KACH;IACD,OAAO;AACL,QAAA,IAAI,EAAE,QAAQ;AACd,QAAA,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;AAC1B,QAAA,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;;KAEd,CAAC;AACX,CAAC;AAED,SAAS,cAAc,CACrB,QAAuB,EAAA;;I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AACvC,4BAAA,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,oCAAoC,CAAC;AAChE,yBAAA;qBACF,CAAC;iBACH;aACF;iBAAM;AACL,gBAAA,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;AAC5B,gBAAA,MAAM,sBAAsB,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,KAChE,OAAO,CAAC,IAAI,CACV,CAAC,WAAW,KACV,CAAC,WAAW,CAAC,IAAI,KAAK,UAAU;AAC9B,oBAAA,WAAW,CAAC,IAAI,KAAK,kBAAkB;oBACzC,WAAW,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,CACjC,CACF,CAAC;gBACF,IAAI,sBAAsB,EAAE;AAC1B,oBAAA,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;iBACH;gBACD,OAAO;oBACL,IAAI;AACJ,oBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;iBACzC,CAAC;aACH;SACF;aAAM;YACL,OAAO;gBACL,IAAI;AACJ,gBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;aACzC,CAAC;SACH;AACH,KAAC,CAAC,CAAC;IACH,OAAO;AACL,QAAA,QAAQ,EAAE,iBAAiB;QAC3B,MAAM;KACyB,CAAC;AACpC;;;;"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
2
|
+
|
|
3
|
+
function _makeMessageChunkFromAnthropicEvent(data, fields) {
|
|
4
|
+
if (data.type === 'message_start') {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6
|
+
const { content, usage, ...additionalKwargs } = data.message;
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
const filteredAdditionalKwargs = {};
|
|
9
|
+
for (const [key, value] of Object.entries(additionalKwargs)) {
|
|
10
|
+
if (value !== undefined && value !== null) {
|
|
11
|
+
filteredAdditionalKwargs[key] = value;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const usageMetadata = {
|
|
15
|
+
input_tokens: usage.input_tokens,
|
|
16
|
+
output_tokens: usage.output_tokens,
|
|
17
|
+
total_tokens: usage.input_tokens + usage.output_tokens,
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
chunk: new AIMessageChunk({
|
|
21
|
+
content: fields.coerceContentToString ? '' : [],
|
|
22
|
+
additional_kwargs: filteredAdditionalKwargs,
|
|
23
|
+
usage_metadata: fields.streamUsage ? usageMetadata : undefined,
|
|
24
|
+
id: data.message.id,
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
else if (data.type === 'message_delta') {
|
|
29
|
+
const usageMetadata = {
|
|
30
|
+
input_tokens: 0,
|
|
31
|
+
output_tokens: data.usage.output_tokens,
|
|
32
|
+
total_tokens: data.usage.output_tokens,
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
chunk: new AIMessageChunk({
|
|
36
|
+
content: fields.coerceContentToString ? '' : [],
|
|
37
|
+
additional_kwargs: { ...data.delta },
|
|
38
|
+
usage_metadata: fields.streamUsage ? usageMetadata : undefined,
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
else if (data.type === 'content_block_start' &&
|
|
43
|
+
data.content_block.type === 'tool_use') {
|
|
44
|
+
const toolCallContentBlock = data.content_block;
|
|
45
|
+
return {
|
|
46
|
+
chunk: new AIMessageChunk({
|
|
47
|
+
content: fields.coerceContentToString
|
|
48
|
+
? ''
|
|
49
|
+
: [
|
|
50
|
+
{
|
|
51
|
+
index: data.index,
|
|
52
|
+
...data.content_block,
|
|
53
|
+
input: '',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
additional_kwargs: {},
|
|
57
|
+
tool_call_chunks: [
|
|
58
|
+
{
|
|
59
|
+
id: toolCallContentBlock.id,
|
|
60
|
+
index: data.index,
|
|
61
|
+
name: toolCallContentBlock.name,
|
|
62
|
+
args: '',
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
else if (data.type === 'content_block_delta' &&
|
|
69
|
+
data.delta.type === 'text_delta') {
|
|
70
|
+
const content = data.delta.text;
|
|
71
|
+
if (content !== undefined) {
|
|
72
|
+
return {
|
|
73
|
+
chunk: new AIMessageChunk({
|
|
74
|
+
content: fields.coerceContentToString
|
|
75
|
+
? content
|
|
76
|
+
: [
|
|
77
|
+
{
|
|
78
|
+
index: data.index,
|
|
79
|
+
...data.delta,
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
additional_kwargs: {},
|
|
83
|
+
}),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else if (data.type === 'content_block_delta' &&
|
|
88
|
+
data.delta.type === 'input_json_delta') {
|
|
89
|
+
return {
|
|
90
|
+
chunk: new AIMessageChunk({
|
|
91
|
+
content: fields.coerceContentToString
|
|
92
|
+
? ''
|
|
93
|
+
: [
|
|
94
|
+
{
|
|
95
|
+
index: data.index,
|
|
96
|
+
input: data.delta.partial_json,
|
|
97
|
+
type: data.delta.type,
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
additional_kwargs: {},
|
|
101
|
+
tool_call_chunks: [
|
|
102
|
+
{
|
|
103
|
+
index: data.index,
|
|
104
|
+
args: data.delta.partial_json,
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
else if (data.type === 'content_block_start' &&
|
|
111
|
+
data.content_block.type === 'text') {
|
|
112
|
+
const content = data.content_block.text;
|
|
113
|
+
if (content !== undefined) {
|
|
114
|
+
return {
|
|
115
|
+
chunk: new AIMessageChunk({
|
|
116
|
+
content: fields.coerceContentToString
|
|
117
|
+
? content
|
|
118
|
+
: [
|
|
119
|
+
{
|
|
120
|
+
index: data.index,
|
|
121
|
+
...data.content_block,
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
additional_kwargs: {},
|
|
125
|
+
}),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export { _makeMessageChunkFromAnthropicEvent };
|
|
133
|
+
//# sourceMappingURL=message_outputs.mjs.map
|