mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 14:49:24 +00:00
fix(agent): require mandatory userId for media tools and optimize authz
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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<typeof analyzeImageSchema> {
|
||||
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 },
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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<typeof readPdfPagesSchema> {
|
||||
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 },
|
||||
})
|
||||
|
||||
@@ -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<typeof screenshotSchema> {
|
||||
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 },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user