1use std::collections::BTreeMap;
22
23use serde::{Deserialize, Serialize};
24use serde_json::{Map, Value};
25
26use crate::completion::provider_options::reply_field;
27use crate::completion::{ExtensionOptions, ProviderExtension, ReplyExtras};
28use crate::message::Api;
29
30#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
32pub struct OpenRouterExt;
33
34impl ProviderExtension for OpenRouterExt {
35 const PROVIDER: &'static str = super::PROVIDER_NAME;
36 type Options = OpenRouterOptions;
37 type Extras = OpenRouterExtras;
38}
39
40#[non_exhaustive]
42#[derive(Clone, Debug, Default, PartialEq, Serialize)]
43pub struct OpenRouterOptions {
44 #[serde(rename = "*")]
46 pub shared: OpenRouterShared,
47}
48
49#[non_exhaustive]
51#[derive(Clone, Debug, Default, PartialEq, Serialize)]
52pub struct OpenRouterShared {
53 #[serde(skip_serializing_if = "Option::is_none")]
55 pub provider: Option<ProviderPreferences>,
56 #[serde(skip_serializing_if = "Option::is_none")]
58 pub models: Option<ModelFallbacks>,
59 #[serde(skip_serializing_if = "Vec::is_empty")]
61 pub plugins: Vec<Value>,
62 #[serde(skip_serializing_if = "Option::is_none")]
64 pub session_id: Option<String>,
65 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
67 pub metadata: BTreeMap<String, String>,
68 #[serde(skip_serializing_if = "ReasoningExtra::is_empty")]
70 pub reasoning: ReasoningExtra,
71 #[serde(skip_serializing_if = "Option::is_none")]
73 pub top_k: Option<u32>,
74 #[serde(skip_serializing_if = "Option::is_none")]
76 pub min_p: Option<f64>,
77 #[serde(skip_serializing_if = "Option::is_none")]
79 pub top_a: Option<f64>,
80 #[serde(skip_serializing_if = "Option::is_none")]
82 pub repetition_penalty: Option<f64>,
83 #[serde(skip_serializing_if = "Option::is_none")]
85 pub user: Option<String>,
86}
87
88#[non_exhaustive]
91#[derive(Clone, Debug, Default, PartialEq, Serialize)]
92pub struct ReasoningExtra {
93 #[serde(skip_serializing_if = "Option::is_none")]
95 pub exclude: Option<bool>,
96 #[serde(skip_serializing_if = "Option::is_none")]
98 pub summary: Option<ReasoningSummary>,
99}
100
101impl ReasoningExtra {
102 fn is_empty(&self) -> bool {
103 self.exclude.is_none() && self.summary.is_none()
104 }
105}
106
107#[non_exhaustive]
109#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
110#[serde(rename_all = "lowercase")]
111pub enum ReasoningSummary {
112 Auto,
114 Concise,
116 Detailed,
118}
119
120impl OpenRouterOptions {
121 pub fn new() -> Self {
123 Self::default()
124 }
125
126 pub fn provider(mut self, preferences: ProviderPreferences) -> Self {
128 self.shared.provider = Some(preferences);
129 self
130 }
131
132 pub fn models(mut self, models: ModelFallbacks) -> Self {
134 self.shared.models = Some(models);
135 self
136 }
137
138 pub fn plugin(mut self, plugin: Value) -> Self {
140 self.shared.plugins.push(plugin);
141 self
142 }
143
144 pub fn session_id(mut self, id: impl Into<String>) -> Self {
146 self.shared.session_id = Some(id.into());
147 self
148 }
149
150 pub fn metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
152 self.shared.metadata.insert(key.into(), value.into());
153 self
154 }
155
156 pub fn reasoning_exclude(mut self, exclude: bool) -> Self {
158 self.shared.reasoning.exclude = Some(exclude);
159 self
160 }
161
162 pub fn reasoning_summary(mut self, summary: ReasoningSummary) -> Self {
164 self.shared.reasoning.summary = Some(summary);
165 self
166 }
167
168 pub fn top_k(mut self, k: u32) -> Self {
170 self.shared.top_k = Some(k);
171 self
172 }
173
174 pub fn min_p(mut self, p: f64) -> Self {
176 self.shared.min_p = Some(p);
177 self
178 }
179
180 pub fn top_a(mut self, a: f64) -> Self {
182 self.shared.top_a = Some(a);
183 self
184 }
185
186 pub fn repetition_penalty(mut self, penalty: f64) -> Self {
188 self.shared.repetition_penalty = Some(penalty);
189 self
190 }
191
192 pub fn user(mut self, user: impl Into<String>) -> Self {
194 self.shared.user = Some(user.into());
195 self
196 }
197}
198
199impl ExtensionOptions for OpenRouterOptions {
200 type Ext = OpenRouterExt;
201}
202
203#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
205#[serde(transparent)]
206pub struct ModelFallbacks(Vec<String>);
207
208#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
210#[error("model fallbacks need at least one model")]
211pub struct EmptyFallbacks;
212
213impl ModelFallbacks {
214 pub fn new(
220 models: impl IntoIterator<Item = impl Into<String>>,
221 ) -> Result<Self, EmptyFallbacks> {
222 let models: Vec<String> = models.into_iter().map(Into::into).collect();
223 if models.is_empty() {
224 return Err(EmptyFallbacks);
225 }
226 Ok(Self(models))
227 }
228
229 pub fn models(&self) -> &[String] {
231 &self.0
232 }
233}
234
235#[non_exhaustive]
237#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
238#[serde(rename_all = "lowercase")]
239pub enum DataCollection {
240 Allow,
242 Deny,
244}
245
246#[non_exhaustive]
248#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
249#[serde(rename_all = "lowercase")]
250pub enum Quantization {
251 Int4,
253 Int8,
255 Fp16,
257 Bf16,
259 Fp32,
261 Fp8,
263 Unknown,
265}
266
267#[non_exhaustive]
269#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
270#[serde(rename_all = "lowercase")]
271pub enum ProviderSortStrategy {
272 Price,
274 Throughput,
276 Latency,
278}
279
280#[non_exhaustive]
282#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
283#[serde(rename_all = "lowercase")]
284pub enum SortPartition {
285 Model,
287 None,
289}
290
291#[non_exhaustive]
293#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
294pub struct ProviderSortConfig {
295 pub by: ProviderSortStrategy,
297 #[serde(skip_serializing_if = "Option::is_none")]
299 pub partition: Option<SortPartition>,
300}
301
302impl ProviderSortConfig {
303 pub fn new(by: ProviderSortStrategy) -> Self {
305 Self {
306 by,
307 partition: None,
308 }
309 }
310
311 pub fn partition(mut self, partition: SortPartition) -> Self {
313 self.partition = Some(partition);
314 self
315 }
316}
317
318#[non_exhaustive]
320#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
321#[serde(untagged)]
322pub enum ProviderSort {
323 Simple(ProviderSortStrategy),
325 Complex(ProviderSortConfig),
327}
328
329impl From<ProviderSortStrategy> for ProviderSort {
330 fn from(strategy: ProviderSortStrategy) -> Self {
331 Self::Simple(strategy)
332 }
333}
334
335impl From<ProviderSortConfig> for ProviderSort {
336 fn from(config: ProviderSortConfig) -> Self {
337 Self::Complex(config)
338 }
339}
340
341#[non_exhaustive]
344#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
345#[serde(untagged)]
346pub enum ThroughputThreshold {
347 Simple(#[serde(deserialize_with = "crate::json_utils::floats")] f64),
349 Percentile(PercentileThresholds),
351}
352
353#[non_exhaustive]
356#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
357#[serde(untagged)]
358pub enum LatencyThreshold {
359 Simple(#[serde(deserialize_with = "crate::json_utils::floats")] f64),
361 Percentile(PercentileThresholds),
363}
364
365#[non_exhaustive]
367#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
368#[serde(from = "PercentilesRead")]
369pub struct PercentileThresholds {
370 #[serde(skip_serializing_if = "Option::is_none")]
372 pub p50: Option<f64>,
373 #[serde(skip_serializing_if = "Option::is_none")]
375 pub p75: Option<f64>,
376 #[serde(skip_serializing_if = "Option::is_none")]
378 pub p90: Option<f64>,
379 #[serde(skip_serializing_if = "Option::is_none")]
381 pub p99: Option<f64>,
382}
383
384#[derive(Deserialize)]
388struct PercentilesRead {
389 p50: Option<Percentile>,
390 p75: Option<Percentile>,
391 p90: Option<Percentile>,
392 p99: Option<Percentile>,
393}
394
395#[derive(Deserialize)]
397struct Percentile(#[serde(deserialize_with = "crate::json_utils::floats")] f64);
398
399impl From<PercentilesRead> for PercentileThresholds {
400 fn from(read: PercentilesRead) -> Self {
401 let value = |percentile: Option<Percentile>| percentile.map(|Percentile(value)| value);
402 Self {
403 p50: value(read.p50),
404 p75: value(read.p75),
405 p90: value(read.p90),
406 p99: value(read.p99),
407 }
408 }
409}
410
411impl PercentileThresholds {
412 pub fn new() -> Self {
414 Self::default()
415 }
416
417 pub fn p50(mut self, value: f64) -> Self {
419 self.p50 = Some(value);
420 self
421 }
422
423 pub fn p75(mut self, value: f64) -> Self {
425 self.p75 = Some(value);
426 self
427 }
428
429 pub fn p90(mut self, value: f64) -> Self {
431 self.p90 = Some(value);
432 self
433 }
434
435 pub fn p99(mut self, value: f64) -> Self {
437 self.p99 = Some(value);
438 self
439 }
440}
441
442#[non_exhaustive]
445#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
446#[serde(deny_unknown_fields)]
447pub struct MaxPrice {
448 #[serde(skip_serializing_if = "Option::is_none")]
450 pub prompt: Option<f64>,
451 #[serde(skip_serializing_if = "Option::is_none")]
453 pub completion: Option<f64>,
454 #[serde(skip_serializing_if = "Option::is_none")]
456 pub request: Option<f64>,
457 #[serde(skip_serializing_if = "Option::is_none")]
459 pub image: Option<f64>,
460}
461
462impl MaxPrice {
463 pub fn new() -> Self {
465 Self::default()
466 }
467
468 pub fn prompt(mut self, price: f64) -> Self {
470 self.prompt = Some(price);
471 self
472 }
473
474 pub fn completion(mut self, price: f64) -> Self {
476 self.completion = Some(price);
477 self
478 }
479
480 pub fn request(mut self, price: f64) -> Self {
482 self.request = Some(price);
483 self
484 }
485
486 pub fn image(mut self, price: f64) -> Self {
488 self.image = Some(price);
489 self
490 }
491}
492
493#[non_exhaustive]
497#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
498#[serde(deny_unknown_fields)]
499pub struct ProviderPreferences {
500 #[serde(skip_serializing_if = "Option::is_none")]
502 pub order: Option<Vec<String>>,
503 #[serde(skip_serializing_if = "Option::is_none")]
505 pub only: Option<Vec<String>>,
506 #[serde(skip_serializing_if = "Option::is_none")]
508 pub ignore: Option<Vec<String>>,
509 #[serde(skip_serializing_if = "Option::is_none")]
511 pub allow_fallbacks: Option<bool>,
512 #[serde(skip_serializing_if = "Option::is_none")]
514 pub require_parameters: Option<bool>,
515 #[serde(skip_serializing_if = "Option::is_none")]
517 pub data_collection: Option<DataCollection>,
518 #[serde(skip_serializing_if = "Option::is_none")]
520 pub zdr: Option<bool>,
521 #[serde(skip_serializing_if = "Option::is_none")]
523 pub sort: Option<ProviderSort>,
524 #[serde(skip_serializing_if = "Option::is_none")]
526 pub preferred_min_throughput: Option<ThroughputThreshold>,
527 #[serde(skip_serializing_if = "Option::is_none")]
529 pub preferred_max_latency: Option<LatencyThreshold>,
530 #[serde(skip_serializing_if = "Option::is_none")]
532 pub max_price: Option<MaxPrice>,
533 #[serde(skip_serializing_if = "Option::is_none")]
535 pub quantizations: Option<Vec<Quantization>>,
536}
537
538fn strings(items: impl IntoIterator<Item = impl Into<String>>) -> Option<Vec<String>> {
539 Some(items.into_iter().map(Into::into).collect())
540}
541
542impl ProviderPreferences {
543 pub fn new() -> Self {
545 Self::default()
546 }
547
548 pub fn order(mut self, providers: impl IntoIterator<Item = impl Into<String>>) -> Self {
550 self.order = strings(providers);
551 self
552 }
553
554 pub fn only(mut self, providers: impl IntoIterator<Item = impl Into<String>>) -> Self {
556 self.only = strings(providers);
557 self
558 }
559
560 pub fn ignore(mut self, providers: impl IntoIterator<Item = impl Into<String>>) -> Self {
562 self.ignore = strings(providers);
563 self
564 }
565
566 pub fn allow_fallbacks(mut self, allow: bool) -> Self {
568 self.allow_fallbacks = Some(allow);
569 self
570 }
571
572 pub fn require_parameters(mut self, require: bool) -> Self {
574 self.require_parameters = Some(require);
575 self
576 }
577
578 pub fn data_collection(mut self, policy: DataCollection) -> Self {
580 self.data_collection = Some(policy);
581 self
582 }
583
584 pub fn zdr(mut self, enable: bool) -> Self {
586 self.zdr = Some(enable);
587 self
588 }
589
590 pub fn sort(mut self, sort: impl Into<ProviderSort>) -> Self {
592 self.sort = Some(sort.into());
593 self
594 }
595
596 pub fn preferred_min_throughput(mut self, threshold: ThroughputThreshold) -> Self {
598 self.preferred_min_throughput = Some(threshold);
599 self
600 }
601
602 pub fn preferred_max_latency(mut self, threshold: LatencyThreshold) -> Self {
604 self.preferred_max_latency = Some(threshold);
605 self
606 }
607
608 pub fn max_price(mut self, price: MaxPrice) -> Self {
610 self.max_price = Some(price);
611 self
612 }
613
614 pub fn quantizations(mut self, quantizations: impl IntoIterator<Item = Quantization>) -> Self {
616 self.quantizations = Some(quantizations.into_iter().collect());
617 self
618 }
619
620 pub fn cheapest(self) -> Self {
622 self.sort(ProviderSortStrategy::Price)
623 }
624}
625
626#[non_exhaustive]
628#[derive(Clone, Debug, Default, PartialEq)]
629pub struct OpenRouterExtras {
630 pub provider: Option<String>,
633 pub native_finish_reason: Option<String>,
635 pub service_tier: Option<String>,
637 pub system_fingerprint: Option<String>,
639 pub cost: Option<f64>,
641 pub cost_details: Option<Map<String, Value>>,
644 pub is_byok: Option<bool>,
647 pub prompt_tokens_details: Option<Value>,
650 pub server_tool_use_details: Option<Value>,
652 pub openrouter_metadata: Option<Value>,
654 pub annotations: Option<Vec<Value>>,
656}
657
658impl ReplyExtras for OpenRouterExtras {
659 fn from_reply(api: &Api, raw: &Value) -> Result<Self, serde_json::Error> {
660 if api.as_str() == "openai.responses" {
661 return Ok(Self {
662 provider: reply_field(raw, "/provider")?,
663 service_tier: reply_field(raw, "/service_tier")?,
664 cost: reply_field(raw, "/usage/cost")?,
665 cost_details: reply_field(raw, "/usage/cost_details")?,
666 is_byok: reply_field(raw, "/usage/is_byok")?,
667 prompt_tokens_details: reply_field(raw, "/usage/input_tokens_details")?,
668 ..Self::default()
669 });
670 }
671 Ok(Self {
672 provider: reply_field(raw, "/provider")?,
673 native_finish_reason: reply_field(raw, "/choices/0/native_finish_reason")?,
674 service_tier: reply_field(raw, "/service_tier")?,
675 system_fingerprint: reply_field(raw, "/system_fingerprint")?,
676 cost: reply_field(raw, "/usage/cost")?,
677 cost_details: reply_field(raw, "/usage/cost_details")?,
678 is_byok: reply_field(raw, "/usage/is_byok")?,
679 prompt_tokens_details: reply_field(raw, "/usage/prompt_tokens_details")?,
680 server_tool_use_details: reply_field(raw, "/usage/server_tool_use_details")?,
681 openrouter_metadata: reply_field(raw, "/openrouter_metadata")?,
682 annotations: reply_field(raw, "/choices/0/message/annotations")?,
683 })
684 }
685}
686
687#[cfg(test)]
688mod tests;