Generated the JSON contracts of the stored data at compile time (#1148)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-02 18:23:19 +01:00
1 parent fc7399e81b
commit e954881fda
75 files changed
+5437 -298

No files matched your search

@@ -51,12 +51,13 @@ public static class DataBackupFormat
/// How the manifest and the plan of a restore are written: readable for anybody who opens them,
/// and for the runtime, which applies the plan.
/// </summary>
public static readonly JsonSerializerOptions JSON_OPTIONS = new()
public static readonly JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
Converters = { new JsonStringEnumConverter() },
};
TypeInfoResolver = DataBackupJsonContext.Default,
}.AsReadOnly();
/// <summary>
/// File types which are compressed already. Compressing them once more costs time and makes the
@@ -0,0 +1,13 @@
using System.Text.Json.Serialization;
namespace AIStudio.Tools.DataBackup;
/// <summary>
/// The contracts with which System.Text.Json reads and writes the manifest of a backup and the plan and report of
/// a restore, built at compile time.
/// </summary>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(DataBackupManifest))]
[JsonSerializable(typeof(DataRestorePlan))]
[JsonSerializable(typeof(DataRestoreReport))]
internal sealed partial class DataBackupJsonContext : JsonSerializerContext;
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
namespace AIStudio.Tools.Databases.ProvenanceStore;
/// <summary>
/// The contracts with which System.Text.Json reads and writes the content around a passage, built at compile time.
/// </summary>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(IReadOnlyList<string>))]
[JsonSerializable(typeof(List<string>))]
internal sealed partial class ProvenanceJsonContext : JsonSerializerContext;
@@ -39,7 +39,11 @@ public sealed class SqliteProvenanceStoreClientImplementation(string name, strin
/// Writes the content around a passage the way it reads: the default encoder would turn every
/// character outside ASCII into an escape, cf. ToolExecutionResult.MODEL_CONTENT_OPTIONS.
/// </summary>
private static readonly JsonSerializerOptions JSON_OPTIONS = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
internal static readonly JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
TypeInfoResolver = ProvenanceJsonContext.Default,
}.AsReadOnly();
private readonly string databasePath = databasePath;
private readonly string lastCleanupPath = Path.Join(basePath, LAST_CLEANUP_FILENAME);
@@ -351,7 +355,7 @@ public sealed class SqliteProvenanceStoreClientImplementation(string name, strin
try
{
return JsonSerializer.Deserialize<List<string>>(row.SurroundingContent) ?? [];
return JsonSerializer.Deserialize<List<string>>(row.SurroundingContent, JSON_OPTIONS) ?? [];
}
catch (JsonException exception)
{
@@ -0,0 +1,21 @@
using System.Text.Json;
namespace AIStudio.Tools;
public static class JsonSerializerOptionsExtensions
{
/// <summary>
/// Makes the options read-only and returns them, so that a static field gets its options in one expression.
/// </summary>
/// <remarks>
/// Options with contracts of their own, i.e., with a TypeInfoResolver, should never change after they were
/// declared: whoever used them before such a change would have read or written with other contracts.
/// </remarks>
/// <param name="options">The options, with their TypeInfoResolver set.</param>
/// <returns>The same options, now read-only.</returns>
public static JsonSerializerOptions AsReadOnly(this JsonSerializerOptions options)
{
options.MakeReadOnly();
return options;
}
}
@@ -4,13 +4,13 @@ namespace AIStudio.Tools.PluginSystem.Assistants;
public sealed class PluginAssistantAudit
{
public Guid PluginId { get; init; }
public string PluginHash { get; init; } = string.Empty;
public Guid PluginId { get; set; }
public string PluginHash { get; set; } = string.Empty;
public DateTimeOffset AuditedAtUtc { get; set; }
public string AuditProviderId { get; set; } = string.Empty;
public string AuditProviderName { get; set; } = string.Empty;
public AssistantAuditLevel Level { get; init; } = AssistantAuditLevel.UNKNOWN;
public string Summary { get; init; } = string.Empty;
public AssistantAuditLevel Level { get; set; } = AssistantAuditLevel.UNKNOWN;
public string Summary { get; set; } = string.Empty;
public float Confidence { get; set; }
public string PromptPreview { get; set; } = string.Empty;
public List<AssistantAuditFinding> Findings { get; set; } = [];
@@ -5,19 +5,19 @@ public abstract record ConfigurationBaseObject : IConfigurationObject
#region Implementation of IConfigurationObject
/// <inheritdoc />
public abstract string Id { get; init; }
public abstract string Id { get; set; }
/// <inheritdoc />
public abstract uint Num { get; init; }
public abstract uint Num { get; set; }
/// <inheritdoc />
public abstract string Name { get; init; }
public abstract string Name { get; set; }
/// <inheritdoc />
public abstract bool IsEnterpriseConfiguration { get; init; }
public abstract bool IsEnterpriseConfiguration { get; set; }
/// <inheritdoc />
public abstract Guid EnterpriseConfigurationPluginId { get; init; }
public abstract Guid EnterpriseConfigurationPluginId { get; set; }
#endregion
}
@@ -32,7 +32,7 @@ public sealed class RetrievalImageContext : IRetrievalContext, IImageSource
/// <remarks>
/// Is the image source a URL, a local file path, a base64 string, etc.?
/// </remarks>
public required ContentImageSource SourceType { get; init; }
public required ContentImageSource SourceType { get; set; }
/// <summary>
/// The image source.
@@ -25,27 +25,27 @@ public sealed record SourcePassage
/// the passage. The list of sources tells sources apart by their address without the fragment,
/// and so does the search for the passages of a source, see GetPassagesOf.
/// </remarks>
public string SourceUrl { get; init; } = string.Empty;
public string SourceUrl { get; set; } = string.Empty;
/// <summary>
/// The name of where the passage was found, e.g., a document or a database.
/// </summary>
public string DataSourceName { get; init; } = string.Empty;
public string DataSourceName { get; set; } = string.Empty;
/// <summary>
/// The path to the content, e.g., a file path or a URL.
/// </summary>
public string Path { get; init; } = string.Empty;
public string Path { get; set; } = string.Empty;
/// <summary>
/// The category of the content, like text or image.
/// </summary>
public RetrievalContentCategory Category { get; init; }
public RetrievalContentCategory Category { get; set; }
/// <summary>
/// What type of content the passage is, like a document or a transcript.
/// </summary>
public RetrievalContentType Type { get; init; }
public RetrievalContentType Type { get; set; }
/// <summary>
/// The entry of the provenance store which holds the matched text and the content around it,
@@ -57,7 +57,7 @@ public sealed record SourcePassage
/// stored, Text and SurroundingContent are empty. Left out of the chat file while null.
/// </remarks>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Guid? ProvenanceId { get; init; }
public Guid? ProvenanceId { get; set; }
/// <summary>
/// The matched text, filtered for prompt injections, as long as it is not stored, see ProvenanceId.
@@ -65,22 +65,22 @@ public sealed record SourcePassage
/// <remarks>
/// Empty for an image which matched: the AI is only told that it did, never shown the image.
/// </remarks>
public string Text { get; init; } = string.Empty;
public string Text { get; set; } = string.Empty;
/// <summary>
/// The content around the matched text, filtered for prompt injections, as long as it is not stored, see ProvenanceId.
/// </summary>
public IReadOnlyList<string> SurroundingContent { get; init; } = [];
public IReadOnlyList<string> SurroundingContent { get; set; } = [];
/// <summary>
/// The page the passage starts on, or null when it has none.
/// </summary>
public int? PageNumber { get; init; }
public int? PageNumber { get; set; }
/// <summary>
/// The page the passage ends on, or null when it is not known.
/// </summary>
public int? EndPageNumber { get; init; }
public int? EndPageNumber { get; set; }
//
// Where a passage without pages is. Left out of the chat file while unknown, which they are for
@@ -91,31 +91,31 @@ public sealed record SourcePassage
/// The sheet of a spreadsheet the passage is from, or null.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? SheetName { get; init; }
public string? SheetName { get; set; }
/// <summary>
/// The row the passage starts at, counted from one: a row of its sheet, or of a table such as a CSV file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? StartRow { get; init; }
public int? StartRow { get; set; }
/// <summary>
/// The row the passage ends at, or null when it is not known.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? EndRow { get; init; }
public int? EndRow { get; set; }
/// <summary>
/// The line the passage starts at, counted from one, for text and source code, or null.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? StartLine { get; init; }
public int? StartLine { get; set; }
/// <summary>
/// The line the passage ends at, or null when it is not known.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? EndLine { get; init; }
public int? EndLine { get; set; }
/// <summary>
/// Which part of its document the passage is, counted from one, or null when the data source
@@ -126,5 +126,5 @@ public sealed record SourcePassage
/// indexed before these were kept. Wherever they are known, they say it better, see
/// SourcePassageExtensions.GetLocation.
/// </remarks>
public int? ChunkNumber { get; init; }
public int? ChunkNumber { get; set; }
}
@@ -63,7 +63,7 @@ public static partial class WorkspaceBehaviour
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(WorkspaceBehaviour).Namespace, nameof(WorkspaceBehaviour));
public static readonly JsonSerializerOptions JSON_OPTIONS = new()
public static readonly JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions
{
WriteIndented = true,
AllowTrailingCommas = true,
@@ -73,8 +73,9 @@ public static partial class WorkspaceBehaviour
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper),
}
};
},
TypeInfoResolver = ChatJsonContext.Default,
}.AsReadOnly();
private static readonly TimeSpan PREFETCH_DELAY_DURATION = TimeSpan.FromMilliseconds(45);