@providerkit/core 0.26.0 → 0.26.2

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/src/errors.ts CHANGED
@@ -10,7 +10,12 @@
10
10
  // read before the status — status alone gives the wrong advice ("retry" for an
11
11
  // empty balance, "check your key" for a plan that never included the API).
12
12
 
13
- import { parseRateLimitResponse, parseUsageLimitBody, type RateLimitWindow } from "./rate-limit.ts";
13
+ import {
14
+ parseRateLimitReset,
15
+ parseRateLimitResponse,
16
+ parseUsageLimitBody,
17
+ type RateLimitWindow,
18
+ } from "./rate-limit.ts";
14
19
  import type { FilePart } from "./types.ts";
15
20
 
16
21
  /** What kind of failure this is, named by what actually fixes it. */
@@ -164,11 +169,12 @@ export class ProviderError extends Error {
164
169
  const code = readString(err, "code") ?? readString(err, "type");
165
170
  const message = messageOf(err);
166
171
  const body = bodyTextOf(err);
172
+ const resetBody = providerBodyOf(err) ?? body;
167
173
  return new ProviderError(provider, classify(err, status, body), message, {
168
174
  status,
169
175
  code,
170
- retryAfterMs: parseRetryAfterMs(err, body),
171
- ...parseUsageLimitBody(body),
176
+ retryAfterMs: parseRetryAfterMs(err, resetBody),
177
+ ...parseUsageLimitBody(resetBody),
172
178
  body: body.slice(0, 2_000) || undefined,
173
179
  cause: err,
174
180
  });
@@ -218,13 +224,17 @@ export function messageOf(err: unknown): string {
218
224
  */
219
225
  function bodyTextOf(err: unknown): string {
220
226
  const message = messageOf(err);
221
- if (typeof err !== "object" || err === null) return message;
222
- const body = (err as Record<string, unknown>).error;
223
- if (body === undefined) return message;
227
+ const body = providerBodyOf(err);
228
+ return body === undefined ? message : `${message} ${body}`;
229
+ }
230
+
231
+ /** Kept as JSON for reset parsing; the searchable text above is not JSON. */
232
+ function providerBodyOf(err: unknown): string | undefined {
233
+ if (typeof err !== "object" || err === null || !("error" in err)) return undefined;
224
234
  try {
225
- return `${message} ${typeof body === "string" ? body : JSON.stringify(body)}`;
235
+ return typeof err.error === "string" ? err.error : JSON.stringify(err.error);
226
236
  } catch {
227
- return message;
237
+ return undefined;
228
238
  }
229
239
  }
230
240
 
@@ -361,7 +371,7 @@ const CONTEXT_PATTERNS: readonly RegExp[] = [
361
371
  // it, and its wording overlaps both other categories ("plan" appears in all
362
372
  // three), so it is checked first.
363
373
  const ENTITLEMENT_PATTERNS: readonly RegExp[] = [
364
- /plan does(?:n't| not) (?:include|support)/i,
374
+ /plan does(?:n't| not) (?:yet )?(?:include|support)/i,
365
375
  /not included (?:in|with) your .{0,40}plan/i,
366
376
  /upgrade to [\w ]{1,30}(?:or higher|plan)/i,
367
377
  /requires? (?:the |a |an )?[\w\s]{1,30}plan/i,
@@ -381,6 +391,9 @@ const QUOTA_PATTERNS: readonly RegExp[] = [
381
391
  /insufficient[_\s]quota/i,
382
392
  /exceeded your current quota/i,
383
393
  /insufficient (?:balance|credits?)/i,
394
+ /insufficient (?:account )?funds/i,
395
+ /\bcredit limit exceeded\b/i,
396
+ /\baccount invoice is overdue\b/i,
384
397
  /credit balance is too low/i,
385
398
  /(?:no|any|out of) credits/i,
386
399
  /(?:credits?|balance) depleted/i,
@@ -401,6 +414,13 @@ const QUOTA_PATTERNS: readonly RegExp[] = [
401
414
  // The OpenAI-shape `type` field for billing failures, serialized into the
402
415
  // scanned text — a quota signal with no prose wording at all (codex-router).
403
416
  /billing_error/i,
417
+ // OpenCode's typed account caps can carry throttle prose or no prose at all.
418
+ // Their names survive both the HTTP and in-band error envelopes.
419
+ /\b(?:GoUsageLimitError|BudgetLimitError|FreeUsageLimitError|CreditLimitExceeded)\b/i,
420
+ /\bbudget exceeded\b/i,
421
+ // Z.ai reports expired coding packages and spent weekly/monthly windows as 429.
422
+ /\bpackage has expired\b/i,
423
+ /\b(?:weekly|monthly) limit exhausted\b/i,
404
424
  // Alibaba intl's English spelling of 欠费.
405
425
  /\barrears?\b/i,
406
426
  /余额不足/,
@@ -721,19 +741,34 @@ const STATUS_BY_TYPE: ReadonlyMap<string, number> = new Map([
721
741
  * body does name keeps its own kind, which is what tells the caller to wait,
722
742
  * rotate a key, or compact instead of retrying a failure that will repeat.
723
743
  */
724
- export function streamError(provider: string, error: unknown): ProviderError {
744
+ export function streamError(provider: string, envelope: unknown): ProviderError {
745
+ // Only failure data is searchable or logged. A failed Responses object can
746
+ // echo instructions, tool definitions and output alongside its error.
747
+ const outer =
748
+ typeof envelope === "object" && envelope !== null ? (envelope as Record<string, unknown>) : {};
749
+ const error = outer.error ?? outer.errors ?? envelope;
725
750
  const body = JSON.stringify(error ?? {}) ?? "";
726
751
  const status = [
727
752
  readNumber(error, "code"),
728
753
  readNumber(error, "status"),
729
754
  readNumber(error, "status_code"),
755
+ readNumber(envelope, "status_code"),
730
756
  ].find((value) => value !== undefined && value >= 400 && value < 600);
731
757
  const code =
732
758
  readString(error, "code") ?? readString(error, "status") ?? readString(error, "type");
733
759
  const typeStatus =
734
760
  STATUS_BY_TYPE.get(readString(error, "type") ?? "") ??
735
761
  STATUS_BY_TYPE.get(readString(error, "code") ?? "");
736
- const kind = classifyHttp(status ?? typeStatus, body);
762
+ // Keep type-derived 4xx precedence. Only a generic 5xx label without a real
763
+ // status may hide an account cap; OpenCode uses server_error for those.
764
+ const classified = classifyHttp(status ?? typeStatus, body);
765
+ const kind =
766
+ status === undefined &&
767
+ typeStatus !== undefined &&
768
+ typeStatus >= 500 &&
769
+ (matches(QUOTA_PATTERNS, body) || matches(ENTITLEMENT_PATTERNS, body))
770
+ ? classifyHttp(undefined, body)
771
+ : classified;
737
772
  return new ProviderError(
738
773
  provider,
739
774
  kind === "unknown" ? "overload" : kind,
@@ -741,27 +776,35 @@ export function streamError(provider: string, error: unknown): ProviderError {
741
776
  {
742
777
  ...(status !== undefined ? { status } : {}),
743
778
  ...(code ? { code } : {}),
744
- retryAfterMs: parseRetryAfterMs(error, body),
745
- ...readReset(error, body),
779
+ ...readReset(envelope, error, body, kind),
746
780
  body: body.slice(0, 2_000),
747
781
  },
748
782
  );
749
783
  }
750
784
 
751
- /** The reset a stream error carries: the body's clock, plus the rate-limit
752
- * headers the ChatGPT backend mirrors into its error frame, the only place a
753
- * failure after the 200 can say which window ran out. */
754
- function readReset(error: unknown, body: string) {
755
- const raw =
756
- typeof error === "object" && error !== null
757
- ? (error as { headers?: unknown }).headers
758
- : undefined;
759
- if (typeof raw !== "object" || raw === null) return parseUsageLimitBody(body);
785
+ /** Read reset metadata without copying echoed request data into diagnostics.
786
+ * Mirrored account windows describe only rate/quota failures, not a 503. */
787
+ function readReset(envelope: unknown, error: unknown, body: string, kind: ErrorKind) {
760
788
  const headers = new Headers();
761
- for (const [name, value] of Object.entries(raw)) {
762
- if (typeof value === "string" || typeof value === "number") headers.set(name, String(value));
789
+ for (const source of [error, envelope]) {
790
+ if (typeof source !== "object" || source === null || !("headers" in source)) continue;
791
+ const raw = source.headers;
792
+ if (typeof raw !== "object" || raw === null) continue;
793
+ for (const [name, value] of Object.entries(raw)) {
794
+ if (typeof value === "string" || typeof value === "number") headers.set(name, String(value));
795
+ }
763
796
  }
764
- return parseRateLimitResponse(headers, body);
797
+ if (kind !== "rate" && kind !== "quota") {
798
+ return {
799
+ retryAfterMs: parseRateLimitReset(headers).retryAfterMs ?? parseRetryAfterMs(error, body),
800
+ };
801
+ }
802
+ const metadata =
803
+ typeof envelope === "object" && envelope !== null && "metadata" in envelope
804
+ ? envelope.metadata
805
+ : undefined;
806
+ const reset = parseRateLimitResponse(headers, JSON.stringify({ error, metadata }));
807
+ return { ...reset, retryAfterMs: reset.retryAfterMs ?? parseRetryAfterMs(error, body) };
765
808
  }
766
809
 
767
810
  /**
@@ -578,7 +578,7 @@ export function createAnthropicProvider(config: AnthropicConfig): Provider {
578
578
  // most of the time, but a prompt found too long mid-stream arrives the
579
579
  // same way, and retrying that one only fails it again more slowly.
580
580
  case "error":
581
- throw streamError(id, event.error);
581
+ throw streamError(id, event);
582
582
 
583
583
  case "message_start": {
584
584
  const usage = event.message?.usage;
@@ -84,6 +84,11 @@ export interface PresetProviderConfig extends ProviderFallbackConfig {
84
84
  * between rounds. Pinning keeps a conversation's rounds (and their cache)
85
85
  * on one host. Empty/absent = default routing. */
86
86
  providerOrder?: string[];
87
+ /** OpenRouter-only: `provider.data_collection`. `"deny"` keeps the request
88
+ * on hosts that don't train on the prompt. Not zero retention: that is
89
+ * OpenRouter's separate `zdr`. Absent = the account's own setting. Never
90
+ * sent to another host. */
91
+ dataCollection?: "allow" | "deny";
87
92
  /** Default `service_tier` for every call on the OpenAI and Responses shapes;
88
93
  * `StreamOptions.serviceTier` wins. Anthropic and Gemini presets refuse it. */
89
94
  serviceTier?: ServiceTier;
@@ -208,6 +213,7 @@ function buildWireAdapter(
208
213
  ...(wire.path ? { path: wire.path } : {}),
209
214
  headers,
210
215
  ...(config.providerOrder ? { providerOrder: config.providerOrder } : {}),
216
+ ...(config.dataCollection ? { dataCollection: config.dataCollection } : {}),
211
217
  ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
212
218
  ...(preset.initiatorHeader ? { initiatorHeader: true } : {}),
213
219
  ...(preset.noSampling ? { noSampling: true } : {}),
@@ -594,7 +594,7 @@ export function createGeminiProvider(config: GeminiConfig): Provider {
594
594
  // `?alt=sse` commits to 200 the moment the headers go out, so a
595
595
  // throttle or an overload landing after that arrives here as a
596
596
  // google.rpc.Status in the body rather than as a status line.
597
- if (chunk.error) throw streamError(id, chunk.error);
597
+ if (chunk.error) throw streamError(id, chunk);
598
598
  // A blocked PROMPT has no candidate to finish, so it reads as a cut
599
599
  // stream — retried, into the same block. It is a content failure.
600
600
  const blocked = chunk.promptFeedback?.blockReason;
@@ -103,6 +103,16 @@ export interface OpenAIConfig extends ProviderFallbackConfig {
103
103
  * when hitting openrouter.ai and providerOrder is not passed. Defaults to true.
104
104
  */
105
105
  pinHost?: boolean;
106
+ /**
107
+ * OpenRouter's `provider.data_collection`. `"deny"` routes only to hosts
108
+ * that, by OpenRouter's records, don't train on the prompt; a model with no
109
+ * such host answers 404 instead of falling back to one that does. It is not
110
+ * zero retention: some of those hosts keep prompts for a while, which only
111
+ * OpenRouter's separate `zdr` rules out. Sent on OpenRouter only, with or
112
+ * without a pin; any other host never sees it. Unset leaves the account's
113
+ * own setting in charge.
114
+ */
115
+ dataCollection?: "allow" | "deny";
106
116
  /** Header that carries `StreamOptions.sessionId`. When set and a call has
107
117
  * no sessionId, the provider's own per-instance id rides instead, so the
108
118
  * gateway never sees a call without one. */
@@ -654,8 +664,12 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
654
664
  })()
655
665
  : undefined;
656
666
 
657
- if (pinOrder?.length) {
658
- request.provider = { order: pinOrder, allow_fallbacks: true };
667
+ const dataCollection = isOpenRouter(baseUrl) ? config.dataCollection : undefined;
668
+ if (pinOrder?.length || dataCollection) {
669
+ request.provider = {
670
+ ...(pinOrder?.length ? { order: pinOrder, allow_fallbacks: true } : {}),
671
+ ...(dataCollection ? { data_collection: dataCollection } : {}),
672
+ };
659
673
  }
660
674
 
661
675
  const defaultPath = /\/v\d+[^/]*$/i.test(baseUrl.replace(/\/+$/, ""))
@@ -701,7 +715,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
701
715
  // line, and a frame carrying `error` carries no choices: unread, it
702
716
  // falls through both branches below and the turn ends as a successful
703
717
  // zero-token completion nobody retries.
704
- if (chunk.error) throw streamError(id, chunk.error);
718
+ if (chunk.error) throw streamError(id, chunk);
705
719
 
706
720
  // A usage-only frame carries no choices — this shape sends it last.
707
721
  if (chunk.usage) {
@@ -689,7 +689,7 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
689
689
  // error failed, and one that states `incomplete` was cut. Read as
690
690
  // a clean stop, either is a broken turn reported as a finished one.
691
691
  if (response?.status === "failed" || response?.error) {
692
- throw streamError(id, response.error ?? { message: "response failed" });
692
+ throw streamError(id, response.error ? response : { message: "response failed" });
693
693
  }
694
694
  // Any other status (`cancelled`, `queued`, one added later) says
695
695
  // nothing about the answer being whole. Read as a clean stop, it
@@ -734,7 +734,10 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
734
734
  // out: a terminal `response.failed`, or a bare error frame from a
735
735
  // gateway in front of it.
736
736
  case "response.failed":
737
- throw streamError(id, event.response?.error);
737
+ throw streamError(
738
+ id,
739
+ event.response?.error ? event.response : { message: "response failed" },
740
+ );
738
741
 
739
742
  case "error":
740
743
  case "response.error":
@@ -742,12 +745,7 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
742
745
  // them a spent Codex window reads as an unnamed in-stream failure:
743
746
  // floored to overload, then retried and walked across every backup
744
747
  // on the same account wall.
745
- throw streamError(
746
- id,
747
- event.error
748
- ? { ...event.error, status_code: event.status_code, headers: event.headers }
749
- : { message: event.message, code: event.code },
750
- );
748
+ throw streamError(id, event);
751
749
  }
752
750
  }
753
751
 
package/src/rate-limit.ts CHANGED
@@ -315,6 +315,28 @@ export function parseUsageLimitBody(bodyText: string, now = Date.now()): RateLim
315
315
  ? (source as Record<string, unknown>)
316
316
  : (body as Record<string, unknown>);
317
317
 
318
+ // OpenCode puts a budget's ISO reset in envelope metadata, or in Google's
319
+ // ErrorInfo details. It names the real horizon, not a short retry hint.
320
+ const envelopes: unknown[] = [
321
+ body,
322
+ error,
323
+ ...(Array.isArray(error.details) ? error.details : []),
324
+ ];
325
+ let metadataReset = -Infinity;
326
+ for (const envelope of envelopes) {
327
+ if (typeof envelope !== "object" || envelope === null || !("metadata" in envelope)) continue;
328
+ const metadata = envelope.metadata;
329
+ if (
330
+ typeof metadata !== "object" ||
331
+ metadata === null ||
332
+ !("resetsAt" in metadata) ||
333
+ typeof metadata.resetsAt !== "string"
334
+ )
335
+ continue;
336
+ const at = Date.parse(metadata.resetsAt);
337
+ if (Number.isFinite(at)) metadataReset = Math.max(metadataReset, at);
338
+ }
339
+
318
340
  const inSeconds = jsonSeconds(error.resets_in_seconds);
319
341
  const atSeconds = jsonSeconds(error.resets_at);
320
342
  // The plain wait some gateways put in the error body instead of a header,
@@ -323,12 +345,12 @@ export function parseUsageLimitBody(bodyText: string, now = Date.now()): RateLim
323
345
  jsonSeconds(error.retry_after) ??
324
346
  jsonSeconds(error.retry_after_seconds) ??
325
347
  jsonSeconds((body as Record<string, unknown>).retry_after_seconds);
326
- if (inSeconds !== undefined && inSeconds >= 0) return resetAfter(inSeconds * 1000, now);
327
- if (atSeconds !== undefined && atSeconds > 0) {
328
- return resetAfter(Math.max(0, atSeconds * 1000 - now), now);
329
- }
330
- if (afterSeconds !== undefined && afterSeconds >= 0) {
331
- return resetAfter(afterSeconds * 1000, now);
348
+ let waitMs: number | undefined;
349
+ if (inSeconds !== undefined && inSeconds >= 0) waitMs = inSeconds * 1000;
350
+ else if (atSeconds !== undefined && atSeconds > 0) waitMs = Math.max(0, atSeconds * 1000 - now);
351
+ else if (afterSeconds !== undefined && afterSeconds >= 0) waitMs = afterSeconds * 1000;
352
+ if (waitMs !== undefined || Number.isFinite(metadataReset)) {
353
+ return resetAfter(Math.max(0, waitMs ?? 0, metadataReset - now), now);
332
354
  }
333
355
  }
334
356