mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 16:24:17 +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,50 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user