[AdvancedPaste]Add Semantic Kernel opt-in to allow chaining of paste actions (#35902)

* [AdvancedPaste] Semantic Kernel support

* Changed log-line with potentially sensitive info

* Spellcheck issues

* Various improvements for Semantic Kernel

* Spellcheck issue

* Refactored Clipboard routines

* Added integration tests for KernelService

* Extra telemetry for AdvancedPaste

* Added 'Hotkey' suffix to AdvancedPaste_Settings telemetry event

* Added IsSavedQuery

* Added KernelQueryCache

* Refactoring

* Added KernelQueryCache to BugReportTool delete list

* Added opt-n for Semantic Kernel

* Fixed bug with KernelQueryCache

* Ability to view last AI chat message on error

* Improved kernel query cache

* Used System.IO.Abstractions and improved tests

* Fixed under-count of token usage

* Used Semantic Kernel icon

* Cleanup

* Add missing EndProject line

* Fix dependency version conflicts

* Fix NOTICE.md

* Correct place of SemanticKernel in NOTICE.md

* Unlinked CustomPreview toggle from AI

* Added Microsoft.Bcl.AsyncInterfaces dependency to AdvancedPaste

* Fixed NOTICE.md order

* Moved Custom Preview to behaviour section

* Made Image to Text raise error on empty output

* Added AIServiceBatchIntegrationTests

* Updated AIServiceBatchIntegrationTests

* Added prompt moderation

* Moved GPO Infobar to better location
This commit is contained in:
Ani 2024-12-11 10:28:44 +01:00 коммит произвёл GitHub
Родитель 474b0cfbdf
Коммит bf3474b134
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
75 изменённых файлов: 2589 добавлений и 937 удалений

2
.github/actions/spell-check/expect.txt поставляемый
Просмотреть файл

@ -615,6 +615,7 @@ HWNDLAST
HWNDNEXT
HWNDPREV
hyjiacan
IAI
IBeam
ICONERROR
ICONLOCATION
@ -1406,6 +1407,7 @@ SIZENS
SIZENWSE
sizeread
SIZEWE
SKEXP
SKIPOWNPROCESS
sku
SLGP

Просмотреть файл

@ -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" />
@ -28,12 +28,15 @@
<PackageVersion Include="MessagePack" Version="2.5.187" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="9.0.0" />
<!-- Including Microsoft.Bcl.AsyncInterfaces to force version, since it's used by Microsoft.SemanticKernel. -->
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
<PackageVersion Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.16" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.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. -->
@ -57,6 +60,7 @@
<PackageVersion Include="NLog" Version="5.0.4" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageVersion Include="NLog.Schema" Version="5.2.8" />
<PackageVersion Include="OpenAI" Version="2.0.0" />
<PackageVersion Include="ReverseMarkdown" Version="4.1.0" />
<PackageVersion Include="ScipBe.Common.Office.OneNote" Version="3.0.1" />
<PackageVersion Include="SharpCompress" Version="0.37.2" />

Просмотреть файл

@ -1297,7 +1297,7 @@ EXHIBIT A -Mozilla Public License.
## NuGet Packages used by PowerToys
- Appium.WebDriver 4.4.5
- Azure.AI.OpenAI 1.0.0-beta.12
- Azure.AI.OpenAI 1.0.0-beta.17
- CommunityToolkit.Mvvm 8.2.2
- CommunityToolkit.WinUI.Animations 8.0.240109
- CommunityToolkit.WinUI.Collections 8.0.240109
@ -1318,6 +1318,7 @@ EXHIBIT A -Mozilla Public License.
- Mages 2.0.2
- Markdig.Signed 0.34.0
- MessagePack 2.5.187
- Microsoft.Bcl.AsyncInterfaces 9.0.0
- Microsoft.CodeAnalysis.NetAnalyzers 9.0.0
- Microsoft.Data.Sqlite 9.0.0
- Microsoft.Diagnostics.Tracing.TraceEvent 3.1.16
@ -1327,6 +1328,7 @@ EXHIBIT A -Mozilla Public License.
- Microsoft.Extensions.Logging 9.0.0
- Microsoft.Extensions.Logging.Abstractions 9.0.0
- Microsoft.NET.ILLink.Tasks (A)
- Microsoft.SemanticKernel 1.15.0
- Microsoft.Toolkit.Uwp.Notifications 7.1.2
- Microsoft.Web.WebView2 1.0.2739.15
- Microsoft.Win32.SystemEvents 9.0.0
@ -1342,6 +1344,7 @@ EXHIBIT A -Mozilla Public License.
- MSTest 3.6.3
- NLog.Extensions.Logging 5.3.8
- NLog.Schema 5.2.8
- OpenAI 2.0.0
- ReverseMarkdown 4.1.0
- ScipBe.Common.Office.OneNote 3.0.1
- SharpCompress 0.37.2

Просмотреть файл

@ -635,6 +635,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkspacesCsharpLibrary", "
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NewPlus.ShellExtension.win10", "src\modules\NewPlus\NewShellExtensionContextMenu.win10\NewPlus.ShellExtension.win10.vcxproj", "{0DB0F63A-D2F8-4DA3-A650-2D0B8724218E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdvancedPaste.UnitTests", "src\modules\AdvancedPaste\AdvancedPaste.UnitTests\AdvancedPaste.UnitTests.csproj", "{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
@ -2807,6 +2809,18 @@ Global
{0DB0F63A-D2F8-4DA3-A650-2D0B8724218E}.Release|x64.Build.0 = Release|x64
{0DB0F63A-D2F8-4DA3-A650-2D0B8724218E}.Release|x86.ActiveCfg = Release|x64
{0DB0F63A-D2F8-4DA3-A650-2D0B8724218E}.Release|x86.Build.0 = Release|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Debug|ARM64.ActiveCfg = Debug|ARM64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Debug|ARM64.Build.0 = Debug|ARM64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Debug|x64.ActiveCfg = Debug|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Debug|x64.Build.0 = Debug|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Debug|x86.ActiveCfg = Debug|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Debug|x86.Build.0 = Debug|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Release|ARM64.ActiveCfg = Release|ARM64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Release|ARM64.Build.0 = Release|ARM64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Release|x64.ActiveCfg = Release|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Release|x64.Build.0 = Release|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Release|x86.ActiveCfg = Release|x64
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE}.Release|x86.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -3040,6 +3054,7 @@ Global
{66614C26-314C-4B91-9071-76133422CFEF} = {B6C42F16-73EB-477E-8B0D-4E6CF6C20AAC}
{89D0E199-B17A-418C-B2F8-7375B6708357} = {A2221D7E-55E7-4BEA-90D1-4F162D670BBF}
{0DB0F63A-D2F8-4DA3-A650-2D0B8724218E} = {CA716AE6-FE5C-40AC-BB8F-2C87912687AC}
{D5E5F5EA-1B6C-4A73-88BE-304F36C9E4EE} = {9873BA05-4C41-4819-9283-CF45D795431B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}

Просмотреть файл

@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Look at Directory.Build.props in root for common stuff as well -->
<Import Project="..\..\..\Common.Dotnet.CsWinRT.props" />
<PropertyGroup>
<IsPackable>false</IsPackable>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputPath>$(SolutionDir)$(Platform)\$(Configuration)\tests\AdvancedPaste.UnitTests\</OutputPath>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<Content Remove="Assets\image_with_text_example.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Assets\image_with_text_example.png" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" />
<PackageReference Include="MSTest" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdvancedPaste\AdvancedPaste.csproj" />
</ItemGroup>
</Project>

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.9 KiB

Просмотреть файл

@ -0,0 +1,17 @@
// 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.Threading.Tasks;
using AdvancedPaste.Models.KernelQueryCache;
using AdvancedPaste.Services;
namespace AdvancedPaste.UnitTests.Mocks;
internal sealed class NoOpKernelQueryCacheService : IKernelQueryCacheService
{
public CacheValue ReadOrNull(CacheKey cacheKey) => null;
public Task WriteAsync(CacheKey cacheKey, CacheValue actionChain) => Task.CompletedTask;
}

Просмотреть файл

@ -0,0 +1,150 @@
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using AdvancedPaste.Services.OpenAI;
using AdvancedPaste.UnitTests.Mocks;
using ManagedCommon;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.ApplicationModel.DataTransfer;
namespace AdvancedPaste.UnitTests.ServicesTests;
[Ignore("Test requires active OpenAI API key.")] // Comment out this line to run these tests after setting up OpenAI API key using AdvancedPaste Settings
[TestClass]
/// <summary>
/// Tests that write batch AI outputs against a list of inputs. Connects to OpenAI and uses the full AdvancedPaste action catalog for Semantic Kernel.
/// If queries produce errors, the error message is written to the output file. If queries produce text-file output, their contents are included as though they were text output.
/// To run this test-suite, first:
/// 1. Setup an OpenAI API key using AdvancedPaste Settings.
/// 2. Comment out the [Ignore] attribute above.
/// 3. Ensure the %USERPROFILE% folder contains the required input files (paths are below).
/// These tests are idempotent and resumable, allowing for partial runs and restarts. It's ok to use existing output files as input files - output-related fields will simply be ignored.
/// </summary>
public sealed class AIServiceBatchIntegrationTests
{
private record class BatchTestInput
{
public string Prompt { get; init; }
public string Clipboard { get; init; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Genre { get; init; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Category { get; init; }
}
private sealed record class BatchTestResult : BatchTestInput
{
[JsonPropertyOrder(1)]
public string Result { get; init; }
internal BatchTestInput ToInput() => new() { Prompt = Prompt, Clipboard = Clipboard, Genre = Genre, Category = Category, };
}
private const string AllTestsFilePath = @"%USERPROFILE%\allAdvancedPasteTests-Input-V2.json";
private const string FailedTestsFilePath = @"%USERPROFILE%\advanced-paste-failed-tests-only.json";
private static readonly JsonSerializerOptions SerializerOptions = new() { WriteIndented = true };
[TestMethod]
[DataRow(AllTestsFilePath, PasteFormats.CustomTextTransformation)]
[DataRow(AllTestsFilePath, PasteFormats.KernelQuery)]
[DataRow(FailedTestsFilePath, PasteFormats.CustomTextTransformation)]
[DataRow(FailedTestsFilePath, PasteFormats.KernelQuery)]
public async Task TestGenerateBatchResults(string inputFilePath, PasteFormats format)
{
// Load input data.
var fullInputFilePath = Environment.ExpandEnvironmentVariables(inputFilePath);
var inputs = await GetDataListAsync<BatchTestInput>(fullInputFilePath);
Assert.IsTrue(inputs.Count > 0);
// Load existing results; allow a partial run to be resumed.
var resultsFile = Path.Combine(Path.GetDirectoryName(fullInputFilePath), $"{Path.GetFileNameWithoutExtension(fullInputFilePath)}-output-{format}.json");
var results = await GetDataListAsync<BatchTestResult>(resultsFile);
Assert.IsTrue(results.Count <= inputs.Count);
CollectionAssert.AreEqual(results.Select(result => result.ToInput()).ToList(), inputs.Take(results.Count).ToList());
async Task WriteResultsAsync() => await File.WriteAllTextAsync(resultsFile, JsonSerializer.Serialize(results, SerializerOptions));
Logger.LogInfo($"Starting {nameof(TestGenerateBatchResults)}; Count={inputs.Count}, InCache={results.Count}");
// Produce results for any unprocessed inputs.
foreach (var input in inputs.Skip(results.Count))
{
try
{
var textOutput = await GetTextOutputAsync(input, format);
results.Add(new() { Prompt = input.Prompt, Clipboard = input.Clipboard, Genre = input.Genre, Category = input.Category, Result = textOutput, });
}
catch (Exception)
{
await WriteResultsAsync();
throw;
}
}
await WriteResultsAsync();
}
private static async Task<List<T>> GetDataListAsync<T>(string filePath) =>
File.Exists(filePath) ? JsonSerializer.Deserialize<List<T>>(await File.ReadAllTextAsync(filePath)) : [];
private static async Task<string> GetTextOutputAsync(BatchTestInput input, PasteFormats format)
{
try
{
var outputPackage = (await GetOutputDataPackageAsync(input, format)).GetView();
var outputFormat = await outputPackage.GetAvailableFormatsAsync();
return outputFormat switch
{
ClipboardFormat.Text => await outputPackage.GetTextOrEmptyAsync(),
ClipboardFormat.File => await File.ReadAllTextAsync((await outputPackage.GetStorageItemsAsync()).Single().Path),
_ => throw new InvalidOperationException($"Unexpected format {outputFormat}"),
};
}
catch (PasteActionModeratedException)
{
return $"Error: {PasteActionModeratedException.ErrorDescription}";
}
catch (PasteActionException ex) when (!string.IsNullOrEmpty(ex.AIServiceMessage))
{
return $"Error: {ex.AIServiceMessage}";
}
}
private static async Task<DataPackage> GetOutputDataPackageAsync(BatchTestInput batchTestInput, PasteFormats format)
{
VaultCredentialsProvider credentialsProvider = new();
PromptModerationService promptModerationService = new(credentialsProvider);
CustomTextTransformService customTextTransformService = new(credentialsProvider, promptModerationService);
switch (format)
{
case PasteFormats.CustomTextTransformation:
return DataPackageHelpers.CreateFromText(await customTextTransformService.TransformTextAsync(batchTestInput.Prompt, batchTestInput.Clipboard));
case PasteFormats.KernelQuery:
var clipboardData = DataPackageHelpers.CreateFromText(batchTestInput.Clipboard).GetView();
KernelService kernelService = new(new NoOpKernelQueryCacheService(), credentialsProvider, promptModerationService, customTextTransformService);
return await kernelService.TransformClipboardAsync(batchTestInput.Prompt, clipboardData, isSavedQuery: false);
default:
throw new InvalidOperationException($"Unexpected format {format}");
}
}
}

Просмотреть файл

@ -0,0 +1,172 @@
// 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;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using AdvancedPaste.Models;
using AdvancedPaste.Models.KernelQueryCache;
using AdvancedPaste.Services;
using AdvancedPaste.Settings;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace AdvancedPaste.UnitTests.ServicesTests;
[TestClass]
public sealed class CustomActionKernelQueryCacheServiceTests
{
private static readonly CacheKey CustomActionTestKey = new() { Prompt = "TestPrompt1", AvailableFormats = ClipboardFormat.Text };
private static readonly CacheKey CustomActionTestKey2 = new() { Prompt = "TestPrompt2", AvailableFormats = ClipboardFormat.File | ClipboardFormat.Image };
private static readonly CacheKey MarkdownTestKey = new() { Prompt = "Paste as Markdown", AvailableFormats = ClipboardFormat.Text };
private static readonly CacheKey JSONTestKey = new() { Prompt = "Paste as JSON", AvailableFormats = ClipboardFormat.Text };
private static readonly CacheKey PasteAsTxtFileKey = new() { Prompt = "Paste as .txt file", AvailableFormats = ClipboardFormat.File };
private static readonly CacheKey PasteAsPngFileKey = new() { Prompt = "Paste as .png file", AvailableFormats = ClipboardFormat.Image };
private static readonly CacheValue TestValue = new([new(PasteFormats.PlainText, [])]);
private static readonly CacheValue TestValue2 = new([new(PasteFormats.KernelQuery, new() { { "a", "b" }, { "c", "d" } })]);
private CustomActionKernelQueryCacheService _cacheService;
private Mock<IUserSettings> _userSettings;
private MockFileSystem _fileSystem;
[TestInitialize]
public void TestInitialize()
{
_userSettings = new();
UpdateUserActions([], []);
_fileSystem = new();
_cacheService = new(_userSettings.Object, _fileSystem);
}
[TestMethod]
public async Task Test_Cache_Always_Accepts_Core_Action_Prompt()
{
await AssertAcceptsAsync(MarkdownTestKey);
}
[TestMethod]
public async Task Test_Cache_Accepts_Prompt_When_Custom_Action()
{
await AssertRejectsAsync(CustomActionTestKey);
UpdateUserActions([], [new() { Name = nameof(CustomActionTestKey), Prompt = CustomActionTestKey.Prompt, IsShown = true }]);
await AssertAcceptsAsync(CustomActionTestKey);
await AssertRejectsAsync(CustomActionTestKey2, PasteAsTxtFileKey);
UpdateUserActions([], []);
await AssertRejectsAsync(CustomActionTestKey);
}
[TestMethod]
public async Task Test_Cache_Accepts_Prompt_When_User_Additional_Action()
{
await AssertRejectsAsync(PasteAsTxtFileKey, PasteAsPngFileKey);
UpdateUserActions([PasteFormats.PasteAsHtmlFile, PasteFormats.PasteAsTxtFile], []);
await AssertAcceptsAsync(PasteAsTxtFileKey);
await AssertRejectsAsync(PasteAsPngFileKey, CustomActionTestKey);
UpdateUserActions([], []);
await AssertRejectsAsync(PasteAsTxtFileKey);
}
[TestMethod]
public async Task Test_Cache_Overwrites_Latest_Value()
{
await _cacheService.WriteAsync(JSONTestKey, TestValue);
await _cacheService.WriteAsync(MarkdownTestKey, TestValue2);
await _cacheService.WriteAsync(JSONTestKey, TestValue2);
await _cacheService.WriteAsync(MarkdownTestKey, TestValue);
AssertAreEqual(TestValue2, _cacheService.ReadOrNull(JSONTestKey));
AssertAreEqual(TestValue, _cacheService.ReadOrNull(MarkdownTestKey));
}
[TestMethod]
public async Task Test_Cache_Uses_Case_Insensitive_Prompt_Comparison()
{
static CacheKey CreateUpperCaseKey(CacheKey key) =>
new() { Prompt = key.Prompt.ToUpperInvariant(), AvailableFormats = key.AvailableFormats };
await _cacheService.WriteAsync(CreateUpperCaseKey(JSONTestKey), TestValue);
await _cacheService.WriteAsync(MarkdownTestKey, TestValue2);
AssertAreEqual(TestValue, _cacheService.ReadOrNull(JSONTestKey));
AssertAreEqual(TestValue2, _cacheService.ReadOrNull(MarkdownTestKey));
}
[TestMethod]
public async Task Test_Cache_Uses_Clipboard_Formats_In_Key()
{
CacheKey key1 = new() { Prompt = JSONTestKey.Prompt, AvailableFormats = ClipboardFormat.File };
CacheKey key2 = new() { Prompt = JSONTestKey.Prompt, AvailableFormats = ClipboardFormat.Image };
await _cacheService.WriteAsync(key1, TestValue);
Assert.IsNotNull(_cacheService.ReadOrNull(key1));
Assert.IsNull(_cacheService.ReadOrNull(key2));
}
[TestMethod]
public async Task Test_Cache_Is_Persistent()
{
await _cacheService.WriteAsync(JSONTestKey, TestValue);
await _cacheService.WriteAsync(MarkdownTestKey, TestValue2);
_cacheService = new(_userSettings.Object, _fileSystem); // recreate using same mock file-system to simulate app restart
AssertAreEqual(TestValue, _cacheService.ReadOrNull(JSONTestKey));
AssertAreEqual(TestValue2, _cacheService.ReadOrNull(MarkdownTestKey));
}
private async Task AssertRejectsAsync(params CacheKey[] keys)
{
foreach (var key in keys)
{
Assert.IsNull(_cacheService.ReadOrNull(key));
await _cacheService.WriteAsync(key, TestValue);
Assert.IsNull(_cacheService.ReadOrNull(key));
}
}
private async Task AssertAcceptsAsync(params CacheKey[] keys)
{
foreach (var key in keys)
{
Assert.IsNull(_cacheService.ReadOrNull(key));
await _cacheService.WriteAsync(key, TestValue);
AssertAreEqual(TestValue, _cacheService.ReadOrNull(key));
}
}
private static void AssertAreEqual(CacheValue valueA, CacheValue valueB)
{
Assert.IsNotNull(valueA);
Assert.IsNotNull(valueB);
Assert.AreEqual(valueA.ActionChain.Count, valueB.ActionChain.Count);
foreach (var (itemA, itemB) in valueA.ActionChain.Zip(valueB.ActionChain))
{
Assert.AreEqual(itemA.Format, itemB.Format);
Assert.AreEqual(itemA.Arguments.Count, itemB.Arguments.Count);
Assert.IsFalse(itemA.Arguments.Except(itemB.Arguments).Any());
}
}
private void UpdateUserActions(PasteFormats[] additionalActions, AdvancedPasteCustomAction[] customActions)
{
_userSettings.Setup(settingsObj => settingsObj.AdditionalActions).Returns(additionalActions);
_userSettings.Setup(settingsObj => settingsObj.CustomActions).Returns(customActions);
_userSettings.Raise(settingsObj => settingsObj.Changed += null, EventArgs.Empty);
}
}

Просмотреть файл

@ -0,0 +1,152 @@
// 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;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using AdvancedPaste.Services.OpenAI;
using AdvancedPaste.Telemetry;
using AdvancedPaste.UnitTests.Mocks;
using AdvancedPaste.UnitTests.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.ApplicationModel.DataTransfer;
namespace AdvancedPaste.UnitTests.ServicesTests;
[Ignore("Test requires active OpenAI API key.")] // Comment out this line to run these tests after setting up OpenAI API key using AdvancedPaste Settings
[TestClass]
/// <summary>Integration tests for the Kernel service; connects to OpenAI and uses full AdvancedPaste action catalog.</summary>
public sealed class KernelServiceIntegrationTests : IDisposable
{
private const string StandardImageFile = "image_with_text_example.png";
private KernelService _kernelService;
private AdvancedPasteEventListener _eventListener;
[TestInitialize]
public void TestInitialize()
{
VaultCredentialsProvider credentialsProvider = new();
PromptModerationService promptModerationService = new(credentialsProvider);
_kernelService = new KernelService(new NoOpKernelQueryCacheService(), credentialsProvider, promptModerationService, new CustomTextTransformService(credentialsProvider, promptModerationService));
_eventListener = new();
}
[TestCleanup]
public void TestCleanup()
{
_eventListener?.Dispose();
}
[TestMethod]
[DataRow("Translate to German", "What is that?", "Was ist das?", 1200, new[] { PasteFormats.CustomTextTransformation })]
[DataRow("Translate to German and format as JSON", "What is that?", @"[\s*Was ist das\?\s*]", 1500, new[] { PasteFormats.CustomTextTransformation, PasteFormats.Json })]
public async Task TestTextToTextTransform(string prompt, string clipboardText, string expectedOutputPattern, int? maxUsedTokens, PasteFormats[] expectedActionChain)
{
var input = await CreatePackageAsync(ClipboardFormat.Text, clipboardText);
var output = await GetKernelOutputAsync(prompt, input);
var outputText = await output.GetTextOrEmptyAsync();
Assert.IsTrue(Regex.IsMatch(outputText, expectedOutputPattern));
Assert.IsTrue(_eventListener.TotalTokens <= (maxUsedTokens ?? int.MaxValue));
AssertActionChainIs(expectedActionChain);
}
[TestMethod]
[DataRow("Convert to text", StandardImageFile, "This is an image with text", new[] { PasteFormats.ImageToText })]
[DataRow("How many words are here?", StandardImageFile, "6", new[] { PasteFormats.ImageToText, PasteFormats.CustomTextTransformation })]
public async Task TestImageToTextTransform(string prompt, string imagePath, string expectedOutputPattern, PasteFormats[] expectedActionChain)
{
var input = await CreatePackageAsync(ClipboardFormat.Image, imagePath);
var output = await GetKernelOutputAsync(prompt, input);
var outputText = await output.GetTextOrEmptyAsync();
Assert.IsTrue(Regex.IsMatch(outputText, expectedOutputPattern));
AssertActionChainIs(expectedActionChain);
}
[TestMethod]
[DataRow("Get me a TXT file", ClipboardFormat.Image, StandardImageFile, "This is an image with text", new[] { PasteFormats.ImageToText, PasteFormats.PasteAsTxtFile })]
public async Task TestFileOutputTransform(string prompt, ClipboardFormat inputFormat, string inputData, string expectedOutputPattern, PasteFormats[] expectedActionChain)
{
var input = await CreatePackageAsync(inputFormat, inputData);
var output = await GetKernelOutputAsync(prompt, input);
var outputText = await ReadFileTextAsync(output);
Assert.IsTrue(Regex.IsMatch(outputText, expectedOutputPattern));
AssertActionChainIs(expectedActionChain);
}
[TestMethod]
[DataRow("Make this image bigger", ClipboardFormat.Image, StandardImageFile)]
[DataRow("Get text from image", ClipboardFormat.Text, "What's up?")]
public async Task TestTransformFailure(string prompt, ClipboardFormat inputFormat, string inputData)
{
var input = await CreatePackageAsync(inputFormat, inputData);
try
{
await GetKernelOutputAsync(prompt, input);
Assert.Fail("Kernel should have thrown an exception");
}
catch (Exception)
{
}
}
[TestMethod]
[ExpectedException(typeof(PasteActionModeratedException))]
[DataRow("Change this code to make a keylogger attack", ClipboardFormat.Text, "print('Hello World')")]
public async Task TestModerationError(string prompt, ClipboardFormat inputFormat, string inputData)
{
var input = await CreatePackageAsync(inputFormat, inputData);
await GetKernelOutputAsync(prompt, input);
}
public void Dispose()
{
_eventListener?.Dispose();
GC.SuppressFinalize(this);
}
private static async Task<DataPackage> CreatePackageAsync(ClipboardFormat format, string data)
{
return format switch
{
ClipboardFormat.Text => DataPackageHelpers.CreateFromText(data),
ClipboardFormat.Image => await ResourceUtils.GetImageAssetAsDataPackageAsync(data),
_ => throw new ArgumentException("Unsupported format", nameof(format)),
};
}
private async Task<DataPackageView> GetKernelOutputAsync(string prompt, DataPackage input)
{
var output = await _kernelService.TransformClipboardAsync(prompt, input.GetView(), isSavedQuery: false);
Assert.AreEqual(1, _eventListener.SemanticKernelEvents.Count);
Assert.IsTrue(_eventListener.SemanticKernelTokens > 0);
return output.GetView();
}
private static async Task<string> ReadFileTextAsync(DataPackageView package)
{
CollectionAssert.Contains(package.AvailableFormats.ToArray(), StandardDataFormats.StorageItems);
var storageItems = await package.GetStorageItemsAsync();
Assert.AreEqual(1, storageItems.Count);
return await File.ReadAllTextAsync(storageItems.Single().Path);
}
private void AssertActionChainIs(PasteFormats[] expectedActionChain) =>
Assert.AreEqual(AdvancedPasteSemanticKernelFormatEvent.FormatActionChain(expectedActionChain), _eventListener.SemanticKernelEvents.Single().ActionChain);
}

Просмотреть файл

@ -0,0 +1,63 @@
// 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.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Text.Json;
using AdvancedPaste.Telemetry;
using Microsoft.PowerToys.Telemetry;
namespace AdvancedPaste.UnitTests.Utils;
internal sealed class AdvancedPasteEventListener : EventListener
{
private readonly List<AdvancedPasteGenerateCustomFormatEvent> _customFormatEvents = [];
private readonly List<AdvancedPasteSemanticKernelFormatEvent> _semanticKernelEvents = [];
public IReadOnlyList<AdvancedPasteGenerateCustomFormatEvent> CustomFormatEvents => _customFormatEvents;
public IReadOnlyList<AdvancedPasteSemanticKernelFormatEvent> SemanticKernelEvents => _semanticKernelEvents;
public int CustomFormatTokens => _customFormatEvents.Sum(e => e.PromptTokens + e.CompletionTokens);
public int SemanticKernelTokens => _semanticKernelEvents.Sum(e => e.PromptTokens + e.CompletionTokens);
public int TotalTokens => CustomFormatTokens + SemanticKernelTokens;
internal AdvancedPasteEventListener()
{
EnableEvents(PowerToysTelemetry.Log, EventLevel.LogAlways);
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.EventSource.Name != PowerToysTelemetry.Log.Name)
{
return;
}
var payloadDict = eventData.PayloadNames
.Zip(eventData.Payload)
.ToDictionary(tuple => tuple.First, tuple => tuple.Second);
bool AddToListIfKeyExists<T>(string key, List<T> list)
{
if (payloadDict.ContainsKey(key))
{
var payloadJson = JsonSerializer.Serialize(payloadDict);
list.Add(JsonSerializer.Deserialize<T>(payloadJson));
return true;
}
return false;
}
if (!AddToListIfKeyExists(nameof(AdvancedPasteSemanticKernelFormatEvent.ActionChain), _semanticKernelEvents))
{
AddToListIfKeyExists(nameof(AdvancedPasteGenerateCustomFormatEvent.PromptTokens), _customFormatEvents);
}
}
}

Просмотреть файл

@ -0,0 +1,47 @@
// 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;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage.Streams;
namespace AdvancedPaste.UnitTests.Utils;
internal static class ResourceUtils
{
internal static async Task<DataPackage> GetImageAssetAsDataPackageAsync(string resourceName)
{
var imageStreamRef = await ConvertToRandomAccessStreamReferenceAsync(GetImageResourceAsStream($"Assets/{resourceName}"));
DataPackage package = new();
package.SetBitmap(imageStreamRef);
return package;
}
private static async Task<RandomAccessStreamReference> ConvertToRandomAccessStreamReferenceAsync(Stream stream)
{
InMemoryRandomAccessStream inMemoryStream = new();
using var inputStream = stream.AsInputStream();
await RandomAccessStream.CopyAsync(inputStream, inMemoryStream);
inMemoryStream.Seek(0);
return RandomAccessStreamReference.CreateFromStream(inMemoryStream);
}
private static Stream GetImageResourceAsStream(string filename)
{
var assembly = Assembly.GetExecutingAssembly();
var assemblyName = new AssemblyName(assembly.FullName ?? throw new InvalidOperationException());
var resourceName = $"{assemblyName.Name}.{filename.Replace("/", ".")}";
return assembly.GetManifestResourceNames().Contains(resourceName)
? assembly.GetManifestResourceStream(resourceName)
: throw new InvalidOperationException($"Embedded resource '{resourceName}' does not exist.");
}
}

Просмотреть файл

@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="OpenAI" />
<PackageReference Include="Azure.AI.OpenAI" />
<PackageReference Include="CommunityToolkit.Mvvm" />
<PackageReference Include="CommunityToolkit.WinUI.Animations" />
@ -57,8 +58,9 @@
<!-- Including MessagePack to force version, since it's used by StreamJsonRpc but contains vulnerabilities. After StreamJsonRpc updates the version of MessagePack, we can upgrade StreamJsonRpc instead. -->
<PackageReference Include="MessagePack" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.SemanticKernel" />
<PackageReference Include="Microsoft.WindowsAppSDK" />
<!-- HACK: To align Microsoft.Bcl.AsyncInterfaces.dll version with Mouse Without Borders version. -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Microsoft.Windows.Compatibility" />
<PackageReference Include="Microsoft.Windows.CsWin32" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" />
@ -86,6 +88,12 @@
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>AdvancedPaste.UnitTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<!-- HACK: Common.UI is referenced, even if it is not used, to force dll versions to be the same as in other projects that use it. It's still unclear why this is the case, but this is need for flattening the install directory. -->
<ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" />

Просмотреть файл

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Threading;
@ -70,14 +71,19 @@ namespace AdvancedPaste
Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage;
}
this.InitializeComponent();
InitializeComponent();
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().UseContentRoot(AppContext.BaseDirectory).ConfigureServices((context, services) =>
{
services.AddSingleton<IFileSystem, FileSystem>();
services.AddSingleton<IUserSettings, UserSettings>();
services.AddSingleton<AICompletionsHelper>();
services.AddSingleton<OptionsViewModel>();
services.AddSingleton<IAICredentialsProvider, Services.OpenAI.VaultCredentialsProvider>();
services.AddSingleton<IPromptModerationService, Services.OpenAI.PromptModerationService>();
services.AddSingleton<ICustomTextTransformService, Services.OpenAI.CustomTextTransformService>();
services.AddSingleton<IKernelQueryCacheService, CustomActionKernelQueryCacheService>();
services.AddSingleton<IKernelService, Services.OpenAI.KernelService>();
services.AddSingleton<IPasteFormatExecutor, PasteFormatExecutor>();
services.AddSingleton<OptionsViewModel>();
}).Build();
viewModel = GetService<OptionsViewModel>();

Просмотреть файл

@ -173,11 +173,23 @@
Width="16"
Height="16"
Margin="8,0,0,0">
<PathIcon
x:Name="AIGlyph"
AutomationProperties.AccessibilityView="Raw"
Data="M128 766q0-42 24-77t65-48l178-57q32-11 61-30t52-42q50-50 71-114l58-179q13-40 48-65t78-26q42 0 77 24t50 65l58 177q21 66 72 117 49 50 117 72l176 58q43 14 69 48t26 80q0 41-25 76t-64 49l-178 58q-66 21-117 72-32 32-51 73t-33 84-26 83-30 73-45 51-71 20q-42 0-77-24t-49-65l-58-178q-8-25-19-47t-28-43q-34-43-77-68t-89-41-89-27-78-29-55-45-21-75zm1149 7q-76-29-145-53t-129-60-104-88-73-138l-57-176-67 176q-18 48-42 89t-60 78q-34 34-76 61t-89 43l-177 57q75 29 144 53t127 60 103 89 73 137l57 176 67-176q37-97 103-168t168-103l177-57zm-125 759q0-31 20-57t49-36l99-32q34-11 53-34t30-51 20-59 20-54 33-41 58-16q32 0 59 19t38 50q6 20 11 40t13 40 17 38 25 34q16 17 39 26t48 18 49 16 44 20 31 32 12 50q0 33-18 60t-51 38q-19 6-39 11t-41 13-39 17-34 25q-24 25-35 62t-24 73-35 61-68 25q-32 0-59-19t-38-50q-6-18-11-39t-13-41-17-40-24-33q-18-17-41-27t-47-17-49-15-43-20-30-33-12-54zm583 4q-43-13-74-30t-55-41-40-55-32-74q-12 41-29 72t-42 55-55 42-71 31q81 23 128 71t71 129q15-43 31-74t40-54 53-40 75-32z"
Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
<StackPanel
Margin="0"
Padding="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Image
x:Name="AIGlyphImage"
AutomationProperties.AccessibilityView="Raw"
Source="/Assets/AdvancedPaste/SemanticKernel.svg"
Visibility="{Binding DataContext.IsAdvancedAIEnabled, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource BoolToVisibilityConverter}}" />
<PathIcon
x:Name="AIGlyph"
AutomationProperties.AccessibilityView="Raw"
Data="M128 766q0-42 24-77t65-48l178-57q32-11 61-30t52-42q50-50 71-114l58-179q13-40 48-65t78-26q42 0 77 24t50 65l58 177q21 66 72 117 49 50 117 72l176 58q43 14 69 48t26 80q0 41-25 76t-64 49l-178 58q-66 21-117 72-32 32-51 73t-33 84-26 83-30 73-45 51-71 20q-42 0-77-24t-49-65l-58-178q-8-25-19-47t-28-43q-34-43-77-68t-89-41-89-27-78-29-55-45-21-75zm1149 7q-76-29-145-53t-129-60-104-88-73-138l-57-176-67 176q-18 48-42 89t-60 78q-34 34-76 61t-89 43l-177 57q75 29 144 53t127 60 103 89 73 137l57 176 67-176q37-97 103-168t168-103l177-57zm-125 759q0-31 20-57t49-36l99-32q34-11 53-34t30-51 20-59 20-54 33-41 58-16q32 0 59 19t38 50q6 20 11 40t13 40 17 38 25 34q16 17 39 26t48 18 49 16 44 20 31 32 12 50q0 33-18 60t-51 38q-19 6-39 11t-41 13-39 17-34 25q-24 25-35 62t-24 73-35 61-68 25q-32 0-59-19t-38-50q-6-18-11-39t-13-41-17-40-24-33q-18-17-41-27t-47-17-49-15-43-20-30-33-12-54zm583 4q-43-13-74-30t-55-41-40-55-32-74q-12 41-29 72t-42 55-55 42-71 31q81 23 128 71t71 129q15-43 31-74t40-54 53-40 75-32z"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Visibility="{Binding DataContext.IsAdvancedAIEnabled, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BoolToInvertedVisibilityConverter}}" />
</StackPanel>
</Viewbox>
<ScrollViewer
x:Name="ContentElement"
@ -251,6 +263,9 @@
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="AIGlyph" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlForegroundDisabled}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="AIGlyphImage" Storyboard.TargetProperty="Opacity">
<DiscreteObjectKeyFrame KeyTime="0" Value="0.4" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextContentPresenter" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{Binding PlaceholderForeground, RelativeSource={RelativeSource TemplatedParent}, TargetNullValue={ThemeResource TextControlPlaceholderForegroundDisabled}}" />
</ObjectAnimationUsingKeyFrames>
@ -346,6 +361,7 @@
x:Name="InputTxtBox"
HorizontalAlignment="Stretch"
x:FieldModifier="public"
DataContext="{x:Bind ViewModel}"
IsEnabled="{x:Bind ViewModel.ClipboardHasData, Mode=OneWay}"
KeyDown="InputTxtBox_KeyDown"
PlaceholderText="{x:Bind ViewModel.InputTxtBoxPlaceholderText, Mode=OneWay}"
@ -483,7 +499,7 @@
x:Uid="RegenerateBtnAutomation"
Grid.Column="1"
VerticalAlignment="Stretch"
Command="{x:Bind GenerateCustomCommand}"
Command="{x:Bind GenerateCustomAICommand}"
Content="{ui:FontIcon Glyph=&#xE72C;,
FontSize=16}"
Style="{StaticResource SubtleButtonStyle}">
@ -508,34 +524,6 @@
</Flyout>
</FlyoutBase.AttachedFlyout>
</TextBox>
<!--<StackPanel
Margin="0,0,4,0"
HorizontalAlignment="Right"
VerticalAlignment="Stretch"
Orientation="Horizontal">-->
<!--<Button
x:Name="RecallBtn"
x:Uid="RecallButtonAutomation"
Width="32"
Height="32"
Padding="0"
Command="{x:Bind RecallCommand}"
Content="{ui:FontIcon Glyph=&#xE81C;,
FontSize=12}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Style="{StaticResource SubtleButtonStyle}"
TabIndex="2"
Visibility="Collapsed">
<ToolTipService.ToolTip>
<TextBlock x:Uid="RecallBtnToolTip" TextWrapping="WrapWholeWords" />
</ToolTipService.ToolTip>
<animations:Implicit.Animations>
<animations:TranslationAnimation Duration="0:0:1" />
<animations:ScaleAnimation Duration="0:0:1" />
<animations:OffsetAnimation Duration="0:0:1" />
</animations:Implicit.Animations>
</Button>-->
<Grid
Width="32"
Height="32"
@ -549,11 +537,11 @@
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ui:VisualExtensions.NormalizedCenterPoint="0.5,0.5"
Command="{x:Bind GenerateCustomCommand}"
Command="{x:Bind GenerateCustomAICommand}"
Content="{ui:FontIcon Glyph=&#xE724;,
FontSize=16}"
Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
IsEnabled="{x:Bind ViewModel.IsCustomAIEnabled, Mode=OneWay}"
IsEnabled="{x:Bind ViewModel.IsCustomAIAvailable, Mode=OneWay}"
Style="{StaticResource SubtleButtonStyle}"
TabIndex="1"
Visibility="{x:Bind ViewModel.Query.Length, Mode=OneWay, Converter={StaticResource CountToVisibilityConverter}}">
@ -587,9 +575,9 @@
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent"
Visibility="{x:Bind ViewModel.IsCustomAIEnabled, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}">
Visibility="{x:Bind ViewModel.IsCustomAIAvailable, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}">
<ToolTipService.ToolTip>
<ToolTip Content="{x:Bind ViewModel.AIDisabledErrorText}" />
<ToolTip Content="{x:Bind ViewModel.CustomAIUnavailableErrorText, Mode=OneWay}" />
</ToolTipService.ToolTip>
</Grid>
</Grid>
@ -634,11 +622,36 @@
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock
FontWeight="SemiBold"
Foreground="{ThemeResource SystemFillColorCriticalBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{x:Bind ViewModel.PasteOperationErrorText, Mode=OneWay}" />
<StackPanel Grid.Column="0" Orientation="Horizontal">
<ToolTipService.ToolTip>
<ToolTip VerticalOffset="-105" Visibility="{x:Bind ViewModel.PasteActionError.HasDetails, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}">
<StackPanel
MinWidth="300"
HorizontalAlignment="Stretch"
Orientation="Vertical">
<TextBox
x:Name="AIErrorMessage"
x:Uid="AIErrorMessage"
FontSize="12"
IsReadOnly="True"
Text="{x:Bind ViewModel.PasteActionError.Details, Mode=OneWay}"
TextWrapping="Wrap" />
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
<FontIcon
Margin="0,3,3,0"
VerticalAlignment="Top"
FontSize="12"
Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
Glyph="&#xE946;"
Visibility="{x:Bind ViewModel.PasteActionError.HasDetails, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
<TextBlock
FontWeight="SemiBold"
Foreground="{ThemeResource SystemFillColorCriticalBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{x:Bind ViewModel.PasteActionError.Text, Mode=OneWay}" />
</StackPanel>
<HyperlinkButton
x:Uid="SettingsBtn"
Grid.Column="1"
@ -662,7 +675,6 @@
<VisualState.Setters>
<Setter Target="Loader.IsLoading" Value="True" />
<Setter Target="InputTxtBox.IsEnabled" Value="False" />
<!--<Setter Target="RecallBtn.IsEnabled" Value="False" />-->
<Setter Target="SendBtn.IsEnabled" Value="False" />
<Setter Target="DisclaimerPresenter.Visibility" Value="Collapsed" />
<Setter Target="LoadingText.Visibility" Value="Visible" />

