fix(agent): resolve S3 MIME type detection and prompt parameter in image analysis

This commit is contained in:
yiling
2026-07-28 14:28:41 +08:00
parent f46fa31262
commit 3727879c19
7 changed files with 64 additions and 44 deletions
-1
View File
@@ -202,4 +202,3 @@ jobs:
if: always()
run: |
docker compose -f docker-compose/local/docker-compose.yaml down -v
+4 -1
View File
@@ -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,
},
]
+3 -1
View File
@@ -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)
}
+3 -1
View File
@@ -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)
}
@@ -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') {
+37 -37
View File
@@ -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 },
+16 -2
View File
@@ -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<S3Object> {
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',
}
}