@tobi-or-not/quorum 0.4.0 → 0.6.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 +3 -1
- package/dist/bench/fingerprint.d.ts +8 -5
- package/dist/bench/fingerprint.js +28 -9
- package/dist/bench/records.d.ts +168 -0
- package/dist/bench/records.js +106 -1
- package/dist/bench/runner.d.ts +18 -0
- package/dist/bench/runner.js +30 -4
- package/dist/cli/bench.d.ts +11 -4
- package/dist/cli/bench.js +34 -17
- package/dist/cli/bin.js +2 -2
- package/dist/cli/io.d.ts +5 -0
- package/dist/cli/main.d.ts +67 -24
- package/dist/cli/main.js +288 -59
- package/dist/cli/print-config.d.ts +8 -6
- package/dist/cli/print-config.js +26 -8
- package/dist/cli/render.d.ts +9 -3
- package/dist/cli/render.js +43 -2
- package/dist/config/claude-code.d.ts +2 -6
- package/dist/config/claude-code.js +6 -44
- package/dist/config/command.d.ts +10 -0
- package/dist/config/command.js +47 -0
- package/dist/config/env.d.ts +32 -19
- package/dist/config/env.js +95 -32
- package/dist/config/from-config.js +21 -3
- package/dist/config/resolve.d.ts +13 -1
- package/dist/config/resolve.js +27 -1
- package/dist/config/schema.d.ts +152 -5
- package/dist/config/schema.js +156 -12
- package/dist/core/clock.d.ts +17 -0
- package/dist/core/clock.js +14 -0
- package/dist/core/coverage.d.ts +74 -0
- package/dist/core/coverage.js +221 -0
- package/dist/core/diff.d.ts +101 -0
- package/dist/core/diff.js +178 -35
- package/dist/core/errors.d.ts +12 -3
- package/dist/core/errors.js +21 -2
- package/dist/core/estimate.d.ts +45 -0
- package/dist/core/estimate.js +188 -0
- package/dist/core/exclude.d.ts +42 -0
- package/dist/core/exclude.js +189 -0
- package/dist/core/flags.d.ts +14 -1
- package/dist/core/flags.js +56 -0
- package/dist/core/inputs.d.ts +38 -0
- package/dist/core/inputs.js +228 -0
- package/dist/core/limits.d.ts +83 -0
- package/dist/core/limits.js +169 -0
- package/dist/core/output.d.ts +3 -2
- package/dist/core/output.js +3 -2
- package/dist/core/parts.d.ts +146 -0
- package/dist/core/parts.js +388 -0
- package/dist/core/ports.d.ts +67 -2
- package/dist/core/ports.js +36 -1
- package/dist/core/prompts.d.ts +22 -3
- package/dist/core/prompts.js +43 -7
- package/dist/core/quorum.d.ts +26 -8
- package/dist/core/quorum.js +310 -73
- package/dist/core/retry.d.ts +36 -0
- package/dist/core/retry.js +46 -0
- package/dist/core/secrets.d.ts +98 -0
- package/dist/core/secrets.js +507 -0
- package/dist/core/stages/context.d.ts +131 -23
- package/dist/core/stages/context.js +331 -46
- package/dist/core/stages/panel.d.ts +13 -6
- package/dist/core/stages/panel.js +79 -19
- package/dist/core/stages/research.d.ts +110 -25
- package/dist/core/stages/research.js +338 -44
- package/dist/core/tokens.d.ts +31 -0
- package/dist/core/tokens.js +81 -0
- package/dist/core/types.d.ts +280 -25
- package/dist/core/verify.d.ts +6 -0
- package/dist/core/verify.js +6 -3
- package/dist/git/run.d.ts +223 -0
- package/dist/git/run.js +608 -0
- package/dist/git/tree.d.ts +43 -0
- package/dist/git/tree.js +171 -0
- package/dist/index.d.ts +26 -2
- package/dist/index.js +34 -2
- package/dist/providers/anthropic.d.ts +3 -1
- package/dist/providers/anthropic.js +10 -3
- package/dist/providers/claude-code.d.ts +56 -11
- package/dist/providers/claude-code.js +70 -25
- package/dist/providers/gemini.d.ts +5 -1
- package/dist/providers/gemini.js +65 -11
- package/dist/providers/http.d.ts +49 -4
- package/dist/providers/http.js +155 -14
- package/dist/providers/ollama.d.ts +4 -1
- package/dist/providers/ollama.js +13 -5
- package/dist/providers/openai-compatible.d.ts +4 -1
- package/dist/providers/openai-compatible.js +13 -5
- package/dist/providers/options.d.ts +12 -3
- package/dist/providers/options.js +30 -10
- package/dist/providers/size.d.ts +19 -15
- package/dist/providers/size.js +33 -73
- package/dist/sources/added.d.ts +49 -0
- package/dist/sources/added.js +134 -0
- package/dist/sources/diff.d.ts +13 -0
- package/dist/sources/diff.js +63 -0
- package/dist/sources/local.d.ts +41 -0
- package/dist/sources/local.js +251 -0
- package/dist/sources/paths.d.ts +45 -0
- package/dist/sources/paths.js +230 -0
- package/dist/sources/range.d.ts +21 -0
- package/dist/sources/range.js +75 -0
- package/dist/sources/read.d.ts +69 -0
- package/dist/sources/read.js +37 -0
- package/dist/sources/repository.d.ts +9 -0
- package/dist/sources/repository.js +28 -0
- package/dist/sources/skips.d.ts +49 -0
- package/dist/sources/skips.js +126 -0
- package/package.json +1 -1
- package/schema/quorum.schema.json +49 -1
|
@@ -4,10 +4,12 @@ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import { isAbsolute, join, resolve } from 'node:path';
|
|
6
6
|
import { ConfigError } from "../core/errors.js";
|
|
7
|
+
import { DEFAULT_RETRIES, DEFAULT_TIMEOUT_MS } from "../core/ports.js";
|
|
8
|
+
import { tokenBound } from "../core/tokens.js";
|
|
7
9
|
import { snippet } from "./http.js";
|
|
8
|
-
import { contextTokensIssues, isRecord, NOT_VISIBLE_ASCII, wordList } from "./options.js";
|
|
10
|
+
import { attemptIssues, contextTokensIssues, isRecord, maxTokensIssues, NOT_VISIBLE_ASCII, wordList } from "./options.js";
|
|
9
11
|
import { replyFailure, tokenUsage } from "./reply.js";
|
|
10
|
-
import { checkFits, contextBudget, utf8Length } from "./size.js";
|
|
12
|
+
import { checkFits, contextBudget, DEFAULT_REPLY_TOKENS, roomTokens, utf8Length, worstCaseTokens } from "./size.js";
|
|
11
13
|
/**
|
|
12
14
|
* The `claude-code` adapter runs the user's installed `claude` CLI once per request, headless and isolated,
|
|
13
15
|
* as a pure completion: no tool but the schema tool, no settings file, MCP server, slash command or
|
|
@@ -39,7 +41,6 @@ const HTTP_FIELDS = [
|
|
|
39
41
|
'query',
|
|
40
42
|
'extraBody',
|
|
41
43
|
'temperature',
|
|
42
|
-
'maxTokens',
|
|
43
44
|
'systemPrompt',
|
|
44
45
|
];
|
|
45
46
|
const WINDOWS_SCRIPT = /\.(?:cmd|bat)$/i;
|
|
@@ -274,7 +275,7 @@ function apiKeyIssues(path, auth, apiKey) {
|
|
|
274
275
|
* message repeats a value: a key, a model id or an argument can be a secret.
|
|
275
276
|
*/
|
|
276
277
|
export function claudeCodeIssues(options) {
|
|
277
|
-
const { id, model, contextTokens, auth, apiKey, structuredOutput, args, command } = options;
|
|
278
|
+
const { id, model, contextTokens, maxTokens, auth, apiKey, structuredOutput, args, command, timeoutMs, retries } = options;
|
|
278
279
|
const at = (field, message) => ({ path: `${id}.${field}`, message });
|
|
279
280
|
const given = options;
|
|
280
281
|
const issues = HTTP_FIELDS
|
|
@@ -282,7 +283,8 @@ export function claudeCodeIssues(options) {
|
|
|
282
283
|
.map(field => at(field, 'is not an option of the claude-code protocol'));
|
|
283
284
|
if (typeof model !== 'string' || !plainValue(model))
|
|
284
285
|
issues.push(at('model', 'must be a model id of visible ASCII characters, with no space, that does not start with -'));
|
|
285
|
-
issues.push(...
|
|
286
|
+
issues.push(...maxTokensIssues(id, maxTokens));
|
|
287
|
+
issues.push(...contextTokensIssues(id, contextTokens, maxTokens, CLAUDE_CODE_TEMPLATE_TOKENS));
|
|
286
288
|
if (auth !== undefined && !AUTH_MODES.includes(auth))
|
|
287
289
|
issues.push(at('auth', `must be ${wordList(AUTH_MODES)}`));
|
|
288
290
|
issues.push(...apiKeyIssues(`${id}.apiKey`, auth, apiKey));
|
|
@@ -299,6 +301,7 @@ export function claudeCodeIssues(options) {
|
|
|
299
301
|
if (problem !== undefined)
|
|
300
302
|
issues.push(at('command', problem));
|
|
301
303
|
}
|
|
304
|
+
issues.push(...attemptIssues(id, timeoutMs, retries));
|
|
302
305
|
return issues;
|
|
303
306
|
}
|
|
304
307
|
/**
|
|
@@ -337,14 +340,16 @@ function removedVariable(name, auth, runVariables) {
|
|
|
337
340
|
* every credential, header and endpoint variable, so a login or a key never reaches another host or travels
|
|
338
341
|
* with another credential. Names are matched in any letter case, since Windows reads them so. Under
|
|
339
342
|
* `api-key`, `ANTHROPIC_API_KEY` is then `apiKey` when it is a non-empty string, else the environment's
|
|
340
|
-
* own, trimmed, when that is not blank. Last come
|
|
343
|
+
* own, trimmed, when that is not blank. Last come thirteen variables the run sets over whatever the environment
|
|
341
344
|
* held, in any case: no auto memory, no compaction of a long prompt, a temporary directory of its own, no
|
|
342
|
-
* IDE connection or claude.ai MCP connector, which are on by default,
|
|
343
|
-
* `CLAUDE.md` file, bundled skill or managed skill
|
|
344
|
-
*
|
|
345
|
+
* IDE connection or claude.ai MCP connector, which are on by default, no attachment, file mention,
|
|
346
|
+
* `CLAUDE.md` file, bundled skill or managed skill, Claude Code's wait for each attempt's response headers
|
|
347
|
+
* and its own retries, and, the last, its limit on each reply, which follow the provider's settings
|
|
348
|
+
* (`settings`). A line naming a path (`@/path`) in the message makes Claude Code attach that file for the
|
|
349
|
+
* model to read, with every tool off, unless the first two are set.
|
|
345
350
|
*/
|
|
346
|
-
export function childEnv(env, auth, apiKey, tmpDir) {
|
|
347
|
-
const
|
|
351
|
+
export function childEnv(env, auth, apiKey, tmpDir, settings) {
|
|
352
|
+
const runVariables = {
|
|
348
353
|
CLAUDE_CODE_DISABLE_AUTO_MEMORY: '1',
|
|
349
354
|
DISABLE_COMPACT: '1',
|
|
350
355
|
CLAUDE_CODE_TMPDIR: tmpDir,
|
|
@@ -355,6 +360,9 @@ export function childEnv(env, auth, apiKey, tmpDir) {
|
|
|
355
360
|
CLAUDE_CODE_DISABLE_CLAUDE_MDS: '1',
|
|
356
361
|
CLAUDE_CODE_DISABLE_BUNDLED_SKILLS: '1',
|
|
357
362
|
CLAUDE_CODE_DISABLE_POLICY_SKILLS: '1',
|
|
363
|
+
API_TIMEOUT_MS: String(settings.timeoutMs),
|
|
364
|
+
CLAUDE_CODE_MAX_RETRIES: String(settings.retries),
|
|
365
|
+
CLAUDE_CODE_MAX_OUTPUT_TOKENS: String(settings.maxTokens),
|
|
358
366
|
};
|
|
359
367
|
const own = Object.entries(env).filter((entry) => typeof entry[1] === 'string');
|
|
360
368
|
const inheritedKey = (own.find(([name]) => name === KEY_VARIABLE) ?? own.find(([name]) => name.toUpperCase() === KEY_VARIABLE))?.[1].trim();
|
|
@@ -362,11 +370,11 @@ export function childEnv(env, auth, apiKey, tmpDir) {
|
|
|
362
370
|
? apiKey
|
|
363
371
|
: inheritedKey;
|
|
364
372
|
return {
|
|
365
|
-
...Object.fromEntries(own.filter(([name]) => !removedVariable(name, auth,
|
|
373
|
+
...Object.fromEntries(own.filter(([name]) => !removedVariable(name, auth, runVariables))),
|
|
366
374
|
...(auth === 'api-key' && key !== undefined && key !== ''
|
|
367
375
|
? { [KEY_VARIABLE]: key }
|
|
368
376
|
: {}),
|
|
369
|
-
...
|
|
377
|
+
...runVariables,
|
|
370
378
|
};
|
|
371
379
|
}
|
|
372
380
|
/**
|
|
@@ -515,11 +523,13 @@ function signalName(signal) {
|
|
|
515
523
|
}
|
|
516
524
|
/**
|
|
517
525
|
* The reply of a result, or the `ProviderError` it amounts to, in this order: a prompt too long, a
|
|
518
|
-
* refusal,
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
526
|
+
* refusal, a reply Claude Code reports cut off at its output limit (an error result whose `api_error` is
|
|
527
|
+
* `max_output_tokens`, which it gives after continuing the reply three times), an error (anything but an
|
|
528
|
+
* `is_error` of `false` is one), a child a signal ended or a non-zero exit after a success, a tool the server
|
|
529
|
+
* ran, a reply the token limit cut off, then the text. Claude Code reports an API error as exit 1 with
|
|
530
|
+
* `subtype: "success"` and `is_error: true`, so the JSON on stdout is read before the exit code. Every
|
|
531
|
+
* failure carries the usage the result reports, summed as `sumUsage` does, since the model generated and
|
|
532
|
+
* billed those tokens whatever came after.
|
|
523
533
|
*/
|
|
524
534
|
function readFinished(result, run, context) {
|
|
525
535
|
const modelUsage = isRecord(result.modelUsage)
|
|
@@ -531,6 +541,8 @@ function readFinished(result, run, context) {
|
|
|
531
541
|
throw fail('too-large', 'Claude Code refused the prompt as too long for the model\'s context');
|
|
532
542
|
if (result.stop_reason === 'refusal')
|
|
533
543
|
throw fail('refusal', 'model refused');
|
|
544
|
+
if (result.api_error === 'max_output_tokens')
|
|
545
|
+
throw fail('truncated', 'reply was cut off by the token limit');
|
|
534
546
|
if (result.is_error !== false || result.subtype !== 'success')
|
|
535
547
|
throw errorFailure(result, run, context, usage);
|
|
536
548
|
if (run.signal !== null)
|
|
@@ -557,7 +569,7 @@ function readFinished(result, run, context) {
|
|
|
557
569
|
* JSON result: a signal, a non-zero exit with a quote of stderr, or nothing at all, each a failure. Every
|
|
558
570
|
* quoted text goes through `snippet`, redacted of the key.
|
|
559
571
|
*
|
|
560
|
-
* @throws ProviderError of the kind the failure is: `too-large`, `refusal`, `http`, `process` or `invalid-response`
|
|
572
|
+
* @throws ProviderError of the kind the failure is: `too-large`, `refusal`, `truncated`, `http`, `process` or `invalid-response`
|
|
561
573
|
*/
|
|
562
574
|
export function readResult(run, context) {
|
|
563
575
|
const result = parseResult(run.stdout);
|
|
@@ -887,7 +899,8 @@ async function requireVersion(launch, signal, fail) {
|
|
|
887
899
|
* 1. exactly one message, a user message, else `unsupported`;
|
|
888
900
|
* 2. a message that starts with `/` after white space, which `-p` would run as a command: `unsupported`;
|
|
889
901
|
* 3. a message over `MAX_STDIN_BYTES` bytes: `too-large`;
|
|
890
|
-
* 4. the size guard, with `CLAUDE_CODE_TEMPLATE_TOKENS` kept for the
|
|
902
|
+
* 4. the size guard, with the reply budget, `maxTokens`, and `CLAUDE_CODE_TEMPLATE_TOKENS` kept for the
|
|
903
|
+
* context Claude Code adds: `too-large`;
|
|
891
904
|
* 5. an aborted signal: `aborted`;
|
|
892
905
|
* 6. a directory of its own, under the temporary directory, holding an empty working directory, `claude`'s
|
|
893
906
|
* scratch directory and the system text's file: a failure to make it is `process`;
|
|
@@ -906,8 +919,27 @@ async function requireVersion(launch, signal, fail) {
|
|
|
906
919
|
* 13. an aborted signal: `aborted`; else `readResult`'s reply or failure, with every credential of the
|
|
907
920
|
* child's environment redacted from the text it quotes (`credentialValues`).
|
|
908
921
|
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
922
|
+
* Claude Code retries a failed API call by itself, ten times by default, so the provider sets `API_TIMEOUT_MS`
|
|
923
|
+
* and `CLAUDE_CODE_MAX_RETRIES` in the child's environment from `timeoutMs` and `retries` (600000 ms and 2 by
|
|
924
|
+
* default) over any the environment held, and states no retry to the core, which would retry what Claude Code
|
|
925
|
+
* retried. It sets `CLAUDE_CODE_MAX_OUTPUT_TOKENS` the same way from `maxTokens` (16000 by default, where
|
|
926
|
+
* Claude Code asks for up to 128000 by itself), so the model's API bounds every reply. It states one deadline
|
|
927
|
+
* for the whole run, `timeoutMs` times `retries` plus one: Claude Code's own attempts and the waits between
|
|
928
|
+
* them, its start and the version check, and each reply's time after its headers all count against it, where
|
|
929
|
+
* `API_TIMEOUT_MS` limits only an attempt's wait for its headers. At it the core aborts the signal, which
|
|
930
|
+
* stops the child's group as step 11 says.
|
|
931
|
+
*
|
|
932
|
+
* It states its worst case from the count its size guard keeps, over the model turns of a run: in `schema`
|
|
933
|
+
* mode two, since the model answers by calling the schema tool and `claude` then makes a second turn after
|
|
934
|
+
* the tool's result, which carries the first reply as input, so `2 * (text + 4096) + 3 * maxTokens`; in
|
|
935
|
+
* `prompt` mode one, `text + 4096 + maxTokens`. It leaves out what Claude Code spends beyond that, a reply
|
|
936
|
+
* it continues after its limit, up to three times a turn, and its own retries: those count once the run
|
|
937
|
+
* reports its usage.
|
|
938
|
+
*
|
|
939
|
+
* It states the room its checks leave: the lesser of what its size guard leaves, from the same count, and what
|
|
940
|
+
* the stdin cap leaves, `MAX_STDIN_BYTES` less the message at one token per byte, which bounds the bytes the
|
|
941
|
+
* cap checks, so a request whose room is not negative passes both and text added to the message lowers the
|
|
942
|
+
* room by exactly its count.
|
|
911
943
|
*
|
|
912
944
|
* @throws ConfigError with every problem of the options, before any request
|
|
913
945
|
*/
|
|
@@ -915,15 +947,28 @@ export function claudeCode(options) {
|
|
|
915
947
|
const issues = claudeCodeIssues(options);
|
|
916
948
|
if (issues.length > 0)
|
|
917
949
|
throw new ConfigError(issues);
|
|
918
|
-
const { id, model, contextTokens, auth = 'inherit', apiKey, structuredOutput = 'schema', command = 'claude' } = options;
|
|
950
|
+
const { id, model, contextTokens, maxTokens = DEFAULT_REPLY_TOKENS, auth = 'inherit', apiKey, structuredOutput = 'schema', command = 'claude', timeoutMs = DEFAULT_TIMEOUT_MS, retries = DEFAULT_RETRIES } = options;
|
|
951
|
+
const settings = { timeoutMs, retries, maxTokens };
|
|
919
952
|
const args = [
|
|
920
953
|
...options.args ?? [],
|
|
921
954
|
];
|
|
922
955
|
const env = Object.fromEntries(Object.entries(options.env ?? process.env));
|
|
923
|
-
const budget = contextBudget(contextTokens,
|
|
956
|
+
const budget = contextBudget(contextTokens, maxTokens, CLAUDE_CODE_TEMPLATE_TOKENS);
|
|
924
957
|
return {
|
|
925
958
|
id,
|
|
926
959
|
model,
|
|
960
|
+
timeoutMs: timeoutMs * (retries + 1),
|
|
961
|
+
retries: 0,
|
|
962
|
+
worstCase: request => worstCaseTokens({
|
|
963
|
+
system: request.system,
|
|
964
|
+
messages: [...request.messages],
|
|
965
|
+
}, request.output, budget, structuredOutput === 'schema'
|
|
966
|
+
? 2
|
|
967
|
+
: 1),
|
|
968
|
+
room: request => Math.min(roomTokens({
|
|
969
|
+
system: request.system,
|
|
970
|
+
messages: [...request.messages],
|
|
971
|
+
}, request.output, budget), MAX_STDIN_BYTES - tokenBound(request.messages[0]?.content ?? '')),
|
|
927
972
|
async complete(request) {
|
|
928
973
|
const fail = replyFailure(id);
|
|
929
974
|
const message = messageOf(request, fail);
|
|
@@ -938,7 +983,7 @@ export function claudeCode(options) {
|
|
|
938
983
|
const untrack = trackRun(live);
|
|
939
984
|
try {
|
|
940
985
|
throwIfAborted(signal, fail);
|
|
941
|
-
const environment = childEnv(env, auth, apiKey, run.tmp);
|
|
986
|
+
const environment = childEnv(env, auth, apiKey, run.tmp, settings);
|
|
942
987
|
await requireVersion({
|
|
943
988
|
command,
|
|
944
989
|
args: [],
|
|
@@ -5,7 +5,11 @@ export type GeminiOptions = HttpProviderOptions;
|
|
|
5
5
|
/**
|
|
6
6
|
* A provider for the Gemini API's `generateContent`. The key, when there is one, travels only in the
|
|
7
7
|
* header `auth` names, `x-goog-api-key` by default (invariant I1), never in the `key` query parameter. It
|
|
8
|
-
* refuses, before any request, a request that may not fit `contextTokens`.
|
|
8
|
+
* refuses, before any request, a request that may not fit `contextTokens`. It sends the reply's limit,
|
|
9
|
+
* `maxTokens` else the budget of the size guard, as `generationConfig.maxOutputTokens`, so the server bounds
|
|
10
|
+
* a reply's length. It states its worst case from the count its size guard keeps: the text it sends, the
|
|
11
|
+
* template reserve and that budget, and the room its guard leaves from the same count: `contextTokens` less
|
|
12
|
+
* those three.
|
|
9
13
|
*
|
|
10
14
|
* @throws ConfigError with every problem of the options, before any request
|
|
11
15
|
*/
|
package/dist/providers/gemini.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { issuePath } from "../core/errors.js";
|
|
2
|
+
import { DEFAULT_RETRIES, DEFAULT_TIMEOUT_MS } from "../core/ports.js";
|
|
2
3
|
import { postJson, snippet } from "./http.js";
|
|
3
4
|
import { shapeMessages } from "./messages.js";
|
|
4
|
-
import { bodyKeyForm, isRecord, mergeBody, prepareHttp, spellingIssues
|
|
5
|
+
import { bodyKeyForm, isRecord, mergeBody, prepareHttp, spellingIssues } from "./options.js";
|
|
5
6
|
import { endedEarly, joinedText, replyFailure, tokenUsage } from "./reply.js";
|
|
6
|
-
import { checkFits, contextBudget } from "./size.js";
|
|
7
|
+
import { checkFits, contextBudget, roomTokens, worstCaseTokens } from "./size.js";
|
|
7
8
|
/** A Gemini model id, such as `gemini-3.8-flash`. It goes in the URL's path, so it holds no `/`, `?`, `#`, `%` or `:`. */
|
|
8
9
|
const MODEL_ID = /^[a-z0-9][a-z0-9.-]*$/i;
|
|
9
10
|
/** The longest block reason quoted, in characters: the pattern's bound and the quote's cut. */
|
|
@@ -33,6 +34,50 @@ const FILTERED = new Set([
|
|
|
33
34
|
'SPII',
|
|
34
35
|
'IMAGE_SAFETY',
|
|
35
36
|
]);
|
|
37
|
+
const RETRY_INFO = 'type.googleapis.com/google.rpc.RetryInfo';
|
|
38
|
+
const QUOTA_FAILURE = 'type.googleapis.com/google.rpc.QuotaFailure';
|
|
39
|
+
/** A `retryDelay` as Google's protobuf JSON writes a duration: whole or fractional seconds, nine digits at most. */
|
|
40
|
+
const RETRY_DELAY = /^(\d+(?:\.\d{1,9})?)s$/;
|
|
41
|
+
/** The part of a quota's id that names a per-day limit, which no wait of seconds lifts. */
|
|
42
|
+
const PER_DAY = 'PerDay';
|
|
43
|
+
/** The entries of `list` that are objects of the given `@type`, when it is a list. */
|
|
44
|
+
function detailsOfType(list, type) {
|
|
45
|
+
return Array.isArray(list)
|
|
46
|
+
? list.filter((entry) => isRecord(entry) && entry['@type'] === type)
|
|
47
|
+
: [];
|
|
48
|
+
}
|
|
49
|
+
/** A `retryDelay` as milliseconds, when it is a positive duration in seconds. */
|
|
50
|
+
function delayMs(retryDelay) {
|
|
51
|
+
const match = typeof retryDelay === 'string'
|
|
52
|
+
? RETRY_DELAY.exec(retryDelay)
|
|
53
|
+
: null;
|
|
54
|
+
const ms = Number(match?.[1]) * 1000;
|
|
55
|
+
return Number.isFinite(ms) && ms > 0
|
|
56
|
+
? ms
|
|
57
|
+
: undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* What an error body of Google's `{ error: { details } }` says of a retry, read defensively: the first
|
|
61
|
+
* `RetryInfo` with a valid `retryDelay` is the wait, and a `QuotaFailure` violation whose `quotaId` names
|
|
62
|
+
* a per-day limit turns the retry off, whatever the delay says, since the quota is back only the next day.
|
|
63
|
+
* Only numbers and a verdict leave this function: no text of the body does.
|
|
64
|
+
*/
|
|
65
|
+
function errorHints(body) {
|
|
66
|
+
const details = isRecord(body) && isRecord(body.error)
|
|
67
|
+
? body.error.details
|
|
68
|
+
: undefined;
|
|
69
|
+
const retryAfterMs = detailsOfType(details, RETRY_INFO).map(info => delayMs(info.retryDelay)).find(ms => ms !== undefined);
|
|
70
|
+
const perDay = detailsOfType(details, QUOTA_FAILURE).some(failure => Array.isArray(failure.violations)
|
|
71
|
+
&& failure.violations.some(violation => isRecord(violation) && typeof violation.quotaId === 'string' && violation.quotaId.includes(PER_DAY)));
|
|
72
|
+
return {
|
|
73
|
+
...(retryAfterMs === undefined
|
|
74
|
+
? {}
|
|
75
|
+
: { retryAfterMs }),
|
|
76
|
+
...(perDay
|
|
77
|
+
? { retryable: false }
|
|
78
|
+
: {}),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
36
81
|
/** A `key` query parameter, in any letter case, would put the key in the URL: it travels only in the header `auth` names. */
|
|
37
82
|
function keyParameterIssues(id, query) {
|
|
38
83
|
return Object.keys(query)
|
|
@@ -58,18 +103,18 @@ function generationConfigIssues(id, extraBody) {
|
|
|
58
103
|
];
|
|
59
104
|
});
|
|
60
105
|
}
|
|
61
|
-
/** The generation config of a request
|
|
62
|
-
function generationConfig({ temperature,
|
|
63
|
-
return
|
|
106
|
+
/** The generation config of a request: it always holds the reply's limit. */
|
|
107
|
+
function generationConfig({ temperature, replyTokens, structuredOutput }, output) {
|
|
108
|
+
return {
|
|
64
109
|
temperature: temperature ?? undefined,
|
|
65
|
-
maxOutputTokens:
|
|
110
|
+
maxOutputTokens: replyTokens,
|
|
66
111
|
responseMimeType: structuredOutput === 'prompt'
|
|
67
112
|
? undefined
|
|
68
113
|
: 'application/json',
|
|
69
114
|
responseJsonSchema: structuredOutput === 'schema'
|
|
70
115
|
? output.schema
|
|
71
116
|
: undefined,
|
|
72
|
-
}
|
|
117
|
+
};
|
|
73
118
|
}
|
|
74
119
|
/**
|
|
75
120
|
* Turn a `generateContent` reply into text, or a `ProviderError`. Every property is read defensively: the
|
|
@@ -124,12 +169,16 @@ function readReply(reply, { id, model }, redact) {
|
|
|
124
169
|
/**
|
|
125
170
|
* A provider for the Gemini API's `generateContent`. The key, when there is one, travels only in the
|
|
126
171
|
* header `auth` names, `x-goog-api-key` by default (invariant I1), never in the `key` query parameter. It
|
|
127
|
-
* refuses, before any request, a request that may not fit `contextTokens`.
|
|
172
|
+
* refuses, before any request, a request that may not fit `contextTokens`. It sends the reply's limit,
|
|
173
|
+
* `maxTokens` else the budget of the size guard, as `generationConfig.maxOutputTokens`, so the server bounds
|
|
174
|
+
* a reply's length. It states its worst case from the count its size guard keeps: the text it sends, the
|
|
175
|
+
* template reserve and that budget, and the room its guard leaves from the same count: `contextTokens` less
|
|
176
|
+
* those three.
|
|
128
177
|
*
|
|
129
178
|
* @throws ConfigError with every problem of the options, before any request
|
|
130
179
|
*/
|
|
131
180
|
export function gemini(options) {
|
|
132
|
-
const { id, model, auth = { header: 'x-goog-api-key' }, query, temperature, maxTokens, contextTokens, systemPrompt = 'native', structuredOutput = 'json' } = options;
|
|
181
|
+
const { id, model, auth = { header: 'x-goog-api-key' }, query, temperature, maxTokens, contextTokens, systemPrompt = 'native', structuredOutput = 'json', timeoutMs = DEFAULT_TIMEOUT_MS, retries = DEFAULT_RETRIES } = options;
|
|
133
182
|
const modelId = typeof model === 'string'
|
|
134
183
|
? model.replace(/^models\//, '')
|
|
135
184
|
: '';
|
|
@@ -147,10 +196,14 @@ export function gemini(options) {
|
|
|
147
196
|
return {
|
|
148
197
|
id,
|
|
149
198
|
model,
|
|
199
|
+
timeoutMs,
|
|
200
|
+
retries,
|
|
201
|
+
worstCase: request => worstCaseTokens(shapeMessages(request, systemPrompt), request.output, budget),
|
|
202
|
+
room: request => roomTokens(shapeMessages(request, systemPrompt), request.output, budget),
|
|
150
203
|
async complete(request) {
|
|
151
204
|
const { system, messages } = shapeMessages(request, systemPrompt);
|
|
152
205
|
checkFits(id, { system, messages }, request.output, budget);
|
|
153
|
-
// JSON.stringify drops the undefined fields: no systemInstruction inline, no
|
|
206
|
+
// JSON.stringify drops the undefined fields: no systemInstruction inline, no temperature unless set.
|
|
154
207
|
const reply = await postJson({
|
|
155
208
|
providerId: id,
|
|
156
209
|
url,
|
|
@@ -171,10 +224,11 @@ export function gemini(options) {
|
|
|
171
224
|
{ text: system },
|
|
172
225
|
],
|
|
173
226
|
},
|
|
174
|
-
generationConfig: generationConfig({ temperature,
|
|
227
|
+
generationConfig: generationConfig({ temperature, replyTokens: budget.replyTokens, structuredOutput }, request.output),
|
|
175
228
|
}, extraBody),
|
|
176
229
|
signal: request.signal,
|
|
177
230
|
redact,
|
|
231
|
+
errorHints,
|
|
178
232
|
});
|
|
179
233
|
return readReply(reply, { id, model }, redact);
|
|
180
234
|
},
|
package/dist/providers/http.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an adapter reads of a failed reply's body that `postJson` cannot: a wait the server asks for, in
|
|
3
|
+
* milliseconds, and a reason never to retry. A hook can add a hint and turn a retry off, never on.
|
|
4
|
+
*/
|
|
5
|
+
export interface ErrorHints {
|
|
6
|
+
readonly retryAfterMs?: number;
|
|
7
|
+
readonly retryable?: false;
|
|
8
|
+
}
|
|
1
9
|
export interface PostJsonOptions {
|
|
2
10
|
providerId: string;
|
|
3
11
|
url: string;
|
|
@@ -6,6 +14,8 @@ export interface PostJsonOptions {
|
|
|
6
14
|
signal?: AbortSignal;
|
|
7
15
|
/** Values redacted from any server text that echoes them: the key, the base URL's path and the header values. */
|
|
8
16
|
redact?: readonly string[];
|
|
17
|
+
/** What the adapter reads of a failed reply's body, handed the body parsed as JSON, or `undefined` when it is none. */
|
|
18
|
+
errorHints?: (body: unknown) => ErrorHints;
|
|
9
19
|
}
|
|
10
20
|
/**
|
|
11
21
|
* Server or model text made safe to put in an error message. The text is first cut to 64 KiB, as much as
|
|
@@ -40,11 +50,46 @@ export declare function originOf(url: string): string;
|
|
|
40
50
|
* Anything that is not a string URL with the `http:` or `https:` scheme has none.
|
|
41
51
|
*/
|
|
42
52
|
export declare function httpOrigin(url: unknown): string | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Node's global dispatcher handed each request with both of undici's timers off, the 300-second
|
|
55
|
+
* limits on the headers and on the body, or undefined when the runtime has none: Bun's `fetch`
|
|
56
|
+
* never reads a dispatcher, and its own timer goes off with `timeout: false`. The global is read at
|
|
57
|
+
* each request, never kept, so a proxy agent an operator set still carries it. The delegate calls
|
|
58
|
+
* the global's `dispatch` as its method, with the request's options and both timeouts at 0 (none),
|
|
59
|
+
* and the same handler.
|
|
60
|
+
*/
|
|
61
|
+
export declare function timersOff(): {
|
|
62
|
+
dispatch: (options: object, handler: unknown) => unknown;
|
|
63
|
+
} | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* The wait a failed reply asks for, in milliseconds: `retry-after-ms`, then `retry-after` in seconds, then
|
|
66
|
+
* `retry-after` as an HTTP date less `now`, the wall clock in milliseconds. The first that is a positive
|
|
67
|
+
* finite number is the hint. A value is read as decimal digits with an optional fraction, or as an
|
|
68
|
+
* IMF-fixdate matched by its shape before `Date.parse` reads it, since the runtimes read a bare `1` or
|
|
69
|
+
* `120` as a year and the other date forms as local time. Anything else is no hint, and the header is
|
|
70
|
+
* never copied anywhere.
|
|
71
|
+
*/
|
|
72
|
+
export declare function retryAfterHint(headers: Headers, now: number): number | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* What the body of a failed reply says of a retry that the vendors' own codes settle: `false` for a used-up
|
|
75
|
+
* credit, spend limit or quota (OpenAI's `error.code`, or its `error.type` for `insufficient_quota`, and
|
|
76
|
+
* Anthropic's `error.details.error_code`), `true` for OpenRouter's 402 of its in-flight budget when the
|
|
77
|
+
* reply is `hinted` with a wait, else `undefined`, and the status decides. A code is only compared, never
|
|
78
|
+
* copied. This checks objects with a local test, since `options.ts` imports this module.
|
|
79
|
+
*/
|
|
80
|
+
export declare function vendorRetryable(body: unknown, status: number, hinted: boolean): boolean | undefined;
|
|
43
81
|
/**
|
|
44
82
|
* POST a JSON body and return the parsed JSON reply. A redirect is not followed: it is an `http` error,
|
|
45
|
-
* so a key sent in any header never reaches another endpoint.
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* a
|
|
83
|
+
* so a key sent in any header never reaches another endpoint. The runtimes' own timers are off, so
|
|
84
|
+
* the core's deadline is the only one: a long reply of a thinking model is not cut at 300 seconds.
|
|
85
|
+
* A reply body over 16 MiB is refused unread. Every failure is a `ProviderError` without a `cause`:
|
|
86
|
+
* the runtime's own error text can hold the URL with its credentials or a header value, so it is
|
|
87
|
+
* never copied, and no header value appears in a message.
|
|
88
|
+
*
|
|
89
|
+
* A failed reply's error quotes the body in its message, as it always did, and carries what the reply
|
|
90
|
+
* says of a retry as a number and a boolean, never as text: `retryAfterMs` from `retry-after-ms` or
|
|
91
|
+
* `retry-after`, else from `errorHints`, and `retryable` when the reply settles it, which a "never" wins:
|
|
92
|
+
* `x-should-retry: false`, a used-up credit, spend limit or quota, or a hook's, else `x-should-retry: true`
|
|
93
|
+
* or OpenRouter's in-flight 402 with a hint, else the status decides.
|
|
49
94
|
*/
|
|
50
95
|
export declare function postJson<T>(options: PostJsonOptions): Promise<T>;
|
package/dist/providers/http.js
CHANGED
|
@@ -8,9 +8,20 @@ const MAX_ERROR_BODY = 500;
|
|
|
8
8
|
const MAX_ERROR_BYTES = 64 * 1024;
|
|
9
9
|
/** A reply body over this many bytes is refused: the core refuses reply text over 1,000,000 characters anyway. */
|
|
10
10
|
const MAX_REPLY_BYTES = 16 * 1024 * 1024;
|
|
11
|
+
/** The symbol under which Node keeps the dispatcher its `fetch` uses. */
|
|
12
|
+
const NODE_GLOBAL_DISPATCHER = Symbol.for('undici.globalDispatcher.1');
|
|
11
13
|
const REDACTED = '[redacted]';
|
|
12
14
|
const UNKNOWN_ENDPOINT = 'the endpoint';
|
|
13
15
|
const PLAIN_IDENTIFIER = /^[A-Z]\w{0,39}$/i;
|
|
16
|
+
/** A wait as a header writes it: decimal digits with an optional fraction. Never a sign, an exponent, a hexadecimal or a list. */
|
|
17
|
+
const DECIMAL = /^\d+(?:\.\d+)?$/;
|
|
18
|
+
/** The one date form read: an IMF-fixdate. Both engines read the others as local time, or `1` as a year. */
|
|
19
|
+
const IMF_FIXDATE = /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
|
20
|
+
/** The codes of OpenAI that say a credit or a spend limit is used up: a retry cannot succeed, and would be billed. */
|
|
21
|
+
const SPEND_CODES = new Set(['credit_balance_exhausted', 'organization_usage_limit_exceeded', 'insufficient_quota']);
|
|
22
|
+
const SPEND_LIMIT_SUFFIX = '_spend_limit_exceeded';
|
|
23
|
+
const ANTHROPIC_SPEND_CODE = 'enforced_spend_limit_reached';
|
|
24
|
+
const OPENROUTER_IN_FLIGHT = 'openrouter_in_flight_budget';
|
|
14
25
|
/** A JSON unicode escape (a backslash, then u00 and two hex digits) for printable ASCII, U+0020 to U+007E. */
|
|
15
26
|
const ESCAPED_PRINTABLE_ASCII = /\\u00(2[0-9a-fA-F]|[3-6][0-9a-fA-F]|7[0-9a-eA-E])/g;
|
|
16
27
|
/** Every form in which a server may echo the secret. */
|
|
@@ -159,6 +170,29 @@ function codeSuffix(error) {
|
|
|
159
170
|
}
|
|
160
171
|
return '';
|
|
161
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Node's global dispatcher handed each request with both of undici's timers off, the 300-second
|
|
175
|
+
* limits on the headers and on the body, or undefined when the runtime has none: Bun's `fetch`
|
|
176
|
+
* never reads a dispatcher, and its own timer goes off with `timeout: false`. The global is read at
|
|
177
|
+
* each request, never kept, so a proxy agent an operator set still carries it. The delegate calls
|
|
178
|
+
* the global's `dispatch` as its method, with the request's options and both timeouts at 0 (none),
|
|
179
|
+
* and the same handler.
|
|
180
|
+
*/
|
|
181
|
+
export function timersOff() {
|
|
182
|
+
// eslint-disable-next-line no-restricted-globals -- Node's fetch keeps its dispatcher on the global object, under this symbol alone.
|
|
183
|
+
const shared = globalThis[NODE_GLOBAL_DISPATCHER];
|
|
184
|
+
if (typeof shared !== 'object' || shared === null)
|
|
185
|
+
return undefined;
|
|
186
|
+
const { dispatch } = shared;
|
|
187
|
+
if (typeof dispatch !== 'function')
|
|
188
|
+
return undefined;
|
|
189
|
+
return {
|
|
190
|
+
dispatch: (options, handler) => Reflect.apply(dispatch, shared, [
|
|
191
|
+
{ ...options, headersTimeout: 0, bodyTimeout: 0 },
|
|
192
|
+
handler,
|
|
193
|
+
]),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
162
196
|
/** At most `max` bytes of the body, decoded as UTF-8, and whether that was all of it. The rest is never read. */
|
|
163
197
|
async function readCapped(response, max) {
|
|
164
198
|
const reader = response.body?.getReader();
|
|
@@ -188,15 +222,119 @@ async function readErrorText(response) {
|
|
|
188
222
|
return '';
|
|
189
223
|
}
|
|
190
224
|
}
|
|
225
|
+
/** The number if it is positive and finite, else nothing: a hint of 0, a negative one or one that is no number is none. */
|
|
226
|
+
function positiveOrUndefined(value) {
|
|
227
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0
|
|
228
|
+
? value
|
|
229
|
+
: undefined;
|
|
230
|
+
}
|
|
231
|
+
/** `value` times `unit`, when it is written as decimal digits with an optional fraction. */
|
|
232
|
+
function scaled(value, unit) {
|
|
233
|
+
return value !== null && DECIMAL.test(value)
|
|
234
|
+
? positiveOrUndefined(Number(value) * unit)
|
|
235
|
+
: undefined;
|
|
236
|
+
}
|
|
237
|
+
/** The wait, in milliseconds, until an HTTP date written as an IMF-fixdate, given the wall clock `now`. */
|
|
238
|
+
function untilDate(value, now) {
|
|
239
|
+
return value !== null && IMF_FIXDATE.test(value)
|
|
240
|
+
? positiveOrUndefined(Date.parse(value) - now)
|
|
241
|
+
: undefined;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* The wait a failed reply asks for, in milliseconds: `retry-after-ms`, then `retry-after` in seconds, then
|
|
245
|
+
* `retry-after` as an HTTP date less `now`, the wall clock in milliseconds. The first that is a positive
|
|
246
|
+
* finite number is the hint. A value is read as decimal digits with an optional fraction, or as an
|
|
247
|
+
* IMF-fixdate matched by its shape before `Date.parse` reads it, since the runtimes read a bare `1` or
|
|
248
|
+
* `120` as a year and the other date forms as local time. Anything else is no hint, and the header is
|
|
249
|
+
* never copied anywhere.
|
|
250
|
+
*/
|
|
251
|
+
export function retryAfterHint(headers, now) {
|
|
252
|
+
const after = headers.get('retry-after');
|
|
253
|
+
return scaled(headers.get('retry-after-ms'), 1) ?? scaled(after, 1000) ?? untilDate(after, now);
|
|
254
|
+
}
|
|
255
|
+
/** The own property `key` of `value` when it is an object, else nothing: the body is untrusted, and so is its prototype. */
|
|
256
|
+
function member(value, key) {
|
|
257
|
+
return typeof value === 'object' && value !== null && Object.hasOwn(value, key)
|
|
258
|
+
? value[key]
|
|
259
|
+
: undefined;
|
|
260
|
+
}
|
|
261
|
+
function isSpendCode(code) {
|
|
262
|
+
return typeof code === 'string' && (SPEND_CODES.has(code) || code.endsWith(SPEND_LIMIT_SUFFIX));
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* What the body of a failed reply says of a retry that the vendors' own codes settle: `false` for a used-up
|
|
266
|
+
* credit, spend limit or quota (OpenAI's `error.code`, or its `error.type` for `insufficient_quota`, and
|
|
267
|
+
* Anthropic's `error.details.error_code`), `true` for OpenRouter's 402 of its in-flight budget when the
|
|
268
|
+
* reply is `hinted` with a wait, else `undefined`, and the status decides. A code is only compared, never
|
|
269
|
+
* copied. This checks objects with a local test, since `options.ts` imports this module.
|
|
270
|
+
*/
|
|
271
|
+
export function vendorRetryable(body, status, hinted) {
|
|
272
|
+
const error = member(body, 'error');
|
|
273
|
+
if (isSpendCode(member(error, 'code')) || member(error, 'type') === 'insufficient_quota' || member(member(error, 'details'), 'error_code') === ANTHROPIC_SPEND_CODE)
|
|
274
|
+
return false;
|
|
275
|
+
if (status === 402 && hinted && member(member(error, 'metadata'), 'limit_source') === OPENROUTER_IN_FLIGHT)
|
|
276
|
+
return true;
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
/** The body as JSON, or nothing when it is none. */
|
|
280
|
+
function jsonOrUndefined(text) {
|
|
281
|
+
try {
|
|
282
|
+
return JSON.parse(text);
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** A failed reply's error: its message quotes the body, and its hint and verdict are numbers and booleans, never text. */
|
|
289
|
+
function httpFailure(options, response, text) {
|
|
290
|
+
const { providerId, redact } = options;
|
|
291
|
+
const { status } = response;
|
|
292
|
+
const body = jsonOrUndefined(text);
|
|
293
|
+
const extra = options.errorHints?.(body) ?? {};
|
|
294
|
+
const retryAfterMs = retryAfterHint(response.headers, Date.now()) ?? positiveOrUndefined(extra.retryAfterMs);
|
|
295
|
+
const said = response.headers.get('x-should-retry');
|
|
296
|
+
const verdicts = [
|
|
297
|
+
said === 'false'
|
|
298
|
+
? false
|
|
299
|
+
: said === 'true'
|
|
300
|
+
? true
|
|
301
|
+
: undefined,
|
|
302
|
+
vendorRetryable(body, status, retryAfterMs !== undefined),
|
|
303
|
+
extra.retryable,
|
|
304
|
+
];
|
|
305
|
+
const retryable = verdicts.includes(false)
|
|
306
|
+
? false
|
|
307
|
+
: verdicts.includes(true)
|
|
308
|
+
? true
|
|
309
|
+
: undefined;
|
|
310
|
+
const redirect = status >= 300 && status < 400
|
|
311
|
+
? ' (redirects are not followed)'
|
|
312
|
+
: '';
|
|
313
|
+
return new ProviderError({
|
|
314
|
+
providerId,
|
|
315
|
+
kind: 'http',
|
|
316
|
+
message: `HTTP ${status}${redirect}: ${snippet(text, MAX_ERROR_BODY, redact)}`,
|
|
317
|
+
status,
|
|
318
|
+
retryAfterMs,
|
|
319
|
+
retryable,
|
|
320
|
+
});
|
|
321
|
+
}
|
|
191
322
|
/**
|
|
192
323
|
* POST a JSON body and return the parsed JSON reply. A redirect is not followed: it is an `http` error,
|
|
193
|
-
* so a key sent in any header never reaches another endpoint.
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
* a
|
|
324
|
+
* so a key sent in any header never reaches another endpoint. The runtimes' own timers are off, so
|
|
325
|
+
* the core's deadline is the only one: a long reply of a thinking model is not cut at 300 seconds.
|
|
326
|
+
* A reply body over 16 MiB is refused unread. Every failure is a `ProviderError` without a `cause`:
|
|
327
|
+
* the runtime's own error text can hold the URL with its credentials or a header value, so it is
|
|
328
|
+
* never copied, and no header value appears in a message.
|
|
329
|
+
*
|
|
330
|
+
* A failed reply's error quotes the body in its message, as it always did, and carries what the reply
|
|
331
|
+
* says of a retry as a number and a boolean, never as text: `retryAfterMs` from `retry-after-ms` or
|
|
332
|
+
* `retry-after`, else from `errorHints`, and `retryable` when the reply settles it, which a "never" wins:
|
|
333
|
+
* `x-should-retry: false`, a used-up credit, spend limit or quota, or a hook's, else `x-should-retry: true`
|
|
334
|
+
* or OpenRouter's in-flight 402 with a hint, else the status decides.
|
|
197
335
|
*/
|
|
198
336
|
export async function postJson(options) {
|
|
199
|
-
const { providerId, url, headers, body, signal
|
|
337
|
+
const { providerId, url, headers, body, signal } = options;
|
|
200
338
|
const fail = (kind, message, status) => new ProviderError({ providerId, kind, message, status });
|
|
201
339
|
const lost = (what, error) => signal?.aborted
|
|
202
340
|
? fail('aborted', 'request aborted')
|
|
@@ -205,24 +343,27 @@ export async function postJson(options) {
|
|
|
205
343
|
const payload = JSON.stringify(body);
|
|
206
344
|
let response;
|
|
207
345
|
try {
|
|
208
|
-
|
|
346
|
+
// `timeout: false` turns Bun's idle timer off. Node reads `dispatcher` when its fetch reads the
|
|
347
|
+
// init, which is the first moment its global dispatcher is certain to exist, so it is a getter.
|
|
348
|
+
// undici types the option as its whole `Dispatcher` class, which the delegate is not.
|
|
349
|
+
const init = {
|
|
209
350
|
method: 'POST',
|
|
210
351
|
headers: { 'content-type': 'application/json', ...headers },
|
|
211
352
|
body: payload,
|
|
212
353
|
redirect: 'manual',
|
|
213
354
|
signal,
|
|
214
|
-
|
|
355
|
+
timeout: false,
|
|
356
|
+
get dispatcher() {
|
|
357
|
+
return timersOff();
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
response = await fetch(url, init);
|
|
215
361
|
}
|
|
216
362
|
catch (error) {
|
|
217
363
|
throw lost('request to', error);
|
|
218
364
|
}
|
|
219
|
-
if (!response.ok)
|
|
220
|
-
|
|
221
|
-
const redirect = status >= 300 && status < 400
|
|
222
|
-
? ' (redirects are not followed)'
|
|
223
|
-
: '';
|
|
224
|
-
throw fail('http', `HTTP ${status}${redirect}: ${snippet(await readErrorText(response), MAX_ERROR_BODY, redact)}`, status);
|
|
225
|
-
}
|
|
365
|
+
if (!response.ok)
|
|
366
|
+
throw httpFailure(options, response, await readErrorText(response));
|
|
226
367
|
let reply;
|
|
227
368
|
try {
|
|
228
369
|
reply = await readCapped(response, MAX_REPLY_BYTES);
|