feat(asset): replace mediaType/mimeType logic with proxyType

This commit is contained in:
yiling
2026-07-20 10:22:36 +08:00
parent c45a1d9ee9
commit f158d04686
40 changed files with 219 additions and 119 deletions
@@ -64,6 +64,7 @@ describe('Agent Autofill Workflow', () => {
storageKey: { key: 'asset-key' },
project: { id: 'p1', teamId: 't1' },
mediaType: 'image/png',
media: { proxyType: 'image' },
})
mockActivities.downloadMediaToTmpActivity.mockResolvedValue({
filePath: '/tmp/test.png',
@@ -290,6 +291,7 @@ describe('Agent Autofill Workflow', () => {
storageKey: { key: 'asset-key' },
project: { id: 'p1', teamId: 't1' },
mediaType: 'audio/mp3',
media: { proxyType: 'audio' },
})
const task = await prisma.workflowTask.create({
@@ -64,8 +64,9 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
const projectId = asset.project.id
// 2. Prepare Data (Images)
const isImage = asset.mediaType?.startsWith('image/') || false
const isVideo = asset.mediaType?.startsWith('video/') || false
const proxyType = (asset.media as PrismaJson.MediaInfo | null)?.proxyType
const isImage = proxyType === 'image'
const isVideo = proxyType === 'video'
if (!isImage && !isVideo) {
throw ApplicationFailure.create({
@@ -25,7 +25,13 @@ describe('AgentChatPromptBuilder', () => {
})
it('should build prompt with image asset details', () => {
const builder = new AgentChatPromptBuilder('a1').withAssetDetails('test.png', 'image/png')
const builder = new AgentChatPromptBuilder('a1').withAssetDetails(
'test.png',
'image/png',
undefined,
undefined,
'image',
)
const result = builder.build()
expect(result).toContain('File Name: test.png')
@@ -39,6 +45,8 @@ describe('AgentChatPromptBuilder', () => {
'movie.mp4',
'video/mp4',
12.34,
undefined,
'video',
)
const result = builder.build()
@@ -55,6 +63,7 @@ describe('AgentChatPromptBuilder', () => {
'application/pdf',
undefined,
15,
'pdf',
)
const result = builder.build()
@@ -69,7 +78,7 @@ describe('AgentChatPromptBuilder', () => {
it('should build prompt with comment page for PDF asset', () => {
const builder = new AgentChatPromptBuilder('a1')
.withAssetDetails('doc.pdf', 'application/pdf', undefined, 10)
.withAssetDetails('doc.pdf', 'application/pdf', undefined, 10, 'pdf')
.withCommentTimestamp(3)
const result = builder.build()
@@ -79,7 +88,7 @@ describe('AgentChatPromptBuilder', () => {
it('should build prompt for PDF continuation mode with comment page', () => {
const builder = new AgentChatPromptBuilder('a1')
.withContinuation(true)
.withAssetDetails('doc.pdf', 'application/pdf', undefined, 10)
.withAssetDetails('doc.pdf', 'application/pdf', undefined, 10, 'pdf')
.withCommentTimestamp(4)
const result = builder.build()
@@ -98,7 +107,7 @@ describe('AgentChatPromptBuilder', () => {
const builder = new AgentChatPromptBuilder('a1')
.withExplicitMention(true)
.withPathContext('src/main.ts')
.withAssetDetails('movie.mp4', 'video/mp4', 12.34)
.withAssetDetails('movie.mp4', 'video/mp4', 12.34, undefined, 'video')
.withCommentTimestamp(8.2)
const result = builder.build()
@@ -1,19 +1,7 @@
export function getSimpleMediaType(mediaType?: string): 'image' | 'video' | 'pdf' | 'other' {
if (!mediaType) return 'other'
const lower = mediaType.toLowerCase()
if (
lower.startsWith('image/') ||
lower === 'application/x-photoshop' ||
lower === 'image/vnd.adobe.photoshop'
) {
return 'image'
}
if (lower.startsWith('video/')) {
return 'video'
}
if (lower === 'application/pdf' || lower.endsWith('.pdf')) {
return 'pdf'
}
export function getSimpleMediaType(proxyType?: string | null): 'image' | 'video' | 'pdf' | 'other' {
if (proxyType === 'image') return 'image'
if (proxyType === 'video') return 'video'
if (proxyType === 'pdf') return 'pdf'
return 'other'
}
@@ -21,6 +9,7 @@ export class AgentChatPromptBuilder {
private assetId: string
private assetName?: string
private mediaType?: string
private proxyType?: string
private videoDuration?: number
private totalPages?: number
private commentTimestamp?: number
@@ -64,9 +53,11 @@ export class AgentChatPromptBuilder {
mediaType: string | null,
duration?: number,
totalPages?: number,
proxyType?: string | null,
): this {
this.assetName = name
this.mediaType = mediaType || undefined
this.proxyType = proxyType || undefined
this.videoDuration = duration
this.totalPages = totalPages
return this
@@ -85,10 +76,12 @@ export class AgentChatPromptBuilder {
}
build(): string {
const effectiveType = this.proxyType || this.mediaType
if (this.isContinuation) {
let instruction = ''
if (this.commentTimestamp !== undefined) {
const type = getSimpleMediaType(this.mediaType)
const type = getSimpleMediaType(effectiveType)
if (type === 'pdf') {
instruction += `Comment Page: ${Math.round(this.commentTimestamp)}`
} else {
@@ -111,8 +104,8 @@ export class AgentChatPromptBuilder {
if (this.assetName) {
instruction += `\nFile Name: ${this.assetName}`
}
if (this.mediaType) {
const type = getSimpleMediaType(this.mediaType)
if (effectiveType) {
const type = getSimpleMediaType(effectiveType)
instruction += `\nFile Type: ${type}`
if (type === 'video' && this.videoDuration !== undefined) {
instruction += `\nVideo Length: ${this.videoDuration.toFixed(2)} seconds`
@@ -121,7 +114,7 @@ export class AgentChatPromptBuilder {
}
}
if (this.commentTimestamp !== undefined) {
const type = getSimpleMediaType(this.mediaType)
const type = getSimpleMediaType(effectiveType)
if (type === 'pdf') {
instruction += `\nComment Page: ${Math.round(this.commentTimestamp)}`
} else {
@@ -142,8 +135,8 @@ export class AgentChatPromptBuilder {
}
}
if (this.mediaType) {
const type = getSimpleMediaType(this.mediaType)
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.`
} else if (type === 'video') {
@@ -61,6 +61,7 @@ describe('Agent Chat Workflow', () => {
type: 'file',
name: 'test-file.png',
mediaType: 'image/png',
media: { proxyType: 'image' },
parentId: 'parent-folder-id',
})
mockActivities.getCommentActivity.mockResolvedValue({
@@ -133,7 +134,7 @@ describe('Agent Chat Workflow', () => {
// Verify agent instruction composition
const expectedInstruction1 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('test-file.png', 'image/png', undefined)
.withAssetDetails('test-file.png', 'image/png', undefined, undefined, 'image')
.withCommentTimestamp(null)
.withExplicitMention(true)
.build()
@@ -239,7 +240,7 @@ describe('Agent Chat Workflow', () => {
// Verify instruction contains not explicitly mentioned instructions
const expectedInstruction3 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('test-file.png', 'image/png', undefined)
.withAssetDetails('test-file.png', 'image/png', undefined, undefined, 'image')
.withCommentTimestamp(null)
.withExplicitMention(false)
.build()
@@ -271,18 +272,21 @@ describe('Agent Chat Workflow', () => {
{
asset: {
mediaType: 'image/png',
media: { proxyType: 'image' },
storageKey: { key: 'attachments/image1.png' },
},
},
{
asset: {
mediaType: 'video/mp4', // non-image, should be ignored
media: { proxyType: 'video' },
storageKey: { key: 'attachments/video.mp4' },
},
},
{
asset: {
mediaType: 'image/jpeg',
media: { proxyType: 'image' },
storageKey: { key: 'attachments/image2.jpg' },
},
},
@@ -305,7 +309,7 @@ describe('Agent Chat Workflow', () => {
const expectedInstruction4 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withAssetDetails('test-file.png', 'image/png', undefined)
.withAssetDetails('test-file.png', 'image/png', undefined, undefined, 'image')
.withCommentTimestamp(null)
.withExplicitMention(false)
.build()
+4 -2
View File
@@ -74,7 +74,8 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
// 5. Prepare Images (Attachments only)
if (userComment.attachments) {
for (const att of userComment.attachments) {
if (att.asset?.mediaType?.startsWith('image/') && att.asset?.storageKey?.key) {
const attProxyType = (att.asset?.media as PrismaJson.MediaInfo | null)?.proxyType
if (attProxyType === 'image' && att.asset?.storageKey?.key) {
attachmentImageUrls.push(att.asset.storageKey.key)
}
}
@@ -161,10 +162,11 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
}
}
const proxyType = (asset.media as PrismaJson.MediaInfo | null)?.proxyType
const instruction = new AgentChatPromptBuilder(asset.id)
.withContinuation(!isNewChat)
.withPathContext(pathContext)
.withAssetDetails(asset.name, asset.mediaType, duration, totalPages)
.withAssetDetails(asset.name, asset.mediaType, duration, totalPages, proxyType)
.withCommentTimestamp(commentTimestamp)
.withExplicitMention(payload.agent?.explicitMention)
.withAttachedFiles(attachedFileDetailsList)