Signed-off-by: Shawn Yuan (from Dev Box) <shuaiyuan@microsoft.com>
This commit is contained in:
Shawn Yuan (from Dev Box)
2025-11-14 16:14:53 +08:00
parent 4668b16700
commit 013a1763b5
2 changed files with 30 additions and 41 deletions

View File

@@ -168,6 +168,15 @@ namespace AdvancedPaste.Settings
} }
var properties = settings.Properties; var properties = settings.Properties;
bool legacyAdvancedAIConsumed = properties.TryConsumeLegacyAdvancedAIEnabled(out var advancedFlag);
bool legacyAdvancedAIEnabled = legacyAdvancedAIConsumed && advancedFlag;
PasswordCredential legacyCredential = TryGetLegacyOpenAICredential();
if (legacyCredential is null)
{
return legacyAdvancedAIConsumed;
}
var configuration = properties.PasteAIConfiguration; var configuration = properties.PasteAIConfiguration;
if (configuration is null) if (configuration is null)
@@ -176,28 +185,11 @@ namespace AdvancedPaste.Settings
properties.PasteAIConfiguration = configuration; properties.PasteAIConfiguration = configuration;
} }
bool legacyAdvancedAIConsumed = properties.TryConsumeLegacyAdvancedAIEnabled(out var advancedFlag);
bool legacyAdvancedAIEnabled = legacyAdvancedAIConsumed && advancedFlag;
PasswordCredential legacyCredential = TryGetLegacyOpenAICredential();
if (legacyCredential is null && !legacyAdvancedAIConsumed)
{
return false;
}
bool configurationUpdated = false; bool configurationUpdated = false;
const string openAIServiceType = "OpenAI"; var ensureResult = AdvancedPasteMigrationHelper.EnsureOpenAIProvider(configuration);
PasteAIProviderDefinition openAIProvider = configuration?.Providers?.FirstOrDefault( PasteAIProviderDefinition openAIProvider = ensureResult.Provider;
provider => string.Equals(provider.ServiceType, openAIServiceType, StringComparison.OrdinalIgnoreCase)); configurationUpdated |= ensureResult.Updated;
bool shouldEnsureOpenAIProvider = legacyCredential is not null;
if (shouldEnsureOpenAIProvider)
{
var ensureResult = AdvancedPasteMigrationHelper.EnsureOpenAIProvider(configuration);
openAIProvider = ensureResult.Provider;
configurationUpdated |= ensureResult.Updated;
}
if (legacyAdvancedAIConsumed && openAIProvider is not null && openAIProvider.EnableAdvancedAI != legacyAdvancedAIEnabled) if (legacyAdvancedAIConsumed && openAIProvider is not null && openAIProvider.EnableAdvancedAI != legacyAdvancedAIEnabled)
{ {
@@ -205,13 +197,13 @@ namespace AdvancedPaste.Settings
configurationUpdated = true; configurationUpdated = true;
} }
if (legacyCredential is not null && openAIProvider is not null) if (openAIProvider is not null)
{ {
StoreMigratedOpenAICredential(openAIProvider.Id, openAIProvider.ServiceType, legacyCredential.Password); StoreMigratedOpenAICredential(openAIProvider.Id, openAIProvider.ServiceType, legacyCredential.Password);
RemoveLegacyOpenAICredential(); RemoveLegacyOpenAICredential();
} }
bool shouldEnableAI = legacyCredential is not null; const bool shouldEnableAI = true;
bool enabledUpdated = false; bool enabledUpdated = false;
if (properties.IsAIEnabled != shouldEnableAI) if (properties.IsAIEnabled != shouldEnableAI)
{ {

View File

@@ -191,6 +191,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
return; return;
} }
PasswordCredential legacyCredential = TryGetLegacyOpenAICredential();
if (legacyCredential is null)
{
if (legacyAdvancedAIConsumed)
{
SaveAndNotifySettings();
}
return;
}
var configuration = properties.PasteAIConfiguration; var configuration = properties.PasteAIConfiguration;
if (configuration is null) if (configuration is null)
{ {
@@ -198,26 +210,11 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
properties.PasteAIConfiguration = configuration; properties.PasteAIConfiguration = configuration;
} }
PasswordCredential legacyCredential = TryGetLegacyOpenAICredential();
if (legacyCredential is null && !legacyAdvancedAIConsumed)
{
return;
}
bool configurationUpdated = false; bool configurationUpdated = false;
const string openAIServiceType = "OpenAI"; var ensureResult = AdvancedPasteMigrationHelper.EnsureOpenAIProvider(configuration);
PasteAIProviderDefinition openAIProvider = configuration?.Providers?.FirstOrDefault( PasteAIProviderDefinition openAIProvider = ensureResult.Provider;
provider => string.Equals(provider.ServiceType, openAIServiceType, StringComparison.OrdinalIgnoreCase)); configurationUpdated |= ensureResult.Updated;
bool shouldEnsureOpenAIProvider = legacyCredential is not null;
if (shouldEnsureOpenAIProvider)
{
var ensureResult = AdvancedPasteMigrationHelper.EnsureOpenAIProvider(configuration);
openAIProvider = ensureResult.Provider;
configurationUpdated |= ensureResult.Updated;
}
if (legacyAdvancedAIConsumed && openAIProvider is not null && openAIProvider.EnableAdvancedAI != legacyAdvancedAIEnabled) if (legacyAdvancedAIConsumed && openAIProvider is not null && openAIProvider.EnableAdvancedAI != legacyAdvancedAIEnabled)
{ {
@@ -231,7 +228,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
RemoveLegacyOpenAICredential(); RemoveLegacyOpenAICredential();
} }
bool shouldEnableAI = legacyCredential is not null; const bool shouldEnableAI = true;
bool enabledChanged = false; bool enabledChanged = false;
if (properties.IsAIEnabled != shouldEnableAI) if (properties.IsAIEnabled != shouldEnableAI)
{ {