-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanthropic.go
767 lines (675 loc) · 22.5 KB
/
anthropic.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//nolint:tagliatelle
package fun
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/rs/zerolog"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/go/x"
"gitlab.com/tozd/identifier"
)
var anthropicRateLimiter = &keyedRateLimiter{ //nolint:gochecknoglobals
mu: sync.RWMutex{},
limiters: map[string]map[string]any{},
}
type anthropicMessage struct {
Role string `json:"role"`
Content []anthropicContent `json:"content"`
}
type anthropicSystem struct {
Type string `json:"type"`
Text string `json:"text"`
CacheControl *anthropicCacheControl `json:"cache_control,omitempty"`
}
type anthropicRequest struct {
Model string `json:"model"`
Messages []anthropicMessage `json:"messages"`
MaxTokens int `json:"max_tokens"`
System []anthropicSystem `json:"system,omitempty"`
Temperature float64 `json:"temperature"`
Tools []anthropicTool `json:"tools,omitempty"`
}
type anthropicCacheControl struct {
Type string `json:"type"`
}
type anthropicContent struct {
Type string `json:"type"`
Text *string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
ToolUseID string `json:"tool_use_id,omitempty"`
IsError bool `json:"is_error,omitempty"`
Content *string `json:"content,omitempty"`
CacheControl *anthropicCacheControl `json:"cache_control,omitempty"`
}
type anthropicResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Content []anthropicContent `json:"content"`
Model string `json:"model"`
StopReason string `json:"stop_reason"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
CacheCreationInputTokens *int `json:"cache_creation_input_tokens,omitempty"`
CacheReadInputTokens *int `json:"cache_read_input_tokens,omitempty"`
} `json:"usage"`
Error *struct {
Message string `json:"message"`
Type string `json:"type"`
} `json:"error,omitempty"`
}
func parseAnthropicRateLimitHeaders(resp *http.Response) ( //nolint:nonamedreturns
limitRequests, limitInputTokens, limitOutputTokens,
remainingRequests, remainingInputTokens, remainingOutputTokens int,
resetRequests, resetInputTokens, resetOutputTokens time.Time,
ok bool, errE errors.E,
) {
limitRequestsStr := resp.Header.Get("Anthropic-Ratelimit-Requests-Limit") // Request per minute.
remainingRequestsStr := resp.Header.Get("Anthropic-Ratelimit-Requests-Remaining") // Remaining requests in current window (a minute).
resetRequestsStr := resp.Header.Get("Anthropic-Ratelimit-Requests-Reset") // When will requests window reset.
limitInputTokensStr := resp.Header.Get("Anthropic-Ratelimit-Input-Tokens-Limit") // Input tokens per minute or day.
remainingInputTokensStr := resp.Header.Get("Anthropic-Ratelimit-Input-Tokens-Remaining") // Remaining input tokens in current window (a minute).
resetInputTokensStr := resp.Header.Get("Anthropic-Ratelimit-Input-Tokens-Reset") // When will input tokens window reset.
limitOutputTokensStr := resp.Header.Get("Anthropic-Ratelimit-Output-Tokens-Limit") // Output tokens per minute or day.
remainingOutputTokensStr := resp.Header.Get("Anthropic-Ratelimit-Output-Tokens-Remaining") // Remaining output tokens in current window (a minute).
resetOutputTokensStr := resp.Header.Get("Anthropic-Ratelimit-Output-Tokens-Reset") // When will output tokens window reset.
if limitRequestsStr == "" || remainingRequestsStr == "" || resetRequestsStr == "" ||
limitInputTokensStr == "" || remainingInputTokensStr == "" || resetInputTokensStr == "" ||
limitOutputTokensStr == "" || remainingOutputTokensStr == "" || resetOutputTokensStr == "" {
// ok == false here.
return //nolint:nakedret
}
// We have all the headers we want.
ok = true
var err error
limitRequests, err = strconv.Atoi(limitRequestsStr)
if err != nil {
errE = errors.WithDetails(err, "value", limitRequestsStr)
return //nolint:nakedret
}
remainingRequests, err = strconv.Atoi(remainingRequestsStr)
if err != nil {
errE = errors.WithDetails(err, "value", remainingRequestsStr)
return //nolint:nakedret
}
resetRequests, err = time.Parse(time.RFC3339, resetRequestsStr)
if err != nil {
errE = errors.WithDetails(err, "value", resetRequestsStr)
return //nolint:nakedret
}
limitInputTokens, err = strconv.Atoi(limitInputTokensStr)
if err != nil {
errE = errors.WithDetails(err, "value", limitInputTokensStr)
return //nolint:nakedret
}
remainingInputTokens, err = strconv.Atoi(remainingInputTokensStr)
if err != nil {
errE = errors.WithDetails(err, "value", remainingInputTokensStr)
return //nolint:nakedret
}
resetInputTokens, err = time.Parse(time.RFC3339, resetInputTokensStr)
if err != nil {
errE = errors.WithDetails(err, "value", resetInputTokensStr)
return //nolint:nakedret
}
limitOutputTokens, err = strconv.Atoi(limitOutputTokensStr)
if err != nil {
errE = errors.WithDetails(err, "value", limitOutputTokensStr)
return //nolint:nakedret
}
remainingOutputTokens, err = strconv.Atoi(remainingOutputTokensStr)
if err != nil {
errE = errors.WithDetails(err, "value", remainingOutputTokensStr)
return //nolint:nakedret
}
resetOutputTokens, err = time.Parse(time.RFC3339, resetOutputTokensStr)
if err != nil {
errE = errors.WithDetails(err, "value", resetOutputTokensStr)
return //nolint:nakedret
}
return //nolint:nakedret
}
type anthropicTool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputJSONSchema json.RawMessage `json:"input_schema"`
CacheControl *anthropicCacheControl `json:"cache_control,omitempty"`
tool TextTooler
}
var _ TextProvider = (*AnthropicTextProvider)(nil)
// AnthropicTextProvider is a [TextProvider] which provides integration with
// text-based [Anthropic] AI models.
//
// [Anthropic]: https://www.anthropic.com/
type AnthropicTextProvider struct {
// Client is a HTTP client to be used for API calls. If not provided
// a rate-limited retryable HTTP client is initialized instead.
Client *http.Client `json:"-"`
// APIKey is the API key to be used for API calls.
APIKey string `json:"-"`
// Model is the name of the model to be used.
Model string `json:"model"`
// MaxContextLength is the maximum total number of tokens allowed to be used
// with the underlying AI model (i.e., the maximum context window).
// If not provided, heuristics are used to determine it automatically.
MaxContextLength int `json:"maxContextLength"`
// MaxResponseLength is the maximum number of tokens allowed to be used in
// a response with the underlying AI model. If not provided, heuristics
// are used to determine it automatically.
MaxResponseLength int `json:"maxResponseLength"`
// PromptCaching set to true enables prompt caching.
PromptCaching bool `json:"promptCaching"`
// Temperature is how creative should the AI model be.
// Default is 0 which means not at all.
Temperature float64 `json:"temperature"`
rateLimiterKey string
system []anthropicSystem
messages []anthropicMessage
tools []anthropicTool
}
func (a AnthropicTextProvider) MarshalJSON() ([]byte, error) {
// We define a new type to not recurse into this same MarshalJSON.
type P AnthropicTextProvider
t := struct {
Type string `json:"type"`
P
}{
Type: "anthropic",
P: P(a),
}
return x.MarshalWithoutEscapeHTML(t)
}
// Init implements [TextProvider] interface.
func (a *AnthropicTextProvider) Init(_ context.Context, messages []ChatMessage) errors.E {
if a.messages != nil {
return errors.WithStack(ErrAlreadyInitialized)
}
a.messages = []anthropicMessage{}
for _, message := range messages {
if message.Role == roleSystem {
if a.system != nil {
return errors.WithStack(ErrMultipleSystemMessages)
}
a.system = []anthropicSystem{
{
Type: "text",
Text: message.Content,
CacheControl: nil,
},
}
} else {
a.messages = append(a.messages, anthropicMessage{
Role: message.Role,
Content: []anthropicContent{
{ //nolint:exhaustruct
Type: typeText,
Text: &message.Content,
CacheControl: nil,
},
},
})
}
}
if a.PromptCaching {
// We want to set a cache breakpoint as late as possible. And the order is tools, system, then messages.
if len(a.messages) > 0 {
a.messages[len(a.messages)-1].Content[len(a.messages[len(a.messages)-1].Content)-1].CacheControl = &anthropicCacheControl{
Type: "ephemeral",
}
} else if len(a.system) > 0 {
a.system[len(a.system)-1].CacheControl = &anthropicCacheControl{
Type: "ephemeral",
}
}
}
a.rateLimiterKey = fmt.Sprintf("%s-%s", a.APIKey, a.Model)
if a.Client == nil {
a.Client = newClient(
func(req *http.Request) error {
ctx := req.Context()
estimatedInputTokens, estimatedOutputTokens := getEstimatedTokens(ctx)
// Rate limit retries.
return anthropicRateLimiter.Take(ctx, a.rateLimiterKey, map[string]int{
"rpm": 1,
"itpd": estimatedInputTokens,
"otpm": estimatedOutputTokens,
})
},
nil,
nil,
)
a.Client.Transport.(*retryablehttp.RoundTripper).Client.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) { //nolint:forcetypeassert
if err != nil {
check, err := retryablehttp.ErrorPropagatedRetryPolicy(ctx, resp, err) //nolint:govet
return check, errors.WithStack(err)
}
if resp.StatusCode == http.StatusTooManyRequests {
// We read the body and provide it back.
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body = io.NopCloser(bytes.NewReader(body))
if resp.Header.Get("Content-Type") == applicationJSONHeader && json.Valid(body) {
zerolog.Ctx(ctx).Warn().RawJSON("body", body).Msg("hit rate limit")
} else {
zerolog.Ctx(ctx).Warn().Str("body", string(body)).Msg("hit rate limit")
}
}
limitRequests, limitInputTokens, limitOutputTokens,
remainingRequests, remainingInputTokens, remainingOutputTokens,
resetRequests, resetInputTokens, resetOutputTokens, ok, errE := parseAnthropicRateLimitHeaders(resp)
if errE != nil {
return false, errE
}
if ok {
anthropicRateLimiter.Set(a.rateLimiterKey, map[string]any{
"rpm": resettingRateLimit{
Limit: limitRequests,
Remaining: remainingRequests,
Window: time.Minute,
Resets: resetRequests,
},
"itpd": resettingRateLimit{
Limit: limitInputTokens,
Remaining: remainingInputTokens,
Window: time.Minute,
Resets: resetInputTokens,
},
"otpd": resettingRateLimit{
Limit: limitOutputTokens,
Remaining: remainingOutputTokens,
Window: time.Minute,
Resets: resetOutputTokens,
},
})
}
check, err := retryablehttp.ErrorPropagatedRetryPolicy(ctx, resp, err)
return check, errors.WithStack(err)
}
}
if a.MaxContextLength == 0 {
a.MaxContextLength = a.maxContextLength()
}
if a.MaxResponseLength == 0 {
a.MaxResponseLength = a.maxResponseTokens()
}
return nil
}
// Chat implements [TextProvider] interface.
func (a *AnthropicTextProvider) Chat(ctx context.Context, message ChatMessage) (string, errors.E) { //nolint:maintidx
callID := identifier.New().String()
var callRecorder *TextRecorderCall
if recorder := GetTextRecorder(ctx); recorder != nil {
callRecorder = recorder.newCall(callID, a)
defer recorder.recordCall(callRecorder)
}
logger := zerolog.Ctx(ctx).With().Str("fun", callID).Logger()
ctx = logger.WithContext(ctx)
messages := slices.Clone(a.messages)
messages = append(messages, anthropicMessage{
Role: message.Role,
Content: []anthropicContent{
{ //nolint:exhaustruct
Type: typeText,
Text: &message.Content,
},
},
})
if callRecorder != nil {
for _, system := range a.system {
callRecorder.addMessage(roleSystem, system.Text, "", "", false)
}
for _, message := range messages {
a.recordMessage(callRecorder, message)
}
callRecorder.notify("", nil)
}
for {
request, errE := x.MarshalWithoutEscapeHTML(anthropicRequest{
Model: a.Model,
Messages: messages,
MaxTokens: a.MaxResponseLength,
System: a.system,
Temperature: a.Temperature,
Tools: a.tools,
})
if errE != nil {
return "", errE
}
estimatedInputTokens, estimatedOutputTokens := a.estimatedTokens(messages)
req, err := http.NewRequestWithContext(
withEstimatedTokens(ctx, estimatedInputTokens, estimatedOutputTokens),
http.MethodPost,
"https://api.anthropic.com/v1/messages",
bytes.NewReader(request),
)
if err != nil {
return "", errors.WithStack(err)
}
req.Header.Add("X-Api-Key", a.APIKey)
req.Header.Add("Anthropic-Version", "2023-06-01")
req.Header.Add("Content-Type", "application/json")
if a.PromptCaching {
req.Header.Add("Anthropic-Beta", "prompt-caching-2024-07-31")
}
// Rate limit the initial request.
errE = anthropicRateLimiter.Take(ctx, a.rateLimiterKey, map[string]int{
"rpm": 1,
"itpd": estimatedInputTokens,
"otpm": estimatedOutputTokens,
})
if errE != nil {
return "", errE
}
start := time.Now()
resp, err := a.Client.Do(req)
var apiRequest string
if resp != nil {
apiRequest = resp.Header.Get("Request-Id")
}
if err != nil {
errE = errors.Prefix(err, ErrAPIRequestFailed)
if apiRequest != "" {
errors.Details(errE)["apiRequest"] = apiRequest
}
return "", errE
}
defer resp.Body.Close()
defer io.Copy(io.Discard, resp.Body) //nolint:errcheck
if apiRequest == "" {
return "", errors.WithStack(ErrMissingRequestID)
}
var response anthropicResponse
errE = x.DecodeJSON(resp.Body, &response)
if errE != nil {
errors.Details(errE)["apiRequest"] = apiRequest
return "", errE
}
apiCallDuration := time.Since(start)
if response.Error != nil {
return "", errors.WithDetails(
ErrAPIResponseError,
"body", response.Error,
"apiRequest", apiRequest,
)
}
if callRecorder != nil {
callRecorder.addUsedTokens(
apiRequest,
a.MaxContextLength,
a.MaxResponseLength,
response.Usage.InputTokens,
response.Usage.OutputTokens,
response.Usage.CacheCreationInputTokens,
response.Usage.CacheReadInputTokens,
)
callRecorder.addUsedTime(
apiRequest,
0,
0,
apiCallDuration,
)
a.recordMessage(callRecorder, anthropicMessage{
Role: response.Role,
Content: response.Content,
})
callRecorder.notify("", nil)
}
if response.Usage.InputTokens+response.Usage.OutputTokens >= a.MaxContextLength {
return "", errors.WithDetails(
ErrUnexpectedNumberOfTokens,
"prompt", response.Usage.InputTokens,
"response", response.Usage.OutputTokens,
"total", response.Usage.InputTokens+response.Usage.OutputTokens,
"maxTotal", a.MaxContextLength,
"maxResponse", a.MaxResponseLength,
"apiRequest", apiRequest,
)
}
if response.Role != roleAssistant {
return "", errors.WithDetails(
ErrUnexpectedRole,
"role", response.Role,
"apiRequest", apiRequest,
)
}
if response.StopReason == roleToolUse {
if len(response.Content) == 0 {
return "", errors.WithDetails(
ErrUnexpectedNumberOfMessages,
"number", len(response.Content),
"apiRequest", apiRequest,
)
}
// We have already recorded this message above.
messages = append(messages, anthropicMessage{
Role: roleAssistant,
Content: response.Content,
})
// We make space for tool results (one per tool call) so that the Content slice
// does not grow when appending below and invalidate pointers goroutines keep.
messages = append(messages, anthropicMessage{
Role: roleUser,
Content: make([]anthropicContent, 0, len(response.Content)),
})
if callRecorder != nil {
// We grow the slice inside call recorder as well.
callRecorder.prepareForToolMessages(len(response.Content))
}
ct, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
for _, content := range response.Content {
switch content.Type {
case typeText:
// We do nothing.
case roleToolUse:
messages[len(messages)-1].Content = append(messages[len(messages)-1].Content, anthropicContent{ //nolint:exhaustruct
Type: roleToolResult,
ToolUseID: content.ID,
})
result := &messages[len(messages)-1].Content[len(messages[len(messages)-1].Content)-1]
toolCtx := ct
var toolMessage *TextRecorderMessage
if callRecorder != nil {
toolCtx, toolMessage = callRecorder.startToolMessage(ct, content.ID)
}
wg.Add(1)
go func() {
defer wg.Done()
a.callToolWrapper(toolCtx, apiRequest, content, result, callRecorder, toolMessage)
}()
default:
cancel()
return "", errors.WithDetails(
ErrUnexpectedMessageType,
"type", content.Type,
"apiRequest", apiRequest,
)
}
}
wg.Wait()
cancel()
if len(messages[len(messages)-1].Content) == 0 {
return "", errors.WithDetails(
ErrToolCallsWithoutCalls,
"apiRequest", apiRequest,
)
}
continue
}
if response.StopReason != "end_turn" {
return "", errors.WithDetails(
ErrUnexpectedStop,
"reason", response.StopReason,
"apiRequest", apiRequest,
)
}
// Model sometimes returns no content when the last message to the agent
// was the tool result and that concluded the conversation.
if len(response.Content) == 0 {
return "", nil
}
if len(response.Content) != 1 {
return "", errors.WithDetails(
ErrUnexpectedNumberOfMessages,
"number", len(response.Content),
"apiRequest", apiRequest,
)
}
if response.Content[0].Type != typeText {
return "", errors.WithDetails(
ErrUnexpectedMessageType,
"type", response.Content[0].Type,
"apiRequest", apiRequest,
)
}
if response.Content[0].Text == nil {
return "", errors.WithDetails(
ErrUnexpectedMessageType,
"apiRequest", apiRequest,
)
}
return *response.Content[0].Text, nil
}
}
func (a *AnthropicTextProvider) estimatedTokens(messages []anthropicMessage) (int, int) {
// We estimate inputTokens from training messages (including system message) by
// dividing number of characters by 4.
inputTokens := 0
for _, message := range messages {
for _, content := range message.Content {
if content.Text != nil {
inputTokens += len(*content.Text) / 4 //nolint:mnd
}
inputTokens += len(content.Input) / 4 //nolint:mnd
if content.Content != nil {
inputTokens += len(*content.Content) / 4 //nolint:mnd
}
}
}
for _, system := range a.system {
inputTokens += len(system.Text) / 4 //nolint:mnd
}
for _, tool := range a.tools {
inputTokens += len(tool.Name) / 4 //nolint:mnd
inputTokens += len(tool.Description) / 4 //nolint:mnd
inputTokens += len(tool.InputJSONSchema) / 4 //nolint:mnd
}
// TODO: Can we provide a better estimate for output tokens?
return inputTokens, a.MaxResponseLength
}
func (a *AnthropicTextProvider) maxContextLength() int {
// Currently this is the same for all Anthropic models.
return 200_000 //nolint:mnd
}
func (a *AnthropicTextProvider) maxResponseTokens() int {
if strings.Contains(a.Model, "3-5") {
return 8192 //nolint:mnd
}
return 4096 //nolint:mnd
}
// InitTools implements [WithTools] interface.
func (a *AnthropicTextProvider) InitTools(ctx context.Context, tools map[string]TextTooler) errors.E {
if a.tools != nil {
return errors.WithStack(ErrAlreadyInitialized)
}
a.tools = []anthropicTool{}
for name, tool := range tools {
errE := tool.Init(ctx)
if errE != nil {
errors.Details(errE)["name"] = name
return errE
}
a.tools = append(a.tools, anthropicTool{
Name: name,
Description: tool.GetDescription(),
InputJSONSchema: tool.GetInputJSONSchema(),
CacheControl: nil,
tool: tool,
})
}
// We want to set a cache breakpoint as late as possible. And the order is tools, system, then messages.
if a.PromptCaching && len(a.messages) == 0 && len(a.system) == 0 {
a.tools[len(a.tools)-1].CacheControl = &anthropicCacheControl{
Type: "ephemeral",
}
}
return nil
}
func (a *AnthropicTextProvider) callToolWrapper(
ctx context.Context, apiRequest string, toolCall anthropicContent, result *anthropicContent, callRecorder *TextRecorderCall, toolMessage *TextRecorderMessage,
) {
if callRecorder != nil {
defer func() {
callRecorder.notify("", nil)
}()
}
defer func() {
if err := recover(); err != nil {
content := fmt.Sprintf("Error: %s", err)
result.Content = &content
result.IsError = true
toolMessage.setContent(content, true)
}
}()
defer func() {
toolMessage.setToolCalls(GetTextRecorder(ctx).Calls())
}()
logger := zerolog.Ctx(ctx).With().Str("tool", toolCall.ID).Logger()
ctx = logger.WithContext(ctx)
output, duration, errE := a.callTool(ctx, toolCall)
if errE != nil {
zerolog.Ctx(ctx).Warn().Err(errE).Str("name", toolCall.Name).Str("apiRequest", apiRequest).
Str("tool", toolCall.ID).RawJSON("input", toolCall.Input).Msg("tool error")
content := "Error: " + errE.Error()
result.Content = &content
result.IsError = true
toolMessage.setContent(content, true)
} else {
result.Content = &output
toolMessage.setContent(output, false)
}
toolMessage.setToolDuration(duration)
}
func (a *AnthropicTextProvider) callTool(ctx context.Context, toolCall anthropicContent) (string, Duration, errors.E) {
var tool TextTooler
for _, t := range a.tools {
if t.Name == toolCall.Name {
tool = t.tool
break
}
}
if tool == nil {
return "", 0, errors.Errorf("%w: %s", ErrToolNotFound, toolCall.Name)
}
start := time.Now()
output, errE := tool.Call(ctx, toolCall.Input)
duration := time.Since(start)
return output, Duration(duration), errE
}
func (a *AnthropicTextProvider) recordMessage(recorder *TextRecorderCall, message anthropicMessage) {
for _, content := range message.Content {
switch content.Type {
case roleToolResult:
panic(errors.New("recording tool result message should not happen"))
case typeText:
if content.Text != nil {
recorder.addMessage(message.Role, *content.Text, "", "", false)
}
case roleToolUse:
recorder.addMessage(roleToolUse, string(content.Input), content.ID, content.Name, false)
}
}
}