rexis-llm 0.1.0

Rexis LLM - Multi-provider LLM client with streaming, tool calling, and JSON schema support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! # RSLLM Provider Abstraction
//!
//! Multi-provider support for different LLM APIs with unified interface.
//! Supports OpenAI, Claude (Anthropic), Ollama, and custom providers.

use crate::{ChatMessage, ChatResponse, RsllmError, RsllmResult, StreamChunk};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
use url::Url;

/// Normalize URL to ensure it has a trailing slash for proper path joining
/// This allows users to provide URLs with or without trailing slashes
fn normalize_base_url(url: &Url) -> Url {
    let url_str = url.as_str();
    if url_str.ends_with('/') {
        url.clone()
    } else {
        // Add trailing slash
        format!("{}/", url_str)
            .parse()
            .unwrap_or_else(|_| url.clone())
    }
}

/// Supported LLM providers
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Provider {
    /// OpenAI (GPT models)
    OpenAI,
    /// Anthropic Claude
    Claude,
    /// Ollama (local models)
    Ollama,
}

impl Provider {
    /// Get the default base URL for this provider
    pub fn default_base_url(&self) -> Url {
        match self {
            Provider::OpenAI => "https://api.openai.com/v1/".parse().unwrap(),
            Provider::Claude => "https://api.anthropic.com/v1/".parse().unwrap(),
            Provider::Ollama => "http://localhost:11434/api/".parse().unwrap(),
        }
    }

    /// Get the default models for this provider
    pub fn default_models(&self) -> Vec<&'static str> {
        match self {
            Provider::OpenAI => vec![
                "gpt-4o",
                "gpt-4o-mini",
                "gpt-4-turbo",
                "gpt-4",
                "gpt-3.5-turbo",
                "gpt-3.5-turbo-instruct",
            ],
            Provider::Claude => vec![
                "claude-3-5-sonnet-20241022",
                "claude-3-5-haiku-20241022",
                "claude-3-opus-20240229",
                "claude-3-sonnet-20240229",
                "claude-3-haiku-20240307",
            ],
            Provider::Ollama => vec![
                "llama3.1",
                "llama3.1:70b",
                "llama3.1:405b",
                "mistral",
                "codellama",
                "vicuna",
            ],
        }
    }

    /// Get the recommended model for this provider
    pub fn default_model(&self) -> &'static str {
        match self {
            Provider::OpenAI => "gpt-4o-mini",
            Provider::Claude => "claude-3-5-haiku-20241022",
            Provider::Ollama => "llama3.1",
        }
    }

    /// Check if this provider supports streaming
    pub fn supports_streaming(&self) -> bool {
        match self {
            Provider::OpenAI => true,
            Provider::Claude => true,
            Provider::Ollama => true,
        }
    }

    /// Check if this provider requires authentication
    pub fn requires_auth(&self) -> bool {
        match self {
            Provider::OpenAI => true,
            Provider::Claude => true,
            Provider::Ollama => false, // Local deployment typically doesn't need auth
        }
    }
}

impl fmt::Display for Provider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Provider::OpenAI => write!(f, "openai"),
            Provider::Claude => write!(f, "claude"),
            Provider::Ollama => write!(f, "ollama"),
        }
    }
}

impl FromStr for Provider {
    type Err = RsllmError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "openai" | "gpt" => Ok(Provider::OpenAI),
            "claude" | "anthropic" => Ok(Provider::Claude),
            "ollama" => Ok(Provider::Ollama),
            _ => Err(RsllmError::configuration(format!(
                "Unknown provider: {}",
                s
            ))),
        }
    }
}

/// Provider configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// Provider type
    pub provider: Provider,

    /// API key (if required)
    pub api_key: Option<String>,

    /// Base URL (if custom)
    pub base_url: Option<Url>,

    /// Organization ID (for providers that support it)
    pub organization_id: Option<String>,
}

impl Default for ProviderConfig {
    fn default() -> Self {
        Self {
            provider: Provider::OpenAI,
            api_key: None,
            base_url: None,
            organization_id: None,
        }
    }
}

/// Core provider trait for LLM interactions
#[async_trait]
pub trait LLMProvider: Send + Sync {
    /// Provider name/identifier
    fn name(&self) -> &str;

