mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 23:24:21 +00:00
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:
@@ -0,0 +1,23 @@
|
||||
import { createFolder } from '@shumai/operations'
|
||||
import { AuditAction } from '@shumai/dtos'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { implementOperation } from '../implement'
|
||||
import { toAssetSummary } from './to-asset-summary'
|
||||
|
||||
export const createFolderOp = implementOperation(createFolder, {
|
||||
authz: (input) => [{ permission: Permission.Edit, type: ResourceType.Asset, id: input.parent }],
|
||||
execute: async (input, ctx) =>
|
||||
toAssetSummary(
|
||||
await assetService.createAsset({
|
||||
name: input.name,
|
||||
parentId: input.parent,
|
||||
type: 'folder',
|
||||
creatorId: ctx.userId,
|
||||
agentId: ctx.agentId,
|
||||
}),
|
||||
),
|
||||
audit: (_input, output) => [
|
||||
{ action: AuditAction.folder_create, itemId: output.id, contextAssetId: output.id },
|
||||
],
|
||||
})
|
||||
@@ -0,0 +1,23 @@
|
||||
import { deleteAsset } from '@shumai/operations'
|
||||
import { AuditAction } from '@shumai/dtos'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { implementOperation } from '../implement'
|
||||
|
||||
export const deleteAssetOp = implementOperation(deleteAsset, {
|
||||
authz: (input) => [{ permission: Permission.Edit, type: ResourceType.Asset, id: input.assetId }],
|
||||
// Resolve type and team before deleting: the audit entry needs them afterwards.
|
||||
prepare: (input) => assetService.getAssetMeta(input.assetId),
|
||||
execute: async (input) => {
|
||||
await assetService.deleteAssets([input.assetId])
|
||||
return { deletedAssetId: input.assetId }
|
||||
},
|
||||
audit: (input, _output, _ctx, asset) => [
|
||||
{
|
||||
action: asset.type === 'folder' ? AuditAction.folder_delete : AuditAction.file_delete,
|
||||
itemId: input.assetId,
|
||||
teamId: asset.teamId,
|
||||
projectId: asset.projectId,
|
||||
},
|
||||
],
|
||||
})
|
||||
@@ -0,0 +1,25 @@
|
||||
import { DEFAULT_PAGE_SIZE, listAssets } from '@shumai/operations'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { implementOperation } from '../implement'
|
||||
import { toAssetSummary } from './to-asset-summary'
|
||||
|
||||
export const listAssetsOp = implementOperation(listAssets, {
|
||||
authz: (input) => [{ permission: Permission.Read, type: ResourceType.Asset, id: input.parent }],
|
||||
execute: async (input) => {
|
||||
const result = await assetService.listChildren({
|
||||
assetId: input.parent,
|
||||
assetType: input.type ?? 'all',
|
||||
first: input.first ?? DEFAULT_PAGE_SIZE,
|
||||
after: input.after,
|
||||
})
|
||||
return {
|
||||
data: result.data.map(toAssetSummary),
|
||||
pageInfo: {
|
||||
hasNextPage: Boolean(result.pageInfo.cursor),
|
||||
endCursor: result.pageInfo.cursor ?? null,
|
||||
},
|
||||
total: result.pageInfo.total ?? result.data.length,
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,30 @@
|
||||
import { moveAssets } from '@shumai/operations'
|
||||
import { AuditAction } from '@shumai/dtos'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { implementOperation } from '../implement'
|
||||
|
||||
export const moveAssetsOp = implementOperation(moveAssets, {
|
||||
authz: (input) => [
|
||||
{ permission: Permission.Edit, type: ResourceType.Asset, id: input.newParentId },
|
||||
...input.assetIds.map((id) => ({
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id,
|
||||
})),
|
||||
],
|
||||
execute: async (input, ctx) => {
|
||||
await assetService.reparentAssets({
|
||||
assetIds: input.assetIds,
|
||||
newParentId: input.newParentId,
|
||||
creatorId: ctx.userId,
|
||||
})
|
||||
return { movedAssetIds: input.assetIds, newParentId: input.newParentId }
|
||||
},
|
||||
audit: (input) =>
|
||||
input.assetIds.map((assetId) => ({
|
||||
action: AuditAction.asset_reparent,
|
||||
itemId: assetId,
|
||||
contextAssetId: input.newParentId,
|
||||
})),
|
||||
})
|
||||
@@ -0,0 +1,19 @@
|
||||
import { renameAsset } from '@shumai/operations'
|
||||
import { AuditAction } from '@shumai/dtos'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { implementOperation } from '../implement'
|
||||
import { toAssetSummary } from './to-asset-summary'
|
||||
|
||||
export const renameAssetOp = implementOperation(renameAsset, {
|
||||
authz: (input) => [{ permission: Permission.Edit, type: ResourceType.Asset, id: input.assetId }],
|
||||
execute: async (input) =>
|
||||
toAssetSummary(await assetService.updateAssetName({ id: input.assetId, name: input.name })),
|
||||
audit: (input, output) => [
|
||||
{
|
||||
action: output.type === 'folder' ? AuditAction.folder_update : AuditAction.file_update,
|
||||
itemId: input.assetId,
|
||||
contextAssetId: input.assetId,
|
||||
},
|
||||
],
|
||||
})
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { AssetSummary } from '@shumai/operations'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
|
||||
export function toAssetSummary(asset: AssetInfo): AssetSummary {
|
||||
return {
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
type: asset.type,
|
||||
size: Number(asset.sizeByte),
|
||||
projectId: asset.projectId ?? null,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user