diff --git a/.github/actions/spell-check/allow/code.txt b/.github/actions/spell-check/allow/code.txt index f8097284ff2f..26748d9de3f1 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 000000000000..1ab58bf26943 --- /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 8ddb1e7778bf..a28626ca1f21 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs @@ -18,7 +18,7 @@ internal struct INPUT 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 8d5f51314db5..70a4cf0f9e4d 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs @@ -16,7 +16,7 @@ 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 928ba7ab3751..721d9a24ed55 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.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 record class CacheItem(CacheKey CacheKey, CacheValue CacheValue); 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 000000000000..dadb2d80c112 --- /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 08526d0b0cc7..c988d2f8ced8 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs @@ -174,9 +174,8 @@ private void LogResult(bool cacheUsed, bool isSavedQuery, IEnumerable TransformTextAsync(string prompt, string inputText) 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; }