mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 03:07:56 +01:00
Fix AP system prompt textbox display issue (#43486)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This pull request refactors how the system prompt is managed and displayed in the Advanced Paste settings UI. The main improvements center around normalizing the system prompt value, ensuring placeholder prompts are handled consistently, and updating the logic for switching between advanced and simple AI modes. These changes help prevent placeholder text from being mistakenly saved as a custom system prompt and improve maintainability. **System prompt normalization and placeholder handling:** * Added static normalization for both `AdvancedAISystemPrompt` and `SimpleAISystemPrompt`, and introduced logic to clear the system prompt if it matches a placeholder value. This prevents default placeholder text from being persisted as a custom prompt. [[1]](diffhunk://#diff-14126907329c7fcd49dd33bab32283296c7dd68ddc3902163a482a3b3ce58d36R39-R40) [[2]](diffhunk://#diff-14126907329c7fcd49dd33bab32283296c7dd68ddc3902163a482a3b3ce58d36R942-R982) * Refactored event handlers to use the new normalization logic and updated the placeholder switching mechanism when toggling between advanced and simple AI modes. * Ensured system prompt normalization is applied when saving provider configuration, further enforcing consistent prompt handling. **UI improvements:** * Removed hard-coded placeholder text from the XAML, ensuring that the placeholder is now dynamically set based on the current mode and normalized prompt value. * Updated the logic for setting the system prompt textbox placeholder to use the new normalization and mode detection methods, improving clarity and reducing potential user confusion. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [ ] Closes: #xxx - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Signed-off-by: Shawn Yuan (from Dev Box) <shuaiyuan@microsoft.com>
This commit is contained in:
@@ -533,7 +533,7 @@
|
||||
MinHeight="76"
|
||||
HorizontalAlignment="Stretch"
|
||||
AcceptsReturn="True"
|
||||
PlaceholderText="You are tasked with reformatting user's clipboard data. Use the user's instructions, and the content of their clipboard below to edit their clipboard content as they have requested it. Do not output anything else besides the reformatted clipboard content."
|
||||
Header="System prompt"
|
||||
Text="{x:Bind ViewModel.PasteAIProviderDraft.SystemPrompt, Mode=TwoWay}"
|
||||
TextWrapping="Wrap" />
|
||||
<Grid
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
|
||||
private const string AdvancedAISystemPrompt = "You are an agent who is tasked with helping users paste their clipboard data. You have functions available to help you with this task. Call function when necessary to help user finish the transformation task. You never need to ask permission, always try to do as the user asks. The user will only input one message and will not be available for further questions, so try your best. The user will put in a request to format their clipboard data and you will fulfill it. Do not output anything else besides the reformatted clipboard content.";
|
||||
private const string SimpleAISystemPrompt = "You are tasked with reformatting user's clipboard data. Use the user's instructions, and the content of their clipboard below to edit their clipboard content as they have requested it. Do not output anything else besides the reformatted clipboard content.";
|
||||
private static readonly string AdvancedAISystemPromptNormalized = AdvancedAISystemPrompt.Trim();
|
||||
private static readonly string SimpleAISystemPromptNormalized = SimpleAISystemPrompt.Trim();
|
||||
|
||||
private AdvancedPasteViewModel ViewModel { get; set; }
|
||||
|
||||
@@ -804,6 +806,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
return;
|
||||
}
|
||||
|
||||
NormalizeSystemPrompt(draft);
|
||||
string serviceType = draft.ServiceType ?? "OpenAI";
|
||||
string apiKey = PasteAIApiKeyPasswordBox.Password;
|
||||
string trimmedApiKey = apiKey?.Trim() ?? string.Empty;
|
||||
@@ -834,22 +837,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
return;
|
||||
}
|
||||
|
||||
bool isEmptyOrDefault = string.IsNullOrWhiteSpace(draft.SystemPrompt) ||
|
||||
draft.SystemPrompt.Trim() == AdvancedAISystemPrompt.Trim() ||
|
||||
draft.SystemPrompt.Trim() == SimpleAISystemPrompt.Trim();
|
||||
|
||||
if (isEmptyOrDefault)
|
||||
{
|
||||
if (!draft.EnableAdvancedAI)
|
||||
{
|
||||
// Now we'll switch
|
||||
draft.SystemPrompt = AdvancedAISystemPrompt;
|
||||
}
|
||||
else
|
||||
{
|
||||
draft.SystemPrompt = SimpleAISystemPrompt;
|
||||
}
|
||||
}
|
||||
NormalizeSystemPrompt(draft);
|
||||
UpdateSystemPromptPlaceholder();
|
||||
}
|
||||
|
||||
private static bool RequiresApiKeyForService(string serviceType)
|
||||
@@ -950,15 +939,47 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
|
||||
private Visibility GetServicePrivacyVisibility(string serviceType) => HasServicePrivacyLink(serviceType) ? Visibility.Visible : Visibility.Collapsed;
|
||||
|
||||
private void UpdateSystemPromptPlaceholder()
|
||||
private static bool IsPlaceholderSystemPrompt(string prompt)
|
||||
{
|
||||
var draft = ViewModel?.PasteAIProviderDraft;
|
||||
if (draft is null || PasteAISystemPromptTextBox is null)
|
||||
if (string.IsNullOrWhiteSpace(prompt))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
string trimmedPrompt = prompt.Trim();
|
||||
return string.Equals(trimmedPrompt, AdvancedAISystemPromptNormalized, StringComparison.Ordinal)
|
||||
|| string.Equals(trimmedPrompt, SimpleAISystemPromptNormalized, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static void NormalizeSystemPrompt(PasteAIProviderDefinition draft)
|
||||
{
|
||||
if (draft is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PasteAISystemPromptTextBox.PlaceholderText = draft.EnableAdvancedAI
|
||||
if (IsPlaceholderSystemPrompt(draft.SystemPrompt))
|
||||
{
|
||||
draft.SystemPrompt = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateSystemPromptPlaceholder()
|
||||
{
|
||||
var draft = ViewModel?.PasteAIProviderDraft;
|
||||
if (draft is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NormalizeSystemPrompt(draft);
|
||||
if (PasteAISystemPromptTextBox is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool useAdvancedPlaceholder = PasteAIEnableAdvancedAICheckBox?.IsOn ?? draft.EnableAdvancedAI;
|
||||
PasteAISystemPromptTextBox.PlaceholderText = useAdvancedPlaceholder
|
||||
? AdvancedAISystemPrompt
|
||||
: SimpleAISystemPrompt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user