From 3727879c1963ed66b67759c4bc15cc7fc442da16 Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 28 Jul 2026 14:28:41 +0800 Subject: [PATCH] fix(agent): resolve S3 MIME type detection and prompt parameter in image analysis --- .github/workflows/ci.yaml | 1 - packages/agent/src/tools/analyze-image.ts | 5 +- packages/agent/src/tools/read-pdf-pages.ts | 4 +- packages/agent/src/tools/screenshot.ts | 4 +- .../workflows/agent-chat-prompt-builder.ts | 2 +- packages/core/src/chat/chat.test.ts | 74 +++++++++---------- packages/core/src/s3/s3.ts | 18 ++++- 7 files changed, 64 insertions(+), 44 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c2ae0067..98195156 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -202,4 +202,3 @@ jobs: if: always() run: | docker compose -f docker-compose/local/docker-compose.yaml down -v - diff --git a/packages/agent/src/tools/analyze-image.ts b/packages/agent/src/tools/analyze-image.ts index 98f7f6e8..66e0f05b 100644 --- a/packages/agent/src/tools/analyze-image.ts +++ b/packages/agent/src/tools/analyze-image.ts @@ -106,11 +106,14 @@ export function createAnalyzeImageTool( const bucket = process.env.S3_BUCKET || 'shumai' const { buffer, contentType } = await s3Service.getObject(bucket, keyToUse) + const mimeType = + contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp' + const content: ImageContent[] = [ { type: 'image', data: buffer.toString('base64'), - mimeType: contentType || 'image/webp', + mimeType, }, ] diff --git a/packages/agent/src/tools/read-pdf-pages.ts b/packages/agent/src/tools/read-pdf-pages.ts index 16863a26..9826fae5 100644 --- a/packages/agent/src/tools/read-pdf-pages.ts +++ b/packages/agent/src/tools/read-pdf-pages.ts @@ -121,10 +121,12 @@ export function createReadPdfPagesTool( for (const pageItem of pages) { const { buffer, contentType } = await s3Service.getObject(bucket, pageItem.key) + const mimeType = + contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp' content.push({ type: 'image', data: buffer.toString('base64'), - mimeType: contentType || 'image/webp', + mimeType, }) sourceKeys.push(pageItem.key) } diff --git a/packages/agent/src/tools/screenshot.ts b/packages/agent/src/tools/screenshot.ts index f2030a1f..bf4d00e6 100644 --- a/packages/agent/src/tools/screenshot.ts +++ b/packages/agent/src/tools/screenshot.ts @@ -102,10 +102,12 @@ export function createScreenshotTool( for (const shot of screenshots) { const { buffer, contentType } = await s3Service.getObject(bucket, shot.key) + const mimeType = + contentType && contentType !== 'application/octet-stream' ? contentType : 'image/webp' content.push({ type: 'image', data: buffer.toString('base64'), - mimeType: contentType || 'image/webp', + mimeType, }) sourceKeys.push(shot.key) } diff --git a/packages/agent/src/workflows/agent-chat-prompt-builder.ts b/packages/agent/src/workflows/agent-chat-prompt-builder.ts index a8f2324c..e219f2c6 100644 --- a/packages/agent/src/workflows/agent-chat-prompt-builder.ts +++ b/packages/agent/src/workflows/agent-chat-prompt-builder.ts @@ -166,7 +166,7 @@ export class AgentChatPromptBuilder { if (effectiveType) { const type = getSimpleMediaType(effectiveType) if (type === 'image') { - instruction += `\n\nIf you need to view the image data, call the 'analyze_image' tool. It does not require any parameters.` + instruction += `\n\nIf you need to view the image data, call the 'analyze_image' tool with assetId "${this.assetId}".` } else if (type === 'video') { instruction += `\n\nIf you need to view visual frames or take screenshots of the video, call the 'screenshot' tool. You must specify the 'start' (seconds), 'end' (seconds), and 'count' (number of screenshots) parameters.` } else if (type === 'pdf') { diff --git a/packages/core/src/chat/chat.test.ts b/packages/core/src/chat/chat.test.ts index ecba3c08..7e56035d 100644 --- a/packages/core/src/chat/chat.test.ts +++ b/packages/core/src/chat/chat.test.ts @@ -351,49 +351,49 @@ describe('ChatService', () => { }, }) - // 2. Manually insert a test entry representing a custom context message - await prisma.agentSessionEntry.create({ + // 2. Manually insert a test entry representing a custom context message + await prisma.agentSessionEntry.create({ + data: { + id: 'test-entry-2-context', + sessionId, + type: 'custom_message', + parentId: 'test-entry-1-user', data: { - id: 'test-entry-2-context', - sessionId, - type: 'custom_message', - parentId: 'test-entry-1-user', - data: { - customType: 'context', - content: 'some context', - display: 'chat', - } as PrismaJson.PiSessionEntryData, - }, - }) + customType: 'context', + content: 'some context', + display: 'chat', + } as PrismaJson.PiSessionEntryData, + }, + }) - // 3. Manually insert a test entry representing a custom thinking level change - await prisma.agentSessionEntry.create({ + // 3. Manually insert a test entry representing a custom thinking level change + await prisma.agentSessionEntry.create({ + data: { + id: 'test-entry-3-thinking', + sessionId, + type: 'thinking_level_change', + parentId: 'test-entry-2-context', data: { - id: 'test-entry-3-thinking', - sessionId, - type: 'thinking_level_change', - parentId: 'test-entry-2-context', - data: { - thinkingLevel: 'deep', - } as PrismaJson.PiSessionEntryData, - }, - }) + thinkingLevel: 'deep', + } as PrismaJson.PiSessionEntryData, + }, + }) - // 4. Manually insert a test entry representing custom context_display_info entry - await prisma.agentSessionEntry.create({ + // 4. Manually insert a test entry representing custom context_display_info entry + await prisma.agentSessionEntry.create({ + data: { + id: 'test-entry-4-display', + sessionId, + type: 'custom', + parentId: 'test-entry-3-thinking', data: { - id: 'test-entry-4-display', - sessionId, - type: 'custom', - parentId: 'test-entry-3-thinking', + customType: 'context_display_info', data: { - customType: 'context_display_info', - data: { - assets: [{ id: 'a1', name: 'File A', type: 'file' }], - }, - } as PrismaJson.PiSessionEntryData, - }, - }) + assets: [{ id: 'a1', name: 'File A', type: 'file' }], + }, + } as PrismaJson.PiSessionEntryData, + }, + }) await prisma.agentSession.update({ where: { id: sessionId }, diff --git a/packages/core/src/s3/s3.ts b/packages/core/src/s3/s3.ts index 10ea5bf3..3c4b1aee 100644 --- a/packages/core/src/s3/s3.ts +++ b/packages/core/src/s3/s3.ts @@ -5,6 +5,7 @@ import * as fs from 'fs' import * as path from 'path' import { ulid } from 'ulid' import { LruTtlCache } from '../cache/lru-ttl-cache' +import { detectSupportedMimeType } from '../utils/mime' export function signLocalUrl(bucket: string, key: string): string { const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret' @@ -99,9 +100,22 @@ export class S3StorageService implements S3Service { async getObject(bucket: string, key: string): Promise { const file = this.client.file(key, { bucket }) const arrayBuffer = await file.arrayBuffer() + const buffer = Buffer.from(arrayBuffer) + let contentType = file.type + if (!contentType || contentType === 'application/octet-stream') { + const detected = detectSupportedMimeType(buffer) + if (detected) { + contentType = detected + } else { + const fileType = Bun.file(key).type + if (fileType && fileType !== 'application/octet-stream') { + contentType = fileType + } + } + } return { - buffer: Buffer.from(arrayBuffer), - contentType: file.type || 'application/octet-stream', + buffer, + contentType: contentType || 'application/octet-stream', } }