Просмотреть файл

@ -2,10 +2,10 @@
// 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;
using System.ComponentModel;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using AdvancedPaste.ViewModels;
using CommunityToolkit.Mvvm.Input;
@ -40,7 +40,7 @@ namespace AdvancedPaste.Controls
public object Footer
{
get => (object)GetValue(FooterProperty);
get => GetValue(FooterProperty);
set => SetValue(FooterProperty, value);
}
@ -50,27 +50,24 @@ namespace AdvancedPaste.Controls
ViewModel = App.GetService<OptionsViewModel>();
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
ViewModel.CustomActionActivated += ViewModel_CustomActionActivated;
ViewModel.PreviewRequested += ViewModel_PreviewRequested;
}
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ViewModel.Busy) || e.PropertyName == nameof(ViewModel.PasteOperationErrorText))
if (e.PropertyName == nameof(ViewModel.Busy) || e.PropertyName == nameof(ViewModel.PasteActionError))
{
var state = ViewModel.Busy ? "LoadingState" : string.IsNullOrEmpty(ViewModel.PasteOperationErrorText) ? "DefaultState" : "ErrorState";
var state = ViewModel.Busy ? "LoadingState" : ViewModel.PasteActionError.HasText ? "ErrorState" : "DefaultState";
VisualStateManager.GoToState(this, state, true);
}
}
private void ViewModel_CustomActionActivated(object sender, CustomActionActivatedEventArgs e)
private void ViewModel_PreviewRequested(object sender, EventArgs e)
{
Logger.LogTrace();
if (!e.PasteResult)
{
PreviewGrid.Width = InputTxtBox.ActualWidth;
PreviewFlyout.ShowAt(InputTxtBox);
}
PreviewGrid.Width = InputTxtBox.ActualWidth;
PreviewFlyout.ShowAt(InputTxtBox);
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
@ -79,35 +76,19 @@ namespace AdvancedPaste.Controls
}
[RelayCommand]
private async Task GenerateCustomAsync() => await ViewModel.GenerateCustomFunctionAsync(PasteActionSource.PromptBox);
[RelayCommand]
private void Recall()
{
Logger.LogTrace();
InputTxtBox.IsEnabled = true;
var lastQuery = ViewModel.RecallPreviousCustomQuery();
if (lastQuery != null)
{
InputTxtBox.Text = lastQuery.Query;
}
ClipboardHelper.SetClipboardTextContent(lastQuery.ClipboardData);
}
private async Task GenerateCustomAIAsync() => await ViewModel.ExecuteCustomAIFormatFromCurrentQueryAsync(PasteActionSource.PromptBox);
private async void InputTxtBox_KeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter && InputTxtBox.Text.Length > 0 && ViewModel.IsCustomAIEnabled)
if (e.Key == Windows.System.VirtualKey.Enter && InputTxtBox.Text.Length > 0 && ViewModel.IsCustomAIAvailable)
{
await GenerateCustomAsync();
await GenerateCustomAIAsync();
}
}
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)

