@prestyj/agent 4.3.239 → 4.5.0

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.cjs CHANGED
@@ -53,6 +53,8 @@ function isAbortError(err) {
53
53
  }
54
54
  function isContextOverflow(err) {
55
55
  if (!(err instanceof Error)) return false;
56
+ const overflowStatus = err.statusCode;
57
+ if (overflowStatus === 402) return false;
56
58
  if (isBillingError(err)) return false;
57
59
  const msg = err.message.toLowerCase();
58
60
  return msg.includes("prompt is too long") || msg.includes("prompt too long") || msg.includes("input is too long") || msg.includes("context_length_exceeded") || msg.includes("context_window_exceeded") || msg.includes("maximum context length") || msg.includes("exceeds model context window") || msg.includes("exceeds the context window") || msg.includes("content_too_large") || msg.includes("request_too_large") || msg.includes("reduce the length") || msg.includes("please shorten") || msg.includes("token") && msg.includes("exceed");
@@ -97,13 +99,21 @@ function extractContextOverflowDetails(err) {
97
99
  }
98
100
  function isBillingError(err) {
99
101
  if (!(err instanceof Error)) return false;
100
- const msg = err.message.toLowerCase();
101
- return msg.includes("insufficient balance") || msg.includes("no resource package") || msg.includes("quota exceeded") || msg.includes("billing") || msg.includes("recharge") || msg.includes("subscription plan") || msg.includes("does not yet include access") || msg.includes("token quota") || msg.includes("exceeded_current_quota_error") || msg.includes("check your account balance");
102
+ const statusCode = err.statusCode;
103
+ if (statusCode === 402) return true;
104
+ return (0, import_ai.isHardBillingMessage)(err.message);
102
105
  }
103
106
  function isUsageLimitError(err) {
104
107
  if (!(err instanceof Error)) return false;
105
108
  return /usage limit reached/i.test(err.message);
106
109
  }
110
+ function serverResetDelayMs(err) {
111
+ if (!(err instanceof Error)) return void 0;
112
+ const resetsAt = err.resetsAt;
113
+ if (typeof resetsAt !== "number" || !Number.isFinite(resetsAt)) return void 0;
114
+ const delayMs = resetsAt * 1e3 - Date.now();
115
+ return delayMs > 0 ? delayMs : void 0;
116
+ }
107
117
  function isToolPairingError(err) {
108
118
  if (!(err instanceof Error)) return false;
109
119
  const msg = err.message.toLowerCase();
@@ -124,6 +134,7 @@ function classifyOverload(err) {
124
134
  const msg = err.message.toLowerCase();
125
135
  const errorWithStatus = err;
126
136
  const statusCode = typeof errorWithStatus.statusCode === "number" ? errorWithStatus.statusCode : void 0;
137
+ if (statusCode === 402) return null;
127
138
  if (statusCode === 429 || msg.includes("rate_limit") || msg.includes("rate limit") || msg.includes("too many requests") || msg.includes("429")) {
128
139
  return "rate_limit";
129
140
  }
@@ -183,23 +194,51 @@ function isTransportFailure(err) {
183
194
  }
184
195
  return false;
185
196
  }
197
+ function createAbortError() {
198
+ return new DOMException("Aborted", "AbortError");
199
+ }
200
+ function abortablePromise(promise, signal) {
201
+ if (!signal) return promise;
202
+ if (signal.aborted) return Promise.reject(createAbortError());
203
+ return new Promise((resolve, reject) => {
204
+ let settled = false;
205
+ const cleanup = () => signal.removeEventListener("abort", onAbort);
206
+ const resolveOnce = (value) => {
207
+ if (settled) return;
208
+ settled = true;
209
+ cleanup();
210
+ resolve(value);
211
+ };
212
+ const rejectOnce = (err) => {
213
+ if (settled) return;
214
+ settled = true;
215
+ cleanup();
216
+ reject(err instanceof Error ? err : new Error(String(err)));
217
+ };
218
+ const onAbort = () => rejectOnce(createAbortError());
219
+ signal.addEventListener("abort", onAbort, { once: true });
220
+ promise.then(resolveOnce, rejectOnce);
221
+ });
222
+ }
186
223
  function abortableSleep(ms, signal) {
187
- if (signal?.aborted) {
188
- return Promise.reject(new DOMException("Aborted", "AbortError"));
189
- }
224
+ if (signal?.aborted) return Promise.reject(createAbortError());
190
225
  return new Promise((resolve, reject) => {
191
- let onAbort = null;
192
226
  const timer = setTimeout(() => {
193
- if (onAbort) signal?.removeEventListener("abort", onAbort);
227
+ signal?.removeEventListener("abort", onAbort);
194
228
  resolve();
195
229
  }, ms);
196
- onAbort = () => {
230
+ const onAbort = () => {
197
231
  clearTimeout(timer);
198
- reject(new DOMException("Aborted", "AbortError"));
232
+ reject(createAbortError());
199
233
  };
200
234
  signal?.addEventListener("abort", onAbort, { once: true });
201
235
  });
202
236
  }
237
+ function closeIterator(iterator) {
238
+ if (!iterator?.return) return;
239
+ Promise.resolve(iterator.return()).catch(() => {
240
+ });
241
+ }
203
242
  async function* agentLoop(messages, options) {
204
243
  const maxTurns = options.maxTurns ?? DEFAULT_MAX_TURNS;
205
244
  const maxContinuations = options.maxContinuations ?? 5;
@@ -324,6 +363,7 @@ async function* agentLoop(messages, options) {
324
363
  idleTimedOut = true;
325
364
  streamController.abort();
326
365
  }, hardTimeoutMs);
366
+ let streamIterator = null;
327
367
  try {
328
368
  diag("stream_call", { nonStreaming: useNonStreamingFallback });
329
369
  streamCallStart = Date.now();
@@ -346,9 +386,11 @@ async function* agentLoop(messages, options) {
346
386
  promptCacheKey: options.promptCacheKey,
347
387
  serviceTier: options.serviceTier,
348
388
  supportsImages: options.supportsImages,
389
+ supportsVideo: options.supportsVideo,
349
390
  compaction: options.compaction,
350
391
  clearToolUses: options.clearToolUses,
351
392
  userAgent: options.userAgent,
393
+ defaultHeaders: options.defaultHeaders,
352
394
  // Flip to non-streaming fallback after repeated stream stalls.
353
395
  ...useNonStreamingFallback ? { streaming: false } : {}
354
396
  });
@@ -361,7 +403,11 @@ async function* agentLoop(messages, options) {
361
403
  streamCallStart = Date.now();
362
404
  lastYieldEndTime = Date.now();
363
405
  resetIdleTimer();
364
- for await (const event of result) {
406
+ streamIterator = result[Symbol.asyncIterator]();
407
+ while (true) {
408
+ const next = await abortablePromise(streamIterator.next(), streamController.signal);
409
+ if (next.done) break;
410
+ const event = next.value;
365
411
  const pullTime = Date.now();
366
412
  const consumerLag = pullTime - lastYieldEndTime;
367
413
  if (streamEventCount > 0 && consumerLag > maxConsumerLagMs) {
@@ -458,8 +504,9 @@ async function* agentLoop(messages, options) {
458
504
  maxConsumerLagMs,
459
505
  eventTypes: eventTypeCounts
460
506
  });
461
- response = await result.response;
507
+ response = await abortablePromise(result.response, streamController.signal);
462
508
  } catch (err) {
509
+ if (streamController.signal.aborted) closeIterator(streamIterator);
463
510
  const errMsg = err instanceof Error ? err.message : String(err);
464
511
  diag("stream_error", {
465
512
  error: errMsg.slice(0, 200),
@@ -558,7 +605,8 @@ async function* agentLoop(messages, options) {
558
605
  const overloadKind = classifyOverload(err);
559
606
  if (overloadRetries < MAX_OVERLOAD_RETRIES && overloadKind) {
560
607
  overloadRetries++;
561
- const delayMs = Math.min(
608
+ const serverDelayMs = serverResetDelayMs(err);
609
+ const delayMs = serverDelayMs !== void 0 ? Math.min(serverDelayMs, OVERLOAD_MAX_DELAY_MS) : Math.min(
562
610
  OVERLOAD_BASE_DELAY_MS * 2 ** (overloadRetries - 1),
563
611
  OVERLOAD_MAX_DELAY_MS
564
612
  );
@@ -858,7 +906,10 @@ async function executeSingleToolCall(toolCall, options, pushEvent) {
858
906
  });
859
907
  }
860
908
  };
861
- const raw = await tool.execute(parsed, ctx);
909
+ const raw = await abortablePromise(
910
+ Promise.resolve().then(() => tool.execute(parsed, ctx)),
911
+ ctx.signal
912
+ );
862
913
  const normalized = normalizeToolResult(raw);
863
914
  resultContent = normalized.content;
864
915
  details = normalized.details;