@@ -4,7 +4,7 @@ import axios, { CancelTokenSource } from 'axios'
44import { randomUUID , createHash } from 'crypto'
55import { Logger } from '@shared/utils/logger'
66
7- const CHAIN_RESOLUTION_DELAY_MS = 3000
7+ const CHAIN_RESOLUTION_DELAY_MS = 5000
88
99export class ApiManager {
1010 private cancel_token_sources : Map < string , CancelTokenSource > = new Map ( )
@@ -33,6 +33,7 @@ export class ApiManager {
3333 const body_hash = createHash ( 'md5' )
3434 . update ( JSON . stringify ( params . body ) )
3535 . digest ( 'hex' )
36+
3637 const previous_waiting = this . waiting_chain . get ( params . endpoint_url )
3738
3839 let resolve_current : ( ) => void = ( ) => { }
@@ -43,11 +44,13 @@ export class ApiManager {
4344 let is_chain_resolution_scheduled = false
4445 const schedule_chain_resolution = ( ) => {
4546 if ( is_chain_resolution_scheduled ) return
47+
4648 const chain_entry = this . waiting_chain . get ( params . endpoint_url )
4749 if ( chain_entry && chain_entry . resolve === resolve_current ) {
4850 is_chain_resolution_scheduled = true
4951 setTimeout ( ( ) => {
5052 chain_entry . resolve ( )
53+
5154 const current_entry = this . waiting_chain . get ( params . endpoint_url )
5255 if ( current_entry && current_entry . resolve === resolve_current ) {
5356 this . waiting_chain . delete ( params . endpoint_url )
@@ -65,23 +68,38 @@ export class ApiManager {
6568 }
6669
6770 try {
71+ const is_queued =
72+ previous_waiting && previous_waiting . body_hash == body_hash
73+
74+ // 1. Show appropriate initial state (Queued vs Waiting)
6875 this . panel_provider . send_message ( {
6976 command : 'SHOW_API_MANAGER_PROGRESS' ,
7077 id : request_id ,
71- title : 'Waiting for server...' ,
78+ title : is_queued ? 'Queued...' : 'Waiting for server...' ,
7279 provider_name : params . provider_name ,
7380 model : params . model ,
7481 reasoning_effort : params . reasoning_effort
7582 } )
7683
77- if ( previous_waiting && previous_waiting . body_hash == body_hash ) {
84+ // 2. Handle the queuing block if this is a duplicate request
85+ if ( is_queued ) {
7886 if ( cancel_token_source . token . reason ) {
7987 throw cancel_token_source . token . reason
8088 }
8189 await Promise . race ( [
8290 previous_waiting . promise ,
8391 cancel_token_source . token . promise . then ( ( c ) => Promise . reject ( c ) )
8492 ] )
93+
94+ // 3. Queue cleared, update UI to show we are now hitting the server
95+ this . panel_provider . send_message ( {
96+ command : 'SHOW_API_MANAGER_PROGRESS' ,
97+ id : request_id ,
98+ title : 'Waiting for server...' ,
99+ provider_name : params . provider_name ,
100+ model : params . model ,
101+ reasoning_effort : params . reasoning_effort
102+ } )
85103 }
86104
87105 const result = await make_api_request ( {
@@ -114,6 +132,7 @@ export class ApiManager {
114132 } )
115133 }
116134 } )
135+
117136 return result
118137 } catch ( error ) {
119138 if ( axios . isCancel ( error ) ) {
0 commit comments