@librechat/agents 1.8.1 → 1.8.2
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/utils/message_inputs.cjs +8 -30
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +8 -30
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/package.json +3 -3
- package/src/llm/anthropic/utils/message_inputs.ts +8 -29
- package/src/proto/collab_design_v5.ts +1 -1
|
@@ -21,13 +21,13 @@ function _formatImage(imageUrl) {
|
|
|
21
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function _ensureMessageContents(messages$1) {
|
|
25
25
|
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
26
|
-
const
|
|
26
|
+
const updatedMsgs = [];
|
|
27
27
|
for (const message of messages$1) {
|
|
28
28
|
if (message._getType() === 'tool') {
|
|
29
29
|
if (typeof message.content === 'string') {
|
|
30
|
-
const previousMessage =
|
|
30
|
+
const previousMessage = updatedMsgs[updatedMsgs.length - 1];
|
|
31
31
|
if (previousMessage &&
|
|
32
32
|
previousMessage._getType() === 'human' &&
|
|
33
33
|
Array.isArray(previousMessage.content) &&
|
|
@@ -42,7 +42,7 @@ function _mergeMessages(messages$1) {
|
|
|
42
42
|
}
|
|
43
43
|
else {
|
|
44
44
|
// If not, we create a new human message with the tool result.
|
|
45
|
-
|
|
45
|
+
updatedMsgs.push(new messages.HumanMessage({
|
|
46
46
|
content: [
|
|
47
47
|
{
|
|
48
48
|
type: 'tool_result',
|
|
@@ -54,7 +54,7 @@ function _mergeMessages(messages$1) {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
else {
|
|
57
|
-
|
|
57
|
+
updatedMsgs.push(new messages.HumanMessage({
|
|
58
58
|
content: [
|
|
59
59
|
{
|
|
60
60
|
type: 'tool_result',
|
|
@@ -66,32 +66,10 @@ function _mergeMessages(messages$1) {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
else {
|
|
69
|
-
|
|
70
|
-
if (previousMessage &&
|
|
71
|
-
previousMessage._getType() === 'human' &&
|
|
72
|
-
message._getType() === 'human') {
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
-
let combinedContent;
|
|
75
|
-
if (typeof previousMessage.content === 'string') {
|
|
76
|
-
combinedContent = [{ type: 'text', text: previousMessage.content }];
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
combinedContent = previousMessage.content;
|
|
80
|
-
}
|
|
81
|
-
if (typeof message.content === 'string') {
|
|
82
|
-
combinedContent.push({ type: 'text', text: message.content });
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
combinedContent = combinedContent.concat(message.content);
|
|
86
|
-
}
|
|
87
|
-
previousMessage.content = combinedContent;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
merged.push(message);
|
|
91
|
-
}
|
|
69
|
+
updatedMsgs.push(message);
|
|
92
70
|
}
|
|
93
71
|
}
|
|
94
|
-
return
|
|
72
|
+
return updatedMsgs;
|
|
95
73
|
}
|
|
96
74
|
function _convertLangChainToolCallToAnthropic(toolCall) {
|
|
97
75
|
if (toolCall.id === undefined) {
|
|
@@ -178,7 +156,7 @@ function _formatContent(content) {
|
|
|
178
156
|
* @returns The formatted prompt.
|
|
179
157
|
*/
|
|
180
158
|
function _convertMessagesToAnthropicPayload(messages$1) {
|
|
181
|
-
const mergedMessages =
|
|
159
|
+
const mergedMessages = _ensureMessageContents(messages$1);
|
|
182
160
|
let system;
|
|
183
161
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
184
162
|
system = messages$1[0].content;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message_inputs.cjs","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":["messages","HumanMessage","isAIMessage"],"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,CACrBA,UAAuB,EAAA;;IAGvB,MAAM,MAAM,GAAG,EAAE,CAAC;AAClB,IAAA,KAAK,MAAM,OAAO,IAAIA,UAAQ,EAAE;AAC9B,QAAA,IAAI,OAAO,CAAC,QAAQ,EAAE,KAAK,MAAM,EAAE;AACjC,YAAA,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,EAAE;gBACvC,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAA4B,CAAC;AAC7E,gBAAA,IACE,eAAe;AACf,oBAAA,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AC,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;;;;;"}
|
|
1
|
+
{"version":3,"file":"message_inputs.cjs","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 _ensureMessageContents(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = updatedMsgs[updatedMsgs.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 updatedMsgs.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 updatedMsgs.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 updatedMsgs.push(message);\n }\n }\n return updatedMsgs;\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 = _ensureMessageContents(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":["messages","HumanMessage","isAIMessage"],"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,sBAAsB,CAC7BA,UAAuB,EAAA;;IAGvB,MAAM,WAAW,GAAG,EAA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CAAC;AACpC;;;;;"}
|
|
@@ -19,13 +19,13 @@ function _formatImage(imageUrl) {
|
|
|
19
19
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
function
|
|
22
|
+
function _ensureMessageContents(messages) {
|
|
23
23
|
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
24
|
-
const
|
|
24
|
+
const updatedMsgs = [];
|
|
25
25
|
for (const message of messages) {
|
|
26
26
|
if (message._getType() === 'tool') {
|
|
27
27
|
if (typeof message.content === 'string') {
|
|
28
|
-
const previousMessage =
|
|
28
|
+
const previousMessage = updatedMsgs[updatedMsgs.length - 1];
|
|
29
29
|
if (previousMessage &&
|
|
30
30
|
previousMessage._getType() === 'human' &&
|
|
31
31
|
Array.isArray(previousMessage.content) &&
|
|
@@ -40,7 +40,7 @@ function _mergeMessages(messages) {
|
|
|
40
40
|
}
|
|
41
41
|
else {
|
|
42
42
|
// If not, we create a new human message with the tool result.
|
|
43
|
-
|
|
43
|
+
updatedMsgs.push(new HumanMessage({
|
|
44
44
|
content: [
|
|
45
45
|
{
|
|
46
46
|
type: 'tool_result',
|
|
@@ -52,7 +52,7 @@ function _mergeMessages(messages) {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
else {
|
|
55
|
-
|
|
55
|
+
updatedMsgs.push(new HumanMessage({
|
|
56
56
|
content: [
|
|
57
57
|
{
|
|
58
58
|
type: 'tool_result',
|
|
@@ -64,32 +64,10 @@ function _mergeMessages(messages) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
else {
|
|
67
|
-
|
|
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
|
-
}
|
|
67
|
+
updatedMsgs.push(message);
|
|
90
68
|
}
|
|
91
69
|
}
|
|
92
|
-
return
|
|
70
|
+
return updatedMsgs;
|
|
93
71
|
}
|
|
94
72
|
function _convertLangChainToolCallToAnthropic(toolCall) {
|
|
95
73
|
if (toolCall.id === undefined) {
|
|
@@ -176,7 +154,7 @@ function _formatContent(content) {
|
|
|
176
154
|
* @returns The formatted prompt.
|
|
177
155
|
*/
|
|
178
156
|
function _convertMessagesToAnthropicPayload(messages) {
|
|
179
|
-
const mergedMessages =
|
|
157
|
+
const mergedMessages = _ensureMessageContents(messages);
|
|
180
158
|
let system;
|
|
181
159
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
182
160
|
system = messages[0].content;
|
|
@@ -1 +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,YAA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AO,CAAC,OAAO,CAAC;aACzC,CAAC;SACH;AACH,KAAC,CAAC,CAAC;IACH,OAAO;AACL,QAAA,QAAQ,EAAE,iBAAiB;QAC3B,MAAM;KACyB,CAAC;AACpC;;;;"}
|
|
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 _ensureMessageContents(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = updatedMsgs[updatedMsgs.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 updatedMsgs.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 updatedMsgs.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 updatedMsgs.push(message);\n }\n }\n return updatedMsgs;\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 = _ensureMessageContents(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,sBAAsB,CAC7B,QAAuB,EAAA;;IAGvB,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,IAAA,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;AAC9B,QAAA,IAAI,OAAO,CAAC,QAAQ,EAAE,KAAK,MAAM,EAAE;AACjC,YAAA,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,EAAE;gBACvC,MAAM,eAAe,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAA4B,CAAC;AACvF,gBAAA,IACE,eAAe;AACf,oBAAA,eAAe,CAAC,QAAQ,EAAE,KAAK,OAAO;AACtC,oBAAA,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC;AACtC,oBAAA,MAAM,IAAI,eAAe,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AC,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;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"start:cli": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/cli.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
43
43
|
"content": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/content.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
44
44
|
"stream": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/stream.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
45
|
-
"code_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec.ts --provider '
|
|
45
|
+
"code_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
46
46
|
"code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
47
47
|
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
48
48
|
"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'",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"@aws-crypto/sha256-js": "^5.2.0",
|
|
70
70
|
"@aws-sdk/credential-provider-node": "^3.613.0",
|
|
71
71
|
"@aws-sdk/types": "^3.609.0",
|
|
72
|
-
"@langchain/anthropic": "^0.3.
|
|
72
|
+
"@langchain/anthropic": "^0.3.8",
|
|
73
73
|
"@langchain/aws": "^0.1.1",
|
|
74
74
|
"@langchain/community": "^0.3.14",
|
|
75
75
|
"@langchain/core": "^0.3.18",
|
|
@@ -35,15 +35,15 @@ function _formatImage(imageUrl: string): { type: string; media_type: string; dat
|
|
|
35
35
|
} as any;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
function
|
|
38
|
+
function _ensureMessageContents(
|
|
39
39
|
messages: BaseMessage[]
|
|
40
40
|
): (SystemMessage | HumanMessage | AIMessage)[] {
|
|
41
41
|
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
42
|
-
const
|
|
42
|
+
const updatedMsgs = [];
|
|
43
43
|
for (const message of messages) {
|
|
44
44
|
if (message._getType() === 'tool') {
|
|
45
45
|
if (typeof message.content === 'string') {
|
|
46
|
-
const previousMessage =
|
|
46
|
+
const previousMessage = updatedMsgs[updatedMsgs.length - 1] as BaseMessage | undefined;
|
|
47
47
|
if (
|
|
48
48
|
previousMessage &&
|
|
49
49
|
previousMessage._getType() === 'human' &&
|
|
@@ -59,7 +59,7 @@ function _mergeMessages(
|
|
|
59
59
|
});
|
|
60
60
|
} else {
|
|
61
61
|
// If not, we create a new human message with the tool result.
|
|
62
|
-
|
|
62
|
+
updatedMsgs.push(
|
|
63
63
|
new HumanMessage({
|
|
64
64
|
content: [
|
|
65
65
|
{
|
|
@@ -72,7 +72,7 @@ function _mergeMessages(
|
|
|
72
72
|
);
|
|
73
73
|
}
|
|
74
74
|
} else {
|
|
75
|
-
|
|
75
|
+
updatedMsgs.push(
|
|
76
76
|
new HumanMessage({
|
|
77
77
|
content: [
|
|
78
78
|
{
|
|
@@ -85,31 +85,10 @@ function _mergeMessages(
|
|
|
85
85
|
);
|
|
86
86
|
}
|
|
87
87
|
} else {
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
previousMessage &&
|
|
91
|
-
previousMessage._getType() === 'human' &&
|
|
92
|
-
message._getType() === 'human'
|
|
93
|
-
) {
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
-
let combinedContent: Record<string, any>[];
|
|
96
|
-
if (typeof previousMessage.content === 'string') {
|
|
97
|
-
combinedContent = [{ type: 'text', text: previousMessage.content }];
|
|
98
|
-
} else {
|
|
99
|
-
combinedContent = previousMessage.content;
|
|
100
|
-
}
|
|
101
|
-
if (typeof message.content === 'string') {
|
|
102
|
-
combinedContent.push({ type: 'text', text: message.content });
|
|
103
|
-
} else {
|
|
104
|
-
combinedContent = combinedContent.concat(message.content);
|
|
105
|
-
}
|
|
106
|
-
previousMessage.content = combinedContent;
|
|
107
|
-
} else {
|
|
108
|
-
merged.push(message);
|
|
109
|
-
}
|
|
88
|
+
updatedMsgs.push(message);
|
|
110
89
|
}
|
|
111
90
|
}
|
|
112
|
-
return
|
|
91
|
+
return updatedMsgs;
|
|
113
92
|
}
|
|
114
93
|
|
|
115
94
|
export function _convertLangChainToolCallToAnthropic(
|
|
@@ -205,7 +184,7 @@ function _formatContent(content: MessageContent): string | Record<string, any>[]
|
|
|
205
184
|
export function _convertMessagesToAnthropicPayload(
|
|
206
185
|
messages: BaseMessage[]
|
|
207
186
|
): AnthropicMessageCreateParams {
|
|
208
|
-
const mergedMessages =
|
|
187
|
+
const mergedMessages = _ensureMessageContents(messages);
|
|
209
188
|
let system;
|
|
210
189
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
211
190
|
system = messages[0].content;
|
|
@@ -77,7 +77,7 @@ export class CollaborativeProcessor {
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
private async createGraph(): Promise<t.CompiledWorkflow> {
|
|
80
|
-
const agentStateChannels: StateGraphArgs['channels'] = {
|
|
80
|
+
const agentStateChannels: StateGraphArgs<AgentStateChannels>['channels'] = {
|
|
81
81
|
messages: {
|
|
82
82
|
value: (x?: BaseMessage[], y?: BaseMessage[]) => (x ?? []).concat(y ?? []),
|
|
83
83
|
default: () => [],
|