    /// Provider type
    fn provider_type(&self) -> Provider;

    /// Supported models
    fn supported_models(&self) -> Vec<String>;

    /// Health check
    async fn health_check(&self) -> RsllmResult<bool>;

    /// Chat completion (non-streaming)
    async fn chat_completion(
        &self,
        messages: Vec<ChatMessage>,
        model: Option<&str>,
        temperature: Option<f32>,
        max_tokens: Option<u32>,
    ) -> RsllmResult<ChatResponse>;

    /// Chat completion (streaming)
    async fn chat_completion_stream(
        &self,
        messages: Vec<ChatMessage>,
        model: Option<String>,
        temperature: Option<f32>,
        max_tokens: Option<u32>,
    ) -> RsllmResult<Box<dyn futures_util::Stream<Item = RsllmResult<StreamChunk>> + Send + Unpin>>;

    /// Chat completion with tool calling support
    async fn chat_completion_with_tools(
        &self,
        messages: Vec<ChatMessage>,
        tools: Vec<crate::tools::ToolDefinition>,
        model: Option<&str>,
        temperature: Option<f32>,
        max_tokens: Option<u32>,
    ) -> RsllmResult<ChatResponse> {
        // Default implementation: call without tools (fallback for providers without tool support)
        let _ = tools; // Suppress unused warning
        self.chat_completion(messages, model, temperature, max_tokens)
            .await
    }
}

/// OpenAI provider implementation
#[cfg(feature = "openai")]
pub struct OpenAIProvider {
    client: reqwest::Client,
    api_key: String,
    base_url: Url,
    organization_id: Option<String>,
}

#[cfg(feature = "openai")]
impl OpenAIProvider {
    /// Create a new OpenAI provider
    pub fn new(
        api_key: String,
        base_url: Option<Url>,
        organization_id: Option<String>,
    ) -> RsllmResult<Self> {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .map_err(|e| {
                RsllmError::configuration_with_source("Failed to create HTTP client", e)
            })?;

        let base = base_url.unwrap_or_else(|| Provider::OpenAI.default_base_url());
        let normalized_base_url = normalize_base_url(&base);

        Ok(Self {
            client,
            api_key,
            base_url: normalized_base_url,
            organization_id,
        })
    }

    /// Build request headers
    fn build_headers(&self) -> reqwest::header::HeaderMap {
        let mut headers = reqwest::header::HeaderMap::new();

        headers.insert(
            reqwest::header::AUTHORIZATION,
            format!("Bearer {}", self.api_key).parse().unwrap(),
        );

        headers.insert(
            reqwest::header::CONTENT_TYPE,
            "application/json".parse().unwrap(),
        );

        if let Some(org_id) = &self.organization_id {
            headers.insert("OpenAI-Organization", org_id.parse().unwrap());
        }

        headers
    }
}

#[cfg(feature = "openai")]
#[async_trait]
impl LLMProvider for OpenAIProvider {
    fn name(&self) -> &str {
        "OpenAI"
    }

    fn provider_type(&self) -> Provider {
        Provider::OpenAI
    }

    fn supported_models(&self) -> Vec<String> {
        Provider::OpenAI
            .default_models()
            .iter()
            .map(|s| s.to_string())
            .collect()
    }

    async fn health_check(&self) -> RsllmResult<bool> {
        let url = self.base_url.join("models")?;
        let response = self
            .client
            .get(url)
            .headers(self.build_headers())
            .send()
            .await?;

        Ok(response.status().is_success())
    }

    async fn chat_completion(
        &self,
        messages: Vec<ChatMessage>,
        model: Option<&str>,
        temperature: Option<f32>,
        max_tokens: Option<u32>,
    ) -> RsllmResult<ChatResponse> {
        let url = self.base_url.join("chat/completions")?;

        let mut request_body = serde_json::json!({
            "model": model.unwrap_or(Provider::OpenAI.default_model()),
            "messages": messages,
        });

        if let Some(temp) = temperature {
            request_body["temperature"] = temp.into();
        }

        if let Some(max_tokens) = max_tokens {
            request_body["max_tokens"] = max_tokens.into();
        }

        let response = self
            .client
            .post(url)
            .headers(self.build_headers())
            .json(&request_body)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(RsllmError::api(
                "OpenAI",
                format!("API request failed: {}", error_text),
                status.as_str(),
            ));
        }

