mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 04:44:28 +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:
@@ -189,6 +189,61 @@ describe('WorkflowService', () => {
|
||||
submitSpy.mockRestore()
|
||||
})
|
||||
|
||||
describe('waitFor', () => {
|
||||
async function createTask(status: WorkflowTaskStatus, output?: Record<string, unknown>) {
|
||||
const submitSpy = vi.spyOn(workflowService, 'submit').mockImplementation(async () => 'id')
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: { assetId: 'wait-asset', type: WorkflowTaskType.transcode_screenshot, status },
|
||||
})
|
||||
if (output) {
|
||||
await prisma.workflowTask.update({ where: { id: task.id }, data: { output } })
|
||||
}
|
||||
submitSpy.mockRestore()
|
||||
return task
|
||||
}
|
||||
|
||||
it('returns done: true with the completed task', async () => {
|
||||
const task = await createTask(WorkflowTaskStatus.completed, { screenshots: [] })
|
||||
|
||||
const result = await workflowService.waitFor(task, 1000)
|
||||
|
||||
expect(result.done).toBe(true)
|
||||
expect(result.task.output).toEqual({ screenshots: [] })
|
||||
})
|
||||
|
||||
it('returns done: false instead of throwing when the budget runs out', async () => {
|
||||
const task = await createTask(WorkflowTaskStatus.processing)
|
||||
|
||||
const start = Date.now()
|
||||
const result = await workflowService.waitFor(task, 300)
|
||||
|
||||
expect(result.done).toBe(false)
|
||||
expect(result.task.status).toBe(WorkflowTaskStatus.processing)
|
||||
expect(Date.now() - start).toBeLessThan(2000)
|
||||
})
|
||||
|
||||
it('checks once and returns immediately with a zero budget', async () => {
|
||||
const task = await createTask(WorkflowTaskStatus.pending)
|
||||
|
||||
const result = await workflowService.waitFor(task, 0)
|
||||
|
||||
expect(result.done).toBe(false)
|
||||
})
|
||||
|
||||
it('throws the task error when the task failed', async () => {
|
||||
const task = await createTask(WorkflowTaskStatus.failed, { error: 'ffmpeg exploded' })
|
||||
|
||||
await expect(workflowService.waitFor(task, 1000)).rejects.toThrow('ffmpeg exploded')
|
||||
})
|
||||
|
||||
it('keeps executeWait throwing on timeout', async () => {
|
||||
const task = await createTask(WorkflowTaskStatus.processing)
|
||||
|
||||
await expect(workflowService.executeWait(task, 200)).rejects.toThrow(
|
||||
'Workflow task timed out',
|
||||
)
|
||||
})
|
||||
})
|
||||
describe('Stale Task Recovery', () => {
|
||||
it('should resume stale pending workflow task in DBOS and increment recoveryAttempts', async () => {
|
||||
const task = await prisma.workflowTask.create({
|
||||
|
||||
@@ -30,8 +30,24 @@ export class WorkflowService {
|
||||
}
|
||||
|
||||
async executeWait(task: WorkflowTask, timeoutMs: number = 30000): Promise<WorkflowTask> {
|
||||
const { task: finished, done } = await this.waitFor(task, timeoutMs)
|
||||
if (!done) {
|
||||
throw new Error('Workflow task timed out')
|
||||
}
|
||||
return finished
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait up to `timeoutMs` for a task to finish. Unlike `executeWait`, running out of time is
|
||||
* not an error: it resolves with the latest task state and `done: false`, so callers can hand
|
||||
* the task ID back and complete the work later. Throws if the task failed or disappeared.
|
||||
*/
|
||||
async waitFor(
|
||||
task: WorkflowTask,
|
||||
timeoutMs: number,
|
||||
): Promise<{ task: WorkflowTask; done: boolean }> {
|
||||
const start = Date.now()
|
||||
while (Date.now() - start < timeoutMs) {
|
||||
for (;;) {
|
||||
const updatedTask = await prisma.workflowTask.findUnique({
|
||||
where: { id: task.id },
|
||||
})
|
||||
@@ -41,7 +57,7 @@ export class WorkflowService {
|
||||
}
|
||||
|
||||
if (updatedTask.status === 'completed') {
|
||||
return updatedTask
|
||||
return { task: updatedTask, done: true }
|
||||
}
|
||||
|
||||
if (updatedTask.status === 'failed') {
|
||||
@@ -50,10 +66,12 @@ export class WorkflowService {
|
||||
throw new Error(error)
|
||||
}
|
||||
|
||||
if (Date.now() - start >= timeoutMs) {
|
||||
return { task: updatedTask, done: false }
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 200))
|
||||
}
|
||||
|
||||
throw new Error('Workflow task timed out')
|
||||
}
|
||||
|
||||
start(listenQueues?: string[]): void {
|
||||
|
||||
Reference in New Issue
Block a user