mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 03:14:11 +00:00
feat(agent): enable read_asset tool for autofill agent (#396)
* feat(agent): enable read_asset tool for autofill agent * fix(transcode): trigger autofill task for pdf assets * fix(transcode): allow autofill task creation on all file assets * fix(agent): require targetAssetId and teamId for autofill read_asset authorization * feat(agent): instruct autofill agent to report failure directly if read_asset fails
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { createReadAssetTool } from './read-asset'
|
||||
import { createReadAssetTool, type ReadAssetAuthContext } from './read-asset'
|
||||
import {
|
||||
prisma,
|
||||
WorkflowTaskType,
|
||||
@@ -641,4 +641,135 @@ describe('readAssetTool', () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ReadAssetAuthContext & Security Boundaries', () => {
|
||||
it('throws authorization error if chat agent lacks userId', async () => {
|
||||
const tool = createReadAssetTool({
|
||||
agentType: 'chat',
|
||||
teamId: 'team-1',
|
||||
})
|
||||
await expect(
|
||||
tool.execute('call-1', {
|
||||
assetId: 'asset-1',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
videoConfig: null,
|
||||
docConfig: null,
|
||||
}),
|
||||
).rejects.toThrow('User ID is required for authorization.')
|
||||
})
|
||||
|
||||
it('throws error if autofill agent lacks targetAssetId', async () => {
|
||||
const tool = createReadAssetTool({
|
||||
agentType: 'autofill',
|
||||
teamId: 'team-1',
|
||||
} as unknown as ReadAssetAuthContext)
|
||||
|
||||
await expect(
|
||||
tool.execute('call-1', {
|
||||
assetId: 'asset-1',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
videoConfig: null,
|
||||
docConfig: null,
|
||||
}),
|
||||
).rejects.toThrow('Target asset ID is required for autofill agent authorization.')
|
||||
})
|
||||
|
||||
it('throws error if autofill agent lacks teamId', async () => {
|
||||
const tool = createReadAssetTool({
|
||||
agentType: 'autofill',
|
||||
targetAssetId: 'asset-1',
|
||||
} as unknown as ReadAssetAuthContext)
|
||||
|
||||
await expect(
|
||||
tool.execute('call-1', {
|
||||
assetId: 'asset-1',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
videoConfig: null,
|
||||
docConfig: null,
|
||||
}),
|
||||
).rejects.toThrow('Team ID is required for autofill agent authorization.')
|
||||
})
|
||||
|
||||
it('denies autofill agent if asset belongs to another team', async () => {
|
||||
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
|
||||
id: 'asset-1',
|
||||
project: { teamId: 'other-team' },
|
||||
} as unknown as Asset)
|
||||
|
||||
const tool = createReadAssetTool({
|
||||
agentType: 'autofill',
|
||||
teamId: 'team-1',
|
||||
targetAssetId: 'asset-1',
|
||||
})
|
||||
|
||||
await expect(
|
||||
tool.execute('call-1', {
|
||||
assetId: 'asset-1',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
videoConfig: null,
|
||||
docConfig: null,
|
||||
}),
|
||||
).rejects.toThrow('Access denied: asset asset-1 does not belong to team team-1.')
|
||||
})
|
||||
|
||||
it('denies autofill agent if reading an unrelated asset not matching targetAssetId', async () => {
|
||||
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
|
||||
id: 'other-asset',
|
||||
parentId: null,
|
||||
project: { teamId: 'team-1' },
|
||||
} as unknown as Asset)
|
||||
|
||||
const tool = createReadAssetTool({
|
||||
agentType: 'autofill',
|
||||
teamId: 'team-1',
|
||||
targetAssetId: 'target-asset-id',
|
||||
})
|
||||
|
||||
await expect(
|
||||
tool.execute('call-1', {
|
||||
assetId: 'other-asset',
|
||||
annotationId: null,
|
||||
imageConfig: null,
|
||||
videoConfig: null,
|
||||
docConfig: null,
|
||||
}),
|
||||
).rejects.toThrow('Access denied: autofill agent can only read target asset target-asset-id.')
|
||||
})
|
||||
|
||||
it('allows autofill agent to read target asset without userId', async () => {
|
||||
vi.mocked(prisma.asset.findUnique).mockResolvedValue({
|
||||
id: 'target-asset-id',
|
||||
name: 'test.png',
|
||||
mediaType: 'image/png',
|
||||
storageKey: { key: 'raw/test.png' },
|
||||
media: { proxyType: 'image' },
|
||||
project: { teamId: 'team-1' },
|
||||
} as unknown as Asset)
|
||||
|
||||
const tool = createReadAssetTool({
|
||||
agentType: 'autofill',
|
||||
teamId: 'team-1',
|
||||
targetAssetId: 'target-asset-id',
|
||||
})
|
||||
|
||||
const result = await tool.execute('call-1', {
|
||||
assetId: 'target-asset-id',
|
||||
annotationId: null,
|
||||
s3KeyOnly: true,
|
||||
imageConfig: null,
|
||||
videoConfig: null,
|
||||
docConfig: null,
|
||||
})
|
||||
|
||||
expect(authzService.hasPermission).not.toHaveBeenCalled()
|
||||
expect(result.content.length).toBe(1)
|
||||
expect((result.content[0] as { type: 'text'; text: string }).text).toContain(
|
||||
'target-asset-id',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -141,7 +141,27 @@ export function isPlainTextAsset(mediaType: string, filename: string): boolean {
|
||||
const MAX_TEXT_BYTES = 50 * 1024
|
||||
const MAX_TEXT_LINES = 1000
|
||||
|
||||
export function createReadAssetTool(userId: string): AgentTool<typeof readAssetSchema> {
|
||||
export type ReadAssetAuthContext =
|
||||
| {
|
||||
userId?: string
|
||||
teamId?: string
|
||||
agentType?: 'chat' | 'embedding'
|
||||
targetAssetId?: string
|
||||
}
|
||||
| {
|
||||
userId?: undefined
|
||||
teamId: string
|
||||
agentType: 'autofill'
|
||||
targetAssetId: string
|
||||
}
|
||||
|
||||
export function createReadAssetTool(
|
||||
auth: string | ReadAssetAuthContext,
|
||||
): AgentTool<typeof readAssetSchema> {
|
||||
const authContext: ReadAssetAuthContext =
|
||||
typeof auth === 'string' ? { userId: auth, agentType: 'chat' } : auth
|
||||
const { userId, teamId, agentType = 'chat', targetAssetId } = authContext
|
||||
|
||||
return {
|
||||
name: 'read_asset',
|
||||
label: 'Read Asset',
|
||||
@@ -154,16 +174,48 @@ export function createReadAssetTool(userId: string): AgentTool<typeof readAssetS
|
||||
execute: async (_toolCallId, params) => {
|
||||
const { assetId, annotationId, s3KeyOnly, videoConfig, docConfig } = params
|
||||
|
||||
if (!userId) {
|
||||
throw new Error('User ID is required for authorization.')
|
||||
}
|
||||
if (agentType === 'autofill' && !userId) {
|
||||
if (!targetAssetId) {
|
||||
throw new Error('Target asset ID is required for autofill agent authorization.')
|
||||
}
|
||||
if (!teamId) {
|
||||
throw new Error('Team ID is required for autofill agent authorization.')
|
||||
}
|
||||
|
||||
await authzService.hasPermission({
|
||||
user: { id: userId } as User,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: assetId,
|
||||
})
|
||||
// System background execution for Autofill Agent
|
||||
const assetRecord = await prisma.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
include: { project: true, teamRootFolder: true },
|
||||
})
|
||||
if (!assetRecord) {
|
||||
throw new Error(`Asset with ID ${assetId} not found.`)
|
||||
}
|
||||
|
||||
// Strict team boundary check
|
||||
const assetTeamId = assetRecord.project?.teamId || assetRecord.teamRootFolder?.id
|
||||
if (!assetTeamId || assetTeamId !== teamId) {
|
||||
throw new Error(`Access denied: asset ${assetId} does not belong to team ${teamId}.`)
|
||||
}
|
||||
|
||||
// Strict target asset boundary check (allows the target asset or its version stack children)
|
||||
if (assetRecord.id !== targetAssetId && assetRecord.parentId !== targetAssetId) {
|
||||
throw new Error(
|
||||
`Access denied: autofill agent can only read target asset ${targetAssetId}.`,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Chat agents or user-driven executions strictly require a userId and full ACL validation
|
||||
if (!userId) {
|
||||
throw new Error('User ID is required for authorization.')
|
||||
}
|
||||
|
||||
await authzService.hasPermission({
|
||||
user: { id: userId } as User,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: assetId,
|
||||
})
|
||||
}
|
||||
|
||||
let asset = await prisma.asset.findUnique({
|
||||
where: { id: assetId },
|
||||
|
||||
Reference in New Issue
Block a user