Skip to content

Commit

Permalink
[AdvancedPaste] Semantic Kernel support
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbyperpetual committed Oct 9, 2024
1 parent 7d8a57c commit 737e179
Show file tree
Hide file tree
Showing 27 changed files with 722 additions and 436 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Appium.WebDriver" Version="4.4.5" />
<PackageVersion Include="Azure.AI.OpenAI" Version="1.0.0-beta.12" />
<PackageVersion Include="Azure.AI.OpenAI" Version="1.0.0-beta.17" />
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageVersion Include="CommunityToolkit.WinUI.Animations" Version="8.0.240109" />
<PackageVersion Include="CommunityToolkit.WinUI.Collections" Version="8.0.240109" />
Expand All @@ -31,6 +31,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.15.0" />
<PackageVersion Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
<PackageVersion Include="Microsoft.Web.WebView2" Version="1.0.2739.15" />
<!-- Package Microsoft.Win32.SystemEvents added as a hack for being able to exclude the runtime assets so they don't conflict with 8.0.1. This is a dependency of System.Drawing.Common but the 8.0.1 version wasn't published to nuget. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<PackageReference Include="CommunityToolkit.WinUI.Extensions" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Primitives" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.SemanticKernel" />
<PackageReference Include="Microsoft.WindowsAppSDK" />
<PackageReference Include="Microsoft.Windows.CsWin32" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ public App()
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().UseContentRoot(AppContext.BaseDirectory).ConfigureServices((context, services) =>
{
services.AddSingleton<IUserSettings, UserSettings>();
services.AddSingleton<AICompletionsHelper>();
services.AddSingleton<OptionsViewModel>();
services.AddSingleton<IAICredentialsProvider, Services.OpenAI.CredentialsProvider>();
services.AddSingleton<ICustomTextTransformService, Services.OpenAI.CustomTextTransformService>();
services.AddSingleton<IKernelService, Services.OpenAI.KernelService>();
services.AddSingleton<IPasteFormatExecutor, PasteFormatExecutor>();
services.AddSingleton<OptionsViewModel>();
}).Build();

viewModel = GetService<OptionsViewModel>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ private async void InputTxtBox_KeyDown(object sender, Microsoft.UI.Xaml.Input.Ke
}
}

private void PreviewPasteBtn_Click(object sender, RoutedEventArgs e)
private async void PreviewPasteBtn_Click(object sender, RoutedEventArgs e)
{
ViewModel.PasteCustom();
await ViewModel.PasteCustomAsync();
}

private void ThumbUpDown_Click(object sender, RoutedEventArgs e)
Expand Down
142 changes: 0 additions & 142 deletions src/modules/AdvancedPaste/AdvancedPaste/Helpers/AICompletionsHelper.cs

This file was deleted.

60 changes: 48 additions & 12 deletions src/modules/AdvancedPaste/AdvancedPaste/Helpers/ClipboardHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using AdvancedPaste.Models;
Expand Down Expand Up @@ -41,13 +42,55 @@ internal static async Task<ClipboardFormat> GetAvailableClipboardFormatsAsync(Da

if (storageItems.Count == 1 && storageItems.Single() is StorageFile file && ImageFileTypes.Contains(file.FileType))
{
availableClipboardFormats |= ClipboardFormat.ImageFile;
availableClipboardFormats |= ClipboardFormat.Image;
}

if (availableClipboardFormats == ClipboardFormat.None)
{
// Advertise the "generic" File format only if there is no other specific format available; confusing for AI otherwise.
availableClipboardFormats |= ClipboardFormat.File;
}
}

return availableClipboardFormats;
}

internal static async Task<bool> HasDataAsync(DataPackageView clipboardData)
{
var availableFormats = await GetAvailableClipboardFormatsAsync(clipboardData);

return availableFormats == ClipboardFormat.Text ? !string.IsNullOrEmpty(await clipboardData.GetTextAsync()) : availableFormats != ClipboardFormat.None;
}

internal static async Task TryCopyPasteDataPackageAsync(DataPackage dataPackage, Action onCopied)
{
Logger.LogTrace();

if (await HasDataAsync(dataPackage.GetView()))
{
Clipboard.SetContent(dataPackage);
await FlushAsync();
onCopied();
SendPasteKeyCombination();
}
}

