diff --git a/packages/agent/src/database-session-storage.test.ts b/packages/agent/src/database-session-storage.test.ts index 2b3a4fe2..eb98708e 100644 --- a/packages/agent/src/database-session-storage.test.ts +++ b/packages/agent/src/database-session-storage.test.ts @@ -2,7 +2,7 @@ import { prisma } from '@shumai/db' import { setupTestDbHooks } from '@shumai/db/test' import { s3Service } from '@shumai/core/src/s3/s3' import { type SessionTreeEntry } from '@earendil-works/pi-agent-core' -import { describe, expect, it, vi } from 'vitest' +import { describe, expect, it, vi, afterEach } from 'vitest' import { DatabaseSessionStorage } from './database-session-storage' import { agentService } from '@shumai/core/src/agent/agent' import { createAgentSession } from './index' @@ -13,6 +13,10 @@ import * as screenshotModule from './tools/screenshot' describe('DatabaseSessionStorage', () => { setupTestDbHooks() + afterEach(() => { + vi.restoreAllMocks() + }) + const mockModelConfig: PrismaJson.ModelConfig = { reasoning: false, input: ['text'], @@ -392,7 +396,7 @@ describe('DatabaseSessionStorage', () => { expect(entries[2].id).toBe('01C') }) - it('should use the current comment ID for media tools in subsequent turns', async () => { + it('should use the current comment ID for media tools in subsequent turns (image)', async () => { const { agent, user, team } = await setupTestData() // Create a project and asset to satisfy FK constraints @@ -441,6 +445,58 @@ describe('DatabaseSessionStorage', () => { // 4. Assert that the tools were instantiated using the new comment ID, not the original one expect(createAnalyzeImageToolSpy).toHaveBeenCalledWith(asset.id, 'new-comment-456') + expect(createScreenshotToolSpy).not.toHaveBeenCalled() + }) + + it('should use the current comment ID for media tools in subsequent turns (video)', async () => { + const { agent, user, team } = await setupTestData() + + // Create a project and asset to satisfy FK constraints + const project = await prisma.project.create({ + data: { name: 'Test Project', teamId: team.id }, + }) + const asset = await prisma.asset.create({ + data: { + id: 'asset-456', + name: 'test.mp4', + type: 'file', + mediaType: 'video/mp4', + projectId: project.id, + status: 'uploaded', + }, + }) + + // 1. Create a session with an original comment ID and an asset ID using the storage class + const initialStorage = await DatabaseSessionStorage.create({ + agentId: agent.id, + userId: user.id, + cwd: '/test/cwd', + assetId: asset.id, + userCommentId: 'original-comment-123', + }) + const sessionId = initialStorage.sessionId + + // 2. Spy on media tools constructors + const createAnalyzeImageToolSpy = vi.spyOn(analyzeImageModule, 'createAnalyzeImageTool') + const createScreenshotToolSpy = vi.spyOn(screenshotModule, 'createScreenshotTool') + + // 3. Resume the session via createAgentSession with a NEW comment ID + await createAgentSession({ + teamId: 't1', + agentId: agent.id, + providerName: 'test-provider', + modelId: 'test-model', + systemPrompt: 'Test prompt', + teamSkills: [], + allowedDomains: [], + sessionId: sessionId, + userId: user.id, + userCommentId: 'new-comment-456', + providers: [], + }) + + // 4. Assert that the tools were instantiated using the new comment ID, not the original one expect(createScreenshotToolSpy).toHaveBeenCalledWith(asset.id, 'new-comment-456') + expect(createAnalyzeImageToolSpy).not.toHaveBeenCalled() }) }) diff --git a/packages/agent/src/index.ts b/packages/agent/src/index.ts index f4eb3686..6ae42d62 100644 --- a/packages/agent/src/index.ts +++ b/packages/agent/src/index.ts @@ -15,6 +15,7 @@ import { agentService } from '@shumai/core/src/agent/agent' import { DatabaseSessionStorage } from './database-session-storage' import { createAnalyzeImageTool } from './tools/analyze-image' import { createScreenshotTool } from './tools/screenshot' +import { getSimpleMediaType } from './workflows/agent-chat-prompt-builder' import { createCreateFileTool } from './tools/create-file' import { createCreateFolderTool } from './tools/create-folder' import { createCreateVersionTool } from './tools/create-version' @@ -212,10 +213,17 @@ export async function createAgentSession(params: CreateAgentSessionParams) { const mediaTools: AgentTool[] = [] if (assetId) { - mediaTools.push( - createAnalyzeImageTool(assetId, userCommentId), - createScreenshotTool(assetId, userCommentId), - ) + const asset = await prisma.asset.findUnique({ + where: { id: assetId }, + }) + if (asset?.mediaType) { + const type = getSimpleMediaType(asset.mediaType) + if (type === 'image') { + mediaTools.push(createAnalyzeImageTool(assetId, userCommentId)) + } else if (type === 'video') { + mediaTools.push(createScreenshotTool(assetId, userCommentId)) + } + } } const sandboxedBash = createSandboxedBashTool(process.cwd(), skillEnvs, { diff --git a/packages/agent/src/workflows/agent-autofill.test.ts b/packages/agent/src/workflows/agent-autofill.test.ts index 2988298c..f58fe802 100644 --- a/packages/agent/src/workflows/agent-autofill.test.ts +++ b/packages/agent/src/workflows/agent-autofill.test.ts @@ -283,4 +283,30 @@ describe('Agent Autofill Workflow', () => { tmpDir: '/tmp/test-dir', }) }) + + it('should fail with non-retryable ApplicationFailure for unsupported audio files', async () => { + mockActivities.getAssetActivity.mockResolvedValue({ + id: 'a1', + storageKey: { key: 'asset-key' }, + project: { id: 'p1', teamId: 't1' }, + mediaType: 'audio/mp3', + }) + + const task = await prisma.workflowTask.create({ + data: { + type: 'ai_metadata_autofill', + status: 'pending', + assetId: 'a1', + }, + }) + + await expect(agentAutofillMedia(task)).rejects.toThrow('unsupported media type for autofill: audio/mp3') + + // Verify task failed status + expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({ + taskId: task.id, + status: 'failed', + output: { error: 'unsupported media type for autofill: audio/mp3' }, + }) + }) }) diff --git a/packages/agent/src/workflows/agent-autofill.ts b/packages/agent/src/workflows/agent-autofill.ts index 340fa09f..84e620f8 100644 --- a/packages/agent/src/workflows/agent-autofill.ts +++ b/packages/agent/src/workflows/agent-autofill.ts @@ -65,6 +65,14 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise { // 2. Prepare Data (Images) const isImage = asset.mediaType?.startsWith('image/') || false + const isVideo = asset.mediaType?.startsWith('video/') || false + + if (!isImage && !isVideo) { + throw ApplicationFailure.create({ + message: `unsupported media type for autofill: ${asset.mediaType}`, + nonRetryable: true, + }) + } const download = await executeActivity(transcodeWorkerQueue, downloadMediaToTmpActivity, { assetKey: key,