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

@@ -140,6 +140,12 @@
{
if (this.Content.IsStreaming)
{
@if (this.ShowsThinkingStatus)
{
<MudText Typo="Typo.body2" Class="mb-2">
@T("The model is thinking...")
</MudText>
}
<MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="mb-6" />
}
@@ -225,6 +231,12 @@
if (textContent.InitialRemoteWait)
{
@if (!textContent.ToolRuntimeStatus.IsRunning)
{
<MudText Typo="Typo.body2" Class="mb-2">
@T("Processing the prompt...")
</MudText>
}
<MudSkeleton Width="30%" Height="42px;"/>
<MudSkeleton Width="80%"/>
<MudSkeleton Width="100%"/>
@@ -217,6 +217,14 @@ public partial class ContentBlockComponent : MSGComponentBase
/// </summary>
private bool GetsAIGeneratedContentDisclosure => this.Role is ChatRole.AI && this.AddsAIGeneratedContentDisclosure;
/// <summary>
/// Whether this block tells the user above the progress bar that the model is thinking.
/// </summary>
/// <remarks>
/// Not while a tool runs: the status of the tools tells what happens then.
/// </remarks>
private bool ShowsThinkingStatus => this.Content is ContentText { IsThinking: true, ToolRuntimeStatus.IsRunning: false };
/// <summary>
/// The files this block holds, tables, code blocks, and formulas, so that the export menu can offer each of them.
/// </summary>
@@ -449,6 +457,7 @@ public partial class ContentBlockComponent : MSGComponentBase
hash.Add(text.ToolInvocations.Count);
hash.Add(text.Presentation?.Kind);
hash.Add(text.Presentation?.GetDataHashCode() ?? 0);
hash.Add(text.IsThinking);
hash.Add(text.ToolRuntimeStatus.IsRunning);
hash.Add(text.ToolRuntimeStatus.Message);
hash.Add(this.showToolTrace);
@@ -33,6 +33,16 @@ public sealed class ContentText : IContent
/// </summary>
private static readonly TimeSpan MIN_TIME = TimeSpan.FromSeconds(3);
/// <summary>
/// Whether the provider says that the model reasons, from a chunk which said so until the first text after it.
/// </summary>
private bool isReasoning;
/// <summary>
/// Whether the text holds anything to read besides a think block, see ApplyStreamChunk.
/// </summary>
private bool hasReadableText;
#region Implementation of IContent
/// <inheritdoc />
@@ -43,6 +53,18 @@ public sealed class ContentText : IContent
[JsonIgnore]
public bool IsStreaming { get; set; }
/// <summary>
/// Whether the model thinks right now, as far as the user can tell: its provider says that it reasons, or
/// nothing has arrived yet which the user may read, as while a model reasons inside think tags.
/// </summary>
/// <remarks>
/// Only while the answer streams, so that it holds however the stream ends: in this class, in the job service of
/// the chat, or without any stream at all. Both parts are decided once per chunk, see ApplyStreamChunk, and not
/// with every render of the answer.
/// </remarks>
[JsonIgnore]
public bool IsThinking => this.IsStreaming && (this.isReasoning || !this.hasReadableText);
/// <summary>
/// Tells whether the model ended its turn without an answer, so that the text holds a notice of the app rather
/// than a word of the model, see ContentStreamChunk.EndsWithoutAnswer.
@@ -387,7 +409,18 @@ public sealed class ContentText : IContent
this.InitialRemoteWait = false;
this.IsStreaming = true;
// Reasoning lasts until the first text after it:
if (chunk.IsReasoning)
this.isReasoning = true;
else if (!string.IsNullOrEmpty(chunk.Content))
this.isReasoning = false;
this.Text += chunk;
// The text only grows while it streams, so once there is something to read, there always is:
if (!this.hasReadableText)
this.hasReadableText = this.Text.HasTextBeyondThinkTags;
if (chunk.EndsWithoutAnswer)
this.EndedWithoutAnswer = true;
+31 -13
View File
@@ -2,22 +2,26 @@
public static class StringExtensions
{
private const string THINK_OPEN_TAG = "<think>";
private const string THINK_CLOSE_TAG = "</think>";
/// <param name="text">The text.</param>
extension(string text)
{
public string RemoveThinkTags()
{
const string OPEN_TAG = "<think>";
const string CLOSE_TAG = "</think>";
if (string.IsNullOrWhiteSpace(text) || !text.StartsWith(OPEN_TAG, StringComparison.Ordinal))
return text;
var endIndex = text.IndexOf(CLOSE_TAG, StringComparison.Ordinal);
if (endIndex == -1)
return string.Empty;
return text[(endIndex + CLOSE_TAG.Length)..];
}
/// <summary>
/// The text without the think block which a model may open its answer with.
/// </summary>
/// <returns>The text as it was without such a block, nothing while the block is still open, and otherwise what follows the block.</returns>
public string RemoveThinkTags() => AnswerStart(text) is { } start ? text[start..] : string.Empty;
/// <summary>
/// Whether the text holds anything to read besides the think block which a model may open its answer with.
/// </summary>
/// <remarks>
/// Tells what RemoveThinkTags would leave without copying it: the chat asks with every chunk of an answer,
/// and a model which thinks out loud writes long texts.
/// </remarks>
public bool HasTextBeyondThinkTags => AnswerStart(text) is { } start && !text.AsSpan(start).IsWhiteSpace();
/// <summary>
/// Shortens a text to the given number of characters, and marks the cut with three dots.
@@ -38,4 +42,18 @@ public static class StringExtensions
return string.Concat(text.AsSpan(0, end).TrimEnd(), "...");
}
}
/// <summary>
/// Where the answer starts in a text which a model may open with a think block.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>0 without a think block, the position right behind the block otherwise, and null while the block is still open.</returns>
private static int? AnswerStart(string text)
{
if (string.IsNullOrWhiteSpace(text) || !text.StartsWith(THINK_OPEN_TAG, StringComparison.Ordinal))
return 0;
var endIndex = text.IndexOf(THINK_CLOSE_TAG, StringComparison.Ordinal);
return endIndex == -1 ? null : endIndex + THINK_CLOSE_TAG.Length;
}
}