        let response_data: serde_json::Value = response.json().await?;

        // Extract the response content
        let content = response_data["choices"][0]["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string();

        Ok(
            ChatResponse::new(content, model.unwrap_or(Provider::OpenAI.default_model()))
                .with_finish_reason("stop"),
        )
    }

    async fn chat_completion_stream(
        &self,
        messages: Vec<ChatMessage>,
        model: Option<String>,
        temperature: Option<f32>,
        max_tokens: Option<u32>,
    ) -> RsllmResult<Box<dyn futures_util::Stream<Item = RsllmResult<StreamChunk>> + Send + Unpin>>
    {
        use futures_util::stream;

        // For now, implement a simple mock stream
        // In production, this would handle Server-Sent Events (SSE) from OpenAI
        let _url = self.base_url.join("chat/completions")?;

        let model_name = model.unwrap_or_else(|| Provider::OpenAI.default_model().to_string());
        let mut _request_body = serde_json::json!({
            "model": &model_name,
            "messages": messages,
            "stream": true,
        });

        if let Some(temp) = temperature {
            _request_body["temperature"] = temp.into();
        }

        if let Some(max_tokens) = max_tokens {
            _request_body["max_tokens"] = max_tokens.into();
        }

        // Mock streaming response
        let chunks = vec![
            "Hello",
            " there!",
            " This",
            " is",
            " a",
            " streaming",
            " response",
            " from",
            " OpenAI.",
        ];

        let stream = stream::iter(chunks.into_iter().enumerate().map(move |(i, chunk)| {
            let _ = tokio::time::sleep(std::time::Duration::from_millis(100));

            if i == 8 {
                // Last chunk
                Ok(StreamChunk::done(&model_name).with_finish_reason("stop"))
            } else {
                Ok(StreamChunk::delta(chunk, &model_name))
            }
        }));

        Ok(Box::new(stream))
    }

    async fn chat_completion_with_tools(
        &self,
        messages: Vec<ChatMessage>,
        tools: Vec<crate::tools::ToolDefinition>,
        model: Option<&str>,
        temperature: Option<f32>,
        max_tokens: Option<u32>,
    ) -> RsllmResult<ChatResponse> {
        let url = self.base_url.join("chat/completions")?;

        // Build tools in OpenAI format
        let tools_json: Vec<serde_json::Value> = tools
            .iter()
            .map(|tool| {
                serde_json::json!({
                    "type": "function",
                    "function": {
                        "name": tool.name,
                        "description": tool.description,
                        "parameters": tool.parameters
                    }
                })
            })
            .collect();

        let mut request_body = serde_json::json!({
            "model": model.unwrap_or(Provider::OpenAI.default_model()),
            "messages": messages,
            "tools": tools_json,
        });

        if let Some(temp) = temperature {
            request_body["temperature"] = temp.into();
        }

        if let Some(max_tokens) = max_tokens {
            request_body["max_tokens"] = max_tokens.into();
        }

        let response = self
            .client
            .post(url)
            .headers(self.build_headers())
            .json(&request_body)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(RsllmError::api(
                "OpenAI",
                format!("API request failed: {}", error_text),
                status.as_str(),
            ));
        }

        let response_data: serde_json::Value = response.json().await?;

        // Extract content
        let content = response_data["choices"][0]["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string();

        // Parse tool calls if present (OpenAI format)
        let tool_calls = if let Some(calls_array) =
            response_data["choices"][0]["message"]["tool_calls"].as_array()
        {
            let parsed_calls: Vec<crate::message::ToolCall> = calls_array
                .iter()
                .filter_map(|call| {
                    Some(crate::message::ToolCall {
                        id: call["id"].as_str()?.to_string(),
                        call_type: crate::message::ToolCallType::Function,
                        function: crate::message::ToolFunction {
                            name: call["function"]["name"].as_str()?.to_string(),
                            arguments: serde_json::from_str(
                                call["function"]["arguments"].as_str()?,
                            )
                            .ok()?,
                        },
                    })
                })
                .collect();

            if parsed_calls.is_empty() {
                None
            } else {
                Some(parsed_calls)
            }
        } else {
            None
        };

        let mut response =
            ChatResponse::new(content, model.unwrap_or(Provider::OpenAI.default_model()))
                .with_finish_reason("stop");

        if let Some(calls) = tool_calls {
            response = response.with_tool_calls(calls);
        }

        Ok(response)
    }
}

