modelmux 1.0.0

ModelMux - high-performance Rust gateway that translates OpenAI-compatible API requests to Vertex AI (Claude), with streaming, tool calling, and production-grade reliability.
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
//!
//! OpenAI to Anthropic format converter for API request translation.
//!
//! Converts OpenAI-compatible chat completion requests to Anthropic/Vertex AI format.
//! Handles message conversion, tool calling, and streaming configuration while
//! maintaining semantic equivalence between the two API formats.
//!
//! Authors:
//!   Jaro <yarenty@gmail.com>
//!
//! Copyright (c) 2026 SkyCorp

/* --- uses ------------------------------------------------------------------------------------ */

use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::config::LogLevel;
use crate::error::{ProxyError, Result};

/* --- helper functions ----------------------------------------------------------------------- */

///
/// Custom serialization helper for tools field.
///
/// Skips serialization when tools is None or empty to avoid sending invalid data to Vertex AI.
///
/// # Arguments
///  * `tools` - optional tools vector
///
/// # Returns
///  * true if field should be skipped (None or empty), false otherwise
fn skip_empty_tools(tools: &Option<Vec<AnthropicTool>>) -> bool {
    match tools {
        None => true,
        Some(vec) => vec.is_empty(),
    }
}

/* --- types ----------------------------------------------------------------------------------- */

///
/// OpenAI chat completion request structure.
///
/// Represents an incoming request in OpenAI's chat completions API format.
/// Contains messages, model configuration, and optional tool definitions.
#[derive(Debug, Deserialize)]
pub struct OpenAiRequest {
    /** the model identifier to use for completion */
    pub model: Option<String>,
    /** conversation messages array */
    pub messages: Vec<OpenAiMessage>,
    /** maximum number of tokens to generate */
    pub max_tokens: Option<u32>,
    /** sampling temperature for response generation */
    pub temperature: Option<f64>,
    /** whether to stream the response */
    pub stream: Option<bool>,
    /** available tools for function calling */
    pub tools: Option<Vec<OpenAiTool>>,
    /** tool choice configuration */
    pub tool_choice: Option<OpenAiToolChoice>,
}

///
/// OpenAI message structure within a chat completion request.
///
/// Represents a single message in the conversation with role-based content
/// and optional tool call information.
#[derive(Debug, Deserialize)]
pub struct OpenAiMessage {
    /** message role: system, user, assistant, or tool */
    pub role: String,
    /** message content, can be string or structured blocks */
    pub content: Option<OpenAiContent>,
    /** tool calls made by the assistant */
    pub tool_calls: Option<Vec<OpenAiToolCall>>,
    /** tool call ID for tool response messages */
    #[serde(rename = "tool_call_id")]
    pub tool_call_id: Option<String>,
}

///
/// OpenAI content union type for flexible message content.
///
/// Supports both simple string content and structured content blocks
/// for multimodal messages including text and images.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum OpenAiContent {
    /** simple string content */
    String(String),
    /** structured content blocks array */
    Array(Vec<OpenAiContentBlock>),
}

///
/// OpenAI structured content block for multimodal messages.
///
/// Represents individual content elements within a message, supporting
/// text and image content types with appropriate metadata.
#[derive(Debug, Deserialize)]
pub struct OpenAiContentBlock {
    /** content block type: text or image_url */
    #[serde(rename = "type")]
    pub block_type: String,
    /** text content for text blocks */
    pub text: Option<String>,
    /** image URL reference for image blocks */
    #[serde(rename = "image_url")]
    pub image_url: Option<ImageUrl>,
}

///
/// Image URL reference structure for image content blocks.
///
/// Contains the URL pointing to the image resource.
#[derive(Debug, Deserialize)]
pub struct ImageUrl {
    /** the image URL */
    pub url: String,
}

///
/// OpenAI tool call structure for function invocations.
///
/// Represents a function call made by the assistant during response generation.
#[derive(Debug, Deserialize)]
pub struct OpenAiToolCall {
    /** unique identifier for this tool call */
    pub id: String,
    /** tool call type, typically "function" */
    #[serde(rename = "type")]
    #[allow(dead_code)]
    pub call_type: String,
    /** function call details */
    pub function: OpenAiFunction,
}

