1package provider
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "strings"
10 "time"
11
12 "github.com/anthropics/anthropic-sdk-go"
13 "github.com/anthropics/anthropic-sdk-go/bedrock"
14 "github.com/anthropics/anthropic-sdk-go/option"
15 "github.com/charmbracelet/crush/internal/config"
16 "github.com/charmbracelet/crush/internal/llm/models"
17 "github.com/charmbracelet/crush/internal/llm/tools"
18 "github.com/charmbracelet/crush/internal/logging"
19 "github.com/charmbracelet/crush/internal/message"
20)
21
22type anthropicOptions struct {
23 useBedrock bool
24 disableCache bool
25 shouldThink func(userMessage string) bool
26}
27
28type AnthropicOption func(*anthropicOptions)
29
30type anthropicClient struct {
31 providerOptions providerClientOptions
32 options anthropicOptions
33 client anthropic.Client
34}
35
36type AnthropicClient ProviderClient
37
38func newAnthropicClient(opts providerClientOptions) AnthropicClient {
39 anthropicOpts := anthropicOptions{}
40 for _, o := range opts.anthropicOptions {
41 o(&anthropicOpts)
42 }
43
44 anthropicClientOptions := []option.RequestOption{}
45 if opts.apiKey != "" {
46 anthropicClientOptions = append(anthropicClientOptions, option.WithAPIKey(opts.apiKey))
47 }
48 if anthropicOpts.useBedrock {
49 anthropicClientOptions = append(anthropicClientOptions, bedrock.WithLoadDefaultConfig(context.Background()))
50 }
51
52 client := anthropic.NewClient(anthropicClientOptions...)
53 return &anthropicClient{
54 providerOptions: opts,
55 options: anthropicOpts,
56 client: client,
57 }
58}
59
60func (a *anthropicClient) convertMessages(messages []message.Message) (anthropicMessages []anthropic.MessageParam) {
61 for i, msg := range messages {
62 cache := false
63 if i > len(messages)-3 {
64 cache = true
65 }
66 switch msg.Role {
67 case message.User:
68 content := anthropic.NewTextBlock(msg.Content().String())
69 if cache && !a.options.disableCache {
70 content.OfText.CacheControl = anthropic.CacheControlEphemeralParam{
71 Type: "ephemeral",
72 }
73 }
74 var contentBlocks []anthropic.ContentBlockParamUnion
75 contentBlocks = append(contentBlocks, content)
76 for _, binaryContent := range msg.BinaryContent() {
77 base64Image := binaryContent.String(models.ProviderAnthropic)
78 imageBlock := anthropic.NewImageBlockBase64(binaryContent.MIMEType, base64Image)
79 contentBlocks = append(contentBlocks, imageBlock)
80 }
81 anthropicMessages = append(anthropicMessages, anthropic.NewUserMessage(contentBlocks...))
82
83 case message.Assistant:
84 blocks := []anthropic.ContentBlockParamUnion{}
85 if msg.Content().String() != "" {
86 content := anthropic.NewTextBlock(msg.Content().String())
87 if cache && !a.options.disableCache {
88 content.OfText.CacheControl = anthropic.CacheControlEphemeralParam{
89 Type: "ephemeral",
90 }
91 }
92 blocks = append(blocks, content)
93 }
94
95 for _, toolCall := range msg.ToolCalls() {
96 var inputMap map[string]any
97 err := json.Unmarshal([]byte(toolCall.Input), &inputMap)
98 if err != nil {
99 continue
100 }
101 blocks = append(blocks, anthropic.NewToolUseBlock(toolCall.ID, inputMap, toolCall.Name))
102 }
103
104 if len(blocks) == 0 {
105 logging.Warn("There is a message without content, investigate, this should not happen")
106 continue
107 }
108 anthropicMessages = append(anthropicMessages, anthropic.NewAssistantMessage(blocks...))
109
110 case message.Tool:
111 results := make([]anthropic.ContentBlockParamUnion, len(msg.ToolResults()))
112 for i, toolResult := range msg.ToolResults() {
113 results[i] = anthropic.NewToolResultBlock(toolResult.ToolCallID, toolResult.Content, toolResult.IsError)
114 }
115 anthropicMessages = append(anthropicMessages, anthropic.NewUserMessage(results...))
116 }
117 }
118 return
119}
120
121func (a *anthropicClient) convertTools(tools []tools.BaseTool) []anthropic.ToolUnionParam {
122 anthropicTools := make([]anthropic.ToolUnionParam, len(tools))
123
124 for i, tool := range tools {
125 info := tool.Info()
126 toolParam := anthropic.ToolParam{
127 Name: info.Name,
128 Description: anthropic.String(info.Description),
129 InputSchema: anthropic.ToolInputSchemaParam{
130 Properties: info.Parameters,
131 // TODO: figure out how we can tell claude the required fields?
132 },
133 }
134
135 if i == len(tools)-1 && !a.options.disableCache {
136 toolParam.CacheControl = anthropic.CacheControlEphemeralParam{
137 Type: "ephemeral",
138 }
139 }
140
141 anthropicTools[i] = anthropic.ToolUnionParam{OfTool: &toolParam}
142 }
143
144 return anthropicTools
145}
146
147func (a *anthropicClient) finishReason(reason string) message.FinishReason {
148 switch reason {
149 case "end_turn":
150 return message.FinishReasonEndTurn
151 case "max_tokens":
152 return message.FinishReasonMaxTokens
153 case "tool_use":
154 return message.FinishReasonToolUse
155 case "stop_sequence":
156 return message.FinishReasonEndTurn
157 default:
158 return message.FinishReasonUnknown
159 }
160}
161
162func (a *anthropicClient) preparedMessages(messages []anthropic.MessageParam, tools []anthropic.ToolUnionParam) anthropic.MessageNewParams {
163 var thinkingParam anthropic.ThinkingConfigParamUnion
164 lastMessage := messages[len(messages)-1]
165 isUser := lastMessage.Role == anthropic.MessageParamRoleUser
166 messageContent := ""
167 temperature := anthropic.Float(0)
168 if isUser {
169 for _, m := range lastMessage.Content {
170 if m.OfText != nil && m.OfText.Text != "" {
171 messageContent = m.OfText.Text
172 }
173 }
174 if messageContent != "" && a.options.shouldThink != nil && a.options.shouldThink(messageContent) {
175 thinkingParam = anthropic.ThinkingConfigParamOfEnabled(int64(float64(a.providerOptions.maxTokens) * 0.8))
176 temperature = anthropic.Float(1)
177 }
178 }
179
180 return anthropic.MessageNewParams{
181 Model: anthropic.Model(a.providerOptions.model.APIModel),
182 MaxTokens: a.providerOptions.maxTokens,
183 Temperature: temperature,
184 Messages: messages,
185 Tools: tools,
186 Thinking: thinkingParam,
187 System: []anthropic.TextBlockParam{
188 {
189 Text: a.providerOptions.systemMessage,
190 CacheControl: anthropic.CacheControlEphemeralParam{
191 Type: "ephemeral",
192 },
193 },
194 },
195 }
196}
197
198func (a *anthropicClient) send(ctx context.Context, messages []message.Message, tools []tools.BaseTool) (response *ProviderResponse, err error) {
199 preparedMessages := a.preparedMessages(a.convertMessages(messages), a.convertTools(tools))
200 cfg := config.Get()
201 if cfg.Debug {
202 jsonData, _ := json.Marshal(preparedMessages)
203 logging.Debug("Prepared messages", "messages", string(jsonData))
204 }
205
206 attempts := 0
207 for {
208 attempts++
209 anthropicResponse, err := a.client.Messages.New(
210 ctx,
211 preparedMessages,
212 )
213 // If there is an error we are going to see if we can retry the call
214 if err != nil {
215 logging.Error("Error in Anthropic API call", "error", err)
216 retry, after, retryErr := a.shouldRetry(attempts, err)
217 if retryErr != nil {
218 return nil, retryErr
219 }
220 if retry {
221 logging.WarnPersist(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries), logging.PersistTimeArg, time.Millisecond*time.Duration(after+100))
222 select {
223 case <-ctx.Done():
224 return nil, ctx.Err()
225 case <-time.After(time.Duration(after) * time.Millisecond):
226 continue
227 }
228 }
229 return nil, retryErr
230 }
231
232 content := ""
233 for _, block := range anthropicResponse.Content {
234 if text, ok := block.AsAny().(anthropic.TextBlock); ok {
235 content += text.Text
236 }
237 }
238
239 return &ProviderResponse{
240 Content: content,
241 ToolCalls: a.toolCalls(*anthropicResponse),
242 Usage: a.usage(*anthropicResponse),
243 }, nil
244 }
245}
246
247func (a *anthropicClient) stream(ctx context.Context, messages []message.Message, tools []tools.BaseTool) <-chan ProviderEvent {
248 preparedMessages := a.preparedMessages(a.convertMessages(messages), a.convertTools(tools))
249 cfg := config.Get()
250 if cfg.Debug {
251 // jsonData, _ := json.Marshal(preparedMessages)
252 // logging.Debug("Prepared messages", "messages", string(jsonData))
253 }
254 attempts := 0
255 eventChan := make(chan ProviderEvent)
256 go func() {
257 for {
258 attempts++
259 anthropicStream := a.client.Messages.NewStreaming(
260 ctx,
261 preparedMessages,
262 )
263 accumulatedMessage := anthropic.Message{}
264
265 currentToolCallID := ""
266 for anthropicStream.Next() {
267 event := anthropicStream.Current()
268 err := accumulatedMessage.Accumulate(event)
269 if err != nil {
270 logging.Warn("Error accumulating message", "error", err)
271 continue
272 }
273
274 switch event := event.AsAny().(type) {
275 case anthropic.ContentBlockStartEvent:
276 if event.ContentBlock.Type == "text" {
277 eventChan <- ProviderEvent{Type: EventContentStart}
278 } else if event.ContentBlock.Type == "tool_use" {
279 currentToolCallID = event.ContentBlock.ID
280 eventChan <- ProviderEvent{
281 Type: EventToolUseStart,
282 ToolCall: &message.ToolCall{
283 ID: event.ContentBlock.ID,
284 Name: event.ContentBlock.Name,
285 Finished: false,
286 },
287 }
288 }
289
290 case anthropic.ContentBlockDeltaEvent:
291 if event.Delta.Type == "thinking_delta" && event.Delta.Thinking != "" {
292 eventChan <- ProviderEvent{
293 Type: EventThinkingDelta,
294 Thinking: event.Delta.Thinking,
295 }
296 } else if event.Delta.Type == "text_delta" && event.Delta.Text != "" {
297 eventChan <- ProviderEvent{
298 Type: EventContentDelta,
299 Content: event.Delta.Text,
300 }
301 } else if event.Delta.Type == "input_json_delta" {
302 if currentToolCallID != "" {
303 eventChan <- ProviderEvent{
304 Type: EventToolUseDelta,
305 ToolCall: &message.ToolCall{
306 ID: currentToolCallID,
307 Finished: false,
308 Input: event.Delta.PartialJSON,
309 },
310 }
311 }
312 }
313 case anthropic.ContentBlockStopEvent:
314 if currentToolCallID != "" {
315 eventChan <- ProviderEvent{
316 Type: EventToolUseStop,
317 ToolCall: &message.ToolCall{
318 ID: currentToolCallID,
319 },
320 }
321 currentToolCallID = ""
322 } else {
323 eventChan <- ProviderEvent{Type: EventContentStop}
324 }
325
326 case anthropic.MessageStopEvent:
327 content := ""
328 for _, block := range accumulatedMessage.Content {
329 if text, ok := block.AsAny().(anthropic.TextBlock); ok {
330 content += text.Text
331 }
332 }
333
334 eventChan <- ProviderEvent{
335 Type: EventComplete,
336 Response: &ProviderResponse{
337 Content: content,
338 ToolCalls: a.toolCalls(accumulatedMessage),
339 Usage: a.usage(accumulatedMessage),
340 FinishReason: a.finishReason(string(accumulatedMessage.StopReason)),
341 },
342 Content: content,
343 }
344 }
345 }
346
347 err := anthropicStream.Err()
348 if err == nil || errors.Is(err, io.EOF) {
349 close(eventChan)
350 return
351 }
352 // If there is an error we are going to see if we can retry the call
353 retry, after, retryErr := a.shouldRetry(attempts, err)
354 if retryErr != nil {
355 eventChan <- ProviderEvent{Type: EventError, Error: retryErr}
356 close(eventChan)
357 return
358 }
359 if retry {
360 logging.WarnPersist(fmt.Sprintf("Retrying due to rate limit... attempt %d of %d", attempts, maxRetries), logging.PersistTimeArg, time.Millisecond*time.Duration(after+100))
361 select {
362 case <-ctx.Done():
363 // context cancelled
364 if ctx.Err() != nil {
365 eventChan <- ProviderEvent{Type: EventError, Error: ctx.Err()}
366 }
367 close(eventChan)
368 return
369 case <-time.After(time.Duration(after) * time.Millisecond):
370 continue
371 }
372 }
373 if ctx.Err() != nil {
374 eventChan <- ProviderEvent{Type: EventError, Error: ctx.Err()}
375 }
376
377 close(eventChan)
378 return
379 }
380 }()
381 return eventChan
382}
383
384func (a *anthropicClient) shouldRetry(attempts int, err error) (bool, int64, error) {
385 var apierr *anthropic.Error
386 if !errors.As(err, &apierr) {
387 return false, 0, err
388 }
389
390 if apierr.StatusCode != 429 && apierr.StatusCode != 529 {
391 return false, 0, err
392 }
393
394 if attempts > maxRetries {
395 return false, 0, fmt.Errorf("maximum retry attempts reached for rate limit: %d retries", maxRetries)
396 }
397
398 retryMs := 0
399 retryAfterValues := apierr.Response.Header.Values("Retry-After")
400
401 backoffMs := 2000 * (1 << (attempts - 1))
402 jitterMs := int(float64(backoffMs) * 0.2)
403 retryMs = backoffMs + jitterMs
404 if len(retryAfterValues) > 0 {
405 if _, err := fmt.Sscanf(retryAfterValues[0], "%d", &retryMs); err == nil {
406 retryMs = retryMs * 1000
407 }
408 }
409 return true, int64(retryMs), nil
410}
411
412func (a *anthropicClient) toolCalls(msg anthropic.Message) []message.ToolCall {
413 var toolCalls []message.ToolCall
414
415 for _, block := range msg.Content {
416 switch variant := block.AsAny().(type) {
417 case anthropic.ToolUseBlock:
418 toolCall := message.ToolCall{
419 ID: variant.ID,
420 Name: variant.Name,
421 Input: string(variant.Input),
422 Type: string(variant.Type),
423 Finished: true,
424 }
425 toolCalls = append(toolCalls, toolCall)
426 }
427 }
428
429 return toolCalls
430}
431
432func (a *anthropicClient) usage(msg anthropic.Message) TokenUsage {
433 return TokenUsage{
434 InputTokens: msg.Usage.InputTokens,
435 OutputTokens: msg.Usage.OutputTokens,
436 CacheCreationTokens: msg.Usage.CacheCreationInputTokens,
437 CacheReadTokens: msg.Usage.CacheReadInputTokens,
438 }
439}
440
441func WithAnthropicBedrock(useBedrock bool) AnthropicOption {
442 return func(options *anthropicOptions) {
443 options.useBedrock = useBedrock
444 }
445}
446
447func WithAnthropicDisableCache() AnthropicOption {
448 return func(options *anthropicOptions) {
449 options.disableCache = true
450 }
451}
452
453func DefaultShouldThinkFn(s string) bool {
454 return strings.Contains(strings.ToLower(s), "think")
455}
456
457func WithAnthropicShouldThinkFn(fn func(string) bool) AnthropicOption {
458 return func(options *anthropicOptions) {
459 options.shouldThink = fn
460 }
461}