@@ -116,7 +116,6 @@ export async function constructRequest(
116
116
return {
117
117
url : url . toString ( ) ,
118
118
fetchOptions : {
119
- ...buildFetchOptions ( requestOptions ) ,
120
119
method : 'POST' ,
121
120
headers : await getHeaders ( url ) ,
122
121
body
@@ -134,6 +133,7 @@ export async function makeRequest(
134
133
) : Promise < Response > {
135
134
const url = new RequestUrl ( model , task , apiSettings , stream , requestOptions ) ;
136
135
let response ;
136
+ let fetchTimeoutId : string | number | NodeJS . Timeout | undefined ;
137
137
try {
138
138
const request = await constructRequest (
139
139
model ,
@@ -143,6 +143,15 @@ export async function makeRequest(
143
143
body ,
144
144
requestOptions
145
145
) ;
146
+ // Timeout is 180s by default
147
+ const timeoutMillis =
148
+ requestOptions ?. timeout !== undefined
149
+ ? requestOptions . timeout
150
+ : 180 * 1000 ;
151
+ const abortController = new AbortController ( ) ;
152
+ fetchTimeoutId = setTimeout ( ( ) => abortController . abort ( ) , timeoutMillis ) ;
153
+ request . fetchOptions . signal = abortController . signal ;
154
+
146
155
response = await fetch ( request . url , request . fetchOptions ) ;
147
156
if ( ! response . ok ) {
148
157
let message = '' ;
@@ -211,24 +220,10 @@ export async function makeRequest(
211
220
}
212
221
213
222
throw err ;
223
+ } finally {
224
+ if ( fetchTimeoutId ) {
225
+ clearTimeout ( fetchTimeoutId ) ;
226
+ }
214
227
}
215
228
return response ;
216
229
}
217
-
218
- /**
219
- * Generates the request options to be passed to the fetch API.
220
- * @param requestOptions - The user-defined request options.
221
- * @returns The generated request options.
222
- */
223
- function buildFetchOptions ( requestOptions ?: RequestOptions ) : RequestInit {
224
- const fetchOptions = { } as RequestInit ;
225
- let timeoutMillis = 180 * 1000 ; // default: 180 s
226
- if ( requestOptions ?. timeout && requestOptions ?. timeout >= 0 ) {
227
- timeoutMillis = requestOptions . timeout ;
228
- }
229
- const abortController = new AbortController ( ) ;
230
- const signal = abortController . signal ;
231
- setTimeout ( ( ) => abortController . abort ( ) , timeoutMillis ) ;
232
- fetchOptions . signal = signal ;
233
- return fetchOptions ;
234
- }
0 commit comments