diff --git a/src/platform/assets/components/modelInfo/ModelInfoPanel.vue b/src/platform/assets/components/modelInfo/ModelInfoPanel.vue index 5a3b668b7f0..88865b1f8a7 100644 --- a/src/platform/assets/components/modelInfo/ModelInfoPanel.vue +++ b/src/platform/assets/components/modelInfo/ModelInfoPanel.vue @@ -215,6 +215,7 @@ import { useI18n } from 'vue-i18n' import EditableText from '@/components/common/EditableText.vue' import { useCopyToClipboard } from '@/composables/useCopyToClipboard' +import { useFeatureFlags } from '@/composables/useFeatureFlags' import PropertiesAccordionItem from '@/components/rightSidePanel/layout/PropertiesAccordionItem.vue' import Button from '@/components/ui/button/Button.vue' import Select from '@/components/ui/select/Select.vue' @@ -231,15 +232,16 @@ import type { AssetDisplayItem } from '@/platform/assets/composables/useAssetBro import { useModelTypes } from '@/platform/assets/composables/useModelTypes' import type { AssetUserMetadata } from '@/platform/assets/schemas/assetSchema' import { + buildModelTypeTagUpdate, getAssetAdditionalTags, getAssetBaseModels, getAssetDescription, getAssetDisplayName, getAssetFilename, - getAssetModelType, getAssetSourceUrl, getAssetTriggerPhrases, getAssetUserDescription, + getEditableModelType, getSourceName } from '@/platform/assets/utils/assetMetadataUtils' import { useAssetsStore } from '@/stores/assetsStore' @@ -265,6 +267,7 @@ const { asset, cacheKey, selectContentStyle } = defineProps<{ }>() const assetsStore = useAssetsStore() +const { flags } = useFeatureFlags() const { modelTypes } = useModelTypes() const pendingUpdates = ref({}) @@ -319,11 +322,16 @@ function handleDisplayNameEdit(newName: string) { const debouncedSaveModelType = useDebounceFn((newModelType: string) => { if (isImmutable.value) return - const currentModelType = getAssetModelType(asset) + const currentModelType = getEditableModelType( + asset, + flags.supportsModelTypeTags + ) if (currentModelType === newModelType) return - const newTags = asset.tags - .filter((tag) => tag !== currentModelType) - .concat(newModelType) + const newTags = buildModelTypeTagUpdate( + asset, + newModelType, + flags.supportsModelTypeTags + ) assetsStore.updateAssetTags(asset, newTags, cacheKey) }, 500) @@ -345,7 +353,10 @@ const userDescription = computed({ }) const selectedModelType = computed({ - get: () => pendingModelType.value ?? getAssetModelType(asset) ?? undefined, + get: () => + pendingModelType.value ?? + getEditableModelType(asset, flags.supportsModelTypeTags) ?? + undefined, set: (value: string | undefined) => { if (!value) return pendingModelType.value = value diff --git a/src/platform/assets/composables/useUploadModelWizard.test.ts b/src/platform/assets/composables/useUploadModelWizard.test.ts index 9cf769e4a5a..d1129580e1e 100644 --- a/src/platform/assets/composables/useUploadModelWizard.test.ts +++ b/src/platform/assets/composables/useUploadModelWizard.test.ts @@ -7,6 +7,7 @@ import { createI18n } from 'vue-i18n' import enMessages from '@/locales/en/main.json' with { type: 'json' } import type { AsyncUploadResponse } from '@/platform/assets/schemas/assetSchema' +import { api } from '@/scripts/api' import { useUploadModelWizard } from './useUploadModelWizard' @@ -38,7 +39,10 @@ vi.mock('@/scripts/api', () => ({ api: { fetchApi: vi.fn(), addEventListener: vi.fn(), - apiURL: vi.fn((path: string) => path) + apiURL: vi.fn((path: string) => path), + getServerFeature: vi.fn( + (_name: string, defaultValue?: unknown) => defaultValue + ) } })) @@ -90,6 +94,7 @@ describe('useUploadModelWizard', () => { }) afterEach(() => { + vi.mocked(api.getServerFeature).mockReset() for (const app of mountedApps.splice(0)) { app.unmount() } @@ -135,7 +140,6 @@ describe('useUploadModelWizard', () => { status: 'completed' as const } const event = new CustomEvent('asset_download', { detail }) - const { api } = await import('@/scripts/api') const handler = vi .mocked(api.addEventListener) .mock.calls.find((c) => c[0] === 'asset_download')?.[1] as @@ -171,7 +175,6 @@ describe('useUploadModelWizard', () => { expect(wizard.uploadStatus.value).toBe('processing') // Simulate WebSocket: download fails - const { api } = await import('@/scripts/api') const handler = vi .mocked(api.addEventListener) .mock.calls.find((c) => c[0] === 'asset_download')?.[1] as @@ -279,6 +282,35 @@ describe('useUploadModelWizard', () => { expect(result?.modelType).toBe('checkpoints') }) + it('namespaces the tag but keeps user_metadata.model_type bare when the backend supports it', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({ + type: 'sync', + asset: { + id: 'asset-1', + name: 'model.safetensors', + tags: ['models', 'model_type:checkpoints'] + } + }) + vi.mocked(api.getServerFeature).mockImplementation((name, defaultValue) => + name === 'supports_model_type_tags' ? true : defaultValue + ) + + const wizard = setupUploadModelWizard(modelTypes, { + requiredModelType: 'checkpoints' + }) + wizard.wizardData.value.url = 'https://civitai.com/models/12345' + + await wizard.uploadModel() + + const uploadArg = vi.mocked(assetService.uploadAssetAsync).mock.calls[0][0] + expect(uploadArg.tags).toEqual(['models', 'model_type:checkpoints']) + expect(uploadArg.user_metadata?.model_type).toBe('checkpoints') + // The namespaced returned tag must not trip the required-type guard. + expect(wizard.uploadTypeMismatch.value).toBeNull() + }) + it('returns the synced asset filename for sync imports', async () => { const { assetService } = await import('@/platform/assets/services/assetService') @@ -347,6 +379,65 @@ describe('useUploadModelWizard', () => { }) }) + it('treats a namespaced model_type: tag as satisfying the required type', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({ + type: 'sync', + asset: { + id: 'asset-1', + name: 'model.safetensors', + tags: ['models', 'model_type:checkpoints'] + } + }) + + const wizard = setupUploadModelWizard( + ref([ + { name: 'Checkpoint', value: 'checkpoints' }, + { name: 'LoRA', value: 'loras' } + ]), + { requiredModelType: 'checkpoints' } + ) + wizard.wizardData.value.url = 'https://civitai.com/models/12345' + + const result = await wizard.uploadModel() + + expect(result).not.toBeNull() + expect(wizard.uploadTypeMismatch.value).toBeNull() + }) + + it('strips the model_type: prefix from the imported-type label on a real mismatch', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({ + type: 'sync', + asset: { + id: 'asset-lora', + name: 'model.safetensors', + tags: ['models', 'model_type:loras'] + } + }) + + const wizard = setupUploadModelWizard( + ref([ + { name: 'Checkpoint', value: 'checkpoints' }, + { name: 'LoRA', value: 'loras' } + ]), + { requiredModelType: 'checkpoints' } + ) + wizard.wizardData.value.url = 'https://civitai.com/models/12345' + + const result = await wizard.uploadModel() + + expect(result).toBeNull() + expect(wizard.uploadTypeMismatch.value).toEqual({ + importedModelType: 'loras', + importedModelTypeLabel: 'LoRA', + requiredModelType: 'checkpoints', + requiredModelTypeLabel: 'Checkpoint' + }) + }) + it('does not block sync imports as mismatches without a required model type', async () => { const { assetService } = await import('@/platform/assets/services/assetService') diff --git a/src/platform/assets/composables/useUploadModelWizard.ts b/src/platform/assets/composables/useUploadModelWizard.ts index f00827752bb..131bb88dd2e 100644 --- a/src/platform/assets/composables/useUploadModelWizard.ts +++ b/src/platform/assets/composables/useUploadModelWizard.ts @@ -2,6 +2,7 @@ import type { Ref } from 'vue' import { computed, ref, watch } from 'vue' import { useI18n } from 'vue-i18n' +import { useFeatureFlags } from '@/composables/useFeatureFlags' import { st } from '@/i18n' import { civitaiImportSource } from '@/platform/assets/importSources/civitaiImportSource' import { huggingfaceImportSource } from '@/platform/assets/importSources/huggingfaceImportSource' @@ -11,7 +12,11 @@ import type { } from '@/platform/assets/schemas/assetSchema' import { assetService } from '@/platform/assets/services/assetService' import type { ImportSource } from '@/platform/assets/types/importSource' -import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils' +import { + getAssetFilename, + stripModelTypePrefix, + toModelTypeTag +} from '@/platform/assets/utils/assetMetadataUtils' import { validateSourceUrl } from '@/platform/assets/utils/importSourceUtil' import { useAssetDownloadStore } from '@/stores/assetDownloadStore' import { useAssetsStore } from '@/stores/assetsStore' @@ -68,6 +73,7 @@ export function useUploadModelWizard( options: UploadModelWizardOptions = {} ) { const { t } = useI18n() + const { flags } = useFeatureFlags() const assetsStore = useAssetsStore() const assetDownloadStore = useAssetDownloadStore() const modelToNodeStore = useModelToNodeStore() @@ -271,19 +277,22 @@ export function useUploadModelWizard( } function getImportedModelType(asset: AssetItem): string | undefined { - const knownType = asset.tags.find( - (tag) => - tag !== MODEL_ROOT_TAG && + const subtypeTags = asset.tags + .filter((tag) => tag !== MODEL_ROOT_TAG) + .map(stripModelTypePrefix) + return ( + subtypeTags.find((tag) => modelTypes.value.some((type) => type.value === tag) + ) ?? subtypeTags[0] ) - return knownType ?? asset.tags.find((tag) => tag !== MODEL_ROOT_TAG) } function blockMismatchedImportedModel( asset: AssetItem, requiredType: string ): boolean { - if (asset.tags.includes(requiredType)) return false + if (asset.tags.map(stripModelTypePrefix).includes(requiredType)) + return false const importedType = getImportedModelType(asset) uploadStatus.value = 'error' @@ -317,7 +326,11 @@ export function useUploadModelWizard( try { const modelType = resolvedModelType.value - const tags = modelType ? ['models', modelType] : ['models'] + const subtypeTag = + modelType && flags.supportsModelTypeTags + ? toModelTypeTag(modelType) + : modelType + const tags = subtypeTag ? [MODEL_ROOT_TAG, subtypeTag] : [MODEL_ROOT_TAG] const filename = wizardData.value.metadata?.filename || wizardData.value.metadata?.name || diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index ec0e1998f1b..ffbe05331aa 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -6,6 +6,7 @@ import { MODELS_TAG } from '@/platform/assets/services/assetService' import { + buildModelTypeTagUpdate, getAssetAdditionalTags, getAssetBaseModel, getAssetBaseModels, @@ -24,9 +25,11 @@ import { getAssetTriggerPhrases, getAssetTypeBadges, getAssetUserDescription, + getEditableModelType, getSourceName, resolveDisplayImageDimensions, - stripModelTypePrefix + stripModelTypePrefix, + toModelTypeTag } from '@/platform/assets/utils/assetMetadataUtils' const { isCloudRef } = vi.hoisted(() => ({ @@ -853,3 +856,164 @@ describe('reserved tag mirrors', () => { expect(getAssetModelType(asset([MODELS_TAG]))).toBeNull() }) }) + +describe('toModelTypeTag', () => { + it('prefixes a folder_name with the model_type namespace', () => { + expect(toModelTypeTag('checkpoints')).toBe('model_type:checkpoints') + expect(toModelTypeTag('ultralytics_bbox')).toBe( + 'model_type:ultralytics_bbox' + ) + }) + + it('returns an already-namespaced value unchanged instead of double-prefixing', () => { + expect(toModelTypeTag('model_type:checkpoints')).toBe( + 'model_type:checkpoints' + ) + }) +}) + +describe('getEditableModelType', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('prefers the model_type value over a distinct bare tag in model_type mode', () => { + expect( + getEditableModelType( + asset(['models', 'checkpoints', 'model_type:loras']), + true + ) + ).toBe('loras') + }) + + it('returns the model_type value even when no bare twin is present', () => { + expect( + getEditableModelType(asset(['models', 'model_type:loras']), true) + ).toBe('loras') + }) + + it('ignores an empty model_type value and falls back to the bare tag', () => { + expect( + getEditableModelType( + asset(['models', 'checkpoints', 'model_type:']), + true + ) + ).toBe('checkpoints') + }) + + it('resolves the same primary model_type regardless of tag array order', () => { + const forward = ['models', 'model_type:diffusion_models', 'model_type:unet'] + const reversed = [ + 'models', + 'model_type:unet', + 'model_type:diffusion_models' + ] + expect(getEditableModelType(asset(forward), true)).toBe('diffusion_models') + expect(getEditableModelType(asset(reversed), true)).toBe('diffusion_models') + }) + + it('falls back to the bare tag for an uncovered asset in model_type mode', () => { + expect(getEditableModelType(asset(['models', 'sam2']), true)).toBe('sam2') + }) + + it('uses the legacy first-non-models tag when mode is off', () => { + expect( + getEditableModelType(asset(['models', 'checkpoints', 'sdxl']), false) + ).toBe('checkpoints') + }) + + it('returns null when only the models tag is present', () => { + expect(getEditableModelType(asset(['models']), true)).toBeNull() + }) +}) + +describe('buildModelTypeTagUpdate', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('swaps the bare subtype tag when mode is off', () => { + expect( + buildModelTypeTagUpdate(asset(['models', 'checkpoints']), 'loras', false) + ).toEqual(['models', 'loras']) + }) + + it('preserves user labels and swaps only the subtype tag when mode is off', () => { + expect( + buildModelTypeTagUpdate( + asset(['models', 'checkpoints', 'sdxl']), + 'loras', + false + ) + ).toEqual(['models', 'sdxl', 'loras']) + }) + + it('drops the primary model_type form and its stale bare twin for a covered asset', () => { + expect( + buildModelTypeTagUpdate( + asset(['models', 'checkpoints', 'model_type:checkpoints']), + 'loras', + true + ) + ).toEqual(['models', 'model_type:loras']) + }) + + it('replaces only the primary membership, preserving sibling model_type memberships', () => { + expect( + buildModelTypeTagUpdate( + asset([ + 'models', + 'diffusion_models', + 'model_type:diffusion_models', + 'model_type:unet_gguf' + ]), + 'loras', + true + ) + ).toEqual(['models', 'model_type:unet_gguf', 'model_type:loras']) + }) + + it('replaces the deterministic primary regardless of model_type tag order', () => { + expect( + buildModelTypeTagUpdate( + asset([ + 'models', + 'model_type:unet_gguf', + 'model_type:diffusion_models' + ]), + 'loras', + true + ) + ).toEqual(['models', 'model_type:unet_gguf', 'model_type:loras']) + }) + + it('does not duplicate when re-typing to an existing sibling membership', () => { + expect( + buildModelTypeTagUpdate( + asset(['models', 'model_type:diffusion_models', 'model_type:loras']), + 'loras', + true + ) + ).toEqual(['models', 'model_type:loras']) + }) + + it('drops the bare current type for an uncovered asset in model_type mode', () => { + expect( + buildModelTypeTagUpdate(asset(['models', 'sam2']), 'loras', true) + ).toEqual(['models', 'model_type:loras']) + }) + + it('keeps user labels but drops the primary bare twin in model_type mode', () => { + expect( + buildModelTypeTagUpdate( + asset(['models', 'checkpoints', 'model_type:checkpoints', 'sdxl']), + 'loras', + true + ) + ).toEqual(['models', 'sdxl', 'model_type:loras']) + }) +}) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 2686e2b736e..c4112472d3c 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -162,6 +162,18 @@ export function getAssetModelType(asset: AssetItem): string | null { return typeTag ?? null } +/** + * Builds the namespaced subtype tag the backend stores in `model_type:` mode. + * The argument is a discovery folder_name (e.g. `checkpoints`, + * `ultralytics_bbox`); the backend keeps the bare directory-path twin in sync. + * An already-namespaced input is returned unchanged rather than silently + * double-prefixed. + */ +export function toModelTypeTag(folderName: string): string { + if (folderName.startsWith(MODEL_TYPE_TAG_PREFIX)) return folderName + return `${MODEL_TYPE_TAG_PREFIX}${folderName}` +} + /** Strips the `model_type:` prefix off each namespaced tag, dropping non-`model_type:` tags. */ function getModelTypeTagValues(asset: AssetItem): string[] { return asset.tags @@ -170,6 +182,67 @@ function getModelTypeTagValues(asset: AssetItem): string[] { .filter((tag) => tag.length > 0) } +/** + * The asset's primary `model_type:` membership: the lexicographically-first of + * its stripped `model_type:` values, or `undefined` for an uncovered asset. + * Sorting makes the choice deterministic because the backend does not guarantee + * the order of an asset's `model_type:` tags. This is the single membership a + * re-type replaces and the value the edit dropdown / browser title reflect. + */ +function getPrimaryModelType(asset: AssetItem): string | undefined { + return getModelTypeTagValues(asset).toSorted()[0] +} + +/** + * Resolves the folder_name shown as the asset's current model type in the edit + * dropdown. In `modelTypeMode` the primary `model_type:` value is authoritative + * (covered assets); an uncovered asset with no `model_type:` tag falls back to + * its bare subtype tag, mirroring the read-side grouping. Outside the mode this + * is the legacy first-non-`models` tag. + */ +export function getEditableModelType( + asset: AssetItem, + modelTypeMode: boolean +): string | null { + if (modelTypeMode) { + const primary = getPrimaryModelType(asset) + if (primary !== undefined) return primary + } + return getAssetModelType(asset) +} + +/** + * Computes the tag set for re-typing a model asset to `newFolderName`. In + * `modelTypeMode` only the primary `model_type:` membership is replaced: a + * covered asset drops `model_type:` and its bare directory-path twin, + * leaving any sibling `model_type:` memberships of a multi-type asset intact; + * an uncovered asset drops its bare current type. The new + * `model_type:` is then added (deduped if it already exists as a + * sibling). Outside the mode it swaps the legacy bare subtype tag, preserving + * the pre-namespace behavior. + */ +export function buildModelTypeTagUpdate( + asset: AssetItem, + newFolderName: string, + modelTypeMode: boolean +): string[] { + if (!modelTypeMode) { + const currentType = getAssetModelType(asset) + return asset.tags.filter((tag) => tag !== currentType).concat(newFolderName) + } + + const primary = getPrimaryModelType(asset) + const currentBareType = getAssetModelType(asset) + const tagsToRemove = + primary !== undefined + ? new Set([toModelTypeTag(primary), primary]) + : new Set(currentBareType ? [currentBareType] : []) + + const newTag = toModelTypeTag(newFolderName) + const retained = asset.tags.filter((tag) => !tagsToRemove.has(tag)) + return retained.includes(newTag) ? retained : retained.concat(newTag) +} + /** Legacy grouping: each non-`models` tag's top-level path segment. */ function getBareTagCategories(asset: AssetItem): string[] { return asset.tags @@ -205,7 +278,7 @@ export function getAssetCategories( /** * Resolves the primary tag a browser surface titles itself after. In - * `modelTypeMode` a covered asset uses its first `model_type:*` value — the + * `modelTypeMode` a covered asset uses its primary `model_type:*` value — the * key it groups under — while an uncovered asset keeps the legacy selection * (first non-`models` tag, verbatim, hierarchical paths intact). Outside the * mode this is exactly the legacy selection. @@ -215,8 +288,8 @@ export function getPrimaryCategoryTag( modelTypeMode: boolean ): string | undefined { if (modelTypeMode) { - const [modelType] = getModelTypeTagValues(asset) - if (modelType) return modelType + const primary = getPrimaryModelType(asset) + if (primary !== undefined) return primary return asset.tags.find( (tag) => tag !== MODELS_TAG && !tag.startsWith(MODEL_TYPE_TAG_PREFIX) )