Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(webv2): add model taxonomy helpers
joshistoast4540593e5334
diff --git a/invokeai/frontend/webv2/src/workbench/models/baseIdentity.test.ts b/invokeai/frontend/webv2/src/workbench/models/baseIdentity.test.tsnew file mode 100644index 0000000000..b603d8bdba--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/models/baseIdentity.test.ts@@ -0,0 +1,86 @@+import { describe, expect, it } from 'vitest';++import type { ModelBase, ModelConfig } from './types';++import {+ getModelBaseColorPalette,+ getModelBaseInfo,+ getModelBaseLabel,+ isConvertibleToDiffusers,+ KNOWN_MODEL_BASES,+ MODEL_BASES,+} from './baseIdentity';++const createModel = (overrides: Partial<ModelConfig>): ModelConfig => ({+ base: 'sdxl',+ file_size: 1024,+ format: 'checkpoint',+ hash: 'hash',+ key: 'key',+ name: 'Model',+ path: '/models/model.safetensors',+ source: '/models/model.safetensors',+ source_type: 'path',+ type: 'main',+ ...overrides,+});++describe('MODEL_BASES', () => {+ it('matches known base labels, colors, and conversion support', () => {+ expect(getModelBaseInfo('sd-1')).toMatchObject({+ label: 'SD 1.x',+ colorPalette: 'green',+ supportsDiffusersConversion: true,+ });+ expect(getModelBaseInfo('sd-2')).toMatchObject({+ label: 'SD 2.x',+ colorPalette: 'teal',+ supportsDiffusersConversion: true,+ });+ expect(getModelBaseInfo('sdxl')).toMatchObject({+ label: 'SDXL',+ colorPalette: 'blue',+ supportsDiffusersConversion: true,+ });+ expect(getModelBaseInfo('flux2')).toMatchObject({ label: 'FLUX.2', colorPalette: 'orange' });+ expect(getModelBaseInfo('qwen-image')).toMatchObject({ label: 'Qwen Image', colorPalette: 'cyan' });+ expect(isConvertibleToDiffusers(createModel({ base: 'sdxl', format: 'checkpoint', type: 'main' }))).toBe(true);+ expect(isConvertibleToDiffusers(createModel({ base: 'flux', format: 'checkpoint', type: 'main' }))).toBe(false);+ });++ it('falls back safely for unknown bases', () => {+ const base = 'made-up' as ModelBase;++ expect(getModelBaseInfo(base)).toEqual({ base, label: 'Made Up', colorPalette: 'gray' });+ expect(getModelBaseLabel(base)).toBe('Made Up');+ expect(getModelBaseColorPalette(base)).toBe('gray');+ });++ it('does not include generation fields', () => {+ for (const info of Object.values(MODEL_BASES)) {+ expect(info).not.toHaveProperty('defaults');+ expect(info).not.toHaveProperty('schedulerSet');+ expect(info).not.toHaveProperty('negativePrompt');+ expect(info).not.toHaveProperty('ui');+ }+ });++ it('covers all known ModelBase literals', () => {+ expect(KNOWN_MODEL_BASES).toEqual([+ 'sd-1',+ 'sd-2',+ 'sdxl',+ 'sdxl-refiner',+ 'sd-3',+ 'flux',+ 'flux2',+ 'cogview4',+ 'qwen-image',+ 'z-image',+ 'anima',+ 'any',+ 'external',+ 'unknown',+ ]);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/models/baseIdentity.ts b/invokeai/frontend/webv2/src/workbench/models/baseIdentity.tsnew file mode 100644index 0000000000..a212d8e695--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/models/baseIdentity.ts@@ -0,0 +1,114 @@+import type { ModelBase, ModelConfig } from './types';++// Model-base identity registry: labels, colors, and scalar display facts only.+// Generation behavior keyed by base lives in @workbench/generation/baseGenerationPolicies.++export interface ModelBaseInfo {+ base: ModelBase;+ label: string;+ colorPalette: string;+ description?: string;+ supportsDiffusersConversion?: boolean;+}++export const MODEL_BASES = {+ 'sd-1': {+ base: 'sd-1',+ label: 'SD 1.x',+ colorPalette: 'green',+ supportsDiffusersConversion: true,+ },+ 'sd-2': {+ base: 'sd-2',+ label: 'SD 2.x',+ colorPalette: 'teal',+ supportsDiffusersConversion: true,+ },+ sdxl: {+ base: 'sdxl',+ label: 'SDXL',+ colorPalette: 'blue',+ supportsDiffusersConversion: true,+ },+ 'sdxl-refiner': {+ base: 'sdxl-refiner',+ label: 'SDXL Refiner',+ colorPalette: 'blue',+ },+ 'sd-3': {+ base: 'sd-3',+ label: 'SD 3.x',+ colorPalette: 'purple',+ },+ flux: {+ base: 'flux',+ label: 'FLUX',+ colorPalette: 'yellow',+ },+ flux2: {+ base: 'flux2',+ label: 'FLUX.2',+ colorPalette: 'orange',+ },+ cogview4: {+ base: 'cogview4',+ label: 'CogView4',+ colorPalette: 'red',+ },+ 'qwen-image': {+ base: 'qwen-image',+ label: 'Qwen Image',+ colorPalette: 'cyan',+ },+ 'z-image': {+ base: 'z-image',+ label: 'Z-Image',+ colorPalette: 'pink',+ },+ anima: {+ base: 'anima',+ label: 'Anima',+ colorPalette: 'pink',+ },+ any: {+ base: 'any',+ label: 'Any',+ colorPalette: 'gray',+ },+ external: {+ base: 'external',+ label: 'External',+ colorPalette: 'gray',+ },+ unknown: {+ base: 'unknown',+ label: 'Unknown',+ colorPalette: 'gray',+ },+} satisfies Record<string, ModelBaseInfo>;++export type KnownModelBase = keyof typeof MODEL_BASES;++export const KNOWN_MODEL_BASES = Object.keys(MODEL_BASES) as KnownModelBase[];++const toTitleCase = (value: string): string =>+ value.replaceAll(/[_-]+/g, ' ').replace(/\w\S*/g, (word) => word[0].toUpperCase() + word.slice(1));++// Unknown bases are display-safe here, but generation support is decided in baseGenerationPolicies.ts.+export const getModelBaseInfo = (base: ModelBase): ModelBaseInfo =>+ (MODEL_BASES as Record<string, ModelBaseInfo>)[base] ?? {+ base,+ label: toTitleCase(base),+ colorPalette: 'gray',+ };++export const getModelBaseLabel = (base: ModelBase): string => getModelBaseInfo(base).label;++export const getModelBaseColorPalette = (base: ModelBase): string => getModelBaseInfo(base).colorPalette;++export const isKnownModelBase = (base: ModelBase): base is KnownModelBase => base in MODEL_BASES;++export const isConvertibleToDiffusers = (model: ModelConfig): boolean =>+ model.format === 'checkpoint' &&+ model.type === 'main' &&+ Boolean(getModelBaseInfo(model.base).supportsDiffusersConversion);diff --git a/invokeai/frontend/webv2/src/workbench/models/library.test.ts b/invokeai/frontend/webv2/src/workbench/models/library.test.tsindex 01cf99dac9..d40e69ee5f 100644--- a/invokeai/frontend/webv2/src/workbench/models/library.test.ts+++ b/invokeai/frontend/webv2/src/workbench/models/library.test.ts@@ -8,6 +8,7 @@ import {DEFAULT_LIBRARY_FILTERS,filterModels,flattenGroupsToRows,+ getModelPickerGroups,groupModelsByType,} from './library';@@ -92,6 +93,34 @@ describe('groupModelsByType', () => {});});+describe('getModelPickerGroups', () => {+ it('filters by allowed type, exclusions, custom predicate, and multi-term search', () => {+ const result = getModelPickerGroups(library, {+ excludeKeys: new Set(['b']),+ filter: (model) => model.base !== 'any',+ modelTypes: ['main', 'lora', 'vae'],+ searchTerm: 'sd detail',+ });++ expect(result.candidates.map((m) => m.key)).toEqual(['a', 'c']);+ expect(result.groups.map((group) => group.type)).toEqual(['lora']);+ expect(result.groups[0]?.models.map((m) => m.key)).toEqual(['c']);+ });++ it('sorts picker results by taxonomy rank then name', () => {+ const result = getModelPickerGroups(library, {+ modelTypes: ['main', 'lora', 'vae'],+ searchTerm: '',+ });++ expect(result.groups.map((group) => [group.type, group.models.map((m) => m.key)])).toEqual([+ ['main', ['b', 'a']],+ ['lora', ['c']],+ ['vae', ['d']],+ ]);+ });+});+describe('collect helpers', () => {it('collects distinct bases alphabetically and types in category order', () => {expect(collectBases(library)).toEqual(['any', 'flux', 'sd-1', 'sdxl']);diff --git a/invokeai/frontend/webv2/src/workbench/models/library.ts b/invokeai/frontend/webv2/src/workbench/models/library.tsindex 02c74be9d3..c01255e66f 100644--- a/invokeai/frontend/webv2/src/workbench/models/library.ts+++ b/invokeai/frontend/webv2/src/workbench/models/library.ts@@ -1,6 +1,7 @@import type { ModelConfig, ModelTaxonomyType } from './types';-import { getModelBaseLabel, getModelCategoryRank, getModelTypePluralLabel } from './taxonomy';+import { getModelBaseLabel } from './baseIdentity';+import { getModelCategoryRank, getModelTypePluralLabel } from './taxonomy';/*** Pure filtering/grouping/sorting for the model library. Kept free of React so@@ -36,6 +37,21 @@ export interface ModelGroup {models: ModelConfig[];}+export interface ModelPickerOptions {+ /** Hide specific models, e.g. the current model or already-linked choices. */+ excludeKeys?: ReadonlySet<string>;+ /** Extra predicate supplied by the owning form, e.g. base compatibility. */+ filter?: (model: ModelConfig) => boolean;+ modelTypes: ModelTaxonomyType[];+ searchTerm: string;+}++export interface ModelPickerResult {+ /** Models available before text search, used for empty-state copy. */+ candidates: ModelConfig[];+ groups: ModelGroup[];+}+const matchesSearch = (model: ModelConfig, searchTerm: string): boolean => {if (!searchTerm) {return true;@@ -102,6 +118,38 @@ export const groupModelsByType = (models: ModelConfig[]): ModelGroup[] => {.map(([type, groupModels]) => ({ label: getModelTypePluralLabel(type), models: groupModels, type }));};+const matchesPickerSearch = (model: ModelConfig, searchTerm: string): boolean => {+ const terms = searchTerm.trim().toLowerCase().split(/\s+/).filter(Boolean);++ if (terms.length === 0) {+ return true;+ }++ const haystack = `${model.name} ${model.base} ${model.type}`.toLowerCase();++ return terms.every((term) => haystack.includes(term));+};++/** Candidate, search, sort, and grouping rules shared by every model picker instance. */+export const getModelPickerGroups = (models: ModelConfig[], options: ModelPickerOptions): ModelPickerResult => {+ const allowedTypes = new Set(options.modelTypes);+ const candidates = models.filter(+ (model) =>+ allowedTypes.has(model.type) &&+ !options.excludeKeys?.has(model.key) &&+ (options.filter ? options.filter(model) : true)+ );+ const visibleModels = candidates+ .filter((model) => matchesPickerSearch(model, options.searchTerm))+ .sort(+ (a, b) =>+ getModelCategoryRank(a.type) - getModelCategoryRank(b.type) ||+ a.name.localeCompare(b.name, undefined, { sensitivity: 'base' })+ );++ return { candidates, groups: groupModelsByType(visibleModels) };+};+/** Distinct bases present in a model list, for base filter menus. */export const collectBases = (models: Pick<ModelConfig, 'base'>[]): string[] =>[...new Set(models.map((model) => String(model.base)))].sort();diff --git a/invokeai/frontend/webv2/src/workbench/models/modelsStore.ts b/invokeai/frontend/webv2/src/workbench/models/modelsStore.tsindex 0598265c56..e121022eaf 100644--- a/invokeai/frontend/webv2/src/workbench/models/modelsStore.ts+++ b/invokeai/frontend/webv2/src/workbench/models/modelsStore.ts@@ -83,6 +83,8 @@ export const ensureModelsLoaded = (): void => {}};+export const getModelsSnapshot = (): ModelsSnapshot => store.getSnapshot();+/** Patch one model in place after a successful update/convert. */export const replaceModelInStore = (model: ModelConfig): void => {store.patchSnapshot({diff --git a/invokeai/frontend/webv2/src/workbench/models/taxonomy.ts b/invokeai/frontend/webv2/src/workbench/models/taxonomy.tsindex cf6692fcf9..82c337b4c3 100644--- a/invokeai/frontend/webv2/src/workbench/models/taxonomy.ts+++ b/invokeai/frontend/webv2/src/workbench/models/taxonomy.ts@@ -1,4 +1,4 @@-import type { ModelBase, ModelConfig, ModelFileFormat, ModelInstallJob, ModelTaxonomyType } from './types';+import type { ModelFileFormat, ModelInstallJob, ModelTaxonomyType } from './types';/*** Display metadata for the model taxonomy. Open-union friendly: unknown bases,@@ -55,45 +55,6 @@ export const getModelCategoryRank = (type: ModelTaxonomyType): number => {return index === -1 ? MODEL_CATEGORIES.length : index;};-const BASE_LABELS: Record<string, string> = {- anima: 'Anima',- any: 'Any',- cogview4: 'CogView4',- external: 'External',- flux: 'FLUX',- flux2: 'FLUX.2',- 'qwen-image': 'Qwen Image',- 'sd-1': 'SD 1.x',- 'sd-2': 'SD 2.x',- 'sd-3': 'SD 3.x',- sdxl: 'SDXL',- 'sdxl-refiner': 'SDXL Refiner',- unknown: 'Unknown',- 'z-image': 'Z-Image',-};--export const getModelBaseLabel = (base: ModelBase): string => BASE_LABELS[base] ?? toTitleCase(base);--/** Chakra color palette per base, so architecture is scannable at a glance. */-const BASE_COLOR_PALETTES: Record<string, string> = {- anima: 'pink',- any: 'gray',- cogview4: 'red',- external: 'gray',- flux: 'yellow',- flux2: 'orange',- 'qwen-image': 'cyan',- 'sd-1': 'green',- 'sd-2': 'teal',- 'sd-3': 'purple',- sdxl: 'blue',- 'sdxl-refiner': 'blue',- unknown: 'gray',- 'z-image': 'pink',-};--export const getModelBaseColorPalette = (base: ModelBase): string => BASE_COLOR_PALETTES[base] ?? 'gray';-const FORMAT_LABELS: Record<string, string> = {bnb_quantized_int8b: 'BnB int8',bnb_quantized_nf4b: 'BnB nf4',@@ -116,10 +77,6 @@ const FORMAT_LABELS: Record<string, string> = {export const getModelFormatLabel = (format: ModelFileFormat): string => FORMAT_LABELS[format] ?? toTitleCase(format);-/** Checkpoint→diffusers conversion is only supported for these bases. */-export const isConvertibleToDiffusers = (model: ModelConfig): boolean =>- model.format === 'checkpoint' && model.type === 'main' && ['sd-1', 'sd-2', 'sdxl'].includes(model.base);-const BYTE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB'] as const;export const formatBytes = (bytes: number | null | undefined): string => {diff --git a/invokeai/frontend/webv2/src/workbench/models/types.ts b/invokeai/frontend/webv2/src/workbench/models/types.tsindex edfef733b9..ab8ca1231f 100644--- a/invokeai/frontend/webv2/src/workbench/models/types.ts+++ b/invokeai/frontend/webv2/src/workbench/models/types.ts@@ -71,7 +71,7 @@ export type ModelFileFormat =export type PredictionType = 'epsilon' | 'v_prediction' | 'sample';-/** Per-model defaults applied when the model is selected for generation. */+/** Per-model defaults that Generate can apply explicitly. */export interface MainModelDefaultSettings {vae?: string | null;vae_precision?: 'fp16' | 'fp32' | null;