Skip to content

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.

fix(ui): derive source-row installed state from the live library

joshistoast1dd1fc0aa803
diff --git a/invokeai/frontend/webv2/src/features/models/core/schemas.test.ts b/invokeai/frontend/webv2/src/features/models/core/schemas.test.ts
index 58219c6ba3..1a63cf8cc5 100644
--- a/invokeai/frontend/webv2/src/features/models/core/schemas.test.ts
+++ b/invokeai/frontend/webv2/src/features/models/core/schemas.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { isAbsoluteModelPath, modelPathSchema } from './schemas';
+import { isAbsoluteModelPath, modelPathSchema, resolveModelAbsolutePath } from './schemas';
describe('isAbsoluteModelPath', () => {
it.each([
@@ -30,3 +30,21 @@ describe('modelPathSchema', () => {
expect(modelPathSchema.safeParse('models/x.safetensors').success).toBe(false);
});
});
+
+describe('resolveModelAbsolutePath', () => {
+ it('resolves managed relative paths against the models directory', () => {
+ expect(resolveModelAbsolutePath('sdxl/main/model.safetensors', '/data/models')).toBe(
+ '/data/models/sdxl/main/model.safetensors'
+ );
+ expect(resolveModelAbsolutePath('sdxl/main/model.safetensors', '/data/models/')).toBe(
+ '/data/models/sdxl/main/model.safetensors'
+ );
+ });
+
+ it('keeps absolute in-place paths and falls back when the directory is unknown', () => {
+ expect(resolveModelAbsolutePath('/home/user/model.safetensors', '/data/models')).toBe(
+ '/home/user/model.safetensors'
+ );
+ expect(resolveModelAbsolutePath('sdxl/model.safetensors', null)).toBe('sdxl/model.safetensors');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/models/core/schemas.ts b/invokeai/frontend/webv2/src/features/models/core/schemas.ts
index 8351193f0d..1bd2c2a995 100644
--- a/invokeai/frontend/webv2/src/features/models/core/schemas.ts
+++ b/invokeai/frontend/webv2/src/features/models/core/schemas.ts
@@ -94,6 +94,14 @@ export type LoraDefaultSettingsFormValues = z.infer<typeof loraDefaultSettingsSc
export const isAbsoluteModelPath = (path: string): boolean =>
path.startsWith('/') || path.startsWith('\\\\') || /^[A-Za-z]:[\\/]/.test(path);
+/**
+ * Mirrors the backend's `models_path / model.path` resolution: managed models
+ * store paths relative to the models directory, in-place installs store them
+ * absolute. Falls back to the stored path when the directory is unknown.
+ */
+export const resolveModelAbsolutePath = (path: string, modelsDir: string | null): string =>
+ isAbsoluteModelPath(path) || !modelsDir ? path : `${modelsDir.replace(/\/+$/, '')}/${path}`;
+
/**
* Replacement path for a relocated model. Only absolute paths are accepted:
* managed models store paths relative to the models directory and must not be
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.tsx
index c06a6ee82d..ddb9ddbb43 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,6 +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 { useDeferredValue, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -26,6 +27,9 @@ export const HuggingFaceFiles = ({
const { t } = useTranslation();
const [filter, setFilter] = useState('');
const deferredFilter = useDeferredValue(filter);
+ // 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 filteredUrls = useMemo(() => {
const term = deferredFilter.trim().toLowerCase();
@@ -59,7 +63,12 @@ export const HuggingFaceFiles = ({
title={fileNameOf(url)}
titleTooltip={url}
trailing={
- <InstallSourceButton isPending={pendingSources.has(url)} source={url} onInstall={() => onInstall(url)} />
+ <InstallSourceButton
+ isInstalled={installedSources.has(url)}
+ 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.tsx
new file mode 100644
index 0000000000..deee6a3284
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/models/ui/add-models/ScanResults.browser.test.tsx
@@ -0,0 +1,104 @@
+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 { accountLifecycle } from '@platform/state/accountLifecycle';
+import { system } from '@theme/system';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { ScanResults } from './ScanResults';
+
+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
+
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+const SCANNED_PATH = '/home/user/downloads/juggernaut.safetensors';
+
+// The shape the backend returns for a local install: source is a structured
+// LocalModelSource, not the raw string the frontend sent.
+const queuedJob: ModelInstallJob = {
+ id: 1,
+ source: { inplace: true, path: SCANNED_PATH, type: 'local' },
+ status: 'waiting',
+};
+
+const installedModel = {
+ base: 'sdxl',
+ file_size: 1024,
+ format: 'checkpoint',
+ hash: 'hash',
+ key: 'installed-key',
+ name: 'Juggernaut',
+ path: SCANNED_PATH,
+ source: SCANNED_PATH,
+ source_type: 'path',
+ type: 'main',
+} as ModelConfig;
+
+const noop = () => undefined;
+const NO_PENDING: ReadonlySet<string> = new Set<string>();
+const SCAN = { path: '/home/user/downloads', results: [{ is_installed: false, path: SCANNED_PATH }] };
+
+const Harness = () => (
+ <ChakraProvider value={system}>
+ <ScanResults
+ inplace
+ pendingSources={NO_PENDING}
+ scan={SCAN}
+ onClear={noop}
+ onInstall={noop}
+ onInstallAll={noop}
+ onSetInplace={noop}
+ />
+ </ChakraProvider>
+);
+
+describe('ScanResults install badge lifecycle', () => {
+ let host: HTMLDivElement;
+ let root: Root;
+
+ beforeEach(async () => {
+ accountLifecycle.activate('scan-results-test-a', ':user:scan-results-test-a');
+ setModelsSnapshotForTests({ models: [], status: 'loaded' });
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+
+ await act(() => {
+ root.render(<Harness />);
+ });
+ });
+
+ afterEach(async () => {
+ await act(() => root.unmount());
+ host.remove();
+ accountLifecycle.invalidate();
+ });
+
+ it('walks Install -> Installing -> Installed as the job progresses', async () => {
+ expect(host.textContent).toContain('models.install');
+ expect(host.textContent).not.toContain('models.installing');
+
+ // The install POST resolved: the queued job (backend-shaped source) lands
+ // in the installs store.
+ await act(async () => {
+ addInstallJob(queuedJob);
+ await Promise.resolve();
+ });
+
+ expect(host.textContent).toContain('models.installing');
+
+ // The job completes and the library refresh lands the new model.
+ await act(async () => {
+ replaceInstallJob({ ...queuedJob, status: 'completed' });
+ setModelsSnapshotForTests({ models: [installedModel], status: 'loaded' });
+ await Promise.resolve();
+ });
+
+ expect(host.textContent).not.toContain('models.installing');
+ expect(host.textContent).toContain('models.installed');
+ });
+});
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.tsx
index e07ac64a41..f15e8f19c3 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,6 +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 { IconButton } from '@platform/ui';
import { XIcon } from 'lucide-react';
import { useDeferredValue, useMemo, useState } from 'react';
@@ -32,6 +33,10 @@ export const ScanResults = ({
const { t } = useTranslation();
const [filter, setFilter] = useState('');
const deferredFilter = useDeferredValue(filter);
+ // 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 filteredResults = useMemo(() => {
const term = deferredFilter.trim().toLowerCase();
@@ -43,8 +48,8 @@ export const ScanResults = ({
return scan.results.filter((result) => fileNameOf(result.path).toLowerCase().includes(term));
}, [deferredFilter, scan.results]);
- const notInstalledCount = scan.results.filter((result) => !result.is_installed).length;
- const installable = filteredResults.filter((result) => !result.is_installed);
+ const notInstalledCount = scan.results.filter((result) => !isRowInstalled(result)).length;
+ const installable = filteredResults.filter((result) => !isRowInstalled(result));
const installAll = () => {
onInstallAll(installable.map((result) => result.path));
@@ -98,7 +103,7 @@ export const ScanResults = ({
titleTooltip={result.path}
trailing={
<InstallSourceButton
- isInstalled={result.is_installed}
+ isInstalled={isRowInstalled(result)}
isPending={pendingSources.has(result.path)}
source={result.path}
onInstall={() => onInstall(result.path)}
diff --git a/invokeai/frontend/webv2/src/features/models/ui/detail/ModelDetail.tsx b/invokeai/frontend/webv2/src/features/models/ui/detail/ModelDetail.tsx
index ec10d1d4ec..877ef09441 100644
--- a/invokeai/frontend/webv2/src/features/models/ui/detail/ModelDetail.tsx
+++ b/invokeai/frontend/webv2/src/features/models/ui/detail/ModelDetail.tsx
@@ -5,7 +5,7 @@ import type { ReactNode } from 'react';
import { DataList, HStack, Icon, Menu, Portal, Separator, Stack, Text } from '@chakra-ui/react';
import { isConvertibleToDiffusers } from '@features/models/core/baseIdentity';
import { isLinkableType } from '@features/models/core/relationships';
-import { isAbsoluteModelPath } from '@features/models/core/schemas';
+import { isAbsoluteModelPath, resolveModelAbsolutePath } from '@features/models/core/schemas';
import { formatBytes } from '@features/models/core/taxonomy';
import { useModelsSelector } from '@features/models/data/modelsStore';
import { useNotify } from '@features/models/ui/useModelsNotify';
@@ -440,8 +440,7 @@ const ModelAttributes = ({ isMissing, model }: { isMissing: boolean; model: Mode
const [isPathDialogOpen, setIsPathDialogOpen] = useState(false);
// Managed models store paths relative to the models directory; show the
// resolved absolute path so it can be found on disk.
- const fullPath =
- isAbsoluteModelPath(model.path) || !modelsDir ? model.path : `${modelsDir.replace(/\/+$/, '')}/${model.path}`;
+ const fullPath = resolveModelAbsolutePath(model.path, modelsDir);
// In-place installs (absolute paths) may be repointed after the file moves;
// a missing model gets the affordance too — that is exactly when it helps.
const canUpdatePath = isAbsoluteModelPath(model.path) || isMissing;
diff --git a/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.ts b/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.ts
new file mode 100644
index 0000000000..ab95cee5a6
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/models/ui/shared/useInstalledSources.ts
@@ -0,0 +1,26 @@
+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 selectInstalledSources = (snapshot: ModelsSnapshot): ReadonlySet<string> => {
+ const sources = new Set<string>();
+
+ for (const model of snapshot.models) {
+ sources.add(model.source);
+ sources.add(resolveModelAbsolutePath(model.path, snapshot.modelsDir));
+ }
+
+ return sources;
+};
+
+/**
+ * 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.
+ */
+export const useInstalledSources = (): ReadonlySet<string> => useModelsSelector(selectInstalledSources, areSetsEqual);