Skip to content

Commit ad6a9cc

Browse files
committed
Update model verification functions to use streaming responses and enhance cancellation handling
- we need our test to be as fast as possible, is it usefult o place such [Pasted text] thing if for some reason [Fragment] is not supported by the provider? - "Promise executor functions should not be async." - use axios instead of fetch
1 parent df25656 commit ad6a9cc

2 files changed

Lines changed: 54 additions & 34 deletions

File tree

‎apps/editor/src/views/shared/actions/api/create/interactions/verify-model.ts‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export const verify_model = async (params: {
2727
try {
2828
const messages: any[] = params.is_voice_input
2929
? [
30+
{
31+
role: 'user',
32+
content: 'Respond with "OK", and nothing else.'
33+
},
3034
{
3135
role: 'user',
3236
content: [
@@ -47,16 +51,21 @@ export const verify_model = async (params: {
4751
: [
4852
{
4953
role: 'user',
50-
content: 'Hello!'
54+
content: 'Respond with "OK", and nothing else.'
5155
}
5256
]
5357

58+
const cancel_token_source = axios.CancelToken.source()
59+
const disposable = token.onCancellationRequested(() => {
60+
cancel_token_source.cancel('User cancelled')
61+
})
62+
5463
await axios.post(
5564
`${params.base_url}/chat/completions`,
5665
{
5766
model: params.model,
5867
messages,
59-
max_tokens: 1
68+
stream: true
6069
},
6170
{
6271
headers: {
@@ -65,17 +74,20 @@ export const verify_model = async (params: {
6574
? { Authorization: `Bearer ${params.api_key}` }
6675
: {})
6776
},
68-
cancelToken: new axios.CancelToken((c) => {
69-
token.onCancellationRequested(() => {
70-
c('User cancelled')
71-
})
72-
})
77+
cancelToken: cancel_token_source.token,
78+
responseType: 'stream'
7379
}
7480
)
81+
82+
cancel_token_source.cancel('Verified')
83+
disposable.dispose()
7584
success = true
7685
} catch (e: any) {
77-
if (!token.isCancellationRequested) {
78-
console.log(e.response?.data || e.message)
86+
if (!axios.isCancel(e)) {
87+
if (e.response) {
88+
e.status = e.response.status
89+
}
90+
console.log(e.message)
7991
error = e
8092
}
8193
}
@@ -105,9 +117,9 @@ export const verify_model = async (params: {
105117
'views.shared.actions.api.create.interactions.verify-model.warning.test.detail'
106118
)
107119

108-
if (axios.isAxiosError(error)) {
109-
if (error.response) {
110-
const status = error.response.status
120+
if (error) {
121+
if (error.status) {
122+
const status = error.status
111123
let reason = t(
112124
'views.shared.actions.api.create.interactions.verify-model.status.server-error'
113125
)
@@ -164,7 +176,7 @@ export const verify_model = async (params: {
164176
reason
165177
}
166178
)
167-
} else if (error.code) {
179+
} else if (error.message) {
168180
detail = params.is_voice_input
169181
? t(
170182
'views.shared.actions.api.create.interactions.verify-model.warning.audio.detail'

‎apps/editor/src/views/shared/actions/api/create/interactions/verify-reasoning-effort.ts‎

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode'
2-
import axios from 'axios'
32
import { ModelProvider } from '@/services/model-providers-manager'
43
import { apply_reasoning_effort } from '@/utils/apply-reasoning-effort'
4+
import axios from 'axios'
55

66
export const verify_reasoning_effort = async (params: {
77
endpoint_url: string
@@ -11,10 +11,10 @@ export const verify_reasoning_effort = async (params: {
1111
model_provider: ModelProvider
1212
cancellation_token: vscode.CancellationToken
1313
}): Promise<void> => {
14-
const cancel_source = axios.CancelToken.source()
14+
const cancel_token_source = axios.CancelToken.source()
1515

1616
const disposable = params.cancellation_token.onCancellationRequested(() => {
17-
cancel_source.cancel('User cancelled')
17+
cancel_token_source.cancel('User cancelled')
1818
})
1919

2020
const body = {
@@ -45,36 +45,44 @@ export const verify_reasoning_effort = async (params: {
4545
: {}),
4646
['Content-Type']: 'application/json'
4747
},
48-
responseType: 'stream',
49-
cancelToken: cancel_source.token
48+
cancelToken: cancel_token_source.token,
49+
responseType: 'stream'
5050
}
5151
)
5252

5353
await new Promise<void>((resolve, reject) => {
54-
const stream = response.data
55-
56-
stream.on('data', () => {
57-
cancel_source.cancel('Verified')
58-
resolve()
54+
let resolved = false
55+
response.data.on('data', () => {
56+
if (!resolved) {
57+
resolved = true
58+
cancel_token_source.cancel('Verified')
59+
resolve()
60+
}
5961
})
60-
61-
stream.on('error', (err: any) => {
62-
reject(err)
62+
response.data.on('end', () => {
63+
if (!resolved) {
64+
resolved = true
65+
resolve()
66+
}
6367
})
64-
65-
stream.on('end', () => {
66-
resolve()
68+
response.data.on('error', (err: any) => {
69+
if (!resolved) {
70+
resolved = true
71+
reject(err)
72+
}
6773
})
6874
})
69-
} catch (error) {
75+
} catch (error: any) {
7076
if (axios.isCancel(error)) {
71-
if (error.message == 'Verified') {
72-
return
73-
}
74-
if (error.message == 'User cancelled') {
77+
if (
78+
error.message === 'User cancelled' ||
79+
params.cancellation_token.isCancellationRequested
80+
) {
7581
throw new Error('Cancelled')
7682
}
83+
return
7784
}
85+
7886
throw error
7987
} finally {
8088
disposable.dispose()

0 commit comments

Comments
 (0)