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// Effort represents the output effort level for Anthropic models.
11//
12// This maps to Messages API `output_config.effort`.
13type Effort string
14
15const (
16 // EffortLow represents low output effort.
17 EffortLow Effort = "low"
18 // EffortMedium represents medium output effort.
19 EffortMedium Effort = "medium"
20 // EffortHigh represents high output effort.
21 EffortHigh Effort = "high"
22 // EffortMax represents maximum output effort.
23 EffortMax Effort = "max"
24)
25
26// Global type identifiers for Anthropic-specific provider data.
27const (
28 TypeProviderOptions = Name + ".options"
29 TypeReasoningOptionMetadata = Name + ".reasoning_metadata"
30 TypeProviderCacheControl = Name + ".cache_control_options"
31)
32
33// Register Anthropic provider-specific types with the global registry.
34func init() {
35 fantasy.RegisterProviderType(TypeProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) {
36 var v ProviderOptions
37 if err := json.Unmarshal(data, &v); err != nil {
38 return nil, err
39 }
40 return &v, nil
41 })
42 fantasy.RegisterProviderType(TypeReasoningOptionMetadata, func(data []byte) (fantasy.ProviderOptionsData, error) {
43 var v ReasoningOptionMetadata
44 if err := json.Unmarshal(data, &v); err != nil {
45 return nil, err
46 }
47 return &v, nil
48 })
49 fantasy.RegisterProviderType(TypeProviderCacheControl, func(data []byte) (fantasy.ProviderOptionsData, error) {
50 var v ProviderCacheControlOptions
51 if err := json.Unmarshal(data, &v); err != nil {
52 return nil, err
53 }
54 return &v, nil
55 })
56}
57
58// ProviderOptions represents additional options for the Anthropic provider.
59type ProviderOptions struct {
60 SendReasoning *bool `json:"send_reasoning"`
61 Thinking *ThinkingProviderOption `json:"thinking"`
62 Effort *Effort `json:"effort"`
63 DisableParallelToolUse *bool `json:"disable_parallel_tool_use"`
64}
65
66// Options implements the ProviderOptions interface.
67func (o *ProviderOptions) Options() {}
68
69// MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.
70func (o ProviderOptions) MarshalJSON() ([]byte, error) {
71 type plain ProviderOptions
72 return fantasy.MarshalProviderType(TypeProviderOptions, plain(o))
73}
74
75// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.
76func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
77 type plain ProviderOptions
78 var p plain
79 if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
80 return err
81 }
82 *o = ProviderOptions(p)
83 return nil
84}
85
86// ThinkingProviderOption represents thinking options for the Anthropic provider.
87type ThinkingProviderOption struct {
88 BudgetTokens int64 `json:"budget_tokens"`
89}
90
91// ReasoningOptionMetadata represents reasoning metadata for the Anthropic provider.
92type ReasoningOptionMetadata struct {
93 Signature string `json:"signature"`
94 RedactedData string `json:"redacted_data"`
95}
96
97// Options implements the ProviderOptions interface.
98func (*ReasoningOptionMetadata) Options() {}
99
100// MarshalJSON implements custom JSON marshaling with type info for ReasoningOptionMetadata.
101func (m ReasoningOptionMetadata) MarshalJSON() ([]byte, error) {
102 type plain ReasoningOptionMetadata
103 return fantasy.MarshalProviderType(TypeReasoningOptionMetadata, plain(m))
104}
105
106// UnmarshalJSON implements custom JSON unmarshaling with type info for ReasoningOptionMetadata.
107func (m *ReasoningOptionMetadata) UnmarshalJSON(data []byte) error {
108 type plain ReasoningOptionMetadata
109 var p plain
110 if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
111 return err
112 }
113 *m = ReasoningOptionMetadata(p)
114 return nil
115}
116
117// ProviderCacheControlOptions represents cache control options for the Anthropic provider.
118type ProviderCacheControlOptions struct {
119 CacheControl CacheControl `json:"cache_control"`
120}
121
122// Options implements the ProviderOptions interface.
123func (*ProviderCacheControlOptions) Options() {}
124
125// MarshalJSON implements custom JSON marshaling with type info for ProviderCacheControlOptions.
126func (o ProviderCacheControlOptions) MarshalJSON() ([]byte, error) {
127 type plain ProviderCacheControlOptions
128 return fantasy.MarshalProviderType(TypeProviderCacheControl, plain(o))
129}
130
131// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderCacheControlOptions.
132func (o *ProviderCacheControlOptions) UnmarshalJSON(data []byte) error {
133 type plain ProviderCacheControlOptions
134 var p plain
135 if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
136 return err
137 }
138 *o = ProviderCacheControlOptions(p)
139 return nil
140}
141
142// CacheControl represents cache control settings for the Anthropic provider.
143type CacheControl struct {
144 Type string `json:"type"`
145}
146
147// NewProviderOptions creates new provider options for the Anthropic provider.
148func NewProviderOptions(opts *ProviderOptions) fantasy.ProviderOptions {
149 return fantasy.ProviderOptions{
150 Name: opts,
151 }
152}
153
154// NewProviderCacheControlOptions creates new cache control options for the Anthropic provider.
155func NewProviderCacheControlOptions(opts *ProviderCacheControlOptions) fantasy.ProviderOptions {
156 return fantasy.ProviderOptions{
157 Name: opts,
158 }
159}
160
161// ParseOptions parses provider options from a map for the Anthropic provider.
162func ParseOptions(data map[string]any) (*ProviderOptions, error) {
163 var options ProviderOptions
164 if err := fantasy.ParseOptions(data, &options); err != nil {
165 return nil, err
166 }
167 return &options, nil
168}