From 668087cda44066bcfb169170b973662cf2730c25 Mon Sep 17 00:00:00 2001 From: Mika Kuns Date: Tue, 21 Apr 2026 15:00:00 +0200 Subject: [PATCH] refactor(ui): skeleton dispatch for StreamLineFormatter rewrite --- .../Helpers/StreamLineFormatter.cs | 78 ++++++------------- 1 file changed, 24 insertions(+), 54 deletions(-) diff --git a/src/ClaudeDo.Ui/Helpers/StreamLineFormatter.cs b/src/ClaudeDo.Ui/Helpers/StreamLineFormatter.cs index 267e8c3..3c2fdb6 100644 --- a/src/ClaudeDo.Ui/Helpers/StreamLineFormatter.cs +++ b/src/ClaudeDo.Ui/Helpers/StreamLineFormatter.cs @@ -6,6 +6,7 @@ namespace ClaudeDo.Ui.Helpers; public class StreamLineFormatter { private const int MaxLength = 50_000; + private const int MaxArgChars = 120; public string? FormatLine(string line) { @@ -22,73 +23,42 @@ public class StreamLineFormatter using (doc) { var root = doc.RootElement; + if (root.ValueKind != JsonValueKind.Object) + return null; if (!root.TryGetProperty("type", out var typeProp)) return null; - var type = typeProp.GetString(); - - switch (type) + return typeProp.GetString() switch { - case "stream_event": - return FormatStreamEvent(root); - - case "result": - if (root.TryGetProperty("result", out var resultProp)) - return $"\n--- Result ---\n{resultProp.GetString()}\n"; - return null; - - case "system": - if (root.TryGetProperty("subtype", out var subtypeProp) && - subtypeProp.GetString() == "api_retry") - return "\n[Retrying API call...]\n"; - return null; - - default: - return null; - } + "system" => FormatSystem(root), + "assistant" => FormatAssistant(root), + "user" => FormatUser(root), + "result" => FormatResult(root), + _ => null, + }; } } - private static string? FormatStreamEvent(JsonElement root) + private static string? FormatSystem(JsonElement root) { - if (!root.TryGetProperty("event", out var ev)) + if (!root.TryGetProperty("subtype", out var subtypeProp)) return null; - if (!ev.TryGetProperty("type", out var evTypeProp)) - return null; - - var evType = evTypeProp.GetString(); - - switch (evType) + return subtypeProp.GetString() switch { - case "content_block_delta": - if (!ev.TryGetProperty("delta", out var delta)) - return null; - if (!delta.TryGetProperty("type", out var deltaTypeProp)) - return null; - var deltaType = deltaTypeProp.GetString(); - if (deltaType == "text_delta") - { - return delta.TryGetProperty("text", out var textProp) - ? textProp.GetString() - : null; - } - return null; // input_json_delta and others → skip + "api_retry" => "[Retrying API call...]\n", + _ => null, + }; + } - case "content_block_stop": - return "\n"; + private static string? FormatAssistant(JsonElement root) => null; - case "content_block_start": - if (!ev.TryGetProperty("content_block", out var cb)) - return null; - if (cb.TryGetProperty("type", out var cbTypeProp) && - cbTypeProp.GetString() == "tool_use" && - cb.TryGetProperty("name", out var nameProp)) - return $"\n[Tool: {nameProp.GetString()}]\n"; - return null; + private static string? FormatUser(JsonElement root) => null; - default: - return null; // message_start, message_delta, etc. - } + private static string? FormatResult(JsonElement root) + { + if (root.TryGetProperty("result", out var resultProp)) + return $"\n--- Result ---\n{resultProp.GetString()}\n"; + return null; } public string FormatFile(string filePath)