mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
feat(asset-read): rewrite read_asset as a reader registry and Operations
Replace the monolithic agent read_asset tool with a reader registry in @shumai/core/src/asset-read (model3d, image, video, document) behind a shared pipeline that handles access checks, version stacks, annotations, render tasks, and waiting. - Add read_asset and get_asset_render Operations (agent surface only). Renders that outlast the caller's wait budget return "pending" with the workflow task ID; the read request is stored on the task payload so get_asset_render works across restarts and instances (requester only). - Add workflowService.waitFor and presign expiry support. - Keep agent output compatible via an agent tool extension (s3KeyOnly, image blocks, session image details); autofill keeps its team-scoped tool. - Cap video frames at 20 per call; add the get_asset_render agent toggle.
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
import { z } from 'zod'
|
||||
import { defineOperation } from '../define'
|
||||
|
||||
/**
|
||||
* Asset readers, in matching order. Each reader handles one family of assets and contributes
|
||||
* one optional config field to `read_asset`. Implementations live in
|
||||
* `@shumai/core/src/asset-read/readers`.
|
||||
*/
|
||||
export const READER_KINDS = ['model3d', 'image', 'video', 'document'] as const
|
||||
export type ReaderKind = (typeof READER_KINDS)[number]
|
||||
|
||||
/** Most frames or pages a single read may render. */
|
||||
export const MAX_RENDER_ITEMS = 20
|
||||
|
||||
export const readerOptionSchemas = {
|
||||
model3d: {
|
||||
key: 'config3d',
|
||||
schema: z
|
||||
.object({
|
||||
degree: z
|
||||
.number()
|
||||
.int()
|
||||
.nullable()
|
||||
.optional()
|
||||
.describe(
|
||||
'Turntable rotation degree (integer: 0, 15, 30, ..., 345 in 15° increments) to inspect a specific angle of a 3D model. Leave null/omitted to view the 4-view composite overview.',
|
||||
),
|
||||
})
|
||||
.describe(
|
||||
'Configuration for 3D model assets. Optionally pass degree to inspect a specific turntable angle, or leave it out to view the 4-view composite overview.',
|
||||
),
|
||||
},
|
||||
image: {
|
||||
key: 'imageConfig',
|
||||
schema: z.object({}).describe('Optional configuration for image assets.'),
|
||||
},
|
||||
video: {
|
||||
key: 'videoConfig',
|
||||
schema: z
|
||||
.object({
|
||||
start: z.number().describe('Start time in seconds.'),
|
||||
end: z.number().describe('End time in seconds.'),
|
||||
count: z
|
||||
.number()
|
||||
.describe(`Number of frames to extract, evenly spaced (1 to ${MAX_RENDER_ITEMS}).`),
|
||||
})
|
||||
.describe('Configuration for video assets. Required when inspecting videos.'),
|
||||
},
|
||||
document: {
|
||||
key: 'docConfig',
|
||||
schema: z
|
||||
.object({
|
||||
mode: z
|
||||
.enum(['pages', 'text'])
|
||||
.describe(
|
||||
'"pages" renders a page range as images (visual layout, charts, markups; required for binary PDFs). "text" reads raw text (ideal for .md, .txt, .csv, code files).',
|
||||
),
|
||||
startPage: z
|
||||
.number()
|
||||
.nullable()
|
||||
.optional()
|
||||
.describe('Start page number (1-based, required when mode="pages"; omit for "text").'),
|
||||
endPage: z
|
||||
.number()
|
||||
.nullable()
|
||||
.optional()
|
||||
.describe(
|
||||
`End page number (1-based, max ${MAX_RENDER_ITEMS} pages per call, required when mode="pages"; omit for "text").`,
|
||||
),
|
||||
})
|
||||
.describe('Configuration for document, PDF, and text assets.'),
|
||||
},
|
||||
} as const satisfies Record<ReaderKind, { key: string; schema: z.ZodObject }>
|
||||
|
||||
export type ReaderOptions<TKind extends ReaderKind> = z.output<
|
||||
(typeof readerOptionSchemas)[TKind]['schema']
|
||||
>
|
||||
|
||||
/** The config field name each reader contributes, e.g. `videoConfig`. */
|
||||
export type ReaderConfigKey = (typeof readerOptionSchemas)[ReaderKind]['key']
|
||||
|
||||
const readImageSchema = z.object({
|
||||
label: z.string().describe('What the image shows, e.g. "Frame at 30s" or "Page 3".'),
|
||||
mimeType: z.string(),
|
||||
url: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('Short-lived link to the image (about 1 hour). Download it to look at the image.'),
|
||||
/** Storage key; only returned to Shumai's built-in agent. */
|
||||
key: z.string().optional(),
|
||||
})
|
||||
|
||||
/** Shared by `read_asset` and `get_asset_render`. */
|
||||
export const assetReadOutputSchema = z.object({
|
||||
status: z
|
||||
.enum(['ready', 'pending'])
|
||||
.describe('"pending" means a render is still running; call get_asset_render with taskId.'),
|
||||
summary: z.string(),
|
||||
asset: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
kind: z.enum(READER_KINDS),
|
||||
mediaType: z.string().nullable(),
|
||||
}),
|
||||
details: z.array(z.string()),
|
||||
images: z.array(readImageSchema),
|
||||
text: z
|
||||
.object({
|
||||
content: z.string(),
|
||||
truncated: z.boolean(),
|
||||
size: z.number(),
|
||||
mimeType: z.string(),
|
||||
url: z.string().optional(),
|
||||
key: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
taskId: z.string().optional(),
|
||||
retryAfterSeconds: z.number().optional(),
|
||||
})
|
||||
export type AssetReadOutput = z.infer<typeof assetReadOutputSchema>
|
||||
|
||||
const maxWaitSeconds = z
|
||||
.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.optional()
|
||||
.describe(
|
||||
'Longest time to wait for rendering, in seconds. If rendering takes longer, the result is "pending" with a taskId to pass to get_asset_render. 0 returns immediately.',
|
||||
)
|
||||
|
||||
export const readAsset = defineOperation({
|
||||
name: 'read_asset',
|
||||
title: 'Read Asset',
|
||||
description:
|
||||
'Inspects and reads the content of an asset without downloading it. ' +
|
||||
'Supports images, 3D models (4-view overview or a specific degree angle), video frame extraction, PDF page rendering, and direct text reading for text/markdown/code files. ' +
|
||||
'Pass assetId and the config matching the asset type (imageConfig, config3d, videoConfig, or docConfig); omit the others. ' +
|
||||
'Optionally pass annotationId to view visual markups drawn on the asset. ' +
|
||||
'Rendering frames or pages can take a while: if the result has status "pending", call get_asset_render with its taskId.',
|
||||
input: z.object({
|
||||
assetId: z.string().min(1).describe('The ID of the asset to inspect.'),
|
||||
annotationId: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
'The ID of a comment or message (from <annotation id="..." />) whose visual markup should be overlaid.',
|
||||
),
|
||||
config3d: readerOptionSchemas.model3d.schema.optional(),
|
||||
imageConfig: readerOptionSchemas.image.schema.optional(),
|
||||
videoConfig: readerOptionSchemas.video.schema.optional(),
|
||||
docConfig: readerOptionSchemas.document.schema.optional(),
|
||||
includeContent: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe('Set to false to skip reading text content and only describe the asset.'),
|
||||
maxWaitSeconds,
|
||||
}),
|
||||
output: assetReadOutputSchema,
|
||||
surfaces: ['agent'],
|
||||
annotations: { readOnly: true, destructive: false },
|
||||
})
|
||||
|
||||
export const getAssetRender = defineOperation({
|
||||
name: 'get_asset_render',
|
||||
title: 'Get Asset Render',
|
||||
description:
|
||||
'Gets the result of a read_asset call that returned status "pending". ' +
|
||||
'Returns "pending" again while rendering is still running; wait retryAfterSeconds and try again.',
|
||||
input: z.object({
|
||||
taskId: z.string().min(1).describe('The taskId returned by read_asset.'),
|
||||
maxWaitSeconds,
|
||||
}),
|
||||
output: assetReadOutputSchema,
|
||||
surfaces: ['agent'],
|
||||
annotations: { readOnly: true, destructive: false, idempotent: true },
|
||||
})
|
||||
@@ -6,4 +6,5 @@ export { deleteAsset } from './asset/delete-asset'
|
||||
export { listAssets } from './asset/list-assets'
|
||||
export { moveAssets } from './asset/move-assets'
|
||||
export { renameAsset } from './asset/rename-asset'
|
||||
export * from './asset/read-asset'
|
||||
export { listProjects } from './project/list-projects'
|
||||
|
||||
@@ -15,13 +15,15 @@ describe('operation registry', () => {
|
||||
expect(new Set(names).size).toBe(names.length)
|
||||
})
|
||||
|
||||
it('contains the initial asset and project operations', () => {
|
||||
it('contains the asset and project operations', () => {
|
||||
expect(operationDefinitions.map((d) => d.name).sort()).toEqual([
|
||||
'create_folder',
|
||||
'delete_asset',
|
||||
'get_asset_render',
|
||||
'list_assets',
|
||||
'list_projects',
|
||||
'move_assets',
|
||||
'read_asset',
|
||||
'rename_asset',
|
||||
])
|
||||
})
|
||||
@@ -40,7 +42,12 @@ describe('operation registry', () => {
|
||||
const destructive = operationDefinitions.filter((d) => d.annotations.destructive)
|
||||
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(['list_assets', 'list_projects'])
|
||||
expect(readOnly.map((d) => d.name).sort()).toEqual([
|
||||
'get_asset_render',
|
||||
'list_assets',
|
||||
'list_projects',
|
||||
'read_asset',
|
||||
])
|
||||
})
|
||||
|
||||
it.each(operationDefinitions.map((d) => [d.name, d] as const))(
|
||||
@@ -71,6 +78,9 @@ describe('operation registry', () => {
|
||||
['rename_asset', ['rename']],
|
||||
['move_assets', ['move', 'mv']],
|
||||
['delete_asset', ['delete', 'rm']],
|
||||
// CLI commands for reading assets arrive with the CLI surface
|
||||
['read_asset', []],
|
||||
['get_asset_render', []],
|
||||
])
|
||||
})
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createFolder } from './asset/create-folder'
|
||||
import { deleteAsset } from './asset/delete-asset'
|
||||
import { listAssets } from './asset/list-assets'
|
||||
import { moveAssets } from './asset/move-assets'
|
||||
import { getAssetRender, readAsset } from './asset/read-asset'
|
||||
import { renameAsset } from './asset/rename-asset'
|
||||
import type { AnyOperationDefinition, Surface } from './define'
|
||||
import { listProjects } from './project/list-projects'
|
||||
@@ -17,6 +18,8 @@ export const operationDefinitions = [
|
||||
renameAsset,
|
||||
moveAssets,
|
||||
deleteAsset,
|
||||
readAsset,
|
||||
getAssetRender,
|
||||
] as const
|
||||
|
||||
export type RegisteredOperationDefinition = (typeof operationDefinitions)[number]
|
||||
|
||||
Reference in New Issue
Block a user