mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 01:39:32 +00:00
* feat(asset-read): expose read_asset on MCP and the CLI Enable the mcp and cli surfaces for read_asset and get_asset_render. - MCP: add per-Operation tool extensions. The read presenter attaches up to 8 images (2 MB each) inline, lists image links, supports inlineImages: false, and tells the model to call get_asset_render when a render is pending. Storage keys are stripped from the result. - CLI: generated commands can flatten object inputs into prefixed flags (cli.flatten) and omit fields (cli.omit); add per-command extensions. New `read` and `render` commands print image links, download them with --out, and support --no-wait with a printed `render <taskId>` hint. - Round frame timestamps in labels to 3 decimals. * fix(mcp): make tool input schemas portable to single-type dialects Nullable inputs were emitted as `type: ["number", "null"]` (or a `null` anyOf branch), which clients that map tool schemas onto an OpenAPI-style subset (e.g. Gemini function declarations) may reject. - Make read_asset's startPage, endPage, and degree optional instead of nullable. - Move null handling into a shared dropNullOptionals helper in @shumai/operations that also strips nulls inside nested config objects. The agent uses it, and the MCP server applies it before the SDK validates arguments, so models that send null keep working. - Test that no Operation input schema (and no listed MCP tool) uses a nullable type. * feat(asset-download): share download_asset across agent, MCP and CLI Turn the agent-only download_asset tool into an Operation on all three surfaces. - download_asset takes up to 50 file or version stack IDs and returns presigned links. proxy (default true) picks the web preview when there is one, as the agent always did; proxy: false returns the original. Folders are rejected. Read is checked on every ID before anything runs. - Agent: an extension saves the files into the .pi sandbox and keeps the agent-only storage key mode (e.g. frames from read_asset). Agent tool extensions gain description, parameterOverrides and intercept. - CLI: `shumai-cli download <assetId...> [--out <dir>] [--original]` streams files to disk without overwriting existing names. - read_asset's unsupported-type error now names the right way to download on each surface (download_asset, or `shumai-cli download`). - Add the Download Asset toggle to agent settings.
128 lines
5.2 KiB
TypeScript
128 lines
5.2 KiB
TypeScript
import type { z } from 'zod'
|
|
|
|
/**
|
|
* Channels through which an Operation can be invoked by an agent.
|
|
*
|
|
* - `agent`: native tool of Shumai's built-in agent
|
|
* - `mcp`: tool on Shumai's MCP server
|
|
* - `cli`: command of `shumai-cli` (served by `POST /api/ops/:name`)
|
|
*
|
|
* REST routes are not a surface: they call Operations explicitly.
|
|
*/
|
|
export const SURFACES = ['agent', 'mcp', 'cli'] as const
|
|
export type Surface = (typeof SURFACES)[number]
|
|
|
|
/** Where an invocation came from: one of the surfaces, or a REST route (`api`). Recorded in audit logs. */
|
|
export type Via = Surface | 'api'
|
|
|
|
/** Behavioral hints, mapped to MCP tool annotations and surface-specific safety gates. */
|
|
export interface OperationAnnotations {
|
|
/** The Operation does not modify any state. */
|
|
readOnly: boolean
|
|
/** The Operation may irreversibly remove data (e.g. delete). */
|
|
destructive: boolean
|
|
/** Repeating the call with the same input has no additional effect. */
|
|
idempotent?: boolean
|
|
}
|
|
|
|
/** How an Operation is exposed as a `shumai-cli` command. */
|
|
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[] }
|
|
/**
|
|
* Object input fields exposed as one flag per sub-field, keyed by flag prefix: with
|
|
* `{ videoConfig: 'video' }`, `videoConfig.start` becomes `--video-start`. An empty prefix
|
|
* uses the sub-field name alone (`--degree`). Object fields must be flattened or omitted.
|
|
*/
|
|
flatten?: { readonly [K in keyof TInput & string]?: string }
|
|
/** Input fields the CLI does not expose. They must be optional. */
|
|
omit?: readonly (keyof TInput & 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
|
|
}
|
|
|
|
/**
|
|
* A single capability exposed to agents, defined once and shared by every surface.
|
|
*
|
|
* Definitions are pure metadata and schemas: they must not depend on server code so
|
|
* that clients such as the CLI can import them. Implementations live in
|
|
* `@shumai/core/src/operations`.
|
|
*/
|
|
export interface OperationDefinition<
|
|
TName extends string = string,
|
|
TInput extends z.ZodObject = z.ZodObject,
|
|
TOutput extends z.ZodType = z.ZodType,
|
|
> {
|
|
/** Unique snake_case name, used as the agent/MCP tool name and the `/api/ops/:name` path. */
|
|
readonly name: TName
|
|
/** Short human-readable label. */
|
|
readonly title: string
|
|
/** What the Operation does, written for an LLM choosing between tools. */
|
|
readonly description: string
|
|
readonly input: TInput
|
|
readonly output: TOutput
|
|
readonly surfaces: readonly Surface[]
|
|
readonly annotations: OperationAnnotations
|
|
readonly cli?: CliSpec<z.input<TInput>, z.output<TOutput>>
|
|
}
|
|
|
|
export type AnyOperationDefinition = OperationDefinition<string, z.ZodObject, z.ZodType>
|
|
|
|
const NAME_PATTERN = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/
|
|
|
|
/**
|
|
* Define an Operation. Validates the metadata eagerly so mistakes fail at import time.
|
|
*/
|
|
export function defineOperation<
|
|
const TName extends string,
|
|
TInput extends z.ZodObject,
|
|
TOutput extends z.ZodType,
|
|
>(def: OperationDefinition<TName, TInput, TOutput>): OperationDefinition<TName, TInput, TOutput> {
|
|
if (!NAME_PATTERN.test(def.name)) {
|
|
throw new Error(`Invalid operation name "${def.name}": expected snake_case`)
|
|
}
|
|
if (!def.title.trim() || !def.description.trim()) {
|
|
throw new Error(`Operation "${def.name}" must have a title and a description`)
|
|
}
|
|
if (def.surfaces.length === 0 || new Set(def.surfaces).size !== def.surfaces.length) {
|
|
throw new Error(`Operation "${def.name}" must list each surface at most once`)
|
|
}
|
|
if (def.annotations.readOnly && def.annotations.destructive) {
|
|
throw new Error(`Operation "${def.name}" cannot be both readOnly and destructive`)
|
|
}
|
|
if (def.surfaces.includes('cli') && !def.cli) {
|
|
throw new Error(`Operation "${def.name}" has the cli surface but no cli spec`)
|
|
}
|
|
if (def.cli) {
|
|
const fields = new Set(Object.keys(def.input.shape))
|
|
const referenced = [
|
|
...(def.cli.positional ?? []),
|
|
...Object.keys(def.cli.flags ?? {}),
|
|
...Object.keys(def.cli.defaults ?? {}),
|
|
...Object.keys(def.cli.flatten ?? {}),
|
|
...(def.cli.omit ?? []),
|
|
]
|
|
for (const field of referenced) {
|
|
if (!fields.has(field)) {
|
|
throw new Error(`Operation "${def.name}" cli spec references unknown input "${field}"`)
|
|
}
|
|
}
|
|
for (const field of def.cli.omit ?? []) {
|
|
if (!(def.input.shape[field] as z.ZodType).safeParse(undefined).success) {
|
|
throw new Error(`Operation "${def.name}" cli spec omits required input "${field}"`)
|
|
}
|
|
}
|
|
}
|
|
return Object.freeze(def)
|
|
}
|