From b1e843973c798b86c2fb2809f9945ec425d58c48 Mon Sep 17 00:00:00 2001 From: caozhiyuan <568022847@qq.com> Date: Wed, 20 Aug 2025 18:26:38 +0800 Subject: [PATCH] Fix AWS Bedrock models not receiving uploaded document content (#9337) * Initial plan * Add file content processing to AWS Bedrock client convertMessageToSdkParam method Co-authored-by: caozhiyuan <3415285+caozhiyuan@users.noreply.github.com> * Fix file content format to match other AI clients and update tests Co-authored-by: caozhiyuan <3415285+caozhiyuan@users.noreply.github.com> * Update src/renderer/src/aiCore/clients/aws/AwsBedrockAPIClient.ts --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: caozhiyuan <3415285+caozhiyuan@users.noreply.github.com> Co-authored-by: Phantom <59059173+EurFelux@users.noreply.github.com> --- .../aiCore/clients/aws/AwsBedrockAPIClient.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/aiCore/clients/aws/AwsBedrockAPIClient.ts b/src/renderer/src/aiCore/clients/aws/AwsBedrockAPIClient.ts index d9bd9af9c8..48159b24b9 100644 --- a/src/renderer/src/aiCore/clients/aws/AwsBedrockAPIClient.ts +++ b/src/renderer/src/aiCore/clients/aws/AwsBedrockAPIClient.ts @@ -19,6 +19,7 @@ import { estimateTextTokens } from '@renderer/services/TokenService' import { Assistant, EFFORT_RATIO, + FileTypes, GenerateImageParams, MCPCallToolResponse, MCPTool, @@ -53,7 +54,7 @@ import { mcpToolCallResponseToAwsBedrockMessage, mcpToolsToAwsBedrockTools } from '@renderer/utils/mcp-tools' -import { findImageBlocks } from '@renderer/utils/messageUtils/find' +import { findImageBlocks, findFileBlocks } from '@renderer/utils/messageUtils/find' import { t } from 'i18next' import { BaseApiClient } from '../BaseApiClient' @@ -683,6 +684,30 @@ export class AwsBedrockAPIClient extends BaseApiClient< } } + // 处理文件内容 + const fileBlocks = findFileBlocks(message) + for (const fileBlock of fileBlocks) { + const file = fileBlock.file + if (!file) { + logger.warn(`No file in the file block. Passed.`, { fileBlock }) + continue + } + + if ([FileTypes.TEXT, FileTypes.DOCUMENT].includes(file.type)) { + try { + const fileContent = (await window.api.file.read(file.id + file.ext, true)).trim() + if (fileContent) { + parts.push({ + text: `${file.origin_name}\n${fileContent}` + }) + } + } catch (error) { + logger.error('Error reading file content:', error as Error) + parts.push({ text: `[File: ${file.origin_name} - Failed to read content]` }) + } + } + } + // 如果没有任何内容,添加默认文本而不是空文本 if (parts.length === 0) { parts.push({ text: 'No content provided' })