mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 08:04:37 +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.
125 lines
5.3 KiB
TypeScript
125 lines
5.3 KiB
TypeScript
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
import { z } from 'zod'
|
|
import type { AgentToolResult } from '@earendil-works/pi-agent-core'
|
|
import { downloadAsset, MAX_DOWNLOAD_ASSETS, type DownloadAssetOutput } from '@shumai/operations'
|
|
import type { User } from '@shumai/db'
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
import { storageBucket } from '@shumai/core/src/asset-read'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
|
import { readFileMimeType } from '@shumai/core/src/utils/file-mime'
|
|
import { resolveAssetIdFromKey } from '../tools/download-asset'
|
|
import { registerAgentToolExtension, type AgentToolExtension } from '../operation-tools'
|
|
|
|
const CLEANUP_REMINDER = 'Remember to delete these temporary files when finished.'
|
|
|
|
/** The agent's sandbox, where downloaded files are saved for local scripts and bash. */
|
|
function sandboxDir(): string {
|
|
const dir = path.join(process.cwd(), '.pi')
|
|
fs.mkdirSync(dir, { recursive: true })
|
|
return dir
|
|
}
|
|
|
|
function formatSize(bytes: number): string {
|
|
if (bytes < 1024) return `${bytes} B`
|
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
|
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
|
|
}
|
|
|
|
/** Saves a storage object into the sandbox; returns the paths and detected content type. */
|
|
async function saveToSandbox(key: string, filename: string) {
|
|
const dir = sandboxDir()
|
|
const absolutePath = path.join(dir, filename)
|
|
const bucket = storageBucket()
|
|
const info = await s3Service.headObject(bucket, key).catch(() => null)
|
|
await s3Service.downloadToFile(bucket, key, absolutePath)
|
|
const size = await fs.promises
|
|
.stat(absolutePath)
|
|
.then((s) => s.size)
|
|
.catch(() => info?.size ?? 0)
|
|
const contentType =
|
|
info?.contentType && info.contentType !== 'application/octet-stream'
|
|
? info.contentType
|
|
: readFileMimeType(absolutePath)
|
|
return { absolutePath, filePath: path.join('.pi', filename), size, contentType }
|
|
}
|
|
|
|
/** Downloads one storage key (e.g. a frame from read_asset) after checking Read on its asset. */
|
|
async function downloadKey(key: string, userId: string): Promise<AgentToolResult<unknown>> {
|
|
const owningAssetId = await resolveAssetIdFromKey(key)
|
|
await authzService.hasPermission({
|
|
user: { id: userId } as User,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Asset,
|
|
id: owningAssetId,
|
|
})
|
|
const filename = sanitizeFilename(path.basename(key)) || 'downloaded_file'
|
|
const saved = await saveToSandbox(key, filename)
|
|
return {
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
text: `Downloaded storage key "${key}" to "${saved.filePath}". Remember to delete this temporary file when finished.`,
|
|
},
|
|
],
|
|
details: { key, assetId: owningAssetId, ...saved },
|
|
}
|
|
}
|
|
|
|
/** Saves each file of a download_asset result into the sandbox. */
|
|
export async function presentDownloadOutput(
|
|
output: DownloadAssetOutput,
|
|
): Promise<AgentToolResult<unknown>> {
|
|
const lines: string[] = []
|
|
const files = []
|
|
for (const file of output.files) {
|
|
if (!file.key) throw new Error(`download_asset returned no storage key for ${file.assetId}`)
|
|
const saved = await saveToSandbox(
|
|
file.key,
|
|
`${file.assetId}_${sanitizeFilename(file.name) || 'file'}`,
|
|
)
|
|
lines.push(
|
|
`Downloaded "${file.name}" (ID: ${file.assetId}, ${file.kind}, ${saved.contentType}, ${formatSize(saved.size)}) to "${saved.filePath}".`,
|
|
)
|
|
files.push({ assetId: file.assetId, name: file.name, kind: file.kind, ...saved })
|
|
}
|
|
lines.push(CLEANUP_REMINDER)
|
|
return { content: [{ type: 'text', text: lines.join('\n') }], details: { files } }
|
|
}
|
|
|
|
const downloadAssetExtension: AgentToolExtension = {
|
|
description:
|
|
`Downloads up to ${MAX_DOWNLOAD_ASSETS} workspace files into the local .pi directory, for processing with local scripts or bash commands. ` +
|
|
'By default it saves the web preview version (proxy) when there is one, e.g. the transcoded video or the web image; set proxy to false for the original uploaded file. ' +
|
|
'A version stack downloads its latest version; folders are not supported. ' +
|
|
'To download a specific artifact from read_asset (e.g. a frame or page), pass its storage key as "key" instead of assetIds. ' +
|
|
'Remember to delete the downloaded files when finished.',
|
|
parameterOverrides: z.object({
|
|
assetIds: downloadAsset.input.shape.assetIds
|
|
.optional()
|
|
.describe(
|
|
`IDs of the files or version stacks to download (1 to ${MAX_DOWNLOAD_ASSETS}). Omit when passing "key".`,
|
|
),
|
|
}),
|
|
extraParameters: z.object({
|
|
key: z
|
|
.string()
|
|
.optional()
|
|
.describe(
|
|
'Storage S3 key of a specific media artifact to download instead of assetIds, e.g. a screenshot or page key from read_asset.',
|
|
),
|
|
}),
|
|
async intercept(input, extra, ctx) {
|
|
if (typeof extra.key !== 'string') return undefined
|
|
if (Array.isArray(input.assetIds) && input.assetIds.length > 0) {
|
|
throw new Error('Provide either "assetIds" or "key", not both.')
|
|
}
|
|
if (!ctx.userId) throw new Error('User ID is required for authorization.')
|
|
return downloadKey(extra.key, ctx.userId)
|
|
},
|
|
present: (output) => presentDownloadOutput(output as DownloadAssetOutput),
|
|
}
|
|
|
|
registerAgentToolExtension('download_asset', downloadAssetExtension)
|