Compare commits

...
9 Commits
Author SHA1 Message Date
yiling 49a2d7c380 Release v0.6.3 2026-11-01 19:52:23 +08:00
Yiling-J 6292f3c792 Revise changelog for upload and agent behavior changes
Updated changelog to reflect important behavior changes in file uploads and agent comments.
2026-11-01 17:19:08 +08:00
Yiling-J 20ad0abeba feat(upload): simplify upload file list display and add live speed & ETA (#513)
* feat(upload): simplify upload file list display and add live speed and ETA

* fix(upload): retain authoritative server search results and handle local failed upload state gracefully
2026-11-01 13:29:52 +08:00
Yiling-J fbff404b9b feat(file): optimize upload and transcoding status polling with batch api (#512)
* feat(file): optimize upload and transcoding status polling with batch api

* fix(webui): throttle search invalidation during upload and fix stuck 100% progress ring

* fix(webui): deduplicate upload search invalidation and stabilize batch polling
2026-10-31 20:11:31 +08:00
Yiling-J c2348b0e68 feat(upload): allow canceling in-progress upload tasks from sidebar (#511)
* feat(upload): allow canceling in-progress upload tasks from sidebar

* fix(upload): resolve concurrent confirmation race and store counter issues on cancellation

* fix(webui): immediately remove cancelled upload files from file list view
2026-10-31 16:39:54 +08:00
Yiling-J ff3d71ed3d feat(agent)!: suppress success comments for autofill and embedding agents (#510)
* feat(agent)!: suppress success comments for autofill and embedding agents

* fix(agent): ensure createCommentActivity runs on workflow cancellation
2026-10-31 12:02:30 +08:00
Yiling-J 77bd1422d8 feat(upload): state-based retry tracking and resilient gateway error handling (#509)
* feat(upload): state-based retry tracking and resilient gateway error handling

* fix(upload): check non-retryable statuses before offline state in isNetworkIssue
2026-10-31 10:37:35 +08:00
Yiling-J 9529c63c0a feat(upload): add 12-hour upload expiration and continuous backoff retry (#508)
* feat(upload): add 12-hour upload expiration and continuous backoff retry

* fix(upload): clamp jittered backoff delay to maxDelayMs and update presign JSDoc
2026-10-30 16:06:34 +08:00
Yiling-J 3b00a42a89 fix(webui): show clear toast when upload fails due to localhost s3 endpoint (#507)
* fix(webui): show clear toast when upload fails due to localhost s3 endpoint

* fix(webui): center empty state in file browser list area
2026-10-30 14:16:34 +08:00
66 changed files with 3431 additions and 544 deletions
+16
View File
@@ -12,6 +12,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Changed
## [0.6.3] - 2026-11-01
### Added
- **upload**: Allow users to cancel running upload tasks directly from the upload tasks sidebar with a confirmation dialog, preserving already completed files while discarding incomplete assets
- **upload**: Improve upload resilience during network drops, device sleep, and transient gateway errors by adding state-based exponential backoff retries and extending upload link expiration to 12 hours
### Fixed
- **upload**: Display a clear diagnostic error toast when remote file uploads fail due to an unconfigured or localhost S3 storage endpoint
### Changed
- **upload**: ⚠️ **Important Behavior Change**: Uploading files now appear in their correct sorted position in the file list, following the current sort setting, instead of being pinned to the top and jumping into place after the upload finishes. Cards no longer move when an upload completes. Note that if you sort by name, an uploading file may land outside your current view. Local uploads also show remaining time and transfer speed (e.g. `8m left (390.88 KB/s)`), and other users viewing the folder see `[User] is uploading...`.
- **agent**: ⚠️ **Important Behavior Change**: Autofill and Embedding agents will no longer post completion comments when finishing successfully to prevent clutter in asset comment feeds. Comments are now only posted if a task encounters an error.
## [0.6.2] - 2026-10-29
### Added
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent-app",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/cli",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/premiere-uxp",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "commonjs",
"scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode-app",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/web-app",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"dependencies": {
+16 -16
View File
@@ -32,7 +32,7 @@
},
"apps/agent": {
"name": "@shumai/agent-app",
"version": "0.6.2",
"version": "0.6.3",
"bin": {
"shumai-agent": "./src/index.ts",
},
@@ -44,7 +44,7 @@
},
"apps/cli": {
"name": "@shumai/cli",
"version": "0.6.2",
"version": "0.6.3",
"bin": {
"shumai-cli": "./src/index.ts",
},
@@ -61,7 +61,7 @@
},
"apps/premiere-uxp": {
"name": "@shumai/premiere-uxp",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@shumai/api": "workspace:*",
"@shumai/dtos": "workspace:*",
@@ -115,7 +115,7 @@
},
"apps/transcode": {
"name": "@shumai/transcode-app",
"version": "0.6.2",
"version": "0.6.3",
"bin": {
"shumai-transcode": "./src/index.ts",
},
@@ -127,7 +127,7 @@
},
"apps/web": {
"name": "@shumai/web-app",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@shumai/agent": "workspace:*",
"@shumai/api": "workspace:*",
@@ -147,7 +147,7 @@
},
"packages/agent": {
"name": "@shumai/agent",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@anthropic-ai/sandbox-runtime": "0.0.74",
"@earendil-works/pi-agent-core": "0.82.1",
@@ -171,7 +171,7 @@
},
"packages/api": {
"name": "@shumai/api",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@better-auth/mcp": "1.7.12",
"@hono/zod-validator": "0.7.6",
@@ -191,7 +191,7 @@
},
"packages/core": {
"name": "@shumai/core",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@ai-sdk/alibaba": "2.0.38",
"@ai-sdk/amazon-bedrock": "5.0.67",
@@ -255,7 +255,7 @@
},
"packages/db": {
"name": "@shumai/db",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@earendil-works/pi-agent-core": "0.82.1",
"@prisma/adapter-pg": "7.10.0",
@@ -271,7 +271,7 @@
},
"packages/dtos": {
"name": "@shumai/dtos",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@earendil-works/pi-agent-core": "0.82.1",
"zod": "4.5.4",
@@ -279,7 +279,7 @@
},
"packages/e2e": {
"name": "@shumai/e2e",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@earendil-works/pi-agent-core": "0.82.1",
"@google/genai": "2.28.0",
@@ -294,18 +294,18 @@
},
"packages/operations": {
"name": "@shumai/operations",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"zod": "4.5.4",
},
},
"packages/tableprinter": {
"name": "@shumai/tableprinter",
"version": "0.6.2",
"version": "0.6.3",
},
"packages/transcode": {
"name": "@shumai/transcode",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
@@ -316,7 +316,7 @@
},
"packages/webui": {
"name": "@shumai/webui",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@base-ui/react": "1.7.0",
"@better-auth/oauth-provider": "1.7.12",
@@ -419,7 +419,7 @@
},
"packages/workflow-core": {
"name": "@shumai/workflow-core",
"version": "0.6.2",
"version": "0.6.3",
"dependencies": {
"@dbos-inc/dbos-sdk": "5.2.11",
"@shumai/core": "workspace:*",
+18
View File
@@ -3,6 +3,24 @@ title: "Changelog"
description: "Recent updates, new features, and improvements in Shumai"
---
<Update label="v0.6.3" description="November 1, 2026">
### Added
- **upload**: Allow users to cancel running upload tasks directly from the upload tasks sidebar with a confirmation dialog, preserving already completed files while discarding incomplete assets
- **upload**: Improve upload resilience during network drops, device sleep, and transient gateway errors by adding state-based exponential backoff retries and extending upload link expiration to 12 hours
### Fixed
- **upload**: Display a clear diagnostic error toast when remote file uploads fail due to an unconfigured or localhost S3 storage endpoint
### Changed
- **upload**: ⚠️ **Important Behavior Change**: Uploading files now appear in their correct sorted position in the file list, following the current sort setting, instead of being pinned to the top and jumping into place after the upload finishes. Cards no longer move when an upload completes. Note that if you sort by name, an uploading file may land outside your current view. Local uploads also show remaining time and transfer speed (e.g. `8m left (390.88 KB/s)`), and other users viewing the folder see `[User] is uploading...`.
- **agent**: ⚠️ **Important Behavior Change**: Autofill and Embedding agents will no longer post completion comments when finishing successfully to prevent clutter in asset comment feeds. Comments are now only posted if a task encounters an error.
</Update>
<Update label="v0.6.2" description="October 29, 2026">
### Added
+10 -9
View File
@@ -38,15 +38,16 @@ Shumai is configured via environment variables. You can define these variables i
Shumai supports storing files locally or on S3-compatible endpoints.
| Variable | Description | Example / Default |
| :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------ |
| `STORAGE_BACKEND` | Storage type. Use `local` for local file system storage, or `s3` for S3-compatible cloud storage. | `local` (default) or `s3` |
| `AWS_ENDPOINT_URL_S3` | Endpoint for S3 storage or local storage. Used by the browser to build file upload URLs. Must include port if deploying remotely or using custom ports. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback if unset) |
| `S3_BUCKET` | S3 bucket name. | `shumai` (default) |
| `S3_ACCESS_KEY_ID` | Access key credentials for S3. | `minioadmin` (default) |
| `S3_SECRET_ACCESS_KEY` | Secret key credentials for S3. | `minioadmin` (default) |
| `S3_REGION` | S3 storage region. | `auto` (default) |
| `PRESIGNED_URL_EXPIRES_IN` | Number of hours for which generated presigned download/upload links are valid. | `5` (default) |
| Variable | Description | Example / Default |
| :-------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------ |
| `STORAGE_BACKEND` | Storage type. Use `local` for local file system storage, or `s3` for S3-compatible cloud storage. | `local` (default) or `s3` |
| `AWS_ENDPOINT_URL_S3` | Endpoint for S3 storage or local storage. Used by the browser to build file upload URLs. Must include port if deploying remotely or using custom ports. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback if unset) |
| `S3_BUCKET` | S3 bucket name. | `shumai` (default) |
| `S3_ACCESS_KEY_ID` | Access key credentials for S3. | `minioadmin` (default) |
| `S3_SECRET_ACCESS_KEY` | Secret key credentials for S3. | `minioadmin` (default) |
| `S3_REGION` | S3 storage region. | `auto` (default) |
| `PRESIGNED_URL_EXPIRES_IN` | Number of hours for which generated presigned download links are valid. | `5` (default) |
| `PRESIGNED_UPLOAD_URL_EXPIRES_IN` | Number of hours for which generated presigned upload links are valid. Falls back to `PRESIGNED_URL_EXPIRES_IN` if unset. | `12` (default) |
### Local Storage Port Configuration
+10 -9
View File
@@ -13,15 +13,16 @@ Using external S3 storage is highly recommended for production teams. It allows
To enable S3-compatible storage, update the following environment variables in your `.env` file or Docker Compose configuration:
| Variable | Description | Required | Default / Example |
| :------------------------- | :------------------------------------------------------------------------ | :------- | :----------------------------------- |
| `STORAGE_BACKEND` | Set to `s3` to enable S3 compatible storage. | Yes | `s3` |
| `S3_BUCKET` | The name of the S3 bucket where files will be stored. | Yes | `shumai-bucket` |
| `S3_REGION` | The region of your S3 bucket. | No | `auto` |
| `S3_ACCESS_KEY_ID` | The access key ID for your storage service. | Yes | `your-access-key-id` |
| `S3_SECRET_ACCESS_KEY` | The secret access key for your storage service. | Yes | `your-secret-access-key` |
| `AWS_ENDPOINT_URL_S3` | The endpoint URL of your S3 compatible storage provider. | Yes | `https://s3.us-east-1.amazonaws.com` |
| `PRESIGNED_URL_EXPIRES_IN` | Duration in hours before generated presigned download/upload URLs expire. | No | `5` |
| Variable | Description | Required | Default / Example |
| :-------------------------------- | :------------------------------------------------------------------------------------------------------------------ | :------- | :----------------------------------- |
| `STORAGE_BACKEND` | Set to `s3` to enable S3 compatible storage. | Yes | `s3` |
| `S3_BUCKET` | The name of the S3 bucket where files will be stored. | Yes | `shumai-bucket` |
| `S3_REGION` | The region of your S3 bucket. | No | `auto` |
| `S3_ACCESS_KEY_ID` | The access key ID for your storage service. | Yes | `your-access-key-id` |
| `S3_SECRET_ACCESS_KEY` | The secret access key for your storage service. | Yes | `your-secret-access-key` |
| `AWS_ENDPOINT_URL_S3` | The endpoint URL of your S3 compatible storage provider. | Yes | `https://s3.us-east-1.amazonaws.com` |
| `PRESIGNED_URL_EXPIRES_IN` | Duration in hours before generated presigned download URLs expire. | No | `5` |
| `PRESIGNED_UPLOAD_URL_EXPIRES_IN` | Duration in hours before generated presigned upload URLs expire. Falls back to `PRESIGNED_URL_EXPIRES_IN` if unset. | No | `12` |
---
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "shumai",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"repository": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
+23 -1
View File
@@ -23,7 +23,7 @@ import {
import * as piAgent from '../index'
import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core'
import { DatabaseSessionStorage, type DatabaseSessionMetadata } from '../database-session-storage'
import { triggerLocalCancel, runInLocalTaskContext } from '@shumai/workflow-core'
import { triggerLocalCancel, runInLocalTaskContext, DBOS, DBOSError } from '@shumai/workflow-core'
import { logger } from '@shumai/core/src/logger'
import {
@@ -1240,6 +1240,28 @@ describe('Agent Database Activities Integration', () => {
const deleted = await prisma.assetComment.findUnique({ where: { id: comment.id } })
expect(deleted).toBeNull()
})
it('should create comment even if cancelled with DBOSWorkflowCancelledError (runOnCancel: true)', async () => {
const realCancelErr = new DBOSError.DBOSWorkflowCancelledError('test-workflow-id')
const isWithinWorkflowSpy = vi.spyOn(DBOS, 'isWithinWorkflow').mockReturnValue(true)
const runStepSpy = vi.spyOn(DBOS, 'runStep').mockRejectedValue(realCancelErr)
const comment = await createCommentActivity({
assetId: asset.id,
message: 'Autofill failed: workflow cancelled',
agentId: 'default',
})
expect(comment.id).toBeDefined()
expect(comment.message).toBe('Autofill failed: workflow cancelled')
const savedComment = await prisma.assetComment.findUnique({ where: { id: comment.id } })
expect(savedComment).not.toBeNull()
expect(savedComment?.message).toBe('Autofill failed: workflow cancelled')
isWithinWorkflowSpy.mockRestore()
runStepSpy.mockRestore()
})
})
describe('Context Retrievals', () => {
+73 -65
View File
@@ -969,78 +969,86 @@ export const deleteCommentActivity = defineStep(async function deleteCommentActi
})
})
export const createCommentActivity = defineStep(async function createCommentActivity(params: {
assetId: string
message: string
sessionId?: string | null
agentId?: string | null
replyToId?: string | null
}) {
if (params.sessionId) {
const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default'
export const createCommentActivity = defineStep(
async function createCommentActivity(params: {
assetId: string
message: string
sessionId?: string | null
agentId?: string | null
replyToId?: string | null
}) {
if (params.sessionId) {
const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default'
// Fetch teamId from asset
const asset = await prisma.asset.findUnique({
where: { id: params.assetId },
include: { project: true },
})
if (!asset || !asset.project) {
throw ApplicationFailure.create({ message: 'asset or project not found', nonRetryable: true })
}
const teamId = asset.project.teamId
// Ensure user exists for agent
const userExists = await prisma.user.findUnique({ where: { id: agentId } })
if (!userExists) {
await prisma.user.create({
data: {
id: agentId,
name: 'Ai Agent',
email: `${agentId}@shumai.ai`,
type: 'agent',
},
// Fetch teamId from asset
const asset = await prisma.asset.findUnique({
where: { id: params.assetId },
include: { project: true },
})
}
if (!asset || !asset.project) {
throw ApplicationFailure.create({
message: 'asset or project not found',
nonRetryable: true,
})
}
const teamId = asset.project.teamId
// Ensure agent exists
const agentExists = await prisma.agent.findUnique({ where: { id: agentId } })
if (!agentExists) {
await prisma.agent.create({
data: {
id: agentId,
teamId,
type: 'chat',
config: {
provider: 'openai',
model: 'gpt-4',
// Ensure user exists for agent
const userExists = await prisma.user.findUnique({ where: { id: agentId } })
if (!userExists) {
await prisma.user.create({
data: {
id: agentId,
name: 'Ai Agent',
email: `${agentId}@shumai.ai`,
type: 'agent',
},
},
})
}
// Ensure agent exists
const agentExists = await prisma.agent.findUnique({ where: { id: agentId } })
if (!agentExists) {
await prisma.agent.create({
data: {
id: agentId,
teamId,
type: 'chat',
config: {
provider: 'openai',
model: 'gpt-4',
},
},
})
}
// Ensure agent session exists
const sessionExists = await prisma.agentSession.findUnique({
where: { id: params.sessionId },
})
if (!sessionExists) {
await prisma.agentSession.create({
data: {
id: params.sessionId,
agentId: agentId,
cwd: process.cwd(),
},
})
}
}
// Ensure agent session exists
const sessionExists = await prisma.agentSession.findUnique({ where: { id: params.sessionId } })
if (!sessionExists) {
await prisma.agentSession.create({
data: {
id: params.sessionId,
agentId: agentId,
cwd: process.cwd(),
},
})
}
}
return prisma.assetComment.create({
data: {
assetId: params.assetId,
message: params.message,
sessionId: params.sessionId ?? null,
creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null,
replyToId: params.replyToId,
},
})
})
return prisma.assetComment.create({
data: {
assetId: params.assetId,
message: params.message,
sessionId: params.sessionId ?? null,
creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null,
replyToId: params.replyToId,
},
})
},
{ runOnCancel: true },
)
export const updateCommentActivity = defineStep(
async function updateCommentActivity(params: {
@@ -3,6 +3,7 @@ import { agentAutofillMedia } from './agent-autofill'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import * as workflowCore from '@shumai/workflow-core'
import { DBOSError } from '@shumai/workflow-core'
import * as agentActivities from '../activities/agent'
vi.mock('@shumai/workflow-core', async () => {
@@ -95,13 +96,9 @@ describe('Agent Autofill Workflow', () => {
// Verify asset fetched
expect(mockActivities.getAssetActivity).toHaveBeenCalledWith('a1')
// Verify placeholder comment created
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
assetId: 'a1',
message: '__AUTOFILL__',
sessionId: 's1',
agentId: 'agent-1',
})
// Verify no success or placeholder comments created
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
// Verify fields and context fetched
expect(mockActivities.getProjectAutofillFieldsActivity).toHaveBeenCalledWith('p1')
@@ -147,13 +144,6 @@ describe('Agent Autofill Workflow', () => {
],
})
// Verify placeholder comment updated
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
commentId: 'comment-placeholder-id',
message: 'Autofill completed successfully.',
sessionId: 'session-123',
})
// Verify completed task status
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
@@ -205,11 +195,8 @@ describe('Agent Autofill Workflow', () => {
await agentAutofillMedia(task)
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
commentId: 'comment-placeholder-id',
message: 'Autofill completed: No autofill fields defined in project.',
})
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
expect(mockActivities.autofillAiActivity).not.toHaveBeenCalled()
expect(mockActivities.updateAssetMetadataActivity).not.toHaveBeenCalled()
@@ -219,7 +206,7 @@ describe('Agent Autofill Workflow', () => {
})
})
it('should handle failures, update placeholder with error, and set status to failed', async () => {
it('should handle failures, create comment with error, and set status to failed', async () => {
mockActivities.getProjectAutofillFieldsActivity.mockRejectedValue(new Error('DB failure'))
const task = await prisma.workflowTask.create({
@@ -232,10 +219,12 @@ describe('Agent Autofill Workflow', () => {
await expect(agentAutofillMedia(task)).rejects.toThrow('DB failure')
// Verify placeholder updated with error
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
commentId: 'comment-placeholder-id',
// Verify error comment created
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
assetId: 'a1',
message: 'Autofill failed: DB failure',
sessionId: task.id,
agentId: 'default',
})
// Verify task failed status
@@ -246,6 +235,33 @@ describe('Agent Autofill Workflow', () => {
})
})
it('should post an error comment when workflow is cancelled with DBOSWorkflowCancelledError', async () => {
const cancelErr = new DBOSError.DBOSWorkflowCancelledError('wf-cancel')
mockActivities.getProjectAutofillFieldsActivity.mockRejectedValue(cancelErr)
const task = await prisma.workflowTask.create({
data: {
type: 'ai_metadata_autofill',
status: 'pending',
assetId: 'a1',
},
})
await expect(agentAutofillMedia(task)).rejects.toThrow()
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
assetId: 'a1',
message: expect.stringContaining('Autofill failed:'),
sessionId: task.id,
agentId: 'default',
})
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
status: 'failed',
output: { error: expect.stringContaining('wf-cancel') },
})
})
it('should fail with non-retryable ApplicationFailure when asset or project is not found', async () => {
mockActivities.getAssetActivity.mockResolvedValue(null)
+11 -38
View File
@@ -12,12 +12,9 @@ import {
getAssetActivity,
getProjectAutofillFieldsActivity,
updateAssetMetadataActivity,
updateCommentActivity,
} from '../activities/agent'
export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
let placeholderCommentId: string | undefined
try {
// Update status to processing
await updateTaskStatusActivity({
@@ -25,16 +22,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
status: 'processing',
})
// 0. Create Placeholder Comment
const payload = task.payload
const placeholder = await createCommentActivity({
assetId: task.assetId,
message: '__AUTOFILL__',
sessionId: payload?.agent?.sessionId || task.id,
agentId: payload?.agent?.agentId || 'default',
})
placeholderCommentId = placeholder.id
// 1. Get Asset
const asset = await getAssetActivity(task.assetId)
if (!asset || !asset.project) {
@@ -46,12 +33,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
// 2. Get Project Autofill Fields
const fields = await getProjectAutofillFieldsActivity(projectId)
if (fields.length === 0) {
if (placeholderCommentId) {
await updateCommentActivity({
commentId: placeholderCommentId,
message: 'Autofill completed: No autofill fields defined in project.',
})
}
await updateTaskStatusActivity({
taskId: task.id,
status: 'completed',
@@ -124,15 +105,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
})
}
// 7. Update Placeholder Comment
if (placeholderCommentId) {
await updateCommentActivity({
commentId: placeholderCommentId,
message: 'Autofill completed successfully.',
sessionId: aiResult.sessionId,
})
}
// Update status to completed
await updateTaskStatusActivity({
taskId: task.id,
@@ -141,16 +113,17 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
} catch (err) {
console.error(`AgentAutofillMedia failed for task ${task.id}:`, err)
// Update placeholder comment with error message
if (placeholderCommentId) {
try {
await updateCommentActivity({
commentId: placeholderCommentId,
message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`,
})
} catch (commentErr) {
console.error('Failed to update error comment:', commentErr)
}
// Create error comment
try {
const payload = task.payload
await createCommentActivity({
assetId: task.assetId,
message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`,
sessionId: payload?.agent?.sessionId || task.id,
agentId: payload?.agent?.agentId || 'default',
})
} catch (commentErr) {
console.error('Failed to create error comment:', commentErr)
}
// Update status to failed
@@ -3,6 +3,7 @@ import { agentEmbeddingMedia } from './agent-embedding'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import * as workflowCore from '@shumai/workflow-core'
import { DBOSError } from '@shumai/workflow-core'
import * as agentActivities from '../activities/agent'
import * as aiActivities from '../activities/ai'
@@ -122,13 +123,9 @@ describe('Agent Embedding Workflow', () => {
status: 'processing',
})
// Verify placeholder comment created
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
assetId: 'a1',
message: '__EMBEDDING__',
sessionId: 's1',
agentId: 'agent-1',
})
// Verify no comments created on success
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
// Verify context fetching
expect(mockActivities.getEmbeddingContextActivity).toHaveBeenCalledWith({
@@ -157,12 +154,6 @@ describe('Agent Embedding Workflow', () => {
model: 'gemini-embedding-2',
})
// Verify placeholder comment updated
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
commentId: 'comment-placeholder-id',
message: 'Embedding completed successfully.',
})
// Verify completed task status
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
@@ -417,7 +408,7 @@ describe('Agent Embedding Workflow', () => {
})
})
it('should handle failures, update placeholder with error, and set status to failed', async () => {
it('should handle failures, create comment with error, and set status to failed', async () => {
mockActivities.generateImageEmbeddingActivity.mockRejectedValue(
new Error('AI Service Unavailable'),
)
@@ -433,11 +424,14 @@ describe('Agent Embedding Workflow', () => {
await expect(agentEmbeddingMedia(task)).rejects.toThrow('AI Service Unavailable')
// Verify placeholder updated with error
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
commentId: 'comment-placeholder-id',
// Verify error comment created
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
assetId: 'a1',
message: 'Embedding failed: AI Service Unavailable',
sessionId: task.id,
agentId: 'default',
})
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
// Verify status updated to failed
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
@@ -447,6 +441,34 @@ describe('Agent Embedding Workflow', () => {
})
})
it('should post an error comment when workflow is cancelled with DBOSWorkflowCancelledError', async () => {
const cancelErr = new DBOSError.DBOSWorkflowCancelledError('wf-cancel')
mockActivities.generateImageEmbeddingActivity.mockRejectedValue(cancelErr)
const task = await prisma.workflowTask.create({
data: {
type: 'ai_embedding',
status: 'pending',
assetId: 'a1',
teamId: 't1',
},
})
await expect(agentEmbeddingMedia(task)).rejects.toThrow()
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
assetId: 'a1',
message: expect.stringContaining('Embedding failed:'),
sessionId: task.id,
agentId: 'default',
})
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: task.id,
status: 'failed',
output: { error: expect.stringContaining('wf-cancel') },
})
})
it('should save completed chunks to DB immediately before subsequent chunk failure', async () => {
mockActivities.getEmbeddingContextActivity.mockResolvedValue({
agent: { id: 'b1' },
+12 -31
View File
@@ -4,7 +4,7 @@ import {
updateTaskStatusActivity,
updateTaskUsageActivity,
} from '@shumai/workflow-core'
import { createCommentActivity, updateCommentActivity } from '../activities/agent'
import { createCommentActivity } from '../activities/agent'
import {
deleteAssetEmbeddingsActivity,
deleteS3ObjectActivity,
@@ -25,8 +25,6 @@ export interface GeneratedEmbedding {
export type { ExistingAssetEmbeddingInfo }
export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
let placeholderCommentId: string | undefined
try {
// Update status to processing
await updateTaskStatusActivity({
@@ -34,16 +32,6 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
status: 'processing',
})
// 0. Create Placeholder Comment
const payload = task.payload
const placeholder = await createCommentActivity({
assetId: task.assetId,
message: '__EMBEDDING__',
sessionId: payload?.agent?.sessionId || task.id,
agentId: payload?.agent?.agentId || 'default',
})
placeholderCommentId = placeholder.id
if (!task.teamId) {
throw ApplicationFailure.create({ message: 'Task has no teamId', nonRetryable: true })
}
@@ -238,14 +226,6 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
model: usage.model,
})
// 7. Update Placeholder Comment
if (placeholderCommentId) {
await updateCommentActivity({
commentId: placeholderCommentId,
message: 'Embedding completed successfully.',
})
}
// Update status to completed
await updateTaskStatusActivity({
taskId: task.id,
@@ -254,16 +234,17 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
} catch (err) {
console.error(`AgentEmbeddingMedia failed for task ${task.id}:`, err)
// Update placeholder comment with error message
if (placeholderCommentId) {
try {
await updateCommentActivity({
commentId: placeholderCommentId,
message: `Embedding failed: ${err instanceof Error ? err.message : String(err)}`,
})
} catch (commentErr) {
console.error('Failed to update error comment:', commentErr)
}
// Create error comment
try {
const payload = task.payload
await createCommentActivity({
assetId: task.assetId,
message: `Embedding failed: ${err instanceof Error ? err.message : String(err)}`,
sessionId: payload?.agent?.sessionId || task.id,
agentId: payload?.agent?.agentId || 'default',
})
} catch (commentErr) {
console.error('Failed to create error comment:', commentErr)
}
// Update status to failed
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/api",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
+74
View File
@@ -9,6 +9,7 @@ import { s3Service } from '@shumai/core/src/s3/s3'
import { transcodeService } from '@shumai/core'
import { authMiddleware } from './middleware/auth'
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
import type { AssetInfo } from '@shumai/dtos'
import {
renameAssetOp,
resolveCommentOp,
@@ -72,6 +73,7 @@ describe('file api', () => {
vi.mocked(authzService.hasPermission).mockClear()
vi.spyOn(assetService, 'getAsset').mockImplementation(vi.fn())
vi.spyOn(assetService, 'listAssetsByIds').mockImplementation(vi.fn())
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({ teamId: 'test-team' })
vi.spyOn(assetService, 'updateAssetName').mockImplementation(vi.fn())
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
@@ -126,6 +128,78 @@ describe('file api', () => {
})
})
describe('POST /files/batch', () => {
it('returns empty array when ids is empty', async () => {
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: [] }),
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json).toEqual([])
expect(assetService.listAssetsByIds).not.toHaveBeenCalled()
})
it('returns asset list for accessible IDs', async () => {
const assets = [
{ id: 'f1', name: 'file-1.mp4', status: 'processing' },
{ id: 'f2', name: 'file-2.mp4', status: 'processed' },
]
vi.mocked(assetService.listAssetsByIds).mockResolvedValue(assets as unknown as AssetInfo[])
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: ['f1', 'f2'] }),
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json).toEqual(assets)
expect(assetService.listAssetsByIds).toHaveBeenCalledWith(['f1', 'f2'])
})
it('gracefully omits unauthorized or missing assets without failing the batch', async () => {
vi.mocked(authzService.hasPermission).mockImplementation(async (req) => {
if (req.id === 'unauthorized-id') {
throw new HTTPException(403, { message: 'Permission denied' })
}
})
const accessibleAssets = [{ id: 'f1', name: 'file-1.mp4', status: 'processing' }]
vi.mocked(assetService.listAssetsByIds).mockResolvedValue(
accessibleAssets as unknown as AssetInfo[],
)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: ['f1', 'unauthorized-id'] }),
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json).toEqual(accessibleAssets)
expect(assetService.listAssetsByIds).toHaveBeenCalledWith(['f1'])
})
it('rejects with 400 when more than 100 IDs are requested', async () => {
const tooManyIds = Array.from({ length: 101 }, (_, i) => `id-${i}`)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: tooManyIds }),
})
expect(res.status).toBe(400)
})
})
it('PUT /files/:fileId runs rename_asset', async () => {
const summary = { id: 'test-id', name: 'new-name', type: 'file', size: 10, projectId: 'p1' }
vi.mocked(runOperation).mockResolvedValue(summary)
+35
View File
@@ -14,6 +14,7 @@ import {
import {
createCommentRequestSchema,
deleteFilesRequestSchema,
batchGetFilesRequestSchema,
paginationParamsSchema,
restoreFilesRequestSchema,
updateAssetMetadataRequestSchema,
@@ -39,6 +40,40 @@ import { z } from 'zod'
type User = Prisma.UserGetPayload<Record<string, never>>
const route = new Hono<{ Variables: { user: User } }>()
.post('/files/batch', zValidator('json', batchGetFilesRequestSchema), async (c) => {
const user = c.get('user')
const req = c.req.valid('json')
if (req.ids.length === 0) {
return c.json([])
}
const accessibleIds: string[] = []
await Promise.all(
req.ids.map(async (id) => {
try {
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id,
})
accessibleIds.push(id)
} catch {
// Permissive: silently ignore missing or unauthorized IDs
}
}),
)
if (accessibleIds.length === 0) {
return c.json([])
}
const accessibleSet = new Set(accessibleIds)
const orderedAccessibleIds = req.ids.filter((id) => accessibleSet.has(id))
const assets = await assetService.listAssetsByIds(orderedAccessibleIds)
return c.json(assets)
})
.get('/files/:fileId', async (c) => {
const fileId = c.req.param('fileId')
const user = c.get('user')
+23
View File
@@ -220,4 +220,27 @@ describe('Upload API', () => {
key: 'files/test.mp4',
})
})
it('POST /teams/:teamId/upload/tasks/:taskId/cancel', async () => {
vi.spyOn(uploadService, 'cancelUploadTask').mockResolvedValue({
success: true,
})
const res = await app.request('/teams/team1/upload/tasks/task1/cancel', {
method: 'POST',
headers: { Authorization: 'Bearer test' },
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json).toEqual({ success: true })
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
type: ResourceType.Team,
id: 'team1',
permission: Permission.Edit,
}),
)
expect(uploadService.cancelUploadTask).toHaveBeenCalledWith('team1', 'user1', 'task1')
})
})
+15
View File
@@ -163,5 +163,20 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json(resp)
},
)
.post('/teams/:teamId/upload/tasks/:taskId/cancel', async (c) => {
const teamId = c.req.param('teamId')
const taskId = c.req.param('taskId')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Edit,
type: ResourceType.Team,
id: teamId,
})
const resp = await uploadService.cancelUploadTask(teamId, user.id, taskId)
return c.json(resp)
})
export default route
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/core",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
+73 -1
View File
@@ -40,6 +40,7 @@ describe('S3Service implementations', () => {
vi.clearAllMocks()
getSignedUrlSpy.mockReset()
delete process.env.PRESIGNED_URL_EXPIRES_IN
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
getSignedUrlSpy.mockResolvedValue('http://presigned-url')
s3SendSpy.mockResolvedValue({})
})
@@ -146,7 +147,7 @@ describe('S3Service implementations', () => {
expect(getSignedUrlSpy).toHaveBeenCalledTimes(2)
})
it('should respect PRESIGNED_URL_EXPIRES_IN env', async () => {
it('should respect PRESIGNED_URL_EXPIRES_IN env for GET', async () => {
process.env.PRESIGNED_URL_EXPIRES_IN = '10'
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
@@ -159,6 +160,77 @@ describe('S3Service implementations', () => {
expect.objectContaining({ expiresIn: 10 * 3600 }),
)
delete process.env.PRESIGNED_URL_EXPIRES_IN
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
})
it('should default PUT presign and multipart to 12 hours (43200 seconds)', async () => {
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'PUT')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 12 * 3600 }),
)
getSignedUrlSpy.mockClear()
await s3.presignMultipart('bucket', 'key', {
method: 'PUT',
key: 'key',
fileId: 'file-1',
uploadId: 'up-1',
partNumber: 1,
})
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(UploadPartCommand),
expect.objectContaining({ expiresIn: 12 * 3600 }),
)
})
it('should respect PRESIGNED_UPLOAD_URL_EXPIRES_IN env for PUT and multipart', async () => {
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN = '24'
process.env.PRESIGNED_URL_EXPIRES_IN = '5'
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'PUT')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 24 * 3600 }),
)
getSignedUrlSpy.mockClear()
await s3.presignMultipart('bucket', 'key', {
method: 'PUT',
key: 'key',
fileId: 'file-1',
})
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 24 * 3600 }),
)
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
it('should allow PRESIGNED_URL_EXPIRES_IN to override PUT if PRESIGNED_UPLOAD_URL_EXPIRES_IN is unset', async () => {
process.env.PRESIGNED_URL_EXPIRES_IN = '8'
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'PUT')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 8 * 3600 }),
)
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
+15 -4
View File
@@ -112,7 +112,7 @@ export interface S3Service {
download?: boolean,
filename?: string,
internal?: boolean,
/** URL lifetime; defaults to PRESIGNED_URL_EXPIRES_IN hours. Ignored by local storage. */
/** URL lifetime; defaults to PRESIGNED_UPLOAD_URL_EXPIRES_IN (12h) for PUT, or PRESIGNED_URL_EXPIRES_IN (5h) for GET. Ignored by local storage. */
expiresInSeconds?: number,
) => Promise<string>
presignMultipart: (
@@ -364,8 +364,16 @@ export class S3StorageService implements S3Service {
internal?: boolean,
expiresInSeconds?: number,
): Promise<string> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const lifetimeSeconds = expiresInSeconds ?? expireHours * 3600
const defaultExpireHours =
method === 'PUT'
? parseInt(
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN ||
process.env.PRESIGNED_URL_EXPIRES_IN ||
'12',
10,
)
: parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const lifetimeSeconds = expiresInSeconds ?? defaultExpireHours * 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
@@ -416,7 +424,10 @@ export class S3StorageService implements S3Service {
key: string,
request: S3SignRequest,
): Promise<{ url: string }> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const expireHours = parseInt(
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN || process.env.PRESIGNED_URL_EXPIRES_IN || '12',
10,
)
const expiresInSeconds = expireHours * 3600
let url: string
+259
View File
@@ -797,6 +797,7 @@ describe('UploadService', () => {
expect(result.data).toHaveLength(1)
expect(result.data[0].id).toBe(task.id)
expect(result.data[0].name).toBe('test-upload-task')
expect(result.data[0].status).toBe(TaskStatus.pending)
})
it('should correct the mediaType for .wma files that browser incorrectly reports as video', async () => {
@@ -1120,4 +1121,262 @@ describe('UploadService', () => {
expect(deletedAsset).toBeNull()
})
})
describe('cancelUploadTask', () => {
it('cancels uploading assets, leaves uploaded assets and folders intact, and marks task failed', async () => {
const task = await prisma.task.create({
data: {
creatorId: userId,
type: 'upload',
name: 'multi-file-task',
total: 3,
uploaded: 1,
status: TaskStatus.pending,
},
})
// 1. A folder created by the task
const folder = await prisma.asset.create({
data: {
name: 'subfolder',
type: AssetType.folder,
projectId,
parentId,
taskId: task.id,
status: AssetStatus.uploaded,
},
})
// 2. An asset already finished uploading
const finishedKey = await prisma.storageKey.create({ data: { key: 'files/test/done.mp4' } })
const finishedAsset = await prisma.asset.create({
data: {
name: 'done.mp4',
type: AssetType.file,
projectId,
parentId: folder.id,
storageKeyId: finishedKey.id,
taskId: task.id,
status: AssetStatus.uploaded,
},
})
// 3. An incomplete multipart asset
const multipartKey = await prisma.storageKey.create({
data: { key: 'files/test/part.mp4' },
})
const multipartAsset = await prisma.asset.create({
data: {
name: 'part.mp4',
type: AssetType.file,
projectId,
parentId: folder.id,
storageKeyId: multipartKey.id,
taskId: task.id,
uploadId: 'upload-multipart-cancel',
status: AssetStatus.uploading,
},
})
// 4. An incomplete single PUT asset
const singleKey = await prisma.storageKey.create({
data: { key: 'files/test/single.mp4' },
})
const singleAsset = await prisma.asset.create({
data: {
name: 'single.mp4',
type: AssetType.file,
projectId,
parentId: folder.id,
storageKeyId: singleKey.id,
taskId: task.id,
status: AssetStatus.uploading,
},
})
const res = await uploadService.cancelUploadTask(teamId, userId, task.id)
expect(res).toEqual({ success: true })
// Incomplete assets deleted
expect(await prisma.asset.findUnique({ where: { id: multipartAsset.id } })).toBeNull()
expect(await prisma.storageKey.findUnique({ where: { id: multipartKey.id } })).toBeNull()
expect(await prisma.asset.findUnique({ where: { id: singleAsset.id } })).toBeNull()
expect(await prisma.storageKey.findUnique({ where: { id: singleKey.id } })).toBeNull()
// S3 cleanup called
expect(s3Service.abortMultipartUpload).toHaveBeenCalledWith(
expect.anything(),
'files/test/part.mp4',
'upload-multipart-cancel',
)
expect(s3Service.deleteObject).toHaveBeenCalledWith(
expect.anything(),
'files/test/single.mp4',
)
// Completed asset and folder remain intact
expect(await prisma.asset.findUnique({ where: { id: finishedAsset.id } })).not.toBeNull()
expect(await prisma.storageKey.findUnique({ where: { id: finishedKey.id } })).not.toBeNull()
expect(await prisma.asset.findUnique({ where: { id: folder.id } })).not.toBeNull()
// Task status updated to failed
const updatedTask = await prisma.task.findUnique({ where: { id: task.id } })
expect(updatedTask?.status).toBe(TaskStatus.failed)
expect(updatedTask?.uploaded).toBe(1)
})
it('rejects cancellation if the caller is not the task creator', async () => {
const otherUser = await prisma.user.create({
data: {
email: 'other@example.com',
name: 'Other User',
},
})
const task = await prisma.task.create({
data: {
creatorId: otherUser.id,
type: 'upload',
name: 'other-task',
total: 1,
status: TaskStatus.pending,
},
})
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
'Unauthorized to cancel this upload task',
)
})
it('throws when task does not exist', async () => {
await expect(
uploadService.cancelUploadTask(teamId, userId, 'non-existent-task'),
).rejects.toThrow('Upload task not found')
})
it('throws when task assets belong to a different team', async () => {
const otherTeam = await prisma.team.create({
data: { name: 'Other Team' },
})
const otherProject = await prisma.project.create({
data: { name: 'Other Project', teamId: otherTeam.id },
})
const task = await prisma.task.create({
data: {
creatorId: userId,
type: 'upload',
name: 'wrong-team-task',
total: 1,
status: TaskStatus.pending,
},
})
await prisma.asset.create({
data: {
name: 'wrong-team-file.mp4',
type: AssetType.file,
projectId: otherProject.id,
taskId: task.id,
status: AssetStatus.uploading,
},
})
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
'Task does not belong to specified team',
)
})
it('throws when task is already completed and preserves completed status', async () => {
const task = await prisma.task.create({
data: {
creatorId: userId,
type: 'upload',
name: 'completed-task',
total: 1,
uploaded: 1,
status: TaskStatus.completed,
},
})
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
'Cannot cancel a completed upload task',
)
const refreshed = await prisma.task.findUnique({ where: { id: task.id } })
expect(refreshed?.status).toBe(TaskStatus.completed)
})
it('preserves an asset and its storage if it was confirmed uploaded concurrently', async () => {
const task = await prisma.task.create({
data: {
creatorId: userId,
type: 'upload',
name: 'race-task',
total: 2,
uploaded: 0,
status: TaskStatus.uploading,
},
})
const confirmedAsset = await prisma.asset.create({
data: {
name: 'race-confirmed.mp4',
type: AssetType.file,
project: { connect: { id: projectId } },
task: { connect: { id: task.id } },
status: AssetStatus.uploading,
storageKey: {
create: { key: 'files/test/race-confirmed.mp4' },
},
},
include: { storageKey: true },
})
const cancelledAsset = await prisma.asset.create({
data: {
name: 'race-cancelled.mp4',
type: AssetType.file,
project: { connect: { id: projectId } },
task: { connect: { id: task.id } },
status: AssetStatus.uploading,
storageKey: {
create: { key: 'files/test/race-cancelled.mp4' },
},
},
include: { storageKey: true },
})
// Simulate concurrent confirm: update status to uploaded
await prisma.asset.update({
where: { id: confirmedAsset.id },
data: { status: AssetStatus.uploaded },
})
const deleteObjectSpy = vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(1)
await uploadService.cancelUploadTask(teamId, userId, task.id)
// Confirmed asset must still exist in DB
const dbConfirmed = await prisma.asset.findUnique({ where: { id: confirmedAsset.id } })
expect(dbConfirmed).not.toBeNull()
expect(dbConfirmed?.status).toBe(AssetStatus.uploaded)
// Cancelled asset must be deleted
const dbCancelled = await prisma.asset.findUnique({ where: { id: cancelledAsset.id } })
expect(dbCancelled).toBeNull()
// S3 deleteObject was called for the cancelled asset, but NOT for the confirmed asset
expect(deleteObjectSpy).toHaveBeenCalledWith(
expect.anything(),
'files/test/race-cancelled.mp4',
)
expect(deleteObjectSpy).not.toHaveBeenCalledWith(
expect.anything(),
'files/test/race-confirmed.mp4',
)
deleteObjectSpy.mockRestore()
})
})
})
+106 -48
View File
@@ -5,6 +5,7 @@ import { AssetStatus, AssetType, Prisma, TaskStatus, prisma } from '@shumai/db'
import {
AbortUploadRequest,
AbortUploadResponse,
CancelUploadTaskResponse,
ConfirmFileUploadRequest,
CreateUploadTaskRequest,
CreateUploadTaskResponse,
@@ -210,30 +211,7 @@ export class UploadService {
if (!key) throw new Error('Asset has no key')
if (req.errorMessage) {
const bucket = process.env.S3_BUCKET || 'shumai'
if (asset.uploadId && key) {
try {
await s3Service.abortMultipartUpload(bucket, key, asset.uploadId)
} catch (err) {
logger.warn(
{ err, key, uploadId: asset.uploadId },
'Failed to abort multipart upload on confirm error',
)
}
} else if (key) {
try {
await s3Service.deleteObject(bucket, key)
} catch (err) {
logger.warn({ err, key }, 'Failed to delete storage object on confirm error')
}
}
await this.prismaClient.asset.delete({ where: { id: asset.id } }).catch(() => {})
if (asset.storageKeyId) {
await this.prismaClient.storageKey
.delete({ where: { id: asset.storageKeyId } })
.catch(() => {})
}
await this.discardUploadingAsset(asset)
const t = await this.prismaClient.task.findUnique({ where: { id: taskId } })
if (t) {
const updatedTask = await this.prismaClient.task.update({
@@ -250,6 +228,10 @@ export class UploadService {
return
}
if (asset.status !== AssetStatus.uploading) {
throw new Error('Asset is not in uploading status')
}
const size = await s3Service.getObjectSize(process.env.S3_BUCKET || 'shumai', key)
const project = asset.project
@@ -425,6 +407,7 @@ export class UploadService {
total: t.total || 0,
uploaded: t.uploaded,
createdAt: t.createdAt.toISOString(),
status: t.status,
}))
return { data: infos, pageInfo }
@@ -490,7 +473,6 @@ export class UploadService {
taskId: string,
req: AbortUploadRequest,
): Promise<AbortUploadResponse> {
const bucket = process.env.S3_BUCKET || 'shumai'
const asset = await this.prismaClient.asset.findUnique({
where: { id: req.fileId },
include: {
@@ -520,33 +502,11 @@ export class UploadService {
throw new Error('Provided key does not match asset storage key')
}
const uploadId = asset.uploadId || req.uploadId
if (asset.uploadId && req.uploadId && asset.uploadId !== req.uploadId) {
throw new Error('Provided uploadId does not match asset uploadId')
}
if (uploadId && key) {
try {
await s3Service.abortMultipartUpload(bucket, key, uploadId)
} catch (err) {
logger.warn({ err, key, uploadId }, 'Failed to abort multipart upload in S3')
}
} else if (key) {
try {
await s3Service.deleteObject(bucket, key)
} catch (err) {
logger.warn({ err, key }, 'Failed to delete object from storage')
}
}
await this.prismaClient.asset.delete({ where: { id: asset.id } }).catch((err) => {
logger.warn({ err, assetId: asset.id }, 'Failed to delete asset on upload abort')
})
if (asset.storageKeyId) {
await this.prismaClient.storageKey
.delete({ where: { id: asset.storageKeyId } })
.catch(() => {})
}
await this.discardUploadingAsset(asset, req.uploadId)
if (taskId) {
const remaining = await this.prismaClient.asset.count({
@@ -564,6 +524,104 @@ export class UploadService {
return { success: true }
}
private async discardUploadingAsset(
asset: {
id: string
uploadId: string | null
storageKeyId: string | null
storageKey: { key: string } | null
},
requestedUploadId?: string,
): Promise<void> {
const deleteResult = await this.prismaClient.asset.deleteMany({
where: {
id: asset.id,
status: AssetStatus.uploading,
},
})
if (deleteResult.count === 0) {
return
}
const bucket = process.env.S3_BUCKET || 'shumai'
const key = asset.storageKey?.key
const uploadId = asset.uploadId || requestedUploadId
if (uploadId && key) {
try {
await s3Service.abortMultipartUpload(bucket, key, uploadId)
} catch (err) {
logger.warn({ err, key, uploadId }, 'Failed to abort multipart upload in S3')
}
} else if (key) {
try {
await s3Service.deleteObject(bucket, key)
} catch (err) {
logger.warn({ err, key }, 'Failed to delete object from storage')
}
}
if (asset.storageKeyId) {
await this.prismaClient.storageKey
.delete({ where: { id: asset.storageKeyId } })
.catch(() => {})
}
}
async cancelUploadTask(
teamId: string,
userId: string,
taskId: string,
): Promise<CancelUploadTaskResponse> {
const task = await this.prismaClient.task.findUnique({
where: { id: taskId },
include: {
assets: {
include: {
storageKey: true,
project: true,
},
},
},
})
if (!task) {
throw new Error('Upload task not found')
}
if (task.creatorId !== userId) {
throw new Error('Unauthorized to cancel this upload task')
}
if (task.status === TaskStatus.completed) {
throw new Error('Cannot cancel a completed upload task')
}
const invalidTeamAsset = task.assets.find(
(a) => a.project?.teamId && a.project.teamId !== teamId,
)
if (invalidTeamAsset) {
throw new Error('Task does not belong to specified team')
}
const uploadingAssets = task.assets.filter((a) => a.status === AssetStatus.uploading)
for (const asset of uploadingAssets) {
await this.discardUploadingAsset(asset)
}
await this.prismaClient.task.updateMany({
where: {
id: taskId,
status: { not: TaskStatus.completed },
},
data: { status: TaskStatus.failed },
})
return { success: true }
}
}
export const uploadService = new UploadService()
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/db",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/dtos",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
+5
View File
@@ -329,6 +329,11 @@ export const deleteFilesRequestSchema = z.object({
})
export type DeleteFilesRequest = z.infer<typeof deleteFilesRequestSchema>
export const batchGetFilesRequestSchema = z.object({
ids: z.array(z.string()).max(100),
})
export type BatchGetFilesRequest = z.infer<typeof batchGetFilesRequestSchema>
export const restoreFilesRequestSchema = z.object({
ids: z.array(z.string()),
})
+6
View File
@@ -75,6 +75,11 @@ export const abortUploadResponseSchema = z.object({
})
export type AbortUploadResponse = z.infer<typeof abortUploadResponseSchema>
export const cancelUploadTaskResponseSchema = z.object({
success: z.boolean(),
})
export type CancelUploadTaskResponse = z.infer<typeof cancelUploadTaskResponseSchema>
export const confirmFileUploadRequestSchema = z.object({
fileId: z.string(),
errorMessage: z.string().optional(),
@@ -87,6 +92,7 @@ export const taskInfoSchema = z.object({
total: z.number(),
uploaded: z.number(),
createdAt: z.string(),
status: z.enum(['pending', 'uploading', 'completed', 'failed']).optional(),
})
export type TaskInfo = z.infer<typeof taskInfoSchema>
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/e2e",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"dependencies": {
+121 -5
View File
@@ -263,15 +263,12 @@ describe('Workflow E2E - agentAutofillMedia', () => {
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify placeholder comment is updated correctly
// Verify no comments created on successful autofill
const comments = await prisma.assetComment.findMany({
where: { assetId: asset.id },
orderBy: { createdAt: 'asc' },
})
expect(comments.length).toBeGreaterThan(0)
// The last updated message of placeholder comment should indicate success
const finalComment = comments[comments.length - 1]
expect(finalComment.message).toBe('Autofill completed successfully.')
expect(comments.length).toBe(0)
// Verify AssetMetadataValue contains the AI-extracted title
const titleVal = await prisma.assetMetadataValue.findUnique({
@@ -324,4 +321,123 @@ describe('Workflow E2E - agentAutofillMedia', () => {
})
expect(manualNotesVal).toBeNull()
}, 50000)
it('should post an error comment when agent autofill workflow fails', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Autofill Fail Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Autofill Fail Project', teamId: team.id },
})
await prisma.metadataField.create({
data: {
key: 'title_fail_field',
scope: 'PROJECT',
projectId: project.id,
teamId: team.id,
config: { name: 'Title', type: 'text', autofillSource: 'CONTENT' },
description: 'AI-extracted document title',
},
})
const user = await prisma.user.create({
data: {
name: 'Autofill Fail Agent User',
email: 'failautofill@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: user.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google-fail',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini-fail',
name: 'Gemini Fail',
providerId: provider.id,
config: {
input: ['text', 'image'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
const agent = await prisma.agent.create({
data: {
id: user.id,
type: 'autofill',
enabled: true,
teamId: team.id,
providerId: provider.id,
modelId: model.id,
config: { provider: 'google', model: 'gemini-fail' },
},
})
const asset = await prisma.asset.create({
data: {
name: 'fail-sample.mp4',
type: 'file',
projectId: project.id,
mediaType: 'video/mp4',
status: 'uploaded',
media: {
proxyType: 'video',
duration: 12.0,
videoTranscodes: [{ key: 'files/fail-sample.mp4', width: 1280, height: 720 }],
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- partial MediaInfo mock for testing
} as any,
},
})
// Mock prompt to fail
vi.spyOn(AgentHarness.prototype, 'prompt').mockRejectedValueOnce(
new Error('LLM rate limit reached'),
)
const task = await prisma.workflowTask.create({
data: {
type: 'ai_metadata_autofill',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: { sessionId: 'mock-session-fail', agentId: agent.id },
},
},
})
await expect(workflowService.executeWait(task, 45000)).rejects.toThrow()
// Verify error comment was created on the asset
const comments = await prisma.assetComment.findMany({
where: { assetId: asset.id },
orderBy: { createdAt: 'asc' },
})
expect(comments.length).toBe(1)
expect(comments[0].message).toContain('Autofill failed:')
expect(comments[0].message).toContain('LLM rate limit reached')
}, 50000)
})
+12 -4
View File
@@ -178,14 +178,12 @@ describe('Workflow E2E - agentEmbeddingMedia', () => {
// 5. Verification
expect(completedTask.status).toBe('completed')
// Verify AssetComment indicates success
// Verify no comments created on successful embedding
const comments = await prisma.assetComment.findMany({
where: { assetId: asset.id },
orderBy: { createdAt: 'asc' },
})
expect(comments.length).toBeGreaterThan(0)
const finalComment = comments[comments.length - 1]
expect(finalComment.message).toBe('Embedding completed successfully.')
expect(comments.length).toBe(0)
// Verify raw embedding is saved in DB
const dbEmbeddings = (await prisma.$queryRaw`
@@ -445,5 +443,15 @@ describe('Workflow E2E - agentEmbeddingMedia', () => {
where: { id: task.id },
})
expect(dbTask?.status).toBe(WorkflowTaskStatus.failed)
// Verify failure comment was posted to the asset
const comments = await prisma.assetComment.findMany({
where: { assetId: asset.id },
orderBy: { createdAt: 'asc' },
})
expect(comments.length).toBe(1)
expect(comments[0].message).toContain(
'Embedding failed: unsupported media type for embeddings: audio/wav',
)
}, 50000)
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/operations",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/tableprinter",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
@@ -1,9 +1,10 @@
// @vitest-environment happy-dom
import React from 'react'
import { cleanup, render, fireEvent } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { CommentInfo } from '@shumai/dtos'
import { useTeamContextStore } from '@/ui/stores/team-context'
import { ChatInput } from './message-input'
function createWrapper() {
@@ -16,6 +17,13 @@ function createWrapper() {
}
describe('ChatInput auto-mention on reply', () => {
beforeEach(() => {
useTeamContextStore.setState({
teamId: 'team-123',
projectTeamMap: { 'proj-1': 'team-123' },
})
})
afterEach(() => {
cleanup()
vi.clearAllMocks()
@@ -195,9 +195,12 @@ describe('FileBrowser', () => {
})
it('renders empty state when there are no folders and files and not loading', () => {
renderComponent({ folders: [], files: [], isLoading: false })
const { container } = renderComponent({ folders: [], files: [], isLoading: false })
expect(screen.getByText(/This folder is empty|此文件夹为空/i)).toBeDefined()
const emptyArea = container.querySelector('.empty-area')
expect(emptyArea).not.toBeNull()
expect(emptyArea?.classList.contains('flex-1')).toBe(true)
})
it('renders loading skeleton and does not render empty state when isLoading is true (card view)', () => {
@@ -57,6 +57,7 @@ import { FileBrowserGridView } from './grid-view'
import { FileBrowserListView } from './list-view'
import { FileBrowserToolbar } from './toolbar'
import { useFileActions } from './use-file-actions'
import { useBatchFileStatus } from '@/ui/hooks/use-batch-file-status'
interface FileBrowserProps {
teamId: string
@@ -249,13 +250,9 @@ export function FileBrowser({
}
}
const [localUploadingFiles, setLocalUploadingFiles] = useState<AssetInfo[]>([])
const displayedFiles = files
const displayedFiles = useMemo(() => {
const existingIds = new Set(files.map((f) => f.id))
const filteredLocal = localUploadingFiles.filter((f) => !existingIds.has(f.id))
return [...filteredLocal, ...files]
}, [files, localUploadingFiles])
useBatchFileStatus({ teamId, items: displayedFiles })
const activePreviewItem = useMemo(() => {
if (selectedIds.size === 0) return null
@@ -530,6 +527,47 @@ export function FileBrowser({
},
})
const throttledInvalidateSearch = useMemo(() => {
let timer: ReturnType<typeof setTimeout> | null = null
let lastRun = 0
const THROTTLE_MS = 2500
const run = () => {
const now = Date.now()
if (now - lastRun >= THROTTLE_MS) {
lastRun = now
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
} else if (!timer) {
timer = setTimeout(
() => {
timer = null
lastRun = Date.now()
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
},
THROTTLE_MS - (now - lastRun),
)
}
}
const cancel = () => {
if (timer) {
clearTimeout(timer)
timer = null
}
}
const reset = () => {
cancel()
lastRun = Date.now()
}
return Object.assign(run, { cancel, reset })
}, [queryClient, teamId, assetId])
const $createUploadTask = client.api.teams[':teamId'].upload.tasks.$post
const { mutate: createUploadTaskMutation } = useMutation<
InferResponseType<typeof $createUploadTask>,
@@ -545,9 +583,11 @@ export function FileBrowser({
onMutate: () => {
return { files: [...filesToUpload] }
},
onError: () => {
toast.error(m.failed_upload_file())
},
onSuccess: async (data, variables, context) => {
const currentFiles = context?.files || []
const isCurrentFolderUpload = variables.json.parentId === assetId
// Register task and files in the store for real-time progress tracking
const filesProgressInfo = currentFiles
@@ -572,77 +612,36 @@ export function FileBrowser({
startTask(data.taskId, taskName, filesProgressInfo)
}
if (isCurrentFolderUpload) {
const newLocalFiles: AssetInfo[] = currentFiles
.filter((f) => {
const path = (f.file.webkitRelativePath || f.file.name).split('/')
return path.length === 1
})
.map((f) => {
const assetInfo = data.createdAssets?.find((a) => a.tempId === f.id)
const urlInfo = (
data.presignedUrls as { id?: string; url?: string; fileId?: string }[] | undefined
)?.find((p) => p.id === f.id)
const fileId = assetInfo?.assetId || urlInfo?.fileId || f.id
const hasUrl = !!urlInfo && !!urlInfo.url
return {
id: fileId,
name: f.file.name,
sizeByte: f.file.size,
fileCount: 0,
type: 'file',
status: hasUrl ? 'uploading' : 'error',
proxyType:
f.file.name.toLowerCase().endsWith('.wma') && f.file.type?.startsWith('video/')
? 'audio'
: f.file.type?.startsWith('image/')
? 'image'
: f.file.type?.startsWith('video/')
? 'video'
: f.file.type?.startsWith('audio/')
? 'audio'
: f.file.name.endsWith('.pdf') ||
f.file.name.endsWith('.txt') ||
f.file.name.endsWith('.csv') ||
f.file.name.endsWith('.md') ||
f.file.name.endsWith('.markdown')
? 'pdf'
: null,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
}
})
setLocalUploadingFiles((prev) => [...newLocalFiles, ...prev])
// Invalidate queries immediately so newly created assets (and folders) appear
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'upload', 'tasks'],
})
throttledInvalidateSearch.reset()
try {
await uploadFilesWithUppy({
files: currentFiles,
taskId: data.taskId!,
teamId: teamId!,
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
createdAssets: data.createdAssets,
presignedUrls: data.presignedUrls,
onFileFinished: () => {
throttledInvalidateSearch()
},
})
} finally {
throttledInvalidateSearch.cancel()
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'upload', 'tasks'],
})
}
// Invalidate queries immediately so any newly created folders appear
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'upload', 'tasks'],
})
await uploadFilesWithUppy({
files: currentFiles,
taskId: data.taskId!,
teamId: teamId!,
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
createdAssets: data.createdAssets,
presignedUrls: data.presignedUrls,
onFileFinished: async (fileId) => {
await queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
setLocalUploadingFiles((prev) => prev.filter((f) => f.id !== fileId))
},
})
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'upload', 'tasks'],
})
},
})
@@ -1007,6 +1006,27 @@ export function FileBrowser({
>
{isLoading && folders.length === 0 && displayedFiles.length === 0 ? (
<FileBrowserContentSkeleton displayStyle={displayStyle} />
) : !isLoading &&
folders.length === 0 &&
displayedFiles.length === 0 &&
!isFetchingNextFoldersPage &&
!isFetchingNextFilesPage ? (
<div
className="empty-area flex-1 flex items-center justify-center text-sm text-muted-foreground"
onClick={handleEmptyAreaClick}
>
{isRecents ? (
<div className="flex flex-col items-center gap-2 p-8 text-center animate-in fade-in duration-300">
<FolderClock className="h-10 w-10 text-muted-foreground/50 mb-1" />
<p className="font-medium text-foreground">{m.no_recent_files()}</p>
<p className="text-xs text-muted-foreground">
{m.no_recent_files_description()}
</p>
</div>
) : (
<span>{m.this_folder_is_empty()}</span>
)}
</div>
) : displayStyle === 'list' ? (
<FileBrowserListView
folders={folders}
@@ -1079,26 +1099,6 @@ export function FileBrowser({
<p>{m.loading_more()}</p>
</div>
)}
{!isLoading &&
folders.length === 0 &&
displayedFiles.length === 0 &&
!isFetchingNextFoldersPage &&
!isFetchingNextFilesPage && (
<div className="empty-area flex h-full items-center justify-center text-sm text-muted-foreground">
{isRecents ? (
<div className="flex flex-col items-center gap-2 p-8 text-center animate-in fade-in duration-300">
<FolderClock className="h-10 w-10 text-muted-foreground/50 mb-1" />
<p className="font-medium text-foreground">{m.no_recent_files()}</p>
<p className="text-xs text-muted-foreground">
{m.no_recent_files_description()}
</p>
</div>
) : (
<span>{m.this_folder_is_empty()}</span>
)}
</div>
)}
</div>
{selectedCount > 0 && (
@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { FileCard } from './file-card'
import type { AssetInfo } from '@shumai/dtos'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { useUploadStore } from '@/ui/stores/upload'
vi.mock('@dnd-kit/react', () => ({
useDraggable: () => ({
@@ -328,7 +329,7 @@ describe('FileCard', () => {
expect(screen.queryByText('00:10')).toBeNull()
})
it('shows "Uploading..." in place of the creator while uploading', () => {
it('shows "[User] is uploading..." and indeterminate circle for remote viewer', () => {
const uploadingItem: AssetInfo = {
...fileItem,
status: 'uploading',
@@ -337,7 +338,115 @@ describe('FileCard', () => {
renderComponent({ item: uploadingItem })
expect(screen.getByText(/Alice.*(is uploading|正在上传)/i)).toBeTruthy()
expect(screen.getByTestId('file-card-remote-uploading-circle')).toBeTruthy()
})
it('shows "Uploading..." during local warm-up and progress circle for local uploader', () => {
useUploadStore.setState({
fileProgress: {
'file-1': {
fileId: 'file-1',
name: 'photo.jpg',
loaded: 100,
total: 1000,
status: 'uploading',
},
},
})
const uploadingItem: AssetInfo = {
...fileItem,
id: 'file-1',
status: 'uploading',
creator: { id: 'u1', name: 'Alice' },
} as AssetInfo
renderComponent({ item: uploadingItem })
expect(screen.getByText(/Uploading|上传中/i)).toBeTruthy()
expect(screen.queryByTestId('file-card-remote-uploading-circle')).toBeNull()
})
it('shows dynamic ETA and speed for local uploader when available', () => {
useUploadStore.setState({
fileProgress: {
'file-1': {
fileId: 'file-1',
name: 'photo.jpg',
loaded: 500,
total: 1000,
status: 'uploading',
speed: 400 * 1024,
eta: 480, // 8 minutes
},
},
})
const uploadingItem: AssetInfo = {
...fileItem,
id: 'file-1',
status: 'uploading',
creator: { id: 'u1', name: 'Alice' },
} as AssetInfo
renderComponent({ item: uploadingItem })
expect(screen.getByText(/8m.*(left|剩余).*409\.6 KB\/s/i)).toBeTruthy()
})
it('shows failed state when fileProgress marks upload as failed', () => {
useUploadStore.setState({
fileProgress: {
'file-1': {
fileId: 'file-1',
name: 'photo.jpg',
loaded: 100,
total: 1000,
status: 'failed',
},
},
})
const uploadingItem: AssetInfo = {
...fileItem,
id: 'file-1',
status: 'uploading',
creator: { id: 'u1', name: 'Alice' },
} as AssetInfo
renderComponent({ item: uploadingItem })
expect(screen.getAllByText(/Failed to upload|上传失败/i).length).toBeGreaterThan(0)
expect(screen.queryByTestId('file-card-remote-uploading-circle')).toBeNull()
})
it('transitions from uploading to preparing circle when upload store marks file completed', () => {
useUploadStore.setState({
fileProgress: {
'file-uploading': {
fileId: 'file-uploading',
name: 'clip.mp4',
loaded: 1000,
total: 1000,
status: 'completed',
},
},
})
const uploadingItem: AssetInfo = {
...fileItem,
id: 'file-uploading',
status: 'uploading',
creator: { id: 'u1', name: 'Alice' },
} as AssetInfo
renderComponent({ item: uploadingItem })
expect(screen.queryByTestId('file-card-preview-media')).toBeNull()
const circle = screen.getByTestId('file-card-preparing-circle')
expect(circle.tagName.toLowerCase()).toBe('svg')
expect(screen.getByText(/Preparing|准备中/i)).toBeTruthy()
expect(screen.queryByText(/Alice/i)).toBeNull()
})
@@ -21,7 +21,8 @@ import {
TooltipTrigger,
} from '@/ui/components/ui/tooltip'
import { ProgressCircle } from '@/ui/components/ui/progress-circle'
import { formatTimeAgo, getTrashDaysLeft } from '@/ui/lib/time'
import { formatRemainingTimeSeconds, formatTimeAgo, getTrashDaysLeft } from '@/ui/lib/time'
import { formatSize } from '@/ui/lib/format'
import { selectFileNameWithoutExtension } from '@/ui/lib/rename-utils'
import { cn } from '@/ui/lib/utils'
import { useUploadStore } from '@/ui/stores/upload'
@@ -119,11 +120,6 @@ export function FileCard({
: 0
: 0
const shouldPoll =
(item.type === 'file' || item.type === 'version_stack') &&
!!teamId &&
(item.status === 'uploading' || item.status === 'uploaded' || item.status === 'processing')
const { data: polledItem } = useQuery({
queryKey: ['file', teamId, item.id],
queryFn: async () => {
@@ -133,13 +129,7 @@ export function FileCard({
if (!res.ok) throw new Error('failed to fetch file')
return (await res.json()) as unknown as AssetInfo
},
enabled: shouldPoll,
refetchInterval: (query: unknown) => {
const data = (query as { state: { data: { status?: string } } }).state.data
return data?.status === 'processed' || data?.status === 'failed' || data?.status === 'error'
? false
: 1000
},
enabled: false,
})
const displayItem = useMemo(() => {
@@ -277,16 +267,38 @@ export function FileCard({
// since the poster/sprite are generated and persisted ahead of the proxy transcodes.
const hasPreview = Boolean(displayItem.preview?.thumbnailUrl || displayItem.preview?.spriteUrl)
const isFailed = displayItem.status === 'failed'
const isProcessing = displayItem.status === 'processing' || displayItem.status === 'uploaded'
const isLocalUploader = Boolean(fileUploadState)
const isUploadFailed = fileUploadState?.status === 'failed'
const isUploadDone = fileUploadState?.status === 'completed' || uploadPercent >= 100
const isUploading = displayItem.status === 'uploading' && !isUploadDone && !isUploadFailed
const isProcessing =
displayItem.status === 'processing' ||
displayItem.status === 'uploaded' ||
(displayItem.status === 'uploading' && isUploadDone)
// While uploading/transcoding, the creator row is replaced by a short status label so the real
// While uploading/transcoding/error, the creator row is replaced by a status label so the real
// date/author only appears once the asset is ready or failed.
const statusText =
displayItem.status === 'uploading'
? m.uploading()
: displayItem.status === 'uploaded' || displayItem.status === 'processing'
? m.preparing()
: null
let statusText: string | null = null
if (isUploadFailed || displayItem.status === 'error') {
statusText = m.failed_to_upload()
} else if (isUploading) {
if (isLocalUploader) {
if (fileUploadState?.speed && fileUploadState?.eta != null) {
statusText = m.upload_eta_speed({
eta: formatRemainingTimeSeconds(fileUploadState.eta),
speed: `${formatSize(fileUploadState.speed)}/s`,
})
} else {
statusText = m.uploading()
}
} else {
statusText = m.user_is_uploading({
user: displayItem.creator?.name || m.someone(),
})
}
} else if (isProcessing) {
statusText = m.preparing()
}
const failedErrorMessage = displayItem.media?.error || m.processing_failed()
@@ -393,9 +405,26 @@ export function FileCard({
</div>
<div className="relative aspect-square overflow-hidden bg-muted/30">
{displayItem.status === 'uploading' ? (
{isUploading ? (
<div className="flex h-full w-full items-center justify-center bg-background/50">
<ProgressCircle progress={uploadPercent} className="w-16 h-16 z-10" />
{isLocalUploader ? (
<ProgressCircle progress={uploadPercent} className="w-16 h-16 z-10" />
) : (
<svg
viewBox="0 0 50 50"
data-testid="file-card-remote-uploading-circle"
className="h-16 w-16 opacity-60 animate-preparing-breathe z-10"
>
<circle
cx="25"
cy="25"
r="20"
className="stroke-muted-foreground"
strokeWidth="4"
fill="transparent"
/>
</svg>
)}
</div>
) : hasPreview ? (
<>
@@ -415,7 +444,7 @@ export function FileCard({
</div>
{previewBadges}
</>
) : displayItem.status === 'error' ? (
) : displayItem.status === 'error' || isUploadFailed ? (
<div className="flex h-full w-full items-center justify-center bg-background/50">
<span className="z-10 font-medium px-2 text-center text-sm text-destructive font-semibold">
{m.failed_to_upload()}
@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { FileListItem } from './file-list-item'
import type { AssetInfo } from '@shumai/dtos'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { useUploadStore } from '@/ui/stores/upload'
vi.mock('@dnd-kit/react', () => ({
useDraggable: () => ({
@@ -35,6 +36,7 @@ describe('FileListItem', () => {
queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
useUploadStore.setState({ fileProgress: {} })
vi.useFakeTimers()
})
@@ -183,4 +185,69 @@ describe('FileListItem', () => {
expect(screen.getByDisplayValue('List text value')).toBeTruthy()
expect(onSelect).not.toHaveBeenCalled()
})
it('renders dynamic ETA and speed for local uploader', () => {
useUploadStore.setState({
fileProgress: {
'file-123': {
fileId: 'file-123',
name: 'very_long_file_name_for_testing_purposes.mov',
loaded: 500,
total: 1000,
status: 'uploading',
speed: 400 * 1024,
eta: 480,
},
},
})
const uploadingItem: AssetInfo = {
...mockItem,
status: 'uploading',
} as AssetInfo
renderComponent({ item: uploadingItem })
const statusBadge = screen.getByTestId('file-list-item-uploading-status')
expect(statusBadge.textContent).toMatch(/8m.*(left|剩余).*409\.6 KB\/s/i)
expect(screen.queryByTestId('file-list-item-remote-uploading-circle')).toBeNull()
})
it('renders "Uploading..." fallback during local warm-up', () => {
useUploadStore.setState({
fileProgress: {
'file-123': {
fileId: 'file-123',
name: 'very_long_file_name_for_testing_purposes.mov',
loaded: 100,
total: 1000,
status: 'uploading',
},
},
})
const uploadingItem: AssetInfo = {
...mockItem,
status: 'uploading',
} as AssetInfo
renderComponent({ item: uploadingItem })
const statusBadge = screen.getByTestId('file-list-item-uploading-status')
expect(statusBadge.textContent).toMatch(/Uploading|上传中/i)
})
it('renders "[User] is uploading..." and indeterminate circle for remote viewer', () => {
const uploadingItem: AssetInfo = {
...mockItem,
status: 'uploading',
creator: { id: 'u1', name: 'Alice' },
} as AssetInfo
renderComponent({ item: uploadingItem })
const statusBadge = screen.getByTestId('file-list-item-uploading-status')
expect(statusBadge.textContent).toMatch(/Alice.*(is uploading|正在上传)/i)
expect(screen.getByTestId('file-list-item-remote-uploading-circle')).toBeTruthy()
})
})
@@ -8,6 +8,7 @@ import { AlertCircle, File, Folder, MoreVertical, AudioLines } from 'lucide-reac
import React, { useEffect, useMemo, useRef, useState } from 'react'
import { cn } from '@/ui/lib/utils'
import { formatSize } from '@/ui/lib/format'
import { formatRemainingTimeSeconds } from '@/ui/lib/time'
import { selectFileNameWithoutExtension } from '@/ui/lib/rename-utils'
import type { DragState } from '../dnd-types'
import FieldRenderer from '../field-renderer'
@@ -74,11 +75,6 @@ export function FileListItem({
: 0
: 0
const shouldPoll =
(item.type === 'file' || item.type === 'version_stack') &&
!!teamId &&
(item.status === 'uploading' || item.status === 'uploaded' || item.status === 'processing')
const { data: polledItem } = useQuery({
queryKey: ['file', teamId, item.id],
queryFn: async () => {
@@ -88,13 +84,7 @@ export function FileListItem({
if (!res.ok) throw new Error('failed to fetch file')
return (await res.json()) as unknown as AssetInfo
},
enabled: shouldPoll,
refetchInterval: (query: unknown) => {
const data = (query as { state: { data: { status?: string } } }).state.data
return data?.status === 'processed' || data?.status === 'failed' || data?.status === 'error'
? false
: 1000
},
enabled: false,
})
const displayItem = useMemo(() => {
@@ -111,6 +101,29 @@ export function FileListItem({
return polledItem || item
}, [item, polledItem])
const isLocalUploader = Boolean(fileUploadState)
const isUploadFailed = fileUploadState?.status === 'failed'
const isUploadDone = fileUploadState?.status === 'completed' || uploadPercent >= 100
const isUploading = displayItem.status === 'uploading' && !isUploadDone && !isUploadFailed
let uploadingText: string | null = null
if (isUploading) {
if (isLocalUploader) {
if (fileUploadState?.speed && fileUploadState?.eta != null) {
uploadingText = m.upload_eta_speed({
eta: formatRemainingTimeSeconds(fileUploadState.eta),
speed: `${formatSize(fileUploadState.speed)}/s`,
})
} else {
uploadingText = m.uploading()
}
} else {
uploadingText = m.user_is_uploading({
user: displayItem.creator?.name || m.someone(),
})
}
}
// dnd-kit hooks
const { ref: setDraggableRef, isDragging: isDraggableDragging } = useDraggable({
id: `browser:${displayItem.id!}`,
@@ -220,8 +233,25 @@ export function FileListItem({
className="h-4 w-4 shrink-0 data-[state=checked]:bg-primary data-[state=checked]:border-primary"
/>
<div className="w-8 h-8 shrink-0 flex items-center justify-center bg-muted rounded overflow-hidden">
{displayItem.status === 'uploading' ? (
<ProgressCircle progress={uploadPercent} className="w-7 h-7" />
{isUploading ? (
isLocalUploader ? (
<ProgressCircle progress={uploadPercent} className="w-7 h-7" />
) : (
<svg
viewBox="0 0 50 50"
data-testid="file-list-item-remote-uploading-circle"
className="w-7 h-7 opacity-60 animate-preparing-breathe"
>
<circle
cx="25"
cy="25"
r="20"
className="stroke-muted-foreground"
strokeWidth="4"
fill="transparent"
/>
</svg>
)
) : displayItem.status === 'failed' && !displayItem.preview?.thumbnailUrl ? (
<AlertCircle className="h-4 w-4 text-destructive" />
) : displayItem.preview?.thumbnailUrl ? (
@@ -247,7 +277,15 @@ export function FileListItem({
disabled={!isEditing}
className="h-auto p-0 text-sm font-medium text-foreground truncate min-w-0 flex-1"
/>
{displayItem.status === 'error' && (
{uploadingText && (
<span
data-testid="file-list-item-uploading-status"
className="text-xs text-muted-foreground font-medium shrink-0"
>
{uploadingText}
</span>
)}
{(displayItem.status === 'error' || isUploadFailed) && (
<span className="text-xs text-destructive font-semibold shrink-0">
{m.failed_to_upload_parenthesized()}
</span>
@@ -358,7 +358,7 @@ describe('MobileFileBrowser', () => {
window.XMLHttpRequest = originalXhr
})
it('fails upload and confirms with error on network failure', async () => {
it('keeps upload active and does not fail task on network failure', async () => {
const originalXhr = window.XMLHttpRequest
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
@@ -409,17 +409,13 @@ describe('MobileFileBrowser', () => {
fireEvent.change(fileInput, { target: { files: [testFile] } })
await vi.waitFor(() => {
expect(patchMock).toHaveBeenCalledWith(
expect.objectContaining({
param: { teamId: 'team-1', taskId: 'task-3' },
json: expect.objectContaining({
fileId: 'server-file-3',
errorMessage: expect.any(String),
}),
}),
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((client.api.teams[':teamId'].upload.tasks as any)['$post']).toHaveBeenCalled()
})
// Network failures are retried automatically with backoff and do not fail task on server
expect(patchMock).not.toHaveBeenCalled()
window.XMLHttpRequest = originalXhr
})
@@ -20,7 +20,7 @@ import {
Plus,
Upload,
} from 'lucide-react'
import React, { useRef, useState } from 'react'
import React, { useMemo, useRef, useState } from 'react'
import { useInView } from 'react-intersection-observer'
import { toast } from 'sonner'
import { ulid } from 'ulid'
@@ -54,6 +54,7 @@ import { FileCard } from './file-card'
import { FolderCard } from './folder-card'
import { useFileActions } from './use-file-actions'
import { MobileFileBrowserContentSkeleton } from '../loading-skeletons'
import { useBatchFileStatus } from '@/ui/hooks/use-batch-file-status'
export interface MobileFileBrowserProps {
teamId: string
@@ -125,6 +126,7 @@ export function MobileFileBrowser({
allowDownload = true,
}: MobileFileBrowserProps) {
const queryClient = useQueryClient()
useBatchFileStatus({ teamId, items: files })
const { canEdit, canAdmin } = usePermissions(projectId)
const fileInputRef = useRef<HTMLInputElement>(null)
const folderInputRef = useRef<HTMLInputElement>(null)
@@ -200,6 +202,47 @@ export function MobileFileBrowser({
// Upload actions
const startTask = useUploadStore((state) => state.startTask)
const throttledInvalidateSearch = useMemo(() => {
let timer: ReturnType<typeof setTimeout> | null = null
let lastRun = 0
const THROTTLE_MS = 2500
const run = () => {
const now = Date.now()
if (now - lastRun >= THROTTLE_MS) {
lastRun = now
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
} else if (!timer) {
timer = setTimeout(
() => {
timer = null
lastRun = Date.now()
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
},
THROTTLE_MS - (now - lastRun),
)
}
}
const cancel = () => {
if (timer) {
clearTimeout(timer)
timer = null
}
}
const reset = () => {
cancel()
lastRun = Date.now()
}
return Object.assign(run, { cancel, reset })
}, [queryClient, teamId, assetId])
const $createUploadTask = client.api.teams[':teamId'].upload.tasks.$post
const { mutate: createUploadTaskMutation } = useMutation<
InferResponseType<typeof $createUploadTask>,
@@ -215,7 +258,10 @@ export function MobileFileBrowser({
onMutate: () => {
return { files: [...pendingFilesToUpload] }
},
onSuccess: async (data, _variables, context) => {
onError: () => {
toast.error(m.failed_upload_file())
},
onSuccess: async (data, variables, context) => {
const currentFiles = context?.files || []
const filesProgressInfo = currentFiles
.map((f) => {
@@ -239,20 +285,25 @@ export function MobileFileBrowser({
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
throttledInvalidateSearch.reset()
await uploadFilesWithUppy({
files: currentFiles,
taskId: data.taskId!,
teamId: teamId!,
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
createdAssets: data.createdAssets,
presignedUrls: data.presignedUrls,
onFileFinished: async () => {
await queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
},
})
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
try {
await uploadFilesWithUppy({
files: currentFiles,
taskId: data.taskId!,
teamId: teamId!,
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
createdAssets: data.createdAssets,
presignedUrls: data.presignedUrls,
onFileFinished: () => {
throttledInvalidateSearch()
},
})
} finally {
throttledInvalidateSearch.cancel()
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
}
},
})
@@ -0,0 +1,172 @@
// @vitest-environment happy-dom
import { cleanup, render, screen, fireEvent, waitFor } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { UploadTasks } from './upload-tasks'
import { useUploadStore } from '@/ui/stores/upload'
import * as uploaderLib from '@/ui/lib/uploader'
import { toast } from 'sonner'
const mockGetTasks = vi.fn()
const mockCancelTask = vi.fn()
vi.mock('@/ui/api/client', () => ({
client: {
api: {
teams: {
':teamId': {
upload: {
tasks: Object.assign(
{
$get: () => mockGetTasks(),
},
{
':taskId': {
cancel: {
$post: (args: unknown) => mockCancelTask(args),
},
},
},
),
},
},
},
},
},
}))
vi.mock('@/ui/hooks/use-team-id', () => ({
useTeamId: () => 'team-1',
}))
vi.mock('sonner', () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
},
}))
describe('UploadTasks', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.clearAllMocks()
queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
useUploadStore.setState({
uploading: 0,
tasks: {},
fileProgress: {},
})
})
afterEach(() => {
cleanup()
})
it('renders completed and failed tasks without a cancel button', async () => {
mockGetTasks.mockResolvedValue({
ok: true,
json: async () => ({
data: [
{
id: 'task-done',
name: 'Completed Task',
total: 2,
uploaded: 2,
status: 'completed',
createdAt: new Date().toISOString(),
},
{
id: 'task-failed',
name: 'Failed Task',
total: 2,
uploaded: 1,
status: 'failed',
createdAt: new Date().toISOString(),
},
],
pageInfo: {},
}),
})
render(
<QueryClientProvider client={queryClient}>
<UploadTasks />
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getByText('Completed Task')).toBeDefined()
expect(screen.getByText('Failed Task')).toBeDefined()
})
// No cancel button should be rendered for completed or failed tasks
expect(screen.queryByRole('button', { name: /cancel/i })).toBeNull()
})
it('renders cancel button for running tasks and executes cancel flow on confirmation', async () => {
const cancelActiveSpy = vi.spyOn(uploaderLib, 'cancelActiveUploadTask')
mockGetTasks.mockResolvedValue({
ok: true,
json: async () => ({
data: [
{
id: 'task-running',
name: 'Running Task',
total: 3,
uploaded: 1,
status: 'uploading',
createdAt: new Date().toISOString(),
},
],
pageInfo: {},
}),
})
mockCancelTask.mockResolvedValue({
ok: true,
json: async () => ({ success: true }),
})
render(
<QueryClientProvider client={queryClient}>
<UploadTasks />
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getByText('Running Task')).toBeDefined()
})
// Cancel button should be present
const cancelButton = screen.getByRole('button', { name: /cancel/i })
expect(cancelButton).toBeDefined()
// Click Cancel to open confirmation dialog
fireEvent.click(cancelButton)
await waitFor(() => {
expect(screen.getByText(/cancel upload task\?/i)).toBeDefined()
})
// Click confirm in the dialog
const confirmButtons = screen.getAllByRole('button', { name: /cancel upload/i })
fireEvent.click(confirmButtons[confirmButtons.length - 1])
await waitFor(() => {
expect(cancelActiveSpy).toHaveBeenCalledWith('task-running')
expect(mockCancelTask).toHaveBeenCalledWith(
expect.objectContaining({
param: { teamId: 'team-1', taskId: 'task-running' },
}),
)
expect(toast.success).toHaveBeenCalled()
})
})
})
+119 -17
View File
@@ -1,13 +1,26 @@
import { client } from '@/ui/api/client'
import type { TaskInfo } from '@shumai/dtos'
import { useTeamId } from '@/ui/hooks/use-team-id'
import { useInfiniteQuery } from '@tanstack/react-query'
import { useInfiniteQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { useUploadStore } from '@/ui/stores/upload'
import { Loader2, CheckCircle2, Clock, UploadCloud } from 'lucide-react'
import { useEffect, useMemo } from 'react'
import { cancelActiveUploadTask } from '@/ui/lib/uploader'
import { Loader2, CheckCircle2, Clock, UploadCloud, AlertCircle } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { useInView } from 'react-intersection-observer'
import { format, parseISO, isToday, isYesterday } from 'date-fns'
import { Progress } from '@/ui/components/ui/progress'
import { Button } from '@/ui/components/ui/button'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/ui/components/ui/alert-dialog'
import { toast } from 'sonner'
import { formatTimeAgo } from '@/ui/lib/time'
import { m } from '@/ui/paraglide/messages.js'
@@ -33,20 +46,29 @@ function formatBytes(bytes: number): string {
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
}
function UploadTaskItem({ task }: { task: TaskInfo }) {
function UploadTaskItem({ task, teamId }: { task: TaskInfo; teamId?: string }) {
const [isCancelConfirmOpen, setIsCancelConfirmOpen] = useState(false)
const clientProgress = useUploadStore((state) => state.tasks[task.id])
const cancelTaskInStore = useUploadStore((state) => state.cancelTask)
const queryClient = useQueryClient()
const isCompleted = clientProgress
? clientProgress.loaded === clientProgress.total && task.uploaded === task.total
: task.uploaded === task.total
const percent = clientProgress
? clientProgress.total > 0
? (clientProgress.loaded / clientProgress.total) * 100
: 0
: task.total > 0
? (task.uploaded / task.total) * 100
: 0
const isFailed = task.status === 'failed'
const isDone = task.status === 'completed' || isCompleted
const isRunning = !isDone && !isFailed
const percent = isDone
? 100
: clientProgress
? clientProgress.total > 0
? (clientProgress.loaded / clientProgress.total) * 100
: 0
: task.total > 0
? (task.uploaded / task.total) * 100
: 0
let formattedTime: string
try {
@@ -55,6 +77,37 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
formattedTime = m.unknown()
}
const { mutate: cancelMutation, isPending: isCanceling } = useMutation({
mutationFn: async () => {
cancelActiveUploadTask(task.id)
cancelTaskInStore(task.id)
if (teamId) {
const res = await client.api.teams[':teamId'].upload.tasks[':taskId'].cancel.$post({
param: { teamId, taskId: task.id },
})
if (!res.ok) {
throw new Error('Failed to cancel upload task')
}
return await res.json()
}
},
onSuccess: () => {
setIsCancelConfirmOpen(false)
toast.success(m.upload_task_canceled())
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'upload', 'tasks'],
})
queryClient.invalidateQueries({
queryKey: ['search', teamId],
})
},
onError: (err) => {
console.error('Failed to cancel upload task:', err)
toast.error(m.failed_to_cancel_upload_task())
},
})
return (
<div className="group relative flex flex-col p-4 rounded-xl border border-sidebar-border bg-sidebar-accent/30 text-sidebar-foreground shadow-xs transition-all duration-200 hover:shadow-md hover:border-sidebar-foreground/20 hover:bg-sidebar-accent/50">
{/* Top row: Name & Status Badge */}
@@ -68,11 +121,16 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
</span>
</div>
<div className="flex-shrink-0">
{isCompleted ? (
{isDone ? (
<span className="inline-flex items-center gap-1 text-xs font-semibold text-muted-foreground bg-muted px-2 py-0.5 rounded-full transition-colors">
<CheckCircle2 className="w-3.5 h-3.5" />
{m.done()}
</span>
) : isFailed ? (
<span className="inline-flex items-center gap-1 text-xs font-semibold text-destructive bg-destructive/10 px-2 py-0.5 rounded-full">
<AlertCircle className="w-3.5 h-3.5" />
{m.status_failed()}
</span>
) : (
<span className="inline-flex items-center gap-1 text-xs font-semibold text-sidebar-primary bg-sidebar-primary/10 px-2 py-0.5 rounded-full animate-pulse">
<Loader2 className="w-3.5 h-3.5 animate-spin" />
@@ -88,7 +146,7 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
<span>{formattedTime}</span>
</div>
{/* Bottom row: Progress Bar and Fraction */}
{/* Bottom row: Progress Bar, Fraction & Cancel Button */}
<div className="space-y-1.5 mt-auto">
<div className="flex items-center justify-between text-xs font-semibold">
<span className="text-muted-foreground/80">
@@ -96,12 +154,56 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
? `${formatBytes(clientProgress.loaded)} / ${formatBytes(clientProgress.total)} (${m.files_progress({ uploaded: task.uploaded, total: task.total })})`
: m.files_progress({ uploaded: task.uploaded, total: task.total })}
</span>
{!isCompleted && (
<span className="text-sidebar-primary font-mono font-bold">{Math.round(percent)}%</span>
)}
<div className="flex items-center gap-2">
{!isDone && !isFailed && (
<span className="text-sidebar-primary font-mono font-bold">
{Math.round(percent)}%
</span>
)}
{isRunning && (
<Button
variant="ghost"
size="sm"
className="h-6 px-2 text-xs font-medium text-muted-foreground hover:text-destructive hover:bg-destructive/10"
disabled={isCanceling}
onClick={(e) => {
e.stopPropagation()
setIsCancelConfirmOpen(true)
}}
>
{isCanceling ? <Loader2 className="w-3 h-3 animate-spin mr-1" /> : null}
{m.cancel()}
</Button>
)}
</div>
</div>
<Progress value={percent} className="h-1.5 w-full bg-muted" />
</div>
<AlertDialog open={isCancelConfirmOpen} onOpenChange={setIsCancelConfirmOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{m.cancel_upload_task_confirm_title()}</AlertDialogTitle>
<AlertDialogDescription>
{m.cancel_upload_task_confirm_description()}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={isCanceling}>{m.cancel()}</AlertDialogCancel>
<AlertDialogAction
disabled={isCanceling}
onClick={(e) => {
e.preventDefault()
cancelMutation()
}}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{isCanceling ? <Loader2 className="w-4 h-4 animate-spin mr-1" /> : null}
{m.cancel_upload_task()}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)
}
@@ -218,7 +320,7 @@ export function UploadTasks() {
{/* Tasks in Day */}
<div className="mt-3 space-y-3">
{group.tasks.map((task) => (
<UploadTaskItem key={task.id} task={task} />
<UploadTaskItem key={task.id} task={task} teamId={teamId} />
))}
</div>
</div>
+91 -2
View File
@@ -1,10 +1,13 @@
import { type ReactElement, useCallback, useRef, useState } from 'react'
import { type ReactElement, useCallback, useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom/client'
import type { MediaController } from '@/ui/components/viewers/types'
import VideoPlayer from '@/ui/components/viewers/video/video-viewer'
import ModelViewer from '@/ui/components/viewers/model/model-viewer'
import { calculateFrameCenterTime } from '@/ui/components/viewers/video/utils'
import { useUiStore } from '@/ui/stores/ui'
import { uploadFilesWithUppy } from '@/ui/lib/uploader'
import { useUploadStore } from '@/ui/stores/upload'
import { Toaster } from 'sonner'
import {
SAMPLE_FRAME_RATE,
sampleVideoAsset,
@@ -185,9 +188,95 @@ function Harness(): ReactElement {
)
}
function UploaderHarness(): ReactElement {
const [status, setStatus] = useState<'idle' | 'uploading' | 'completed' | 'failed'>('idle')
const [errorMessage, setErrorMessage] = useState<string | null>(null)
const taskState = useUploadStore((state) => state.tasks['e2e-task-1'])
useEffect(() => {
;(window as unknown as Record<string, number>)['__SHUMAI_UPLOAD_BASE_DELAY__'] = 100
}, [])
const startUpload = async (file: File) => {
setStatus('uploading')
setErrorMessage(null)
const taskId = 'e2e-task-1'
const teamId = 'e2e-team-1'
const fileId = 'e2e-file-1'
useUploadStore
.getState()
.startTask(taskId, 'e2e-task', [{ fileId, name: file.name, size: file.size }])
try {
await uploadFilesWithUppy({
files: [{ id: 'temp-1', file }],
taskId,
teamId,
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-1', assetId: fileId, key: `files/${file.name}` }],
})
setStatus('completed')
} catch (err) {
setStatus('failed')
setErrorMessage(err instanceof Error ? err.message : String(err))
}
}
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]
if (file) {
void startUpload(file)
}
}
const handleSimulatedStart = () => {
const mockFile = new File(['mock content for test'], 'test-file.mp4', {
type: 'video/mp4',
})
void startUpload(mockFile)
}
const derivedStatus = taskState?.files['e2e-file-1']?.status ?? status
return (
<div style={{ padding: 24, fontFamily: 'sans-serif' }}>
<Toaster position="top-right" />
<h2>Uploader Resilience E2E Harness</h2>
<div style={{ marginBottom: 16 }}>
<input data-testid="uploader-file-input" type="file" onChange={handleFileChange} />
<button
data-testid="start-upload-btn"
onClick={handleSimulatedStart}
style={{ marginLeft: 8 }}
>
Start Simulated Upload
</button>
</div>
<div style={{ display: 'flex', gap: 16, alignItems: 'center' }}>
<div>
Status: <span data-testid="upload-status">{derivedStatus}</span>
</div>
<div>
Progress:{' '}
<span data-testid="upload-progress">{taskState?.files['e2e-file-1']?.loaded ?? 0}</span>
</div>
{errorMessage && <div data-testid="upload-error">{errorMessage}</div>}
</div>
</div>
)
}
const rootElement = document.getElementById('root')
if (!rootElement) {
throw new Error('Harness root element not found')
}
ReactDOM.createRoot(rootElement).render(<Harness />)
const currentView =
typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('view') : null
ReactDOM.createRoot(rootElement).render(
currentView === 'uploader' ? <UploaderHarness /> : <Harness />,
)
@@ -0,0 +1,193 @@
import { expect, test } from '@playwright/test'
test.describe('Uploader Resilience E2E Tests', () => {
test.beforeEach(async ({ page }) => {
// Default route mocks for the isolated harness
await page.route('**/api/teams/**/upload/tasks/**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ ok: true }),
}),
)
})
test('keeps upload active on network drop and automatically resumes on reconnection', async ({
page,
}) => {
let releaseSign: () => void = () => {}
const signGate = new Promise<void>((resolve) => {
releaseSign = resolve
})
let signCalls = 0
await page.route('**/api/teams/**/upload/sign', async (route) => {
signCalls++
if (signCalls === 1) {
// Wait until offline event is dispatched before releasing first sign call
await signGate
}
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ url: 'http://localhost:5199/mock-upload' }),
})
})
await page.route('**/mock-upload', (route) =>
route.fulfill({
status: 200,
headers: { ['ETag']: '"test-etag"' },
}),
)
await page.goto('/?view=uploader')
await expect(page.getByTestId('upload-status')).toHaveText('idle')
// Start upload
await page.getByTestId('start-upload-btn').click()
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
// Simulate cutting the connection
await page.context().setOffline(true)
releaseSign()
// Status remains 'uploading' during offline state and no error toast appears
await page.waitForTimeout(500)
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
// Reconnect network
await page.context().setOffline(false)
// Upload automatically resumes and completes
await expect(page.getByTestId('upload-status')).toHaveText('completed', { timeout: 15_000 })
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
})
test('retries with backoff on in-flight socket drop and completes when network recovers', async ({
page,
}) => {
let signCalls = 0
await page.route('**/api/teams/**/upload/sign', (route) => {
signCalls++
if (signCalls <= 2) {
// In-flight connection reset / socket drop
route.abort('connectionreset')
} else {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ url: 'http://localhost:5199/mock-upload' }),
})
}
})
await page.route('**/mock-upload', (route) =>
route.fulfill({
status: 200,
headers: { ['ETag']: '"test-etag"' },
}),
)
await page.goto('/?view=uploader')
await page.getByTestId('start-upload-btn').click()
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
// After backoff retries, succeeds on attempt 3
await expect(page.getByTestId('upload-status')).toHaveText('completed', { timeout: 15_000 })
expect(signCalls).toBeGreaterThanOrEqual(3)
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
})
test('retries continuously on 502 Bad Gateway and recovers once server returns 200', async ({
page,
}) => {
let signAttempts = 0
await page.route('**/api/teams/**/upload/sign', (route) => {
signAttempts++
if (signAttempts <= 2) {
// Return 502 Bad Gateway for the first 2 attempts
route.fulfill({
status: 502,
contentType: 'text/html',
body: '<html><body>502 Bad Gateway</body></html>',
})
} else {
// Server recovers on attempt 3
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ url: 'http://localhost:5199/mock-upload' }),
})
}
})
await page.route('**/mock-upload', (route) =>
route.fulfill({
status: 200,
headers: { ['ETag']: '"test-etag"' },
}),
)
await page.goto('/?view=uploader')
await page.getByTestId('start-upload-btn').click()
// Verify it stays uploading without failing on 502
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
// Once server recovers, upload completes
await expect(page.getByTestId('upload-status')).toHaveText('completed', { timeout: 15_000 })
expect(signAttempts).toBeGreaterThanOrEqual(3)
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
})
test('halts immediately without retrying on 500 Internal Server Error', async ({ page }) => {
let signAttempts = 0
await page.route('**/api/teams/**/upload/sign', (route) => {
signAttempts++
route.fulfill({
status: 500,
contentType: 'application/json',
body: JSON.stringify({ message: 'Internal Server Error' }),
})
})
await page.goto('/?view=uploader')
await page.getByTestId('start-upload-btn').click()
// Must fail immediately with error toast
await expect(page.getByTestId('upload-status')).toHaveText('failed', { timeout: 5_000 })
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toBeVisible({
timeout: 5_000,
})
// 0 retries (attempt count must be 1)
expect(signAttempts).toBe(1)
})
test('halts immediately without retrying on 403 Forbidden', async ({ page }) => {
let signAttempts = 0
await page.route('**/api/teams/**/upload/sign', (route) => {
signAttempts++
route.fulfill({
status: 403,
contentType: 'application/json',
body: JSON.stringify({ message: 'Forbidden' }),
})
})
await page.goto('/?view=uploader')
await page.getByTestId('start-upload-btn').click()
// Must fail immediately with error toast
await expect(page.getByTestId('upload-status')).toHaveText('failed', { timeout: 5_000 })
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toBeVisible({
timeout: 5_000,
})
// 0 retries (attempt count must be 1)
expect(signAttempts).toBe(1)
})
})
@@ -0,0 +1,158 @@
// @vitest-environment happy-dom
import { renderHook, waitFor } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { AssetInfo } from '@shumai/dtos'
import { useBatchFileStatus } from './use-batch-file-status'
import { client } from '@/ui/api/client'
import { useUploadStore } from '@/ui/stores/upload'
vi.mock('@/ui/api/client', () => ({
client: {
api: {
files: {
batch: {
$post: vi.fn(),
},
},
},
},
}))
describe('useBatchFileStatus', () => {
let queryClient: QueryClient
beforeEach(() => {
useUploadStore.setState({ fileProgress: {} })
queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
vi.clearAllMocks()
})
afterEach(() => {
vi.restoreAllMocks()
})
const wrapper = ({ children }: { children: React.ReactNode }) =>
React.createElement(QueryClientProvider, { client: queryClient }, children)
it('does not poll when items array is empty or no files are processing', () => {
const items: AssetInfo[] = [
{ id: 'f1', type: 'file', status: 'processed' } as AssetInfo,
{ id: 'f2', type: 'file', status: 'error' } as AssetInfo,
]
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
expect(client.api.files.batch.$post).not.toHaveBeenCalled()
})
it('does NOT poll when files are in "uploading" status and upload is in progress', () => {
const items: AssetInfo[] = [
{ id: 'f1', type: 'file', status: 'uploading' } as AssetInfo,
{ id: 'f2', type: 'file', status: 'uploading' } as AssetInfo,
]
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
expect(client.api.files.batch.$post).not.toHaveBeenCalled()
})
it('polls batch API when file has status "uploading" but upload is completed in store', async () => {
useUploadStore.setState({
fileProgress: {
f1: { fileId: 'f1', name: 'f1.mp4', loaded: 1000, total: 1000, status: 'completed' },
},
})
const items: AssetInfo[] = [
{ id: 'f1', type: 'file', status: 'uploading' } as AssetInfo,
{ id: 'f2', type: 'file', status: 'uploading' } as AssetInfo,
]
const updatedAssets: AssetInfo[] = [
{ id: 'f1', type: 'file', status: 'uploaded', name: 'f1.mp4' } as AssetInfo,
]
const invalidateSpy = vi.spyOn(queryClient, 'invalidateQueries')
vi.mocked(client.api.files.batch.$post).mockResolvedValue({
ok: true,
json: async () => updatedAssets,
} as unknown as Awaited<ReturnType<typeof client.api.files.batch.$post>>)
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
await waitFor(() => {
expect(client.api.files.batch.$post).toHaveBeenCalledWith({
json: { ids: ['f1'] },
})
})
expect(queryClient.getQueryData(['file', 'team-1', 'f1'])).toEqual(updatedAssets[0])
// Should NOT invalidate search query for intermediate 'uploaded' state; card updates via setQueryData
expect(invalidateSpy).not.toHaveBeenCalledWith({ queryKey: ['search', 'team-1'] })
})
it('polls batch API when files are in "uploaded" or "processing" status and updates cache', async () => {
const items: AssetInfo[] = [
{ id: 'f1', type: 'file', status: 'uploaded' } as AssetInfo,
{ id: 'f2', type: 'file', status: 'processing' } as AssetInfo,
{ id: 'f3', type: 'file', status: 'uploading' } as AssetInfo,
]
const updatedAssets: AssetInfo[] = [
{ id: 'f1', type: 'file', status: 'processing', name: 'f1.mp4' } as AssetInfo,
{ id: 'f2', type: 'file', status: 'processed', name: 'f2.mp4' } as AssetInfo,
]
const invalidateSpy = vi.spyOn(queryClient, 'invalidateQueries')
vi.mocked(client.api.files.batch.$post).mockResolvedValue({
ok: true,
json: async () => updatedAssets,
} as unknown as Awaited<ReturnType<typeof client.api.files.batch.$post>>)
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
await waitFor(() => {
expect(client.api.files.batch.$post).toHaveBeenCalledWith({
json: { ids: ['f1', 'f2'] },
})
})
// Should update per-file cache in TanStack Query
expect(queryClient.getQueryData(['file', 'team-1', 'f1'])).toEqual(updatedAssets[0])
expect(queryClient.getQueryData(['file', 'team-1', 'f2'])).toEqual(updatedAssets[1])
// Should invalidate folder search query because f2 finished ('processed')
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: ['search', 'team-1'] })
})
it('chunks requests when processing items exceed 100', async () => {
const items: AssetInfo[] = Array.from({ length: 150 }, (_, i) => ({
id: `file-${i}`,
type: 'file',
status: 'processing',
})) as AssetInfo[]
vi.mocked(client.api.files.batch.$post).mockResolvedValue({
ok: true,
json: async () => [],
} as unknown as Awaited<ReturnType<typeof client.api.files.batch.$post>>)
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
await waitFor(() => {
expect(client.api.files.batch.$post).toHaveBeenCalledTimes(2)
})
const calls = vi.mocked(client.api.files.batch.$post).mock.calls
expect(calls[0][0].json.ids).toHaveLength(100)
expect(calls[1][0].json.ids).toHaveLength(50)
})
})
@@ -0,0 +1,96 @@
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useMemo, useRef } from 'react'
import type { AssetInfo } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { useUploadStore } from '@/ui/stores/upload'
export const BATCH_SIZE = 100
export const BATCH_POLL_INTERVAL_MS = 2000
export const BATCH_SEARCH_INVALIDATE_THROTTLE_MS = 5000
export interface UseBatchFileStatusOptions {
teamId?: string
items: AssetInfo[]
}
export function useBatchFileStatus({ teamId, items }: UseBatchFileStatusOptions): void {
const queryClient = useQueryClient()
const fileProgress = useUploadStore((state) => state.fileProgress)
const lastInvalidateTimeRef = useRef(0)
const processingIds = useMemo(() => {
if (!teamId) return []
return items
.filter((item) => {
if (!item.id || (item.type !== 'file' && item.type !== 'version_stack')) return false
const isUploadedOrProcessing = item.status === 'uploaded' || item.status === 'processing'
const progress = fileProgress[item.id]
const isUploadCompleted =
item.status === 'uploading' &&
(progress?.status === 'completed' ||
(progress?.total != null && progress.total > 0 && progress.loaded >= progress.total))
return isUploadedOrProcessing || isUploadCompleted
})
.map((item) => item.id!)
}, [items, teamId, fileProgress])
const processingIdsRef = useRef(processingIds)
processingIdsRef.current = processingIds
const queryKey = useMemo(() => {
return ['files', 'batch', teamId]
}, [teamId])
useQuery({
queryKey,
queryFn: async () => {
const currentIds = processingIdsRef.current
if (currentIds.length === 0) return []
const chunks: string[][] = []
for (let i = 0; i < currentIds.length; i += BATCH_SIZE) {
chunks.push(currentIds.slice(i, i + BATCH_SIZE))
}
const results = await Promise.all(
chunks.map(async (chunkIds) => {
const res = await client.api.files.batch.$post({
json: { ids: chunkIds },
})
if (!res.ok) throw new Error('Failed to fetch batch file statuses')
return (await res.json()) as unknown as AssetInfo[]
}),
)
const assets = results.flat()
for (const asset of assets) {
if (asset.id) {
queryClient.setQueryData(['file', teamId, asset.id], asset)
}
}
const anyFinished = assets.some(
(a) => a.status === 'processed' || a.status === 'failed' || a.status === 'error',
)
const allFinished =
assets.length > 0 &&
assets.every(
(a) => a.status === 'processed' || a.status === 'failed' || a.status === 'error',
)
const now = Date.now()
if (
allFinished ||
(anyFinished && now - lastInvalidateTimeRef.current >= BATCH_SEARCH_INVALIDATE_THROTTLE_MS)
) {
lastInvalidateTimeRef.current = now
queryClient.invalidateQueries({ queryKey: ['search', teamId] })
}
return assets
},
enabled: !!teamId && processingIds.length > 0,
refetchInterval: BATCH_POLL_INTERVAL_MS,
})
}
+28 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { formatRemainingTime, getTrashDaysLeft } from './time'
import { formatRemainingTime, formatRemainingTimeSeconds, getTrashDaysLeft } from './time'
describe('formatRemainingTime', () => {
const baseNow = new Date('2026-08-17T12:00:00Z')
@@ -45,6 +45,33 @@ describe('formatRemainingTime', () => {
})
})
describe('formatRemainingTimeSeconds', () => {
it('formats hours and minutes', () => {
expect(formatRemainingTimeSeconds(3 * 3600 + 18 * 60)).toBe('3h 18m')
})
it('formats exact hours', () => {
expect(formatRemainingTimeSeconds(3 * 3600)).toBe('3h')
})
it('formats minutes', () => {
expect(formatRemainingTimeSeconds(8 * 60)).toBe('8m')
expect(formatRemainingTimeSeconds(45 * 60 + 20)).toBe('45m')
})
it('formats seconds', () => {
expect(formatRemainingTimeSeconds(45)).toBe('45s')
expect(formatRemainingTimeSeconds(1)).toBe('1s')
})
it('handles zero, negative, or invalid numbers', () => {
expect(formatRemainingTimeSeconds(0)).toBe('< 1s')
expect(formatRemainingTimeSeconds(-5)).toBe('< 1s')
expect(formatRemainingTimeSeconds(NaN)).toBe('< 1s')
expect(formatRemainingTimeSeconds(Infinity)).toBe('< 1s')
})
})
describe('getTrashDaysLeft', () => {
const now = new Date('2026-08-17T12:00:00Z')
+22
View File
@@ -75,6 +75,28 @@ export const formatRemainingTime = (targetDate: string | Date, now: Date = new D
return minutes > 0 ? `${minutes}m` : '< 1m'
}
export const formatRemainingTimeSeconds = (totalSeconds: number): string => {
if (!isFinite(totalSeconds) || totalSeconds <= 0) {
return '< 1s'
}
const total = Math.round(totalSeconds)
const days = Math.floor(total / 86400)
const hours = Math.floor((total % 86400) / 3600)
const minutes = Math.floor((total % 3600) / 60)
const seconds = total % 60
if (days > 0) {
return hours > 0 ? `${days}d ${hours}h` : `${days}d`
}
if (hours > 0) {
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`
}
if (minutes > 0) {
return `${minutes}m`
}
return `${seconds}s`
}
/**
* Whole days remaining before a soft-deleted asset is permanently purged.
* Returns `null` when there is no valid `deletedAt` timestamp, and `0` once the
+568 -2
View File
@@ -1,8 +1,20 @@
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { activeUploads, uploadFilesWithUppy, isNetworkIssue } from './uploader'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
activeUploads,
cancelActiveUploadTask,
uploadFilesWithUppy,
isNetworkIssue,
isLocalhostStorageMismatch,
calculateBackoffDelay,
MAX_RETRY_DELAY_MS,
BASE_RETRY_DELAY_MS,
UploadNetworkError,
UploadHttpError,
} from './uploader'
import { acquireWakeLock, releaseWakeLock } from './wake-lock'
import { useUploadStore } from '@/ui/stores/upload'
import { m } from '@/ui/paraglide/messages.js'
vi.mock('sonner', () => ({
toast: {
@@ -37,6 +49,16 @@ import { client } from '@/ui/api/client'
import { toast } from 'sonner'
describe('Uploader', () => {
const initialLocation = window.location
afterEach(() => {
Object.defineProperty(window, 'location', {
value: initialLocation,
configurable: true,
writable: true,
})
})
beforeEach(() => {
vi.clearAllMocks()
activeUploads.clear()
@@ -187,9 +209,59 @@ describe('Uploader', () => {
Object.defineProperty(navigator, 'onLine', { value: false, configurable: true })
expect(isNetworkIssue(new Error('Any error'))).toBe(true)
expect(isNetworkIssue()).toBe(true)
// Non-retryable statuses must return false even while offline
expect(
isNetworkIssue(
new UploadHttpError('Internal Server Error', { status: 500, isTransient: false }),
),
).toBe(false)
expect(
isNetworkIssue(new UploadHttpError('Forbidden', { status: 403, isTransient: false })),
).toBe(false)
expect(isNetworkIssue({ status: 500 })).toBe(false)
expect(isNetworkIssue({ status: 403 })).toBe(false)
expect(isNetworkIssue({ request: { status: 500 } })).toBe(false)
expect(isNetworkIssue({ request: { status: 403 } })).toBe(false)
expect(isNetworkIssue('upload failed with status: 500')).toBe(false)
expect(isNetworkIssue('upload failed with status: 403')).toBe(false)
// Transient statuses while offline return true
expect(
isNetworkIssue(new UploadHttpError('Bad Gateway', { status: 502, isTransient: true })),
).toBe(true)
expect(isNetworkIssue({ status: 502 })).toBe(true)
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
expect(isNetworkIssue(new Error('Failed to fetch'))).toBe(true)
expect(isNetworkIssue(new TypeError('Failed to fetch'))).toBe(true)
expect(isNetworkIssue('Failed to fetch')).toBe(true)
expect(isNetworkIssue('net::ERR_NETWORK_CHANGED')).toBe(true)
expect(isNetworkIssue(new UploadNetworkError('Network error while signing request'))).toBe(
true,
)
expect(
isNetworkIssue(new UploadHttpError('Bad Gateway', { status: 502, isTransient: true })),
).toBe(true)
expect(
isNetworkIssue(
new UploadHttpError('Service Unavailable', { status: 503, isTransient: true }),
),
).toBe(true)
expect(
isNetworkIssue(new UploadHttpError('Gateway Timeout', { status: 504, isTransient: true })),
).toBe(true)
expect(
isNetworkIssue(new UploadHttpError('Rate Limited', { status: 429, isTransient: true })),
).toBe(true)
expect(
isNetworkIssue(
new UploadHttpError('Internal Server Error', { status: 500, isTransient: false }),
),
).toBe(false)
expect(
isNetworkIssue(new UploadHttpError('Forbidden', { status: 403, isTransient: false })),
).toBe(false)
expect(isNetworkIssue({ status: 502 })).toBe(true)
expect(isNetworkIssue({ status: 500 })).toBe(false)
expect(isNetworkIssue(new Error('NetworkError when attempting to fetch resource.'))).toBe(
true,
)
@@ -206,6 +278,81 @@ describe('Uploader', () => {
})
})
describe('isLocalhostStorageMismatch', () => {
it('detects mismatch when upload URL is localhost and window location is remote', () => {
// In happy-dom window.location defaults to localhost:3000
Object.defineProperty(window, 'location', {
value: {
hostname: '192.168.1.100',
origin: 'http://192.168.1.100:3000',
},
configurable: true,
writable: true,
})
expect(isLocalhostStorageMismatch('http://localhost:3000/files/shumai/files/1.png')).toBe(
true,
)
expect(isLocalhostStorageMismatch('http://127.0.0.1:3000/files/shumai/files/1.png')).toBe(
true,
)
expect(isLocalhostStorageMismatch('http://[::1]:3000/files/shumai/files/1.png')).toBe(true)
expect(isLocalhostStorageMismatch('http://0.0.0.0:3000/files/shumai/files/1.png')).toBe(true)
// Remote upload URL with remote window -> no mismatch
expect(isLocalhostStorageMismatch('http://192.168.1.100:3000/files/shumai/files/1.png')).toBe(
false,
)
expect(isLocalhostStorageMismatch('https://s3.amazonaws.com/bucket/files/1.png')).toBe(false)
expect(isLocalhostStorageMismatch('/files/shumai/files/1.png')).toBe(false)
expect(isLocalhostStorageMismatch(undefined)).toBe(false)
expect(isLocalhostStorageMismatch('')).toBe(false)
})
it('returns false when window location itself is localhost', () => {
Object.defineProperty(window, 'location', {
value: {
hostname: 'localhost',
origin: 'http://localhost:3000',
},
configurable: true,
writable: true,
})
expect(isLocalhostStorageMismatch('http://localhost:3000/files/shumai/files/1.png')).toBe(
false,
)
expect(isLocalhostStorageMismatch('http://127.0.0.1:3000/files/shumai/files/1.png')).toBe(
false,
)
})
})
describe('calculateBackoffDelay', () => {
it('increases delay with attempts and caps at MAX_RETRY_DELAY_MS (60s)', () => {
const delay1 = calculateBackoffDelay(1)
expect(delay1).toBeGreaterThanOrEqual(1700)
expect(delay1).toBeLessThanOrEqual(2300)
const delay2 = calculateBackoffDelay(2)
expect(delay2).toBeGreaterThanOrEqual(3400)
expect(delay2).toBeLessThanOrEqual(4600)
const delay3 = calculateBackoffDelay(3)
expect(delay3).toBeGreaterThanOrEqual(6800)
expect(delay3).toBeLessThanOrEqual(9200)
const delay10 = calculateBackoffDelay(10)
expect(delay10).toBeGreaterThanOrEqual(Math.round(MAX_RETRY_DELAY_MS * 0.85))
expect(delay10).toBeLessThanOrEqual(MAX_RETRY_DELAY_MS)
})
it('never returns less than baseDelayMs', () => {
const delay0 = calculateBackoffDelay(0)
expect(delay0).toBeGreaterThanOrEqual(BASE_RETRY_DELAY_MS)
})
})
it('pauses on network disconnect, does not report terminal failure, and resumes on back-online', async () => {
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
useUploadStore
@@ -315,9 +462,428 @@ describe('Uploader', () => {
param: { teamId: 'team-1', taskId: 'task-1' },
json: { fileId: 'file-1', errorMessage: '403 Forbidden' },
})
expect(toast.error).toHaveBeenCalledWith(m.failed_upload_file_with_name({ name: 'test.mp4' }))
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
})
it('shows S3 endpoint localhost error toast without retrying when remote user encounters localhost upload URL', async () => {
Object.defineProperty(window, 'location', {
value: {
hostname: '192.168.1.100',
origin: 'http://192.168.1.100:3000',
},
configurable: true,
writable: true,
})
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
useUploadStore
.getState()
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'remote.mp4', size: 100 }])
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll')
const uploadPromise = uploadFilesWithUppy({
files: [{ id: 'temp-1', file: new File(['content'], 'remote.mp4', { type: 'video/mp4' }) }],
taskId: 'task-1',
teamId: 'team-1',
storageBackend: 'local',
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/remote.mp4' }],
presignedUrls: [
{
id: 'temp-1',
fileId: 'file-1',
url: 'http://localhost:3000/files/shumai/files/remote.mp4',
},
],
})
await new Promise((r) => setTimeout(r, 10))
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
expect(capturedUppy).toBeDefined()
const mockFile = {
id: 'f1',
name: 'remote.mp4',
meta: {
fileId: 'file-1',
url: 'http://localhost:3000/files/shumai/files/remote.mp4',
},
} as unknown as UppyFile<Meta, Record<string, never>>
const networkError = new Error('Failed to fetch')
capturedUppy.emit('upload-error', mockFile, networkError)
capturedUppy.emit('complete', {
successful: [],
failed: [{ ...mockFile, error: networkError.message }],
})
await uploadPromise
// Should NOT retry when localhost mismatch is detected
expect(retryAllSpy).not.toHaveBeenCalled()
// Must show specific AWS_ENDPOINT_URL_S3 toast
expect(toast.error).toHaveBeenCalledWith(m.upload_failed_s3_endpoint_localhost(), {
id: 's3-endpoint-localhost-mismatch',
duration: 10000,
})
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
param: { teamId: 'team-1', taskId: 'task-1' },
json: { fileId: 'file-1', errorMessage: 'Failed to fetch' },
})
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
retryAllSpy.mockRestore()
})
it('retries continuously on transient network errors while online without capping at 3 attempts', async () => {
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
useUploadStore
.getState()
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'test.mp4', size: 100 }])
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll').mockResolvedValue(undefined as never)
const uploadPromise = uploadFilesWithUppy({
files: [{ id: 'temp-1', file: new File(['content'], 'test.mp4', { type: 'video/mp4' }) }],
taskId: 'task-1',
teamId: 'team-1',
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/test.mp4' }],
})
await new Promise((r) => setTimeout(r, 10))
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
expect(capturedUppy).toBeDefined()
const mockFile = {
id: 'f1',
name: 'test.mp4',
meta: { fileId: 'file-1' },
} as unknown as UppyFile<Meta, Record<string, never>>
const networkError = new Error('Failed to fetch')
// Simulate 5 consecutive network errors while online (previously would have aborted after 3)
for (let i = 1; i <= 5; i++) {
capturedUppy.emit('upload-error', mockFile, networkError)
}
// Should still be active and uploading, NOT permanently failed
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('uploading')
expect(activeUploads.has('file-1')).toBe(true)
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).not.toHaveBeenCalled()
// Now succeed on retry
capturedUppy.emit('upload-success', mockFile, { status: 200 })
capturedUppy.emit('complete', { successful: [mockFile], failed: [] })
await uploadPromise
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('completed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
retryAllSpy.mockRestore()
})
it('halts retry loop and fails permanently when S3 returns 403 (presigned URL expired)', async () => {
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
useUploadStore
.getState()
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'expired.mp4', size: 100 }])
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
const uploadPromise = uploadFilesWithUppy({
files: [{ id: 'temp-1', file: new File(['content'], 'expired.mp4', { type: 'video/mp4' }) }],
taskId: 'task-1',
teamId: 'team-1',
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/expired.mp4' }],
})
await new Promise((r) => setTimeout(r, 10))
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
const mockFile = {
id: 'f1',
name: 'expired.mp4',
meta: { fileId: 'file-1' },
} as unknown as UppyFile<Meta, Record<string, never>>
// S3 returns 403 when presigned URL expires after 12h
const expiredError = Object.assign(new Error('Request has expired'), { status: 403 })
capturedUppy.emit('upload-error', mockFile, expiredError)
capturedUppy.emit('complete', {
successful: [],
failed: [{ ...mockFile, error: 'Request has expired' }],
})
await uploadPromise
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
param: { teamId: 'team-1', taskId: 'task-1' },
json: { fileId: 'file-1', errorMessage: 'Request has expired' },
})
expect(toast.error).toHaveBeenCalledWith(
m.failed_upload_file_with_name({ name: 'expired.mp4' }),
)
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
})
it('keeps upload active and retries when socket drops with TypeError: Failed to fetch while navigator.onLine is still true', async () => {
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
useUploadStore
.getState()
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'socket-drop.mp4', size: 100 }])
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll').mockResolvedValue(undefined as never)
const uploadPromise = uploadFilesWithUppy({
files: [
{ id: 'temp-1', file: new File(['content'], 'socket-drop.mp4', { type: 'video/mp4' }) },
],
taskId: 'task-1',
teamId: 'team-1',
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/socket-drop.mp4' }],
})
await new Promise((r) => setTimeout(r, 10))
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
const mockFile = {
id: 'f1',
name: 'socket-drop.mp4',
meta: { fileId: 'file-1' },
} as unknown as UppyFile<Meta, Record<string, never>>
// Socket drops: navigator.onLine is STILL TRUE at this exact moment
const fetchError = new TypeError('Failed to fetch')
capturedUppy.emit('upload-error', mockFile, fetchError)
// Uppy emits complete with stripped error string
capturedUppy.emit('complete', {
successful: [],
failed: [{ ...mockFile, error: 'Failed to fetch' }],
})
await new Promise((r) => setTimeout(r, 20))
// Must NOT be marked failed, activeUploads must retain file, no error toast
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('uploading')
expect(activeUploads.has('file-1')).toBe(true)
expect(toast.error).not.toHaveBeenCalled()
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).not.toHaveBeenCalled()
// Simulate recovery on retry
capturedUppy.emit('upload-success', mockFile, { status: 200 })
capturedUppy.emit('complete', { successful: [mockFile], failed: [] })
await uploadPromise
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('completed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
retryAllSpy.mockRestore()
})
it('retries continuously on 502 Bad Gateway and recovers on subsequent attempt', async () => {
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
useUploadStore
.getState()
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'gateway.mp4', size: 100 }])
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll').mockResolvedValue(undefined as never)
const uploadPromise = uploadFilesWithUppy({
files: [{ id: 'temp-1', file: new File(['content'], 'gateway.mp4', { type: 'video/mp4' }) }],
taskId: 'task-1',
teamId: 'team-1',
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/gateway.mp4' }],
})
await new Promise((r) => setTimeout(r, 10))
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
const mockFile = {
id: 'f1',
name: 'gateway.mp4',
meta: { fileId: 'file-1' },
} as unknown as UppyFile<Meta, Record<string, never>>
const gatewayError = new UploadHttpError('Failed to sign request: Bad Gateway', {
status: 502,
isTransient: true,
})
capturedUppy.emit('upload-error', mockFile, gatewayError)
capturedUppy.emit('complete', {
successful: [],
failed: [{ ...mockFile, error: 'Failed to sign request: Bad Gateway' }],
})
await new Promise((r) => setTimeout(r, 20))
// Must NOT be marked failed, activeUploads must retain file, no error toast
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('uploading')
expect(activeUploads.has('file-1')).toBe(true)
expect(toast.error).not.toHaveBeenCalled()
// Simulate recovery on retry
capturedUppy.emit('upload-success', mockFile, { status: 200 })
capturedUppy.emit('complete', { successful: [mockFile], failed: [] })
await uploadPromise
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('completed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
retryAllSpy.mockRestore()
})
it('fails permanently on 500 Internal Server Error without retrying', async () => {
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
useUploadStore
.getState()
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'server-err.mp4', size: 100 }])
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
const uploadPromise = uploadFilesWithUppy({
files: [
{ id: 'temp-1', file: new File(['content'], 'server-err.mp4', { type: 'video/mp4' }) },
],
taskId: 'task-1',
teamId: 'team-1',
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/server-err.mp4' }],
})
await new Promise((r) => setTimeout(r, 10))
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
const mockFile = {
id: 'f1',
name: 'server-err.mp4',
meta: { fileId: 'file-1' },
} as unknown as UppyFile<Meta, Record<string, never>>
const serverError = new UploadHttpError('Internal Server Error', {
status: 500,
isTransient: false,
})
capturedUppy.emit('upload-error', mockFile, serverError)
capturedUppy.emit('complete', {
successful: [],
failed: [{ ...mockFile, error: 'Internal Server Error' }],
})
await uploadPromise
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
param: { teamId: 'team-1', taskId: 'task-1' },
json: { fileId: 'file-1', errorMessage: 'Internal Server Error' },
})
expect(toast.error).toHaveBeenCalledWith(
m.failed_upload_file_with_name({ name: 'server-err.mp4' }),
)
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
})
it('fails permanently on 500/403 status even while navigator.onLine is false', async () => {
Object.defineProperty(navigator, 'onLine', { value: false, configurable: true })
useUploadStore
.getState()
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'offline-500.mp4', size: 100 }])
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
const uploadPromise = uploadFilesWithUppy({
files: [
{ id: 'temp-1', file: new File(['content'], 'offline-500.mp4', { type: 'video/mp4' }) },
],
taskId: 'task-1',
teamId: 'team-1',
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/offline-500.mp4' }],
})
await new Promise((r) => setTimeout(r, 10))
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
const mockFile = {
id: 'f1',
name: 'offline-500.mp4',
meta: { fileId: 'file-1' },
} as unknown as UppyFile<Meta, Record<string, never>>
const serverError = new UploadHttpError('Internal Server Error', {
status: 500,
isTransient: false,
})
capturedUppy.emit('upload-error', mockFile, serverError)
capturedUppy.emit('complete', {
successful: [],
failed: [{ ...mockFile, error: 'Internal Server Error' }],
})
await uploadPromise
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
param: { teamId: 'team-1', taskId: 'task-1' },
json: { fileId: 'file-1', errorMessage: 'Internal Server Error' },
})
expect(toast.error).toHaveBeenCalledWith(
m.failed_upload_file_with_name({ name: 'offline-500.mp4' }),
)
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
expect(activeUploads.size).toBe(0)
addFileSpy.mockRestore()
})
it('cancelActiveUploadTask cancels in-flight uppy upload and cleans up activeUploads', async () => {
useUploadStore
.getState()
.startTask('task-cancel', 'Task Cancel', [
{ fileId: 'file-c1', name: 'cancel.mp4', size: 1000 },
])
const uploadPromise = uploadFilesWithUppy({
files: [
{
id: 'temp-c1',
file: new File(['data'], 'cancel.mp4', { type: 'video/mp4' }),
},
],
taskId: 'task-cancel',
teamId: 'team-1',
storageBackend: 's3',
createdAssets: [{ tempId: 'temp-c1', assetId: 'file-c1', key: 'files/cancel.mp4' }],
})
await new Promise((r) => setTimeout(r, 10))
expect(activeUploads.has('file-c1')).toBe(true)
cancelActiveUploadTask('task-cancel')
expect(activeUploads.has('file-c1')).toBe(false)
await expect(uploadPromise).resolves.toBeUndefined()
})
})
+311 -63
View File
@@ -5,6 +5,7 @@ import type { PresignedUrl } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { useUploadStore } from '@/ui/stores/upload'
import { toast } from 'sonner'
import { m } from '@/ui/paraglide/messages.js'
import { acquireWakeLock, releaseWakeLock } from './wake-lock'
export type FileWithId = {
@@ -18,9 +19,28 @@ export interface ActiveUploadFile {
fileId: string
key?: string
uploadId?: string
url?: string
}
export const activeUploads = new Map<string, ActiveUploadFile>()
export const activeUppyByTaskId = new Map<string, Uppy>()
export function cancelActiveUploadTask(taskId: string): void {
const uppy = activeUppyByTaskId.get(taskId)
if (uppy) {
try {
uppy.cancelAll()
} catch (err) {
console.warn('Failed to cancel uppy uploads:', err)
}
}
for (const [fileId, item] of activeUploads.entries()) {
if (item.taskId === taskId) {
activeUploads.delete(fileId)
}
}
}
// Setup beforeunload and pagehide listeners once
if (typeof window !== 'undefined') {
@@ -63,26 +83,151 @@ if (typeof window !== 'undefined') {
})
}
const LOCALHOST_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]', '0.0.0.0'])
export const MAX_RETRY_DELAY_MS = 60000
export const BASE_RETRY_DELAY_MS = 1000
export class UploadNetworkError extends Error {
readonly code = 'NETWORK_ERROR' as const
constructor(message: string, options?: ErrorOptions) {
super(message, options)
this.name = 'UploadNetworkError'
}
}
export class UploadHttpError extends Error {
readonly code = 'HTTP_ERROR' as const
readonly status: number
readonly isTransient: boolean
constructor(message: string, options: { status: number; isTransient: boolean; cause?: unknown }) {
super(message, { cause: options.cause })
this.name = 'UploadHttpError'
this.status = options.status
this.isTransient = options.isTransient
}
}
export function calculateBackoffDelay(
attempts: number,
maxDelayMs = MAX_RETRY_DELAY_MS,
baseDelayMs = BASE_RETRY_DELAY_MS,
): number {
const effectiveBaseDelay =
(typeof window !== 'undefined' &&
(window as unknown as Record<string, number | undefined>)['__SHUMAI_UPLOAD_BASE_DELAY__']) ||
baseDelayMs
const exponentialDelay = Math.min(Math.pow(2, attempts) * effectiveBaseDelay, maxDelayMs)
const jitter = exponentialDelay * (Math.random() * 0.3 - 0.15) // ±15%
return Math.min(maxDelayMs, Math.max(effectiveBaseDelay, Math.round(exponentialDelay + jitter)))
}
export function isLocalhostStorageMismatch(uploadUrlString?: string): boolean {
if (!uploadUrlString || typeof window === 'undefined') return false
try {
const uploadUrl = new URL(uploadUrlString, window.location.origin)
const isUploadLocalhost = LOCALHOST_HOSTS.has(uploadUrl.hostname)
const isWindowLocalhost = LOCALHOST_HOSTS.has(window.location.hostname)
return isUploadLocalhost && !isWindowLocalhost
} catch {
return false
}
}
export function isNetworkIssue(error?: unknown): boolean {
if (!error) {
return typeof navigator !== 'undefined' && !navigator.onLine
}
// 1. Structured typed errors
if (error instanceof UploadHttpError) {
return error.isTransient
}
if (error instanceof UploadNetworkError) {
return true
}
const err = (typeof error === 'object' && error !== null ? error : {}) as Record<string, unknown>
// 2. HTTP status inspection (check first before generic offline or NetworkError flags,
// so non-retryable HTTP 500 / 4xx responses delivered as the browser transitions offline
// fail immediately instead of being treated as network drops)
const status =
typeof err.status === 'number'
? err.status
: typeof (err.request as Record<string, unknown> | undefined)?.status === 'number'
? ((err.request as Record<string, unknown>).status as number)
: undefined
if (status !== undefined) {
if (status === 0 || status === 429 || status === 502 || status === 503 || status === 504) {
return true
}
// Explicitly non-retryable statuses (500 Internal Server Error, 4xx except 429)
if (status === 500 || (status >= 400 && status < 500)) {
return false
}
if (status >= 500 && status < 600) {
return true
}
}
// 3. Fallback string matching for explicit non-retryable HTTP status messages
const rawMsg =
typeof error === 'string' ? error : typeof err.message === 'string' ? err.message : ''
if (rawMsg) {
const msg = rawMsg.toLowerCase()
// Explicit non-retryable status codes in string messages
if (
msg.includes('status: 500') ||
msg.includes('status: 400') ||
msg.includes('status: 401') ||
msg.includes('status: 403') ||
msg.includes('status: 404')
) {
return false
}
if (
msg.includes('network error') ||
msg.includes('failed to fetch') ||
msg.includes('networkerror') ||
msg.includes('network changed') ||
msg.includes('err_network_changed') ||
msg.includes('err_internet_disconnected') ||
msg.includes('err_connection_') ||
msg.includes('err_name_not_resolved') ||
msg.includes('the network connection was lost') ||
msg.includes('the internet connection appears to be offline') ||
(msg.includes('load failed') && !msg.includes('upload failed')) ||
msg.includes('timed out') ||
msg.includes('timeout') ||
msg.includes('bad gateway') ||
msg.includes('gateway timeout') ||
msg.includes('service unavailable')
) {
return true
}
}
// 4. Generic offline state (only when the error does not carry a non-retryable status code)
if (typeof navigator !== 'undefined' && !navigator.onLine) {
return true
}
if (!error) return false
const err = error as Record<string, unknown>
if (err.isNetworkError === true) return true
if (err.name === 'S3NetworkError' || err.name === 'NetworkError') return true
const msg = (typeof err.message === 'string' ? err.message : '').toLowerCase()
if (
msg.includes('network error') ||
msg.includes('failed to fetch') ||
msg.includes('networkerror') ||
msg.includes('load failed') ||
msg.includes('err_internet_disconnected')
) {
if (err.code === 'NETWORK' || err.code === 'NETWORK_ERROR') return true
if (err.name === 'S3NetworkError' || err.name === 'UploadNetworkError') {
return true
}
const req = err.request as Record<string, unknown> | undefined
if (req && req.status === 0) return true
// Standard fetch network failure
if (error instanceof TypeError) {
return true
}
// If there was no HTTP status attached, check generic Uppy NetworkError flag
if (err.name === 'NetworkError' || err.isNetworkError === true) return true
return false
}
@@ -110,7 +255,7 @@ export async function uploadFilesWithUppy({
await acquireWakeLock()
const store = useUploadStore.getState()
const { increment, decrement, updateFileProgress, completeFile, failFile } = store
const { increment, updateFileProgress, completeFile, failFile } = store
// Build lookup maps
const assetByTempId = new Map<string, { assetId: string; key?: string }>()
@@ -131,6 +276,7 @@ export async function uploadFilesWithUppy({
autoProceed: true,
allowMultipleUploadBatches: false,
})
activeUppyByTaskId.set(taskId, uppy)
if (storageBackend === 's3') {
uppy.use(AwsS3, {
@@ -153,24 +299,43 @@ export async function uploadFilesWithUppy({
}
}
const res = await fetch(`/api/teams/${teamId}/upload/sign`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
key: req.key,
method: req.method,
uploadId,
partNumber,
fileId,
}),
credentials: 'include',
})
let res: Response
try {
res = await fetch(`/api/teams/${teamId}/upload/sign`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
key: req.key,
method: req.method,
uploadId,
partNumber,
fileId,
}),
credentials: 'include',
})
} catch (err) {
if (err instanceof TypeError) {
throw new UploadNetworkError('Network error while signing request', { cause: err })
}
throw err
}
if (!res.ok) {
throw new Error(`Failed to sign request: ${res.statusText}`)
const isTransient =
res.status === 429 || res.status === 502 || res.status === 503 || res.status === 504
throw new UploadHttpError(`Failed to sign request: ${res.statusText}`, {
status: res.status,
isTransient,
})
}
const data = (await res.json()) as { url: string }
if (fileId) {
const active = activeUploads.get(fileId)
if (active) {
active.url = data.url
}
}
return { url: data.url }
},
})
@@ -199,7 +364,7 @@ export async function uploadFilesWithUppy({
const confirmationPromises = new Map<string, Promise<void>>()
const retryCounts = new Map<string, number>()
const MAX_ONLINE_RETRIES = 3
const pendingNetworkRetries = new Set<string>()
let retryTimeout: ReturnType<typeof setTimeout> | null = null
const triggerRetry = () => {
@@ -225,6 +390,11 @@ export async function uploadFilesWithUppy({
const handleBackOnline = () => {
const isOnline = typeof navigator !== 'undefined' ? navigator.onLine : true
console.log(`[Uploader] Network 'online' event received! navigator.onLine = ${isOnline}`)
retryCounts.clear()
if (retryTimeout) {
clearTimeout(retryTimeout)
retryTimeout = null
}
triggerRetry()
}
@@ -239,17 +409,63 @@ export async function uploadFilesWithUppy({
window.addEventListener('offline', handleOffline)
}
const fileSpeedTrackers = new Map<
string,
{ lastBytes: number; lastTime: number; smoothedSpeed?: number }
>()
uppy.on('upload-progress', (file, progress) => {
if (!file) return
const fileId = file.meta.fileId as string
if (fileId && progress.bytesUploaded != null) {
updateFileProgress(taskId, fileId, progress.bytesUploaded)
if (!fileId || progress.bytesUploaded == null) return
const now = Date.now()
const tracker = fileSpeedTrackers.get(fileId)
let speed: number | undefined
let eta: number | undefined
if (tracker) {
const timeDelta = (now - tracker.lastTime) / 1000
const bytesDelta = progress.bytesUploaded - tracker.lastBytes
if (timeDelta >= 0.5 && bytesDelta >= 0) {
const instantSpeed = bytesDelta / timeDelta
speed =
tracker.smoothedSpeed !== undefined
? tracker.smoothedSpeed * 0.7 + instantSpeed * 0.3
: instantSpeed
tracker.smoothedSpeed = speed
tracker.lastBytes = progress.bytesUploaded
tracker.lastTime = now
const remainingBytes = (progress.bytesTotal ?? 0) - progress.bytesUploaded
if (speed > 0 && remainingBytes > 0) {
eta = Math.round(remainingBytes / speed)
}
} else {
speed = tracker.smoothedSpeed
if (speed && speed > 0) {
const remainingBytes = (progress.bytesTotal ?? 0) - progress.bytesUploaded
eta = Math.round(remainingBytes / speed)
}
}
} else {
fileSpeedTrackers.set(fileId, {
lastBytes: progress.bytesUploaded,
lastTime: now,
})
}
updateFileProgress(taskId, fileId, progress.bytesUploaded, speed, eta)
})
uppy.on('upload-success', (file) => {
if (!file) return
const fileId = file.meta.fileId as string
if (fileId) {
pendingNetworkRetries.delete(fileId)
retryCounts.delete(fileId)
fileSpeedTrackers.delete(fileId)
}
activeUploads.delete(fileId)
const confirmPromise = (async () => {
@@ -262,13 +478,11 @@ export async function uploadFilesWithUppy({
throw new Error(`Failed to confirm upload: ${res.statusText}`)
}
completeFile(taskId, fileId)
decrement()
await onFileFinished?.(fileId)
} catch (err) {
console.error('Failed to confirm upload:', err)
failFile(taskId, fileId)
decrement()
toast.error(`Failed to confirm upload: ${file.name}`)
toast.error(m.failed_confirm_upload_with_name({ name: file.name }))
await onFileFinished?.(fileId)
throw err
}
@@ -283,11 +497,21 @@ export async function uploadFilesWithUppy({
) => {
const fileId = file.meta.fileId as string
if (!activeUploads.has(fileId)) return
activeUploads.delete(fileId)
failFile(taskId, fileId)
decrement()
const active = activeUploads.get(fileId)
const uploadUrl = (file.meta?.url as string | undefined) || active?.url
toast.error(`Failed to upload file: ${file.name}`)
activeUploads.delete(fileId)
fileSpeedTrackers.delete(fileId)
failFile(taskId, fileId)
if (isLocalhostStorageMismatch(uploadUrl)) {
toast.error(m.upload_failed_s3_endpoint_localhost(), {
id: 's3-endpoint-localhost-mismatch',
duration: 10000,
})
} else {
toast.error(m.failed_upload_file_with_name({ name: file.name }))
}
const errorPromise = (async () => {
try {
@@ -295,7 +519,8 @@ export async function uploadFilesWithUppy({
param: { teamId, taskId },
json: {
fileId,
errorMessage: (error as Error)?.message || 'Upload failed',
errorMessage:
(error as Error)?.message || (typeof error === 'string' ? error : 'Upload failed'),
},
})
} catch (err) {
@@ -311,32 +536,49 @@ export async function uploadFilesWithUppy({
if (!file) return
console.error(`[Uploader] upload-error for ${file.name}:`, error)
const fileId = file.meta?.fileId as string | undefined
const uploadUrl =
(file.meta?.url as string | undefined) ||
(fileId ? activeUploads.get(fileId)?.url : undefined)
if (isLocalhostStorageMismatch(uploadUrl)) {
console.warn(
`[Uploader] Localhost storage endpoint mismatch detected for ${file.name}. Failing permanently without retry.`,
)
if (fileId) {
pendingNetworkRetries.delete(fileId)
}
handlePermanentError(file, error)
return
}
if (isNetworkIssue(error)) {
console.warn(
`[Uploader] Network error detected for ${file.name}. Keeping upload active to resume when connection is restored.`,
)
if (typeof navigator !== 'undefined' && navigator.onLine) {
const fileId = file.meta.fileId as string
const attempts = (retryCounts.get(fileId) || 0) + 1
retryCounts.set(fileId, attempts)
if (fileId) {
pendingNetworkRetries.add(fileId)
}
if (attempts > MAX_ONLINE_RETRIES) {
console.error(
`[Uploader] Exceeded max online retries (${MAX_ONLINE_RETRIES}) for ${file.name}. Failing permanently.`,
)
handlePermanentError(file, error)
return
if (typeof navigator !== 'undefined' && navigator.onLine) {
const attempts = (fileId ? retryCounts.get(fileId) || 0 : 0) + 1
if (fileId) {
retryCounts.set(fileId, attempts)
}
const delayMs = calculateBackoffDelay(attempts)
console.log(
`[Uploader] Scheduling retry for ${file.name} (attempt ${attempts}/${MAX_ONLINE_RETRIES})...`,
`[Uploader] Scheduling retry for ${file.name} (attempt ${attempts}, delay ${delayMs}ms)...`,
)
scheduleRetry(2000)
scheduleRetry(delayMs)
}
return
}
if (fileId) {
pendingNetworkRetries.delete(fileId)
}
handlePermanentError(file, error)
})
@@ -357,6 +599,7 @@ export async function uploadFilesWithUppy({
taskId,
fileId,
key,
url,
})
increment()
@@ -383,20 +626,24 @@ export async function uploadFilesWithUppy({
}
const failedFiles = result?.failed || []
const isOffline = typeof navigator !== 'undefined' && !navigator.onLine
const hasNetworkFailures =
isOffline || failedFiles.some((f) => isNetworkIssue((f as { error?: unknown })?.error))
if (hasNetworkFailures) {
console.log(
'[Uploader] Batch completed with network issues; waiting for reconnection to resume...',
)
return
}
for (const f of failedFiles) {
const file = f as { name: string; meta: Record<string, unknown> }
handlePermanentError(file, (f as { error?: unknown })?.error)
const fId = file.meta?.fileId as string | undefined
if (fId && !pendingNetworkRetries.has(fId)) {
const err = (f as { error?: unknown })?.error
if (isNetworkIssue(err)) {
pendingNetworkRetries.add(fId)
continue
}
handlePermanentError(file, err)
}
}
if (pendingNetworkRetries.size > 0) {
console.log(
`[Uploader] Batch completed with ${pendingNetworkRetries.size} retrying file(s); waiting for reconnection or backoff to resume...`,
)
return
}
if (activeUploads.size === 0) {
@@ -412,6 +659,7 @@ export async function uploadFilesWithUppy({
await Promise.allSettled(Array.from(confirmationPromises.values()))
} finally {
activeUppyByTaskId.delete(taskId)
if (retryTimeout) {
clearTimeout(retryTimeout)
}
+12 -1
View File
@@ -126,6 +126,9 @@
"project_cover": "Project Cover",
"remove_cover": "Remove Cover",
"uploading": "Uploading...",
"user_is_uploading": "{user} is uploading...",
"upload_eta_speed": "{eta} left ({speed})",
"someone": "Someone",
"cover_preview": "Cover Preview",
"upload_image": "Upload Image",
"upload_cover": "Upload cover",
@@ -1124,6 +1127,9 @@
"n_folders_singular": "{count} Folder",
"n_folders_plural": "{count} Folders",
"failed_upload_file": "Failed to upload file",
"failed_upload_file_with_name": "Failed to upload file: {name}",
"failed_confirm_upload_with_name": "Failed to confirm upload: {name}",
"upload_failed_s3_endpoint_localhost": "Upload failed: Server storage endpoint is configured as localhost, but Shumai is being accessed from a remote address. Please set the AWS_ENDPOINT_URL_S3 environment variable on your server to your external URL.",
"failed_fetch_share_link": "Failed to fetch share link",
"failed_fetch_folders": "Failed to fetch folders",
@@ -1604,5 +1610,10 @@
"deny": "Deny",
"denying": "Denying...",
"consent_failed": "Failed to process authorization",
"mcp_access_scope_desc": "Full user-level access to Shumai workspace and MCP tools"
"mcp_access_scope_desc": "Full user-level access to Shumai workspace and MCP tools",
"cancel_upload_task": "Cancel upload",
"cancel_upload_task_confirm_title": "Cancel upload task?",
"cancel_upload_task_confirm_description": "Files that have already finished uploading will be kept in your project. Any incomplete or pending uploads will be canceled and removed.",
"upload_task_canceled": "Upload task canceled",
"failed_to_cancel_upload_task": "Failed to cancel upload task"
}
+12 -1
View File
@@ -126,6 +126,9 @@
"project_cover": "项目封面",
"remove_cover": "移除封面",
"uploading": "上传中...",
"user_is_uploading": "{user} 正在上传...",
"upload_eta_speed": "剩余 {eta} ({speed})",
"someone": "有人",
"cover_preview": "封面预览",
"upload_image": "上传图片",
"upload_cover": "上传封面",
@@ -1124,6 +1127,9 @@
"n_folders_singular": "{count} 个文件夹",
"n_folders_plural": "{count} 个文件夹",
"failed_upload_file": "上传文件失败",
"failed_upload_file_with_name": "上传文件失败: {name}",
"failed_confirm_upload_with_name": "确认上传失败: {name}",
"upload_failed_s3_endpoint_localhost": "上传失败:服务器存储端点配置为 localhost,但当前通过远程地址访问 Shumai。请在服务器上将 AWS_ENDPOINT_URL_S3 环境变量配置为外部访问地址。",
"failed_fetch_share_link": "获取分享链接失败",
"failed_fetch_folders": "获取文件夹失败",
@@ -1604,5 +1610,10 @@
"deny": "拒绝",
"denying": "拒绝中...",
"consent_failed": "处理授权失败",
"mcp_access_scope_desc": "以当前用户权限完全访问 Shumai 工作空间及 MCP 工具"
"mcp_access_scope_desc": "以当前用户权限完全访问 Shumai 工作空间及 MCP 工具",
"cancel_upload_task": "取消上传",
"cancel_upload_task_confirm_title": "取消上传任务?",
"cancel_upload_task_confirm_description": "已完成上传的文件将保留在项目中。任何未完成或等待中的上传将被取消并移除。",
"upload_task_canceled": "上传任务已取消",
"failed_to_cancel_upload_task": "取消上传任务失败"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/webui",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./index.html",
+6 -1
View File
@@ -75,7 +75,12 @@ function RootComponent() {
})
if (!showSidebar) {
return <Outlet />
return (
<>
<Toaster />
<Outlet />
</>
)
}
const unreadCount = me?.unreadNotificationCount ?? 0
+96
View File
@@ -0,0 +1,96 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { useUploadStore } from './upload'
describe('useUploadStore', () => {
beforeEach(() => {
useUploadStore.setState({
uploading: 0,
tasks: {},
fileProgress: {},
})
})
it('starts task and tracks initial progress', () => {
useUploadStore.getState().startTask('task-1', 'My Task', [
{ fileId: 'f1', name: 'video1.mp4', size: 1000 },
{ fileId: 'f2', name: 'video2.mp4', size: 2000 },
])
const state = useUploadStore.getState()
expect(state.tasks['task-1']).toBeDefined()
expect(state.tasks['task-1'].name).toBe('My Task')
expect(state.tasks['task-1'].total).toBe(3000)
expect(state.tasks['task-1'].loaded).toBe(0)
expect(state.tasks['task-1'].files['f1'].status).toBe('uploading')
expect(state.tasks['task-1'].files['f2'].status).toBe('uploading')
})
it('updates file progress and task loaded bytes', () => {
useUploadStore.getState().startTask('task-1', 'My Task', [
{ fileId: 'f1', name: 'video1.mp4', size: 1000 },
{ fileId: 'f2', name: 'video2.mp4', size: 2000 },
])
useUploadStore.getState().updateFileProgress('task-1', 'f1', 500, 250, 2)
expect(useUploadStore.getState().tasks['task-1'].loaded).toBe(500)
expect(useUploadStore.getState().fileProgress['f1'].loaded).toBe(500)
expect(useUploadStore.getState().fileProgress['f1'].speed).toBe(250)
expect(useUploadStore.getState().fileProgress['f1'].eta).toBe(2)
useUploadStore.getState().completeFile('task-1', 'f1')
expect(useUploadStore.getState().tasks['task-1'].files['f1'].status).toBe('completed')
expect(useUploadStore.getState().tasks['task-1'].files['f1'].loaded).toBe(1000)
expect(useUploadStore.getState().tasks['task-1'].loaded).toBe(1000)
})
it('cancelTask marks incomplete files as failed and decrements uploading count', () => {
useUploadStore.setState({ uploading: 3 })
useUploadStore.getState().startTask('task-1', 'My Task', [
{ fileId: 'f1', name: 'done.mp4', size: 1000 },
{ fileId: 'f2', name: 'stuck1.mp4', size: 2000 },
{ fileId: 'f3', name: 'stuck2.mp4', size: 3000 },
])
// f1 completes (automatically decrements uploading from 3 to 2)
useUploadStore.getState().completeFile('task-1', 'f1')
expect(useUploadStore.getState().uploading).toBe(2)
// cancelTask is called
useUploadStore.getState().cancelTask('task-1')
const task = useUploadStore.getState().tasks['task-1']
expect(task.files['f1'].status).toBe('completed')
expect(task.files['f2'].status).toBe('failed')
expect(task.files['f3'].status).toBe('failed')
expect(useUploadStore.getState().fileProgress['f2'].status).toBe('failed')
expect(useUploadStore.getState().fileProgress['f3'].status).toBe('failed')
expect(useUploadStore.getState().uploading).toBe(0)
})
it('does not decrement uploading count twice if completeFile or failFile is called after cancelTask', () => {
useUploadStore.setState({ uploading: 2 })
useUploadStore.getState().startTask('task-1', 'My Task', [
{ fileId: 'f1', name: 'in-flight.mp4', size: 1000 },
{ fileId: 'f2', name: 'waiting.mp4', size: 2000 },
])
// Cancel task while f1 confirmation was in-flight
useUploadStore.getState().cancelTask('task-1')
expect(useUploadStore.getState().uploading).toBe(0)
expect(useUploadStore.getState().tasks['task-1'].files['f1'].status).toBe('failed')
// In-flight confirmation settles late
useUploadStore.getState().completeFile('task-1', 'f1')
expect(useUploadStore.getState().uploading).toBe(0)
// Another late error settles
useUploadStore.getState().failFile('task-1', 'f2')
expect(useUploadStore.getState().uploading).toBe(0)
})
it('cancelTask gracefully handles non-existent task', () => {
useUploadStore.setState({ uploading: 1 })
useUploadStore.getState().cancelTask('non-existent')
expect(useUploadStore.getState().uploading).toBe(1)
})
})
+51 -3
View File
@@ -6,6 +6,8 @@ export interface FileUploadState {
loaded: number
total: number
status: 'uploading' | 'completed' | 'failed'
speed?: number
eta?: number
}
export interface TaskUploadState {
@@ -27,9 +29,16 @@ type UploadStore = {
taskName: string,
files: Array<{ fileId: string; name: string; size: number }>,
) => void
updateFileProgress: (taskId: string, fileId: string, loaded: number) => void
updateFileProgress: (
taskId: string,
fileId: string,
loaded: number,
speed?: number,
eta?: number,
) => void
completeFile: (taskId: string, fileId: string) => void
failFile: (taskId: string, fileId: string) => void
cancelTask: (taskId: string) => void
}
export const useUploadStore = create<UploadStore>((set) => ({
@@ -77,7 +86,7 @@ export const useUploadStore = create<UploadStore>((set) => ({
}
}),
updateFileProgress: (taskId, fileId, loaded) =>
updateFileProgress: (taskId, fileId, loaded, speed, eta) =>
set((state) => {
const task = state.tasks[taskId]
if (!task) return {}
@@ -85,7 +94,12 @@ export const useUploadStore = create<UploadStore>((set) => ({
const file = task.files[fileId]
if (!file) return {}
const updatedFile = { ...file, loaded }
const updatedFile: FileUploadState = {
...file,
loaded,
...(speed !== undefined ? { speed } : {}),
...(eta !== undefined ? { eta } : {}),
}
const updatedFiles = { ...task.files, [fileId]: updatedFile }
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
@@ -114,12 +128,14 @@ export const useUploadStore = create<UploadStore>((set) => ({
const file = task.files[fileId]
if (!file) return {}
const wasUploading = file.status === 'uploading'
const updatedFile: FileUploadState = { ...file, status: 'completed', loaded: file.total }
const updatedFiles = { ...task.files, [fileId]: updatedFile }
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
return {
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
tasks: {
...state.tasks,
[taskId]: {
@@ -143,12 +159,14 @@ export const useUploadStore = create<UploadStore>((set) => ({
const file = task.files[fileId]
if (!file) return {}
const wasUploading = file.status === 'uploading'
const updatedFile: FileUploadState = { ...file, status: 'failed' }
const updatedFiles = { ...task.files, [fileId]: updatedFile }
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
return {
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
tasks: {
...state.tasks,
[taskId]: {
@@ -163,4 +181,34 @@ export const useUploadStore = create<UploadStore>((set) => ({
},
}
}),
cancelTask: (taskId) =>
set((state) => {
const task = state.tasks[taskId]
if (!task) return {}
let decrements = 0
const updatedFiles = { ...task.files }
const updatedFileProgress = { ...state.fileProgress }
for (const [fileId, file] of Object.entries(task.files)) {
if (file.status === 'uploading') {
updatedFiles[fileId] = { ...file, status: 'failed' }
updatedFileProgress[fileId] = { ...file, status: 'failed' }
decrements++
}
}
return {
uploading: Math.max(0, state.uploading - decrements),
tasks: {
...state.tasks,
[taskId]: {
...task,
files: updatedFiles,
},
},
fileProgress: updatedFileProgress,
}
}),
}))
+1 -1
View File
@@ -1 +1 @@
export const APP_VERSION = '0.6.2'
export const APP_VERSION = '0.6.3'
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/workflow-core",
"version": "0.6.2",
"version": "0.6.3",
"private": true,
"type": "module",
"main": "./src/index.ts",
@@ -1,5 +1,7 @@
import { DBOS, Error as DBOSError } from '@dbos-inc/dbos-sdk'
export { DBOS, DBOSError }
/**
* Task queue constants.
*/