@@ -4,6 +4,8 @@ import axios, { CancelTokenSource } from 'axios'
44import { randomUUID , createHash } from 'crypto'
55import { Logger } from '@shared/utils/logger'
66
7+ const CHAIN_RESOLUTION_DELAY_MS = 2000
8+
79export class ApiManager {
810 private cancel_token_sources : Map < string , CancelTokenSource > = new Map ( )
911 private next_allowed_finish_time = 0
@@ -35,6 +37,22 @@ export class ApiManager {
3537 resolve_current = resolve
3638 } )
3739
40+ let is_chain_resolution_scheduled = false
41+ const schedule_chain_resolution = ( ) => {
42+ if ( is_chain_resolution_scheduled ) return
43+ const chain_entry = this . waiting_chain . get ( params . endpoint_url )
44+ if ( chain_entry && chain_entry . resolve === resolve_current ) {
45+ is_chain_resolution_scheduled = true
46+ setTimeout ( ( ) => {
47+ chain_entry . resolve ( )
48+ const current_entry = this . waiting_chain . get ( params . endpoint_url )
49+ if ( current_entry && current_entry . resolve === resolve_current ) {
50+ this . waiting_chain . delete ( params . endpoint_url )
51+ }
52+ } , CHAIN_RESOLUTION_DELAY_MS )
53+ }
54+ }
55+
3856 if ( ! previous_waiting || previous_waiting . body_hash !== body_hash ) {
3957 this . waiting_chain . set ( params . endpoint_url , {
4058 promise : current_promise ,
@@ -60,23 +78,15 @@ export class ApiManager {
6078 body : params . body ,
6179 cancellation_token : cancel_token_source . token ,
6280 on_thinking_chunk : ( ) => {
63- const chain_entry = this . waiting_chain . get ( params . endpoint_url )
64- if ( chain_entry && chain_entry . resolve === resolve_current ) {
65- chain_entry . resolve ( )
66- this . waiting_chain . delete ( params . endpoint_url )
67- }
81+ schedule_chain_resolution ( )
6882 this . panel_provider . send_message ( {
6983 command : 'SHOW_API_MANAGER_PROGRESS' ,
7084 id : request_id ,
7185 title : 'Thinking...'
7286 } )
7387 } ,
7488 on_chunk : ( tokens_per_second , total_tokens ) => {
75- const chain_entry = this . waiting_chain . get ( params . endpoint_url )
76- if ( chain_entry && chain_entry . resolve === resolve_current ) {
77- chain_entry . resolve ( )
78- this . waiting_chain . delete ( params . endpoint_url )
79- }
89+ schedule_chain_resolution ( )
8090 this . panel_provider . send_message ( {
8191 command : 'SHOW_API_MANAGER_PROGRESS' ,
8292 id : request_id ,
@@ -104,10 +114,12 @@ export class ApiManager {
104114 return null
105115 } finally {
106116 // Unblock anyone waiting for this request if it was the one in the chain
107- const chain_entry = this . waiting_chain . get ( params . endpoint_url )
108- if ( chain_entry && chain_entry . resolve === resolve_current ) {
109- chain_entry . resolve ( )
110- this . waiting_chain . delete ( params . endpoint_url )
117+ if ( ! is_chain_resolution_scheduled ) {
118+ const chain_entry = this . waiting_chain . get ( params . endpoint_url )
119+ if ( chain_entry && chain_entry . resolve === resolve_current ) {
120+ chain_entry . resolve ( )
121+ this . waiting_chain . delete ( params . endpoint_url )
122+ }
111123 }
112124
113125 this . panel_provider . send_message ( {
0 commit comments