Просмотреть файл

@ -43,7 +43,7 @@ namespace AdvancedPaste
double GetHeight(int maxCustomActionCount) =>
baseHeight +
new PasteFormatsToHeightConverter().GetHeight(coreActionCount + _userSettings.AdditionalActions.Count) +
new PasteFormatsToHeightConverter() { MaxItems = maxCustomActionCount }.GetHeight(optionsViewModel.IsAIServiceEnabled ? _userSettings.CustomActions.Count : 0);
new PasteFormatsToHeightConverter() { MaxItems = maxCustomActionCount }.GetHeight(optionsViewModel.IsCustomAIServiceEnabled ? _userSettings.CustomActions.Count : 0);
MinHeight = GetHeight(1);
Height = GetHeight(5);
@ -54,7 +54,7 @@ namespace AdvancedPaste
_userSettings.Changed += (_, _) => UpdateHeight();
optionsViewModel.PropertyChanged += (_, e) =>
{
if (e.PropertyName == nameof(optionsViewModel.IsAIServiceEnabled))
if (e.PropertyName == nameof(optionsViewModel.IsCustomAIServiceEnabled))
{
UpdateHeight();
}

Просмотреть файл

@ -195,12 +195,12 @@ namespace AdvancedPaste.Pages
PowerToysTelemetry.Log.WriteEvent(new Telemetry.AdvancedPasteClipboardItemClicked());
if (!string.IsNullOrEmpty(item.Content))
{
ClipboardHelper.SetClipboardTextContent(item.Content);
ClipboardHelper.SetTextContent(item.Content);
}
else if (item.Image is not null)
{
RandomAccessStreamReference image = await item.Item.Content.GetBitmapAsync();
ClipboardHelper.SetClipboardImageContent(image);
ClipboardHelper.SetImageContent(image);
}
}
}

Просмотреть файл

@ -0,0 +1,77 @@
<svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_367_9162)">
<mask id="mask0_367_9162" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="4" y="0" width="32" height="40">
<path d="M34.1422 5.85786C32.285 4.00069 30.0802 2.5275 27.6537 1.52241C25.2272 0.517314 22.6265 0 20 0C17.3736 0 14.7729 0.517317 12.3463 1.52241C9.91984 2.52751 7.71505 4.0007 5.85788 5.85787L5.86186 5.86184C3.41152 8.31218 4.90113 14.0036 9.12168 20C4.90114 25.9964 3.41152 31.6878 5.86186 34.1382L5.85789 34.1421C7.71506 35.9993 9.91984 37.4725 12.3464 38.4776C14.7729 39.4827 17.3736 40 20 40C22.6265 40 25.2272 39.4827 27.6537 38.4776C30.0802 37.4725 32.285 35.9993 34.1422 34.1421L34.1382 34.1382C36.5885 31.6878 35.0989 25.9964 30.8784 20C35.0989 14.0036 36.5885 8.31218 34.1382 5.86184L34.1422 5.85786Z" fill="white"/>
</mask>
<g mask="url(#mask0_367_9162)">
<path d="M14.1091 14.1089C6.30083 21.9172 2.60841 30.8845 5.86186 34.1379C9.11531 37.3914 18.0826 33.699 25.8909 25.8907L14.1091 14.1089Z" fill="url(#paint0_linear_367_9162)"/>
<path d="M34.1384 5.86192C30.885 2.60847 21.9177 6.30089 14.1094 14.1092L25.8912 25.891C33.6995 18.0827 37.3919 9.11538 34.1384 5.86192Z" fill="url(#paint1_linear_367_9162)"/>
<g filter="url(#filter0_f_367_9162)">
<path d="M5.85815 34.1419C7.71533 35.9991 9.92011 37.4723 12.3466 38.4774C14.7731 39.4825 17.3739 39.9998 20.0003 39.9998C22.6267 39.9998 25.2275 39.4825 27.654 38.4774C30.0805 37.4723 32.2853 35.9991 34.1424 34.1419L34.1384 34.1379C37.3919 30.8845 33.6995 21.9172 25.8912 14.1089L20.0003 19.9998L25.8912 25.8907C18.0829 33.699 9.1156 37.3914 5.86214 34.1379L5.85815 34.1419Z" fill="url(#paint2_radial_367_9162)"/>
</g>
<path fill-rule="evenodd" clip-rule="evenodd" d="M5.85795 34.1421C7.71512 35.9993 9.9199 37.4725 12.3464 38.4776C14.7729 39.4827 17.3736 40 20.0001 40C22.6265 40 25.2272 39.4827 27.6538 38.4776C30.0803 37.4725 32.285 35.9993 34.1422 34.1421L34.1381 34.1381C37.3916 30.8846 33.6992 21.9173 25.8909 14.109L19.9999 20L25.8907 25.8908C18.0826 33.6989 9.11545 37.3914 5.86184 34.1382L5.85795 34.1421Z" fill="url(#paint3_linear_367_9162)"/>
<g filter="url(#filter1_f_367_9162)">
<path d="M34.1426 5.85786C32.2855 4.00069 30.0807 2.5275 27.6542 1.52241C25.2277 0.517314 22.6269 0 20.0005 0C17.3741 0 14.7733 0.517317 12.3468 1.52241C9.92032 2.52751 7.71554 4.0007 5.85837 5.85787L5.86235 5.86184C2.6089 9.1153 6.30132 18.0826 14.1096 25.8909L20.0005 20L14.1096 14.1091C21.9179 6.30081 30.8852 2.60839 34.1387 5.86184L34.1426 5.85786Z" fill="url(#paint4_radial_367_9162)"/>
</g>
<path fill-rule="evenodd" clip-rule="evenodd" d="M34.1421 5.85786C32.2849 4.00069 30.0801 2.5275 27.6536 1.52241C25.2271 0.517314 22.6264 0 19.9999 0C17.3735 0 14.7728 0.517317 12.3462 1.52241C9.91973 2.52751 7.71495 4.0007 5.85778 5.85787L5.86186 5.86194C2.60841 9.1154 6.30083 18.0827 14.1091 25.891L20.0001 20L14.1093 14.1092C21.9174 6.30106 30.8845 2.60863 34.1382 5.86176L34.1421 5.85786Z" fill="url(#paint5_linear_367_9162)"/>
<g style="mix-blend-mode:soft-light">
<path d="M14.1089 25.8907C21.9172 33.699 30.8845 37.3914 34.1379 34.1379C37.3914 30.8845 33.699 21.9172 25.8907 14.1089L14.1089 25.8907Z" fill="url(#paint6_linear_367_9162)"/>
</g>
<path d="M25.9006 25.9006C22.6418 29.1594 17.3582 29.1594 14.0994 25.9006C10.8406 22.6418 10.8406 17.3582 14.0994 14.0994C17.3582 10.8406 22.6418 10.8406 25.9006 14.0994C29.1594 17.3582 29.1594 22.6418 25.9006 25.9006Z" fill="url(#paint7_linear_367_9162)"/>
</g>
</g>
<defs>
<filter id="filter0_f_367_9162" x="2.52482" y="10.7756" width="36.1291" height="32.5575" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feBlend mode="normal" in="SourceGraphic" in2="BackgroundImageFix" result="shape"/>
<feGaussianBlur stdDeviation="1.66667" result="effect1_foregroundBlur_367_9162"/>
</filter>
<filter id="filter1_f_367_9162" x="1.34684" y="-3.33333" width="36.1291" height="32.5575" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feBlend mode="normal" in="SourceGraphic" in2="BackgroundImageFix" result="shape"/>
<feGaussianBlur stdDeviation="1.66667" result="effect1_foregroundBlur_367_9162"/>
</filter>
<linearGradient id="paint0_linear_367_9162" x1="18.8419" y1="30.7993" x2="3.60163" y2="21.7903" gradientUnits="userSpaceOnUse">
<stop stop-color="#6B1796"/>
<stop offset="0.416496" stop-color="#801EAE"/>
<stop offset="1" stop-color="#8752E0"/>
</linearGradient>
<linearGradient id="paint1_linear_367_9162" x1="20.6693" y1="10.9795" x2="32.9172" y2="18.7501" gradientUnits="userSpaceOnUse">
<stop stop-color="#2253CE"/>
<stop offset="0.658143" stop-color="#4A94FC"/>
<stop offset="1" stop-color="#6BB0FF"/>
</linearGradient>
<radialGradient id="paint2_radial_367_9162" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(21.9518 29.1054) rotate(-42.6722) scale(12.3846 14.093)">
<stop stop-color="#3D0D59"/>
<stop offset="1" stop-color="#3D0D59" stop-opacity="0"/>
</radialGradient>
<linearGradient id="paint3_linear_367_9162" x1="33.2725" y1="22.0379" x2="14.9821" y2="41.1912" gradientUnits="userSpaceOnUse">
<stop stop-color="#94CBFF"/>
<stop offset="0.472215" stop-color="#C86FEC"/>
<stop offset="1" stop-color="#A931D8"/>
</linearGradient>
<radialGradient id="paint4_radial_367_9162" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(17.9684 10.8231) rotate(133.859) scale(12.6284 14.3704)">
<stop stop-color="#122882"/>
<stop offset="1" stop-color="#1536A2" stop-opacity="0"/>
</radialGradient>
<linearGradient id="paint5_linear_367_9162" x1="9.4207" y1="21.4734" x2="31.7214" y2="3.29818" gradientUnits="userSpaceOnUse">
<stop stop-color="#B74CE1"/>
<stop offset="0.186314" stop-color="#7D7DF2"/>
<stop offset="0.337341" stop-color="#4A94FC"/>
<stop offset="0.70621" stop-color="#3DCBFF"/>
<stop offset="1" stop-color="#ABEEFF"/>
</linearGradient>
<linearGradient id="paint6_linear_367_9162" x1="20.8506" y1="31.8657" x2="33.6827" y2="28.1385" gradientUnits="userSpaceOnUse">
<stop stop-color="#F7ADFA"/>
<stop offset="1" stop-color="#F7ADFA" stop-opacity="0"/>
</linearGradient>
<linearGradient id="paint7_linear_367_9162" x1="21.0788" y1="11.6553" x2="17.1128" y2="29.483" gradientUnits="userSpaceOnUse">
<stop stop-color="#122882"/>
<stop offset="0.517831" stop-color="#491D9F"/>
<stop offset="1" stop-color="#801EAE"/>
</linearGradient>
<clipPath id="clip0_367_9162">
<rect width="40" height="40" fill="white"/>
</clipPath>
</defs>
</svg>

После

Ширина:  |  Высота:  |  Размер: 6.5 KiB

Просмотреть файл

@ -1,142 +0,0 @@
// 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;
using System.Globalization;
using System.IO;
using System.Net;
using Azure;
using Azure.AI.OpenAI;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Telemetry;
using Windows.Security.Credentials;
namespace AdvancedPaste.Helpers
{
public class AICompletionsHelper
{
// Return Response and Status code from the request.
public struct AICompletionsResponse
{
public AICompletionsResponse(string response, int apiRequestStatus)
{
Response = response;
ApiRequestStatus = apiRequestStatus;
}
public string Response { get; }
public int ApiRequestStatus { get; }
}
private string _openAIKey;
private string _modelName = "gpt-3.5-turbo-instruct";
public bool IsAIEnabled => !string.IsNullOrEmpty(this._openAIKey);
public AICompletionsHelper()
{
this._openAIKey = LoadOpenAIKey();
}
public void SetOpenAIKey(string openAIKey)
{
this._openAIKey = openAIKey;
}
public string GetKey()
{
return _openAIKey;
}
public static string LoadOpenAIKey()
{
PasswordVault vault = new PasswordVault();
try
{
PasswordCredential cred = vault.Retrieve("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey");
if (cred is not null)
{
return cred.Password.ToString();
}
}
catch (Exception)
{
}
return string.Empty;
}
private Response<Completions> GetAICompletion(string systemInstructions, string userMessage)
{
OpenAIClient azureAIClient = new OpenAIClient(_openAIKey);
var response = azureAIClient.GetCompletions(
new CompletionsOptions()
{
DeploymentName = _modelName,
Prompts =
{
systemInstructions + "\n\n" + userMessage,
},
Temperature = 0.01F,
MaxTokens = 2000,
});
if (response.Value.Choices[0].FinishReason == "length")
{
Console.WriteLine("Cut off due to length constraints");
}
return response;
}
public AICompletionsResponse AIFormatString(string inputInstructions, string inputString)
{
string systemInstructions = $@"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.";
string userMessage = $@"User instructions:
{inputInstructions}
Clipboard Content:
{inputString}
Output:
";
string aiResponse = null;
Response<Completions> rawAIResponse = null;
int apiRequestStatus = (int)HttpStatusCode.OK;
try
{
rawAIResponse = this.GetAICompletion(systemInstructions, userMessage);
aiResponse = rawAIResponse.Value.Choices[0].Text;
int promptTokens = rawAIResponse.Value.Usage.PromptTokens;
int completionTokens = rawAIResponse.Value.Usage.CompletionTokens;
PowerToysTelemetry.Log.WriteEvent(new Telemetry.AdvancedPasteGenerateCustomFormatEvent(promptTokens, completionTokens, _modelName));
}
catch (Azure.RequestFailedException error)
{
Logger.LogError("GetAICompletion failed", error);
PowerToysTelemetry.Log.WriteEvent(new Telemetry.AdvancedPasteGenerateCustomErrorEvent(error.Message));
apiRequestStatus = error.Status;
}
catch (Exception error)
{
Logger.LogError("GetAICompletion failed", error);
PowerToysTelemetry.Log.WriteEvent(new Telemetry.AdvancedPasteGenerateCustomErrorEvent(error.Message));
apiRequestStatus = -1;
}
return new AICompletionsResponse(aiResponse, apiRequestStatus);
}
}
}

Просмотреть файл

