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.
This commit is contained in:
yiling
2026-10-26 15:33:37 +08:00
parent 4581058e0d
commit 4fc8476b0f
26 changed files with 1158 additions and 46 deletions
@@ -547,7 +547,8 @@ describe('read_asset and get_asset_render Operations', () => {
it.each([
['agent', true],
['mcp', false],
// The MCP presenter attaches image bytes, then strips the keys (packages/api)
['mcp', true],
['cli', false],
['api', false],
] as const)('via %s includes storage keys: %s', async (via, includesKey) => {
+2 -2
View File
@@ -265,8 +265,8 @@ export class AssetReadService {
export const assetReadService = new AssetReadService()
/**
* Converts a read outcome into the Operation output. Storage keys are included only for
* Shumai's built-in agent; every image gets a short-lived link.
* Converts a read outcome into the Operation output. Every image gets a short-lived link;
* storage keys (and the source file link) only when the result is presented server-side.
*/
export async function toAssetReadOutput(
outcome: ReadOutcome,
+1
View File
@@ -10,4 +10,5 @@ export {
export { resolveAnnotationsById } from './annotations'
export { findReader, getReader, readers } from './registry'
export { isPlainTextAsset, MAX_TEXT_BYTES, MAX_TEXT_LINES } from './readers/document'
export { storageBucket } from './types'
export type { AssetReader, ReadableAsset, ReadImage, ReadPlan, ReadResult } from './types'
@@ -122,6 +122,16 @@ describe('videoReader', () => {
})
})
it('rounds frame timestamps in labels', () => {
const result = videoReader.collect!(
video,
{ start: 0, end: 8, count: 3 },
{ screenshots: [{ key: 's/1.webp', timestamp: 2.6666666666666665 }] },
{},
)
expect(result.images[0].label).toBe('Frame at 2.667s')
})
it('fails when the render produced no frames', () => {
expect(() =>
videoReader.collect!(video, { start: 0, end: 1, count: 1 }, { screenshots: [] }, {}),
@@ -67,7 +67,8 @@ export const videoReader: AssetReader<'video'> = {
details: [],
images: screenshots.map((shot) => ({
key: shot.key,
label: `Frame at ${shot.timestamp}s`,
// Evenly spaced timestamps can be long fractions (2.6666666666666665)
label: `Frame at ${Number(shot.timestamp.toFixed(3))}s`,
mimeType: getFileMimeType(null, shot.key, 'image/webp'),
})),
}
@@ -13,6 +13,13 @@ export const READ_WAIT_BUDGET_MS: Record<Via, number> = {
api: 20_000,
}
/**
* Surfaces whose results are presented on the server (agent tools, MCP tool results). They get
* storage keys so the presenter can attach image bytes; the MCP presenter strips the keys
* before anything leaves the server. CLI and REST callers only get links.
*/
const SERVER_PRESENTED: ReadonlySet<Via> = new Set(['agent', 'mcp'])
/** `maxWaitSeconds` can only shorten the surface budget. */
export function waitBudgetMs(via: Via, maxWaitSeconds: number | undefined): number {
const budget = READ_WAIT_BUDGET_MS[via]
@@ -27,7 +34,7 @@ export const readAssetOp = implementOperation(readAsset, {
const outcome = await assetReadService.read({ kind: 'user', userId: ctx.userId }, request, {
waitBudgetMs: waitBudgetMs(ctx.via, maxWaitSeconds),
})
return toAssetReadOutput(outcome, { includeKeys: ctx.via === 'agent' })
return toAssetReadOutput(outcome, { includeKeys: SERVER_PRESENTED.has(ctx.via) })
},
})
@@ -38,6 +45,6 @@ export const getAssetRenderOp = implementOperation(getAssetRender, {
const outcome = await assetReadService.getRender(ctx.userId, input.taskId, {
waitBudgetMs: waitBudgetMs(ctx.via, input.maxWaitSeconds),
})
return toAssetReadOutput(outcome, { includeKeys: ctx.via === 'agent' })
return toAssetReadOutput(outcome, { includeKeys: SERVER_PRESENTED.has(ctx.via) })
},
})