feat(asset-read): rewrite read_asset as a reader registry and Operations (#498)

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:
Yiling-J
2026-10-27 08:09:10 +08:00
committed by GitHub
parent e44938cb8f
commit 155fb95a0e
40 changed files with 3787 additions and 2229 deletions
+37
View File
@@ -94,6 +94,43 @@ describe('S3Service implementations', () => {
expect(getSignedUrlSpy).toHaveBeenCalledTimes(1)
})
it('signs with an explicit lifetime and caches it separately from the default', async () => {
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
getSignedUrlSpy.mockResolvedValueOnce('http://default-url')
getSignedUrlSpy.mockResolvedValueOnce('http://short-url')
const defaultUrl = await s3.presign('bucket', 'expiry-key', 'GET')
const shortUrl = await s3.presign(
'bucket',
'expiry-key',
'GET',
false,
undefined,
false,
3600,
)
const shortAgain = await s3.presign(
'bucket',
'expiry-key',
'GET',
false,
undefined,
false,
3600,
)
expect(defaultUrl).toBe('http://default-url')
expect(shortUrl).toBe('http://short-url')
expect(shortAgain).toBe('http://short-url')
expect(getSignedUrlSpy).toHaveBeenCalledTimes(2)
expect(getSignedUrlSpy).toHaveBeenLastCalledWith(
expect.anything(),
expect.any(GetObjectCommand),
expect.objectContaining({ expiresIn: 3600 }),
)
})
it('should NOT cache PUT presign URLs', async () => {
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
+11 -6
View File
@@ -112,6 +112,8 @@ export interface S3Service {
download?: boolean,
filename?: string,
internal?: boolean,
/** URL lifetime; defaults to PRESIGNED_URL_EXPIRES_IN hours. Ignored by local storage. */
expiresInSeconds?: number,
) => Promise<string>
presignMultipart: (
bucket: string,
@@ -360,12 +362,15 @@ export class S3StorageService implements S3Service {
download?: boolean,
filename?: string,
internal?: boolean,
expiresInSeconds?: number,
): Promise<string> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const expiresInSeconds = expireHours * 3600
const cacheTtlMs = expiresInSeconds * 1000
const lifetimeSeconds = expiresInSeconds ?? expireHours * 3600
// With an explicit (usually short) lifetime, only reuse a cached URL while it has at
// least half of its lifetime left, so callers never receive a nearly expired link.
const cacheTtlMs = expiresInSeconds ? (lifetimeSeconds * 1000) / 2 : lifetimeSeconds * 1000
const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&internal=${internal || false}`
const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&internal=${internal || false}&expires=${expiresInSeconds ?? 'default'}`
if (method === 'GET' && !download) {
const cached = this.presignCache.get(cacheKey)
if (cached) {
@@ -382,19 +387,19 @@ export class S3StorageService implements S3Service {
Key: key,
ResponseContentDisposition: download ? buildContentDisposition(filename) : undefined,
})
url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds })
url = await getSignedUrl(signingClient, command, { expiresIn: lifetimeSeconds })
} else if (method === 'PUT') {
const command = new PutObjectCommand({
Bucket: bucket,
Key: key,
})
url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds })
url = await getSignedUrl(signingClient, command, { expiresIn: lifetimeSeconds })
} else if (method === 'DELETE') {
const command = new DeleteObjectCommand({
Bucket: bucket,
Key: key,
})
url = await getSignedUrl(signingClient, command, { expiresIn: expiresInSeconds })
url = await getSignedUrl(signingClient, command, { expiresIn: lifetimeSeconds })
} else {
throw new Error(`Unsupported method for presign: ${method}`)
}