@prestyj/agent 5.11.0 → 5.12.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
@@ -83,7 +83,13 @@ function isContextOverflow(err) {
83
83
  if (overflowStatus === 402) return false;
84
84
  if (isBillingError(err)) return false;
85
85
  const msg = err.message.toLowerCase();
86
- 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");
86
+ if (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")) {
87
+ return true;
88
+ }
89
+ const rateLimited = overflowStatus === 429 || msg.includes("rate limit") || msg.includes("rate_limit") || msg.includes("too many requests");
90
+ const perUnitTime = msg.includes("per min") || msg.includes("/min") || msg.includes("per minute") || msg.includes("per hour") || msg.includes("per day") || msg.includes("tpm") || msg.includes("rpm");
91
+ if (rateLimited && perUnitTime) return false;
92
+ return msg.includes("token") && msg.includes("exceed");
87
93
  }
88
94
  function parseOverflowNumber(value) {
89
95
  return Number(value.replace(/[,_\s]/g, ""));
@@ -196,6 +202,17 @@ function isMalformedStream(err) {
196
202
  const msg = err.message;
197
203
  return /\bin JSON at position \d+/i.test(msg);
198
204
  }
205
+ var TIMEOUT_NAMES = /* @__PURE__ */ new Set(["TimeoutError", "ConnectTimeoutError", "HeadersTimeoutError"]);
206
+ var TIMEOUT_MESSAGES = [
207
+ /^request timed out\.?$/i,
208
+ /\brequest to [\w .-]+ timed out\b/i,
209
+ /\b(?:connection|socket|headers|stream) timed out\b/i
210
+ ];
211
+ function isBareTimeout(e) {
212
+ if (typeof e.name === "string" && TIMEOUT_NAMES.has(e.name)) return true;
213
+ if (typeof e.message !== "string") return false;
214
+ return TIMEOUT_MESSAGES.some((re) => re.test(e.message));
215
+ }
199
216
  function isTransportFailure(err) {
200
217
  const codes = /* @__PURE__ */ new Set([
201
218
  "ECONNRESET",
@@ -232,6 +249,8 @@ function isTransportFailure(err) {
232
249
  if (typeof e.message === "string") {
233
250
  for (const re of messages) if (re.test(e.message)) return true;
234
251
  }
252
+ const clientError = typeof e.status === "number" && e.status >= 400 && e.status < 500;
253
+ if (!clientError && isBareTimeout(e)) return true;
235
254
  cur = e.cause;
236
255
  }
237
256
  return false;
@@ -457,6 +476,21 @@ async function* agentLoop(messages, options) {
457
476
  diag("stream_call", { nonStreaming: useNonStreamingFallback });
458
477
  streamCallStart = Date.now();
459
478
  providerAttemptStartedAt = streamCallStart;
479
+ let liveApiKey = options.apiKey;
480
+ let liveAccountId = options.accountId;
481
+ let liveProjectId = options.projectId;
482
+ if (options.resolveCredentials) {
483
+ try {
484
+ const fresh = await options.resolveCredentials();
485
+ liveApiKey = fresh.apiKey;
486
+ if (fresh.accountId !== void 0) liveAccountId = fresh.accountId;
487
+ if (fresh.projectId !== void 0) liveProjectId = fresh.projectId;
488
+ } catch (credErr) {
489
+ diag("credential_refresh_failed", {
490
+ error: (credErr instanceof Error ? credErr.message : String(credErr)).slice(0, 200)
491
+ });
492
+ }
493
+ }
460
494
  const result = (0, import_ai.stream)({
461
495
  provider: options.provider,
462
496
  model: options.model,
@@ -468,12 +502,12 @@ async function* agentLoop(messages, options) {
468
502
  maxTokens: options.maxTokens,
469
503
  temperature: options.temperature,
470
504
  thinking: options.thinking,
471
- apiKey: options.apiKey,
505
+ apiKey: liveApiKey,
472
506
  baseUrl: options.baseUrl,
473
507
  signal: streamController.signal,
474
- accountId: options.accountId,
508
+ accountId: liveAccountId,
475
509
  transportSessionId: options.transportSessionId,
476
- projectId: options.projectId,
510
+ projectId: liveProjectId,
477
511
  cacheRetention: options.cacheRetention,
478
512
  promptCacheKey: options.promptCacheKey,
479
513
  serviceTier: options.serviceTier,