llmkit 0.1.3

Production-grade LLM client - 100+ providers, 11,000+ models. Pure Rust.
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
//! OpenRouter API provider implementation.
//!
//! OpenRouter provides access to 100+ models through a single API endpoint.
//! It uses an OpenAI-compatible format with some extensions.

use std::pin::Pin;

use async_trait::async_trait;
use futures::Stream;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::{Error, Result};
use crate::provider::{Provider, ProviderConfig};
use crate::types::{
    CompletionRequest, CompletionResponse, ContentBlock, ContentDelta, Message, Role, StopReason,
    StreamChunk, StreamEventType, ThinkingEffort, ThinkingType, Usage,
};

const OPENROUTER_API_URL: &str = "https://openrouter.ai/api/v1/chat/completions";

/// OpenRouter API provider.
///
/// OpenRouter provides access to models from multiple providers including:
/// - Anthropic (Claude)
/// - OpenAI (GPT)
/// - Google (Gemini)
/// - Meta (Llama)
/// - Mistral
/// - And many more
pub struct OpenRouterProvider {
    config: ProviderConfig,
    client: Client,
    /// Optional app name for OpenRouter analytics
    app_name: Option<String>,
    /// Optional site URL for OpenRouter analytics
    site_url: Option<String>,
}

impl OpenRouterProvider {
    /// Create a new OpenRouter provider with the given configuration.
    pub fn new(config: ProviderConfig) -> Result<Self> {
        let mut headers = reqwest::header::HeaderMap::new();

        if let Some(ref key) = config.api_key {
            headers.insert(
                reqwest::header::AUTHORIZATION,
                format!("Bearer {}", key)
                    .parse()
                    .map_err(|_| Error::config("Invalid API key format"))?,
            );
        }

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

        // Add custom headers
        for (key, value) in &config.custom_headers {
            headers.insert(
                reqwest::header::HeaderName::try_from(key.as_str())
                    .map_err(|_| Error::config(format!("Invalid header name: {}", key)))?,
                value
                    .parse()
                    .map_err(|_| Error::config(format!("Invalid header value for {}", key)))?,
            );
        }

        let client = Client::builder()
            .timeout(config.timeout)
            .default_headers(headers)
            .build()?;

        Ok(Self {
            config,
            client,
            app_name: None,
            site_url: None,
        })
    }

    /// Create a new OpenRouter provider from environment variable.
    pub fn from_env() -> Result<Self> {
        let config = ProviderConfig::from_env("OPENROUTER_API_KEY");
        Self::new(config)
    }

    /// Create a new OpenRouter provider with an API key.
    pub fn with_api_key(api_key: impl Into<String>) -> Result<Self> {
        let config = ProviderConfig::new(api_key);
        Self::new(config)
    }

    /// Set the app name for OpenRouter analytics.
    pub fn with_app_name(mut self, name: impl Into<String>) -> Self {
        self.app_name = Some(name.into());
        self
    }

    /// Set the site URL for OpenRouter analytics.
    pub fn with_site_url(mut self, url: impl Into<String>) -> Self {
        self.site_url = Some(url.into());
        self
    }

    fn api_url(&self) -> &str {
        self.config
            .base_url
            .as_deref()
            .unwrap_or(OPENROUTER_API_URL)
    }

