Files
shumai/packages/core/src/operations/registry.ts
T
Yiling-J 76057ccd21 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.
2026-10-27 08:09:09 +08:00

51 lines
1.8 KiB
TypeScript

import type { OperationName, Surface } from '@shumai/operations'
import { createFolderOp } from './asset/create-folder'
import { deleteAssetOp } from './asset/delete-asset'
import { listAssetsOp } from './asset/list-assets'
import { moveAssetsOp } from './asset/move-assets'
import { renameAssetOp } from './asset/rename-asset'
import type { Operation } from './implement'
import { listProjectsOp } from './project/list-projects'
type ImplementedName<TOps extends readonly Operation[]> = TOps[number]['definition']['name']
type MissingNames<TOps extends readonly Operation[]> = Exclude<OperationName, ImplementedName<TOps>>
/**
* Type-level guard: compiling fails with `missingImplementations` listing any definition in
* `@shumai/operations` that has no implementation here.
*/
function defineRegistry<const TOps extends readonly Operation[]>(
ops: TOps &
([MissingNames<TOps>] extends [never]
? unknown
: { missingImplementations: MissingNames<TOps> }),
): TOps {
return ops
}
/** Every implemented Operation. Each definition in `@shumai/operations` must appear exactly once. */
export const operations = defineRegistry([
listProjectsOp,
listAssetsOp,
createFolderOp,
renameAssetOp,
moveAssetsOp,
deleteAssetOp,
] as const)
const operationsByName = new Map<string, Operation>()
for (const op of operations) {
if (operationsByName.has(op.definition.name)) {
throw new Error(`Duplicate operation implementation "${op.definition.name}"`)
}
operationsByName.set(op.definition.name, op)
}
export function getOperation(name: string): Operation | undefined {
return operationsByName.get(name)
}
export function getOperationsForSurface(surface: Surface): Operation[] {
return operations.filter((op) => op.definition.surfaces.includes(surface))
}