11use crate :: server:: router:: PolluxState ;
22use axum:: {
33 Router ,
4+ extract:: { DefaultBodyLimit , Request } ,
5+ http:: header:: CONTENT_LENGTH ,
6+ middleware:: { self , Next } ,
7+ response:: Response ,
48 routing:: { get, post} ,
59} ;
10+ use tracing:: debug;
611
712pub mod extract;
813pub mod handlers;
@@ -14,6 +19,8 @@ use crate::providers::codex::SUPPORTED_MODEL_NAMES;
1419use pollux_schema:: openai:: OpenaiModelList ;
1520use std:: sync:: LazyLock ;
1621
22+ const CODEX_RESPONSES_BODY_LIMIT_BYTES : usize = 100 * 1024 * 1024 ;
23+
1724pub static CODEX_MODEL_LIST : LazyLock < OpenaiModelList > = LazyLock :: new ( || {
1825 OpenaiModelList :: from_model_names ( SUPPORTED_MODEL_NAMES . iter ( ) . cloned ( ) , "codex" . to_string ( ) )
1926} ) ;
@@ -25,11 +32,36 @@ pub struct CodexContext {
2532 pub model_mask : u64 ,
2633}
2734
35+ async fn debug_codex_responses_body_size ( req : Request , next : Next ) -> Response {
36+ let content_length = req
37+ . headers ( )
38+ . get ( CONTENT_LENGTH )
39+ . and_then ( |value| value. to_str ( ) . ok ( ) )
40+ . and_then ( |value| value. parse :: < u64 > ( ) . ok ( ) ) ;
41+
42+ match content_length {
43+ Some ( bytes) => {
44+ debug ! (
45+ content_length_bytes = bytes,
46+ content_length_mib = format_args!( "{:.2}" , bytes as f64 / ( 1024.0 * 1024.0 ) ) ,
47+ "Incoming Codex /responses request body size"
48+ ) ;
49+ }
50+ None => {
51+ debug ! ( "Incoming Codex /responses request body size unknown (no Content-Length)" ) ;
52+ }
53+ }
54+
55+ next. run ( req) . await
56+ }
57+
2858pub fn router ( ) -> Router < PolluxState > {
2959 Router :: new ( )
3060 . route (
3161 "/codex/v1/responses" ,
32- post ( handlers:: codex_response_handler) ,
62+ post ( handlers:: codex_response_handler)
63+ . layer ( DefaultBodyLimit :: max ( CODEX_RESPONSES_BODY_LIMIT_BYTES ) )
64+ . layer ( middleware:: from_fn ( debug_codex_responses_body_size) ) ,
3365 )
3466 . route ( "/codex/v1/models" , get ( handlers:: codex_models_handler) )
3567 . route ( "/codex/resource:add" , post ( resource:: codex_resource_add) )
0 commit comments