/// Ollama provider implementation  
#[cfg(feature = "ollama")]
pub struct OllamaProvider {
    client: reqwest::Client,
    base_url: Url,
}

#[cfg(feature = "ollama")]
impl OllamaProvider {
    /// Create a new Ollama provider
    pub fn new(base_url: Option<Url>) -> RsllmResult<Self> {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(60)) // Ollama can be slower
            .build()
            .map_err(|e| {
                RsllmError::configuration_with_source("Failed to create HTTP client", e)
            })?;

        let base = base_url.unwrap_or_else(|| Provider::Ollama.default_base_url());
        let normalized_base_url = normalize_base_url(&base);

        Ok(Self {
            client,
            base_url: normalized_base_url,
        })
    }
}

#[cfg(feature = "ollama")]
#[async_trait]
impl LLMProvider for OllamaProvider {
    fn name(&self) -> &str {
        "Ollama"
    }

    fn provider_type(&self) -> Provider {
        Provider::Ollama
    }

    fn supported_models(&self) -> Vec<String> {
        Provider::Ollama
            .default_models()
            .iter()
            .map(|s| s.to_string())
            .collect()
    }

    async fn health_check(&self) -> RsllmResult<bool> {
        let url = self.base_url.join("tags")?;
        let response = self.client.get(url).send().await?;
        Ok(response.status().is_success())
    }

    async fn chat_completion(
        &self,
        messages: Vec<ChatMessage>,
        model: Option<&str>,
        temperature: Option<f32>,
        _max_tokens: Option<u32>,
    ) -> RsllmResult<ChatResponse> {
        let url = self.base_url.join("chat")?;

        let mut request_body = serde_json::json!({
            "model": model.unwrap_or(Provider::Ollama.default_model()),
            "messages": messages,
            "stream": false,
        });

        if let Some(temp) = temperature {
            request_body["options"] = serde_json::json!({
                "temperature": temp
            });
        }

        let response = self.client.post(url).json(&request_body).send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(RsllmError::api(
                "Ollama",
                format!("API request failed: {}", error_text),
                status.as_str(),
            ));
        }

        let response_data: serde_json::Value = response.json().await?;

        let content = response_data["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string();

        Ok(
            ChatResponse::new(content, model.unwrap_or(Provider::Ollama.default_model()))
                .with_finish_reason("stop"),
        )
    }

    async fn chat_completion_stream(
        &self,
        messages: Vec<ChatMessage>,
        model: Option<String>,
        temperature: Option<f32>,
        _max_tokens: Option<u32>,
    ) -> RsllmResult<Box<dyn futures_util::Stream<Item = RsllmResult<StreamChunk>> + Send + Unpin>>
    {
        use futures_util::stream;

        // Mock streaming response for Ollama
        let _url = self.base_url.join("chat")?;

        let model_name = model.unwrap_or_else(|| Provider::Ollama.default_model().to_string());
        let mut _request_body = serde_json::json!({
            "model": &model_name,
            "messages": messages,
            "stream": true,
        });

        if let Some(temp) = temperature {
            _request_body["options"] = serde_json::json!({
                "temperature": temp
            });
        }

        // Mock streaming response
        let chunks = vec![
            "This",
            " is",
            " a",
            " response",
            " from",
            " Ollama",
            " running",
            " locally.",
        ];

        let stream = stream::iter(chunks.into_iter().enumerate().map(move |(i, chunk)| {
            let _ = tokio::time::sleep(std::time::Duration::from_millis(150));

            if i == 7 {
                // Last chunk
                Ok(StreamChunk::done(&model_name).with_finish_reason("stop"))
            } else {
                Ok(StreamChunk::delta(chunk, &model_name))
            }
        }));

        Ok(Box::new(stream))
    }

