Added status texts for prompt processing and reasoning to the chat (#1205)

Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
j-erlerandThorsten Sommer authored and GitHub committed 2026-11-21 09:43:31 +01:00
1 parent 6cdb0bc551
commit 28b88580a9
29 files changed
+504 -30

No files matched your search

@@ -13,4 +13,12 @@ public sealed record ChatCompletionDelta
[JsonIgnore]
public string Content => ChatCompletionContent.GetText(this.RawContent) ?? string.Empty;
/// <summary>
/// The reasoning text some providers stream next to the answer.
/// </summary>
/// <remarks>
/// Read only to tell that the model is reasoning, see ContentStreamChunk.IsReasoning.
/// </remarks>
public string? ReasoningContent { get; set; }
}
@@ -44,7 +44,7 @@ public record ChatCompletionDeltaStreamLine(string Id, string Object, uint Creat
public bool ContainsContent() => this.Choices.Count > 0;
/// <inheritdoc />
public ContentStreamChunk GetContent() => new(this.Choices[0].Delta.Content, []);
public ContentStreamChunk GetContent() => new(this.Choices[0].Delta.Content, []) { IsReasoning = !string.IsNullOrEmpty(this.Choices[0].Delta.ReasoningContent) };
/// <inheritdoc />
public TokenUsage GetUsage() => this.Usage?.ToTokenUsage() ?? TokenUsage.UNKNOWN;
@@ -21,4 +21,9 @@ public readonly record struct ChatCompletionStreamPart(string TextDelta, IList<I
/// all is the tool calling loop's decision, which knows which round this is.
/// </remarks>
public bool HasContent => this.TextDelta.Length > 0 || this.Sources.Count > 0;
/// <summary>
/// Whether this line carried reasoning of the model, see ContentStreamChunk.IsReasoning.
/// </summary>
public bool IsReasoning { get; init; }
}
@@ -73,7 +73,8 @@ public sealed class ChatCompletionToolCallAccumulator(Func<ServerSentEvent, ILis
this.hasReadAnything = true;
if (!string.IsNullOrEmpty(delta.ReasoningContent))
var isReasoning = !string.IsNullOrEmpty(delta.ReasoningContent);
if (isReasoning)
this.reasoning.Append(delta.ReasoningContent);
foreach (var toolCallDelta in delta.ToolCalls ?? [])
@@ -86,10 +87,12 @@ public sealed class ChatCompletionToolCallAccumulator(Func<ServerSentEvent, ILis
var textDelta = delta.Content;
if (textDelta.Length is 0)
return WithSources(string.Empty, sources, usage);
return isReasoning
? new ChatCompletionStreamPart(string.Empty, sources, usage) { IsReasoning = true }
: WithSources(string.Empty, sources, usage);
this.text.Append(textDelta);
return new ChatCompletionStreamPart(textDelta, sources, usage);
return new ChatCompletionStreamPart(textDelta, sources, usage) { IsReasoning = isReasoning };
}
/// <summary>
@@ -76,8 +76,8 @@ public sealed class ChatCompletionToolCallingAdapter<TRequest>(
await foreach (var serverSentEvent in streamRequestAsync(requestDto, streamEnd, token))
{
var part = accumulator.Process(serverSentEvent);
if (part.HasContent || part.Usage.IsKnown)
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage));
if (part.HasContent || part.Usage.IsKnown || part.IsReasoning)
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage) { IsReasoning = part.IsReasoning });
}
// A stream which was cut off ends the round without an outcome, cf. IToolCallingProviderAdapter.ExecuteRoundAsync:
@@ -19,7 +19,11 @@ public sealed class ResponsesStreamAccumulator
private const string EVENT_TEXT_DELTA = "response.output_text.delta";
private const string EVENT_ANNOTATION_ADDED = "response.output_text.annotation.added";
private const string EVENT_OUTPUT_ITEM_DONE = "response.output_item.done";
private const string EVENT_OUTPUT_ITEM_ADDED = "response.output_item.added";
private const string EVENT_REASONING_SUMMARY_DELTA = "response.reasoning_summary_text.delta";
private const string EVENT_REASONING_DELTA = "response.reasoning_text.delta";
private const string ITEM_TYPE_REASONING = "reasoning";
private readonly List<JsonElement> completedOutputItems = [];
private ResponsesResponse? completedResponse;
@@ -51,6 +55,18 @@ public sealed class ResponsesStreamAccumulator
//
if (eventType is EVENT_OUTPUT_ITEM_DONE && root.TryGetProperty("item", out var outputItem))
this.completedOutputItems.Add(outputItem.Clone());
//
// The API mostly keeps its reasoning to itself: all it says is that a reasoning item
// opened, and then nothing until the item is done. That opening is the signal.
//
if (eventType is EVENT_OUTPUT_ITEM_ADDED &&
root.TryGetProperty("item", out var addedItem) &&
addedItem.ValueKind is JsonValueKind.Object &&
addedItem.TryGetProperty("type", out var addedItemType) &&
addedItemType.ValueKind is JsonValueKind.String &&
addedItemType.GetString() is ITEM_TYPE_REASONING)
return ResponsesStreamPart.Reasoning;
}
catch (JsonException)
{
@@ -84,7 +100,11 @@ public sealed class ResponsesStreamAccumulator
return ResponsesStreamPart.Nothing;
return new ResponsesStreamPart(string.Empty, annotationLine.GetSources());
// Reasoning which does arrive as text, as a summary or from an open model:
case EVENT_REASONING_SUMMARY_DELTA or EVENT_REASONING_DELTA:
return ResponsesStreamPart.Reasoning;
default:
return ResponsesStreamPart.Nothing;
}
@@ -13,6 +13,11 @@ public readonly record struct ResponsesStreamPart(string TextDelta, IList<ISourc
/// </summary>
public static ResponsesStreamPart Nothing => new(string.Empty, []);
/// <summary>
/// The part of a line which says that the model reasons, and nothing else.
/// </summary>
public static ResponsesStreamPart Reasoning => new(string.Empty, []) { IsReasoning = true };
/// <summary>
/// Whether this part has anything to show at all.
/// </summary>
@@ -21,4 +26,9 @@ public readonly record struct ResponsesStreamPart(string TextDelta, IList<ISourc
/// all is the tool calling loop's decision, which knows which round this is.
/// </remarks>
public bool HasContent => this.TextDelta.Length > 0 || this.Sources.Count > 0;
/// <summary>
/// Whether this line says that the model reasons, see ContentStreamChunk.IsReasoning.
/// </summary>
public bool IsReasoning { get; init; }
}
@@ -64,8 +64,8 @@ public sealed class ResponsesToolCallingAdapter(Model chatModel, IList<object> b
await foreach (var serverSentEvent in streamRequestAsync(request, streamEnd, token))
{
var part = accumulator.Process(serverSentEvent);
if (part.HasContent || part.Usage.IsKnown)
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage));
if (part.HasContent || part.Usage.IsKnown || part.IsReasoning)
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage) { IsReasoning = part.IsReasoning });
}
// A stream which was cut off ends the round without an outcome, cf. IToolCallingProviderAdapter.ExecuteRoundAsync: