feat(operations): add shared Operation layer for agent capabilities (#494)

Introduce @shumai/operations (pure definitions: Zod schemas, metadata,
CLI hints) and @shumai/core/src/operations (implementOperation and the
runOperation pipeline: validate, authorize, prepare, execute, validate
output, audit).

Migrate list_assets, create_folder, rename_asset, move_assets and
delete_asset to Operations, and add list_projects. The agent now builds
these tools from the registry, and the folder create/rename, file
rename and reparent routes call the same Operations.

Audit logs gain a nullable via column recording the surface. A lint
rule keeps the Prisma client out of Operation implementations.
This commit is contained in:
Yiling-J
2026-10-27 08:09:09 +08:00
committed by GitHub
parent 47ee794ae4
commit 76057ccd21
65 changed files with 2372 additions and 1149 deletions
+15 -40
View File
@@ -6,6 +6,7 @@ import { projectService } from '@shumai/core/src/project/project'
import { assetService } from '@shumai/core/src/asset/asset'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import type { ProjectInfo } from '@shumai/dtos'
import { moveAssetsOp, runOperation } from '@shumai/core/src/operations'
vi.mock('./middleware/auth', () => ({
authMiddleware: async (
@@ -35,6 +36,10 @@ vi.mock('@shumai/core/src/project/project', () => ({
},
}))
vi.mock('@shumai/core/src/asset/asset')
vi.mock('@shumai/core/src/operations', async (importOriginal) => ({
...(await importOriginal<typeof import('@shumai/core/src/operations')>()),
runOperation: vi.fn(),
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
@@ -181,9 +186,11 @@ describe('project api', () => {
})
})
it('POST /projects/:projectId/reparent', async () => {
vi.mocked(projectService.getProjectTeam).mockResolvedValue('t1')
vi.mocked(assetService.reparentAssets).mockResolvedValue(undefined)
it('POST /projects/:projectId/reparent runs move_assets', async () => {
vi.mocked(runOperation).mockResolvedValue({
movedAssetIds: ['asset_1', 'asset_2'],
newParentId: 'new_parent_id',
})
const res = await app.request('/projects/p/reparent', {
method: 'POST',
@@ -195,43 +202,11 @@ describe('project api', () => {
})
expect(res.status).toBe(204)
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'new_parent_id',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'asset_1',
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Edit,
type: ResourceType.Asset,
id: 'asset_2',
})
expect(assetService.reparentAssets).toHaveBeenCalledWith({
newParentId: 'new_parent_id',
assetIds: ['asset_1', 'asset_2'],
creatorId: 'user1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_reparent',
teamId: 't1',
userId: 'user1',
projectId: 'p',
itemId: 'asset_1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_reparent',
teamId: 't1',
userId: 'user1',
projectId: 'p',
itemId: 'asset_2',
})
expect(runOperation).toHaveBeenCalledWith(
moveAssetsOp,
{ userId: 'user1', via: 'api' },
{ assetIds: ['asset_1', 'asset_2'], newParentId: 'new_parent_id' },
)
})
it('POST /projects/:projectId/copy', async () => {