Skip to main content

nenjo_models/
compatible.rs

1//! Generic OpenAI-compatible provider.
2//! Most LLM APIs follow the same `/v1/chat/completions` format.
3//! This module provides a single implementation that works for all of them.
4
5use crate::ToolSpec;
6use crate::traits::{ChatMessage, ChatRequest, ChatResponse, ModelProvider, TokenUsage, ToolCall};
7use async_trait::async_trait;
8use reqwest::Client;
9use serde::{Deserialize, Serialize};
10use tracing::warn;
11
12/// A provider that speaks the OpenAI-compatible chat completions API.
13/// Used by: Venice, Vercel AI Gateway, Cloudflare AI Gateway, Moonshot,
14/// Synthetic, `OpenCode` Zen, `Z.AI`, `GLM`, `MiniMax`, Bedrock, Qianfan, Groq, Mistral, `xAI`, etc.
15pub struct OpenAiCompatibleProvider {
16    pub(crate) name: String,
17    pub(crate) base_url: String,
18    pub(crate) api_key: Option<String>,
19    pub(crate) auth_header: AuthStyle,
20    /// When false, do not fall back to /v1/responses on chat completions 404.
21    /// GLM/Zhipu does not support the responses API.
22    supports_responses_fallback: bool,
23    client: Client,
24}
25
26/// How the provider expects the API key to be sent.
27#[derive(Debug, Clone)]
28pub enum AuthStyle {
29    /// `Authorization: Bearer <key>`
30    Bearer,
31    /// `x-api-key: <key>` (used by some Chinese providers)
32    XApiKey,
33    /// Custom header name
34    Custom(String),
35}
36
37impl OpenAiCompatibleProvider {
38    pub fn new(name: &str, base_url: &str, api_key: Option<&str>, auth_style: AuthStyle) -> Self {
39        Self {
40            name: name.to_string(),
41            base_url: base_url.trim_end_matches('/').to_string(),
42            api_key: api_key.map(ToString::to_string),
43            auth_header: auth_style,
44            supports_responses_fallback: true,
45            client: Client::builder()
46                .timeout(std::time::Duration::from_secs(120))
47                .connect_timeout(std::time::Duration::from_secs(10))
48                .build()
49                .unwrap_or_else(|_| Client::new()),
50        }
51    }
52
53    /// Same as `new` but skips the /v1/responses fallback on 404.
54    /// Use for providers (e.g. GLM) that only support chat completions.
55    pub fn new_no_responses_fallback(
56        name: &str,
57        base_url: &str,
58        api_key: Option<&str>,
59        auth_style: AuthStyle,
60    ) -> Self {
61        Self {
62            name: name.to_string(),
63            base_url: base_url.trim_end_matches('/').to_string(),
64            api_key: api_key.map(ToString::to_string),
65            auth_header: auth_style,
66            supports_responses_fallback: false,
67            client: Client::builder()
68                .timeout(std::time::Duration::from_secs(120))
69                .connect_timeout(std::time::Duration::from_secs(10))
70                .build()
71                .unwrap_or_else(|_| Client::new()),
72        }
73    }
74
75    /// Build the full URL for chat completions, detecting if base_url already includes the path.
76    /// This allows custom providers with non-standard endpoints (e.g., VolcEngine ARK uses
77    /// `/api/coding/v3/chat/completions` instead of `/v1/chat/completions`).
78    fn chat_completions_url(&self) -> String {
79        let path = reqwest::Url::parse(&self.base_url)
80            .map(|url| url.path().trim_end_matches('/').to_string())
81            .unwrap_or_else(|_| self.base_url.trim_end_matches('/').to_string());
82
83        // If the base URL already contains a full chat endpoint path, use as-is.
84        // Covers standard `/chat/completions` and provider-specific paths like
85        // MiniMax's `/text/chatcompletion_v2`.
86        let has_full_endpoint =
87            path.ends_with("/chat/completions") || path.contains("/chatcompletion");
88
89        if has_full_endpoint {
90            self.base_url.clone()
91        } else {
92            format!("{}/chat/completions", self.base_url)
93        }
94    }
95
96    fn path_ends_with(&self, suffix: &str) -> bool {
97        if let Ok(url) = reqwest::Url::parse(&self.base_url) {
98            return url.path().trim_end_matches('/').ends_with(suffix);
99        }
100
101        self.base_url.trim_end_matches('/').ends_with(suffix)
102    }
103
104    fn has_explicit_api_path(&self) -> bool {
105        let Ok(url) = reqwest::Url::parse(&self.base_url) else {
106            return false;
107        };
108
109        let path = url.path().trim_end_matches('/');
110        !path.is_empty() && path != "/"
111    }
112
113    /// Build the full URL for responses API, detecting if base_url already includes the path.
114    fn responses_url(&self) -> String {
115        if self.path_ends_with("/responses") {
116            return self.base_url.clone();
117        }
118
119        let normalized_base = self.base_url.trim_end_matches('/');
120
121        // If chat endpoint is explicitly configured, derive sibling responses endpoint.
122        if let Some(prefix) = normalized_base.strip_suffix("/chat/completions") {
123            return format!("{prefix}/responses");
124        }
125
126        // If an explicit API path already exists (e.g. /v1, /openai, /api/coding/v3),
127        // append responses directly to avoid duplicate /v1 segments.
128        if self.has_explicit_api_path() {
129            format!("{normalized_base}/responses")
130        } else {
131            format!("{normalized_base}/v1/responses")
132        }
133    }
134}
135
136#[derive(Debug, Serialize)]
137struct NativeChatRequest {
138    model: String,
139    messages: Vec<Message>,
140    temperature: f64,
141    #[serde(skip_serializing_if = "Option::is_none")]
142    stream: Option<bool>,
143    #[serde(skip_serializing_if = "Option::is_none")]
144    tools: Option<Vec<NativeToolSpec>>,
145    #[serde(skip_serializing_if = "Option::is_none")]
146    tool_choice: Option<String>,
147}
148
149#[derive(Debug, Serialize)]
150struct Message {
151    role: String,
152    #[serde(skip_serializing_if = "Option::is_none")]
153    content: Option<String>,
154    #[serde(skip_serializing_if = "Option::is_none")]
155    tool_call_id: Option<String>,
156    #[serde(skip_serializing_if = "Option::is_none")]
157    tool_calls: Option<Vec<NativeToolCall>>,
158}
159
160#[derive(Debug, Serialize)]
161struct NativeToolSpec {
162    #[serde(rename = "type")]
163    kind: String,
164    function: NativeToolFunctionSpec,
165}
166
167#[derive(Debug, Serialize)]
168struct NativeToolFunctionSpec {
169    name: String,
170    description: String,
171    parameters: serde_json::Value,
172}
173
174#[derive(Debug, Serialize, Deserialize)]
175struct NativeToolCall {
176    #[serde(skip_serializing_if = "Option::is_none")]
177    id: Option<String>,
178    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
179    kind: Option<String>,
180    function: NativeFunctionCall,
181}
182
183#[derive(Debug, Serialize, Deserialize)]
184struct NativeFunctionCall {
185    name: String,
186    arguments: String,
187}
188
189#[derive(Debug, Deserialize)]
190struct NativeUsage {
191    #[serde(default)]
192    prompt_tokens: u64,
193    #[serde(default)]
194    completion_tokens: u64,
195}
196
197#[derive(Debug, Deserialize)]
198struct ApiChatResponse {
199    choices: Vec<Choice>,
200    #[serde(default)]
201    usage: Option<NativeUsage>,
202}
203
204#[derive(Debug, Deserialize)]
205struct Choice {
206    message: ResponseMessage,
207}
208
209#[derive(Debug, Deserialize, Serialize)]
210struct ResponseMessage {
211    #[serde(default)]
212    content: Option<String>,
213    #[serde(default)]
214    tool_calls: Option<Vec<ResponseToolCall>>,
215}
216
217#[derive(Debug, Deserialize, Serialize)]
218struct ResponseToolCall {
219    #[serde(default)]
220    id: Option<String>,
221    #[serde(rename = "type")]
222    kind: Option<String>,
223    function: Option<ResponseFunction>,
224}
225
226#[derive(Debug, Deserialize, Serialize)]
227struct ResponseFunction {
228    name: Option<String>,
229    arguments: Option<String>,
230}
231
232#[derive(Debug, Serialize)]
233struct ResponsesRequest {
234    model: String,
235    input: Vec<ResponsesInput>,
236    #[serde(skip_serializing_if = "Option::is_none")]
237    instructions: Option<String>,
238    #[serde(skip_serializing_if = "Option::is_none")]
239    stream: Option<bool>,
240}
241
242#[derive(Debug, Serialize)]
243struct ResponsesInput {
244    role: String,
245    content: String,
246}
247
248#[derive(Debug, Deserialize)]
249struct ResponsesResponse {
250    #[serde(default)]
251    output: Vec<ResponsesOutput>,
252    #[serde(default)]
253    output_text: Option<String>,
254}
255
256#[derive(Debug, Deserialize)]
257struct ResponsesOutput {
258    #[serde(default)]
259    content: Vec<ResponsesContent>,
260}
261
262#[derive(Debug, Deserialize)]
263struct ResponsesContent {
264    #[serde(rename = "type")]
265    kind: Option<String>,
266    text: Option<String>,
267}
268
269fn first_nonempty(text: Option<&str>) -> Option<String> {
270    text.and_then(|value| {
271        let trimmed = value.trim();
272        if trimmed.is_empty() {
273            None
274        } else {
275            Some(trimmed.to_string())
276        }
277    })
278}
279
280fn extract_responses_text(response: ResponsesResponse) -> Option<String> {
281    if let Some(text) = first_nonempty(response.output_text.as_deref()) {
282        return Some(text);
283    }
284
285    for item in &response.output {
286        for content in &item.content {
287            if content.kind.as_deref() == Some("output_text")
288                && let Some(text) = first_nonempty(content.text.as_deref())
289            {
290                return Some(text);
291            }
292        }
293    }
294
295    for item in &response.output {
296        for content in &item.content {
297            if let Some(text) = first_nonempty(content.text.as_deref()) {
298                return Some(text);
299            }
300        }
301    }
302
303    None
304}
305
306impl OpenAiCompatibleProvider {
307    fn apply_auth_header(
308        &self,
309        req: reqwest::RequestBuilder,
310        api_key: &str,
311    ) -> reqwest::RequestBuilder {
312        match &self.auth_header {
313            AuthStyle::Bearer => req.header("Authorization", format!("Bearer {api_key}")),
314            AuthStyle::XApiKey => req.header("x-api-key", api_key),
315            AuthStyle::Custom(header) => req.header(header, api_key),
316        }
317    }
318
319    fn convert_tools(tools: Option<&[ToolSpec]>) -> Option<Vec<NativeToolSpec>> {
320        tools.map(|items| {
321            items
322                .iter()
323                .map(|tool| NativeToolSpec {
324                    kind: "function".to_string(),
325                    function: NativeToolFunctionSpec {
326                        name: crate::sanitize_tool_name(&tool.name),
327                        description: tool.description.clone(),
328                        parameters: tool.parameters.clone(),
329                    },
330                })
331                .collect()
332        })
333    }
334
335    /// Reconstruct native OpenAI-compatible messages from the turn loop's
336    /// JSON-encoded `ChatMessage` values.
337    ///
338    /// Assistant messages with `tool_calls` JSON are converted into native
339    /// tool call messages. Tool result messages with `tool_call_id` JSON
340    /// are converted into native tool result messages.
341    fn convert_messages(messages: &[ChatMessage]) -> Vec<Message> {
342        messages
343            .iter()
344            .map(|m| {
345                // Assistant message with tool calls encoded as JSON
346                if m.role == "assistant"
347                    && let Ok(value) = serde_json::from_str::<serde_json::Value>(&m.content)
348                    && let Some(tool_calls_value) = value.get("tool_calls")
349                    && let Ok(parsed_calls) =
350                        serde_json::from_value::<Vec<ToolCall>>(tool_calls_value.clone())
351                {
352                    let tool_calls = parsed_calls
353                        .into_iter()
354                        .map(|tc| NativeToolCall {
355                            id: Some(tc.id),
356                            kind: Some("function".to_string()),
357                            function: NativeFunctionCall {
358                                name: tc.name,
359                                arguments: tc.arguments,
360                            },
361                        })
362                        .collect::<Vec<_>>();
363                    let content = value
364                        .get("content")
365                        .and_then(serde_json::Value::as_str)
366                        .map(ToString::to_string);
367                    return Message {
368                        role: "assistant".to_string(),
369                        content,
370                        tool_call_id: None,
371                        tool_calls: Some(tool_calls),
372                    };
373                }
374
375                // Tool result message with tool_call_id encoded as JSON
376                if m.role == "tool"
377                    && let Ok(value) = serde_json::from_str::<serde_json::Value>(&m.content)
378                {
379                    let tool_call_id = value
380                        .get("tool_call_id")
381                        .and_then(serde_json::Value::as_str)
382                        .map(ToString::to_string);
383                    let content = value
384                        .get("content")
385                        .and_then(serde_json::Value::as_str)
386                        .map(ToString::to_string);
387                    return Message {
388                        role: "tool".to_string(),
389                        content,
390                        tool_call_id,
391                        tool_calls: None,
392                    };
393                }
394
395                // Regular message (system, user, plain assistant)
396                Message {
397                    role: m.role.clone(),
398                    content: Some(m.content.clone()),
399                    tool_call_id: None,
400                    tool_calls: None,
401                }
402            })
403            .collect()
404    }
405
406    async fn chat_via_responses(
407        &self,
408        api_key: &str,
409        system_prompt: Option<&str>,
410        message: &str,
411        model: &str,
412    ) -> anyhow::Result<String> {
413        let request = ResponsesRequest {
414            model: model.to_string(),
415            input: vec![ResponsesInput {
416                role: "user".to_string(),
417                content: message.to_string(),
418            }],
419            instructions: system_prompt.map(str::to_string),
420            stream: Some(false),
421        };
422
423        let url = self.responses_url();
424
425        let response = self
426            .apply_auth_header(self.client.post(&url).json(&request), api_key)
427            .send()
428            .await?;
429
430        if !response.status().is_success() {
431            let error = response.text().await?;
432            anyhow::bail!("{} Responses API error: {error}", self.name);
433        }
434
435        let responses: ResponsesResponse = response.json().await?;
436
437        extract_responses_text(responses)
438            .ok_or_else(|| anyhow::anyhow!("No response from {} Responses API", self.name))
439    }
440}
441
442#[async_trait]
443impl ModelProvider for OpenAiCompatibleProvider {
444    async fn chat(
445        &self,
446        request: ChatRequest<'_>,
447        model: &str,
448        temperature: f64,
449    ) -> anyhow::Result<ChatResponse> {
450        let api_key = self.api_key.as_ref().ok_or_else(|| {
451            anyhow::anyhow!(
452                "{} API key not set. Run `nenjo onboard` or set the appropriate env var.",
453                self.name
454            )
455        })?;
456
457        let tools = Self::convert_tools(request.tools);
458        let chat_request = NativeChatRequest {
459            model: model.to_string(),
460            messages: Self::convert_messages(request.messages),
461            temperature,
462            stream: Some(false),
463            tool_choice: tools.as_ref().map(|_| "auto".to_string()),
464            tools,
465        };
466
467        let url = self.chat_completions_url();
468        let response = self
469            .apply_auth_header(self.client.post(&url).json(&chat_request), api_key)
470            .send()
471            .await?;
472
473        if !response.status().is_success() {
474            let status = response.status();
475
476            // 404 may mean this provider uses the Responses API instead
477            if status == reqwest::StatusCode::NOT_FOUND && self.supports_responses_fallback {
478                warn!(
479                    provider = %self.name,
480                    url = %url,
481                    "Chat completions returned 404 — falling back to Responses API (tool calls will be unavailable)"
482                );
483                let system = request.messages.iter().find(|m| m.role == "system");
484                let last_user = request.messages.iter().rfind(|m| m.role == "user");
485                if let Some(user_msg) = last_user {
486                    let text = self
487                        .chat_via_responses(
488                            api_key,
489                            system.map(|m| m.content.as_str()),
490                            &user_msg.content,
491                            model,
492                        )
493                        .await
494                        .map_err(|responses_err| {
495                            anyhow::anyhow!(
496                                "{} API error (chat completions unavailable; responses fallback failed: {responses_err})",
497                                self.name
498                            )
499                        })?;
500                    return Ok(ChatResponse {
501                        text: Some(text),
502                        tool_calls: vec![],
503                        provider_tool_calls: vec![],
504                        usage: TokenUsage::default(),
505                    });
506                }
507            }
508
509            return Err(crate::api_error(&self.name, response).await);
510        }
511
512        let body_text = response.text().await?;
513
514        // Some providers (e.g. OpenRouter routing to Clarifai) return HTTP 200
515        // with an error payload instead of a valid chat completion.
516        if let Ok(value) = serde_json::from_str::<serde_json::Value>(&body_text)
517            && let Some(err) = value.get("error")
518        {
519            let msg = err
520                .get("message")
521                .and_then(serde_json::Value::as_str)
522                .unwrap_or("unknown error");
523            return Err(anyhow::anyhow!(
524                "{} returned an error in a 200 response: {msg}",
525                self.name
526            ));
527        }
528
529        let chat_response: ApiChatResponse = serde_json::from_str(&body_text).map_err(|e| {
530            anyhow::anyhow!(
531                "{} response decode error: {e}\nBody: {}",
532                self.name,
533                &body_text[..body_text.len().min(500)]
534            )
535        })?;
536
537        let usage = chat_response
538            .usage
539            .map(|u| TokenUsage {
540                input_tokens: u.prompt_tokens,
541                output_tokens: u.completion_tokens,
542            })
543            .unwrap_or_default();
544
545        let message = chat_response
546            .choices
547            .into_iter()
548            .next()
549            .ok_or_else(|| anyhow::anyhow!("No response from {}", self.name))?
550            .message;
551
552        let tool_calls = message
553            .tool_calls
554            .unwrap_or_default()
555            .into_iter()
556            .filter_map(|tc| {
557                let function = tc.function?;
558                let name = function.name?;
559                let arguments = function.arguments.unwrap_or_else(|| "{}".to_string());
560                Some(ToolCall {
561                    id: tc.id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
562                    name,
563                    arguments,
564                })
565            })
566            .collect::<Vec<_>>();
567
568        Ok(ChatResponse {
569            text: message.content,
570            tool_calls,
571            provider_tool_calls: vec![],
572            usage,
573        })
574    }
575
576    fn context_window(&self, model: &str) -> Option<usize> {
577        let m = model.to_lowercase();
578        // Match known models served through OpenAI-compatible endpoints.
579        if m.contains("deepseek") {
580            Some(128_000)
581        } else if m.contains("mistral-large") || m.contains("mistral-medium") {
582            Some(256_000)
583        } else if m.contains("mistral") {
584            Some(128_000)
585        } else if m.contains("qwen") {
586            Some(256_000)
587        } else if m.contains("grok-4") && (m.contains("fast") || m.contains("4.1")) {
588            Some(2_000_000)
589        } else if m.contains("grok-4") {
590            Some(256_000)
591        } else if m.contains("grok-3") || m.contains("llama-4") || m.contains("llama4") {
592            Some(1_000_000)
593        } else if m.contains("llama-3") || m.contains("llama3") {
594            Some(128_000)
595        } else if m.contains("kimi") || m.contains("moonshot") {
596            Some(256_000)
597        } else if m.contains("minimax") {
598            Some(200_000)
599        } else {
600            // Unknown model on a compatible endpoint — no opinion
601            None
602        }
603    }
604
605    fn supports_native_tools(&self) -> bool {
606        true
607    }
608
609    fn supports_developer_role(&self, model: &str) -> bool {
610        let m = model.to_lowercase();
611        m.starts_with("o1")
612            || m.starts_with("o3")
613            || m.starts_with("o4")
614            || m.starts_with("gpt-5")
615            || m.starts_with("gpt-4.5")
616            || m.starts_with("gpt-4.1")
617    }
618}
619
620#[cfg(test)]
621mod tests {
622    use super::*;
623
624    fn make_provider(name: &str, url: &str, key: Option<&str>) -> OpenAiCompatibleProvider {
625        OpenAiCompatibleProvider::new(name, url, key, AuthStyle::Bearer)
626    }
627
628    #[test]
629    fn creates_with_key() {
630        let p = make_provider("venice", "https://api.venice.ai", Some("vn-key"));
631        assert_eq!(p.name, "venice");
632        assert_eq!(p.base_url, "https://api.venice.ai");
633        assert_eq!(p.api_key.as_deref(), Some("vn-key"));
634    }
635
636    #[test]
637    fn creates_without_key() {
638        let p = make_provider("test", "https://example.com", None);
639        assert!(p.api_key.is_none());
640    }
641
642    #[test]
643    fn strips_trailing_slash() {
644        let p = make_provider("test", "https://example.com/", None);
645        assert_eq!(p.base_url, "https://example.com");
646    }
647
648    #[test]
649    fn developer_role_supported_for_openai_style_newer_models() {
650        let p = make_provider("OpenAI-compatible", "https://example.com", None);
651        assert!(p.supports_developer_role("gpt-5.1"));
652        assert!(p.supports_developer_role("gpt-4.1"));
653        assert!(p.supports_developer_role("o4-mini"));
654        assert!(!p.supports_developer_role("gpt-4o"));
655        assert!(!p.supports_developer_role("llama-3.3-70b"));
656    }
657
658    #[tokio::test]
659    async fn chat_fails_without_key() {
660        use crate::traits::{ChatMessage, ChatRequest};
661        let p = make_provider("Venice", "https://api.venice.ai", None);
662        let messages = vec![ChatMessage::user("hello")];
663        let request = ChatRequest {
664            messages: &messages,
665            tools: None,
666            native_tools: None,
667        };
668        let result = p.chat(request, "llama-3.3-70b", 0.7).await;
669        assert!(result.is_err());
670        assert!(
671            result
672                .unwrap_err()
673                .to_string()
674                .contains("Venice API key not set")
675        );
676    }
677
678    #[test]
679    fn request_serializes_correctly() {
680        let req = NativeChatRequest {
681            model: "llama-3.3-70b".to_string(),
682            messages: vec![
683                Message {
684                    role: "system".to_string(),
685                    content: Some("You are Nenjo".to_string()),
686                    tool_call_id: None,
687                    tool_calls: None,
688                },
689                Message {
690                    role: "user".to_string(),
691                    content: Some("hello".to_string()),
692                    tool_call_id: None,
693                    tool_calls: None,
694                },
695            ],
696            temperature: 0.4,
697            stream: Some(false),
698            tools: None,
699            tool_choice: None,
700        };
701        let json = serde_json::to_string(&req).unwrap();
702        assert!(json.contains("llama-3.3-70b"));
703        assert!(json.contains("system"));
704        assert!(json.contains("user"));
705        // Optional fields should be omitted
706        assert!(!json.contains("tool_call_id"));
707        assert!(!json.contains("tool_calls"));
708        assert!(!json.contains("tool_choice"));
709    }
710
711    #[test]
712    fn response_deserializes() {
713        let json = r#"{"choices":[{"message":{"content":"Hello from Venice!"}}]}"#;
714        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
715        assert_eq!(
716            resp.choices[0].message.content,
717            Some("Hello from Venice!".to_string())
718        );
719    }
720
721    #[test]
722    fn response_empty_choices() {
723        let json = r#"{"choices":[]}"#;
724        let resp: ApiChatResponse = serde_json::from_str(json).unwrap();
725        assert!(resp.choices.is_empty());
726    }
727
728    #[test]
729    fn x_api_key_auth_style() {
730        let p = OpenAiCompatibleProvider::new(
731            "moonshot",
732            "https://api.moonshot.cn",
733            Some("ms-key"),
734            AuthStyle::XApiKey,
735        );
736        assert!(matches!(p.auth_header, AuthStyle::XApiKey));
737    }
738
739    #[test]
740    fn custom_auth_style() {
741        let p = OpenAiCompatibleProvider::new(
742            "custom",
743            "https://api.example.com",
744            Some("key"),
745            AuthStyle::Custom("X-Custom-Key".into()),
746        );
747        assert!(matches!(p.auth_header, AuthStyle::Custom(_)));
748    }
749
750    #[tokio::test]
751    async fn all_compatible_providers_fail_without_key() {
752        use crate::traits::{ChatMessage, ChatRequest};
753        let providers = vec![
754            make_provider("Venice", "https://api.venice.ai", None),
755            make_provider("Moonshot", "https://api.moonshot.cn", None),
756            make_provider("GLM", "https://open.bigmodel.cn", None),
757            make_provider("MiniMax", "https://api.minimax.io/v1", None),
758            make_provider("Groq", "https://api.groq.com/openai", None),
759            make_provider("Mistral", "https://api.mistral.ai", None),
760            make_provider("xAI", "https://api.x.ai", None),
761        ];
762
763        for p in providers {
764            let messages = vec![ChatMessage::user("test")];
765            let request = ChatRequest {
766                messages: &messages,
767                tools: None,
768                native_tools: None,
769            };
770            let result = p.chat(request, "model", 0.7).await;
771            assert!(result.is_err(), "{} should fail without key", p.name);
772            assert!(
773                result.unwrap_err().to_string().contains("API key not set"),
774                "{} error should mention key",
775                p.name
776            );
777        }
778    }
779
780    #[test]
781    fn responses_extracts_top_level_output_text() {
782        let json = r#"{"output_text":"Hello from top-level","output":[]}"#;
783        let response: ResponsesResponse = serde_json::from_str(json).unwrap();
784        assert_eq!(
785            extract_responses_text(response).as_deref(),
786            Some("Hello from top-level")
787        );
788    }
789
790    #[test]
791    fn responses_extracts_nested_output_text() {
792        let json =
793            r#"{"output":[{"content":[{"type":"output_text","text":"Hello from nested"}]}]}"#;
794        let response: ResponsesResponse = serde_json::from_str(json).unwrap();
795        assert_eq!(
796            extract_responses_text(response).as_deref(),
797            Some("Hello from nested")
798        );
799    }
800
801    #[test]
802    fn responses_extracts_any_text_as_fallback() {
803        let json = r#"{"output":[{"content":[{"type":"message","text":"Fallback text"}]}]}"#;
804        let response: ResponsesResponse = serde_json::from_str(json).unwrap();
805        assert_eq!(
806            extract_responses_text(response).as_deref(),
807            Some("Fallback text")
808        );
809    }
810
811    // ══════════════════════════════════════════════════════════
812    // Custom endpoint path tests (Issue #114)
813    // ══════════════════════════════════════════════════════════
814
815    #[test]
816    fn chat_completions_url_standard_openai() {
817        // Standard OpenAI-compatible providers get /chat/completions appended
818        let p = make_provider("openai", "https://api.openai.com/v1", None);
819        assert_eq!(
820            p.chat_completions_url(),
821            "https://api.openai.com/v1/chat/completions"
822        );
823    }
824
825    #[test]
826    fn chat_completions_url_trailing_slash() {
827        // Trailing slash is stripped, then /chat/completions appended
828        let p = make_provider("test", "https://api.example.com/v1/", None);
829        assert_eq!(
830            p.chat_completions_url(),
831            "https://api.example.com/v1/chat/completions"
832        );
833    }
834
835    #[test]
836    fn chat_completions_url_volcengine_ark() {
837        // VolcEngine ARK uses custom path - should use as-is
838        let p = make_provider(
839            "volcengine",
840            "https://ark.cn-beijing.volces.com/api/coding/v3/chat/completions",
841            None,
842        );
843        assert_eq!(
844            p.chat_completions_url(),
845            "https://ark.cn-beijing.volces.com/api/coding/v3/chat/completions"
846        );
847    }
848
849    #[test]
850    fn chat_completions_url_custom_full_endpoint() {
851        // Custom provider with full endpoint path
852        let p = make_provider(
853            "custom",
854            "https://my-api.example.com/v2/llm/chat/completions",
855            None,
856        );
857        assert_eq!(
858            p.chat_completions_url(),
859            "https://my-api.example.com/v2/llm/chat/completions"
860        );
861    }
862
863    #[test]
864    fn chat_completions_url_requires_exact_suffix_match() {
865        let p = make_provider(
866            "custom",
867            "https://my-api.example.com/v2/llm/chat/completions-proxy",
868            None,
869        );
870        assert_eq!(
871            p.chat_completions_url(),
872            "https://my-api.example.com/v2/llm/chat/completions-proxy/chat/completions"
873        );
874    }
875
876    #[test]
877    fn responses_url_standard() {
878        // Standard providers get /v1/responses appended
879        let p = make_provider("test", "https://api.example.com", None);
880        assert_eq!(p.responses_url(), "https://api.example.com/v1/responses");
881    }
882
883    #[test]
884    fn responses_url_custom_full_endpoint() {
885        // Custom provider with full responses endpoint
886        let p = make_provider(
887            "custom",
888            "https://my-api.example.com/api/v2/responses",
889            None,
890        );
891        assert_eq!(
892            p.responses_url(),
893            "https://my-api.example.com/api/v2/responses"
894        );
895    }
896
897    #[test]
898    fn responses_url_requires_exact_suffix_match() {
899        let p = make_provider(
900            "custom",
901            "https://my-api.example.com/api/v2/responses-proxy",
902            None,
903        );
904        assert_eq!(
905            p.responses_url(),
906            "https://my-api.example.com/api/v2/responses-proxy/responses"
907        );
908    }
909
910    #[test]
911    fn responses_url_derives_from_chat_endpoint() {
912        let p = make_provider(
913            "custom",
914            "https://my-api.example.com/api/v2/chat/completions",
915            None,
916        );
917        assert_eq!(
918            p.responses_url(),
919            "https://my-api.example.com/api/v2/responses"
920        );
921    }
922
923    #[test]
924    fn responses_url_base_with_v1_no_duplicate() {
925        let p = make_provider("test", "https://api.example.com/v1", None);
926        assert_eq!(p.responses_url(), "https://api.example.com/v1/responses");
927    }
928
929    #[test]
930    fn responses_url_non_v1_api_path_uses_raw_suffix() {
931        let p = make_provider("test", "https://api.example.com/api/coding/v3", None);
932        assert_eq!(
933            p.responses_url(),
934            "https://api.example.com/api/coding/v3/responses"
935        );
936    }
937
938    #[test]
939    fn chat_completions_url_without_v1() {
940        // Provider configured without /v1 in base URL
941        let p = make_provider("test", "https://api.example.com", None);
942        assert_eq!(
943            p.chat_completions_url(),
944            "https://api.example.com/chat/completions"
945        );
946    }
947
948    #[test]
949    fn chat_completions_url_base_with_v1() {
950        // Provider configured with /v1 in base URL
951        let p = make_provider("test", "https://api.example.com/v1", None);
952        assert_eq!(
953            p.chat_completions_url(),
954            "https://api.example.com/v1/chat/completions"
955        );
956    }
957
958    // ══════════════════════════════════════════════════════════
959    // Provider-specific endpoint tests (Issue #167)
960    // ══════════════════════════════════════════════════════════
961
962    #[test]
963    fn chat_completions_url_zai() {
964        // Z.AI uses /api/paas/v4 base path
965        let p = make_provider("zai", "https://api.z.ai/api/paas/v4", None);
966        assert_eq!(
967            p.chat_completions_url(),
968            "https://api.z.ai/api/paas/v4/chat/completions"
969        );
970    }
971
972    #[test]
973    fn chat_completions_url_minimax() {
974        // MiniMax uses /v1/text/chatcompletion_v2 — non-standard path used as-is.
975        let p = make_provider(
976            "minimax",
977            "https://api.minimax.io/v1/text/chatcompletion_v2",
978            None,
979        );
980        assert_eq!(
981            p.chat_completions_url(),
982            "https://api.minimax.io/v1/text/chatcompletion_v2"
983        );
984    }
985
986    #[test]
987    fn chat_completions_url_glm() {
988        // GLM (BigModel) uses /api/paas/v4 base path
989        let p = make_provider("glm", "https://open.bigmodel.cn/api/paas/v4", None);
990        assert_eq!(
991            p.chat_completions_url(),
992            "https://open.bigmodel.cn/api/paas/v4/chat/completions"
993        );
994    }
995
996    #[test]
997    fn chat_completions_url_opencode() {
998        // OpenCode Zen uses /zen/v1 base path
999        let p = make_provider("opencode", "https://opencode.ai/zen/v1", None);
1000        assert_eq!(
1001            p.chat_completions_url(),
1002            "https://opencode.ai/zen/v1/chat/completions"
1003        );
1004    }
1005}