-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmessage.go
3000 lines (2591 loc) · 91.8 KB
/
message.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
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"
"fmt"
"net/http"
"reflect"
"github.com/anthropics/anthropic-sdk-go/internal/apijson"
"github.com/anthropics/anthropic-sdk-go/internal/param"
"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
"github.com/anthropics/anthropic-sdk-go/option"
"github.com/anthropics/anthropic-sdk-go/packages/ssestream"
"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.
//
// 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 = append(r.Options[:], opts...)
path := "v1/messages"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, 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.
//
// 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[MessageStreamEvent]) {
var (
raw *http.Response
err error
)
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithJSONSet("stream", true)}, opts...)
path := "v1/messages"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &raw, opts...)
return ssestream.NewStream[MessageStreamEvent](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.
func (r *MessageService) CountTokens(ctx context.Context, body MessageCountTokensParams, opts ...option.RequestOption) (res *MessageTokensCount, err error) {
opts = append(r.Options[:], opts...)
path := "v1/messages/count_tokens"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
type Base64PDFSourceParam struct {
Data param.Field[string] `json:"data,required" format:"byte"`
MediaType param.Field[Base64PDFSourceMediaType] `json:"media_type,required"`
Type param.Field[Base64PDFSourceType] `json:"type,required"`
}
func (r Base64PDFSourceParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r Base64PDFSourceParam) implementsDocumentBlockParamSourceUnion() {}
type Base64PDFSourceMediaType string
const (
Base64PDFSourceMediaTypeApplicationPDF Base64PDFSourceMediaType = "application/pdf"
)
func (r Base64PDFSourceMediaType) IsKnown() bool {
switch r {
case Base64PDFSourceMediaTypeApplicationPDF:
return true
}
return false
}
type Base64PDFSourceType string
const (
Base64PDFSourceTypeBase64 Base64PDFSourceType = "base64"
)
func (r Base64PDFSourceType) IsKnown() bool {
switch r {
case Base64PDFSourceTypeBase64:
return true
}
return false
}
type CacheControlEphemeralParam struct {
Type param.Field[CacheControlEphemeralType] `json:"type,required"`
}
func (r CacheControlEphemeralParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CacheControlEphemeralType string
const (
CacheControlEphemeralTypeEphemeral CacheControlEphemeralType = "ephemeral"
)
func (r CacheControlEphemeralType) IsKnown() bool {
switch r {
case CacheControlEphemeralTypeEphemeral:
return true
}
return false
}
type CitationCharLocation struct {
CitedText string `json:"cited_text,required"`
DocumentIndex int64 `json:"document_index,required"`
DocumentTitle string `json:"document_title,required,nullable"`
EndCharIndex int64 `json:"end_char_index,required"`
StartCharIndex int64 `json:"start_char_index,required"`
Type CitationCharLocationType `json:"type,required"`
JSON citationCharLocationJSON `json:"-"`
}
// citationCharLocationJSON contains the JSON metadata for the struct
// [CitationCharLocation]
type citationCharLocationJSON struct {
CitedText apijson.Field
DocumentIndex apijson.Field
DocumentTitle apijson.Field
EndCharIndex apijson.Field
StartCharIndex apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CitationCharLocation) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r citationCharLocationJSON) RawJSON() string {
return r.raw
}
func (r CitationCharLocation) implementsCitationsDeltaCitation() {}
func (r CitationCharLocation) implementsTextCitation() {}
type CitationCharLocationType string
const (
CitationCharLocationTypeCharLocation CitationCharLocationType = "char_location"
)
func (r CitationCharLocationType) IsKnown() bool {
switch r {
case CitationCharLocationTypeCharLocation:
return true
}
return false
}
type CitationCharLocationParam struct {
CitedText param.Field[string] `json:"cited_text,required"`
DocumentIndex param.Field[int64] `json:"document_index,required"`
DocumentTitle param.Field[string] `json:"document_title,required"`
EndCharIndex param.Field[int64] `json:"end_char_index,required"`
StartCharIndex param.Field[int64] `json:"start_char_index,required"`
Type param.Field[CitationCharLocationParamType] `json:"type,required"`
}
func (r CitationCharLocationParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CitationCharLocationParam) implementsTextCitationParamUnion() {}
type CitationCharLocationParamType string
const (
CitationCharLocationParamTypeCharLocation CitationCharLocationParamType = "char_location"
)
func (r CitationCharLocationParamType) IsKnown() bool {
switch r {
case CitationCharLocationParamTypeCharLocation:
return true
}
return false
}
type CitationContentBlockLocation struct {
CitedText string `json:"cited_text,required"`
DocumentIndex int64 `json:"document_index,required"`
DocumentTitle string `json:"document_title,required,nullable"`
EndBlockIndex int64 `json:"end_block_index,required"`
StartBlockIndex int64 `json:"start_block_index,required"`
Type CitationContentBlockLocationType `json:"type,required"`
JSON citationContentBlockLocationJSON `json:"-"`
}
// citationContentBlockLocationJSON contains the JSON metadata for the struct
// [CitationContentBlockLocation]
type citationContentBlockLocationJSON struct {
CitedText apijson.Field
DocumentIndex apijson.Field
DocumentTitle apijson.Field
EndBlockIndex apijson.Field
StartBlockIndex apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CitationContentBlockLocation) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r citationContentBlockLocationJSON) RawJSON() string {
return r.raw
}
func (r CitationContentBlockLocation) implementsCitationsDeltaCitation() {}
func (r CitationContentBlockLocation) implementsTextCitation() {}
type CitationContentBlockLocationType string
const (
CitationContentBlockLocationTypeContentBlockLocation CitationContentBlockLocationType = "content_block_location"
)
func (r CitationContentBlockLocationType) IsKnown() bool {
switch r {
case CitationContentBlockLocationTypeContentBlockLocation:
return true
}
return false
}
type CitationContentBlockLocationParam struct {
CitedText param.Field[string] `json:"cited_text,required"`
DocumentIndex param.Field[int64] `json:"document_index,required"`
DocumentTitle param.Field[string] `json:"document_title,required"`
EndBlockIndex param.Field[int64] `json:"end_block_index,required"`
StartBlockIndex param.Field[int64] `json:"start_block_index,required"`
Type param.Field[CitationContentBlockLocationParamType] `json:"type,required"`
}
func (r CitationContentBlockLocationParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CitationContentBlockLocationParam) implementsTextCitationParamUnion() {}
type CitationContentBlockLocationParamType string
const (
CitationContentBlockLocationParamTypeContentBlockLocation CitationContentBlockLocationParamType = "content_block_location"
)
func (r CitationContentBlockLocationParamType) IsKnown() bool {
switch r {
case CitationContentBlockLocationParamTypeContentBlockLocation:
return true
}
return false
}
type CitationPageLocation struct {
CitedText string `json:"cited_text,required"`
DocumentIndex int64 `json:"document_index,required"`
DocumentTitle string `json:"document_title,required,nullable"`
EndPageNumber int64 `json:"end_page_number,required"`
StartPageNumber int64 `json:"start_page_number,required"`
Type CitationPageLocationType `json:"type,required"`
JSON citationPageLocationJSON `json:"-"`
}
// citationPageLocationJSON contains the JSON metadata for the struct
// [CitationPageLocation]
type citationPageLocationJSON struct {
CitedText apijson.Field
DocumentIndex apijson.Field
DocumentTitle apijson.Field
EndPageNumber apijson.Field
StartPageNumber apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CitationPageLocation) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r citationPageLocationJSON) RawJSON() string {
return r.raw
}
func (r CitationPageLocation) implementsCitationsDeltaCitation() {}
func (r CitationPageLocation) implementsTextCitation() {}
type CitationPageLocationType string
const (
CitationPageLocationTypePageLocation CitationPageLocationType = "page_location"
)
func (r CitationPageLocationType) IsKnown() bool {
switch r {
case CitationPageLocationTypePageLocation:
return true
}
return false
}
type CitationPageLocationParam struct {
CitedText param.Field[string] `json:"cited_text,required"`
DocumentIndex param.Field[int64] `json:"document_index,required"`
DocumentTitle param.Field[string] `json:"document_title,required"`
EndPageNumber param.Field[int64] `json:"end_page_number,required"`
StartPageNumber param.Field[int64] `json:"start_page_number,required"`
Type param.Field[CitationPageLocationParamType] `json:"type,required"`
}
func (r CitationPageLocationParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CitationPageLocationParam) implementsTextCitationParamUnion() {}
type CitationPageLocationParamType string
const (
CitationPageLocationParamTypePageLocation CitationPageLocationParamType = "page_location"
)
func (r CitationPageLocationParamType) IsKnown() bool {
switch r {
case CitationPageLocationParamTypePageLocation:
return true
}
return false
}
type CitationsConfigParam struct {
Enabled param.Field[bool] `json:"enabled"`
}
func (r CitationsConfigParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CitationsDelta struct {
Citation CitationsDeltaCitation `json:"citation,required"`
Type CitationsDeltaType `json:"type,required"`
JSON citationsDeltaJSON `json:"-"`
}
// citationsDeltaJSON contains the JSON metadata for the struct [CitationsDelta]
type citationsDeltaJSON struct {
Citation apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CitationsDelta) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r citationsDeltaJSON) RawJSON() string {
return r.raw
}
func (r CitationsDelta) implementsContentBlockDeltaEventDelta() {}
type CitationsDeltaCitation struct {
CitedText string `json:"cited_text,required"`
DocumentIndex int64 `json:"document_index,required"`
DocumentTitle string `json:"document_title,required,nullable"`
Type CitationsDeltaCitationType `json:"type,required"`
EndBlockIndex int64 `json:"end_block_index"`
EndCharIndex int64 `json:"end_char_index"`
EndPageNumber int64 `json:"end_page_number"`
StartBlockIndex int64 `json:"start_block_index"`
StartCharIndex int64 `json:"start_char_index"`
StartPageNumber int64 `json:"start_page_number"`
JSON citationsDeltaCitationJSON `json:"-"`
union CitationsDeltaCitationUnion
}
// citationsDeltaCitationJSON contains the JSON metadata for the struct
// [CitationsDeltaCitation]
type citationsDeltaCitationJSON struct {
CitedText apijson.Field
DocumentIndex apijson.Field
DocumentTitle apijson.Field
Type apijson.Field
EndBlockIndex apijson.Field
EndCharIndex apijson.Field
EndPageNumber apijson.Field
StartBlockIndex apijson.Field
StartCharIndex apijson.Field
StartPageNumber apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r citationsDeltaCitationJSON) RawJSON() string {
return r.raw
}
func (r *CitationsDeltaCitation) UnmarshalJSON(data []byte) (err error) {
*r = CitationsDeltaCitation{}
err = apijson.UnmarshalRoot(data, &r.union)
if err != nil {
return err
}
return apijson.Port(r.union, &r)
}
// AsUnion returns a [CitationsDeltaCitationUnion] interface which you can cast to
// the specific types for more type safety.
//
// Possible runtime types of the union are [CitationCharLocation],
// [CitationPageLocation], [CitationContentBlockLocation].
func (r CitationsDeltaCitation) AsUnion() CitationsDeltaCitationUnion {
return r.union
}
// Union satisfied by [CitationCharLocation], [CitationPageLocation] or
// [CitationContentBlockLocation].
type CitationsDeltaCitationUnion interface {
implementsCitationsDeltaCitation()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*CitationsDeltaCitationUnion)(nil)).Elem(),
"type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(CitationCharLocation{}),
DiscriminatorValue: "char_location",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(CitationPageLocation{}),
DiscriminatorValue: "page_location",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(CitationContentBlockLocation{}),
DiscriminatorValue: "content_block_location",
},
)
}
type CitationsDeltaCitationType string
const (
CitationsDeltaCitationTypeCharLocation CitationsDeltaCitationType = "char_location"
CitationsDeltaCitationTypePageLocation CitationsDeltaCitationType = "page_location"
CitationsDeltaCitationTypeContentBlockLocation CitationsDeltaCitationType = "content_block_location"
)
func (r CitationsDeltaCitationType) IsKnown() bool {
switch r {
case CitationsDeltaCitationTypeCharLocation, CitationsDeltaCitationTypePageLocation, CitationsDeltaCitationTypeContentBlockLocation:
return true
}
return false
}
type CitationsDeltaType string
const (
CitationsDeltaTypeCitationsDelta CitationsDeltaType = "citations_delta"
)
func (r CitationsDeltaType) IsKnown() bool {
switch r {
case CitationsDeltaTypeCitationsDelta:
return true
}
return false
}
type ContentBlock struct {
Type ContentBlockType `json:"type,required"`
ID string `json:"id"`
// This field can have the runtime type of [[]TextCitation].
Citations interface{} `json:"citations"`
// This field can have the runtime type of [interface{}].
Input json.RawMessage `json:"input,required"`
Name string `json:"name"`
Text string `json:"text"`
JSON contentBlockJSON `json:"-"`
union ContentBlockUnion
}
// contentBlockJSON contains the JSON metadata for the struct [ContentBlock]
type contentBlockJSON struct {
Type apijson.Field
ID apijson.Field
Citations apijson.Field
Input apijson.Field
Name apijson.Field
Text apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r contentBlockJSON) RawJSON() string {
return r.raw
}
func (r *ContentBlock) UnmarshalJSON(data []byte) (err error) {
*r = ContentBlock{}
err = apijson.UnmarshalRoot(data, &r.union)
if err != nil {
return err
}
return apijson.Port(r.union, &r)
}
// AsUnion returns a [ContentBlockUnion] interface which you can cast to the
// specific types for more type safety.
//
// Possible runtime types of the union are [TextBlock], [ToolUseBlock].
func (r ContentBlock) AsUnion() ContentBlockUnion {
return r.union
}
// Union satisfied by [TextBlock] or [ToolUseBlock].
type ContentBlockUnion interface {
implementsContentBlock()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*ContentBlockUnion)(nil)).Elem(),
"type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(TextBlock{}),
DiscriminatorValue: "text",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(ToolUseBlock{}),
DiscriminatorValue: "tool_use",
},
)
}
type ContentBlockType string
const (
ContentBlockTypeText ContentBlockType = "text"
ContentBlockTypeToolUse ContentBlockType = "tool_use"
)
func (r ContentBlockType) IsKnown() bool {
switch r {
case ContentBlockTypeText, ContentBlockTypeToolUse:
return true
}
return false
}
type ContentBlockParam struct {
Type param.Field[ContentBlockParamType] `json:"type,required"`
ID param.Field[string] `json:"id"`
CacheControl param.Field[CacheControlEphemeralParam] `json:"cache_control"`
Citations param.Field[interface{}] `json:"citations"`
Content param.Field[interface{}] `json:"content"`
Context param.Field[string] `json:"context"`
Input param.Field[interface{}] `json:"input"`
IsError param.Field[bool] `json:"is_error"`
Name param.Field[string] `json:"name"`
Source param.Field[interface{}] `json:"source"`
Text param.Field[string] `json:"text"`
Title param.Field[string] `json:"title"`
ToolUseID param.Field[string] `json:"tool_use_id"`
}
func (r ContentBlockParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r ContentBlockParam) implementsContentBlockParamUnion() {}
// Satisfied by [TextBlockParam], [ImageBlockParam], [ToolUseBlockParam],
// [ToolResultBlockParam], [DocumentBlockParam], [ContentBlockParam].
type ContentBlockParamUnion interface {
implementsContentBlockParamUnion()
}
type ContentBlockParamType string
const (
ContentBlockParamTypeText ContentBlockParamType = "text"
ContentBlockParamTypeImage ContentBlockParamType = "image"
ContentBlockParamTypeToolUse ContentBlockParamType = "tool_use"
ContentBlockParamTypeToolResult ContentBlockParamType = "tool_result"
ContentBlockParamTypeDocument ContentBlockParamType = "document"
)
func (r ContentBlockParamType) IsKnown() bool {
switch r {
case ContentBlockParamTypeText, ContentBlockParamTypeImage, ContentBlockParamTypeToolUse, ContentBlockParamTypeToolResult, ContentBlockParamTypeDocument:
return true
}
return false
}
type ContentBlockSourceParam struct {
Content param.Field[ContentBlockSourceContentUnionParam] `json:"content,required"`
Type param.Field[ContentBlockSourceType] `json:"type,required"`
}
func (r ContentBlockSourceParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r ContentBlockSourceParam) implementsDocumentBlockParamSourceUnion() {}
// Satisfied by [shared.UnionString],
// [ContentBlockSourceContentContentBlockSourceContentParam].
type ContentBlockSourceContentUnionParam interface {
ImplementsContentBlockSourceContentUnionParam()
}
type ContentBlockSourceContentContentBlockSourceContentParam []ContentBlockSourceContentUnionParam
func (r ContentBlockSourceContentContentBlockSourceContentParam) ImplementsContentBlockSourceContentUnionParam() {
}
type ContentBlockSourceType string
const (
ContentBlockSourceTypeContent ContentBlockSourceType = "content"
)
func (r ContentBlockSourceType) IsKnown() bool {
switch r {
case ContentBlockSourceTypeContent:
return true
}
return false
}
type DocumentBlockParam struct {
Source param.Field[DocumentBlockParamSourceUnion] `json:"source,required"`
Type param.Field[DocumentBlockParamType] `json:"type,required"`
CacheControl param.Field[CacheControlEphemeralParam] `json:"cache_control"`
Citations param.Field[CitationsConfigParam] `json:"citations"`
Context param.Field[string] `json:"context"`
Title param.Field[string] `json:"title"`
}
func (r DocumentBlockParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r DocumentBlockParam) implementsContentBlockParamUnion() {}
type DocumentBlockParamSource struct {
Type param.Field[DocumentBlockParamSourceType] `json:"type,required"`
Content param.Field[interface{}] `json:"content"`
Data param.Field[string] `json:"data" format:"byte"`
MediaType param.Field[DocumentBlockParamSourceMediaType] `json:"media_type"`
}
func (r DocumentBlockParamSource) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r DocumentBlockParamSource) implementsDocumentBlockParamSourceUnion() {}
// Satisfied by [Base64PDFSourceParam], [PlainTextSourceParam],
// [ContentBlockSourceParam], [DocumentBlockParamSource].
type DocumentBlockParamSourceUnion interface {
implementsDocumentBlockParamSourceUnion()
}
type DocumentBlockParamSourceType string
const (
DocumentBlockParamSourceTypeBase64 DocumentBlockParamSourceType = "base64"
DocumentBlockParamSourceTypeText DocumentBlockParamSourceType = "text"
DocumentBlockParamSourceTypeContent DocumentBlockParamSourceType = "content"
)
func (r DocumentBlockParamSourceType) IsKnown() bool {
switch r {
case DocumentBlockParamSourceTypeBase64, DocumentBlockParamSourceTypeText, DocumentBlockParamSourceTypeContent:
return true
}
return false
}
type DocumentBlockParamSourceMediaType string
const (
DocumentBlockParamSourceMediaTypeApplicationPDF DocumentBlockParamSourceMediaType = "application/pdf"
DocumentBlockParamSourceMediaTypeTextPlain DocumentBlockParamSourceMediaType = "text/plain"
)
func (r DocumentBlockParamSourceMediaType) IsKnown() bool {
switch r {
case DocumentBlockParamSourceMediaTypeApplicationPDF, DocumentBlockParamSourceMediaTypeTextPlain:
return true
}
return false
}
type DocumentBlockParamType string
const (
DocumentBlockParamTypeDocument DocumentBlockParamType = "document"
)
func (r DocumentBlockParamType) IsKnown() bool {
switch r {
case DocumentBlockParamTypeDocument:
return true
}
return false
}
type ImageBlockParam struct {
Source param.Field[ImageBlockParamSource] `json:"source,required"`
Type param.Field[ImageBlockParamType] `json:"type,required"`
CacheControl param.Field[CacheControlEphemeralParam] `json:"cache_control"`
}
func NewImageBlockBase64(mediaType string, encodedData string) ImageBlockParam {
return ImageBlockParam{
Type: F(ImageBlockParamTypeImage),
Source: F(ImageBlockParamSource{
Type: F(ImageBlockParamSourceTypeBase64),
Data: F(encodedData),
MediaType: F(ImageBlockParamSourceMediaType(mediaType)),
}),
}
}
func (r ImageBlockParam) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r ImageBlockParam) implementsContentBlockParamUnion() {}
func (r ImageBlockParam) implementsContentBlockSourceContentUnionParam() {}
func (r ImageBlockParam) implementsToolResultBlockParamContentUnion() {}
type ImageBlockParamSource struct {
Data param.Field[string] `json:"data,required" format:"byte"`
MediaType param.Field[ImageBlockParamSourceMediaType] `json:"media_type,required"`
Type param.Field[ImageBlockParamSourceType] `json:"type,required"`
}
func (r ImageBlockParamSource) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ImageBlockParamSourceMediaType string
const (
ImageBlockParamSourceMediaTypeImageJPEG ImageBlockParamSourceMediaType = "image/jpeg"
ImageBlockParamSourceMediaTypeImagePNG ImageBlockParamSourceMediaType = "image/png"
ImageBlockParamSourceMediaTypeImageGIF ImageBlockParamSourceMediaType = "image/gif"
ImageBlockParamSourceMediaTypeImageWebP ImageBlockParamSourceMediaType = "image/webp"
)
func (r ImageBlockParamSourceMediaType) IsKnown() bool {
switch r {
case ImageBlockParamSourceMediaTypeImageJPEG, ImageBlockParamSourceMediaTypeImagePNG, ImageBlockParamSourceMediaTypeImageGIF, ImageBlockParamSourceMediaTypeImageWebP:
return true
}
return false
}
type ImageBlockParamSourceType string
const (
ImageBlockParamSourceTypeBase64 ImageBlockParamSourceType = "base64"
)
func (r ImageBlockParamSourceType) IsKnown() bool {
switch r {
case ImageBlockParamSourceTypeBase64:
return true
}
return false
}
type ImageBlockParamType string
const (
ImageBlockParamTypeImage ImageBlockParamType = "image"
)
func (r ImageBlockParamType) IsKnown() bool {
switch r {
case ImageBlockParamTypeImage:
return true
}
return false
}
type InputJSONDelta struct {
PartialJSON string `json:"partial_json,required"`
Type InputJSONDeltaType `json:"type,required"`
JSON inputJSONDeltaJSON `json:"-"`
}
// inputJSONDeltaJSON contains the JSON metadata for the struct [InputJSONDelta]
type inputJSONDeltaJSON struct {
PartialJSON apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *InputJSONDelta) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r inputJSONDeltaJSON) RawJSON() string {
return r.raw
}
func (r InputJSONDelta) implementsContentBlockDeltaEventDelta() {}
type InputJSONDeltaType string
const (
InputJSONDeltaTypeInputJSONDelta InputJSONDeltaType = "input_json_delta"
)
func (r InputJSONDeltaType) IsKnown() bool {
switch r {
case InputJSONDeltaTypeInputJSONDelta:
return true
}
return false
}
// Accumulate builds up the Message incrementally from a MessageStreamEvent. The Message then can be used as
// any other Message, except with the caveat that the Message.JSON field which normally can be used to inspect
// the JSON sent over the network may not be populated fully.
//
// message := anthropic.Message{}
// for stream.Next() {
// event := stream.Current()
// message.Accumulate(event)
// }
func (a *Message) Accumulate(event MessageStreamEvent) error {
if a == nil {
*a = Message{}
}
switch event := event.AsUnion().(type) {
case MessageStartEvent:
*a = event.Message
case MessageDeltaEvent:
a.StopReason = MessageStopReason(event.Delta.StopReason)
a.JSON.StopReason = event.Delta.JSON.StopReason
a.StopSequence = event.Delta.StopSequence
a.JSON.StopSequence = event.Delta.JSON.StopSequence
a.Usage.OutputTokens = event.Usage.OutputTokens
a.Usage.JSON.OutputTokens = event.Usage.JSON.OutputTokens
case MessageStopEvent:
case ContentBlockStartEvent:
a.Content = append(a.Content, ContentBlock{})
err := a.Content[len(a.Content)-1].UnmarshalJSON([]byte(event.ContentBlock.JSON.RawJSON()))
if err != nil {
return err
}
case ContentBlockDeltaEvent:
if len(a.Content) == 0 {
return fmt.Errorf("received event of type %s but there was no content block", event.Type)
}
switch delta := event.Delta.AsUnion().(type) {
case TextDelta:
cb := &a.Content[len(a.Content)-1]
cb.Text += delta.Text
if tb, ok := cb.union.(TextBlock); ok {
tb.Text = cb.Text
cb.union = tb
}
case InputJSONDelta:
cb := &a.Content[len(a.Content)-1]
if string(cb.Input) == "{}" {
cb.Input = json.RawMessage{}
}
cb.Input = append(cb.Input, []byte(delta.PartialJSON)...)
if tb, ok := cb.union.(ToolUseBlock); ok {
tb.Input = cb.Input
cb.union = tb
}
}
case ContentBlockStopEvent:
if len(a.Content) == 0 {
return fmt.Errorf("received event of type %s but there was no content block", event.Type)
}
}
return nil
}
// ToParam converts a Message to a MessageParam, which can be used when constructing a new
// Create
type Message struct {
// Unique object identifier.
//
// The format and length of IDs may change over time.
ID string `json:"id,required"`
// Content generated by the model.
//
// This is an array of content blocks, each of which has a `type` that determines
// its shape.
//
// Example:
//
// ```json
// [{ "type": "text", "text": "Hi, I'm Claude." }]
// ```
//
// If the request input `messages` ended with an `assistant` turn, then the
// response `content` will continue directly from that last turn. You can use this
// to constrain the model's output.
//
// For example, if the input `messages` were:
//
// ```json
// [
//
// {
// "role": "user",
// "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
// },
// { "role": "assistant", "content": "The best answer is (" }
//
// ]
// ```
//
// Then the response `content` might be:
//
// ```json
// [{ "type": "text", "text": "B)" }]
// ```
Content []ContentBlock `json:"content,required"`
// The model that will complete your prompt.\n\nSee