diff --git a/packages/agent/src/index.ts b/packages/agent/src/index.ts index f8d2618d..1e693b71 100644 --- a/packages/agent/src/index.ts +++ b/packages/agent/src/index.ts @@ -228,11 +228,14 @@ export async function createAgentSession(params: CreateAgentSessionParams) { const userCommentId = passedUserCommentId !== undefined ? passedUserCommentId : metadata.userCommentId - const mediaTools: AgentTool[] = [ - createAnalyzeImageTool(userId, userCommentId), - createScreenshotTool(userId, userCommentId), - createReadPdfPagesTool(userId, userCommentId), - ] + const mediaTools: AgentTool[] = [] + if (userId) { + mediaTools.push( + createAnalyzeImageTool(userId, userCommentId), + createScreenshotTool(userId, userCommentId), + createReadPdfPagesTool(userId, userCommentId), + ) + } const sandboxedBash = createSandboxedBashTool(process.cwd(), skillEnvs, { getBlockedHost: () => sandboxState.blockedHost, diff --git a/packages/agent/src/tools/analyze-image.test.ts b/packages/agent/src/tools/analyze-image.test.ts index c6249666..5ecba36b 100644 --- a/packages/agent/src/tools/analyze-image.test.ts +++ b/packages/agent/src/tools/analyze-image.test.ts @@ -139,7 +139,7 @@ describe('analyzeImageTool', () => { contentType: 'image/webp', } as unknown as { buffer: Buffer; contentType: string }) - const tool = createAnalyzeImageTool(undefined, 'comment-1') + const tool = createAnalyzeImageTool('user-1', 'comment-1') const result = await tool.execute('call-1', { assetId: 'asset-1' }) expect(prisma.workflowTask.create).toHaveBeenCalledWith({ diff --git a/packages/agent/src/tools/analyze-image.ts b/packages/agent/src/tools/analyze-image.ts index 129c06d5..0991627d 100644 --- a/packages/agent/src/tools/analyze-image.ts +++ b/packages/agent/src/tools/analyze-image.ts @@ -1,7 +1,7 @@ import { Type } from '@sinclair/typebox' import { type AgentTool } from '@earendil-works/pi-agent-core' import { type ImageContent } from '@earendil-works/pi-ai' -import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db' +import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db' import { s3Service } from '@shumai/core/src/s3/s3' import { workflowService } from '@shumai/workflow-core' import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz' @@ -13,7 +13,7 @@ const analyzeImageSchema = Type.Object({ }) export function createAnalyzeImageTool( - userId?: string, + userId: string, userCommentId?: string | null, ): AgentTool { return { @@ -26,22 +26,20 @@ export function createAnalyzeImageTool( try { const assetId = params.assetId - if (userId) { - const user = await prisma.user.findUnique({ where: { id: userId } }) - if (!user) { - return { - content: [{ type: 'text', text: `User not found: ${userId}` }], - details: {}, - } + if (!userId) { + return { + content: [{ type: 'text', text: 'User ID is required for authorization.' }], + details: {}, } - await authzService.hasPermission({ - user, - permission: Permission.Read, - type: ResourceType.Asset, - id: assetId, - }) } + await authzService.hasPermission({ + user: { id: userId } as User, + permission: Permission.Read, + type: ResourceType.Asset, + id: assetId, + }) + const asset = await prisma.asset.findUnique({ where: { id: assetId }, include: { storageKey: true }, diff --git a/packages/agent/src/tools/read-pdf-pages.test.ts b/packages/agent/src/tools/read-pdf-pages.test.ts index b0a6fdf6..510c6ecb 100644 --- a/packages/agent/src/tools/read-pdf-pages.test.ts +++ b/packages/agent/src/tools/read-pdf-pages.test.ts @@ -56,11 +56,11 @@ vi.mock('@shumai/core/src/authz/authz', () => ({ describe('readPdfPagesTool', () => { beforeEach(() => { vi.clearAllMocks() + vi.mocked(authzService.hasPermission).mockResolvedValue() }) it('should trigger pdfPages transcode workflow and return image outputs', async () => { vi.mocked(prisma.user.findUnique).mockResolvedValue({ id: 'user-1' } as User) - vi.mocked(authzService.hasPermission).mockResolvedValue() vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'asset-1', @@ -152,7 +152,7 @@ describe('readPdfPagesTool', () => { }) it('should return error text if start page is less than 1', async () => { - const tool = createReadPdfPagesTool() + const tool = createReadPdfPagesTool('user-1') const result = await tool.execute('call-1', { assetId: 'asset-1', start: 0, end: 5 }) expect(result.content[0]).toEqual({ @@ -162,7 +162,7 @@ describe('readPdfPagesTool', () => { }) it('should return error text if start page is greater than end page', async () => { - const tool = createReadPdfPagesTool() + const tool = createReadPdfPagesTool('user-1') const result = await tool.execute('call-1', { assetId: 'asset-1', start: 5, end: 2 }) expect(result.content[0]).toEqual({ @@ -172,7 +172,7 @@ describe('readPdfPagesTool', () => { }) it('should return error text if page range exceeds maximum limit of 20', async () => { - const tool = createReadPdfPagesTool() + const tool = createReadPdfPagesTool('user-1') const result = await tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 25 }) expect(result.content[0]).toEqual({ @@ -184,7 +184,7 @@ describe('readPdfPagesTool', () => { it('should return error text if asset not found', async () => { vi.mocked(prisma.asset.findUnique).mockResolvedValue(null) - const tool = createReadPdfPagesTool() + const tool = createReadPdfPagesTool('user-1') const result = await tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 3 }) expect(result.content[0]).toEqual({ @@ -200,7 +200,7 @@ describe('readPdfPagesTool', () => { media: { proxyType: 'image' }, } as unknown as Asset) - const tool = createReadPdfPagesTool() + const tool = createReadPdfPagesTool('user-1') const result = await tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 3 }) expect(result.content[0]).toEqual({ @@ -216,7 +216,7 @@ describe('readPdfPagesTool', () => { media: null, } as unknown as Asset) - const tool = createReadPdfPagesTool() + const tool = createReadPdfPagesTool('user-1') const result = await tool.execute('call-1', { assetId: 'asset-1', start: 1, end: 3 }) expect(result.content[0]).toEqual({ diff --git a/packages/agent/src/tools/read-pdf-pages.ts b/packages/agent/src/tools/read-pdf-pages.ts index 0a6e3011..ca0cab8f 100644 --- a/packages/agent/src/tools/read-pdf-pages.ts +++ b/packages/agent/src/tools/read-pdf-pages.ts @@ -1,7 +1,7 @@ import { Type } from '@sinclair/typebox' import { type AgentTool } from '@earendil-works/pi-agent-core' import { type ImageContent } from '@earendil-works/pi-ai' -import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db' +import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db' import { s3Service } from '@shumai/core/src/s3/s3' import { workflowService } from '@shumai/workflow-core' import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz' @@ -15,7 +15,7 @@ const readPdfPagesSchema = Type.Object({ }) export function createReadPdfPagesTool( - userId?: string, + userId: string, userCommentId?: string | null, ): AgentTool { return { @@ -66,22 +66,20 @@ export function createReadPdfPagesTool( const targetAssetId = params.assetId - if (userId) { - const user = await prisma.user.findUnique({ where: { id: userId } }) - if (!user) { - return { - content: [{ type: 'text', text: `User not found: ${userId}` }], - details: {}, - } + if (!userId) { + return { + content: [{ type: 'text', text: 'User ID is required for authorization.' }], + details: {}, } - await authzService.hasPermission({ - user, - permission: Permission.Read, - type: ResourceType.Asset, - id: targetAssetId, - }) } + await authzService.hasPermission({ + user: { id: userId } as User, + permission: Permission.Read, + type: ResourceType.Asset, + id: targetAssetId, + }) + const asset = await prisma.asset.findUnique({ where: { id: targetAssetId }, }) diff --git a/packages/agent/src/tools/screenshot.ts b/packages/agent/src/tools/screenshot.ts index 198fb333..0636d23c 100644 --- a/packages/agent/src/tools/screenshot.ts +++ b/packages/agent/src/tools/screenshot.ts @@ -1,7 +1,7 @@ import { Type } from '@sinclair/typebox' import { type AgentTool } from '@earendil-works/pi-agent-core' import { type ImageContent } from '@earendil-works/pi-ai' -import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db' +import { prisma, WorkflowTaskType, WorkflowTaskStatus, type User } from '@shumai/db' import { s3Service } from '@shumai/core/src/s3/s3' import { workflowService } from '@shumai/workflow-core' import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz' @@ -16,7 +16,7 @@ const screenshotSchema = Type.Object({ }) export function createScreenshotTool( - userId?: string, + userId: string, userCommentId?: string | null, ): AgentTool { return { @@ -29,22 +29,20 @@ export function createScreenshotTool( try { const assetId = params.assetId - if (userId) { - const user = await prisma.user.findUnique({ where: { id: userId } }) - if (!user) { - return { - content: [{ type: 'text', text: `User not found: ${userId}` }], - details: {}, - } + if (!userId) { + return { + content: [{ type: 'text', text: 'User ID is required for authorization.' }], + details: {}, } - await authzService.hasPermission({ - user, - permission: Permission.Read, - type: ResourceType.Asset, - id: assetId, - }) } + await authzService.hasPermission({ + user: { id: userId } as User, + permission: Permission.Read, + type: ResourceType.Asset, + id: assetId, + }) + const asset = await prisma.asset.findUnique({ where: { id: assetId }, })