"tests/testdata/blimp_inchoative-v0-res.json" did not exist on "5f48dfc2ab16917ff5b18862c522bbf42a46f053"
mod.rs 4.72 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
23

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    /// 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
157
    fn liveness(&self) -> Response {
158
        // Simple liveness check - if we can respond, we're alive
159
        (StatusCode::OK, "OK").into_response()
160
161
162
    }

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