    /// Convert our unified request to OpenRouter's format.
    fn convert_request(&self, request: &CompletionRequest) -> OpenRouterRequest {
        let mut messages: Vec<OpenRouterMessage> = Vec::new();

        // Check if we need to inject /no_think for Qwen3 models
        // OpenRouter's reasoning.effort: "none" doesn't work for Qwen3, so we need the prompt directive
        let is_qwen3 = request.model.to_lowercase().contains("qwen3");
        let thinking_disabled = request
            .thinking
            .as_ref()
            .map(|t| !t.is_enabled())
            .unwrap_or(false);
        let inject_no_think = is_qwen3 && thinking_disabled;

        // Add system message if present (with /no_think prefix for Qwen3 if needed)
        if let Some(ref system) = request.system {
            let system_content = if inject_no_think {
                format!("/no_think\n\n{}", system)
            } else {
                system.clone()
            };
            messages.push(OpenRouterMessage {
                role: "system".to_string(),
                content: Some(OpenRouterContent::Text(system_content)),
                tool_calls: None,
                tool_call_id: None,
            });
        } else if inject_no_think {
            // No system message but we need /no_think - create a minimal system message
            messages.push(OpenRouterMessage {
                role: "system".to_string(),
                content: Some(OpenRouterContent::Text("/no_think".to_string())),
                tool_calls: None,
                tool_call_id: None,
            });
        }

        // Convert messages
        for msg in &request.messages {
            messages.extend(self.convert_message(msg));
        }

        // Convert tools
        let tools = request.tools.as_ref().map(|tools| {
            tools
                .iter()
                .map(|t| OpenRouterTool {
                    tool_type: "function".to_string(),
                    function: OpenRouterFunction {
                        name: t.name.clone(),
                        description: Some(t.description.clone()),
                        parameters: t.input_schema.clone(),
                    },
                })
                .collect()
        });

        // Convert response format for structured output (passthrough to underlying provider)
        let response_format = request.response_format.as_ref().map(|rf| {
            use crate::types::StructuredOutputType;
            match rf.format_type {
                StructuredOutputType::JsonObject => ORResponseFormat::JsonObject,
                StructuredOutputType::JsonSchema => {
                    if let Some(ref schema_def) = rf.json_schema {
                        ORResponseFormat::JsonSchema {
                            json_schema: ORJsonSchema {
                                name: schema_def.name.clone(),
                                description: schema_def.description.clone(),
                                schema: schema_def.schema.clone(),
                                strict: Some(schema_def.strict),
                            },
                        }
                    } else {
                        ORResponseFormat::JsonObject
                    }
                }
                StructuredOutputType::Text => ORResponseFormat::Text,
            }
        });

        // Convert ThinkingConfig to OpenRouter reasoning parameter
        let reasoning = request.thinking.as_ref().map(|thinking| {
            let effort = match thinking.thinking_type {
                ThinkingType::Disabled => Some("none".to_string()),
                ThinkingType::Enabled => thinking.effort.as_ref().map(|e| match e {
                    ThinkingEffort::Low => "low".to_string(),
                    ThinkingEffort::Medium => "medium".to_string(),
                    ThinkingEffort::High => "high".to_string(),
                    ThinkingEffort::Max => "max".to_string(),
                }),
            };

            OpenRouterReasoning {
                effort,
                max_tokens: thinking.budget_tokens,
                exclude: if thinking.exclude_from_response {
                    Some(true)
                } else {
                    None
                },
            }
        });

        OpenRouterRequest {
            model: request.model.clone(),
            messages,
            max_tokens: request.max_tokens,
            temperature: request.temperature,
            top_p: request.top_p,
            stop: request.stop_sequences.clone(),
            stream: request.stream,
            tools,
            stream_options: if request.stream {
                Some(StreamOptions {
                    include_usage: true,
                })
            } else {
                None
            },
            response_format,
            // OpenRouter specific
            transforms: None,
            route: None,
            reasoning,
        }
    }

