mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 22:14:50 +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:
@@ -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