mod.rs 4.79 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
14
    ChatCompletionRequest, CompletionRequest, EmbeddingRequest, GenerateRequest, RerankRequest,
    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
pub mod worker_initializer;
23
24

pub use factory::RouterFactory;
25
pub use worker_initializer::WorkerInitializer;
26
27
// Re-export HTTP routers for convenience (keeps routers::openai_router path working)
pub use http::{openai_router, pd_router, pd_types, router};
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

/// Worker management trait for administrative operations
///
/// This trait is separate from RouterTrait to allow Send futures
/// for use in service discovery and other background tasks
#[async_trait]
pub trait WorkerManagement: Send + Sync {
    /// Add a worker to the router
    async fn add_worker(&self, worker_url: &str) -> Result<String, String>;

    /// Remove a worker from the router
    fn remove_worker(&self, worker_url: &str);

    /// Get all worker URLs
    fn get_worker_urls(&self) -> Vec<String>;
}

/// 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.
49
#[async_trait]
50
51
52
pub trait RouterTrait: Send + Sync + Debug + WorkerManagement {
    /// Get a reference to self as Any for downcasting
    fn as_any(&self) -> &dyn std::any::Any;
53

54
    /// Route a health check request
55
    async fn health(&self, req: Request<Body>) -> Response;
56
57

    /// Route a health generate request
58
    async fn health_generate(&self, req: Request<Body>) -> Response;
59
60

    /// Get server information
61
    async fn get_server_info(&self, req: Request<Body>) -> Response;
62
63

    /// Get available models
64
    async fn get_models(&self, req: Request<Body>) -> Response;
65
66

    /// Get model information
67
    async fn get_model_info(&self, req: Request<Body>) -> Response;
68
69

    /// Route a generate request
70
71
72
73
74
75
    async fn route_generate(
        &self,
        headers: Option<&HeaderMap>,
        body: &GenerateRequest,
        model_id: Option<&str>,
    ) -> Response;
76
77
78
79

    /// Route a chat completion request
    async fn route_chat(
        &self,
80
81
        headers: Option<&HeaderMap>,
        body: &ChatCompletionRequest,
82
        model_id: Option<&str>,
83
    ) -> Response;
84
85
86
87

    /// Route a completion request
    async fn route_completion(
        &self,
88
89
        headers: Option<&HeaderMap>,
        body: &CompletionRequest,
90
        model_id: Option<&str>,
91
    ) -> Response;
92

93
94
95
96
97
    /// Route a responses request
    async fn route_responses(
        &self,
        headers: Option<&HeaderMap>,
        body: &ResponsesRequest,
98
        model_id: Option<&str>,
99
100
    ) -> Response;

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
    /// Retrieve a stored/background response by id
    async fn get_response(&self, headers: Option<&HeaderMap>, response_id: &str) -> Response;

    /// 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()
    }

129
130
131
132
133
134
135
    /// Route embedding requests (OpenAI-compatible /v1/embeddings)
    async fn route_embeddings(
        &self,
        headers: Option<&HeaderMap>,
        body: &EmbeddingRequest,
        model_id: Option<&str>,
    ) -> Response;
136

137
138
139
140
141
142
    async fn route_rerank(
        &self,
        headers: Option<&HeaderMap>,
        body: &RerankRequest,
        model_id: Option<&str>,
    ) -> Response;
143

144
    /// Flush cache on all workers
145
    async fn flush_cache(&self) -> Response;
146
147

    /// Get worker loads (for monitoring)
148
    async fn get_worker_loads(&self) -> Response;
149
150
151
152
153
154
155
156
157
158

    /// 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"
    }

    /// Server liveness check - is the server process running
159
    fn liveness(&self) -> Response {
160
        // Simple liveness check - if we can respond, we're alive
161
        (StatusCode::OK, "OK").into_response()
162
163
164
    }

    /// Server readiness check - is the server ready to handle requests
165
    fn readiness(&self) -> Response;
166
}