Skip to main content

rig_core/providers/
zai.rs

1//! Z.AI API clients and Rig integrations.
2//!
3//! Z.AI exposes OpenAI-compatible APIs for both its general platform and
4//! coding-focused platform, plus an Anthropic-compatible endpoint for tools
5//! like Claude Code.
6//!
7//! # OpenAI-compatible example
8//! ```no_run
9//! use rig_core::client::CompletionClient;
10//! use rig_core::providers::zai;
11//!
12//! let client = zai::Client::new("YOUR_API_KEY").expect("Failed to build client");
13//! let glm_4_6 = client.completion_model(zai::GLM_4_6);
14//! ```
15//!
16//! # Anthropic-compatible example
17//! ```no_run
18//! use rig_core::client::CompletionClient;
19//! use rig_core::providers::zai;
20//!
21//! let client = zai::AnthropicClient::new("YOUR_API_KEY").expect("Failed to build client");
22//! let glm_4_6 = client.completion_model(zai::GLM_4_6);
23//! ```
24
25use crate::client::{
26    self, BearerAuth, Capabilities, Capable, DebugExt, Nothing, Provider, ProviderBuilder,
27    ProviderClient,
28};
29use crate::http_client::{self, HttpClientExt};
30use crate::providers::anthropic::client::{
31    AnthropicBuilder as AnthropicCompatBuilder, AnthropicKey, finish_anthropic_builder,
32};
33
34/// General-purpose OpenAI-compatible base URL.
35pub const GENERAL_API_BASE_URL: &str = "https://api.z.ai/api/paas/v4";
36/// Coding-focused OpenAI-compatible base URL.
37pub const CODING_API_BASE_URL: &str = "https://api.z.ai/api/coding/paas/v4";
38/// Anthropic-compatible base URL.
39pub const ANTHROPIC_API_BASE_URL: &str = "https://api.z.ai/api/anthropic";
40
41/// `glm-4.6`
42pub const GLM_4_6: &str = "glm-4.6";
43/// `glm-4.6-air`
44pub const GLM_4_6_AIR: &str = "glm-4.6-air";
45/// `glm-4.6-x`
46pub const GLM_4_6_X: &str = "glm-4.6-x";
47/// `glm-4.5`
48pub const GLM_4_5: &str = "glm-4.5";
49/// `glm-4.5-air`
50pub const GLM_4_5_AIR: &str = "glm-4.5-air";
51/// `glm-4.5v`
52pub const GLM_4_5V: &str = "glm-4.5v";
53/// `glm-4.5-airx`
54pub const GLM_4_5_AIRX: &str = "glm-4.5-airx";
55
56#[derive(Debug, Default, Clone, Copy)]
57pub struct ZAiExt;
58
59#[derive(Debug, Default, Clone, Copy)]
60pub struct ZAiBuilder;
61
62#[derive(Debug, Default, Clone)]
63pub struct ZAiAnthropicBuilder {
64    anthropic: AnthropicCompatBuilder,
65}
66
67#[derive(Debug, Default, Clone, Copy)]
68pub struct ZAiAnthropicExt;
69
70type ZAiApiKey = BearerAuth;
71
72pub type Client<H = reqwest::Client> = client::Client<ZAiExt, H>;
73pub type ClientBuilder<H = crate::markers::Missing> =
74    client::ClientBuilder<ZAiBuilder, ZAiApiKey, H>;
75
76pub type AnthropicClient<H = reqwest::Client> = client::Client<ZAiAnthropicExt, H>;
77pub type AnthropicClientBuilder<H = crate::markers::Missing> =
78    client::ClientBuilder<ZAiAnthropicBuilder, AnthropicKey, H>;
79
80impl Provider for ZAiExt {
81    type Builder = ZAiBuilder;
82
83    const VERIFY_PATH: &'static str = "/models";
84}
85
86impl Provider for ZAiAnthropicExt {
87    type Builder = ZAiAnthropicBuilder;
88
89    const VERIFY_PATH: &'static str = "/v1/models";
90}
91
92impl<H> Capabilities<H> for ZAiExt {
93    type Completion = Capable<super::openai::completion::GenericCompletionModel<ZAiExt, H>>;
94    type Embeddings = Nothing;
95    type Transcription = Nothing;
96    type ModelListing = Nothing;
97    #[cfg(feature = "image")]
98    type ImageGeneration = Nothing;
99    #[cfg(feature = "audio")]
100    type AudioGeneration = Nothing;
101    type Rerank = Nothing;
102}
103
104impl<H> Capabilities<H> for ZAiAnthropicExt {
105    type Completion =
106        Capable<super::anthropic::completion::GenericCompletionModel<ZAiAnthropicExt, H>>;
107    type Embeddings = Nothing;
108    type Transcription = Nothing;
109    type ModelListing = Nothing;
110    #[cfg(feature = "image")]
111    type ImageGeneration = Nothing;
112    #[cfg(feature = "audio")]
113    type AudioGeneration = Nothing;
114    type Rerank = Nothing;
115}
116
117impl DebugExt for ZAiExt {}
118impl DebugExt for ZAiAnthropicExt {}
119
120impl ProviderBuilder for ZAiBuilder {
121    type Extension<H>
122        = ZAiExt
123    where
124        H: HttpClientExt;
125    type ApiKey = ZAiApiKey;
126
127    const BASE_URL: &'static str = GENERAL_API_BASE_URL;
128
129    fn build<H>(
130        _builder: &client::ClientBuilder<Self, Self::ApiKey, H>,
131    ) -> http_client::Result<Self::Extension<H>>
132    where
133        H: HttpClientExt,
134    {
135        Ok(ZAiExt)
136    }
137}
138
139impl ProviderBuilder for ZAiAnthropicBuilder {
140    type Extension<H>
141        = ZAiAnthropicExt
142    where
143        H: HttpClientExt;
144    type ApiKey = AnthropicKey;
145
146    const BASE_URL: &'static str = ANTHROPIC_API_BASE_URL;
147
148    fn build<H>(
149        _builder: &client::ClientBuilder<Self, Self::ApiKey, H>,
150    ) -> http_client::Result<Self::Extension<H>>
151    where
152        H: HttpClientExt,
153    {
154        Ok(ZAiAnthropicExt)
155    }
156
157    fn finish<H>(
158        &self,
159        builder: client::ClientBuilder<Self, AnthropicKey, H>,
160    ) -> http_client::Result<client::ClientBuilder<Self, AnthropicKey, H>> {
161        finish_anthropic_builder(&self.anthropic, builder)
162    }
163}
164
165impl super::anthropic::completion::AnthropicCompatibleProvider for ZAiAnthropicExt {
166    const PROVIDER_NAME: &'static str = "z.ai";
167
168    fn default_max_tokens(_model: &str) -> Option<u64> {
169        Some(4096)
170    }
171}
172
173impl ProviderClient for Client {
174    type Input = ZAiApiKey;
175    type Error = crate::client::ProviderClientError;
176
177    fn from_env() -> Result<Self, Self::Error> {
178        let api_key = crate::client::required_env_var("ZAI_API_KEY")?;
179        let mut builder = Self::builder().api_key(api_key);
180
181        if let Some(base_url) = crate::client::optional_env_var("ZAI_API_BASE")? {
182            builder = builder.base_url(base_url);
183        }
184
185        builder.build().map_err(Into::into)
186    }
187
188    fn from_val(input: Self::Input) -> Result<Self, Self::Error> {
189        Self::new(input).map_err(Into::into)
190    }
191}
192
193impl ProviderClient for AnthropicClient {
194    type Input = String;
195    type Error = crate::client::ProviderClientError;
196
197    fn from_env() -> Result<Self, Self::Error> {
198        let api_key = crate::client::required_env_var("ZAI_API_KEY")?;
199        let mut builder = Self::builder().api_key(api_key);
200
201        if let Some(base_url) = anthropic_base_override("ZAI_ANTHROPIC_API_BASE", "ZAI_API_BASE")? {
202            builder = builder.base_url(base_url);
203        }
204
205        builder.build().map_err(Into::into)
206    }
207
208    fn from_val(input: Self::Input) -> Result<Self, Self::Error> {
209        Self::builder().api_key(input).build().map_err(Into::into)
210    }
211}
212
213fn anthropic_base_override(
214    primary_env: &'static str,
215    fallback_env: &'static str,
216) -> crate::client::ProviderClientResult<Option<String>> {
217    let primary = crate::client::optional_env_var(primary_env)?;
218    let fallback = crate::client::optional_env_var(fallback_env)?;
219
220    Ok(resolve_anthropic_base_override(
221        primary.as_deref(),
222        fallback.as_deref(),
223    ))
224}
225
226fn resolve_anthropic_base_override(
227    primary: Option<&str>,
228    fallback: Option<&str>,
229) -> Option<String> {
230    primary
231        .map(str::to_owned)
232        .or_else(|| fallback.and_then(normalize_anthropic_base_url))
233}
234
235fn normalize_anthropic_base_url(base_url: &str) -> Option<String> {
236    if base_url.contains("/anthropic") {
237        return Some(base_url.to_owned());
238    }
239
240    match base_url.trim_end_matches('/') {
241        GENERAL_API_BASE_URL | CODING_API_BASE_URL => Some(ANTHROPIC_API_BASE_URL.to_owned()),
242        _ => {
243            let mut url = url::Url::parse(base_url).ok()?;
244            if !matches!(
245                url.path(),
246                "/api/paas/v4" | "/api/paas/v4/" | "/api/coding/paas/v4" | "/api/coding/paas/v4/"
247            ) {
248                return None;
249            }
250            url.set_path("/api/anthropic");
251            Some(url.to_string())
252        }
253    }
254}
255
256impl<H> ClientBuilder<H> {
257    pub fn general(self) -> Self {
258        self.base_url(GENERAL_API_BASE_URL)
259    }
260
261    pub fn coding(self) -> Self {
262        self.base_url(CODING_API_BASE_URL)
263    }
264}
265
266impl<H> AnthropicClientBuilder<H> {
267    pub fn general(self) -> Self {
268        self.base_url(ANTHROPIC_API_BASE_URL)
269    }
270
271    pub fn anthropic_version(self, anthropic_version: &str) -> Self {
272        self.over_ext(|mut ext| {
273            ext.anthropic.anthropic_version = anthropic_version.into();
274            ext
275        })
276    }
277
278    pub fn anthropic_betas(self, anthropic_betas: &[&str]) -> Self {
279        self.over_ext(|mut ext| {
280            ext.anthropic
281                .anthropic_betas
282                .extend(anthropic_betas.iter().copied().map(String::from));
283            ext
284        })
285    }
286
287    pub fn anthropic_beta(self, anthropic_beta: &str) -> Self {
288        self.over_ext(|mut ext| {
289            ext.anthropic.anthropic_betas.push(anthropic_beta.into());
290            ext
291        })
292    }
293}
294
295#[cfg(test)]
296mod tests {
297    use super::{
298        ANTHROPIC_API_BASE_URL, CODING_API_BASE_URL, GENERAL_API_BASE_URL,
299        normalize_anthropic_base_url, resolve_anthropic_base_override,
300    };
301
302    #[test]
303    fn test_client_initialization() {
304        let _client = crate::providers::zai::Client::new("dummy-key").expect("Client::new()");
305        let _client_from_builder = crate::providers::zai::Client::builder()
306            .api_key("dummy-key")
307            .build()
308            .expect("Client::builder()");
309        let _anthropic_client = crate::providers::zai::AnthropicClient::new("dummy-key")
310            .expect("AnthropicClient::new()");
311        let _anthropic_client_from_builder = crate::providers::zai::AnthropicClient::builder()
312            .api_key("dummy-key")
313            .build()
314            .expect("AnthropicClient::builder()");
315    }
316
317    #[test]
318    fn normalize_openai_style_bases_to_anthropic_base() {
319        assert_eq!(
320            normalize_anthropic_base_url(GENERAL_API_BASE_URL).as_deref(),
321            Some(ANTHROPIC_API_BASE_URL)
322        );
323        assert_eq!(
324            normalize_anthropic_base_url(CODING_API_BASE_URL).as_deref(),
325            Some(ANTHROPIC_API_BASE_URL)
326        );
327        assert_eq!(
328            normalize_anthropic_base_url("https://proxy.example.com/api/paas/v4").as_deref(),
329            Some("https://proxy.example.com/api/anthropic")
330        );
331        assert_eq!(
332            normalize_anthropic_base_url("https://proxy.example.com/api/coding/paas/v4").as_deref(),
333            Some("https://proxy.example.com/api/anthropic")
334        );
335    }
336
337    #[test]
338    fn normalize_preserves_existing_anthropic_base() {
339        assert_eq!(
340            normalize_anthropic_base_url("https://proxy.example.com/api/anthropic").as_deref(),
341            Some("https://proxy.example.com/api/anthropic")
342        );
343    }
344
345    #[test]
346    fn anthropic_primary_override_wins() {
347        let override_url = resolve_anthropic_base_override(
348            Some("https://primary.example.com/api/anthropic"),
349            Some(GENERAL_API_BASE_URL),
350        );
351
352        assert_eq!(
353            override_url.as_deref(),
354            Some("https://primary.example.com/api/anthropic")
355        );
356    }
357}