Skip to main content

litellm_rs/core/completion/default_router/
mod.rs

1//! Compatibility facade for the canonical router runtime.
2
3use super::{CompletionOptions, CompletionResponse, CompletionStream, Message, Router};
4
5use crate::core::router::{RuntimeBinding, default_runtime};
6use crate::utils::error::gateway_error::{GatewayError, Result};
7use async_trait::async_trait;
8
9mod router_impl;
10
11/// Completion compatibility facade backed only by [`crate::core::router::UnifiedRouter`].
12///
13/// New integrations should retain a [`RuntimeBinding`] and use [`Self::from_runtime`].
14/// The type remains public for the 0.6 compatibility window.
15pub struct DefaultRouter {
16    runtime_binding: Option<RuntimeBinding>,
17}
18
19impl DefaultRouter {
20    /// Create a facade that binds the process-default runtime for each operation.
21    pub async fn new() -> Result<Self> {
22        Ok(Self {
23            runtime_binding: None,
24        })
25    }
26
27    /// Create a completion facade backed by an explicit canonical runtime.
28    pub fn from_runtime(runtime: RuntimeBinding) -> Self {
29        Self {
30            runtime_binding: Some(runtime),
31        }
32    }
33}
34
35/// Fallback router retained for source compatibility.
36pub struct ErrorRouter {
37    error: String,
38}
39
40impl ErrorRouter {
41    /// Create a facade that always returns the supplied initialization error.
42    pub fn new(error: impl Into<String>) -> Self {
43        Self {
44            error: error.into(),
45        }
46    }
47}
48
49#[async_trait]
50impl Router for ErrorRouter {
51    async fn complete(
52        &self,
53        _model: &str,
54        _messages: Vec<Message>,
55        _options: CompletionOptions,
56    ) -> Result<CompletionResponse> {
57        Err(GatewayError::internal(format!(
58            "Router initialization failed: {}",
59            self.error
60        )))
61    }
62
63    async fn complete_stream(
64        &self,
65        _model: &str,
66        _messages: Vec<Message>,
67        _options: CompletionOptions,
68    ) -> Result<CompletionStream> {
69        Err(GatewayError::internal(format!(
70            "Router initialization failed: {}",
71            self.error
72        )))
73    }
74}
75
76/// Core completion function backed by the process-default canonical runtime.
77pub async fn completion(
78    model: &str,
79    messages: Vec<Message>,
80    options: Option<CompletionOptions>,
81) -> Result<CompletionResponse> {
82    let handle = default_runtime().map_err(GatewayError::from)?;
83    router_impl::complete_with_runtime_handle(&handle, model, messages, options.unwrap_or_default())
84        .await
85}
86
87/// Async compatibility alias for [`completion`].
88pub async fn acompletion(
89    model: &str,
90    messages: Vec<Message>,
91    options: Option<CompletionOptions>,
92) -> Result<CompletionResponse> {
93    completion(model, messages, options).await
94}
95
96/// Streaming completion backed by the process-default canonical runtime.
97pub async fn completion_stream(
98    model: &str,
99    messages: Vec<Message>,
100    options: Option<CompletionOptions>,
101) -> Result<CompletionStream> {
102    let handle = default_runtime().map_err(GatewayError::from)?;
103    router_impl::complete_stream_with_runtime_handle(
104        &handle,
105        model,
106        messages,
107        options.unwrap_or_default(),
108    )
109    .await
110}