-
Notifications
You must be signed in to change notification settings - Fork 0
/
claude_agent.go
143 lines (130 loc) · 3.75 KB
/
claude_agent.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package corefactorer
import (
"context"
"fmt"
"log/slog"
"github.com/liushuangls/go-anthropic/v2"
"github.com/sashabaranov/go-openai/jsonschema"
)
type ClaudeAgent struct {
client *anthropic.Client
logger *slog.Logger
model anthropic.Model
toolUse *anthropic.MessageContentToolUse
}
func NewClaudeAgent(client *anthropic.Client, logger *slog.Logger) Agent {
return &ClaudeAgent{
client: client,
logger: logger,
}
}
func (a *ClaudeAgent) CreateRefactoringTarget(ctx context.Context, prompt string, modelName string, temperature float32) (*RefactoringTarget, error) {
a.model = anthropic.Model(modelName)
resp, err := a.client.CreateMessages(ctx, anthropic.MessagesRequest{
Model: a.model,
Messages: []anthropic.Message{
anthropic.NewUserTextMessage(prompt),
},
MaxTokens: 1000,
Tools: []anthropic.ToolDefinition{a.getTool()},
})
if err != nil {
// TODO: Wrap error
return nil, err
}
if len(resp.Content) == 0 {
return nil, fmt.Errorf("no content in response")
}
var toolUse *anthropic.MessageContentToolUse
for _, c := range resp.Content {
if c.Type == anthropic.MessagesContentTypeToolUse {
toolUse = c.MessageContentToolUse
break
}
}
if toolUse == nil {
return nil, fmt.Errorf("no tool use in response")
}
target := &RefactoringTarget{
UserPrompt: prompt,
ToolCallID: toolUse.ID,
}
for _, c := range resp.Content {
if c.Type != anthropic.MessagesContentTypeToolUse {
continue
}
a.toolUse = c.MessageContentToolUse
var tmp RefactoringTarget
if err := c.UnmarshalInput(&tmp); err != nil {
return nil, fmt.Errorf("failed to UnmarshalInput: %w", err)
}
target.PullRequestURLs = append(target.PullRequestURLs, tmp.PullRequestURLs...)
target.Files = append(target.Files, tmp.Files...)
}
return target.Unique(), nil
}
func (a *ClaudeAgent) CreateRefactoringResult(ctx context.Context, req *RefactoringRequest) (*RefactoringResult, error) {
assistanceMessage, err := req.CreateAssistanceMessage()
if err != nil {
return nil, fmt.Errorf("failed to create assistance message: %w", err)
}
messages := []anthropic.Message{
anthropic.NewUserTextMessage(req.UserPrompt),
{
Role: anthropic.RoleAssistant,
Content: []anthropic.MessageContent{
anthropic.NewToolUseMessageContent(req.ToolCallID, a.toolUse.Name, a.toolUse.Input),
},
},
anthropic.NewToolResultsMessage(req.ToolCallID, assistanceMessage, false),
}
a.logger.Debug("API call: a.client.CreateMessages")
resp, err := a.client.CreateMessages(
ctx,
anthropic.MessagesRequest{
MaxTokens: 4096,
Model: a.model,
Messages: messages,
Tools: []anthropic.ToolDefinition{a.getTool()},
},
)
if err != nil {
return nil, fmt.Errorf("failed to create chat completion: %w", err)
}
if len(resp.Content) == 0 {
return nil, fmt.Errorf("no content in response")
}
for i, c := range resp.Content {
a.logger.Debug("CreateMessages response", slog.Int("index", i), slog.Any("content", c))
}
return &RefactoringResult{
RawContent: resp.Content[0].GetText(),
}, nil
}
func (a *ClaudeAgent) getTool() anthropic.ToolDefinition {
tool := anthropic.ToolDefinition{
Name: functionName,
Description: functionDescription,
InputSchema: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
functionParameter1Name: {
Type: jsonschema.Array,
Description: functionParameter1Description,
Items: &jsonschema.Definition{
Type: jsonschema.String,
},
},
functionParameter2Name: {
Type: jsonschema.Array,
Description: functionParameter2Description,
Items: &jsonschema.Definition{
Type: jsonschema.String,
},
},
},
Required: []string{functionParameter1Name, functionParameter2Name},
},
}
return tool
}