Skip to content

Commit 33705e0

Browse files
authored
Merge pull request #1039 from ionite34/prompt-amplify-n-stuff
Prompt amplify n stuff
2 parents 944774a + 9d792c5 commit 33705e0

File tree

94 files changed

+1693
-329
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1693
-329
lines changed

.husky/task-runner.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"group": "generate-openapi",
2020
"command": "dotnet",
2121
"args": ["refitter", "--settings-file", "./StabilityMatrix.Core/Api/LykosAuthApi/.refitter"]
22+
},
23+
{
24+
"name": "Run refitter for PromptGenApi",
25+
"group": "generate-promptgen-openapi",
26+
"command": "dotnet",
27+
"args": ["refitter", "--settings-file", "./StabilityMatrix.Core/Api/PromptGen/.refitter"]
2228
}
2329
]
2430
}

StabilityMatrix.Avalonia/App.axaml.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
using StabilityMatrix.Avalonia.Views;
6060
using StabilityMatrix.Core.Api;
6161
using StabilityMatrix.Core.Api.LykosAuthApi;
62+
using StabilityMatrix.Core.Api.PromptGenApi;
6263
using StabilityMatrix.Core.Attributes;
6364
using StabilityMatrix.Core.Converters.Json;
6465
using StabilityMatrix.Core.Database;
@@ -139,6 +140,14 @@ public sealed class App : Application
139140
#else
140141
public const string LykosAccountApiBaseUrl = "https://account.lykos.ai/";
141142
#endif
143+
#if DEBUG
144+
// ReSharper disable twice LocalizableElement
145+
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
146+
public static string PromptGenApiBaseUrl =>
147+
Config?["PromptGenApiBaseUrl"] ?? "https://promptgen.lykos.ai/api";
148+
#else
149+
public const string PromptGenApiBaseUrl = "https://promptgen.lykos.ai/api";
150+
#endif
142151

143152
// ReSharper disable once MemberCanBePrivate.Global
144153
public IClassicDesktopStyleApplicationLifetime? DesktopLifetime =>
@@ -375,7 +384,7 @@ internal static void ConfigurePageViewModels(IServiceCollection services)
375384
new MainWindowViewModel(
376385
provider.GetRequiredService<ISettingsManager>(),
377386
provider.GetRequiredService<IDiscordRichPresenceService>(),
378-
provider.GetRequiredService<ServiceManager<ViewModelBase>>(),
387+
provider.GetRequiredService<IServiceManager<ViewModelBase>>(),
379388
provider.GetRequiredService<ITrackedDownloadService>(),
380389
provider.GetRequiredService<IModelIndexService>(),
381390
provider.GetRequiredService<Lazy<IModelDownloadLinkHandler>>(),
@@ -641,6 +650,21 @@ internal static IServiceCollection ConfigureServices()
641650
new TokenAuthHeaderHandler(serviceProvider.GetRequiredService<LykosAuthTokenProvider>())
642651
);
643652

