Generated the JSON contracts of visual briefings and model answers at compile time (#1151)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-03 14:20:41 +01:00
1 parent 24e3c22db3
commit 770c16e20a
101 files changed
+2333 -392

No files matched your search

@@ -15,10 +15,12 @@ namespace AIStudio.Assistants.SWOTAnalysis;
/// </remarks>
internal static class SwotAnalysisModeration
{
private static readonly JsonSerializerOptions JSON_OPTIONS = new()
// Internal rather than private, so that a test can check the JSON the moderator gets:
internal static readonly JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions
{
WriteIndented = true,
};
TypeInfoResolver = SwotModerationJsonContext.Default,
}.AsReadOnly();
public static string BuildSystemPrompt(string outputLanguageInstruction) =>
$"""
@@ -138,16 +140,9 @@ internal static class SwotAnalysisModeration
};
}
private static object CreateCategoryPayload(SwotCategoryAnalysisResult analysis, string factorIdPrefix) => new
{
empty_message = analysis.EmptyMessage,
findings = analysis.Findings.Select((finding, index) => new
{
factor_id = $"{factorIdPrefix}{index + 1}",
summary = finding.Summary,
explanation = finding.Explanation,
}),
};
private static SwotModerationCategoryInput CreateCategoryPayload(SwotCategoryAnalysisResult analysis, string factorIdPrefix) => new(
analysis.EmptyMessage,
[.. analysis.Findings.Select((finding, index) => new SwotModerationFindingInput($"{factorIdPrefix}{index + 1}", finding.Summary, finding.Explanation))]);
private static SwotCategory CreateCategory(SwotModeratedCategory category) => new()
{
@@ -8,13 +8,14 @@ namespace AIStudio.Assistants.SWOTAnalysis;
/// </summary>
internal static class SwotModelAnswer
{
private static readonly JsonSerializerOptions JSON_OPTIONS = new()
internal static readonly JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
RespectNullableAnnotations = true,
};
TypeInfoResolver = SwotModelAnswerJsonContext.Default,
}.AsReadOnly();
/// <summary>
/// Reads the JSON object of a model answer into the given type, without checking its content.
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace AIStudio.Assistants.SWOTAnalysis;
/// <summary>
/// The contracts with which System.Text.Json reads the answers of the models which work on a SWOT analysis, built at
/// compile time.
/// </summary>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(SwotCategoryAnalysisResult))]
[JsonSerializable(typeof(SwotModerationResult))]
internal sealed partial class SwotModelAnswerJsonContext : JsonSerializerContext;
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace AIStudio.Assistants.SWOTAnalysis;
/// <summary>
/// The preliminary result of one category, as the moderator gets it.
/// </summary>
/// <param name="EmptyMessage">Why the category has no findings, or an empty string.</param>
/// <param name="Findings">The findings of the category, numbered for reference.</param>
internal sealed record SwotModerationCategoryInput(
[property: JsonPropertyName("empty_message")] string EmptyMessage,
[property: JsonPropertyName("findings")] IReadOnlyList<SwotModerationFindingInput> Findings);
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;
namespace AIStudio.Assistants.SWOTAnalysis;
/// <summary>
/// A preliminary finding, as the moderator gets it.
/// </summary>
/// <param name="FactorId">The ID of the finding, for reference only: the moderator numbers the final findings anew.</param>
/// <param name="Summary">The summary of the finding.</param>
/// <param name="Explanation">The explanation of the finding.</param>
internal sealed record SwotModerationFindingInput(
[property: JsonPropertyName("factor_id")] string FactorId,
[property: JsonPropertyName("summary")] string Summary,
[property: JsonPropertyName("explanation")] string Explanation);
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
namespace AIStudio.Assistants.SWOTAnalysis;
/// <summary>
/// The contracts with which System.Text.Json writes the preliminary results the moderator of a SWOT analysis gets,
/// built at compile time.
/// </summary>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(Dictionary<string, SwotModerationCategoryInput>))]
internal sealed partial class SwotModerationJsonContext : JsonSerializerContext;