-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
106 lines (92 loc) · 2.66 KB
/
main.go
File metadata and controls
106 lines (92 loc) · 2.66 KB
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
package main
import (
"context"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"github.com/mhpenta/claude-code-sdk-go/claudecode"
)
func main() {
projectRoot, err := filepath.Abs("../..")
if err != nil {
log.Fatal(err)
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
fmt.Println("Comment Improvement Example")
fmt.Println("===========================")
fmt.Printf("Project root: %s\n\n", projectRoot)
client, err := claudecode.New(
claudecode.WithWorkingDirectory(projectRoot),
claudecode.WithLogger(logger),
claudecode.WithSystemPrompt("Improve Go code documentation and comments."),
claudecode.WithPermissionMode(claudecode.PermissionModeAcceptEdits),
claudecode.WithAddDirs(filepath.Join(projectRoot, "claudecode")),
)
if err != nil {
log.Fatal("Failed to create client:", err)
}
defer client.Close()
session, err := client.NewSession(context.Background())
if err != nil {
log.Fatal("Failed to create session:", err)
}
defer session.Close()
fmt.Println("Searching for improvable comments...")
prompt := `Search the claudecode/ directory and:
1. Find ONE comment that could be improved (clarity, context, grammar)
2. Show the current comment and file location
3. Explain why it needs improvement
4. Use the Edit tool to improve it
Focus on function/type documentation. Choose something meaningful.`
if err := session.Send(context.Background(), prompt); err != nil {
log.Fatal("Failed to send prompt:", err)
}
fmt.Println("\nAnalysis:")
fmt.Println("---------")
var hasEdit bool
msgChan, err := session.Receive(context.Background())
if err != nil {
log.Fatal("Failed to receive messages:", err)
}
for msg := range msgChan {
switch m := msg.(type) {
case *claudecode.AssistantMessage:
for _, block := range m.Content {
switch block.Type {
case "text":
if block.Text != nil {
fmt.Print(*block.Text)
}
case "tool_use":
if block.Tool != nil && block.Tool.Name == "Edit" {
hasEdit = true
fmt.Printf("\n\nEdit applied to: %v\n", block.Tool.Input["file_path"])
}
}
}
case *claudecode.SystemMessage:
if m.Subtype == "tool_use" {
if name, ok := m.Data["name"].(string); ok && name == "Edit" {
fmt.Println("\n[Applying edit...]")
}
}
case *claudecode.ResultMessage:
fmt.Printf("\n\nSummary:")
fmt.Printf("\n- Duration: %dms", m.DurationMS)
if m.TotalCostUSD != nil {
fmt.Printf("\n- Cost: $%.4f", *m.TotalCostUSD)
}
fmt.Printf("\n- Success: %v", !m.IsError)
if hasEdit {
fmt.Println("\n\nComment successfully improved!")
} else {
fmt.Println("\n\nNo edits were made")
}
return
}
}
}