@@ -109,6 +109,7 @@ const DEFAULT_QUOTA_REMAINING_THRESHOLD = 10;
109109const DEFAULT_AUTH_FAILURE_COOLDOWN_MS = 30_000 ;
110110const DEFAULT_MAX_RUNTIME_ACCOUNT_ATTEMPTS = 4 ;
111111const MAX_REQUEST_BODY_BYTES = 64 * 1024 * 1024 ;
112+ const MAX_THREAD_GOAL_FALLBACKS = 512 ;
112113const HOP_BY_HOP_HEADERS = new Set ( [
113114 "connection" ,
114115 "content-length" ,
@@ -345,6 +346,33 @@ function isThreadGoalFallbackStatus(status: number): boolean {
345346 return status === HTTP_STATUS . FORBIDDEN ;
346347}
347348
349+ function setThreadGoalFallback (
350+ fallbacks : Map < string , string | null > ,
351+ key : string ,
352+ goal : string | null ,
353+ ) : void {
354+ if ( fallbacks . has ( key ) ) {
355+ fallbacks . delete ( key ) ;
356+ }
357+ fallbacks . set ( key , goal ) ;
358+ while ( fallbacks . size > MAX_THREAD_GOAL_FALLBACKS ) {
359+ const oldestKey = fallbacks . keys ( ) . next ( ) . value ;
360+ if ( typeof oldestKey !== "string" ) break ;
361+ fallbacks . delete ( oldestKey ) ;
362+ }
363+ }
364+
365+ function getThreadGoalFallback (
366+ fallbacks : Map < string , string | null > ,
367+ key : string ,
368+ ) : string | null {
369+ if ( ! fallbacks . has ( key ) ) return null ;
370+ const goal = fallbacks . get ( key ) ?? null ;
371+ fallbacks . delete ( key ) ;
372+ fallbacks . set ( key , goal ) ;
373+ return goal ;
374+ }
375+
348376function resolveSessionKey ( headers : Headers , parsedBody : RequestBody | null ) : string | null {
349377 const headerKey =
350378 headers . get ( OPENAI_HEADERS . SESSION_ID ) ??
@@ -942,7 +970,7 @@ export async function startRuntimeRotationProxy(
942970 operation : isModelsRequest
943971 ? "models"
944972 : isThreadGoalRequest
945- ? "responses "
973+ ? "thread-goal "
946974 : "responses" ,
947975 model : context . model ,
948976 projectKey,
@@ -1169,12 +1197,12 @@ export async function startRuntimeRotationProxy(
11691197 account : refreshed . account ,
11701198 } ) ;
11711199 if ( context . upstreamPath . endsWith ( "/set" ) ) {
1172- threadGoalFallbacks . set ( fallbackKey , goal ) ;
1200+ setThreadGoalFallback ( threadGoalFallbacks , fallbackKey , goal ) ;
11731201 writeJson ( res , HTTP_STATUS . OK , { ok : true , goal } ) ;
11741202 return ;
11751203 }
11761204 writeJson ( res , HTTP_STATUS . OK , {
1177- goal : threadGoalFallbacks . get ( fallbackKey ) ?? null ,
1205+ goal : getThreadGoalFallback ( threadGoalFallbacks , fallbackKey ) ,
11781206 } ) ;
11791207 return ;
11801208 }
0 commit comments