mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 10:26:52 +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.
117 lines
4.0 KiB
TypeScript
117 lines
4.0 KiB
TypeScript
import { mkdir } from 'node:fs/promises'
|
|
import * as path from 'node:path'
|
|
import { Option } from 'commander'
|
|
import type { AssetReadOutput } from '@shumai/operations'
|
|
import { CliError } from '../errors'
|
|
import { saveUrlToFile } from './save-url'
|
|
import type { CliCommandExtension } from '../generate'
|
|
import type { Output } from '../render'
|
|
|
|
const EXTENSIONS: Record<string, string> = {
|
|
'image/webp': 'webp',
|
|
'image/png': 'png',
|
|
'image/jpeg': 'jpg',
|
|
'image/gif': 'gif',
|
|
'image/avif': 'avif',
|
|
}
|
|
|
|
function slug(value: string): string {
|
|
return (
|
|
value
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9.]+/g, '-')
|
|
.replace(/^-+|-+$/g, '') || 'image'
|
|
)
|
|
}
|
|
|
|
/** File name for the n-th image of a read, e.g. `01ABC-1-frame-at-2s.webp`. */
|
|
export function imageFileName(
|
|
assetId: string,
|
|
index: number,
|
|
image: AssetReadOutput['images'][number],
|
|
): string {
|
|
const ext = EXTENSIONS[image.mimeType] ?? image.mimeType.split('/')[1] ?? 'bin'
|
|
return `${assetId}-${index + 1}-${slug(image.label).replace(/\.+$/, '')}.${ext}`
|
|
}
|
|
|
|
/** Downloads every image of a ready read into `dir`; returns the saved paths in order. */
|
|
export async function downloadImages(output: AssetReadOutput, dir: string): Promise<string[]> {
|
|
await mkdir(dir, { recursive: true })
|
|
const saved: string[] = []
|
|
for (const [index, image] of output.images.entries()) {
|
|
if (!image.url) {
|
|
throw new CliError(`No download link was returned for "${image.label}".`)
|
|
}
|
|
const filePath = path.join(dir, imageFileName(output.asset.id, index, image))
|
|
await saveUrlToFile(image.url, filePath, image.label)
|
|
saved.push(filePath)
|
|
}
|
|
return saved
|
|
}
|
|
|
|
function renderCommand(taskId: string, out: unknown): string {
|
|
const outArg = typeof out === 'string' ? ` --out ${JSON.stringify(out)}` : ''
|
|
return `shumai-cli render ${taskId}${outArg}`
|
|
}
|
|
|
|
function writeReady(output: AssetReadOutput, paths: string[] | null, o: Output): void {
|
|
const lines = [output.summary, ...output.details]
|
|
output.images.forEach((image, i) => {
|
|
lines.push(`- ${image.label}: ${paths ? paths[i] : (image.url ?? '')}`)
|
|
})
|
|
o.out.write(`${lines.join('\n')}\n`)
|
|
if (output.text) {
|
|
o.out.write(`\n${output.text.content}`)
|
|
if (!output.text.content.endsWith('\n')) o.out.write('\n')
|
|
if (output.text.truncated) {
|
|
o.out.write(
|
|
`\n[Content truncated: showing the first part of a ${output.text.size}-byte file. Run \`shumai-cli download ${output.asset.id}\` to get the whole file.]\n`,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `read` and `render`: images are printed as links (valid for about an hour) or downloaded
|
|
* with `--out`; a render that takes longer than the wait prints the `render` command to run.
|
|
*/
|
|
function readExtension(noWaitHelp: string): CliCommandExtension {
|
|
return {
|
|
options: () => [
|
|
new Option('-o, --out <dir>', 'Download the images into this folder and print their paths.'),
|
|
new Option('--no-wait', noWaitHelp),
|
|
],
|
|
toInput: (input, opts) => (opts.wait === false ? { ...input, maxWaitSeconds: 0 } : input),
|
|
async present(raw, { opts, json, output: o }) {
|
|
const output = raw as AssetReadOutput
|
|
if (output.status === 'pending') {
|
|
if (json) {
|
|
o.out.write(`${JSON.stringify(output, null, 2)}\n`)
|
|
return
|
|
}
|
|
o.out.write(
|
|
`${output.summary}\nRun this to get the result: ${renderCommand(output.taskId ?? '', opts.out)}\n`,
|
|
)
|
|
return
|
|
}
|
|
|
|
const paths = typeof opts.out === 'string' ? await downloadImages(output, opts.out) : null
|
|
if (json) {
|
|
const images = output.images.map((image, i) =>
|
|
paths ? { ...image, path: paths[i] } : image,
|
|
)
|
|
o.out.write(`${JSON.stringify({ ...output, images }, null, 2)}\n`)
|
|
return
|
|
}
|
|
writeReady(output, paths, o)
|
|
},
|
|
}
|
|
}
|
|
|
|
export const readAssetExtension = readExtension(
|
|
'Return right away with a task ID if the asset needs rendering.',
|
|
)
|
|
export const getAssetRenderExtension = readExtension(
|
|
'Check once and return right away if the render is still running.',
|
|
)
|