feat(agent): improve timestamp precision and proxy selection for video screenshots (#374)

This commit is contained in:
Yiling-J
2026-09-01 21:15:28 +08:00
committed by GitHub
parent 64b68e7d63
commit 5a754bcb94
5 changed files with 82 additions and 10 deletions
@@ -119,7 +119,7 @@ describe('serializeContextToXml', () => {
<folder id="ast_01JC03_PARENT" name="Footage" />
</ancestors>
</current_asset>
<position type="time" seconds="14.25" />
<position type="time" seconds="14.2500" />
<annotation />
<attached_files>
<file id="ast_01JC05_ATT1" name="brand_guidelines_2026.pdf" type="file" media_type="pdf" path="Brand/brand_guidelines_2026.pdf" />
@@ -315,7 +315,7 @@ describe('serializeContextToXml', () => {
expect(refOrder).toEqual(['a_asset', 'm_asset', 'z_asset'])
})
it('normalizes float formatting with fixed .toFixed(2)', () => {
it('normalizes float formatting with fixed precision (toFixed(4) for position, toFixed(2) for duration)', () => {
const context1: ShumaiMessageContext = {
currentAsset: { id: '1', name: 'a', type: 'file', durationSeconds: 14 },
position: { type: 'time', seconds: 5.5 },
@@ -326,9 +326,9 @@ describe('serializeContextToXml', () => {
}
expect(serializeContextToXml(context1)).toContain('duration_seconds="14.00"')
expect(serializeContextToXml(context1)).toContain('seconds="5.50"')
expect(serializeContextToXml(context1)).toContain('seconds="5.5000"')
expect(serializeContextToXml(context2)).toContain('duration_seconds="14.00"')
expect(serializeContextToXml(context2)).toContain('seconds="5.50"')
expect(serializeContextToXml(context2)).toContain('seconds="5.5000"')
})
})
})
+3 -3
View File
@@ -1225,12 +1225,12 @@ export class TranscodeService {
// 4. Extract each screenshot
for (const t of timestamps) {
const outName = `shot-${t.toFixed(3)}-${ulid()}.webp`
const outName = `shot-${t.toFixed(4)}-${ulid()}.webp`
const localShotPath = path.join(tmpDir, outName)
const args = [
'-ss',
t.toFixed(3),
t.toFixed(4),
'-i',
videoPath,
'-vframes',
@@ -1248,7 +1248,7 @@ export class TranscodeService {
const isMatch =
commentTimestamp !== undefined &&
commentTimestamp !== null &&
Math.abs(t - commentTimestamp) < 1e-4
Math.abs(t - commentTimestamp) <= 1e-3
// 5. Overlay annotations if timestamp matches commentTimestamp (within float tolerance)
if (isMatch && params.annotations && params.annotations.length > 0) {
+2 -2
View File
@@ -26,7 +26,7 @@ function formatAttr(name: string, value: string | undefined): string {
* 1. Tag sequence: <user>, <thread>, <current_asset>, <position>, <annotation>, <attached_files>, <referenced_assets>
* 2. Attribute sequence on every tag follows fixed, invariant order.
* 3. Arrays (attachedFiles, referencedAssets) sorted by id ascending.
* 4. Floats formatted with .toFixed(2), integers rounded.
* 4. Time position floats formatted with .toFixed(4), duration with .toFixed(2), integers rounded.
* 5. String values escaped via escapeXmlAttr.
* 6. Exact 2-space indentation and LF newlines.
*/
@@ -101,7 +101,7 @@ export function serializeContextToXml(context?: ShumaiMessageContext | null): st
// 3. <position ... />
if (context.position) {
if (context.position.type === 'time') {
lines.push(` <position type="time" seconds="${context.position.seconds.toFixed(2)}" />`)
lines.push(` <position type="time" seconds="${context.position.seconds.toFixed(4)}" />`)
} else if (context.position.type === 'page') {
lines.push(` <position type="page" page="${Math.round(context.position.page)}" />`)
}
@@ -107,4 +107,63 @@ describe('takeVideoScreenshotsWorkflow', () => {
},
})
})
it('should prefer highest-resolution video transcode proxy over master storageKey', async () => {
const task: WorkflowTask = {
id: 'task-screenshot-proxy',
assetId: 'asset-video-proxy',
type: WorkflowTaskType.transcode_screenshot,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
screenshot: {
start: 1.2345,
end: 1.2345,
count: 1,
commentTimestamp: 1.2345,
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-proxy',
storageKey: { key: 'raw-master.mp4' },
mediaType: 'video/mp4',
media: {
videoTranscodes: [
{ key: 'proxy-720p.mp4', height: 720, width: 1280 },
{ key: 'proxy-1080p.mp4', height: 1080, width: 1920 },
{ key: 'proxy-360p.mp4', height: 360, width: 640 },
],
},
})
mockActivities.takeScreenshotsActivity.mockResolvedValue([
{ key: 'files/asset-video-proxy/screenshots/shot1.webp', timestamp: 1.2345 },
])
await takeVideoScreenshotsWorkflow(task)
expect(mockActivities.takeScreenshotsActivity).toHaveBeenCalledWith({
assetKey: 'proxy-1080p.mp4',
assetId: 'asset-video-proxy',
start: 1.2345,
end: 1.2345,
count: 1,
commentTimestamp: 1.2345,
annotations: [],
})
})
})
@@ -17,10 +17,23 @@ export async function takeVideoScreenshotsWorkflow(task: WorkflowTask): Promise<
}
const { asset, key } = await fetchAssetWithKey(workerQueue, task.assetId)
let targetKey = key
if (asset?.media) {
const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo
if (mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) {
const sorted = [...mediaInfo.videoTranscodes]
.filter((t) => t.key)
.sort((a, b) => (b.height || 0) - (a.height || 0))
if (sorted.length > 0 && sorted[0].key) {
targetKey = sorted[0].key
}
}
}
const { takeScreenshotsActivity } = getActivities()
const screenshots = await executeActivity(workerQueue, takeScreenshotsActivity, {
assetKey: key,
assetKey: targetKey,
assetId: asset.id,
start: payload.screenshot.start,
end: payload.screenshot.end,