diff --git a/StabilityMatrix.Avalonia/Languages/Resources.Designer.cs b/StabilityMatrix.Avalonia/Languages/Resources.Designer.cs index 44bca5b52..a91f4cf7a 100644 --- a/StabilityMatrix.Avalonia/Languages/Resources.Designer.cs +++ b/StabilityMatrix.Avalonia/Languages/Resources.Designer.cs @@ -3048,6 +3048,15 @@ public static string Label_OnlyAvailableOnWindows { } } + /// + /// Looks up a localized string similar to Only shared components (VAE, text encoder, etc.) are installed. + /// + public static string Label_OnlySharedComponentsInstalled { + get { + return ResourceManager.GetString("Label_OnlySharedComponentsInstalled", resourceCulture); + } + } + /// /// Looks up a localized string similar to OpenArt Browser. /// diff --git a/StabilityMatrix.Avalonia/Languages/Resources.resx b/StabilityMatrix.Avalonia/Languages/Resources.resx index ede9d4b94..bf674fe83 100644 --- a/StabilityMatrix.Avalonia/Languages/Resources.resx +++ b/StabilityMatrix.Avalonia/Languages/Resources.resx @@ -498,6 +498,9 @@ Only available on Windows + + Only shared components (VAE, text encoder, etc.) are installed + Add for Current User diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/ModelVersionViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/ModelVersionViewModel.cs index b162ee8ab..b42f2d903 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/ModelVersionViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/ModelVersionViewModel.cs @@ -17,30 +17,25 @@ public partial class ModelVersionViewModel : DisposableViewModelBase [ObservableProperty] public partial bool IsInstalled { get; set; } + [ObservableProperty] + public partial bool IsComponentsOnlyInstalled { get; set; } + public ModelVersionViewModel(IModelIndexService modelIndexService, CivitModelVersion modelVersion) { this.modelIndexService = modelIndexService; ModelVersion = modelVersion; - // Install detection is purely hash-based: a file whose hash is in the local model index is - // on disk, regardless of the type Civitai reports (which may be Unknown for new types) - IsInstalled = - ModelVersion.Files?.Any(file => - file is { Hashes.BLAKE3: not null } - && modelIndexService.ModelIndexBlake3Hashes.Contains(file.Hashes.BLAKE3) - ) ?? false; + RefreshInstallStatus(); EventManager.Instance.ModelIndexChanged += ModelIndexChanged; } public void RefreshInstallStatus() { - IsInstalled = - ModelVersion.Files?.Any(file => - file is { Hashes.BLAKE3: not null } - && modelIndexService.ModelIndexBlake3Hashes.Contains(file.Hashes.BLAKE3) - ) ?? false; + var installState = ModelVersion.GetInstallState(modelIndexService.ModelIndexBlake3Hashes); + IsInstalled = installState is CivitModelVersionInstallState.Installed; + IsComponentsOnlyInstalled = installState is CivitModelVersionInstallState.ComponentsOnly; } private void ModelIndexChanged(object? sender, EventArgs e) diff --git a/StabilityMatrix.Avalonia/Views/CivitDetailsPage.axaml b/StabilityMatrix.Avalonia/Views/CivitDetailsPage.axaml index bb25fb707..9c8ca2bea 100644 --- a/StabilityMatrix.Avalonia/Views/CivitDetailsPage.axaml +++ b/StabilityMatrix.Avalonia/Views/CivitDetailsPage.axaml @@ -285,6 +285,15 @@ VerticalAlignment="Center" IsVisible="{Binding IsInstalled}" Value="fa-solid fa-check" /> + diff --git a/StabilityMatrix.Core/Models/Api/CivitFileType.cs b/StabilityMatrix.Core/Models/Api/CivitFileType.cs index 6ad9cbf75..1dadf2254 100644 --- a/StabilityMatrix.Core/Models/Api/CivitFileType.cs +++ b/StabilityMatrix.Core/Models/Api/CivitFileType.cs @@ -55,6 +55,25 @@ or CivitFileType.PrunedModel or CivitFileType.DiffusionModel or CivitFileType.UNet; + /// + /// True for companion files (VAEs, encoders, configs, etc) that ship alongside a version's main + /// weights. These are often shared between versions and models, so having one installed does + /// not mean the version itself is installed. + /// + public static bool IsSupportingComponent(this CivitFileType type) => + type + is CivitFileType.VAE + or CivitFileType.TextEncoder + or CivitFileType.VisionEncoder + or CivitFileType.CLIPVision + or CivitFileType.Config + or CivitFileType.TrainingData + or CivitFileType.Negative + or CivitFileType.ControlNet + or CivitFileType.Upscaler + or CivitFileType.EnhancementLora + or CivitFileType.Workflow; + /// /// True for file types worth listing as downloadable files in the model browser — /// model weights plus companion components (VAE, text/vision encoders, etc). diff --git a/StabilityMatrix.Core/Models/Api/CivitModelVersion.cs b/StabilityMatrix.Core/Models/Api/CivitModelVersion.cs index 10991b633..ca6599e90 100644 --- a/StabilityMatrix.Core/Models/Api/CivitModelVersion.cs +++ b/StabilityMatrix.Core/Models/Api/CivitModelVersion.cs @@ -50,4 +50,37 @@ public class CivitModelVersion public bool IsEarlyAccess => (Availability?.Equals("EarlyAccess", StringComparison.OrdinalIgnoreCase) ?? false) || EarlyAccessDeadline > DateTimeOffset.UtcNow; + + /// + /// Determines how much of this version is present locally, by BLAKE3 hash. Any one installed + /// main model file (any format or precision) makes the version installed; installed supporting + /// components alone do not, since they are frequently shared with other versions. + /// Files of unknown type count as main files so newly added Civitai types are still detected. + /// + public CivitModelVersionInstallState GetInstallState(IReadOnlySet installedBlake3Hashes) + { + if (Files is not { Count: > 0 } || installedBlake3Hashes.Count == 0) + { + return CivitModelVersionInstallState.NotInstalled; + } + + var installedFiles = Files + .Where(file => + file is { Hashes.BLAKE3: not null } && installedBlake3Hashes.Contains(file.Hashes.BLAKE3) + ) + .ToList(); + + if (installedFiles.Count == 0) + { + return CivitModelVersionInstallState.NotInstalled; + } + + var hasMainFiles = Files.Any(file => !file.Type.IsSupportingComponent()); + if (!hasMainFiles || installedFiles.Any(file => !file.Type.IsSupportingComponent())) + { + return CivitModelVersionInstallState.Installed; + } + + return CivitModelVersionInstallState.ComponentsOnly; + } } diff --git a/StabilityMatrix.Core/Models/Api/CivitModelVersionInstallState.cs b/StabilityMatrix.Core/Models/Api/CivitModelVersionInstallState.cs new file mode 100644 index 000000000..4013cf62b --- /dev/null +++ b/StabilityMatrix.Core/Models/Api/CivitModelVersionInstallState.cs @@ -0,0 +1,14 @@ +namespace StabilityMatrix.Core.Models.Api; + +public enum CivitModelVersionInstallState +{ + NotInstalled, + + /// + /// Only supporting components (VAE, text encoder, etc) of the version are installed, + /// none of its main model files. + /// + ComponentsOnly, + + Installed, +} diff --git a/StabilityMatrix.Tests/Core/CivitModelVersionInstallStateTests.cs b/StabilityMatrix.Tests/Core/CivitModelVersionInstallStateTests.cs new file mode 100644 index 000000000..10734afd9 --- /dev/null +++ b/StabilityMatrix.Tests/Core/CivitModelVersionInstallStateTests.cs @@ -0,0 +1,73 @@ +using StabilityMatrix.Core.Models.Api; + +namespace StabilityMatrix.Tests.Core; + +[TestClass] +public class CivitModelVersionInstallStateTests +{ + private static CivitFile File(CivitFileType type, string hash) => + new() + { + Type = type, + Hashes = new CivitFileHashes { BLAKE3 = hash }, + }; + + private static CivitModelVersion Version(params CivitFile[] files) => new() { Files = files.ToList() }; + + private static CivitModelVersion MultiFileVersion() => + Version( + File(CivitFileType.DiffusionModel, "bf16"), + File(CivitFileType.DiffusionModel, "fp8"), + File(CivitFileType.TextEncoder, "shared-encoder"), + File(CivitFileType.VAE, "shared-vae") + ); + + [TestMethod] + public void NothingInstalled_IsNotInstalled() + { + Assert.AreEqual( + CivitModelVersionInstallState.NotInstalled, + MultiFileVersion().GetInstallState(new HashSet { "unrelated" }) + ); + } + + [TestMethod] + public void OnlySharedComponentsInstalled_IsComponentsOnly() + { + Assert.AreEqual( + CivitModelVersionInstallState.ComponentsOnly, + MultiFileVersion().GetInstallState(new HashSet { "shared-encoder", "shared-vae" }) + ); + } + + [TestMethod] + public void AnyMainModelVariantInstalled_IsInstalled() + { + Assert.AreEqual( + CivitModelVersionInstallState.Installed, + MultiFileVersion().GetInstallState(new HashSet { "fp8" }) + ); + } + + [TestMethod] + public void UnknownFileTypeInstalled_IsInstalled() + { + var version = Version(File(CivitFileType.Unknown, "new-type"), File(CivitFileType.VAE, "vae")); + + Assert.AreEqual( + CivitModelVersionInstallState.Installed, + version.GetInstallState(new HashSet { "new-type" }) + ); + } + + [TestMethod] + public void VersionWithOnlyComponentFiles_IsInstalledWhenOneIsPresent() + { + var version = Version(File(CivitFileType.VAE, "vae"), File(CivitFileType.Config, "config")); + + Assert.AreEqual( + CivitModelVersionInstallState.Installed, + version.GetInstallState(new HashSet { "vae" }) + ); + } +}