@@ -7,6 +7,8 @@ import { Logger } from '@shared/utils/logger'
77export class ApiManager {
88 private cancel_token_sources : Map < string , CancelTokenSource > = new Map ( )
99 private next_allowed_finish_time = 0
10+ // Waiting pipeline to utilize KV cache for the same endpoint urls
11+ private waiting_chain : Map < string , Promise < void > > = new Map ( )
1012
1113 constructor ( private panel_provider : PanelProvider ) { }
1214
@@ -20,26 +22,39 @@ export class ApiManager {
2022 const cancel_token_source = axios . CancelToken . source ( )
2123 this . cancel_token_sources . set ( request_id , cancel_token_source )
2224
25+ const previous_waiting_promise = this . waiting_chain . get ( params . endpoint_url )
26+ let resolve_waiting : ( ) => void = ( ) => { }
27+ const waiting_promise = new Promise < void > ( ( resolve ) => {
28+ resolve_waiting = resolve
29+ } )
30+ this . waiting_chain . set ( params . endpoint_url , waiting_promise )
31+
2332 try {
2433 this . panel_provider . send_message ( {
2534 command : 'SHOW_API_MANAGER_PROGRESS' ,
2635 id : request_id ,
2736 title : 'Waiting...'
2837 } )
2938
39+ if ( previous_waiting_promise ) {
40+ await previous_waiting_promise
41+ }
42+
3043 const result = await make_api_request ( {
3144 endpoint_url : params . endpoint_url ,
3245 api_key : params . api_key ,
3346 body : params . body ,
3447 cancellation_token : cancel_token_source . token ,
3548 on_thinking_chunk : ( ) => {
49+ resolve_waiting ( )
3650 this . panel_provider . send_message ( {
3751 command : 'SHOW_API_MANAGER_PROGRESS' ,
3852 id : request_id ,
3953 title : 'Thinking...'
4054 } )
4155 } ,
4256 on_chunk : ( tokens_per_second , total_tokens ) => {
57+ resolve_waiting ( )
4358 this . panel_provider . send_message ( {
4459 command : 'SHOW_API_MANAGER_PROGRESS' ,
4560 id : request_id ,
@@ -56,7 +71,7 @@ export class ApiManager {
5671 function_name : 'get' ,
5772 message : 'API call cancelled by user'
5873 } )
59- throw error // Re-throw cancellation error to be handled by caller
74+ throw error
6075 }
6176
6277 Logger . error ( {
@@ -66,6 +81,7 @@ export class ApiManager {
6681 } )
6782 return null
6883 } finally {
84+ resolve_waiting ( )
6985 this . panel_provider . send_message ( {
7086 command : 'HIDE_API_MANAGER_PROGRESS' ,
7187 id : request_id
0 commit comments