feat(cli): generate shumai-cli commands from Operations with commander (#496)

Rebuild the CLI on commander. Commands for every cli-surface Operation
(project ls, ls, mkdir, rename, move/mv, delete/rm) are generated from
@shumai/operations: positionals, options, choices, numeric parsing and
help text come from the Zod input schema. They call POST /api/ops/:name
with x-shumai-client: cli.

Existing invocations keep working. Adds --json, ls --type/--first/
--after, typo suggestions, and generated help. upload and create-version
stay hand-written. Drops the unused @shumai/core dependency.
This commit is contained in:
Yiling-J
2026-10-27 08:09:10 +08:00
committed by GitHub
parent 4d61786799
commit e44938cb8f
25 changed files with 976 additions and 745 deletions
+4 -1
View File
@@ -20,7 +20,7 @@ export const listAssets = defineOperation({
.min(1)
.max(MAX_PAGE_SIZE)
.optional()
.describe(`Maximum number of assets to return. Default is 20, maximum ${MAX_PAGE_SIZE}.`),
.describe(`Maximum number of assets to return (at most ${MAX_PAGE_SIZE}).`),
after: z
.string()
.optional()
@@ -35,6 +35,9 @@ export const listAssets = defineOperation({
annotations: { readOnly: true, destructive: false, idempotent: true },
cli: {
command: ['ls'],
description: 'List the files and folders inside a parent folder.',
flags: { parent: ['-p', '--parent'] },
// The CLI shows up to the maximum page by default; agents use the smaller default.
defaults: { first: MAX_PAGE_SIZE },
},
})
+7
View File
@@ -73,5 +73,12 @@ describe('defineOperation', () => {
cli: { command: ['get'], flags: { ['other' as 'id']: ['-o'] } },
}),
).toThrow('unknown input "other"')
expect(() =>
defineOperation({
...baseDefinition(),
surfaces: ['cli'],
cli: { command: ['get'], defaults: { ['extra' as 'id']: 'x' } },
}),
).toThrow('unknown input "extra"')
})
})
+9 -1
View File
@@ -29,12 +29,16 @@ export interface OperationAnnotations {
export interface CliSpec<TInput, TOutput> {
/** Command path, e.g. `['mkdir']` or `['project', 'ls']`. */
command: readonly string[]
/** Help text for the command; defaults to the Operation description (which is written for LLMs). */
description?: string
/** Alternative names for the last command segment, e.g. `['mv']`. */
aliases?: readonly string[]
/** Input fields taken from positional arguments, in order. An array field must be last (variadic). */
positional?: readonly (keyof TInput & string)[]
/** Flag spellings for input fields, e.g. `{ parent: ['-p', '--parent'] }`. */
flags?: { readonly [K in keyof TInput & string]?: readonly string[] }
/** Input values the CLI uses when the user does not pass them (e.g. a larger page size). */
defaults?: { readonly [K in keyof TInput & string]?: TInput[K] }
/** Human-friendly rendering of the output; the CLI falls back to a generic renderer. */
format?(output: TOutput): string
}
@@ -93,7 +97,11 @@ export function defineOperation<
}
if (def.cli) {
const fields = new Set(Object.keys(def.input.shape))
const referenced = [...(def.cli.positional ?? []), ...Object.keys(def.cli.flags ?? {})]
const referenced = [
...(def.cli.positional ?? []),
...Object.keys(def.cli.flags ?? {}),
...Object.keys(def.cli.defaults ?? {}),
]
for (const field of referenced) {
if (!fields.has(field)) {
throw new Error(`Operation "${def.name}" cli spec references unknown input "${field}"`)
@@ -24,5 +24,6 @@ export const listProjects = defineOperation({
annotations: { readOnly: true, destructive: false, idempotent: true },
cli: {
command: ['project', 'ls'],
description: 'List the projects you can access, with their root folder IDs.',
},
})