@@ -238,6 +238,10 @@ function normalizeResponsesInputToChatMessages(input) {
238238 out . push ( { type : 'text' , text : block . text } ) ;
239239 continue ;
240240 }
241+ if ( ( type === 'reasoning' || type === 'reasoning_text' || type === 'reasoning_content' ) && typeof block . text === 'string' ) {
242+ out . push ( { type : 'text' , text : block . text } ) ;
243+ continue ;
244+ }
241245 if ( type === 'input_image' ) {
242246 const raw = block . image_url != null ? block . image_url : block . imageUrl ;
243247 const url = typeof raw === 'string'
@@ -255,7 +259,21 @@ function normalizeResponsesInputToChatMessages(input) {
255259 }
256260 if ( type === 'image_url' && block . image_url ) {
257261 out . push ( { type : 'image_url' , image_url : block . image_url } ) ;
262+ continue ;
263+ }
264+ const text = typeof block . text === 'string'
265+ ? block . text
266+ : ( typeof block . content === 'string' ? block . content : '' ) ;
267+ if ( text ) {
268+ out . push ( { type : 'text' , text } ) ;
269+ continue ;
258270 }
271+ try {
272+ const raw = JSON . stringify ( block ) ;
273+ if ( raw ) {
274+ out . push ( { type : 'text' , text : raw . slice ( 0 , 4000 ) } ) ;
275+ }
276+ } catch ( _ ) { }
259277 }
260278 if ( out . length === 0 ) return '' ;
261279 return out ;
@@ -635,6 +653,9 @@ async function proxyRequestJson(targetUrl, options = {}) {
635653 const parsed = new URL ( targetUrl ) ;
636654 const transport = parsed . protocol === 'https:' ? https : http ;
637655 const bodyText = options . body ? JSON . stringify ( options . body ) : '' ;
656+ const maxBytes = Number . isFinite ( options . maxBytes ) && options . maxBytes > 0
657+ ? Math . floor ( options . maxBytes )
658+ : 0 ;
638659 const headers = {
639660 'Accept' : 'application/json' ,
640661 ...( options . body ? { 'Content-Type' : 'application/json' } : { } ) ,
@@ -664,7 +685,21 @@ async function proxyRequestJson(targetUrl, options = {}) {
664685 agent : parsed . protocol === 'https:' ? options . httpsAgent : options . httpAgent
665686 } , ( upstreamRes ) => {
666687 const chunks = [ ] ;
667- upstreamRes . on ( 'data' , ( chunk ) => chunk && chunks . push ( chunk ) ) ;
688+ let size = 0 ;
689+ upstreamRes . on ( 'data' , ( chunk ) => {
690+ if ( ! chunk ) return ;
691+ if ( maxBytes > 0 ) {
692+ size += chunk . length ;
693+ if ( size > maxBytes ) {
694+ chunks . length = 0 ;
695+ try { upstreamRes . destroy ( new Error ( 'response too large' ) ) ; } catch ( _ ) { }
696+ try { req . destroy ( new Error ( 'response too large' ) ) ; } catch ( _ ) { }
697+ finish ( { ok : false , error : 'response too large' } ) ;
698+ return ;
699+ }
700+ }
701+ chunks . push ( chunk ) ;
702+ } ) ;
668703 upstreamRes . on ( 'end' , ( ) => {
669704 const text = chunks . length ? Buffer . concat ( chunks ) . toString ( 'utf-8' ) : '' ;
670705 finish ( {
@@ -689,12 +724,16 @@ async function proxyRequestJson(targetUrl, options = {}) {
689724
690725function createOpenaiBridgeHttpHandler ( options = { } ) {
691726 const settingsFile = options . settingsFile ;
692- const expectedToken = typeof options . expectedToken === 'string' && options . expectedToken . trim ( )
693- ? options . expectedToken . trim ( )
694- : DEFAULT_BRIDGE_TOKEN ;
727+ const expectedTokenRaw = typeof options . expectedToken === 'string' ? options . expectedToken . trim ( ) : '' ;
728+ const expectedToken = Object . prototype . hasOwnProperty . call ( options , 'expectedToken' )
729+ ? expectedTokenRaw
730+ : ( expectedTokenRaw || DEFAULT_BRIDGE_TOKEN ) ;
695731 const maxBodySize = Number . isFinite ( options . maxBodySize ) ? options . maxBodySize : 0 ;
696732 const httpAgent = options . httpAgent ;
697733 const httpsAgent = options . httpsAgent ;
734+ const maxUpstreamBytes = Number . isFinite ( options . maxUpstreamBytes ) && options . maxUpstreamBytes > 0
735+ ? Math . floor ( options . maxUpstreamBytes )
736+ : Math . max ( 16 * 1024 * 1024 , maxBodySize > 0 ? maxBodySize * 4 : 0 ) ;
698737
699738 if ( ! settingsFile ) {
700739 throw new Error ( 'createOpenaiBridgeHttpHandler 缺少 settingsFile' ) ;
@@ -730,6 +769,11 @@ function createOpenaiBridgeHttpHandler(options = {}) {
730769 // 为避免在 LAN 暴露无鉴权的代理,这里仅允许 loopback 连接缺省 token。
731770 const remoteAddr = req && req . socket ? req . socket . remoteAddress : '' ;
732771 const isLoopback = isLoopbackAddress ( remoteAddr ) ;
772+ if ( ! isLoopback && ! expectedToken ) {
773+ res . writeHead ( 403 , { 'Content-Type' : 'application/json; charset=utf-8' } ) ;
774+ res . end ( JSON . stringify ( { error : 'Remote access is disabled (set CODEXMATE_HTTP_TOKEN)' } ) ) ;
775+ return ;
776+ }
733777 if ( ! token && ! isLoopback ) {
734778 res . writeHead ( 401 , { 'Content-Type' : 'application/json; charset=utf-8' } ) ;
735779 res . end ( JSON . stringify ( { error : 'Unauthorized' } ) ) ;
@@ -774,6 +818,7 @@ function createOpenaiBridgeHttpHandler(options = {}) {
774818 ...( authHeader ? { Authorization : authHeader } : { } ) ,
775819 ...upstreamHeaders
776820 } ,
821+ maxBytes : maxUpstreamBytes ,
777822 httpAgent,
778823 httpsAgent
779824 } ) ;
@@ -827,6 +872,7 @@ function createOpenaiBridgeHttpHandler(options = {}) {
827872 ...( authHeader ? { Authorization : authHeader } : { } ) ,
828873 ...upstreamHeaders
829874 } ,
875+ maxBytes : maxUpstreamBytes ,
830876 httpAgent,
831877 httpsAgent
832878 } ) ;
@@ -887,6 +933,7 @@ function createOpenaiBridgeHttpHandler(options = {}) {
887933 ...( authHeader ? { Authorization : authHeader } : { } ) ,
888934 ...upstreamHeaders
889935 } ,
936+ maxBytes : maxUpstreamBytes ,
890937 httpAgent,
891938 httpsAgent
892939 } ) ;
0 commit comments