///
/// OpenAI function call details within a tool call.
///
/// Contains the function name and arguments for execution.
#[derive(Debug, Deserialize)]
pub struct OpenAiFunction {
    /** function name to call */
    pub name: String,
    /** function arguments as JSON value */
    pub arguments: serde_json::Value,
}

///
/// OpenAI tool definition for available functions.
///
/// Describes a function that can be called by the model during response generation.
#[derive(Debug, Deserialize)]
pub struct OpenAiTool {
    /** tool type, typically "function" */
    #[serde(rename = "type")]
    #[allow(dead_code)]
    pub tool_type: String,
    /** function definition and schema */
    pub function: OpenAiToolFunction,
}

///
/// OpenAI function definition within a tool.
///
/// Contains function metadata and parameter schema for validation.
#[derive(Debug, Deserialize)]
pub struct OpenAiToolFunction {
    /** function name */
    pub name: String,
    /** function description */
    pub description: String,
    /** JSON schema for function parameters */
    pub parameters: serde_json::Value,
}

///
/// OpenAI tool choice configuration.
///
/// Controls how the model should choose which tools to use during generation.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum OpenAiToolChoice {
    /** string choice: "auto", "none", etc. */
    String(String),
    /** object choice with specific function */
    Object(OpenAiToolChoiceObject),
}

///
/// OpenAI tool choice object for specific function selection.
///
/// Allows forcing the model to use a specific function.
#[derive(Debug, Deserialize)]
pub struct OpenAiToolChoiceObject {
    /** choice type */
    #[serde(rename = "type")]
    #[allow(dead_code)]
    pub choice_type: String,
    /** specific function to choose */
    pub function: Option<OpenAiToolChoiceFunction>,
}

///
/// OpenAI specific function choice within tool choice object.
///
/// Identifies the exact function to use.
#[derive(Debug, Deserialize)]
pub struct OpenAiToolChoiceFunction {
    /** function name to force */
    pub name: String,
}

///
/// Anthropic chat completion request structure.
///
/// Target format for requests to Anthropic's Claude API via Vertex AI.
/// Contains converted messages and configuration from OpenAI format.
#[derive(Debug, Serialize)]
pub struct AnthropicRequest {
    /** Anthropic API version identifier */
    #[serde(rename = "anthropic_version")]
    pub anthropic_version: String,
    /** conversation messages in Anthropic format */
    pub messages: Vec<AnthropicMessage>,
    /** maximum tokens to generate */
    #[serde(rename = "max_tokens")]
    pub max_tokens: u32,
    /** sampling temperature */
    pub temperature: f64,
    /** whether to stream the response */
    pub stream: bool,
    /** available tools in Anthropic format */
    #[serde(skip_serializing_if = "skip_empty_tools")]
    pub tools: Option<Vec<AnthropicTool>>,
    /** tool choice configuration in Anthropic format */
    #[serde(rename = "tool_choice", skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<AnthropicToolChoice>,
}

///
/// Anthropic message structure for chat conversations.
///
/// Contains role and content blocks in Anthropic's preferred format.
#[derive(Debug, Serialize)]
pub struct AnthropicMessage {
    /** message role: user or assistant */
    pub role: String,
    /** message content as structured blocks */
    pub content: Vec<AnthropicContentBlock>,
}

///
/// Anthropic content block for message content.
///
/// Supports text, tool usage, tool results, and image content types
/// with proper tagging for serialization.
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub enum AnthropicContentBlock {
    /** text content block */
    #[serde(rename = "text")]
    Text {
        /** the text content */
        text: String,
    },
    /** tool usage block for function calls */
    #[serde(rename = "tool_use")]
    ToolUse {
        /** tool call identifier */
        id: String,
        /** function name */
        name: String,
        /** function input arguments */
        input: serde_json::Value,
    },
    /** tool result block for function responses */
    #[serde(rename = "tool_result")]
    ToolResult {
        /** corresponding tool use identifier */
        #[serde(rename = "tool_use_id")]
        tool_use_id: String,
        /** tool execution result */
        content: AnthropicToolResultContent,
    },
    /** image content block */
    #[serde(rename = "image")]
    Image {
        /** image source information */
        source: ImageSource,
    },
}

