mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
feat(agent): add asset management tools and CLI commands (#387)
* feat(agent): add asset management tools and CLI commands * fix(core): resolve code review findings for reparenting cycles, agent deniedTools update, and CLI folder delete
This commit is contained in:
@@ -257,6 +257,68 @@ describe('AgentService', () => {
|
||||
expect(updated.user.image).toBe('files/01JK23456789ABCDEF01234567')
|
||||
})
|
||||
|
||||
test('Update Agent preserves deniedTools when omitted, and allows explicit empty array', async () => {
|
||||
const db = prisma
|
||||
const svc = new AgentService()
|
||||
const { team, provider, model, agent: legacyAgent } = await setupTestData(db)
|
||||
|
||||
// Legacy agent has no deniedTools in config, update without deniedTools should fall back to DEFAULT_DENIED_TOOLS
|
||||
const updatedLegacy = await svc.updateAgent({
|
||||
agentId: legacyAgent.id,
|
||||
name: 'Updated Legacy',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: legacyAgent.providerId!,
|
||||
modelId: legacyAgent.modelId!,
|
||||
thinkingLevel: 'medium',
|
||||
})
|
||||
expect((updatedLegacy.config as PrismaJson.AgentConfig).deniedTools).toEqual(['delete_asset'])
|
||||
|
||||
// Create new agent with default deniedTools
|
||||
const agent = await svc.createAgent({
|
||||
teamId: team.id,
|
||||
name: 'New Agent with Defaults',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
thinkingLevel: 'medium',
|
||||
})
|
||||
|
||||
expect(agent).not.toBeNull()
|
||||
|
||||
// Agent created with default deniedTools: ['delete_asset']
|
||||
const createdConfig = agent!.config as PrismaJson.AgentConfig
|
||||
expect(createdConfig.deniedTools).toEqual(['delete_asset'])
|
||||
|
||||
// Update without deniedTools: should preserve ['delete_asset']
|
||||
const updatedWithoutDenied = await svc.updateAgent({
|
||||
agentId: agent!.id,
|
||||
name: 'Updated Name',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: agent!.providerId!,
|
||||
modelId: agent!.modelId!,
|
||||
thinkingLevel: 'medium',
|
||||
})
|
||||
const config1 = updatedWithoutDenied.config as PrismaJson.AgentConfig
|
||||
expect(config1.deniedTools).toEqual(['delete_asset'])
|
||||
|
||||
// Update with explicit deniedTools: []
|
||||
const updatedWithEmptyDenied = await svc.updateAgent({
|
||||
agentId: agent!.id,
|
||||
name: 'Updated Name 2',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: agent!.providerId!,
|
||||
modelId: agent!.modelId!,
|
||||
thinkingLevel: 'medium',
|
||||
deniedTools: [],
|
||||
})
|
||||
const config2 = updatedWithEmptyDenied.config as PrismaJson.AgentConfig
|
||||
expect(config2.deniedTools).toEqual([])
|
||||
})
|
||||
|
||||
test('Create and Update Agent with Preset Avatars', async () => {
|
||||
const db = prisma
|
||||
const svc = new AgentService()
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
AgentSessionInfo,
|
||||
AgentInfo,
|
||||
AgentType,
|
||||
DEFAULT_DENIED_TOOLS,
|
||||
} from '@shumai/dtos'
|
||||
import {
|
||||
paginateQuery,
|
||||
@@ -208,7 +209,7 @@ export class AgentService {
|
||||
model: modelId || '',
|
||||
thinkingLevel,
|
||||
systemPrompt,
|
||||
deniedTools: deniedTools || [],
|
||||
deniedTools: deniedTools ?? [...DEFAULT_DENIED_TOOLS],
|
||||
avatarPreset,
|
||||
}
|
||||
|
||||
@@ -351,7 +352,10 @@ export class AgentService {
|
||||
model: modelId || '',
|
||||
thinkingLevel,
|
||||
systemPrompt,
|
||||
deniedTools: deniedTools || [],
|
||||
deniedTools:
|
||||
deniedTools !== undefined
|
||||
? deniedTools
|
||||
: (currentConfig.deniedTools ?? [...DEFAULT_DENIED_TOOLS]),
|
||||
avatarPreset: newAvatarPreset,
|
||||
}
|
||||
|
||||
|
||||
@@ -626,6 +626,39 @@ describe('AssetService', () => {
|
||||
).rejects.toThrow('Can only move one file at a time into a version stack')
|
||||
})
|
||||
|
||||
it('rejects reparenting a folder into itself', async () => {
|
||||
const { user, assets } = await setupBasicAssets()
|
||||
await expect(
|
||||
assetService.reparentAssets({
|
||||
assetIds: [assets.folderA.id],
|
||||
newParentId: assets.folderA.id,
|
||||
creatorId: user.id,
|
||||
}),
|
||||
).rejects.toThrow('Cannot move an asset into itself or its own descendant')
|
||||
})
|
||||
|
||||
it('rejects reparenting a folder into its own descendant', async () => {
|
||||
const { user, assets, project } = await setupBasicAssets()
|
||||
const subfolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'subfolderA',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
parentId: assets.folderA.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
assetService.reparentAssets({
|
||||
assetIds: [assets.folderA.id],
|
||||
newParentId: subfolder.id,
|
||||
creatorId: user.id,
|
||||
}),
|
||||
).rejects.toThrow('Cannot move an asset into itself or its own descendant')
|
||||
})
|
||||
|
||||
describe('fileCount verification', () => {
|
||||
it('createAsset should increment parent fileCount', async () => {
|
||||
const { user, assets, project } = await setupBasicAssets()
|
||||
|
||||
@@ -223,6 +223,20 @@ export class AssetService {
|
||||
|
||||
if (assetsToMove.length === 0) return
|
||||
|
||||
// Check if newParentId is equal to or a descendant of any asset being moved
|
||||
const ancestors = await tx.$queryRaw<{ id: string }[]>`
|
||||
WITH RECURSIVE ancestor AS (
|
||||
SELECT id, parent_id FROM assets WHERE id = ${newParent.id}
|
||||
UNION ALL
|
||||
SELECT a.id, a.parent_id FROM assets a
|
||||
INNER JOIN ancestor d ON a.id = d.parent_id
|
||||
)
|
||||
SELECT id FROM ancestor WHERE id IN (${Prisma.join(req.assetIds)}) LIMIT 1;
|
||||
`
|
||||
if (ancestors.length > 0) {
|
||||
throw new Error('Cannot move an asset into itself or its own descendant')
|
||||
}
|
||||
|
||||
if (newParent.type === AssetType.version_stack) {
|
||||
for (const a of assetsToMove) {
|
||||
if (a.type !== AssetType.file) {
|
||||
|
||||
Reference in New Issue
Block a user