    async fn chat_completion_with_tools(
        &self,
        messages: Vec<ChatMessage>,
        tools: Vec<crate::tools::ToolDefinition>,
        model: Option<&str>,
        temperature: Option<f32>,
        _max_tokens: Option<u32>,
    ) -> RsllmResult<ChatResponse> {
        let url = self.base_url.join("chat")?;

        // Build tools in Ollama/OpenAI format
        let tools_json: Vec<serde_json::Value> = tools
            .iter()
            .map(|tool| {
                serde_json::json!({
                    "type": "function",
                    "function": {
                        "name": tool.name,
                        "description": tool.description,
                        "parameters": tool.parameters
                    }
                })
            })
            .collect();

        let mut request_body = serde_json::json!({
            "model": model.unwrap_or(Provider::Ollama.default_model()),
            "messages": messages,
            "stream": false,
            "tools": tools_json,
        });

        if let Some(temp) = temperature {
            request_body["options"] = serde_json::json!({
                "temperature": temp
            });
        }

        let response = self.client.post(url).json(&request_body).send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(RsllmError::api(
                "Ollama",
                format!("API request failed: {}", error_text),
                status.as_str(),
            ));
        }

        let response_data: serde_json::Value = response.json().await?;

        let content = response_data["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string();

        // Parse tool calls if present
        let tool_calls =
            if let Some(calls_array) = response_data["message"]["tool_calls"].as_array() {
                let parsed_calls: Vec<crate::message::ToolCall> = calls_array
                    .iter()
                    .enumerate()
                    .filter_map(|(idx, call)| {
                        let function_name = call["function"]["name"].as_str()?;

                        // Ollama returns arguments as an object, sometimes with string values
                        // Convert string numbers to actual numbers for compatibility
                        let mut arguments = call["function"]["arguments"].clone();
                        if let serde_json::Value::Object(ref mut args_obj) = arguments {
                            for (_key, value) in args_obj.iter_mut() {
                                if let serde_json::Value::String(s) = value {
                                    // Try to parse as number
                                    if let Ok(num) = s.parse::<f64>() {
                                        *value = serde_json::json!(num);
                                    } else if let Ok(int_num) = s.parse::<i64>() {
                                        *value = serde_json::json!(int_num);
                                    }
                                }
                            }
                        }

                        // Ollama doesn't provide an ID, so generate one
                        let id = call["id"]
                            .as_str()
                            .map(|s| s.to_string())
                            .unwrap_or_else(|| format!("call_{}", idx));

                        Some(crate::message::ToolCall {
                            id,
                            call_type: crate::message::ToolCallType::Function,
                            function: crate::message::ToolFunction {
                                name: function_name.to_string(),
                                arguments,
                            },
                        })
                    })
                    .collect();

                if parsed_calls.is_empty() {
                    None
                } else {
                    Some(parsed_calls)
                }
            } else {
                None
            };

        let mut response =
            ChatResponse::new(content, model.unwrap_or(Provider::Ollama.default_model()))
                .with_finish_reason("stop");

        if let Some(calls) = tool_calls {
            response = response.with_tool_calls(calls);
        }

        Ok(response)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_normalize_base_url_without_trailing_slash() {
        let url = Url::parse("http://localhost:11434/api").unwrap();
        let normalized = normalize_base_url(&url);
        assert_eq!(normalized.as_str(), "http://localhost:11434/api/");
    }

    #[test]
    fn test_normalize_base_url_with_trailing_slash() {
        let url = Url::parse("http://localhost:11434/api/").unwrap();
        let normalized = normalize_base_url(&url);
        assert_eq!(normalized.as_str(), "http://localhost:11434/api/");
    }

    #[test]
    fn test_normalize_base_url_complex() {
        let url = Url::parse("https://api.openai.com/v1").unwrap();
        let normalized = normalize_base_url(&url);
        assert_eq!(normalized.as_str(), "https://api.openai.com/v1/");
    }

    #[test]
    fn test_url_join_after_normalization() {
        // Test that after normalization, joining works correctly
        let url_without_slash = Url::parse("http://localhost:11434/api").unwrap();
        let normalized = normalize_base_url(&url_without_slash);
        let joined = normalized.join("chat").unwrap();
        assert_eq!(joined.as_str(), "http://localhost:11434/api/chat");

        let url_with_slash = Url::parse("http://localhost:11434/api/").unwrap();
        let normalized2 = normalize_base_url(&url_with_slash);
        let joined2 = normalized2.join("chat").unwrap();
        assert_eq!(joined2.as_str(), "http://localhost:11434/api/chat");
    }
}