From 4d787e9434a6859fdfb65d166066018009cc16b9 Mon Sep 17 00:00:00 2001 From: moooyo <42196638+moooyo@users.noreply.github.com> Date: Tue, 21 Jan 2025 15:20:59 +0800 Subject: [PATCH] [AOT] Clean up some AOT issues in Advanced Paste module (#36297) * Clean up the aot build issue. * Merge main and clean up new AOT build issue * Update LogEvent define. * Update src/modules/AdvancedPaste/AdvancedPaste/Helpers/LogEvent.cs Co-authored-by: Jeremy Sinclair <4016293+snickler@users.noreply.github.com> * update typo bug * Remove unused allow list item * Create a new folder to place source generation context * Merge main and rename LogEvent to AIServiceFormatEvent --------- Co-authored-by: Yu Leng (from Dev Box) Co-authored-by: Jeremy Sinclair <4016293+snickler@users.noreply.github.com> --- .github/actions/spell-check/allow/code.txt | 2 +- .../Helpers/AIServiceFormatEvent.cs | 45 +++++++++++++++++++ .../AdvancedPaste/Helpers/NativeMethods.cs | 2 +- .../AdvancedPaste/Helpers/UserSettings.cs | 2 +- .../Models/KernelQueryCache/PersistedCache.cs | 13 ++---- .../SourceGenerationContext.cs | 16 +++++++ .../Services/KernelServiceBase.cs | 5 +-- .../OpenAI/CustomTextTransformService.cs | 4 +- 8 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 src/modules/AdvancedPaste/AdvancedPaste/Helpers/AIServiceFormatEvent.cs create mode 100644 src/modules/AdvancedPaste/AdvancedPaste/SerializationContext/SourceGenerationContext.cs diff --git a/.github/actions/spell-check/allow/code.txt b/.github/actions/spell-check/allow/code.txt index f8097284ff..26748d9de3 100644 --- a/.github/actions/spell-check/allow/code.txt +++ b/.github/actions/spell-check/allow/code.txt @@ -265,4 +265,4 @@ onefuzz leilzh #Tools -OIP +OIP \ No newline at end of file diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/AIServiceFormatEvent.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/AIServiceFormatEvent.cs new file mode 100644 index 0000000000..1ab58bf269 --- /dev/null +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/AIServiceFormatEvent.cs @@ -0,0 +1,45 @@ +// 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.Models.KernelQueryCache; +using AdvancedPaste.SerializationContext; +using AdvancedPaste.Telemetry; + +namespace AdvancedPaste.Helpers +{ + public class AIServiceFormatEvent + { + public AIServiceFormatEvent(AdvancedPasteSemanticKernelFormatEvent semanticKernelFormatEvent) + { + CacheUsed = semanticKernelFormatEvent.CacheUsed; + IsSavedQuery = semanticKernelFormatEvent.IsSavedQuery; + PromptTokens = semanticKernelFormatEvent.PromptTokens; + CompletionTokens = semanticKernelFormatEvent.CompletionTokens; + ModelName = semanticKernelFormatEvent.ModelName; + ActionChain = semanticKernelFormatEvent.ActionChain; + } + + public AIServiceFormatEvent(AdvancedPasteGenerateCustomFormatEvent generateCustomFormatEvent) + { + PromptTokens = generateCustomFormatEvent.PromptTokens; + CompletionTokens = generateCustomFormatEvent.CompletionTokens; + ModelName = generateCustomFormatEvent.ModelName; + } + + public bool IsSavedQuery { get; set; } + + public bool CacheUsed { get; set; } + + public int PromptTokens { get; set; } + + public int CompletionTokens { get; set; } + + public string ModelName { get; set; } + + public string ActionChain { get; set; } + + public string ToJsonString() => JsonSerializer.Serialize(this, SourceGenerationContext.Default.AIServiceFormatEvent); + } +} diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs index 8ddb1e7778..a28626ca1f 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs @@ -18,7 +18,7 @@ namespace AdvancedPaste.Helpers internal static int Size { - get { return Marshal.SizeOf(typeof(INPUT)); } + get { return Marshal.SizeOf(); } } } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs index 8d5f51314d..70a4cf0f9e 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs @@ -16,7 +16,7 @@ using Microsoft.PowerToys.Settings.UI.Library.Utilities; namespace AdvancedPaste.Settings { - internal sealed class UserSettings : IUserSettings, IDisposable + internal sealed partial class UserSettings : IUserSettings, IDisposable { private readonly SettingsUtils _settingsUtils; private readonly TaskScheduler _taskScheduler; diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Models/KernelQueryCache/PersistedCache.cs b/src/modules/AdvancedPaste/AdvancedPaste/Models/KernelQueryCache/PersistedCache.cs index 928ba7ab37..721d9a24ed 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Models/KernelQueryCache/PersistedCache.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Models/KernelQueryCache/PersistedCache.cs @@ -7,6 +7,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using AdvancedPaste.Helpers; +using AdvancedPaste.SerializationContext; using Microsoft.PowerToys.Settings.UI.Library.Interfaces; namespace AdvancedPaste.Models.KernelQueryCache; @@ -15,15 +16,7 @@ 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(json, SerializerOptions); + public static PersistedCache FromJsonString(string json) => JsonSerializer.Deserialize(json, SourceGenerationContext.Default.PersistedCache); public string Version { get; init; } @@ -31,7 +24,7 @@ public sealed class PersistedCache : ISettingsConfig public string GetModuleName() => Constants.AdvancedPasteModuleName; - public string ToJsonString() => JsonSerializer.Serialize(this, SerializerOptions); + public string ToJsonString() => JsonSerializer.Serialize(this, SourceGenerationContext.Default.PersistedCache); public override string ToString() => ToJsonString(); diff --git a/src/modules/AdvancedPaste/AdvancedPaste/SerializationContext/SourceGenerationContext.cs b/src/modules/AdvancedPaste/AdvancedPaste/SerializationContext/SourceGenerationContext.cs new file mode 100644 index 0000000000..dadb2d80c1 --- /dev/null +++ b/src/modules/AdvancedPaste/AdvancedPaste/SerializationContext/SourceGenerationContext.cs @@ -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.Text.Json.Serialization; +using AdvancedPaste.Helpers; +using AdvancedPaste.Models.KernelQueryCache; + +namespace AdvancedPaste.SerializationContext; + +[JsonSerializable(typeof(PersistedCache))] +[JsonSerializable(typeof(AIServiceFormatEvent))] +[JsonSourceGenerationOptions(UseStringEnumConverter = true)] +public sealed partial class SourceGenerationContext : JsonSerializerContext +{ +} diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs index 08526d0b0c..c988d2f8ce 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs @@ -174,9 +174,8 @@ public abstract class KernelServiceBase(IKernelQueryCacheService queryCacheServi { 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)}"); + var logEvent = new AIServiceFormatEvent(telemetryEvent); + Logger.LogDebug($"{nameof(TransformClipboardAsync)} complete; {logEvent.ToJsonString()}"); } private Kernel CreateKernel() diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/OpenAI/CustomTextTransformService.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/OpenAI/CustomTextTransformService.cs index c249771bd9..95823d8d24 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/OpenAI/CustomTextTransformService.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/OpenAI/CustomTextTransformService.cs @@ -85,9 +85,9 @@ Output: var usage = response.Usage; AdvancedPasteGenerateCustomFormatEvent telemetryEvent = new(usage.PromptTokens, usage.CompletionTokens, ModelName); PowerToysTelemetry.Log.WriteEvent(telemetryEvent); + var logEvent = new AIServiceFormatEvent(telemetryEvent); - var logEvent = new { telemetryEvent.PromptTokens, telemetryEvent.CompletionTokens, telemetryEvent.ModelName }; - Logger.LogDebug($"{nameof(TransformTextAsync)} complete; {JsonSerializer.Serialize(logEvent)}"); + Logger.LogDebug($"{nameof(TransformTextAsync)} complete; {logEvent.ToJsonString()}"); return response.Choices[0].Text; }