1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package cli
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/thepeterstone/claudomator/internal/storage"
)
type executionStore interface {
GetExecution(id string) (*storage.Execution, error)
}
func newLogsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "logs <exec-id>",
Short: "Show logs for an execution",
Args: cobra.ExactArgs(1),
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
store, err := storage.Open(cfg.DBPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer store.Close()
return renderLogs(args[0], store, cmd.OutOrStdout())
},
}
return cmd
}
type streamMessage struct {
Type string `json:"type"`
Message *assistantMsg `json:"message,omitempty"`
}
type assistantMsg struct {
Content []contentBlock `json:"content"`
}
type contentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
}
func renderLogs(execID string, store executionStore, w io.Writer) error {
exec, err := store.GetExecution(execID)
if err != nil {
fmt.Fprintf(w, "execution %s not found\n", execID)
return fmt.Errorf("execution %s not found", execID)
}
if exec.StdoutPath == "" {
fmt.Fprintln(w, "no output recorded")
return nil
}
fi, err := os.Stat(exec.StdoutPath)
if err != nil || fi.Size() == 0 {
fmt.Fprintln(w, "no output recorded")
return nil
}
f, err := os.Open(exec.StdoutPath)
if err != nil {
fmt.Fprintln(w, "no output recorded")
return nil
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Bytes()
if len(line) == 0 {
continue
}
var msg streamMessage
if err := json.Unmarshal(line, &msg); err != nil {
continue
}
if msg.Type != "assistant" || msg.Message == nil {
continue
}
for _, block := range msg.Message.Content {
switch block.Type {
case "text":
fmt.Fprintln(w, block.Text)
case "tool_use":
summary := string(block.Input)
if len(summary) > 80 {
summary = summary[:80]
}
fmt.Fprintf(w, " > %s (%s)\n", block.Name, summary)
}
}
}
fmt.Fprintf(w, "Cost: $%.4f Exit: %d\n", exec.CostUSD, exec.ExitCode)
return nil
}
|