mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 14:34:40 +00:00
feat: expose read_asset and download_asset on MCP and the CLI (#499)
* 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.
This commit is contained in:
@@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
import type { TSchema } from 'typebox'
|
||||
import { Compile } from 'typebox/compile'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { dropNullOptionals } from '@shumai/operations'
|
||||
import {
|
||||
getOperationsForSurface,
|
||||
runOperation,
|
||||
@@ -18,10 +19,27 @@ export type AgentOperationContext = Omit<OperationContext, 'via'>
|
||||
* and render the output (e.g. as image blocks).
|
||||
*/
|
||||
export interface AgentToolExtension {
|
||||
/** Replaces the Operation description for the agent (e.g. where files end up). */
|
||||
description?: string
|
||||
/** Extra parameters only the agent sees; they are removed before the Operation runs. */
|
||||
extraParameters?: z.ZodObject
|
||||
/**
|
||||
* Agent-facing versions of Operation input fields, e.g. a required field made optional
|
||||
* because an extra parameter can be used instead. The Operation still validates its own
|
||||
* input when it runs.
|
||||
*/
|
||||
parameterOverrides?: z.ZodObject
|
||||
/** Adjust the Operation input using the extra parameters. */
|
||||
toInput?(input: Record<string, unknown>, extra: Record<string, unknown>): Record<string, unknown>
|
||||
/**
|
||||
* Handle an agent-only mode without running the Operation (e.g. a storage key instead of
|
||||
* an asset). Return undefined to run the Operation.
|
||||
*/
|
||||
intercept?(
|
||||
input: Record<string, unknown>,
|
||||
extra: Record<string, unknown>,
|
||||
ctx: AgentOperationContext,
|
||||
): Promise<AgentToolResult<unknown> | undefined>
|
||||
present(output: unknown, extra: Record<string, unknown>): Promise<AgentToolResult<unknown>>
|
||||
}
|
||||
|
||||
@@ -33,9 +51,10 @@ export function registerAgentToolExtension(name: string, extension: AgentToolExt
|
||||
}
|
||||
|
||||
function toolInputSchema(op: Operation, extension?: AgentToolExtension): z.ZodObject {
|
||||
return extension?.extraParameters
|
||||
? op.definition.input.extend(extension.extraParameters.shape)
|
||||
: op.definition.input
|
||||
let schema = op.definition.input
|
||||
if (extension?.parameterOverrides) schema = schema.extend(extension.parameterOverrides.shape)
|
||||
if (extension?.extraParameters) schema = schema.extend(extension.extraParameters.shape)
|
||||
return schema
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,27 +72,13 @@ export function toToolParameters(op: Operation, extension?: AgentToolExtension):
|
||||
return schema as unknown as TSchema
|
||||
}
|
||||
|
||||
/**
|
||||
* Models sometimes send `null` for optional parameters they don't want to set. Drop those
|
||||
* so the input schema treats them as omitted; nulls for required fields still fail.
|
||||
*/
|
||||
export function dropNullOptionalFields(schema: z.ZodObject, params: unknown): unknown {
|
||||
if (!params || typeof params !== 'object' || Array.isArray(params)) return params
|
||||
const shape = schema.shape
|
||||
return Object.fromEntries(
|
||||
Object.entries(params).filter(
|
||||
([key, value]) => !(value === null && shape[key]?.safeParse(undefined).success),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/** Split agent-only parameters from the Operation input. */
|
||||
export function splitAgentParams(
|
||||
op: Operation,
|
||||
extension: AgentToolExtension | undefined,
|
||||
params: unknown,
|
||||
): { input: unknown; extra: Record<string, unknown> } {
|
||||
const cleaned = dropNullOptionalFields(toolInputSchema(op, extension), params)
|
||||
const cleaned = dropNullOptionals(toolInputSchema(op, extension), params)
|
||||
if (!extension?.extraParameters || !cleaned || typeof cleaned !== 'object') {
|
||||
return { input: cleaned, extra: {} }
|
||||
}
|
||||
@@ -93,10 +98,14 @@ export function toAgentTool(op: Operation, ctx: AgentOperationContext): AgentToo
|
||||
return {
|
||||
name: definition.name,
|
||||
label: definition.title,
|
||||
description: definition.description,
|
||||
description: extension?.description ?? definition.description,
|
||||
parameters: toToolParameters(op, extension),
|
||||
execute: async (_toolCallId, params) => {
|
||||
const { input, extra } = splitAgentParams(op, extension, params)
|
||||
if (extension?.intercept) {
|
||||
const handled = await extension.intercept(input as Record<string, unknown>, extra, ctx)
|
||||
if (handled) return handled
|
||||
}
|
||||
const output = await runOperation(op, { ...ctx, via: 'agent' }, input)
|
||||
if (extension) {
|
||||
return extension.present(output, extra)
|
||||
|
||||
Reference in New Issue
Block a user