11import { PanelProvider } from '@/views/panel/backend/panel-provider'
22import { ApiManagerProvider } from '@/views/api-manager/backend/api-manager-provider'
33import { make_api_request } from '@/utils/make-api-request'
4- import axios , { CancelTokenSource } from 'axios'
4+ import axios from 'axios'
55import { randomUUID , createHash } from 'crypto'
66import { Logger } from '@shared/utils/logger'
77
88const CHAIN_RESOLUTION_DELAY_MS = 5000
99
1010export class ApiManager {
11- private cancel_token_sources : Map < string , CancelTokenSource > = new Map ( )
11+ private abort_controllers : Map < string , AbortController > = new Map ( )
1212 private next_allowed_finish_time = 0
1313 private waiting_chain : Map <
1414 string ,
@@ -35,8 +35,8 @@ export class ApiManager {
3535 reasoning_effort ?: string
3636 } ) : Promise < { response : string ; thoughts ?: string } | null > {
3737 const request_id = params . request_id || randomUUID ( )
38- const cancel_token_source = axios . CancelToken . source ( )
39- this . cancel_token_sources . set ( request_id , cancel_token_source )
38+ const abort_controller = new AbortController ( )
39+ this . abort_controllers . set ( request_id , abort_controller )
4040
4141 const body_to_hash = JSON . parse ( JSON . stringify ( params . body ) )
4242 delete body_to_hash . reasoning_effort
@@ -91,13 +91,21 @@ export class ApiManager {
9191 } )
9292
9393 if ( is_queued ) {
94- if ( cancel_token_source . token . reason ) {
95- throw cancel_token_source . token . reason
94+ if ( abort_controller . signal . aborted ) {
95+ throw abort_controller . signal . reason
9696 }
97- await Promise . race ( [
98- previous_waiting . promise ,
99- cancel_token_source . token . promise . then ( ( c ) => Promise . reject ( c ) )
100- ] )
97+
98+ const abort_promise = new Promise < void > ( ( _ , reject ) => {
99+ abort_controller . signal . addEventListener (
100+ 'abort' ,
101+ ( ) => {
102+ reject ( abort_controller . signal . reason )
103+ } ,
104+ { once : true }
105+ )
106+ } )
107+
108+ await Promise . race ( [ previous_waiting . promise , abort_promise ] )
101109
102110 this . broadcast_message ( {
103111 command : 'SHOW_API_MANAGER_PROGRESS' ,
@@ -113,7 +121,7 @@ export class ApiManager {
113121 endpoint_url : params . endpoint_url ,
114122 api_key : params . api_key ,
115123 body : params . body ,
116- cancellation_token : cancel_token_source . token ,
124+ abort_signal : abort_controller . signal ,
117125 on_thinking_chunk : ( ) => {
118126 schedule_chain_resolution ( )
119127 this . broadcast_message ( {
@@ -162,7 +170,7 @@ export class ApiManager {
162170 command : 'HIDE_API_MANAGER_PROGRESS' ,
163171 id : request_id
164172 } )
165- this . cancel_token_sources . delete ( request_id )
173+ this . abort_controllers . delete ( request_id )
166174
167175 const now = Date . now ( )
168176 const wait_until = Math . max ( now , this . next_allowed_finish_time )
@@ -176,17 +184,17 @@ export class ApiManager {
176184 }
177185
178186 public cancel_api_call ( request_id : string ) {
179- const source = this . cancel_token_sources . get ( request_id )
180- if ( source ) {
181- source . cancel ( 'Cancelled by user.' )
182- this . cancel_token_sources . delete ( request_id )
187+ const controller = this . abort_controllers . get ( request_id )
188+ if ( controller ) {
189+ controller . abort ( 'Cancelled by user.' )
190+ this . abort_controllers . delete ( request_id )
183191 }
184192 }
185193
186194 public cancel_all_requests ( ) {
187- this . cancel_token_sources . forEach ( ( source ) => {
188- source . cancel ( 'Cancelled by user.' )
195+ this . abort_controllers . forEach ( ( controller ) => {
196+ controller . abort ( 'Cancelled by user.' )
189197 } )
190- this . cancel_token_sources . clear ( )
198+ this . abort_controllers . clear ( )
191199 }
192200}
0 commit comments