|
|
@ -1,6 +1,8 @@ |
|
|
|
using System; |
|
|
|
using System; |
|
|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.Collections.ObjectModel; |
|
|
|
using System.Collections.ObjectModel; |
|
|
|
|
|
|
|
using System.Collections.Specialized; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq; |
|
|
|
using System.Reactive.Linq; |
|
|
|
using System.Reactive.Linq; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Text.Json; |
|
|
@ -51,6 +53,8 @@ public partial class InferenceViewModel : PageViewModelBase, IAsyncDisposable |
|
|
|
private readonly ServiceManager<ViewModelBase> vmFactory; |
|
|
|
private readonly ServiceManager<ViewModelBase> vmFactory; |
|
|
|
private readonly IModelIndexService modelIndexService; |
|
|
|
private readonly IModelIndexService modelIndexService; |
|
|
|
private readonly ILiteDbContext liteDbContext; |
|
|
|
private readonly ILiteDbContext liteDbContext; |
|
|
|
|
|
|
|
private readonly RunningPackageService runningPackageService; |
|
|
|
|
|
|
|
private Guid? selectedPackageId; |
|
|
|
|
|
|
|
|
|
|
|
public override string Title => "Inference"; |
|
|
|
public override string Title => "Inference"; |
|
|
|
public override IconSource IconSource => |
|
|
|
public override IconSource IconSource => |
|
|
@ -86,6 +90,8 @@ public partial class InferenceViewModel : PageViewModelBase, IAsyncDisposable |
|
|
|
|
|
|
|
|
|
|
|
public bool IsComfyRunning => RunningPackage?.BasePackage is ComfyUI; |
|
|
|
public bool IsComfyRunning => RunningPackage?.BasePackage is ComfyUI; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IDisposable? onStartupComplete; |
|
|
|
|
|
|
|
|
|
|
|
public InferenceViewModel( |
|
|
|
public InferenceViewModel( |
|
|
|
ServiceManager<ViewModelBase> vmFactory, |
|
|
|
ServiceManager<ViewModelBase> vmFactory, |
|
|
|
INotificationService notificationService, |
|
|
|
INotificationService notificationService, |
|
|
@ -93,6 +99,7 @@ public partial class InferenceViewModel : PageViewModelBase, IAsyncDisposable |
|
|
|
ISettingsManager settingsManager, |
|
|
|
ISettingsManager settingsManager, |
|
|
|
IModelIndexService modelIndexService, |
|
|
|
IModelIndexService modelIndexService, |
|
|
|
ILiteDbContext liteDbContext, |
|
|
|
ILiteDbContext liteDbContext, |
|
|
|
|
|
|
|
RunningPackageService runningPackageService, |
|
|
|
SharedState sharedState |
|
|
|
SharedState sharedState |
|
|
|
) |
|
|
|
) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -101,12 +108,13 @@ public partial class InferenceViewModel : PageViewModelBase, IAsyncDisposable |
|
|
|
this.settingsManager = settingsManager; |
|
|
|
this.settingsManager = settingsManager; |
|
|
|
this.modelIndexService = modelIndexService; |
|
|
|
this.modelIndexService = modelIndexService; |
|
|
|
this.liteDbContext = liteDbContext; |
|
|
|
this.liteDbContext = liteDbContext; |
|
|
|
|
|
|
|
this.runningPackageService = runningPackageService; |
|
|
|
|
|
|
|
|
|
|
|
ClientManager = inferenceClientManager; |
|
|
|
ClientManager = inferenceClientManager; |
|
|
|
SharedState = sharedState; |
|
|
|
SharedState = sharedState; |
|
|
|
|
|
|
|
|
|
|
|
// Keep RunningPackage updated with the current package pair |
|
|
|
// Keep RunningPackage updated with the current package pair |
|
|
|
EventManager.Instance.RunningPackageStatusChanged += OnRunningPackageStatusChanged; |
|
|
|
runningPackageService.RunningPackages.CollectionChanged += RunningPackagesOnCollectionChanged; |
|
|
|
|
|
|
|
|
|
|
|
// "Send to Inference" |
|
|
|
// "Send to Inference" |
|
|
|
EventManager.Instance.InferenceTextToImageRequested += OnInferenceTextToImageRequested; |
|
|
|
EventManager.Instance.InferenceTextToImageRequested += OnInferenceTextToImageRequested; |
|
|
@ -118,24 +126,62 @@ public partial class InferenceViewModel : PageViewModelBase, IAsyncDisposable |
|
|
|
MenuOpenProjectCommand.WithConditionalNotificationErrorHandler(notificationService); |
|
|
|
MenuOpenProjectCommand.WithConditionalNotificationErrorHandler(notificationService); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void DisconnectFromComfy() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
RunningPackage = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Cancel any pending connection |
|
|
|
|
|
|
|
if (ConnectCancelCommand.CanExecute(null)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ConnectCancelCommand.Execute(null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
onStartupComplete?.Dispose(); |
|
|
|
|
|
|
|
onStartupComplete = null; |
|
|
|
|
|
|
|
IsWaitingForConnection = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Disconnect |
|
|
|
|
|
|
|
Logger.Trace("On package close - disconnecting"); |
|
|
|
|
|
|
|
DisconnectCommand.Execute(null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Updates the RunningPackage property when the running package changes. |
|
|
|
/// Updates the RunningPackage property when the running package changes. |
|
|
|
/// Also starts a connection to the backend if a new ComfyUI package is running. |
|
|
|
/// Also starts a connection to the backend if a new ComfyUI package is running. |
|
|
|
/// And disconnects if the package is closed. |
|
|
|
/// And disconnects if the package is closed. |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
private void OnRunningPackageStatusChanged(object? sender, RunningPackageStatusChangedEventArgs e) |
|
|
|
private void RunningPackagesOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
e.NewItems?.OfType<KeyValuePair<Guid, RunningPackageViewModel>>().Select(x => x.Value) |
|
|
|
|
|
|
|
is not { } newItems |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (RunningPackage != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
RunningPackage = e.CurrentPackagePair; |
|
|
|
DisconnectFromComfy(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IDisposable? onStartupComplete = null; |
|
|
|
var comfyViewModel = newItems.FirstOrDefault( |
|
|
|
|
|
|
|
vm => |
|
|
|
|
|
|
|
vm.RunningPackage.InstalledPackage.Id == selectedPackageId |
|
|
|
|
|
|
|
|| vm.RunningPackage.BasePackage is ComfyUI |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(() => |
|
|
|
if (comfyViewModel is null && RunningPackage?.BasePackage is ComfyUI) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (e.CurrentPackagePair?.BasePackage is ComfyUI package) |
|
|
|
DisconnectFromComfy(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (comfyViewModel != null && RunningPackage == null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
IsWaitingForConnection = true; |
|
|
|
IsWaitingForConnection = true; |
|
|
|
|
|
|
|
RunningPackage = comfyViewModel.RunningPackage; |
|
|
|
onStartupComplete = Observable |
|
|
|
onStartupComplete = Observable |
|
|
|
.FromEventPattern<string>(package, nameof(package.StartupComplete)) |
|
|
|
.FromEventPattern<string>( |
|
|
|
|
|
|
|
comfyViewModel.RunningPackage.BasePackage, |
|
|
|
|
|
|
|
nameof(comfyViewModel.RunningPackage.BasePackage.StartupComplete) |
|
|
|
|
|
|
|
) |
|
|
|
.Take(1) |
|
|
|
.Take(1) |
|
|
|
.Subscribe(_ => |
|
|
|
.Subscribe(_ => |
|
|
|
{ |
|
|
|
{ |
|
|
@ -146,26 +192,11 @@ public partial class InferenceViewModel : PageViewModelBase, IAsyncDisposable |
|
|
|
Logger.Trace("On package launch - starting connection"); |
|
|
|
Logger.Trace("On package launch - starting connection"); |
|
|
|
ConnectCommand.Execute(null); |
|
|
|
ConnectCommand.Execute(null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IsWaitingForConnection = false; |
|
|
|
IsWaitingForConnection = false; |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Cancel any pending connection |
|
|
|
|
|
|
|
if (ConnectCancelCommand.CanExecute(null)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ConnectCancelCommand.Execute(null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
onStartupComplete?.Dispose(); |
|
|
|
|
|
|
|
onStartupComplete = null; |
|
|
|
|
|
|
|
IsWaitingForConnection = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Disconnect |
|
|
|
|
|
|
|
Logger.Trace("On package close - disconnecting"); |
|
|
|
|
|
|
|
DisconnectCommand.Execute(null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override void OnLoaded() |
|
|
|
public override void OnLoaded() |
|
|
@ -390,7 +421,12 @@ public partial class InferenceViewModel : PageViewModelBase, IAsyncDisposable |
|
|
|
private async Task ShowConnectionHelp() |
|
|
|
private async Task ShowConnectionHelp() |
|
|
|
{ |
|
|
|
{ |
|
|
|
var vm = vmFactory.Get<InferenceConnectionHelpViewModel>(); |
|
|
|
var vm = vmFactory.Get<InferenceConnectionHelpViewModel>(); |
|
|
|
await vm.CreateDialog().ShowAsync(); |
|
|
|
var result = await vm.CreateDialog().ShowAsync(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (result != ContentDialogResult.Primary) |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
selectedPackageId = vm.SelectedPackage?.Id; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|