From 95a2da49cf318c76c44cc389e6f1af7b4b3b20cd Mon Sep 17 00:00:00 2001 From: arvinxx Date: Fri, 13 Dec 2024 00:34:31 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20fix=20Claude=20first=20ca?= =?UTF-8?q?n=20not=20be=20assistant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/anthropicHelpers.test.ts | 27 ++++++++++++------- .../agent-runtime/utils/anthropicHelpers.ts | 13 ++++----- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/libs/agent-runtime/utils/anthropicHelpers.test.ts b/src/libs/agent-runtime/utils/anthropicHelpers.test.ts index 600f83cb5dc3..081ebca06bf9 100644 --- a/src/libs/agent-runtime/utils/anthropicHelpers.test.ts +++ b/src/libs/agent-runtime/utils/anthropicHelpers.test.ts @@ -228,6 +228,21 @@ describe('anthropicHelpers', () => { ]); }); + it('messages should start with user', async () => { + const messages: OpenAIChatMessage[] = [ + { content: 'Hi', role: 'assistant' }, + { content: 'Hello', role: 'user' }, + ]; + + const contents = await buildAnthropicMessages(messages); + + expect(contents).toHaveLength(2); + expect(contents).toEqual([ + { content: 'Hi', role: 'user' }, + { content: 'Hello', role: 'user' }, + ]); + }); + it('messages should end with user', async () => { const messages: OpenAIChatMessage[] = [ { content: 'Hello', role: 'user' }, @@ -237,10 +252,9 @@ describe('anthropicHelpers', () => { const contents = await buildAnthropicMessages(messages); - expect(contents).toHaveLength(4); + expect(contents).toHaveLength(3); expect(contents).toEqual([ { content: 'Hello', role: 'user' }, - { content: '_', role: 'assistant' }, { content: 'Hello', role: 'user' }, { content: 'Hi', role: 'assistant' }, ]); @@ -257,15 +271,11 @@ describe('anthropicHelpers', () => { const contents = await buildAnthropicMessages(messages); - expect(contents).toHaveLength(9); + expect(contents).toHaveLength(5); expect(contents).toEqual([ - { content: '_', role: 'user' }, - { content: 'a', role: 'assistant' }, - { content: '_', role: 'user' }, + { content: 'a', role: 'user' }, { content: 'b', role: 'assistant' }, - { content: '_', role: 'user' }, { content: 'c', role: 'assistant' }, - { content: '_', role: 'user' }, { content: 'd', role: 'assistant' }, { content: '你好', role: 'user' }, ]); @@ -370,7 +380,6 @@ describe('anthropicHelpers', () => { ], role: 'user', }, - { content: '_', role: 'assistant' }, { content: '继续', role: 'user' }, ]); }); diff --git a/src/libs/agent-runtime/utils/anthropicHelpers.ts b/src/libs/agent-runtime/utils/anthropicHelpers.ts index fe919e098552..41f8f5abaa94 100644 --- a/src/libs/agent-runtime/utils/anthropicHelpers.ts +++ b/src/libs/agent-runtime/utils/anthropicHelpers.ts @@ -114,11 +114,11 @@ export const buildAnthropicMessages = async ( oaiMessages: OpenAIChatMessage[], ): Promise => { const messages: Anthropic.Messages.MessageParam[] = []; - let lastRole = 'assistant'; let pendingToolResults: Anthropic.ToolResultBlockParam[] = []; for (const message of oaiMessages) { const index = oaiMessages.indexOf(message); + // refs: https://docs.anthropic.com/claude/docs/tool-use#tool-use-and-tool-result-content-blocks if (message.role === 'tool') { pendingToolResults.push({ @@ -135,17 +135,14 @@ export const buildAnthropicMessages = async ( role: 'user', }); pendingToolResults = []; - lastRole = 'user'; } } else { const anthropicMessage = await buildAnthropicMessage(message); - if (lastRole === anthropicMessage.role) { - messages.push({ content: '_', role: lastRole === 'user' ? 'assistant' : 'user' }); - } - - lastRole = anthropicMessage.role; - messages.push(anthropicMessage); + messages.push({ + ...anthropicMessage, + role: index === 0 && anthropicMessage.role === 'assistant' ? 'user' : anthropicMessage.role, + }); } }