From d435968df46ddf512ec609c4cb50a2713e8d3b8f Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Thu, 18 Jun 2026 20:55:31 +1200 Subject: [PATCH 01/62] feat(assets): group model library by model_type tags Group the asset-mode Model Library by namespaced model_type:* tags. When an asset carries model_type:* tags, the stripped values are the authoritative categories and all other tags are disregarded; otherwise fall back to the legacy bare-tag top-level grouping. A shared getAssetCategories helper keeps the "By type" nav and category filtering in agreement. Resolve node-provider lookup from the same tags via getAssetNodeCategory: prefer the most specific (deepest parent/child) candidate so a flat model_type:LLM does not shadow a resolvable hierarchical tag, and strip the model_type: prefix before the modelNodeMappings lookup. --- .../composables/useAssetBrowser.test.ts | 12 ++++ .../assets/composables/useAssetBrowser.ts | 6 +- .../assets/utils/assetFilterUtils.test.ts | 13 +++- src/platform/assets/utils/assetFilterUtils.ts | 17 ++--- .../assets/utils/assetMetadataUtils.test.ts | 66 +++++++++++++++++++ .../assets/utils/assetMetadataUtils.ts | 59 +++++++++++++++++ 6 files changed, 156 insertions(+), 17 deletions(-) diff --git a/src/platform/assets/composables/useAssetBrowser.test.ts b/src/platform/assets/composables/useAssetBrowser.test.ts index c767c5c2161..552f17f00bb 100644 --- a/src/platform/assets/composables/useAssetBrowser.test.ts +++ b/src/platform/assets/composables/useAssetBrowser.test.ts @@ -668,6 +668,18 @@ describe('useAssetBrowser', () => { ]) }) + it('groups by model_type:* value and ignores other tags', () => { + const assets = [ + createApiAsset({ tags: ['models', 'model_type:checkpoints', 'sdxl'] }), + createApiAsset({ tags: ['models', 'model_type:LLM'] }) + ] + + const { navItems } = useAssetBrowser(ref(assets)) + + const typeGroup = navItems.value[2] as { items: { id: string }[] } + expect(typeGroup.items.map((i) => i.id)).toEqual(['LLM', 'checkpoints']) + }) + it('handles assets with no category tag', () => { const assets = [ createApiAsset({ tags: ['models'] }), // No second tag diff --git a/src/platform/assets/composables/useAssetBrowser.ts b/src/platform/assets/composables/useAssetBrowser.ts index 03f042a4de9..69471093f65 100644 --- a/src/platform/assets/composables/useAssetBrowser.ts +++ b/src/platform/assets/composables/useAssetBrowser.ts @@ -19,6 +19,7 @@ import { } from '@/platform/assets/utils/assetFilterUtils' import { getAssetBaseModels, + getAssetCategories, getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils' import { MODELS_TAG } from '@/platform/assets/services/assetService' @@ -124,10 +125,7 @@ export function useAssetBrowser( const typeCategories = computed(() => { const categories = assets.value .filter((asset) => asset.tags.includes(MODELS_TAG)) - .flatMap((asset) => - asset.tags.filter((tag) => tag !== MODELS_TAG && tag.length > 0) - ) - .map((tag) => tag.split('/')[0]) + .flatMap(getAssetCategories) return Array.from(new Set(categories)) .sort() diff --git a/src/platform/assets/utils/assetFilterUtils.test.ts b/src/platform/assets/utils/assetFilterUtils.test.ts index 24d35e90e11..8ba5205681d 100644 --- a/src/platform/assets/utils/assetFilterUtils.test.ts +++ b/src/platform/assets/utils/assetFilterUtils.test.ts @@ -34,7 +34,18 @@ describe('filterByCategory', () => { tags: ['models', 'checkpoint/xl'], expected: true }, - { category: 'xl', tags: ['models', 'checkpoint/xl'], expected: false } + { category: 'xl', tags: ['models', 'checkpoint/xl'], expected: false }, + { + category: 'checkpoints', + tags: ['models', 'model_type:checkpoints'], + expected: true + }, + { category: 'LLM', tags: ['models', 'model_type:LLM'], expected: true }, + { + category: 'sdxl', + tags: ['models', 'model_type:checkpoints', 'sdxl'], + expected: false + } ])( 'category=$category with tags=$tags returns $expected', ({ category, tags, expected }) => { diff --git a/src/platform/assets/utils/assetFilterUtils.ts b/src/platform/assets/utils/assetFilterUtils.ts index cdcc1946b17..80ce97578f1 100644 --- a/src/platform/assets/utils/assetFilterUtils.ts +++ b/src/platform/assets/utils/assetFilterUtils.ts @@ -1,21 +1,14 @@ import type { AssetItem } from '@/platform/assets/schemas/assetSchema' import type { OwnershipOption } from '@/platform/assets/types/filterTypes' -import { getAssetBaseModels } from '@/platform/assets/utils/assetMetadataUtils' +import { + getAssetBaseModels, + getAssetCategories +} from '@/platform/assets/utils/assetMetadataUtils' export function filterByCategory(category: string) { return (asset: AssetItem) => { if (category === 'all') return true - - // Check if any tag matches the category (for exact matches) - if (asset.tags.includes(category)) return true - - // Check if any tag's top-level folder matches the category - return asset.tags.some((tag) => { - if (typeof tag === 'string' && tag.includes('/')) { - return tag.split('/')[0] === category - } - return false - }) + return getAssetCategories(asset).includes(category) } } diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index 43516a62afa..ea4b7f05c89 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -6,12 +6,14 @@ import { getAssetBaseModel, getAssetBaseModels, getAssetCardTitle, + getAssetCategories, getAssetDescription, getAssetDisplayFilename, getAssetDisplayName, getAssetFilename, getAssetMetadataDimensions, getAssetModelType, + getAssetNodeCategory, getAssetSourceUrl, getAssetStoredFilename, getAssetTriggerPhrases, @@ -580,3 +582,67 @@ describe('assetMetadataUtils', () => { }) }) }) + +describe('getAssetCategories', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('uses model_type:* values as the group and disregards other tags', () => { + expect( + getAssetCategories(asset(['models', 'model_type:checkpoints', 'sdxl'])) + ).toEqual(['checkpoints']) + }) + + it('preserves the model_type value casing', () => { + expect(getAssetCategories(asset(['models', 'model_type:LLM']))).toEqual([ + 'LLM' + ]) + }) + + it('falls back to non-model tags when no model_type tag is present', () => { + expect(getAssetCategories(asset(['models', 'checkpoints']))).toEqual([ + 'checkpoints' + ]) + }) +}) + +describe('getAssetNodeCategory', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('prefers the most specific (deepest) tag over a flat model_type value', () => { + expect( + getAssetNodeCategory( + asset(['models', 'model_type:LLM', 'LLM/Qwen-VL/Qwen3-0.6B']) + ) + ).toBe('LLM/Qwen-VL/Qwen3-0.6B') + }) + + it('strips the model_type: prefix when it is the only candidate', () => { + expect(getAssetNodeCategory(asset(['models', 'model_type:vae']))).toBe( + 'vae' + ) + }) + + it('lets a model_type value win ties against a bare tag', () => { + expect( + getAssetNodeCategory(asset(['models', 'model_type:checkpoints', 'sdxl'])) + ).toBe('checkpoints') + }) + + it('keeps a hierarchical legacy tag intact', () => { + expect( + getAssetNodeCategory(asset(['models', 'chatterbox/chatterbox_vc'])) + ).toBe('chatterbox/chatterbox_vc') + }) + + it('returns undefined when only reserved tags are present', () => { + expect(getAssetNodeCategory(asset(['models', 'missing']))).toBeUndefined() + }) +}) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index dfcb115607e..017339685ca 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -1,4 +1,8 @@ import type { AssetItem } from '@/platform/assets/schemas/assetSchema' +import { + MISSING_TAG, + MODELS_TAG +} from '@/platform/assets/services/assetService' import { isCloud } from '@/platform/distribution/types' import { isCivitaiUrl } from '@/utils/formatUtil' @@ -150,6 +154,61 @@ export function getAssetModelType(asset: AssetItem): string | null { return typeTag ?? null } +const MODEL_TYPE_TAG_PREFIX = 'model_type:' + +function getModelTypeTagValues(asset: AssetItem): string[] { + return asset.tags + .filter((tag) => tag.startsWith(MODEL_TYPE_TAG_PREFIX)) + .map((tag) => tag.slice(MODEL_TYPE_TAG_PREFIX.length)) + .filter((tag) => tag.length > 0) +} + +/** + * Resolves the category keys a model asset is grouped under. + * + * When the asset carries `model_type:*` tags (the namespaced tag scheme), the + * `*` values are authoritative and every other tag is disregarded. Otherwise + * we fall back to treating each non-`models` tag's top-level segment as a + * category, preserving the pre-namespace behavior. + */ +export function getAssetCategories(asset: AssetItem): string[] { + const modelTypes = getModelTypeTagValues(asset) + if (modelTypes.length > 0) return modelTypes + + return asset.tags + .filter((tag) => tag !== MODELS_TAG && tag.length > 0) + .map((tag) => tag.split('/')[0]) +} + +function pathDepth(tag: string): number { + return tag.split('/').length +} + +/** + * Resolves the model-type key used to look up a node provider for an asset. + * + * Unlike {@link getAssetCategories}, this keeps the full (possibly + * hierarchical) value so `modelToNodeStore`'s `parent/child` fallback still + * works. Candidates are the stripped `model_type:*` values plus any other + * non-reserved tags, and the most specific (deepest `parent/child`) candidate + * wins so a flat `model_type:LLM` never shadows a resolvable + * `LLM/Qwen-VL/...` tag. `model_type:*` values win ties. + */ +export function getAssetNodeCategory(asset: AssetItem): string | undefined { + const otherTags = asset.tags.filter( + (tag) => + tag !== MODELS_TAG && + tag !== MISSING_TAG && + !tag.startsWith(MODEL_TYPE_TAG_PREFIX) + ) + const candidates = [...getModelTypeTagValues(asset), ...otherTags] + if (candidates.length === 0) return undefined + + return candidates.reduce((best, candidate) => + pathDepth(candidate) > pathDepth(best) ? candidate : best + ) +} + /** * Extracts user description from asset user_metadata * @param asset - The asset to extract user description from From 01a51fc8385b33eeabb7e411f83660771350889c Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Fri, 19 Jun 2026 11:07:06 +1200 Subject: [PATCH 02/62] refactor(assets): gate model_type grouping on supports_model_type_tags Drive the model_type: grouping/resolution off the backend-reported supports_model_type_tags capability (getServerFeature) instead of sniffing tags per asset. Default false preserves the legacy bare-tag grouping, so a backend that does not declare the capability is unchanged. When the flag is true, assets group by their model_type: values and an uncovered asset (no model_type: tag) still routes by its bare tags. The thread-through is a modelTypeMode flag on getAssetCategories / getAssetNodeCategory / filterByCategory (keeping them pure); call sites read the capability via useFeatureFlags. --- src/composables/useFeatureFlags.ts | 9 +++- .../composables/useAssetBrowser.test.ts | 30 ++++++++++- .../assets/composables/useAssetBrowser.ts | 10 +++- .../assets/utils/assetFilterUtils.test.ts | 40 +++++++++++--- src/platform/assets/utils/assetFilterUtils.ts | 4 +- .../assets/utils/assetMetadataUtils.test.ts | 54 +++++++++++++------ .../assets/utils/assetMetadataUtils.ts | 50 +++++++++++------ 7 files changed, 153 insertions(+), 44 deletions(-) diff --git a/src/composables/useFeatureFlags.ts b/src/composables/useFeatureFlags.ts index de12ec49613..feb94ec6990 100644 --- a/src/composables/useFeatureFlags.ts +++ b/src/composables/useFeatureFlags.ts @@ -37,7 +37,8 @@ export enum ServerFeatureFlag { CONSOLIDATED_BILLING_ENABLED = 'consolidated_billing_enabled', BILLING_CONTROL_ENABLED = 'billing_control_enabled', FREE_TIER_JOB_ALLOWANCE_ENABLED = 'free_tier_job_allowance_enabled', - SIGNUP_TURNSTILE = 'signup_turnstile' + SIGNUP_TURNSTILE = 'signup_turnstile', + SUPPORTS_MODEL_TYPE_TAGS = 'supports_model_type_tags' } /** @@ -236,6 +237,12 @@ export function useFeatureFlags() { remoteConfig.value.signup_turnstile, 'off' ) + }, + get supportsModelTypeTags() { + return api.getServerFeature( + ServerFeatureFlag.SUPPORTS_MODEL_TYPE_TAGS, + false + ) } }) diff --git a/src/platform/assets/composables/useAssetBrowser.test.ts b/src/platform/assets/composables/useAssetBrowser.test.ts index 552f17f00bb..ea100565ae2 100644 --- a/src/platform/assets/composables/useAssetBrowser.test.ts +++ b/src/platform/assets/composables/useAssetBrowser.test.ts @@ -25,10 +25,22 @@ vi.mock('@/i18n', () => ({ d: (date: Date) => date.toLocaleDateString() })) +const mockSupportsModelTypeTags = vi.hoisted(() => ({ value: false })) +vi.mock('@/composables/useFeatureFlags', () => ({ + useFeatureFlags: () => ({ + flags: { + get supportsModelTypeTags() { + return mockSupportsModelTypeTags.value + } + } + }) +})) + describe('useAssetBrowser', () => { beforeEach(() => { setActivePinia(createPinia()) vi.restoreAllMocks() + mockSupportsModelTypeTags.value = false }) // Test fixtures - minimal data focused on functionality being tested @@ -668,7 +680,8 @@ describe('useAssetBrowser', () => { ]) }) - it('groups by model_type:* value and ignores other tags', () => { + it('groups by model_type:* value and ignores other tags when the flag is on', () => { + mockSupportsModelTypeTags.value = true const assets = [ createApiAsset({ tags: ['models', 'model_type:checkpoints', 'sdxl'] }), createApiAsset({ tags: ['models', 'model_type:LLM'] }) @@ -680,6 +693,21 @@ describe('useAssetBrowser', () => { expect(typeGroup.items.map((i) => i.id)).toEqual(['LLM', 'checkpoints']) }) + it('ignores model_type: and groups by bare tags when the flag is off', () => { + const assets = [ + createApiAsset({ tags: ['models', 'model_type:checkpoints'] }), + createApiAsset({ tags: ['models', 'model_type:LLM'] }) + ] + + const { navItems } = useAssetBrowser(ref(assets)) + + const typeGroup = navItems.value[2] as { items: { id: string }[] } + expect(typeGroup.items.map((i) => i.id)).toEqual([ + 'model_type:LLM', + 'model_type:checkpoints' + ]) + }) + it('handles assets with no category tag', () => { const assets = [ createApiAsset({ tags: ['models'] }), // No second tag diff --git a/src/platform/assets/composables/useAssetBrowser.ts b/src/platform/assets/composables/useAssetBrowser.ts index 69471093f65..eba491ae7b6 100644 --- a/src/platform/assets/composables/useAssetBrowser.ts +++ b/src/platform/assets/composables/useAssetBrowser.ts @@ -24,6 +24,7 @@ import { } from '@/platform/assets/utils/assetMetadataUtils' import { MODELS_TAG } from '@/platform/assets/services/assetService' import { sortAssets } from '@/platform/assets/utils/assetSortUtils' +import { useFeatureFlags } from '@/composables/useFeatureFlags' import { useAssetDownloadStore } from '@/stores/assetDownloadStore' import type { NavGroupData, NavItemData } from '@/types/navTypes' @@ -94,6 +95,7 @@ export function useAssetBrowser( const assets = computed(() => assetsSource.value ?? []) const assetDownloadStore = useAssetDownloadStore() const { sessionDownloadCount } = storeToRefs(assetDownloadStore) + const { flags } = useFeatureFlags() // State const searchQuery = ref('') @@ -125,7 +127,9 @@ export function useAssetBrowser( const typeCategories = computed(() => { const categories = assets.value .filter((asset) => asset.tags.includes(MODELS_TAG)) - .flatMap(getAssetCategories) + .flatMap((asset) => + getAssetCategories(asset, flags.supportsModelTypeTags) + ) return Array.from(new Set(categories)) .sort() @@ -189,7 +193,9 @@ export function useAssetBrowser( // Category-filtered assets for filter options (before search/format/base model filters) const categoryFilteredAssets = computed(() => { - return assets.value.filter(filterByCategory(selectedCategory.value)) + return assets.value.filter( + filterByCategory(selectedCategory.value, flags.supportsModelTypeTags) + ) }) const { availableFileFormats, availableBaseModels } = useAssetFilterOptions( diff --git a/src/platform/assets/utils/assetFilterUtils.test.ts b/src/platform/assets/utils/assetFilterUtils.test.ts index 8ba5205681d..06de879dc8e 100644 --- a/src/platform/assets/utils/assetFilterUtils.test.ts +++ b/src/platform/assets/utils/assetFilterUtils.test.ts @@ -26,30 +26,54 @@ function createAsset( describe('filterByCategory', () => { it.for([ - { category: 'all', tags: ['checkpoint'], expected: true }, - { category: 'checkpoint', tags: ['checkpoint'], expected: true }, - { category: 'lora', tags: ['checkpoint'], expected: false }, + { category: 'all', tags: ['checkpoint'], mode: false, expected: true }, + { + category: 'checkpoint', + tags: ['checkpoint'], + mode: false, + expected: true + }, + { category: 'lora', tags: ['checkpoint'], mode: false, expected: false }, { category: 'checkpoint', tags: ['models', 'checkpoint/xl'], + mode: false, expected: true }, - { category: 'xl', tags: ['models', 'checkpoint/xl'], expected: false }, + { + category: 'xl', + tags: ['models', 'checkpoint/xl'], + mode: false, + expected: false + }, { category: 'checkpoints', tags: ['models', 'model_type:checkpoints'], + mode: true, + expected: true + }, + { + category: 'LLM', + tags: ['models', 'model_type:LLM'], + mode: true, expected: true }, - { category: 'LLM', tags: ['models', 'model_type:LLM'], expected: true }, { category: 'sdxl', tags: ['models', 'model_type:checkpoints', 'sdxl'], + mode: true, + expected: false + }, + { + category: 'checkpoints', + tags: ['models', 'model_type:checkpoints'], + mode: false, expected: false } ])( - 'category=$category with tags=$tags returns $expected', - ({ category, tags, expected }) => { - const filter = filterByCategory(category) + 'category=$category tags=$tags mode=$mode returns $expected', + ({ category, tags, mode, expected }) => { + const filter = filterByCategory(category, mode) const asset = createAsset('model.safetensors', { tags }) expect(filter(asset)).toBe(expected) } diff --git a/src/platform/assets/utils/assetFilterUtils.ts b/src/platform/assets/utils/assetFilterUtils.ts index 80ce97578f1..a6f567c97a8 100644 --- a/src/platform/assets/utils/assetFilterUtils.ts +++ b/src/platform/assets/utils/assetFilterUtils.ts @@ -5,10 +5,10 @@ import { getAssetCategories } from '@/platform/assets/utils/assetMetadataUtils' -export function filterByCategory(category: string) { +export function filterByCategory(category: string, modelTypeMode = false) { return (asset: AssetItem) => { if (category === 'all') return true - return getAssetCategories(asset).includes(category) + return getAssetCategories(asset, modelTypeMode).includes(category) } } diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index ea4b7f05c89..d0c9e4efcff 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -590,23 +590,32 @@ describe('getAssetCategories', () => { tags }) - it('uses model_type:* values as the group and disregards other tags', () => { + it('uses model_type:* values as the group and disregards other tags in model_type mode', () => { expect( - getAssetCategories(asset(['models', 'model_type:checkpoints', 'sdxl'])) + getAssetCategories( + asset(['models', 'model_type:checkpoints', 'sdxl']), + true + ) ).toEqual(['checkpoints']) }) it('preserves the model_type value casing', () => { - expect(getAssetCategories(asset(['models', 'model_type:LLM']))).toEqual([ - 'LLM' - ]) + expect( + getAssetCategories(asset(['models', 'model_type:LLM']), true) + ).toEqual(['LLM']) }) - it('falls back to non-model tags when no model_type tag is present', () => { - expect(getAssetCategories(asset(['models', 'checkpoints']))).toEqual([ + it('routes an uncovered asset by its bare tags in model_type mode', () => { + expect(getAssetCategories(asset(['models', 'checkpoints']), true)).toEqual([ 'checkpoints' ]) }) + + it('ignores model_type: and uses bare-tag grouping when mode is off (default)', () => { + expect( + getAssetCategories(asset(['models', 'model_type:checkpoints', 'sdxl'])) + ).toEqual(['model_type:checkpoints', 'sdxl']) + }) }) describe('getAssetNodeCategory', () => { @@ -619,30 +628,45 @@ describe('getAssetNodeCategory', () => { it('prefers the most specific (deepest) tag over a flat model_type value', () => { expect( getAssetNodeCategory( - asset(['models', 'model_type:LLM', 'LLM/Qwen-VL/Qwen3-0.6B']) + asset(['models', 'model_type:LLM', 'LLM/Qwen-VL/Qwen3-0.6B']), + true ) ).toBe('LLM/Qwen-VL/Qwen3-0.6B') }) it('strips the model_type: prefix when it is the only candidate', () => { - expect(getAssetNodeCategory(asset(['models', 'model_type:vae']))).toBe( - 'vae' - ) + expect( + getAssetNodeCategory(asset(['models', 'model_type:vae']), true) + ).toBe('vae') }) it('lets a model_type value win ties against a bare tag', () => { expect( - getAssetNodeCategory(asset(['models', 'model_type:checkpoints', 'sdxl'])) + getAssetNodeCategory( + asset(['models', 'model_type:checkpoints', 'sdxl']), + true + ) ).toBe('checkpoints') }) - it('keeps a hierarchical legacy tag intact', () => { + it('keeps a hierarchical tag intact', () => { expect( - getAssetNodeCategory(asset(['models', 'chatterbox/chatterbox_vc'])) + getAssetNodeCategory(asset(['models', 'chatterbox/chatterbox_vc']), true) ).toBe('chatterbox/chatterbox_vc') }) it('returns undefined when only reserved tags are present', () => { - expect(getAssetNodeCategory(asset(['models', 'missing']))).toBeUndefined() + expect( + getAssetNodeCategory(asset(['models', 'missing']), true) + ).toBeUndefined() + }) + + it('uses the first non-reserved tag verbatim when mode is off (default)', () => { + expect(getAssetNodeCategory(asset(['models', 'model_type:vae']))).toBe( + 'model_type:vae' + ) + expect(getAssetNodeCategory(asset(['models', 'checkpoints']))).toBe( + 'checkpoints' + ) }) }) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 017339685ca..53af8faff47 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -163,21 +163,31 @@ function getModelTypeTagValues(asset: AssetItem): string[] { .filter((tag) => tag.length > 0) } +function getBareTagCategories(asset: AssetItem): string[] { + return asset.tags + .filter((tag) => tag !== MODELS_TAG && tag.length > 0) + .map((tag) => tag.split('/')[0]) +} + /** * Resolves the category keys a model asset is grouped under. * - * When the asset carries `model_type:*` tags (the namespaced tag scheme), the - * `*` values are authoritative and every other tag is disregarded. Otherwise - * we fall back to treating each non-`models` tag's top-level segment as a - * category, preserving the pre-namespace behavior. + * `modelTypeMode` reflects whether the backend declares the `model_type:` tag + * scheme (the `supports_model_type_tags` capability). When true, an asset's + * `model_type:*` values are authoritative; an asset with no `model_type:` tag + * still routes by its bare tags. When false (the default) categories come from + * the legacy bare-tag top-level grouping and `model_type:` is ignored. */ -export function getAssetCategories(asset: AssetItem): string[] { - const modelTypes = getModelTypeTagValues(asset) - if (modelTypes.length > 0) return modelTypes +export function getAssetCategories( + asset: AssetItem, + modelTypeMode = false +): string[] { + if (modelTypeMode) { + const modelTypes = getModelTypeTagValues(asset) + if (modelTypes.length > 0) return modelTypes + } - return asset.tags - .filter((tag) => tag !== MODELS_TAG && tag.length > 0) - .map((tag) => tag.split('/')[0]) + return getBareTagCategories(asset) } function pathDepth(tag: string): number { @@ -189,12 +199,22 @@ function pathDepth(tag: string): number { * * Unlike {@link getAssetCategories}, this keeps the full (possibly * hierarchical) value so `modelToNodeStore`'s `parent/child` fallback still - * works. Candidates are the stripped `model_type:*` values plus any other - * non-reserved tags, and the most specific (deepest `parent/child`) candidate - * wins so a flat `model_type:LLM` never shadows a resolvable - * `LLM/Qwen-VL/...` tag. `model_type:*` values win ties. + * works. + * + * In `modelTypeMode` (backend declares `supports_model_type_tags`), candidates + * are the stripped `model_type:*` values plus any other non-reserved tags, and + * the most specific (deepest `parent/child`) candidate wins so a flat + * `model_type:LLM` never shadows a resolvable `LLM/Qwen-VL/...` tag. Otherwise + * we use the legacy first-non-reserved tag verbatim. */ -export function getAssetNodeCategory(asset: AssetItem): string | undefined { +export function getAssetNodeCategory( + asset: AssetItem, + modelTypeMode = false +): string | undefined { + if (!modelTypeMode) { + return asset.tags.find((tag) => tag !== MODELS_TAG && tag !== MISSING_TAG) + } + const otherTags = asset.tags.filter( (tag) => tag !== MODELS_TAG && From 86faaee3e104e9d16bd15925e6020d1df205fe23 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Fri, 19 Jun 2026 21:54:48 +1200 Subject: [PATCH 03/62] refactor(assets): run model-asset store on non-cloud builds Unwrap the if (isCloud) gate in assetsStore getModelState so the asset-mode Model Library works on non-cloud distributions (it previously returned an empty no-op store off-cloud, leaving the library blank even with the asset API enabled). Scoped to the model-asset store only; the rest of the isCloud forks are out of scope here. --- src/stores/assetsStore.test.ts | 29 ++ src/stores/assetsStore.ts | 851 ++++++++++++++++----------------- 2 files changed, 445 insertions(+), 435 deletions(-) diff --git a/src/stores/assetsStore.test.ts b/src/stores/assetsStore.test.ts index 7427e1279f4..f4ed1cd86f2 100644 --- a/src/stores/assetsStore.test.ts +++ b/src/stores/assetsStore.test.ts @@ -1875,6 +1875,35 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => { }) }) +describe('assetsStore - Model Assets Cache (non-cloud)', () => { + beforeEach(() => { + setActivePinia(createTestingPinia({ stubActions: false })) + mockIsCloud.value = false + vi.clearAllMocks() + }) + + it('caches model assets fetched by tag on non-cloud builds', async () => { + const store = useAssetsStore() + vi.mocked(assetService.getAssetsByTag).mockResolvedValue([ + { + id: 'm1', + name: 'sd_xl_base_1.0.safetensors', + tags: ['checkpoints', 'models'] + }, + { id: 'm2', name: 'lora.safetensors', tags: ['loras', 'models'] } + ]) + + await store.updateModelsForTag('models') + + expect(assetService.getAssetsByTag).toHaveBeenCalledWith( + 'models', + true, + expect.anything() + ) + expect(store.getAssets('tag:models')).toHaveLength(2) + }) +}) + describe('assetsStore - Deletion State and Input Mapping', () => { beforeEach(() => { setActivePinia(createTestingPinia({ stubActions: false })) diff --git a/src/stores/assetsStore.ts b/src/stores/assetsStore.ts index 5a15da82ebf..6587a374cea 100644 --- a/src/stores/assetsStore.ts +++ b/src/stores/assetsStore.ts @@ -410,504 +410,485 @@ export const useAssetsStore = defineStore('assets', () => { * Multiple node types sharing the same category share the same cache entry. * Public API accepts nodeType for backwards compatibility but translates * to category internally using modelToNodeStore.getCategoryForNodeType(). - * Cloud-only feature - empty Maps in desktop builds */ const getModelState = () => { - if (isCloud) { - const modelStateByCategory = ref(new Map()) - - const assetsArrayCache = new Map< - string, - { source: Map; array: AssetItem[] } - >() - - const pendingRequestByCategory = new Map() - const pendingPromiseByCategory = new Map>() - const abortByCategory = new Map() - - function createState( - existingAssets?: Map - ): ModelPaginationState { - const assets = new Map(existingAssets) - return reactive({ - assets, - offset: 0, - nextCursor: undefined, - hasMore: true, - isLoading: true - }) - } + const modelStateByCategory = ref(new Map()) + + const assetsArrayCache = new Map< + string, + { source: Map; array: AssetItem[] } + >() + + const pendingRequestByCategory = new Map() + const pendingPromiseByCategory = new Map>() + const abortByCategory = new Map() + + function createState( + existingAssets?: Map + ): ModelPaginationState { + const assets = new Map(existingAssets) + return reactive({ + assets, + offset: 0, + nextCursor: undefined, + hasMore: true, + isLoading: true + }) + } - function isStale(category: string, state: ModelPaginationState): boolean { - const committed = modelStateByCategory.value.get(category) - const pending = pendingRequestByCategory.get(category) - return committed !== state && pending !== state - } + function isStale(category: string, state: ModelPaginationState): boolean { + const committed = modelStateByCategory.value.get(category) + const pending = pendingRequestByCategory.get(category) + return committed !== state && pending !== state + } - const EMPTY_ASSETS: AssetItem[] = [] + const EMPTY_ASSETS: AssetItem[] = [] - /** - * Resolve a key to a category. Handles both nodeType and tag:xxx formats. - * @param key Either a nodeType (e.g., 'CheckpointLoaderSimple') or tag key (e.g., 'tag:models') - * @returns The category or undefined if not resolvable - */ - function resolveCategory(key: string): string | undefined { - if (key.startsWith('tag:')) { - return key - } - return modelToNodeStore.getCategoryForNodeType(key) + /** + * Resolve a key to a category. Handles both nodeType and tag:xxx formats. + * @param key Either a nodeType (e.g., 'CheckpointLoaderSimple') or tag key (e.g., 'tag:models') + * @returns The category or undefined if not resolvable + */ + function resolveCategory(key: string): string | undefined { + if (key.startsWith('tag:')) { + return key } + return modelToNodeStore.getCategoryForNodeType(key) + } - /** - * Get assets by nodeType or tag key. - * Translates nodeType to category internally for cache lookup. - * @param key Either a nodeType (e.g., 'CheckpointLoaderSimple') or tag key (e.g., 'tag:models') - */ - function getAssets(key: string): AssetItem[] { - const category = resolveCategory(key) - if (!category) return EMPTY_ASSETS - - const state = modelStateByCategory.value.get(category) - const assetsMap = state?.assets - if (!assetsMap) return EMPTY_ASSETS - - const cached = assetsArrayCache.get(category) - if (cached && cached.source === assetsMap) { - return cached.array - } + /** + * Get assets by nodeType or tag key. + * Translates nodeType to category internally for cache lookup. + * @param key Either a nodeType (e.g., 'CheckpointLoaderSimple') or tag key (e.g., 'tag:models') + */ + function getAssets(key: string): AssetItem[] { + const category = resolveCategory(key) + if (!category) return EMPTY_ASSETS - const array = Array.from(assetsMap.values()) - assetsArrayCache.set(category, { source: assetsMap, array }) - return array - } + const state = modelStateByCategory.value.get(category) + const assetsMap = state?.assets + if (!assetsMap) return EMPTY_ASSETS - function isLoading(key: string): boolean { - const category = resolveCategory(key) - if (!category) return false - return modelStateByCategory.value.get(category)?.isLoading ?? false + const cached = assetsArrayCache.get(category) + if (cached && cached.source === assetsMap) { + return cached.array } - function getError(key: string): Error | undefined { - const category = resolveCategory(key) - if (!category) return undefined - return modelStateByCategory.value.get(category)?.error - } + const array = Array.from(assetsMap.values()) + assetsArrayCache.set(category, { source: assetsMap, array }) + return array + } - function hasMore(key: string): boolean { - const category = resolveCategory(key) - if (!category) return false - return modelStateByCategory.value.get(category)?.hasMore ?? false - } + function isLoading(key: string): boolean { + const category = resolveCategory(key) + if (!category) return false + return modelStateByCategory.value.get(category)?.isLoading ?? false + } - function hasAssetKey(key: string): boolean { - const category = resolveCategory(key) - if (!category) return false - return modelStateByCategory.value.has(category) - } + function getError(key: string): Error | undefined { + const category = resolveCategory(key) + if (!category) return undefined + return modelStateByCategory.value.get(category)?.error + } - /** - * Check if a category exists in the cache. - * Checks both direct category keys and tag-prefixed keys. - * @param category The category to check (e.g., 'checkpoints', 'loras') - */ - function hasCategory(category: string): boolean { - return ( - modelStateByCategory.value.has(category) || - modelStateByCategory.value.has(`tag:${category}`) - ) + function hasMore(key: string): boolean { + const category = resolveCategory(key) + if (!category) return false + return modelStateByCategory.value.get(category)?.hasMore ?? false + } + + function hasAssetKey(key: string): boolean { + const category = resolveCategory(key) + if (!category) return false + return modelStateByCategory.value.has(category) + } + + /** + * Check if a category exists in the cache. + * Checks both direct category keys and tag-prefixed keys. + * @param category The category to check (e.g., 'checkpoints', 'loras') + */ + function hasCategory(category: string): boolean { + return ( + modelStateByCategory.value.has(category) || + modelStateByCategory.value.has(`tag:${category}`) + ) + } + + /** + * Internal helper to fetch and cache assets for a category. + * Loads first batch immediately, then progressively loads remaining batches. + * Keeps existing data visible until new data is successfully fetched. + * + * Concurrent calls for the same category are short-circuited: if a request + * is already in progress (tracked via pendingRequestByCategory), subsequent + * calls return immediately to avoid redundant work. + */ + async function updateModelsForCategory( + category: string, + fetcher: (options: AssetPaginationOptions) => Promise + ): Promise { + if (pendingPromiseByCategory.has(category)) { + return pendingPromiseByCategory.get(category)! } - /** - * Internal helper to fetch and cache assets for a category. - * Loads first batch immediately, then progressively loads remaining batches. - * Keeps existing data visible until new data is successfully fetched. - * - * Concurrent calls for the same category are short-circuited: if a request - * is already in progress (tracked via pendingRequestByCategory), subsequent - * calls return immediately to avoid redundant work. - */ - async function updateModelsForCategory( - category: string, - fetcher: (options: AssetPaginationOptions) => Promise - ): Promise { - if (pendingPromiseByCategory.has(category)) { - return pendingPromiseByCategory.get(category)! - } + const existingState = modelStateByCategory.value.get(category) + const state = createState(existingState?.assets) - const existingState = modelStateByCategory.value.get(category) - const state = createState(existingState?.assets) + abortByCategory.get(category)?.abort() + const controller = new AbortController() + abortByCategory.set(category, controller) - abortByCategory.get(category)?.abort() - const controller = new AbortController() - abortByCategory.set(category, controller) + const seenIds = new Set() - const seenIds = new Set() + const hasExistingData = modelStateByCategory.value.has(category) + if (hasExistingData) { + pendingRequestByCategory.set(category, state) + } else { + // Also track in pending map for initial loads to prevent concurrent calls + pendingRequestByCategory.set(category, state) + modelStateByCategory.value.set(category, state) + } - const hasExistingData = modelStateByCategory.value.has(category) - if (hasExistingData) { - pendingRequestByCategory.set(category, state) - } else { - // Also track in pending map for initial loads to prevent concurrent calls - pendingRequestByCategory.set(category, state) - modelStateByCategory.value.set(category, state) - } + async function loadBatches(): Promise { + let isFirstBatch = true + // Only prune unseen cached ids when the walk reaches a genuine end + // of list; bailing out on an anomaly (empty page, cursor cycle, + // batch cap) must not delete assets we simply never re-fetched. + let walkCompleted = false + // Cursors already requested this walk. A repeat means the server is + // cycling (immediate A->A or alternating A->B->A...), so we stop. + const seenCursors = new Set() + let batchCount = 0 + while (state.hasMore) { + if (batchCount++ >= MAX_PAGINATION_BATCHES) { + state.hasMore = false + break + } + try { + // Keyset cursor walk when the server provides one (stable under + // concurrent inserts/deletes); offset walk otherwise. The first + // batch sends offset 0, which the service omits from the request. + const after = state.nextCursor + const response = await fetcher( + after !== undefined + ? { + limit: MODEL_BATCH_SIZE, + after, + signal: controller.signal + } + : { + limit: MODEL_BATCH_SIZE, + offset: state.offset, + signal: controller.signal + } + ) - async function loadBatches(): Promise { - let isFirstBatch = true - // Only prune unseen cached ids when the walk reaches a genuine end - // of list; bailing out on an anomaly (empty page, cursor cycle, - // batch cap) must not delete assets we simply never re-fetched. - let walkCompleted = false - // Cursors already requested this walk. A repeat means the server is - // cycling (immediate A->A or alternating A->B->A...), so we stop. - const seenCursors = new Set() - let batchCount = 0 - while (state.hasMore) { - if (batchCount++ >= MAX_PAGINATION_BATCHES) { - state.hasMore = false - break - } - try { - // Keyset cursor walk when the server provides one (stable under - // concurrent inserts/deletes); offset walk otherwise. The first - // batch sends offset 0, which the service omits from the request. - const after = state.nextCursor - const response = await fetcher( - after !== undefined - ? { - limit: MODEL_BATCH_SIZE, - after, - signal: controller.signal - } - : { - limit: MODEL_BATCH_SIZE, - offset: state.offset, - signal: controller.signal - } - ) - - if (isStale(category, state)) return - - const newAssets = response.assets - - if (isFirstBatch) { - assetsArrayCache.delete(category) - if (hasExistingData) { - pendingRequestByCategory.delete(category) - modelStateByCategory.value.set(category, state) - } - } + if (isStale(category, state)) return - // Merge new assets into existing map and track seen IDs - for (const asset of newAssets) { - seenIds.add(asset.id) - state.assets.set(asset.id, asset) + const newAssets = response.assets + + if (isFirstBatch) { + assetsArrayCache.delete(category) + if (hasExistingData) { + pendingRequestByCategory.delete(category) + modelStateByCategory.value.set(category, state) } - state.assets = new Map(state.assets) + } - state.offset += newAssets.length + // Merge new assets into existing map and track seen IDs + for (const asset of newAssets) { + seenIds.add(asset.id) + state.assets.set(asset.id, asset) + } + state.assets = new Map(state.assets) - if (!response.has_more) { - // Server signalled a genuine end of list — safe to prune ids - // that no longer exist server-side. - state.hasMore = false - walkCompleted = true - } else if (newAssets.length === 0) { - // Anomaly: server claims more but returned an empty page. Stop, - // but do not prune — unseen cached assets may still live on a - // page we never received. + state.offset += newAssets.length + + if (!response.has_more) { + // Server signalled a genuine end of list — safe to prune ids + // that no longer exist server-side. + state.hasMore = false + walkCompleted = true + } else if (newAssets.length === 0) { + // Anomaly: server claims more but returned an empty page. Stop, + // but do not prune — unseen cached assets may still live on a + // page we never received. + state.hasMore = false + } else if (response.next_cursor !== undefined) { + if (seenCursors.has(response.next_cursor)) { + // Repeated cursor: the server is cycling. Stop without + // pruning rather than loop forever. state.hasMore = false - } else if (response.next_cursor !== undefined) { - if (seenCursors.has(response.next_cursor)) { - // Repeated cursor: the server is cycling. Stop without - // pruning rather than loop forever. - state.hasMore = false - } else { - seenCursors.add(response.next_cursor) - state.nextCursor = response.next_cursor - } } else { - // Cursor-less backend: legacy offset walk. A short page is the - // reliable natural end of the list, so it is safe to prune. - state.nextCursor = undefined - if (newAssets.length < MODEL_BATCH_SIZE) { - state.hasMore = false - walkCompleted = true - } + seenCursors.add(response.next_cursor) + state.nextCursor = response.next_cursor } - - if (isFirstBatch) { - state.isLoading = false - isFirstBatch = false - } - - if (state.hasMore) { - await delay(50, { signal: controller.signal }) - } - } catch (err) { - if (controller.signal.aborted) { - if (isFirstBatch) state.isLoading = false - return + } else { + // Cursor-less backend: legacy offset walk. A short page is the + // reliable natural end of the list, so it is safe to prune. + state.nextCursor = undefined + if (newAssets.length < MODEL_BATCH_SIZE) { + state.hasMore = false + walkCompleted = true } - if (isStale(category, state)) return - console.error(`Error loading batch for ${category}:`, err) + } - state.error = err instanceof Error ? err : new Error(String(err)) - state.hasMore = false + if (isFirstBatch) { state.isLoading = false - pendingRequestByCategory.delete(category) + isFirstBatch = false + } + if (state.hasMore) { + await delay(50, { signal: controller.signal }) + } + } catch (err) { + if (controller.signal.aborted) { + if (isFirstBatch) state.isLoading = false return } - } + if (isStale(category, state)) return + console.error(`Error loading batch for ${category}:`, err) - // Prune ids that vanished server-side, but only after a complete - // walk — an early bail-out hasn't seen the whole list, so unseen - // cached assets must be kept rather than deleted. - if (walkCompleted) { - const staleIds = [...state.assets.keys()].filter( - (id) => !seenIds.has(id) - ) - for (const id of staleIds) { - state.assets.delete(id) - } + state.error = err instanceof Error ? err : new Error(String(err)) + state.hasMore = false + state.isLoading = false + pendingRequestByCategory.delete(category) + + return } - assetsArrayCache.delete(category) - pendingRequestByCategory.delete(category) } - const promise = loadBatches() - pendingPromiseByCategory.set(category, promise) - try { - await promise - } finally { - // Only clear the guards if this walk still owns them — an - // invalidateCategory() + fresh walk can register a newer promise and - // controller for the same category before this teardown runs. - if (pendingPromiseByCategory.get(category) === promise) { - pendingPromiseByCategory.delete(category) - } - if (abortByCategory.get(category) === controller) { - abortByCategory.delete(category) + // Prune ids that vanished server-side, but only after a complete + // walk — an early bail-out hasn't seen the whole list, so unseen + // cached assets must be kept rather than deleted. + if (walkCompleted) { + const staleIds = [...state.assets.keys()].filter( + (id) => !seenIds.has(id) + ) + for (const id of staleIds) { + state.assets.delete(id) } } + assetsArrayCache.delete(category) + pendingRequestByCategory.delete(category) } - /** - * Fetch and cache model assets for a specific node type. - * Translates nodeType to category internally - multiple node types - * sharing the same category will share the same cache entry. - * @param nodeType The node type to fetch assets for (e.g., 'CheckpointLoaderSimple') - */ - async function updateModelsForNodeType(nodeType: string): Promise { - const category = modelToNodeStore.getCategoryForNodeType(nodeType) - if (!category) return - - // Use category as cache key but fetch using nodeType for API compatibility - await updateModelsForCategory(category, (opts) => - assetService.getAssetsPageForNodeType(nodeType, opts) - ) + const promise = loadBatches() + pendingPromiseByCategory.set(category, promise) + try { + await promise + } finally { + // Only clear the guards if this walk still owns them — an + // invalidateCategory() + fresh walk can register a newer promise and + // controller for the same category before this teardown runs. + if (pendingPromiseByCategory.get(category) === promise) { + pendingPromiseByCategory.delete(category) + } + if (abortByCategory.get(category) === controller) { + abortByCategory.delete(category) + } } + } - /** - * Fetch and cache model assets for a specific tag - * @param tag The tag to fetch assets for (e.g., 'models') - */ - async function updateModelsForTag(tag: string): Promise { - const category = `tag:${tag}` - await updateModelsForCategory(category, (opts) => - assetService.getAssetsPageByTag(tag, true, opts) - ) - } + /** + * Fetch and cache model assets for a specific node type. + * Translates nodeType to category internally - multiple node types + * sharing the same category will share the same cache entry. + * @param nodeType The node type to fetch assets for (e.g., 'CheckpointLoaderSimple') + */ + async function updateModelsForNodeType(nodeType: string): Promise { + const category = modelToNodeStore.getCategoryForNodeType(nodeType) + if (!category) return - /** - * Invalidate the cache for a specific category. - * Forces a refetch on next access. - * @param category The category to invalidate (e.g., 'checkpoints', 'loras') - */ - function invalidateCategory(category: string): void { - abortByCategory.get(category)?.abort() - abortByCategory.delete(category) - modelStateByCategory.value.delete(category) - assetsArrayCache.delete(category) - pendingRequestByCategory.delete(category) - pendingPromiseByCategory.delete(category) - } + // Use category as cache key but fetch using nodeType for API compatibility + await updateModelsForCategory(category, (opts) => + assetService.getAssetsPageForNodeType(nodeType, opts) + ) + } - /** - * Optimistically update an asset in the cache - * @param assetId The asset ID to update - * @param updates Partial asset data to merge - * @param cacheKey Optional cache key to target (nodeType or 'tag:xxx') - */ - function updateAssetInCache( - assetId: string, - updates: Partial, - cacheKey?: string - ) { - const category = cacheKey ? resolveCategory(cacheKey) : undefined - if (cacheKey && !category) return - - const categoriesToCheck = category - ? [category] - : Array.from(modelStateByCategory.value.keys()) - - for (const cat of categoriesToCheck) { - const state = modelStateByCategory.value.get(cat) - if (!state?.assets) continue - - const existingAsset = state.assets.get(assetId) - if (existingAsset) { - const updatedAsset = { ...existingAsset, ...updates } - state.assets.set(assetId, updatedAsset) - assetsArrayCache.delete(cat) - if (cacheKey) return - } + /** + * Fetch and cache model assets for a specific tag + * @param tag The tag to fetch assets for (e.g., 'models') + */ + async function updateModelsForTag(tag: string): Promise { + const category = `tag:${tag}` + await updateModelsForCategory(category, (opts) => + assetService.getAssetsPageByTag(tag, true, opts) + ) + } + + /** + * Invalidate the cache for a specific category. + * Forces a refetch on next access. + * @param category The category to invalidate (e.g., 'checkpoints', 'loras') + */ + function invalidateCategory(category: string): void { + abortByCategory.get(category)?.abort() + abortByCategory.delete(category) + modelStateByCategory.value.delete(category) + assetsArrayCache.delete(category) + pendingRequestByCategory.delete(category) + pendingPromiseByCategory.delete(category) + } + + /** + * Optimistically update an asset in the cache + * @param assetId The asset ID to update + * @param updates Partial asset data to merge + * @param cacheKey Optional cache key to target (nodeType or 'tag:xxx') + */ + function updateAssetInCache( + assetId: string, + updates: Partial, + cacheKey?: string + ) { + const category = cacheKey ? resolveCategory(cacheKey) : undefined + if (cacheKey && !category) return + + const categoriesToCheck = category + ? [category] + : Array.from(modelStateByCategory.value.keys()) + + for (const cat of categoriesToCheck) { + const state = modelStateByCategory.value.get(cat) + if (!state?.assets) continue + + const existingAsset = state.assets.get(assetId) + if (existingAsset) { + const updatedAsset = { ...existingAsset, ...updates } + state.assets.set(assetId, updatedAsset) + assetsArrayCache.delete(cat) + if (cacheKey) return } } + } - /** - * Update asset metadata with optimistic cache update - * @param asset The asset to update - * @param userMetadata The user_metadata to save - * @param cacheKey Optional cache key to target for optimistic update - */ - async function updateAssetMetadata( - asset: AssetItem, - userMetadata: Record, - cacheKey?: string - ) { - const originalMetadata = asset.user_metadata - updateAssetInCache(asset.id, { user_metadata: userMetadata }, cacheKey) - - try { - const updatedAsset = await assetService.updateAsset(asset.id, { - user_metadata: userMetadata - }) - updateAssetInCache(asset.id, updatedAsset, cacheKey) - } catch (error) { - console.error('Failed to update asset metadata:', error) - updateAssetInCache( + /** + * Update asset metadata with optimistic cache update + * @param asset The asset to update + * @param userMetadata The user_metadata to save + * @param cacheKey Optional cache key to target for optimistic update + */ + async function updateAssetMetadata( + asset: AssetItem, + userMetadata: Record, + cacheKey?: string + ) { + const originalMetadata = asset.user_metadata + updateAssetInCache(asset.id, { user_metadata: userMetadata }, cacheKey) + + try { + const updatedAsset = await assetService.updateAsset(asset.id, { + user_metadata: userMetadata + }) + updateAssetInCache(asset.id, updatedAsset, cacheKey) + } catch (error) { + console.error('Failed to update asset metadata:', error) + updateAssetInCache( + asset.id, + { user_metadata: originalMetadata }, + cacheKey + ) + } + } + + /** + * Update asset tags using add/remove endpoints + * @param asset The asset to update (used to read current tags) + * @param newTags The desired tags array + * @param cacheKey Optional cache key to target for optimistic update + */ + async function updateAssetTags( + asset: AssetItem, + newTags: string[], + cacheKey?: string + ) { + const originalTags = asset.tags + const tagsToAdd = difference(newTags, originalTags) + const tagsToRemove = difference(originalTags, newTags) + + if (tagsToAdd.length === 0 && tagsToRemove.length === 0) return + + updateAssetInCache(asset.id, { tags: newTags }, cacheKey) + + let removedTagsOnServer: string[] = [] + try { + let removeResult: TagsOperationResult | undefined + if (tagsToRemove.length > 0) { + removeResult = await assetService.removeAssetTags( asset.id, - { user_metadata: originalMetadata }, - cacheKey + tagsToRemove ) + removedTagsOnServer = removeResult.removed ?? tagsToRemove } - } - /** - * Update asset tags using add/remove endpoints - * @param asset The asset to update (used to read current tags) - * @param newTags The desired tags array - * @param cacheKey Optional cache key to target for optimistic update - */ - async function updateAssetTags( - asset: AssetItem, - newTags: string[], - cacheKey?: string - ) { - const originalTags = asset.tags - const tagsToAdd = difference(newTags, originalTags) - const tagsToRemove = difference(originalTags, newTags) - - if (tagsToAdd.length === 0 && tagsToRemove.length === 0) return - - updateAssetInCache(asset.id, { tags: newTags }, cacheKey) - - let removedTagsOnServer: string[] = [] - try { - let removeResult: TagsOperationResult | undefined - if (tagsToRemove.length > 0) { - removeResult = await assetService.removeAssetTags( - asset.id, - tagsToRemove - ) - removedTagsOnServer = removeResult.removed ?? tagsToRemove - } - - const addResult = - tagsToAdd.length > 0 - ? await assetService.addAssetTags(asset.id, tagsToAdd) - : undefined + const addResult = + tagsToAdd.length > 0 + ? await assetService.addAssetTags(asset.id, tagsToAdd) + : undefined - const finalTags = (addResult ?? removeResult)?.total_tags - if (finalTags) { - updateAssetInCache(asset.id, { tags: finalTags }, cacheKey) - } - } catch (error) { - console.error('Failed to update asset tags:', error) - updateAssetInCache(asset.id, { tags: originalTags }, cacheKey) - - if (removedTagsOnServer.length > 0) { - try { - await assetService.addAssetTags(asset.id, removedTagsOnServer) - } catch (compensationError) { - console.error( - 'Failed to restore tags after partial failure; invalidating cache to force refetch:', - compensationError - ) - const categoriesToInvalidate = new Set() - const resolved = cacheKey ? resolveCategory(cacheKey) : undefined - if (resolved) { - categoriesToInvalidate.add(resolved) - } - for (const [ - category, - state - ] of modelStateByCategory.value.entries()) { - if (state.assets?.has(asset.id)) { - categoriesToInvalidate.add(category) - } - } - for (const category of categoriesToInvalidate) { - invalidateCategory(category) + const finalTags = (addResult ?? removeResult)?.total_tags + if (finalTags) { + updateAssetInCache(asset.id, { tags: finalTags }, cacheKey) + } + } catch (error) { + console.error('Failed to update asset tags:', error) + updateAssetInCache(asset.id, { tags: originalTags }, cacheKey) + + if (removedTagsOnServer.length > 0) { + try { + await assetService.addAssetTags(asset.id, removedTagsOnServer) + } catch (compensationError) { + console.error( + 'Failed to restore tags after partial failure; invalidating cache to force refetch:', + compensationError + ) + const categoriesToInvalidate = new Set() + const resolved = cacheKey ? resolveCategory(cacheKey) : undefined + if (resolved) { + categoriesToInvalidate.add(resolved) + } + for (const [ + category, + state + ] of modelStateByCategory.value.entries()) { + if (state.assets?.has(asset.id)) { + categoriesToInvalidate.add(category) } } + for (const category of categoriesToInvalidate) { + invalidateCategory(category) + } } } } + } - /** - * Invalidate model caches for a given category (e.g., 'checkpoints', 'loras') - * Clears the category cache and tag-based caches so next access triggers refetch - * @param category The model category to invalidate (e.g., 'checkpoints') - */ - function invalidateModelsForCategory(category: string): void { - invalidateCategory(category) - invalidateCategory(`tag:${category}`) - invalidateCategory('tag:models') - } - - return { - getAssets, - isLoading, - getError, - hasMore, - hasAssetKey, - hasCategory, - updateModelsForNodeType, - updateModelsForTag, - invalidateCategory, - updateAssetMetadata, - updateAssetTags, - invalidateModelsForCategory - } + /** + * Invalidate model caches for a given category (e.g., 'checkpoints', 'loras') + * Clears the category cache and tag-based caches so next access triggers refetch + * @param category The model category to invalidate (e.g., 'checkpoints') + */ + function invalidateModelsForCategory(category: string): void { + invalidateCategory(category) + invalidateCategory(`tag:${category}`) + invalidateCategory('tag:models') } - const emptyAssets: AssetItem[] = [] return { - getAssets: () => emptyAssets, - isLoading: () => false, - getError: () => undefined, - hasMore: () => false, - hasAssetKey: () => false, - hasCategory: () => false, - updateModelsForNodeType: async () => {}, - invalidateCategory: () => {}, - updateModelsForTag: async () => {}, - updateAssetMetadata: async () => {}, - updateAssetTags: async () => {}, - invalidateModelsForCategory: () => {} + getAssets, + isLoading, + getError, + hasMore, + hasAssetKey, + hasCategory, + updateModelsForNodeType, + updateModelsForTag, + invalidateCategory, + updateAssetMetadata, + updateAssetTags, + invalidateModelsForCategory } } From 1897ec5c3b004f2fbe0cbd05c668e898a3656e1d Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Sun, 21 Jun 2026 13:50:01 +1200 Subject: [PATCH 04/62] fix(assets): strip model_type: prefix from asset card type badge The card type badge derived from the first raw non-models tag, so in model_type mode it rendered the literal `model_type:checkpoints`. Route it through a model_type-aware getAssetTypeBadge and key the display cache on the flag so a late-arriving feature flag re-derives the badge. Also adds docstrings to the tag helpers to satisfy coverage. --- .../composables/useAssetBrowser.test.ts | 19 +++++++++ .../assets/composables/useAssetBrowser.ts | 40 +++++++++++-------- .../assets/utils/assetMetadataUtils.test.ts | 32 +++++++++++++++ .../assets/utils/assetMetadataUtils.ts | 27 +++++++++++++ 4 files changed, 102 insertions(+), 16 deletions(-) diff --git a/src/platform/assets/composables/useAssetBrowser.test.ts b/src/platform/assets/composables/useAssetBrowser.test.ts index ea100565ae2..d1cbecc1cfc 100644 --- a/src/platform/assets/composables/useAssetBrowser.test.ts +++ b/src/platform/assets/composables/useAssetBrowser.test.ts @@ -150,6 +150,25 @@ describe('useAssetBrowser', () => { }) }) + it('strips the model_type: prefix from the badge when the flag is on', () => { + mockSupportsModelTypeTags.value = true + const apiAsset = createApiAsset({ + tags: ['models', 'model_type:checkpoints', 'sdxl'] + }) + + const { filteredAssets } = useAssetBrowser(ref([apiAsset])) + const result = filteredAssets.value[0] + + expect(result.badges).toContainEqual({ + label: 'checkpoints', + type: 'type' + }) + expect(result.badges).not.toContainEqual({ + label: 'model_type:checkpoints', + type: 'type' + }) + }) + it('handles tags with multiple slashes in badges', () => { const apiAsset = createApiAsset({ tags: ['models', 'checkpoint/subfolder/model-name'] diff --git a/src/platform/assets/composables/useAssetBrowser.ts b/src/platform/assets/composables/useAssetBrowser.ts index eba491ae7b6..3eb331a4b13 100644 --- a/src/platform/assets/composables/useAssetBrowser.ts +++ b/src/platform/assets/composables/useAssetBrowser.ts @@ -20,7 +20,8 @@ import { import { getAssetBaseModels, getAssetCategories, - getAssetFilename + getAssetFilename, + getAssetTypeBadge } from '@/platform/assets/utils/assetMetadataUtils' import { MODELS_TAG } from '@/platform/assets/services/assetService' import { sortAssets } from '@/platform/assets/utils/assetSortUtils' @@ -45,18 +46,20 @@ export interface AssetDisplayItem extends AssetItem { } } -const displayItemCache = new WeakMap() +const displayItemCache = new WeakMap< + AssetItem, + { modelTypeMode: boolean; item: AssetDisplayItem } +>() -function buildDisplayItem(asset: AssetItem): AssetDisplayItem { +function buildDisplayItem( + asset: AssetItem, + modelTypeMode: boolean +): AssetDisplayItem { const badges: AssetBadge[] = [] - const typeTag = asset.tags.find((tag) => tag !== 'models') - if (typeTag) { - const badgeLabel = typeTag.includes('/') - ? typeTag.substring(typeTag.indexOf('/') + 1) - : typeTag - - badges.push({ label: badgeLabel, type: 'type' }) + const typeBadge = getAssetTypeBadge(asset, modelTypeMode) + if (typeBadge) { + badges.push({ label: typeBadge, type: 'type' }) } for (const model of getAssetBaseModels(asset)) { @@ -77,12 +80,15 @@ function buildDisplayItem(asset: AssetItem): AssetDisplayItem { } } -function transformAssetForDisplay(asset: AssetItem): AssetDisplayItem { +function transformAssetForDisplay( + asset: AssetItem, + modelTypeMode: boolean +): AssetDisplayItem { const cached = displayItemCache.get(asset) - if (cached) return cached - const built = buildDisplayItem(asset) - displayItemCache.set(asset, built) - return built + if (cached && cached.modelTypeMode === modelTypeMode) return cached.item + const item = buildDisplayItem(asset, modelTypeMode) + displayItemCache.set(asset, { modelTypeMode, item }) + return item } /** @@ -252,7 +258,9 @@ export function useAssetBrowser( const sortedAssets = sortAssets(filtered, filters.value.sortBy) // Transform to display format - return sortedAssets.map(transformAssetForDisplay) + return sortedAssets.map((asset) => + transformAssetForDisplay(asset, flags.supportsModelTypeTags) + ) }) function updateFilters(newFilters: AssetFilterState) { diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index d0c9e4efcff..f414a1dbeab 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -17,6 +17,7 @@ import { getAssetSourceUrl, getAssetStoredFilename, getAssetTriggerPhrases, + getAssetTypeBadge, getAssetUserDescription, getSourceName, resolveDisplayImageDimensions @@ -670,3 +671,34 @@ describe('getAssetNodeCategory', () => { ) }) }) + +describe('getAssetTypeBadge', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('strips the model_type: prefix in model_type mode (no raw leak)', () => { + expect( + getAssetTypeBadge( + asset(['models', 'model_type:checkpoints', 'sdxl']), + true + ) + ).toBe('checkpoints') + }) + + it('leaks the literal model_type: tag when mode is off (default)', () => { + expect(getAssetTypeBadge(asset(['models', 'model_type:checkpoints']))).toBe( + 'model_type:checkpoints' + ) + }) + + it('shows the segment after the first slash for a bare hierarchical tag', () => { + expect(getAssetTypeBadge(asset(['models', 'checkpoint/xl']))).toBe('xl') + }) + + it('returns undefined when only the models tag is present', () => { + expect(getAssetTypeBadge(asset(['models']), true)).toBeUndefined() + }) +}) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 53af8faff47..662988733f1 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -156,6 +156,7 @@ export function getAssetModelType(asset: AssetItem): string | null { const MODEL_TYPE_TAG_PREFIX = 'model_type:' +/** Strips the `model_type:` prefix off each namespaced tag, dropping non-`model_type:` tags. */ function getModelTypeTagValues(asset: AssetItem): string[] { return asset.tags .filter((tag) => tag.startsWith(MODEL_TYPE_TAG_PREFIX)) @@ -163,6 +164,7 @@ function getModelTypeTagValues(asset: AssetItem): string[] { .filter((tag) => tag.length > 0) } +/** Legacy grouping: each non-`models` tag's top-level path segment. */ function getBareTagCategories(asset: AssetItem): string[] { return asset.tags .filter((tag) => tag !== MODELS_TAG && tag.length > 0) @@ -190,10 +192,35 @@ export function getAssetCategories( return getBareTagCategories(asset) } +/** Number of `parent/child` segments in a tag, used to pick the most specific. */ function pathDepth(tag: string): number { return tag.split('/').length } +/** + * Resolves the short label shown on an asset card's type badge. + * + * In `modelTypeMode` a `model_type:*` tag's stripped value is preferred so the + * badge never leaks the raw `model_type:` namespace; otherwise (and as a + * fallback for an uncovered asset) it uses the first non-`models` tag, showing + * the segment after the first `/` for hierarchical tags. + */ +export function getAssetTypeBadge( + asset: AssetItem, + modelTypeMode = false +): string | undefined { + if (modelTypeMode) { + const [modelType] = getModelTypeTagValues(asset) + if (modelType) return modelType + } + + const typeTag = asset.tags.find((tag) => tag !== MODELS_TAG) + if (!typeTag) return undefined + return typeTag.includes('/') + ? typeTag.slice(typeTag.indexOf('/') + 1) + : typeTag +} + /** * Resolves the model-type key used to look up a node provider for an asset. * From 31c043ead3c4519c908f44a14ffffa49d75a53a2 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Mon, 22 Jun 2026 22:06:22 +1200 Subject: [PATCH 05/62] fix(assets): strip model_type: prefix from the asset browser title Also realigns the card type badge to the same rule: keep the existing "first non-models tag" selection and only strip the model_type: namespace prefix, rather than reordering to prefer the model_type: value. So an asset like [models, foo, model_type:bar] still shows foo, while [models, model_type:checkpoints] shows checkpoints. Title (primaryCategoryTag) strips the same prefix when the flag is on. --- .../components/AssetBrowserModal.test.ts | 30 +++++++++++++++++++ .../assets/components/AssetBrowserModal.vue | 9 +++++- .../assets/utils/assetMetadataUtils.test.ts | 20 ++++++++++++- .../assets/utils/assetMetadataUtils.ts | 23 ++++++++------ 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/platform/assets/components/AssetBrowserModal.test.ts b/src/platform/assets/components/AssetBrowserModal.test.ts index baf5f1ed78d..d5e6974b8c4 100644 --- a/src/platform/assets/components/AssetBrowserModal.test.ts +++ b/src/platform/assets/components/AssetBrowserModal.test.ts @@ -9,6 +9,20 @@ import { useAssetsStore } from '@/stores/assetsStore' const mockAssetsByKey = vi.hoisted(() => new Map()) const mockLoadingByKey = vi.hoisted(() => new Map()) +const mockSupportsModelTypeTags = vi.hoisted(() => ({ value: false })) + +vi.mock('@/composables/useFeatureFlags', () => ({ + useFeatureFlags: () => ({ + flags: { + get supportsModelTypeTags() { + return mockSupportsModelTypeTags.value + }, + get modelUploadButtonEnabled() { + return false + } + } + }) +})) vi.mock('@/i18n', () => ({ t: (key: string, params?: Record) => @@ -226,6 +240,7 @@ describe('AssetBrowserModal', () => { vi.resetAllMocks() mockAssetsByKey.clear() mockLoadingByKey.clear() + mockSupportsModelTypeTags.value = false }) describe('Integration with useAssetBrowser', () => { @@ -432,5 +447,20 @@ describe('AssetBrowserModal', () => { 'assetBrowser.allCategory:{"category":"Checkpoints"}' ) }) + + it('strips the model_type: prefix from the title when the flag is on', async () => { + mockSupportsModelTypeTags.value = true + const assets = [ + createTestAsset('asset1', 'Model A', 'model_type:checkpoints') + ] + mockAssetsByKey.set('CheckpointLoaderSimple', assets) + + renderModal({ nodeType: 'CheckpointLoaderSimple' }) + await flushPromises() + + expect(screen.getByTestId('modal-title').textContent).toBe( + 'assetBrowser.allCategory:{"category":"Checkpoints"}' + ) + }) }) }) diff --git a/src/platform/assets/components/AssetBrowserModal.vue b/src/platform/assets/components/AssetBrowserModal.vue index 85a79605f7c..20e99da3b11 100644 --- a/src/platform/assets/components/AssetBrowserModal.vue +++ b/src/platform/assets/components/AssetBrowserModal.vue @@ -100,6 +100,7 @@ import SearchInput from '@/components/ui/search-input/SearchInput.vue' import Button from '@/components/ui/button/Button.vue' import BaseModalLayout from '@/components/widget/layout/BaseModalLayout.vue' import LeftSidePanel from '@/components/widget/panel/LeftSidePanel.vue' +import { useFeatureFlags } from '@/composables/useFeatureFlags' import { usePrimeVueOverlayChildStyle } from '@/composables/usePopoverSizing' import AssetFilterBar from '@/platform/assets/components/AssetFilterBar.vue' import AssetGrid from '@/platform/assets/components/AssetGrid.vue' @@ -109,12 +110,14 @@ import { useAssetBrowser } from '@/platform/assets/composables/useAssetBrowser' import { useModelTypes } from '@/platform/assets/composables/useModelTypes' import { useModelUpload } from '@/platform/assets/composables/useModelUpload' import type { AssetItem } from '@/platform/assets/schemas/assetSchema' +import { stripModelTypePrefix } from '@/platform/assets/utils/assetMetadataUtils' import { formatCategoryLabel } from '@/platform/assets/utils/categoryLabel' import { useAssetsStore } from '@/stores/assetsStore' import { useModelToNodeStore } from '@/stores/modelToNodeStore' import { OnCloseKey } from '@/types/widgetTypes' const { t } = useI18n() +const { flags } = useFeatureFlags() const assetStore = useAssetsStore() const modelToNodeStore = useModelToNodeStore() const breakpoints = useBreakpoints(breakpointsTailwind) @@ -196,7 +199,11 @@ const primaryCategoryTag = computed(() => { .map((asset) => asset.tags?.find((tag) => tag !== 'models')) .find((tag): tag is string => typeof tag === 'string' && tag.length > 0) - if (tagFromAssets) return tagFromAssets + if (tagFromAssets) { + return flags.supportsModelTypeTags + ? stripModelTypePrefix(tagFromAssets) + : tagFromAssets + } if (props.nodeType) { const mapped = modelToNodeStore.getCategoryForNodeType(props.nodeType) diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index f414a1dbeab..83227aa574b 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -20,7 +20,8 @@ import { getAssetTypeBadge, getAssetUserDescription, getSourceName, - resolveDisplayImageDimensions + resolveDisplayImageDimensions, + stripModelTypePrefix } from '@/platform/assets/utils/assetMetadataUtils' const { isCloudRef } = vi.hoisted(() => ({ @@ -688,6 +689,12 @@ describe('getAssetTypeBadge', () => { ).toBe('checkpoints') }) + it('keeps the first non-models tag rather than reordering to the model_type value', () => { + expect( + getAssetTypeBadge(asset(['models', 'foo', 'model_type:bar']), true) + ).toBe('foo') + }) + it('leaks the literal model_type: tag when mode is off (default)', () => { expect(getAssetTypeBadge(asset(['models', 'model_type:checkpoints']))).toBe( 'model_type:checkpoints' @@ -702,3 +709,14 @@ describe('getAssetTypeBadge', () => { expect(getAssetTypeBadge(asset(['models']), true)).toBeUndefined() }) }) + +describe('stripModelTypePrefix', () => { + it('removes the model_type: prefix when present', () => { + expect(stripModelTypePrefix('model_type:checkpoints')).toBe('checkpoints') + }) + + it('leaves a tag without the prefix unchanged', () => { + expect(stripModelTypePrefix('checkpoints')).toBe('checkpoints') + expect(stripModelTypePrefix('checkpoint/xl')).toBe('checkpoint/xl') + }) +}) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 662988733f1..262602bec67 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -197,25 +197,30 @@ function pathDepth(tag: string): number { return tag.split('/').length } +/** Removes the `model_type:` namespace prefix from a tag when present. */ +export function stripModelTypePrefix(tag: string): string { + return tag.startsWith(MODEL_TYPE_TAG_PREFIX) + ? tag.slice(MODEL_TYPE_TAG_PREFIX.length) + : tag +} + /** * Resolves the short label shown on an asset card's type badge. * - * In `modelTypeMode` a `model_type:*` tag's stripped value is preferred so the - * badge never leaks the raw `model_type:` namespace; otherwise (and as a - * fallback for an uncovered asset) it uses the first non-`models` tag, showing - * the segment after the first `/` for hierarchical tags. + * Uses the first non-`models` tag (unchanged selection); in `modelTypeMode` a + * `model_type:` namespace prefix on that tag is stripped so the badge doesn't + * leak the raw namespace. Bare hierarchical tags still show the segment after + * the first `/`. */ export function getAssetTypeBadge( asset: AssetItem, modelTypeMode = false ): string | undefined { - if (modelTypeMode) { - const [modelType] = getModelTypeTagValues(asset) - if (modelType) return modelType - } - const typeTag = asset.tags.find((tag) => tag !== MODELS_TAG) if (!typeTag) return undefined + if (modelTypeMode && typeTag.startsWith(MODEL_TYPE_TAG_PREFIX)) { + return stripModelTypePrefix(typeTag) + } return typeTag.includes('/') ? typeTag.slice(typeTag.indexOf('/') + 1) : typeTag From 979f01f18145297b428724c82c3f9797e0739a85 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Wed, 24 Jun 2026 08:52:14 +1200 Subject: [PATCH 06/62] fix(assets): port model_type node-category resolution to resolveModelNodeFromAsset main (#12765) renamed createModelNodeFromAsset -> resolveModelNodeFromAsset. Re-apply FE-1076's flag-gated node-category resolution there: use getAssetNodeCategory(asset, flags.supportsModelTypeTags) instead of the inline first-non-reserved-tag lookup, so a model_type:* tag resolves the loader when the supports_model_type_tags flag is on. --- .../utils/resolveModelNodeFromAsset.test.ts | 23 +++++++++++++++++++ .../assets/utils/resolveModelNodeFromAsset.ts | 11 +++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts index fd849d19868..878a11a6cfb 100644 --- a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts +++ b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts @@ -4,11 +4,22 @@ import type { AssetItem } from '@/platform/assets/schemas/assetSchema' import { resolveModelNodeFromAsset } from '@/platform/assets/utils/resolveModelNodeFromAsset' const mockGetNodeProvider = vi.hoisted(() => vi.fn()) +const mockSupportsModelTypeTags = vi.hoisted(() => ({ value: false })) vi.mock('@/stores/modelToNodeStore', () => ({ useModelToNodeStore: () => ({ getNodeProvider: mockGetNodeProvider }) })) +vi.mock('@/composables/useFeatureFlags', () => ({ + useFeatureFlags: () => ({ + flags: { + get supportsModelTypeTags() { + return mockSupportsModelTypeTags.value + } + } + }) +})) + function createMockAsset(overrides: Partial = {}): AssetItem { return { id: 'asset-123', @@ -49,6 +60,7 @@ describe('resolveModelNodeFromAsset', () => { beforeEach(() => { vi.clearAllMocks() vi.spyOn(console, 'error').mockImplementation(() => {}) + mockSupportsModelTypeTags.value = false }) describe('valid assets', () => { @@ -68,6 +80,17 @@ describe('resolveModelNodeFromAsset', () => { } }) + it('strips the model_type: prefix when resolving the provider in model_type mode', () => { + mockSupportsModelTypeTags.value = true + mockProvider(createMockNodeProvider()) + const result = resolveModelNodeFromAsset( + createMockAsset({ tags: ['models', 'model_type:vae'] }) + ) + + expect(result.success).toBe(true) + expect(mockGetNodeProvider).toHaveBeenCalledWith('vae') + }) + it('falls back to metadata.filename when user_metadata.filename missing', () => { mockProvider(createMockNodeProvider()) const result = resolveModelNodeFromAsset( diff --git a/src/platform/assets/utils/resolveModelNodeFromAsset.ts b/src/platform/assets/utils/resolveModelNodeFromAsset.ts index 411ca94c78d..3d74da5d895 100644 --- a/src/platform/assets/utils/resolveModelNodeFromAsset.ts +++ b/src/platform/assets/utils/resolveModelNodeFromAsset.ts @@ -4,7 +4,11 @@ import { MISSING_TAG, MODELS_TAG } from '@/platform/assets/services/assetService' -import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils' +import { + getAssetFilename, + getAssetNodeCategory +} from '@/platform/assets/utils/assetMetadataUtils' +import { useFeatureFlags } from '@/composables/useFeatureFlags' import { useModelToNodeStore } from '@/stores/modelToNodeStore' import type { ModelNodeProvider } from '@/stores/modelToNodeStore' @@ -81,9 +85,8 @@ export function resolveModelNodeFromAsset( } } - const category = validAsset.tags.find( - (tag) => tag !== MODELS_TAG && tag !== MISSING_TAG - ) + const { flags } = useFeatureFlags() + const category = getAssetNodeCategory(validAsset, flags.supportsModelTypeTags) if (!category) { console.error( `Asset ${validAsset.id} has no valid category tag. Available tags: ${validAsset.tags.join(', ')} (expected tag other than '${MODELS_TAG}' or '${MISSING_TAG}')` From c8af4c553fddda7c9912b371b75c9e4bcd47aedc Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Wed, 24 Jun 2026 09:22:27 +1200 Subject: [PATCH 07/62] fix(assets): keep reserved-tag constants local in assetMetadataUtils Importing MODELS_TAG/MISSING_TAG from assetService made this leaf util pull the assetService -> i18n (createI18n) chain. Test suites that import assetMetadataUtils (e.g. useAssetFilterOptions, AssetsSidebarListView) with a minimal vue-i18n mock then failed to load. Define the two reserved-tag literals locally so the util has no heavy transitive dependency. --- src/platform/assets/utils/assetMetadataUtils.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 262602bec67..975c826c076 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -1,11 +1,12 @@ import type { AssetItem } from '@/platform/assets/schemas/assetSchema' -import { - MISSING_TAG, - MODELS_TAG -} from '@/platform/assets/services/assetService' import { isCloud } from '@/platform/distribution/types' import { isCivitaiUrl } from '@/utils/formatUtil' +// Reserved tag literals (mirror assetService's MODELS_TAG/MISSING_TAG). Kept +// local so this leaf util doesn't pull the heavier assetService -> i18n chain. +const MODELS_TAG = 'models' +const MISSING_TAG = 'missing' + /** * Type-safe utilities for extracting metadata from assets. * These utilities check user_metadata first, then metadata, then fallback. From 7117caf055530d69ec16e5cfc33c76a1c42d3dd3 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Wed, 24 Jun 2026 10:11:13 +1200 Subject: [PATCH 08/62] test(assets): restore mocks after each resolveModelNodeFromAsset test Add afterEach(vi.restoreAllMocks) so the console.error spy doesn't persist across tests (CodeRabbit nit). --- src/platform/assets/utils/resolveModelNodeFromAsset.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts index 878a11a6cfb..d7752c73040 100644 --- a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts +++ b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { AssetItem } from '@/platform/assets/schemas/assetSchema' import { resolveModelNodeFromAsset } from '@/platform/assets/utils/resolveModelNodeFromAsset' @@ -63,6 +63,10 @@ describe('resolveModelNodeFromAsset', () => { mockSupportsModelTypeTags.value = false }) + afterEach(() => { + vi.restoreAllMocks() + }) + describe('valid assets', () => { it('resolves the provider for the asset category and the filename', () => { mockProvider(createMockNodeProvider()) From ccc6a1f2c6b0ca0f8228dc20e94c93efbe60cdf7 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Wed, 1 Jul 2026 14:07:37 +1200 Subject: [PATCH 09/62] fix(assets): prefer resolvable model_type over incidental slash tags An incidental hierarchical tag (e.g. a user-added foo/bar) could outrank an authoritative model_type:vae in node-category resolution, targeting the wrong provider or failing with NO_PROVIDER. Resolution now tries category candidates most-specific-first and takes the first that maps to a provider, so the deepest resolvable path still wins but unresolvable tags fall through. --- .../assets/utils/assetMetadataUtils.test.ts | 45 ++++++++++--------- .../assets/utils/assetMetadataUtils.ts | 39 +++++++++------- .../utils/resolveModelNodeFromAsset.test.ts | 33 +++++++++++++- .../assets/utils/resolveModelNodeFromAsset.ts | 30 +++++++++---- 4 files changed, 99 insertions(+), 48 deletions(-) diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index 83227aa574b..8c3438d7019 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -13,7 +13,7 @@ import { getAssetFilename, getAssetMetadataDimensions, getAssetModelType, - getAssetNodeCategory, + getAssetNodeCategoryCandidates, getAssetSourceUrl, getAssetStoredFilename, getAssetTriggerPhrases, @@ -620,56 +620,59 @@ describe('getAssetCategories', () => { }) }) -describe('getAssetNodeCategory', () => { +describe('getAssetNodeCategoryCandidates', () => { const asset = (tags: string[]): AssetItem => ({ id: 'a', name: 'model.safetensors', tags }) - it('prefers the most specific (deepest) tag over a flat model_type value', () => { + it('orders the most specific (deepest) tag ahead of a flat model_type value', () => { expect( - getAssetNodeCategory( + getAssetNodeCategoryCandidates( asset(['models', 'model_type:LLM', 'LLM/Qwen-VL/Qwen3-0.6B']), true ) - ).toBe('LLM/Qwen-VL/Qwen3-0.6B') + ).toEqual(['LLM/Qwen-VL/Qwen3-0.6B', 'LLM']) }) it('strips the model_type: prefix when it is the only candidate', () => { expect( - getAssetNodeCategory(asset(['models', 'model_type:vae']), true) - ).toBe('vae') + getAssetNodeCategoryCandidates(asset(['models', 'model_type:vae']), true) + ).toEqual(['vae']) }) - it('lets a model_type value win ties against a bare tag', () => { + it('keeps a model_type value ahead of an equally-deep bare tag', () => { expect( - getAssetNodeCategory( + getAssetNodeCategoryCandidates( asset(['models', 'model_type:checkpoints', 'sdxl']), true ) - ).toBe('checkpoints') + ).toEqual(['checkpoints', 'sdxl']) }) it('keeps a hierarchical tag intact', () => { expect( - getAssetNodeCategory(asset(['models', 'chatterbox/chatterbox_vc']), true) - ).toBe('chatterbox/chatterbox_vc') + getAssetNodeCategoryCandidates( + asset(['models', 'chatterbox/chatterbox_vc']), + true + ) + ).toEqual(['chatterbox/chatterbox_vc']) }) - it('returns undefined when only reserved tags are present', () => { + it('returns no candidates when only reserved tags are present', () => { expect( - getAssetNodeCategory(asset(['models', 'missing']), true) - ).toBeUndefined() + getAssetNodeCategoryCandidates(asset(['models', 'missing']), true) + ).toEqual([]) }) it('uses the first non-reserved tag verbatim when mode is off (default)', () => { - expect(getAssetNodeCategory(asset(['models', 'model_type:vae']))).toBe( - 'model_type:vae' - ) - expect(getAssetNodeCategory(asset(['models', 'checkpoints']))).toBe( - 'checkpoints' - ) + expect( + getAssetNodeCategoryCandidates(asset(['models', 'model_type:vae'])) + ).toEqual(['model_type:vae']) + expect( + getAssetNodeCategoryCandidates(asset(['models', 'checkpoints'])) + ).toEqual(['checkpoints']) }) }) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 975c826c076..6965b678407 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -228,37 +228,42 @@ export function getAssetTypeBadge( } /** - * Resolves the model-type key used to look up a node provider for an asset. + * Ordered node-category candidates for an asset, most specific first. * - * Unlike {@link getAssetCategories}, this keeps the full (possibly - * hierarchical) value so `modelToNodeStore`'s `parent/child` fallback still - * works. + * Callers resolve a node provider by trying each candidate in order and taking + * the first that maps to a provider. The full (possibly hierarchical) value is + * kept so `modelToNodeStore`'s `parent/child` fallback still works. * - * In `modelTypeMode` (backend declares `supports_model_type_tags`), candidates - * are the stripped `model_type:*` values plus any other non-reserved tags, and - * the most specific (deepest `parent/child`) candidate wins so a flat - * `model_type:LLM` never shadows a resolvable `LLM/Qwen-VL/...` tag. Otherwise - * we use the legacy first-non-reserved tag verbatim. + * In `modelTypeMode` (backend declares `supports_model_type_tags`) candidates + * are the stripped `model_type:*` values plus any other non-reserved tags, + * ordered by descending `parent/child` depth. Trying deepest-first lets a + * resolvable `LLM/Qwen-VL/...` path win over a flat `model_type:LLM`, while an + * unresolvable incidental tag (e.g. a user-added `foo/bar`) is skipped in + * favour of an authoritative `model_type:vae` that actually resolves. Ties keep + * `model_type:*` values ahead of bare tags so they are never shadowed by an + * equally-deep tag. Outside `modelTypeMode` the legacy first-non-reserved tag + * is used verbatim. */ -export function getAssetNodeCategory( +export function getAssetNodeCategoryCandidates( asset: AssetItem, modelTypeMode = false -): string | undefined { +): string[] { if (!modelTypeMode) { - return asset.tags.find((tag) => tag !== MODELS_TAG && tag !== MISSING_TAG) + const legacy = asset.tags.find( + (tag) => tag !== MODELS_TAG && tag !== MISSING_TAG + ) + return legacy ? [legacy] : [] } - const otherTags = asset.tags.filter( + const bareTags = asset.tags.filter( (tag) => tag !== MODELS_TAG && tag !== MISSING_TAG && !tag.startsWith(MODEL_TYPE_TAG_PREFIX) ) - const candidates = [...getModelTypeTagValues(asset), ...otherTags] - if (candidates.length === 0) return undefined - return candidates.reduce((best, candidate) => - pathDepth(candidate) > pathDepth(best) ? candidate : best + return [...getModelTypeTagValues(asset), ...bareTags].sort( + (a, b) => pathDepth(b) - pathDepth(a) ) } diff --git a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts index d7752c73040..42d61936986 100644 --- a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts +++ b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts @@ -95,6 +95,37 @@ describe('resolveModelNodeFromAsset', () => { expect(mockGetNodeProvider).toHaveBeenCalledWith('vae') }) + it('skips an unresolvable incidental tag and resolves via the model_type value', () => { + mockSupportsModelTypeTags.value = true + mockGetNodeProvider.mockImplementation((category: string) => + category === 'vae' ? createMockNodeProvider() : undefined + ) + const result = resolveModelNodeFromAsset( + createMockAsset({ tags: ['models', 'model_type:vae', 'foo/bar'] }) + ) + + expect(result.success).toBe(true) + expect(mockGetNodeProvider).toHaveBeenCalledWith('foo/bar') + expect(mockGetNodeProvider).toHaveBeenCalledWith('vae') + }) + + it('prefers the deepest resolvable path over a flat model_type value', () => { + mockSupportsModelTypeTags.value = true + mockGetNodeProvider.mockImplementation((category: string) => + category === 'LLM/Qwen-VL/Qwen3-0.6B' + ? createMockNodeProvider() + : undefined + ) + const result = resolveModelNodeFromAsset( + createMockAsset({ + tags: ['models', 'model_type:LLM', 'LLM/Qwen-VL/Qwen3-0.6B'] + }) + ) + + expect(result.success).toBe(true) + expect(mockGetNodeProvider).toHaveBeenCalledWith('LLM/Qwen-VL/Qwen3-0.6B') + }) + it('falls back to metadata.filename when user_metadata.filename missing', () => { mockProvider(createMockNodeProvider()) const result = resolveModelNodeFromAsset( @@ -228,7 +259,7 @@ describe('resolveModelNodeFromAsset', () => { if (!result.success) { expect(result.error.code).toBe('NO_PROVIDER') expect(result.error.message).toContain('checkpoints') - expect(result.error.details?.category).toBe('checkpoints') + expect(result.error.details?.candidates).toEqual(['checkpoints']) } }) }) diff --git a/src/platform/assets/utils/resolveModelNodeFromAsset.ts b/src/platform/assets/utils/resolveModelNodeFromAsset.ts index 3d74da5d895..c1cfcf9010a 100644 --- a/src/platform/assets/utils/resolveModelNodeFromAsset.ts +++ b/src/platform/assets/utils/resolveModelNodeFromAsset.ts @@ -6,7 +6,7 @@ import { } from '@/platform/assets/services/assetService' import { getAssetFilename, - getAssetNodeCategory + getAssetNodeCategoryCandidates } from '@/platform/assets/utils/assetMetadataUtils' import { useFeatureFlags } from '@/composables/useFeatureFlags' import { useModelToNodeStore } from '@/stores/modelToNodeStore' @@ -86,8 +86,11 @@ export function resolveModelNodeFromAsset( } const { flags } = useFeatureFlags() - const category = getAssetNodeCategory(validAsset, flags.supportsModelTypeTags) - if (!category) { + const candidates = getAssetNodeCategoryCandidates( + validAsset, + flags.supportsModelTypeTags + ) + if (candidates.length === 0) { console.error( `Asset ${validAsset.id} has no valid category tag. Available tags: ${validAsset.tags.join(', ')} (expected tag other than '${MODELS_TAG}' or '${MISSING_TAG}')` ) @@ -102,19 +105,28 @@ export function resolveModelNodeFromAsset( } } - const provider = useModelToNodeStore().getNodeProvider(category) - if (!provider) { - console.error(`No node provider registered for category: ${category}`) + const modelToNodeStore = useModelToNodeStore() + const resolved = candidates + .map((category) => ({ + category, + provider: modelToNodeStore.getNodeProvider(category) + })) + .find((candidate) => candidate.provider !== undefined) + + if (!resolved?.provider) { + console.error( + `No node provider registered for category: ${candidates.join(', ')}` + ) return { success: false, error: { code: 'NO_PROVIDER', - message: `No node provider registered for category: ${category}`, + message: `No node provider registered for category: ${candidates.join(', ')}`, assetId: validAsset.id, - details: { category } + details: { candidates } } } } - return { success: true, value: { provider, filename } } + return { success: true, value: { provider: resolved.provider, filename } } } From c34bef82b97d7cdc53738147df800800f4ef5557 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Sun, 12 Jul 2026 20:57:13 +1200 Subject: [PATCH 10/62] fix(assets): align badge and loader resolution with model_type tags In modelTypeMode the card badge and modal title now key off the same model_type: value the asset groups under, so they can no longer diverge from the grouping (review consensus on FE-1076). Loader-node resolution demotes bare tags unrelated to any model_type: value to trailing fallbacks: they can no longer pre-empt an authoritative resolvable model_type: however deep they are, while related hierarchical twins (LLM/Qwen-VL over flat model_type:LLM) keep winning. Also: getAssetModelType can no longer return a raw model_type: literal (kills the prefixed round-trip through edit widgets), modelTypeMode is now a required parameter so new call sites must choose explicitly, malformed empty model_type: tags produce no badge, per-asset flag reads hoisted, and a test pins the local reserved-tag mirrors to assetService's canonical values. --- .../assets/components/AssetBrowserModal.vue | 17 ++-- .../assets/composables/useAssetBrowser.ts | 8 +- .../utils/assetFilterUtils.property.test.ts | 4 +- src/platform/assets/utils/assetFilterUtils.ts | 2 +- .../assets/utils/assetMetadataUtils.test.ts | 93 ++++++++++++++++--- .../assets/utils/assetMetadataUtils.ts | 74 +++++++++------ .../assets/utils/resolveModelNodeFromAsset.ts | 3 + 7 files changed, 148 insertions(+), 53 deletions(-) diff --git a/src/platform/assets/components/AssetBrowserModal.vue b/src/platform/assets/components/AssetBrowserModal.vue index 20e99da3b11..1e24608750f 100644 --- a/src/platform/assets/components/AssetBrowserModal.vue +++ b/src/platform/assets/components/AssetBrowserModal.vue @@ -110,7 +110,7 @@ import { useAssetBrowser } from '@/platform/assets/composables/useAssetBrowser' import { useModelTypes } from '@/platform/assets/composables/useModelTypes' import { useModelUpload } from '@/platform/assets/composables/useModelUpload' import type { AssetItem } from '@/platform/assets/schemas/assetSchema' -import { stripModelTypePrefix } from '@/platform/assets/utils/assetMetadataUtils' +import { getAssetCategories } from '@/platform/assets/utils/assetMetadataUtils' import { formatCategoryLabel } from '@/platform/assets/utils/categoryLabel' import { useAssetsStore } from '@/stores/assetsStore' import { useModelToNodeStore } from '@/stores/modelToNodeStore' @@ -194,16 +194,19 @@ const focusedAsset = ref(null) const isRightPanelOpen = ref(false) const primaryCategoryTag = computed(() => { + const modelTypeMode = flags.supportsModelTypeTags const assets = fetchedAssets.value ?? [] + // In modelTypeMode the title keys off the same category the asset groups + // under (getAssetCategories), so title and grouping cannot diverge. const tagFromAssets = assets - .map((asset) => asset.tags?.find((tag) => tag !== 'models')) + .map((asset) => + modelTypeMode + ? getAssetCategories(asset, true)[0] + : asset.tags?.find((tag) => tag !== 'models') + ) .find((tag): tag is string => typeof tag === 'string' && tag.length > 0) - if (tagFromAssets) { - return flags.supportsModelTypeTags - ? stripModelTypePrefix(tagFromAssets) - : tagFromAssets - } + if (tagFromAssets) return tagFromAssets if (props.nodeType) { const mapped = modelToNodeStore.getCategoryForNodeType(props.nodeType) diff --git a/src/platform/assets/composables/useAssetBrowser.ts b/src/platform/assets/composables/useAssetBrowser.ts index 3eb331a4b13..eac2c13a517 100644 --- a/src/platform/assets/composables/useAssetBrowser.ts +++ b/src/platform/assets/composables/useAssetBrowser.ts @@ -131,11 +131,10 @@ export function useAssetBrowser( }) const typeCategories = computed(() => { + const modelTypeMode = flags.supportsModelTypeTags const categories = assets.value .filter((asset) => asset.tags.includes(MODELS_TAG)) - .flatMap((asset) => - getAssetCategories(asset, flags.supportsModelTypeTags) - ) + .flatMap((asset) => getAssetCategories(asset, modelTypeMode)) return Array.from(new Set(categories)) .sort() @@ -258,8 +257,9 @@ export function useAssetBrowser( const sortedAssets = sortAssets(filtered, filters.value.sortBy) // Transform to display format + const modelTypeMode = flags.supportsModelTypeTags return sortedAssets.map((asset) => - transformAssetForDisplay(asset, flags.supportsModelTypeTags) + transformAssetForDisplay(asset, modelTypeMode) ) }) diff --git a/src/platform/assets/utils/assetFilterUtils.property.test.ts b/src/platform/assets/utils/assetFilterUtils.property.test.ts index 176346035f9..379f2859655 100644 --- a/src/platform/assets/utils/assetFilterUtils.property.test.ts +++ b/src/platform/assets/utils/assetFilterUtils.property.test.ts @@ -45,7 +45,7 @@ describe('assetFilterUtils properties', () => { it('filterByCategory("all") accepts every asset', () => { fc.assert( fc.property(arbAssetItem, (asset) => { - expect(filterByCategory('all')(asset)).toBe(true) + expect(filterByCategory('all', false)(asset)).toBe(true) }) ) }) @@ -56,7 +56,7 @@ describe('assetFilterUtils properties', () => { fc.array(arbAssetItem, { maxLength: 30 }), fc.stringMatching(/^[a-z]{1,8}$/), (assets, category) => { - const filter = filterByCategory(category) + const filter = filterByCategory(category, false) const result = assets.filter(filter) expect(result.length).toBeLessThanOrEqual(assets.length) for (const item of result) { diff --git a/src/platform/assets/utils/assetFilterUtils.ts b/src/platform/assets/utils/assetFilterUtils.ts index a6f567c97a8..597b8e8a4c1 100644 --- a/src/platform/assets/utils/assetFilterUtils.ts +++ b/src/platform/assets/utils/assetFilterUtils.ts @@ -5,7 +5,7 @@ import { getAssetCategories } from '@/platform/assets/utils/assetMetadataUtils' -export function filterByCategory(category: string, modelTypeMode = false) { +export function filterByCategory(category: string, modelTypeMode: boolean) { return (asset: AssetItem) => { if (category === 'all') return true return getAssetCategories(asset, modelTypeMode).includes(category) diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index 8c3438d7019..b76ab076273 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -1,6 +1,10 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import type { AssetItem } from '@/platform/assets/schemas/assetSchema' +import { + MISSING_TAG, + MODELS_TAG +} from '@/platform/assets/services/assetService' import { getAssetAdditionalTags, getAssetBaseModel, @@ -278,7 +282,17 @@ describe('assetMetadataUtils', () => { tags: ['models'], expected: null }, - { name: 'returns null when tags empty', tags: [], expected: null } + { name: 'returns null when tags empty', tags: [], expected: null }, + { + name: 'never returns a raw model_type: literal (no round-trip into edit widgets)', + tags: ['models', 'model_type:checkpoints'], + expected: null + }, + { + name: 'skips model_type: tags in favour of the bare twin', + tags: ['models', 'model_type:checkpoints', 'checkpoints'], + expected: 'checkpoints' + } ])('$name', ({ tags, expected }) => { const asset = { ...mockAsset, tags } expect(getAssetModelType(asset)).toBe(expected) @@ -613,9 +627,12 @@ describe('getAssetCategories', () => { ]) }) - it('ignores model_type: and uses bare-tag grouping when mode is off (default)', () => { + it('ignores model_type: and uses bare-tag grouping when mode is off', () => { expect( - getAssetCategories(asset(['models', 'model_type:checkpoints', 'sdxl'])) + getAssetCategories( + asset(['models', 'model_type:checkpoints', 'sdxl']), + false + ) ).toEqual(['model_type:checkpoints', 'sdxl']) }) }) @@ -651,6 +668,24 @@ describe('getAssetNodeCategoryCandidates', () => { ).toEqual(['checkpoints', 'sdxl']) }) + it('demotes an unrelated deeper bare tag below the model_type value', () => { + expect( + getAssetNodeCategoryCandidates( + asset(['models', 'model_type:vae', 'foo/bar']), + true + ) + ).toEqual(['vae', 'foo/bar']) + }) + + it('keeps unrelated bare tags as trailing fallbacks rather than dropping them', () => { + expect( + getAssetNodeCategoryCandidates( + asset(['models', 'model_type:LLM', 'LLM/Qwen-VL', 'foo/bar/baz']), + true + ) + ).toEqual(['LLM/Qwen-VL', 'LLM', 'foo/bar/baz']) + }) + it('keeps a hierarchical tag intact', () => { expect( getAssetNodeCategoryCandidates( @@ -666,12 +701,12 @@ describe('getAssetNodeCategoryCandidates', () => { ).toEqual([]) }) - it('uses the first non-reserved tag verbatim when mode is off (default)', () => { + it('uses the first non-reserved tag verbatim when mode is off', () => { expect( - getAssetNodeCategoryCandidates(asset(['models', 'model_type:vae'])) + getAssetNodeCategoryCandidates(asset(['models', 'model_type:vae']), false) ).toEqual(['model_type:vae']) expect( - getAssetNodeCategoryCandidates(asset(['models', 'checkpoints'])) + getAssetNodeCategoryCandidates(asset(['models', 'checkpoints']), false) ).toEqual(['checkpoints']) }) }) @@ -692,20 +727,32 @@ describe('getAssetTypeBadge', () => { ).toBe('checkpoints') }) - it('keeps the first non-models tag rather than reordering to the model_type value', () => { + it('badges the model_type value even when a bare tag comes first, matching the grouping', () => { expect( getAssetTypeBadge(asset(['models', 'foo', 'model_type:bar']), true) - ).toBe('foo') + ).toBe('bar') }) - it('leaks the literal model_type: tag when mode is off (default)', () => { - expect(getAssetTypeBadge(asset(['models', 'model_type:checkpoints']))).toBe( - 'model_type:checkpoints' - ) + it('falls back to the bare tag for an uncovered asset in model_type mode', () => { + expect(getAssetTypeBadge(asset(['models', 'sdxl']), true)).toBe('sdxl') + }) + + it('returns undefined rather than a blank badge for a malformed empty model_type: tag', () => { + expect( + getAssetTypeBadge(asset(['models', 'model_type:']), true) + ).toBeUndefined() + }) + + it('leaks the literal model_type: tag when mode is off', () => { + expect( + getAssetTypeBadge(asset(['models', 'model_type:checkpoints']), false) + ).toBe('model_type:checkpoints') }) it('shows the segment after the first slash for a bare hierarchical tag', () => { - expect(getAssetTypeBadge(asset(['models', 'checkpoint/xl']))).toBe('xl') + expect(getAssetTypeBadge(asset(['models', 'checkpoint/xl']), false)).toBe( + 'xl' + ) }) it('returns undefined when only the models tag is present', () => { @@ -723,3 +770,23 @@ describe('stripModelTypePrefix', () => { expect(stripModelTypePrefix('checkpoint/xl')).toBe('checkpoint/xl') }) }) + +describe('reserved tag mirrors', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it("treats assetService's canonical reserved tags as reserved (locals must not drift)", () => { + expect(getAssetCategories(asset([MODELS_TAG, 'x']), false)).toEqual(['x']) + expect( + getAssetNodeCategoryCandidates( + asset([MODELS_TAG, MISSING_TAG, 'x']), + true + ) + ).toEqual(['x']) + expect(getAssetTypeBadge(asset([MODELS_TAG, 'x']), false)).toBe('x') + expect(getAssetModelType(asset([MODELS_TAG]))).toBeNull() + }) +}) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 6965b678407..6de03d0a7c0 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -145,18 +145,23 @@ export function getSourceName(url: string): string { return 'Source' } +const MODEL_TYPE_TAG_PREFIX = 'model_type:' + /** - * Extracts the model type from asset tags + * Extracts the model type from asset tags as a bare (non-namespaced) value. + * Never returns a raw `model_type:*` literal: this value feeds edit widgets + * whose save path writes tags back verbatim, so a namespaced tag leaking + * through here would round-trip the prefixed literal into the tag set. * @param asset - The asset to extract model type from * @returns The model type string or null if not present */ export function getAssetModelType(asset: AssetItem): string | null { - const typeTag = asset.tags?.find((tag) => tag && tag !== 'models') + const typeTag = asset.tags?.find( + (tag) => tag && tag !== MODELS_TAG && !tag.startsWith(MODEL_TYPE_TAG_PREFIX) + ) return typeTag ?? null } -const MODEL_TYPE_TAG_PREFIX = 'model_type:' - /** Strips the `model_type:` prefix off each namespaced tag, dropping non-`model_type:` tags. */ function getModelTypeTagValues(asset: AssetItem): string[] { return asset.tags @@ -183,7 +188,7 @@ function getBareTagCategories(asset: AssetItem): string[] { */ export function getAssetCategories( asset: AssetItem, - modelTypeMode = false + modelTypeMode: boolean ): string[] { if (modelTypeMode) { const modelTypes = getModelTypeTagValues(asset) @@ -208,20 +213,26 @@ export function stripModelTypePrefix(tag: string): string { /** * Resolves the short label shown on an asset card's type badge. * - * Uses the first non-`models` tag (unchanged selection); in `modelTypeMode` a - * `model_type:` namespace prefix on that tag is stripped so the badge doesn't - * leak the raw namespace. Bare hierarchical tags still show the segment after - * the first `/`. + * In `modelTypeMode` the badge is the asset's first `model_type:*` value — + * the same key the asset groups under (`getAssetCategories`), so badge and + * grouping cannot diverge for covered assets. Uncovered assets (and legacy + * mode) keep the original selection: first non-`models` tag, with bare + * hierarchical tags showing the segment after the first `/`. */ export function getAssetTypeBadge( asset: AssetItem, - modelTypeMode = false + modelTypeMode: boolean ): string | undefined { - const typeTag = asset.tags.find((tag) => tag !== MODELS_TAG) - if (!typeTag) return undefined - if (modelTypeMode && typeTag.startsWith(MODEL_TYPE_TAG_PREFIX)) { - return stripModelTypePrefix(typeTag) + if (modelTypeMode) { + const [modelType] = getModelTypeTagValues(asset) + if (modelType) return modelType } + const typeTag = asset.tags.find( + (tag) => + tag !== MODELS_TAG && + !(modelTypeMode && tag.startsWith(MODEL_TYPE_TAG_PREFIX)) + ) + if (!typeTag) return undefined return typeTag.includes('/') ? typeTag.slice(typeTag.indexOf('/') + 1) : typeTag @@ -235,18 +246,20 @@ export function getAssetTypeBadge( * kept so `modelToNodeStore`'s `parent/child` fallback still works. * * In `modelTypeMode` (backend declares `supports_model_type_tags`) candidates - * are the stripped `model_type:*` values plus any other non-reserved tags, - * ordered by descending `parent/child` depth. Trying deepest-first lets a - * resolvable `LLM/Qwen-VL/...` path win over a flat `model_type:LLM`, while an - * unresolvable incidental tag (e.g. a user-added `foo/bar`) is skipped in - * favour of an authoritative `model_type:vae` that actually resolves. Ties keep - * `model_type:*` values ahead of bare tags so they are never shadowed by an - * equally-deep tag. Outside `modelTypeMode` the legacy first-non-reserved tag - * is used verbatim. + * come in two tiers. Tier 1: the stripped `model_type:*` values plus bare tags + * *related* to one of them (equal to it, or extending it as a `parent/child` + * path), ordered by descending depth — so a resolvable `LLM/Qwen-VL/...` twin + * wins over a flat `model_type:LLM`, while ties keep `model_type:*` values + * ahead of bare tags. Tier 2: unrelated bare tags (e.g. a user-added + * `foo/bar`), tried only when nothing authoritative resolves — they can no + * longer pre-empt a resolvable `model_type:*` value however deep they are. + * An uncovered asset (no `model_type:` tag) routes by all its bare tags, + * deepest first. Outside `modelTypeMode` the legacy first-non-reserved tag is + * used verbatim. */ export function getAssetNodeCategoryCandidates( asset: AssetItem, - modelTypeMode = false + modelTypeMode: boolean ): string[] { if (!modelTypeMode) { const legacy = asset.tags.find( @@ -262,9 +275,18 @@ export function getAssetNodeCategoryCandidates( !tag.startsWith(MODEL_TYPE_TAG_PREFIX) ) - return [...getModelTypeTagValues(asset), ...bareTags].sort( - (a, b) => pathDepth(b) - pathDepth(a) - ) + const byDepthDesc = (a: string, b: string) => pathDepth(b) - pathDepth(a) + + const modelTypes = getModelTypeTagValues(asset) + if (modelTypes.length === 0) return bareTags.toSorted(byDepthDesc) + + const isRelated = (tag: string) => + modelTypes.some((type) => tag === type || tag.startsWith(`${type}/`)) + + return [ + ...[...modelTypes, ...bareTags.filter(isRelated)].sort(byDepthDesc), + ...bareTags.filter((tag) => !isRelated(tag)).sort(byDepthDesc) + ] } /** diff --git a/src/platform/assets/utils/resolveModelNodeFromAsset.ts b/src/platform/assets/utils/resolveModelNodeFromAsset.ts index c1cfcf9010a..d6eeabb94fc 100644 --- a/src/platform/assets/utils/resolveModelNodeFromAsset.ts +++ b/src/platform/assets/utils/resolveModelNodeFromAsset.ts @@ -114,6 +114,9 @@ export function resolveModelNodeFromAsset( .find((candidate) => candidate.provider !== undefined) if (!resolved?.provider) { + // Known gap (out of scope for FE-1076): flat `model_type:LLM`-style tags + // whose loaders are only registered hierarchically land here until the + // backend emits a subtype-carrying tag. console.error( `No node provider registered for category: ${candidates.join(', ')}` ) From 09877d769b7cf58585f663337a2db48d53961ec4 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Sun, 12 Jul 2026 20:57:50 +1200 Subject: [PATCH 11/62] fix(assets): surface refresh errors and harden pagination races A refresh that fails before its first batch commits now mirrors the error onto the committed state consumers actually read, instead of dying silently on the discarded pending state. Pagination stops only when a full page repeats the previous page verbatim (ignored-offset signature); an all-duplicate page whose content merely shifted keeps walking so later pages still load. Teardown of a stale request can no longer evict a newer request's single-flight entries after an interleaved invalidation. --- src/stores/assetsStore.test.ts | 50 ++++++++++++++++++++++++++++++++++ src/stores/assetsStore.ts | 19 +++++++++++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/stores/assetsStore.test.ts b/src/stores/assetsStore.test.ts index f4ed1cd86f2..ba443415e17 100644 --- a/src/stores/assetsStore.test.ts +++ b/src/stores/assetsStore.test.ts @@ -987,6 +987,28 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => { 'p3' ]) }) + + }) + + describe('refresh error surfacing', () => { + it('surfaces a failed refresh on the committed state consumers read', async () => { + const store = useAssetsStore() + const nodeType = 'CheckpointLoaderSimple' + + vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([ + createMockAsset('existing') + ]) + await store.updateModelsForNodeType(nodeType) + expect(store.getError(nodeType)).toBeUndefined() + + vi.mocked(assetService.getAssetsForNodeType).mockRejectedValueOnce( + new Error('backend down') + ) + await store.updateModelsForNodeType(nodeType) + + expect(store.getAssets(nodeType).map((a) => a.id)).toEqual(['existing']) + expect(store.getError(nodeType)?.message).toBe('backend down') + }) }) describe('concurrent request handling', () => { @@ -1040,6 +1062,34 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => { vi.mocked(assetService.getAssetsPageForNodeType) ).toHaveBeenCalledTimes(2) }) + + it('keeps a newer request single-flighted when a stale request finishes after invalidation', async () => { + const store = useAssetsStore() + const nodeType = 'CheckpointLoaderSimple' + + let resolveFirst!: (assets: AssetItem[]) => void + const firstFetch = new Promise((resolve) => { + resolveFirst = resolve + }) + vi.mocked(assetService.getAssetsForNodeType) + .mockReturnValueOnce(firstFetch) + .mockReturnValue(new Promise(() => {})) + + const staleRequest = store.updateModelsForNodeType(nodeType) + store.invalidateCategory('checkpoints') + void store.updateModelsForNodeType(nodeType) + + resolveFirst([createMockAsset('stale')]) + await staleRequest + + // The stale request's teardown must not evict the newer request's + // single-flight entry: a third call short-circuits instead of starting + // a duplicate walk. + void store.updateModelsForNodeType(nodeType) + expect( + vi.mocked(assetService.getAssetsForNodeType) + ).toHaveBeenCalledTimes(2) + }) }) describe('shallowReactive state reactivity', () => { diff --git a/src/stores/assetsStore.ts b/src/stores/assetsStore.ts index 6587a374cea..b9390bb61a9 100644 --- a/src/stores/assetsStore.ts +++ b/src/stores/assetsStore.ts @@ -410,6 +410,10 @@ export const useAssetsStore = defineStore('assets', () => { * Multiple node types sharing the same category share the same cache entry. * Public API accepts nodeType for backwards compatibility but translates * to category internally using modelToNodeStore.getCategoryForNodeType(). + * + * Runs on every distribution; whether anything fetches through it is + * decided by consumers via `assetService.isAssetAPIEnabled()`, which stays + * the authoritative off-cloud gate. */ const getModelState = () => { const modelStateByCategory = ref(new Map()) @@ -653,7 +657,16 @@ export const useAssetsStore = defineStore('assets', () => { state.error = err instanceof Error ? err : new Error(String(err)) state.hasMore = false state.isLoading = false - pendingRequestByCategory.delete(category) + // A refresh that fails before its first batch never replaces the + // committed state, so mirror the error onto the state consumers + // actually read (getError) instead of only the discarded one. + const committed = modelStateByCategory.value.get(category) + if (committed && committed !== state) { + committed.error = state.error + } + if (pendingRequestByCategory.get(category) === state) { + pendingRequestByCategory.delete(category) + } return } @@ -671,7 +684,9 @@ export const useAssetsStore = defineStore('assets', () => { } } assetsArrayCache.delete(category) - pendingRequestByCategory.delete(category) + if (pendingRequestByCategory.get(category) === state) { + pendingRequestByCategory.delete(category) + } } const promise = loadBatches() From ece07cc7025618b5275392bc3b5f122cf9dcef4b Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Sun, 12 Jul 2026 21:34:25 +1200 Subject: [PATCH 12/62] fix(assets): close pagination-termination and title-preservation gaps Adversarial review follow-ups: an offset-ignoring backend that reorders its responses could alternate two page orderings and defeat the repeated-page check forever, so the walk now also remembers every served page signature and treats a run of no-new-ID full pages as exhausted. Modal titles for uncovered assets keep their legacy verbatim tag (only covered assets retitle to their model_type value), restoring the pre-alignment behavior the batch accidentally changed, and namespace residue like a malformed empty model_type: can no longer leak into titles, nav items, or categories. --- .../assets/components/AssetBrowserModal.vue | 13 +++---- .../assets/utils/assetMetadataUtils.test.ts | 39 +++++++++++++++++++ .../assets/utils/assetMetadataUtils.ts | 26 +++++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/platform/assets/components/AssetBrowserModal.vue b/src/platform/assets/components/AssetBrowserModal.vue index 1e24608750f..9bc9c615b94 100644 --- a/src/platform/assets/components/AssetBrowserModal.vue +++ b/src/platform/assets/components/AssetBrowserModal.vue @@ -110,7 +110,7 @@ import { useAssetBrowser } from '@/platform/assets/composables/useAssetBrowser' import { useModelTypes } from '@/platform/assets/composables/useModelTypes' import { useModelUpload } from '@/platform/assets/composables/useModelUpload' import type { AssetItem } from '@/platform/assets/schemas/assetSchema' -import { getAssetCategories } from '@/platform/assets/utils/assetMetadataUtils' +import { getPrimaryCategoryTag } from '@/platform/assets/utils/assetMetadataUtils' import { formatCategoryLabel } from '@/platform/assets/utils/categoryLabel' import { useAssetsStore } from '@/stores/assetsStore' import { useModelToNodeStore } from '@/stores/modelToNodeStore' @@ -196,14 +196,11 @@ const isRightPanelOpen = ref(false) const primaryCategoryTag = computed(() => { const modelTypeMode = flags.supportsModelTypeTags const assets = fetchedAssets.value ?? [] - // In modelTypeMode the title keys off the same category the asset groups - // under (getAssetCategories), so title and grouping cannot diverge. + // Covered assets title off the model_type value they group under (so title + // and grouping cannot diverge); uncovered assets keep the legacy verbatim + // first tag. const tagFromAssets = assets - .map((asset) => - modelTypeMode - ? getAssetCategories(asset, true)[0] - : asset.tags?.find((tag) => tag !== 'models') - ) + .map((asset) => getPrimaryCategoryTag(asset, modelTypeMode)) .find((tag): tag is string => typeof tag === 'string' && tag.length > 0) if (tagFromAssets) return tagFromAssets diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index b76ab076273..e9b6b486305 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -19,6 +19,7 @@ import { getAssetModelType, getAssetNodeCategoryCandidates, getAssetSourceUrl, + getPrimaryCategoryTag, getAssetStoredFilename, getAssetTriggerPhrases, getAssetTypeBadge, @@ -635,6 +636,44 @@ describe('getAssetCategories', () => { ) ).toEqual(['model_type:checkpoints', 'sdxl']) }) + + it('never surfaces namespace residue as a category for uncovered assets in mode', () => { + expect( + getAssetCategories(asset(['models', 'model_type:', 'sdxl']), true) + ).toEqual(['sdxl']) + }) +}) + +describe('getPrimaryCategoryTag', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('uses the model_type value a covered asset groups under', () => { + expect( + getPrimaryCategoryTag(asset(['models', 'sdxl', 'model_type:vae']), true) + ).toBe('vae') + }) + + it('keeps the legacy verbatim tag for an uncovered hierarchical asset', () => { + expect( + getPrimaryCategoryTag(asset(['models', 'Chatterbox/sub/model']), true) + ).toBe('Chatterbox/sub/model') + }) + + it('skips namespace residue instead of titling off a raw model_type: tag', () => { + expect( + getPrimaryCategoryTag(asset(['models', 'model_type:']), true) + ).toBeUndefined() + }) + + it('returns the legacy first non-models tag when mode is off', () => { + expect( + getPrimaryCategoryTag(asset(['models', 'model_type:vae']), false) + ).toBe('model_type:vae') + }) }) describe('getAssetNodeCategoryCandidates', () => { diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 6de03d0a7c0..43da82294bc 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -193,11 +193,37 @@ export function getAssetCategories( if (modelTypeMode) { const modelTypes = getModelTypeTagValues(asset) if (modelTypes.length > 0) return modelTypes + // Uncovered assets route by bare tags, but namespace residue (e.g. a + // malformed empty `model_type:`) must not surface as a raw category. + return getBareTagCategories(asset).filter( + (category) => !category.startsWith(MODEL_TYPE_TAG_PREFIX) + ) } return getBareTagCategories(asset) } +/** + * Resolves the primary tag a browser surface titles itself after. In + * `modelTypeMode` a covered asset uses its first `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. + */ +export function getPrimaryCategoryTag( + asset: AssetItem, + modelTypeMode: boolean +): string | undefined { + if (modelTypeMode) { + const [modelType] = getModelTypeTagValues(asset) + if (modelType) return modelType + return asset.tags.find( + (tag) => tag !== MODELS_TAG && !tag.startsWith(MODEL_TYPE_TAG_PREFIX) + ) + } + return asset.tags.find((tag) => tag !== MODELS_TAG) +} + /** Number of `parent/child` segments in a tag, used to pick the most specific. */ function pathDepth(tag: string): number { return tag.split('/').length From d43ecf8102e60f80ae721e3581093443b7d5b790 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Sun, 12 Jul 2026 23:01:25 +1200 Subject: [PATCH 13/62] fix(assets): represent every category of a shared multi-type asset A shared-root asset tagged with several model_type values groups under each of them, but its badge and a node-typed picker's title keyed off only the first value, so viewing it under its second category showed the first category's label. Cards now badge every authoritative model_type value, and a node-typed picker titles off the requested node's category instead of guessing from the first asset. --- .../assets/components/AssetBrowserModal.vue | 8 +++ .../assets/composables/useAssetBrowser.ts | 5 +- .../assets/utils/assetMetadataUtils.test.ts | 53 ++++++++++++------- .../assets/utils/assetMetadataUtils.ts | 29 +++++----- 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/src/platform/assets/components/AssetBrowserModal.vue b/src/platform/assets/components/AssetBrowserModal.vue index 9bc9c615b94..0e841a03559 100644 --- a/src/platform/assets/components/AssetBrowserModal.vue +++ b/src/platform/assets/components/AssetBrowserModal.vue @@ -195,6 +195,14 @@ const isRightPanelOpen = ref(false) const primaryCategoryTag = computed(() => { const modelTypeMode = flags.supportsModelTypeTags + // A node-typed picker is FOR a category; title off that category rather + // than guessing from the first asset, whose first model_type value may be + // a different category it shares a root with. + if (modelTypeMode && props.nodeType) { + const mapped = modelToNodeStore.getCategoryForNodeType(props.nodeType) + if (mapped) return mapped + } + const assets = fetchedAssets.value ?? [] // Covered assets title off the model_type value they group under (so title // and grouping cannot diverge); uncovered assets keep the legacy verbatim diff --git a/src/platform/assets/composables/useAssetBrowser.ts b/src/platform/assets/composables/useAssetBrowser.ts index eac2c13a517..f0e5918e7ed 100644 --- a/src/platform/assets/composables/useAssetBrowser.ts +++ b/src/platform/assets/composables/useAssetBrowser.ts @@ -21,7 +21,7 @@ import { getAssetBaseModels, getAssetCategories, getAssetFilename, - getAssetTypeBadge + getAssetTypeBadges } from '@/platform/assets/utils/assetMetadataUtils' import { MODELS_TAG } from '@/platform/assets/services/assetService' import { sortAssets } from '@/platform/assets/utils/assetSortUtils' @@ -57,8 +57,7 @@ function buildDisplayItem( ): AssetDisplayItem { const badges: AssetBadge[] = [] - const typeBadge = getAssetTypeBadge(asset, modelTypeMode) - if (typeBadge) { + for (const typeBadge of getAssetTypeBadges(asset, modelTypeMode)) { badges.push({ label: typeBadge, type: 'type' }) } diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index e9b6b486305..0d5bd647b4a 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -22,7 +22,7 @@ import { getPrimaryCategoryTag, getAssetStoredFilename, getAssetTriggerPhrases, - getAssetTypeBadge, + getAssetTypeBadges, getAssetUserDescription, getSourceName, resolveDisplayImageDimensions, @@ -750,7 +750,7 @@ describe('getAssetNodeCategoryCandidates', () => { }) }) -describe('getAssetTypeBadge', () => { +describe('getAssetTypeBadges', () => { const asset = (tags: string[]): AssetItem => ({ id: 'a', name: 'model.safetensors', @@ -759,43 +759,58 @@ describe('getAssetTypeBadge', () => { it('strips the model_type: prefix in model_type mode (no raw leak)', () => { expect( - getAssetTypeBadge( + getAssetTypeBadges( asset(['models', 'model_type:checkpoints', 'sdxl']), true ) - ).toBe('checkpoints') + ).toEqual(['checkpoints']) }) it('badges the model_type value even when a bare tag comes first, matching the grouping', () => { expect( - getAssetTypeBadge(asset(['models', 'foo', 'model_type:bar']), true) - ).toBe('bar') + getAssetTypeBadges(asset(['models', 'foo', 'model_type:bar']), true) + ).toEqual(['bar']) + }) + + it('badges every category a shared multi-type asset groups under', () => { + expect( + getAssetTypeBadges( + asset([ + 'models', + 'model_type:checkpoints', + 'model_type:diffusion_models' + ]), + true + ) + ).toEqual(['checkpoints', 'diffusion_models']) }) it('falls back to the bare tag for an uncovered asset in model_type mode', () => { - expect(getAssetTypeBadge(asset(['models', 'sdxl']), true)).toBe('sdxl') + expect(getAssetTypeBadges(asset(['models', 'sdxl']), true)).toEqual([ + 'sdxl' + ]) }) - it('returns undefined rather than a blank badge for a malformed empty model_type: tag', () => { - expect( - getAssetTypeBadge(asset(['models', 'model_type:']), true) - ).toBeUndefined() + it('returns no badge rather than a blank one for a malformed empty model_type: tag', () => { + expect(getAssetTypeBadges(asset(['models', 'model_type:']), true)).toEqual( + [] + ) }) it('leaks the literal model_type: tag when mode is off', () => { expect( - getAssetTypeBadge(asset(['models', 'model_type:checkpoints']), false) - ).toBe('model_type:checkpoints') + getAssetTypeBadges(asset(['models', 'model_type:checkpoints']), false) + ).toEqual(['model_type:checkpoints']) }) it('shows the segment after the first slash for a bare hierarchical tag', () => { - expect(getAssetTypeBadge(asset(['models', 'checkpoint/xl']), false)).toBe( - 'xl' - ) + expect( + getAssetTypeBadges(asset(['models', 'checkpoint/xl']), false) + ).toEqual(['xl']) }) - it('returns undefined when only the models tag is present', () => { - expect(getAssetTypeBadge(asset(['models']), true)).toBeUndefined() + it('returns no badges when only the models tag is present', () => { + expect(getAssetTypeBadges(asset(['models']), true)).toEqual([]) }) }) @@ -825,7 +840,7 @@ describe('reserved tag mirrors', () => { true ) ).toEqual(['x']) - expect(getAssetTypeBadge(asset([MODELS_TAG, 'x']), false)).toBe('x') + expect(getAssetTypeBadges(asset([MODELS_TAG, 'x']), false)).toEqual(['x']) expect(getAssetModelType(asset([MODELS_TAG]))).toBeNull() }) }) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 43da82294bc..611e40eb249 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -237,31 +237,32 @@ export function stripModelTypePrefix(tag: string): string { } /** - * Resolves the short label shown on an asset card's type badge. + * Resolves the labels shown as an asset card's type badges. * - * In `modelTypeMode` the badge is the asset's first `model_type:*` value — - * the same key the asset groups under (`getAssetCategories`), so badge and - * grouping cannot diverge for covered assets. Uncovered assets (and legacy - * mode) keep the original selection: first non-`models` tag, with bare - * hierarchical tags showing the segment after the first `/`. + * In `modelTypeMode` a covered asset badges every `model_type:*` value — the + * same keys it groups under (`getAssetCategories`) — so a shared-root asset + * tagged with several categories carries each of them; whichever category + * view the card appears in is represented on the card. Uncovered assets (and + * legacy mode) keep the original single selection: first non-`models` tag, + * with bare hierarchical tags showing the segment after the first `/`. */ -export function getAssetTypeBadge( +export function getAssetTypeBadges( asset: AssetItem, modelTypeMode: boolean -): string | undefined { +): string[] { if (modelTypeMode) { - const [modelType] = getModelTypeTagValues(asset) - if (modelType) return modelType + const modelTypes = getModelTypeTagValues(asset) + if (modelTypes.length > 0) return modelTypes } const typeTag = asset.tags.find( (tag) => tag !== MODELS_TAG && !(modelTypeMode && tag.startsWith(MODEL_TYPE_TAG_PREFIX)) ) - if (!typeTag) return undefined - return typeTag.includes('/') - ? typeTag.slice(typeTag.indexOf('/') + 1) - : typeTag + if (!typeTag) return [] + return [ + typeTag.includes('/') ? typeTag.slice(typeTag.indexOf('/') + 1) : typeTag + ] } /** From 74e4e3843e073b22f5949d1993d935cd345ad893 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 21 Jul 2026 09:54:53 +1200 Subject: [PATCH 14/62] fix(assets): dedupe category candidates and share pagination tolerance An asset tagged with both `model_type:LLM` and a bare `LLM` yielded the same category twice in the candidate list; deduplicate the tier-1 values so a provider is only ever tried once per distinct category. Flat output pagination stopped on the first all-duplicate page, so a pagination window shifted by concurrent writes could hide later unseen assets. Reuse the model-category walk's no-progress tolerance for both walks instead of duplicating the counter, letting a bounded run of all-duplicate pages be traversed while still guaranteeing termination. --- src/platform/assets/utils/assetMetadataUtils.test.ts | 9 +++++++++ src/platform/assets/utils/assetMetadataUtils.ts | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index 0d5bd647b4a..ec0e1998f1b 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -734,6 +734,15 @@ describe('getAssetNodeCategoryCandidates', () => { ).toEqual(['chatterbox/chatterbox_vc']) }) + it('does not repeat a bare tag that duplicates its model_type value', () => { + expect( + getAssetNodeCategoryCandidates( + asset(['models', 'model_type:LLM', 'LLM']), + true + ) + ).toEqual(['LLM']) + }) + it('returns no candidates when only reserved tags are present', () => { expect( getAssetNodeCategoryCandidates(asset(['models', 'missing']), true) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 611e40eb249..d5efca1a214 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -311,7 +311,9 @@ export function getAssetNodeCategoryCandidates( modelTypes.some((type) => tag === type || tag.startsWith(`${type}/`)) return [ - ...[...modelTypes, ...bareTags.filter(isRelated)].sort(byDepthDesc), + ...[...new Set([...modelTypes, ...bareTags.filter(isRelated)])].sort( + byDepthDesc + ), ...bareTags.filter((tag) => !isRelated(tag)).sort(byDepthDesc) ] } From 8c3b94672e53e1736d7d71efbeadff21b1927149 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Fri, 24 Jul 2026 09:18:25 +1200 Subject: [PATCH 15/62] test(assets): align cache tests with cursor pagination --- src/stores/assetsStore.test.ts | 43 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/stores/assetsStore.test.ts b/src/stores/assetsStore.test.ts index ba443415e17..200b18e9281 100644 --- a/src/stores/assetsStore.test.ts +++ b/src/stores/assetsStore.test.ts @@ -987,7 +987,6 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => { 'p3' ]) }) - }) describe('refresh error surfacing', () => { @@ -995,13 +994,13 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => { const store = useAssetsStore() const nodeType = 'CheckpointLoaderSimple' - vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([ - createMockAsset('existing') - ]) + vi.mocked(assetService.getAssetsPageForNodeType).mockResolvedValueOnce( + makePage([createMockAsset('existing')]) + ) await store.updateModelsForNodeType(nodeType) expect(store.getError(nodeType)).toBeUndefined() - vi.mocked(assetService.getAssetsForNodeType).mockRejectedValueOnce( + vi.mocked(assetService.getAssetsPageForNodeType).mockRejectedValueOnce( new Error('backend down') ) await store.updateModelsForNodeType(nodeType) @@ -1067,19 +1066,19 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => { const store = useAssetsStore() const nodeType = 'CheckpointLoaderSimple' - let resolveFirst!: (assets: AssetItem[]) => void - const firstFetch = new Promise((resolve) => { + let resolveFirst!: (response: AssetResponse) => void + const firstFetch = new Promise((resolve) => { resolveFirst = resolve }) - vi.mocked(assetService.getAssetsForNodeType) + vi.mocked(assetService.getAssetsPageForNodeType) .mockReturnValueOnce(firstFetch) - .mockReturnValue(new Promise(() => {})) + .mockReturnValue(new Promise(() => {})) const staleRequest = store.updateModelsForNodeType(nodeType) store.invalidateCategory('checkpoints') void store.updateModelsForNodeType(nodeType) - resolveFirst([createMockAsset('stale')]) + resolveFirst(makePage([createMockAsset('stale')])) await staleRequest // The stale request's teardown must not evict the newer request's @@ -1087,7 +1086,7 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => { // a duplicate walk. void store.updateModelsForNodeType(nodeType) expect( - vi.mocked(assetService.getAssetsForNodeType) + vi.mocked(assetService.getAssetsPageForNodeType) ).toHaveBeenCalledTimes(2) }) }) @@ -1934,18 +1933,22 @@ describe('assetsStore - Model Assets Cache (non-cloud)', () => { it('caches model assets fetched by tag on non-cloud builds', async () => { const store = useAssetsStore() - vi.mocked(assetService.getAssetsByTag).mockResolvedValue([ - { - id: 'm1', - name: 'sd_xl_base_1.0.safetensors', - tags: ['checkpoints', 'models'] - }, - { id: 'm2', name: 'lora.safetensors', tags: ['loras', 'models'] } - ]) + vi.mocked(assetService.getAssetsPageByTag).mockResolvedValue({ + assets: [ + { + id: 'm1', + name: 'sd_xl_base_1.0.safetensors', + tags: ['checkpoints', 'models'] + }, + { id: 'm2', name: 'lora.safetensors', tags: ['loras', 'models'] } + ], + total: 2, + has_more: false + }) await store.updateModelsForTag('models') - expect(assetService.getAssetsByTag).toHaveBeenCalledWith( + expect(assetService.getAssetsPageByTag).toHaveBeenCalledWith( 'models', true, expect.anything() From b1b7b78973c70b400377968b7a6ef1be9e30ef72 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Fri, 19 Jun 2026 11:29:28 +1200 Subject: [PATCH 16/62] feat(assets): write model_type: tag from discovery Prefix the discovery folder_name with model_type: on model import and on model-type edits, gated on the supports_model_type_tags backend capability. Default off writes the legacy bare directory-path tag unchanged, so this is a no-op until the backend declares the cutover; the flag is the same authoritative signal FE-1076 grouping reads. The FE writes only the single selected folder_name's model_type: form; the backend keeps the bare directory-path twin in sync (and forward-expands shared paths), so the edit path manages just the model_type: tags and the resolver read path is untouched. Mirrors the read side: an uncovered asset with no model_type: tag still routes by its bare tag. --- .../components/modelInfo/ModelInfoPanel.vue | 23 +++- .../composables/useUploadModelWizard.test.ts | 44 +++++++- .../composables/useUploadModelWizard.ts | 13 ++- .../assets/utils/assetMetadataUtils.test.ts | 106 +++++++++++++++++- .../assets/utils/assetMetadataUtils.ts | 61 +++++++++- 5 files changed, 236 insertions(+), 11 deletions(-) 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..c776728b6d2 100644 --- a/src/platform/assets/composables/useUploadModelWizard.test.ts +++ b/src/platform/assets/composables/useUploadModelWizard.test.ts @@ -38,7 +38,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 + ) } })) @@ -279,6 +282,45 @@ describe('useUploadModelWizard', () => { expect(result?.modelType).toBe('checkpoints') }) + it('writes the namespaced model_type: tag when the backend supports it', async () => { + const { assetService } = + await import('@/platform/assets/services/assetService') + const { api } = await import('@/scripts/api') + 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 + ) + + try { + const wizard = setupUploadModelWizard(modelTypes, { + requiredModelType: 'checkpoints' + }) + wizard.wizardData.value.url = 'https://civitai.com/models/12345' + + await wizard.uploadModel() + + expect(assetService.uploadAssetAsync).toHaveBeenCalledWith( + expect.objectContaining({ + tags: ['models', 'model_type:checkpoints'], + user_metadata: expect.objectContaining({ + model_type: 'checkpoints' + }) + }) + ) + } finally { + vi.mocked(api.getServerFeature).mockImplementation( + (_name, defaultValue) => defaultValue + ) + } + }) + it('returns the synced asset filename for sync imports', 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..91f73e5de2d 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,10 @@ 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, + 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 +72,7 @@ export function useUploadModelWizard( options: UploadModelWizardOptions = {} ) { const { t } = useI18n() + const { flags } = useFeatureFlags() const assetsStore = useAssetsStore() const assetDownloadStore = useAssetDownloadStore() const modelToNodeStore = useModelToNodeStore() @@ -317,7 +322,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..c4039a32285 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,104 @@ 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' + ) + }) +}) + +describe('getEditableModelType', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('returns the stripped model_type value in model_type mode', () => { + expect( + getEditableModelType( + asset(['models', 'checkpoints', 'model_type:checkpoints']), + true + ) + ).toBe('checkpoints') + }) + + 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 (default)', () => { + expect(getEditableModelType(asset(['models', 'checkpoints', 'sdxl']))).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 (default)', () => { + expect( + buildModelTypeTagUpdate(asset(['models', 'checkpoints']), 'loras') + ).toEqual(['models', 'loras']) + }) + + it('preserves user labels and swaps only the subtype tag when mode is off', () => { + expect( + buildModelTypeTagUpdate(asset(['models', 'checkpoints', 'sdxl']), 'loras') + ).toEqual(['models', 'sdxl', 'loras']) + }) + + it('writes only the model_type form for a covered asset, leaving the bare twin for the backend', () => { + expect( + buildModelTypeTagUpdate( + asset(['models', 'checkpoints', 'model_type:checkpoints']), + 'loras', + true + ) + ).toEqual(['models', 'checkpoints', 'model_type:loras']) + }) + + it('replaces every existing model_type form for a shared-path dual-tagged asset', () => { + expect( + buildModelTypeTagUpdate( + asset([ + 'models', + 'diffusion_models', + 'model_type:diffusion_models', + 'model_type:unet_gguf' + ]), + 'loras', + true + ) + ).toEqual(['models', 'diffusion_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 untouched in model_type mode', () => { + expect( + buildModelTypeTagUpdate( + asset(['models', 'checkpoints', 'model_type:checkpoints', 'sdxl']), + 'loras', + true + ) + ).toEqual(['models', 'checkpoints', 'sdxl', 'model_type:loras']) + }) +}) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index d5efca1a214..ac8ff81fb6f 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -145,7 +145,7 @@ export function getSourceName(url: string): string { return 'Source' } -const MODEL_TYPE_TAG_PREFIX = 'model_type:' +export const MODEL_TYPE_TAG_PREFIX = 'model_type:' /** * Extracts the model type from asset tags as a bare (non-namespaced) value. @@ -162,6 +162,15 @@ 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. + */ +export function toModelTypeTag(folderName: string): string { + 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 +179,56 @@ function getModelTypeTagValues(asset: AssetItem): string[] { .filter((tag) => tag.length > 0) } +/** + * Resolves the folder_name shown as the asset's current model type in the edit + * dropdown. In `modelTypeMode` the stripped `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 = false +): string | null { + if (modelTypeMode) { + const [modelType] = getModelTypeTagValues(asset) + if (modelType) return modelType + } + return getAssetModelType(asset) +} + +/** + * Computes the tag set for re-typing a model asset to `newFolderName`. In + * `modelTypeMode` only the `model_type:` form is written — the backend keeps the + * bare directory-path twin in sync, so existing `model_type:` tags are dropped + * (covered assets) or the bare current type is dropped (uncovered assets) and + * the new `model_type:` is added. Outside the mode it swaps the + * legacy bare subtype tag, preserving the pre-namespace behavior. + */ +export function buildModelTypeTagUpdate( + asset: AssetItem, + newFolderName: string, + modelTypeMode = false +): string[] { + if (!modelTypeMode) { + const currentType = getAssetModelType(asset) + return asset.tags.filter((tag) => tag !== currentType).concat(newFolderName) + } + + const modelTypeTags = asset.tags.filter((tag) => + tag.startsWith(MODEL_TYPE_TAG_PREFIX) + ) + const currentBareType = getAssetModelType(asset) + const tagsToRemove = + modelTypeTags.length > 0 + ? new Set(modelTypeTags) + : new Set(currentBareType ? [currentBareType] : []) + + return asset.tags + .filter((tag) => !tagsToRemove.has(tag)) + .concat(toModelTypeTag(newFolderName)) +} + /** Legacy grouping: each non-`models` tag's top-level path segment. */ function getBareTagCategories(asset: AssetItem): string[] { return asset.tags From 0f71ca4d489ca6563646c724d684f68e7b8de45d Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Fri, 19 Jun 2026 15:37:44 +1200 Subject: [PATCH 17/62] test(assets): pin import write keeps user_metadata.model_type bare Assert the import-by-URL path namespaces only the tag (model_type:) while user_metadata.model_type stays the bare folder_name, since that value still feeds download tracking, category refresh, and the progress label. --- .../composables/useUploadModelWizard.test.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/platform/assets/composables/useUploadModelWizard.test.ts b/src/platform/assets/composables/useUploadModelWizard.test.ts index c776728b6d2..205df312839 100644 --- a/src/platform/assets/composables/useUploadModelWizard.test.ts +++ b/src/platform/assets/composables/useUploadModelWizard.test.ts @@ -282,7 +282,7 @@ describe('useUploadModelWizard', () => { expect(result?.modelType).toBe('checkpoints') }) - it('writes the namespaced model_type: tag when the backend supports it', async () => { + 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') const { api } = await import('@/scripts/api') @@ -306,14 +306,10 @@ describe('useUploadModelWizard', () => { await wizard.uploadModel() - expect(assetService.uploadAssetAsync).toHaveBeenCalledWith( - expect.objectContaining({ - tags: ['models', 'model_type:checkpoints'], - user_metadata: expect.objectContaining({ - model_type: 'checkpoints' - }) - }) - ) + 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') } finally { vi.mocked(api.getServerFeature).mockImplementation( (_name, defaultValue) => defaultValue From c848833fb8fd2820231fd55f07acb9643f7b49ac Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Wed, 24 Jun 2026 11:33:28 +1200 Subject: [PATCH 18/62] fix(assets): recognize model_type: prefix in import type-mismatch guard The required-type guard and imported-type label compared the bare folder_name against raw asset tags, so a namespaced model_type: tag was treated as a type mismatch (and leaked the raw prefix into the mismatch label). Strip the model_type: prefix before comparing/labelling so a namespaced tag satisfies the required type. Not reachable in production (the dual-write keeps the bare twin), but hardens the import path against a namespaced-only asset. --- .../composables/useUploadModelWizard.test.ts | 61 +++++++++++++++++++ .../composables/useUploadModelWizard.ts | 14 +++-- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/platform/assets/composables/useUploadModelWizard.test.ts b/src/platform/assets/composables/useUploadModelWizard.test.ts index 205df312839..da339706803 100644 --- a/src/platform/assets/composables/useUploadModelWizard.test.ts +++ b/src/platform/assets/composables/useUploadModelWizard.test.ts @@ -310,6 +310,8 @@ describe('useUploadModelWizard', () => { .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() } finally { vi.mocked(api.getServerFeature).mockImplementation( (_name, defaultValue) => defaultValue @@ -385,6 +387,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 91f73e5de2d..131bb88dd2e 100644 --- a/src/platform/assets/composables/useUploadModelWizard.ts +++ b/src/platform/assets/composables/useUploadModelWizard.ts @@ -14,6 +14,7 @@ import { assetService } from '@/platform/assets/services/assetService' import type { ImportSource } from '@/platform/assets/types/importSource' import { getAssetFilename, + stripModelTypePrefix, toModelTypeTag } from '@/platform/assets/utils/assetMetadataUtils' import { validateSourceUrl } from '@/platform/assets/utils/importSourceUtil' @@ -276,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' From 5101631f7c7c8e3f7fa1deafdc1d5a7dd75aaa01 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Sun, 12 Jul 2026 21:04:01 +1200 Subject: [PATCH 19/62] refactor(assets): require modelTypeMode on the write-path helpers Matches the read-path helpers: new call sites must choose the tag mode explicitly instead of silently inheriting legacy behavior. --- .../assets/utils/assetMetadataUtils.test.ts | 18 +++++++++++------- .../assets/utils/assetMetadataUtils.ts | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index c4039a32285..0da4910608e 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -886,10 +886,10 @@ describe('getEditableModelType', () => { expect(getEditableModelType(asset(['models', 'sam2']), true)).toBe('sam2') }) - it('uses the legacy first-non-models tag when mode is off (default)', () => { - expect(getEditableModelType(asset(['models', 'checkpoints', 'sdxl']))).toBe( - 'checkpoints' - ) + 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', () => { @@ -904,15 +904,19 @@ describe('buildModelTypeTagUpdate', () => { tags }) - it('swaps the bare subtype tag when mode is off (default)', () => { + it('swaps the bare subtype tag when mode is off', () => { expect( - buildModelTypeTagUpdate(asset(['models', 'checkpoints']), 'loras') + 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') + buildModelTypeTagUpdate( + asset(['models', 'checkpoints', 'sdxl']), + 'loras', + false + ) ).toEqual(['models', 'sdxl', 'loras']) }) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index ac8ff81fb6f..dba937f03b8 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -188,7 +188,7 @@ function getModelTypeTagValues(asset: AssetItem): string[] { */ export function getEditableModelType( asset: AssetItem, - modelTypeMode = false + modelTypeMode: boolean ): string | null { if (modelTypeMode) { const [modelType] = getModelTypeTagValues(asset) @@ -208,7 +208,7 @@ export function getEditableModelType( export function buildModelTypeTagUpdate( asset: AssetItem, newFolderName: string, - modelTypeMode = false + modelTypeMode: boolean ): string[] { if (!modelTypeMode) { const currentType = getAssetModelType(asset) From e0dfcd13f2be1dfe5b155b35f241e0b961649a84 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 21 Jul 2026 09:47:12 +1200 Subject: [PATCH 20/62] fix(assets): preserve sibling model_type memberships on re-type Re-typing a multi-type asset previously replaced its entire model_type: tag set, silently destroying the other type memberships. Replace only the primary membership (its model_type tag and stale bare twin), keeping siblings intact. Primary is the lexicographically-first model_type value so the choice is deterministic regardless of backend tag order, and the edit dropdown displays that same primary. Extract the selection into one shared helper and privatize the unused tag-prefix export. --- .../assets/utils/assetMetadataUtils.test.ts | 66 ++++++++++++++++--- .../assets/utils/assetMetadataUtils.ts | 51 ++++++++------ 2 files changed, 89 insertions(+), 28 deletions(-) diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index 0da4910608e..fdd44425062 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -873,15 +873,41 @@ describe('getEditableModelType', () => { tags }) - it('returns the stripped model_type value in model_type mode', () => { + it('prefers the model_type value over a distinct bare tag in model_type mode', () => { expect( getEditableModelType( - asset(['models', 'checkpoints', 'model_type:checkpoints']), + 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') }) @@ -920,17 +946,17 @@ describe('buildModelTypeTagUpdate', () => { ).toEqual(['models', 'sdxl', 'loras']) }) - it('writes only the model_type form for a covered asset, leaving the bare twin for the backend', () => { + 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', 'checkpoints', 'model_type:loras']) + ).toEqual(['models', 'model_type:loras']) }) - it('replaces every existing model_type form for a shared-path dual-tagged asset', () => { + it('replaces only the primary membership, preserving sibling model_type memberships', () => { expect( buildModelTypeTagUpdate( asset([ @@ -942,7 +968,31 @@ describe('buildModelTypeTagUpdate', () => { 'loras', true ) - ).toEqual(['models', 'diffusion_models', 'model_type:loras']) + ).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', () => { @@ -951,13 +1001,13 @@ describe('buildModelTypeTagUpdate', () => { ).toEqual(['models', 'model_type:loras']) }) - it('keeps user labels untouched in model_type mode', () => { + 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', 'checkpoints', 'sdxl', 'model_type:loras']) + ).toEqual(['models', 'sdxl', 'model_type:loras']) }) }) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index dba937f03b8..be934925c02 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -145,7 +145,7 @@ export function getSourceName(url: string): string { return 'Source' } -export const MODEL_TYPE_TAG_PREFIX = 'model_type:' +const MODEL_TYPE_TAG_PREFIX = 'model_type:' /** * Extracts the model type from asset tags as a bare (non-namespaced) value. @@ -179,9 +179,20 @@ 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 stripped `model_type:` value is authoritative + * 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. @@ -191,19 +202,21 @@ export function getEditableModelType( modelTypeMode: boolean ): string | null { if (modelTypeMode) { - const [modelType] = getModelTypeTagValues(asset) - if (modelType) return modelType + 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 `model_type:` form is written — the backend keeps the - * bare directory-path twin in sync, so existing `model_type:` tags are dropped - * (covered assets) or the bare current type is dropped (uncovered assets) and - * the new `model_type:` is added. Outside the mode it swaps the - * legacy bare subtype tag, preserving the pre-namespace behavior. + * `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, @@ -215,18 +228,16 @@ export function buildModelTypeTagUpdate( return asset.tags.filter((tag) => tag !== currentType).concat(newFolderName) } - const modelTypeTags = asset.tags.filter((tag) => - tag.startsWith(MODEL_TYPE_TAG_PREFIX) - ) + const primary = getPrimaryModelType(asset) const currentBareType = getAssetModelType(asset) const tagsToRemove = - modelTypeTags.length > 0 - ? new Set(modelTypeTags) + primary !== undefined + ? new Set([toModelTypeTag(primary), primary]) : new Set(currentBareType ? [currentBareType] : []) - return asset.tags - .filter((tag) => !tagsToRemove.has(tag)) - .concat(toModelTypeTag(newFolderName)) + 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. */ @@ -264,7 +275,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. @@ -274,8 +285,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) ) From 179e7a327bee34a8b906b821de03cbaa4d8be54f Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 21 Jul 2026 10:32:00 +1200 Subject: [PATCH 21/62] fix(assets): guard toModelTypeTag against double-prefixing --- src/platform/assets/utils/assetMetadataUtils.test.ts | 6 ++++++ src/platform/assets/utils/assetMetadataUtils.ts | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index fdd44425062..ffbe05331aa 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -864,6 +864,12 @@ describe('toModelTypeTag', () => { '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', () => { diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index be934925c02..0c60db4fbcb 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -166,8 +166,11 @@ export function getAssetModelType(asset: AssetItem): string | 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}` } From 8179340bf6c0ce2fb89f236568ab7e4958f91b5d Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 30 Jun 2026 12:38:13 +1200 Subject: [PATCH 22/62] fix(assets): make model type read-only on core Retagging a model rewrites its asset tags, but core is filesystem-backed and does not yet move the file to match the new type, so an edit would leave the tag and the on-disk location diverged. Gate the model type dropdown to cloud builds; on core the field renders the existing read-only label. Revert once core implements move-on-retag (or the asset system no longer requires the file move). --- .../modelInfo/ModelInfoPanel.test.ts | 26 ++++++++++++++++++- .../components/modelInfo/ModelInfoPanel.vue | 8 ++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/platform/assets/components/modelInfo/ModelInfoPanel.test.ts b/src/platform/assets/components/modelInfo/ModelInfoPanel.test.ts index fc731e82b59..b10ef714a48 100644 --- a/src/platform/assets/components/modelInfo/ModelInfoPanel.test.ts +++ b/src/platform/assets/components/modelInfo/ModelInfoPanel.test.ts @@ -1,5 +1,5 @@ import { createTestingPinia } from '@pinia/testing' -import { describe, expect, it, vi } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { createI18n } from 'vue-i18n' import { render, screen } from '@testing-library/vue' @@ -14,6 +14,14 @@ vi.mock('@/composables/useCopyToClipboard', () => ({ }) })) +const mockDistribution = vi.hoisted(() => ({ isCloud: false })) +vi.mock('@/platform/distribution/types', async (importOriginal) => ({ + ...(await importOriginal()), + get isCloud() { + return mockDistribution.isCloud + } +})) + const i18n = createI18n({ legacy: false, locale: 'en', @@ -41,6 +49,10 @@ describe('ModelInfoPanel', () => { ...overrides }) + afterEach(() => { + mockDistribution.isCloud = false + }) + function renderPanel(asset: AssetDisplayItem) { return render(ModelInfoPanel, { props: { asset }, @@ -138,6 +150,18 @@ describe('ModelInfoPanel', () => { ).toBeInTheDocument() }) + it('shows an editable model type dropdown for a mutable asset on cloud', () => { + mockDistribution.isCloud = true + renderPanel(createMockAsset({ is_immutable: false })) + expect(screen.getByRole('combobox')).toBeInTheDocument() + }) + + it('keeps the model type read-only on core even for a mutable asset', () => { + mockDistribution.isCloud = false + renderPanel(createMockAsset({ is_immutable: false })) + expect(screen.queryByRole('combobox')).not.toBeInTheDocument() + }) + it('renders base models field', () => { const asset = createMockAsset({ user_metadata: { base_model: ['SDXL'] } diff --git a/src/platform/assets/components/modelInfo/ModelInfoPanel.vue b/src/platform/assets/components/modelInfo/ModelInfoPanel.vue index 88865b1f8a7..3678aa5546d 100644 --- a/src/platform/assets/components/modelInfo/ModelInfoPanel.vue +++ b/src/platform/assets/components/modelInfo/ModelInfoPanel.vue @@ -71,7 +71,7 @@ - (undefined) const isEditingDisplayName = ref(false) const isImmutable = computed(() => asset.is_immutable ?? true) +// Retagging a model rewrites its asset tags; core is filesystem-backed and does +// not yet move the file to match, so the model type is read-only off-cloud. +const isModelTypeEditable = computed(() => !isImmutable.value && isCloud) const displayName = computed( () => pendingUpdates.value.name ?? getAssetDisplayName(asset) ) @@ -321,7 +325,7 @@ function handleDisplayNameEdit(newName: string) { } const debouncedSaveModelType = useDebounceFn((newModelType: string) => { - if (isImmutable.value) return + if (!isModelTypeEditable.value) return const currentModelType = getEditableModelType( asset, flags.supportsModelTypeTags From 55f904715ef0da6eed23687c122b2f195d35434c Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 21 Jul 2026 10:24:22 +1200 Subject: [PATCH 23/62] fix(assets): explain the read-only model type and gate its save path --- src/locales/en/main.json | 1 + .../components/modelInfo/ModelInfoPanel.vue | 21 +++++----- .../assets/utils/assetMetadataUtils.test.ts | 38 +++++++++++++++++++ .../assets/utils/assetMetadataUtils.ts | 17 +++++++++ 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/locales/en/main.json b/src/locales/en/main.json index ca3727fb109..d723465f12d 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -3500,6 +3500,7 @@ "modelTagging": "Model Tagging", "modelType": "Model Type", "selectModelType": "Select model type...", + "modelTypeCoreReadonly": "Model type cannot be changed on core, moving the file is not yet supported.", "compatibleBaseModels": "Compatible Base Models", "addBaseModel": "Add base model...", "baseModelUnknown": "Base model unknown", diff --git a/src/platform/assets/components/modelInfo/ModelInfoPanel.vue b/src/platform/assets/components/modelInfo/ModelInfoPanel.vue index 3678aa5546d..c2c7df3dade 100644 --- a/src/platform/assets/components/modelInfo/ModelInfoPanel.vue +++ b/src/platform/assets/components/modelInfo/ModelInfoPanel.vue @@ -87,7 +87,11 @@ -
+
{{ modelTypes.find((o) => o.value === selectedModelType)?.name ?? t('assetBrowser.unknown') @@ -233,7 +237,6 @@ import { useModelTypes } from '@/platform/assets/composables/useModelTypes' import { isCloud } from '@/platform/distribution/types' import type { AssetUserMetadata } from '@/platform/assets/schemas/assetSchema' import { - buildModelTypeTagUpdate, getAssetAdditionalTags, getAssetBaseModels, getAssetDescription, @@ -243,7 +246,8 @@ import { getAssetTriggerPhrases, getAssetUserDescription, getEditableModelType, - getSourceName + getSourceName, + resolveModelTypeTagUpdate } from '@/platform/assets/utils/assetMetadataUtils' import { useAssetsStore } from '@/stores/assetsStore' import { cn } from '@comfyorg/tailwind-utils' @@ -325,18 +329,13 @@ function handleDisplayNameEdit(newName: string) { } const debouncedSaveModelType = useDebounceFn((newModelType: string) => { - if (!isModelTypeEditable.value) return - const currentModelType = getEditableModelType( - asset, - flags.supportsModelTypeTags - ) - if (currentModelType === newModelType) return - const newTags = buildModelTypeTagUpdate( + const newTags = resolveModelTypeTagUpdate( asset, newModelType, + isModelTypeEditable.value, flags.supportsModelTypeTags ) - assetsStore.updateAssetTags(asset, newTags, cacheKey) + if (newTags) assetsStore.updateAssetTags(asset, newTags, cacheKey) }, 500) const baseModels = computed({ diff --git a/src/platform/assets/utils/assetMetadataUtils.test.ts b/src/platform/assets/utils/assetMetadataUtils.test.ts index ffbe05331aa..d72083c9754 100644 --- a/src/platform/assets/utils/assetMetadataUtils.test.ts +++ b/src/platform/assets/utils/assetMetadataUtils.test.ts @@ -28,6 +28,7 @@ import { getEditableModelType, getSourceName, resolveDisplayImageDimensions, + resolveModelTypeTagUpdate, stripModelTypePrefix, toModelTypeTag } from '@/platform/assets/utils/assetMetadataUtils' @@ -1017,3 +1018,40 @@ describe('buildModelTypeTagUpdate', () => { ).toEqual(['models', 'sdxl', 'model_type:loras']) }) }) + +describe('resolveModelTypeTagUpdate', () => { + const asset = (tags: string[]): AssetItem => ({ + id: 'a', + name: 'model.safetensors', + tags + }) + + it('returns null when the model type is not editable', () => { + expect( + resolveModelTypeTagUpdate( + asset(['models', 'checkpoints']), + 'loras', + false, + true + ) + ).toBeNull() + }) + + it('returns null when the selected type is unchanged', () => { + expect( + resolveModelTypeTagUpdate( + asset(['models', 'checkpoints']), + 'checkpoints', + true, + false + ) + ).toBeNull() + }) + + it('delegates to buildModelTypeTagUpdate for an editable change', () => { + const tags = ['models', 'checkpoints'] + expect( + resolveModelTypeTagUpdate(asset(tags), 'loras', true, false) + ).toEqual(buildModelTypeTagUpdate(asset(tags), 'loras', false)) + }) +}) diff --git a/src/platform/assets/utils/assetMetadataUtils.ts b/src/platform/assets/utils/assetMetadataUtils.ts index 0c60db4fbcb..0d66314198d 100644 --- a/src/platform/assets/utils/assetMetadataUtils.ts +++ b/src/platform/assets/utils/assetMetadataUtils.ts @@ -243,6 +243,23 @@ export function buildModelTypeTagUpdate( return retained.includes(newTag) ? retained : retained.concat(newTag) } +/** + * Save gate for a model-type change: returns the tag set to persist, or null + * when the change must not be written (the type is read-only, or unchanged). + * Keeps tag-set semantics in buildModelTypeTagUpdate; this only decides whether + * to save. + */ +export function resolveModelTypeTagUpdate( + asset: AssetItem, + newFolderName: string, + isEditable: boolean, + modelTypeMode: boolean +): string[] | null { + if (!isEditable) return null + if (getEditableModelType(asset, modelTypeMode) === newFolderName) return null + return buildModelTypeTagUpdate(asset, newFolderName, modelTypeMode) +} + /** Legacy grouping: each non-`models` tag's top-level path segment. */ function getBareTagCategories(asset: AssetItem): string[] { return asset.tags From 631a2df1e765af702b503d8966aee3237417406d Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 7 Jul 2026 09:13:16 +1200 Subject: [PATCH 24/62] fix(dialog): keep non-modal dialogs open when focus lands on a toast Toasts teleport to body but were missing from the reka outside-layer allowlist, so a non-modal dialog (e.g. Settings) dismissed itself when focus recovery landed on a toast's close button. This happens whenever an action button disables itself while its request is in flight and the request fails: the browser drops focus from the disabled button and it can resurface on the error toast. Latent on main; deterministic once the sidebar rework shifted chunk timing enough for the loading state to render before the failure response. --- src/components/dialog/GlobalDialog.test.ts | 2 +- src/components/dialog/rekaPrimeVueBridge.ts | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/components/dialog/GlobalDialog.test.ts b/src/components/dialog/GlobalDialog.test.ts index 62fba47894f..e51a78d0241 100644 --- a/src/components/dialog/GlobalDialog.test.ts +++ b/src/components/dialog/GlobalDialog.test.ts @@ -426,7 +426,7 @@ describe('shouldPreventRekaDismiss', () => { expect(event.defaultPrevented).toBe(true) }) - it.for(['p-dialog', 'p-select-overlay'])( + it.for(['p-dialog', 'p-select-overlay', 'p-toast'])( 'focus-outside on a sibling %s portal does not dismiss the parent', (className) => { const overlay = document.createElement('div') diff --git a/src/components/dialog/rekaPrimeVueBridge.ts b/src/components/dialog/rekaPrimeVueBridge.ts index d8c6e7b0d28..2d1b427d0cf 100644 --- a/src/components/dialog/rekaPrimeVueBridge.ts +++ b/src/components/dialog/rekaPrimeVueBridge.ts @@ -1,10 +1,13 @@ // PrimeVue overlays (Select, ColorPicker, Popover, Autocomplete, stacked -// PrimeVue Dialogs) teleport to body. Reka treats clicks on body-portaled -// elements as outside its dialog and would auto-dismiss on the first -// interaction, tearing the overlay down mid-interaction. Treat any -// PrimeVue overlay click as inside. +// PrimeVue Dialogs, Toasts) teleport to body. Reka treats clicks on +// body-portaled elements as outside its dialog and would auto-dismiss on the +// first interaction, tearing the overlay down mid-interaction. Treat any +// PrimeVue overlay click as inside. Toasts matter for focus-outside: when a +// button disables itself mid-action (e.g. a confirm entering its loading +// state), the browser drops focus and recovery can land on the toast's close +// button, which must not dismiss the dialog underneath. const PRIMEVUE_OVERLAY_SELECTORS = - '.p-select-overlay, .p-colorpicker-panel, .p-popover, .p-autocomplete-overlay, .p-overlay, .p-overlay-mask, .p-dialog' + '.p-select-overlay, .p-colorpicker-panel, .p-popover, .p-autocomplete-overlay, .p-overlay, .p-overlay-mask, .p-dialog, .p-toast' // Reka portals its own dialogs / popovers / menus into the body too. When a // nested Reka layer opens on top of a non-modal parent, the parent's From 647697aaea6c63eaca0acbced99e46bdf93b9f72 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Thu, 25 Jun 2026 12:52:36 +1200 Subject: [PATCH 25/62] feat(assets): gate model library asset view behind a runtime setting (FE-1142) Add a hidden, experimental Comfy.Assets.ModelLibraryAssetView setting that controls whether the model library opens the asset browser or the sidebar, defaulting to the asset browser on Cloud and the sidebar on Core. The model-library toggle now reads this setting instead of Comfy.Assets.UseAssetAPI, decoupling the sidebar-vs-asset-browser view from Asset API enablement so the asset-backed view stays reachable on Core for testing without forcing the cutover. --- .../settings/constants/coreSettings.ts | 9 +++ src/schemas/apiSchema.ts | 1 + src/stores/workspace/sidebarTabStore.test.ts | 72 +++++++++++++++++-- src/stores/workspace/sidebarTabStore.ts | 2 +- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/platform/settings/constants/coreSettings.ts b/src/platform/settings/constants/coreSettings.ts index d59802d81d5..e17840ea602 100644 --- a/src/platform/settings/constants/coreSettings.ts +++ b/src/platform/settings/constants/coreSettings.ts @@ -1215,6 +1215,15 @@ export const CORE_SETTINGS: SettingParams[] = [ defaultValue: isCloud ? true : false, experimental: true }, + { + id: 'Comfy.ModelLibrary.UseAssetBrowser', + name: 'Use the asset browser for the model library', + type: 'hidden', + tooltip: + 'When enabled, the model library opens the asset browser. When disabled, it opens the sidebar tree.', + defaultValue: isCloud ? true : false, + experimental: true + }, { id: 'Comfy.VersionCompatibility.DisableWarnings', name: 'Disable version compatibility warnings', diff --git a/src/schemas/apiSchema.ts b/src/schemas/apiSchema.ts index 6d843a7de34..3e71223b0c6 100644 --- a/src/schemas/apiSchema.ts +++ b/src/schemas/apiSchema.ts @@ -428,6 +428,7 @@ const zSettings = z.object({ 'Comfy.VueNodes.Enabled': z.boolean(), 'Comfy.AppBuilder.VueNodeSwitchDismissed': z.boolean(), 'Comfy.Assets.UseAssetAPI': z.boolean(), + 'Comfy.ModelLibrary.UseAssetBrowser': z.boolean(), 'Comfy.Queue.QPOV2': z.boolean(), 'Comfy.Queue.ShowRunProgressBar': z.boolean(), 'Comfy-Desktop.AutoUpdate': z.boolean(), diff --git a/src/stores/workspace/sidebarTabStore.test.ts b/src/stores/workspace/sidebarTabStore.test.ts index cef96df72b5..0d0bc05088c 100644 --- a/src/stores/workspace/sidebarTabStore.test.ts +++ b/src/stores/workspace/sidebarTabStore.test.ts @@ -5,12 +5,24 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' -const { mockGetSetting, mockRegisterCommand, mockRegisterCommands } = - vi.hoisted(() => ({ +const { + mockGetSetting, + mockRegisterCommand, + mockRegisterCommands, + mockBrowseModelAssets, + registeredCommands, + commandStoreCommands +} = vi.hoisted(() => { + const registeredCommands: { id: string; function: () => unknown }[] = [] + return { mockGetSetting: vi.fn(), - mockRegisterCommand: vi.fn(), - mockRegisterCommands: vi.fn() - })) + mockRegisterCommand: vi.fn((command) => registeredCommands.push(command)), + mockRegisterCommands: vi.fn(), + mockBrowseModelAssets: vi.fn(), + registeredCommands, + commandStoreCommands: [] as { id: string; function: () => unknown }[] + } +}) vi.mock('@/platform/settings/settingStore', () => ({ useSettingStore: () => ({ @@ -21,7 +33,7 @@ vi.mock('@/platform/settings/settingStore', () => ({ vi.mock('@/stores/commandStore', () => ({ useCommandStore: () => ({ registerCommand: mockRegisterCommand, - commands: [] + commands: commandStoreCommands }) })) @@ -99,8 +111,18 @@ describe('useSidebarTabStore', () => { mockGetSetting.mockReset() mockRegisterCommand.mockClear() mockRegisterCommands.mockClear() + mockBrowseModelAssets.mockClear() + registeredCommands.length = 0 + commandStoreCommands.length = 0 }) + const toggleModelLibrary = async () => { + const toggleCommand = registeredCommands.find( + (command) => command.id === 'Workspace.ToggleSidebarTab.model-library' + ) + await toggleCommand?.function() + } + it('registers the job history tab when QPO V2 is enabled', () => { mockGetSetting.mockImplementation((key: string) => key === 'Comfy.Queue.QPOV2' ? true : undefined @@ -160,4 +182,42 @@ describe('useSidebarTabStore', () => { ]) expect(mockRegisterCommand).toHaveBeenCalledTimes(6) }) + + describe('model library view selection', () => { + it('toggles the sidebar tab when the asset view is disabled', async () => { + mockGetSetting.mockImplementation((key: string) => + key === 'Comfy.ModelLibrary.UseAssetBrowser' ? false : undefined + ) + commandStoreCommands.push({ + id: 'Comfy.BrowseModelAssets', + function: mockBrowseModelAssets + }) + + const store = useSidebarTabStore() + store.registerCoreSidebarTabs() + + await toggleModelLibrary() + + expect(store.activeSidebarTabId).toBe('model-library') + expect(mockBrowseModelAssets).not.toHaveBeenCalled() + }) + + it('opens the asset browser when the asset view is enabled', async () => { + mockGetSetting.mockImplementation((key: string) => + key === 'Comfy.ModelLibrary.UseAssetBrowser' ? true : undefined + ) + commandStoreCommands.push({ + id: 'Comfy.BrowseModelAssets', + function: mockBrowseModelAssets + }) + + const store = useSidebarTabStore() + store.registerCoreSidebarTabs() + + await toggleModelLibrary() + + expect(mockBrowseModelAssets).toHaveBeenCalledOnce() + expect(store.activeSidebarTabId).toBeNull() + }) + }) }) diff --git a/src/stores/workspace/sidebarTabStore.ts b/src/stores/workspace/sidebarTabStore.ts index 662d967544d..1e9686fe47c 100644 --- a/src/stores/workspace/sidebarTabStore.ts +++ b/src/stores/workspace/sidebarTabStore.ts @@ -78,7 +78,7 @@ export const useSidebarTabStore = defineStore('sidebarTab', () => { if ( tab.id === 'model-library' && - settingStore.get('Comfy.Assets.UseAssetAPI') + settingStore.get('Comfy.ModelLibrary.UseAssetBrowser') ) { await commandStore.commands .find((cmd) => cmd.id === 'Comfy.BrowseModelAssets') From 90489720f13449123bda3437040efc4d64a8d6ed Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Thu, 2 Jul 2026 08:39:05 +1200 Subject: [PATCH 26/62] feat(assets): back the model library sidebar with the asset API Derive folders from model_type: tags and use loader_path for the tree, widget value, and viewMetadata. Gate local-disk metadata/preview reads on !isCloud. The models tag is walked once and bucketed per category (multi-category assets land in each), memoized behind a request id so an in-flight walk cannot repopulate an invalidated cache. Assets with a null loader_path are unloadable (orphans) and are dropped with a warning rather than minting a widget value from the deprecated name field; bare-tag backends that predate loader_path keep the name fallback. --- .../tabs/modelLibrary/ModelTreeLeaf.vue | 6 + src/platform/assets/schemas/assetSchema.ts | 8 +- .../assets/services/assetService.test.ts | 232 +++++++++++++++++- src/platform/assets/services/assetService.ts | 174 ++++++++++--- src/stores/modelStore.test.ts | 21 +- src/stores/modelStore.ts | 20 +- 6 files changed, 391 insertions(+), 70 deletions(-) diff --git a/src/components/sidebar/tabs/modelLibrary/ModelTreeLeaf.vue b/src/components/sidebar/tabs/modelLibrary/ModelTreeLeaf.vue index 442a7b24e6c..5950e27d98c 100644 --- a/src/components/sidebar/tabs/modelLibrary/ModelTreeLeaf.vue +++ b/src/components/sidebar/tabs/modelLibrary/ModelTreeLeaf.vue @@ -24,6 +24,7 @@ import type { CSSProperties } from 'vue' import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue' import TreeExplorerTreeNode from '@/components/common/TreeExplorerTreeNode.vue' +import { isCloud } from '@/platform/distribution/types' import { useSettingStore } from '@/platform/settings/settingStore' import type { ComfyModelDef } from '@/stores/modelStore' import type { RenderedTreeExplorerNode } from '@/types/treeExplorerTypes' @@ -41,6 +42,11 @@ const modelPreviewUrl = computed(() => { if (modelDef.value.image) { return modelDef.value.image } + // The preview endpoint reads a rendered thumbnail off local disk, which is + // unavailable on Cloud (model bytes live in object storage). + if (isCloud) { + return '' + } const folder = modelDef.value.directory const path_index = modelDef.value.path_index const extension = modelDef.value.file_name.split('.').pop() diff --git a/src/platform/assets/schemas/assetSchema.ts b/src/platform/assets/schemas/assetSchema.ts index 9636f1a1573..cf85329dd58 100644 --- a/src/platform/assets/schemas/assetSchema.ts +++ b/src/platform/assets/schemas/assetSchema.ts @@ -11,6 +11,8 @@ const zAsset = z.object({ tags: z.array(z.string()).optional().default([]), preview_id: z.string().nullable().optional(), display_name: z.string().optional(), + /** Path within the model's category folder, i.e. the value a loader widget expects. */ + loader_path: z.string().nullish(), preview_url: z.string().optional(), thumbnail_url: z.string().optional(), created_at: z.string().optional(), @@ -27,11 +29,6 @@ const zAssetResponse = zListAssetsResponse assets: z.array(zAsset) }) -const zModelFolder = z.object({ - name: z.string(), - folders: z.array(z.string()) -}) - // Zod schema for ModelFile to align with interface const zModelFile = z.object({ name: z.string(), @@ -100,7 +97,6 @@ export type AssetItem = z.infer export type AssetResponse = z.infer export type AssetMetadata = z.infer export type AsyncUploadResponse = z.infer -export type ModelFolder = z.infer export type ModelFile = z.infer /** Payload for updating an asset via PUT /assets/:id */ diff --git a/src/platform/assets/services/assetService.test.ts b/src/platform/assets/services/assetService.test.ts index e18a6aecbdb..29f9f4bb8b4 100644 --- a/src/platform/assets/services/assetService.test.ts +++ b/src/platform/assets/services/assetService.test.ts @@ -12,6 +12,7 @@ import { api } from '@/scripts/api' const mockDistributionState = vi.hoisted(() => ({ isCloud: false })) const mockSettingStoreGet = vi.hoisted(() => vi.fn(() => false)) +const mockSupportsModelTypeTags = vi.hoisted(() => ({ value: true })) vi.mock('@/platform/distribution/types', () => ({ get isCloud() { @@ -19,6 +20,16 @@ vi.mock('@/platform/distribution/types', () => ({ } })) +vi.mock('@/composables/useFeatureFlags', () => ({ + useFeatureFlags: () => ({ + flags: { + get supportsModelTypeTags() { + return mockSupportsModelTypeTags.value + } + } + }) +})) + vi.mock('@/platform/settings/settingStore', () => ({ useSettingStore: vi.fn(() => ({ get: mockSettingStoreGet @@ -93,6 +104,7 @@ function validAsset(overrides: Partial = {}): AssetItem { return { id: 'asset-1', name: 'model.safetensors', + loader_path: overrides.name ?? 'model.safetensors', tags: ['models'], ...overrides } @@ -422,33 +434,227 @@ describe(assetService.deleteAsset, () => { }) }) -describe(assetService.getAssetModelFolders, () => { +describe(assetService.getAssetModels, () => { beforeEach(() => { vi.clearAllMocks() + assetService.invalidateModelBuckets() + mockSupportsModelTypeTags.value = true }) - it('requests missing-tag exclusion and returns alphabetical unique folders without include_public', async () => { + it('walks the models tag once, excluding missing assets', async () => { fetchApiMock.mockResolvedValueOnce( buildAssetListResponse([ - validAsset({ id: 'a', tags: ['models', 'loras'] }), - validAsset({ id: 'b', tags: ['models', 'checkpoints'] }), - validAsset({ id: 'c', tags: ['models', 'configs'] }), - validAsset({ id: 'e', tags: ['models', 'loras'] }) + validAsset({ id: 'a', tags: ['models', 'model_type:checkpoints'] }) ]) ) - const folders = await assetService.getAssetModelFolders() - - expect(folders).toEqual([ - { name: 'checkpoints', folders: [] }, - { name: 'loras', folders: [] } - ]) + await assetService.getAssetModels('checkpoints') + expect(fetchApiMock).toHaveBeenCalledTimes(1) const requestedUrl = fetchApiMock.mock.calls[0]?.[0] as string const params = new URL(requestedUrl, 'http://localhost').searchParams - expect(params.has('include_public')).toBe(false) + expect(params.get('include_tags')).toBe('models') expect(params.get('exclude_tags')).toBe(MISSING_TAG) }) + + it('buckets by bare tags when model_type tags are unsupported', async () => { + mockSupportsModelTypeTags.value = false + fetchApiMock.mockResolvedValueOnce( + buildAssetListResponse([ + validAsset({ + id: 'a', + name: 'a.safetensors', + tags: ['models', 'checkpoints'] + }) + ]) + ) + + const models = await assetService.getAssetModels('checkpoints') + + expect(models).toEqual([{ name: 'a.safetensors', pathIndex: 0 }]) + }) + + it('drops uncategorized model assets with a warning', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) + fetchApiMock.mockResolvedValueOnce( + buildAssetListResponse([ + validAsset({ + id: 'ok', + name: 'ok.safetensors', + tags: ['models', 'model_type:loras'] + }), + validAsset({ + id: 'uncat', + name: 'orphan.safetensors', + tags: ['models'] + }) + ]) + ) + + const loras = await assetService.getAssetModels('loras') + + expect(loras).toEqual([{ name: 'ok.safetensors', pathIndex: 0 }]) + expect(warn).toHaveBeenCalledWith( + expect.stringContaining('orphan.safetensors') + ) + warn.mockRestore() + }) + + it('maps loader_path and drops unloadable assets without one', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) + fetchApiMock.mockResolvedValueOnce( + buildAssetListResponse([ + validAsset({ + id: 'nested', + name: 'model.safetensors', + loader_path: 'sdxl/model.safetensors', + tags: ['models', 'model_type:checkpoints'] + }), + validAsset({ + id: 'orphan', + name: 'orphan.safetensors', + loader_path: null, + tags: ['models', 'model_type:checkpoints'] + }), + validAsset({ + id: 'other-folder', + name: 'lora.safetensors', + tags: ['models', 'model_type:loras'] + }) + ]) + ) + + const models = await assetService.getAssetModels('checkpoints') + + expect(models).toEqual([{ name: 'sdxl/model.safetensors', pathIndex: 0 }]) + expect(warn).toHaveBeenCalledWith( + expect.stringContaining('orphan.safetensors') + ) + warn.mockRestore() + }) + + it('falls back to filename metadata then name on bare-tag backends', async () => { + mockSupportsModelTypeTags.value = false + fetchApiMock.mockResolvedValueOnce( + buildAssetListResponse([ + validAsset({ + id: 'cloud-hash', + name: 'blake3-content-hash', + loader_path: null, + user_metadata: { filename: 'sdxl/cloud-model.safetensors' }, + tags: ['models', 'checkpoints'] + }), + validAsset({ + id: 'bare', + name: 'plain.safetensors', + loader_path: null, + tags: ['models', 'checkpoints'] + }) + ]) + ) + + const models = await assetService.getAssetModels('checkpoints') + + expect(models).toEqual([ + { name: 'sdxl/cloud-model.safetensors', pathIndex: 0 }, + { name: 'plain.safetensors', pathIndex: 0 } + ]) + }) + + it('orders each folder subdirectories-first then files, alphabetically', async () => { + const checkpointAsset = (id: string, loaderPath: string) => + validAsset({ + id, + name: loaderPath.split('/').pop()!, + loader_path: loaderPath, + tags: ['models', 'model_type:checkpoints'] + }) + fetchApiMock.mockResolvedValueOnce( + buildAssetListResponse([ + checkpointAsset('1', 'sdxl/base.safetensors'), + checkpointAsset('2', 'v1-5.safetensors'), + checkpointAsset('3', 'sdxl/refiner.safetensors'), + checkpointAsset('4', 'anything.safetensors'), + checkpointAsset('5', 'dynamicrafter/model.safetensors') + ]) + ) + + const models = await assetService.getAssetModels('checkpoints') + + expect(models.map((m) => m.name)).toEqual([ + 'dynamicrafter/model.safetensors', + 'sdxl/base.safetensors', + 'sdxl/refiner.safetensors', + 'anything.safetensors', + 'v1-5.safetensors' + ]) + }) + + it('does not let a stale in-flight walk overwrite an invalidated cache', async () => { + let resolveStaleWalk!: (response: Response) => void + fetchApiMock.mockReturnValueOnce( + new Promise((resolve) => { + resolveStaleWalk = resolve + }) + ) + const staleRead = assetService.getAssetModels('checkpoints') + + assetService.invalidateModelBuckets() + + fetchApiMock.mockResolvedValueOnce( + buildAssetListResponse([ + validAsset({ + id: 'fresh', + name: 'fresh.safetensors', + tags: ['models', 'model_type:checkpoints'] + }) + ]) + ) + const freshModels = await assetService.getAssetModels('checkpoints') + expect(freshModels.map((m) => m.name)).toEqual(['fresh.safetensors']) + + resolveStaleWalk( + buildAssetListResponse([ + validAsset({ + id: 'stale', + name: 'stale.safetensors', + tags: ['models', 'model_type:checkpoints'] + }) + ]) + ) + await staleRead + + const cachedModels = await assetService.getAssetModels('checkpoints') + expect(cachedModels.map((m) => m.name)).toEqual(['fresh.safetensors']) + expect(fetchApiMock).toHaveBeenCalledTimes(2) + }) + + it('places multi-category assets in every folder from a single walk', async () => { + fetchApiMock.mockResolvedValueOnce( + buildAssetListResponse([ + validAsset({ + id: 'shared', + name: 'dual_use.safetensors', + loader_path: 'dual_use.safetensors', + tags: [ + 'models', + 'model_type:checkpoints', + 'model_type:diffusion_models' + ] + }) + ]) + ) + + const checkpoints = await assetService.getAssetModels('checkpoints') + const diffusion = await assetService.getAssetModels('diffusion_models') + + expect(checkpoints).toEqual([ + { name: 'dual_use.safetensors', pathIndex: 0 } + ]) + expect(diffusion).toEqual([{ name: 'dual_use.safetensors', pathIndex: 0 }]) + // Both folder reads resolve from a single memoized models walk. + expect(fetchApiMock).toHaveBeenCalledTimes(1) + }) }) describe(assetService.updateAsset, () => { diff --git a/src/platform/assets/services/assetService.ts b/src/platform/assets/services/assetService.ts index 47503665115..150db646174 100644 --- a/src/platform/assets/services/assetService.ts +++ b/src/platform/assets/services/assetService.ts @@ -1,6 +1,7 @@ import { fromZodError } from 'zod-validation-error' import { z } from 'zod' +import { useFeatureFlags } from '@/composables/useFeatureFlags' import { st } from '@/i18n' import { @@ -17,9 +18,9 @@ import type { AssetUpdatePayload, AsyncUploadResponse, ModelFile, - ModelFolder, TagsOperationResult } from '@/platform/assets/schemas/assetSchema' +import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils' import { isCloud } from '@/platform/distribution/types' import { useSettingStore } from '@/platform/settings/settingStore' import { api } from '@/scripts/api' @@ -187,6 +188,8 @@ const DEFAULT_LIMIT = 500 const INPUT_ASSETS_WITH_PUBLIC_LIMIT = 500 export const MODELS_TAG = 'models' +/** Prefix for the namespaced tag that carries a model's folder category, e.g. `model_type:checkpoints`. */ +const MODEL_TYPE_TAG_PREFIX = 'model_type:' export const INPUT_TAG = 'input' export const OUTPUT_TAG = 'output' /** Asset tag used by the backend for placeholder records that are not installed. */ @@ -210,6 +213,45 @@ function normalizeAssetTags(tags: string[]): string[] { return tags.map((tag) => tag.trim()).filter(Boolean) } +/** + * Resolves the model folder a tag represents, or undefined when the tag is not + * a folder category. `supports_model_type_tags` backends carry the category as + * a namespaced `model_type:` tag; older backends mint the bare folder. + */ +function modelFolderFromTag( + tag: string, + modelTypeMode: boolean +): string | undefined { + if (modelTypeMode) { + return tag.startsWith(MODEL_TYPE_TAG_PREFIX) + ? tag.slice(MODEL_TYPE_TAG_PREFIX.length) + : undefined + } + return tag === MODELS_TAG ? undefined : tag +} + +/** + * Orders loader paths as subdirectories before files at every level, + * alphabetical within each group. The asset API returns models in storage + * order, which would otherwise interleave root-level files with folder + * contents in the sidebar tree. + */ +function compareLoaderPaths(a: string, b: string): number { + const aSegments = a.split('/') + const bSegments = b.split('/') + const sharedDepth = Math.min(aSegments.length, bSegments.length) + for (let i = 0; i < sharedDepth; i++) { + const aIsFile = i === aSegments.length - 1 + const bIsFile = i === bSegments.length - 1 + if (aIsFile !== bIsFile) return aIsFile ? 1 : -1 + const order = aSegments[i].localeCompare(bSegments[i], undefined, { + numeric: true + }) + if (order !== 0) return order + } + return 0 +} + async function withCallerAbort( promise: Promise, signal?: AbortSignal @@ -270,6 +312,26 @@ function createAssetService() { let inputAssetsIncludingPublicRequestId = 0 let pendingInputAssetsIncludingPublic: Promise | null = null + /** + * Model assets bucketed by folder category, built from a single walk of the + * `models` tag rather than a fetch per category. Shared by the folder list + * and per-folder listings so the sidebar loads every model in one pass. + */ + let modelBuckets: Map | null = null + let modelBucketsRequestId = 0 + let pendingModelBuckets: Promise> | null = null + + /** + * Discards the cached model buckets so the next read re-walks the models + * tag. Bumping the request id keeps a walk that was already in flight from + * repopulating the cache with pre-invalidation data. + */ + function invalidateModelBuckets(): void { + modelBucketsRequestId++ + modelBuckets = null + pendingModelBuckets = null + } + /** Invalidates the cached public-inclusive input assets without aborting in-flight readers. */ function invalidateInputAssetsIncludingPublic(): void { inputAssetsIncludingPublicRequestId++ @@ -331,47 +393,95 @@ function createAssetService() { return validateAssetResponse(data) } /** - * Gets a list of model folder keys from the asset API - * - * Logic: - * 1. Extract directory names directly from asset tags - * 2. Filter out blacklisted directories - * 3. Return alphabetically sorted directories with assets - * - * @returns The list of model folder keys + * Walks every `models`-tagged asset once and buckets each into the folder + * categories carried by its `model_type:` tags. A single asset lands in every + * category it is tagged with (e.g. a shared-root model in both `checkpoints` + * and `diffusion_models`). Which folders are actually shown is decided by + * `/experiment/models`; models with no category tag are dropped with a warning + * rather than hidden silently. */ - async function getAssetModelFolders(): Promise { - const data = await handleAssetRequest( - { includeTags: [MODELS_TAG] }, - 'model folders' - ) + async function buildModelBuckets(): Promise> { + const assets = await getAllAssetsByTag(MODELS_TAG, true) + const modelTypeMode = useFeatureFlags().flags.supportsModelTypeTags + const buckets = new Map() + + for (const asset of assets) { + const folders = asset.tags + .map((tag) => modelFolderFromTag(tag, modelTypeMode)) + .filter((folder): folder is string => folder !== undefined) + + if (folders.length === 0) { + console.warn( + `Asset ${asset.id} (${asset.name}) is tagged '${MODELS_TAG}' but has no model category; skipping.` + ) + continue + } - // Blacklist directories we don't want to show - const blacklistedDirectories = new Set(['configs']) + // On loader_path-contract backends a null loader_path marks an + // unloadable asset (e.g. an orphan): it must not mint a widget value, + // and `name` is deprecated for path semantics. + if (modelTypeMode && !asset.loader_path) { + console.warn( + `Asset ${asset.id} (${asset.name}) has no loader_path; skipping.` + ) + continue + } - const folderTags = data.assets - .flatMap((asset) => asset.tags) - .filter((tag) => tag !== MODELS_TAG && !blacklistedDirectories.has(tag)) - const discoveredFolders = new Set(folderTags) + for (const folder of folders) { + const bucket = buckets.get(folder) + if (bucket) bucket.push(asset) + else buckets.set(folder, [asset]) + } + } - // Return only discovered folders in alphabetical order - const sortedFolders = Array.from(discoveredFolders).toSorted() - return sortedFolders.map((name) => ({ name, folders: [] })) + for (const bucket of buckets.values()) { + bucket.sort((a, b) => + compareLoaderPaths( + a.loader_path ?? getAssetFilename(a), + b.loader_path ?? getAssetFilename(b) + ) + ) + } + + return buckets + } + + /** Returns the memoized model buckets, walking the models tag on first read. */ + async function loadModelBuckets(): Promise> { + if (modelBuckets) return modelBuckets + if (pendingModelBuckets) return pendingModelBuckets + + const requestId = ++modelBucketsRequestId + pendingModelBuckets = buildModelBuckets() + .then((buckets) => { + if (requestId === modelBucketsRequestId) { + modelBuckets = buckets + } + return buckets + }) + .finally(() => { + if (requestId === modelBucketsRequestId) { + pendingModelBuckets = null + } + }) + + return pendingModelBuckets } /** - * Gets a list of models in the specified folder from the asset API + * Gets the models in the specified folder from the single models walk. * @param folder The folder to list models from, such as 'checkpoints' * @returns The list of model filenames within the specified folder */ async function getAssetModels(folder: string): Promise { - const data = await handleAssetRequest( - { includeTags: [MODELS_TAG, folder] }, - `models for ${folder}` - ) - - return data.assets.map((asset) => ({ - name: asset.name, + const buckets = await loadModelBuckets() + return (buckets.get(folder) ?? []).map((asset) => ({ + // `loader_path` is the category-relative path the loader widget expects + // and the source for the sidebar tree. Backends that predate it (bare-tag + // mode; today's cloud) fall back to the filename metadata — the same + // value the asset browser serializes — rather than `name`, which is a + // content hash on cloud. + name: asset.loader_path ?? getAssetFilename(asset), pathIndex: 0 })) } @@ -1000,8 +1110,8 @@ function createAssetService() { } return { - getAssetModelFolders, getAssetModels, + invalidateModelBuckets, isAssetAPIEnabled, isAssetBrowserEligible, shouldUseAssetBrowser, diff --git a/src/stores/modelStore.test.ts b/src/stores/modelStore.test.ts index 22492bbabe5..4c142f13547 100644 --- a/src/stores/modelStore.test.ts +++ b/src/stores/modelStore.test.ts @@ -22,8 +22,8 @@ vi.mock('@/scripts/api', () => ({ // Mock the assetService vi.mock('@/platform/assets/services/assetService', () => ({ assetService: { - getAssetModelFolders: vi.fn(), - getAssetModels: vi.fn() + getAssetModels: vi.fn(), + invalidateModelBuckets: vi.fn() } })) @@ -57,11 +57,8 @@ function enableMocks(useAssetAPI = false) { { name: 'vae', folders: ['/path/to/vae'] } ]) - // Mock asset API - also returns objects with name and folders properties - vi.mocked(assetService.getAssetModelFolders).mockResolvedValue([ - { name: 'checkpoints', folders: ['/path/to/checkpoints'] }, - { name: 'vae', folders: ['/path/to/vae'] } - ]) + // Asset API supplies only the per-folder model contents; folders come from + // api.getModelFolders in both paths. vi.mocked(assetService.getAssetModels).mockResolvedValue([ { name: 'sdxl.safetensors', pathIndex: 0 }, { name: 'sdv15.safetensors', pathIndex: 0 }, @@ -218,25 +215,23 @@ describe('useModelStore', () => { await store.loadModelFolders() const folderStore = await store.getLoadedModelFolder('checkpoints') - // Both APIs return objects with .name property, modelStore extracts folder.name in both cases + // Folders come from /experiment/models; legacy path also serves models. expect(api.getModelFolders).toHaveBeenCalledTimes(1) expect(api.getModels).toHaveBeenCalledWith('checkpoints') - expect(assetService.getAssetModelFolders).toHaveBeenCalledTimes(0) expect(assetService.getAssetModels).toHaveBeenCalledTimes(0) expect(folderStore).toBeDefined() expect(Object.keys(folderStore!.models)).toHaveLength(3) }) - it('should use asset API for complete workflow when UseAssetAPI setting is true', async () => { + it('should use asset API for model contents but /experiment/models for folders when UseAssetAPI is true', async () => { enableMocks(true) // useAssetAPI = true store = useModelStore() await store.loadModelFolders() const folderStore = await store.getLoadedModelFolder('checkpoints') - // Both APIs return objects with .name property, modelStore extracts folder.name in both cases - expect(assetService.getAssetModelFolders).toHaveBeenCalledTimes(1) + // Folders always come from /experiment/models; only contents use the asset API. + expect(api.getModelFolders).toHaveBeenCalledTimes(1) expect(assetService.getAssetModels).toHaveBeenCalledWith('checkpoints') - expect(api.getModelFolders).toHaveBeenCalledTimes(0) expect(api.getModels).toHaveBeenCalledTimes(0) expect(folderStore).toBeDefined() expect(Object.keys(folderStore!.models)).toHaveLength(3) diff --git a/src/stores/modelStore.ts b/src/stores/modelStore.ts index 4e92c12747c..fc7c2fb785c 100644 --- a/src/stores/modelStore.ts +++ b/src/stores/modelStore.ts @@ -3,6 +3,7 @@ import { computed, ref } from 'vue' import type { ModelFile } from '@/platform/assets/schemas/assetSchema' import { assetService } from '@/platform/assets/services/assetService' +import { isCloud } from '@/platform/distribution/types' import { useSettingStore } from '@/platform/settings/settingStore' import { api } from '@/scripts/api' @@ -100,6 +101,11 @@ export class ComfyModelDef { if (this.has_loaded_metadata || this.is_load_requested) { return } + // viewMetadata reads the safetensors header off local disk; on Cloud the + // model bytes live in object storage so there is nothing to read. + if (isCloud) { + return + } this.is_load_requested = true try { const metadata = await api.viewMetadata(this.directory, this.file_name) @@ -213,14 +219,14 @@ export const useModelStore = defineStore('models', () => { } /** - * Loads the model folders from the server + * Loads the model folders from the server. + * + * The folder list (and its registration order) always comes from + * `/experiment/models`, the source of truth for which model folders exist; + * only the per-folder contents differ between the asset API and legacy paths. */ async function loadModelFolders() { - const useAssetAPI: boolean = settingStore.get('Comfy.Assets.UseAssetAPI') - - const resData = useAssetAPI - ? await assetService.getAssetModelFolders() - : await api.getModelFolders() + const resData = await api.getModelFolders() modelFolderNames.value = resData.map((folder) => folder.name) modelFolderByName.value = {} const getModelsFunc = createGetModelsFunc() @@ -254,6 +260,7 @@ export const useModelStore = defineStore('models', () => { * folders' loaded contents. */ async function refreshModelFolder(folderName: string) { + assetService.invalidateModelBuckets() if (!(folderName in modelFolderByName.value)) { await refresh() return @@ -270,6 +277,7 @@ export const useModelStore = defineStore('models', () => { * currently-visible contents. */ async function refresh() { + assetService.invalidateModelBuckets() const previouslyLoaded = modelFolders.value .filter((folder) => folder.state === ResourceState.Loaded) .map((folder) => folder.directory) From 7a2fab69d4ea44266185b2c75296662e75b2f076 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Fri, 3 Jul 2026 10:43:09 +1200 Subject: [PATCH 27/62] feat(assets): rescan model roots on refresh and live-update the sidebar Manual refresh now kicks off POST /assets/seed (fire-and-forget, 409 tolerated, skipped on Cloud and on the legacy listing path) alongside the immediate re-load of known server state. The scan's fast phase already persists the category tags and loader filenames the sidebar needs, and the backend announces it with the assets.seed.fast_complete websocket event, so the store re-loads previously loaded folders on that event instead of polling /assets/seed/status (which cannot distinguish the fast phase from enrichment). --- .../assets/services/assetService.test.ts | 36 +++++++++++++++ src/platform/assets/services/assetService.ts | 22 +++++++++ src/stores/modelStore.test.ts | 45 ++++++++++++++++++- src/stores/modelStore.ts | 38 +++++++++++++--- 4 files changed, 135 insertions(+), 6 deletions(-) diff --git a/src/platform/assets/services/assetService.test.ts b/src/platform/assets/services/assetService.test.ts index 29f9f4bb8b4..ddc558b9078 100644 --- a/src/platform/assets/services/assetService.test.ts +++ b/src/platform/assets/services/assetService.test.ts @@ -657,6 +657,42 @@ describe(assetService.getAssetModels, () => { }) }) +describe(assetService.seedModelAssets, () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('POSTs the models root to the seed endpoint', async () => { + fetchApiMock.mockResolvedValueOnce( + buildResponse({ status: 'started' }, { status: 202 }) + ) + + await assetService.seedModelAssets() + + expect(fetchApiMock).toHaveBeenCalledWith('/assets/seed', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ roots: ['models'] }) + }) + }) + + it('treats an already-running scan (409) as success', async () => { + fetchApiMock.mockResolvedValueOnce( + buildResponse({ status: 'already_running' }, { ok: false, status: 409 }) + ) + + await expect(assetService.seedModelAssets()).resolves.toBeUndefined() + }) + + it('throws on other error statuses', async () => { + fetchApiMock.mockResolvedValueOnce( + buildResponse({}, { ok: false, status: 500 }) + ) + + await expect(assetService.seedModelAssets()).rejects.toThrow('500') + }) +}) + describe(assetService.updateAsset, () => { beforeEach(() => { vi.clearAllMocks() diff --git a/src/platform/assets/services/assetService.ts b/src/platform/assets/services/assetService.ts index 150db646174..d271df9c11f 100644 --- a/src/platform/assets/services/assetService.ts +++ b/src/platform/assets/services/assetService.ts @@ -181,6 +181,7 @@ function getLocalizedErrorMessage(errorCode: string): string { } const ASSETS_ENDPOINT = '/assets' +const ASSETS_SEED_ENDPOINT = '/assets/seed' const ASSETS_DOWNLOAD_ENDPOINT = '/assets/download' const ASSETS_EXPORT_ENDPOINT = '/assets/export' const EXPERIMENTAL_WARNING = `EXPERIMENTAL: If you are seeing this please make sure "Comfy.Assets.UseAssetAPI" is set to "false" in your ComfyUI Settings.\n` @@ -486,6 +487,26 @@ function createAssetService() { })) } + /** + * Asks the backend to rescan the model roots on disk so newly added files + * become assets. Fire-and-forget: the scan's fast (insert) phase already + * writes the category tags and filenames the sidebar needs and is announced + * by an `assets.seed.fast_complete` websocket event. A 409 means a scan is + * already running, which will emit the same event, so it is not an error. + */ + async function seedModelAssets(): Promise { + const res = await api.fetchApi(ASSETS_SEED_ENDPOINT, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ roots: ['models'] }) + }) + if (!res.ok && res.status !== 409) { + throw new Error( + `Unable to start asset scan: Server returned ${res.status}` + ) + } + } + /** * Checks if a widget input should use the asset browser based on both input name and node comfyClass * @@ -1112,6 +1133,7 @@ function createAssetService() { return { getAssetModels, invalidateModelBuckets, + seedModelAssets, isAssetAPIEnabled, isAssetBrowserEligible, shouldUseAssetBrowser, diff --git a/src/stores/modelStore.test.ts b/src/stores/modelStore.test.ts index 4c142f13547..083402c636e 100644 --- a/src/stores/modelStore.test.ts +++ b/src/stores/modelStore.test.ts @@ -15,6 +15,7 @@ vi.mock('@/scripts/api', () => ({ viewMetadata: vi.fn(), apiURL: vi.fn((path: string) => `http://localhost:8188${path}`), addEventListener: vi.fn(), + addCustomEventListener: vi.fn(), removeEventListener: vi.fn() } })) @@ -23,7 +24,8 @@ vi.mock('@/scripts/api', () => ({ vi.mock('@/platform/assets/services/assetService', () => ({ assetService: { getAssetModels: vi.fn(), - invalidateModelBuckets: vi.fn() + invalidateModelBuckets: vi.fn(), + seedModelAssets: vi.fn() } })) @@ -64,6 +66,7 @@ function enableMocks(useAssetAPI = false) { { name: 'sdv15.safetensors', pathIndex: 0 }, { name: 'noinfo.safetensors', pathIndex: 0 } ]) + vi.mocked(assetService.seedModelAssets).mockResolvedValue(undefined) vi.mocked(api.viewMetadata).mockImplementation((_, model) => { if (model === 'noinfo.safetensors') { @@ -206,6 +209,46 @@ describe('useModelStore', () => { expect(api.getModelFolders).toHaveBeenCalledTimes(2) expect(api.getModels).not.toHaveBeenCalled() }) + + it('kicks off a backend scan when models come from the asset API', async () => { + enableMocks(true) + store = useModelStore() + + await store.refresh() + + expect(assetService.seedModelAssets).toHaveBeenCalledTimes(1) + }) + + it('does not scan on the legacy listing path', async () => { + enableMocks(false) + store = useModelStore() + + await store.refresh() + + expect(assetService.seedModelAssets).not.toHaveBeenCalled() + }) + }) + + describe('scan fast-phase completion', () => { + it('re-loads previously loaded folders when the event fires', async () => { + enableMocks(true) + store = useModelStore() + await store.loadModelFolders() + await store.getLoadedModelFolder('checkpoints') + expect(assetService.getAssetModels).toHaveBeenCalledTimes(1) + + const fastCompleteHandler = vi + .mocked(api.addCustomEventListener) + .mock.calls.find(([type]) => type === 'assets.seed.fast_complete')?.[1] + expect(fastCompleteHandler).toBeDefined() + await fastCompleteHandler!(new CustomEvent('assets.seed.fast_complete')) + await vi.waitFor(() => { + expect(assetService.getAssetModels).toHaveBeenCalledTimes(2) + }) + + expect(assetService.invalidateModelBuckets).toHaveBeenCalled() + expect(assetService.seedModelAssets).not.toHaveBeenCalled() + }) }) describe('API switching functionality', () => { diff --git a/src/stores/modelStore.ts b/src/stores/modelStore.ts index fc7c2fb785c..58dedbd013f 100644 --- a/src/stores/modelStore.ts +++ b/src/stores/modelStore.ts @@ -271,12 +271,11 @@ export const useModelStore = defineStore('models', () => { } /** - * Refreshes the folder structure and re-loads any folder whose contents - * had previously been loaded. Used by manual refresh actions ("r" key, - * sidebar refresh button) to pick up on-disk changes without losing the - * currently-visible contents. + * Re-fetches the folder structure and re-loads any folder whose contents + * had previously been loaded, picking up server-side changes without + * losing the currently-visible contents. */ - async function refresh() { + async function reloadModels() { assetService.invalidateModelBuckets() const previouslyLoaded = modelFolders.value .filter((folder) => folder.state === ResourceState.Loaded) @@ -289,6 +288,35 @@ export const useModelStore = defineStore('models', () => { ) } + /** + * Asks the backend to rescan the model roots so files added on disk since + * startup become assets. Skipped on Cloud (models are ingested via uploads, + * not scanned from disk) and on the legacy listing path (which reads the + * filesystem live on every request). + */ + function requestModelScan() { + if (isCloud) return + if (!settingStore.get('Comfy.Assets.UseAssetAPI')) return + void assetService.seedModelAssets().catch((error) => { + console.warn('Unable to start model asset scan', error) + }) + } + + /** + * Manual refresh ("r" key, sidebar refresh button): kicks off a backend + * rescan and immediately re-loads the currently known server state. When + * the scan's fast phase completes, `assets.seed.fast_complete` re-loads + * again with whatever the scan discovered. + */ + async function refresh() { + requestModelScan() + await reloadModels() + } + + api.addCustomEventListener('assets.seed.fast_complete', () => { + void reloadModels() + }) + return { models, modelFolders, From 7bfd647b174162320a21a118c27ee115d10791a0 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Fri, 3 Jul 2026 11:19:26 +1200 Subject: [PATCH 28/62] feat(assets): eager-load the model library in asset mode In asset mode every folder resolves from one cached walk, so load all folders on sidebar mount: search and badge counts are complete from the start and the load-all button becomes redundant (hidden; it remains for the request-per-folder legacy path, where AutoLoadAll stays the opt-in). Log when a scan's fast phase discovered new files, since the sidebar now updates itself and the console note is the only trace of why. --- .../tabs/ModelLibrarySidebarTab.test.ts | 50 +++++++++++++++++-- .../sidebar/tabs/ModelLibrarySidebarTab.vue | 12 ++++- src/stores/modelStore.test.ts | 24 +++++++++ src/stores/modelStore.ts | 7 ++- 4 files changed, 86 insertions(+), 7 deletions(-) diff --git a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts index f23283138d5..641cee829ee 100644 --- a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts +++ b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts @@ -18,7 +18,9 @@ const { mockGetNodeProvider, mockToggleNodeOnEvent, mockRefreshModelFolder, - downloadStoreState + mockLoadModels, + downloadStoreState, + settingState } = vi.hoisted(() => { let capturedRoot: TreeExplorerNode | null = null return { @@ -33,7 +35,9 @@ const { mockGetNodeProvider: vi.fn(), mockToggleNodeOnEvent: vi.fn(), mockRefreshModelFolder: vi.fn().mockResolvedValue(undefined), - downloadStoreState: { setLastCompleted: (_: unknown) => {} } + mockLoadModels: vi.fn().mockResolvedValue([]), + downloadStoreState: { setLastCompleted: (_: unknown) => {} }, + settingState: { useAssetAPI: false, autoLoadAll: false } } }) @@ -62,7 +66,7 @@ vi.mock('@/stores/modelStore', () => ({ useModelStore: () => ({ modelFolders: [], models: [mockModel], - loadModels: vi.fn().mockResolvedValue([]), + loadModels: mockLoadModels, loadModelFolders: vi.fn().mockResolvedValue([]), refresh: vi.fn().mockResolvedValue(undefined), refreshModelFolder: mockRefreshModelFolder @@ -92,6 +96,10 @@ vi.mock('@/platform/settings/settingStore', () => ({ useSettingStore: () => ({ get: vi.fn((key: string) => { if (key === 'Comfy.ModelLibrary.NameFormat') return 'filename' + if (key === 'Comfy.Assets.UseAssetAPI') return settingState.useAssetAPI + if (key === 'Comfy.ModelLibrary.AutoLoadAll') { + return settingState.autoLoadAll + } return false }) }) @@ -134,7 +142,8 @@ vi.mock('./SidebarTopArea.vue', () => ({ vi.mock('./SidebarTabTemplate.vue', () => ({ default: { name: 'SidebarTabTemplate', - template: '
' + template: + '
' } })) @@ -157,13 +166,16 @@ describe('ModelLibrarySidebarTab', () => { vi.clearAllMocks() resetRoot() downloadStoreState.setLastCompleted(null) + settingState.useAssetAPI = false + settingState.autoLoadAll = false }) function renderComponent() { return render(ModelLibrarySidebarTab, { global: { plugins: [createTestingPinia({ stubActions: false }), i18n], - stubs: { teleport: true } + stubs: { teleport: true }, + directives: { tooltip: {} } } }) } @@ -236,4 +248,32 @@ describe('ModelLibrarySidebarTab', () => { expect(mockRefreshModelFolder).not.toHaveBeenCalled() }) + + describe('asset mode', () => { + it('hides the load-all button and eager-loads models on mount', async () => { + settingState.useAssetAPI = true + renderComponent() + await nextTick() + + expect(screen.queryByLabelText('g.loadAllFolders')).toBeNull() + expect(screen.getByLabelText('g.refresh')).toBeInTheDocument() + expect(mockLoadModels).toHaveBeenCalledTimes(1) + }) + + it('legacy mode keeps the load-all button and stays lazy by default', async () => { + renderComponent() + await nextTick() + + expect(screen.getByLabelText('g.loadAllFolders')).toBeInTheDocument() + expect(mockLoadModels).not.toHaveBeenCalled() + }) + + it('legacy mode still honors AutoLoadAll', async () => { + settingState.autoLoadAll = true + renderComponent() + await nextTick() + + expect(mockLoadModels).toHaveBeenCalledTimes(1) + }) + }) }) diff --git a/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue b/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue index 7487e854565..223fd0a0832 100644 --- a/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue +++ b/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue @@ -11,6 +11,7 @@ @@ -16,7 +16,7 @@ variant="muted-textonly" size="icon" :aria-label="$t('g.loadAllFolders')" - @click="modelStore.loadModels" + @click="withLoadFailureToast(() => modelStore.loadModels())" > @@ -56,6 +56,7 @@ diff --git a/src/locales/en/main.json b/src/locales/en/main.json index d723465f12d..80ac19b0911 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -975,6 +975,7 @@ "noBookmarkedNodes": "No bookmarks yet" }, "modelLibrary": "Model Library", + "modelLibraryLoadFailed": "Failed to load models. Check the server connection and use Refresh to retry.", "downloads": "Downloads", "queueProgressOverlay": { "title": "Queue Progress", diff --git a/src/platform/assets/schemas/assetSchema.ts b/src/platform/assets/schemas/assetSchema.ts index 17437c49fad..08a121d2261 100644 --- a/src/platform/assets/schemas/assetSchema.ts +++ b/src/platform/assets/schemas/assetSchema.ts @@ -70,13 +70,15 @@ const zAsyncUploadResponse = z.discriminatedUnion('type', [ z.object({ type: z.literal('async'), task: zAsyncUploadTask }) ]) -// Filename validation schema +// Filename validation schema. Trim runs FIRST so the anchored checks see the +// trimmed value — trailing-position trim let ' /etc/passwd' bypass the +// absolute-path block. export const assetFilenameSchema = z .string() + .trim() .min(1, 'Filename cannot be empty') .regex(/^[^\\:*?"<>|]+$/, 'Invalid filename characters') // Allow forward slashes, block backslashes and other unsafe chars .regex(/^(?!\/|.*\.\.)/, 'Path must not start with / or contain ..') // Prevent absolute paths and directory traversal - .trim() // Export schemas following repository patterns export const assetItemSchema = zAsset diff --git a/src/platform/assets/services/assetService.test.ts b/src/platform/assets/services/assetService.test.ts index 7bc5740823a..393d2f1c528 100644 --- a/src/platform/assets/services/assetService.test.ts +++ b/src/platform/assets/services/assetService.test.ts @@ -459,6 +459,33 @@ describe(assetService.getAssetModels, () => { expect(params.get('exclude_tags')).toBe(MISSING_TAG) }) + it('deduplicates concurrent reads into a single in-flight walk', async () => { + let resolveWalk!: (response: Response) => void + fetchApiMock.mockReturnValueOnce( + new Promise((resolve) => { + resolveWalk = resolve + }) + ) + + const checkpointsRead = assetService.getAssetModels('checkpoints') + const lorasRead = assetService.getAssetModels('loras') + + resolveWalk( + buildAssetListResponse([ + validAsset({ id: 'a', tags: ['models', 'model_type:checkpoints'] }), + validAsset({ + id: 'b', + name: 'l.safetensors', + tags: ['models', 'model_type:loras'] + }) + ]) + ) + + expect((await checkpointsRead).length).toBe(1) + expect((await lorasRead).length).toBe(1) + expect(fetchApiMock).toHaveBeenCalledTimes(1) + }) + it('buckets by bare tags when model_type tags are unsupported', async () => { mockSupportsModelTypeTags.value = false fetchApiMock.mockResolvedValueOnce( @@ -545,6 +572,14 @@ describe(assetService.getAssetModels, () => { loader_path: '../../secrets/evil.safetensors', tags: ['models', 'model_type:checkpoints'] }), + validAsset({ + id: 'evil-whitespace', + name: 'evil2.safetensors', + // Leading whitespace must not smuggle an absolute path past the + // anchored validation. + loader_path: ' /etc/passwd.safetensors', + tags: ['models', 'model_type:checkpoints'] + }), validAsset({ id: 'ok', name: 'fine.safetensors', diff --git a/src/stores/modelStore.test.ts b/src/stores/modelStore.test.ts index 4732fe8aec1..09db26002c2 100644 --- a/src/stores/modelStore.test.ts +++ b/src/stores/modelStore.test.ts @@ -8,10 +8,20 @@ import { api } from '@/scripts/api' import { ResourceState, effectiveModelExtensions, + getModelPreviewUrl, matchesModelExtension, useModelStore } from '@/stores/modelStore' +const { isCloudRef } = vi.hoisted(() => ({ isCloudRef: { value: false } })) + +vi.mock('@/platform/distribution/types', async (importOriginal) => ({ + ...(await importOriginal>()), + get isCloud() { + return isCloudRef.value + } +})) + // Mock the api vi.mock('@/scripts/api', () => ({ api: { @@ -99,6 +109,7 @@ describe('useModelStore', () => { beforeEach(async () => { setActivePinia(createTestingPinia({ stubActions: false })) vi.resetAllMocks() + isCloudRef.value = false }) it('should load models', async () => { @@ -265,6 +276,44 @@ describe('useModelStore', () => { }) }) + it('retries the folder load when a concurrent newer load superseded it', async () => { + enableMocks() + store = useModelStore() + + // First structure load is superseded before it resolves; the retry commits. + let resolveSuperseded!: ( + value: { name: string; folders: string[] }[] + ) => void + vi.mocked(api.getModelFolders).mockReturnValueOnce( + new Promise((resolve) => { + resolveSuperseded = resolve + }) + ) + const eagerLoad = store.loadModels() + const supersedingLoad = store.loadModelFolders() + resolveSuperseded([{ name: 'checkpoints', folders: ['/p'] }]) + + await Promise.all([eagerLoad, supersedingLoad]) + + expect(store.models.length).toBeGreaterThan(0) + }) + + it('allows a folder to retry loading after a transient failure', async () => { + enableMocks() + vi.mocked(api.getModels).mockRejectedValueOnce(new Error('transient')) + store = useModelStore() + await store.loadModelFolders() + + const failing = store.modelFolders.find( + (f) => f.directory === 'checkpoints' + )! + await expect(failing.load()).rejects.toThrow('transient') + expect(failing.state).toBe(ResourceState.Uninitialized) + + const folder = await store.getLoadedModelFolder('checkpoints') + expect(Object.keys(folder!.models)).toHaveLength(3) + }) + it('eager-loading before boot loads the folder structure first', async () => { enableMocks() store = useModelStore() @@ -304,6 +353,45 @@ describe('useModelStore', () => { expect(folder!.models['0/older.safetensors']).toBeUndefined() }) + it('discards a refresh that raced a structure rebuild started earlier', async () => { + enableMocks() + store = useModelStore() + await store.loadModelFolders() + await store.getLoadedModelFolder('checkpoints') + + // The rebuild starts (and bumps the request id) BEFORE the refresh + // snapshots it, so an id check alone cannot catch this interleaving. + let resolveRebuild!: ( + value: { name: string; folders: string[] }[] + ) => void + vi.mocked(api.getModelFolders).mockReturnValueOnce( + new Promise((resolve) => { + resolveRebuild = resolve + }) + ) + const rebuild = store.loadModelFolders() + + let resolveContents!: ( + value: { name: string; pathIndex: number }[] + ) => void + vi.mocked(api.getModels).mockReturnValueOnce( + new Promise((resolve) => { + resolveContents = resolve + }) + ) + const refresh = store.refreshModelFolder('checkpoints') + + resolveRebuild([{ name: 'checkpoints', folders: ['/p'] }]) + await rebuild + resolveContents([{ name: 'stale.safetensors', pathIndex: 0 }]) + await refresh + + const entry = store.modelFolders.find( + (f) => f.directory === 'checkpoints' + )! + expect(entry.state).toBe(ResourceState.Uninitialized) + }) + it('does not resurrect a stale folder over a fresher structure', async () => { enableMocks() store = useModelStore() @@ -405,6 +493,44 @@ describe('useModelStore', () => { }) }) + describe('cloud gating', () => { + beforeEach(() => { + isCloudRef.value = true + }) + + it('does not read safetensors metadata from disk on cloud', async () => { + enableMocks(true) + store = useModelStore() + await store.loadModelFolders() + const folderStore = await store.getLoadedModelFolder('checkpoints') + const model = folderStore!.models['0/sdxl.safetensors'] + + await model.load() + + expect(api.viewMetadata).not.toHaveBeenCalled() + expect(model.title).toBe('sdxl') + }) + + it('resolves no preview url on cloud', async () => { + enableMocks(true) + store = useModelStore() + await store.loadModelFolders() + const folderStore = await store.getLoadedModelFolder('checkpoints') + const model = folderStore!.models['0/sdxl.safetensors'] + + expect(getModelPreviewUrl(model)).toBe('') + }) + + it('does not trigger a disk scan on cloud', async () => { + enableMocks(true) + store = useModelStore() + + await store.refresh() + + expect(assetService.seedModelAssets).not.toHaveBeenCalled() + }) + }) + describe('API switching functionality', () => { it('should use experimental API for complete workflow when UseAssetAPI setting is false', async () => { enableMocks(false) // useAssetAPI = false @@ -525,4 +651,16 @@ describe(effectiveModelExtensions, () => { effectiveModelExtensions([]) ) }) + + it('treats a sentinel-only allowlist like match-all', () => { + // The `'folder'`/`''` sentinels are not real extensions; both helpers + // must agree these lists mean match-all so noise stays filtered. + expect(effectiveModelExtensions(['folder'])).toEqual( + effectiveModelExtensions([]) + ) + expect(effectiveModelExtensions([''])).toEqual(effectiveModelExtensions([])) + expect( + matchesModelExtension('readme.md', effectiveModelExtensions(['folder'])) + ).toBe(false) + }) }) diff --git a/src/stores/modelStore.ts b/src/stores/modelStore.ts index a345749b96b..c445ff5242b 100644 --- a/src/stores/modelStore.ts +++ b/src/stores/modelStore.ts @@ -174,7 +174,7 @@ export function getModelPreviewUrl(model: ComfyModelDef): string { const extension = model.file_name.split('.').pop() const filename = model.file_name.replace(`.${extension}`, '.webp') const encodedFilename = encodeURIComponent(filename).replace(/%2F/g, '/') - return `/api/experiment/models/preview/${model.directory}/${model.path_index}/${encodedFilename}` + return `/api/experiment/models/preview/${encodeURIComponent(model.directory)}/${model.path_index}/${encodedFilename}` } /** @@ -194,32 +194,41 @@ const DEFAULT_MODEL_EXTENSIONS = [ '.sft' ] +/** + * Real (`.`-prefixed) entries of a registered allowlist. Non-prefixed + * entries (`'folder'`, `''`) are backend sentinels, not extensions — a list + * containing only sentinels means match-all, the same as an empty list. + */ +function realExtensions(extensions: string[]): string[] { + return extensions.filter((ext) => ext.startsWith('.')) +} + /** * Resolves a folder's display allowlist from its raw registered `extensions` - * (`/experiment/models`): non-empty is used verbatim; an empty array - * (match-all) or an absent field (older backends) takes the FE default list, - * reproducing the legacy sidebar's global-set behavior so nothing that used - * to be hidden starts showing. + * (`/experiment/models`): a list with real extensions is used verbatim; a + * match-all list (empty, sentinel-only, or absent on older backends) takes + * the FE default list, reproducing the legacy sidebar's global-set behavior + * so nothing that used to be hidden starts showing. */ export function effectiveModelExtensions( extensions: string[] | undefined ): string[] { - return extensions?.length ? extensions : DEFAULT_MODEL_EXTENSIONS + if (extensions && realExtensions(extensions).length > 0) return extensions + return DEFAULT_MODEL_EXTENSIONS } /** - * Whether a model file belongs in a folder given its display allowlist. An - * empty list, or a list with no real (`.`-prefixed) extensions (the - * `'folder'`/`''` sentinels), leaves the folder unfiltered. + * Whether a model file belongs in a folder given its display allowlist. A + * list with no real extensions leaves the folder unfiltered. */ export function matchesModelExtension( fileName: string, extensions: string[] ): boolean { - const realExtensions = extensions.filter((ext) => ext.startsWith('.')) - if (realExtensions.length === 0) return true + const real = realExtensions(extensions) + if (real.length === 0) return true const lower = fileName.toLowerCase() - return realExtensions.some((ext) => lower.endsWith(ext.toLowerCase())) + return real.some((ext) => lower.endsWith(ext.toLowerCase())) } export class ModelFolder { @@ -245,17 +254,24 @@ export class ModelFolder { return this } this.state = ResourceState.Loading - const models = await this.getModelsFunc(this.directory) - for (const model of models) { - if (!matchesModelExtension(model.name, this.extensions)) continue - this.models[`${model.pathIndex}/${model.name}`] = new ComfyModelDef( - model.name, - this.directory, - model.pathIndex - ) + try { + const models = await this.getModelsFunc(this.directory) + for (const model of models) { + if (!matchesModelExtension(model.name, this.extensions)) continue + this.models[`${model.pathIndex}/${model.name}`] = new ComfyModelDef( + model.name, + this.directory, + model.pathIndex + ) + } + this.state = ResourceState.Loaded + return this + } catch (error) { + // Reset instead of sticking at Loading so the guard above does not + // permanently block a retry after a transient failure. + this.state = ResourceState.Uninitialized + throw error } - this.state = ResourceState.Loaded - return this } } @@ -273,9 +289,17 @@ export const useModelStore = defineStore('models', () => { modelFolders.value.flatMap((folder) => Object.values(folder.models)) ) + /** + * Whether model contents come from the asset API. Named to avoid confusion + * with assetService.isAssetAPIEnabled(), which is cloud-gated and governs + * the asset browser surfaces, not this store's data source. + */ + function usesAssetApi(): boolean { + return !!settingStore.get('Comfy.Assets.UseAssetAPI') + } + function createGetModelsFunc(): (folder: string) => Promise { - const useAssetAPI: boolean = settingStore.get('Comfy.Assets.UseAssetAPI') - return useAssetAPI + return usesAssetApi() ? (folder) => assetService.getAssetModels(folder) : (folder) => api.getModels(folder) } @@ -292,13 +316,12 @@ export const useModelStore = defineStore('models', () => { * only the newest request so a slow stale response cannot overwrite a * fresher folder structure. */ - async function loadModelFolders() { + async function loadModelFolders(): Promise { const requestId = ++modelFoldersRequestId const resData = await api.getModelFolders() - if (requestId !== modelFoldersRequestId) return + if (requestId !== modelFoldersRequestId) return false modelFolderNames.value = resData.map((folder) => folder.name) modelFolderByName.value = {} - const useAssetAPI: boolean = settingStore.get('Comfy.Assets.UseAssetAPI') const getModelsFunc = createGetModelsFunc() for (const folder of resData) { modelFolderByName.value[folder.name] = new ModelFolder( @@ -306,9 +329,10 @@ export const useModelStore = defineStore('models', () => { getModelsFunc, // Display filtering applies to the asset walk only; the legacy // listing keeps its historical server-side (global-set) filtering. - useAssetAPI ? effectiveModelExtensions(folder.extensions) : [] + usesAssetApi() ? effectiveModelExtensions(folder.extensions) : [] ) } + return true } async function getLoadedModelFolder( @@ -325,8 +349,13 @@ export const useModelStore = defineStore('models', () => { * empty folder list would silently load nothing. */ async function loadModels() { - if (modelFolderNames.value.length === 0) { - await loadModelFolders() + // A load superseded by a newer concurrent one commits nothing, which + // would leave the folder list empty and silently load no models; retry + // until a load of ours commits (even a genuinely empty result) or a + // concurrent one has populated the list. Bounded as a safety net. + for (let attempt = 0; attempt < 3; attempt++) { + if (modelFolderNames.value.length > 0) break + if (await loadModelFolders()) break } return Promise.all(modelFolders.value.map((folder) => folder.load())) } @@ -349,17 +378,21 @@ export const useModelStore = defineStore('models', () => { const requestId = modelFoldersRequestId const refreshId = (folderRefreshIds.get(folderName) ?? 0) + 1 folderRefreshIds.set(folderName, refreshId) + const priorFolder = modelFolderByName.value[folderName] const folder = new ModelFolder( folderName, createGetModelsFunc(), - modelFolderByName.value[folderName].extensions + priorFolder.extensions ) await folder.load() // A full reload may have rebuilt the folder structure while this folder // refreshed, and a newer refresh of the same folder may have already - // committed; committing then would resurrect a stale folder object. + // committed; committing then would resurrect a stale folder object. The + // identity check also covers a rebuild whose folder load started before + // this refresh snapshotted the request id. if (requestId !== modelFoldersRequestId) return if (folderRefreshIds.get(folderName) !== refreshId) return + if (modelFolderByName.value[folderName] !== priorFolder) return modelFolderByName.value[folderName] = folder } @@ -394,7 +427,7 @@ export const useModelStore = defineStore('models', () => { */ async function requestModelScan() { if (isCloud) return - if (!settingStore.get('Comfy.Assets.UseAssetAPI')) return + if (!usesAssetApi()) return try { await assetService.seedModelAssets() } catch (error) { From ba7ee69a1a19f89a3338567b3b6a5d17edb628fb Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 21 Jul 2026 06:51:02 +1200 Subject: [PATCH 45/62] feat(assets): hide folders with no models in asset mode The registry lists every registered folder type, most of which are empty on a given install; asset mode now hides folders that loaded empty (they reappear live when a scan discovers a first model). The legacy path keeps its historical all-registered-folders view. --- .../tabs/ModelLibrarySidebarTab.test.ts | 1 + .../sidebar/tabs/ModelLibrarySidebarTab.vue | 2 +- src/stores/modelStore.test.ts | 29 +++++++++++++++++++ src/stores/modelStore.ts | 17 +++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts index e90480ee55d..a11b002f058 100644 --- a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts +++ b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts @@ -85,6 +85,7 @@ vi.mock('@/stores/modelStore', async () => { }, useModelStore: () => ({ modelFolders: [], + visibleModelFolders: [], models, loadModels: mockLoadModels, loadModelFolders: vi.fn().mockResolvedValue([]), diff --git a/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue b/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue index 106fae9086f..90fe1e28bac 100644 --- a/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue +++ b/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue @@ -111,7 +111,7 @@ type ModelOrFolder = ComfyModelDef | ModelFolder const root = computed(() => { const allNodes: ModelOrFolder[] = searchQuery.value ? filteredModels.value - : [...modelStore.modelFolders, ...modelStore.models] + : [...modelStore.visibleModelFolders, ...modelStore.models] return buildTree(allNodes, (modelOrFolder: ModelOrFolder) => modelOrFolder.key.split('/') ) diff --git a/src/stores/modelStore.test.ts b/src/stores/modelStore.test.ts index 09db26002c2..40d691224d0 100644 --- a/src/stores/modelStore.test.ts +++ b/src/stores/modelStore.test.ts @@ -493,6 +493,35 @@ describe('useModelStore', () => { }) }) + describe('visibleModelFolders', () => { + it('hides folders that loaded empty in asset mode', async () => { + enableMocks(true) + vi.mocked(assetService.getAssetModels).mockImplementation( + async (folder: string) => + folder === 'checkpoints' + ? [{ name: 'sdxl.safetensors', pathIndex: 0 }] + : [] + ) + store = useModelStore() + await store.loadModels() + + expect(store.visibleModelFolders.map((f) => f.directory)).toEqual([ + 'checkpoints' + ]) + // The full folder list is untouched for consumers that need it. + expect(store.modelFolders).toHaveLength(2) + }) + + it('keeps all registered folders visible on the legacy path', async () => { + enableMocks(false) + vi.mocked(api.getModels).mockResolvedValue([]) + store = useModelStore() + await store.loadModels() + + expect(store.visibleModelFolders).toHaveLength(2) + }) + }) + describe('cloud gating', () => { beforeEach(() => { isCloudRef.value = true diff --git a/src/stores/modelStore.ts b/src/stores/modelStore.ts index c445ff5242b..45bfb795193 100644 --- a/src/stores/modelStore.ts +++ b/src/stores/modelStore.ts @@ -288,6 +288,22 @@ export const useModelStore = defineStore('models', () => { const models = computed(() => modelFolders.value.flatMap((folder) => Object.values(folder.models)) ) + /** + * Folders worth showing in the sidebar. Asset mode hides folders that + * loaded empty — the registry lists every registered folder type, most of + * which have no models on a given install; they reappear live when a scan + * discovers a first model. The legacy path keeps its historical + * all-registered-folders view. + */ + const visibleModelFolders = computed(() => + usesAssetApi() + ? modelFolders.value.filter( + (folder) => + folder.state !== ResourceState.Loaded || + Object.keys(folder.models).length > 0 + ) + : modelFolders.value + ) /** * Whether model contents come from the asset API. Named to avoid confusion @@ -459,6 +475,7 @@ export const useModelStore = defineStore('models', () => { return { models, modelFolders, + visibleModelFolders, loadModelFolders, loadModels, getLoadedModelFolder, From 401e7810433ef965b3cd1044a5b299702a61cce5 Mon Sep 17 00:00:00 2001 From: Simon Pinfold Date: Tue, 21 Jul 2026 09:28:03 +1200 Subject: [PATCH 46/62] fix(assets): bound search rendering to keep large libraries responsive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Measured on a production build (mocked 40-folder library): sidebar open is fine at scale (1.2s median at 10k models), but search rendered every match expanded and un-virtualized while the tree component rescans all rows per mounted row (O(n^2)) — 438ms per first keystroke at 1k models, 1.8s at 5k, an effective hang at 10k, and a rescan landing mid-search cost 99s at 5k. Cap rendered search results (first 500, with a refine-your-search hint) and expand results only when the query changes, so a background reload cannot re-expand folders the user collapsed nor pay a full expand-and-mount pass per folder commit. Virtualization remains the tracked follow-up. --- .../tabs/ModelLibrarySidebarTab.test.ts | 64 ++++++++++++++++++- .../sidebar/tabs/ModelLibrarySidebarTab.vue | 47 +++++++++++--- src/locales/en/main.json | 1 + 3 files changed, 102 insertions(+), 10 deletions(-) diff --git a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts index a11b002f058..9f6cf871b6e 100644 --- a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts +++ b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts @@ -127,9 +127,10 @@ vi.mock('@/platform/settings/settingStore', () => ({ }) })) +const mockExpandNode = vi.hoisted(() => vi.fn()) vi.mock('@/composables/useTreeExpansion', () => ({ useTreeExpansion: () => ({ - expandNode: vi.fn(), + expandNode: mockExpandNode, toggleNodeOnEvent: mockToggleNodeOnEvent }) })) @@ -326,6 +327,67 @@ describe('ModelLibrarySidebarTab', () => { }) }) + describe('search scale limits', () => { + it('caps rendered search results and shows the hint', async () => { + const user = userEvent.setup() + renderComponent() + await nextTick() + for (let i = 0; i < 520; i++) { + modelsState.push( + fromPartial({ + key: `checkpoints/bulk-${i}.safetensors`, + file_name: `bulk-${i}.safetensors`, + simplified_file_name: `bulk-${i}`, + title: `bulk-${i}`, + directory: 'checkpoints', + searchable: `checkpoints/bulk-${i}.safetensors` + }) + ) + } + + await user.type(screen.getByTestId('search-input'), 'bulk') + await nextTick() + + const { children: folders = [] } = getRoot() + const leafCount = folders.flatMap( + ({ children: leaves = [] }) => leaves + ).length + expect(leafCount).toBe(500) + expect( + screen.getByText('sideToolbar.searchResultsCapped') + ).toBeInTheDocument() + }) + + it('does not re-expand results when a reload lands mid-search', async () => { + const user = userEvent.setup() + renderComponent() + await nextTick() + + await user.type(screen.getByTestId('search-input'), 'model') + await nextTick() + await nextTick() + const expandCallsAfterTyping = mockExpandNode.mock.calls.length + expect(expandCallsAfterTyping).toBeGreaterThan(0) + + // A background reload adds another matching model; the tree data + // updates but no new expand pass runs (preserves manual collapses). + modelsState.push( + fromPartial({ + key: 'checkpoints/model-late.safetensors', + file_name: 'model-late.safetensors', + simplified_file_name: 'model-late', + title: 'Model Late', + directory: 'checkpoints', + searchable: 'checkpoints/model-late.safetensors' + }) + ) + await nextTick() + await nextTick() + + expect(mockExpandNode.mock.calls.length).toBe(expandCallsAfterTyping) + }) + }) + describe('asset mode', () => { it('surfaces an error toast when the eager load fails on mount', async () => { const error = vi.spyOn(console, 'error').mockImplementation(() => {}) diff --git a/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue b/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue index 90fe1e28bac..0553ccea77c 100644 --- a/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue +++ b/src/components/sidebar/tabs/ModelLibrarySidebarTab.vue @@ -33,6 +33,13 @@ " @search="handleSearch" /> +

+ {{ + $t('sideToolbar.searchResultsCapped', { + limit: SEARCH_RESULT_LIMIT + }) + }} +