653+
services
654+
.AddRefitClient<IPromptGenApi>(defaultRefitSettings)
655+
.ConfigureHttpClient(c =>
656+
{
657+
c.BaseAddress = new Uri(PromptGenApiBaseUrl);
658+
c.Timeout = TimeSpan.FromHours(1);
659+
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "");
660+
})
661+
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false })
662+
.AddPolicyHandler(retryPolicy)
663+
.AddHttpMessageHandler(
664+
serviceProvider =>
665+
new TokenAuthHeaderHandler(serviceProvider.GetRequiredService<LykosAuthTokenProvider>())
666+
);
667+
644668
services
645669
.AddRefitClient<ILykosAnalyticsApi>(defaultRefitSettings)
646670
.ConfigureHttpClient(c =>

StabilityMatrix.Avalonia/Controls/BetterDownloadableComboBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static async Task PromptDownloadAsync(IDownloadableResource downloadable
5454
if (downloadable.DownloadableResource is not { } resource)
5555
return;
5656

57-
var vmFactory = App.Services.GetRequiredService<ServiceManager<ViewModelBase>>();
57+
var vmFactory = App.Services.GetRequiredService<IServiceManager<ViewModelBase>>();
5858
var confirmDialog = vmFactory.Get<DownloadResourceViewModel>();
5959
confirmDialog.Resource = resource;
6060
confirmDialog.FileName = resource.FileName;

StabilityMatrix.Avalonia/Controls/FADownloadableComboBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static async Task PromptDownloadAsync(IDownloadableResource downloadable
5555
if (downloadable.DownloadableResource is not { } resource)
5656
return;
5757

58-
var vmFactory = App.Services.GetRequiredService<ServiceManager<ViewModelBase>>();
58+
var vmFactory = App.Services.GetRequiredService<IServiceManager<ViewModelBase>>();
5959
var confirmDialog = vmFactory.Get<DownloadResourceViewModel>();
6060
confirmDialog.Resource = resource;
6161
confirmDialog.FileName = resource.FileName;

StabilityMatrix.Avalonia/Controls/Inference/PromptCard.axaml

Lines changed: 299 additions & 0 deletions
Large diffs are not rendered by default.

StabilityMatrix.Avalonia/DesignData/DesignData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static void Initialize()
164164

165165
Services = services.BuildServiceProvider();
166166

167-
var dialogFactory = Services.GetRequiredService<ServiceManager<ViewModelBase>>();
167+
var dialogFactory = Services.GetRequiredService<IServiceManager<ViewModelBase>>();
168168
var settingsManager = Services.GetRequiredService<ISettingsManager>();
169169
var downloadService = Services.GetRequiredService<IDownloadService>();
170170
var modelFinder = Services.GetRequiredService<ModelFinder>();
@@ -438,8 +438,8 @@ public static void Initialize()
438438
[NotNull]
439439
public static UpdateViewModel? UpdateViewModel { get; private set; }
440440

441-
public static ServiceManager<ViewModelBase> DialogFactory =>
442-
Services.GetRequiredService<ServiceManager<ViewModelBase>>();
441+
public static IServiceManager<ViewModelBase> DialogFactory =>
442+
Services.GetRequiredService<IServiceManager<ViewModelBase>>();
443443

444444
public static MainWindowViewModel MainWindowViewModel =>
445445
Services.GetRequiredService<MainWindowViewModel>();

StabilityMatrix.Avalonia/DesignData/MockLaunchPageViewModel.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public MockLaunchPageViewModel(
2121
IPyRunner pyRunner,
2222
INotificationService notificationService,
2323
ISharedFolders sharedFolders,
24-
ServiceManager<ViewModelBase> dialogFactory
24+
IServiceManager<ViewModelBase> dialogFactory
2525
)
2626
: base(
2727
logger,
@@ -34,34 +34,35 @@ ServiceManager<ViewModelBase> dialogFactory
3434
) { }
3535

3636
public override BasePackage? SelectedBasePackage =>
37-
SelectedPackage?.PackageName != "dank-diffusion" ? base.SelectedBasePackage :
38-
new DankDiffusion(null!, null!, null!, null!);
39-
37+
SelectedPackage?.PackageName != "dank-diffusion"
38+
? base.SelectedBasePackage
39+
: new DankDiffusion(null!, null!, null!, null!);
40+
4041
protected override Task LaunchImpl(string? command)
4142
{
4243
IsLaunchTeachingTipsOpen = false;
4344

44-
RunningPackage = new PackagePair(
45-
null!,
46-
new DankDiffusion(null!, null!, null!, null!));
47-
48-
Console.Document.Insert(0,
45+
RunningPackage = new PackagePair(null!, new DankDiffusion(null!, null!, null!, null!));
46+
47+
Console.Document.Insert(
48+
0,
4949
"""
5050
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
5151
Version: 1.5.0
5252
Commit hash: <none>
5353
5454
Fetching updates for midas...
5555
Checking out commit for midas with hash: 2e42b7f...
56-
""");
57-
56+
"""
57+
);
58+
5859
return Task.CompletedTask;
5960
}
6061

6162
public override Task Stop()
6263
{
6364
RunningPackage = null;
64-
65+
6566
return Task.CompletedTask;
6667
}
6768
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.ComponentModel;
2+
using Avalonia.Controls;
3+
using StabilityMatrix.Avalonia.Controls;
4+
using StabilityMatrix.Avalonia.Services;
5+
using StabilityMatrix.Core.Attributes;
6+
7+
namespace StabilityMatrix.Avalonia.Extensions;
8+
9+
[Localizable(false)]
10+
public static class ServiceManagerExtensions
11+
{
12+
/// <summary>
13+
/// Get a view model instance, set as DataContext of its View, and return
14+
/// a BetterContentDialog with that View as its Content
15+
/// </summary>
16+
public static BetterContentDialog GetDialog<TService>(this IServiceManager<TService> serviceManager)
17+
{
18+
var instance = serviceManager.Get<TService>()!;
19+
20+
if (
21+
Attribute.GetCustomAttribute(instance.GetType(), typeof(ViewAttribute))
22+
is not ViewAttribute viewAttr
23+
)
24+
{
25+
throw new InvalidOperationException($"View not found for {instance.GetType().FullName}");
26+
}
27+
28+
if (Activator.CreateInstance(viewAttr.ViewType) is not Control view)
29+
{
30+
throw new NullReferenceException($"Unable to create instance for {instance.GetType().FullName}");
31+
}
32+
33+
return new BetterContentDialog { Content = view };
34+
}
35+
36+
/// <summary>
37+
/// Get a view model instance with initializer, set as DataContext of its View, and return
38+
/// a BetterContentDialog with that View as its Content
39+
/// </summary>
40+
public static BetterContentDialog GetDialog<TService>(
41+
this IServiceManager<TService> serviceManager,
42+
Action<TService> initializer
43+
)
44+
{
45+
var instance = serviceManager.Get(initializer)!;
46+
47+
if (
48+
Attribute.GetCustomAttribute(instance.GetType(), typeof(ViewAttribute))
49+
is not ViewAttribute viewAttr
50+
)
51+
{
52+
throw new InvalidOperationException($"View not found for {instance.GetType().FullName}");
53+
}
54+
55+
if (Activator.CreateInstance(viewAttr.ViewType) is not Control view)
56+
{
57+
throw new NullReferenceException($"Unable to create instance for {instance.GetType().FullName}");
58+
}
59+
60+
view.DataContext = instance;
61+
62+
return new BetterContentDialog { Content = view };
63+
}
64+
}

StabilityMatrix.Avalonia/Helpers/AttributeServiceInjector.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,21 @@ public static void AddServicesByAttributesSourceGen(IServiceCollection services)
4949
services.AddStabilityMatrixAvalonia();
5050
}
5151

52+
private static bool IsScoped(IServiceProvider serviceProvider)
53+
{
54+
// Hacky check for if service provider is scoped
55+
var typeName = serviceProvider.GetType().Name;
56+
if (typeName == "ServiceProviderEngineScope" || typeName.Contains("Scope"))
57+
{
58+
return true;
59+
}
60+
return false;
61+
}
62+
5263
/// <summary>
53-
/// Adds a <see cref="ServiceManager{T}"/> to the <see cref="IServiceCollection"/>.
64+
/// Adds a <see cref="IServiceManager{T}"/> to the <see cref="IServiceCollection"/>.
5465
/// </summary>
55-
/// <param name="services">The <see cref="IServiceCollection"/> to which the <see cref="ServiceManager{T}"/> will be added.</param>
66+
/// <param name="services">The <see cref="IServiceCollection"/> to which the <see cref="IServiceManager{T}"/> will be added.</param>
5667
/// <param name="serviceFilter">An optional filter for the services.</param>
5768
/// <typeparam name="TService">The base type of the services.</typeparam>
5869
/// <exception cref="InvalidOperationException"></exception>
@@ -61,13 +72,14 @@ public static IServiceCollection AddServiceManagerWithCurrentCollectionServices<
6172
Func<ServiceDescriptor, bool>? serviceFilter = null
6273
)
6374
{
64-
return services.AddSingleton<ServiceManager<TService>>(provider =>
75+
// Register main service manager as singleton
76+
services.AddSingleton<ServiceManager<TService>>(provider =>
6577
{
6678
using var _ = CodeTimer.StartDebug(
6779
callerName: $"{nameof(AddServiceManagerWithCurrentCollectionServices)}<{typeof(TService)}>"
6880
);
6981

70-
var serviceManager = new ServiceManager<TService>();
82+
var serviceManager = new ServiceManager<TService>(provider);
7183

7284
// Get registered services that are assignable to TService
7385
var serviceDescriptors = services.Where(s => s.ServiceType.IsAssignableTo(typeof(TService)));
@@ -84,10 +96,36 @@ public static IServiceCollection AddServiceManagerWithCurrentCollectionServices<
8496
Debug.Assert(type is not null, "type is not null");
8597
Debug.Assert(type.IsAssignableTo(typeof(TService)), "type is assignable to TService");
8698

87-
serviceManager.Register(type, () => (TService)provider.GetRequiredService(type));
99+
if (service.Lifetime == ServiceLifetime.Scoped)
100+
{
101+
serviceManager.RegisterScoped(type, sp => (TService)sp.GetRequiredService(type));
102+
}
103+
else
104+
{
105+
serviceManager.Register(type, () => (TService)provider.GetRequiredService(type));
106+
}
88107
}
89108

90109
return serviceManager;
91110
});
111+
112+
// Register scoped for interface
113+
services.AddScoped<IServiceManager<TService>>(provider =>
114+
{
115+
var rootServiceManager = provider.GetRequiredService<ServiceManager<TService>>();
116+
117+
// For non scoped, just return the singleton
118+
if (!IsScoped(provider))
119+
{
120+
return rootServiceManager;
121+
}
122+
123+
// For scoped, create a new instance using root and provider
124+
var scopedServiceManager = new ScopedServiceManager<TService>(rootServiceManager, provider);
125+
126+
return scopedServiceManager;
127+
});
128+
129+
return services;
92130
}
93131
}

StabilityMatrix.Avalonia/Languages/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)