1use crate::catalog::ModelFacts;
14use crate::client::env::{self, EnvError};
15use crate::completion::options::FinalBody;
16use crate::completion::{CompletionRequest, ProviderCapabilities};
17use crate::error::EncodeError;
18use crate::operation::Completion;
19use crate::providers::internal::{credential_header, named_dialect};
20use crate::wire::{Body, Capabilities, Descriptor, Encoded, Framing, Mode, Secret, Wire};
21use serde::{Deserialize, Serialize};
22
23use super::completion::{
24 CacheTtl, default_max_tokens_for_model, document_source, image_source,
25 rejects_forced_tool_choice,
26};
27use super::streaming::MessagesDecoder;
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub struct Dialect {
34 pub name: &'static str,
36 pub base_url: &'static str,
38 pub api_key_env: &'static str,
40 pub base_url_env: Option<&'static str>,
43 pub request_id_header: Option<&'static str>,
45 pub quirks: Quirks,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct Quirks {
54 pub max_tokens: MaxTokens,
58 pub strict_tool_schemas: bool,
61 pub unsigned_thinking: bool,
65 pub tool_input_streaming: ToolInputStreaming,
67 pub auth: Auth,
69 pub session_header: Option<&'static str>,
72 pub supports_output_format: bool,
77 pub catalog_priced: bool,
80 pub cache_ttl_pricing: bool,
84}
85
86#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
88pub enum Auth {
89 #[default]
91 ApiKey,
92 OptionalBearer,
96}
97
98#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
104#[serde(rename_all = "snake_case")]
105pub enum ToolInputStreaming {
106 #[default]
108 Eager,
109 BetaHeader,
112 Off,
114}
115
116impl Quirks {
117 pub const fn anthropic() -> Self {
119 Self {
120 max_tokens: MaxTokens::ByModel,
121 strict_tool_schemas: true,
122 unsigned_thinking: false,
123 tool_input_streaming: ToolInputStreaming::Eager,
124 auth: Auth::ApiKey,
125 session_header: None,
126 supports_output_format: true,
127 catalog_priced: true,
128 cache_ttl_pricing: true,
129 }
130 }
131
132 pub const fn gateway() -> Self {
136 Self {
137 max_tokens: MaxTokens::Fixed(4096),
138 strict_tool_schemas: false,
139 unsigned_thinking: false,
140 tool_input_streaming: ToolInputStreaming::Eager,
141 auth: Auth::ApiKey,
142 session_header: None,
143 supports_output_format: true,
144 catalog_priced: true,
145 cache_ttl_pricing: false,
146 }
147 }
148}
149
150#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
152pub enum MaxTokens {
153 ByModel,
155 Fixed(u64),
157}
158
159pub(crate) const MESSAGES_API: &str = "anthropic.messages";
161
162pub const ANTHROPIC: Dialect = Dialect {
164 name: super::PROVIDER_NAME,
165 base_url: "https://api.anthropic.com",
166 api_key_env: "ANTHROPIC_API_KEY",
167 base_url_env: Some("ANTHROPIC_BASE_URL"),
168 request_id_header: Some("request-id"),
169 quirks: Quirks::anthropic(),
170};
171
172const ALL: &[&Dialect] = &[
174 &ANTHROPIC,
175 &ZAI,
176 &MINIMAX,
177 &MOONSHOT,
178 &XIAOMIMIMO,
179 &OLLAMA,
180 &OPENCODE,
181 &OPENCODE_GO,
182];
183
184pub fn all() -> impl Iterator<Item = &'static Dialect> {
186 ALL.iter().copied()
187}
188
189impl Dialect {
190 pub fn by_name(name: &str) -> Option<Self> {
192 all().copied().find(|dialect| dialect.name == name)
193 }
194}
195
196impl Serialize for Dialect {
197 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
198 let registered = Self::by_name(self.name).as_ref() == Some(self);
199 named_dialect::serialize(serializer, "Anthropic", self.name, registered)
200 }
201}
202
203impl<'de> Deserialize<'de> for Dialect {
204 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
205 named_dialect::deserialize(deserializer, "Anthropic", Self::by_name)
206 }
207}
208
209pub const fn compatible(
212 name: &'static str,
213 base_url: &'static str,
214 api_key_env: &'static str,
215 base_url_env: Option<&'static str>,
216) -> Dialect {
217 Dialect {
218 name,
219 base_url,
220 api_key_env,
221 base_url_env,
222 request_id_header: Some("request-id"),
223 quirks: Quirks::gateway(),
224 }
225}
226
227impl Dialect {
228 pub fn default_max_tokens(&self, facts: &ModelFacts, model: &str) -> Option<u64> {
231 match self.quirks.max_tokens {
232 MaxTokens::ByModel => default_max_tokens_for_model(facts, model),
233 MaxTokens::Fixed(tokens) => Some(tokens),
234 }
235 }
236}
237
238pub const ZAI: Dialect = compatible(
240 crate::providers::zai::PROVIDER_NAME,
241 crate::providers::zai::ANTHROPIC_API_BASE_URL,
242 "ZAI_API_KEY",
243 Some("ZAI_ANTHROPIC_API_BASE"),
244);
245
246pub const MINIMAX: Dialect = compatible(
248 crate::providers::minimax::PROVIDER_NAME,
249 crate::providers::minimax::GLOBAL_ANTHROPIC_API_BASE_URL,
250 "MINIMAX_API_KEY",
251 Some("MINIMAX_ANTHROPIC_API_BASE"),
252);
253
254pub const MOONSHOT: Dialect = Dialect {
257 quirks: Quirks {
258 unsigned_thinking: true,
259 ..Quirks::gateway()
260 },
261 ..compatible(
262 crate::providers::moonshot::PROVIDER_NAME,
263 crate::providers::moonshot::ANTHROPIC_API_BASE_URL,
264 "MOONSHOT_API_KEY",
265 Some("MOONSHOT_ANTHROPIC_API_BASE"),
266 )
267};
268
269pub const XIAOMIMIMO: Dialect = compatible(
271 crate::providers::xiaomimimo::PROVIDER_NAME,
272 crate::providers::xiaomimimo::ANTHROPIC_API_BASE_URL,
273 "XIAOMI_MIMO_API_KEY",
274 Some("XIAOMI_MIMO_ANTHROPIC_API_BASE"),
275);
276
277pub const OLLAMA: Dialect = Dialect {
283 quirks: Quirks {
284 auth: Auth::OptionalBearer,
285 unsigned_thinking: true,
286 supports_output_format: false,
288 catalog_priced: false,
290 ..Quirks::gateway()
291 },
292 ..compatible(
293 crate::providers::ollama::PROVIDER_NAME,
294 "http://localhost:11434",
295 "OLLAMA_API_KEY",
296 Some("OLLAMA_API_BASE_URL"),
297 )
298};
299
300pub const OPENCODE: Dialect = compatible(
303 crate::providers::opencode::ZEN,
304 "https://opencode.ai/inference/anthropic",
305 "OPENCODE_API_KEY",
306 Some("OPENCODE_ANTHROPIC_BASE_URL"),
307);
308
309pub const OPENCODE_GO: Dialect = Dialect {
312 quirks: Quirks {
313 session_header: Some(crate::providers::opencode::SESSION_HEADER),
314 ..Quirks::gateway()
315 },
316 ..compatible(
317 crate::providers::opencode::GO,
318 "https://opencode.ai/inference/go/anthropic",
319 "OPENCODE_API_KEY",
320 Some("OPENCODE_GO_ANTHROPIC_BASE_URL"),
321 )
322};
323
324#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
328#[serde(deny_unknown_fields)]
329pub struct AnthropicConfig {
330 pub api_key: Secret,
332 pub base_url: String,
334 pub version: String,
336 pub betas: Vec<String>,
338 pub dialect: Dialect,
340 #[serde(default, skip_serializing_if = "Addressing::is_body")]
342 pub addressing: Addressing,
343}
344
345#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
347#[serde(rename_all = "snake_case")]
348pub enum Addressing {
349 #[default]
352 Body,
353 Url,
357}
358
359impl Addressing {
360 fn is_body(&self) -> bool {
362 *self == Self::Body
363 }
364}
365
366impl AnthropicConfig {
367 pub fn new(api_key: impl Into<Secret>) -> Self {
369 Self::with_key(&ANTHROPIC, api_key)
370 }
371
372 pub fn with_key(dialect: &Dialect, api_key: impl Into<Secret>) -> Self {
376 Self {
377 api_key: api_key.into(),
378 base_url: normalize_base_url(dialect.base_url),
379 version: super::completion::ANTHROPIC_VERSION_LATEST.to_owned(),
380 betas: Vec::new(),
381 dialect: *dialect,
382 addressing: Addressing::Body,
383 }
384 }
385
386 pub fn from_env() -> Result<Self, EnvError> {
388 Self::from_env_with(&ANTHROPIC)
389 }
390
391 pub fn from_env_with(dialect: &Dialect) -> Result<Self, EnvError> {
395 let api_key = match dialect.quirks.auth {
396 Auth::ApiKey => env::required(dialect.api_key_env)?,
397 Auth::OptionalBearer => env::optional(dialect.api_key_env)?.unwrap_or_default(),
398 };
399 Self::from_env_with_credential(dialect, api_key)
400 }
401
402 pub(crate) fn from_env_with_credential(
405 dialect: &Dialect,
406 api_key: impl Into<Secret>,
407 ) -> Result<Self, EnvError> {
408 let mut provider = Self::with_key(dialect, api_key);
409 if let Some(name) = dialect.base_url_env
410 && let Some(base_url) = env::optional(name)?
411 {
412 provider.base_url = normalize_base_url(&base_url);
413 }
414 Ok(provider)
415 }
416
417 pub fn with_version(mut self, version: impl Into<String>) -> Self {
419 self.version = version.into();
420 self
421 }
422
423 pub fn with_beta(mut self, beta: impl Into<String>) -> Self {
425 self.betas.push(beta.into());
426 self
427 }
428
429 pub fn with_base_url(mut self, base_url: impl AsRef<str>) -> Self {
431 self.base_url = normalize_base_url(base_url.as_ref());
432 self
433 }
434
435 pub fn completion(&self, model: impl Into<String>) -> Messages {
437 let model = model.into();
438 Messages {
439 default_max_tokens: self
440 .dialect
441 .default_max_tokens(ModelFacts::builtin(), &model),
442 provider: self.clone(),
443 model,
444 prompt_caching: false,
445 static_prefix_cache_ttl: None,
446 strict_tools: false,
447 tool_input_streaming: self.dialect.quirks.tool_input_streaming,
448 facts: ModelFacts::default(),
449 }
450 }
451
452 pub(super) fn headers(&self, builder: http::request::Builder) -> http::request::Builder {
454 self.headers_with(builder, &self.betas)
455 }
456
457 fn headers_with(
459 &self,
460 builder: http::request::Builder,
461 betas: &[String],
462 ) -> http::request::Builder {
463 let builder = match self.dialect.quirks.auth {
464 Auth::ApiKey => credential_header(builder, "x-api-key", self.api_key.expose()),
465 Auth::OptionalBearer if self.api_key.is_empty() => builder,
466 Auth::OptionalBearer => credential_header(
467 builder,
468 "authorization",
469 &format!("Bearer {}", self.api_key.expose()),
470 ),
471 };
472 let builder = builder.header("anthropic-version", &self.version);
473 if betas.is_empty() {
474 builder
475 } else {
476 builder.header("anthropic-beta", betas.join(","))
477 }
478 }
479
480 fn betas_with(&self, extra: &[&str]) -> Vec<String> {
483 let mut betas = self.betas.clone();
484 for extra in extra {
485 if !betas.iter().any(|beta| beta == extra) {
486 betas.push((*extra).to_owned());
487 }
488 }
489 betas
490 }
491}
492
493pub fn normalize_base_url(base_url: &str) -> String {
496 let trimmed = base_url.trim_end_matches('/');
497 for suffix in ["/v1/messages", "/messages", "/v1"] {
498 if let Some(stripped) = trimmed.strip_suffix(suffix) {
499 return stripped.to_owned();
500 }
501 }
502 trimmed.to_owned()
503}
504
505#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
507pub struct Messages {
508 pub provider: AnthropicConfig,
510 pub model: String,
512 pub default_max_tokens: Option<u64>,
516 pub prompt_caching: bool,
522 pub static_prefix_cache_ttl: Option<CacheTtl>,
525 pub strict_tools: bool,
527 #[serde(default)]
530 pub tool_input_streaming: ToolInputStreaming,
531 #[serde(skip)]
533 pub facts: ModelFacts,
534}
535
536impl Messages {
537 pub fn with_facts(mut self, facts: ModelFacts) -> Self {
541 self.default_max_tokens = self
542 .provider
543 .dialect
544 .default_max_tokens(&facts, &self.model);
545 self.facts = facts;
546 self
547 }
548
549 pub(super) fn max_tokens(&self, request: &CompletionRequest, model: &str) -> Option<u64> {
552 let other_model = (model != self.model).then_some(model);
553 request
554 .max_tokens
555 .or_else(|| {
556 other_model.and_then(|m| self.provider.dialect.default_max_tokens(&self.facts, m))
557 })
558 .or(self.default_max_tokens)
559 }
560
561 pub fn with_prompt_caching(mut self) -> Self {
566 self.prompt_caching = true;
567 self
568 }
569
570 pub fn with_static_prefix_cache_ttl(mut self, ttl: CacheTtl) -> Self {
593 self.static_prefix_cache_ttl = Some(ttl);
594 self
595 }
596
597 pub fn with_strict_tools(mut self) -> Self {
612 self.strict_tools = true;
613 self
614 }
615
616 pub fn with_tool_input_streaming(mut self, streaming: ToolInputStreaming) -> Self {
632 self.tool_input_streaming = streaming;
633 self
634 }
635}
636
637#[derive(Debug, Clone, PartialEq)]
640#[non_exhaustive]
641pub struct Prepared {
642 pub model: String,
644 pub body: FinalBody,
646 pub betas: Vec<String>,
649}
650
651impl Messages {
652 pub fn encode_with(
663 &self,
664 request: CompletionRequest,
665 mode: Mode,
666 envelope: impl FnOnce(Prepared) -> Result<http::Request<Body>, EncodeError>,
667 ) -> Result<Encoded, EncodeError> {
668 let model = request.model.clone().unwrap_or_else(|| self.model.clone());
669 let fine_grained = !request.tools.is_empty()
670 && self.tool_input_streaming == ToolInputStreaming::BetaHeader;
671 let body = super::completion::body(self, &request, mode)?;
672 let betas: Vec<&str> = [
673 body.get("thinking")
674 .and_then(|thinking| thinking.get("block_binding"))
675 .is_some_and(serde_json::Value::is_object)
676 .then_some(super::completion::THINKING_BINDING_BETA),
677 fine_grained.then_some(super::completion::FINE_GRAINED_TOOL_STREAMING_BETA),
678 ]
679 .into_iter()
680 .flatten()
681 .collect();
682 crate::providers::internal::trace_json(
683 crate::providers::internal::LogTarget::Completions,
684 "Anthropic completion request",
685 &body,
686 );
687 let request = envelope(Prepared {
688 model,
689 body,
690 betas: self.provider.betas_with(&betas),
691 })?;
692 Ok(Encoded::new(
693 request,
694 match mode {
695 Mode::Unary => Framing::Whole,
696 Mode::Streaming => Framing::Sse,
697 },
698 )
699 .with_request_id_header(self.provider.dialect.request_id_header))
700 }
701}
702
703impl Wire for Messages {
704 type Op = Completion;
705 type Payload = crate::wire::Encoded;
706 type Frame = crate::wire::WireFrame;
707 type Event = super::streaming::MessagesEvent;
708 type Decoder<'id> = MessagesDecoder;
709 type Reassembler = super::streaming::document::Message;
710
711 fn describe(&self) -> Descriptor<'_> {
715 Descriptor::new(self.provider.dialect.name)
716 .model(self.model.as_str())
717 .facts(&self.facts)
718 .capabilities(Capabilities::completion(
719 ProviderCapabilities::default()
720 .with_native_output_tool_composition(
721 self.provider.dialect.quirks.supports_output_format,
722 )
723 .with_forced_tool_choice_rejected(rejects_forced_tool_choice(
724 &self.facts,
725 &self.model,
726 )),
727 ))
728 .replay(self)
729 }
730
731 fn encode(&self, request: CompletionRequest, mode: Mode) -> Result<Encoded, EncodeError> {
732 let session = self.provider.dialect.quirks.session_header.map(|name| {
733 let id = crate::providers::internal::request_session_id(&request);
734 (name, id)
735 });
736 self.encode_with(request, mode, |prepared| {
737 let mut builder = self
738 .provider
739 .headers_with(
740 http::Request::post(format!("{}/v1/messages", self.provider.base_url)),
741 &prepared.betas,
742 )
743 .header(http::header::CONTENT_TYPE, "application/json");
744 if let Some((name, id)) = session {
745 builder = builder.header(name, id);
746 }
747 Ok(builder.body(prepared.body.into_body())?)
748 })
749 }
750
751 fn decoder<'id>(&self) -> Self::Decoder<'id> {
752 MessagesDecoder::new(self.provider.dialect.quirks)
753 }
754}
755
756impl crate::completion::ReplayTarget for Messages {
757 fn catalog_priced(&self) -> bool {
760 self.provider.dialect.quirks.catalog_priced
761 }
762
763 fn map_options(
765 &self,
766 request: &CompletionRequest,
767 fields: crate::completion::options::OptionFields<'_>,
768 ) -> crate::completion::options::OptionMap {
769 super::options::map_options(self, request, fields)
770 }
771
772 fn facts(&self) -> Option<&ModelFacts> {
773 Some(&self.facts)
774 }
775
776 fn api(&self) -> crate::message::Api {
777 crate::message::Api::from_static(MESSAGES_API)
778 }
779
780 fn provider(&self) -> &str {
781 self.provider.dialect.name
782 }
783
784 fn model(&self) -> &str {
785 &self.model
786 }
787
788 fn accepts(&self, model: &str) -> crate::completion::Accepts {
794 let rule: Option<fn(&str) -> bool> = match self.provider.dialect.name {
795 name if name == ANTHROPIC.name => None,
796 name if name == ZAI.name => Some(crate::providers::zai::reads_images),
797 name if name == MOONSHOT.name => Some(crate::providers::moonshot::reads_images),
798 name if name == MINIMAX.name => Some(crate::providers::minimax::reads_images),
799 name if name == XIAOMIMIMO.name => Some(crate::providers::xiaomimimo::reads_images),
800 _ => Some(|_| true),
801 };
802 let images = rule.is_none_or(|rule| {
803 self.facts
804 .reads_images_or(self.provider.dialect.name, model, rule)
805 });
806 crate::completion::Accepts {
807 user_images: images,
808 assistant_images: false,
809 tool_result_images: images,
810 tools: true,
811 foreign_reasoning: false,
813 }
814 }
815
816 fn encodes(&self, _model: &str, media: crate::completion::Media<'_>) -> bool {
821 use crate::completion::{Media, Place};
822 match media {
823 Media::Image(image, Place::User | Place::ToolResult) => image_source(image).is_some(),
824 Media::Document(document) => document_source(document).is_some(),
825 Media::Image(_, Place::Assistant) | Media::Audio(_) | Media::Video(_) => false,
826 }
827 }
828
829 fn normalize_tool_call_id(
831 &self,
832 id: &str,
833 _model: &str,
834 _source: Option<&crate::message::Origin>,
835 ) -> String {
836 crate::providers::internal::wire_ids::legal_call_id(id, 64)
837 }
838
839 fn identity(&self, item: &serde_json::Value) -> serde_json::Map<String, serde_json::Value> {
842 item.get("caller")
843 .filter(|_| item.get("type").and_then(serde_json::Value::as_str) == Some("tool_use"))
844 .map(|caller| serde_json::Map::from_iter([("caller".to_owned(), caller.clone())]))
845 .unwrap_or_default()
846 }
847
848 fn later_system(&self, model: &str) -> crate::completion::LaterSystem {
851 if super::completion::takes_mid_conversation_system(&self.facts, model) {
852 crate::completion::LaterSystem::InPlace
853 } else {
854 crate::completion::LaterSystem::Leading
855 }
856 }
857
858 fn binds_context(&self, model: &str) -> bool {
863 self.provider.dialect.name == ANTHROPIC.name
864 && super::completion::binds_context(&self.facts, model)
865 }
866
867 fn drops_unbound_items(&self, request: &CompletionRequest) -> bool {
872 let model = request.model.as_deref().unwrap_or(&self.model);
873 let thinking = crate::completion::options::param(self, request, "thinking");
874 super::completion::drops_unbound_thinking(self, model, thinking.as_ref())
875 }
876
877 fn sends_alone(&self, block: &crate::message::AssistantContent) -> bool {
879 let ids = crate::providers::internal::wire_ids::WireIds::default();
880 super::completion::assistant_part(block, self, &ids).is_some()
881 }
882
883 fn call_id_slot(&self) -> Option<&'static str> {
884 Some("/id")
885 }
886
887 fn blob_slots(&self) -> &'static [&'static str] {
888 &["/signature", "/data"]
889 }
890
891 fn hosted_pair(
894 &self,
895 item: &serde_json::Value,
896 ) -> Option<(crate::completion::Pairing, String)> {
897 use crate::completion::Pairing;
898 let kind = item.get("type")?.as_str()?;
899 let (side, key) = if kind.ends_with("_tool_use") {
900 (Pairing::Use, "id")
901 } else if kind.ends_with("_tool_result") {
902 (Pairing::Result, "tool_use_id")
903 } else {
904 return None;
905 };
906 Some((side, item.get(key)?.as_str()?.to_owned()))
907 }
908}
909
910#[cfg(test)]
911mod tests;