mod.rs 6.48 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
use serde_json::Value;
17

18
pub mod factory;
19
pub mod grpc;
20
pub mod header_utils;
21
pub mod http;
22
pub mod openai; // New refactored OpenAI router module
23
pub mod router_manager;
24
25

pub use factory::RouterFactory;
26

27
28
// Re-export HTTP routers for convenience
pub use http::{pd_router, pd_types, router};
29
30
31
32
33

/// 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.
34
#[async_trait]
35
pub trait RouterTrait: Send + Sync + Debug {
36
37
    /// Get a reference to self as Any for downcasting
    fn as_any(&self) -> &dyn std::any::Any;
38

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

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

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

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

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

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

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

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

83
    /// Retrieve a stored/background response by id
84
85
86
87
88
89
    async fn get_response(
        &self,
        headers: Option<&HeaderMap>,
        response_id: &str,
        params: &ResponsesGetParams,
    ) -> Response;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

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

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

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

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    // Conversations API
    async fn create_conversation(&self, _headers: Option<&HeaderMap>, _body: &Value) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversations create endpoint not implemented",
        )
            .into_response()
    }

    async fn get_conversation(
        &self,
        _headers: Option<&HeaderMap>,
        _conversation_id: &str,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversations get endpoint not implemented",
        )
            .into_response()
    }

    async fn update_conversation(
        &self,
        _headers: Option<&HeaderMap>,
        _conversation_id: &str,
        _body: &Value,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversations update endpoint not implemented",
        )
            .into_response()
    }

    async fn delete_conversation(
        &self,
        _headers: Option<&HeaderMap>,
        _conversation_id: &str,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversations delete endpoint not implemented",
        )
            .into_response()
    }

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    /// List items for a conversation
    async fn list_conversation_items(
        &self,
        _headers: Option<&HeaderMap>,
        _conversation_id: &str,
        _limit: Option<usize>,
        _order: Option<String>,
        _after: Option<String>,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversation items list endpoint not implemented",
        )
            .into_response()
    }

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    /// Create items in a conversation
    async fn create_conversation_items(
        &self,
        _headers: Option<&HeaderMap>,
        _conversation_id: &str,
        _body: &Value,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversation items create endpoint not implemented",
        )
            .into_response()
    }

    /// Get a single conversation item
    /// The `include` parameter is accepted but not yet implemented
    async fn get_conversation_item(
        &self,
        _headers: Option<&HeaderMap>,
        _conversation_id: &str,
        _item_id: &str,
        _include: Option<Vec<String>>,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversation item get endpoint not implemented",
        )
            .into_response()
    }

    /// Delete a conversation item
    async fn delete_conversation_item(
        &self,
        _headers: Option<&HeaderMap>,
        _conversation_id: &str,
        _item_id: &str,
    ) -> Response {
        (
            StatusCode::NOT_IMPLEMENTED,
            "Conversation item delete endpoint not implemented",
        )
            .into_response()
    }

237
238
239
240
241
242
243
244
    /// 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"
    }
}