///
/// Anthropic tool result content union type.
///
/// Supports both simple string results and structured array results
/// for complex tool responses.
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum AnthropicToolResultContent {
    /** simple string result */
    String(String),
    /** structured array result */
    Array(Vec<serde_json::Value>),
}

///
/// Image source information for Anthropic image blocks.
///
/// Contains metadata about image resources.
#[derive(Debug, Serialize)]
pub struct ImageSource {
    /** source type identifier */
    #[serde(rename = "type")]
    pub source_type: String,
    /** image URL */
    pub url: String,
}

///
/// Anthropic tool definition for function calling.
///
/// Describes available functions in Anthropic's format.
#[derive(Debug, Serialize)]
pub struct AnthropicTool {
    /** function name */
    pub name: String,
    /** function description */
    pub description: String,
    /** function input schema */
    #[serde(rename = "input_schema")]
    pub input_schema: serde_json::Value,
}

///
/// Anthropic tool choice configuration.
///
/// Controls tool selection behavior in Anthropic format.
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub enum AnthropicToolChoice {
    /** automatic tool selection */
    #[serde(rename = "auto")]
    Auto,
    /** force specific tool usage */
    #[serde(rename = "tool")]
    Tool {
        /** tool name to force */
        name: String,
    },
}

///
/// Converter from OpenAI format to Anthropic format.
///
/// Follows Single Responsibility Principle - handles only format conversion
/// from OpenAI chat completions to Anthropic message format.
pub struct OpenAiToAnthropicConverter {
    /** logging level for debug output */
    log_level: LogLevel,
}

/* --- constants ------------------------------------------------------------------------------ */

/** Anthropic API version to use for requests */
const ANTHROPIC_VERSION: &str = "vertex-2023-10-16";

/** Default maximum tokens if not specified */
const DEFAULT_MAX_TOKENS: u32 = 8000;

/** Default temperature if not specified */
const DEFAULT_TEMPERATURE: f64 = 0.9;

/* --- start of code -------------------------------------------------------------------------- */

impl OpenAiToAnthropicConverter {
    ///
    /// Create a new OpenAI to Anthropic converter.
    ///
    /// # Arguments
    ///  * `log_level` - logging level for debug output
    ///
    /// # Returns
    ///  * New converter instance
    pub fn new(log_level: LogLevel) -> Self {
        Self { log_level }
    }

    ///
    /// Convert OpenAI request to Anthropic request format.
    ///
    /// Transforms the entire request structure including messages, tools, and
    /// configuration parameters. Handles system messages, tool calls, and
    /// multimodal content appropriately.
    ///
    /// # Arguments
    ///  * `request` - OpenAI format request to convert
    ///
    /// # Returns
    ///  * Converted Anthropic format request
    ///  * `ProxyError::Conversion` if conversion fails
    pub fn convert(&self, request: OpenAiRequest) -> Result<AnthropicRequest> {
        self.debug(&format!(
            "Converting {} message(s) from OpenAI to Anthropic format",
            request.messages.len()
        ));

        let mut anthropic_messages = Vec::new();
        let mut pending_tool_results = Vec::new();
        let mut last_assistant_message: Option<&'_ OpenAiMessage> = None;
        let mut system_messages = Vec::new();

        self.process_messages(
            &request.messages,
            &mut anthropic_messages,
            &mut pending_tool_results,
            &mut last_assistant_message,
            &mut system_messages,
        )?;

        self.handle_remaining_tool_results(
            &mut anthropic_messages,
            &mut pending_tool_results,
            last_assistant_message,
        )?;

        self.prepend_system_messages(&mut anthropic_messages, system_messages);

        let tools = self.convert_tools(request.tools);
        let tool_choice = self.convert_tool_choice(request.tool_choice);

        let anthropic_request = AnthropicRequest {
            anthropic_version: ANTHROPIC_VERSION.to_string(),
            messages: anthropic_messages,
            max_tokens: request.max_tokens.unwrap_or(DEFAULT_MAX_TOKENS),
            temperature: request.temperature.unwrap_or(DEFAULT_TEMPERATURE),
            stream: request.stream.unwrap_or(false),
            tools,
            tool_choice,
        };

        self.debug(&format!(
            "Converted Anthropic request with {} messages",
            anthropic_request.messages.len()
        ));

        Ok(anthropic_request)
    }

