Castor-Gemini/format_rich_log.go

98 lines
2.2 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"go.wit.com/lib/protobuf/chatpb"
"go.wit.com/log"
)
const termWidth = 120 // The target width for the formatted output boxes.
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: go run format_rich_log.go <path_to_log_file>\n")
os.Exit(1)
}
filename := os.Args[1]
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %v", filename, err)
}
logData, err := chatpb.UnmarshalChatsTEXT(data)
if err != nil {
log.Fatalf("Error unmarshaling log file %s: %v", filename, err)
}
for _, chat := range logData.GetChats() {
author := chat.GetFrom().String()
content := chat.GetContent()
// Print the conversational content first.
if content != "" {
fmt.Printf("✦ %s: %s\n", author, content)
}
// Now, format and print any tool calls.
for _, toolCall := range chat.GetToolCalls() {
printToolCallBox(toolCall)
}
}
}
// printToolCallBox handles the decorative formatting for a single tool call.
func printToolCallBox(tc *chatpb.ToolCall) {
boxWidth := termWidth - 2 // Account for the side borders.
// ---
fmt.Printf(" ╭%s╮\n", strings.Repeat("─", boxWidth))
// ---
header := fmt.Sprintf(" ✔ %s %s (%s)", tc.GetName(), tc.GetInput(), tc.GetDescription())
printWrappedLine(header, boxWidth)
printEmptyLine(boxWidth)
// ---
if stdout := tc.GetOutputStdout(); stdout != "" {
for _, line := range strings.Split(stdout, "\n") {
printWrappedLine(" "+line, boxWidth)
}
}
// ---
if stderr := tc.GetOutputStderr(); stderr != "" {
for _, line := range strings.Split(stderr, "\n") {
printWrappedLine(" "+line, boxWidth)
}
}
printEmptyLine(boxWidth)
// ---
fmt.Printf(" ╰%s╯\n", strings.Repeat("─", boxWidth))
}
// printWrappedLine prints a line of text, wrapping it if it's too long.
func printWrappedLine(text string, width int) {
if len(text) == 0 {
printEmptyLine(width)
return
}
// Simple wrapping logic.
for len(text) > width {
fmt.Printf(" │ %-*s │\n", width, text[:width])
text = text[width:]
}
fmt.Printf(" │ %-*s │\n", width, text)
}
// printEmptyLine prints a blank line within the box.
func printEmptyLine(width int) {
fmt.Printf(" │ %*s │\n", width, "")
}