    fn convert_message(&self, message: &Message) -> Vec<OpenRouterMessage> {
        let mut result = Vec::new();

        match message.role {
            Role::System => {
                let text = message.text_content();
                if !text.is_empty() {
                    result.push(OpenRouterMessage {
                        role: "system".to_string(),
                        content: Some(OpenRouterContent::Text(text)),
                        tool_calls: None,
                        tool_call_id: None,
                    });
                }
            }
            Role::User => {
                // Check for tool results
                let tool_results: Vec<_> = message
                    .content
                    .iter()
                    .filter_map(|b| match b {
                        ContentBlock::ToolResult {
                            tool_use_id,
                            content,
                            ..
                        } => Some((tool_use_id.clone(), content.clone())),
                        _ => None,
                    })
                    .collect();

                if !tool_results.is_empty() {
                    for (tool_call_id, content) in tool_results {
                        result.push(OpenRouterMessage {
                            role: "tool".to_string(),
                            content: Some(OpenRouterContent::Text(content)),
                            tool_calls: None,
                            tool_call_id: Some(tool_call_id),
                        });
                    }
                } else {
                    let content_parts: Vec<OpenRouterContentPart> = message
                        .content
                        .iter()
                        .filter_map(|block| match block {
                            ContentBlock::Text { text } => {
                                Some(OpenRouterContentPart::Text { text: text.clone() })
                            }
                            ContentBlock::Image { media_type, data } => {
                                Some(OpenRouterContentPart::ImageUrl {
                                    image_url: ImageUrl {
                                        url: format!("data:{};base64,{}", media_type, data),
                                    },
                                })
                            }
                            ContentBlock::ImageUrl { url } => {
                                Some(OpenRouterContentPart::ImageUrl {
                                    image_url: ImageUrl { url: url.clone() },
                                })
                            }
                            _ => None,
                        })
                        .collect();

                    if content_parts.len() == 1 {
                        if let OpenRouterContentPart::Text { text } = &content_parts[0] {
                            result.push(OpenRouterMessage {
                                role: "user".to_string(),
                                content: Some(OpenRouterContent::Text(text.clone())),
                                tool_calls: None,
                                tool_call_id: None,
                            });
                        } else {
                            result.push(OpenRouterMessage {
                                role: "user".to_string(),
                                content: Some(OpenRouterContent::Parts(content_parts)),
                                tool_calls: None,
                                tool_call_id: None,
                            });
                        }
                    } else if !content_parts.is_empty() {
                        result.push(OpenRouterMessage {
                            role: "user".to_string(),
                            content: Some(OpenRouterContent::Parts(content_parts)),
                            tool_calls: None,
                            tool_call_id: None,
                        });
                    }
                }
            }
            Role::Assistant => {
                let tool_calls: Vec<OpenRouterToolCall> = message
                    .content
                    .iter()
                    .filter_map(|block| match block {
                        ContentBlock::ToolUse { id, name, input } => Some(OpenRouterToolCall {
                            id: id.clone(),
                            call_type: "function".to_string(),
                            function: OpenRouterFunctionCall {
                                name: name.clone(),
                                arguments: input.to_string(),
                            },
                        }),
                        _ => None,
                    })
                    .collect();

                let text_content = message.text_content();

                result.push(OpenRouterMessage {
                    role: "assistant".to_string(),
                    content: if text_content.is_empty() {
                        None
                    } else {
                        Some(OpenRouterContent::Text(text_content))
                    },
                    tool_calls: if tool_calls.is_empty() {
                        None
                    } else {
                        Some(tool_calls)
                    },
                    tool_call_id: None,
                });
            }
        }

        result
    }

    fn convert_response(&self, response: OpenRouterResponse) -> CompletionResponse {
        let choice = response.choices.into_iter().next().unwrap_or_default();
        let mut content = Vec::new();

        if let Some(text) = choice.message.content {
            content.push(ContentBlock::Text { text });
        }

        if let Some(tool_calls) = choice.message.tool_calls {
            for tc in tool_calls {
                let input = serde_json::from_str(&tc.function.arguments)
                    .unwrap_or_else(|_| Value::Object(serde_json::Map::new()));
                content.push(ContentBlock::ToolUse {
                    id: tc.id,
                    name: tc.function.name,
                    input,
                });
            }
        }

        let stop_reason = match choice.finish_reason.as_deref() {
            Some("stop") => StopReason::EndTurn,
            Some("length") => StopReason::MaxTokens,
            Some("tool_calls") => StopReason::ToolUse,
            Some("content_filter") => StopReason::ContentFilter,
            _ => StopReason::EndTurn,
        };

        let (input_tokens, output_tokens) = match response.usage {
            Some(u) => (u.prompt_tokens, u.completion_tokens),
            None => (0, 0),
        };

        CompletionResponse {
            id: response.id,
            model: response.model.unwrap_or_default(),
            content,
            stop_reason,
            usage: Usage {
                input_tokens,
                output_tokens,
                cache_creation_input_tokens: 0,
                cache_read_input_tokens: 0,
            },
        }
    }

    async fn handle_error_response(&self, response: reqwest::Response) -> Error {
        let status = response.status().as_u16();

        match response.json::<OpenRouterErrorResponse>().await {
            Ok(err) => {
                let code = err.error.code.unwrap_or(status as i32);
                let message = &err.error.message;

                match code {
                    401 => Error::auth(message),
                    429 => Error::rate_limited(message, None),
                    400 => Error::invalid_request(message),
                    404 => Error::ModelNotFound(message.clone()),
                    _ if status >= 500 => Error::server(status, message),
                    _ => Error::other(message),
                }
            }
            Err(_) => Error::server(status, "Unknown error"),
        }
    }
}

