-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (85 loc) · 2.26 KB
/
Copy pathmain.go
File metadata and controls
100 lines (85 loc) · 2.26 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
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
// Config holds the assistant's runtime configuration.
// Loaded from config.json if present, otherwise uses defaults.
type Config struct {
SystemPrompt string `json:"system_prompt"`
Model string `json:"model"`
MaxTokens int64 `json:"max_tokens"`
}
func loadConfig() Config {
cfg := Config{
SystemPrompt: "You are a helpful assistant. Be concise and direct.",
Model: string(anthropic.ModelClaudeOpus4_7),
MaxTokens: 1024,
}
data, err := os.ReadFile("config.json")
if err == nil {
json.Unmarshal(data, &cfg) // silently ignore parse errors, use defaults
}
return cfg
}
func main() {
client := anthropic.NewClient(option.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")))
ctx := context.Background()
cfg := loadConfig()
history := []anthropic.MessageParam{}
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Claude CLI Assistant — type 'exit' to quit, 'reset' to clear history")
fmt.Printf("Model: %s | System: %q\n\n", cfg.Model, cfg.SystemPrompt)
for {
fmt.Print("You: ")
if !scanner.Scan() {
break
}
input := strings.TrimSpace(scanner.Text())
if input == "" {
continue
}
if input == "exit" {
fmt.Println("Goodbye!")
break
}
if input == "reset" {
history = nil
fmt.Println("[History cleared]")
continue
}
history = append(history, anthropic.NewUserMessage(anthropic.NewTextBlock(input)))
stream := client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
Model: anthropic.Model(cfg.Model),
MaxTokens: cfg.MaxTokens,
System: []anthropic.TextBlockParam{
anthropic.NewTextBlockParam(cfg.SystemPrompt),
},
Messages: history,
})
message := anthropic.Message{}
fmt.Print("Claude: ")
for stream.Next() {
event := stream.Current()
message.Accumulate(event)
switch delta := event.Delta.(type) {
case anthropic.ContentBlockDeltaEventDelta:
if delta.Type == "text_delta" {
fmt.Print(delta.Text)
}
}
}
if err := stream.Err(); err != nil {
fmt.Fprintf(os.Stderr, "\nstream error: %v\n", err)
continue
}
fmt.Println()
history = append(history, message.ToParam())
}
}