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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use tera::{Context as TeraContext, Filter, Tera, Value};
use crate::provider::{ChatRequest, ContentPart, Message, MessageContent};
/// Template processor for handling request/response transformations
#[derive(Clone)]
pub struct TemplateProcessor {
tera: Tera,
}
/// Endpoint-specific templates with model pattern support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EndpointTemplates {
/// Default template for all models
#[serde(default)]
pub template: Option<TemplateConfig>,
/// Model-specific templates (exact match)
#[serde(default)]
pub model_templates: HashMap<String, TemplateConfig>,
/// Model pattern templates (regex match)
#[serde(default)]
pub model_template_patterns: HashMap<String, TemplateConfig>,
}
/// Template configuration for request/response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateConfig {
/// Request transformation template
pub request: Option<String>,
/// Response parsing template
pub response: Option<String>,
/// Streaming response parsing template
pub stream_response: Option<String>,
}
/// Model-specific endpoint templates (for backward compatibility)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelEndpointTemplates {
#[serde(default)]
pub chat: Option<TemplateConfig>,
#[serde(default)]
pub images: Option<TemplateConfig>,
#[serde(default)]
pub embeddings: Option<TemplateConfig>,
}
impl EndpointTemplates {
/// Get template for a specific model, checking patterns and defaults
#[allow(dead_code)]
pub fn get_template_for_model(&self, model_name: &str, template_type: &str) -> Option<String> {
// First check exact match
if let Some(template) = self.model_templates.get(model_name) {
return match template_type {
"request" => template.request.clone(),
"response" => template.response.clone(),
"stream_response" => template.stream_response.clone(),
_ => None,
};
}
// Then check regex patterns
for (pattern, template) in &self.model_template_patterns {
if let Ok(re) = regex::Regex::new(pattern) {
if re.is_match(model_name) {
return match template_type {
"request" => template.request.clone(),
"response" => template.response.clone(),
"stream_response" => template.stream_response.clone(),
_ => None,
};
}
}
}
// Finally fall back to default template
if let Some(template) = &self.template {
return match template_type {
"request" => template.request.clone(),
"response" => template.response.clone(),
"stream_response" => template.stream_response.clone(),
_ => None,
};
}
None
}
}
impl TemplateProcessor {
/// Create a new template processor
pub fn new() -> Result<Self> {
let mut tera = Tera::default();
// Register custom filters
tera.register_filter("json", JsonFilter);
tera.register_filter("gemini_role", GeminiRoleFilter);
tera.register_filter("system_to_user_role", SystemToUserRoleFilter);
tera.register_filter("default", DefaultFilter);
tera.register_filter("select_tool_calls", SelectToolCallsFilter);
tera.register_filter("from_json", FromJsonFilter);
tera.register_filter("selectattr", SelectAttrFilter);
tera.register_filter("base_messages", BaseMessagesFilter);
tera.register_filter("anthropic_messages", AnthropicMessagesFilter);
tera.register_filter("gemini_messages", GeminiMessagesFilter);
Ok(Self { tera })
}
/// Render a template directly
#[allow(dead_code)]
pub fn render_template(
&mut self,
name: &str,
template: &str,
context: &TeraContext,
) -> Result<String> {
self.tera.add_raw_template(name, template)?;
Ok(self.tera.render(name, context)?)
}
/// Process a chat request using the provided template
pub fn process_request(
&mut self,
request: &ChatRequest,
template: &str,
provider_vars: &HashMap<String, String>,
) -> Result<JsonValue> {
// Add template to Tera
self.tera
.add_raw_template("request", template)
.context("Failed to parse request template")?;
// Build context from ChatRequest
let mut context = TeraContext::new();
// Add basic fields
context.insert("model", &request.model);
context.insert("max_tokens", &request.max_tokens);
context.insert("temperature", &request.temperature);
context.insert("stream", &request.stream);
context.insert("tools", &request.tools);
// Process messages into a format suitable for templates
let processed_messages = self.process_messages(&request.messages)?;
context.insert("messages", &processed_messages);
// Extract system prompt if present
if let Some(system_msg) = request.messages.iter().find(|m| m.role == "system") {
if let Some(content) = system_msg.get_text_content() {
context.insert("system_prompt", content);
}
}
// Add provider-specific variables
for (key, value) in provider_vars {
context.insert(key, value);
}
// Render template
let rendered = self
.tera
.render("request", &context)
.context("Failed to render request template")?;
// Parse as JSON to validate
let json_value: JsonValue =
serde_json::from_str(&rendered).context("Template did not produce valid JSON")?;
Ok(json_value)
}
/// Process an image generation request using the provided template
pub fn process_image_request(
&mut self,
request: &crate::provider::ImageGenerationRequest,
template: &str,
provider_vars: &HashMap<String, String>,
) -> Result<JsonValue> {
// Add template to Tera
self.tera
.add_raw_template("image_request", template)
.context("Failed to parse image request template")?;
// Build context from ImageGenerationRequest
let mut context = TeraContext::new();
// Add basic fields
context.insert("prompt", &request.prompt);
context.insert("model", &request.model);
context.insert("n", &request.n);
context.insert("size", &request.size);
context.insert("quality", &request.quality);
context.insert("style", &request.style);
context.insert("response_format", &request.response_format);
// Add provider-specific variables
for (key, value) in provider_vars {
context.insert(key, value);
}
// Render template
let rendered = self
.tera
.render("image_request", &context)
.context("Failed to render image request template")?;
// Parse as JSON to validate
let json_value: JsonValue =
serde_json::from_str(&rendered).context("Image template did not produce valid JSON")?;
Ok(json_value)
}
/// Process an audio transcription request using the provided template
#[allow(dead_code)]
pub fn process_audio_request(
&mut self,
request: &crate::provider::AudioTranscriptionRequest,
template: &str,
provider_vars: &HashMap<String, String>,
) -> Result<JsonValue> {
// Add template to Tera
self.tera
.add_raw_template("audio_request", template)
.context("Failed to parse audio request template")?;
// Build context from AudioTranscriptionRequest
let mut context = TeraContext::new();
// Add basic fields
context.insert("file", &request.file);
context.insert("model", &request.model);
context.insert("language", &request.language);
context.insert("prompt", &request.prompt);
context.insert("response_format", &request.response_format);
context.insert("temperature", &request.temperature);
// Add provider-specific variables
for (key, value) in provider_vars {
context.insert(key, value);
}
// Render template
let rendered = self
.tera
.render("audio_request", &context)
.context("Failed to render audio request template")?;
// Parse as JSON to validate
let json_value: JsonValue =
serde_json::from_str(&rendered).context("Audio template did not produce valid JSON")?;
Ok(json_value)
}
/// Process a speech generation request using the provided template
pub fn process_speech_request(
&mut self,
request: &crate::provider::AudioSpeechRequest,
template: &str,
provider_vars: &HashMap<String, String>,
) -> Result<JsonValue> {
// Add template to Tera
self.tera
.add_raw_template("speech_request", template)
.context("Failed to parse speech request template")?;
// Build context from AudioSpeechRequest
let mut context = TeraContext::new();
// Add basic fields
context.insert("model", &request.model);
context.insert("input", &request.input);
context.insert("voice", &request.voice);
context.insert("response_format", &request.response_format);
context.insert("speed", &request.speed);
// Add provider-specific variables
for (key, value) in provider_vars {
context.insert(key, value);
}
// Render template
let rendered = self
.tera
.render("speech_request", &context)
.context("Failed to render speech request template")?;
// Parse as JSON to validate
let json_value: JsonValue = serde_json::from_str(&rendered)
.context("Speech template did not produce valid JSON")?;
Ok(json_value)
}
/// Process an embeddings request using the provided template
pub fn process_embeddings_request(
&mut self,
request: &crate::provider::EmbeddingRequest,
template: &str,
provider_vars: &HashMap<String, String>,
) -> Result<JsonValue> {
// Add template to Tera
self.tera
.add_raw_template("embeddings_request", template)
.context("Failed to parse embeddings request template")?;
// Build context from EmbeddingRequest
let mut context = TeraContext::new();
// Add basic fields
context.insert("model", &request.model);
context.insert("input", &request.input);
context.insert("encoding_format", &request.encoding_format);
// Add provider-specific variables
for (key, value) in provider_vars {
context.insert(key, value);
}
// Render template
let rendered = self
.tera
.render("embeddings_request", &context)
.context("Failed to render embeddings request template")?;
// Parse as JSON to validate
let json_value: JsonValue = serde_json::from_str(&rendered)
.context("Embeddings template did not produce valid JSON")?;
Ok(json_value)
}
/// Process a response using the provided template
pub fn process_response(&mut self, response: &JsonValue, template: &str) -> Result<JsonValue> {
// Add template to Tera
self.tera
.add_raw_template("response", template)
.context("Failed to parse response template")?;
// Build context from response
let context = TeraContext::from_serialize(response)
.context("Failed to serialize response to context")?;
// Render template
let rendered = self
.tera
.render("response", &context)
.context("Failed to render response template")?;
// Parse as JSON
let json_value: JsonValue = serde_json::from_str(&rendered)
.context("Response template did not produce valid JSON")?;
Ok(json_value)
}
/// Process messages into a format suitable for templates
fn process_messages(&self, messages: &[Message]) -> Result<Vec<ProcessedMessage>> {
let mut processed = Vec::new();
for message in messages {
let mut proc_msg = ProcessedMessage {
role: message.role.clone(),
content: None,
images: Vec::new(),
tool_calls: message.tool_calls.clone(),
tool_call_id: message.tool_call_id.clone(),
};
match &message.content_type {
MessageContent::Text { content } => {
proc_msg.content = content.clone();
}
MessageContent::Multimodal { content } => {
for part in content {
match part {
ContentPart::Text { text } => {
proc_msg.content = Some(text.clone());
}
ContentPart::ImageUrl { image_url } => {
// Extract base64 data and mime type from data URL
if let Some(data_url) = image_url.url.strip_prefix("data:") {
if let Some(comma_pos) = data_url.find(',') {
let header = &data_url[..comma_pos];
let data = &data_url[comma_pos + 1..];
let mime_type = if let Some(semi_pos) = header.find(';') {
header[..semi_pos].to_string()
} else {
header.to_string()
};
proc_msg.images.push(ProcessedImage {
mime_type,
data: data.to_string(),
url: image_url.url.clone(),
});
}
} else {
// Regular URL
proc_msg.images.push(ProcessedImage {
mime_type: "image/jpeg".to_string(), // Default
data: String::new(),
url: image_url.url.clone(),
});
}
}
}
}
}
}
processed.push(proc_msg);
}
Ok(processed)
}
}
/// Processed message format for templates
#[derive(Debug, Serialize)]
struct ProcessedMessage {
role: String,
content: Option<String>,
images: Vec<ProcessedImage>,
tool_calls: Option<Vec<crate::provider::ToolCall>>,
tool_call_id: Option<String>,
}
#[derive(Debug, Serialize)]
struct ProcessedImage {
mime_type: String,
data: String,
url: String,
}
/// Custom filter to convert values to JSON
struct JsonFilter;
impl Filter for JsonFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
match serde_json::to_string(&value) {
Ok(json_str) => Ok(Value::String(json_str)),
Err(e) => Err(tera::Error::msg(format!(
"Failed to serialize to JSON: {}",
e
))),
}
}
}
/// Filter to convert OpenAI roles to Gemini roles
struct GeminiRoleFilter;
impl Filter for GeminiRoleFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
match value.as_str() {
Some("user") => Ok(Value::String("user".to_string())),
Some("assistant") => Ok(Value::String("model".to_string())),
Some("system") => Ok(Value::String("user".to_string())), // Gemini handles system as user
Some(other) => Ok(Value::String(other.to_string())),
None => Ok(value.clone()),
}
}
}
/// Filter to convert system roles to user roles (for providers that don't support system roles)
struct SystemToUserRoleFilter;
impl Filter for SystemToUserRoleFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
match value.as_str() {
Some("system") => Ok(Value::String("user".to_string())), // Convert system to user
Some(other) => Ok(Value::String(other.to_string())),
None => Ok(value.clone()),
}
}
}
/// Filter to provide default values
struct DefaultFilter;
impl Filter for DefaultFilter {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> tera::Result<Value> {
if value.is_null() || (value.is_string() && value.as_str() == Some("")) {
if let Some(default_value) = args.get("value") {
Ok(default_value.clone())
} else {
Ok(Value::Null)
}
} else {
Ok(value.clone())
}
}
}
/// Filter to select items with tool calls
struct SelectToolCallsFilter;
impl Filter for SelectToolCallsFilter {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> tera::Result<Value> {
if let Some(array) = value.as_array() {
let key = args
.get("key")
.and_then(|v| v.as_str())
.unwrap_or("functionCall");
let filtered: Vec<Value> = array
.iter()
.filter(|item| {
item.as_object()
.map(|obj| obj.contains_key(key))
.unwrap_or(false)
})
.cloned()
.collect();
Ok(Value::Array(filtered))
} else {
Ok(Value::Array(vec![]))
}
}
}
/// Filter to parse JSON strings
struct FromJsonFilter;
impl Filter for FromJsonFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
if let Some(json_str) = value.as_str() {
match serde_json::from_str::<JsonValue>(json_str) {
Ok(parsed) => {
// Convert JsonValue to Tera Value
match serde_json::to_value(&parsed) {
Ok(tera_value) => Ok(tera_value),
Err(e) => Err(tera::Error::msg(format!(
"Failed to convert to Tera value: {}",
e
))),
}
}
Err(e) => Err(tera::Error::msg(format!("Failed to parse JSON: {}", e))),
}
} else {
Ok(value.clone())
}
}
}
/// Filter to select items by attribute value (simplified version of Jinja2's selectattr)
struct SelectAttrFilter;
impl Filter for SelectAttrFilter {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> tera::Result<Value> {
if let Some(array) = value.as_array() {
let attr_name = args
.get("attr")
.and_then(|v| v.as_str())
.ok_or_else(|| tera::Error::msg("selectattr filter requires 'attr' argument"))?;
let test_value = args
.get("value")
.ok_or_else(|| tera::Error::msg("selectattr filter requires 'value' argument"))?;
let filtered: Vec<Value> = array
.iter()
.filter(|item| {
if let Some(obj) = item.as_object() {
if let Some(attr_value) = obj.get(attr_name) {
attr_value == test_value
} else {
false
}
} else {
false
}
})
.cloned()
.collect();
Ok(Value::Array(filtered))
} else {
Ok(Value::Array(vec![]))
}
}
}
/// Filter to create base messages with only essential fields (role, content) for simple providers
struct BaseMessagesFilter;
impl Filter for BaseMessagesFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
if let Some(array) = value.as_array() {
let cleaned: Vec<Value> = array
.iter()
.map(|item| {
if let Some(obj) = item.as_object() {
let mut cleaned_obj = serde_json::Map::new();
// Only include non-null, non-empty fields that are commonly supported
for (key, value) in obj {
match key.as_str() {
"role" | "content" => {
// Always include role and content
cleaned_obj.insert(key.clone(), value.clone());
}
"tool_calls" => {
// Only include tool_calls if it's not null and not empty
if !value.is_null()
&& value.as_array().map_or(true, |arr| !arr.is_empty())
{
cleaned_obj.insert(key.clone(), value.clone());
}
}
"tool_call_id" => {
// Only include tool_call_id if it's not null and not empty
if !value.is_null()
&& value.as_str().map_or(false, |s| !s.is_empty())
{
cleaned_obj.insert(key.clone(), value.clone());
}
}
// Skip images and any other fields that might cause issues
_ => {}
}
}
Value::Object(cleaned_obj)
} else {
item.clone()
}
})
.collect();
Ok(Value::Array(cleaned))
} else {
Ok(value.clone())
}
}
}
/// Filter to convert messages to Anthropic's specific format with content arrays
struct AnthropicMessagesFilter;
impl Filter for AnthropicMessagesFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
if let Some(array) = value.as_array() {
let converted: Vec<Value> = array
.iter()
.map(|item| {
if let Some(obj) = item.as_object() {
let mut anthropic_msg = serde_json::Map::new();
// Always include role
if let Some(role) = obj.get("role") {
anthropic_msg.insert("role".to_string(), role.clone());
}
// Convert content to Anthropic's format
let mut content_parts = Vec::new();
// Add text content if present
if let Some(text_content) = obj.get("content") {
if !text_content.is_null()
&& text_content.as_str().map_or(false, |s| !s.is_empty())
{
let text_part = serde_json::json!({
"type": "text",
"text": text_content
});
content_parts.push(text_part);
}
}
// Add image content if present
if let Some(images) = obj.get("images") {
if let Some(images_array) = images.as_array() {
for image in images_array {
if let Some(image_obj) = image.as_object() {
if let (Some(data), Some(mime_type)) = (
image_obj.get("data").and_then(|v| v.as_str()),
image_obj.get("mime_type").and_then(|v| v.as_str()),
) {
if !data.is_empty() {
// Base64 image
let image_part = serde_json::json!({
"type": "image",
"source": {
"type": "base64",
"media_type": mime_type,
"data": data
}
});
content_parts.push(image_part);
}
} else if let Some(url) =
image_obj.get("url").and_then(|v| v.as_str())
{
if !url.starts_with("data:") && !url.is_empty() {
// URL image
let image_part = serde_json::json!({
"type": "image",
"source": {
"type": "url",
"url": url
}
});
content_parts.push(image_part);
}
}
}
}
}
}
// Set content as array if we have parts, otherwise as string
if content_parts.len() > 1
|| (content_parts.len() == 1
&& content_parts[0].get("type")
== Some(&serde_json::Value::String("image".to_string())))
{
anthropic_msg.insert(
"content".to_string(),
serde_json::Value::Array(content_parts),
);
} else if let Some(first_part) = content_parts.first() {
if let Some(text) = first_part.get("text") {
anthropic_msg.insert("content".to_string(), text.clone());
}
}
// Include tool_calls if present and not empty
if let Some(tool_calls) = obj.get("tool_calls") {
if !tool_calls.is_null()
&& tool_calls.as_array().map_or(true, |arr| !arr.is_empty())
{
anthropic_msg.insert("tool_calls".to_string(), tool_calls.clone());
}
}
// Include tool_call_id if present and not empty
if let Some(tool_call_id) = obj.get("tool_call_id") {
if !tool_call_id.is_null()
&& tool_call_id.as_str().map_or(false, |s| !s.is_empty())
{
anthropic_msg
.insert("tool_call_id".to_string(), tool_call_id.clone());
}
}
Value::Object(anthropic_msg)
} else {
item.clone()
}
})
.collect();
Ok(Value::Array(converted))
} else {
Ok(value.clone())
}
}
}
/// Filter to convert messages to Gemini's specific format with parts arrays
struct GeminiMessagesFilter;
impl Filter for GeminiMessagesFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
if let Some(array) = value.as_array() {
let converted: Vec<Value> = array
.iter()
.map(|item| {
if let Some(obj) = item.as_object() {
let mut gemini_msg = serde_json::Map::new();
// Convert role to Gemini format
if let Some(role) = obj.get("role").and_then(|v| v.as_str()) {
let gemini_role = match role {
"assistant" => "model",
"system" => "user", // Gemini handles system as user
other => other,
};
gemini_msg.insert(
"role".to_string(),
serde_json::Value::String(gemini_role.to_string()),
);
}
// Convert content to Gemini's parts format
let mut parts = Vec::new();
// Add text content if present
if let Some(text_content) = obj.get("content") {
if !text_content.is_null()
&& text_content.as_str().map_or(false, |s| !s.is_empty())
{
let text_part = serde_json::json!({
"text": text_content
});
parts.push(text_part);
}
}
// Add image content if present
if let Some(images) = obj.get("images") {
if let Some(images_array) = images.as_array() {
for image in images_array {
if let Some(image_obj) = image.as_object() {
if let (Some(data), Some(mime_type)) = (
image_obj.get("data").and_then(|v| v.as_str()),
image_obj.get("mime_type").and_then(|v| v.as_str()),
) {
if !data.is_empty() {
// Base64 image for Gemini
let image_part = serde_json::json!({
"inlineData": {
"mimeType": mime_type,
"data": data
}
});
parts.push(image_part);
}
}
}
}
}
}
// Set parts array
gemini_msg.insert("parts".to_string(), serde_json::Value::Array(parts));
// Include tool_calls if present and not empty (for function calling)
if let Some(tool_calls) = obj.get("tool_calls") {
if !tool_calls.is_null()
&& tool_calls.as_array().map_or(true, |arr| !arr.is_empty())
{
gemini_msg.insert("tool_calls".to_string(), tool_calls.clone());
}
}
// Include tool_call_id if present and not empty
if let Some(tool_call_id) = obj.get("tool_call_id") {
if !tool_call_id.is_null()
&& tool_call_id.as_str().map_or(false, |s| !s.is_empty())
{
gemini_msg.insert("tool_call_id".to_string(), tool_call_id.clone());
}
}
Value::Object(gemini_msg)
} else {
item.clone()
}
})
.collect();
Ok(Value::Array(converted))
} else {
Ok(value.clone())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_json_filter() {
let filter = JsonFilter;
let value = Value::String("test".to_string());
let args = HashMap::new();
let result = filter.filter(&value, &args).unwrap();
assert_eq!(result, Value::String("\"test\"".to_string()));
}
#[test]
fn test_gemini_role_filter() {
let filter = GeminiRoleFilter;
let args = HashMap::new();
let value = Value::String("assistant".to_string());
let result = filter.filter(&value, &args).unwrap();
assert_eq!(result, Value::String("model".to_string()));
let value = Value::String("system".to_string());
let result = filter.filter(&value, &args).unwrap();
assert_eq!(result, Value::String("user".to_string()));
}
#[test]
fn test_default_filter() {
let filter = DefaultFilter;
let mut args = HashMap::new();
args.insert("value".to_string(), Value::String("default".to_string()));
let value = Value::Null;
let result = filter.filter(&value, &args).unwrap();
assert_eq!(result, Value::String("default".to_string()));
let value = Value::String("existing".to_string());
let result = filter.filter(&value, &args).unwrap();
assert_eq!(result, Value::String("existing".to_string()));
}
}