2026-11-11 16:05:18 +08:00
|
|
|
import { getProxyType } from '@shumai/core/src/utils/mime'
|
2026-06-01 23:16:22 +08:00
|
|
|
import type { WorkflowTask } from '@shumai/db'
|
2026-10-19 15:35:08 +08:00
|
|
|
import {
|
|
|
|
|
ApplicationFailure,
|
|
|
|
|
updateTaskStatusActivity,
|
|
|
|
|
updateTaskUsageActivity,
|
|
|
|
|
} from '@shumai/workflow-core'
|
|
|
|
|
import {
|
|
|
|
|
autofillAiActivity,
|
|
|
|
|
createCommentActivity,
|
|
|
|
|
getAgentAutofillContextActivity,
|
|
|
|
|
getAssetActivity,
|
|
|
|
|
getProjectAutofillFieldsActivity,
|
|
|
|
|
updateAssetMetadataActivity,
|
|
|
|
|
} from '../activities/agent'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-27 23:40:14 +08:00
|
|
|
export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
2026-05-19 16:05:09 +08:00
|
|
|
try {
|
|
|
|
|
// Update status to processing
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateTaskStatusActivity({
|
2026-05-19 16:05:09 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'processing',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 1. Get Asset
|
2026-10-19 15:35:08 +08:00
|
|
|
const asset = await getAssetActivity(task.assetId)
|
2026-09-08 14:19:03 +08:00
|
|
|
if (!asset || !asset.project) {
|
2026-05-28 09:57:30 +08:00
|
|
|
throw ApplicationFailure.create({ message: 'Asset or project not found', nonRetryable: true })
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
const teamId = asset.project.teamId
|
|
|
|
|
const projectId = asset.project.id
|
|
|
|
|
|
2026-09-08 14:19:03 +08:00
|
|
|
// 2. Get Project Autofill Fields
|
2026-10-19 15:35:08 +08:00
|
|
|
const fields = await getProjectAutofillFieldsActivity(projectId)
|
2026-05-19 16:05:09 +08:00
|
|
|
if (fields.length === 0) {
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateTaskStatusActivity({
|
2026-05-19 16:05:09 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'completed',
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 14:19:03 +08:00
|
|
|
// 3. Fetch Agent Context
|
2026-10-19 15:35:08 +08:00
|
|
|
const context = await getAgentAutofillContextActivity({
|
2026-05-27 23:40:14 +08:00
|
|
|
teamId,
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-08 14:19:03 +08:00
|
|
|
// 4. Call AI Service (inspects asset on demand via read_asset)
|
|
|
|
|
const mediaInfo = asset.media as PrismaJson.MediaInfo | null
|
2026-09-08 14:52:37 +08:00
|
|
|
const proxyType = mediaInfo?.proxyType || getProxyType(asset.mediaType, asset.name) || undefined
|
|
|
|
|
|
|
|
|
|
const duration =
|
|
|
|
|
(proxyType === 'video' || proxyType === 'audio') && typeof mediaInfo?.duration === 'number'
|
|
|
|
|
? mediaInfo.duration
|
|
|
|
|
: undefined
|
|
|
|
|
|
2026-09-08 14:19:03 +08:00
|
|
|
const pageCount =
|
2026-09-08 14:52:37 +08:00
|
|
|
proxyType === 'pdf'
|
|
|
|
|
? typeof mediaInfo?.metadata?.totalFrames === 'number'
|
|
|
|
|
? mediaInfo.metadata.totalFrames
|
|
|
|
|
: typeof mediaInfo?.frames === 'number'
|
|
|
|
|
? mediaInfo.frames
|
|
|
|
|
: undefined
|
|
|
|
|
: undefined
|
2026-09-08 14:19:03 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const aiResult = await autofillAiActivity({
|
2026-11-11 16:05:18 +08:00
|
|
|
taskId: task.id,
|
2026-05-19 16:05:09 +08:00
|
|
|
teamId,
|
2026-08-15 10:25:46 +08:00
|
|
|
assetId: asset.id,
|
2026-09-08 14:19:03 +08:00
|
|
|
assetName: asset.name,
|
|
|
|
|
mediaType: asset.mediaType ?? undefined,
|
|
|
|
|
duration,
|
|
|
|
|
pageCount,
|
2026-08-15 10:25:46 +08:00
|
|
|
projectId: asset.projectId ?? undefined,
|
2026-10-19 15:35:08 +08:00
|
|
|
fields: fields.map((f) => ({
|
|
|
|
|
id: f.key,
|
|
|
|
|
config: (f.config as unknown as PrismaJson.FieldConfig) ?? {},
|
|
|
|
|
description: f.description,
|
|
|
|
|
})),
|
2026-05-27 23:40:14 +08:00
|
|
|
context,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (aiResult.usage) {
|
|
|
|
|
// Update Usage
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateTaskUsageActivity({
|
2026-05-19 16:05:09 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
inputTokens: aiResult.usage.inputTokens,
|
|
|
|
|
outputTokens: aiResult.usage.outputTokens,
|
|
|
|
|
model: aiResult.usage.model,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6. Update Asset Metadata
|
|
|
|
|
const result = JSON.parse(aiResult.text)
|
2026-08-13 20:06:09 +08:00
|
|
|
const metadataUpdates = Object.entries(result)
|
|
|
|
|
.filter(([, value]) => value !== null)
|
|
|
|
|
.map(([key, value]) => ({
|
|
|
|
|
key,
|
|
|
|
|
value,
|
|
|
|
|
}))
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
if (metadataUpdates.length > 0) {
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateAssetMetadataActivity({
|
2026-05-19 16:05:09 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
metadata: metadataUpdates,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update status to completed
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateTaskStatusActivity({
|
2026-05-19 16:05:09 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'completed',
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
2026-05-27 23:40:14 +08:00
|
|
|
console.error(`AgentAutofillMedia failed for task ${task.id}:`, err)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-10-31 12:02:30 +08:00
|
|
|
// Create error comment
|
|
|
|
|
try {
|
|
|
|
|
const payload = task.payload
|
|
|
|
|
await createCommentActivity({
|
|
|
|
|
assetId: task.assetId,
|
|
|
|
|
message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
|
|
|
sessionId: payload?.agent?.sessionId || task.id,
|
|
|
|
|
agentId: payload?.agent?.agentId || 'default',
|
|
|
|
|
})
|
|
|
|
|
} catch (commentErr) {
|
|
|
|
|
console.error('Failed to create error comment:', commentErr)
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update status to failed
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateTaskStatusActivity({
|
|
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
output: { error: err instanceof Error ? err.message : String(err) },
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 23:40:14 +08:00
|
|
|
export const agentAutofillWorkflow = agentAutofillMedia
|