ai 5.0.0-beta.19 → 5.0.0-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -95,7 +95,33 @@ var RetryError = class extends AISDKError3 {
95
95
  _a3 = symbol3;
96
96
 
97
97
  // src/util/retry-with-exponential-backoff.ts
98
- var retryWithExponentialBackoff = ({
98
+ function getRetryDelay(error, exponentialBackoffDelay) {
99
+ const headers = error.responseHeaders;
100
+ if (!headers)
101
+ return exponentialBackoffDelay;
102
+ let timeoutMillis;
103
+ const retryAfterMs = headers["retry-after-ms"];
104
+ if (retryAfterMs) {
105
+ const timeoutMs = parseFloat(retryAfterMs);
106
+ if (!Number.isNaN(timeoutMs)) {
107
+ timeoutMillis = timeoutMs;
108
+ }
109
+ }
110
+ const retryAfter = headers["retry-after"];
111
+ if (retryAfter && timeoutMillis === void 0) {
112
+ const timeoutSeconds = parseFloat(retryAfter);
113
+ if (!Number.isNaN(timeoutSeconds)) {
114
+ timeoutMillis = timeoutSeconds * 1e3;
115
+ } else {
116
+ timeoutMillis = Date.parse(retryAfter) - Date.now();
117
+ }
118
+ }
119
+ if (timeoutMillis !== void 0 && 0 <= timeoutMillis && timeoutMillis < 60 * 1e3) {
120
+ return timeoutMillis;
121
+ }
122
+ return exponentialBackoffDelay;
123
+ }
124
+ var retryWithExponentialBackoffRespectingRetryHeaders = ({
99
125
  maxRetries = 2,
100
126
  initialDelayInMs = 2e3,
101
127
  backoffFactor = 2
@@ -129,7 +155,8 @@ async function _retryWithExponentialBackoff(f, {
129
155
  });
130
156
  }
131
157
  if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
132
- await delay(delayInMs);
158
+ const actualDelay = getRetryDelay(error, delayInMs);
159
+ await delay(actualDelay);
133
160
  return _retryWithExponentialBackoff(
134
161
  f,
135
162
  { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
@@ -170,7 +197,9 @@ function prepareRetries({
170
197
  const maxRetriesResult = maxRetries != null ? maxRetries : 2;
171
198
  return {
172
199
  maxRetries: maxRetriesResult,
173
- retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
200
+ retry: retryWithExponentialBackoffRespectingRetryHeaders({
201
+ maxRetries: maxRetriesResult
202
+ })
174
203
  };
175
204
  }
176
205