@providerkit/core 0.34.0 → 0.35.1
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/README.md +18 -0
- package/dist/decisions.js +1 -1
- package/dist/decisions.js.map +1 -1
- package/dist/errors.d.ts +2 -7
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +114 -69
- package/dist/errors.js.map +1 -1
- package/dist/hosted/anthropic.d.ts +4 -2
- package/dist/hosted/anthropic.d.ts.map +1 -1
- package/dist/hosted/anthropic.js +7 -6
- package/dist/hosted/anthropic.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +113 -12
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +5 -4
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/openai.d.ts +3 -34
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +10 -40
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/openrouter-billing.d.ts +27 -0
- package/dist/providers/openrouter-billing.d.ts.map +1 -0
- package/dist/providers/openrouter-billing.js +37 -0
- package/dist/providers/openrouter-billing.js.map +1 -0
- package/dist/providers/responses.d.ts +2 -1
- package/dist/providers/responses.d.ts.map +1 -1
- package/dist/providers/responses.js +26 -13
- package/dist/providers/responses.js.map +1 -1
- package/dist/rate-limit.d.ts +11 -8
- package/dist/rate-limit.d.ts.map +1 -1
- package/dist/rate-limit.js +123 -77
- package/dist/rate-limit.js.map +1 -1
- package/dist/tools.d.ts +2 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js.map +1 -1
- package/dist/transport.d.ts +6 -0
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +12 -9
- package/dist/transport.js.map +1 -1
- package/dist/watchdog.d.ts.map +1 -1
- package/dist/watchdog.js +4 -1
- package/dist/watchdog.js.map +1 -1
- package/dist/zod.d.ts +17 -0
- package/dist/zod.d.ts.map +1 -1
- package/dist/zod.js +34 -14
- package/dist/zod.js.map +1 -1
- package/package.json +1 -1
- package/src/decisions.ts +1 -1
- package/src/errors.ts +128 -70
- package/src/hosted/anthropic.ts +7 -6
- package/src/index.ts +9 -2
- package/src/providers/anthropic.ts +124 -12
- package/src/providers/gemini.ts +5 -4
- package/src/providers/openai.ts +29 -63
- package/src/providers/openrouter-billing.ts +42 -0
- package/src/providers/responses.ts +29 -15
- package/src/rate-limit.ts +132 -79
- package/src/tools.ts +2 -0
- package/src/transport.ts +14 -13
- package/src/watchdog.ts +4 -0
- package/src/zod.ts +48 -10
package/dist/zod.d.ts
CHANGED
|
@@ -38,6 +38,23 @@ export interface ZodToolSpec<I, O, R = O> {
|
|
|
38
38
|
* Off by default: an ordinary tool can simply be called again.
|
|
39
39
|
*/
|
|
40
40
|
clampOverflow?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Writes the problems zod found in your words instead of zod's, for the input check and the
|
|
43
|
+
* `output` check. zod's sentence can list every value a field accepts and quote the caller's
|
|
44
|
+
* key names, and an app that saves tool replies keeps that text: an adopter shows each reply
|
|
45
|
+
* again in its studio for a week, so it words a problem from its path and code alone. That
|
|
46
|
+
* tells the model less. A bare `invalid_type` at `sheets` once sent a model back to resend
|
|
47
|
+
* the same call, which is why the default keeps zod's sentence.
|
|
48
|
+
*
|
|
49
|
+
* It gets zod's error, with every issue, and `checked`: `input` for the model's arguments,
|
|
50
|
+
* `result` for what `run` returned. On input it words the same parse `invoke` and `accepts`
|
|
51
|
+
* decide by, after the repairs. Only the default stops at five issues. A check that throws
|
|
52
|
+
* reads as its own message, and so does a `describeIssues` that throws. On both checks, the
|
|
53
|
+
* error the tool throws keeps zod's error as its `cause`, or what was thrown in its place, so
|
|
54
|
+
* your logs find it at `ToolOutcome.cause.cause`. Defaults to zod's message for each issue,
|
|
55
|
+
* after its path.
|
|
56
|
+
*/
|
|
57
|
+
describeIssues?: (error: z.ZodError, checked: "input" | "result") => string;
|
|
41
58
|
}
|
|
42
59
|
/**
|
|
43
60
|
* A zod tool that checks its results too. It returns what `output` makes of `run`'s result, so
|
package/dist/zod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../src/zod.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAc,KAAK,IAAI,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAGrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAanD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,EAAE,KAAK,SAAW,GAAG,gBAAgB,CASxF;AAED,0FAA0F;AAC1F,MAAM,WAAW,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IACpB;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACzB,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAChD,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,MAAM,CAAC;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,UAAU,CAAC,EAAE,OAAO,C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O,EAAE,OAAO,GAAG,QAAQ,KAAK,MAAM,CAAC;CAC7E;AA+HD;;;GAGG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAC7B,IAAI,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG;IAAE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CAAE,GACvD,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACd;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
package/dist/zod.js
CHANGED
|
@@ -38,7 +38,7 @@ const MAX_ISSUES = 5;
|
|
|
38
38
|
* call. The rest are counted, so a model that fixes five does not take them for all of it.
|
|
39
39
|
* `root` names a problem with the whole value: the model's arguments, or the tool's result.
|
|
40
40
|
*/
|
|
41
|
-
function describeIssues(issues, root) {
|
|
41
|
+
function describeIssues({ issues }, root) {
|
|
42
42
|
const lines = issues
|
|
43
43
|
.slice(0, MAX_ISSUES)
|
|
44
44
|
.map((issue) => `${issue.path.map(String).join(".") || root}: ${issue.message}`);
|
|
@@ -123,26 +123,30 @@ function repairSlips(raw, issues) {
|
|
|
123
123
|
* `spec.run` with each result checked against `output` before the model or your code reads it.
|
|
124
124
|
* The result is read as unknown: `output` decides its type, whatever `run` declared.
|
|
125
125
|
*/
|
|
126
|
-
function checkedRun(spec, output) {
|
|
126
|
+
function checkedRun(spec, output, describe) {
|
|
127
127
|
return async (input, ctx) => {
|
|
128
128
|
const result = await spec.run(input, ctx);
|
|
129
129
|
let problems;
|
|
130
|
+
let cause;
|
|
130
131
|
try {
|
|
131
132
|
// Async, because the sync parse throws on an async refine or transform.
|
|
132
133
|
const parsed = await output.safeParseAsync(result);
|
|
133
134
|
if (parsed.success)
|
|
134
135
|
return parsed.data;
|
|
135
|
-
|
|
136
|
+
cause = parsed.error;
|
|
137
|
+
// Inside the try, so a wording that throws still tells the model the call ran.
|
|
138
|
+
problems = describe(parsed.error, "result");
|
|
136
139
|
}
|
|
137
140
|
catch (err) {
|
|
138
141
|
// zod does not catch a refine or transform that throws (`new URL` on a bad link). The
|
|
139
142
|
// call ran all the same, and that is what the model must read.
|
|
143
|
+
cause = err;
|
|
140
144
|
problems = messageOf(err);
|
|
141
145
|
}
|
|
142
146
|
// The call ran, so a write may already have happened. The model decides whether to
|
|
143
147
|
// call again from this, as it does from `repairHistory`'s interrupted-call note.
|
|
144
148
|
const effect = spec.isReadOnly ? "" : " Check its effect before calling it again.";
|
|
145
|
-
throw new Error(`${spec.name} ran, but its result does not match its output schema.${effect} Problems: ${problems}
|
|
149
|
+
throw new Error(`${spec.name} ran, but its result does not match its output schema.${effect} Problems: ${problems}`, { cause });
|
|
146
150
|
};
|
|
147
151
|
}
|
|
148
152
|
// Two signatures, so only a tool with `output` can return a type `run` does not. `output` comes
|
|
@@ -153,6 +157,7 @@ function checkedRun(spec, output) {
|
|
|
153
157
|
export function zodTool(spec) {
|
|
154
158
|
const inputSchema = toJsonObjectSchema(spec.input, `Tool "${spec.name}" input`);
|
|
155
159
|
const { output } = spec;
|
|
160
|
+
const describe = spec.describeIssues ?? describeIssues;
|
|
156
161
|
function clamp(value) {
|
|
157
162
|
return spec.clampOverflow ? clampToSchema(value, inputSchema) : value;
|
|
158
163
|
}
|
|
@@ -160,15 +165,30 @@ export function zodTool(spec) {
|
|
|
160
165
|
// a decoded string, or beside a fixed wrapper, still reaches the model. The repaired
|
|
161
166
|
// value is clamped too, since a string unwrapped from `{ text }` can be the one too long.
|
|
162
167
|
function parseInput(raw) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
168
|
+
let problems;
|
|
169
|
+
let cause;
|
|
170
|
+
try {
|
|
171
|
+
const candidate = clamp(raw);
|
|
172
|
+
const first = spec.input.safeParse(candidate);
|
|
173
|
+
if (first.success)
|
|
174
|
+
return first.data;
|
|
175
|
+
const repaired = repairSlips(candidate, first.error.issues);
|
|
176
|
+
const parsed = repaired === candidate ? first : spec.input.safeParse(clamp(repaired));
|
|
177
|
+
if (parsed.success)
|
|
178
|
+
return parsed.data;
|
|
179
|
+
cause = parsed.error;
|
|
180
|
+
problems = describe(parsed.error, "input");
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
// A refine or transform that throws, or a `describeIssues` that does, is wrapped as it is
|
|
184
|
+
// on a result. Thrown bare, it was `ToolOutcome.cause` itself, so a log reading
|
|
185
|
+
// `cause.cause`, where zod's error sits, got `undefined`.
|
|
186
|
+
cause = err;
|
|
187
|
+
problems = messageOf(err);
|
|
188
|
+
}
|
|
189
|
+
// The model reads the message, which may be the app's words. What went wrong stays as the
|
|
190
|
+
// cause, and `invoke` keeps what was thrown, so logs still have all of it.
|
|
191
|
+
throw new Error(problems, { cause });
|
|
172
192
|
}
|
|
173
193
|
return defineTool({
|
|
174
194
|
name: spec.name,
|
|
@@ -186,7 +206,7 @@ export function zodTool(spec) {
|
|
|
186
206
|
...(spec.isConcurrencySafe !== undefined ? { isConcurrencySafe: spec.isConcurrencySafe } : {}),
|
|
187
207
|
...(spec.isTerminal !== undefined ? { isTerminal: spec.isTerminal } : {}),
|
|
188
208
|
validate: parseInput,
|
|
189
|
-
run: output ? checkedRun(spec, output) : spec.run,
|
|
209
|
+
run: output ? checkedRun(spec, output, describe) : spec.run,
|
|
190
210
|
});
|
|
191
211
|
}
|
|
192
212
|
//# sourceMappingURL=zod.js.map
|
package/dist/zod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.js","sourceRoot":"","sources":["../src/zod.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,EAAE;AACF,wE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AAC"}
|
package/package.json
CHANGED
package/src/decisions.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// Zen's free model explicitly reports $0.
|
|
6
6
|
import { attributionHeaders, type Attribution } from "./attribution.ts";
|
|
7
7
|
import { ProviderError, streamError } from "./errors.ts";
|
|
8
|
-
import { openRouterCostUsd } from "./providers/
|
|
8
|
+
import { openRouterCostUsd } from "./providers/openrouter-billing.ts";
|
|
9
9
|
import { withRetry, type RetryOptions } from "./retry.ts";
|
|
10
10
|
import { getJson, postJson, type FetchLike } from "./transport.ts";
|
|
11
11
|
import type { TokenUsage } from "./types.ts";
|
package/src/errors.ts
CHANGED
|
@@ -10,12 +10,7 @@
|
|
|
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 {
|
|
14
|
-
parseRateLimitReset,
|
|
15
|
-
parseRateLimitResponse,
|
|
16
|
-
parseUsageLimitBody,
|
|
17
|
-
type RateLimitWindow,
|
|
18
|
-
} from "./rate-limit.ts";
|
|
13
|
+
import { failureReset, type RateLimitReset, type RateLimitWindow } from "./rate-limit.ts";
|
|
19
14
|
import type { FilePart } from "./types.ts";
|
|
20
15
|
|
|
21
16
|
/** What kind of failure this is, named by what actually fixes it. */
|
|
@@ -176,12 +171,11 @@ export class ProviderError extends Error {
|
|
|
176
171
|
const code = readString(err, "code") ?? readString(err, "type");
|
|
177
172
|
const message = messageOf(err);
|
|
178
173
|
const body = bodyTextOf(err);
|
|
179
|
-
const
|
|
180
|
-
return new ProviderError(provider,
|
|
174
|
+
const kind = classify(err, status, body);
|
|
175
|
+
return new ProviderError(provider, kind, message, {
|
|
181
176
|
status,
|
|
182
177
|
code,
|
|
183
|
-
|
|
184
|
-
...parseUsageLimitBody(resetBody),
|
|
178
|
+
...sdkReset(err, kind),
|
|
185
179
|
body: body.slice(0, 2_000) || undefined,
|
|
186
180
|
cause: err,
|
|
187
181
|
});
|
|
@@ -390,6 +384,11 @@ const ENTITLEMENT_PATTERNS: readonly RegExp[] = [
|
|
|
390
384
|
// The ChatGPT backend's error code, which arrives with no prose at all.
|
|
391
385
|
/usage_not_included/i,
|
|
392
386
|
/usage not included/i,
|
|
387
|
+
// Z.ai's plan gate, code 1311: "[1311][当前订阅套餐暂未开放GLM-5.3-FlashX权限]", the plan does
|
|
388
|
+
// not include this model yet. It comes as a 403, so it read as a wrong key, and a key pool
|
|
389
|
+
// benched a working key for 12 hours. The code is matched with the bracket after it, as Z.ai
|
|
390
|
+
// writes it, because OpenAI's errors name one message of a long history as `messages[1311]`.
|
|
391
|
+
/\[1311\]\[|暂未开放/,
|
|
393
392
|
];
|
|
394
393
|
|
|
395
394
|
const OPENCODE_FREE_LIMIT = /\bFreeUsageLimitError\b/i;
|
|
@@ -470,6 +469,10 @@ const MODEL_PATTERNS: readonly RegExp[] = [
|
|
|
470
469
|
// requested model." On status alone that is a wrong key, but the key is fine: another model is
|
|
471
470
|
// the fix (its errors page, read 2026-11-11).
|
|
472
471
|
/does not have access to the requested model/i,
|
|
472
|
+
// Google, for a model that answers one turn only: "Multiturn chat is not enabled for
|
|
473
|
+
// models/antigravity-preview-latest". A 400, so `invalid` on status alone, and a pool stopped on
|
|
474
|
+
// it. Every turn after the first is multiturn, so the fix is another model.
|
|
475
|
+
/multiturn chat is not enabled/i,
|
|
473
476
|
];
|
|
474
477
|
|
|
475
478
|
/** An optional model id after the word "model", quoted or not: "model `gpt-4`", "Model mimo-v2.5". */
|
|
@@ -497,7 +500,13 @@ const RETIRED_MODEL_PATTERNS: readonly RegExp[] = [
|
|
|
497
500
|
|
|
498
501
|
const AUTH_PATTERNS: readonly RegExp[] = [
|
|
499
502
|
/invalid[_\s]?(?:x-api-key|api[ _-]?key|token|credentials?)/i,
|
|
500
|
-
|
|
503
|
+
// "missing" and "required" come from an adopter's own classifier, with no recorded body: a key
|
|
504
|
+
// that never arrived is fixed the same way as a wrong one.
|
|
505
|
+
/api[ _-]?key (?:is )?(?:not valid|invalid|incorrect|missing|required)/i,
|
|
506
|
+
// Google's OpenAI-compatible endpoint answers a bad key with a 400 that says only "Please pass a
|
|
507
|
+
// valid API key". On status alone that is `invalid`, so a key pool never tried its next key and
|
|
508
|
+
// a fallback chain stopped, though the key is the fix.
|
|
509
|
+
/pass a valid api[ _-]?key/i,
|
|
501
510
|
/unauthorized|UNAUTHENTICATED|PERMISSION_DENIED/i,
|
|
502
511
|
/authentication[_\s](?:failed|invalid|error)/i,
|
|
503
512
|
/account (?:disabled|suspended|deactivated|banned)/i,
|
|
@@ -651,38 +660,51 @@ export function classify(err: unknown, status?: number, body?: string): ErrorKin
|
|
|
651
660
|
return "unknown";
|
|
652
661
|
}
|
|
653
662
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
663
|
+
function normalizedWait(err: unknown): number | undefined {
|
|
664
|
+
const ms = readNumber(err, "retryAfterMs");
|
|
665
|
+
return ms !== undefined && Number.isFinite(ms) && ms >= 0 ? ms : undefined;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function sdkReset(err: unknown, kind: ErrorKind, body?: string): RateLimitReset {
|
|
669
|
+
const message = typeof err === "string" ? err : readString(err, "message");
|
|
670
|
+
let payload = body ?? providerBodyOf(err);
|
|
671
|
+
if (err instanceof ProviderError) payload ??= err.body;
|
|
672
|
+
if (payload === undefined && typeof err === "object" && !(err instanceof Error)) {
|
|
673
|
+
payload = providerBodyOf({ error: err });
|
|
674
|
+
}
|
|
675
|
+
const headers = errorHeaders(err);
|
|
676
|
+
// Some SDKs repeat the entire JSON reply after a status prefix in Error.message.
|
|
677
|
+
// Do not search that copy when the real envelope says nothing about a wait.
|
|
678
|
+
const plainMessage = message && !/[{[]/.test(message) ? message : undefined;
|
|
679
|
+
return failureReset(payload ?? message ?? "", {
|
|
680
|
+
headers,
|
|
681
|
+
accountWindow: kind === "rate" || kind === "quota" || (kind === "unknown" && !headers),
|
|
682
|
+
normalizedDelay: normalizedWait(err),
|
|
683
|
+
fallbackMessage: err instanceof ProviderError ? undefined : plainMessage,
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/** How long the provider asked us to wait, in ms. */
|
|
660
688
|
export function parseRetryAfterMs(err: unknown, body?: string): number | undefined {
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
const normalized = readNumber(err, "retryAfterMs");
|
|
664
|
-
if (normalized !== undefined && Number.isFinite(normalized) && normalized >= 0) return normalized;
|
|
665
|
-
const text = body ?? bodyTextOf(err);
|
|
666
|
-
const usageLimit = parseUsageLimitBody(text);
|
|
667
|
-
if (usageLimit.retryAfterMs !== undefined) return usageLimit.retryAfterMs;
|
|
668
|
-
const delay = text.match(/"?retryDelay"?\s*[:=]\s*"?(\d+(?:\.\d+)?)s"?/i);
|
|
669
|
-
if (delay?.[1]) return Math.round(parseFloat(delay[1]) * 1000);
|
|
689
|
+
return normalizedWait(err) ?? sdkReset(err, classify(err, undefined, body), body).retryAfterMs;
|
|
690
|
+
}
|
|
670
691
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
692
|
+
/** SDK errors and stream envelopes can carry headers as plain objects. */
|
|
693
|
+
function errorHeaders(source: unknown): Headers | undefined {
|
|
694
|
+
if (typeof source !== "object" || source === null || !("headers" in source)) return undefined;
|
|
695
|
+
const raw = source.headers;
|
|
696
|
+
if (raw instanceof Headers) return raw;
|
|
697
|
+
if (typeof raw !== "object" || raw === null) return undefined;
|
|
698
|
+
const headers = new Headers();
|
|
699
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
700
|
+
if (typeof value !== "string" && typeof value !== "number") continue;
|
|
701
|
+
try {
|
|
702
|
+
headers.set(name, String(value));
|
|
703
|
+
} catch {
|
|
704
|
+
// A malformed header must not mask the original failure or the other retry hints.
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
return headers;
|
|
686
708
|
}
|
|
687
709
|
|
|
688
710
|
export interface ContextOverflowInfo {
|
|
@@ -789,18 +811,27 @@ const STATUS_BY_TYPE: ReadonlyMap<string, number> = new Map([
|
|
|
789
811
|
* body does name keeps its own kind, which is what tells the caller to wait,
|
|
790
812
|
* rotate a key, or compact instead of retrying a failure that will repeat.
|
|
791
813
|
*/
|
|
792
|
-
export function streamError(
|
|
814
|
+
export function streamError(
|
|
815
|
+
provider: string,
|
|
816
|
+
envelope: unknown,
|
|
817
|
+
responseHeaders?: Headers,
|
|
818
|
+
): ProviderError {
|
|
793
819
|
// Only failure data is searchable or logged. A failed Responses object can
|
|
794
820
|
// echo instructions, tool definitions and output alongside its error.
|
|
795
|
-
const outer =
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
821
|
+
const outer = recordOf(envelope) ?? {};
|
|
822
|
+
const response = recordOf(outer.response);
|
|
823
|
+
let error = response?.error ?? outer.error ?? outer.errors;
|
|
824
|
+
if (error == null) {
|
|
825
|
+
error =
|
|
826
|
+
response || outer.type === "response.failed" ? { message: "response failed" } : envelope;
|
|
827
|
+
}
|
|
828
|
+
const body = failureJson(error ?? {});
|
|
799
829
|
const status = [
|
|
800
830
|
readNumber(error, "code"),
|
|
801
831
|
readNumber(error, "status"),
|
|
802
832
|
readNumber(error, "status_code"),
|
|
803
|
-
readNumber(
|
|
833
|
+
readNumber(outer, "status_code"),
|
|
834
|
+
readNumber(response, "status_code"),
|
|
804
835
|
].find((value) => value !== undefined && value >= 400 && value < 600);
|
|
805
836
|
const code =
|
|
806
837
|
readString(error, "code") ?? readString(error, "status") ?? readString(error, "type");
|
|
@@ -824,35 +855,62 @@ export function streamError(provider: string, envelope: unknown): ProviderError
|
|
|
824
855
|
{
|
|
825
856
|
...(status !== undefined ? { status } : {}),
|
|
826
857
|
...(code ? { code } : {}),
|
|
827
|
-
...readReset(
|
|
858
|
+
...readReset(outer, response, error, kind, responseHeaders),
|
|
828
859
|
body: body.slice(0, 2_000),
|
|
829
860
|
},
|
|
830
861
|
);
|
|
831
862
|
}
|
|
832
863
|
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
864
|
+
function recordOf(value: unknown): Record<string, unknown> | undefined {
|
|
865
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
866
|
+
? (value as Record<string, unknown>)
|
|
867
|
+
: undefined;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// Gateways can mirror request fields or headers even inside a flat error.
|
|
871
|
+
const ECHOED_FIELDS = new Set([
|
|
872
|
+
"headers",
|
|
873
|
+
"request",
|
|
874
|
+
"response",
|
|
875
|
+
"instructions",
|
|
876
|
+
"input",
|
|
877
|
+
"output",
|
|
878
|
+
"tools",
|
|
879
|
+
]);
|
|
880
|
+
|
|
881
|
+
function failureJson(value: unknown): string {
|
|
882
|
+
return (
|
|
883
|
+
JSON.stringify(value, (key, field: unknown) => (ECHOED_FIELDS.has(key) ? undefined : field)) ??
|
|
884
|
+
""
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/** A gateway can put retry hints beside its nested error. Never scan the whole
|
|
889
|
+
* event for them: an echoed prompt could name a different failure or delay. */
|
|
890
|
+
function readReset(
|
|
891
|
+
outer: Record<string, unknown>,
|
|
892
|
+
response: Record<string, unknown> | undefined,
|
|
893
|
+
error: unknown,
|
|
894
|
+
kind: ErrorKind,
|
|
895
|
+
responseHeaders?: Headers,
|
|
896
|
+
): RateLimitReset {
|
|
897
|
+
const headers = new Headers(errorHeaders(error));
|
|
898
|
+
for (const source of [errorHeaders(response), errorHeaders(outer), responseHeaders]) {
|
|
899
|
+
source?.forEach((value, name) => headers.set(name, value));
|
|
849
900
|
}
|
|
850
|
-
const
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
:
|
|
854
|
-
|
|
855
|
-
|
|
901
|
+
const body = failureJson({
|
|
902
|
+
error,
|
|
903
|
+
metadata: {
|
|
904
|
+
resetsAt:
|
|
905
|
+
readString(outer.metadata, "resetsAt") ?? readString(response?.metadata, "resetsAt"),
|
|
906
|
+
},
|
|
907
|
+
retry_after_seconds: outer.retry_after_seconds ?? response?.retry_after_seconds,
|
|
908
|
+
});
|
|
909
|
+
// Standing account windows describe a throttle, not an unrelated 503.
|
|
910
|
+
return failureReset(body, {
|
|
911
|
+
headers,
|
|
912
|
+
accountWindow: kind === "rate" || kind === "quota",
|
|
913
|
+
});
|
|
856
914
|
}
|
|
857
915
|
|
|
858
916
|
/**
|
package/src/hosted/anthropic.ts
CHANGED
|
@@ -393,15 +393,16 @@ export const anthropicHosted = {
|
|
|
393
393
|
}
|
|
394
394
|
}
|
|
395
395
|
return {
|
|
396
|
-
|
|
396
|
+
/** The conversation a pass sends: `body`, then what the paused passes
|
|
397
|
+
* wrote, as the turn they continue. */
|
|
398
|
+
messages(body: AnthropicMessage[]): AnthropicMessage[] {
|
|
399
|
+
return resumed.length > 0 ? resumeWith(body, resumed) : body;
|
|
400
|
+
},
|
|
401
|
+
payload(request: Record<string, unknown>) {
|
|
397
402
|
blocks = [];
|
|
398
403
|
open = undefined;
|
|
399
404
|
servers = undefined;
|
|
400
|
-
return {
|
|
401
|
-
...request,
|
|
402
|
-
...(resumed.length > 0 ? { messages: resumeWith(body, resumed) } : {}),
|
|
403
|
-
...(container ? { container } : {}),
|
|
404
|
-
};
|
|
405
|
+
return { ...request, ...(container ? { container } : {}) };
|
|
405
406
|
},
|
|
406
407
|
*event(event: AnthropicEvent): Generator<ProviderChunk> {
|
|
407
408
|
const usage = event.type === "message_start" ? event.message?.usage : event.usage;
|
package/src/index.ts
CHANGED
|
@@ -36,7 +36,14 @@ export {
|
|
|
36
36
|
type RequestInit_,
|
|
37
37
|
type SseHooks,
|
|
38
38
|
} from "./transport.ts";
|
|
39
|
-
export
|
|
39
|
+
export {
|
|
40
|
+
parseRateLimitReset,
|
|
41
|
+
parseRateLimitResponse,
|
|
42
|
+
parseUsageLimitBody,
|
|
43
|
+
type RateLimitReading,
|
|
44
|
+
type RateLimitReset,
|
|
45
|
+
type RateLimitWindow,
|
|
46
|
+
} from "./rate-limit.ts";
|
|
40
47
|
export * from "./tool-args.ts";
|
|
41
48
|
export * from "./tool-markup.ts";
|
|
42
49
|
export * from "./tools.ts";
|
|
@@ -55,11 +62,11 @@ export {
|
|
|
55
62
|
createOpenAIProvider,
|
|
56
63
|
toOpenAIMessages,
|
|
57
64
|
effortParams,
|
|
58
|
-
openRouterCostUsd,
|
|
59
65
|
openRouterHostFor,
|
|
60
66
|
type OpenAIConfig,
|
|
61
67
|
type EffortDialect,
|
|
62
68
|
} from "./providers/openai.ts";
|
|
69
|
+
export { openRouterCostUsd } from "./providers/openrouter-billing.ts";
|
|
63
70
|
export {
|
|
64
71
|
createResponsesProvider,
|
|
65
72
|
toResponsesInput,
|