From f2903bd67a9276ed0136d3b1cdb2ba8f4e48244a Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 7 Feb 2026 08:22:02 -0800 Subject: [PATCH] fix(agents): use text placeholders for historical attachments to avoid token overflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Historical images/PDFs were being re-downloaded and base64-encoded for every API call, causing 200K+ token requests. Now only the current message includes full attachment blocks; historical ones use text placeholders like "[用户上传了图片: photo.png]". Co-Authored-By: Claude Opus 4.6 --- .../agents/coordinator/coordinator-agent.service.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts b/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts index 428dbf4..ea0de2f 100644 --- a/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts +++ b/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts @@ -449,13 +449,17 @@ export class CoordinatorAgentService implements OnModuleInit { ): Promise { const messages: ClaudeMessage[] = []; - // Convert previous messages + // Convert previous messages (use text placeholders for historical attachments to save tokens) if (context.previousMessages) { for (const msg of context.previousMessages) { if (msg.attachments?.length) { - const contentBlocks = await this.buildAttachmentBlocks(msg.attachments); - contentBlocks.push({ type: 'text', text: msg.content }); - messages.push({ role: msg.role, content: contentBlocks }); + const placeholders = msg.attachments.map( + (att) => `[用户上传了${att.type === 'image' ? '图片' : '文件'}: ${att.originalName}]`, + ); + messages.push({ + role: msg.role, + content: placeholders.join('\n') + '\n' + msg.content, + }); } else { messages.push({ role: msg.role, content: msg.content }); }