1use crate::client::env::{self, EnvError};
14use crate::completion::{CompletionRequest, ProviderCapabilities};
15use crate::error::EncodeError;
16use crate::error::ProviderError;
17use crate::message::Issuer;
18use crate::model::{ModelInfo, ModelList};
19pub use crate::operation::VerifyDecoder;
20use crate::operation::{Completion, ModelListing, ModelPage, Verify as VerifyOp};
21use crate::providers::internal::named_dialect;
22use crate::wire::Flow;
23use crate::wire::{
24 Body, Capabilities, Decoder, Descriptor, Encoded, Framing, Mode, Out, Secret, Wire, WireEvent,
25 WireFrame,
26};
27use serde::{Deserialize, Serialize};
28
29use super::completion::{
30 AnthropicCompletionRequest, AnthropicRequestParams, CacheTtl, ToolDefinition,
31 default_max_tokens_for_model, rejects_forced_tool_choice, sanitize_strict_tool_schema,
32};
33use super::streaming::MessagesDecoder;
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub struct Dialect {
40 pub name: &'static str,
42 pub base_url: &'static str,
44 pub api_key_env: &'static str,
46 pub base_url_env: Option<&'static str>,
49 pub request_id_header: Option<&'static str>,
51 pub quirks: Quirks,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct Quirks {
59 pub max_tokens: MaxTokens,
63 pub strict_tool_schemas: bool,
66}
67
68impl Quirks {
69 pub const fn anthropic() -> Self {
71 Self {
72 max_tokens: MaxTokens::ByModel,
73 strict_tool_schemas: true,
74 }
75 }
76
77 pub const fn gateway() -> Self {
79 Self {
80 max_tokens: MaxTokens::Fixed(4096),
81 strict_tool_schemas: false,
82 }
83 }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
88pub enum MaxTokens {
89 ByModel,
91 Fixed(u64),
93}
94
95pub const ANTHROPIC: Dialect = Dialect {
97 name: "anthropic",
98 base_url: "https://api.anthropic.com",
99 api_key_env: "ANTHROPIC_API_KEY",
100 base_url_env: Some("ANTHROPIC_BASE_URL"),
101 request_id_header: Some("request-id"),
102 quirks: Quirks::anthropic(),
103};
104
105const ALL: &[&Dialect] = &[&ANTHROPIC, &ZAI, &MINIMAX, &MOONSHOT, &XIAOMIMIMO];
107
108pub fn all() -> impl Iterator<Item = &'static Dialect> {
110 ALL.iter().copied()
111}
112
113impl Dialect {
114 pub fn by_name(name: &str) -> Option<Self> {
116 all().copied().find(|dialect| dialect.name == name)
117 }
118}
119
120impl Serialize for Dialect {
121 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
122 let registered = Self::by_name(self.name).as_ref() == Some(self);
123 named_dialect::serialize(serializer, "Anthropic", self.name, registered)
124 }
125}
126
127impl<'de> Deserialize<'de> for Dialect {
128 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
129 named_dialect::deserialize(deserializer, "Anthropic", Self::by_name)
130 }
131}
132
133pub const fn compatible(
136 name: &'static str,
137 base_url: &'static str,
138 api_key_env: &'static str,
139 base_url_env: Option<&'static str>,
140) -> Dialect {
141 Dialect {
142 name,
143 base_url,
144 api_key_env,
145 base_url_env,
146 request_id_header: Some("request-id"),
147 quirks: Quirks::gateway(),
148 }
149}
150
151pub(crate) fn strict_tool_transform(tool: &mut ToolDefinition) {
153 sanitize_strict_tool_schema(&mut tool.input_schema);
154 tool.strict = true;
155}
156
157impl Dialect {
158 pub fn default_max_tokens(&self, model: &str) -> Option<u64> {
160 match self.quirks.max_tokens {
161 MaxTokens::ByModel => default_max_tokens_for_model(model),
162 MaxTokens::Fixed(tokens) => Some(tokens),
163 }
164 }
165}
166
167pub const ZAI: Dialect = compatible(
169 "zai",
170 "https://api.z.ai/api/anthropic",
171 "ZAI_API_KEY",
172 Some("ZAI_ANTHROPIC_API_BASE"),
173);
174
175pub const MINIMAX: Dialect = compatible(
177 "minimax",
178 "https://api.minimax.io/anthropic",
179 "MINIMAX_API_KEY",
180 Some("MINIMAX_ANTHROPIC_API_BASE"),
181);
182
183pub const MOONSHOT: Dialect = compatible(
185 "moonshot",
186 "https://api.moonshot.ai/anthropic",
187 "MOONSHOT_API_KEY",
188 Some("MOONSHOT_ANTHROPIC_API_BASE"),
189);
190
191pub const XIAOMIMIMO: Dialect = compatible(
193 "xiaomimimo",
194 "https://api.xiaomimimo.com/anthropic/v1",
195 "XIAOMI_MIMO_API_KEY",
196 Some("XIAOMI_MIMO_ANTHROPIC_API_BASE"),
197);
198
199#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
203#[serde(deny_unknown_fields)]
204pub struct AnthropicConfig {
205 pub api_key: Secret,
207 pub base_url: String,
209 pub version: String,
211 pub betas: Vec<String>,
213 pub dialect: Dialect,
215}
216
217impl AnthropicConfig {
218 pub fn new(api_key: impl Into<Secret>) -> Self {
220 Self::with_dialect(api_key, &ANTHROPIC)
221 }
222
223 pub fn with_dialect(api_key: impl Into<Secret>, dialect: &Dialect) -> Self {
225 Self {
226 api_key: api_key.into(),
227 base_url: dialect.base_url.to_owned(),
228 version: super::completion::ANTHROPIC_VERSION_LATEST.to_owned(),
229 betas: Vec::new(),
230 dialect: *dialect,
231 }
232 }
233
234 pub fn from_env() -> Result<Self, EnvError> {
236 Self::from_env_with(&ANTHROPIC)
237 }
238
239 pub fn from_env_with(dialect: &Dialect) -> Result<Self, EnvError> {
241 let mut provider = Self::with_dialect(env::required(dialect.api_key_env)?, dialect);
242 if let Some(name) = dialect.base_url_env
243 && let Some(base_url) = env::optional(name)?
244 {
245 provider.base_url = normalize_base_url(&base_url);
246 }
247 Ok(provider)
248 }
249
250 pub fn with_version(mut self, version: impl Into<String>) -> Self {
252 self.version = version.into();
253 self
254 }
255
256 pub fn with_beta(mut self, beta: impl Into<String>) -> Self {
258 self.betas.push(beta.into());
259 self
260 }
261
262 pub fn with_base_url(mut self, base_url: impl AsRef<str>) -> Self {
264 self.base_url = normalize_base_url(base_url.as_ref());
265 self
266 }
267
268 pub(crate) fn completion(&self, model: impl Into<String>) -> Messages {
270 let model = model.into();
271 Messages {
272 default_max_tokens: self.dialect.default_max_tokens(&model),
273 provider: self.clone(),
274 model,
275 prompt_caching: false,
276 automatic_caching: false,
277 automatic_caching_ttl: None,
278 static_prefix_cache_ttl: None,
279 strict_tools: false,
280 }
281 }
282
283 pub(crate) fn models(&self) -> Models {
285 Models {
286 provider: self.clone(),
287 }
288 }
289
290 pub(crate) fn verify(&self) -> Verify {
292 Verify {
293 provider: self.clone(),
294 }
295 }
296
297 fn headers(&self, builder: http::request::Builder) -> http::request::Builder {
299 let builder = builder
300 .header("x-api-key", self.api_key.expose())
301 .header("anthropic-version", &self.version);
302 if self.betas.is_empty() {
303 builder
304 } else {
305 builder.header("anthropic-beta", self.betas.join(","))
306 }
307 }
308}
309
310pub fn normalize_base_url(base_url: &str) -> String {
313 let trimmed = base_url.trim_end_matches('/');
314 for suffix in ["/v1/messages", "/messages", "/v1"] {
315 if let Some(stripped) = trimmed.strip_suffix(suffix) {
316 return stripped.to_owned();
317 }
318 }
319 trimmed.to_owned()
320}
321
322#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
324pub struct Messages {
325 pub provider: AnthropicConfig,
327 pub model: String,
329 pub default_max_tokens: Option<u64>,
333 pub prompt_caching: bool,
337 pub automatic_caching: bool,
340 pub automatic_caching_ttl: Option<CacheTtl>,
342 pub static_prefix_cache_ttl: Option<CacheTtl>,
345 pub strict_tools: bool,
347}
348
349impl Messages {
350 pub fn with_default_max_tokens(mut self, tokens: u64) -> Self {
352 self.default_max_tokens = Some(tokens);
353 self
354 }
355
356 pub fn with_prompt_caching(mut self) -> Self {
360 self.prompt_caching = true;
361 self
362 }
363
364 pub fn with_automatic_caching(mut self) -> Self {
379 self.automatic_caching = true;
380 self
381 }
382
383 pub fn with_automatic_caching_1h(mut self) -> Self {
398 self.automatic_caching = true;
399 self.automatic_caching_ttl = Some(CacheTtl::OneHour);
400 self
401 }
402
403 pub fn with_static_prefix_cache_ttl(mut self, ttl: CacheTtl) -> Self {
424 self.static_prefix_cache_ttl = Some(ttl);
425 self
426 }
427
428 pub fn with_strict_tools(mut self) -> Self {
443 self.strict_tools = true;
444 self
445 }
446
447 fn body(
449 &self,
450 mut request: CompletionRequest,
451 mode: Mode,
452 ) -> Result<serde_json::Value, EncodeError> {
453 if request.max_tokens.is_none() {
454 let Some(tokens) = self.default_max_tokens else {
455 return Err(EncodeError::request(
456 "`max_tokens` must be set for Anthropic",
457 ));
458 };
459 request.max_tokens = Some(tokens);
460 }
461 let model = request.model.clone().unwrap_or_else(|| self.model.clone());
462 let issuers = [Issuer::from_static(self.provider.dialect.name)];
464 let request = request.replayable_to(&issuers)?;
465 let typed = AnthropicCompletionRequest::try_from_params(
466 AnthropicRequestParams {
467 model: &model,
468 issuers: &issuers,
469 request,
470 prompt_caching: self.prompt_caching,
471 automatic_caching: self.automatic_caching,
472 automatic_caching_ttl: self.automatic_caching_ttl.clone(),
473 static_prefix_cache_ttl: self.static_prefix_cache_ttl.clone(),
474 },
475 (self.strict_tools && self.provider.dialect.quirks.strict_tool_schemas)
476 .then_some(strict_tool_transform as fn(&mut ToolDefinition)),
477 )?;
478 let mut body = serde_json::to_value(&typed)?;
479 if mode == Mode::Unary {
480 return Ok(body);
481 }
482 if let Some(map) = body.as_object_mut() {
484 map.insert("stream".to_owned(), serde_json::Value::Bool(true));
485 if map.contains_key("tools") {
486 map.entry("tool_choice")
487 .or_insert_with(|| serde_json::json!({ "type": "auto" }));
488 } else {
489 map.remove("tool_choice");
490 }
491 }
492 Ok(body)
493 }
494}
495
496impl Wire for Messages {
497 type Op = Completion;
498 type Payload = crate::wire::Encoded;
499 type Frame = crate::wire::WireFrame;
500 type Decoder<'id> = MessagesDecoder<'id>;
501
502 fn describe(&self) -> Descriptor<'_> {
504 Descriptor::new(self.provider.dialect.name)
505 .model(self.model.as_str())
506 .capabilities(Capabilities::completion(
507 ProviderCapabilities::default()
508 .with_native_output_tool_composition(true)
509 .with_forced_tool_choice_rejected(rejects_forced_tool_choice(&self.model)),
510 ))
511 }
512
513 fn encode(&self, request: CompletionRequest, mode: Mode) -> Result<Encoded, EncodeError> {
514 let body = self.body(request, mode)?;
515 crate::providers::internal::trace_json(
516 crate::providers::internal::LogTarget::Completions,
517 "Anthropic completion request",
518 &body,
519 );
520 let request = self
521 .provider
522 .headers(http::Request::post(format!(
523 "{}/v1/messages",
524 self.provider.base_url
525 )))
526 .header(http::header::CONTENT_TYPE, "application/json")
527 .body(Body::Bytes(serde_json::to_vec(&body)?))?;
528 Ok(Encoded::new(
529 request,
530 match mode {
531 Mode::Unary => Framing::Whole,
532 Mode::Streaming => Framing::Sse,
533 },
534 )
535 .with_request_id_header(self.provider.dialect.request_id_header)
536 .with_projection(MessagesDecoder::project))
537 }
538
539 fn decoder<'id>(&self) -> Self::Decoder<'id> {
540 MessagesDecoder::new()
541 }
542}
543
544#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
546pub struct Models {
547 pub provider: AnthropicConfig,
549}
550
551impl Wire for Models {
552 type Op = ModelListing;
553 type Payload = crate::wire::Encoded;
554 type Frame = crate::wire::WireFrame;
555 type Decoder<'id> = ModelsDecoder;
556
557 fn describe(&self) -> Descriptor<'_> {
558 Descriptor::new(self.provider.dialect.name)
559 }
560
561 fn encode(&self, cursor: Option<String>, _mode: Mode) -> Result<Encoded, EncodeError> {
562 Ok(Encoded::new(
563 self.models_request(cursor.as_deref())?,
564 Framing::Whole,
565 ))
566 }
567
568 fn decoder<'id>(&self) -> Self::Decoder<'id> {
569 ModelsDecoder
570 }
571}
572
573impl Models {
574 fn models_request(&self, cursor: Option<&str>) -> Result<http::Request<Body>, EncodeError> {
576 let uri = match cursor {
577 Some(cursor) => format!(
578 "{}{}",
579 self.provider.base_url,
580 crate::providers::internal::with_query_pairs("/v1/models", &[("after_id", cursor)],)
581 ),
582 None => format!("{}/v1/models", self.provider.base_url),
583 };
584 self.provider
585 .headers(http::Request::get(uri))
586 .body(Body::empty())
587 .map_err(EncodeError::from)
588 }
589}
590
591#[derive(Debug, Deserialize)]
593#[doc(hidden)]
594pub struct ModelsPage {
595 data: Vec<ModelEntry>,
596 #[serde(default)]
597 has_more: bool,
598 #[serde(default)]
599 last_id: Option<String>,
600}
601
602#[derive(Debug, Deserialize)]
603struct ModelEntry {
604 id: String,
605 display_name: String,
606}
607
608impl From<ModelEntry> for ModelInfo {
609 fn from(entry: ModelEntry) -> Self {
610 ModelInfo::new(entry.id, entry.display_name)
611 }
612}
613
614pub struct ModelsDecoder;
616
617impl<'id> Decoder<'id, ModelListing> for ModelsDecoder {
618 type Event = ModelsPage;
619
620 fn classify(&self, frame: WireFrame) -> WireEvent<Self::Event> {
621 crate::providers::internal::wire::classify_marker_keyed_frame(&frame.as_str(), &["data"])
622 }
623
624 fn decode(
625 &mut self,
626 page: Self::Event,
627 out: Out<'id, ModelListing>,
628 ) -> Result<Flow, ProviderError> {
629 let next = page
631 .last_id
632 .filter(|cursor| page.has_more && !cursor.is_empty());
633 Ok(out.end(ModelPage {
634 models: ModelList::new(page.data.into_iter().map(ModelInfo::from).collect()),
635 next,
636 }))
637 }
638}
639
640#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
643pub struct Verify {
644 pub provider: AnthropicConfig,
646}
647
648impl Wire for Verify {
649 type Op = VerifyOp;
650 type Payload = crate::wire::Encoded;
651 type Frame = crate::wire::WireFrame;
652 type Decoder<'id> = VerifyDecoder;
653
654 fn describe(&self) -> Descriptor<'_> {
655 Descriptor::new(self.provider.dialect.name)
656 }
657
658 fn encode(&self, _request: (), _mode: Mode) -> Result<Encoded, EncodeError> {
659 let request = self
660 .provider
661 .headers(http::Request::get(format!(
662 "{}/v1/models",
663 self.provider.base_url
664 )))
665 .body(Body::empty())?;
666 Ok(Encoded::new(request, Framing::Whole))
667 }
668
669 fn decoder<'id>(&self) -> Self::Decoder<'id> {
670 VerifyDecoder
671 }
672}
673
674#[cfg(test)]
675mod tests;