-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathmessage.go
More file actions
9568 lines (8741 loc) · 361 KB
/
message.go
File metadata and controls
9568 lines (8741 loc) · 361 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
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package anthropic
import (
"context"
"encoding/json"
"net/http"
"reflect"
"slices"
"time"
"github.com/anthropics/anthropic-sdk-go/internal/apijson"
"github.com/anthropics/anthropic-sdk-go/internal/paramutil"
"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
"github.com/anthropics/anthropic-sdk-go/option"
"github.com/anthropics/anthropic-sdk-go/packages/param"
"github.com/anthropics/anthropic-sdk-go/packages/respjson"
"github.com/anthropics/anthropic-sdk-go/packages/ssestream"
"github.com/anthropics/anthropic-sdk-go/shared/constant"
"github.com/tidwall/gjson"
)
// MessageService contains methods and other services that help with interacting
// with the anthropic API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewMessageService] method instead.
type MessageService struct {
Options []option.RequestOption
Batches MessageBatchService
}
// NewMessageService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewMessageService(opts ...option.RequestOption) (r MessageService) {
r = MessageService{}
r.Options = opts
r.Batches = NewMessageBatchService(opts...)
return
}
// Send a structured list of input messages with text and/or image content, and the
// model will generate the next message in the conversation.
//
// The Messages API can be used for either single queries or stateless multi-turn
// conversations.
//
// Learn more about the Messages API in our
// [user guide](https://docs.claude.com/en/docs/initial-setup)
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
func (r *MessageService) New(ctx context.Context, body MessageNewParams, opts ...option.RequestOption) (res *Message, err error) {
opts = slices.Concat(r.Options, opts)
// For non-streaming requests, calculate the appropriate timeout based on maxTokens
// and check against model-specific limits
timeout, timeoutErr := CalculateNonStreamingTimeout(int(body.MaxTokens), body.Model, opts)
if timeoutErr != nil {
return nil, timeoutErr
}
opts = append(opts, option.WithRequestTimeout(timeout))
path := "v1/messages"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// Send a structured list of input messages with text and/or image content, and the
// model will generate the next message in the conversation.
//
// The Messages API can be used for either single queries or stateless multi-turn
// conversations.
//
// Learn more about the Messages API in our
// [user guide](https://docs.claude.com/en/docs/initial-setup)
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
func (r *MessageService) NewStreaming(ctx context.Context, body MessageNewParams, opts ...option.RequestOption) (stream *ssestream.Stream[MessageStreamEventUnion]) {
var (
raw *http.Response
err error
)
opts = slices.Concat(r.Options, opts)
opts = append(opts, option.WithJSONSet("stream", true))
path := "v1/messages"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &raw, opts...)
return ssestream.NewStream[MessageStreamEventUnion](ssestream.NewDecoder(raw), err)
}
// Count the number of tokens in a Message.
//
// The Token Count API can be used to count the number of tokens in a Message,
// including tools, images, and documents, without creating it.
//
// Learn more about token counting in our
// [user guide](https://docs.claude.com/en/docs/build-with-claude/token-counting)
func (r *MessageService) CountTokens(ctx context.Context, body MessageCountTokensParams, opts ...option.RequestOption) (res *MessageTokensCount, err error) {
opts = slices.Concat(r.Options, opts)
path := "v1/messages/count_tokens"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// The properties Data, MediaType, Type are required.
type Base64ImageSourceParam struct {
Data string `json:"data" api:"required" format:"byte"`
// Any of "image/jpeg", "image/png", "image/gif", "image/webp".
MediaType Base64ImageSourceMediaType `json:"media_type,omitzero" api:"required"`
// This field can be elided, and will marshal its zero value as "base64".
Type constant.Base64 `json:"type" default:"base64"`
paramObj
}
func (r Base64ImageSourceParam) MarshalJSON() (data []byte, err error) {
type shadow Base64ImageSourceParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *Base64ImageSourceParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type Base64ImageSourceMediaType string
const (
Base64ImageSourceMediaTypeImageJPEG Base64ImageSourceMediaType = "image/jpeg"
Base64ImageSourceMediaTypeImagePNG Base64ImageSourceMediaType = "image/png"
Base64ImageSourceMediaTypeImageGIF Base64ImageSourceMediaType = "image/gif"
Base64ImageSourceMediaTypeImageWebP Base64ImageSourceMediaType = "image/webp"
)
type Base64PDFSource struct {
Data string `json:"data" api:"required" format:"byte"`
MediaType constant.ApplicationPDF `json:"media_type" default:"application/pdf"`
Type constant.Base64 `json:"type" default:"base64"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
MediaType respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Base64PDFSource) RawJSON() string { return r.JSON.raw }
func (r *Base64PDFSource) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ToParam converts this Base64PDFSource to a Base64PDFSourceParam.
//
// Warning: the fields of the param type will not be present. ToParam should only
// be used at the last possible moment before sending a request. Test for this with
// Base64PDFSourceParam.Overrides()
func (r Base64PDFSource) ToParam() Base64PDFSourceParam {
return param.Override[Base64PDFSourceParam](json.RawMessage(r.RawJSON()))
}
// The properties Data, MediaType, Type are required.
type Base64PDFSourceParam struct {
Data string `json:"data" api:"required" format:"byte"`
// This field can be elided, and will marshal its zero value as "application/pdf".
MediaType constant.ApplicationPDF `json:"media_type" default:"application/pdf"`
// This field can be elided, and will marshal its zero value as "base64".
Type constant.Base64 `json:"type" default:"base64"`
paramObj
}
func (r Base64PDFSourceParam) MarshalJSON() (data []byte, err error) {
type shadow Base64PDFSourceParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *Base64PDFSourceParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BashCodeExecutionOutputBlock struct {
FileID string `json:"file_id" api:"required"`
Type constant.BashCodeExecutionOutput `json:"type" default:"bash_code_execution_output"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
FileID respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r BashCodeExecutionOutputBlock) RawJSON() string { return r.JSON.raw }
func (r *BashCodeExecutionOutputBlock) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties FileID, Type are required.
type BashCodeExecutionOutputBlockParam struct {
FileID string `json:"file_id" api:"required"`
// This field can be elided, and will marshal its zero value as
// "bash_code_execution_output".
Type constant.BashCodeExecutionOutput `json:"type" default:"bash_code_execution_output"`
paramObj
}
func (r BashCodeExecutionOutputBlockParam) MarshalJSON() (data []byte, err error) {
type shadow BashCodeExecutionOutputBlockParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *BashCodeExecutionOutputBlockParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BashCodeExecutionResultBlock struct {
Content []BashCodeExecutionOutputBlock `json:"content" api:"required"`
ReturnCode int64 `json:"return_code" api:"required"`
Stderr string `json:"stderr" api:"required"`
Stdout string `json:"stdout" api:"required"`
Type constant.BashCodeExecutionResult `json:"type" default:"bash_code_execution_result"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Content respjson.Field
ReturnCode respjson.Field
Stderr respjson.Field
Stdout respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r BashCodeExecutionResultBlock) RawJSON() string { return r.JSON.raw }
func (r *BashCodeExecutionResultBlock) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties Content, ReturnCode, Stderr, Stdout, Type are required.
type BashCodeExecutionResultBlockParam struct {
Content []BashCodeExecutionOutputBlockParam `json:"content,omitzero" api:"required"`
ReturnCode int64 `json:"return_code" api:"required"`
Stderr string `json:"stderr" api:"required"`
Stdout string `json:"stdout" api:"required"`
// This field can be elided, and will marshal its zero value as
// "bash_code_execution_result".
Type constant.BashCodeExecutionResult `json:"type" default:"bash_code_execution_result"`
paramObj
}
func (r BashCodeExecutionResultBlockParam) MarshalJSON() (data []byte, err error) {
type shadow BashCodeExecutionResultBlockParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *BashCodeExecutionResultBlockParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BashCodeExecutionToolResultBlock struct {
Content BashCodeExecutionToolResultBlockContentUnion `json:"content" api:"required"`
ToolUseID string `json:"tool_use_id" api:"required"`
Type constant.BashCodeExecutionToolResult `json:"type" default:"bash_code_execution_tool_result"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Content respjson.Field
ToolUseID respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r BashCodeExecutionToolResultBlock) RawJSON() string { return r.JSON.raw }
func (r *BashCodeExecutionToolResultBlock) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// BashCodeExecutionToolResultBlockContentUnion contains all possible properties
// and values from [BashCodeExecutionToolResultError],
// [BashCodeExecutionResultBlock].
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type BashCodeExecutionToolResultBlockContentUnion struct {
// This field is from variant [BashCodeExecutionToolResultError].
ErrorCode BashCodeExecutionToolResultErrorCode `json:"error_code"`
Type string `json:"type"`
// This field is from variant [BashCodeExecutionResultBlock].
Content []BashCodeExecutionOutputBlock `json:"content"`
// This field is from variant [BashCodeExecutionResultBlock].
ReturnCode int64 `json:"return_code"`
// This field is from variant [BashCodeExecutionResultBlock].
Stderr string `json:"stderr"`
// This field is from variant [BashCodeExecutionResultBlock].
Stdout string `json:"stdout"`
JSON struct {
ErrorCode respjson.Field
Type respjson.Field
Content respjson.Field
ReturnCode respjson.Field
Stderr respjson.Field
Stdout respjson.Field
raw string
} `json:"-"`
}
func (u BashCodeExecutionToolResultBlockContentUnion) AsResponseBashCodeExecutionToolResultError() (v BashCodeExecutionToolResultError) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u BashCodeExecutionToolResultBlockContentUnion) AsResponseBashCodeExecutionResultBlock() (v BashCodeExecutionResultBlock) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u BashCodeExecutionToolResultBlockContentUnion) RawJSON() string { return u.JSON.raw }
func (r *BashCodeExecutionToolResultBlockContentUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties Content, ToolUseID, Type are required.
type BashCodeExecutionToolResultBlockParam struct {
Content BashCodeExecutionToolResultBlockParamContentUnion `json:"content,omitzero" api:"required"`
ToolUseID string `json:"tool_use_id" api:"required"`
// Create a cache control breakpoint at this content block.
CacheControl CacheControlEphemeralParam `json:"cache_control,omitzero"`
// This field can be elided, and will marshal its zero value as
// "bash_code_execution_tool_result".
Type constant.BashCodeExecutionToolResult `json:"type" default:"bash_code_execution_tool_result"`
paramObj
}
func (r BashCodeExecutionToolResultBlockParam) MarshalJSON() (data []byte, err error) {
type shadow BashCodeExecutionToolResultBlockParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *BashCodeExecutionToolResultBlockParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type BashCodeExecutionToolResultBlockParamContentUnion struct {
OfRequestBashCodeExecutionToolResultError *BashCodeExecutionToolResultErrorParam `json:",omitzero,inline"`
OfRequestBashCodeExecutionResultBlock *BashCodeExecutionResultBlockParam `json:",omitzero,inline"`
paramUnion
}
func (u BashCodeExecutionToolResultBlockParamContentUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfRequestBashCodeExecutionToolResultError, u.OfRequestBashCodeExecutionResultBlock)
}
func (u *BashCodeExecutionToolResultBlockParamContentUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *BashCodeExecutionToolResultBlockParamContentUnion) asAny() any {
if !param.IsOmitted(u.OfRequestBashCodeExecutionToolResultError) {
return u.OfRequestBashCodeExecutionToolResultError
} else if !param.IsOmitted(u.OfRequestBashCodeExecutionResultBlock) {
return u.OfRequestBashCodeExecutionResultBlock
}
return nil
}
// Returns a pointer to the underlying variant's property, if present.
func (u BashCodeExecutionToolResultBlockParamContentUnion) GetErrorCode() *string {
if vt := u.OfRequestBashCodeExecutionToolResultError; vt != nil {
return (*string)(&vt.ErrorCode)
}
return nil
}
// Returns a pointer to the underlying variant's property, if present.
func (u BashCodeExecutionToolResultBlockParamContentUnion) GetContent() []BashCodeExecutionOutputBlockParam {
if vt := u.OfRequestBashCodeExecutionResultBlock; vt != nil {
return vt.Content
}
return nil
}
// Returns a pointer to the underlying variant's property, if present.
func (u BashCodeExecutionToolResultBlockParamContentUnion) GetReturnCode() *int64 {
if vt := u.OfRequestBashCodeExecutionResultBlock; vt != nil {
return &vt.ReturnCode
}
return nil
}
// Returns a pointer to the underlying variant's property, if present.
func (u BashCodeExecutionToolResultBlockParamContentUnion) GetStderr() *string {
if vt := u.OfRequestBashCodeExecutionResultBlock; vt != nil {
return &vt.Stderr
}
return nil
}
// Returns a pointer to the underlying variant's property, if present.
func (u BashCodeExecutionToolResultBlockParamContentUnion) GetStdout() *string {
if vt := u.OfRequestBashCodeExecutionResultBlock; vt != nil {
return &vt.Stdout
}
return nil
}
// Returns a pointer to the underlying variant's property, if present.
func (u BashCodeExecutionToolResultBlockParamContentUnion) GetType() *string {
if vt := u.OfRequestBashCodeExecutionToolResultError; vt != nil {
return (*string)(&vt.Type)
} else if vt := u.OfRequestBashCodeExecutionResultBlock; vt != nil {
return (*string)(&vt.Type)
}
return nil
}
type BashCodeExecutionToolResultError struct {
// Any of "invalid_tool_input", "unavailable", "too_many_requests",
// "execution_time_exceeded", "output_file_too_large".
ErrorCode BashCodeExecutionToolResultErrorCode `json:"error_code" api:"required"`
Type constant.BashCodeExecutionToolResultError `json:"type" default:"bash_code_execution_tool_result_error"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ErrorCode respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r BashCodeExecutionToolResultError) RawJSON() string { return r.JSON.raw }
func (r *BashCodeExecutionToolResultError) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BashCodeExecutionToolResultErrorCode string
const (
BashCodeExecutionToolResultErrorCodeInvalidToolInput BashCodeExecutionToolResultErrorCode = "invalid_tool_input"
BashCodeExecutionToolResultErrorCodeUnavailable BashCodeExecutionToolResultErrorCode = "unavailable"
BashCodeExecutionToolResultErrorCodeTooManyRequests BashCodeExecutionToolResultErrorCode = "too_many_requests"
BashCodeExecutionToolResultErrorCodeExecutionTimeExceeded BashCodeExecutionToolResultErrorCode = "execution_time_exceeded"
BashCodeExecutionToolResultErrorCodeOutputFileTooLarge BashCodeExecutionToolResultErrorCode = "output_file_too_large"
)
// The properties ErrorCode, Type are required.
type BashCodeExecutionToolResultErrorParam struct {
// Any of "invalid_tool_input", "unavailable", "too_many_requests",
// "execution_time_exceeded", "output_file_too_large".
ErrorCode BashCodeExecutionToolResultErrorCode `json:"error_code,omitzero" api:"required"`
// This field can be elided, and will marshal its zero value as
// "bash_code_execution_tool_result_error".
Type constant.BashCodeExecutionToolResultError `json:"type" default:"bash_code_execution_tool_result_error"`
paramObj
}
func (r BashCodeExecutionToolResultErrorParam) MarshalJSON() (data []byte, err error) {
type shadow BashCodeExecutionToolResultErrorParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *BashCodeExecutionToolResultErrorParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
func NewCacheControlEphemeralParam() CacheControlEphemeralParam {
return CacheControlEphemeralParam{
Type: "ephemeral",
}
}
// This struct has a constant value, construct it with
// [NewCacheControlEphemeralParam].
type CacheControlEphemeralParam struct {
// The time-to-live for the cache control breakpoint.
//
// This may be one the following values:
//
// - `5m`: 5 minutes
// - `1h`: 1 hour
//
// Defaults to `5m`.
//
// Any of "5m", "1h".
TTL CacheControlEphemeralTTL `json:"ttl,omitzero"`
Type constant.Ephemeral `json:"type" default:"ephemeral"`
paramObj
}
func (r CacheControlEphemeralParam) MarshalJSON() (data []byte, err error) {
type shadow CacheControlEphemeralParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CacheControlEphemeralParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The time-to-live for the cache control breakpoint.
//
// This may be one the following values:
//
// - `5m`: 5 minutes
// - `1h`: 1 hour
//
// Defaults to `5m`.
type CacheControlEphemeralTTL string
const (
CacheControlEphemeralTTLTTL5m CacheControlEphemeralTTL = "5m"
CacheControlEphemeralTTLTTL1h CacheControlEphemeralTTL = "1h"
)
type CacheCreation struct {
// The number of input tokens used to create the 1 hour cache entry.
Ephemeral1hInputTokens int64 `json:"ephemeral_1h_input_tokens" api:"required"`
// The number of input tokens used to create the 5 minute cache entry.
Ephemeral5mInputTokens int64 `json:"ephemeral_5m_input_tokens" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Ephemeral1hInputTokens respjson.Field
Ephemeral5mInputTokens respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CacheCreation) RawJSON() string { return r.JSON.raw }
func (r *CacheCreation) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationCharLocation struct {
CitedText string `json:"cited_text" api:"required"`
DocumentIndex int64 `json:"document_index" api:"required"`
DocumentTitle string `json:"document_title" api:"required"`
EndCharIndex int64 `json:"end_char_index" api:"required"`
FileID string `json:"file_id" api:"required"`
StartCharIndex int64 `json:"start_char_index" api:"required"`
Type constant.CharLocation `json:"type" default:"char_location"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CitedText respjson.Field
DocumentIndex respjson.Field
DocumentTitle respjson.Field
EndCharIndex respjson.Field
FileID respjson.Field
StartCharIndex respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CitationCharLocation) RawJSON() string { return r.JSON.raw }
func (r *CitationCharLocation) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties CitedText, DocumentIndex, DocumentTitle, EndCharIndex,
// StartCharIndex, Type are required.
type CitationCharLocationParam struct {
DocumentTitle param.Opt[string] `json:"document_title,omitzero" api:"required"`
CitedText string `json:"cited_text" api:"required"`
DocumentIndex int64 `json:"document_index" api:"required"`
EndCharIndex int64 `json:"end_char_index" api:"required"`
StartCharIndex int64 `json:"start_char_index" api:"required"`
// This field can be elided, and will marshal its zero value as "char_location".
Type constant.CharLocation `json:"type" default:"char_location"`
paramObj
}
func (r CitationCharLocationParam) MarshalJSON() (data []byte, err error) {
type shadow CitationCharLocationParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CitationCharLocationParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationContentBlockLocation struct {
CitedText string `json:"cited_text" api:"required"`
DocumentIndex int64 `json:"document_index" api:"required"`
DocumentTitle string `json:"document_title" api:"required"`
EndBlockIndex int64 `json:"end_block_index" api:"required"`
FileID string `json:"file_id" api:"required"`
StartBlockIndex int64 `json:"start_block_index" api:"required"`
Type constant.ContentBlockLocation `json:"type" default:"content_block_location"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CitedText respjson.Field
DocumentIndex respjson.Field
DocumentTitle respjson.Field
EndBlockIndex respjson.Field
FileID respjson.Field
StartBlockIndex respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CitationContentBlockLocation) RawJSON() string { return r.JSON.raw }
func (r *CitationContentBlockLocation) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties CitedText, DocumentIndex, DocumentTitle, EndBlockIndex,
// StartBlockIndex, Type are required.
type CitationContentBlockLocationParam struct {
DocumentTitle param.Opt[string] `json:"document_title,omitzero" api:"required"`
CitedText string `json:"cited_text" api:"required"`
DocumentIndex int64 `json:"document_index" api:"required"`
EndBlockIndex int64 `json:"end_block_index" api:"required"`
StartBlockIndex int64 `json:"start_block_index" api:"required"`
// This field can be elided, and will marshal its zero value as
// "content_block_location".
Type constant.ContentBlockLocation `json:"type" default:"content_block_location"`
paramObj
}
func (r CitationContentBlockLocationParam) MarshalJSON() (data []byte, err error) {
type shadow CitationContentBlockLocationParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CitationContentBlockLocationParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationPageLocation struct {
CitedText string `json:"cited_text" api:"required"`
DocumentIndex int64 `json:"document_index" api:"required"`
DocumentTitle string `json:"document_title" api:"required"`
EndPageNumber int64 `json:"end_page_number" api:"required"`
FileID string `json:"file_id" api:"required"`
StartPageNumber int64 `json:"start_page_number" api:"required"`
Type constant.PageLocation `json:"type" default:"page_location"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CitedText respjson.Field
DocumentIndex respjson.Field
DocumentTitle respjson.Field
EndPageNumber respjson.Field
FileID respjson.Field
StartPageNumber respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CitationPageLocation) RawJSON() string { return r.JSON.raw }
func (r *CitationPageLocation) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties CitedText, DocumentIndex, DocumentTitle, EndPageNumber,
// StartPageNumber, Type are required.
type CitationPageLocationParam struct {
DocumentTitle param.Opt[string] `json:"document_title,omitzero" api:"required"`
CitedText string `json:"cited_text" api:"required"`
DocumentIndex int64 `json:"document_index" api:"required"`
EndPageNumber int64 `json:"end_page_number" api:"required"`
StartPageNumber int64 `json:"start_page_number" api:"required"`
// This field can be elided, and will marshal its zero value as "page_location".
Type constant.PageLocation `json:"type" default:"page_location"`
paramObj
}
func (r CitationPageLocationParam) MarshalJSON() (data []byte, err error) {
type shadow CitationPageLocationParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CitationPageLocationParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties CitedText, EndBlockIndex, SearchResultIndex, Source,
// StartBlockIndex, Title, Type are required.
type CitationSearchResultLocationParam struct {
Title param.Opt[string] `json:"title,omitzero" api:"required"`
CitedText string `json:"cited_text" api:"required"`
EndBlockIndex int64 `json:"end_block_index" api:"required"`
SearchResultIndex int64 `json:"search_result_index" api:"required"`
Source string `json:"source" api:"required"`
StartBlockIndex int64 `json:"start_block_index" api:"required"`
// This field can be elided, and will marshal its zero value as
// "search_result_location".
Type constant.SearchResultLocation `json:"type" default:"search_result_location"`
paramObj
}
func (r CitationSearchResultLocationParam) MarshalJSON() (data []byte, err error) {
type shadow CitationSearchResultLocationParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CitationSearchResultLocationParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties CitedText, EncryptedIndex, Title, Type, URL are required.
type CitationWebSearchResultLocationParam struct {
Title param.Opt[string] `json:"title,omitzero" api:"required"`
CitedText string `json:"cited_text" api:"required"`
EncryptedIndex string `json:"encrypted_index" api:"required"`
URL string `json:"url" api:"required"`
// This field can be elided, and will marshal its zero value as
// "web_search_result_location".
Type constant.WebSearchResultLocation `json:"type" default:"web_search_result_location"`
paramObj
}
func (r CitationWebSearchResultLocationParam) MarshalJSON() (data []byte, err error) {
type shadow CitationWebSearchResultLocationParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CitationWebSearchResultLocationParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationsConfig struct {
Enabled bool `json:"enabled" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Enabled respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CitationsConfig) RawJSON() string { return r.JSON.raw }
func (r *CitationsConfig) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationsConfigParam struct {
Enabled param.Opt[bool] `json:"enabled,omitzero"`
paramObj
}
func (r CitationsConfigParam) MarshalJSON() (data []byte, err error) {
type shadow CitationsConfigParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CitationsConfigParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationsDelta struct {
Citation CitationsDeltaCitationUnion `json:"citation" api:"required"`
Type constant.CitationsDelta `json:"type" default:"citations_delta"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Citation respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CitationsDelta) RawJSON() string { return r.JSON.raw }
func (r *CitationsDelta) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// CitationsDeltaCitationUnion contains all possible properties and values from
// [CitationCharLocation], [CitationPageLocation], [CitationContentBlockLocation],
// [CitationsWebSearchResultLocation], [CitationsSearchResultLocation].
//
// Use the [CitationsDeltaCitationUnion.AsAny] method to switch on the variant.
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
type CitationsDeltaCitationUnion struct {
CitedText string `json:"cited_text"`
DocumentIndex int64 `json:"document_index"`
DocumentTitle string `json:"document_title"`
// This field is from variant [CitationCharLocation].
EndCharIndex int64 `json:"end_char_index"`
FileID string `json:"file_id"`
// This field is from variant [CitationCharLocation].
StartCharIndex int64 `json:"start_char_index"`
// Any of "char_location", "page_location", "content_block_location",
// "web_search_result_location", "search_result_location".
Type string `json:"type"`
// This field is from variant [CitationPageLocation].
EndPageNumber int64 `json:"end_page_number"`
// This field is from variant [CitationPageLocation].
StartPageNumber int64 `json:"start_page_number"`
EndBlockIndex int64 `json:"end_block_index"`
StartBlockIndex int64 `json:"start_block_index"`
// This field is from variant [CitationsWebSearchResultLocation].
EncryptedIndex string `json:"encrypted_index"`
Title string `json:"title"`
// This field is from variant [CitationsWebSearchResultLocation].
URL string `json:"url"`
// This field is from variant [CitationsSearchResultLocation].
SearchResultIndex int64 `json:"search_result_index"`
// This field is from variant [CitationsSearchResultLocation].
Source string `json:"source"`
JSON struct {
CitedText respjson.Field
DocumentIndex respjson.Field
DocumentTitle respjson.Field
EndCharIndex respjson.Field
FileID respjson.Field
StartCharIndex respjson.Field
Type respjson.Field
EndPageNumber respjson.Field
StartPageNumber respjson.Field
EndBlockIndex respjson.Field
StartBlockIndex respjson.Field
EncryptedIndex respjson.Field
Title respjson.Field
URL respjson.Field
SearchResultIndex respjson.Field
Source respjson.Field
raw string
} `json:"-"`
}
// anyCitationsDeltaCitation is implemented by each variant of
// [CitationsDeltaCitationUnion] to add type safety for the return type of
// [CitationsDeltaCitationUnion.AsAny]
type anyCitationsDeltaCitation interface {
implCitationsDeltaCitationUnion()
}
func (CitationCharLocation) implCitationsDeltaCitationUnion() {}
func (CitationPageLocation) implCitationsDeltaCitationUnion() {}
func (CitationContentBlockLocation) implCitationsDeltaCitationUnion() {}
func (CitationsWebSearchResultLocation) implCitationsDeltaCitationUnion() {}
func (CitationsSearchResultLocation) implCitationsDeltaCitationUnion() {}
// Use the following switch statement to find the correct variant
//
// switch variant := CitationsDeltaCitationUnion.AsAny().(type) {
// case anthropic.CitationCharLocation:
// case anthropic.CitationPageLocation:
// case anthropic.CitationContentBlockLocation:
// case anthropic.CitationsWebSearchResultLocation:
// case anthropic.CitationsSearchResultLocation:
// default:
// fmt.Errorf("no variant present")
// }
func (u CitationsDeltaCitationUnion) AsAny() anyCitationsDeltaCitation {
switch u.Type {
case "char_location":
return u.AsCharLocation()
case "page_location":
return u.AsPageLocation()
case "content_block_location":
return u.AsContentBlockLocation()
case "web_search_result_location":
return u.AsWebSearchResultLocation()
case "search_result_location":
return u.AsSearchResultLocation()
}
return nil
}
func (u CitationsDeltaCitationUnion) AsCharLocation() (v CitationCharLocation) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u CitationsDeltaCitationUnion) AsPageLocation() (v CitationPageLocation) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u CitationsDeltaCitationUnion) AsContentBlockLocation() (v CitationContentBlockLocation) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u CitationsDeltaCitationUnion) AsWebSearchResultLocation() (v CitationsWebSearchResultLocation) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u CitationsDeltaCitationUnion) AsSearchResultLocation() (v CitationsSearchResultLocation) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u CitationsDeltaCitationUnion) RawJSON() string { return u.JSON.raw }
func (r *CitationsDeltaCitationUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationsSearchResultLocation struct {
CitedText string `json:"cited_text" api:"required"`
EndBlockIndex int64 `json:"end_block_index" api:"required"`
SearchResultIndex int64 `json:"search_result_index" api:"required"`
Source string `json:"source" api:"required"`
StartBlockIndex int64 `json:"start_block_index" api:"required"`
Title string `json:"title" api:"required"`
Type constant.SearchResultLocation `json:"type" default:"search_result_location"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CitedText respjson.Field
EndBlockIndex respjson.Field
SearchResultIndex respjson.Field
Source respjson.Field
StartBlockIndex respjson.Field
Title respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CitationsSearchResultLocation) RawJSON() string { return r.JSON.raw }
func (r *CitationsSearchResultLocation) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CitationsWebSearchResultLocation struct {
CitedText string `json:"cited_text" api:"required"`
EncryptedIndex string `json:"encrypted_index" api:"required"`
Title string `json:"title" api:"required"`
Type constant.WebSearchResultLocation `json:"type" default:"web_search_result_location"`
URL string `json:"url" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CitedText respjson.Field
EncryptedIndex respjson.Field
Title respjson.Field
Type respjson.Field
URL respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CitationsWebSearchResultLocation) RawJSON() string { return r.JSON.raw }
func (r *CitationsWebSearchResultLocation) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CodeExecutionOutputBlock struct {
FileID string `json:"file_id" api:"required"`
Type constant.CodeExecutionOutput `json:"type" default:"code_execution_output"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
FileID respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CodeExecutionOutputBlock) RawJSON() string { return r.JSON.raw }
func (r *CodeExecutionOutputBlock) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The properties FileID, Type are required.
type CodeExecutionOutputBlockParam struct {
FileID string `json:"file_id" api:"required"`
// This field can be elided, and will marshal its zero value as
// "code_execution_output".
Type constant.CodeExecutionOutput `json:"type" default:"code_execution_output"`
paramObj
}
func (r CodeExecutionOutputBlockParam) MarshalJSON() (data []byte, err error) {
type shadow CodeExecutionOutputBlockParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CodeExecutionOutputBlockParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CodeExecutionResultBlock struct {
Content []CodeExecutionOutputBlock `json:"content" api:"required"`
ReturnCode int64 `json:"return_code" api:"required"`
Stderr string `json:"stderr" api:"required"`
Stdout string `json:"stdout" api:"required"`
Type constant.CodeExecutionResult `json:"type" default:"code_execution_result"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Content respjson.Field
ReturnCode respjson.Field
Stderr respjson.Field
Stdout respjson.Field
Type respjson.Field