    ///
    /// Process all messages in the OpenAI request.
    ///
    /// Iterates through messages and converts them based on role type,
    /// managing tool calls and results properly.
    ///
    /// # Arguments
    ///  * `messages` - OpenAI messages to process
    ///  * `anthropic_messages` - output Anthropic messages
    ///  * `pending_tool_results` - accumulated tool results
    ///  * `last_assistant_message` - reference to last assistant message
    ///  * `system_messages` - accumulated system messages
    ///
    /// # Returns
    ///  * `Ok(())` on successful processing
    ///  * `ProxyError::Conversion` if message conversion fails
    fn process_messages<'a>(
        &self,
        messages: &'a [OpenAiMessage],
        anthropic_messages: &mut Vec<AnthropicMessage>,
        pending_tool_results: &mut Vec<(String, AnthropicToolResultContent)>,
        last_assistant_message: &mut Option<&'a OpenAiMessage>,
        system_messages: &mut Vec<String>,
    ) -> Result<()> {
        for msg in messages {
            self.debug(&format!("Processing message with role: {}", msg.role));

            match msg.role.as_str() {
                "system" => {
                    self.process_system_message(msg, system_messages);
                }
                "assistant" => {
                    self.process_assistant_message(
                        msg,
                        anthropic_messages,
                        pending_tool_results,
                        last_assistant_message,
                    )?;
                }
                "tool" => {
                    self.process_tool_message(msg, pending_tool_results);
                }
                "user" => {
                    self.process_user_message(
                        msg,
                        anthropic_messages,
                        pending_tool_results,
                        *last_assistant_message,
                    )?;
                }
                _ => {
                    return Err(ProxyError::Conversion(format!(
                        "Unknown message role: {}",
                        msg.role
                    )));
                }
            }
        }
        Ok(())
    }

    ///
    /// Process a system message by extracting its content.
    ///
    /// # Arguments
    ///  * `msg` - system message to process
    ///  * `system_messages` - collection to add system content to
    fn process_system_message(&self, msg: &OpenAiMessage, system_messages: &mut Vec<String>) {
        if let Some(OpenAiContent::String(content)) = &msg.content {
            system_messages.push(content.clone());
        }
    }

    ///
    /// Process an assistant message with optional tool calls.
    ///
    /// # Arguments
    ///  * `msg` - assistant message to process
    ///  * `anthropic_messages` - output Anthropic messages
    ///  * `pending_tool_results` - accumulated tool results
    ///  * `last_assistant_message` - reference to last assistant message
    ///
    /// # Returns
    ///  * `Ok(())` on successful processing
    ///  * `ProxyError::Conversion` if conversion fails
    fn process_assistant_message<'a>(
        &self,
        msg: &'a OpenAiMessage,
        anthropic_messages: &mut Vec<AnthropicMessage>,
        pending_tool_results: &mut Vec<(String, AnthropicToolResultContent)>,
        last_assistant_message: &mut Option<&'a OpenAiMessage>,
    ) -> Result<()> {
        if last_assistant_message.is_some() && !pending_tool_results.is_empty() {
            self.attach_tool_results(anthropic_messages, pending_tool_results)?;
        }

        let anthropic_msg = self.convert_assistant_message(msg)?;
        anthropic_messages.push(anthropic_msg);
        *last_assistant_message = Some(msg);
        Ok(())
    }

    ///
    /// Process a tool message by collecting its result.
    ///
    /// # Arguments
    ///  * `msg` - tool message to process
    ///  * `pending_tool_results` - collection to add tool result to
    fn process_tool_message(
        &self,
        msg: &OpenAiMessage,
        pending_tool_results: &mut Vec<(String, AnthropicToolResultContent)>,
    ) {
        if let Some(tool_call_id) = &msg.tool_call_id {
            let content = self.convert_tool_result_content(&msg.content);
            pending_tool_results.push((tool_call_id.clone(), content));
            self.debug(&format!("Collected tool result for tool_call_id: {}", tool_call_id));
        }
    }

    ///
    /// Process a user message and attach any pending tool results.
    ///
    /// # Arguments
    ///  * `msg` - user message to process
    ///  * `anthropic_messages` - output Anthropic messages
    ///  * `pending_tool_results` - accumulated tool results
    ///  * `last_assistant_message` - optional reference to last assistant message
    ///
    /// # Returns
    ///  * `Ok(())` on successful processing
    ///  * `ProxyError::Conversion` if conversion fails
    fn process_user_message<'a>(
        &self,
        msg: &'a OpenAiMessage,
        anthropic_messages: &mut Vec<AnthropicMessage>,
        pending_tool_results: &mut Vec<(String, AnthropicToolResultContent)>,
        last_assistant_message: Option<&'a OpenAiMessage>,
    ) -> Result<()> {
        if last_assistant_message.is_some() && !pending_tool_results.is_empty() {
            self.debug(&format!(
                "Attaching {} tool result(s) before user message",
                pending_tool_results.len()
            ));
            self.attach_tool_results(anthropic_messages, pending_tool_results)?;
        }

        let anthropic_msg = self.convert_user_message(msg)?;
        anthropic_messages.push(anthropic_msg);
        Ok(())
    }

    ///
    /// Convert tool result content from OpenAI to Anthropic format.
    ///
    /// # Arguments
    ///  * `content` - OpenAI message content to convert
    ///
    /// # Returns
    ///  * Converted tool result content
    fn convert_tool_result_content(
        &self,
        content: &Option<OpenAiContent>,
    ) -> AnthropicToolResultContent {
        match content {
            Some(OpenAiContent::String(s)) => AnthropicToolResultContent::String(s.clone()),
            Some(OpenAiContent::Array(arr)) => {
                let mut json_blocks = Vec::new();
                for block in arr {
                    match block.block_type.as_str() {
                        "text" => {
                            if let Some(text) = &block.text {
                                json_blocks.push(json!({ "type": "text", "text": text }));
                            }
                        }
                        "image_url" => {
                            if let Some(img) = &block.image_url {
                                json_blocks.push(
                                    json!({ "type": "image_url", "image_url": { "url": img.url } }),
                                );
                            }
                        }
                        _ => {}
                    }
                }
                AnthropicToolResultContent::Array(json_blocks)
            }
            None => AnthropicToolResultContent::String(String::new()),
        }
    }

    ///
    /// Handle any remaining tool results after processing all messages.
    ///
    /// # Arguments
    ///  * `anthropic_messages` - output Anthropic messages
    ///  * `pending_tool_results` - accumulated tool results
    ///  * `last_assistant_message` - optional reference to last assistant message
    ///
    /// # Returns
    ///  * `Ok(())` on successful processing
    ///  * `ProxyError::Conversion` if attachment fails
    fn handle_remaining_tool_results(
        &self,
        anthropic_messages: &mut Vec<AnthropicMessage>,
        pending_tool_results: &mut Vec<(String, AnthropicToolResultContent)>,
        last_assistant_message: Option<&OpenAiMessage>,
    ) -> Result<()> {
        if last_assistant_message.is_some() && !pending_tool_results.is_empty() {
            self.attach_tool_results(anthropic_messages, pending_tool_results)?;
        }
        Ok(())
    }

    ///
    /// Prepend system messages to the first user message.
    ///
    /// # Arguments
    ///  * `anthropic_messages` - output Anthropic messages to modify
    ///  * `system_messages` - system messages to prepend
    fn prepend_system_messages(
        &self,
        anthropic_messages: &mut [AnthropicMessage],
        system_messages: Vec<String>,
    ) {
        if !system_messages.is_empty() && !anthropic_messages.is_empty() {
            let system_text = system_messages.join("\n\n");
            if let Some(first_user_msg) = anthropic_messages.iter_mut().find(|m| m.role == "user") {
                self.prepend_system_text(first_user_msg, &system_text);
            }
        }
    }

    ///
    /// Convert OpenAI tools to Anthropic format.
    ///
    /// # Arguments
    ///  * `tools` - optional OpenAI tools to convert
    ///
    /// # Returns
    ///  * Converted Anthropic tools or None
    fn convert_tools(&self, tools: Option<Vec<OpenAiTool>>) -> Option<Vec<AnthropicTool>> {
        tools.map(|tools| {
            self.debug(&format!(
                "Converting {} tool(s) from OpenAI to Anthropic format",
                tools.len()
            ));
            tools
                .into_iter()
                .map(|tool| AnthropicTool {
                    name: tool.function.name,
                    description: tool.function.description,
                    input_schema: tool.function.parameters,
                })
                .collect()
        })
    }

    ///
    /// Convert OpenAI tool choice to Anthropic format.
    ///
    /// # Arguments
    ///  * `tool_choice` - optional OpenAI tool choice to convert
    ///
    /// # Returns
    ///  * Converted Anthropic tool choice or None
    fn convert_tool_choice(
        &self,
        tool_choice: Option<OpenAiToolChoice>,
    ) -> Option<AnthropicToolChoice> {
        tool_choice.and_then(|choice| {
            self.debug(&format!("Tool choice: {:?}", choice));
            match choice {
                OpenAiToolChoice::String(s) if s == "auto" => Some(AnthropicToolChoice::Auto),
                OpenAiToolChoice::String(s) if s == "none" => {
                    self.debug("Tool choice 'none' not supported by Anthropic, omitting");
                    None
                }
                OpenAiToolChoice::Object(obj) => {
                    if let Some(function) = obj.function {
                        self.debug(&format!("Forced tool choice: {}", function.name));
                        Some(AnthropicToolChoice::Tool { name: function.name })
                    } else {
                        None
                    }
                }
                _ => None,
            }
        })
    }

    ///
    /// Convert an OpenAI assistant message to Anthropic format.
    ///
    /// Handles both text content and tool calls within the message.
    ///
    /// # Arguments
    ///  * `msg` - OpenAI assistant message to convert
    ///
    /// # Returns
    ///  * Converted Anthropic message
    ///  * `ProxyError::Conversion` if conversion fails
    fn convert_assistant_message(&self, msg: &OpenAiMessage) -> Result<AnthropicMessage> {
        let mut content = Vec::new();

        self.add_text_content(&mut content, &msg.content);
        self.add_tool_calls(&mut content, &msg.tool_calls)?;

        if content.is_empty() {
            content.push(AnthropicContentBlock::Text { text: String::new() });
        }

        Ok(AnthropicMessage { role: "assistant".to_string(), content })
    }

    ///
    /// Add text content from OpenAI message to Anthropic content blocks.
    ///
    /// # Arguments
    ///  * `content` - content blocks to add to
    ///  * `openai_content` - OpenAI content to extract text from
    fn add_text_content(
        &self,
        content: &mut Vec<AnthropicContentBlock>,
        openai_content: &Option<OpenAiContent>,
    ) {
        match openai_content {
            Some(OpenAiContent::String(text)) if !text.is_empty() => {
                content.push(AnthropicContentBlock::Text { text: text.clone() });
            }
            Some(OpenAiContent::Array(blocks)) => {
                for block in blocks {
                    if block.block_type == "text" {
                        if let Some(text) = &block.text {
                            content.push(AnthropicContentBlock::Text { text: text.clone() });
                        }
                    }
                }
            }
            _ => {}
        }
    }

    ///
    /// Add tool calls from OpenAI message to Anthropic content blocks.
    ///
    /// # Arguments
    ///  * `content` - content blocks to add to
    ///  * `tool_calls` - OpenAI tool calls to convert
    ///
    /// # Returns
    ///  * `Ok(())` on successful addition
    ///  * `ProxyError::Conversion` if tool call conversion fails
    fn add_tool_calls(
        &self,
        content: &mut Vec<AnthropicContentBlock>,
        tool_calls: &Option<Vec<OpenAiToolCall>>,
    ) -> Result<()> {
        if let Some(tool_calls) = tool_calls {
            self.debug(&format!(
                "Converting {} tool call(s) from assistant message",
                tool_calls.len()
            ));
            for tool_call in tool_calls {
                let args = self.parse_tool_arguments(&tool_call.function.arguments);
                content.push(AnthropicContentBlock::ToolUse {
                    id: tool_call.id.clone(),
                    name: tool_call.function.name.clone(),
                    input: args,
                });
            }
        }
        Ok(())
    }

    ///
    /// Parse tool call arguments from JSON value.
    ///
    /// # Arguments
    ///  * `arguments` - JSON arguments value
    ///
    /// # Returns
    ///  * Parsed JSON value for tool input
    fn parse_tool_arguments(&self, arguments: &serde_json::Value) -> serde_json::Value {
        match arguments {
            serde_json::Value::String(s) => {
                serde_json::from_str(s).unwrap_or_else(|_| arguments.clone())
            }
            _ => arguments.clone(),
        }
    }

    ///
    /// Convert an OpenAI user message to Anthropic format.
    ///
    /// Handles text, image, and multimodal content appropriately.
    ///
    /// # Arguments
    ///  * `msg` - OpenAI user message to convert
    ///
    /// # Returns
    ///  * Converted Anthropic message
    ///  * `ProxyError::Conversion` if conversion fails
    fn convert_user_message(&self, msg: &OpenAiMessage) -> Result<AnthropicMessage> {
        let content = match &msg.content {
            Some(OpenAiContent::String(text)) => {
                vec![AnthropicContentBlock::Text { text: text.clone() }]
            }
            Some(OpenAiContent::Array(blocks)) => self.convert_content_blocks(blocks),
            None => vec![AnthropicContentBlock::Text { text: String::new() }],
        };

        Ok(AnthropicMessage { role: "user".to_string(), content })
    }

    ///
    /// Convert OpenAI content blocks to Anthropic content blocks.
    ///
    /// # Arguments
    ///  * `blocks` - OpenAI content blocks to convert
    ///
    /// # Returns
    ///  * Converted Anthropic content blocks
    fn convert_content_blocks(&self, blocks: &[OpenAiContentBlock]) -> Vec<AnthropicContentBlock> {
        blocks
            .iter()
            .filter_map(|block| match block.block_type.as_str() {
                "text" => {
                    block.text.as_ref().map(|t| AnthropicContentBlock::Text { text: t.clone() })
                }
                "image_url" => block.image_url.as_ref().map(|img| AnthropicContentBlock::Image {
                    source: ImageSource { source_type: "url".to_string(), url: img.url.clone() },
                }),
                _ => None,
            })
            .collect()
    }

    ///
    /// Attach pending tool results to the conversation.
    ///
    /// Creates a user message containing tool result blocks and adds it
    /// to the conversation after the last assistant message.
    ///
    /// # Arguments
    ///  * `anthropic_messages` - messages to add tool results to
    ///  * `pending_tool_results` - tool results to attach
    ///
    /// # Returns
    ///  * `Ok(())` on successful attachment
    ///  * `ProxyError::Conversion` if attachment fails
    fn attach_tool_results(
        &self,
        anthropic_messages: &mut Vec<AnthropicMessage>,
        pending_tool_results: &mut Vec<(String, AnthropicToolResultContent)>,
    ) -> Result<()> {
        if let Some(last_msg) = anthropic_messages.last() {
            if last_msg.role == "assistant" {
                let tool_results: Vec<AnthropicContentBlock> = pending_tool_results
                    .drain(..)
                    .map(|(tool_use_id, content)| AnthropicContentBlock::ToolResult {
                        tool_use_id,
                        content,
                    })
                    .collect();

                self.debug(&format!(
                    "Adding tool results user message with {} result(s)",
                    tool_results.len()
                ));

                anthropic_messages
                    .push(AnthropicMessage { role: "user".to_string(), content: tool_results });
            } else {
                self.debug("WARNING: Last message is not assistant, cannot attach tool results");
            }
        }
        Ok(())
    }

    ///
    /// Prepend system text to the first text block of a message.
    ///
    /// Either modifies the first existing text block or inserts a new
    /// text block at the beginning with the system content.
    ///
    /// # Arguments
    ///  * `msg` - message to prepend system text to
    ///  * `system_text` - system text to prepend
    fn prepend_system_text(&self, msg: &mut AnthropicMessage, system_text: &str) {
        if let Some(first_text_block) =
            msg.content.iter_mut().find(|c| matches!(c, AnthropicContentBlock::Text { .. }))
        {
            if let AnthropicContentBlock::Text { text } = first_text_block {
                *text = format!("{}\n\n{}", system_text, text);
            }
        } else {
            msg.content.insert(0, AnthropicContentBlock::Text { text: system_text.to_string() });
        }
    }

    ///
    /// Log debug message if trace logging is enabled.
    ///
    /// # Arguments
    ///  * `msg` - debug message to log
    pub(crate) fn debug(&self, msg: &str) {
        if self.log_level.is_trace_enabled() {
            tracing::debug!("[TRACE] {}", msg);
        }
    }
}