#[async_trait]
impl Provider for OpenRouterProvider {
    fn name(&self) -> &str {
        "openrouter"
    }

    async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse> {
        self.config.require_api_key()?;

        let mut api_request = self.convert_request(&request);
        api_request.stream = false;

        let mut req_builder = self.client.post(self.api_url()).json(&api_request);

        // Add OpenRouter specific headers
        if let Some(ref app_name) = self.app_name {
            req_builder = req_builder.header("X-Title", app_name);
        }
        if let Some(ref site_url) = self.site_url {
            req_builder = req_builder.header("HTTP-Referer", site_url);
        }

        let response = req_builder.send().await?;

        if !response.status().is_success() {
            return Err(self.handle_error_response(response).await);
        }

        let openrouter_response: OpenRouterResponse = response.json().await?;
        Ok(self.convert_response(openrouter_response))
    }

    async fn complete_stream(
        &self,
        request: CompletionRequest,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>> {
        self.config.require_api_key()?;

        let mut api_request = self.convert_request(&request);
        api_request.stream = true;

        let mut req_builder = self.client.post(self.api_url()).json(&api_request);

        if let Some(ref app_name) = self.app_name {
            req_builder = req_builder.header("X-Title", app_name);
        }
        if let Some(ref site_url) = self.site_url {
            req_builder = req_builder.header("HTTP-Referer", site_url);
        }

        let response = req_builder.send().await?;

        if !response.status().is_success() {
            return Err(self.handle_error_response(response).await);
        }

        let stream = parse_openrouter_stream(response);
        Ok(Box::pin(stream))
    }

    fn supports_tools(&self) -> bool {
        true // Most models on OpenRouter support tools
    }

    fn supports_vision(&self) -> bool {
        true // Many models support vision
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    fn default_model(&self) -> Option<&str> {
        Some("anthropic/claude-3.5-sonnet")
    }
}

/// Parse OpenRouter SSE stream (OpenAI-compatible format).
fn parse_openrouter_stream(response: reqwest::Response) -> impl Stream<Item = Result<StreamChunk>> {
    use async_stream::try_stream;
    use futures::StreamExt;

    try_stream! {
        let mut event_stream = response.bytes_stream();
        let mut buffer = String::new();
        let mut sent_start = false;

        while let Some(chunk) = event_stream.next().await {
            let chunk = chunk?;
            buffer.push_str(&String::from_utf8_lossy(&chunk));

            while let Some(pos) = buffer.find('\n') {
                let line = buffer[..pos].trim().to_string();
                buffer = buffer[pos + 1..].to_string();

                if line.is_empty() || !line.starts_with("data: ") {
                    continue;
                }

                let data = &line[6..];

                if data == "[DONE]" {
                    yield StreamChunk {
                        event_type: StreamEventType::MessageStop,
                        index: None,
                        delta: None,
                        stop_reason: None,
                        usage: None,
                    };
                    continue;
                }

                if let Ok(parsed) = serde_json::from_str::<OpenRouterStreamResponse>(data) {
                    if !sent_start {
                        yield StreamChunk {
                            event_type: StreamEventType::MessageStart,
                            index: None,
                            delta: None,
                            stop_reason: None,
                            usage: None,
                        };
                        sent_start = true;
                    }

                    for choice in &parsed.choices {
                        if let Some(ref content) = choice.delta.content {
                            yield StreamChunk {
                                event_type: StreamEventType::ContentBlockDelta,
                                index: Some(0),
                                delta: Some(ContentDelta::Text { text: content.clone() }),
                                stop_reason: None,
                                usage: None,
                            };
                        }

                        if let Some(ref tool_calls) = choice.delta.tool_calls {
                            for tc in tool_calls {
                                let idx = tc.index.unwrap_or(0);
                                yield StreamChunk {
                                    event_type: StreamEventType::ContentBlockDelta,
                                    index: Some(idx + 1),
                                    delta: Some(ContentDelta::ToolUse {
                                        id: tc.id.clone(),
                                        name: tc.function.as_ref().and_then(|f| f.name.clone()),
                                        input_json_delta: tc.function.as_ref().and_then(|f| f.arguments.clone()),
                                    }),
                                    stop_reason: None,
                                    usage: None,
                                };
                            }
                        }

                        if let Some(ref reason) = choice.finish_reason {
                            let stop_reason = match reason.as_str() {
                                "stop" => StopReason::EndTurn,
                                "length" => StopReason::MaxTokens,
                                "tool_calls" => StopReason::ToolUse,
                                "content_filter" => StopReason::ContentFilter,
                                _ => StopReason::EndTurn,
                            };

                            yield StreamChunk {
                                event_type: StreamEventType::MessageDelta,
                                index: None,
                                delta: None,
                                stop_reason: Some(stop_reason),
                                usage: None,
                            };
                        }
                    }

                    if let Some(ref usage) = parsed.usage {
                        yield StreamChunk {
                            event_type: StreamEventType::MessageDelta,
                            index: None,
                            delta: None,
                            stop_reason: None,
                            usage: Some(Usage {
                                input_tokens: usage.prompt_tokens,
                                output_tokens: usage.completion_tokens,
                                cache_creation_input_tokens: 0,
                                cache_read_input_tokens: 0,
                            }),
                        };
                    }
                }
            }
        }
    }
}

// OpenRouter API types (mostly OpenAI-compatible)

#[derive(Debug, Serialize)]
struct OpenRouterRequest {
    model: String,
    messages: Vec<OpenRouterMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    top_p: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stop: Option<Vec<String>>,
    stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    tools: Option<Vec<OpenRouterTool>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stream_options: Option<StreamOptions>,
    #[serde(skip_serializing_if = "Option::is_none")]
    response_format: Option<ORResponseFormat>,
    // OpenRouter specific
    #[serde(skip_serializing_if = "Option::is_none")]
    transforms: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    route: Option<String>,
    /// Reasoning/thinking control for models that support it
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning: Option<OpenRouterReasoning>,
}

/// OpenRouter reasoning configuration.
/// See: https://openrouter.ai/docs/guides/best-practices/reasoning-tokens
#[derive(Debug, Serialize)]
struct OpenRouterReasoning {
    /// Effort level for reasoning: "none", "low", "medium", "high", "max"
    #[serde(skip_serializing_if = "Option::is_none")]
    effort: Option<String>,
    /// Maximum tokens for reasoning
    #[serde(skip_serializing_if = "Option::is_none")]
    max_tokens: Option<u32>,
    /// If true, reasoning is performed but excluded from the response
    #[serde(skip_serializing_if = "Option::is_none")]
    exclude: Option<bool>,
}

/// Response format for structured outputs (OpenAI-compatible passthrough).
#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ORResponseFormat {
    Text,
    JsonObject,
    JsonSchema { json_schema: ORJsonSchema },
}

/// JSON schema for structured output.
#[derive(Debug, Serialize)]
struct ORJsonSchema {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    schema: serde_json::Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    strict: Option<bool>,
}

#[derive(Debug, Serialize)]
struct StreamOptions {
    include_usage: bool,
}

#[derive(Debug, Serialize)]
struct OpenRouterMessage {
    role: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<OpenRouterContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_calls: Option<Vec<OpenRouterToolCall>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_call_id: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(untagged)]
enum OpenRouterContent {
    Text(String),
    Parts(Vec<OpenRouterContentPart>),
}

#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum OpenRouterContentPart {
    Text { text: String },
    ImageUrl { image_url: ImageUrl },
}

