Path: redb-route/redb.Route.Llm/Providers/LlmStreamFrames.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Providers/LlmStreamFrames.cs
----
using System.Text.Json;
using System.Text.Json.Nodes;
namespace redb.Route.Llm.Providers;
///
/// Reading the data: frames of a provider's SSE stream. A frame that cannot be read is lost text or a lost tool
/// call: skipping it would hand on a shorter answer as a whole one, so it fails, and the message shows the frame.
///
internal static class LlmStreamFrames
{
private const int MaxShown = 500;
/// The frame as a JSON object; anything else fails with the frame in the message.
internal static JsonObject Read(string providerId, string payload)
{
JsonNode? node;
try
{
node = JsonNode.Parse(payload);
}
catch (JsonException ex)
{
throw new InvalidOperationException($"{providerId}: a stream frame is not JSON: {Shown(payload)}", ex);
}
return node as JsonObject
?? throw new InvalidOperationException($"{providerId}: a stream frame is not a JSON object: {Shown(payload)}");
}
/// The string value of , or null when it is absent or not a string.
internal static string? Text(JsonNode? node) =>
node is JsonValue value && value.TryGetValue(out var text) ? text : null;
/// cut to a length a log line can carry.
internal static string Shown(string text) =>
text.Length <= MaxShown ? text : string.Concat(text.AsSpan(0, MaxShown), "…");
}