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:
Yiling-J
2026-09-08 14:19:03 +08:00
committed by GitHub
parent 3a05332cd6
commit 04fd571bd9
10 changed files with 548 additions and 144 deletions
+132 -1
View File
@@ -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',
)
})
})
})