#[derive(Debug, Serialize)]
struct ImageUrl {
    url: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct OpenRouterTool {
    #[serde(rename = "type")]
    tool_type: String,
    function: OpenRouterFunction,
}

#[derive(Debug, Serialize, Deserialize)]
struct OpenRouterFunction {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    parameters: Value,
}

#[derive(Debug, Serialize, Deserialize)]
struct OpenRouterToolCall {
    id: String,
    #[serde(rename = "type")]
    call_type: String,
    function: OpenRouterFunctionCall,
}

#[derive(Debug, Serialize, Deserialize)]
struct OpenRouterFunctionCall {
    name: String,
    arguments: String,
}

#[derive(Debug, Deserialize)]
struct OpenRouterResponse {
    id: String,
    model: Option<String>,
    choices: Vec<OpenRouterChoice>,
    usage: Option<OpenRouterUsage>,
}

#[derive(Debug, Default, Deserialize)]
struct OpenRouterChoice {
    message: OpenRouterResponseMessage,
    finish_reason: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
struct OpenRouterResponseMessage {
    content: Option<String>,
    tool_calls: Option<Vec<OpenRouterToolCall>>,
}

#[derive(Debug, Deserialize)]
struct OpenRouterStreamResponse {
    choices: Vec<OpenRouterStreamChoice>,
    usage: Option<OpenRouterUsage>,
}

#[derive(Debug, Deserialize)]
struct OpenRouterStreamChoice {
    delta: OpenRouterStreamDelta,
    finish_reason: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
struct OpenRouterStreamDelta {
    content: Option<String>,
    tool_calls: Option<Vec<OpenRouterStreamToolCall>>,
}

#[derive(Debug, Deserialize)]
struct OpenRouterStreamToolCall {
    index: Option<usize>,
    id: Option<String>,
    function: Option<OpenRouterStreamFunction>,
}

#[derive(Debug, Deserialize)]
struct OpenRouterStreamFunction {
    name: Option<String>,
    arguments: Option<String>,
}

#[derive(Debug, Deserialize)]
struct OpenRouterUsage {
    prompt_tokens: u32,
    completion_tokens: u32,
}

#[derive(Debug, Deserialize)]
struct OpenRouterErrorResponse {
    error: OpenRouterError,
}

#[derive(Debug, Deserialize)]
struct OpenRouterError {
    code: Option<i32>,
    message: String,
}

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

