mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 10:06:56 +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:
@@ -3,6 +3,9 @@ import type { Command } from 'commander'
|
||||
import {
|
||||
createFolder,
|
||||
deleteAsset,
|
||||
downloadAsset,
|
||||
getAssetRender,
|
||||
readAsset,
|
||||
listAssets,
|
||||
listProjects,
|
||||
moveAssets,
|
||||
@@ -39,6 +42,27 @@ function setup() {
|
||||
|
||||
const folder = { id: 'f-1', name: 'Renders', type: 'folder', size: 0, projectId: 'p-1' }
|
||||
|
||||
const frames = {
|
||||
status: 'ready',
|
||||
summary: 'Extracted 2 frame(s) from "clip.mp4" (0s - 4s):',
|
||||
asset: { id: 'vid-1', name: 'clip.mp4', kind: 'video', mediaType: 'video/mp4' },
|
||||
details: [],
|
||||
images: [
|
||||
{ label: 'Frame at 0s', mimeType: 'image/webp', url: 'https://files.example/0.webp' },
|
||||
{ label: 'Frame at 4s', mimeType: 'image/webp', url: 'https://files.example/4.webp' },
|
||||
],
|
||||
}
|
||||
|
||||
const pendingRead = {
|
||||
status: 'pending',
|
||||
summary: 'Rendering is still in progress for "clip.mp4".',
|
||||
asset: frames.asset,
|
||||
details: [],
|
||||
images: [],
|
||||
taskId: 'task-1',
|
||||
retryAfterSeconds: 5,
|
||||
}
|
||||
|
||||
describe('shumai-cli', () => {
|
||||
const originalAllowDelete = process.env.SHUMAI_ALLOW_DELETE
|
||||
|
||||
@@ -251,6 +275,181 @@ describe('shumai-cli', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('read', () => {
|
||||
it('maps flattened reader flags onto the config objects and prints image links', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue(frames)
|
||||
|
||||
await cli.run(
|
||||
'read',
|
||||
'vid-1',
|
||||
'--video-start',
|
||||
'0',
|
||||
'--video-end',
|
||||
'4',
|
||||
'--video-count',
|
||||
'2',
|
||||
'--annotation-id',
|
||||
'c-1',
|
||||
)
|
||||
|
||||
expect(cli.callOperation).toHaveBeenCalledWith(readAsset, {
|
||||
assetId: 'vid-1',
|
||||
annotationId: 'c-1',
|
||||
videoConfig: { start: 0, end: 4, count: 2 },
|
||||
})
|
||||
expect(cli.stdout()).toBe(
|
||||
[
|
||||
'Extracted 2 frame(s) from "clip.mp4" (0s - 4s):',
|
||||
'- Frame at 0s: https://files.example/0.webp',
|
||||
'- Frame at 4s: https://files.example/4.webp',
|
||||
'',
|
||||
].join('\n'),
|
||||
)
|
||||
})
|
||||
|
||||
it.each([
|
||||
[
|
||||
['--doc-mode', 'pages', '--doc-start-page', '1', '--doc-end-page', '3'],
|
||||
{ docConfig: { mode: 'pages', startPage: 1, endPage: 3 } },
|
||||
],
|
||||
[['--doc-mode', 'text'], { docConfig: { mode: 'text' } }],
|
||||
[['--degree', '90'], { config3d: { degree: 90 } }],
|
||||
[[], {}],
|
||||
])('reads with %j', async (flags, config) => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue({ ...frames, images: [] })
|
||||
|
||||
await cli.run('read', 'a-1', ...flags)
|
||||
|
||||
expect(cli.callOperation).toHaveBeenCalledWith(readAsset, { assetId: 'a-1', ...config })
|
||||
})
|
||||
|
||||
it('names the flag that is missing from a reader config', async () => {
|
||||
const cli = setup()
|
||||
|
||||
const error = await cli
|
||||
.run('read', 'vid-1', '--video-start', '0', '--video-count', '2')
|
||||
.catch((err: unknown) => err)
|
||||
|
||||
expect(formatCliError(error)).toContain('--video-end')
|
||||
expect(cli.callOperation).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('prints text content after the summary', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue({
|
||||
...frames,
|
||||
summary: 'Document "notes.md" (ID: d-1):',
|
||||
images: [],
|
||||
text: {
|
||||
content: '# Notes\nhello',
|
||||
truncated: true,
|
||||
size: 90000,
|
||||
mimeType: 'text/markdown',
|
||||
},
|
||||
})
|
||||
|
||||
await cli.run('read', 'd-1', '--doc-mode', 'text')
|
||||
|
||||
expect(cli.stdout()).toBe(
|
||||
'Document "notes.md" (ID: d-1):\n\n# Notes\nhello\n\n[Content truncated: showing the first part of a 90000-byte file. Run `shumai-cli download vid-1` to get the whole file.]\n',
|
||||
)
|
||||
})
|
||||
|
||||
it('returns right away with --no-wait and prints the render command', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue(pendingRead)
|
||||
|
||||
await cli.run(
|
||||
'read',
|
||||
'vid-1',
|
||||
'--video-start',
|
||||
'0',
|
||||
'--video-end',
|
||||
'4',
|
||||
'--video-count',
|
||||
'2',
|
||||
'--no-wait',
|
||||
'--out',
|
||||
'frames',
|
||||
)
|
||||
|
||||
expect(cli.callOperation).toHaveBeenCalledWith(readAsset, {
|
||||
assetId: 'vid-1',
|
||||
videoConfig: { start: 0, end: 4, count: 2 },
|
||||
maxWaitSeconds: 0,
|
||||
})
|
||||
expect(cli.stdout()).toBe(
|
||||
'Rendering is still in progress for "clip.mp4".\nRun this to get the result: shumai-cli render task-1 --out "frames"\n',
|
||||
)
|
||||
})
|
||||
|
||||
it('prints the raw output with --json', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue(frames)
|
||||
|
||||
await cli.run('--json', 'read', 'vid-1')
|
||||
|
||||
expect(JSON.parse(cli.stdout())).toEqual(frames)
|
||||
})
|
||||
})
|
||||
|
||||
describe('render', () => {
|
||||
it('fetches a pending render by task ID', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue(frames)
|
||||
|
||||
await cli.run('render', 'task-1')
|
||||
|
||||
expect(cli.callOperation).toHaveBeenCalledWith(getAssetRender, { taskId: 'task-1' })
|
||||
expect(cli.stdout()).toContain('- Frame at 4s: https://files.example/4.webp')
|
||||
})
|
||||
|
||||
it('checks once with --no-wait', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue(pendingRead)
|
||||
|
||||
await cli.run('render', 'task-1', '--no-wait')
|
||||
|
||||
expect(cli.callOperation).toHaveBeenCalledWith(getAssetRender, {
|
||||
taskId: 'task-1',
|
||||
maxWaitSeconds: 0,
|
||||
})
|
||||
expect(cli.stdout()).toContain('Run this to get the result: shumai-cli render task-1\n')
|
||||
})
|
||||
})
|
||||
|
||||
describe('download', () => {
|
||||
it('downloads the web previews by default, or the originals with --original', async () => {
|
||||
const cli = setup()
|
||||
cli.callOperation.mockResolvedValue({ files: [] })
|
||||
|
||||
await cli.run('download', 'a-1', 'a-2')
|
||||
expect(cli.callOperation).toHaveBeenLastCalledWith(downloadAsset, {
|
||||
assetIds: ['a-1', 'a-2'],
|
||||
})
|
||||
|
||||
await cli.run('download', 'a-1', '--original')
|
||||
expect(cli.callOperation).toHaveBeenLastCalledWith(downloadAsset, {
|
||||
assetIds: ['a-1'],
|
||||
proxy: false,
|
||||
})
|
||||
})
|
||||
|
||||
it('requires at least one asset ID and documents its options', async () => {
|
||||
const cli = setup()
|
||||
|
||||
await expect(cli.run('download')).rejects.toThrow()
|
||||
expect(cli.stderr()).toContain("missing required argument 'assetIds'")
|
||||
|
||||
await expect(cli.run('download', '--help')).rejects.toThrow()
|
||||
expect(cli.stdout()).toContain('-o, --out <dir>')
|
||||
expect(cli.stdout()).toContain('--original')
|
||||
expect(cli.stdout()).not.toContain('--proxy')
|
||||
})
|
||||
})
|
||||
|
||||
describe('--json', () => {
|
||||
it('prints the raw Operation output', async () => {
|
||||
const cli = setup()
|
||||
@@ -312,6 +511,9 @@ describe('shumai-cli', () => {
|
||||
'rename',
|
||||
'move|mv',
|
||||
'delete|rm',
|
||||
'read',
|
||||
'render',
|
||||
'download',
|
||||
'upload',
|
||||
'create-version',
|
||||
]) {
|
||||
@@ -333,6 +535,30 @@ describe('shumai-cli', () => {
|
||||
expect(help).toContain('(default: 200)')
|
||||
expect(help).toContain('List the files and folders inside a parent folder.')
|
||||
})
|
||||
|
||||
it('documents flattened reader options for read', async () => {
|
||||
const cli = setup()
|
||||
|
||||
await expect(cli.run('read', '--help')).rejects.toThrow()
|
||||
|
||||
const help = cli.stdout()
|
||||
for (const flag of [
|
||||
'--video-start <start>',
|
||||
'--video-end <end>',
|
||||
'--video-count <count>',
|
||||
'--doc-mode <mode>',
|
||||
'--doc-start-page <startPage>',
|
||||
'--doc-end-page <endPage>',
|
||||
'--degree <degree>',
|
||||
'--annotation-id <annotationId>',
|
||||
'-o, --out <dir>',
|
||||
'--no-wait',
|
||||
]) {
|
||||
expect(help).toContain(flag)
|
||||
}
|
||||
expect(help).not.toContain('--include-content')
|
||||
expect(help).not.toContain('--image')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user