@providerkit/core 0.1.0 → 0.3.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/README.md +35 -158
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +8 -0
- package/dist/context.js.map +1 -1
- package/dist/errors.d.ts +36 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +75 -2
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/key-pool.d.ts +60 -0
- package/dist/key-pool.d.ts.map +1 -0
- package/dist/key-pool.js +235 -0
- package/dist/key-pool.js.map +1 -0
- package/dist/providers/anthropic.d.ts +10 -6
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +30 -14
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/gemini.d.ts +40 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +303 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/openai.d.ts +38 -1
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +122 -16
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/responses.d.ts +38 -0
- package/dist/providers/responses.d.ts.map +1 -0
- package/dist/providers/responses.js +341 -0
- package/dist/providers/responses.js.map +1 -0
- package/dist/rate-limit.d.ts +29 -0
- package/dist/rate-limit.d.ts.map +1 -0
- package/dist/rate-limit.js +194 -0
- package/dist/rate-limit.js.map +1 -0
- package/dist/transport.d.ts +18 -5
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +61 -35
- package/dist/transport.js.map +1 -1
- package/dist/types.d.ts +13 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +7 -2
- package/dist/types.js.map +1 -1
- package/dist/zod.d.ts +8 -1
- package/dist/zod.d.ts.map +1 -1
- package/dist/zod.js +9 -2
- package/dist/zod.js.map +1 -1
- package/package.json +2 -2
- package/src/context.ts +7 -0
- package/src/errors.ts +82 -2
- package/src/index.ts +4 -0
- package/src/key-pool.ts +272 -0
- package/src/providers/anthropic.ts +41 -20
- package/src/providers/gemini.ts +386 -0
- package/src/providers/openai.ts +153 -16
- package/src/providers/responses.ts +455 -0
- package/src/rate-limit.ts +217 -0
- package/src/transport.ts +61 -35
- package/src/types.ts +19 -2
- package/src/zod.ts +12 -2
package/README.md
CHANGED
|
@@ -40,171 +40,43 @@ loop is where your product lives; it should stay yours. This is everything under
|
|
|
40
40
|
## Use
|
|
41
41
|
|
|
42
42
|
```ts
|
|
43
|
-
import { createAnthropicProvider,
|
|
43
|
+
import { createAnthropicProvider, ProviderError, isTransient } from "@providerkit/core";
|
|
44
44
|
|
|
45
45
|
const provider = createAnthropicProvider({
|
|
46
46
|
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
47
47
|
model: "claude-sonnet-5",
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
for await (const chunk of provider.createStream(messages, tools, { effort: "medium" })) {
|
|
51
|
-
if (chunk.content) process.stdout.write(chunk.content);
|
|
52
|
-
if (chunk.usage) console.log(chunk.usage); // one meaning across every provider
|
|
53
|
-
}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
`createOpenAIProvider` speaks the dialect most gateways do, so it serves OpenAI, OpenRouter,
|
|
57
|
-
DeepSeek, GLM, Kimi, Groq, Together, vLLM, Ollama and LM Studio:
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
const provider = createOpenAIProvider({
|
|
61
|
-
apiKey: process.env.OPENROUTER_API_KEY!,
|
|
62
|
-
baseUrl: "https://openrouter.ai/api",
|
|
63
|
-
id: "openrouter",
|
|
64
|
-
model: "deepseek/deepseek-v4-pro",
|
|
65
|
-
// Keep the prompt cache warm: it lives on the upstream host's account, and
|
|
66
|
-
// OpenRouter's default routing hops hosts between rounds — every hop is a
|
|
67
|
-
// cold cache, in both latency and effective input cost.
|
|
68
|
-
providerOrder: ["deepinfra", "fireworks"],
|
|
69
|
-
});
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Failures, named by what fixes them
|
|
73
|
-
|
|
74
|
-
```ts
|
|
75
|
-
import { ProviderError, isTransient, isBackupEligible } from "@providerkit/core";
|
|
76
|
-
|
|
77
50
|
try {
|
|
78
|
-
|
|
51
|
+
for await (const chunk of provider.createStream(messages, tools, { effort: "medium" })) {
|
|
52
|
+
if (chunk.content) write(chunk.content);
|
|
53
|
+
if (chunk.usage) record(chunk.usage); // one usage shape across every provider
|
|
54
|
+
}
|
|
79
55
|
} catch (raw) {
|
|
80
56
|
const err = ProviderError.from("anthropic", raw);
|
|
81
|
-
|
|
82
|
-
err.kind
|
|
83
|
-
err
|
|
84
|
-
err.body; // the provider's actual words, never "400 status code (no body)"
|
|
85
|
-
|
|
86
|
-
if (err.kind === "context") compactAndRetry();
|
|
87
|
-
else if (isBackupEligible(err.kind)) tryAnotherModel();
|
|
88
|
-
else if (isTransient(err.kind)) retry();
|
|
89
|
-
else surface(err);
|
|
57
|
+
if (err.kind === "context") return compactAndRetry();
|
|
58
|
+
if (isTransient(err.kind)) return retry(err.retryAfterMs);
|
|
59
|
+
surface(err); // .body has their actual words
|
|
90
60
|
}
|
|
91
61
|
```
|
|
92
62
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
import { withStreamRetry, streamWithBackupModels } from "@providerkit/core";
|
|
63
|
+
**Full documentation lives at [providerkit.dev](https://providerkit.dev)** — it is the single
|
|
64
|
+
source of truth for usage, and this README deliberately stays a front door so the two cannot
|
|
65
|
+
drift.
|
|
97
66
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
67
|
+
| Guide | What it covers |
|
|
68
|
+
| ----------------------------------------------------------------------- | ------------------------------------------------ |
|
|
69
|
+
| [Getting started](https://providerkit.dev/guides/getting-started/) | Install, first stream, swapping vendors |
|
|
70
|
+
| [The provider seam](https://providerkit.dev/guides/providers/) | Message, chunk and tool shapes |
|
|
71
|
+
| [Errors](https://providerkit.dev/guides/errors/) | The thirteen kinds, and why status is not enough |
|
|
72
|
+
| [Retries and fallback](https://providerkit.dev/guides/retries/) | Backoff, the commitment rule, backup models |
|
|
73
|
+
| [Streaming and the watchdog](https://providerkit.dev/guides/streaming/) | The stream that stops sending; TTFT |
|
|
74
|
+
| [Tools](https://providerkit.dev/guides/tools/) | Tool kernel, truncated-argument salvage, zod |
|
|
75
|
+
| [Context and compaction](https://providerkit.dev/guides/context/) | When to compact, and where to cut |
|
|
76
|
+
| [Usage and cost](https://providerkit.dev/guides/usage/) | Reconciling cache tokens, cost, savings |
|
|
101
77
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const withFallback = streamWithBackupModels((model) => run(model), {
|
|
105
|
-
models: ["claude-opus-5", "claude-sonnet-5"],
|
|
106
|
-
});
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### The watchdog
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
import { streamWatch, watchChunks } from "@providerkit/core";
|
|
113
|
-
|
|
114
|
-
const watch = streamWatch({ provider: "openai", signal: userSignal });
|
|
115
|
-
for await (const chunk of watchChunks(
|
|
116
|
-
watch,
|
|
117
|
-
provider.createStream(messages, tools, {
|
|
118
|
-
signal: watch.signal,
|
|
119
|
-
}),
|
|
120
|
-
)) {
|
|
121
|
-
// any byte re-arms the deadline
|
|
122
|
-
}
|
|
123
|
-
watch.firstChunkMs(); // TTFT — the number a prompt-cache pin exists to shrink
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
A stream that goes 60 seconds without a byte is aborted and surfaced as a transient
|
|
127
|
-
`timeout`. The watchdog aborts its _own_ controller and only bridges the caller's, so your
|
|
128
|
-
user's Stop stays distinguishable from our deadline: one is never retried, the other always
|
|
129
|
-
is.
|
|
130
|
-
|
|
131
|
-
### Tools
|
|
132
|
-
|
|
133
|
-
JSON Schema by default, so the core needs no zod:
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
import { defineTool, ToolRegistry } from "@providerkit/core";
|
|
137
|
-
|
|
138
|
-
const search = defineTool({
|
|
139
|
-
name: "search",
|
|
140
|
-
description: "Search the corpus",
|
|
141
|
-
inputSchema: { type: "object", properties: { q: { type: "string" } }, required: ["q"] },
|
|
142
|
-
run: async ({ q }, ctx) => findAll(q, { signal: ctx.signal }),
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const outcome = await search.invoke(rawArgsFromModel);
|
|
146
|
-
// { ok: false, kind: "invalid_input" | "timeout" | "aborted" | "failed", error }
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
A failing tool is **data**, not an exception — the loop feeds `error` back so the model can
|
|
150
|
-
correct itself. Only a caller's abort escapes.
|
|
151
|
-
|
|
152
|
-
With zod, from the optional entry point:
|
|
153
|
-
|
|
154
|
-
```ts
|
|
155
|
-
import { zodTool } from "@providerkit/core/zod";
|
|
156
|
-
|
|
157
|
-
const submit = zodTool({
|
|
158
|
-
name: "submit",
|
|
159
|
-
description: "Submit the final answer",
|
|
160
|
-
isTerminal: true,
|
|
161
|
-
clampOverflow: true, // a terminal tool gets no second chance
|
|
162
|
-
input: z.object({ summary: z.string().max(2000) }),
|
|
163
|
-
run: async (input) => input,
|
|
164
|
-
});
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
### Truncated tool calls
|
|
168
|
-
|
|
169
|
-
```ts
|
|
170
|
-
import { parseToolArgs, isCompleteJson } from "@providerkit/core";
|
|
171
|
-
|
|
172
|
-
// Never throws. A turn cut off mid-argument keeps every field that closed
|
|
173
|
-
// before the cut, plus the half-written one the cut landed in.
|
|
174
|
-
const args = parseToolArgs(rawArgumentString);
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### Compaction
|
|
178
|
-
|
|
179
|
-
The decisions, not the prompt — the summary is yours to write:
|
|
180
|
-
|
|
181
|
-
```ts
|
|
182
|
-
import { needsCompaction, pickCut, applyCompaction, historyBudgetTokens } from "@providerkit/core";
|
|
183
|
-
|
|
184
|
-
if (needsCompaction(lastInputTokens, contextWindow)) {
|
|
185
|
-
const cut = pickCut(messages, historyBudgetTokens(contextWindow));
|
|
186
|
-
messages = applyCompaction(messages, cut, await summarize(messages.slice(0, cut)));
|
|
187
|
-
}
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
`pickCut` never splits a tool call from its result (every provider rejects the orphan with a
|
|
191
|
-
400 — the failure compaction was called to avoid), never cuts into the system prompt, and
|
|
192
|
-
always keeps the newest message.
|
|
193
|
-
|
|
194
|
-
### Cost
|
|
195
|
-
|
|
196
|
-
The arithmetic, not the rates. A price table is volatile data about a catalogue that differs
|
|
197
|
-
per application, and a wrong number shipped in a library is a wrong number in everyone's
|
|
198
|
-
ledger — so you keep the numbers, verifiable line by line against a vendor's price sheet:
|
|
199
|
-
|
|
200
|
-
```ts
|
|
201
|
-
import { UsageTracker } from "@providerkit/core";
|
|
202
|
-
|
|
203
|
-
const tracker = new UsageTracker();
|
|
204
|
-
tracker.add(usage, { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 }); // USD per Mtok
|
|
205
|
-
tracker.costUsd;
|
|
206
|
-
tracker.cacheSavingsUsd; // what the cache saved, for the "is caching even working" question
|
|
207
|
-
```
|
|
78
|
+
The [API reference](https://providerkit.dev/reference/) is generated from the source, so it
|
|
79
|
+
cannot drift either.
|
|
208
80
|
|
|
209
81
|
## Origin
|
|
210
82
|
|
|
@@ -224,8 +96,9 @@ ever saw. That is the part worth having.
|
|
|
224
96
|
## Repo
|
|
225
97
|
|
|
226
98
|
```
|
|
227
|
-
core/
|
|
228
|
-
site/
|
|
99
|
+
core/ the npm package `@providerkit/core`
|
|
100
|
+
site/ providerkit.dev — Astro + Starlight, also open source
|
|
101
|
+
brand/ the mark, the OG card, and the generator for both
|
|
229
102
|
```
|
|
230
103
|
|
|
231
104
|
`bun install`, then `cd core && bun run test`. See [AGENTS.md](./AGENTS.md) for the layout,
|
|
@@ -233,12 +106,16 @@ the invariants worth not regressing, and what is left to build.
|
|
|
233
106
|
|
|
234
107
|
## Status
|
|
235
108
|
|
|
236
|
-
Working and tested (
|
|
109
|
+
Working and tested (380 tests): the seam, the error classifier, retry and backup-model
|
|
237
110
|
fallback, the idle watchdog, cost math, the fetch/SSE transport, tool-argument salvage, the
|
|
238
|
-
Anthropic
|
|
239
|
-
decisions.
|
|
240
|
-
|
|
241
|
-
|
|
111
|
+
Anthropic, OpenAI-shape, Responses and Gemini adapters, the multi-key rotation pool,
|
|
112
|
+
rate-limit reset windows, the tool kernel, schema clamping, and the compaction decisions.
|
|
113
|
+
|
|
114
|
+
That is the whole extraction, and the claim it rests on is that an adopting codebase gets
|
|
115
|
+
**smaller**. The first migration — a Chrome MV3 extension, the hardest runtime of the five and
|
|
116
|
+
the one with no Node, no bundler escape hatch and no zod — took **667 lines out and put 215
|
|
117
|
+
back**, and kept every one of its six gates green. If a repo grows on adopting this, the
|
|
118
|
+
boundary was drawn in the wrong place.
|
|
242
119
|
|
|
243
120
|
## License
|
|
244
121
|
|
package/dist/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAWD;sCACsC;AACtC,wBAAgB,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAO1D;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAE3E;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAWD;sCACsC;AACtC,wBAAgB,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAO1D;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAE3E;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CASnF;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CA0BhF;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,SAAS,WAAW,EAAE,EAChC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,GACd,WAAW,EAAE,CAQf;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMxD"}
|
package/dist/context.js
CHANGED
|
@@ -43,6 +43,14 @@ export function conversationTokens(messages) {
|
|
|
43
43
|
* that is not a guess. Estimate only when there is no reported count yet.
|
|
44
44
|
*/
|
|
45
45
|
export function needsCompaction(inputTokens, contextWindow) {
|
|
46
|
+
// A window at or below the reserve puts the threshold at zero or less, and
|
|
47
|
+
// every turn — including one that has not reported usage yet — then reads as
|
|
48
|
+
// already full. That loops: folding a history this side of its first answer
|
|
49
|
+
// changes nothing, so the next check says the same thing. Small windows are
|
|
50
|
+
// not hypothetical; a caller that learns a real ceiling from a rejection
|
|
51
|
+
// (rather than guessing one) can legitimately arrive here with 8k.
|
|
52
|
+
if (inputTokens <= 0)
|
|
53
|
+
return false;
|
|
46
54
|
return inputTokens >= contextWindow - CONTEXT_RESERVE_TOKENS;
|
|
47
55
|
}
|
|
48
56
|
/**
|
package/dist/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,SAAS,MAAM,CAAC,OAAoB;IAClC,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QACnE,sEAAsE;QACtE,qDAAqD;QACrD,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACzF,CAAC;IACD,OAAO,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;AACpE,CAAC;AAED;sCACsC;AACtC,MAAM,UAAU,aAAa,CAAC,OAAoB;IAChD,MAAM,KAAK,GACT,OAAO,CAAC,IAAI,KAAK,WAAW;QAC1B,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,I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AC,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC;IAChE,IAAI,2CAA2C,CAAC,IAAI,CAAC,EAAE,CAAC;QAAE,OAAO,OAAO,CAAC;IACzE,IAAI,0BAA0B,CAAC,IAAI,CAAC,EAAE,CAAC;QAAE,OAAO,OAAO,CAAC;IACxD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/errors.d.ts
CHANGED
|
@@ -72,6 +72,18 @@ export declare function isTransportFailure(err: unknown): boolean;
|
|
|
72
72
|
* entitlement beats quota beats auth — each earlier category's fix is useless
|
|
73
73
|
* for the later ones.
|
|
74
74
|
*/
|
|
75
|
+
/**
|
|
76
|
+
* The kind of a failure that arrived as an HTTP response — a status and a body,
|
|
77
|
+
* with nothing thrown.
|
|
78
|
+
*
|
|
79
|
+
* `classify` below is for a caught error, and it reads `status` and the body
|
|
80
|
+
* text off that error when they are not passed separately. A response has no
|
|
81
|
+
* error object, so callers were inventing one to fill the slot: a bare body
|
|
82
|
+
* string, the same text twice, a `{ status, error }` literal. All three are
|
|
83
|
+
* inert — nothing on them can satisfy `isAbort` or `isTransportFailure` — so
|
|
84
|
+
* they were three spellings of `undefined`.
|
|
85
|
+
*/
|
|
86
|
+
export declare function classifyHttp(status: number | undefined, body: string): ErrorKind;
|
|
75
87
|
export declare function classify(err: unknown, status?: number, body?: string): ErrorKind;
|
|
76
88
|
/**
|
|
77
89
|
* How long the provider asked us to wait, in ms. Two dialects: Gemini's
|
|
@@ -80,6 +92,30 @@ export declare function classify(err: unknown, status?: number, body?: string):
|
|
|
80
92
|
* backoff shorter than the window just burns an attempt.
|
|
81
93
|
*/
|
|
82
94
|
export declare function parseRetryAfterMs(err: unknown, body?: string): number | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* A failure the backend reports INSIDE an already-200 stream.
|
|
97
|
+
*
|
|
98
|
+
* The transport classified everything that failed before the body opened; this
|
|
99
|
+
* is the rest, and every streaming shape has the same hole. An SSE response
|
|
100
|
+
* commits to 200 the moment its headers go out, so a throttle, an overload, or
|
|
101
|
+
* a prompt found too long after the fact arrives as a payload in the body
|
|
102
|
+
* rather than as a status line. Left unread it matches no branch an adapter
|
|
103
|
+
* models, the loop skips it, and the turn ends as a successful zero-token
|
|
104
|
+
* completion: retry sees nothing to retry, and a key pool never rotates off an
|
|
105
|
+
* exhausted key.
|
|
106
|
+
*
|
|
107
|
+
* `error` is the vendor's own payload, whatever shape it came in — the numeric
|
|
108
|
+
* HTTP `code` Gemini and OpenRouter use, the slug OpenAI and Anthropic put in
|
|
109
|
+
* `code`/`type`, the canonical `status` name. The whole thing serializes into
|
|
110
|
+
* the searchable body, so RetryInfo's `retryDelay` is honoured wherever it sits.
|
|
111
|
+
*
|
|
112
|
+
* `unknown` is floored to "overload" rather than kept: a stream that dies after
|
|
113
|
+
* its headers is by construction a transient upstream fault, and "unknown" is
|
|
114
|
+
* never retried. Everything the body DOES name keeps its own kind — which is
|
|
115
|
+
* what tells the caller to wait, rotate a key, or compact instead of retrying
|
|
116
|
+
* a failure that will repeat.
|
|
117
|
+
*/
|
|
118
|
+
export declare function streamError(provider: string, error: unknown): ProviderError;
|
|
83
119
|
/** The loggable surface of a failure — so a dead run never reads
|
|
84
120
|
* "400 status code (no body)". */
|
|
85
121
|
export declare function describeProviderError(err: unknown): Record<string, unknown>;
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAYA,qEAAqE;AACrE,MAAM,MAAM,SAAS;AACnB,+DAA+D;AAC7D,SAAS;AACX,sDAAsD;GACpD,SAAS;AACX,8EAA8E;GAC5E,SAAS;AACX;sCACsC;GACpC,UAAU;AACZ,yEAAyE;GACvE,MAAM;AACR,0EAA0E;GACxE,OAAO;AACT,iFAAiF;GAC/E,aAAa;AACf,mDAAmD;GACjD,MAAM;AACR,yDAAyD;GACvD,OAAO;AACT,sEAAsE;GACpE,SAAS;AACX,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EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,aAAa;CAc3D;AAgBD,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAU9C;AAwDD;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAuBxD;AAmGD;;;;;;GAMG;AACH;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,CAEhF;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAmChF;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAkBjF;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,aAAa,CAiB3E;AAED;mCACmC;AACnC,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAa3E"}
|
package/dist/errors.js
CHANGED
|
@@ -174,6 +174,13 @@ export function isTransportFailure(err) {
|
|
|
174
174
|
return true;
|
|
175
175
|
if (code !== undefined && TRANSPORT_CODES.has(code))
|
|
176
176
|
return true;
|
|
177
|
+
// undici — Node's fetch, and Bun's — parks the real reason in `code` while
|
|
178
|
+
// the thrown error says only "terminated", which is the most common way a
|
|
179
|
+
// stream dies mid-body. Its whole family shares one prefix; enumerating
|
|
180
|
+
// them ages badly, the prefix does not. UND_ERR_ABORTED is the caller's
|
|
181
|
+
// Stop wearing the same prefix, and is never ours to retry.
|
|
182
|
+
if (code?.startsWith("UND_ERR_") && code !== "UND_ERR_ABORTED")
|
|
183
|
+
return true;
|
|
177
184
|
// A bare TypeError is also what a real bug throws ("x is not a function"),
|
|
178
185
|
// so the MESSAGE is checked, not just the type — misfiling one of those
|
|
179
186
|
// would retry a genuine bug three times and hide it.
|
|
@@ -224,7 +231,6 @@ const QUOTA_PATTERNS = [
|
|
|
224
231
|
/upgrade your plan/i,
|
|
225
232
|
/quota\b[^.]{0,40}\b(?:exhausted|exceeded|will be refreshed)/i,
|
|
226
233
|
/balance (?:is )?(?:too low|not enough|insufficient)/i,
|
|
227
|
-
/per\s*day|PerDay|insufficient_quota|billing/i,
|
|
228
234
|
/余额不足/,
|
|
229
235
|
/欠费/,
|
|
230
236
|
/额度(?:不足|已用完)/,
|
|
@@ -246,6 +252,20 @@ const CONTENT_PATTERNS = [
|
|
|
246
252
|
/content policy/i,
|
|
247
253
|
/safety|PROHIBITED_CONTENT|blocked|refusal/i,
|
|
248
254
|
];
|
|
255
|
+
/**
|
|
256
|
+
* A throttle said in words rather than in a 429.
|
|
257
|
+
*
|
|
258
|
+
* Every other kind has body evidence; rate had only the status, which is
|
|
259
|
+
* exactly the evidence an in-band failure lacks — an SSE response is already
|
|
260
|
+
* 200 when the throttle lands, so the reason arrives as a payload with no
|
|
261
|
+
* status line at all. Checked below the status branches, so it only ever
|
|
262
|
+
* catches what would otherwise fall through to "unknown".
|
|
263
|
+
*/
|
|
264
|
+
const RATE_PATTERNS = [
|
|
265
|
+
/rate[_\s-]?limit/i,
|
|
266
|
+
/too many requests/i,
|
|
267
|
+
/RESOURCE_EXHAUSTED/,
|
|
268
|
+
];
|
|
249
269
|
/** Theirs and temporary, said in words rather than a status. Gemini reports
|
|
250
270
|
* UNAVAILABLE/INTERNAL in the body; gateways say "capacity". */
|
|
251
271
|
const OVERLOAD_PATTERNS = [
|
|
@@ -263,6 +283,20 @@ const matches = (patterns, text) => patterns.some((pattern) => pattern.test(text
|
|
|
263
283
|
* entitlement beats quota beats auth — each earlier category's fix is useless
|
|
264
284
|
* for the later ones.
|
|
265
285
|
*/
|
|
286
|
+
/**
|
|
287
|
+
* The kind of a failure that arrived as an HTTP response — a status and a body,
|
|
288
|
+
* with nothing thrown.
|
|
289
|
+
*
|
|
290
|
+
* `classify` below is for a caught error, and it reads `status` and the body
|
|
291
|
+
* text off that error when they are not passed separately. A response has no
|
|
292
|
+
* error object, so callers were inventing one to fill the slot: a bare body
|
|
293
|
+
* string, the same text twice, a `{ status, error }` literal. All three are
|
|
294
|
+
* inert — nothing on them can satisfy `isAbort` or `isTransportFailure` — so
|
|
295
|
+
* they were three spellings of `undefined`.
|
|
296
|
+
*/
|
|
297
|
+
export function classifyHttp(status, body) {
|
|
298
|
+
return classify(undefined, status, body);
|
|
299
|
+
}
|
|
266
300
|
export function classify(err, status, body) {
|
|
267
301
|
if (isAbort(err))
|
|
268
302
|
return "aborted";
|
|
@@ -298,10 +332,14 @@ export function classify(err, status, body) {
|
|
|
298
332
|
return "content";
|
|
299
333
|
// 529 is Anthropic's own "overloaded". 409 is how several gateways say
|
|
300
334
|
// "the model is still loading" — both are worth another attempt.
|
|
301
|
-
|
|
335
|
+
// 425 Too Early: the upstream is asking us to replay, not telling us we were
|
|
336
|
+
// wrong. Left as a plain 4xx it reads as `invalid` and is never retried.
|
|
337
|
+
if (code === 529 || code === 409 || code === 425)
|
|
302
338
|
return "overload";
|
|
303
339
|
if (code !== undefined && code >= 500)
|
|
304
340
|
return "overload";
|
|
341
|
+
if (matches(RATE_PATTERNS, text))
|
|
342
|
+
return "rate";
|
|
305
343
|
if (matches(OVERLOAD_PATTERNS, text))
|
|
306
344
|
return "overload";
|
|
307
345
|
if (/timed out|timeout/i.test(text))
|
|
@@ -337,6 +375,41 @@ export function parseRetryAfterMs(err, body) {
|
|
|
337
375
|
const date = Date.parse(value);
|
|
338
376
|
return Number.isNaN(date) ? undefined : Math.max(0, date - Date.now());
|
|
339
377
|
}
|
|
378
|
+
/**
|
|
379
|
+
* A failure the backend reports INSIDE an already-200 stream.
|
|
380
|
+
*
|
|
381
|
+
* The transport classified everything that failed before the body opened; this
|
|
382
|
+
* is the rest, and every streaming shape has the same hole. An SSE response
|
|
383
|
+
* commits to 200 the moment its headers go out, so a throttle, an overload, or
|
|
384
|
+
* a prompt found too long after the fact arrives as a payload in the body
|
|
385
|
+
* rather than as a status line. Left unread it matches no branch an adapter
|
|
386
|
+
* models, the loop skips it, and the turn ends as a successful zero-token
|
|
387
|
+
* completion: retry sees nothing to retry, and a key pool never rotates off an
|
|
388
|
+
* exhausted key.
|
|
389
|
+
*
|
|
390
|
+
* `error` is the vendor's own payload, whatever shape it came in — the numeric
|
|
391
|
+
* HTTP `code` Gemini and OpenRouter use, the slug OpenAI and Anthropic put in
|
|
392
|
+
* `code`/`type`, the canonical `status` name. The whole thing serializes into
|
|
393
|
+
* the searchable body, so RetryInfo's `retryDelay` is honoured wherever it sits.
|
|
394
|
+
*
|
|
395
|
+
* `unknown` is floored to "overload" rather than kept: a stream that dies after
|
|
396
|
+
* its headers is by construction a transient upstream fault, and "unknown" is
|
|
397
|
+
* never retried. Everything the body DOES name keeps its own kind — which is
|
|
398
|
+
* what tells the caller to wait, rotate a key, or compact instead of retrying
|
|
399
|
+
* a failure that will repeat.
|
|
400
|
+
*/
|
|
401
|
+
export function streamError(provider, error) {
|
|
402
|
+
const body = JSON.stringify(error ?? {}) ?? "";
|
|
403
|
+
const status = readNumber(error, "code") ?? readNumber(error, "status");
|
|
404
|
+
const code = readString(error, "code") ?? readString(error, "status") ?? readString(error, "type");
|
|
405
|
+
const kind = classifyHttp(status, body);
|
|
406
|
+
return new ProviderError(provider, kind === "unknown" ? "overload" : kind, `${provider} stream error: ${readString(error, "message") ?? code ?? "no reason given"}`, {
|
|
407
|
+
...(status !== undefined ? { status } : {}),
|
|
408
|
+
...(code ? { code } : {}),
|
|
409
|
+
retryAfterMs: parseRetryAfterMs(error, body),
|
|
410
|
+
body: body.slice(0, 2_000),
|
|
411
|
+
});
|
|
412
|
+
}
|
|
340
413
|
/** The loggable surface of a failure — so a dead run never reads
|
|
341
414
|
* "400 status code (no body)". */
|
|
342
415
|
export function describeProviderError(err) {
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC/E,4CAA4C;AAC5C,EAAE;AACF,8EAA8E;AAC9E,2EAA2E;AAC3E,+EAA+E;AAC/E,+EAA+E;AAC/E,2EAA2E;AA+B3E,qDAAqD;AACrD,MAAM,SAAS,GAA2B,IAAI,GAAG,CAAY;IAC3D,SAAS;IACT,SAAS;IACT,UAAU;IACV,MAAM;CACP,CAAC,CAAC;AAEH;qEACqE;AACrE,MAAM,eAAe,GAA2B,IAAI,GAAG,CAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;AAEzF,MAAM,UAAU,WAAW,CAAC,IAAe;IACzC,OAAO,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAe;IAC9C,OAAO,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC;AAED,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,QAAQ,CAAS;IACjB,IAAI,CAAY;IAChB,MAAM,CAAU;IAChB,IAAI,CAAU;IACvB;0CACsC;IAC7B,YAAY,CAAU;IAC/B;6DACyD;IAChD,IAAI,CAAU;IAEvB,YACE,QAAgB,EAChB,IAAe,EACf,OAAe,EACf,OAMI,EAAE;QAEN,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAA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AE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC;KAC3B,CACF,CAAC;AACJ,CAAC;AAED;mCACmC;AACnC,MAAM,UAAU,qBAAqB,CAAC,GAAY;IAChD,IAAI,GAAG,YAAY,aAAa,EAAE,CAAC;QACjC,OAAO;YACL,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,YAAY,EAAE,GAAG,CAAC,YAAY;YAC9B,KAAK,EAAE,GAAG,CAAC,OAAO;YAClB,IAAI,EAAE,GAAG,CAAC,IAAI;SACf,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,SAAS,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;AACxD,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
export * from "./types.ts";
|
|
2
2
|
export * from "./errors.ts";
|
|
3
3
|
export * from "./retry.ts";
|
|
4
|
+
export * from "./key-pool.ts";
|
|
4
5
|
export * from "./watchdog.ts";
|
|
5
6
|
export * from "./usage.ts";
|
|
6
7
|
export * from "./transport.ts";
|
|
8
|
+
export * from "./rate-limit.ts";
|
|
7
9
|
export * from "./tool-args.ts";
|
|
8
10
|
export * from "./tools.ts";
|
|
9
11
|
export * from "./schema.ts";
|
|
10
12
|
export * from "./context.ts";
|
|
11
13
|
export * from "./providers/anthropic.ts";
|
|
12
14
|
export * from "./providers/openai.ts";
|
|
15
|
+
export * from "./providers/responses.ts";
|
|
16
|
+
export * from "./providers/gemini.ts";
|
|
13
17
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
export * from "./types.js";
|
|
2
2
|
export * from "./errors.js";
|
|
3
3
|
export * from "./retry.js";
|
|
4
|
+
export * from "./key-pool.js";
|
|
4
5
|
export * from "./watchdog.js";
|
|
5
6
|
export * from "./usage.js";
|
|
6
7
|
export * from "./transport.js";
|
|
8
|
+
export * from "./rate-limit.js";
|
|
7
9
|
export * from "./tool-args.js";
|
|
8
10
|
export * from "./tools.js";
|
|
9
11
|
export * from "./schema.js";
|
|
10
12
|
export * from "./context.js";
|
|
11
13
|
export * from "./providers/anthropic.js";
|
|
12
14
|
export * from "./providers/openai.js";
|
|
15
|
+
export * from "./providers/responses.js";
|
|
16
|
+
export * from "./providers/gemini.js";
|
|
13
17
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ErrorKind } from "./errors.ts";
|
|
2
|
+
import type { Provider } from "./types.ts";
|
|
3
|
+
export type KeyTier = "free" | "paid";
|
|
4
|
+
export interface KeyPoolOptions {
|
|
5
|
+
/** Free-tier keys, walked round-robin before the paid one. */
|
|
6
|
+
keys: readonly string[];
|
|
7
|
+
paidKey?: string | null;
|
|
8
|
+
/** Fires whenever a key is benched — the pool's only report, in place of a
|
|
9
|
+
* logger a zero-dependency package has no business owning. */
|
|
10
|
+
onEvict?: (info: {
|
|
11
|
+
tier: KeyTier;
|
|
12
|
+
kind: ErrorKind;
|
|
13
|
+
forMs: number;
|
|
14
|
+
}) => void;
|
|
15
|
+
/** Injected in tests, so an expiry can be exercised without waiting out a
|
|
16
|
+
* 12-hour cooldown. */
|
|
17
|
+
now?: () => number;
|
|
18
|
+
}
|
|
19
|
+
/** No key can serve right now. `retryAtMs` = when the soonest one is back. */
|
|
20
|
+
export declare class NoAvailableKeyError extends Error {
|
|
21
|
+
readonly retryAtMs: number;
|
|
22
|
+
constructor(label: string, retryAtMs: number);
|
|
23
|
+
}
|
|
24
|
+
export declare class KeyPool {
|
|
25
|
+
private readonly label;
|
|
26
|
+
private readonly free;
|
|
27
|
+
private readonly paid;
|
|
28
|
+
private readonly onEvict;
|
|
29
|
+
private readonly now;
|
|
30
|
+
private cursor;
|
|
31
|
+
constructor(label: string, opts: KeyPoolOptions);
|
|
32
|
+
get size(): number;
|
|
33
|
+
/**
|
|
34
|
+
* Run `fn` with the next available key, rotating on key-specific and
|
|
35
|
+
* transient failures. Everything else (a bad request, a content block) throws
|
|
36
|
+
* straight through — it would fail identically on every key, and spending the
|
|
37
|
+
* pool on it only turns one bad request into an outage.
|
|
38
|
+
*/
|
|
39
|
+
with<T>(fn: (apiKey: string, tier: KeyTier) => Promise<T>): Promise<T>;
|
|
40
|
+
/** Free keys round-robin from a moving cursor, then the paid key. */
|
|
41
|
+
private candidates;
|
|
42
|
+
private nextAvailableAt;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Give any provider a rotating pool of keys.
|
|
46
|
+
*
|
|
47
|
+
* The subtlety is the seam's shape. `createStream` is an async generator, so
|
|
48
|
+
* CALLING it performs no I/O: the POST — and the 429 that should rotate the key
|
|
49
|
+
* — happens on the first `next()`, long after a `pool.with` wrapped around the
|
|
50
|
+
* call itself would have returned, with nothing left to rotate. So the first
|
|
51
|
+
* chunk is pulled INSIDE the pool and only the rest of the stream is consumed
|
|
52
|
+
* outside it.
|
|
53
|
+
*
|
|
54
|
+
* That line is also the honest one. Past the first chunk the answer is
|
|
55
|
+
* committed to one key, exactly as a retry is committed past its first chunk
|
|
56
|
+
* (retry.ts, rule 2): a mid-stream 429 evicts nothing and rotates nothing,
|
|
57
|
+
* because there is no way to resume a half-rendered answer on another key.
|
|
58
|
+
*/
|
|
59
|
+
export declare function withKeyPool(pool: KeyPool, factory: (apiKey: string) => Provider): Provider;
|
|
60
|
+
//# sourceMappingURL=key-pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-pool.d.ts","sourceRoot":"","sources":["../src/key-pool.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EAEV,QAAQ,EAIT,MAAM,YAAY,CAAC;AAapB,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC;AAStC,MAAM,WAAW,cAAc;IAC7B,8DAA8D;IAC9D,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB;mEAC+D;IAC/D,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,IAAI,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,SAAS,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAC5E;4BACwB;IACxB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,8EAA8E;AAC9E,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;gBAEf,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;CAK7C;AAED,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,C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