mod.rs 3.78 KB
Newer Older
1
2
3
//! Router implementations

use async_trait::async_trait;
4
5
6
7
8
9
use axum::{
    body::Body,
    extract::Request,
    http::{HeaderMap, StatusCode},
    response::{IntoResponse, Response},
};
10
11
use std::fmt::Debug;

12
use crate::protocols::spec::{
13
    ChatCompletionRequest, CompletionRequest, EmbeddingRequest, GenerateRequest, RerankRequest,
14
    ResponsesGetParams, ResponsesRequest,
15
};
16

17
pub mod factory;
18
pub mod grpc;
19
pub mod header_utils;
20
pub mod http;
21
pub mod router_manager;
22
23

pub use factory::RouterFactory;
24

25
26
// Re-export HTTP routers for convenience (keeps routers::openai_router path working)
pub use http::{openai_router, pd_router, pd_types, router};
27
28
29
30
31

/// Core trait for all router implementations
///
/// This trait provides a unified interface for routing requests,
/// regardless of whether it's a regular router or PD router.
32
#[async_trait]
33
pub trait RouterTrait: Send + Sync + Debug {
34
35
    /// Get a reference to self as Any for downcasting
    fn as_any(&self) -> &dyn std::any::Any;
36

37
    /// Route a health generate request
38
    async fn health_generate(&self, req: Request<Body>) -> Response;
39
40

    /// Get server information
41
    async fn get_server_info(&self, req: Request<Body>) -> Response;
42
43

    /// Get available models
44
    async fn get_models(&self, req: Request<Body>) -> Response;
45
46

    /// Get model information
47
    async fn get_model_info(&self, req: Request<Body>) -> Response;
48
49

    /// Route a generate request
50
51
52
53
54
55
    async fn route_generate(
        &self,
        headers: Option<&HeaderMap>,
        body: &GenerateRequest,
        model_id: Option<&str>,
    ) -> Response;
56
57
58
59

    /// Route a chat completion request
    async fn route_chat(
        &self,
60
61
        headers: Option<&HeaderMap>,
        body: &ChatCompletionRequest,
62
        model_id: Option<&str>,
63
    ) -> Response;
64
65
66
67

    /// Route a completion request
    async fn route_completion(
        &self,
68
69
        headers: Option<&HeaderMap>,
        body: &CompletionRequest,
70
        model_id: Option<&str>,
71
    ) -> Response;
72

73
74
75
76
77
    /// Route a responses request
    async fn route_responses(
        &self,
        headers: Option<&HeaderMap>,
        body: &ResponsesRequest,
78
        model_id: Option<&str>,
79
80
    ) -> Response;

81
    /// Retrieve a stored/background response by id
82
83
84
85
86
87
    async fn get_response(
        &self,
        headers: Option<&HeaderMap>,
        response_id: &str,
        params: &ResponsesGetParams,
    ) -> Response;
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

    /// Cancel a background response by id
    async fn cancel_response(&self, headers: Option<&HeaderMap>, response_id: &str) -> Response;

    /// Delete a response by id
    async fn delete_response(&self, _headers: Option<&HeaderMap>, _response_id: &str) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Responses delete endpoint not implemented",
        )
            .into_response()
    }

    /// List input items of a response by id
    async fn list_response_input_items(
        &self,
        _headers: Option<&HeaderMap>,
        _response_id: &str,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Responses list input items endpoint not implemented",
        )
            .into_response()
    }

114
115
116
117
118
119
120
    /// Route embedding requests (OpenAI-compatible /v1/embeddings)
    async fn route_embeddings(
        &self,
        headers: Option<&HeaderMap>,
        body: &EmbeddingRequest,
        model_id: Option<&str>,
    ) -> Response;
121

122
123
124
125
126
127
    async fn route_rerank(
        &self,
        headers: Option<&HeaderMap>,
        body: &RerankRequest,
        model_id: Option<&str>,
    ) -> Response;
128

129
    /// Get worker loads (for monitoring)
130
    async fn get_worker_loads(&self) -> Response;
131
132
133
134
135
136
137
138
139

    /// Get router type name
    fn router_type(&self) -> &'static str;

    /// Check if this is a PD router
    fn is_pd_mode(&self) -> bool {
        self.router_type() == "pd"
    }
}