@bastani/pi-ai 0.9.35-alpha.2 → 0.9.35-alpha.4
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/CHANGELOG.md +18 -0
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +62 -27
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/models.d.ts +5 -0
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +1 -0
- package/dist/models.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/dist/providers/data/meta.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/openrouter-api.d.ts +40 -0
- package/dist/providers/openrouter-api.d.ts.map +1 -0
- package/dist/providers/openrouter-api.js +41 -0
- package/dist/providers/openrouter-api.js.map +1 -0
- package/dist/providers/openrouter.d.ts +11 -1
- package/dist/providers/openrouter.d.ts.map +1 -1
- package/dist/providers/openrouter.js +52 -1
- package/dist/providers/openrouter.js.map +1 -1
- package/dist/utils/retry.js +1 -1
- package/dist/utils/retry.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ModelCost } from "../types.js";
|
|
2
|
+
/** OpenRouter's per-token prices, as decimal strings. */
|
|
3
|
+
export interface OpenRouterPricing {
|
|
4
|
+
prompt?: string;
|
|
5
|
+
completion?: string;
|
|
6
|
+
input_cache_read?: string;
|
|
7
|
+
input_cache_write?: string;
|
|
8
|
+
overrides?: OpenRouterPricingOverride[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A conditional price. `min_prompt_tokens` selects prompt-length pricing; `utc_*` fields select
|
|
12
|
+
* time-of-day or weekday pricing. Missing rates keep the base price.
|
|
13
|
+
*/
|
|
14
|
+
export interface OpenRouterPricingOverride {
|
|
15
|
+
min_prompt_tokens?: number;
|
|
16
|
+
utc_start?: number;
|
|
17
|
+
utc_end?: number;
|
|
18
|
+
utc_days?: string[];
|
|
19
|
+
prompt?: string;
|
|
20
|
+
completion?: string;
|
|
21
|
+
input_cache_read?: string;
|
|
22
|
+
input_cache_write?: string;
|
|
23
|
+
}
|
|
24
|
+
/** The fields pi reads from a model in OpenRouter's `GET /models` and `GET /models/user`. */
|
|
25
|
+
export interface OpenRouterModel {
|
|
26
|
+
id: string;
|
|
27
|
+
context_length?: number | null;
|
|
28
|
+
top_provider?: {
|
|
29
|
+
context_length?: number | null;
|
|
30
|
+
max_completion_tokens?: number | null;
|
|
31
|
+
};
|
|
32
|
+
pricing?: OpenRouterPricing;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Convert OpenRouter's $/token prices to pi's $/1M tokens. Missing, invalid and negative base
|
|
36
|
+
* prices are 0. Prompt-length overrides become request-wide tiers; a rate a tier does not list
|
|
37
|
+
* keeps the base price. Time-of-day overrides are skipped because ModelCost cannot express them.
|
|
38
|
+
*/
|
|
39
|
+
export declare function openRouterCost(pricing: OpenRouterPricing | undefined): ModelCost;
|
|
40
|
+
//# sourceMappingURL=openrouter-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openrouter-api.d.ts","sourceRoot":"","sources":["../../src/providers/openrouter-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAiB,MAAM,aAAa,CAAC;AAE5D,yDAAyD;AACzD,MAAM,WAAW,iBAAiB;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,SAAS,CAAC,EAAE,yBAAyB,EAAE,CAAC;CACxC;AAED;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACzC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,6FAA6F;AAC7F,MAAM,WAAW,eAAe;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,YAAY,CAAC,EAAE;QAAE,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,qBAAqB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IACzF,OAAO,CAAC,EAAE,iBAAiB,CAAC;CAC5B;AAYD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,iBAAiB,GAAG,SAAS,GAAG,SAAS,CA2BhF"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert one $/token price to $/1M tokens. 0 means free. Missing, invalid and negative prices use
|
|
3
|
+
* the fallback: OpenRouter reports -1 for router models such as `openrouter/auto`, whose price
|
|
4
|
+
* depends on the model they pick.
|
|
5
|
+
*/
|
|
6
|
+
function perMillion(value, fallback) {
|
|
7
|
+
const perToken = Number.parseFloat(value ?? "");
|
|
8
|
+
return perToken >= 0 ? Number((perToken * 1_000_000).toFixed(6)) : fallback;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Convert OpenRouter's $/token prices to pi's $/1M tokens. Missing, invalid and negative base
|
|
12
|
+
* prices are 0. Prompt-length overrides become request-wide tiers; a rate a tier does not list
|
|
13
|
+
* keeps the base price. Time-of-day overrides are skipped because ModelCost cannot express them.
|
|
14
|
+
*/
|
|
15
|
+
export function openRouterCost(pricing) {
|
|
16
|
+
const base = {
|
|
17
|
+
input: perMillion(pricing?.prompt, 0),
|
|
18
|
+
output: perMillion(pricing?.completion, 0),
|
|
19
|
+
cacheRead: perMillion(pricing?.input_cache_read, 0),
|
|
20
|
+
cacheWrite: perMillion(pricing?.input_cache_write, 0),
|
|
21
|
+
};
|
|
22
|
+
const tiers = (pricing?.overrides ?? []).flatMap((override) => {
|
|
23
|
+
if (override.min_prompt_tokens === undefined ||
|
|
24
|
+
override.utc_start !== undefined ||
|
|
25
|
+
override.utc_end !== undefined ||
|
|
26
|
+
override.utc_days !== undefined) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
return [
|
|
30
|
+
{
|
|
31
|
+
inputTokensAbove: override.min_prompt_tokens,
|
|
32
|
+
input: perMillion(override.prompt, base.input),
|
|
33
|
+
output: perMillion(override.completion, base.output),
|
|
34
|
+
cacheRead: perMillion(override.input_cache_read, base.cacheRead),
|
|
35
|
+
cacheWrite: perMillion(override.input_cache_write, base.cacheWrite),
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
});
|
|
39
|
+
return tiers.length > 0 ? { ...base, tiers } : base;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=openrouter-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openrouter-api.js","sourceRoot":"","sources":["../../src/providers/openrouter-api.ts"],"names":[],"mappings":"AAkCA;;;;GAIG;AACH,SAAS,UAAU,CAAC,KAAyB,EAAE,QAAgB;IAC9D,MAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAChD,OAAO,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC7E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAA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types.js\";\n\n/** OpenRouter's per-token prices, as decimal strings. */\nexport interface OpenRouterPricing {\n\tprompt?: string;\n\tcompletion?: string;\n\tinput_cache_read?: string;\n\tinput_cache_write?: string;\n\toverrides?: OpenRouterPricingOverride[];\n}\n\n/**\n * A conditional price. `min_prompt_tokens` selects prompt-length pricing; `utc_*` fields select\n * time-of-day or weekday pricing. Missing rates keep the base price.\n */\nexport interface OpenRouterPricingOverride {\n\tmin_prompt_tokens?: number;\n\tutc_start?: number;\n\tutc_end?: number;\n\tutc_days?: string[];\n\tprompt?: string;\n\tcompletion?: string;\n\tinput_cache_read?: string;\n\tinput_cache_write?: string;\n}\n\n/** The fields pi reads from a model in OpenRouter's `GET /models` and `GET /models/user`. */\nexport interface OpenRouterModel {\n\tid: string;\n\tcontext_length?: number | null;\n\ttop_provider?: { context_length?: number | null; max_completion_tokens?: number | null };\n\tpricing?: OpenRouterPricing;\n}\n\n/**\n * Convert one $/token price to $/1M tokens. 0 means free. Missing, invalid and negative prices use\n * the fallback: OpenRouter reports -1 for router models such as `openrouter/auto`, whose price\n * depends on the model they pick.\n */\nfunction perMillion(value: string | undefined, fallback: number): number {\n\tconst perToken = Number.parseFloat(value ?? \"\");\n\treturn perToken >= 0 ? Number((perToken * 1_000_000).toFixed(6)) : fallback;\n}\n\n/**\n * Convert OpenRouter's $/token prices to pi's $/1M tokens. Missing, invalid and negative base\n * prices are 0. Prompt-length overrides become request-wide tiers; a rate a tier does not list\n * keeps the base price. Time-of-day overrides are skipped because ModelCost cannot express them.\n */\nexport function openRouterCost(pricing: OpenRouterPricing | undefined): ModelCost {\n\tconst base = {\n\t\tinput: perMillion(pricing?.prompt, 0),\n\t\toutput: perMillion(pricing?.completion, 0),\n\t\tcacheRead: perMillion(pricing?.input_cache_read, 0),\n\t\tcacheWrite: perMillion(pricing?.input_cache_write, 0),\n\t};\n\tconst tiers = (pricing?.overrides ?? []).flatMap((override): ModelCostTier[] => {\n\t\tif (\n\t\t\toverride.min_prompt_tokens === undefined ||\n\t\t\toverride.utc_start !== undefined ||\n\t\t\toverride.utc_end !== undefined ||\n\t\t\toverride.utc_days !== undefined\n\t\t) {\n\t\t\treturn [];\n\t\t}\n\t\treturn [\n\t\t\t{\n\t\t\t\tinputTokensAbove: override.min_prompt_tokens,\n\t\t\t\tinput: perMillion(override.prompt, base.input),\n\t\t\t\toutput: perMillion(override.completion, base.output),\n\t\t\t\tcacheRead: perMillion(override.input_cache_read, base.cacheRead),\n\t\t\t\tcacheWrite: perMillion(override.input_cache_write, base.cacheWrite),\n\t\t\t},\n\t\t];\n\t});\n\treturn tiers.length > 0 ? { ...base, tiers } : base;\n}\n"]}
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
import { type Provider } from "../models.ts";
|
|
1
|
+
import { type Provider, type RefreshModelsContext } from "../models.ts";
|
|
2
|
+
import type { AnyModel } from "../types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Restrict an OpenRouter catalog to the chat models the key may use, with the key's limits and
|
|
5
|
+
* prices from `GET /models/user`. Image and classifier models are returned unchanged. A missing
|
|
6
|
+
* limit keeps the catalog value. The request uses the provider's configured base URL, so regional
|
|
7
|
+
* endpoints such as `https://us.openrouter.ai/api/v1` apply their region's filtering. When the
|
|
8
|
+
* endpoint rejects the key (401/403) or does not exist (404), for example with a proxy configured
|
|
9
|
+
* in models.json, the catalog is returned unchanged.
|
|
10
|
+
*/
|
|
11
|
+
export declare function personalizeOpenRouterCatalog(catalog: readonly AnyModel[], context: RefreshModelsContext): Promise<readonly AnyModel[]>;
|
|
2
12
|
export declare function openrouterProvider(): Provider<"anthropic-messages" | "openai-completions">;
|
|
3
13
|
//# sourceMappingURL=openrouter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openrouter.d.ts","sourceRoot":"","sources":["../../src/providers/openrouter.ts"],"names":[],"mappings":"AAMA,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"openrouter.d.ts","sourceRoot":"","sources":["../../src/providers/openrouter.ts"],"names":[],"mappings":"AAMA,OAAO,EAAkB,KAAK,QAAQ,EAAE,KAAK,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACxF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAQ5C;;;;;;;GAOG;AACH,wBAAsB,4BAA4B,CACjD,OAAO,EAAE,SAAS,QAAQ,EAAE,EAC5B,OAAO,EAAE,oBAAoB,GAC3B,OAAO,CAAC,SAAS,QAAQ,EAAE,CAAC,CAiC9B;AAED,wBAAgB,kBAAkB,IAAI,QAAQ,CAAC,oBAAoB,GAAG,oBAAoB,CAAC,CA2B1F"}
|
|
@@ -5,12 +5,63 @@ import { typesafeSystemOneApi } from "../api/typesafe-system-one.lazy.js";
|
|
|
5
5
|
import { envApiKeyAuth, lazyOAuth } from "../auth/helpers.js";
|
|
6
6
|
import { loadOpenRouterOAuth } from "../auth/oauth/load.js";
|
|
7
7
|
import { createProvider } from "../models.js";
|
|
8
|
+
import { isModelType } from "../utils/model-operations.js";
|
|
8
9
|
import { OPENROUTER_CLASSIFIER_MODELS, OPENROUTER_IMAGE_MODELS, OPENROUTER_MODELS } from "./openrouter.models.js";
|
|
10
|
+
import { openRouterCost } from "./openrouter-api.js";
|
|
11
|
+
const OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1";
|
|
12
|
+
const OPENROUTER_USER_MODELS_TIMEOUT_MS = 15_000;
|
|
13
|
+
/**
|
|
14
|
+
* Restrict an OpenRouter catalog to the chat models the key may use, with the key's limits and
|
|
15
|
+
* prices from `GET /models/user`. Image and classifier models are returned unchanged. A missing
|
|
16
|
+
* limit keeps the catalog value. The request uses the provider's configured base URL, so regional
|
|
17
|
+
* endpoints such as `https://us.openrouter.ai/api/v1` apply their region's filtering. When the
|
|
18
|
+
* endpoint rejects the key (401/403) or does not exist (404), for example with a proxy configured
|
|
19
|
+
* in models.json, the catalog is returned unchanged.
|
|
20
|
+
*/
|
|
21
|
+
export async function personalizeOpenRouterCatalog(catalog, context) {
|
|
22
|
+
const key = context.credential?.type === "oauth" ? context.credential.access : context.credential?.key;
|
|
23
|
+
if (!key)
|
|
24
|
+
return catalog;
|
|
25
|
+
const response = await fetch(`${(context.baseUrl ?? OPENROUTER_BASE_URL).replace(/\/+$/, "")}/models/user`, {
|
|
26
|
+
headers: { accept: "application/json", authorization: `Bearer ${key}` },
|
|
27
|
+
signal: AbortSignal.any([context.signal, AbortSignal.timeout(OPENROUTER_USER_MODELS_TIMEOUT_MS)]),
|
|
28
|
+
});
|
|
29
|
+
if (response.status === 401 || response.status === 403 || response.status === 404)
|
|
30
|
+
return catalog;
|
|
31
|
+
if (!response.ok)
|
|
32
|
+
throw new Error(`OpenRouter model list request failed: ${response.status}`);
|
|
33
|
+
const body = await response.json();
|
|
34
|
+
if (typeof body !== "object" || body === null || !("data" in body) || !Array.isArray(body.data)) {
|
|
35
|
+
throw new Error("Invalid OpenRouter model list response");
|
|
36
|
+
}
|
|
37
|
+
const allowed = new Map();
|
|
38
|
+
for (const entry of body.data) {
|
|
39
|
+
if (typeof entry === "object" && entry !== null && "id" in entry && typeof entry.id === "string") {
|
|
40
|
+
allowed.set(entry.id, entry);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return catalog.flatMap((model) => {
|
|
44
|
+
if (!isModelType(model, "chat"))
|
|
45
|
+
return [model];
|
|
46
|
+
const entry = allowed.get(model.id);
|
|
47
|
+
if (!entry)
|
|
48
|
+
return [];
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
...model,
|
|
52
|
+
// `||`: OpenRouter reports a missing limit as absent, null or 0, like the generator's fallback.
|
|
53
|
+
contextWindow: entry.top_provider?.context_length || entry.context_length || model.contextWindow,
|
|
54
|
+
maxTokens: entry.top_provider?.max_completion_tokens || model.maxTokens,
|
|
55
|
+
cost: entry.pricing ? openRouterCost(entry.pricing) : model.cost,
|
|
56
|
+
},
|
|
57
|
+
];
|
|
58
|
+
});
|
|
59
|
+
}
|
|
9
60
|
export function openrouterProvider() {
|
|
10
61
|
return createProvider({
|
|
11
62
|
id: "openrouter",
|
|
12
63
|
name: "OpenRouter",
|
|
13
|
-
baseUrl:
|
|
64
|
+
baseUrl: OPENROUTER_BASE_URL,
|
|
14
65
|
auth: {
|
|
15
66
|
apiKey: envApiKeyAuth("OpenRouter API key", ["OPENROUTER_API_KEY"]),
|
|
16
67
|
oauth: lazyOAuth({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openrouter.js","sourceRoot":"","sources":["../../src/providers/openrouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,cAAc,
|
|
1
|
+
{"version":3,"file":"openrouter.js","sourceRoot":"","sources":["../../src/providers/openrouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAA4C,MAAM,cAAc,CAAC;AAExF,OAAO,EAAE,WAAW,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,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB;IACjC,OAAO,cAAc,CAA8C;QAClE,EAAE,EAAE,YAAY;QAChB,IAAI,EAAE,YAAY;QAClB,OAAO,EAAE,mBAAmB;QAC5B,IAAI,EAAE;YACL,MAAM,EAAE,aAAa,CAAC,oBAAoB,EAAE,CAAC,oBAAoB,CAAC,CAAC;YACnE,KAAK,EAAE,SAAS,CAAC;gBAChB,IAAI,EAAE,kBAAkB;gBACxB,kBAAkB,EAAE,IAAI;gBACxB,UAAU,EAAE,yBAAyB;gBACrC,IAAI,EAAE,mBAAmB;aACzB,CAAC;SACF;QACD,MAAM,EAAE;YACP,GAAG,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC;YACnC,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC;YACzC,GAAG,MAAM,CAAC,MAAM,CAAC,4BAA4B,CAAC;SAC9C;QACD,GAAG,EAAE;YACJ,oBAAoB,EAAE,oBAAoB,EAAE;YAC5C,oBAAoB,EAAE,oBAAoB,EAAE;SAC5C;QACD,MAAM,EAAE,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,EAAE;QACtD,yEAAyE;QACzE,WAAW,EAAE,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,EAAE;KAC9D,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { anthropicMessagesApi } from \"../api/anthropic-messages.lazy.ts\";\nimport { openAICompletionsApi } from \"../api/openai-completions.lazy.ts\";\nimport { openrouterImagesApi } from \"../api/openrouter-images.lazy.ts\";\nimport { typesafeSystemOneApi } from \"../api/typesafe-system-one.lazy.ts\";\nimport { envApiKeyAuth, lazyOAuth } from \"../auth/helpers.ts\";\nimport { loadOpenRouterOAuth } from \"../auth/oauth/load.ts\";\nimport { createProvider, type Provider, type RefreshModelsContext } from \"../models.ts\";\nimport type { AnyModel } from \"../types.ts\";\nimport { isModelType } from \"../utils/model-operations.ts\";\nimport { OPENROUTER_CLASSIFIER_MODELS, OPENROUTER_IMAGE_MODELS, OPENROUTER_MODELS } from \"./openrouter.models.ts\";\nimport { type OpenRouterModel, openRouterCost } from \"./openrouter-api.ts\";\n\nconst OPENROUTER_BASE_URL = \"https://openrouter.ai/api/v1\";\nconst OPENROUTER_USER_MODELS_TIMEOUT_MS = 15_000;\n\n/**\n * Restrict an OpenRouter catalog to the chat models the key may use, with the key's limits and\n * prices from `GET /models/user`. Image and classifier models are returned unchanged. A missing\n * limit keeps the catalog value. The request uses the provider's configured base URL, so regional\n * endpoints such as `https://us.openrouter.ai/api/v1` apply their region's filtering. When the\n * endpoint rejects the key (401/403) or does not exist (404), for example with a proxy configured\n * in models.json, the catalog is returned unchanged.\n */\nexport async function personalizeOpenRouterCatalog(\n\tcatalog: readonly AnyModel[],\n\tcontext: RefreshModelsContext,\n): Promise<readonly AnyModel[]> {\n\tconst key = context.credential?.type === \"oauth\" ? context.credential.access : context.credential?.key;\n\tif (!key) return catalog;\n\tconst response = await fetch(`${(context.baseUrl ?? OPENROUTER_BASE_URL).replace(/\\/+$/, \"\")}/models/user`, {\n\t\theaders: { accept: \"application/json\", authorization: `Bearer ${key}` },\n\t\tsignal: AbortSignal.any([context.signal, AbortSignal.timeout(OPENROUTER_USER_MODELS_TIMEOUT_MS)]),\n\t});\n\tif (response.status === 401 || response.status === 403 || response.status === 404) return catalog;\n\tif (!response.ok) throw new Error(`OpenRouter model list request failed: ${response.status}`);\n\tconst body: unknown = await response.json();\n\tif (typeof body !== \"object\" || body === null || !(\"data\" in body) || !Array.isArray(body.data)) {\n\t\tthrow new Error(\"Invalid OpenRouter model list response\");\n\t}\n\tconst allowed = new Map<string, OpenRouterModel>();\n\tfor (const entry of body.data as unknown[]) {\n\t\tif (typeof entry === \"object\" && entry !== null && \"id\" in entry && typeof entry.id === \"string\") {\n\t\t\tallowed.set(entry.id, entry as OpenRouterModel);\n\t\t}\n\t}\n\treturn catalog.flatMap((model): AnyModel[] => {\n\t\tif (!isModelType(model, \"chat\")) return [model];\n\t\tconst entry = allowed.get(model.id);\n\t\tif (!entry) return [];\n\t\treturn [\n\t\t\t{\n\t\t\t\t...model,\n\t\t\t\t// `||`: OpenRouter reports a missing limit as absent, null or 0, like the generator's fallback.\n\t\t\t\tcontextWindow: entry.top_provider?.context_length || entry.context_length || model.contextWindow,\n\t\t\t\tmaxTokens: entry.top_provider?.max_completion_tokens || model.maxTokens,\n\t\t\t\tcost: entry.pricing ? openRouterCost(entry.pricing) : model.cost,\n\t\t\t},\n\t\t];\n\t});\n}\n\nexport function openrouterProvider(): Provider<\"anthropic-messages\" | \"openai-completions\"> {\n\treturn createProvider<\"anthropic-messages\" | \"openai-completions\">({\n\t\tid: \"openrouter\",\n\t\tname: \"OpenRouter\",\n\t\tbaseUrl: OPENROUTER_BASE_URL,\n\t\tauth: {\n\t\t\tapiKey: envApiKeyAuth(\"OpenRouter API key\", [\"OPENROUTER_API_KEY\"]),\n\t\t\toauth: lazyOAuth({\n\t\t\t\tname: \"OpenRouter OAuth\",\n\t\t\t\tusesCallbackServer: true,\n\t\t\t\tloginLabel: \"Sign in with OpenRouter\",\n\t\t\t\tload: loadOpenRouterOAuth,\n\t\t\t}),\n\t\t},\n\t\tmodels: [\n\t\t\t...Object.values(OPENROUTER_MODELS),\n\t\t\t...Object.values(OPENROUTER_IMAGE_MODELS),\n\t\t\t...Object.values(OPENROUTER_CLASSIFIER_MODELS),\n\t\t],\n\t\tapi: {\n\t\t\t\"anthropic-messages\": anthropicMessagesApi(),\n\t\t\t\"openai-completions\": openAICompletionsApi(),\n\t\t},\n\t\timages: { \"openrouter-images\": openrouterImagesApi() },\n\t\t// OpenRouter serves TypeSafe's System One protocol at /api/v1/systemone.\n\t\tclassifiers: { \"typesafe-system-one\": typesafeSystemOneApi() },\n\t});\n}\n"]}
|
package/dist/utils/retry.js
CHANGED
|
@@ -39,7 +39,7 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
|
|
|
39
39
|
"internal.?error",
|
|
40
40
|
// Wrapper/provider text for transient upstream failures, including OpenRouter
|
|
41
41
|
// "Provider returned error" responses (#2264).
|
|
42
|
-
"provider.?returned.?error",
|
|
42
|
+
"provider.?returned.?(?:an.?)?error",
|
|
43
43
|
"exceeded request buffer limit while retrying upstream",
|
|
44
44
|
// Network, proxy, and fetch transport failures. This includes OpenAI Codex
|
|
45
45
|
// raw-fetch failures such as "upstream connect", "connection refused", and
|
package/dist/utils/retry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":"AAEA,SAAS,yBAAyB,CAAC,QAA2B;IAC7D,OAAO,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,0CAA0C,GAAG,yBAAyB,CAAC;IAC5E,8EAA8E;IAC9E,2EAA2E;IAC3E,mBAAmB;IACnB,qBAAqB;IAErB,6EAA6E;IAC7E,yDAAyD;IACzD,6BAA6B;IAC7B,mBAAmB;IAEnB,4EAA4E;IAC5E,4EAA4E;IAC5E,oBAAoB;IACpB,eAAe;IACf,gBAAgB;IAChB,SAAS;IACT,2CAA2C;CAC3C,CAAC,CAAC;AAEH,MAAM,gCAAgC,GAAG,yBAAyB,CAAC;IAClE,0EAA0E;IAC1E,YAAY;IACZ,aAAa;IACb,4BAA4B;IAC5B,oCAAoC;IACpC,sBAAsB;IACtB,aAAa;IACb,mBAAmB;IACnB,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,sBAAsB;IACtB,eAAe;IACf,iBAAiB;IAEjB,8EAA8E;IAC9E,+CAA+C;IAC/C,2BAA2B;IAC3B,uDAAuD;IAEvD,2EAA2E;IAC3E,2EAA2E;IAC3E,2EAA2E;IAC3E,gBAAgB;IAChB,mBAAmB;IACnB,qBAAq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OAAyB;IAClE,IAAI,OAAO,CAAC,UAAU,KAAK,OAAO,IAAI,CAAC,OAAO,CAAC,YAAY;QAAE,OAAO,KAAK,CAAC;IAC1E,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAC1C,IAAI,0CAA0C,CAAC,IAAI,CAAC,YAAY,CAAC;QAAE,OAAO,KAAK,CAAC;IAChF,OAAO,gCAAgC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["import type { AssistantMessage } from \"../types.ts\";\n\nfunction buildProviderErrorPattern(patterns: readonly string[]): RegExp {\n\treturn new RegExp(patterns.join(\"|\"), \"i\");\n}\n\nconst NON_RETRYABLE_PROVIDER_LIMIT_ERROR_PATTERN = buildProviderErrorPattern([\n\t// OpenCode Go/free-tier limits returned as 429 JSON error types by OpenCode's\n\t// Zen API. These are subscription/account limits, not transient throttles.\n\t\"GoUsageLimitError\",\n\t\"FreeUsageLimitError\",\n\n\t// OpenCode Go subscription-limit text asks users to enable available-balance\n\t// usage after rolling/weekly/monthly limits are reached.\n\t\"Monthly usage limit reached\",\n\t\"available balance\",\n\n\t// Generic quota/budget/billing exhaustion. `insufficient_quota` is OpenAI's\n\t// quota/billing error code; the other strings cover common gateway wording.\n\t\"insufficient_quota\",\n\t\"out of budget\",\n\t\"quota exceeded\",\n\t\"billing\",\n\t\"subscription_sharing_usage_limit_exceeded\",\n]);\n\nconst RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([\n\t// Generic provider load, HTTP status, and server-side transient failures.\n\t\"overloaded\",\n\t\"server_busy\",\n\t\"servers are currently busy\",\n\t\"currently experiencing high demand\",\n\t\"model is at capacity\",\n\t\"rate.?limit\",\n\t\"too many requests\",\n\t\"429\",\n\t\"500\",\n\t\"502\",\n\t\"503\",\n\t\"504\",\n\t\"520\",\n\t\"524\",\n\t\"service.?unavailable\",\n\t\"server.?error\",\n\t\"internal.?error\",\n\n\t// Wrapper/provider text for transient upstream failures, including OpenRouter\n\t// \"Provider returned error\" responses (#2264).\n\t\"provider.?returned.?error\",\n\t\"exceeded request buffer limit while retrying upstream\",\n\n\t// Network, proxy, and fetch transport failures. This includes OpenAI Codex\n\t// raw-fetch failures such as \"upstream connect\", \"connection refused\", and\n\t// \"reset before headers\" (#733), plus OpenRouter connection drops (#3317).\n\t\"network.?error\",\n\t\"connection.?error\",\n\t\"connection.?refused\",\n\t\"connection.?lost\",\n\t\"other side closed\",\n\t\"fetch failed\",\n\t\"getaddrinfo\",\n\t\"ENOTFOUND\",\n\t\"EAI_AGAIN\",\n\t\"upstream.?connect\",\n\t\"reset before headers\",\n\t\"socket hang up\",\n\t\"socket connection was closed\",\n\t\"timed? out\",\n\t\"timeout\",\n\t\"terminated\",\n\n\t// WebSocket transports can report close/error text instead of HTTP/fetch text.\n\t\"websocket.?closed\",\n\t\"websocket.?error\",\n\n\t// Response-body decompression failures. Node/Bun zlib rejects a body whose\n\t// Content-Encoding lied or whose compressed bytes were corrupted, and the\n\t// fetch/WebSocket layer wraps it with a \"Library error:\" prefix. GitHub\n\t// Copilot on the default `transport: \"auto\"` reports this as\n\t// \"Library error: zlib error: incorrect header check\" (#2553). The transfer\n\t// is already unusable, and a fresh attempt normally succeeds, so treat every\n\t// variant as a transient transport failure rather than a deterministic error.\n\t\"zlib\",\n\t\"incorrect header check\",\n\t\"decompress\",\n\t\"library error\",\n\n\t// Idle-stream deadline enforced below the HTTP layer by `withStreamDeadline`\n\t// so a stream that never settles cannot hang an attempt forever (#2553).\n\t\"stream deadline exceeded\",\n\n\t// Premature stream endings from SDKs and transports. Anthropic can throw\n\t// \"stream ended without ...\" and \"Anthropic stream ended before message_stop\"\n\t// (#4433); Bedrock/Smithy can throw an HTTP/2 no-response error (#3594).\n\t\"ended without\",\n\t\"stream ended before message_stop\",\n\t\"stream ended before a terminal response event\",\n\t\"http2 request did not get a response\",\n\t\"pending stream has been canceled\",\n\n\t// Provider-requested retry delay cap failures should flow through the outer\n\t// retry policy so callers can surface/abort the backoff (#1123).\n\t\"retry delay\",\n\n\t// Explicit retry guidance emitted mid-stream by OpenAI Responses and Bedrock\n\t// stream exceptions (#6019).\n\t\"you can retry your request\",\n\t\"try your request again\",\n\t\"please retry your request\",\n\n\t// gRPC based providers (e.g. NVIDIA NIM)\n\t\"ResourceExhausted\",\n\t\"subscription_sharing_usage_unavailable\",\n\t\"subscription_sharing_user_unavailable\",\n]);\n\n/**\n * Retry policy: bounded attempts with exponential backoff (`baseDelayMs * 2^(attempt-1)`).\n * Each delay is capped by `maxAgentDelayMs` (60 seconds by default).\n * Matches `settings.retry` (`enabled`, `maxRetries`, `baseDelayMs`) in coding-agent; kept\n * here so the classifier and the policy-driven retry loop live together and stay reusable\n * by the SDK and other callers.\n */\nexport interface RetryPolicy {\n\tenabled: boolean;\n\t/** Max retry attempts (0 = no retries). The initial call never counts as a retry. */\n\tmaxRetries: number;\n\t/** Base delay in ms. Per-attempt delay is `baseDelayMs * 2^(attempt-1)` before jitter. */\n\tbaseDelayMs: number;\n\t/** Cap for agent-level retry delays in ms. Defaults to 60 seconds. */\n\tmaxAgentDelayMs?: number;\n}\n\nexport const DEFAULT_MAX_AGENT_RETRY_DELAY_MS = 60_000;\n\nexport function retryDelayMs(policy: Pick<RetryPolicy, \"baseDelayMs\" | \"maxAgentDelayMs\">, attempt: number): number {\n\tconst delay = policy.baseDelayMs * 2 ** Math.max(0, attempt - 1);\n\tconst safeDelay = Number.isSafeInteger(delay) ? delay : Number.MAX_SAFE_INTEGER;\n\treturn Math.min(safeDelay, policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS);\n}\n\n/** Optional callbacks emitted by {@link retryAssistantCall} around each retry. */\nexport interface RetryCallbacks {\n\t/** Emitted before the backoff sleep of each retry attempt (1-indexed). */\n\tonRetryScheduled?: (\n\t\tattempt: number,\n\t\tmaxAttempts: number,\n\t\tdelayMs: number,\n\t\terrorMessage: string,\n\t) => void | Promise<void>;\n\t/** Emitted after the backoff sleep, immediately before the retried call starts. */\n\tonRetryAttemptStart?: () => void | Promise<void>;\n\t/** Emitted once when the loop ends: success if a later call completed normally. */\n\tonRetryFinished?: (success: boolean, attempt: number, finalError?: string) => void | Promise<void>;\n}\n\nclass RetrySleepAbortError extends Error {\n\tconstructor() {\n\t\tsuper(\"Aborted\");\n\t}\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n\treturn new Promise((resolve, reject) => {\n\t\tif (signal?.aborted) {\n\t\t\treject(new RetrySleepAbortError());\n\t\t\treturn;\n\t\t}\n\t\tconst timeout = setTimeout(resolve, ms);\n\t\tsignal?.addEventListener(\n\t\t\t\"abort\",\n\t\t\t() => {\n\t\t\t\tclearTimeout(timeout);\n\t\t\t\treject(new RetrySleepAbortError());\n\t\t\t},\n\t\t\t{ once: true },\n\t\t);\n\t});\n}\n\n/**\n * Run a single assistant-producing call with bounded retry on transient errors.\n *\n * Behavior:\n * - A successful response is returned immediately. Aborts are terminal and never\n * retried, but reported as unsuccessful if they happen after a retry was scheduled.\n * Aborts during the backoff sleep are normalized to an aborted `AssistantMessage`\n * too, so callers do not need to care when cancellation happened.\n * - A non-retryable error (per {@link isRetryableAssistantError}, including quota/\n * billing exhaustion) is returned immediately so deterministic errors fail fast.\n * - Otherwise retries up to `maxRetries` times with exponential backoff, emitting\n * `onRetryScheduled` before each sleep, `onRetryAttemptStart` after each sleep before\n * the retried call starts, and `onRetryFinished` once at the end (whether the loop\n * ends in success, exhausted retries, or an aborted backoff).\n *\n * When `policy` is undefined or disabled, the first response is returned unchanged\n * (equivalent to calling `produce()` directly).\n */\nexport async function retryAssistantCall(\n\tproduce: () => Promise<AssistantMessage>,\n\tpolicy: RetryPolicy | undefined,\n\tsignal: AbortSignal | undefined,\n\tcallbacks?: RetryCallbacks,\n): Promise<AssistantMessage> {\n\tconst maxAttempts = policy?.enabled ? policy.maxRetries : 0;\n\n\tlet attempt = 0;\n\tlet lastRetry: { attempt: number; errorMessage: string } | undefined;\n\tfor (;;) {\n\t\tconst response = await produce();\n\n\t\t// Abort: terminal but not successful. Never retry an aborted message.\n\t\tif (response.stopReason === \"aborted\") {\n\t\t\tif (lastRetry) await callbacks?.onRetryFinished?.(false, lastRetry.attempt);\n\t\t\treturn response;\n\t\t}\n\n\t\t// Success: non-error, non-abort responses return as-is.\n\t\tif (response.stopReason !== \"error\") {\n\t\t\tif (lastRetry) await callbacks?.onRetryFinished?.(true, lastRetry.attempt);\n\t\t\treturn response;\n\t\t}\n\n\t\t// Non-retryable, or budget exhausted: return the final error message.\n\t\tif (attempt >= maxAttempts || !isRetryableAssistantError(response)) {\n\t\t\tif (lastRetry) await callbacks?.onRetryFinished?.(false, lastRetry.attempt, response.errorMessage);\n\t\t\treturn response;\n\t\t}\n\n\t\tattempt++;\n\t\tlastRetry = { attempt, errorMessage: response.errorMessage || \"Unknown error\" };\n\t\tconst delayMs = retryDelayMs(policy!, attempt);\n\t\tawait callbacks?.onRetryScheduled?.(attempt, maxAttempts, delayMs, lastRetry.errorMessage);\n\n\t\t// Normalize aborts during retry backoff to the same AssistantMessage shape as\n\t\t// provider stream aborts, so callers do not need to care when cancellation happened.\n\t\ttry {\n\t\t\tawait sleep(delayMs, signal);\n\t\t} catch (error) {\n\t\t\tawait callbacks?.onRetryFinished?.(false, attempt, lastRetry.errorMessage);\n\t\t\tif (error instanceof RetrySleepAbortError) {\n\t\t\t\treturn { ...response, stopReason: \"aborted\", errorMessage: undefined };\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t\tawait callbacks?.onRetryAttemptStart?.();\n\t}\n}\n\n/**\n * Classifies whether a failed assistant message looks like a transient provider\n * or transport error, so callers can decide if the last assistant turn should be\n * restarted.\n *\n * This does not implement retry policy. Callers should first handle context\n * overflow separately, then apply their own retry budget, backoff, and reporting\n * before restarting the assistant turn.\n */\nexport function isRetryableAssistantError(message: AssistantMessage): boolean {\n\tif (message.stopReason !== \"error\" || !message.errorMessage) return false;\n\tconst errorMessage = message.errorMessage;\n\tif (NON_RETRYABLE_PROVIDER_LIMIT_ERROR_PATTERN.test(errorMessage)) return false;\n\treturn RETRYABLE_PROVIDER_ERROR_PATTERN.test(errorMessage);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":"AAEA,SAAS,yBAAyB,CAAC,QAA2B;IAC7D,OAAO,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,0CAA0C,GAAG,yBAAyB,CAAC;IAC5E,8EAA8E;IAC9E,2EAA2E;IAC3E,mBAAmB;IACnB,qBAAqB;IAErB,6EAA6E;IAC7E,yDAAyD;IACzD,6BAA6B;IAC7B,mBAAmB;IAEnB,4EAA4E;IAC5E,4EAA4E;IAC5E,oBAAoB;IACpB,eAAe;IACf,gBAAgB;IAChB,SAAS;IACT,2CAA2C;CAC3C,CAAC,CAAC;AAEH,MAAM,gCAAgC,GAAG,yBAAyB,CAAC;IAClE,0EAA0E;IAC1E,YAAY;IACZ,aAAa;IACb,4BAA4B;IAC5B,oCAAoC;IACpC,sBAAsB;IACtB,aAAa;IACb,mBAAmB;IACnB,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,sBAAsB;IACtB,eAAe;IACf,iBAAiB;IAEjB,8EAA8E;IAC9E,+CAA+C;IAC/C,oCAAoC;IACpC,uDAAuD;IAEvD,2EAA2E;IAC3E,2EAA2E;IAC3E,2EAA2E;IAC3E,gBAAgB;IAChB,mBAAmB;IACnB,qBAAqB;IACrB,kBAAkB;IAClB,mBAAmB;IACnB,cAAc;IACd,aAAa;IACb,WAAW;IACX,WAAW;IACX,mBAAmB;IACnB,sBAAsB;IACtB,gBAAgB;IAChB,8BAA8B;IAC9B,YAAY;IACZ,SAAS;IACT,YAAY;IAEZ,+EAA+E;IAC/E,mBAAmB;IACnB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QAAE,OAAO,KAAK,CAAC;IAChF,OAAO,gCAAgC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["import type { AssistantMessage } from \"../types.ts\";\n\nfunction buildProviderErrorPattern(patterns: readonly string[]): RegExp {\n\treturn new RegExp(patterns.join(\"|\"), \"i\");\n}\n\nconst NON_RETRYABLE_PROVIDER_LIMIT_ERROR_PATTERN = buildProviderErrorPattern([\n\t// OpenCode Go/free-tier limits returned as 429 JSON error types by OpenCode's\n\t// Zen API. These are subscription/account limits, not transient throttles.\n\t\"GoUsageLimitError\",\n\t\"FreeUsageLimitError\",\n\n\t// OpenCode Go subscription-limit text asks users to enable available-balance\n\t// usage after rolling/weekly/monthly limits are reached.\n\t\"Monthly usage limit reached\",\n\t\"available balance\",\n\n\t// Generic quota/budget/billing exhaustion. `insufficient_quota` is OpenAI's\n\t// quota/billing error code; the other strings cover common gateway wording.\n\t\"insufficient_quota\",\n\t\"out of budget\",\n\t\"quota exceeded\",\n\t\"billing\",\n\t\"subscription_sharing_usage_limit_exceeded\",\n]);\n\nconst RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([\n\t// Generic provider load, HTTP status, and server-side transient failures.\n\t\"overloaded\",\n\t\"server_busy\",\n\t\"servers are currently busy\",\n\t\"currently experiencing high demand\",\n\t\"model is at capacity\",\n\t\"rate.?limit\",\n\t\"too many requests\",\n\t\"429\",\n\t\"500\",\n\t\"502\",\n\t\"503\",\n\t\"504\",\n\t\"520\",\n\t\"524\",\n\t\"service.?unavailable\",\n\t\"server.?error\",\n\t\"internal.?error\",\n\n\t// Wrapper/provider text for transient upstream failures, including OpenRouter\n\t// \"Provider returned error\" responses (#2264).\n\t\"provider.?returned.?(?:an.?)?error\",\n\t\"exceeded request buffer limit while retrying upstream\",\n\n\t// Network, proxy, and fetch transport failures. This includes OpenAI Codex\n\t// raw-fetch failures such as \"upstream connect\", \"connection refused\", and\n\t// \"reset before headers\" (#733), plus OpenRouter connection drops (#3317).\n\t\"network.?error\",\n\t\"connection.?error\",\n\t\"connection.?refused\",\n\t\"connection.?lost\",\n\t\"other side closed\",\n\t\"fetch failed\",\n\t\"getaddrinfo\",\n\t\"ENOTFOUND\",\n\t\"EAI_AGAIN\",\n\t\"upstream.?connect\",\n\t\"reset before headers\",\n\t\"socket hang up\",\n\t\"socket connection was closed\",\n\t\"timed? out\",\n\t\"timeout\",\n\t\"terminated\",\n\n\t// WebSocket transports can report close/error text instead of HTTP/fetch text.\n\t\"websocket.?closed\",\n\t\"websocket.?error\",\n\n\t// Response-body decompression failures. Node/Bun zlib rejects a body whose\n\t// Content-Encoding lied or whose compressed bytes were corrupted, and the\n\t// fetch/WebSocket layer wraps it with a \"Library error:\" prefix. GitHub\n\t// Copilot on the default `transport: \"auto\"` reports this as\n\t// \"Library error: zlib error: incorrect header check\" (#2553). The transfer\n\t// is already unusable, and a fresh attempt normally succeeds, so treat every\n\t// variant as a transient transport failure rather than a deterministic error.\n\t\"zlib\",\n\t\"incorrect header check\",\n\t\"decompress\",\n\t\"library error\",\n\n\t// Idle-stream deadline enforced below the HTTP layer by `withStreamDeadline`\n\t// so a stream that never settles cannot hang an attempt forever (#2553).\n\t\"stream deadline exceeded\",\n\n\t// Premature stream endings from SDKs and transports. Anthropic can throw\n\t// \"stream ended without ...\" and \"Anthropic stream ended before message_stop\"\n\t// (#4433); Bedrock/Smithy can throw an HTTP/2 no-response error (#3594).\n\t\"ended without\",\n\t\"stream ended before message_stop\",\n\t\"stream ended before a terminal response event\",\n\t\"http2 request did not get a response\",\n\t\"pending stream has been canceled\",\n\n\t// Provider-requested retry delay cap failures should flow through the outer\n\t// retry policy so callers can surface/abort the backoff (#1123).\n\t\"retry delay\",\n\n\t// Explicit retry guidance emitted mid-stream by OpenAI Responses and Bedrock\n\t// stream exceptions (#6019).\n\t\"you can retry your request\",\n\t\"try your request again\",\n\t\"please retry your request\",\n\n\t// gRPC based providers (e.g. NVIDIA NIM)\n\t\"ResourceExhausted\",\n\t\"subscription_sharing_usage_unavailable\",\n\t\"subscription_sharing_user_unavailable\",\n]);\n\n/**\n * Retry policy: bounded attempts with exponential backoff (`baseDelayMs * 2^(attempt-1)`).\n * Each delay is capped by `maxAgentDelayMs` (60 seconds by default).\n * Matches `settings.retry` (`enabled`, `maxRetries`, `baseDelayMs`) in coding-agent; kept\n * here so the classifier and the policy-driven retry loop live together and stay reusable\n * by the SDK and other callers.\n */\nexport interface RetryPolicy {\n\tenabled: boolean;\n\t/** Max retry attempts (0 = no retries). The initial call never counts as a retry. */\n\tmaxRetries: number;\n\t/** Base delay in ms. Per-attempt delay is `baseDelayMs * 2^(attempt-1)` before jitter. */\n\tbaseDelayMs: number;\n\t/** Cap for agent-level retry delays in ms. Defaults to 60 seconds. */\n\tmaxAgentDelayMs?: number;\n}\n\nexport const DEFAULT_MAX_AGENT_RETRY_DELAY_MS = 60_000;\n\nexport function retryDelayMs(policy: Pick<RetryPolicy, \"baseDelayMs\" | \"maxAgentDelayMs\">, attempt: number): number {\n\tconst delay = policy.baseDelayMs * 2 ** Math.max(0, attempt - 1);\n\tconst safeDelay = Number.isSafeInteger(delay) ? delay : Number.MAX_SAFE_INTEGER;\n\treturn Math.min(safeDelay, policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS);\n}\n\n/** Optional callbacks emitted by {@link retryAssistantCall} around each retry. */\nexport interface RetryCallbacks {\n\t/** Emitted before the backoff sleep of each retry attempt (1-indexed). */\n\tonRetryScheduled?: (\n\t\tattempt: number,\n\t\tmaxAttempts: number,\n\t\tdelayMs: number,\n\t\terrorMessage: string,\n\t) => void | Promise<void>;\n\t/** Emitted after the backoff sleep, immediately before the retried call starts. */\n\tonRetryAttemptStart?: () => void | Promise<void>;\n\t/** Emitted once when the loop ends: success if a later call completed normally. */\n\tonRetryFinished?: (success: boolean, attempt: number, finalError?: string) => void | Promise<void>;\n}\n\nclass RetrySleepAbortError extends Error {\n\tconstructor() {\n\t\tsuper(\"Aborted\");\n\t}\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n\treturn new Promise((resolve, reject) => {\n\t\tif (signal?.aborted) {\n\t\t\treject(new RetrySleepAbortError());\n\t\t\treturn;\n\t\t}\n\t\tconst timeout = setTimeout(resolve, ms);\n\t\tsignal?.addEventListener(\n\t\t\t\"abort\",\n\t\t\t() => {\n\t\t\t\tclearTimeout(timeout);\n\t\t\t\treject(new RetrySleepAbortError());\n\t\t\t},\n\t\t\t{ once: true },\n\t\t);\n\t});\n}\n\n/**\n * Run a single assistant-producing call with bounded retry on transient errors.\n *\n * Behavior:\n * - A successful response is returned immediately. Aborts are terminal and never\n * retried, but reported as unsuccessful if they happen after a retry was scheduled.\n * Aborts during the backoff sleep are normalized to an aborted `AssistantMessage`\n * too, so callers do not need to care when cancellation happened.\n * - A non-retryable error (per {@link isRetryableAssistantError}, including quota/\n * billing exhaustion) is returned immediately so deterministic errors fail fast.\n * - Otherwise retries up to `maxRetries` times with exponential backoff, emitting\n * `onRetryScheduled` before each sleep, `onRetryAttemptStart` after each sleep before\n * the retried call starts, and `onRetryFinished` once at the end (whether the loop\n * ends in success, exhausted retries, or an aborted backoff).\n *\n * When `policy` is undefined or disabled, the first response is returned unchanged\n * (equivalent to calling `produce()` directly).\n */\nexport async function retryAssistantCall(\n\tproduce: () => Promise<AssistantMessage>,\n\tpolicy: RetryPolicy | undefined,\n\tsignal: AbortSignal | undefined,\n\tcallbacks?: RetryCallbacks,\n): Promise<AssistantMessage> {\n\tconst maxAttempts = policy?.enabled ? policy.maxRetries : 0;\n\n\tlet attempt = 0;\n\tlet lastRetry: { attempt: number; errorMessage: string } | undefined;\n\tfor (;;) {\n\t\tconst response = await produce();\n\n\t\t// Abort: terminal but not successful. Never retry an aborted message.\n\t\tif (response.stopReason === \"aborted\") {\n\t\t\tif (lastRetry) await callbacks?.onRetryFinished?.(false, lastRetry.attempt);\n\t\t\treturn response;\n\t\t}\n\n\t\t// Success: non-error, non-abort responses return as-is.\n\t\tif (response.stopReason !== \"error\") {\n\t\t\tif (lastRetry) await callbacks?.onRetryFinished?.(true, lastRetry.attempt);\n\t\t\treturn response;\n\t\t}\n\n\t\t// Non-retryable, or budget exhausted: return the final error message.\n\t\tif (attempt >= maxAttempts || !isRetryableAssistantError(response)) {\n\t\t\tif (lastRetry) await callbacks?.onRetryFinished?.(false, lastRetry.attempt, response.errorMessage);\n\t\t\treturn response;\n\t\t}\n\n\t\tattempt++;\n\t\tlastRetry = { attempt, errorMessage: response.errorMessage || \"Unknown error\" };\n\t\tconst delayMs = retryDelayMs(policy!, attempt);\n\t\tawait callbacks?.onRetryScheduled?.(attempt, maxAttempts, delayMs, lastRetry.errorMessage);\n\n\t\t// Normalize aborts during retry backoff to the same AssistantMessage shape as\n\t\t// provider stream aborts, so callers do not need to care when cancellation happened.\n\t\ttry {\n\t\t\tawait sleep(delayMs, signal);\n\t\t} catch (error) {\n\t\t\tawait callbacks?.onRetryFinished?.(false, attempt, lastRetry.errorMessage);\n\t\t\tif (error instanceof RetrySleepAbortError) {\n\t\t\t\treturn { ...response, stopReason: \"aborted\", errorMessage: undefined };\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t\tawait callbacks?.onRetryAttemptStart?.();\n\t}\n}\n\n/**\n * Classifies whether a failed assistant message looks like a transient provider\n * or transport error, so callers can decide if the last assistant turn should be\n * restarted.\n *\n * This does not implement retry policy. Callers should first handle context\n * overflow separately, then apply their own retry budget, backoff, and reporting\n * before restarting the assistant turn.\n */\nexport function isRetryableAssistantError(message: AssistantMessage): boolean {\n\tif (message.stopReason !== \"error\" || !message.errorMessage) return false;\n\tconst errorMessage = message.errorMessage;\n\tif (NON_RETRYABLE_PROVIDER_LIMIT_ERROR_PATTERN.test(errorMessage)) return false;\n\treturn RETRYABLE_PROVIDER_ERROR_PATTERN.test(errorMessage);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bastani/pi-ai",
|
|
3
|
-
"version": "0.9.35-alpha.
|
|
3
|
+
"version": "0.9.35-alpha.4",
|
|
4
4
|
"description": "Bastani-branded unified LLM API with automatic model discovery and provider configuration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"dependencies": {
|
|
78
78
|
"@anthropic-ai/sdk": "0.129.0",
|
|
79
79
|
"@aws-sdk/client-bedrock-runtime": "3.1127.0",
|
|
80
|
-
"@earendil-works/pi-telemetry": "1.
|
|
80
|
+
"@earendil-works/pi-telemetry": "1.2.0",
|
|
81
81
|
"@google/genai": "2.21.0",
|
|
82
82
|
"@smithy/node-http-handler": "4.12.1",
|
|
83
83
|
"http-proxy-agent": "9.1.0",
|