internal static DataPackage CreateDataPackageFromText(string text)
{
DataPackage dataPackage = new();
dataPackage.SetText(text);
return dataPackage;
}

internal static async Task<DataPackage> CreateDataPackageFromFileContentAsync(string fileName)
{
var storageFile = await StorageFile.GetFileFromPathAsync(fileName);

DataPackage dataPackage = new();
dataPackage.SetStorageItems([storageFile]);
return dataPackage;
}

internal static void SetClipboardTextContent(string text)
{
Logger.LogTrace();
Expand All @@ -71,7 +114,7 @@ private static bool Flush()
{
try
{
Task.Run(Clipboard.Flush).Wait();
Clipboard.Flush();
return true;
}
catch (Exception ex)
Expand All @@ -86,17 +129,10 @@ private static bool Flush()
return false;
}

private static async Task<bool> FlushAsync() => await Task.Run(Flush);

internal static async Task SetClipboardFileContentAsync(string fileName)
private static async Task<bool> FlushAsync()
{
var storageFile = await StorageFile.GetFileFromPathAsync(fileName);

DataPackage output = new();
output.SetStorageItems([storageFile]);
Clipboard.SetContent(output);

await FlushAsync();
// This should run on the UI thread to avoid the "calling application is not the owner of the data on the clipboard" error.
return await Task.Factory.StartNew(Flush, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}

internal static void SetClipboardImageContent(RandomAccessStreamReference image)
Expand Down
19 changes: 19 additions & 0 deletions src/modules/AdvancedPaste/AdvancedPaste/Helpers/ErrorHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using System.Net;

namespace AdvancedPaste.Helpers;

public static class ErrorHelpers
{
public static string TranslateErrorText(int apiRequestStatus) => (HttpStatusCode)apiRequestStatus switch
{
HttpStatusCode.TooManyRequests => ResourceLoaderInstance.ResourceLoader.GetString("OpenAIApiKeyTooManyRequests"),
HttpStatusCode.Unauthorized => ResourceLoaderInstance.ResourceLoader.GetString("OpenAIApiKeyUnauthorized"),
HttpStatusCode.OK => string.Empty,
_ => ResourceLoaderInstance.ResourceLoader.GetString("OpenAIApiKeyError") + apiRequestStatus.ToString(CultureInfo.InvariantCulture),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public interface IUserSettings
{
public bool ShowCustomPreview { get; }

public bool SendPasteKeyCombination { get; }

public bool CloseAfterLosingFocus { get; }

public IReadOnlyList<AdvancedPasteCustomAction> CustomActions { get; }
Expand Down
Loading

1 comment on commit 737e179

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (4)

IAI
ICompletions
reponse
SKEXP

Previously acknowledged words that are now absent applayout appsfolder buildtask cswinrt directshow DOPUS GBarm netcore nugets QDir SYSTEMSETTINGS SYSTEMWOW telem TOTALCMD USEPOSITION USESIZE winappdriver xplorer 🫥
To accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:microsoft/PowerToys.git repository
on the dev/ani/advanced-paste-semantic-kernel branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.22/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/11260582896/attempts/1'
Available 📚 dictionaries could cover words (expected and unrecognized) not in the 📘 dictionary

This includes both expected items (1931) from .github/actions/spell-check/expect.txt and unrecognized words (4)

Dictionary Entries Covers Uniquely
cspell:r/src/r.txt 543 1 1
cspell:cpp/src/people.txt 23 1
cspell:cpp/src/ecosystem.txt 51 1

Consider adding them (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

      with:
        extra_dictionaries:
          cspell:r/src/r.txt
          cspell:cpp/src/people.txt
          cspell:cpp/src/ecosystem.txt

To stop checking additional dictionaries, add (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

check_extra_dictionaries: ''
Errors (3)

See the 📜action log or 📝 job summary for details.

❌ Errors Count
❌ check-file-path 1
❌ ignored-expect-variant 1
ℹ️ non-alpha-in-dictionary 1

See ❌ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.