Refactor token usage HTML generation for clarity

Refactored HTML generation by introducing a sections array for easier extension and maintainability. Updated FormatTokenUsageDetails to require non-null UsageDetails and display reasoning tokens as a detail within output tokens, improving clarity and reducing redundancy. Only non-null sections are rendered, simplifying the logic.
This commit is contained in:
Marco Minerva
2026-07-28 15:56:59 +02:00
parent 20bf98b131
commit 6f2e928542
@@ -262,26 +262,29 @@
return string.Empty; return string.Empty;
} }
var reformulation = tokenUsageResponse.Reformulation is not null (string Label, UsageDetails? Usage)[] sections =
? $"<p><strong>Reformulation:</strong><br />{FormatTokenUsageDetails(tokenUsageResponse.Reformulation)}</p>" [
: string.Empty; ("Reformulation", tokenUsageResponse.Reformulation),
("Question", tokenUsageResponse.Question)
];
var question = tokenUsageResponse.Question is not null return string.Concat(sections
? $"<p><strong>Question:</strong><br />{FormatTokenUsageDetails(tokenUsageResponse.Question)}</p>" .Where(section => section.Usage is not null)
: string.Empty; .Select(section => $"<p><strong>{section.Label}:</strong><br />{FormatTokenUsageDetails(section.Usage!)}</p>"));
return $"{reformulation}{question}"; static string FormatTokenUsageDetails(UsageDetails tokenUsage)
static string FormatTokenUsageDetails(UsageDetails? tokenUsage)
{ {
if (tokenUsage is null) // Reasoning tokens are part of the output tokens, so they're shown as a detail of that value.
{ var outputTokens = tokenUsage.ReasoningTokenCount is > 0 and var reasoningTokenCount
return string.Empty; ? $"Output tokens: {tokenUsage.OutputTokenCount} (including {reasoningTokenCount} reasoning tokens)"
} : $"Output tokens: {tokenUsage.OutputTokenCount}";
return $"Input tokens: {tokenUsage.InputTokenCount}<br />" + return string.Join("<br />",
$"Output tokens: {tokenUsage.OutputTokenCount}<br />" + [
$"Total tokens: {tokenUsage.TotalTokenCount}"; $"Input tokens: {tokenUsage.InputTokenCount}",
outputTokens,
$"Total tokens: {tokenUsage.TotalTokenCount}"
]);
} }
} }