1// Package anthropic provides an implementation of the fantasy AI SDK for Anthropic's language models.
2package anthropic
3
4import (
5 "encoding/json"
6
7 "charm.land/fantasy"
8)
9
10// Global type identifiers for Anthropic-specific provider data.
11const (
12 TypeProviderOptions = Name + ".options"
13 TypeReasoningOptionMetadata = Name + ".reasoning_metadata"
14 TypeProviderCacheControl = Name + ".cache_control_options"
15)
16
17// ProviderOptions represents additional options for the Anthropic provider.
18type ProviderOptions struct {
19 SendReasoning *bool `json:"send_reasoning"`
20 Thinking *ThinkingProviderOption `json:"thinking"`
21 DisableParallelToolUse *bool `json:"disable_parallel_tool_use"`
22}
23
24// Options implements the ProviderOptions interface.
25func (o *ProviderOptions) Options() {}
26
27// MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.
28func (o ProviderOptions) MarshalJSON() ([]byte, error) {
29 type plain ProviderOptions
30 raw, err := json.Marshal(plain(o))
31 if err != nil {
32 return nil, err
33 }
34 return json.Marshal(struct {
35 Type string `json:"type"`
36 Data json.RawMessage `json:"data"`
37 }{
38 Type: TypeProviderOptions,
39 Data: raw,
40 })
41}
42
43// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.
44func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
45 type plain ProviderOptions
46 var oo plain
47 err := json.Unmarshal(data, &oo)
48 if err != nil {
49 return err
50 }
51 *o = ProviderOptions(oo)
52 return nil
53}
54
55// ThinkingProviderOption represents thinking options for the Anthropic provider.
56type ThinkingProviderOption struct {
57 BudgetTokens int64 `json:"budget_tokens"`
58}
59
60// ReasoningOptionMetadata represents reasoning metadata for the Anthropic provider.
61type ReasoningOptionMetadata struct {
62 Signature string `json:"signature"`
63 RedactedData string `json:"redacted_data"`
64}
65
66// Options implements the ProviderOptions interface.
67func (*ReasoningOptionMetadata) Options() {}
68
69// MarshalJSON implements custom JSON marshaling with type info for ReasoningOptionMetadata.
70func (m ReasoningOptionMetadata) MarshalJSON() ([]byte, error) {
71 type plain ReasoningOptionMetadata
72 raw, err := json.Marshal(plain(m))
73 if err != nil {
74 return nil, err
75 }
76 return json.Marshal(struct {
77 Type string `json:"type"`
78 Data json.RawMessage `json:"data"`
79 }{
80 Type: TypeReasoningOptionMetadata,
81 Data: raw,
82 })
83}
84
85// UnmarshalJSON implements custom JSON unmarshaling with type info for ReasoningOptionMetadata.
86func (m *ReasoningOptionMetadata) UnmarshalJSON(data []byte) error {
87 type plain ReasoningOptionMetadata
88 var rm plain
89 err := json.Unmarshal(data, &rm)
90 if err != nil {
91 return err
92 }
93 *m = ReasoningOptionMetadata(rm)
94 return nil
95}
96
97// ProviderCacheControlOptions represents cache control options for the Anthropic provider.
98type ProviderCacheControlOptions struct {
99 CacheControl CacheControl `json:"cache_control"`
100}
101
102// Options implements the ProviderOptions interface.
103func (*ProviderCacheControlOptions) Options() {}
104
105// MarshalJSON implements custom JSON marshaling with type info for ProviderCacheControlOptions.
106func (o ProviderCacheControlOptions) MarshalJSON() ([]byte, error) {
107 type plain ProviderCacheControlOptions
108 raw, err := json.Marshal(plain(o))
109 if err != nil {
110 return nil, err
111 }
112 return json.Marshal(struct {
113 Type string `json:"type"`
114 Data json.RawMessage `json:"data"`
115 }{
116 Type: TypeProviderCacheControl,
117 Data: raw,
118 })
119}
120
121// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderCacheControlOptions.
122func (o *ProviderCacheControlOptions) UnmarshalJSON(data []byte) error {
123 type plain ProviderCacheControlOptions
124 var cc plain
125 err := json.Unmarshal(data, &cc)
126 if err != nil {
127 return err
128 }
129 *o = ProviderCacheControlOptions(cc)
130 return nil
131}
132
133// CacheControl represents cache control settings for the Anthropic provider.
134type CacheControl struct {
135 Type string `json:"type"`
136}
137
138// NewProviderOptions creates new provider options for the Anthropic provider.
139func NewProviderOptions(opts *ProviderOptions) fantasy.ProviderOptions {
140 return fantasy.ProviderOptions{
141 Name: opts,
142 }
143}
144
145// NewProviderCacheControlOptions creates new cache control options for the Anthropic provider.
146func NewProviderCacheControlOptions(opts *ProviderCacheControlOptions) fantasy.ProviderOptions {
147 return fantasy.ProviderOptions{
148 Name: opts,
149 }
150}
151
152// ParseOptions parses provider options from a map for the Anthropic provider.
153func ParseOptions(data map[string]any) (*ProviderOptions, error) {
154 var options ProviderOptions
155 if err := fantasy.ParseOptions(data, &options); err != nil {
156 return nil, err
157 }
158 return &options, nil
159}
160
161// Register Anthropic provider-specific types with the global registry.
162func init() {
163 fantasy.RegisterProviderType(TypeProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) {
164 var v ProviderOptions
165 if err := json.Unmarshal(data, &v); err != nil {
166 return nil, err
167 }
168 return &v, nil
169 })
170 fantasy.RegisterProviderType(TypeReasoningOptionMetadata, func(data []byte) (fantasy.ProviderOptionsData, error) {
171 var v ReasoningOptionMetadata
172 if err := json.Unmarshal(data, &v); err != nil {
173 return nil, err
174 }
175 return &v, nil
176 })
177 fantasy.RegisterProviderType(TypeProviderCacheControl, func(data []byte) (fantasy.ProviderOptionsData, error) {
178 var v ProviderCacheControlOptions
179 if err := json.Unmarshal(data, &v); err != nil {
180 return nil, err
181 }
182 return &v, nil
183 })
184}