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:
yiling
2026-10-26 22:33:30 +08:00
parent b7dccad6e5
commit bc3368f3e3
40 changed files with 1323 additions and 579 deletions
@@ -0,0 +1,60 @@
import { z } from 'zod'
import { defineOperation } from '../define'
/** Most assets one download call accepts. */
export const MAX_DOWNLOAD_ASSETS = 50
const downloadFileSchema = z.object({
assetId: z
.string()
.describe('The asset this file belongs to (the latest version for a version stack).'),
name: z.string().describe('File name to save it as.'),
kind: z
.enum(['proxy', 'original'])
.describe('"proxy" is the web preview version; "original" is the uploaded file.'),
size: z.number().describe('Size in bytes.'),
mimeType: z.string(),
url: z.string().describe('Short-lived download link (about 1 hour).'),
/** Storage key; only returned to Shumai's built-in agent. */
key: z.string().optional(),
})
export const downloadAssetOutputSchema = z.object({
files: z.array(downloadFileSchema),
})
export type DownloadAssetOutput = z.infer<typeof downloadAssetOutputSchema>
export const downloadAsset = defineOperation({
name: 'download_asset',
title: 'Download Asset',
description:
`Gets download links for up to ${MAX_DOWNLOAD_ASSETS} files. ` +
'By default it returns the web preview version (proxy) when there is one, e.g. the transcoded video or the web image, which is smaller and easier to process; set proxy to false for the original uploaded file. ' +
'Files without a preview (e.g. audio, archives, text) always return the original. ' +
'A version stack returns its latest version. Folders are not supported: download the files inside them instead. ' +
'Links expire after about an hour.',
input: z.object({
assetIds: z
.array(z.string().min(1))
.min(1)
.max(MAX_DOWNLOAD_ASSETS)
.describe(`IDs of the files or version stacks to download (1 to ${MAX_DOWNLOAD_ASSETS}).`),
proxy: z
.boolean()
.optional()
.describe(
'Download the web preview version when there is one (default true). Set to false for the original uploaded file.',
),
}),
output: downloadAssetOutputSchema,
surfaces: ['agent', 'mcp', 'cli'],
annotations: { readOnly: true, destructive: false, idempotent: true },
cli: {
command: ['download'],
description:
'Download files into a folder (the current folder by default). Saves the web preview version when there is one; use --original for the uploaded file.',
positional: ['assetIds'],
// The CLI exposes `--original` instead of a `--proxy` flag that is on by default.
omit: ['proxy'],
},
})
+1
View File
@@ -8,4 +8,5 @@ export { listAssets } from './asset/list-assets'
export { moveAssets } from './asset/move-assets'
export { renameAsset } from './asset/rename-asset'
export * from './asset/read-asset'
export * from './asset/download-asset'
export { listProjects } from './project/list-projects'
+3
View File
@@ -19,6 +19,7 @@ describe('operation registry', () => {
expect(operationDefinitions.map((d) => d.name).sort()).toEqual([
'create_folder',
'delete_asset',
'download_asset',
'get_asset_render',
'list_assets',
'list_projects',
@@ -47,6 +48,7 @@ describe('operation registry', () => {
expect(destructive.map((d) => d.name)).toEqual(['delete_asset'])
const readOnly = operationDefinitions.filter((d) => d.annotations.readOnly)
expect(readOnly.map((d) => d.name).sort()).toEqual([
'download_asset',
'get_asset_render',
'list_assets',
'list_projects',
@@ -96,6 +98,7 @@ describe('operation registry', () => {
['delete_asset', ['delete', 'rm']],
['read_asset', ['read']],
['get_asset_render', ['render']],
['download_asset', ['download']],
])
})
+2
View File
@@ -1,5 +1,6 @@
import { createFolder } from './asset/create-folder'
import { deleteAsset } from './asset/delete-asset'
import { downloadAsset } from './asset/download-asset'
import { listAssets } from './asset/list-assets'
import { moveAssets } from './asset/move-assets'
import { getAssetRender, readAsset } from './asset/read-asset'
@@ -20,6 +21,7 @@ export const operationDefinitions = [
deleteAsset,
readAsset,
getAssetRender,
downloadAsset,
] as const
export type RegisteredOperationDefinition = (typeof operationDefinitions)[number]