@@ -5776,43 +5776,52 @@ export function createCodexBridgeMiddleware(): CodexBridgeMiddleware {
57765776 }
57775777
57785778 if ( req . method === 'GET' && url . pathname === '/codex-api/thread-turn-page' ) {
5779- const threadId = url . searchParams . get ( 'threadId' ) ?. trim ( ) ?? ''
5780- const beforeTurnId = url . searchParams . get ( 'beforeTurnId' ) ?. trim ( ) ?? ''
5781- const limitRaw = url . searchParams . get ( 'limit' ) ?. trim ( ) ?? String ( THREAD_RESPONSE_TURN_LIMIT )
5782- const limit = Math . max ( 1 , Math . min ( 50 , Number . parseInt ( limitRaw , 10 ) || THREAD_RESPONSE_TURN_LIMIT ) )
5783- if ( ! threadId ) {
5784- setJson ( res , 400 , { error : 'Missing threadId' } )
5785- return
5786- }
5779+ try {
5780+ const threadId = url . searchParams . get ( 'threadId' ) ?. trim ( ) ?? ''
5781+ const beforeTurnId = url . searchParams . get ( 'beforeTurnId' ) ?. trim ( ) ?? ''
5782+ const limitRaw = url . searchParams . get ( 'limit' ) ?. trim ( ) ?? String ( THREAD_RESPONSE_TURN_LIMIT )
5783+ const limit = Math . max ( 1 , Math . min ( 50 , Number . parseInt ( limitRaw , 10 ) || THREAD_RESPONSE_TURN_LIMIT ) )
5784+ if ( ! threadId ) {
5785+ setJson ( res , 400 , { error : 'Missing threadId' } )
5786+ return
5787+ }
57875788
5788- const threadReadResult = await appServer . rpc ( 'thread/read' , {
5789- threadId,
5790- includeTurns : true ,
5791- } )
5792- const record = asRecord ( threadReadResult )
5793- const thread = asRecord ( record ?. thread )
5794- const turns = Array . isArray ( thread ?. turns ) ? thread . turns : [ ]
5795- const beforeIndex = beforeTurnId
5796- ? turns . findIndex ( ( turn ) => asRecord ( turn ) ?. id === beforeTurnId )
5797- : turns . length
5798- const endIndex = beforeIndex >= 0 ? beforeIndex : turns . length
5799- const startIndex = Math . max ( 0 , endIndex - limit )
5800- const pageTurns = turns . slice ( startIndex , endIndex )
5801- const pagedResult = {
5802- ...record ,
5803- thread : {
5804- ...thread ,
5805- turns : pageTurns ,
5806- } ,
5807- }
5808- const sanitized = await sanitizeThreadTurnsInlinePayloads ( 'thread/read' , pagedResult )
5809- const result = await mergeSessionSkillInputsIntoThreadResult ( sanitized )
5789+ const threadReadResult = await appServer . rpc ( 'thread/read' , {
5790+ threadId,
5791+ includeTurns : true ,
5792+ } )
5793+ const record = asRecord ( threadReadResult )
5794+ const thread = asRecord ( record ?. thread )
5795+ if ( ! record || ! thread ) {
5796+ setJson ( res , 502 , { error : 'thread/read returned an invalid thread response' } )
5797+ return
5798+ }
58105799
5811- setJson ( res , 200 , {
5812- result,
5813- startTurnIndex : startIndex ,
5814- hasMoreOlder : startIndex > 0 ,
5815- } )
5800+ const turns = Array . isArray ( thread . turns ) ? thread . turns : [ ]
5801+ const beforeIndex = beforeTurnId
5802+ ? turns . findIndex ( ( turn ) => asRecord ( turn ) ?. id === beforeTurnId )
5803+ : turns . length
5804+ const endIndex = beforeIndex >= 0 ? beforeIndex : turns . length
5805+ const startIndex = Math . max ( 0 , endIndex - limit )
5806+ const pageTurns = turns . slice ( startIndex , endIndex )
5807+ const pagedResult = {
5808+ ...record ,
5809+ thread : {
5810+ ...thread ,
5811+ turns : pageTurns ,
5812+ } ,
5813+ }
5814+ const sanitized = await sanitizeThreadTurnsInlinePayloads ( 'thread/read' , pagedResult )
5815+ const result = await mergeSessionSkillInputsIntoThreadResult ( sanitized )
5816+
5817+ setJson ( res , 200 , {
5818+ result,
5819+ startTurnIndex : startIndex ,
5820+ hasMoreOlder : startIndex > 0 ,
5821+ } )
5822+ } catch ( error ) {
5823+ setJson ( res , 500 , { error : getErrorMessage ( error , 'Failed to load earlier thread messages' ) } )
5824+ }
58165825 return
58175826 }
58185827
0 commit comments