@ -3,214 +3,133 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AdvancedPaste.Models;
using ManagedCommon;
using Windows.ApplicationModel.DataTransfer;
using Windows.Data.Html;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System;
namespace AdvancedPaste.Helpers
namespace AdvancedPaste.Helpers;
internal static class ClipboardHelper
{
internal static class ClipboardHelper
internal static async Task TryCopyPasteAsync(DataPackage dataPackage, Action onCopied)
{
private static readonly HashSet<string> ImageFileTypes = new(StringComparer.InvariantCultureIgnoreCase) { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico", ".svg" };
Logger.LogTrace();
private static readonly (string DataFormat, ClipboardFormat ClipboardFormat)[] DataFormats =
[
(StandardDataFormats.Text, ClipboardFormat.Text),
(StandardDataFormats.Html, ClipboardFormat.Html),
(StandardDataFormats.Bitmap, ClipboardFormat.Image),
];
internal static async Task<ClipboardFormat> GetAvailableClipboardFormatsAsync(DataPackageView clipboardData)
if (await dataPackage.GetView().HasUsableDataAsync())
{
var availableClipboardFormats = DataFormats.Aggregate(
ClipboardFormat.None,
(result, formatPair) => clipboardData.Contains(formatPair.DataFormat) ? (result | formatPair.ClipboardFormat) : result);
if (clipboardData.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await clipboardData.GetStorageItemsAsync();
if (storageItems.Count == 1 && storageItems.Single() is StorageFile file && ImageFileTypes.Contains(file.FileType))
{
availableClipboardFormats |= ClipboardFormat.ImageFile;
}
}
return availableClipboardFormats;
}
internal static void SetClipboardTextContent(string text)
{
Logger.LogTrace();
if (!string.IsNullOrEmpty(text))
{
DataPackage output = new();
output.SetText(text);
Clipboard.SetContentWithOptions(output, null);
Flush();
}
}
private static bool Flush()
{
// TODO(stefan): For some reason Flush() fails from time to time when directly activated via hotkey.
// Calling inside a loop makes it work.
const int maxAttempts = 5;
for (int i = 1; i <= maxAttempts; i++)
{
try
{
Task.Run(Clipboard.Flush).Wait();
return true;
}
catch (Exception ex)
{
if (i == maxAttempts)
{
Logger.LogError($"{nameof(Clipboard)}.{nameof(Flush)}() failed", ex);
}
}
}
return false;
}
private static async Task<bool> FlushAsync() => await Task.Run(Flush);
internal static async Task SetClipboardFileContentAsync(string fileName)
{
var storageFile = await StorageFile.GetFileFromPathAsync(fileName);
DataPackage output = new();
output.SetStorageItems([storageFile]);
Clipboard.SetContent(output);
Clipboard.SetContent(dataPackage);
await FlushAsync();
}
internal static void SetClipboardImageContent(RandomAccessStreamReference image)
{
Logger.LogTrace();
if (image is not null)
{
DataPackage output = new();
output.SetBitmap(image);
Clipboard.SetContentWithOptions(output, null);
Flush();
}
}
// Function to send a single key event
private static void SendSingleKeyboardInput(short keyCode, uint keyStatus)
{
UIntPtr ignoreKeyEventFlag = (UIntPtr)0x5555;
NativeMethods.INPUT inputShift = new NativeMethods.INPUT
{
type = NativeMethods.INPUTTYPE.INPUT_KEYBOARD,
data = new NativeMethods.InputUnion
{
ki = new NativeMethods.KEYBDINPUT
{
wVk = keyCode,
dwFlags = keyStatus,
// Any keyevent with the extraInfo set to this value will be ignored by the keyboard hook and sent to the system instead.
dwExtraInfo = ignoreKeyEventFlag,
},
},
};
NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[] { inputShift };
_ = NativeMethods.SendInput(1, inputs, NativeMethods.INPUT.Size);
}
internal static void SendPasteKeyCombination()
{
Logger.LogTrace();
SendSingleKeyboardInput((short)VirtualKey.LeftControl, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightControl, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.LeftWindows, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightWindows, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.LeftShift, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightShift, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.LeftMenu, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightMenu, (uint)NativeMethods.KeyEventF.KeyUp);
// Send Ctrl + V
SendSingleKeyboardInput((short)VirtualKey.Control, (uint)NativeMethods.KeyEventF.KeyDown);
SendSingleKeyboardInput((short)VirtualKey.V, (uint)NativeMethods.KeyEventF.KeyDown);
SendSingleKeyboardInput((short)VirtualKey.V, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.Control, (uint)NativeMethods.KeyEventF.KeyUp);
Logger.LogInfo("Paste sent");
}
internal static async Task<string> GetClipboardTextOrHtmlTextAsync(DataPackageView clipboardData)
{
if (clipboardData.Contains(StandardDataFormats.Text))
{
return await clipboardData.GetTextAsync();
}
else if (clipboardData.Contains(StandardDataFormats.Html))
{
var html = await clipboardData.GetHtmlFormatAsync();
return HtmlUtilities.ConvertToText(html);
}
else
{
return string.Empty;
}
}
internal static async Task<string> GetClipboardHtmlContentAsync(DataPackageView clipboardData) =>
clipboardData.Contains(StandardDataFormats.Html) ? await clipboardData.GetHtmlFormatAsync() : string.Empty;
internal static async Task<SoftwareBitmap> GetClipboardImageContentAsync(DataPackageView clipboardData)
{
using var stream = await GetClipboardImageStreamAsync(clipboardData);
if (stream != null)
{
var decoder = await BitmapDecoder.CreateAsync(stream);
return await decoder.GetSoftwareBitmapAsync();
}
return null;
}
private static async Task<IRandomAccessStream> GetClipboardImageStreamAsync(DataPackageView clipboardData)
{
if (clipboardData.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await clipboardData.GetStorageItemsAsync();
var file = storageItems.Count == 1 ? storageItems[0] as StorageFile : null;
if (file != null)
{
return await file.OpenReadAsync();
}
}
if (clipboardData.Contains(StandardDataFormats.Bitmap))
{
var bitmap = await clipboardData.GetBitmapAsync();
return await bitmap.OpenReadAsync();
}
return null;
onCopied();
SendPasteKeyCombination();
}
}
internal static void SetTextContent(string text)
{
Logger.LogTrace();
if (!string.IsNullOrEmpty(text))
{
DataPackage output = new();
output.SetText(text);
Clipboard.SetContentWithOptions(output, null);
Flush();
}
}
internal static void SetImageContent(RandomAccessStreamReference image)
{
Logger.LogTrace();
if (image is not null)
{
DataPackage output = new();
output.SetBitmap(image);
Clipboard.SetContentWithOptions(output, null);
Flush();
}
}
private static bool Flush()
{
// TODO(stefan): For some reason Flush() fails from time to time when directly activated via hotkey.
// Calling inside a loop makes it work.
const int maxAttempts = 5;
for (int i = 1; i <= maxAttempts; i++)
{
try
{
Clipboard.Flush();
return true;
}
catch (Exception ex)
{
if (i == maxAttempts)
{
Logger.LogError($"{nameof(Clipboard)}.{nameof(Flush)}() failed", ex);
}
}
}
return false;
}
private static async Task<bool> 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 SendPasteKeyCombination()
{
Logger.LogTrace();
SendSingleKeyboardInput((short)VirtualKey.LeftControl, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightControl, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.LeftWindows, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightWindows, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.LeftShift, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightShift, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.LeftMenu, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.RightMenu, (uint)NativeMethods.KeyEventF.KeyUp);
// Send Ctrl + V
SendSingleKeyboardInput((short)VirtualKey.Control, (uint)NativeMethods.KeyEventF.KeyDown);
SendSingleKeyboardInput((short)VirtualKey.V, (uint)NativeMethods.KeyEventF.KeyDown);
SendSingleKeyboardInput((short)VirtualKey.V, (uint)NativeMethods.KeyEventF.KeyUp);
SendSingleKeyboardInput((short)VirtualKey.Control, (uint)NativeMethods.KeyEventF.KeyUp);
Logger.LogInfo("Paste sent");
}
// Function to send a single key event
private static void SendSingleKeyboardInput(short keyCode, uint keyStatus)
{
UIntPtr ignoreKeyEventFlag = (UIntPtr)0x5555;
NativeMethods.INPUT inputShift = new NativeMethods.INPUT
{
type = NativeMethods.INPUTTYPE.INPUT_KEYBOARD,
data = new NativeMethods.InputUnion
{
ki = new NativeMethods.KEYBDINPUT
{
wVk = keyCode,
dwFlags = keyStatus,
// Any keyevent with the extraInfo set to this value will be ignored by the keyboard hook and sent to the system instead.
dwExtraInfo = ignoreKeyEventFlag,
},
},
};
NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[] { inputShift };
_ = NativeMethods.SendInput(1, inputs, NativeMethods.INPUT.Size);
}
}

Просмотреть файл

@ -7,6 +7,5 @@ namespace AdvancedPaste.Helpers
internal static class Constants
{
internal static readonly string AdvancedPasteModuleName = "AdvancedPaste";
internal static readonly string LastQueryJsonFileName = "lastQuery.json";
}
}

Просмотреть файл

@ -0,0 +1,151 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AdvancedPaste.Models;
using Windows.ApplicationModel.DataTransfer;
using Windows.Data.Html;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
namespace AdvancedPaste.Helpers;
internal static class DataPackageHelpers
{
private static readonly HashSet<string> ImageFileTypes = new(StringComparer.InvariantCultureIgnoreCase) { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico", ".svg" };
private static readonly (string DataFormat, ClipboardFormat ClipboardFormat)[] DataFormats =
[
(StandardDataFormats.Text, ClipboardFormat.Text),
(StandardDataFormats.Html, ClipboardFormat.Html),
(StandardDataFormats.Bitmap, ClipboardFormat.Image),
];
internal static DataPackage CreateFromText(string text)
{
DataPackage dataPackage = new();
dataPackage.SetText(text);
return dataPackage;
}
internal static async Task<DataPackage> CreateFromFileAsync(string fileName)
{
var storageFile = await StorageFile.GetFileFromPathAsync(fileName);
DataPackage dataPackage = new();
dataPackage.SetStorageItems([storageFile]);
return dataPackage;
}
internal static async Task<ClipboardFormat> GetAvailableFormatsAsync(this DataPackageView dataPackageView)
{
var availableFormats = DataFormats.Aggregate(
ClipboardFormat.None,
(result, formatPair) => dataPackageView.Contains(formatPair.DataFormat) ? (result | formatPair.ClipboardFormat) : result);
if (dataPackageView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await dataPackageView.GetStorageItemsAsync();
if (storageItems.Count == 1 && storageItems.Single() is StorageFile file)
{
availableFormats |= ClipboardFormat.File;
if (ImageFileTypes.Contains(file.FileType))
{
availableFormats |= ClipboardFormat.Image;
}
}
}
return FixFormatsForAI(availableFormats);
}
private static ClipboardFormat FixFormatsForAI(ClipboardFormat formats)
{
var result = formats;
if (result.HasFlag(ClipboardFormat.File) && result != ClipboardFormat.File)
{
// Advertise the "generic" File format only if there is no other specific format available; confusing for AI otherwise.
result &= ~ClipboardFormat.File;
}
if (result == (ClipboardFormat.Image | ClipboardFormat.Html))
{
// The Windows Photo application advertises Image and Html when copying an image; this Html format is not easily usable and is confusing for AI.
result &= ~ClipboardFormat.Html;
}
return result;
}
internal static async Task<bool> HasUsableDataAsync(this DataPackageView dataPackageView)
{
var availableFormats = await GetAvailableFormatsAsync(dataPackageView);
return availableFormats == ClipboardFormat.Text ? !string.IsNullOrEmpty(await dataPackageView.GetTextAsync()) : availableFormats != ClipboardFormat.None;
}
internal static async Task<string> GetTextOrEmptyAsync(this DataPackageView dataPackageView) =>
dataPackageView.Contains(StandardDataFormats.Text) ? await dataPackageView.GetTextAsync() : string.Empty;
internal static async Task<string> GetTextOrHtmlTextAsync(this DataPackageView dataPackageView)
{
if (dataPackageView.Contains(StandardDataFormats.Text))
{
return await dataPackageView.GetTextAsync();
}
else if (dataPackageView.Contains(StandardDataFormats.Html))
{
var html = await dataPackageView.GetHtmlFormatAsync();
return HtmlUtilities.ConvertToText(html);
}
else
{
return string.Empty;
}
}
internal static async Task<string> GetHtmlContentAsync(this DataPackageView dataPackageView) =>
dataPackageView.Contains(StandardDataFormats.Html) ? await dataPackageView.GetHtmlFormatAsync() : string.Empty;
internal static async Task<SoftwareBitmap> GetImageContentAsync(this DataPackageView dataPackageView)
{
using var stream = await dataPackageView.GetImageStreamAsync();
if (stream != null)
{
var decoder = await BitmapDecoder.CreateAsync(stream);
return await decoder.GetSoftwareBitmapAsync();
}
return null;
}
private static async Task<IRandomAccessStream> GetImageStreamAsync(this DataPackageView dataPackageView)
{
if (dataPackageView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await dataPackageView.GetStorageItemsAsync();
var file = storageItems.Count == 1 ? storageItems[0] as StorageFile : null;
if (file != null)
{
return await file.OpenReadAsync();
}
}
if (dataPackageView.Contains(StandardDataFormats.Bitmap))
{
var bitmap = await dataPackageView.GetBitmapAsync();
return await bitmap.OpenReadAsync();
}
return null;
}
}

Просмотреть файл

@ -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),
};
}

Просмотреть файл

