refactor(ui): skeleton dispatch for StreamLineFormatter rewrite
This commit is contained in:
@@ -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);
|
||||
"system" => FormatSystem(root),
|
||||
"assistant" => FormatAssistant(root),
|
||||
"user" => FormatUser(root),
|
||||
"result" => FormatResult(root),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
case "result":
|
||||
private static string? FormatSystem(JsonElement root)
|
||||
{
|
||||
if (!root.TryGetProperty("subtype", out var subtypeProp))
|
||||
return null;
|
||||
return subtypeProp.GetString() switch
|
||||
{
|
||||
"api_retry" => "[Retrying API call...]\n",
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
private static string? FormatAssistant(JsonElement root) => null;
|
||||
|
||||
private static string? FormatUser(JsonElement root) => null;
|
||||
|
||||
private static string? FormatResult(JsonElement root)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string? FormatStreamEvent(JsonElement root)
|
||||
{
|
||||
if (!root.TryGetProperty("event", out var ev))
|
||||
return null;
|
||||
if (!ev.TryGetProperty("type", out var evTypeProp))
|
||||
return null;
|
||||
|
||||
var evType = evTypeProp.GetString();
|
||||
|
||||
switch (evType)
|
||||
{
|
||||
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
|
||||
|
||||
case "content_block_stop":
|
||||
return "\n";
|
||||
|
||||
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;
|
||||
|
||||
default:
|
||||
return null; // message_start, message_delta, etc.
|
||||
}
|
||||
}
|
||||
|
||||
public string FormatFile(string filePath)
|
||||
|
||||
Reference in New Issue
Block a user