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
+77
View File
@@ -0,0 +1,77 @@
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { defineOperation, type OperationDefinition } from './define'
function baseDefinition(): OperationDefinition<string, z.ZodObject<{ id: z.ZodString }>> {
return {
name: 'get_thing',
title: 'Get Thing',
description: 'Get a thing by ID.',
input: z.object({ id: z.string() }),
output: z.object({ id: z.string() }),
surfaces: ['agent', 'mcp'],
annotations: { readOnly: true, destructive: false },
}
}
describe('defineOperation', () => {
it('returns a frozen definition', () => {
const def = defineOperation(baseDefinition())
expect(def.name).toBe('get_thing')
expect(Object.isFrozen(def)).toBe(true)
})
it.each(['GetThing', 'get-thing', 'get thing', '_get', 'get__thing', '1get', 'get_'])(
'rejects non snake_case name %s',
(name) => {
expect(() => defineOperation({ ...baseDefinition(), name })).toThrow('expected snake_case')
},
)
it('requires a title and description', () => {
expect(() => defineOperation({ ...baseDefinition(), title: ' ' })).toThrow(
'must have a title and a description',
)
expect(() => defineOperation({ ...baseDefinition(), description: '' })).toThrow(
'must have a title and a description',
)
})
it('rejects empty or duplicated surfaces', () => {
expect(() => defineOperation({ ...baseDefinition(), surfaces: [] })).toThrow('each surface')
expect(() => defineOperation({ ...baseDefinition(), surfaces: ['agent', 'agent'] })).toThrow(
'each surface',
)
})
it('rejects contradictory annotations', () => {
expect(() =>
defineOperation({
...baseDefinition(),
annotations: { readOnly: true, destructive: true },
}),
).toThrow('cannot be both readOnly and destructive')
})
it('requires a cli spec for the cli surface', () => {
expect(() => defineOperation({ ...baseDefinition(), surfaces: ['cli'] })).toThrow('no cli spec')
})
it('rejects cli specs that reference unknown input fields', () => {
expect(() =>
defineOperation({
...baseDefinition(),
surfaces: ['cli'],
// Cast simulates a definition built without type checking (e.g. from JS)
cli: { command: ['get'], positional: ['missing' as 'id'] },
}),
).toThrow('unknown input "missing"')
expect(() =>
defineOperation({
...baseDefinition(),
surfaces: ['cli'],
cli: { command: ['get'], flags: { ['other' as 'id']: ['-o'] } },
}),
).toThrow('unknown input "other"')
})
})