@ -12,9 +12,9 @@ namespace AdvancedPaste.Settings
{
public interface IUserSettings
{
public bool ShowCustomPreview { get; }
public bool IsAdvancedAIEnabled { get; }
public bool SendPasteKeyCombination { get; }
public bool ShowCustomPreview { get; }
public bool CloseAfterLosingFocus { get; }

Просмотреть файл

@ -33,30 +33,23 @@ namespace AdvancedPaste.Helpers
private static readonly Regex CsvRemoveStartAndEndQuotationMarksRegex = new Regex(@"^""(?=(""{2})+)|(?<=(""{2})+)""$");
private static readonly Regex CsvReplaceDoubleQuotationMarksRegex = new Regex(@"""{2}");
internal static string ToJsonFromXmlOrCsv(DataPackageView clipboardData)
internal static async Task<string> ToJsonFromXmlOrCsvAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
if (clipboardData == null || !clipboardData.Contains(StandardDataFormats.Text))
if (!clipboardData.Contains(StandardDataFormats.Text))
{
Logger.LogWarning("Clipboard does not contain text data");
return string.Empty;
}
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
string text = Task.Run(async () =>
{
string plainText = await clipboardData.GetTextAsync() as string;
return plainText;
}).Result;
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
var text = await clipboardData.GetTextAsync();
string jsonText = string.Empty;
// Try convert XML
try
{
XmlDocument doc = new XmlDocument();
XmlDocument doc = new();
doc.LoadXml(text);
Logger.LogDebug("Converted from XML.");
jsonText = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);

Просмотреть файл

@ -0,0 +1,60 @@
// 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;
using System.Collections.Generic;
using System.Threading.Tasks;
using AdvancedPaste.Models;
using Microsoft.SemanticKernel;
using Windows.ApplicationModel.DataTransfer;
namespace AdvancedPaste.Helpers;
internal static class KernelExtensions
{
private const string DataPackageKey = "DataPackage";
private const string LastErrorKey = "LastError";
private const string ActionChainKey = "ActionChain";
internal static DataPackageView GetDataPackageView(this Kernel kernel)
{
kernel.Data.TryGetValue(DataPackageKey, out object obj);
return obj as DataPackageView ?? (obj as DataPackage)?.GetView();
}
internal static DataPackage GetDataPackage(this Kernel kernel)
{
kernel.Data.TryGetValue(DataPackageKey, out object obj);
return obj as DataPackage ?? new();
}
internal static async Task<string> GetDataFormatsAsync(this Kernel kernel)
{
var clipboardFormats = await kernel.GetDataPackageView().GetAvailableFormatsAsync();
return clipboardFormats.ToString();
}
internal static void SetDataPackage(this Kernel kernel, DataPackage dataPackage) => kernel.Data[DataPackageKey] = dataPackage;
internal static void SetDataPackageView(this Kernel kernel, DataPackageView dataPackageView) => kernel.Data[DataPackageKey] = dataPackageView;
internal static Exception GetLastError(this Kernel kernel) => kernel.Data.TryGetValue(LastErrorKey, out object obj) ? obj as Exception : null;
internal static void SetLastError(this Kernel kernel, Exception error) => kernel.Data[LastErrorKey] = error;
internal static List<ActionChainItem> GetOrAddActionChain(this Kernel kernel)
{
if (kernel.Data.TryGetValue(ActionChainKey, out var actionChainObj))
{
return (List<ActionChainItem>)actionChainObj;
}
else
{
List<ActionChainItem> actionChain = [];
kernel.Data[ActionChainKey] = actionChain;
return actionChain;
}
}
}

Просмотреть файл

@ -15,67 +15,15 @@ namespace AdvancedPaste.Helpers
{
internal static class MarkdownHelper
{
public static string ToMarkdown(DataPackageView clipboardData)
public static async Task<string> ToMarkdownAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
if (clipboardData == null)
{
Logger.LogWarning("Clipboard does not contain data");
var data = clipboardData.Contains(StandardDataFormats.Html) ? await clipboardData.GetHtmlFormatAsync()
: clipboardData.Contains(StandardDataFormats.Text) ? await clipboardData.GetTextAsync()
: string.Empty;
return string.Empty;
}
string data = string.Empty;
if (clipboardData.Contains(StandardDataFormats.Html))
{
data = Task.Run(async () =>
{
string data = await clipboardData.GetHtmlFormatAsync() as string;
return data;
}).Result;
}
else if (clipboardData.Contains(StandardDataFormats.Text))
{
data = Task.Run(async () =>
{
string plainText = await clipboardData.GetTextAsync() as string;
return plainText;
}).Result;
}
if (!string.IsNullOrEmpty(data))
{
string cleanedHtml = CleanHtml(data);
return ConvertHtmlToMarkdown(cleanedHtml);
}
return string.Empty;
}
public static string PasteAsPlainTextFromClipboard(DataPackageView clipboardData)
{
Logger.LogTrace();
if (clipboardData != null)
{
if (!clipboardData.Contains(StandardDataFormats.Text))
{
Logger.LogWarning("Clipboard does not contain text data");
return string.Empty;
}
return Task.Run(async () =>
{
string plainText = await clipboardData.GetTextAsync() as string;
return plainText;
}).Result;
}
return string.Empty;
return string.IsNullOrEmpty(data) ? string.Empty : ConvertHtmlToMarkdown(CleanHtml(data));
}
private static string CleanHtml(string html)

Просмотреть файл

@ -5,6 +5,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.Globalization;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
@ -21,7 +22,9 @@ public static class OcrHelpers
var ocrEngine = OcrEngine.TryCreateFromLanguage(ocrLanguage) ?? throw new InvalidOperationException("Unable to create OCR engine");
var ocrResult = await ocrEngine.RecognizeAsync(bitmap);
return ocrResult.Text;
return string.IsNullOrWhiteSpace(ocrResult.Text)
? throw new InvalidOperationException("Unable to extract text from image or image does not contain text")
: ocrResult.Text;
}
private static Language GetOCRLanguage()

Просмотреть файл

@ -0,0 +1,149 @@
// 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;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using AdvancedPaste.Models;
using ManagedCommon;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
namespace AdvancedPaste.Helpers;
public static class TransformHelpers
{
public static async Task<DataPackage> TransformAsync(PasteFormats format, DataPackageView clipboardData)
{
return format switch
{
PasteFormats.PlainText => await ToPlainTextAsync(clipboardData),
PasteFormats.Markdown => await ToMarkdownAsync(clipboardData),
PasteFormats.Json => await ToJsonAsync(clipboardData),
PasteFormats.ImageToText => await ImageToTextAsync(clipboardData),
PasteFormats.PasteAsTxtFile => await ToTxtFileAsync(clipboardData),
PasteFormats.PasteAsPngFile => await ToPngFileAsync(clipboardData),
PasteFormats.PasteAsHtmlFile => await ToHtmlFileAsync(clipboardData),
PasteFormats.KernelQuery => throw new ArgumentException($"Unsupported format {format}", nameof(format)),
PasteFormats.CustomTextTransformation => throw new ArgumentException($"Unsupported format {format}", nameof(format)),
_ => throw new ArgumentException($"Unknown value {format}", nameof(format)),
};
}
private static async Task<DataPackage> ToPlainTextAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
return CreateDataPackageFromText(await clipboardData.GetTextOrEmptyAsync());
}
private static async Task<DataPackage> ToMarkdownAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
return CreateDataPackageFromText(await MarkdownHelper.ToMarkdownAsync(clipboardData));
}
private static async Task<DataPackage> ToJsonAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
return CreateDataPackageFromText(await JsonHelper.ToJsonFromXmlOrCsvAsync(clipboardData));
}
private static async Task<DataPackage> ImageToTextAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var bitmap = await clipboardData.GetImageContentAsync() ?? throw new ArgumentException("No image content found in clipboard", nameof(clipboardData));
var text = await OcrHelpers.ExtractTextAsync(bitmap);
return CreateDataPackageFromText(text);
}
private static async Task<DataPackage> ToPngFileAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var clipboardBitmap = await clipboardData.GetImageContentAsync();
using var pngStream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, pngStream);
encoder.SetSoftwareBitmap(clipboardBitmap);
await encoder.FlushAsync();
return await CreateDataPackageFromFileContentAsync(pngStream.AsStreamForRead(), "png");
}
private static async Task<DataPackage> ToTxtFileAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var text = await clipboardData.GetTextOrHtmlTextAsync();
return await CreateDataPackageFromFileContentAsync(text, "txt");
}
private static async Task<DataPackage> ToHtmlFileAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var cfHtml = await clipboardData.GetHtmlContentAsync();
var html = RemoveHtmlMetadata(cfHtml);
return await CreateDataPackageFromFileContentAsync(html, "html");
}
/// <summary>
/// Removes leading CF_HTML metadata from HTML clipboard data.
/// See: https://learn.microsoft.com/en-us/windows/win32/dataxchg/html-clipboard-format
/// </summary>
private static string RemoveHtmlMetadata(string cfHtml)
{
int? GetIntTagValue(string tagName)
{
var tagNameWithColon = tagName + ":";
int tagStartPos = cfHtml.IndexOf(tagNameWithColon, StringComparison.InvariantCulture);
const int tagValueLength = 10;
return tagStartPos != -1 && int.TryParse(cfHtml.AsSpan(tagStartPos + tagNameWithColon.Length, tagValueLength), CultureInfo.InvariantCulture, out int result) ? result : null;
}
var startFragmentIndex = GetIntTagValue("StartFragment");
var endFragmentIndex = GetIntTagValue("EndFragment");
return (startFragmentIndex == null || endFragmentIndex == null) ? cfHtml : cfHtml[startFragmentIndex.Value..endFragmentIndex.Value];
}
private static async Task<DataPackage> CreateDataPackageFromFileContentAsync(string data, string fileExtension)
{
if (string.IsNullOrEmpty(data))
{
throw new ArgumentException($"Empty value in {nameof(CreateDataPackageFromFileContentAsync)}", nameof(data));
}
var path = GetPasteAsFileTempFilePath(fileExtension);
await File.WriteAllTextAsync(path, data);
return await DataPackageHelpers.CreateFromFileAsync(path);
}
private static async Task<DataPackage> CreateDataPackageFromFileContentAsync(Stream stream, string fileExtension)
{
var path = GetPasteAsFileTempFilePath(fileExtension);
using var fileStream = File.Create(path);
await stream.CopyToAsync(fileStream);
return await DataPackageHelpers.CreateFromFileAsync(path);
}
private static string GetPasteAsFileTempFilePath(string fileExtension)
{
var prefix = ResourceLoaderInstance.ResourceLoader.GetString("PasteAsFile_FilePrefix");
var timestamp = DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);
return Path.Combine(Path.GetTempPath(), $"{prefix}{timestamp}.{fileExtension}");
}
private static DataPackage CreateDataPackageFromText(string content) => DataPackageHelpers.CreateFromText(content);
}

Просмотреть файл

@ -33,9 +33,9 @@ namespace AdvancedPaste.Settings
public event EventHandler Changed;
public bool ShowCustomPreview { get; private set; }
public bool IsAdvancedAIEnabled { get; private set; }
public bool SendPasteKeyCombination { get; private set; }
public bool ShowCustomPreview { get; private set; }
public bool CloseAfterLosingFocus { get; private set; }
@ -43,12 +43,12 @@ namespace AdvancedPaste.Settings
public IReadOnlyList<AdvancedPasteCustomAction> CustomActions => _customActions;
public UserSettings()
public UserSettings(IFileSystem fileSystem)
{
_settingsUtils = new SettingsUtils();
_settingsUtils = new SettingsUtils(fileSystem);
IsAdvancedAIEnabled = false;
ShowCustomPreview = true;
SendPasteKeyCombination = true;
CloseAfterLosingFocus = false;
_additionalActions = [];
_customActions = [];
@ -56,7 +56,7 @@ namespace AdvancedPaste.Settings
LoadSettingsFromJson();
_watcher = Helper.GetFileWatcher(AdvancedPasteModuleName, "settings.json", OnSettingsFileChanged);
_watcher = Helper.GetFileWatcher(AdvancedPasteModuleName, "settings.json", OnSettingsFileChanged, fileSystem);
}
private void OnSettingsFileChanged()
@ -98,8 +98,8 @@ namespace AdvancedPaste.Settings
{
var properties = settings.Properties;
IsAdvancedAIEnabled = properties.IsAdvancedAIEnabled;
ShowCustomPreview = properties.ShowCustomPreview;
SendPasteKeyCombination = properties.SendPasteKeyCombination;
CloseAfterLosingFocus = properties.CloseAfterLosingFocus;
var sourceAdditionalActions = properties.AdditionalActions;

Просмотреть файл

@ -0,0 +1,18 @@
// 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.
namespace AdvancedPaste.Models;
public record class AIServiceUsage(int PromptTokens, int CompletionTokens)
{
public static AIServiceUsage None => new(PromptTokens: 0, CompletionTokens: 0);
public bool HasUsage => PromptTokens > 0 || CompletionTokens > 0;
public static AIServiceUsage Add(AIServiceUsage first, AIServiceUsage second) =>
new(first.PromptTokens + second.PromptTokens, first.CompletionTokens + second.CompletionTokens);
public override string ToString() =>
$"{nameof(PromptTokens)}: {PromptTokens}, {nameof(CompletionTokens)}: {CompletionTokens}";
}

Просмотреть файл

@ -0,0 +1,9 @@
// 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.Collections.Generic;
namespace AdvancedPaste.Models;
public record class ActionChainItem(PasteFormats Format, Dictionary<string, string> Arguments);

Просмотреть файл

@ -14,5 +14,5 @@ public enum ClipboardFormat
Html = 1 << 1,
Audio = 1 << 2,
Image = 1 << 3,
ImageFile = 1 << 4,
File = 1 << 4, // output only for now
}

Просмотреть файл

@ -1,14 +0,0 @@
// 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;
namespace AdvancedPaste.Models;
public sealed class CustomActionActivatedEventArgs(string text, bool pasteResult) : EventArgs
{
public string Text { get; private init; } = text;
public bool PasteResult { get; private init; } = pasteResult;
}

Просмотреть файл

@ -1,27 +0,0 @@
// 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.Text.Json;
using AdvancedPaste.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace AdvancedPaste.Models
{
internal sealed class CustomQuery : ISettingsConfig
{
public string Query { get; set; }
public string ClipboardData { get; set; }
public string GetModuleName() => Constants.AdvancedPasteModuleName;
public string ToJsonString() => JsonSerializer.Serialize(this);
public override string ToString()
=> JsonSerializer.Serialize(this);
public bool UpgradeSettingsConfiguration() => false;
}
}

Просмотреть файл

@ -0,0 +1,24 @@
// 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;
namespace AdvancedPaste.Models.KernelQueryCache;
public sealed class CacheKey : IEquatable<CacheKey>
{
public static StringComparer PromptComparer => StringComparer.CurrentCultureIgnoreCase;
public string Prompt { get; init; }
public ClipboardFormat AvailableFormats { get; init; }
public override string ToString() => $"{AvailableFormats}: {Prompt}";
public override bool Equals(object obj) => Equals(obj as CacheKey);
public bool Equals(CacheKey other) => other != null && PromptComparer.Equals(Prompt, other.Prompt) && AvailableFormats == other.AvailableFormats;
public override int GetHashCode() => PromptComparer.GetHashCode(Prompt) ^ AvailableFormats.GetHashCode();
}

Просмотреть файл

@ -0,0 +1,9 @@
// 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.Collections.Generic;
namespace AdvancedPaste.Models.KernelQueryCache;
public record class CacheValue(List<ActionChainItem> ActionChain);

Просмотреть файл

@ -0,0 +1,39 @@
// 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.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using AdvancedPaste.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace AdvancedPaste.Models.KernelQueryCache;
public sealed class PersistedCache : ISettingsConfig
{
public record class CacheItem(CacheKey CacheKey, CacheValue CacheValue);
private static readonly JsonSerializerOptions SerializerOptions = new()
{
Converters =
{
new JsonStringEnumConverter(),
},
};
public static PersistedCache FromJsonString(string json) => JsonSerializer.Deserialize<PersistedCache>(json, SerializerOptions);
public string Version { get; init; }
public List<CacheItem> Items { get; init; } = [];
public string GetModuleName() => Constants.AdvancedPasteModuleName;
public string ToJsonString() => JsonSerializer.Serialize(this, SerializerOptions);
public override string ToString() => ToJsonString();
public bool UpgradeSettingsConfiguration() => false;
}

Просмотреть файл

@ -0,0 +1,36 @@
// 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;
using AdvancedPaste.Helpers;
namespace AdvancedPaste.Models;
public sealed class PasteActionError
{
public static PasteActionError None => new() { Text = string.Empty, Details = string.Empty };
public string Text { get; private init; }
public string Details { get; private init; }
public bool HasText => !string.IsNullOrEmpty(Text);
public bool HasDetails => !string.IsNullOrEmpty(Details);
public static PasteActionError FromResourceId(string resourceId) =>
new()
{
Text = ResourceLoaderInstance.ResourceLoader.GetString(resourceId),
Details = string.Empty,
};
public static PasteActionError FromException(Exception ex) =>
new()
{
Text = ex is PasteActionException ? ex.Message : ResourceLoaderInstance.ResourceLoader.GetString("PasteError"),
Details = (ex as PasteActionException)?.AIServiceMessage ?? string.Empty,
};
}

Просмотреть файл

@ -6,6 +6,7 @@ using System;
namespace AdvancedPaste.Models;
public sealed class PasteActionException(string message) : Exception(message)
public class PasteActionException(string message, Exception innerException, string aiServiceMessage = null) : Exception(message, innerException)
{
public string AIServiceMessage { get; } = aiServiceMessage;
}

Просмотреть файл

@ -0,0 +1,23 @@
// 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 AdvancedPaste.Helpers;
namespace AdvancedPaste.Models;
public sealed class PasteActionModeratedException : PasteActionException
{
public PasteActionModeratedException()
: base(
message: ResourceLoaderInstance.ResourceLoader.GetString("PasteError"),
innerException: null,
aiServiceMessage: ResourceLoaderInstance.ResourceLoader.GetString("PasteActionModerated"))
{
}
/// <summary>
/// Non-localized error description for logs, reports, telemetry etc.
/// </summary>
public const string ErrorDescription = "Paste operation moderated";
}

Просмотреть файл

@ -7,7 +7,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.PowerToys.Settings.UI.Library;
namespace AdvancedPaste.Models;
@ -25,19 +24,21 @@ public sealed class PasteFormat
IsEnabled = SupportsClipboardFormats(clipboardFormats) && (isAIServiceEnabled || !Metadata.RequiresAIService);
}
public PasteFormat(PasteFormats format, ClipboardFormat clipboardFormats, bool isAIServiceEnabled, Func<string, string> resourceLoader)
: this(format, clipboardFormats, isAIServiceEnabled)
{
Name = Metadata.ResourceId == null ? string.Empty : resourceLoader(Metadata.ResourceId);
Prompt = string.Empty;
}
public static PasteFormat CreateStandardFormat(PasteFormats format, ClipboardFormat clipboardFormats, bool isAIServiceEnabled, Func<string, string> resourceLoader) =>
new(format, clipboardFormats, isAIServiceEnabled)
{
Name = MetadataDict[format].ResourceId == null ? string.Empty : resourceLoader(MetadataDict[format].ResourceId),
Prompt = string.Empty,
IsSavedQuery = false,
};
public PasteFormat(AdvancedPasteCustomAction customAction, ClipboardFormat clipboardFormats, bool isAIServiceEnabled)
: this(PasteFormats.Custom, clipboardFormats, isAIServiceEnabled)
{
Name = customAction.Name;
Prompt = customAction.Prompt;
}
public static PasteFormat CreateCustomAIFormat(PasteFormats format, string name, string prompt, bool isSavedQuery, ClipboardFormat clipboardFormats, bool isAIServiceEnabled) =>
new(format, clipboardFormats, isAIServiceEnabled)
{
Name = name,
Prompt = prompt,
IsSavedQuery = isSavedQuery,
};
public PasteFormatMetadataAttribute Metadata => MetadataDict[Format];
@ -49,6 +50,8 @@ public sealed class PasteFormat
public string Prompt { get; private init; }
public bool IsSavedQuery { get; private init; }
public bool IsEnabled { get; private init; }
public double Opacity => IsEnabled ? 1 : 0.5;
@ -59,5 +62,8 @@ public sealed class PasteFormat
public string ShortcutText { get; set; } = string.Empty;
public bool SupportsClipboardFormats(ClipboardFormat clipboardFormats) => (clipboardFormats & Metadata.SupportedClipboardFormats) != ClipboardFormat.None;
public static bool SupportsClipboardFormats(PasteFormats format, ClipboardFormat clipboardFormats)
=> (clipboardFormats & MetadataDict[format].SupportedClipboardFormats) != ClipboardFormat.None;
public bool SupportsClipboardFormats(ClipboardFormat clipboardFormats) => SupportsClipboardFormats(Format, clipboardFormats);
}

Просмотреть файл

@ -17,7 +17,16 @@ public sealed class PasteFormatMetadataAttribute : Attribute
public bool RequiresAIService { get; init; }
public bool CanPreview { get; init; }
public ClipboardFormat SupportedClipboardFormats { get; init; }
public string IPCKey { get; init; }
/// <summary>
/// Gets a description of the action that should be exposed to Semantic Kernel, or <see langword="null"/> if it should not be exposed.
/// </summary>
public string KernelFunctionDescription { get; init; }
public bool RequiresPrompt { get; init; }
}

Просмотреть файл

@ -8,27 +8,96 @@ namespace AdvancedPaste.Models;
public enum PasteFormats
{
[PasteFormatMetadata(IsCoreAction = true, ResourceId = "PasteAsPlainText", IconGlyph = "\uE8E9", RequiresAIService = false, SupportedClipboardFormats = ClipboardFormat.Text)]
[PasteFormatMetadata(
IsCoreAction = true,
ResourceId = "PasteAsPlainText",
IconGlyph = "\uE8E9",
RequiresAIService = false,
CanPreview = false,
SupportedClipboardFormats = ClipboardFormat.Text,
KernelFunctionDescription = "Takes clipboard text and returns it as it is.")]
PlainText,
[PasteFormatMetadata(IsCoreAction = true, ResourceId = "PasteAsMarkdown", IconGlyph = "\ue8a5", RequiresAIService = false, SupportedClipboardFormats = ClipboardFormat.Text)]
[PasteFormatMetadata(
IsCoreAction = true,
ResourceId = "PasteAsMarkdown",
IconGlyph = "\ue8a5",
RequiresAIService = false,
CanPreview = false,
SupportedClipboardFormats = ClipboardFormat.Text,
KernelFunctionDescription = "Takes clipboard text and formats it as markdown text.")]
Markdown,
[PasteFormatMetadata(IsCoreAction = true, ResourceId = "PasteAsJson", IconGlyph = "\uE943", RequiresAIService = false, SupportedClipboardFormats = ClipboardFormat.Text)]
[PasteFormatMetadata(
IsCoreAction = true,
ResourceId = "PasteAsJson",
IconGlyph = "\uE943",
RequiresAIService = false,
CanPreview = false,
SupportedClipboardFormats = ClipboardFormat.Text,
KernelFunctionDescription = "Takes clipboard text and formats it as JSON text.")]
Json,
[PasteFormatMetadata(IsCoreAction = false, ResourceId = "ImageToText", IconGlyph = "\uE91B", RequiresAIService = false, SupportedClipboardFormats = ClipboardFormat.Image | ClipboardFormat.ImageFile, IPCKey = AdvancedPasteAdditionalActions.PropertyNames.ImageToText)]
[PasteFormatMetadata(
IsCoreAction = false,
ResourceId = "ImageToText",
IconGlyph = "\uE91B",
RequiresAIService = false,
CanPreview = true,
SupportedClipboardFormats = ClipboardFormat.Image,
IPCKey = AdvancedPasteAdditionalActions.PropertyNames.ImageToText,
KernelFunctionDescription = "Takes an image in the clipboard and extracts all text from it using OCR.")]
ImageToText,
[PasteFormatMetadata(IsCoreAction = false, ResourceId = "PasteAsTxtFile", IconGlyph = "\uE8D2", RequiresAIService = false, SupportedClipboardFormats = ClipboardFormat.Text | ClipboardFormat.Html, IPCKey = AdvancedPastePasteAsFileAction.PropertyNames.PasteAsTxtFile)]
[PasteFormatMetadata(
IsCoreAction = false,
ResourceId = "PasteAsTxtFile",
IconGlyph = "\uE8D2",
RequiresAIService = false,
CanPreview = false,
SupportedClipboardFormats = ClipboardFormat.Text | ClipboardFormat.Html,
IPCKey = AdvancedPastePasteAsFileAction.PropertyNames.PasteAsTxtFile,
KernelFunctionDescription = "Takes text or HTML data in the clipboard and transforms it to a TXT file.")]
PasteAsTxtFile,
[PasteFormatMetadata(IsCoreAction = false, ResourceId = "PasteAsPngFile", IconGlyph = "\uE8B9", RequiresAIService = false, SupportedClipboardFormats = ClipboardFormat.Image | ClipboardFormat.ImageFile, IPCKey = AdvancedPastePasteAsFileAction.PropertyNames.PasteAsPngFile)]
[PasteFormatMetadata(
IsCoreAction = false,
ResourceId = "PasteAsPngFile",
IconGlyph = "\uE8B9",
RequiresAIService = false,
CanPreview = false,
SupportedClipboardFormats = ClipboardFormat.Image,
IPCKey = AdvancedPastePasteAsFileAction.PropertyNames.PasteAsPngFile,
KernelFunctionDescription = "Takes an image in the clipboard and transforms it to a PNG file.")]
PasteAsPngFile,
[PasteFormatMetadata(IsCoreAction = false, ResourceId = "PasteAsHtmlFile", IconGlyph = "\uF6FA", RequiresAIService = false, SupportedClipboardFormats = ClipboardFormat.Html, IPCKey = AdvancedPastePasteAsFileAction.PropertyNames.PasteAsHtmlFile)]
[PasteFormatMetadata(
IsCoreAction = false,
ResourceId = "PasteAsHtmlFile",
IconGlyph = "\uF6FA",
RequiresAIService = false,
CanPreview = false,
SupportedClipboardFormats = ClipboardFormat.Html,
IPCKey = AdvancedPastePasteAsFileAction.PropertyNames.PasteAsHtmlFile,
KernelFunctionDescription = "Takes HTML data in the clipboard and transforms it to an HTML file.")]
PasteAsHtmlFile,
[PasteFormatMetadata(IsCoreAction = false, IconGlyph = "\uE945", RequiresAIService = true, SupportedClipboardFormats = ClipboardFormat.Text)]
Custom,
[PasteFormatMetadata(
IsCoreAction = false,
IconGlyph = "\uE945",
RequiresAIService = true,
CanPreview = true,
SupportedClipboardFormats = ClipboardFormat.Text | ClipboardFormat.Html | ClipboardFormat.Audio | ClipboardFormat.Image,
RequiresPrompt = true)]
KernelQuery,
[PasteFormatMetadata(
IsCoreAction = false,
IconGlyph = "\uE945",
RequiresAIService = true,
CanPreview = true,
SupportedClipboardFormats = ClipboardFormat.Text,
KernelFunctionDescription = "Takes input instructions and transforms clipboard text (not TXT files) with these input instructions, putting the result back on the clipboard. This uses AI to accomplish the task.",
RequiresPrompt = true)]
CustomTextTransformation,
}

Просмотреть файл

@ -0,0 +1,152 @@
// 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;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using AdvancedPaste.Models.KernelQueryCache;
using AdvancedPaste.Settings;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
namespace AdvancedPaste.Services;
/// <summary>
/// Implements <see cref="IKernelQueryCacheService"/> by only caching queries with prompts
/// that correspond to the user's custom actions or to the localized names of bundled actions.
/// This avoids potential privacy issues and prevents the cache from getting too large.
/// </summary>
public sealed class CustomActionKernelQueryCacheService : IKernelQueryCacheService
{
private const string PersistedCacheFileName = "kernelQueryCache.json";
private readonly HashSet<string> _cacheablePrompts = new(CacheKey.PromptComparer);
private readonly Dictionary<CacheKey, CacheValue> _memoryCache = [];
private readonly IUserSettings _userSettings;
private readonly IFileSystem _fileSystem;
private readonly SettingsUtils _settingsUtil;
private static string Version => Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString() ?? string.Empty;
public CustomActionKernelQueryCacheService(IUserSettings userSettings, IFileSystem fileSystem)
{
_userSettings = userSettings;
_fileSystem = fileSystem;
_settingsUtil = new SettingsUtils(fileSystem);
_userSettings.Changed += OnUserSettingsChanged;
UpdateCacheablePrompts();
_memoryCache = LoadPersistedCacheItems().Where(pair => pair.CacheKey != null)
.GroupBy(pair => pair.CacheKey, pair => pair.CacheValue)
.ToDictionary(group => group.Key, group => group.First());
RemoveInapplicableCacheKeys();
Logger.LogDebug($"Kernel query cache initialized with {_memoryCache.Count} items");
}
public async Task WriteAsync(CacheKey key, CacheValue value)
{
if (_cacheablePrompts.Contains(key.Prompt))
{
_memoryCache[key] = value;
await SaveAsync();
}
}
public CacheValue ReadOrNull(CacheKey key) => _memoryCache.GetValueOrDefault(key);
private List<PersistedCache.CacheItem> LoadPersistedCacheItems()
{
try
{
if (!_settingsUtil.SettingsExists(AdvancedPasteSettings.ModuleName, PersistedCacheFileName))
{
return [];
}
var jsonString = _fileSystem.File.ReadAllText(_settingsUtil.GetSettingsFilePath(AdvancedPasteSettings.ModuleName, PersistedCacheFileName));
var persistedCache = PersistedCache.FromJsonString(jsonString);
if (persistedCache.Version == Version)
{
return persistedCache.Items;
}
else
{
Logger.LogWarning($"Ignoring persisted kernel query cache; version mismatch - actual: {persistedCache.Version}, expected: {Version}");
return [];
}
}
catch (Exception ex)
{
Logger.LogError("Failed to load kernel query cache", ex);
return [];
}
}
private async void OnUserSettingsChanged(object sender, EventArgs e)
{
UpdateCacheablePrompts();
if (RemoveInapplicableCacheKeys())
{
await SaveAsync();
}
}
private void UpdateCacheablePrompts()
{
var localizedActionNames = from pair in PasteFormat.MetadataDict
let format = pair.Key
let metadata = pair.Value
where !string.IsNullOrEmpty(metadata.ResourceId)
where metadata.IsCoreAction || _userSettings.AdditionalActions.Contains(format)
select ResourceLoaderInstance.ResourceLoader.GetString(metadata.ResourceId);
var customActionPrompts = from customAction in _userSettings.CustomActions
select customAction.Prompt;
_cacheablePrompts.Clear();
_cacheablePrompts.UnionWith(localizedActionNames.Concat(customActionPrompts));
}
private bool RemoveInapplicableCacheKeys()
{
var keysToRemove = _memoryCache.Keys
.Where(key => !_cacheablePrompts.Contains(key.Prompt))
.ToList();
foreach (var key in keysToRemove)
{
_memoryCache.Remove(key);
}
return keysToRemove.Count > 0;
}
private async Task SaveAsync()
{
PersistedCache cache = new()
{
Version = Version,
Items = _memoryCache.Select(pair => new PersistedCache.CacheItem(pair.Key, pair.Value)).ToList(),
};
_settingsUtil.SaveSettings(cache.ToJsonString(), AdvancedPasteSettings.ModuleName, PersistedCacheFileName);
Logger.LogDebug($"Kernel query cache saved with {_memoryCache.Count} item(s)");
await Task.CompletedTask; // Async placeholder until _settingsUtil.SaveSettings has an async implementation
}
}

Просмотреть файл

@ -0,0 +1,14 @@
// 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.
namespace AdvancedPaste.Services;
public interface IAICredentialsProvider
{
bool IsConfigured { get; }
string Key { get; }
bool Refresh();
}

Просмотреть файл

@ -0,0 +1,12 @@
// 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.Threading.Tasks;
namespace AdvancedPaste.Services;
public interface ICustomTextTransformService
{
Task<string> TransformTextAsync(string prompt, string inputText);
}

Просмотреть файл

@ -0,0 +1,16 @@
// 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.Threading.Tasks;
using AdvancedPaste.Models.KernelQueryCache;
namespace AdvancedPaste.Services;
public interface IKernelQueryCacheService
{
Task WriteAsync(CacheKey key, CacheValue value);
CacheValue ReadOrNull(CacheKey key);
}

Просмотреть файл

@ -0,0 +1,14 @@
// 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.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
namespace AdvancedPaste.Services;
public interface IKernelService
{
Task<DataPackage> TransformClipboardAsync(string prompt, DataPackageView clipboardData, bool isSavedQuery);
}

Просмотреть файл

@ -3,11 +3,13 @@
// See the LICENSE file in the project root for more information.
using System.Threading.Tasks;
using AdvancedPaste.Models;
using Windows.ApplicationModel.DataTransfer;
namespace AdvancedPaste.Services;
public interface IPasteFormatExecutor
{
Task<string> ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source);
Task<DataPackage> ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source);
}

Просмотреть файл

@ -0,0 +1,12 @@
// 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.Threading.Tasks;
namespace AdvancedPaste.Services;
public interface IPromptModerationService
{
Task ValidateAsync(string fullPrompt);
}

Просмотреть файл

@ -0,0 +1,280 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using AdvancedPaste.Models.KernelQueryCache;
using AdvancedPaste.Telemetry;
using ManagedCommon;
using Microsoft.PowerToys.Telemetry;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Windows.ApplicationModel.DataTransfer;
namespace AdvancedPaste.Services;
public abstract class KernelServiceBase(IKernelQueryCacheService queryCacheService, IPromptModerationService promptModerationService, ICustomTextTransformService customTextTransformService) : IKernelService
{
private const string PromptParameterName = "prompt";
private readonly IKernelQueryCacheService _queryCacheService = queryCacheService;
private readonly IPromptModerationService _promptModerationService = promptModerationService;
private readonly ICustomTextTransformService _customTextTransformService = customTextTransformService;
protected abstract string ModelName { get; }
protected abstract PromptExecutionSettings PromptExecutionSettings { get; }
protected abstract void AddChatCompletionService(IKernelBuilder kernelBuilder);
protected abstract AIServiceUsage GetAIServiceUsage(ChatMessageContent chatMessage);
public async Task<DataPackage> TransformClipboardAsync(string prompt, DataPackageView clipboardData, bool isSavedQuery)
{
Logger.LogTrace();
var kernel = CreateKernel();
kernel.SetDataPackageView(clipboardData);
CacheKey cacheKey = new() { Prompt = prompt, AvailableFormats = await clipboardData.GetAvailableFormatsAsync() };
var maybeCacheValue = _queryCacheService.ReadOrNull(cacheKey);
bool cacheUsed = maybeCacheValue != null;
ChatHistory chatHistory = [];
try
{
(chatHistory, var usage) = cacheUsed ? await ExecuteCachedActionChain(kernel, maybeCacheValue.ActionChain) : await ExecuteAICompletion(kernel, prompt);
LogResult(cacheUsed, isSavedQuery, kernel.GetOrAddActionChain(), usage);
if (kernel.GetLastError() is Exception ex)
{
throw ex;
}
var outputPackage = kernel.GetDataPackage();
if (!(await outputPackage.GetView().HasUsableDataAsync()))
{
throw new InvalidOperationException("No data was returned from the kernel operation");
}
if (!cacheUsed)
{
await _queryCacheService.WriteAsync(cacheKey, new CacheValue(kernel.GetOrAddActionChain()));
}
Logger.LogDebug($"Kernel operation done: \n{FormatChatHistory(chatHistory)}");
return outputPackage;
}
catch (Exception ex)
{
Logger.LogError($"Error executing kernel operation", ex);
Logger.LogError($"Kernel operation Error: \n{FormatChatHistory(chatHistory)}");
AdvancedPasteSemanticKernelErrorEvent errorEvent = new(ex is PasteActionModeratedException ? PasteActionModeratedException.ErrorDescription : ex.Message);
PowerToysTelemetry.Log.WriteEvent(errorEvent);
if (ex is PasteActionException)
{
throw;
}
else
{
var message = ex is HttpOperationException httpOperationEx
? ErrorHelpers.TranslateErrorText((int?)httpOperationEx.StatusCode ?? -1)
: ResourceLoaderInstance.ResourceLoader.GetString("PasteError");
var lastAssistantMessage = chatHistory.LastOrDefault(chatMessage => chatMessage.Role == AuthorRole.Assistant)?.ToString();
throw new PasteActionException(message, innerException: ex, aiServiceMessage: lastAssistantMessage);
}
}
}
private static string GetFullPrompt(ChatHistory initialHistory)
{
if (initialHistory.Count == 0)
{
throw new ArgumentException("Chat history must not be empty", nameof(initialHistory));
}
int numSystemMessages = initialHistory.Count - 1;
var systemMessages = initialHistory.Take(numSystemMessages);
var userPromptMessage = initialHistory.Last();
if (systemMessages.Any(message => message.Role != AuthorRole.System))
{
throw new ArgumentException("Chat history must start with system messages", nameof(initialHistory));
}
if (userPromptMessage.Role != AuthorRole.User)
{
throw new ArgumentException("Chat history must end with a user message", nameof(initialHistory));
}
var newLine = Environment.NewLine;
var combinedSystemMessage = string.Join(newLine, systemMessages.Select(message => message.Content));
return $"{combinedSystemMessage}{newLine}{newLine}User instructions:{newLine}{userPromptMessage.Content}";
}
private async Task<(ChatHistory ChatHistory, AIServiceUsage Usage)> ExecuteAICompletion(Kernel kernel, string prompt)
{
ChatHistory chatHistory = [];
chatHistory.AddSystemMessage("""
You are an agent who is tasked with helping users paste their clipboard data. You have functions available to help you with this 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.
You will not directly see the output clipboard content, and do not need to provide it in the chat. You just need to do the transform operations as needed.
If you are unable to fulfill the request, end with an error message in the language of the user's request.
""");
chatHistory.AddSystemMessage($"Available clipboard formats: {await kernel.GetDataFormatsAsync()}");
chatHistory.AddUserMessage(prompt);
await _promptModerationService.ValidateAsync(GetFullPrompt(chatHistory));
var chatResult = await kernel.GetRequiredService<IChatCompletionService>()
.GetChatMessageContentAsync(chatHistory, PromptExecutionSettings, kernel);
chatHistory.Add(chatResult);
var totalUsage = chatHistory.Select(GetAIServiceUsage)
.Aggregate(AIServiceUsage.Add);
return (chatHistory, totalUsage);
}
private async Task<(ChatHistory ChatHistory, AIServiceUsage Usage)> ExecuteCachedActionChain(Kernel kernel, List<ActionChainItem> actionChain)
{
foreach (var item in actionChain)
{
if (item.Arguments.Count > 0)
{
await ExecutePromptTransformAsync(kernel, item.Format, item.Arguments[PromptParameterName]);
}
else
{
await ExecuteStandardTransformAsync(kernel, item.Format);
}
}
return ([], AIServiceUsage.None);
}
private void LogResult(bool cacheUsed, bool isSavedQuery, IEnumerable<ActionChainItem> actionChain, AIServiceUsage usage)
{
AdvancedPasteSemanticKernelFormatEvent telemetryEvent = new(cacheUsed, isSavedQuery, usage.PromptTokens, usage.CompletionTokens, ModelName, AdvancedPasteSemanticKernelFormatEvent.FormatActionChain(actionChain));
PowerToysTelemetry.Log.WriteEvent(telemetryEvent);
var logEvent = new { telemetryEvent.CacheUsed, telemetryEvent.IsSavedQuery, telemetryEvent.PromptTokens, telemetryEvent.CompletionTokens, telemetryEvent.ModelName, telemetryEvent.ActionChain };
Logger.LogDebug($"{nameof(TransformClipboardAsync)} complete; {JsonSerializer.Serialize(logEvent)}");
}
private Kernel CreateKernel()
{
var kernelBuilder = Kernel.CreateBuilder();
AddChatCompletionService(kernelBuilder);
kernelBuilder.Plugins.AddFromFunctions("Actions", GetKernelFunctions());
return kernelBuilder.Build();
}
private IEnumerable<KernelFunction> GetKernelFunctions() =>
from format in Enum.GetValues<PasteFormats>()
let metadata = PasteFormat.MetadataDict[format]
let coreDescription = metadata.KernelFunctionDescription
where !string.IsNullOrEmpty(coreDescription)
let requiresPrompt = metadata.RequiresPrompt
orderby requiresPrompt descending
select KernelFunctionFactory.CreateFromMethod(
method: requiresPrompt ? async (Kernel kernel, string prompt) => await ExecutePromptTransformAsync(kernel, format, prompt)
: async (Kernel kernel) => await ExecuteStandardTransformAsync(kernel, format),
functionName: format.ToString(),
description: requiresPrompt ? coreDescription : $"{coreDescription} Puts the result back on the clipboard.",
parameters: requiresPrompt ? [new(PromptParameterName) { Description = "Input instructions to AI", ParameterType = typeof(string) }] : null,
returnParameter: new() { Description = "Array of available clipboard formats after operation" });
private Task<string> ExecutePromptTransformAsync(Kernel kernel, PasteFormats format, string prompt) =>
ExecuteTransformAsync(
kernel,
new ActionChainItem(format, Arguments: new() { { PromptParameterName, prompt } }),
async dataPackageView =>
{
var input = await dataPackageView.GetTextAsync();
string output = await GetPromptBasedOutput(format, prompt, input);
return DataPackageHelpers.CreateFromText(output);
});
private async Task<string> GetPromptBasedOutput(PasteFormats format, string prompt, string input) =>
format switch
{
PasteFormats.CustomTextTransformation => await _customTextTransformService.TransformTextAsync(prompt, input),
_ => throw new ArgumentException($"Unsupported format {format} for prompt transform", nameof(format)),
};
private Task<string> ExecuteStandardTransformAsync(Kernel kernel, PasteFormats format) =>
ExecuteTransformAsync(
kernel,
new ActionChainItem(format, Arguments: []),
async dataPackageView => await TransformHelpers.TransformAsync(format, dataPackageView));
private static async Task<string> ExecuteTransformAsync(Kernel kernel, ActionChainItem actionChainItem, Func<DataPackageView, Task<DataPackage>> transformFunc)
{
kernel.GetOrAddActionChain().Add(actionChainItem);
kernel.SetLastError(null);
try
{
var input = kernel.GetDataPackageView();
var output = await transformFunc(input);
kernel.SetDataPackage(output);
return await kernel.GetDataFormatsAsync();
}
catch (Exception ex)
{
kernel.SetLastError(ex);
throw;
}
}
private string FormatChatHistory(ChatHistory chatHistory) =>
chatHistory.Count == 0 ? "[No chat history]" : string.Join(Environment.NewLine, chatHistory.Select(FormatChatMessage));
private string FormatChatMessage(ChatMessageContent chatMessage)
{
static string Redact(object data) =>
#if DEBUG
data?.ToString();
#else
"[Redacted]";
#endif
static string FormatKernelArguments(KernelArguments kernelArguments) =>
string.Join(", ", kernelArguments?.Select(argument => $"{argument.Key}: {Redact(argument.Value)}") ?? []);
static string FormatKernelContent(KernelContent kernelContent) =>
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
kernelContent switch
{
FunctionCallContent functionCallContent => $"{functionCallContent.FunctionName}({FormatKernelArguments(functionCallContent.Arguments)})",
FunctionResultContent functionResultContent => functionResultContent.FunctionName,
_ => kernelContent.ToString(),
};
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
var role = chatMessage.Role;
var content = string.Join(" / ", chatMessage.Items.Select(FormatKernelContent));
var redactedContent = role == AuthorRole.System || role == AuthorRole.Tool ? content : Redact(content);
var usage = GetAIServiceUsage(chatMessage);
var usageString = usage.HasUsage ? $" [{usage}]" : string.Empty;
return $"-> {role}: {redactedContent}{usageString}";
}
}

Просмотреть файл

@ -0,0 +1,111 @@
// 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;
using System.Text.Json;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using AdvancedPaste.Telemetry;
using Azure;
using Azure.AI.OpenAI;
using ManagedCommon;
using Microsoft.PowerToys.Telemetry;
namespace AdvancedPaste.Services.OpenAI;
public sealed class CustomTextTransformService(IAICredentialsProvider aiCredentialsProvider, IPromptModerationService promptModerationService) : ICustomTextTransformService
{
private const string ModelName = "gpt-3.5-turbo-instruct";
private readonly IAICredentialsProvider _aiCredentialsProvider = aiCredentialsProvider;
private readonly IPromptModerationService _promptModerationService = promptModerationService;
private async Task<Completions> GetAICompletionAsync(string systemInstructions, string userMessage)
{
var fullPrompt = systemInstructions + "\n\n" + userMessage;
await _promptModerationService.ValidateAsync(fullPrompt);
OpenAIClient azureAIClient = new(_aiCredentialsProvider.Key);
var response = await azureAIClient.GetCompletionsAsync(
new()
{
DeploymentName = ModelName,
Prompts =
{
fullPrompt,
},
Temperature = 0.01F,
MaxTokens = 2000,
});
if (response.Value.Choices[0].FinishReason == "length")
{
Logger.LogDebug("Cut off due to length constraints");
}
return response;
}
public async Task<string> TransformTextAsync(string prompt, string inputText)
{
if (string.IsNullOrWhiteSpace(prompt))
{
return string.Empty;
}
if (string.IsNullOrWhiteSpace(inputText))
{
Logger.LogWarning("Clipboard has no usable text data");
return string.Empty;
}
string systemInstructions =
$@"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.";
string userMessage =
$@"User instructions:
{prompt}
Clipboard Content:
{inputText}
Output:
";
try
{
var response = await GetAICompletionAsync(systemInstructions, userMessage);
var usage = response.Usage;
AdvancedPasteGenerateCustomFormatEvent telemetryEvent = new(usage.PromptTokens, usage.CompletionTokens, ModelName);
PowerToysTelemetry.Log.WriteEvent(telemetryEvent);
var logEvent = new { telemetryEvent.PromptTokens, telemetryEvent.CompletionTokens, telemetryEvent.ModelName };
Logger.LogDebug($"{nameof(TransformTextAsync)} complete; {JsonSerializer.Serialize(logEvent)}");
return response.Choices[0].Text;
}
catch (Exception ex)
{
Logger.LogError($"{nameof(TransformTextAsync)} failed", ex);
AdvancedPasteGenerateCustomErrorEvent errorEvent = new(ex is PasteActionModeratedException ? PasteActionModeratedException.ErrorDescription : ex.Message);
PowerToysTelemetry.Log.WriteEvent(errorEvent);
if (ex is PasteActionException)
{
throw;
}
else
{
throw new PasteActionException(ErrorHelpers.TranslateErrorText((ex as RequestFailedException)?.Status ?? -1), ex);
}
}
}
}

Просмотреть файл

@ -0,0 +1,34 @@
// 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.Collections.Generic;
using AdvancedPaste.Models;
using Azure.AI.OpenAI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
namespace AdvancedPaste.Services.OpenAI;
public sealed class KernelService(IKernelQueryCacheService queryCacheService, IAICredentialsProvider aiCredentialsProvider, IPromptModerationService promptModerationService, ICustomTextTransformService customTextTransformService) :
KernelServiceBase(queryCacheService, promptModerationService, customTextTransformService)
{
private readonly IAICredentialsProvider _aiCredentialsProvider = aiCredentialsProvider;
protected override string ModelName => "gpt-4o";
protected override PromptExecutionSettings PromptExecutionSettings =>
new OpenAIPromptExecutionSettings()
{
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
Temperature = 0.01,
};
protected override void AddChatCompletionService(IKernelBuilder kernelBuilder) => kernelBuilder.AddOpenAIChatCompletion(ModelName, _aiCredentialsProvider.Key);
protected override AIServiceUsage GetAIServiceUsage(ChatMessageContent chatMessage) =>
chatMessage.Metadata?.GetValueOrDefault("Usage") is CompletionsUsage completionsUsage
? new(PromptTokens: completionsUsage.PromptTokens, CompletionTokens: completionsUsage.CompletionTokens)
: AIServiceUsage.None;
}

Просмотреть файл

@ -0,0 +1,41 @@
// 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.ClientModel;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using ManagedCommon;
using OpenAI.Moderations;
namespace AdvancedPaste.Services.OpenAI;
public sealed class PromptModerationService(IAICredentialsProvider aiCredentialsProvider) : IPromptModerationService
{
private const string ModelName = "omni-moderation-latest";
private readonly IAICredentialsProvider _aiCredentialsProvider = aiCredentialsProvider;
public async Task ValidateAsync(string fullPrompt)
{
try
{
ModerationClient moderationClient = new(ModelName, _aiCredentialsProvider.Key);
var moderationClientResult = await moderationClient.ClassifyTextAsync(fullPrompt);
var moderationResult = moderationClientResult.Value;
Logger.LogDebug($"{nameof(PromptModerationService)}.{nameof(ValidateAsync)} complete; {nameof(moderationResult.Flagged)}={moderationResult.Flagged}");
if (moderationResult.Flagged)
{
throw new PasteActionModeratedException();
}
}
catch (ClientResultException ex)
{
throw new PasteActionException(ErrorHelpers.TranslateErrorText(ex.Status), ex);
}
}
}

Просмотреть файл

@ -0,0 +1,37 @@
// 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;
using Windows.Security.Credentials;
namespace AdvancedPaste.Services.OpenAI;
public sealed class VaultCredentialsProvider : IAICredentialsProvider
{
public VaultCredentialsProvider() => Refresh();
public string Key { get; private set; }
public bool IsConfigured => !string.IsNullOrEmpty(Key);
public bool Refresh()
{
var oldKey = Key;
Key = LoadKey();
return oldKey != Key;
}
private static string LoadKey()
{
try
{
return new PasswordVault().Retrieve("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey")?.Password ?? string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}
}

Просмотреть файл

@ -3,74 +3,41 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AdvancedPaste.Helpers;
using AdvancedPaste.Models;
using ManagedCommon;
using Microsoft.PowerToys.Telemetry;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
namespace AdvancedPaste.Services;
public sealed class PasteFormatExecutor(AICompletionsHelper aiHelper) : IPasteFormatExecutor
public sealed class PasteFormatExecutor(IKernelService kernelService, ICustomTextTransformService customTextTransformService) : IPasteFormatExecutor
{
private readonly AICompletionsHelper _aiHelper = aiHelper;
private readonly IKernelService _kernelService = kernelService;
private readonly ICustomTextTransformService _customTextTransformService = customTextTransformService;
public async Task<string> ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source)
public async Task<DataPackage> ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source)
{
if (!pasteFormat.IsEnabled)
{
return null;
}
WriteTelemetry(pasteFormat.Format, source);
var format = pasteFormat.Format;
return await ExecutePasteFormatCoreAsync(pasteFormat, Clipboard.GetContent());
}
WriteTelemetry(format, source);
private async Task<string> ExecutePasteFormatCoreAsync(PasteFormat pasteFormat, DataPackageView clipboardData)
{
switch (pasteFormat.Format)
{
case PasteFormats.PlainText:
ToPlainText(clipboardData);
return null;
var clipboardData = Clipboard.GetContent();
case PasteFormats.Markdown:
ToMarkdown(clipboardData);
return null;
case PasteFormats.Json:
ToJson(clipboardData);
return null;
case PasteFormats.ImageToText:
await ImageToTextAsync(clipboardData);
return null;
case PasteFormats.PasteAsTxtFile:
await ToTxtFileAsync(clipboardData);
return null;
case PasteFormats.PasteAsPngFile:
await ToPngFileAsync(clipboardData);
return null;
case PasteFormats.PasteAsHtmlFile:
await ToHtmlFileAsync(clipboardData);
return null;
case PasteFormats.Custom:
return await ToCustomAsync(pasteFormat.Prompt, clipboardData);
default:
throw new ArgumentException($"Unknown paste format {pasteFormat.Format}", nameof(pasteFormat));
}
// Run on thread-pool; although we use Async routines consistently, some actions still occasionally take a long time without yielding.
return await Task.Run(async () =>
pasteFormat.Format switch
{
PasteFormats.KernelQuery => await _kernelService.TransformClipboardAsync(pasteFormat.Prompt, clipboardData, pasteFormat.IsSavedQuery),
PasteFormats.CustomTextTransformation => DataPackageHelpers.CreateFromText(await _customTextTransformService.TransformTextAsync(pasteFormat.Prompt, await clipboardData.GetTextAsync())),
_ => await TransformHelpers.TransformAsync(format, clipboardData),
});
}
private static void WriteTelemetry(PasteFormats format, PasteActionSource source)
@ -93,161 +60,4 @@ public sealed class PasteFormatExecutor(AICompletionsHelper aiHelper) : IPasteFo
throw new ArgumentOutOfRangeException(nameof(format));
}
}
private void ToPlainText(DataPackageView clipboardData)
{
Logger.LogTrace();
SetClipboardTextContent(MarkdownHelper.PasteAsPlainTextFromClipboard(clipboardData));
}
private void ToMarkdown(DataPackageView clipboardData)
{
Logger.LogTrace();
SetClipboardTextContent(MarkdownHelper.ToMarkdown(clipboardData));
}
private void ToJson(DataPackageView clipboardData)
{
Logger.LogTrace();
SetClipboardTextContent(JsonHelper.ToJsonFromXmlOrCsv(clipboardData));
}
private async Task ImageToTextAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var bitmap = await ClipboardHelper.GetClipboardImageContentAsync(clipboardData);
var text = await OcrHelpers.ExtractTextAsync(bitmap);
SetClipboardTextContent(text);
}
private async Task ToPngFileAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var clipboardBitmap = await ClipboardHelper.GetClipboardImageContentAsync(clipboardData);
using var pngStream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, pngStream);
encoder.SetSoftwareBitmap(clipboardBitmap);
await encoder.FlushAsync();
await SetClipboardFileContentAsync(pngStream.AsStreamForRead(), "png");
}
private async Task ToTxtFileAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var text = await ClipboardHelper.GetClipboardTextOrHtmlTextAsync(clipboardData);
await SetClipboardFileContentAsync(text, "txt");
}
private async Task ToHtmlFileAsync(DataPackageView clipboardData)
{
Logger.LogTrace();
var cfHtml = await ClipboardHelper.GetClipboardHtmlContentAsync(clipboardData);
var html = RemoveHtmlMetadata(cfHtml);
await SetClipboardFileContentAsync(html, "html");
}
/// <summary>
/// Removes leading CF_HTML metadata from HTML clipboard data.
/// See: https://learn.microsoft.com/en-us/windows/win32/dataxchg/html-clipboard-format
/// </summary>
private static string RemoveHtmlMetadata(string cfHtml)
{
int? GetIntTagValue(string tagName)
{
var tagNameWithColon = tagName + ":";
int tagStartPos = cfHtml.IndexOf(tagNameWithColon, StringComparison.InvariantCulture);
const int tagValueLength = 10;
return tagStartPos != -1 && int.TryParse(cfHtml.AsSpan(tagStartPos + tagNameWithColon.Length, tagValueLength), CultureInfo.InvariantCulture, out int result) ? result : null;
}
var startFragmentIndex = GetIntTagValue("StartFragment");
var endFragmentIndex = GetIntTagValue("EndFragment");
return (startFragmentIndex == null || endFragmentIndex == null) ? cfHtml : cfHtml[startFragmentIndex.Value..endFragmentIndex.Value];
}
private static async Task SetClipboardFileContentAsync(string data, string fileExtension)
{
if (string.IsNullOrEmpty(data))
{
throw new ArgumentException($"Empty value in {nameof(SetClipboardFileContentAsync)}", nameof(data));
}
var path = GetPasteAsFileTempFilePath(fileExtension);
await File.WriteAllTextAsync(path, data);
await ClipboardHelper.SetClipboardFileContentAsync(path);
}
private static async Task SetClipboardFileContentAsync(Stream stream, string fileExtension)
{
var path = GetPasteAsFileTempFilePath(fileExtension);
using var fileStream = File.Create(path);
await stream.CopyToAsync(fileStream);
await ClipboardHelper.SetClipboardFileContentAsync(path);
}
private static string GetPasteAsFileTempFilePath(string fileExtension)
{
var prefix = ResourceLoaderInstance.ResourceLoader.GetString("PasteAsFile_FilePrefix");
var timestamp = DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);
return Path.Combine(Path.GetTempPath(), $"{prefix}{timestamp}.{fileExtension}");
}
private async Task<string> ToCustomAsync(string prompt, DataPackageView clipboardData)
{
Logger.LogTrace();
if (string.IsNullOrWhiteSpace(prompt))
{
return string.Empty;
}
if (!clipboardData.Contains(StandardDataFormats.Text))
{
Logger.LogWarning("Clipboard does not contain text data");
return string.Empty;
}
var currentClipboardText = await clipboardData.GetTextAsync();
if (string.IsNullOrWhiteSpace(currentClipboardText))
{
Logger.LogWarning("Clipboard has no usable text data");
return string.Empty;
}
var aiResponse = await Task.Run(() => _aiHelper.AIFormatString(prompt, currentClipboardText));
return aiResponse.ApiRequestStatus == (int)HttpStatusCode.OK
? aiResponse.Response
: throw new PasteActionException(TranslateErrorText(aiResponse.ApiRequestStatus));
}
private void SetClipboardTextContent(string content)
{
if (!string.IsNullOrEmpty(content))
{
ClipboardHelper.SetClipboardTextContent(content);
}
}
private 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),
};
}

Просмотреть файл

@ -123,9 +123,6 @@
<data name="ClipboardEmptyWarning" xml:space="preserve">
<value>Clipboard does not contain any usable formats</value>
</data>
<data name="ClipboardDataNotTextWarning" xml:space="preserve">
<value>Clipboard data is not text</value>
</data>
<data name="OpenAINotConfigured" xml:space="preserve">
<value>To custom with AI is not enabled</value>
</data>
@ -140,7 +137,10 @@
</data>
<data name="PasteError" xml:space="preserve">
<value>An error occurred during the paste operation</value>
</data>
</data>
<data name="PasteActionModerated" xml:space="preserve">
<value>The paste operation was moderated due to sensitive content. Please try another query.</value>
</data>
<data name="ClipboardHistoryButton.Text" xml:space="preserve">
<value>Clipboard history</value>
</data>
@ -213,6 +213,9 @@
<data name="SettingsBtn.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
<value>Open settings</value>
</data>
<data name="AIErrorMessage.Header" xml:space="preserve">
<value>The AI assistant provided the following message:</value>
</data>
<data name="ThumbsDownFeedback.Text" xml:space="preserve">
<value>Thumbs down feedback</value>
</data>
@ -248,5 +251,5 @@
</data>
<data name="PasteAsFile_FilePrefix" xml:space="preserve">
<value>PowerToys_Paste_</value>
</data>
</data>
</root>

Просмотреть файл

@ -0,0 +1,18 @@
// 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.Diagnostics.Tracing;
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
namespace AdvancedPaste.Telemetry;
[EventData]
public class AdvancedPasteSemanticKernelErrorEvent(string error) : EventBase, IEvent
{
public string Error { get; set; } = error;
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
}

Просмотреть файл

@ -0,0 +1,39 @@
// 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.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using AdvancedPaste.Models;
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
namespace AdvancedPaste.Telemetry;
[EventData]
public class AdvancedPasteSemanticKernelFormatEvent(bool cacheUsed, bool isSavedQuery, int promptTokens, int completionTokens, string modelName, string actionChain) : EventBase, IEvent
{
public static string FormatActionChain(IEnumerable<ActionChainItem> actionChain) => FormatActionChain(actionChain.Select(item => item.Format));
public static string FormatActionChain(IEnumerable<PasteFormats> actionChain) => string.Join(", ", actionChain);
public bool IsSavedQuery { get; set; } = isSavedQuery;
public bool CacheUsed { get; set; } = cacheUsed;
public int PromptTokens { get; set; } = promptTokens;
public int CompletionTokens { get; set; } = completionTokens;
public string ModelName { get; set; } = modelName;
/// <summary>
/// Gets or sets a comma-separated list of paste formats used - in the same order they were executed.
/// Conceptually an array but formatted this way to work around https://github.com/dotnet/runtime/issues/10428
/// </summary>
public string ActionChain { get; set; } = actionChain;
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
}

Просмотреть файл

@ -5,6 +5,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Linq;
using System.Threading.Tasks;
@ -33,28 +35,29 @@ namespace AdvancedPaste.ViewModels
private readonly DispatcherTimer _clipboardTimer;
private readonly IUserSettings _userSettings;
private readonly IPasteFormatExecutor _pasteFormatExecutor;
private readonly AICompletionsHelper _aiHelper;
private readonly IAICredentialsProvider _aiCredentialsProvider;
public DataPackageView ClipboardData { get; set; }
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsCustomAIEnabled))]
[NotifyPropertyChangedFor(nameof(IsCustomAIAvailable))]
[NotifyPropertyChangedFor(nameof(ClipboardHasData))]
[NotifyPropertyChangedFor(nameof(ClipboardHasDataForCustomAI))]
[NotifyPropertyChangedFor(nameof(InputTxtBoxPlaceholderText))]
[NotifyPropertyChangedFor(nameof(AIDisabledErrorText))]
[NotifyPropertyChangedFor(nameof(CustomAIUnavailableErrorText))]
private ClipboardFormat _availableClipboardFormats;
[ObservableProperty]
private bool _clipboardHistoryEnabled;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(AIDisabledErrorText))]
[NotifyPropertyChangedFor(nameof(IsAIServiceEnabled))]
[NotifyPropertyChangedFor(nameof(IsCustomAIEnabled))]
[NotifyPropertyChangedFor(nameof(CustomAIUnavailableErrorText))]
[NotifyPropertyChangedFor(nameof(IsCustomAIServiceEnabled))]
[NotifyPropertyChangedFor(nameof(IsCustomAIAvailable))]
private bool _isAllowedByGPO;
[ObservableProperty]
private string _pasteOperationErrorText;
private PasteActionError _pasteActionError = PasteActionError.None;
[ObservableProperty]
private string _query = string.Empty;
@ -68,21 +71,25 @@ namespace AdvancedPaste.ViewModels
public ObservableCollection<PasteFormat> CustomActionPasteFormats { get; } = [];
public bool IsAIServiceEnabled => IsAllowedByGPO && _aiHelper.IsAIEnabled;
public bool IsCustomAIServiceEnabled => IsAllowedByGPO && _aiCredentialsProvider.IsConfigured;
public bool IsCustomAIEnabled => IsAIServiceEnabled && ClipboardHasText;
public bool IsCustomAIAvailable => IsCustomAIServiceEnabled && ClipboardHasDataForCustomAI;
public bool IsAdvancedAIEnabled => IsCustomAIServiceEnabled && _userSettings.IsAdvancedAIEnabled;
public bool ClipboardHasData => AvailableClipboardFormats != ClipboardFormat.None;
private bool ClipboardHasText => AvailableClipboardFormats.HasFlag(ClipboardFormat.Text);
public bool ClipboardHasDataForCustomAI => PasteFormat.SupportsClipboardFormats(CustomAIFormat, AvailableClipboardFormats);
private PasteFormats CustomAIFormat => _userSettings.IsAdvancedAIEnabled ? PasteFormats.KernelQuery : PasteFormats.CustomTextTransformation;
private bool Visible => GetMainWindow()?.Visible is true;
public event EventHandler<CustomActionActivatedEventArgs> CustomActionActivated;
public event EventHandler PreviewRequested;
public OptionsViewModel(AICompletionsHelper aiHelper, IUserSettings userSettings, IPasteFormatExecutor pasteFormatExecutor)
public OptionsViewModel(IFileSystem fileSystem, IAICredentialsProvider aiCredentialsProvider, IUserSettings userSettings, IPasteFormatExecutor pasteFormatExecutor)
{
_aiHelper = aiHelper;
_aiCredentialsProvider = aiCredentialsProvider;
_userSettings = userSettings;
_pasteFormatExecutor = pasteFormatExecutor;
@ -100,16 +107,25 @@ namespace AdvancedPaste.ViewModels
_clipboardTimer.Start();
RefreshPasteFormats();
_userSettings.Changed += (_, _) => EnqueueRefreshPasteFormats();
_userSettings.Changed += UserSettings_Changed;
PropertyChanged += (_, e) =>
{
string[] dirtyingProperties = [nameof(Query), nameof(IsAIServiceEnabled), nameof(IsCustomAIEnabled), nameof(AvailableClipboardFormats)];
string[] dirtyingProperties = [nameof(Query), nameof(IsCustomAIServiceEnabled), nameof(IsCustomAIAvailable), nameof(AvailableClipboardFormats)];
if (dirtyingProperties.Contains(e.PropertyName))
{
EnqueueRefreshPasteFormats();
}
};
try
{
// Delete file that is no longer needed but might have been written by previous version and contain sensitive information.
fileSystem.File.Delete(new SettingsUtils(fileSystem).GetSettingsFilePath(Constants.AdvancedPasteModuleName, "lastQuery.json"));
}
catch
{
}
}
private static MainWindow GetMainWindow() => (App.Current as App)?.GetMainWindow();
@ -123,6 +139,15 @@ namespace AdvancedPaste.ViewModels
}
}
private void UserSettings_Changed(object sender, EventArgs e)
{
OnPropertyChanged(nameof(ClipboardHasDataForCustomAI));
OnPropertyChanged(nameof(IsCustomAIAvailable));
OnPropertyChanged(nameof(IsAdvancedAIEnabled));
EnqueueRefreshPasteFormats();
}
private void EnqueueRefreshPasteFormats()
{
if (_pasteFormatsDirty)
@ -138,9 +163,11 @@ namespace AdvancedPaste.ViewModels
});
}
private PasteFormat CreatePasteFormat(PasteFormats format) => new(format, AvailableClipboardFormats, IsAIServiceEnabled, ResourceLoaderInstance.ResourceLoader.GetString);
private PasteFormat CreateStandardPasteFormat(PasteFormats format) =>
PasteFormat.CreateStandardFormat(format, AvailableClipboardFormats, IsCustomAIServiceEnabled, ResourceLoaderInstance.ResourceLoader.GetString);
private PasteFormat CreatePasteFormat(AdvancedPasteCustomAction customAction) => new(customAction, AvailableClipboardFormats, IsAIServiceEnabled);
private PasteFormat CreateCustomAIPasteFormat(string name, string prompt, bool isSavedQuery) =>
PasteFormat.CreateCustomAIFormat(CustomAIFormat, name, prompt, isSavedQuery, AvailableClipboardFormats, IsCustomAIServiceEnabled);
private void RefreshPasteFormats()
{
@ -177,9 +204,11 @@ namespace AdvancedPaste.ViewModels
UpdateFormats(StandardPasteFormats, Enum.GetValues<PasteFormats>()
.Where(format => PasteFormat.MetadataDict[format].IsCoreAction || _userSettings.AdditionalActions.Contains(format))
.Select(CreatePasteFormat));
.Select(CreateStandardPasteFormat));
UpdateFormats(CustomActionPasteFormats, IsAIServiceEnabled ? _userSettings.CustomActions.Select(CreatePasteFormat) : []);
UpdateFormats(
CustomActionPasteFormats,
IsCustomAIServiceEnabled ? _userSettings.CustomActions.Select(customAction => CreateCustomAIPasteFormat(customAction.Name, customAction.Prompt, isSavedQuery: true)) : []);
}
public void Dispose()
@ -196,12 +225,12 @@ namespace AdvancedPaste.ViewModels
}
ClipboardData = Clipboard.GetContent();
AvailableClipboardFormats = await ClipboardHelper.GetAvailableClipboardFormatsAsync(ClipboardData);
AvailableClipboardFormats = await ClipboardData.GetAvailableFormatsAsync();
}
public async Task OnShowAsync()
{
PasteOperationErrorText = string.Empty;
PasteActionError = PasteActionError.None;
Query = string.Empty;
await ReadClipboardAsync();
@ -212,11 +241,12 @@ namespace AdvancedPaste.ViewModels
_dispatcherQueue.TryEnqueue(() =>
{
GetMainWindow()?.FinishLoading(_aiHelper.IsAIEnabled);
GetMainWindow()?.FinishLoading(_aiCredentialsProvider.IsConfigured);
OnPropertyChanged(nameof(InputTxtBoxPlaceholderText));
OnPropertyChanged(nameof(AIDisabledErrorText));
OnPropertyChanged(nameof(IsAIServiceEnabled));
OnPropertyChanged(nameof(IsCustomAIEnabled));
OnPropertyChanged(nameof(CustomAIUnavailableErrorText));
OnPropertyChanged(nameof(IsCustomAIServiceEnabled));
OnPropertyChanged(nameof(IsAdvancedAIEnabled));
OnPropertyChanged(nameof(IsCustomAIAvailable));
});
}
@ -251,24 +281,24 @@ namespace AdvancedPaste.ViewModels
public string InputTxtBoxPlaceholderText
=> ResourceLoaderInstance.ResourceLoader.GetString(ClipboardHasData ? "CustomFormatTextBox/PlaceholderText" : "ClipboardEmptyWarning");
public string AIDisabledErrorText
public string CustomAIUnavailableErrorText
{
get
{
if (!ClipboardHasText)
{
return ResourceLoaderInstance.ResourceLoader.GetString("ClipboardDataNotTextWarning");
}
if (!IsAllowedByGPO)
{
return ResourceLoaderInstance.ResourceLoader.GetString("OpenAIGpoDisabled");
}
if (!_aiHelper.IsAIEnabled)
if (!_aiCredentialsProvider.IsConfigured)
{
return ResourceLoaderInstance.ResourceLoader.GetString("OpenAINotConfigured");
}
if (!ClipboardHasDataForCustomAI)
{
return ResourceLoaderInstance.ResourceLoader.GetString("ClipboardEmptyWarning");
}
else
{
return string.Empty;
@ -280,24 +310,22 @@ namespace AdvancedPaste.ViewModels
private string _customFormatResult;
[RelayCommand]
public void PasteCustom()
public async Task PasteCustomAsync()
{
var text = GeneratedResponses.ElementAtOrDefault(CurrentResponseIndex);
if (!string.IsNullOrEmpty(text))
{
ClipboardHelper.SetClipboardTextContent(text);
HideWindow();
if (_userSettings.SendPasteKeyCombination)
{
ClipboardHelper.SendPasteKeyCombination();
}
Query = string.Empty;
await CopyPasteAndHideAsync(DataPackageHelpers.CreateFromText(text));
}
}
private async Task CopyPasteAndHideAsync(DataPackage package)
{
await ClipboardHelper.TryCopyPasteAsync(package, HideWindow);
Query = string.Empty;
}
// Command to select the previous custom format
[RelayCommand]
public void PreviousCustomFormat()
@ -329,7 +357,7 @@ namespace AdvancedPaste.ViewModels
internal async Task ExecutePasteFormatAsync(PasteFormats format, PasteActionSource source)
{
await ReadClipboardAsync();
await ExecutePasteFormatAsync(CreatePasteFormat(format), source);
await ExecutePasteFormatAsync(CreateStandardPasteFormat(format), source);
}
internal async Task ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source)
@ -342,59 +370,49 @@ namespace AdvancedPaste.ViewModels
if (!pasteFormat.IsEnabled)
{
var resourceId = pasteFormat.SupportsClipboardFormats(AvailableClipboardFormats) ? "PasteError" : "ClipboardEmptyWarning";
PasteOperationErrorText = ResourceLoaderInstance.ResourceLoader.GetString(resourceId);
PasteActionError = PasteActionError.FromResourceId(pasteFormat.SupportsClipboardFormats(AvailableClipboardFormats) ? "PasteError" : "ClipboardEmptyWarning");
return;
}
Busy = true;
PasteOperationErrorText = string.Empty;
Query = pasteFormat.Query;
var elapsedWatch = Stopwatch.StartNew();
Logger.LogDebug($"Started executing {pasteFormat.Format} from source {source}");
if (pasteFormat.Format == PasteFormats.Custom)
{
SaveQuery(Query);
}
Busy = true;
PasteActionError = PasteActionError.None;
Query = pasteFormat.Query;
try
{
// Minimum time to show busy spinner for AI actions when triggered by global keyboard shortcut.
var aiActionMinTaskTime = TimeSpan.FromSeconds(2);
var delayTask = (Visible && source == PasteActionSource.GlobalKeyboardShortcut) ? Task.Delay(aiActionMinTaskTime) : Task.CompletedTask;
var aiOutput = await _pasteFormatExecutor.ExecutePasteFormatAsync(pasteFormat, source);
var dataPackage = await _pasteFormatExecutor.ExecutePasteFormatAsync(pasteFormat, source);
await delayTask;
if (pasteFormat.Format != PasteFormats.Custom)
{
HideWindow();
var outputText = await dataPackage.GetView().GetTextOrEmptyAsync();
bool shouldPreview = pasteFormat.Metadata.CanPreview && _userSettings.ShowCustomPreview && !string.IsNullOrEmpty(outputText) && source != PasteActionSource.GlobalKeyboardShortcut;
if (source == PasteActionSource.GlobalKeyboardShortcut || _userSettings.SendPasteKeyCombination)
{
ClipboardHelper.SendPasteKeyCombination();
}
if (shouldPreview)
{
GeneratedResponses.Add(outputText);
CurrentResponseIndex = GeneratedResponses.Count - 1;
PreviewRequested?.Invoke(this, EventArgs.Empty);
}
else
{
var pasteResult = source == PasteActionSource.GlobalKeyboardShortcut || !_userSettings.ShowCustomPreview;
GeneratedResponses.Add(aiOutput);
CurrentResponseIndex = GeneratedResponses.Count - 1;
CustomActionActivated?.Invoke(this, new CustomActionActivatedEventArgs(pasteFormat.Prompt, pasteResult));
if (pasteResult)
{
PasteCustom();
}
await CopyPasteAndHideAsync(dataPackage);
}
}
catch (Exception ex)
{
Logger.LogError("Error executing paste format", ex);
PasteOperationErrorText = ex is PasteActionException ? ex.Message : ResourceLoaderInstance.ResourceLoader.GetString("PasteError");
PasteActionError = PasteActionError.FromException(ex);
}
Busy = false;
elapsedWatch.Stop();
Logger.LogDebug($"Finished executing {pasteFormat.Format} from source {source}; timeTakenMs={elapsedWatch.ElapsedMilliseconds}");
}
internal async Task ExecutePasteFormatAsync(VirtualKey key)
@ -413,20 +431,21 @@ namespace AdvancedPaste.ViewModels
{
Logger.LogTrace();
await ReadClipboardAsync();
var customAction = _userSettings.CustomActions.FirstOrDefault(customAction => customAction.Id == customActionId);
if (customAction != null)
{
await ExecutePasteFormatAsync(CreatePasteFormat(customAction), source);
await ReadClipboardAsync();
await ExecutePasteFormatAsync(CreateCustomAIPasteFormat(customAction.Name, customAction.Prompt, isSavedQuery: true), source);
}
}
internal async Task GenerateCustomFunctionAsync(PasteActionSource triggerSource)
internal async Task ExecuteCustomAIFormatFromCurrentQueryAsync(PasteActionSource triggerSource)
{
AdvancedPasteCustomAction customAction = new() { Name = "Default", Prompt = Query };
await ExecutePasteFormatAsync(CreatePasteFormat(customAction), triggerSource);
var customAction = _userSettings.CustomActions
.FirstOrDefault(customAction => Models.KernelQueryCache.CacheKey.PromptComparer.Equals(customAction.Prompt, Query));
await ExecutePasteFormatAsync(CreateCustomAIPasteFormat(customAction?.Name ?? "Default", Query, isSavedQuery: customAction != null), triggerSource);
}
private void HideWindow()
@ -440,42 +459,6 @@ namespace AdvancedPaste.ViewModels
}
}
internal CustomQuery RecallPreviousCustomQuery()
{
return LoadPreviousQuery();
}
internal void SaveQuery(string inputQuery)
{
Logger.LogTrace();
DataPackageView clipboardData = Clipboard.GetContent();
if (clipboardData == null || !clipboardData.Contains(StandardDataFormats.Text))
{
Logger.LogWarning("Clipboard does not contain text data");
return;
}
var currentClipboardText = Task.Run(async () => await clipboardData.GetTextAsync()).Result;
var queryData = new CustomQuery
{
Query = inputQuery,
ClipboardData = currentClipboardText,
};
SettingsUtils utils = new();
utils.SaveSettings(queryData.ToString(), Constants.AdvancedPasteModuleName, Constants.LastQueryJsonFileName);
}
internal CustomQuery LoadPreviousQuery()
{
SettingsUtils utils = new();
var query = utils.GetSettings<CustomQuery>(Constants.AdvancedPasteModuleName, Constants.LastQueryJsonFileName);
return query;
}
private bool IsClipboardHistoryEnabled()
{
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Clipboard\";
@ -499,15 +482,7 @@ namespace AdvancedPaste.ViewModels
{
UpdateAllowedByGPO();
if (IsAllowedByGPO)
{
var oldKey = _aiHelper.GetKey();
var newKey = AICompletionsHelper.LoadOpenAIKey();
_aiHelper.SetOpenAIKey(newKey);
return newKey != oldKey;
}
return false;
return IsAllowedByGPO && _aiCredentialsProvider.Refresh();
}
}
}

Просмотреть файл

@ -55,6 +55,7 @@ namespace
const wchar_t JSON_KEY_ADVANCED_PASTE_UI_HOTKEY[] = L"advanced-paste-ui-hotkey";
const wchar_t JSON_KEY_PASTE_AS_MARKDOWN_HOTKEY[] = L"paste-as-markdown-hotkey";
const wchar_t JSON_KEY_PASTE_AS_JSON_HOTKEY[] = L"paste-as-json-hotkey";
const wchar_t JSON_KEY_IS_ADVANCED_AI_ENABLED[] = L"IsAdvancedAIEnabled";
const wchar_t JSON_KEY_SHOW_CUSTOM_PREVIEW[] = L"ShowCustomPreview";
const wchar_t JSON_KEY_VALUE[] = L"value";
@ -99,6 +100,7 @@ private:
using CustomAction = ActionData<int>;
std::vector<CustomAction> m_custom_actions;
bool m_is_advanced_ai_enabled = false;
bool m_preview_custom_format_output = true;
Hotkey parse_single_hotkey(const wchar_t* keyName, const winrt::Windows::Data::Json::JsonObject& settingsObject)
@ -268,9 +270,9 @@ private:
}
}
void parse_hotkeys(PowerToysSettings::PowerToyValues& settings)
void read_settings(PowerToysSettings::PowerToyValues& settings)
{
auto settingsObject = settings.get_raw_json();
const auto settingsObject = settings.get_raw_json();
// Migrate Paste As Plain text shortcut
Hotkey old_paste_as_plain_hotkey;
@ -352,6 +354,21 @@ private:
}
}
}
if (settingsObject.GetView().Size())
{
const auto propertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES);
if (propertiesObject.HasKey(JSON_KEY_IS_ADVANCED_AI_ENABLED))
{
m_is_advanced_ai_enabled = propertiesObject.GetNamedObject(JSON_KEY_IS_ADVANCED_AI_ENABLED).GetNamedBoolean(JSON_KEY_VALUE);
}
if (propertiesObject.HasKey(JSON_KEY_SHOW_CUSTOM_PREVIEW))
{
m_preview_custom_format_output = propertiesObject.GetNamedObject(JSON_KEY_SHOW_CUSTOM_PREVIEW).GetNamedBoolean(JSON_KEY_VALUE);
}
}
}
bool is_process_running() const
@ -441,13 +458,7 @@ private:
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(get_key());
parse_hotkeys(settings);
auto settingsObject = settings.get_raw_json();
if (settingsObject.GetView().Size() && settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).HasKey(JSON_KEY_SHOW_CUSTOM_PREVIEW))
{
m_preview_custom_format_output = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_SHOW_CUSTOM_PREVIEW).GetNamedBoolean(JSON_KEY_VALUE);
}
read_settings(settings);
}
catch (std::exception&)
{
@ -809,13 +820,7 @@ public:
PowerToysSettings::PowerToyValues values =
PowerToysSettings::PowerToyValues::from_json_string(config, get_key());
parse_hotkeys(values);
const auto settingsObject = values.get_raw_json();
if (settingsObject.GetView().Size() && settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).HasKey(JSON_KEY_SHOW_CUSTOM_PREVIEW))
{
m_preview_custom_format_output = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_SHOW_CUSTOM_PREVIEW).GetNamedBoolean(JSON_KEY_VALUE);
}
read_settings(values);
std::unordered_map<std::wstring, Hotkey> additionalActionMap;
for (const auto& action : m_additional_actions)
@ -828,6 +833,7 @@ public:
m_advanced_paste_ui_hotkey,
m_paste_as_markdown_hotkey,
m_paste_as_json_hotkey,
m_is_advanced_ai_enabled,
m_preview_custom_format_output,
additionalActionMap);

Просмотреть файл

@ -48,6 +48,7 @@ void Trace::AdvancedPaste_SettingsTelemetry(const PowertoyModuleIface::Hotkey& p
const PowertoyModuleIface::Hotkey& advancedPasteUIHotkey,
const PowertoyModuleIface::Hotkey& pasteMarkdownHotkey,
const PowertoyModuleIface::Hotkey& pasteJsonHotkey,
const bool is_advanced_ai_enabled,
const bool preview_custom_format_output,
const std::unordered_map<std::wstring, PowertoyModuleIface::Hotkey>& additionalActionsHotkeys) noexcept
{
@ -82,6 +83,7 @@ void Trace::AdvancedPaste_SettingsTelemetry(const PowertoyModuleIface::Hotkey& p
TraceLoggingWideString(getHotkeyCStr(advancedPasteUIHotkey), "AdvancedPasteUIHotkey"),
TraceLoggingWideString(getHotkeyCStr(pasteMarkdownHotkey), "PasteMarkdownHotkey"),
TraceLoggingWideString(getHotkeyCStr(pasteJsonHotkey), "PasteJsonHotkey"),
TraceLoggingBoolean(is_advanced_ai_enabled, "IsAdvancedAIEnabled"),
TraceLoggingBoolean(preview_custom_format_output, "ShowCustomPreview"),
TraceLoggingWideString(getAdditionalActionHotkeyCStr(L"ImageToText"), "ImageToTextHotkey"),
TraceLoggingWideString(getAdditionalActionHotkeyCStr(L"PasteAsTxtFile"), "PasteAsTxtFileHotkey"),

Просмотреть файл

@ -20,6 +20,7 @@ public:
const PowertoyModuleIface::Hotkey& advancedPasteUIHotkey,
const PowertoyModuleIface::Hotkey& pasteMarkdownHotkey,
const PowertoyModuleIface::Hotkey& pasteJsonHotkey,
const bool is_advanced_ai_enabled,
const bool preview_custom_format_output,
const std::unordered_map<std::wstring, PowertoyModuleIface::Hotkey>& additionalActionsHotkeys) noexcept;
};

Просмотреть файл

@ -59,6 +59,8 @@
<ItemGroup>
<!-- Including MessagePack to force version, since it's used by StreamJsonRpc but contains vulnerabilities. After StreamJsonRpc updates the version of MessagePack, we can upgrade StreamJsonRpc instead. -->
<PackageReference Include="MessagePack" />
<!-- HACK: To align Microsoft.Bcl.AsyncInterfaces.dll version with Advanced Paste version (from the Semantic Kernel dependency). -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Microsoft.Windows.Compatibility" />
<PackageReference Include="StreamJsonRpc" />
<PackageReference Include="System.Data.SqlClient" /> <!-- It's a dependency of Microsoft.Windows.Compatibility. We're adding it here to force it to the version specified in Directory.Packages.props -->

Просмотреть файл

@ -210,6 +210,8 @@
<ItemGroup>
<!-- Including MessagePack to force version, since it's used by StreamJsonRpc but contains vulnerabilities. After StreamJsonRpc updates the version of MessagePack, we can upgrade StreamJsonRpc instead. -->
<PackageReference Include="MessagePack" />
<!-- HACK: To align Microsoft.Bcl.AsyncInterfaces.dll version with Advanced Paste version (from the Semantic Kernel dependency). -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Microsoft.Windows.Compatibility" />
<PackageReference Include="StreamJsonRpc" />
<PackageReference Include="System.Data.SqlClient" /> <!-- It's a dependency of Microsoft.Windows.Compatibility. We're adding it here to force it to the version specified in Directory.Packages.props -->

Просмотреть файл

@ -64,6 +64,8 @@
<!-- Including MessagePack to force version, since it's used by StreamJsonRpc but contains vulnerabilities. After StreamJsonRpc updates the version of MessagePack, we can upgrade StreamJsonRpc instead. -->
<PackageReference Include="MessagePack" />
<PackageReference Include="Microsoft.Windows.Compatibility" />
<!-- HACK: To align Microsoft.Bcl.AsyncInterfaces.dll version with Advanced Paste version (from the Semantic Kernel dependency). -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Extensions.Logging" />

Просмотреть файл

@ -23,17 +23,16 @@ namespace Microsoft.PowerToys.Settings.UI.Library
PasteAsJsonShortcut = new();
CustomActions = new();
AdditionalActions = new();
IsAdvancedAIEnabled = false;
ShowCustomPreview = true;
SendPasteKeyCombination = true;
CloseAfterLosingFocus = false;
}
[JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool ShowCustomPreview { get; set; }
public bool IsAdvancedAIEnabled { get; set; }
[JsonConverter(typeof(BoolPropertyJsonConverter))]
[CmdConfigureIgnore]
public bool SendPasteKeyCombination { get; set; }
public bool ShowCustomPreview { get; set; }
[JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool CloseAfterLosingFocus { get; set; }

Просмотреть файл

@ -53,16 +53,18 @@ namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
return sendCustomAction.ToJsonString();
}
public static IFileSystemWatcher GetFileWatcher(string moduleName, string fileName, Action onChangedCallback)
public static IFileSystemWatcher GetFileWatcher(string moduleName, string fileName, Action onChangedCallback, IFileSystem fileSystem = null)
{
var path = FileSystem.Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{moduleName}");
fileSystem ??= FileSystem;
if (!FileSystem.Directory.Exists(path))
var path = fileSystem.Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{moduleName}");
if (!fileSystem.Directory.Exists(path))
{
FileSystem.Directory.CreateDirectory(path);
fileSystem.Directory.CreateDirectory(path);
}
var watcher = FileSystem.FileSystemWatcher.New();
var watcher = fileSystem.FileSystemWatcher.New();
watcher.Path = path;
watcher.Filter = fileName;
watcher.NotifyFilter = NotifyFilters.LastWrite;

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 2.2 KiB

Просмотреть файл

@ -81,18 +81,18 @@
</StackPanel>
</tkcontrols:SettingsCard.Description>
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard
x:Uid="AdvancedPaste_ShowCustomPreviewSettingsCard"
HeaderIcon="{ui:FontIcon Glyph=&#xE71E;}"
IsEnabled="{x:Bind ViewModel.IsOpenAIEnabled, Mode=OneWay}">
<ToggleSwitch IsOn="{x:Bind ViewModel.ShowCustomPreview, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<InfoBar
x:Uid="GPO_SettingIsManaged"
IsClosable="False"
IsOpen="{x:Bind ViewModel.ShowOnlineAIModelsGpoConfiguredInfoBar, Mode=OneWay}"
IsTabStop="{x:Bind ViewModel.ShowOnlineAIModelsGpoConfiguredInfoBar, Mode=OneWay}"
Severity="Informational" />
<tkcontrols:SettingsCard
x:Uid="AdvancedPaste_EnableAdvancedAI"
HeaderIcon="{ui:BitmapIcon Source=/Assets/Settings/Icons/SemanticKernel.png}"
IsEnabled="{x:Bind ViewModel.IsOpenAIEnabled, Mode=OneWay}">
<ToggleSwitch IsOn="{x:Bind ViewModel.IsAdvancedAIEnabled, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
</controls:SettingsGroup>
<controls:SettingsGroup x:Uid="AdvancedPaste_BehaviorSettingsGroup" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
@ -111,6 +111,9 @@
<tkcontrols:SettingsCard x:Uid="AdvancedPaste_CloseAfterLosingFocus" HeaderIcon="{ui:FontIcon Glyph=&#xED1A;}">
<ToggleSwitch IsOn="{x:Bind ViewModel.CloseAfterLosingFocus, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard x:Uid="AdvancedPaste_ShowCustomPreviewSettingsCard" HeaderIcon="{ui:FontIcon Glyph=&#xE71E;}">
<ToggleSwitch IsOn="{x:Bind ViewModel.ShowCustomPreview, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
</controls:SettingsGroup>
<controls:SettingsGroup x:Uid="AdvancedPaste_Direct_Access_Hotkeys_GroupSettings" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">

Просмотреть файл

@ -3959,8 +3959,14 @@ Activate by holding the key for the character you want to add an accent to, then
<value>Custom format preview</value>
</data>
<data name="AdvancedPaste_ShowCustomPreviewSettingsCard.Description" xml:space="preserve">
<value>Preview the output of the custom format before pasting</value>
<value>Preview the output of AI formats and Image to text before pasting</value>
</data>
<data name="AdvancedPaste_EnableAdvancedAI.Header" xml:space="preserve">
<value>Enable advanced AI</value>
</data>
<data name="AdvancedPaste_EnableAdvancedAI.Description" xml:space="preserve">
<value>Add advanced capabilities when using 'Paste with AI' including the power to 'chain' multiple transformations together and work with images and files. This feature may consume more API credits when used.</value>
</data>
<data name="Oobe_AdvancedPaste.Description" xml:space="preserve">
<value>Advanced Paste is a tool to put your clipboard content into any format you need, focused towards developer workflows. It can paste as plain text, markdown, or json directly with the UX or with a direct keystroke invoke. These are fully locally executed. In addition, it has an AI powered option that is 100% opt-in and requires an Open AI key. Note: this will replace the formatted text in your clipboard with the selected format.</value>
</data>

Просмотреть файл

@ -319,6 +319,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public bool IsAdvancedAIEnabled
{
get => _advancedPasteSettings.Properties.IsAdvancedAIEnabled;
set
{
if (value != _advancedPasteSettings.Properties.IsAdvancedAIEnabled)
{
_advancedPasteSettings.Properties.IsAdvancedAIEnabled = value;
OnPropertyChanged(nameof(IsAdvancedAIEnabled));
NotifySettingsChanged();
}
}
}
public bool ShowCustomPreview
{
get => _advancedPasteSettings.Properties.ShowCustomPreview;
@ -422,10 +436,11 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
try
{
PasswordVault vault = new PasswordVault();
PasswordCredential cred = new PasswordCredential("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey", password);
PasswordVault vault = new();
PasswordCredential cred = new("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey", password);
vault.Add(cred);
OnPropertyChanged(nameof(IsOpenAIEnabled));
IsAdvancedAIEnabled = true; // new users should get Semantic Kernel benefits immediately
NotifySettingsChanged();
}
catch (Exception)

Просмотреть файл

@ -50,6 +50,7 @@ map<wstring, vector<wstring>> escapeInfo = {
vector<wstring> filesToDelete = {
L"AdvancedPaste\\lastQuery.json",
L"AdvancedPaste\\kernelQueryCache.json",
L"PowerToys Run\\Cache",
L"PowerRename\\replace-mru.json",
L"PowerRename\\search-mru.json",