11import { PanelProvider } from '@/views/panel/backend/panel-provider'
2+ import { ApiManagerProvider } from '@/views/api-manager/backend/api-manager-provider'
23import { make_api_request } from '@/utils/make-api-request'
34import axios , { CancelTokenSource } from 'axios'
45import { randomUUID , createHash } from 'crypto'
@@ -9,13 +10,20 @@ const CHAIN_RESOLUTION_DELAY_MS = 5000
910export class ApiManager {
1011 private cancel_token_sources : Map < string , CancelTokenSource > = new Map ( )
1112 private next_allowed_finish_time = 0
12- // Waiting pipeline to utilize KV cache for the same endpoint urls
1313 private waiting_chain : Map <
1414 string ,
1515 { promise : Promise < void > ; resolve : ( ) => void ; body_hash : string }
1616 > = new Map ( )
1717
18- constructor ( private panel_provider : PanelProvider ) { }
18+ constructor (
19+ private panel_provider : PanelProvider ,
20+ private api_manager_provider : ApiManagerProvider
21+ ) { }
22+
23+ private broadcast_message ( message : any ) {
24+ this . panel_provider . send_message ( message )
25+ this . api_manager_provider . send_message ( message )
26+ }
1927
2028 public async get ( params : {
2129 endpoint_url : string
@@ -73,8 +81,7 @@ export class ApiManager {
7381 const is_queued =
7482 previous_waiting && previous_waiting . body_hash == body_hash
7583
76- // 1. Show appropriate initial state (Queued vs Waiting)
77- this . panel_provider . send_message ( {
84+ this . broadcast_message ( {
7885 command : 'SHOW_API_MANAGER_PROGRESS' ,
7986 id : request_id ,
8087 title : is_queued ? 'Queued...' : 'Waiting for server...' ,
@@ -83,7 +90,6 @@ export class ApiManager {
8390 reasoning_effort : params . reasoning_effort
8491 } )
8592
86- // 2. Handle the queuing block if this is a duplicate request
8793 if ( is_queued ) {
8894 if ( cancel_token_source . token . reason ) {
8995 throw cancel_token_source . token . reason
@@ -93,8 +99,7 @@ export class ApiManager {
9399 cancel_token_source . token . promise . then ( ( c ) => Promise . reject ( c ) )
94100 ] )
95101
96- // 3. Queue cleared, update UI to show we are now hitting the server
97- this . panel_provider . send_message ( {
102+ this . broadcast_message ( {
98103 command : 'SHOW_API_MANAGER_PROGRESS' ,
99104 id : request_id ,
100105 title : 'Waiting for server...' ,
@@ -111,7 +116,7 @@ export class ApiManager {
111116 cancellation_token : cancel_token_source . token ,
112117 on_thinking_chunk : ( ) => {
113118 schedule_chain_resolution ( )
114- this . panel_provider . send_message ( {
119+ this . broadcast_message ( {
115120 command : 'SHOW_API_MANAGER_PROGRESS' ,
116121 id : request_id ,
117122 title : 'Thinking...' ,
@@ -122,7 +127,7 @@ export class ApiManager {
122127 } ,
123128 on_chunk : ( tokens_per_second , total_tokens ) => {
124129 schedule_chain_resolution ( )
125- this . panel_provider . send_message ( {
130+ this . broadcast_message ( {
126131 command : 'SHOW_API_MANAGER_PROGRESS' ,
127132 id : request_id ,
128133 title : 'Receiving...' ,
@@ -149,18 +154,16 @@ export class ApiManager {
149154
150155 return null
151156 } finally {
152- // Unblock anyone waiting for this request if it was the one in the chain
153157 if ( ! is_chain_resolution_scheduled ) {
154158 resolve_current ( )
155159 }
156160
157- this . panel_provider . send_message ( {
161+ this . broadcast_message ( {
158162 command : 'HIDE_API_MANAGER_PROGRESS' ,
159163 id : request_id
160164 } )
161165 this . cancel_token_sources . delete ( request_id )
162166
163- // Prevent race conditions of the applyResponse command when invoked in short successions
164167 const now = Date . now ( )
165168 const wait_until = Math . max ( now , this . next_allowed_finish_time )
166169 const delay = wait_until - now
0 commit comments