mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 04:44:28 +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.
112 lines
4.0 KiB
TypeScript
112 lines
4.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { resolveAssetIdFromKey, resolveS3MediaBuffer } from './download-asset'
|
|
import { prisma, type Asset, type StorageKey } from '@shumai/db'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { authzService } from '@shumai/core/src/authz/authz'
|
|
|
|
vi.mock('@shumai/db', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@shumai/db')>()
|
|
return {
|
|
...actual,
|
|
prisma: {
|
|
asset: {
|
|
findUnique: vi.fn(),
|
|
findFirst: vi.fn(),
|
|
},
|
|
storageKey: {
|
|
findFirst: vi.fn(),
|
|
findUnique: vi.fn(),
|
|
},
|
|
},
|
|
}
|
|
})
|
|
|
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
|
s3Service: {
|
|
getObject: vi.fn(),
|
|
headObject: vi.fn(),
|
|
downloadToFile: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
vi.mock('@shumai/core/src/authz/authz', () => ({
|
|
authzService: {
|
|
hasPermission: vi.fn(),
|
|
},
|
|
Permission: { Read: 'Read' },
|
|
ResourceType: { Asset: 'asset' },
|
|
}))
|
|
|
|
describe('download-asset storage key helpers', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
describe('resolveAssetIdFromKey helper unit tests', () => {
|
|
it('resolves directly by asset ID', async () => {
|
|
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'ast-999' } as unknown as Asset)
|
|
const assetId = await resolveAssetIdFromKey('files/ast-999/pdf_pages/page_1.webp')
|
|
expect(assetId).toBe('ast-999')
|
|
})
|
|
|
|
it('resolves via StorageKey prefix', async () => {
|
|
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
|
|
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue({
|
|
assets: [{ id: 'ast-888' }],
|
|
} as unknown as StorageKey & { assets: Asset[] })
|
|
const assetId = await resolveAssetIdFromKey('files/ulid-777/document.pdf')
|
|
expect(assetId).toBe('ast-888')
|
|
})
|
|
|
|
it('resolves via exact StorageKey match', async () => {
|
|
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
|
|
vi.mocked(prisma.storageKey.findFirst).mockResolvedValue(null)
|
|
vi.mocked(prisma.storageKey.findUnique).mockResolvedValue({
|
|
assets: [{ id: 'ast-777' }],
|
|
} as unknown as StorageKey & { assets: Asset[] })
|
|
const assetId = await resolveAssetIdFromKey('files/ulid-special/document.pdf')
|
|
expect(assetId).toBe('ast-777')
|
|
})
|
|
})
|
|
|
|
describe('resolveS3MediaBuffer helper unit tests', () => {
|
|
it('throws authorization error when userId is empty', async () => {
|
|
await expect(resolveS3MediaBuffer('files/ast-1/img.png', '')).rejects.toThrow(
|
|
'User ID is required for authorization.',
|
|
)
|
|
})
|
|
|
|
it('verifies authz permission on owning asset and returns buffer with mimeType', async () => {
|
|
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'ast-1' } as unknown as Asset)
|
|
vi.mocked(authzService.hasPermission).mockResolvedValue()
|
|
vi.mocked(s3Service.getObject).mockResolvedValue({
|
|
buffer: Buffer.from('png-bytes'),
|
|
contentType: 'image/png',
|
|
} as unknown as { buffer: Buffer; contentType: string })
|
|
|
|
const result = await resolveS3MediaBuffer('files/ast-1/image.png', 'user-123')
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
user: { id: 'user-123' },
|
|
permission: 'Read',
|
|
type: 'asset',
|
|
id: 'ast-1',
|
|
})
|
|
expect(result.buffer.toString()).toBe('png-bytes')
|
|
expect(result.mimeType).toBe('image/png')
|
|
})
|
|
|
|
it('throws error when S3 file is not an image', async () => {
|
|
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'ast-1' } as unknown as Asset)
|
|
vi.mocked(authzService.hasPermission).mockResolvedValue()
|
|
vi.mocked(s3Service.getObject).mockResolvedValue({
|
|
buffer: Buffer.from('some text'),
|
|
contentType: 'text/plain',
|
|
} as unknown as { buffer: Buffer; contentType: string })
|
|
|
|
await expect(resolveS3MediaBuffer('files/ast-1/readme.txt', 'user-123')).rejects.toThrow(
|
|
'has MIME type "text/plain", but an image is required.',
|
|
)
|
|
})
|
|
})
|
|
})
|