From 87f466c2fbfa60626857dc7d26ce5b4756772f2c Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 00:13:58 -0400 Subject: [PATCH 01/10] feat(models): send a model file from the computer to the workspace library Adds the client for uploading a model file and the composable that runs one upload, for the Import dialog's file option to use, and the generated ingest types for the two upload routes (startModelUpload, completeModelUpload, ModelUploadStarted, the features field model_upload_folders) from cloud's ingest OpenAPI. modelFileUploadApi.uploadModelFile asks POST /api/assets/uploads for a signed URL, PUTs the raw file straight to storage with the headers that answer lists (progress from the browser's upload events, cancel through an AbortSignal), then calls POST /api/assets/uploads/{id}/complete and returns its task id. Both answers are checked with the generated zod schemas. It returns a result value and never throws: the server's file-type and size refusals, a storage refusal, a dropped connection and a cancel each come back as a reason, and UNSUPPORTED_MODEL_TYPE as its own reason. useUploadModelFile holds one upload as a single state (idle, chosen, uploading, saving, done, failed): it refuses a file that is not .safetensors, offers only the folders the server lists in model_upload_folders (read through useFeatureFlags, LoRA first when listed), asks for another type when the server refuses the folder, tags the file models plus the chosen folder, follows the stored task the way a link import is followed, so the existing completion refresh updates the library, drops an answer that arrives after reset, aborts on dispose, and reports a failed upload. It is available only while runtime_library_models_enabled is on and the server lists at least one folder. Ref BE-24338 --- packages/ingest-types/src/index.ts | 11 + packages/ingest-types/src/types.gen.ts | 142 ++++++++ packages/ingest-types/src/zod.gen.ts | 52 +++ src/composables/__mocks__/useFeatureFlags.ts | 1 + src/composables/useFeatureFlags.test.ts | 24 ++ src/composables/useFeatureFlags.ts | 8 + src/locales/en/main.json | 6 + .../remoteConfig/serverFeatureFlag.ts | 1 + .../workspace/api/modelFileUploadApi.test.ts | 233 +++++++++++++ .../workspace/api/modelFileUploadApi.ts | 146 ++++++++ .../composables/useUploadModelFile.test.ts | 326 ++++++++++++++++++ .../composables/useUploadModelFile.ts | 166 +++++++++ 12 files changed, 1116 insertions(+) create mode 100644 src/platform/workspace/api/modelFileUploadApi.test.ts create mode 100644 src/platform/workspace/api/modelFileUploadApi.ts create mode 100644 src/platform/workspace/composables/useUploadModelFile.test.ts create mode 100644 src/platform/workspace/composables/useUploadModelFile.ts diff --git a/packages/ingest-types/src/index.ts b/packages/ingest-types/src/index.ts index 0c9a69727e6..a677935b768 100644 --- a/packages/ingest-types/src/index.ts +++ b/packages/ingest-types/src/index.ts @@ -281,6 +281,11 @@ export type { ClearWorkspaceDeploymentResponses, ClientOptions, CloudNodeCompatibility, + CompleteModelUploadData, + CompleteModelUploadError, + CompleteModelUploadErrors, + CompleteModelUploadResponse, + CompleteModelUploadResponses, ContentCatalogProjection, ContentCatalogRecord, CreateAssetData, @@ -1228,6 +1233,7 @@ export type { ModelDownloadFrame, ModelFile, ModelFolder, + ModelUploadStarted, MoveUserdataFileData, MoveUserdataFileError, MoveUserdataFileErrors, @@ -1476,6 +1482,11 @@ export type { StageCccChangesErrors, StageCccChangesResponse, StageCccChangesResponses, + StartModelUploadData, + StartModelUploadError, + StartModelUploadErrors, + StartModelUploadResponse, + StartModelUploadResponses, StartSsoConfirmedData, StartSsoConfirmedError, StartSsoConfirmedErrors, diff --git a/packages/ingest-types/src/types.gen.ts b/packages/ingest-types/src/types.gen.ts index fb955b23409..a5d78117582 100644 --- a/packages/ingest-types/src/types.gen.ts +++ b/packages/ingest-types/src/types.gen.ts @@ -473,6 +473,35 @@ export type ValidationResult = { warnings?: Array } +/** + * Where and how the browser sends a model file's bytes (POST /api/assets/uploads). + */ +export type ModelUploadStarted = { + /** + * Names this upload in `POST /api/assets/uploads/{upload_id}/complete`. + */ + upload_id: string + /** + * Signed URL for exactly one staged object; works until `expires_at`. + */ + upload_url: string + method: 'PUT' + /** + * Every header the PUT must send, with exactly these values (`Content-Type` and `x-goog-content-length-range`). + */ + headers: { + [key: string]: string + } + /** + * When `upload_url` stops working (15 minutes after the answer). + */ + expires_at: string + /** + * The largest file an upload accepts, 2147483648 (2 GiB). + */ + max_size_bytes: number +} + /** * Details of a single validation error encountered during asset operations. */ @@ -10286,6 +10315,115 @@ export type CreateAssetDownloadResponses = { export type CreateAssetDownloadResponse = CreateAssetDownloadResponses[keyof CreateAssetDownloadResponses] +export type StartModelUploadData = { + body: { + /** + * A bare file name ending in `.safetensors` (any case), at most 255 bytes, no path, no leading dot. + */ + filename: string + /** + * The file's size, 1 to 2147483648 (2 GiB). + */ + size_bytes: number + /** + * Exactly `models` plus one model folder from `model_upload_folders` in GET /api/features (`loras`, `checkpoints`, `vae`, `embeddings`, ...); another folder is `400 UNSUPPORTED_MODEL_TYPE`. + */ + tags: Array + } + path?: never + query?: never + url: '/api/assets/uploads' +} + +export type StartModelUploadErrors = { + /** + * `INVALID_BODY` (bad filename, size or tag list), `UNSUPPORTED_FILE_TYPE` (not `.safetensors`), `UNSUPPORTED_MODEL_TYPE` (`models` plus a folder not in `model_upload_folders` of GET /api/features) or `FILE_TOO_LARGE` (over 2 GiB; the message is "Files can be up to 2 GB.") + */ + 400: ErrorResponse + /** + * Unauthorized + */ + 401: ErrorResponse + /** + * `FEATURE_DISABLED` when `runtime_library_models_enabled` is off for this workspace, or a refused session write (see `SessionWriteForbidden`) + */ + 403: ErrorResponse + /** + * Internal server error + */ + 500: ErrorResponse +} + +export type StartModelUploadError = + StartModelUploadErrors[keyof StartModelUploadErrors] + +export type StartModelUploadResponses = { + /** + * Signed upload URL for one staged object + */ + 201: ModelUploadStarted +} + +export type StartModelUploadResponse = + StartModelUploadResponses[keyof StartModelUploadResponses] + +export type CompleteModelUploadData = { + body: { + /** + * The `filename` sent to start the upload. + */ + filename: string + /** + * The `tags` sent to start the upload. + */ + tags: Array + } + path: { + /** + * The `upload_id` from `POST /api/assets/uploads`. + */ + upload_id: string + } + query?: never + url: '/api/assets/uploads/{upload_id}/complete' +} + +export type CompleteModelUploadErrors = { + /** + * `INVALID_BODY` (bad filename or tag list), `UNSUPPORTED_MODEL_TYPE` (a folder not in `model_upload_folders`) or `FILE_TOO_LARGE` (the stored object is over 2 GiB; the message is "Files can be up to 2 GB.") + */ + 400: ErrorResponse + /** + * Unauthorized + */ + 401: ErrorResponse + /** + * `FEATURE_DISABLED` when `runtime_library_models_enabled` is off for this workspace, or a refused session write (see `SessionWriteForbidden`) + */ + 403: ErrorResponse + /** + * `UPLOAD_NOT_FOUND`: no uploaded object for this `upload_id` under this caller and workspace (not uploaded yet, cleaned up, or someone else's) + */ + 404: ErrorResponse + /** + * Internal server error + */ + 500: ErrorResponse +} + +export type CompleteModelUploadError = + CompleteModelUploadErrors[keyof CompleteModelUploadErrors] + +export type CompleteModelUploadResponses = { + /** + * Accepted; a task stores the file as an asset + */ + 202: AssetDownloadResponse +} + +export type CompleteModelUploadResponse = + CompleteModelUploadResponses[keyof CompleteModelUploadResponses] + export type CreateAssetExportData = { body: { /** @@ -13243,6 +13381,10 @@ export type GetFeaturesResponses = { * Maximum upload size in bytes */ max_upload_size?: number + /** + * The model types (folder names, sorted) a model file uploaded from disk may be: the list POST /api/assets/uploads checks, which answers any other with 400 UNSUPPORTED_MODEL_TYPE. Present only while runtime_library_models_enabled is on for the caller. + */ + model_upload_folders?: Array /** * Whether new free-tier subscriptions are enabled for this caller. Current servers always emit a boolean; clients should tolerate absence when talking to older servers that predate this declared field. */ diff --git a/packages/ingest-types/src/zod.gen.ts b/packages/ingest-types/src/zod.gen.ts index 3b6342952f3..625c8888ec6 100644 --- a/packages/ingest-types/src/zod.gen.ts +++ b/packages/ingest-types/src/zod.gen.ts @@ -4396,6 +4396,25 @@ export const zAssetMetadataResponse = z.object({ validation: zValidationResult.optional() }) +/** + * Where and how the browser sends a model file's bytes (POST /api/assets/uploads). + */ +export const zModelUploadStarted = z.object({ + upload_id: z.uuid(), + upload_url: z.string(), + method: z.enum(['PUT']), + headers: z.record(z.string(), z.string()), + expires_at: z.iso.datetime(), + max_size_bytes: z.coerce + .bigint() + .min(BigInt('-9223372036854775808'), { + error: 'Invalid value: Expected int64 to be >= -9223372036854775808' + }) + .max(BigInt('9223372036854775807'), { + error: 'Invalid value: Expected int64 to be <= 9223372036854775807' + }) +}) + /** * Acknowledgement of an async asset download task; clients poll GET /api/tasks/{task_id} for status. */ @@ -5460,6 +5479,38 @@ export const zCreateAssetDownloadResponse = z.union([ zAssetDownloadResponse ]) +export const zStartModelUploadBody = z.object({ + filename: z.string(), + size_bytes: z.coerce + .bigint() + .min(BigInt('-9223372036854775808'), { + error: 'Invalid value: Expected int64 to be >= -9223372036854775808' + }) + .max(BigInt('9223372036854775807'), { + error: 'Invalid value: Expected int64 to be <= 9223372036854775807' + }), + tags: z.array(z.string()) +}) + +/** + * Signed upload URL for one staged object + */ +export const zStartModelUploadResponse = zModelUploadStarted + +export const zCompleteModelUploadBody = z.object({ + filename: z.string(), + tags: z.array(z.string()) +}) + +export const zCompleteModelUploadPath = z.object({ + upload_id: z.string() +}) + +/** + * Accepted; a task stores the file as an asset + */ +export const zCompleteModelUploadResponse = zAssetDownloadResponse + export const zCreateAssetExportBody = z.object({ asset_ids: z.array(z.string()).optional(), include_previews: z.boolean().optional().default(false), @@ -6002,6 +6053,7 @@ export const zGetFeaturesResponse = z.object({ }) .optional(), max_upload_size: z.int().optional(), + model_upload_folders: z.array(z.string()).optional(), new_free_tier_subscriptions: z.boolean().optional(), private_workflow_links_enabled: z.boolean().optional(), sso_enabled: z.boolean().optional(), diff --git a/src/composables/__mocks__/useFeatureFlags.ts b/src/composables/__mocks__/useFeatureFlags.ts index ae0e4116890..67e790cbf72 100644 --- a/src/composables/__mocks__/useFeatureFlags.ts +++ b/src/composables/__mocks__/useFeatureFlags.ts @@ -29,6 +29,7 @@ const defaultFlags: FeatureFlags = { deploymentCompatibilityMarksEnabled: false, lateCustomNodesEnabled: false, runtimeLibraryModelsEnabled: false, + modelUploadFolders: [], hostedBillingDestination: 'stripe', hostedBillingWebEnabled: false, showSignInButton: undefined, diff --git a/src/composables/useFeatureFlags.test.ts b/src/composables/useFeatureFlags.test.ts index 3e139098eb3..a266b519686 100644 --- a/src/composables/useFeatureFlags.test.ts +++ b/src/composables/useFeatureFlags.test.ts @@ -642,6 +642,30 @@ describe('useFeatureFlags', () => { }) }) + describe('modelUploadFolders', () => { + afterEach(() => { + remoteConfig.value = {} + }) + + it('lists the folders the server accepts uploads into', () => { + remoteConfig.value = { model_upload_folders: ['checkpoints', 'loras'] } + + const { flags } = useFeatureFlags() + + expect(flags.modelUploadFolders).toEqual(['checkpoints', 'loras']) + }) + + it('is empty when the server sends no list', () => { + vi.mocked(api.getServerFeature).mockImplementation( + (_path, defaultValue) => defaultValue + ) + + const { flags } = useFeatureFlags() + + expect(flags.modelUploadFolders).toEqual([]) + }) + }) + describe('dev override via localStorage', () => { it('resolveFlag returns localStorage override over remoteConfig and server value', () => { vi.mocked(api.getServerFeature).mockReturnValue(false) diff --git a/src/composables/useFeatureFlags.ts b/src/composables/useFeatureFlags.ts index 00403090fee..ec12fc0994f 100644 --- a/src/composables/useFeatureFlags.ts +++ b/src/composables/useFeatureFlags.ts @@ -245,6 +245,14 @@ export function useFeatureFlags() { false ) }, + /** The model folders an uploaded model file may go into; empty when uploads are off. */ + get modelUploadFolders(): readonly string[] { + return resolveFlag( + ServerFeatureFlag.MODEL_UPLOAD_FOLDERS, + remoteConfig.value.model_upload_folders, + [] + ) + }, get lateCustomNodesEnabled() { return resolveFlag( ServerFeatureFlag.LATE_CUSTOM_NODES_ENABLED, diff --git a/src/locales/en/main.json b/src/locales/en/main.json index bc809a0b813..bfbb80b2115 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -4530,6 +4530,12 @@ "fileName": "File Name", "fileSize": "File Size", "filterBy": "Filter by", + "fileUpload": { + "cancelled": "Upload canceled.", + "failed": "Upload failed. Try again.", + "notSafetensors": "Only .safetensors files can be uploaded.", + "unsupportedType": "Cloud can't store this model type yet. Choose another type." + }, "findInLibrary": "Find it in the {type} section of the models library.", "finish": "Finish", "importAnother": "Import Another", diff --git a/src/platform/remoteConfig/serverFeatureFlag.ts b/src/platform/remoteConfig/serverFeatureFlag.ts index d16253423d9..e4b983bd9ac 100644 --- a/src/platform/remoteConfig/serverFeatureFlag.ts +++ b/src/platform/remoteConfig/serverFeatureFlag.ts @@ -22,6 +22,7 @@ export enum ServerFeatureFlag { DEPLOYMENT_COMPATIBILITY_MARKS_ENABLED = 'deployment_compatibility_marks_enabled', LATE_CUSTOM_NODES_ENABLED = 'late_custom_nodes_enabled', RUNTIME_LIBRARY_MODELS_ENABLED = 'runtime_library_models_enabled', + MODEL_UPLOAD_FOLDERS = 'model_upload_folders', HOSTED_BILLING_DESTINATION = 'hosted_billing_destination', SHOW_SIGNIN_BUTTON = 'show_signin_button', UNIFIED_CLOUD_AUTH = 'unified_cloud_auth', diff --git a/src/platform/workspace/api/modelFileUploadApi.test.ts b/src/platform/workspace/api/modelFileUploadApi.test.ts new file mode 100644 index 00000000000..17b7418db05 --- /dev/null +++ b/src/platform/workspace/api/modelFileUploadApi.test.ts @@ -0,0 +1,233 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { api } from '@/scripts/api' + +import { modelFileUploadApi } from './modelFileUploadApi' + +vi.mock(import('@/scripts/api')) + +const UPLOAD_ID = '2f4b8c1e-6a0d-4e7b-9c3a-1d5e8f7a9b20' +const TASK_ID = '8c2d4e6f-1a3b-4c5d-9e7f-0a1b2c3d4e5f' +const SIGNED_URL = 'https://storage.example.com/temp/model-uploads/x?sig=1' +const SIGNED_HEADERS = { + 'Content-Type': 'application/octet-stream', + 'x-goog-content-length-range': '0,12' +} + +class StorageRequest { + static sent: StorageRequest[] = [] + method = '' + url = '' + headers: Record = {} + body: unknown + status = 0 + aborted = false + upload: { onprogress: ((event: { loaded: number }) => void) | null } = { + onprogress: null + } + onload: (() => void) | null = null + onerror: (() => void) | null = null + onabort: (() => void) | null = null + onloadend: (() => void) | null = null + + open(method: string, url: string) { + this.method = method + this.url = url + } + setRequestHeader(name: string, value: string) { + this.headers[name] = value + } + send(body: unknown) { + this.body = body + StorageRequest.sent.push(this) + } + abort() { + this.aborted = true + this.onabort?.() + this.onloadend?.() + } + progress(loaded: number) { + this.upload.onprogress?.({ loaded }) + } + finish(status: number) { + this.status = status + this.onload?.() + this.onloadend?.() + } + fail() { + this.onerror?.() + this.onloadend?.() + } +} + +function json(status: number, body: unknown) { + return new Response(JSON.stringify(body), { + status, + headers: { 'Content-Type': 'application/json' } + }) +} + +const started = { + upload_id: UPLOAD_ID, + upload_url: SIGNED_URL, + method: 'PUT', + headers: SIGNED_HEADERS, + expires_at: '2026-10-29T15:30:00Z', + max_size_bytes: 2147483648 +} + +function serverAnswers(...answers: Response[]) { + const calls: Array<{ route: string; body: unknown }> = [] + vi.mocked(api.fetchApi).mockImplementation(async (route, options) => { + calls.push({ route, body: JSON.parse(String(options?.body)) }) + return answers.shift() ?? json(500, { code: 'INTERNAL', message: 'x' }) + }) + return calls +} + +async function storageRequest() { + await vi.waitFor(() => expect(StorageRequest.sent).toHaveLength(1)) + return StorageRequest.sent[0] +} + +const file = new File(['twelve bytes'], 'my_style_v2.safetensors') +const tags = ['models', 'loras'] + +describe('modelFileUploadApi.uploadModelFile', () => { + beforeEach(() => { + StorageRequest.sent = [] + vi.stubGlobal('XMLHttpRequest', StorageRequest) + }) + + function upload(signal = new AbortController().signal) { + const onProgress = vi.fn() + const result = modelFileUploadApi.uploadModelFile(file, { + tags, + signal, + onProgress + }) + return { result, onProgress } + } + + it('starts, puts the bytes straight to storage with the signed headers, then completes', async () => { + const calls = serverAnswers( + json(201, started), + json(202, { task_id: TASK_ID, status: 'created' }) + ) + const { result, onProgress } = upload() + + const put = await storageRequest() + expect(put.method).toBe('PUT') + expect(put.url).toBe(SIGNED_URL) + expect(put.headers).toEqual(SIGNED_HEADERS) + expect(put.body).toBe(file) + put.progress(5) + put.finish(200) + + await expect(result).resolves.toEqual({ ok: true, taskId: TASK_ID }) + expect(onProgress).toHaveBeenCalledWith(5) + expect(calls).toEqual([ + { + route: '/assets/uploads', + body: { filename: file.name, size_bytes: file.size, tags } + }, + { + route: `/assets/uploads/${UPLOAD_ID}/complete`, + body: { filename: file.name, tags } + } + ]) + }) + + it('says the file type is refused when the server refuses it', async () => { + serverAnswers( + json(400, { code: 'UNSUPPORTED_FILE_TYPE', message: 'not safetensors' }) + ) + + await expect(upload().result).resolves.toEqual({ + ok: false, + reason: 'unsupportedFile' + }) + expect(StorageRequest.sent).toHaveLength(0) + }) + + it("passes on the server's size limit message", async () => { + serverAnswers( + json(400, { code: 'FILE_TOO_LARGE', message: 'Files can be up to 2 GB.' }) + ) + + await expect(upload().result).resolves.toEqual({ + ok: false, + reason: 'tooLarge', + message: 'Files can be up to 2 GB.' + }) + expect(StorageRequest.sent).toHaveLength(0) + }) + + it('says the model type is refused when the server cannot store that folder', async () => { + serverAnswers( + json(400, { code: 'UNSUPPORTED_MODEL_TYPE', message: 'not stored' }) + ) + + await expect(upload().result).resolves.toEqual({ + ok: false, + reason: 'unsupportedType' + }) + expect(StorageRequest.sent).toHaveLength(0) + }) + + it('says the model type is refused when the finish call refuses it', async () => { + serverAnswers( + json(201, started), + json(400, { code: 'UNSUPPORTED_MODEL_TYPE', message: 'not stored' }) + ) + const { result } = upload() + + ;(await storageRequest()).finish(200) + + await expect(result).resolves.toEqual({ + ok: false, + reason: 'unsupportedType' + }) + }) + + it('fails without completing when storage refuses the bytes', async () => { + const calls = serverAnswers(json(201, started)) + const { result } = upload() + + ;(await storageRequest()).finish(403) + + await expect(result).resolves.toMatchObject({ ok: false, reason: 'failed' }) + expect(calls).toHaveLength(1) + }) + + it('fails when the connection drops during the upload', async () => { + serverAnswers(json(201, started)) + const { result } = upload() + + ;(await storageRequest()).fail() + + await expect(result).resolves.toMatchObject({ ok: false, reason: 'failed' }) + }) + + it('stops the transfer and never completes when cancelled', async () => { + const calls = serverAnswers(json(201, started)) + const controller = new AbortController() + const { result } = upload(controller.signal) + + const put = await storageRequest() + controller.abort() + + await expect(result).resolves.toEqual({ ok: false, reason: 'cancelled' }) + expect(put.aborted).toBe(true) + expect(calls).toHaveLength(1) + }) + + it('fails when the finish answer is not a task', async () => { + serverAnswers(json(201, started), json(202, { status: 'created' })) + const { result } = upload() + + ;(await storageRequest()).finish(200) + + await expect(result).resolves.toMatchObject({ ok: false, reason: 'failed' }) + }) +}) diff --git a/src/platform/workspace/api/modelFileUploadApi.ts b/src/platform/workspace/api/modelFileUploadApi.ts new file mode 100644 index 00000000000..9a97941e6a0 --- /dev/null +++ b/src/platform/workspace/api/modelFileUploadApi.ts @@ -0,0 +1,146 @@ +import type { + CompleteModelUploadData, + ModelUploadStarted, + StartModelUploadData +} from '@comfyorg/ingest-types' +import { + zCompleteModelUploadResponse, + zErrorResponse, + zStartModelUploadResponse +} from '@comfyorg/ingest-types/zod' + +import { api } from '@/scripts/api' + +const UPLOADS_ENDPOINT = '/assets/uploads' + +type ModelFileUploadFailure = + | 'unsupportedFile' + | 'unsupportedType' + | 'tooLarge' + | 'cancelled' + | 'failed' + +/** The outcome of sending one model file: the server task that stores it, or why it stopped. */ +export type ModelFileUploadResult = + | { ok: true; taskId: string } + | { + ok: false + reason: ModelFileUploadFailure + message?: string + cause?: unknown + } + +interface ModelFileUploadOptions { + tags: string[] + signal: AbortSignal + onProgress: (sentBytes: number) => void +} + +async function readFailure( + res: Response +): Promise> { + const body = zErrorResponse.safeParse(await res.json().catch(() => null)) + const code = body.success ? body.data.code : undefined + if (code === 'UNSUPPORTED_FILE_TYPE') { + return { ok: false, reason: 'unsupportedFile' } + } + if (code === 'UNSUPPORTED_MODEL_TYPE') { + return { ok: false, reason: 'unsupportedType' } + } + if (code === 'FILE_TOO_LARGE' && body.success) { + return { ok: false, reason: 'tooLarge', message: body.data.message } + } + return { ok: false, reason: 'failed', cause: `${res.status} ${code ?? ''}` } +} + +function putFile( + start: Pick, + file: File, + { signal, onProgress }: ModelFileUploadOptions +): Promise { + return new Promise((resolve) => { + if (signal.aborted) { + resolve({ ok: false, reason: 'cancelled' }) + return + } + const request = new XMLHttpRequest() + request.open(start.method, start.upload_url) + for (const [name, value] of Object.entries(start.headers)) { + request.setRequestHeader(name, value) + } + request.upload.onprogress = (event) => onProgress(event.loaded) + request.onload = () => + resolve( + request.status >= 200 && request.status < 300 + ? undefined + : { ok: false, reason: 'failed', cause: `storage ${request.status}` } + ) + request.onerror = () => + resolve({ ok: false, reason: 'failed', cause: 'storage network error' }) + request.onabort = () => resolve({ ok: false, reason: 'cancelled' }) + const abort = () => request.abort() + request.onloadend = () => signal.removeEventListener('abort', abort) + signal.addEventListener('abort', abort, { once: true }) + request.send(file) + }) +} + +async function postJson(route: string, body: unknown, signal: AbortSignal) { + return api.fetchApi(route, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + signal + }) +} + +/** + * Sends a model file from the computer into the workspace library: asks for a + * signed upload URL, puts the bytes straight to storage, then asks the server + * to store it. Never throws. + */ +async function uploadModelFile( + file: File, + options: ModelFileUploadOptions +): Promise { + const { tags, signal } = options + try { + const startRes = await postJson( + UPLOADS_ENDPOINT, + { + filename: file.name, + size_bytes: file.size, + tags + } satisfies StartModelUploadData['body'], + signal + ) + if (!startRes.ok) return await readFailure(startRes) + const start = zStartModelUploadResponse.safeParse(await startRes.json()) + if (!start.success) { + return { ok: false, reason: 'failed', cause: start.error } + } + + const sent = await putFile(start.data, file, options) + if (sent) return sent + + const completeRes = await postJson( + `${UPLOADS_ENDPOINT}/${encodeURIComponent(start.data.upload_id)}/complete`, + { filename: file.name, tags } satisfies CompleteModelUploadData['body'], + signal + ) + if (!completeRes.ok) return await readFailure(completeRes) + const task = zCompleteModelUploadResponse.safeParse( + await completeRes.json() + ) + if (!task.success) { + return { ok: false, reason: 'failed', cause: task.error } + } + return { ok: true, taskId: task.data.task_id } + } catch (cause) { + return signal.aborted + ? { ok: false, reason: 'cancelled' } + : { ok: false, reason: 'failed', cause } + } +} + +export const modelFileUploadApi = { uploadModelFile } diff --git a/src/platform/workspace/composables/useUploadModelFile.test.ts b/src/platform/workspace/composables/useUploadModelFile.test.ts new file mode 100644 index 00000000000..5456578965d --- /dev/null +++ b/src/platform/workspace/composables/useUploadModelFile.test.ts @@ -0,0 +1,326 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createApp, nextTick } from 'vue' +import type { App } from 'vue' +import { createI18n } from 'vue-i18n' + +import { useFeatureFlags } from '@/composables/useFeatureFlags' +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import { modelFileUploadApi } from '@/platform/workspace/api/modelFileUploadApi' +import type { ModelFileUploadResult } from '@/platform/workspace/api/modelFileUploadApi' +import type { AssetDownloadWsMessage } from '@/platform/remote/comfyui/execution/types' +import { reportError } from '@comfyorg/telemetry/reportError' +import { api } from '@/scripts/api' +import { useAssetDownloadStore } from '@/stores/assetDownloadStore' + +import { useUploadModelFile } from './useUploadModelFile' + +vi.mock(import('@/scripts/api')) +vi.mock(import('@/composables/useFeatureFlags')) +vi.mock(import('@comfyorg/telemetry/reportError'), { spy: true }) + +const TASK_ID = '8c2d4e6f-1a3b-4c5d-9e7f-0a1b2c3d4e5f' +const lora = new File(['0123456789'], 'my_style_v2.safetensors') + +type UploadOptions = Parameters[1] + +const mountedApps: App[] = [] + +function setup() { + let upload: ReturnType | undefined + const app = createApp({ + setup() { + upload = useUploadModelFile() + return () => null + } + }) + app.use( + createI18n({ legacy: false, locale: 'en', messages: { en: enMessages } }) + ) + app.mount(document.createElement('div')) + mountedApps.push(app) + if (!upload) throw new Error('composable did not run') + return { upload, app } +} + +function pendingUpload() { + let options: UploadOptions | undefined + let finish!: (result: ModelFileUploadResult) => void + vi.spyOn(modelFileUploadApi, 'uploadModelFile').mockImplementation( + (_file, opts) => { + options = opts + return new Promise((resolve) => { + finish = resolve + }) + } + ) + return { + options: () => { + if (!options) throw new Error('upload not started') + return options + }, + finish: (result: ModelFileUploadResult) => finish(result) + } +} + +function taskUpdate(status: AssetDownloadWsMessage['status'], error?: string) { + const listener = vi + .mocked(api.addEventListener) + .mock.calls.findLast(([type]) => type === 'asset_download')?.[1] + if (typeof listener !== 'function') throw new Error('no download listener') + listener( + new CustomEvent('asset_download', { + detail: { + task_id: TASK_ID, + asset_id: 'asset-1', + asset_name: lora.name, + bytes_total: lora.size, + bytes_downloaded: lora.size, + progress: 1, + status, + error + } + }) + ) +} + +describe('useUploadModelFile', () => { + beforeEach(() => { + vi.mocked(reportError).mockImplementation(() => undefined) + const flags = vi.mocked(useFeatureFlags().flags) + flags.runtimeLibraryModelsEnabled = true + flags.modelUploadFolders = ['checkpoints', 'loras'] + }) + + afterEach(() => { + for (const app of mountedApps.splice(0)) app.unmount() + }) + + it('refuses a file that is not .safetensors before any upload', () => { + const { upload } = setup() + const spy = vi.spyOn(modelFileUploadApi, 'uploadModelFile') + + upload.choose(new File(['x'], 'notes.txt')) + + expect(upload.state.value).toEqual({ + status: 'idle', + error: 'Only .safetensors files can be uploaded.' + }) + upload.choose(new File(['x'], 'STYLE.SAFETENSORS')) + expect(upload.state.value.status).toBe('chosen') + expect(spy).not.toHaveBeenCalled() + }) + + it('shows bytes sent, then saving, then done once the library task finishes', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + expect(server.options().tags).toEqual(['models', 'loras']) + server.options().onProgress(4) + expect(upload.state.value).toEqual({ + status: 'uploading', + file: lora, + sent: 4 + }) + + server.finish({ ok: true, taskId: TASK_ID }) + await expect(result).resolves.toEqual({ + filename: lora.name, + modelType: 'loras', + taskId: TASK_ID, + status: 'processing' + }) + expect(upload.state.value.status).toBe('saving') + + taskUpdate('completed') + await nextTick() + expect(upload.state.value).toEqual({ + status: 'done', + file: lora, + modelType: 'loras' + }) + expect(useAssetDownloadStore().lastCompletedDownload).toMatchObject({ + taskId: TASK_ID, + modelType: 'loras' + }) + }) + + it('uploads into the folder the person picked', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + upload.modelType.value = 'checkpoints' + + void upload.upload() + + expect(server.options().tags).toEqual(['models', 'checkpoints']) + }) + + it('stops the transfer on cancel and says so, without reporting an error', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + upload.cancel() + expect(server.options().signal.aborted).toBe(true) + server.finish({ ok: false, reason: 'cancelled' }) + + await expect(result).resolves.toBeNull() + expect(upload.state.value).toEqual({ + status: 'failed', + file: lora, + error: 'Upload canceled.' + }) + expect(reportError).not.toHaveBeenCalled() + }) + + it('reports a failed upload and offers to try again', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: false, reason: 'failed', cause: 'storage 403' }) + + await expect(result).resolves.toBeNull() + expect(upload.state.value).toEqual({ + status: 'failed', + file: lora, + error: 'Upload failed. Try again.' + }) + expect(reportError).toHaveBeenCalledWith( + 'storage 403', + expect.objectContaining({ errorType: 'error_uploading_model_file' }) + ) + + const retry = pendingUpload() + void upload.upload() + expect(retry.options().tags).toEqual(['models', 'loras']) + }) + + it("shows the server's size limit when the file is too large", async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ + ok: false, + reason: 'tooLarge', + message: 'Files can be up to 2 GB.' + }) + + await result + expect(upload.state.value).toMatchObject({ + status: 'failed', + error: 'Files can be up to 2 GB.' + }) + }) + + it('asks for another type when the server cannot store the chosen one', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: false, reason: 'unsupportedType' }) + + await result + expect(upload.state.value).toMatchObject({ + status: 'failed', + error: "Cloud can't store this model type yet. Choose another type." + }) + expect(reportError).not.toHaveBeenCalled() + }) + + it('offers only the folders the server accepts, LoRA first when listed', () => { + const { upload } = setup() + + upload.choose(lora) + + expect(upload.uploadFolders.value).toEqual(['checkpoints', 'loras']) + expect(upload.modelType.value).toBe('loras') + }) + + it('starts on the first accepted folder when LoRA is not one of them', () => { + vi.mocked(useFeatureFlags().flags).modelUploadFolders = [ + 'checkpoints', + 'vae' + ] + const { upload } = setup() + + upload.choose(lora) + + expect(upload.modelType.value).toBe('checkpoints') + }) + + it('starts each newly chosen file on the default type again', () => { + const { upload } = setup() + upload.choose(lora) + upload.modelType.value = 'checkpoints' + + upload.choose(new File(['x'], 'portrait.safetensors')) + + expect(upload.modelType.value).toBe('loras') + }) + + it('is offered only while the flag is on and the server lists folders', () => { + const flags = vi.mocked(useFeatureFlags().flags) + const { upload } = setup() + expect(upload.isAvailable.value).toBe(true) + + flags.modelUploadFolders = [] + expect(upload.isAvailable.value).toBe(false) + + flags.modelUploadFolders = ['loras'] + flags.runtimeLibraryModelsEnabled = false + expect(upload.isAvailable.value).toBe(false) + }) + + it('shows why the library task failed after the bytes arrived', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: true, taskId: TASK_ID }) + await result + + taskUpdate('failed', 'Could not read the file') + await nextTick() + expect(upload.state.value).toEqual({ + status: 'failed', + file: lora, + error: 'Could not read the file' + }) + }) + + it('ignores an upload that answers after the person went back', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + const signal = server.options().signal + upload.reset() + server.finish({ ok: true, taskId: TASK_ID }) + + await expect(result).resolves.toBeNull() + expect(signal.aborted).toBe(true) + expect(upload.state.value).toEqual({ status: 'idle' }) + }) + + it('stops the transfer when the dialog closes', () => { + const { upload, app } = setup() + const server = pendingUpload() + upload.choose(lora) + + void upload.upload() + app.unmount() + mountedApps.splice(0) + + expect(server.options().signal.aborted).toBe(true) + }) +}) diff --git a/src/platform/workspace/composables/useUploadModelFile.ts b/src/platform/workspace/composables/useUploadModelFile.ts new file mode 100644 index 00000000000..125a34b0323 --- /dev/null +++ b/src/platform/workspace/composables/useUploadModelFile.ts @@ -0,0 +1,166 @@ +import { computed, onScopeDispose, ref, shallowRef, watch } from 'vue' +import type { WatchStopHandle } from 'vue' +import { useI18n } from 'vue-i18n' + +import { useFeatureFlags } from '@/composables/useFeatureFlags' +import type { UploadModelSuccess } from '@/platform/assets/composables/useUploadModelWizard' +import { modelFileUploadApi } from '@/platform/workspace/api/modelFileUploadApi' +import type { ModelFileUploadResult } from '@/platform/workspace/api/modelFileUploadApi' +import { reportError } from '@comfyorg/telemetry/reportError' +import { useAssetDownloadStore } from '@/stores/assetDownloadStore' + +const MODEL_FILE_EXTENSION = '.safetensors' +const DEFAULT_MODEL_TYPE = 'loras' + +type ModelFileUpload = + | { status: 'idle'; error?: string } + | { status: 'chosen'; file: File } + | { status: 'uploading'; file: File; sent: number } + | { status: 'saving'; file: File } + | { status: 'done'; file: File; modelType: string } + | { status: 'failed'; file: File; error: string } + +/** + * One model file chosen from the computer and uploaded into the workspace + * library: choose it, pick its type, upload with progress or cancel. + */ +export function useUploadModelFile() { + const { t } = useI18n() + const { flags } = useFeatureFlags() + const downloads = useAssetDownloadStore() + const uploadFolders = computed(() => flags.modelUploadFolders) + const isAvailable = computed( + () => flags.runtimeLibraryModelsEnabled && uploadFolders.value.length > 0 + ) + + function defaultModelType() { + return uploadFolders.value.includes(DEFAULT_MODEL_TYPE) + ? DEFAULT_MODEL_TYPE + : (uploadFolders.value[0] ?? '') + } + + const state = shallowRef({ status: 'idle' }) + const modelType = ref(defaultModelType()) + let controller: AbortController | undefined + let stopTaskWatch: WatchStopHandle | undefined + let generation = 0 + + function stop() { + generation++ + controller?.abort() + stopTaskWatch?.() + stopTaskWatch = undefined + } + + onScopeDispose(stop) + + function choose(file: File) { + if (!file.name.toLowerCase().endsWith(MODEL_FILE_EXTENSION)) { + state.value = { + status: 'idle', + error: t('assetBrowser.fileUpload.notSafetensors') + } + return + } + modelType.value = defaultModelType() + state.value = { status: 'chosen', file } + } + + function failureText(failure: Extract) { + if (failure.reason === 'cancelled') { + return t('assetBrowser.fileUpload.cancelled') + } + if (failure.reason === 'unsupportedFile') { + return t('assetBrowser.fileUpload.notSafetensors') + } + if (failure.reason === 'unsupportedType') { + return t('assetBrowser.fileUpload.unsupportedType') + } + return failure.message ?? t('assetBrowser.fileUpload.failed') + } + + function followTask(taskId: string, file: File, type: string) { + const run = generation + downloads.trackDownload(taskId, type, file.name) + state.value = { status: 'saving', file } + stopTaskWatch = watch( + () => downloads.downloadList.find((d) => d.taskId === taskId), + (download) => { + if (run !== generation || !download) return + if (download.status === 'completed') { + state.value = { status: 'done', file, modelType: type } + } else if (download.status === 'failed') { + state.value = { + status: 'failed', + file, + error: download.error || t('assetBrowser.fileUpload.failed') + } + } + }, + { immediate: true } + ) + } + + async function upload(): Promise { + const current = state.value + if (current.status !== 'chosen' && current.status !== 'failed') return null + + stop() + const run = generation + const { file } = current + const type = modelType.value + controller = new AbortController() + state.value = { status: 'uploading', file, sent: 0 } + + const result = await modelFileUploadApi.uploadModelFile(file, { + tags: ['models', type], + signal: controller.signal, + onProgress: (sent) => { + if (run === generation) + state.value = { status: 'uploading', file, sent } + } + }) + if (run !== generation) return null + + if (!result.ok) { + if (result.reason === 'failed') { + reportError(result.cause, { + surface: 'assets', + errorType: 'error_uploading_model_file', + level: 'warning' + }) + } + state.value = { status: 'failed', file, error: failureText(result) } + return null + } + + followTask(result.taskId, file, type) + return { + filename: file.name, + modelType: type, + taskId: result.taskId, + status: 'processing' + } + } + + function cancel() { + controller?.abort() + } + + function reset() { + stop() + state.value = { status: 'idle' } + modelType.value = defaultModelType() + } + + return { + isAvailable, + uploadFolders, + state, + modelType, + choose, + upload, + cancel, + reset + } +} From dbe7175e736eb81e54b060465cf354033c51daf4 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 12:22:38 -0400 Subject: [PATCH 02/10] feat(models): say when the plan blocks an upload, and start again when storage already holds the file Start and complete can answer PRIVATE_MODELS_DISABLED and PRIVATE_MODELS_UNAVAILABLE; complete can answer UNSUPPORTED_FILE_TYPE. Storage answers 412 to a second write to the same signed URL, so the upload starts again once from a new URL. Ref BE-24338 --- src/locales/en/main.json | 2 + .../workspace/api/modelFileUploadApi.test.ts | 75 ++++++++++++- .../workspace/api/modelFileUploadApi.ts | 106 +++++++++++------- .../composables/useUploadModelFile.test.ts | 28 ++++- .../composables/useUploadModelFile.ts | 24 ++-- 5 files changed, 179 insertions(+), 56 deletions(-) diff --git a/src/locales/en/main.json b/src/locales/en/main.json index bfbb80b2115..1c4dbb082a7 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -4534,6 +4534,8 @@ "cancelled": "Upload canceled.", "failed": "Upload failed. Try again.", "notSafetensors": "Only .safetensors files can be uploaded.", + "planUnavailable": "Couldn't check your plan. Try again.", + "privateModelsDisabled": "Your plan doesn't include private models.", "unsupportedType": "Cloud can't store this model type yet. Choose another type." }, "findInLibrary": "Find it in the {type} section of the models library.", diff --git a/src/platform/workspace/api/modelFileUploadApi.test.ts b/src/platform/workspace/api/modelFileUploadApi.test.ts index 17b7418db05..7542c2e93a4 100644 --- a/src/platform/workspace/api/modelFileUploadApi.test.ts +++ b/src/platform/workspace/api/modelFileUploadApi.test.ts @@ -11,7 +11,8 @@ const TASK_ID = '8c2d4e6f-1a3b-4c5d-9e7f-0a1b2c3d4e5f' const SIGNED_URL = 'https://storage.example.com/temp/model-uploads/x?sig=1' const SIGNED_HEADERS = { 'Content-Type': 'application/octet-stream', - 'x-goog-content-length-range': '0,12' + 'x-goog-content-length-range': '0,12', + 'x-goog-if-generation-match': '0' } class StorageRequest { @@ -85,9 +86,9 @@ function serverAnswers(...answers: Response[]) { return calls } -async function storageRequest() { - await vi.waitFor(() => expect(StorageRequest.sent).toHaveLength(1)) - return StorageRequest.sent[0] +async function storageRequest(nth = 1) { + await vi.waitFor(() => expect(StorageRequest.sent).toHaveLength(nth)) + return StorageRequest.sent[nth - 1] } const file = new File(['twelve bytes'], 'my_style_v2.safetensors') @@ -190,6 +191,72 @@ describe('modelFileUploadApi.uploadModelFile', () => { }) }) + it.for([ + [403, 'PRIVATE_MODELS_DISABLED', 'privateModelsDisabled'], + [503, 'PRIVATE_MODELS_UNAVAILABLE', 'planUnavailable'] + ] as const)( + 'stops before any bytes when start answers %s %s', + async ([status, code, reason]) => { + serverAnswers(json(status, { code, message: 'from the server' })) + + await expect(upload().result).resolves.toEqual({ ok: false, reason }) + expect(StorageRequest.sent).toHaveLength(0) + } + ) + + it.for([ + [403, 'PRIVATE_MODELS_DISABLED', 'privateModelsDisabled'], + [503, 'PRIVATE_MODELS_UNAVAILABLE', 'planUnavailable'], + [400, 'UNSUPPORTED_FILE_TYPE', 'unsupportedFile'] + ] as const)( + 'stops when the finish call answers %s %s', + async ([status, code, reason]) => { + serverAnswers(json(201, started), json(status, { code, message: 'x' })) + const { result } = upload() + + ;(await storageRequest()).finish(200) + + await expect(result).resolves.toEqual({ ok: false, reason }) + } + ) + + it('starts again from a new signed URL when storage already holds the object', async () => { + const second = { + ...started, + upload_id: '5a1c9e2b-7d3f-4b8a-9c6e-2f4d8b1a3c70', + upload_url: `${SIGNED_URL}&second=1` + } + const calls = serverAnswers( + json(201, started), + json(201, second), + json(202, { task_id: TASK_ID, status: 'created' }) + ) + const { result } = upload() + + ;(await storageRequest(1)).finish(412) + const retry = await storageRequest(2) + expect(retry.url).toBe(second.upload_url) + retry.finish(200) + + await expect(result).resolves.toEqual({ ok: true, taskId: TASK_ID }) + expect(calls.map((call) => call.route)).toEqual([ + '/assets/uploads', + '/assets/uploads', + '/assets/uploads/5a1c9e2b-7d3f-4b8a-9c6e-2f4d8b1a3c70/complete' + ]) + }) + + it('gives up when storage refuses the second signed URL the same way', async () => { + const calls = serverAnswers(json(201, started), json(201, started)) + const { result } = upload() + + ;(await storageRequest(1)).finish(412) + ;(await storageRequest(2)).finish(412) + + await expect(result).resolves.toMatchObject({ ok: false, reason: 'failed' }) + expect(calls).toHaveLength(2) + }) + it('fails without completing when storage refuses the bytes', async () => { const calls = serverAnswers(json(201, started)) const { result } = upload() diff --git a/src/platform/workspace/api/modelFileUploadApi.ts b/src/platform/workspace/api/modelFileUploadApi.ts index 9a97941e6a0..cfff91cae43 100644 --- a/src/platform/workspace/api/modelFileUploadApi.ts +++ b/src/platform/workspace/api/modelFileUploadApi.ts @@ -12,23 +12,36 @@ import { import { api } from '@/scripts/api' const UPLOADS_ENDPOINT = '/assets/uploads' +/** Storage refuses a second write to the signed object (`x-goog-if-generation-match: 0`). */ +const STORAGE_ALREADY_WRITTEN = 412 type ModelFileUploadFailure = | 'unsupportedFile' | 'unsupportedType' | 'tooLarge' + | 'privateModelsDisabled' + | 'planUnavailable' | 'cancelled' | 'failed' +const FAILURE_BY_CODE: Partial> = { + UNSUPPORTED_FILE_TYPE: 'unsupportedFile', + UNSUPPORTED_MODEL_TYPE: 'unsupportedType', + PRIVATE_MODELS_DISABLED: 'privateModelsDisabled', + PRIVATE_MODELS_UNAVAILABLE: 'planUnavailable' +} + +interface ModelFileUploadStopped { + ok: false + reason: ModelFileUploadFailure + message?: string + cause?: unknown +} + /** The outcome of sending one model file: the server task that stores it, or why it stopped. */ export type ModelFileUploadResult = | { ok: true; taskId: string } - | { - ok: false - reason: ModelFileUploadFailure - message?: string - cause?: unknown - } + | ModelFileUploadStopped interface ModelFileUploadOptions { tags: string[] @@ -36,17 +49,11 @@ interface ModelFileUploadOptions { onProgress: (sentBytes: number) => void } -async function readFailure( - res: Response -): Promise> { +async function readFailure(res: Response): Promise { const body = zErrorResponse.safeParse(await res.json().catch(() => null)) const code = body.success ? body.data.code : undefined - if (code === 'UNSUPPORTED_FILE_TYPE') { - return { ok: false, reason: 'unsupportedFile' } - } - if (code === 'UNSUPPORTED_MODEL_TYPE') { - return { ok: false, reason: 'unsupportedType' } - } + const reason = code ? FAILURE_BY_CODE[code] : undefined + if (reason) return { ok: false, reason } if (code === 'FILE_TOO_LARGE' && body.success) { return { ok: false, reason: 'tooLarge', message: body.data.message } } @@ -57,7 +64,7 @@ function putFile( start: Pick, file: File, { signal, onProgress }: ModelFileUploadOptions -): Promise { +): Promise { return new Promise((resolve) => { if (signal.aborted) { resolve({ ok: false, reason: 'cancelled' }) @@ -69,12 +76,7 @@ function putFile( request.setRequestHeader(name, value) } request.upload.onprogress = (event) => onProgress(event.loaded) - request.onload = () => - resolve( - request.status >= 200 && request.status < 300 - ? undefined - : { ok: false, reason: 'failed', cause: `storage ${request.status}` } - ) + request.onload = () => resolve(readStorageAnswer(request.status)) request.onerror = () => resolve({ ok: false, reason: 'failed', cause: 'storage network error' }) request.onabort = () => resolve({ ok: false, reason: 'cancelled' }) @@ -85,6 +87,14 @@ function putFile( }) } +function readStorageAnswer( + status: number +): ModelFileUploadStopped | 'alreadyWritten' | undefined { + if (status >= 200 && status < 300) return undefined + if (status === STORAGE_ALREADY_WRITTEN) return 'alreadyWritten' + return { ok: false, reason: 'failed', cause: `storage ${status}` } +} + async function postJson(route: string, body: unknown, signal: AbortSignal) { return api.fetchApi(route, { method: 'POST', @@ -94,10 +104,37 @@ async function postJson(route: string, body: unknown, signal: AbortSignal) { }) } +/** + * Asks for a signed upload URL and puts the bytes straight to storage. Answers + * the upload id, why it stopped, or `alreadyWritten` when storage already holds + * an object at that URL and the upload has to start again. + */ +async function stageFile( + file: File, + options: ModelFileUploadOptions +): Promise { + const startRes = await postJson( + UPLOADS_ENDPOINT, + { + filename: file.name, + size_bytes: file.size, + tags: options.tags + } satisfies StartModelUploadData['body'], + options.signal + ) + if (!startRes.ok) return await readFailure(startRes) + const start = zStartModelUploadResponse.safeParse(await startRes.json()) + if (!start.success) { + return { ok: false, reason: 'failed', cause: start.error } + } + return (await putFile(start.data, file, options)) ?? start.data.upload_id +} + /** * Sends a model file from the computer into the workspace library: asks for a * signed upload URL, puts the bytes straight to storage, then asks the server - * to store it. Never throws. + * to store it. A signed URL storage already wrote to is never written again: + * the upload starts once more from a new URL. Never throws. */ async function uploadModelFile( file: File, @@ -105,26 +142,15 @@ async function uploadModelFile( ): Promise { const { tags, signal } = options try { - const startRes = await postJson( - UPLOADS_ENDPOINT, - { - filename: file.name, - size_bytes: file.size, - tags - } satisfies StartModelUploadData['body'], - signal - ) - if (!startRes.ok) return await readFailure(startRes) - const start = zStartModelUploadResponse.safeParse(await startRes.json()) - if (!start.success) { - return { ok: false, reason: 'failed', cause: start.error } + let staged = await stageFile(file, options) + if (staged === 'alreadyWritten') staged = await stageFile(file, options) + if (staged === 'alreadyWritten') { + return { ok: false, reason: 'failed', cause: 'storage 412 twice' } } - - const sent = await putFile(start.data, file, options) - if (sent) return sent + if (typeof staged !== 'string') return staged const completeRes = await postJson( - `${UPLOADS_ENDPOINT}/${encodeURIComponent(start.data.upload_id)}/complete`, + `${UPLOADS_ENDPOINT}/${encodeURIComponent(staged)}/complete`, { filename: file.name, tags } satisfies CompleteModelUploadData['body'], signal ) diff --git a/src/platform/workspace/composables/useUploadModelFile.test.ts b/src/platform/workspace/composables/useUploadModelFile.test.ts index 5456578965d..d5a2ee9f4b6 100644 --- a/src/platform/workspace/composables/useUploadModelFile.test.ts +++ b/src/platform/workspace/composables/useUploadModelFile.test.ts @@ -235,6 +235,30 @@ describe('useUploadModelFile', () => { expect(reportError).not.toHaveBeenCalled() }) + it.for([ + ['privateModelsDisabled', "Your plan doesn't include private models."], + ['planUnavailable', "Couldn't check your plan. Try again."], + ['unsupportedFile', 'Only .safetensors files can be uploaded.'] + ] as const)( + 'says what the server refused when it answers %s', + async ([reason, copy]) => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: false, reason }) + + await result + expect(upload.state.value).toEqual({ + status: 'failed', + file: lora, + error: copy + }) + expect(reportError).not.toHaveBeenCalled() + } + ) + it('offers only the folders the server accepts, LoRA first when listed', () => { const { upload } = setup() @@ -288,12 +312,12 @@ describe('useUploadModelFile', () => { server.finish({ ok: true, taskId: TASK_ID }) await result - taskUpdate('failed', 'Could not read the file') + taskUpdate('failed', 'This file is not a valid .safetensors file.') await nextTick() expect(upload.state.value).toEqual({ status: 'failed', file: lora, - error: 'Could not read the file' + error: 'This file is not a valid .safetensors file.' }) }) diff --git a/src/platform/workspace/composables/useUploadModelFile.ts b/src/platform/workspace/composables/useUploadModelFile.ts index 125a34b0323..21c710542e7 100644 --- a/src/platform/workspace/composables/useUploadModelFile.ts +++ b/src/platform/workspace/composables/useUploadModelFile.ts @@ -12,6 +12,17 @@ import { useAssetDownloadStore } from '@/stores/assetDownloadStore' const MODEL_FILE_EXTENSION = '.safetensors' const DEFAULT_MODEL_TYPE = 'loras' +type UploadStopped = Extract + +/** The words for each way an upload stops; any other failure shows the server's message or the generic one. */ +const FAILURE_COPY: Partial> = { + cancelled: 'assetBrowser.fileUpload.cancelled', + unsupportedFile: 'assetBrowser.fileUpload.notSafetensors', + unsupportedType: 'assetBrowser.fileUpload.unsupportedType', + privateModelsDisabled: 'assetBrowser.fileUpload.privateModelsDisabled', + planUnavailable: 'assetBrowser.fileUpload.planUnavailable' +} + type ModelFileUpload = | { status: 'idle'; error?: string } | { status: 'chosen'; file: File } @@ -66,16 +77,9 @@ export function useUploadModelFile() { state.value = { status: 'chosen', file } } - function failureText(failure: Extract) { - if (failure.reason === 'cancelled') { - return t('assetBrowser.fileUpload.cancelled') - } - if (failure.reason === 'unsupportedFile') { - return t('assetBrowser.fileUpload.notSafetensors') - } - if (failure.reason === 'unsupportedType') { - return t('assetBrowser.fileUpload.unsupportedType') - } + function failureText(failure: UploadStopped) { + const copy = FAILURE_COPY[failure.reason] + if (copy) return t(copy) return failure.message ?? t('assetBrowser.fileUpload.failed') } From 49c54b98ef631677cc1b6723d402340408b22d13 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 12:48:51 -0400 Subject: [PATCH 03/10] test(models): hold upload requests to POST and to cancelling mid-request The fake server recorded only the route and body, so sending the start or finish call as GET, or dropping its abort signal, passed every test. It now records the method and, for an answer that never comes, rejects the request when its signal aborts, as fetch does. New cases cancel while waiting for the upload URL and while waiting for the finish answer. --- .../workspace/api/modelFileUploadApi.test.ts | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/platform/workspace/api/modelFileUploadApi.test.ts b/src/platform/workspace/api/modelFileUploadApi.test.ts index 7542c2e93a4..2819eac0c35 100644 --- a/src/platform/workspace/api/modelFileUploadApi.test.ts +++ b/src/platform/workspace/api/modelFileUploadApi.test.ts @@ -77,11 +77,22 @@ const started = { max_size_bytes: 2147483648 } -function serverAnswers(...answers: Response[]) { - const calls: Array<{ route: string; body: unknown }> = [] - vi.mocked(api.fetchApi).mockImplementation(async (route, options) => { - calls.push({ route, body: JSON.parse(String(options?.body)) }) - return answers.shift() ?? json(500, { code: 'INTERNAL', message: 'x' }) +function serverAnswers(...answers: Array) { + const calls: Array<{ route: string; method?: string; body: unknown }> = [] + vi.mocked(api.fetchApi).mockImplementation((route, options) => { + calls.push({ + route, + method: options?.method, + body: JSON.parse(String(options?.body)) + }) + const answer = + answers.shift() ?? json(500, { code: 'INTERNAL', message: 'x' }) + if (answer !== 'never') return Promise.resolve(answer) + return new Promise((_resolve, reject) => { + options?.signal?.addEventListener('abort', () => + reject(new DOMException('Aborted', 'AbortError')) + ) + }) }) return calls } @@ -130,10 +141,12 @@ describe('modelFileUploadApi.uploadModelFile', () => { expect(calls).toEqual([ { route: '/assets/uploads', + method: 'POST', body: { filename: file.name, size_bytes: file.size, tags } }, { route: `/assets/uploads/${UPLOAD_ID}/complete`, + method: 'POST', body: { filename: file.name, tags } } ]) @@ -289,6 +302,30 @@ describe('modelFileUploadApi.uploadModelFile', () => { expect(calls).toHaveLength(1) }) + it('stops waiting for the upload URL when cancelled', async () => { + serverAnswers('never') + const controller = new AbortController() + const { result } = upload(controller.signal) + await vi.waitFor(() => expect(api.fetchApi).toHaveBeenCalledOnce()) + + controller.abort() + + await expect(result).resolves.toEqual({ ok: false, reason: 'cancelled' }) + expect(StorageRequest.sent).toHaveLength(0) + }) + + it('stops waiting for the finish answer when cancelled', async () => { + serverAnswers(json(201, started), 'never') + const controller = new AbortController() + const { result } = upload(controller.signal) + ;(await storageRequest()).finish(200) + await vi.waitFor(() => expect(api.fetchApi).toHaveBeenCalledTimes(2)) + + controller.abort() + + await expect(result).resolves.toEqual({ ok: false, reason: 'cancelled' }) + }) + it('fails when the finish answer is not a task', async () => { serverAnswers(json(201, started), json(202, { status: 'created' })) const { result } = upload() From ba22f5edb5427b06e0e1d83eff8a1d1b0e029dd6 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 12:49:36 -0400 Subject: [PATCH 04/10] fix(models): settle a file upload on every library task outcome Three ways the upload dialog's state went wrong after the bytes arrived: - When the task's completion reached the page before the finish call's answer, the download store already held the task without a model type and ignored the upload's tracking. The upload showed done, but no model list refreshed. Tracking a task the store already holds now fills in its model type and, if it already completed, announces the completion. - The store marks a task the server no longer has as failed in place, without replacing the row, so the upload kept saying "saving". The upload now watches the task's status and error, not the row. - A cancelled task left the upload "saving". It now says the upload was canceled, as a cancelled transfer does. Tests: completion before the finish answer refreshes model lists; two task-not-found answers end in "The download task is no longer available."; a cancelled task says "Upload canceled."; another import's completion, listed first, leaves the upload saving. --- .../composables/useUploadModelFile.test.ts | 91 ++++++++++++++++++- .../composables/useUploadModelFile.ts | 11 ++- src/stores/assetDownloadStore.ts | 14 ++- 3 files changed, 112 insertions(+), 4 deletions(-) diff --git a/src/platform/workspace/composables/useUploadModelFile.test.ts b/src/platform/workspace/composables/useUploadModelFile.test.ts index d5a2ee9f4b6..ca6d880a8aa 100644 --- a/src/platform/workspace/composables/useUploadModelFile.test.ts +++ b/src/platform/workspace/composables/useUploadModelFile.test.ts @@ -8,6 +8,10 @@ import enMessages from '@/locales/en/main.json' with { type: 'json' } import { modelFileUploadApi } from '@/platform/workspace/api/modelFileUploadApi' import type { ModelFileUploadResult } from '@/platform/workspace/api/modelFileUploadApi' import type { AssetDownloadWsMessage } from '@/platform/remote/comfyui/execution/types' +import { + TaskNotFoundError, + taskService +} from '@/platform/tasks/services/taskService' import { reportError } from '@comfyorg/telemetry/reportError' import { api } from '@/scripts/api' import { useAssetDownloadStore } from '@/stores/assetDownloadStore' @@ -62,7 +66,11 @@ function pendingUpload() { } } -function taskUpdate(status: AssetDownloadWsMessage['status'], error?: string) { +function taskUpdate( + status: AssetDownloadWsMessage['status'], + error?: string, + taskId = TASK_ID +) { const listener = vi .mocked(api.addEventListener) .mock.calls.findLast(([type]) => type === 'asset_download')?.[1] @@ -70,7 +78,7 @@ function taskUpdate(status: AssetDownloadWsMessage['status'], error?: string) { listener( new CustomEvent('asset_download', { detail: { - task_id: TASK_ID, + task_id: taskId, asset_id: 'asset-1', asset_name: lora.name, bytes_total: lora.size, @@ -321,6 +329,85 @@ describe('useUploadModelFile', () => { }) }) + it('refreshes the model lists when the library task finishes before the upload call answers', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + taskUpdate('completed') + server.finish({ ok: true, taskId: TASK_ID }) + await result + await nextTick() + + expect(upload.state.value).toEqual({ + status: 'done', + file: lora, + modelType: 'loras' + }) + expect(useAssetDownloadStore().lastCompletedDownload).toMatchObject({ + taskId: TASK_ID, + modelType: 'loras' + }) + }) + + it('stops saving when the server no longer has the library task', async () => { + vi.useFakeTimers({ shouldAdvanceTime: false }) + vi.spyOn(taskService, 'getTask').mockResolvedValue({ + ok: false, + error: new TaskNotFoundError(TASK_ID) + }) + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: true, taskId: TASK_ID }) + await result + await vi.advanceTimersByTimeAsync(30_000) + + expect(useAssetDownloadStore().downloadList[0]?.status).toBe('failed') + expect(upload.state.value).toEqual({ + status: 'failed', + file: lora, + error: enMessages.progressToast.taskUnavailable + }) + }) + + it('says the upload was canceled when the library task is canceled', async () => { + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: true, taskId: TASK_ID }) + await result + + taskUpdate('cancelled') + await nextTick() + expect(upload.state.value).toEqual({ + status: 'failed', + file: lora, + error: 'Upload canceled.' + }) + }) + + it('keeps saving when a different import finishes', async () => { + const otherImport = '0f9e8d7c-6b5a-4c3d-8e2f-1a0b9c8d7e6f' + const { upload } = setup() + taskUpdate('running', undefined, otherImport) + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: true, taskId: TASK_ID }) + await result + + taskUpdate('completed', undefined, otherImport) + await nextTick() + expect(upload.state.value).toEqual({ status: 'saving', file: lora }) + }) + it('ignores an upload that answers after the person went back', async () => { const { upload } = setup() const server = pendingUpload() diff --git a/src/platform/workspace/composables/useUploadModelFile.ts b/src/platform/workspace/composables/useUploadModelFile.ts index 21c710542e7..539154c78d9 100644 --- a/src/platform/workspace/composables/useUploadModelFile.ts +++ b/src/platform/workspace/composables/useUploadModelFile.ts @@ -88,7 +88,10 @@ export function useUploadModelFile() { downloads.trackDownload(taskId, type, file.name) state.value = { status: 'saving', file } stopTaskWatch = watch( - () => downloads.downloadList.find((d) => d.taskId === taskId), + () => { + const download = downloads.downloadList.find((d) => d.taskId === taskId) + return download && { status: download.status, error: download.error } + }, (download) => { if (run !== generation || !download) return if (download.status === 'completed') { @@ -99,6 +102,12 @@ export function useUploadModelFile() { file, error: download.error || t('assetBrowser.fileUpload.failed') } + } else if (download.status === 'cancelled') { + state.value = { + status: 'failed', + file, + error: t('assetBrowser.fileUpload.cancelled') + } } }, { immediate: true } diff --git a/src/stores/assetDownloadStore.ts b/src/stores/assetDownloadStore.ts index 7060cba5bea..716409b6a95 100644 --- a/src/stores/assetDownloadStore.ts +++ b/src/stores/assetDownloadStore.ts @@ -309,7 +309,19 @@ export const useAssetDownloadStore = defineStore('assetDownload', () => { } function trackDownload(taskId: TaskId, modelType: string, assetName: string) { - if (downloads.value.has(taskId)) return + const existing = downloads.value.get(taskId) + if (existing) { + if (existing.modelType) return + existing.modelType = modelType + if (existing.status === 'completed') { + lastCompletedDownload.value = { + taskId, + modelType, + timestamp: Date.now() + } + } + return + } dismissedPendingDownloads.delete(taskId) taskNotFoundAttempts.delete(taskId) From ea8f41ac81e6f7a171b7e676eb5de9064468bc5d Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 13:46:41 -0400 Subject: [PATCH 05/10] fix(models): stop saving on every cancellation status of the library task A cancel the server never confirmed (cancellation_unconfirmed) left the upload saving, and still saving after the row was dismissed. Every cancellation status now says the upload was canceled, and the upload is done if the task completes later, including after its row is dismissed (the store still reports that completion). --- .../composables/useUploadModelFile.test.ts | 48 +++++++++++++++ .../composables/useUploadModelFile.ts | 59 ++++++++++++------- 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/src/platform/workspace/composables/useUploadModelFile.test.ts b/src/platform/workspace/composables/useUploadModelFile.test.ts index ca6d880a8aa..6e472bf81de 100644 --- a/src/platform/workspace/composables/useUploadModelFile.test.ts +++ b/src/platform/workspace/composables/useUploadModelFile.test.ts @@ -392,6 +392,54 @@ describe('useUploadModelFile', () => { }) }) + it('says the upload was canceled when the cancel is never confirmed, and finishes if the task completes after its row is dismissed', async () => { + vi.useFakeTimers({ shouldAdvanceTime: false }) + vi.spyOn(taskService, 'cancelTask').mockResolvedValue({ + ok: true, + value: 'cancelling' + }) + vi.spyOn(taskService, 'getTask').mockResolvedValue({ + ok: true, + value: { + id: TASK_ID, + idempotency_key: 'upload-1', + task_name: 'task:download_file', + payload: {}, + status: 'running', + create_time: new Date().toISOString(), + update_time: new Date().toISOString() + } + }) + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + server.finish({ ok: true, taskId: TASK_ID }) + await result + taskUpdate('running') + + const downloads = useAssetDownloadStore() + await downloads.cancelDownload(TASK_ID) + await vi.advanceTimersByTimeAsync(120_000) + expect(downloads.downloadList[0]?.status).toBe('cancellation_unconfirmed') + expect(upload.state.value).toEqual({ + status: 'failed', + file: lora, + error: 'Upload canceled.' + }) + + downloads.clearDismissibleDownloads() + expect(downloads.downloadList).toEqual([]) + taskUpdate('completed') + await nextTick() + expect(upload.state.value).toEqual({ + status: 'done', + file: lora, + modelType: 'loras' + }) + }) + it('keeps saving when a different import finishes', async () => { const otherImport = '0f9e8d7c-6b5a-4c3d-8e2f-1a0b9c8d7e6f' const { upload } = setup() diff --git a/src/platform/workspace/composables/useUploadModelFile.ts b/src/platform/workspace/composables/useUploadModelFile.ts index 539154c78d9..589e8d03895 100644 --- a/src/platform/workspace/composables/useUploadModelFile.ts +++ b/src/platform/workspace/composables/useUploadModelFile.ts @@ -7,7 +7,10 @@ import type { UploadModelSuccess } from '@/platform/assets/composables/useUpload import { modelFileUploadApi } from '@/platform/workspace/api/modelFileUploadApi' import type { ModelFileUploadResult } from '@/platform/workspace/api/modelFileUploadApi' import { reportError } from '@comfyorg/telemetry/reportError' -import { useAssetDownloadStore } from '@/stores/assetDownloadStore' +import { + isDownloadCancelled, + useAssetDownloadStore +} from '@/stores/assetDownloadStore' const MODEL_FILE_EXTENSION = '.safetensors' const DEFAULT_MODEL_TYPE = 'loras' @@ -83,32 +86,44 @@ export function useUploadModelFile() { return failure.message ?? t('assetBrowser.fileUpload.failed') } + function taskOutcome( + taskId: string, + file: File, + type: string + ): ModelFileUpload | undefined { + if (downloads.lastCompletedDownload?.taskId === taskId) { + return { status: 'done', file, modelType: type } + } + const download = downloads.downloadList.find((d) => d.taskId === taskId) + if (!download) return undefined + if (download.status === 'completed') { + return { status: 'done', file, modelType: type } + } + if (download.status === 'failed') { + return { + status: 'failed', + file, + error: download.error || t('assetBrowser.fileUpload.failed') + } + } + if (isDownloadCancelled(download.status)) { + return { + status: 'failed', + file, + error: t('assetBrowser.fileUpload.cancelled') + } + } + return undefined + } + function followTask(taskId: string, file: File, type: string) { const run = generation downloads.trackDownload(taskId, type, file.name) state.value = { status: 'saving', file } stopTaskWatch = watch( - () => { - const download = downloads.downloadList.find((d) => d.taskId === taskId) - return download && { status: download.status, error: download.error } - }, - (download) => { - if (run !== generation || !download) return - if (download.status === 'completed') { - state.value = { status: 'done', file, modelType: type } - } else if (download.status === 'failed') { - state.value = { - status: 'failed', - file, - error: download.error || t('assetBrowser.fileUpload.failed') - } - } else if (download.status === 'cancelled') { - state.value = { - status: 'failed', - file, - error: t('assetBrowser.fileUpload.cancelled') - } - } + () => taskOutcome(taskId, file, type), + (outcome) => { + if (run === generation && outcome) state.value = outcome }, { immediate: true } ) From 8aef9f9d887295dfa9e5e33c9c72f6a77c9be417 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 17:38:11 -0400 Subject: [PATCH 06/10] fix(models): stop the earlier upload when another file is chosen choose() now stops the transfer and the library task watch of the previous file, so neither can overwrite the newly chosen one. Also drops the comment above FAILURE_COPY, and the composable tests finish and await every mocked upload they start. --- .../composables/useUploadModelFile.test.ts | 46 +++++++++++++++++-- .../composables/useUploadModelFile.ts | 2 +- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/platform/workspace/composables/useUploadModelFile.test.ts b/src/platform/workspace/composables/useUploadModelFile.test.ts index 6e472bf81de..f349290a522 100644 --- a/src/platform/workspace/composables/useUploadModelFile.test.ts +++ b/src/platform/workspace/composables/useUploadModelFile.test.ts @@ -160,9 +160,11 @@ describe('useUploadModelFile', () => { upload.choose(lora) upload.modelType.value = 'checkpoints' - void upload.upload() + const result = upload.upload() expect(server.options().tags).toEqual(['models', 'checkpoints']) + server.finish({ ok: true, taskId: TASK_ID }) + await expect(result).resolves.toMatchObject({ modelType: 'checkpoints' }) }) it('stops the transfer on cancel and says so, without reporting an error', async () => { @@ -204,8 +206,10 @@ describe('useUploadModelFile', () => { ) const retry = pendingUpload() - void upload.upload() + const retried = upload.upload() expect(retry.options().tags).toEqual(['models', 'loras']) + retry.finish({ ok: true, taskId: TASK_ID }) + await expect(retried).resolves.toMatchObject({ taskId: TASK_ID }) }) it("shows the server's size limit when the file is too large", async () => { @@ -456,6 +460,38 @@ describe('useUploadModelFile', () => { expect(upload.state.value).toEqual({ status: 'saving', file: lora }) }) + it('stops the earlier transfer when another file is chosen', async () => { + const otherFile = new File(['01234'], 'teammate_style.safetensors') + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + + const result = upload.upload() + upload.choose(otherFile) + server.options().onProgress(4) + server.finish({ ok: true, taskId: TASK_ID }) + + await expect(result).resolves.toBeNull() + expect(server.options().signal.aborted).toBe(true) + expect(upload.state.value).toEqual({ status: 'chosen', file: otherFile }) + }) + + it('stops following the earlier library task when another file is chosen', async () => { + const otherFile = new File(['01234'], 'teammate_style.safetensors') + const { upload } = setup() + const server = pendingUpload() + upload.choose(lora) + const result = upload.upload() + server.finish({ ok: true, taskId: TASK_ID }) + await result + + upload.choose(otherFile) + taskUpdate('completed') + await nextTick() + + expect(upload.state.value).toEqual({ status: 'chosen', file: otherFile }) + }) + it('ignores an upload that answers after the person went back', async () => { const { upload } = setup() const server = pendingUpload() @@ -471,15 +507,17 @@ describe('useUploadModelFile', () => { expect(upload.state.value).toEqual({ status: 'idle' }) }) - it('stops the transfer when the dialog closes', () => { + it('stops the transfer when the dialog closes', async () => { const { upload, app } = setup() const server = pendingUpload() upload.choose(lora) - void upload.upload() + const result = upload.upload() app.unmount() mountedApps.splice(0) expect(server.options().signal.aborted).toBe(true) + server.finish({ ok: false, reason: 'cancelled' }) + await expect(result).resolves.toBeNull() }) }) diff --git a/src/platform/workspace/composables/useUploadModelFile.ts b/src/platform/workspace/composables/useUploadModelFile.ts index 589e8d03895..610deac7a51 100644 --- a/src/platform/workspace/composables/useUploadModelFile.ts +++ b/src/platform/workspace/composables/useUploadModelFile.ts @@ -17,7 +17,6 @@ const DEFAULT_MODEL_TYPE = 'loras' type UploadStopped = Extract -/** The words for each way an upload stops; any other failure shows the server's message or the generic one. */ const FAILURE_COPY: Partial> = { cancelled: 'assetBrowser.fileUpload.cancelled', unsupportedFile: 'assetBrowser.fileUpload.notSafetensors', @@ -69,6 +68,7 @@ export function useUploadModelFile() { onScopeDispose(stop) function choose(file: File) { + stop() if (!file.name.toLowerCase().endsWith(MODEL_FILE_EXTENSION)) { state.value = { status: 'idle', From 277a281e1279081ba1870399e44901f3b672c3af Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 17:38:24 -0400 Subject: [PATCH 07/10] refactor(models): drop comments from the upload API, one table for start refusals Comments on module-private code are removed; the exported object keeps a one-line JSDoc, and the 412 constant is named for the status. The start-call refusal tests share one it.for table. --- .../workspace/api/modelFileUploadApi.test.ts | 91 +++++++++---------- .../workspace/api/modelFileUploadApi.ts | 17 +--- 2 files changed, 46 insertions(+), 62 deletions(-) diff --git a/src/platform/workspace/api/modelFileUploadApi.test.ts b/src/platform/workspace/api/modelFileUploadApi.test.ts index 2819eac0c35..e748c71387d 100644 --- a/src/platform/workspace/api/modelFileUploadApi.test.ts +++ b/src/platform/workspace/api/modelFileUploadApi.test.ts @@ -152,42 +152,50 @@ describe('modelFileUploadApi.uploadModelFile', () => { ]) }) - it('says the file type is refused when the server refuses it', async () => { - serverAnswers( - json(400, { code: 'UNSUPPORTED_FILE_TYPE', message: 'not safetensors' }) - ) - - await expect(upload().result).resolves.toEqual({ - ok: false, - reason: 'unsupportedFile' - }) - expect(StorageRequest.sent).toHaveLength(0) - }) - - it("passes on the server's size limit message", async () => { - serverAnswers( - json(400, { code: 'FILE_TOO_LARGE', message: 'Files can be up to 2 GB.' }) - ) - - await expect(upload().result).resolves.toEqual({ - ok: false, - reason: 'tooLarge', - message: 'Files can be up to 2 GB.' - }) - expect(StorageRequest.sent).toHaveLength(0) - }) - - it('says the model type is refused when the server cannot store that folder', async () => { - serverAnswers( - json(400, { code: 'UNSUPPORTED_MODEL_TYPE', message: 'not stored' }) - ) + it.for([ + { + refusal: 'the file type', + status: 400, + body: { code: 'UNSUPPORTED_FILE_TYPE', message: 'not safetensors' }, + expected: { ok: false, reason: 'unsupportedFile' } + }, + { + refusal: 'the file size, with its limit message', + status: 400, + body: { code: 'FILE_TOO_LARGE', message: 'Files can be up to 2 GB.' }, + expected: { + ok: false, + reason: 'tooLarge', + message: 'Files can be up to 2 GB.' + } + }, + { + refusal: 'the model folder', + status: 400, + body: { code: 'UNSUPPORTED_MODEL_TYPE', message: 'not stored' }, + expected: { ok: false, reason: 'unsupportedType' } + }, + { + refusal: 'a plan without private models', + status: 403, + body: { code: 'PRIVATE_MODELS_DISABLED', message: 'from the server' }, + expected: { ok: false, reason: 'privateModelsDisabled' } + }, + { + refusal: 'a plan it could not read', + status: 503, + body: { code: 'PRIVATE_MODELS_UNAVAILABLE', message: 'from the server' }, + expected: { ok: false, reason: 'planUnavailable' } + } + ])( + 'sends no bytes when the start call refuses $refusal', + async ({ status, body, expected }) => { + serverAnswers(json(status, body)) - await expect(upload().result).resolves.toEqual({ - ok: false, - reason: 'unsupportedType' - }) - expect(StorageRequest.sent).toHaveLength(0) - }) + await expect(upload().result).resolves.toEqual(expected) + expect(StorageRequest.sent).toHaveLength(0) + } + ) it('says the model type is refused when the finish call refuses it', async () => { serverAnswers( @@ -204,19 +212,6 @@ describe('modelFileUploadApi.uploadModelFile', () => { }) }) - it.for([ - [403, 'PRIVATE_MODELS_DISABLED', 'privateModelsDisabled'], - [503, 'PRIVATE_MODELS_UNAVAILABLE', 'planUnavailable'] - ] as const)( - 'stops before any bytes when start answers %s %s', - async ([status, code, reason]) => { - serverAnswers(json(status, { code, message: 'from the server' })) - - await expect(upload().result).resolves.toEqual({ ok: false, reason }) - expect(StorageRequest.sent).toHaveLength(0) - } - ) - it.for([ [403, 'PRIVATE_MODELS_DISABLED', 'privateModelsDisabled'], [503, 'PRIVATE_MODELS_UNAVAILABLE', 'planUnavailable'], diff --git a/src/platform/workspace/api/modelFileUploadApi.ts b/src/platform/workspace/api/modelFileUploadApi.ts index cfff91cae43..4e24755f9a7 100644 --- a/src/platform/workspace/api/modelFileUploadApi.ts +++ b/src/platform/workspace/api/modelFileUploadApi.ts @@ -12,8 +12,7 @@ import { import { api } from '@/scripts/api' const UPLOADS_ENDPOINT = '/assets/uploads' -/** Storage refuses a second write to the signed object (`x-goog-if-generation-match: 0`). */ -const STORAGE_ALREADY_WRITTEN = 412 +const STORAGE_PRECONDITION_FAILED = 412 type ModelFileUploadFailure = | 'unsupportedFile' @@ -91,7 +90,7 @@ function readStorageAnswer( status: number ): ModelFileUploadStopped | 'alreadyWritten' | undefined { if (status >= 200 && status < 300) return undefined - if (status === STORAGE_ALREADY_WRITTEN) return 'alreadyWritten' + if (status === STORAGE_PRECONDITION_FAILED) return 'alreadyWritten' return { ok: false, reason: 'failed', cause: `storage ${status}` } } @@ -104,11 +103,6 @@ async function postJson(route: string, body: unknown, signal: AbortSignal) { }) } -/** - * Asks for a signed upload URL and puts the bytes straight to storage. Answers - * the upload id, why it stopped, or `alreadyWritten` when storage already holds - * an object at that URL and the upload has to start again. - */ async function stageFile( file: File, options: ModelFileUploadOptions @@ -130,12 +124,6 @@ async function stageFile( return (await putFile(start.data, file, options)) ?? start.data.upload_id } -/** - * Sends a model file from the computer into the workspace library: asks for a - * signed upload URL, puts the bytes straight to storage, then asks the server - * to store it. A signed URL storage already wrote to is never written again: - * the upload starts once more from a new URL. Never throws. - */ async function uploadModelFile( file: File, options: ModelFileUploadOptions @@ -169,4 +157,5 @@ async function uploadModelFile( } } +/** Sends a model file from the computer into the workspace library; never throws. */ export const modelFileUploadApi = { uploadModelFile } From ee11b8d04d2ac4f5a0e64df3e52ec1c88a8ca56a Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 18:52:33 -0400 Subject: [PATCH 08/10] fix(models): offer the upload exactly while the server lists upload folders The features contract sends model_upload_folders only while runtime_library_models_enabled is on for the caller, so the folder list alone is the server's answer (ADR-API-SERVER-FACTS-0042). --- .../workspace/composables/useUploadModelFile.test.ts | 8 ++------ src/platform/workspace/composables/useUploadModelFile.ts | 4 +--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/platform/workspace/composables/useUploadModelFile.test.ts b/src/platform/workspace/composables/useUploadModelFile.test.ts index f349290a522..07c4c97f631 100644 --- a/src/platform/workspace/composables/useUploadModelFile.test.ts +++ b/src/platform/workspace/composables/useUploadModelFile.test.ts @@ -95,7 +95,6 @@ describe('useUploadModelFile', () => { beforeEach(() => { vi.mocked(reportError).mockImplementation(() => undefined) const flags = vi.mocked(useFeatureFlags().flags) - flags.runtimeLibraryModelsEnabled = true flags.modelUploadFolders = ['checkpoints', 'loras'] }) @@ -302,17 +301,14 @@ describe('useUploadModelFile', () => { expect(upload.modelType.value).toBe('loras') }) - it('is offered only while the flag is on and the server lists folders', () => { + it('is offered exactly while the server lists upload folders', () => { const flags = vi.mocked(useFeatureFlags().flags) + flags.runtimeLibraryModelsEnabled = false const { upload } = setup() expect(upload.isAvailable.value).toBe(true) flags.modelUploadFolders = [] expect(upload.isAvailable.value).toBe(false) - - flags.modelUploadFolders = ['loras'] - flags.runtimeLibraryModelsEnabled = false - expect(upload.isAvailable.value).toBe(false) }) it('shows why the library task failed after the bytes arrived', async () => { diff --git a/src/platform/workspace/composables/useUploadModelFile.ts b/src/platform/workspace/composables/useUploadModelFile.ts index 610deac7a51..fee9be1d124 100644 --- a/src/platform/workspace/composables/useUploadModelFile.ts +++ b/src/platform/workspace/composables/useUploadModelFile.ts @@ -42,9 +42,7 @@ export function useUploadModelFile() { const { flags } = useFeatureFlags() const downloads = useAssetDownloadStore() const uploadFolders = computed(() => flags.modelUploadFolders) - const isAvailable = computed( - () => flags.runtimeLibraryModelsEnabled && uploadFolders.value.length > 0 - ) + const isAvailable = computed(() => uploadFolders.value.length > 0) function defaultModelType() { return uploadFolders.value.includes(DEFAULT_MODEL_TYPE) From 6a713f9028660c62b2450ab02f2c720caac1484d Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 18:53:54 -0400 Subject: [PATCH 09/10] chore(ingest-types): re-sync the model upload blocks to the current ingest spec Regenerated with @hey-api/openapi-ts 0.97.3 (this package's config) from Comfy-Org/cloud services/ingest/openapi.yaml at 3d15d828875 and copied the model upload blocks: 403 PRIVATE_MODELS_DISABLED and 503 PRIVATE_MODELS_UNAVAILABLE on both routes, UNSUPPORTED_FILE_TYPE on /complete, and x-goog-if-generation-match: 0 in ModelUploadStarted.headers. The zod blocks and the features field were already the same. --- packages/ingest-types/src/types.gen.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/ingest-types/src/types.gen.ts b/packages/ingest-types/src/types.gen.ts index a5d78117582..50af5b69eae 100644 --- a/packages/ingest-types/src/types.gen.ts +++ b/packages/ingest-types/src/types.gen.ts @@ -487,7 +487,7 @@ export type ModelUploadStarted = { upload_url: string method: 'PUT' /** - * Every header the PUT must send, with exactly these values (`Content-Type` and `x-goog-content-length-range`). + * Every header the PUT must send, with exactly these values (`Content-Type`, `x-goog-content-length-range` and `x-goog-if-generation-match` set to `0`, which makes storage refuse a second write to the object). */ headers: { [key: string]: string @@ -10337,7 +10337,7 @@ export type StartModelUploadData = { export type StartModelUploadErrors = { /** - * `INVALID_BODY` (bad filename, size or tag list), `UNSUPPORTED_FILE_TYPE` (not `.safetensors`), `UNSUPPORTED_MODEL_TYPE` (`models` plus a folder not in `model_upload_folders` of GET /api/features) or `FILE_TOO_LARGE` (over 2 GiB; the message is "Files can be up to 2 GB.") + * `INVALID_BODY` (bad filename, size or tag list), `UNSUPPORTED_FILE_TYPE` (not `.safetensors`: a pickle format such as `.ckpt`, `.pt` or `.bin` can run code when loaded), `UNSUPPORTED_MODEL_TYPE` (`models` plus a folder not in `model_upload_folders` of GET /api/features) or `FILE_TOO_LARGE` (over 2 GiB; the message is "Files can be up to 2 GB.") */ 400: ErrorResponse /** @@ -10345,13 +10345,17 @@ export type StartModelUploadErrors = { */ 401: ErrorResponse /** - * `FEATURE_DISABLED` when `runtime_library_models_enabled` is off for this workspace, or a refused session write (see `SessionWriteForbidden`) + * `FEATURE_DISABLED` when `runtime_library_models_enabled` is off for this workspace, `PRIVATE_MODELS_DISABLED` when the workspace's plan does not include private models (the link import's check), or a refused session write (see `SessionWriteForbidden`) */ 403: ErrorResponse /** * Internal server error */ 500: ErrorResponse + /** + * Private-model eligibility is temporarily unavailable (`PRIVATE_MODELS_UNAVAILABLE`); retry without inferring plan ineligibility. + */ + 503: ErrorResponse } export type StartModelUploadError = @@ -10390,7 +10394,7 @@ export type CompleteModelUploadData = { export type CompleteModelUploadErrors = { /** - * `INVALID_BODY` (bad filename or tag list), `UNSUPPORTED_MODEL_TYPE` (a folder not in `model_upload_folders`) or `FILE_TOO_LARGE` (the stored object is over 2 GiB; the message is "Files can be up to 2 GB.") + * `INVALID_BODY` (bad filename or tag list), `UNSUPPORTED_FILE_TYPE` (not `.safetensors`), `UNSUPPORTED_MODEL_TYPE` (a folder not in `model_upload_folders`) or `FILE_TOO_LARGE` (the stored object is over 2 GiB; the message is "Files can be up to 2 GB.") */ 400: ErrorResponse /** @@ -10398,7 +10402,7 @@ export type CompleteModelUploadErrors = { */ 401: ErrorResponse /** - * `FEATURE_DISABLED` when `runtime_library_models_enabled` is off for this workspace, or a refused session write (see `SessionWriteForbidden`) + * `FEATURE_DISABLED` when `runtime_library_models_enabled` is off for this workspace, `PRIVATE_MODELS_DISABLED` when the workspace's plan does not include private models (the link import's check), or a refused session write (see `SessionWriteForbidden`) */ 403: ErrorResponse /** @@ -10409,6 +10413,10 @@ export type CompleteModelUploadErrors = { * Internal server error */ 500: ErrorResponse + /** + * Private-model eligibility is temporarily unavailable (`PRIVATE_MODELS_UNAVAILABLE`); retry without inferring plan ineligibility. + */ + 503: ErrorResponse } export type CompleteModelUploadError = From 56eb3ebc1d5bc8a2bdd4cf64adfb30cf561d9c05 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 29 Oct 2026 18:55:18 -0400 Subject: [PATCH 10/10] fix(models): finish the upload when storage already holds its object The signed URL writes one staged object per fresh upload_id with x-goog-if-generation-match: 0, and a GCS object exists only after a whole PUT succeeds. A 412 on the PUT therefore means this upload's bytes already landed (for example a replayed PUT whose first answer was lost), so the client goes on to /complete with the same upload_id; the finish task still checks the size and that the bytes open as safetensors. Starting over re-sent up to 2 GB and left the first staged object behind. --- .../workspace/api/modelFileUploadApi.test.ts | 28 +++---------------- .../workspace/api/modelFileUploadApi.ts | 18 ++++-------- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/src/platform/workspace/api/modelFileUploadApi.test.ts b/src/platform/workspace/api/modelFileUploadApi.test.ts index e748c71387d..4af33a6857d 100644 --- a/src/platform/workspace/api/modelFileUploadApi.test.ts +++ b/src/platform/workspace/api/modelFileUploadApi.test.ts @@ -228,43 +228,23 @@ describe('modelFileUploadApi.uploadModelFile', () => { } ) - it('starts again from a new signed URL when storage already holds the object', async () => { - const second = { - ...started, - upload_id: '5a1c9e2b-7d3f-4b8a-9c6e-2f4d8b1a3c70', - upload_url: `${SIGNED_URL}&second=1` - } + it('finishes the same upload when storage already holds its object', async () => { const calls = serverAnswers( json(201, started), - json(201, second), json(202, { task_id: TASK_ID, status: 'created' }) ) const { result } = upload() - ;(await storageRequest(1)).finish(412) - const retry = await storageRequest(2) - expect(retry.url).toBe(second.upload_url) - retry.finish(200) + ;(await storageRequest()).finish(412) await expect(result).resolves.toEqual({ ok: true, taskId: TASK_ID }) + expect(StorageRequest.sent).toHaveLength(1) expect(calls.map((call) => call.route)).toEqual([ '/assets/uploads', - '/assets/uploads', - '/assets/uploads/5a1c9e2b-7d3f-4b8a-9c6e-2f4d8b1a3c70/complete' + `/assets/uploads/${UPLOAD_ID}/complete` ]) }) - it('gives up when storage refuses the second signed URL the same way', async () => { - const calls = serverAnswers(json(201, started), json(201, started)) - const { result } = upload() - - ;(await storageRequest(1)).finish(412) - ;(await storageRequest(2)).finish(412) - - await expect(result).resolves.toMatchObject({ ok: false, reason: 'failed' }) - expect(calls).toHaveLength(2) - }) - it('fails without completing when storage refuses the bytes', async () => { const calls = serverAnswers(json(201, started)) const { result } = upload() diff --git a/src/platform/workspace/api/modelFileUploadApi.ts b/src/platform/workspace/api/modelFileUploadApi.ts index 4e24755f9a7..5b6f4adb815 100644 --- a/src/platform/workspace/api/modelFileUploadApi.ts +++ b/src/platform/workspace/api/modelFileUploadApi.ts @@ -12,7 +12,7 @@ import { import { api } from '@/scripts/api' const UPLOADS_ENDPOINT = '/assets/uploads' -const STORAGE_PRECONDITION_FAILED = 412 +const STORAGE_OBJECT_ALREADY_WRITTEN = 412 type ModelFileUploadFailure = | 'unsupportedFile' @@ -63,7 +63,7 @@ function putFile( start: Pick, file: File, { signal, onProgress }: ModelFileUploadOptions -): Promise { +): Promise { return new Promise((resolve) => { if (signal.aborted) { resolve({ ok: false, reason: 'cancelled' }) @@ -86,11 +86,9 @@ function putFile( }) } -function readStorageAnswer( - status: number -): ModelFileUploadStopped | 'alreadyWritten' | undefined { +function readStorageAnswer(status: number): ModelFileUploadStopped | undefined { if (status >= 200 && status < 300) return undefined - if (status === STORAGE_PRECONDITION_FAILED) return 'alreadyWritten' + if (status === STORAGE_OBJECT_ALREADY_WRITTEN) return undefined return { ok: false, reason: 'failed', cause: `storage ${status}` } } @@ -106,7 +104,7 @@ async function postJson(route: string, body: unknown, signal: AbortSignal) { async function stageFile( file: File, options: ModelFileUploadOptions -): Promise { +): Promise { const startRes = await postJson( UPLOADS_ENDPOINT, { @@ -130,11 +128,7 @@ async function uploadModelFile( ): Promise { const { tags, signal } = options try { - let staged = await stageFile(file, options) - if (staged === 'alreadyWritten') staged = await stageFile(file, options) - if (staged === 'alreadyWritten') { - return { ok: false, reason: 'failed', cause: 'storage 412 twice' } - } + const staged = await stageFile(file, options) if (typeof staged !== 'string') return staged const completeRes = await postJson(