mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +00:00
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.
51 lines
1.8 KiB
TypeScript
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))
|
|
}
|