    #[test]
    fn test_provider_creation() {
        let provider = OpenRouterProvider::with_api_key("test-key").unwrap();
        assert_eq!(provider.name(), "openrouter");
        assert!(provider.supports_tools());
        assert!(provider.supports_vision());
        assert!(provider.supports_streaming());
    }

    #[test]
    fn test_provider_with_app_name() {
        let provider = OpenRouterProvider::with_api_key("test-key")
            .unwrap()
            .with_app_name("MyApp");
        assert_eq!(provider.app_name, Some("MyApp".to_string()));
    }

    #[test]
    fn test_provider_with_site_url() {
        let provider = OpenRouterProvider::with_api_key("test-key")
            .unwrap()
            .with_site_url("https://myapp.com");
        assert_eq!(provider.site_url, Some("https://myapp.com".to_string()));
    }

    #[test]
    fn test_api_url() {
        let provider = OpenRouterProvider::with_api_key("test-key").unwrap();
        assert_eq!(provider.api_url(), OPENROUTER_API_URL);
    }

    #[test]
    fn test_api_url_custom_base() {
        let mut config = ProviderConfig::new("test-key");
        config.base_url = Some("https://custom.openrouter.ai".to_string());
        let provider = OpenRouterProvider::new(config).unwrap();
        assert_eq!(provider.api_url(), "https://custom.openrouter.ai");
    }

    #[test]
    fn test_default_model() {
        let provider = OpenRouterProvider::with_api_key("test-key").unwrap();
        assert_eq!(
            provider.default_model(),
            Some("anthropic/claude-3.5-sonnet")
        );
    }

    #[test]
    fn test_request_conversion() {
        let provider = OpenRouterProvider::with_api_key("test-key").unwrap();
        let request =
            CompletionRequest::new("anthropic/claude-3.5-sonnet", vec![Message::user("Hello")])
                .with_system("You are helpful")
                .with_max_tokens(1024)
                .with_temperature(0.7);

        let openrouter_req = provider.convert_request(&request);

        assert_eq!(openrouter_req.model, "anthropic/claude-3.5-sonnet");
        assert_eq!(openrouter_req.max_tokens, Some(1024));
        assert_eq!(openrouter_req.temperature, Some(0.7));
        assert_eq!(openrouter_req.messages.len(), 2); // system + user
    }

    #[test]
    fn test_response_parsing() {
        let provider = OpenRouterProvider::with_api_key("test-key").unwrap();

        let response = OpenRouterResponse {
            id: "resp-123".to_string(),
            model: Some("anthropic/claude-3.5-sonnet".to_string()),
            choices: vec![OpenRouterChoice {
                message: OpenRouterResponseMessage {
                    content: Some("Hello! How can I help?".to_string()),
                    tool_calls: None,
                },
                finish_reason: Some("stop".to_string()),
            }],
            usage: Some(OpenRouterUsage {
                prompt_tokens: 10,
                completion_tokens: 15,
            }),
        };

        let result = provider.convert_response(response);

        assert_eq!(result.id, "resp-123");
        assert_eq!(result.model, "anthropic/claude-3.5-sonnet");
        assert_eq!(result.content.len(), 1);
        match &result.content[0] {
            ContentBlock::Text { text } => {
                assert_eq!(text, "Hello! How can I help?");
            }
            other => {
                panic!("Expected text content block, got {:?}", other);
            }
        }
        assert!(matches!(result.stop_reason, StopReason::EndTurn));
        assert_eq!(result.usage.input_tokens, 10);
        assert_eq!(result.usage.output_tokens, 15);
    }

