generated from zirkelc/template-single-typescript
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrequest-timeout.ts
More file actions
38 lines (35 loc) · 1.04 KB
/
request-timeout.ts
File metadata and controls
38 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type {
EmbeddingModel,
ImageModel,
ResolvableLanguageModel,
Retryable,
RetryableOptions,
} from '../types.js';
import { isErrorAttempt, isTimeoutError } from '../utils.js';
/**
* Fallback to a different model after a timeout/abort error.
* Use in combination with the `abortSignal` option.
* If no timeout is specified, a default of 60 seconds is used.
*/
export function requestTimeout<
MODEL extends ResolvableLanguageModel | EmbeddingModel | ImageModel,
>(model: MODEL, options?: RetryableOptions<MODEL>): Retryable<MODEL> {
return (context) => {
const { current } = context;
if (isErrorAttempt(current)) {
/**
* Fallback to the specified model after a timeout/abort error.
* Provides a fresh timeout signal for the retry attempt.
*/
if (isTimeoutError(current.error)) {
return {
model,
maxAttempts: 1,
timeout: options?.timeout ?? 60000, // Default 60 second timeout for retry
...options,
};
}
}
return undefined;
};
}