using System.Diagnostics.CodeAnalysis;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace AIStudio.Tools.DocumentSegments;
///
/// Writes the segments of a batch for a model and reads them back out of its answer.
///
internal static class SegmentJson
{
///
/// The options with which the segments go into a prompt.
///
///
/// The relaxed encoder writes umlauts and the angle brackets of the tags as they are, rather than as
/// escapes which a model might copy into its answer. Nothing embeds this JSON into HTML.
///
internal static readonly JsonSerializerOptions PROMPT_OPTIONS = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
TypeInfoResolver = SegmentJsonContext.Default,
}.AsReadOnly();
///
/// The options with which the answer of a model is read, forgiving what models like to write.
///
internal static readonly JsonSerializerOptions ANSWER_OPTIONS = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
RespectNullableAnnotations = true,
TypeInfoResolver = SegmentJsonContext.Default,
}.AsReadOnly();
///
/// Writes a batch for a prompt.
///
/// The segments.
public static string Write(SegmentBatch batch) => JsonSerializer.Serialize(batch, PROMPT_OPTIONS);
///
/// Reads the batch out of the answer of a model, without checking its content.
///
/// The answer of the model.
/// The batch, or null when reading failed.
/// Why reading failed, written for the model; empty on success.
/// Whether the answer starts a JSON object but ends before it is complete, e.g., because the model reached its limit of tokens.
/// Whether the answer holds a batch which could be read.
public static bool TryRead(string answer, [NotNullWhen(true)] out SegmentBatch? batch, out string issue, out bool breaksOff)
{
batch = null;
issue = string.Empty;
breaksOff = false;
var json = ModelAnswerJson.ExtractObject(answer);
if (json.IsWhiteSpace())
{
breaksOff = answer.Contains('{');
issue = "The answer contains no complete JSON object.";
return false;
}
try
{
batch = JsonSerializer.Deserialize(json, ANSWER_OPTIONS);
}
catch (JsonException e)
{
issue = $"The JSON object cannot be read: {e.Message}";
return false;
}
if (batch is null)
{
issue = "The JSON object is empty.";
return false;
}
return true;
}
}