Skip to content

Commit 7f0eac2

Browse files
committed
Replace axios cancel tokens with standard abortcontroller across API requests and upgrade axios version
- [Pasted text]
1 parent b6ad130 commit 7f0eac2

18 files changed

Lines changed: 183 additions & 146 deletions

‎apps/editor/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@
12511251
"@mozilla/readability": "^0.6.0",
12521252
"@svgr/webpack": "^8.1.0",
12531253
"@vscode/codicons": "^0.0.45",
1254-
"axios": "^1.10.0",
1254+
"axios": "^1.18.1",
12551255
"bufferutil": "^4.0.9",
12561256
"classnames": "^2.5.1",
12571257
"dayjs": "^1.11.10",

‎apps/editor/src/commands/apply-response-command/handlers/active-editor-intelligent-update-handler.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ export const handle_active_editor_intelligent_update = async (params: {
102102
return null
103103
}
104104

105-
const cancel_token_source = axios.CancelToken.source()
105+
const abort_controller = new AbortController()
106106
if (params.panel_provider) {
107-
params.panel_provider.api_call_cancel_token_source = cancel_token_source
107+
params.panel_provider.api_call_abort_controller = abort_controller
108108
params.panel_provider.send_message({
109109
command: 'SHOW_PROGRESS',
110110
title: 'Thinking...',
@@ -133,7 +133,7 @@ export const handle_active_editor_intelligent_update = async (params: {
133133
file_path: target_file_path,
134134
file_content: original_content,
135135
instruction: file_item.content,
136-
cancel_token: cancel_token_source.token,
136+
abort_signal: abort_controller.signal,
137137
on_chunk: (tokens_per_second, total_tokens) => {
138138
if (params.panel_provider) {
139139
const estimated_total = Math.ceil(original_content.length / 4)
@@ -221,7 +221,7 @@ export const handle_active_editor_intelligent_update = async (params: {
221221
params.panel_provider.send_message({
222222
command: 'HIDE_PROGRESS'
223223
})
224-
params.panel_provider.api_call_cancel_token_source = null
224+
params.panel_provider.api_call_abort_controller = null
225225
}
226226
}
227227
}

‎apps/editor/src/commands/code-at-cursor-command/utils/perform-code-at-cursor.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const perform_code_at_cursor = async (params: {
108108
)
109109
return
110110
}
111-
const cancel_token_source = axios.CancelToken.source()
111+
const abort_controller = new AbortController()
112112
const document = editor.document
113113
const position = editor.selection.active
114114

@@ -170,7 +170,7 @@ export const perform_code_at_cursor = async (params: {
170170

171171
const cursor_listener = vscode.window.onDidChangeTextEditorSelection(
172172
() => {
173-
cancel_token_source.cancel(
173+
abort_controller.abort(
174174
t('command.code-at-cursor.cancel.cursor-moved')
175175
)
176176
}
@@ -185,9 +185,7 @@ export const perform_code_at_cursor = async (params: {
185185
},
186186
async (progress, token) => {
187187
token.onCancellationRequested(() => {
188-
cancel_token_source.cancel(
189-
t('command.code-at-cursor.cancel.user')
190-
)
188+
abort_controller.abort(t('command.code-at-cursor.cancel.user'))
191189
})
192190

193191
progress.report({
@@ -198,7 +196,7 @@ export const perform_code_at_cursor = async (params: {
198196
endpoint_url,
199197
api_key: model_provider.api_key,
200198
body,
201-
cancellation_token: cancel_token_source.token,
199+
abort_signal: abort_controller.signal,
202200
on_chunk: () => {
203201
progress.report({ message: t('common.progress.receiving') })
204202
},

‎apps/editor/src/features/commit-messages/utils/generate-commit-message-with-api.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const generate_commit_message_with_api = async (params: {
3636

3737
const token_count = Math.ceil(params.message.length / 4)
3838

39-
const cancel_token_source = axios.CancelToken.source()
39+
const abort_controller = new AbortController()
4040

4141
return await vscode.window.withProgress(
4242
{
@@ -46,7 +46,7 @@ export const generate_commit_message_with_api = async (params: {
4646
},
4747
async (progress, token) => {
4848
token.onCancellationRequested(() => {
49-
cancel_token_source.cancel('Operation cancelled by user')
49+
abort_controller.abort('Operation cancelled by user')
5050
})
5151

5252
progress.report({
@@ -60,7 +60,7 @@ export const generate_commit_message_with_api = async (params: {
6060
endpoint_url: params.endpoint_url,
6161
api_key: params.model_provider.api_key,
6262
body,
63-
cancellation_token: cancel_token_source.token,
63+
abort_signal: abort_controller.signal,
6464
on_chunk: () => {
6565
progress.report({ message: t('common.progress.receiving') })
6666
},

‎apps/editor/src/features/search-files/utils/fetch-relevant-files-from-api.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const fetch_relevant_files_from_api = async (
5252
reasoning_effort: selected_config.reasoning_effort
5353
})
5454

55-
const cancel_token_source = axios.CancelToken.source()
55+
const abort_controller = new AbortController()
5656

5757
try {
5858
const completion_result = await vscode.window.withProgress(
@@ -63,14 +63,14 @@ export const fetch_relevant_files_from_api = async (
6363
},
6464
async (progress, token) => {
6565
token.onCancellationRequested(() => {
66-
cancel_token_source.cancel(t('feature.search-files.cancel.user'))
66+
abort_controller.abort(t('feature.search-files.cancel.user'))
6767
})
6868
progress.report({ message: t('common.progress.waiting-for-server') })
6969
return await make_api_request({
7070
endpoint_url: model_provider.base_url,
7171
api_key: model_provider.api_key,
7272
body,
73-
cancellation_token: cancel_token_source.token,
73+
abort_signal: abort_controller.signal,
7474
on_chunk: () =>
7575
progress.report({ message: t('common.progress.receiving') }),
7676
on_thinking_chunk: () =>

‎apps/editor/src/services/api-manager.ts‎

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { PanelProvider } from '@/views/panel/backend/panel-provider'
22
import { ApiManagerProvider } from '@/views/api-manager/backend/api-manager-provider'
33
import { make_api_request } from '@/utils/make-api-request'
4-
import axios, { CancelTokenSource } from 'axios'
4+
import axios from 'axios'
55
import { randomUUID, createHash } from 'crypto'
66
import { Logger } from '@shared/utils/logger'
77

88
const CHAIN_RESOLUTION_DELAY_MS = 5000
99

1010
export 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
}

‎apps/editor/src/utils/intelligent-update-utils.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode'
2-
import axios, { CancelToken } from 'axios'
2+
import axios from 'axios'
33
import {
44
ModelProvidersManager,
55
ApiConfiguration,
@@ -108,7 +108,7 @@ export const process_file = async (params: {
108108
file_path: string
109109
file_content: string
110110
instruction: string
111-
cancel_token?: CancelToken
111+
abort_signal?: AbortSignal
112112
on_chunk?: (tokens_per_second: number, total_tokens: number) => void
113113
on_thinking_chunk?: (text: string) => void
114114
}): Promise<string> => {
@@ -144,7 +144,7 @@ export const process_file = async (params: {
144144
endpoint_url: params.endpoint_url,
145145
api_key: params.api_key,
146146
body,
147-
cancellation_token: params.cancel_token,
147+
abort_signal: params.abort_signal,
148148
on_chunk: params.on_chunk,
149149
on_thinking_chunk: params.on_thinking_chunk,
150150
rethrow_error: true

‎apps/editor/src/utils/make-api-request.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const make_api_request = async (params: {
7272
endpoint_url: string
7373
api_key?: string
7474
body: any
75-
cancellation_token: any
75+
abort_signal?: AbortSignal
7676
on_chunk?: StreamCallback
7777
on_thinking_chunk?: ThinkingStreamCallback
7878
rethrow_error?: boolean
@@ -257,7 +257,7 @@ export const make_api_request = async (params: {
257257
try {
258258
response = await axios.post(request_url, request_body, {
259259
headers,
260-
cancelToken: params.cancellation_token,
260+
signal: params.abort_signal,
261261
responseType: 'stream'
262262
})
263263
break
@@ -398,8 +398,8 @@ export const make_api_request = async (params: {
398398
})
399399
})
400400
} catch (error) {
401-
if (params.cancellation_token?.reason) {
402-
throw params.cancellation_token.reason
401+
if (params.abort_signal?.aborted) {
402+
throw error
403403
}
404404

405405
if (axios.isCancel(error)) {

‎apps/editor/src/views/panel/backend/message-handlers/handle-cancel-intelligent-update-file-in-preview.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ export const handle_cancel_intelligent_update_file_in_preview = (
1010
const { file_path, workspace_name } = message
1111

1212
const source_index =
13-
panel_provider.intelligent_update_cancel_token_sources.findIndex(
13+
panel_provider.intelligent_update_abort_controllers.findIndex(
1414
(s) => s.file_path === file_path && s.workspace_name === workspace_name
1515
)
1616

1717
if (source_index > -1) {
18-
const { source } =
19-
panel_provider.intelligent_update_cancel_token_sources[source_index]
18+
const { controller } =
19+
panel_provider.intelligent_update_abort_controllers[source_index]
2020

21-
source.cancel('User cancelled the operation')
21+
controller.abort('User cancelled the operation')
2222

23-
panel_provider.intelligent_update_cancel_token_sources.splice(
23+
panel_provider.intelligent_update_abort_controllers.splice(
2424
source_index,
2525

2626
1

‎apps/editor/src/views/panel/backend/message-handlers/handle-fix-all-failed-files.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode'
2-
import axios, { CancelTokenSource } from 'axios'
2+
import axios from 'axios'
33
import { PanelProvider } from '@/views/panel/backend/panel-provider'
44
import {
55
LAST_APPLIED_CHANGES_STATE_KEY,
@@ -126,7 +126,7 @@ export const handle_fix_all_failed_files = async (params: {
126126
})
127127
.filter((item) => item.instructions && item.safe_path)
128128

129-
const batch_cancel_sources: CancelTokenSource[] = []
129+
const batch_abort_controllers: AbortController[] = []
130130

131131
try {
132132
await Promise.all(
@@ -136,11 +136,11 @@ export const handle_fix_all_failed_files = async (params: {
136136
const file_path = file_state.file_path
137137
const workspace_name = file_state.workspace_name
138138

139-
const cancel_token_source = axios.CancelToken.source()
140-
batch_cancel_sources.push(cancel_token_source)
139+
const abort_controller = new AbortController()
140+
batch_abort_controllers.push(abort_controller)
141141

142-
params.panel_provider.intelligent_update_cancel_token_sources.push({
143-
source: cancel_token_source,
142+
params.panel_provider.intelligent_update_abort_controllers.push({
143+
controller: abort_controller,
144144
file_path,
145145
workspace_name
146146
})
@@ -198,7 +198,7 @@ export const handle_fix_all_failed_files = async (params: {
198198
file_path: file_path,
199199
file_content: file_state.content,
200200
instruction: instructions,
201-
cancel_token: cancel_token_source.token,
201+
abort_signal: abort_controller.signal,
202202
on_chunk,
203203
on_thinking_chunk
204204
})
@@ -253,8 +253,8 @@ export const handle_fix_all_failed_files = async (params: {
253253
data: { error, file_path }
254254
})
255255

256-
batch_cancel_sources.forEach((source) => {
257-
source.cancel(
256+
batch_abort_controllers.forEach((controller) => {
257+
controller.abort(
258258
'Batch operation failed, triggering configuration selection.'
259259
)
260260
})
@@ -276,11 +276,11 @@ export const handle_fix_all_failed_files = async (params: {
276276
})
277277

278278
const index =
279-
params.panel_provider.intelligent_update_cancel_token_sources.findIndex(
280-
(s) => s.source === cancel_token_source
279+
params.panel_provider.intelligent_update_abort_controllers.findIndex(
280+
(s) => s.controller === abort_controller
281281
)
282282
if (index > -1) {
283-
params.panel_provider.intelligent_update_cancel_token_sources.splice(
283+
params.panel_provider.intelligent_update_abort_controllers.splice(
284284
index,
285285
1
286286
)

0 commit comments

Comments
 (0)