feat(agent): improve agent chat workflow and add media tools

This commit is contained in:
yiling
2026-06-05 21:25:08 +08:00
parent 87c82a38da
commit 25d12d1574
28 changed files with 1904 additions and 253 deletions
@@ -37,6 +37,10 @@ describe('Transcode Workflow', () => {
_activityName: 'downloadMediaToTmpActivity',
}),
cleanupTmpDirActivity: Object.assign(vi.fn(), { _activityName: 'cleanupTmpDirActivity' }),
takeScreenshotsActivity: Object.assign(vi.fn(), { _activityName: 'takeScreenshotsActivity' }),
overlayAnnotationsActivity: Object.assign(vi.fn(), {
_activityName: 'overlayAnnotationsActivity',
}),
}
beforeEach(() => {
@@ -234,4 +238,119 @@ describe('Transcode Workflow', () => {
output: { error: 'FFmpeg failed' },
})
})
it('should run screenshot workflow successfully', async () => {
const task: WorkflowTask = {
id: 'task-screenshot',
assetId: 'asset-video',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
output: null,
payload: {
projectId: 'proj-1',
screenshot: {
start: 0,
end: 10,
count: 2,
commentTimestamp: 5.0,
annotations: [],
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-video',
storageKey: { key: 'video.mp4' },
mediaType: 'video/mp4',
})
mockActivities.takeScreenshotsActivity.mockResolvedValue([
{ key: 'file/asset-video/screenshots/shot1.webp', timestamp: 0.0 },
{ key: 'file/asset-video/screenshots/shot2.webp', timestamp: 5.0 },
])
await transcodeMedia(task)
expect(mockActivities.takeScreenshotsActivity).toHaveBeenCalledWith({
assetKey: 'video.mp4',
assetId: 'asset-video',
start: 0,
end: 10,
count: 2,
commentTimestamp: 5.0,
annotations: [],
})
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: 'task-screenshot',
status: WorkflowTaskStatus.completed,
output: {
screenshots: [
{ key: 'file/asset-video/screenshots/shot1.webp', timestamp: 0.0 },
{ key: 'file/asset-video/screenshots/shot2.webp', timestamp: 5.0 },
],
},
})
})
it('should run image annotation overlay workflow successfully', async () => {
const task: WorkflowTask = {
id: 'task-image-ann',
assetId: 'asset-image',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
output: null,
payload: {
projectId: 'proj-1',
imageAnnotation: {
annotations: [{ type: 'box', color: '#ff0000', points: [] }],
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-image',
storageKey: { key: 'image.png' },
mediaType: 'image/png',
media: {
imageTranscodes: [{ key: 'image-transcoded.webp' }],
},
})
mockActivities.overlayAnnotationsActivity.mockResolvedValue(
'file/asset-image/annotations/ann1.webp',
)
await transcodeMedia(task)
expect(mockActivities.overlayAnnotationsActivity).toHaveBeenCalledWith({
assetKey: 'image-transcoded.webp',
assetId: 'asset-image',
annotations: [{ type: 'box', color: '#ff0000', points: [] }],
})
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: 'task-image-ann',
status: WorkflowTaskStatus.completed,
output: { key: 'file/asset-image/annotations/ann1.webp' },
})
})
})
@@ -16,6 +16,8 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
getTranscodeWorkerQueueActivity,
downloadMediaToTmpActivity,
cleanupTmpDirActivity,
takeScreenshotsActivity,
overlayAnnotationsActivity,
} = getActivities()
let tmpDir: string | undefined
@@ -31,6 +33,69 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
status: 'processing',
})
const payload = task.payload
if (payload?.screenshot) {
const asset = await executeActivity(workerQueue, getAssetActivity, task.assetId)
const key = asset?.storageKey?.key
if (!asset || !key) {
throw ApplicationFailure.create({
message: 'Asset not found or has no key',
nonRetryable: true,
})
}
const screenshots = await executeActivity(workerQueue, takeScreenshotsActivity, {
assetKey: key,
assetId: asset.id,
start: payload.screenshot.start,
end: payload.screenshot.end,
count: payload.screenshot.count,
commentTimestamp: payload.screenshot.commentTimestamp,
annotations: payload.screenshot.annotations,
})
// Update Task Status to completed with output
await executeActivity(workerQueue, updateTaskStatusActivity, {
taskId: task.id,
status: 'completed',
output: { screenshots },
})
return
}
if (payload?.imageAnnotation) {
const asset = await executeActivity(workerQueue, getAssetActivity, task.assetId)
let key = asset?.storageKey?.key
if (asset?.media) {
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo
if (mediaInfo.imageTranscodes && mediaInfo.imageTranscodes.length > 0) {
key = mediaInfo.imageTranscodes[0].key || key
}
}
if (!asset || !key) {
throw ApplicationFailure.create({
message: 'Asset not found or has no key',
nonRetryable: true,
})
}
const keyWithOverlay = await executeActivity(workerQueue, overlayAnnotationsActivity, {
assetKey: key,
assetId: asset.id,
annotations: payload.imageAnnotation.annotations,
})
// Update Task Status to completed with output
await executeActivity(workerQueue, updateTaskStatusActivity, {
taskId: task.id,
status: 'completed',
output: { key: keyWithOverlay },
})
return
}
await executeActivity(workerQueue, updateAssetStatusActivity, {
assetId: task.assetId,
status: 'processing',