    #[test]
    fn test_stop_reason_mapping() {
        let provider = OpenRouterProvider::with_api_key("test-key").unwrap();

        // Test "stop" -> EndTurn
        let response1 = OpenRouterResponse {
            id: "1".to_string(),
            model: Some("model".to_string()),
            choices: vec![OpenRouterChoice {
                message: OpenRouterResponseMessage {
                    content: Some("Done".to_string()),
                    tool_calls: None,
                },
                finish_reason: Some("stop".to_string()),
            }],
            usage: None,
        };
        assert!(matches!(
            provider.convert_response(response1).stop_reason,
            StopReason::EndTurn
        ));

        // Test "length" -> MaxTokens
        let response2 = OpenRouterResponse {
            id: "2".to_string(),
            model: Some("model".to_string()),
            choices: vec![OpenRouterChoice {
                message: OpenRouterResponseMessage {
                    content: Some("Truncated".to_string()),
                    tool_calls: None,
                },
                finish_reason: Some("length".to_string()),
            }],
            usage: None,
        };
        assert!(matches!(
            provider.convert_response(response2).stop_reason,
            StopReason::MaxTokens
        ));

        // Test "tool_calls" -> ToolUse
        let response3 = OpenRouterResponse {
            id: "3".to_string(),
            model: Some("model".to_string()),
            choices: vec![OpenRouterChoice {
                message: OpenRouterResponseMessage {
                    content: None,
                    tool_calls: None,
                },
                finish_reason: Some("tool_calls".to_string()),
            }],
            usage: None,
        };
        assert!(matches!(
            provider.convert_response(response3).stop_reason,
            StopReason::ToolUse
        ));

        // Test "content_filter" -> ContentFilter
        let response4 = OpenRouterResponse {
            id: "4".to_string(),
            model: Some("model".to_string()),
            choices: vec![OpenRouterChoice {
                message: OpenRouterResponseMessage {
                    content: Some("Filtered".to_string()),
                    tool_calls: None,
                },
                finish_reason: Some("content_filter".to_string()),
            }],
            usage: None,
        };
        assert!(matches!(
            provider.convert_response(response4).stop_reason,
            StopReason::ContentFilter
        ));
    }

    #[test]
    fn test_request_serialization() {
        let request = OpenRouterRequest {
            model: "anthropic/claude-3.5-sonnet".to_string(),
            messages: vec![OpenRouterMessage {
                role: "user".to_string(),
                content: Some(OpenRouterContent::Text("Hello".to_string())),
                tool_calls: None,
                tool_call_id: None,
            }],
            max_tokens: Some(1024),
            temperature: Some(0.7),
            top_p: None,
            stop: None,
            stream: false,
            tools: None,
            stream_options: None,
            response_format: None,
            transforms: None,
            route: None,
            reasoning: None,
        };

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("anthropic/claude-3.5-sonnet"));
        assert!(json.contains("Hello"));
    }

    #[test]
    fn test_response_deserialization() {
        let json = r#"{
            "id": "test-id",
            "model": "anthropic/claude-3.5-sonnet",
            "choices": [{
                "message": {"content": "Hello!"},
                "finish_reason": "stop"
            }],
            "usage": {"prompt_tokens": 10, "completion_tokens": 5}
        }"#;

        let response: OpenRouterResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.id, "test-id");
        assert_eq!(
            response.model,
            Some("anthropic/claude-3.5-sonnet".to_string())
        );
        assert_eq!(
            response.choices[0].message.content,
            Some("Hello!".to_string())
        );
    }

    #[test]
    fn test_error_response_deserialization() {
        let json = r#"{"error": {"code": 401, "message": "Unauthorized"}}"#;
        let error: OpenRouterErrorResponse = serde_json::from_str(json).unwrap();
        assert_eq!(error.error.code, Some(401));
        assert_eq!(error.error.message, "Unauthorized");
    }
}