From d05ff5ce48e38ad24eaa74a99c442df8f8e54ee0 Mon Sep 17 00:00:00 2001 From: SuYao Date: Thu, 19 Jun 2025 01:11:15 +0800 Subject: [PATCH] fix(AnthropicAPIClient): non stream tooluse (#7338) - Added debug logging in buildSdkMessages for better traceability. - Improved handling of tool calls in the transform method to correctly index multiple tool uses. - Enqueued additional response types to enhance the output structure for better integration with the streaming API. - Refactored event listener attachment for clarity and maintainability. --- .../clients/anthropic/AnthropicAPIClient.ts | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts index 29cf86399a..9509de81ec 100644 --- a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts +++ b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts @@ -372,7 +372,8 @@ export class AnthropicAPIClient extends BaseApiClient< listener: RawStreamListener ): AnthropicSdkRawOutput { console.log(`[AnthropicApiClient] 附加流监听器到原始输出`) - + // 专用的Anthropic事件处理 + const anthropicListener = listener as AnthropicStreamListener // 检查是否为MessageStream if (rawOutput instanceof MessageStream) { console.log(`[AnthropicApiClient] 检测到 Anthropic MessageStream,附加专用监听器`) @@ -387,9 +388,6 @@ export class AnthropicAPIClient extends BaseApiClient< }) } - // 专用的Anthropic事件处理 - const anthropicListener = listener as AnthropicStreamListener - if (anthropicListener.onContentBlock) { rawOutput.on('contentBlock', anthropicListener.onContentBlock) } @@ -413,6 +411,10 @@ export class AnthropicAPIClient extends BaseApiClient< return rawOutput } + if (anthropicListener.onMessage) { + anthropicListener.onMessage(rawOutput) + } + // 对于非MessageStream响应 return rawOutput } @@ -518,6 +520,7 @@ export class AnthropicAPIClient extends BaseApiClient< async transform(rawChunk: AnthropicSdkRawChunk, controller: TransformStreamDefaultController) { switch (rawChunk.type) { case 'message': { + let i = 0 for (const content of rawChunk.content) { switch (content.type) { case 'text': { @@ -528,7 +531,8 @@ export class AnthropicAPIClient extends BaseApiClient< break } case 'tool_use': { - toolCalls[0] = content + toolCalls[i] = content + i++ break } case 'thinking': { @@ -550,6 +554,22 @@ export class AnthropicAPIClient extends BaseApiClient< } } } + if (i > 0) { + controller.enqueue({ + type: ChunkType.MCP_TOOL_CREATED, + tool_calls: Object.values(toolCalls) + } as MCPToolCreatedChunk) + } + controller.enqueue({ + type: ChunkType.LLM_RESPONSE_COMPLETE, + response: { + usage: { + prompt_tokens: rawChunk.usage.input_tokens || 0, + completion_tokens: rawChunk.usage.output_tokens || 0, + total_tokens: (rawChunk.usage.input_tokens || 0) + (rawChunk.usage.output_tokens || 0) + } + } + }) break } case 'content_block_start': {