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(models): link installed Add-models rows to their model page
Josh Corbett3a41eed120c8
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.jsonindex 7388b19e2f..77a490fbd3 100644--- a/invokeai/frontend/webv2/public/locales/en.json+++ b/invokeai/frontend/webv2/public/locales/en.json@@ -1278,6 +1278,7 @@"useSupportedImage": "Use a PNG, JPEG, or WebP image.","variant": "Variant","variantHelp": "Architecture variant, e.g. inpaint or dev.",+ "viewModel": "View model","viewQueue": "View queue","waitingForDownload": "Waiting for download"},diff --git a/invokeai/frontend/webv2/src/features/models/ui/add-models/AddModelsView.browser.test.tsx b/invokeai/frontend/webv2/src/features/models/ui/add-models/AddModelsView.browser.test.tsxindex dcb31dcef6..4bd95bbb1b 100644--- a/invokeai/frontend/webv2/src/features/models/ui/add-models/AddModelsView.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/add-models/AddModelsView.browser.test.tsx@@ -1,4 +1,8 @@+import type { ModelConfig } from '@features/models/core/types';+import { ChakraProvider } from '@chakra-ui/react';+import { setModelsSnapshotForTests } from '@features/models/data/modelsStore';+import { getModelsUiSnapshotForTests, updateModelsUi } from '@features/models/ui/uiStore';import { system } from '@theme/system';import { act, StrictMode } from 'react';import { createRoot, type Root } from 'react-dom/client';@@ -16,11 +20,33 @@ import { AddModelsView } from './AddModelsView';vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));+// One starter the backend marks installed by source, one it marks installed by+// name; the library model behind each is what the row must link to.+const INSTALLED_STARTERS = [+ {+ base: 'sdxl',+ description: 'Pulled from its recorded source.',+ is_installed: true,+ name: 'Juggernaut XL',+ source: 'https://example/juggernaut-xl.safetensors',+ type: 'main',+ },+ {+ base: 'sdxl',+ description: 'Renamed after install.',+ is_installed: true,+ name: 'Dreamshaper XL',+ previous_names: ['Dreamshaper'],+ source: 'https://example/dreamshaper-xl.safetensors',+ type: 'main',+ },+];+vi.mock('@features/models/data/startersStore', async (importOriginal) => ({...(await importOriginal<Record<string, unknown>>()),ensureStartersLoaded: vi.fn(),useStartersSelector: (selector: (snapshot: unknown) => unknown) =>- selector({ error: null, response: { starter_bundles: {}, starter_models: [] }, status: 'loaded' }),+ selector({ error: null, response: { starter_bundles: {}, starter_models: INSTALLED_STARTERS }, status: 'loaded' }),}));vi.mock('@features/models/data/externalProvidersStore', async (importOriginal) => ({@@ -116,4 +142,42 @@ describe('AddModelsView search seed', () => {await unmount();});++ it('links each installed starter to the library model it became', async () => {+ setModelsSnapshotForTests({+ models: [+ {+ base: 'sdxl',+ key: 'by-source',+ name: 'Juggernaut XL',+ path: 'main/juggernaut-xl.safetensors',+ source: INSTALLED_STARTERS[0]!.source,+ type: 'main',+ },+ {+ base: 'sdxl',+ key: 'by-name',+ name: 'Dreamshaper',+ path: 'main/dreamshaper.safetensors',+ source: 'https://elsewhere/dreamshaper',+ type: 'main',+ },+ ] as ModelConfig[],+ status: 'loaded',+ });+ await mount();++ const links = [...document.querySelectorAll('button')].filter(+ (button) => button.textContent === 'models.viewModel'+ );+ expect(links).toHaveLength(2);+ for (const [index, key] of ['by-source', 'by-name'].entries()) {+ updateModelsUi({ activeModelKey: null, activeTab: 'add' });+ await act(() => links[index]!.click());+ expect(getModelsUiSnapshotForTests()).toMatchObject({ activeModelKey: key, activeTab: 'details' });+ }++ await unmount();+ setModelsSnapshotForTests({ models: [], status: 'loaded' });+ });});diff --git a/invokeai/frontend/webv2/src/features/models/ui/add-models/HuggingFaceFiles.tsx b/invokeai/frontend/webv2/src/features/models/ui/add-models/HuggingFaceFiles.tsxindex f6a00c7de3..5d0f3d83dd 100644--- a/invokeai/frontend/webv2/src/features/models/ui/add-models/HuggingFaceFiles.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/add-models/HuggingFaceFiles.tsx@@ -4,7 +4,7 @@ import type { HFLookupState } from '@features/models/ui/uiStore';import { Stack } from '@chakra-ui/react';import { ResultsListHeader } from '@features/models/ui/shared/ResultsListHeader';import { InstallSourceButton, SourceListItem } from '@features/models/ui/shared/SourceListItem';-import { useInstalledSources } from '@features/models/ui/shared/useInstalledSources';+import { useInstalledSourceKeys } from '@features/models/ui/shared/useInstalledSources';import { sourceFileName, useSourceNameFilter } from '@features/models/ui/shared/useSourceNameFilter';import { useTranslation } from 'react-i18next';@@ -28,7 +28,7 @@ export const HuggingFaceFiles = ({const { filter, filteredItems: filteredUrls, setFilter } = useSourceNameFilter(lookup.urls, urlOf);// A model's recorded install source is the URL it was pulled from, so this// marks rows Installed live once the library refresh lands.- const installedSources = useInstalledSources();+ const installedSourceKeys = useInstalledSourceKeys();const installAll = () => {onInstallAll([...filteredUrls]);@@ -53,7 +53,7 @@ export const HuggingFaceFiles = ({titleTooltip={url}trailing={<InstallSourceButton- isInstalled={installedSources.has(url)}+ installedModelKey={installedSourceKeys.get(url) ?? null}isPending={pendingSources.has(url)}source={url}onInstall={() => onInstall(url)}diff --git a/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.browser.test.tsx b/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.browser.test.tsxindex deee6a3284..0425877ed9 100644--- a/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.browser.test.tsx@@ -3,6 +3,7 @@ import type { ModelConfig, ModelInstallJob } from '@features/models/core/types';import { ChakraProvider } from '@chakra-ui/react';import { addInstallJob, replaceInstallJob } from '@features/models/data/installsStore';import { setModelsSnapshotForTests } from '@features/models/data/modelsStore';+import { getModelsUiSnapshotForTests, updateModelsUi } from '@features/models/ui/uiStore';import { accountLifecycle } from '@platform/state/accountLifecycle';import { system } from '@theme/system';import { act } from 'react';@@ -100,5 +101,12 @@ describe('ScanResults install badge lifecycle', () => {expect(host.textContent).not.toContain('models.installing');expect(host.textContent).toContain('models.installed');++ // The installed row links to the model it became.+ updateModelsUi({ activeModelKey: null, activeTab: 'add' });+ const viewModel = [...host.querySelectorAll('button')].find((button) => button.textContent === 'models.viewModel');+ expect(viewModel).toBeDefined();+ await act(() => viewModel!.click());+ expect(getModelsUiSnapshotForTests()).toMatchObject({ activeModelKey: 'installed-key', activeTab: 'details' });});});diff --git a/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.tsx b/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.tsxindex 289170ecaa..51fe6f22f5 100644--- a/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.tsx@@ -4,7 +4,7 @@ import type { FoundModel } from '@features/models/core/types';import { Checkbox, HStack, Icon, Stack, Text } from '@chakra-ui/react';import { ResultsListHeader } from '@features/models/ui/shared/ResultsListHeader';import { InstallSourceButton, SourceListItem } from '@features/models/ui/shared/SourceListItem';-import { useInstalledSources } from '@features/models/ui/shared/useInstalledSources';+import { useInstalledSourceKeys } from '@features/models/ui/shared/useInstalledSources';import { sourceFileName, useSourceNameFilter } from '@features/models/ui/shared/useSourceNameFilter';import { IconButton } from '@platform/ui';import { XIcon } from 'lucide-react';@@ -34,8 +34,8 @@ export const ScanResults = ({const { filter, filteredItems: filteredResults, setFilter } = useSourceNameFilter(scan.results, pathOf);// Live library state: `is_installed` is a scan-time snapshot, so a model// installed from this list would otherwise keep offering Install forever.- const installedSources = useInstalledSources();- const isRowInstalled = (result: FoundModel) => result.is_installed || installedSources.has(result.path);+ const installedSourceKeys = useInstalledSourceKeys();+ const isRowInstalled = (result: FoundModel) => result.is_installed || installedSourceKeys.has(result.path);const notInstalledCount = scan.results.filter((result) => !isRowInstalled(result)).length;const installable = filteredResults.filter((result) => !isRowInstalled(result));@@ -92,6 +92,7 @@ export const ScanResults = ({titleTooltip={result.path}trailing={<InstallSourceButton+ installedModelKey={installedSourceKeys.get(result.path) ?? null}isInstalled={isRowInstalled(result)}isPending={pendingSources.has(result.path)}source={result.path}diff --git a/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterList.tsx b/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterList.tsxindex 6e8cdcc770..27b6ba374f 100644--- a/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterList.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterList.tsx@@ -1,11 +1,13 @@-import type { StarterModel } from '@features/models/core/types';+import type { ModelConfig, StarterModel } from '@features/models/core/types';/* eslint-disable react-perf/jsx-no-jsx-as-prop, react-perf/jsx-no-new-array-as-prop, react-perf/jsx-no-new-function-as-prop, react-perf/jsx-no-new-object-as-prop */import type { StartersSnapshot } from '@features/models/data/startersStore';import { Badge, Flex, Icon, Spinner, Stack, Text } from '@chakra-ui/react';import { getModelBaseColorPalette, getModelBaseLabel } from '@features/models/core/baseIdentity';import { getModelTypeLabel } from '@features/models/core/taxonomy';+import { useModelsSelector } from '@features/models/data/modelsStore';import { InstallSourceButton, SourceListItem } from '@features/models/ui/shared/SourceListItem';+import { findInstalledStarterModelKey, useInstalledSourceKeys } from '@features/models/ui/shared/useInstalledSources';import { Button } from '@platform/ui';import { KeyRoundIcon } from 'lucide-react';import { useTranslation } from 'react-i18next';@@ -18,6 +20,8 @@ const getExternalProviderId = (source: string): string | null => {return source.slice('external://'.length).split('/', 1)[0] || null;};+const selectModels = (snapshot: { models: readonly ModelConfig[] }): readonly ModelConfig[] => snapshot.models;+export const StarterList = ({configuredExternalProviders,isInstallable,@@ -42,6 +46,8 @@ export const StarterList = ({status: StartersSnapshot['status'];}) => {const { t } = useTranslation();+ const installedSourceKeys = useInstalledSourceKeys();+ const installedModels = useModelsSelector(selectModels);if (status === 'error' && loadError) {return (@@ -100,6 +106,7 @@ export const StarterList = ({)) : (<InstallSourceButton+ installedModelKey={findInstalledStarterModelKey(model, installedSourceKeys, installedModels)}isInstalled={model.is_installed}isPending={pendingSources.has(model.source)}source={model.source}diff --git a/invokeai/frontend/webv2/src/features/models/ui/shared/SourceListItem.tsx b/invokeai/frontend/webv2/src/features/models/ui/shared/SourceListItem.tsxindex 4a4a72180f..271a4f82b5 100644--- a/invokeai/frontend/webv2/src/features/models/ui/shared/SourceListItem.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/shared/SourceListItem.tsx@@ -3,7 +3,7 @@ import type { ReactNode } from 'react';import { Badge, HStack, Icon, Spinner, Stack, Text } from '@chakra-ui/react';import { useActiveInstallSources } from '@features/models/data/installsStore';-import { openInstallQueue } from '@features/models/ui/uiStore';+import { openInstallQueue, openModelDetail } from '@features/models/ui/uiStore';import { Button, Panel } from '@platform/ui';import { MiddleTruncate } from '@platform/ui/MiddleTruncate';import { DownloadIcon } from 'lucide-react';@@ -47,15 +47,19 @@ export const SourceListItem = ({/*** Install action with live state: idle → Install button; queuing/active →- * disabled "Installing…" with a jump to the install queue; done → badge.+ * disabled "Installing…" with a jump to the install queue; done → badge with a+ * jump to the installed model's page when the library knows which one it is.*/export const InstallSourceButton = ({+ installedModelKey = null,isInstalled = false,isPending = false,onInstall,source,}: {- /** Already in the library (renders a static badge). */+ /** The library model this source landed as; implies installed. */+ installedModelKey?: string | null;+ /** Already in the library, without a known model to open. */isInstalled?: boolean;/** The install POST is in flight (before a job exists). */isPending?: boolean;@@ -67,11 +71,25 @@ export const InstallSourceButton = ({const activeSources = useActiveInstallSources();const isInstalling = isPending || activeSources.has(source);- if (isInstalled) {+ if (isInstalled || installedModelKey !== null) {return (- <Badge colorPalette="green" flexShrink={0} fontSize="2xs" size="sm" variant="surface">- {t('models.installed')}- </Badge>+ <HStack flexShrink={0} gap="1.5">+ <Badge colorPalette="green" fontSize="2xs" size="sm" variant="surface">+ {t('models.installed')}+ </Badge>+ {installedModelKey !== null ? (+ <Button+ size="2xs"+ variant="ghost"+ onClick={(event) => {+ event.stopPropagation();+ openModelDetail(installedModelKey);+ }}+ >+ {t('models.viewModel')}+ </Button>+ ) : null}+ </HStack>);}diff --git a/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.test.ts b/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.test.tsnew file mode 100644index 0000000000..5427c87d93--- /dev/null+++ b/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.test.ts@@ -0,0 +1,42 @@+import type { ModelConfig, StarterModel } from '@features/models/core/types';++import { describe, expect, it } from 'vitest';++import { findInstalledStarterModelKey } from './useInstalledSources';++const model = (overrides: Partial<ModelConfig>): ModelConfig =>+ ({+ base: 'sdxl',+ key: 'k',+ name: 'Juggernaut',+ source: 'https://example/j',+ type: 'main',+ ...overrides,+ }) as ModelConfig;++const starter: Pick<StarterModel, 'base' | 'name' | 'previous_names' | 'source' | 'type'> = {+ base: 'sdxl',+ name: 'Juggernaut XL',+ previous_names: ['Juggernaut'],+ source: 'https://example/jxl',+ type: 'main',+};++describe('findInstalledStarterModelKey', () => {+ it('prefers the model recorded under the starter source', () => {+ const bySource = new Map([['https://example/jxl', 'from-source']]);+ expect(findInstalledStarterModelKey(starter, bySource, [model({ key: 'by-name', name: 'Juggernaut XL' })])).toBe(+ 'from-source'+ );+ });++ it('falls back to a name or previous name with the same base and type', () => {+ const models = [+ model({ base: 'sd-1', key: 'wrong-base', name: 'Juggernaut XL' }),+ model({ key: 'wrong-type', name: 'Juggernaut XL', type: 'lora' }),+ model({ key: 'renamed', name: 'Juggernaut' }),+ ];+ expect(findInstalledStarterModelKey(starter, new Map(), models)).toBe('renamed');+ expect(findInstalledStarterModelKey(starter, new Map(), models.slice(0, 2))).toBeNull();+ });+});diff --git a/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.ts b/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.tsindex edbed7a233..fd2234c813 100644--- a/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.ts+++ b/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.ts@@ -1,25 +1,26 @@+import type { ModelConfig, StarterModel } from '@features/models/core/types';import type { ModelsSnapshot } from '@features/models/data/modelsStore';import { resolveModelAbsolutePath } from '@features/models/core/schemas';import { useModelsSelector } from '@features/models/data/modelsStore';-const areSetsEqual = (left: ReadonlySet<string>, right: ReadonlySet<string>): boolean =>- left.size === right.size && [...left].every((value) => right.has(value));+const areMapsEqual = (left: ReadonlyMap<string, string>, right: ReadonlyMap<string, string>): boolean =>+ left.size === right.size && [...left].every(([source, key]) => right.get(source) === key);// Selectors run on every store notification for every subscriber; cache by the-// inputs' identities so repeat notifications reuse one set (and pass equality+// inputs' identities so repeat notifications reuse one map (and pass equality// by reference instead of a member-by-member scan).let lastModels: ModelsSnapshot['models'] | null = null;let lastModelsDir: string | null = null;-let lastSources: ReadonlySet<string> = new Set<string>();+let lastSources: ReadonlyMap<string, string> = new Map<string, string>();-const selectInstalledSources = (snapshot: ModelsSnapshot): ReadonlySet<string> => {+const selectInstalledSourceKeys = (snapshot: ModelsSnapshot): ReadonlyMap<string, string> => {if (snapshot.models !== lastModels || snapshot.modelsDir !== lastModelsDir) {- const sources = new Set<string>();+ const sources = new Map<string, string>();for (const model of snapshot.models) {- sources.add(model.source);- sources.add(resolveModelAbsolutePath(model.path, snapshot.modelsDir));+ sources.set(model.source, model.key);+ sources.set(resolveModelAbsolutePath(model.path, snapshot.modelsDir), model.key);}lastModels = snapshot.models;@@ -31,9 +32,33 @@ const selectInstalledSources = (snapshot: ModelsSnapshot): ReadonlySet<string> =};/**- * Every string under which a library model is reachable as an install source:- * its recorded install source plus its resolved absolute file path. Source- * rows (folder scan, HuggingFace files) derive "installed" from this live set- * — a scan-time snapshot would keep offering Install after the job finishes.+ * Every string under which a library model is reachable as an install source+ * — its recorded install source plus its resolved absolute file path — mapped+ * to that model's key. Source rows (folder scan, HuggingFace files) derive+ * "installed" from this live map and link to the model it names; a scan-time+ * snapshot would keep offering Install after the job finishes.*/-export const useInstalledSources = (): ReadonlySet<string> => useModelsSelector(selectInstalledSources, areSetsEqual);+export const useInstalledSourceKeys = (): ReadonlyMap<string, string> =>+ useModelsSelector(selectInstalledSourceKeys, areMapsEqual);++const starterIdentity = (base: string, type: string, name: string): string => `${base}\u0000${type}\u0000${name}`;++/**+ * The installed model a starter entry stands for, matched the way the backend+ * marks starters installed: by install source, else by name (or a previous+ * name) together with base and type.+ */+export const findInstalledStarterModelKey = (+ starter: Pick<StarterModel, 'base' | 'name' | 'previous_names' | 'source' | 'type'>,+ installedSourceKeys: ReadonlyMap<string, string>,+ models: readonly ModelConfig[]+): string | null => {+ const bySource = installedSourceKeys.get(starter.source);+ if (bySource !== undefined) {+ return bySource;+ }+ const names = new Set(+ [starter.name, ...(starter.previous_names ?? [])].map((name) => starterIdentity(starter.base, starter.type, name))+ );+ return models.find((model) => names.has(starterIdentity(model.base, model.type, model.name)))?.key ?? null;+};