Files
shumai/packages/agent/src/workflows/agent-autofill.ts
T

171 lines
5.4 KiB
TypeScript
Raw Normal View History

import type { WorkflowTask } from '@shumai/db'
import { executeActivity, getActivities, TaskQueueAgent } from '@shumai/workflow-core'
import { ApplicationFailure } from '@temporalio/workflow'
2026-05-19 16:05:09 +08:00
export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
2026-05-19 16:05:09 +08:00
const {
updateTaskStatusActivity,
getAssetActivity,
getProjectAutofillFieldsActivity,
getAgentAutofillContextActivity,
2026-05-19 16:05:09 +08:00
autofillAiActivity,
updateTaskUsageActivity,
updateAssetMetadataActivity,
createCommentActivity,
updateCommentActivity,
getAgentWorkerQueueActivity,
} = getActivities()
let placeholderCommentId: string | undefined
let agentWorkerQueue = ''
2026-05-19 16:05:09 +08:00
try {
// 0. Discover queues
agentWorkerQueue = await executeActivity(TaskQueueAgent, getAgentWorkerQueueActivity)
2026-05-19 16:05:09 +08:00
// Update status to processing
await executeActivity(agentWorkerQueue, updateTaskStatusActivity, {
2026-05-19 16:05:09 +08:00
taskId: task.id,
status: 'processing',
})
// 0. Create Placeholder Comment
const payload = task.payload
const placeholder = await executeActivity(agentWorkerQueue, createCommentActivity, {
2026-05-19 16:05:09 +08:00
assetId: task.assetId,
message: '__AUTOFILL__',
sessionId: payload?.agent?.sessionId || task.id,
agentId: payload?.agent?.agentId || 'default',
2026-05-19 16:05:09 +08:00
})
placeholderCommentId = placeholder.id
// 1. Get Asset
const asset = await executeActivity(agentWorkerQueue, getAssetActivity, task.assetId)
if (!asset || !asset.project) {
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
// 2. Get Project Autofill Fields
const fields = await executeActivity(
agentWorkerQueue,
getProjectAutofillFieldsActivity,
projectId,
)
2026-05-19 16:05:09 +08:00
if (fields.length === 0) {
if (placeholderCommentId) {
await executeActivity(agentWorkerQueue, updateCommentActivity, {
2026-05-19 16:05:09 +08:00
commentId: placeholderCommentId,
message: 'Autofill completed: No autofill fields defined in project.',
})
}
await executeActivity(agentWorkerQueue, updateTaskStatusActivity, {
2026-05-19 16:05:09 +08:00
taskId: task.id,
status: 'completed',
})
return
}
// 3. Fetch Agent Context
const context = await executeActivity(agentWorkerQueue, getAgentAutofillContextActivity, {
teamId,
})
// 4. Call AI Service (inspects asset on demand via read_asset)
const mediaInfo = asset.media as PrismaJson.MediaInfo | null
const duration = typeof mediaInfo?.duration === 'number' ? mediaInfo.duration : undefined
const pageCount =
typeof mediaInfo?.metadata?.totalFrames === 'number'
? mediaInfo.metadata.totalFrames
: typeof mediaInfo?.frames === 'number'
? mediaInfo.frames
: undefined
2026-05-19 16:05:09 +08:00
const aiResult = await executeActivity(agentWorkerQueue, autofillAiActivity, {
teamId,
assetId: asset.id,
assetName: asset.name,
mediaType: asset.mediaType ?? undefined,
duration,
pageCount,
projectId: asset.projectId ?? undefined,
fields: fields.map(
(f: { key: string; config: Record<string, unknown>; description?: string | null }) => ({
id: f.key,
config: f.config as unknown as PrismaJson.FieldConfig,
description: f.description,
}),
),
context,
2026-05-19 16:05:09 +08:00
})
if (aiResult.usage) {
// Update Usage
await executeActivity(agentWorkerQueue, 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)
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) {
await executeActivity(agentWorkerQueue, updateAssetMetadataActivity, {
2026-05-19 16:05:09 +08:00
assetId: asset.id,
metadata: metadataUpdates,
})
}
// 7. Update Placeholder Comment
if (placeholderCommentId) {
await executeActivity(agentWorkerQueue, updateCommentActivity, {
2026-05-19 16:05:09 +08:00
commentId: placeholderCommentId,
message: 'Autofill completed successfully.',
sessionId: aiResult.sessionId,
2026-05-19 16:05:09 +08:00
})
}
// Update status to completed
await executeActivity(agentWorkerQueue, updateTaskStatusActivity, {
2026-05-19 16:05:09 +08:00
taskId: task.id,
status: 'completed',
})
} catch (err) {
console.error(`AgentAutofillMedia failed for task ${task.id}:`, err)
2026-05-19 16:05:09 +08:00
// Update placeholder comment with error message
if (placeholderCommentId && agentWorkerQueue) {
2026-05-19 16:05:09 +08:00
try {
await executeActivity(agentWorkerQueue, updateCommentActivity, {
2026-05-19 16:05:09 +08:00
commentId: placeholderCommentId,
message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`,
})
} catch (commentErr) {
console.error('Failed to update error comment:', commentErr)
}
}
// Update status to failed
if (agentWorkerQueue) {
await executeActivity(agentWorkerQueue, 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
}
}
export const agentAutofillWorkflow = agentAutofillMedia