@bastani/pi-ai 0.9.45 → 0.9.46-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +4 -1
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +2 -0
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/azure-openai-responses.d.ts.map +1 -1
- package/dist/api/azure-openai-responses.js +2 -0
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +25 -1
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +2 -0
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +6 -1
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +2 -0
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/api/mistral-conversations.js +6 -2
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +3 -3
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/pi-messages.d.ts +2 -1
- package/dist/api/pi-messages.d.ts.map +1 -1
- package/dist/api/pi-messages.js +18 -2
- package/dist/api/pi-messages.js.map +1 -1
- package/dist/auth/helpers.d.ts +2 -1
- package/dist/auth/helpers.d.ts.map +1 -1
- package/dist/auth/helpers.js +1 -0
- package/dist/auth/helpers.js.map +1 -1
- package/dist/auth/oauth/github-copilot.d.ts.map +1 -1
- package/dist/auth/oauth/github-copilot.js +20 -18
- package/dist/auth/oauth/github-copilot.js.map +1 -1
- package/dist/auth/oauth/kimi-coding.d.ts.map +1 -1
- package/dist/auth/oauth/kimi-coding.js +4 -22
- package/dist/auth/oauth/kimi-coding.js.map +1 -1
- package/dist/auth/oauth/meta.d.ts.map +1 -1
- package/dist/auth/oauth/meta.js +1 -14
- package/dist/auth/oauth/meta.js.map +1 -1
- package/dist/auth/oauth/radius.d.ts.map +1 -1
- package/dist/auth/oauth/radius.js +9 -3
- package/dist/auth/oauth/radius.js.map +1 -1
- package/dist/auth/oauth/trusted-url.d.ts +7 -0
- package/dist/auth/oauth/trusted-url.d.ts.map +1 -0
- package/dist/auth/oauth/trusted-url.js +12 -0
- package/dist/auth/oauth/trusted-url.js.map +1 -0
- package/dist/auth/types.d.ts +6 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/auth/types.js.map +1 -1
- package/dist/cli.js +6 -2
- package/dist/cli.js.map +1 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +26 -3
- package/dist/models.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/providers/data/azure.json +1 -1
- package/dist/providers/data/github-copilot.json +1 -1
- package/dist/providers/data/huggingface.json +1 -1
- package/dist/providers/data/openai-codex.json +1 -1
- package/dist/providers/data/openai.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/github-copilot.d.ts.map +1 -1
- package/dist/providers/github-copilot.js +13 -1
- package/dist/providers/github-copilot.js.map +1 -1
- package/dist/providers/openai-mid-convo-effort.d.ts.map +1 -1
- package/dist/providers/openai-mid-convo-effort.js +1 -0
- package/dist/providers/openai-mid-convo-effort.js.map +1 -1
- package/dist/utils/provider-retry.d.ts.map +1 -1
- package/dist/utils/provider-retry.js +5 -4
- package/dist/utils/provider-retry.js.map +1 -1
- package/dist/utils/retry-hints.d.ts +6 -3
- package/dist/utils/retry-hints.d.ts.map +1 -1
- package/dist/utils/retry-hints.js +98 -18
- package/dist/utils/retry-hints.js.map +1 -1
- package/dist/utils/retry.js +2 -2
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.d.ts.map +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"github-copilot.d.ts","sourceRoot":"","sources":["../../src/providers/github-copilot.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"github-copilot.d.ts","sourceRoot":"","sources":["../../src/providers/github-copilot.ts"],"names":[],"mappings":"AAMA,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAc7D,wBAAgB,qBAAqB,IAAI,QAAQ,CAAC,oBAAoB,GAAG,oBAAoB,GAAG,kBAAkB,CAAC,CAoClH"}
|
|
@@ -11,6 +11,10 @@ function advertisedModelIds(value) {
|
|
|
11
11
|
return undefined;
|
|
12
12
|
return new Set(value);
|
|
13
13
|
}
|
|
14
|
+
/** A login saved before Copilot model lists were stored, or whose list is malformed, must re-read it. */
|
|
15
|
+
function lacksAccountModelList(credential) {
|
|
16
|
+
return advertisedModelIds(credential.availableModelIds) === undefined;
|
|
17
|
+
}
|
|
14
18
|
export function githubCopilotProvider() {
|
|
15
19
|
return createProvider({
|
|
16
20
|
id: "github-copilot",
|
|
@@ -18,7 +22,12 @@ export function githubCopilotProvider() {
|
|
|
18
22
|
baseUrl: "https://api.individual.githubcopilot.com",
|
|
19
23
|
auth: {
|
|
20
24
|
apiKey: envApiKeyAuth("GitHub Copilot token", ["COPILOT_GITHUB_TOKEN"]),
|
|
21
|
-
oauth: lazyOAuth({
|
|
25
|
+
oauth: lazyOAuth({
|
|
26
|
+
name: "GitHub Copilot",
|
|
27
|
+
isSubscription: true,
|
|
28
|
+
needsRefresh: lacksAccountModelList,
|
|
29
|
+
load: loadGitHubCopilotOAuth,
|
|
30
|
+
}),
|
|
22
31
|
},
|
|
23
32
|
models: Object.values(GITHUB_COPILOT_MODELS),
|
|
24
33
|
filterModels: (models, credential) => {
|
|
@@ -31,6 +40,9 @@ export function githubCopilotProvider() {
|
|
|
31
40
|
// a Copilot-owned model that merely ends in `-fast` is an ordinary picker model.
|
|
32
41
|
if (model.fastRoute !== undefined)
|
|
33
42
|
return entitledFastModelIds?.has(model.id) === true;
|
|
43
|
+
// A login without a stored model list (an older login) keeps the catalog so it is never left
|
|
44
|
+
// with no models; `Models.refresh()` with network access re-reads the list once (see
|
|
45
|
+
// `needsRefresh`), and the account list applies from then on.
|
|
34
46
|
return availableModelIds?.has(model.id) ?? true;
|
|
35
47
|
});
|
|
36
48
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"github-copilot.js","sourceRoot":"","sources":["../../src/providers/github-copilot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"github-copilot.js","sourceRoot":"","sources":["../../src/providers/github-copilot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAiB,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAEnE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AA6F;gBAC7F,iFAAiF;gBACjF,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS;oBAAE,OAAO,oBAAoB,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;gBACvF,6FAA6F;gBAC7F,qFAAqF;gBACrF,8DAA8D;gBAC9D,OAAO,iBAAiB,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC;YACjD,CAAC,CAAC,CAAC;QACJ,CAAC;QACD,GAAG,EAAE;YACJ,oBAAoB,EAAE,oBAAoB,EAAE;YAC5C,oBAAoB,EAAE,oBAAoB,EAAE;YAC5C,kBAAkB,EAAE,kBAAkB,EAAE;SACxC;KACD,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { anthropicMessagesApi } from \"../api/anthropic-messages.lazy.ts\";\nimport { openAICompletionsApi } from \"../api/openai-completions.lazy.ts\";\nimport { openAIResponsesApi } from \"../api/openai-responses.lazy.ts\";\nimport { envApiKeyAuth, lazyOAuth } from \"../auth/helpers.ts\";\nimport type { OAuthCredential } from \"../auth/types.ts\";\nimport { loadGitHubCopilotOAuth } from \"../auth/oauth/load.ts\";\nimport { createProvider, type Provider } from \"../models.ts\";\nimport { GITHUB_COPILOT_MODELS } from \"./github-copilot.models.ts\";\n\n/** Copilot advertises entitlements as string ID arrays on the OAuth credential; ignore any other shape. */\nfunction advertisedModelIds(value: unknown): ReadonlySet<string> | undefined {\n\tif (!Array.isArray(value) || !value.every((entry) => typeof entry === \"string\")) return undefined;\n\treturn new Set(value);\n}\n\n/** A login saved before Copilot model lists were stored, or whose list is malformed, must re-read it. */\nfunction lacksAccountModelList(credential: OAuthCredential): boolean {\n\treturn advertisedModelIds(credential.availableModelIds) === undefined;\n}\n\nexport function githubCopilotProvider(): Provider<\"anthropic-messages\" | \"openai-completions\" | \"openai-responses\"> {\n\treturn createProvider({\n\t\tid: \"github-copilot\",\n\t\tname: \"GitHub Copilot\",\n\t\tbaseUrl: \"https://api.individual.githubcopilot.com\",\n\t\tauth: {\n\t\t\tapiKey: envApiKeyAuth(\"GitHub Copilot token\", [\"COPILOT_GITHUB_TOKEN\"]),\n\t\t\toauth: lazyOAuth({\n\t\t\t\tname: \"GitHub Copilot\",\n\t\t\t\tisSubscription: true,\n\t\t\t\tneedsRefresh: lacksAccountModelList,\n\t\t\t\tload: loadGitHubCopilotOAuth,\n\t\t\t}),\n\t\t},\n\t\tmodels: Object.values(GITHUB_COPILOT_MODELS),\n\t\tfilterModels: (models, credential) => {\n\t\t\tconst oauth = credential?.type === \"oauth\" ? credential : undefined;\n\t\t\tconst entitledFastModelIds = advertisedModelIds(oauth?.fastModelIds);\n\t\t\tconst availableModelIds = advertisedModelIds(oauth?.availableModelIds);\n\t\t\treturn models.filter((model) => {\n\t\t\t\t// Fast siblings are advertised per account, so expose only the exact IDs this credential\n\t\t\t\t// entitles. Fast-ness comes from the explicit route metadata, never from the `-fast` suffix:\n\t\t\t\t// a Copilot-owned model that merely ends in `-fast` is an ordinary picker model.\n\t\t\t\tif (model.fastRoute !== undefined) return entitledFastModelIds?.has(model.id) === true;\n\t\t\t\t// A login without a stored model list (an older login) keeps the catalog so it is never left\n\t\t\t\t// with no models; `Models.refresh()` with network access re-reads the list once (see\n\t\t\t\t// `needsRefresh`), and the account list applies from then on.\n\t\t\t\treturn availableModelIds?.has(model.id) ?? true;\n\t\t\t});\n\t\t},\n\t\tapi: {\n\t\t\t\"anthropic-messages\": anthropicMessagesApi(),\n\t\t\t\"openai-completions\": openAICompletionsApi(),\n\t\t\t\"openai-responses\": openAIResponsesApi(),\n\t\t},\n\t});\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-mid-convo-effort.d.ts","sourceRoot":"","sources":["../../src/providers/openai-mid-convo-effort.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iCAAiC,EAAE,WAAW,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"openai-mid-convo-effort.d.ts","sourceRoot":"","sources":["../../src/providers/openai-mid-convo-effort.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iCAAiC,EAAE,WAAW,CAAC,MAAM,CAMhE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-mid-convo-effort.js","sourceRoot":"","sources":["../../src/providers/openai-mid-convo-effort.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iCAAiC,GAAwB,IAAI,GAAG,CAAC;IAC7E,aAAa;IACb,WAAW;IACX,YAAY;IACZ,aAAa;
|
|
1
|
+
{"version":3,"file":"openai-mid-convo-effort.js","sourceRoot":"","sources":["../../src/providers/openai-mid-convo-effort.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iCAAiC,GAAwB,IAAI,GAAG,CAAC;IAC7E,aAAa;IACb,WAAW;IACX,YAAY;IACZ,aAAa;IACb,eAAe;CACf,CAAC,CAAC","sourcesContent":["export const OPENAI_MID_CONVO_EFFORT_MODEL_IDS: ReadonlySet<string> = new Set([\n\t\"gpt-6-astra\",\n\t\"gpt-6-sol\",\n\t\"gpt-6-luna\",\n\t\"gpt-6.1-sol\",\n\t\"gpt-6.1-terra\",\n]);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider-retry.d.ts","sourceRoot":"","sources":["../../src/utils/provider-retry.ts"],"names":[],"mappings":"AAEA,UAAU,oBAAoB;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,oFAAoF;IACpF,eAAe,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACpC;
|
|
1
|
+
{"version":3,"file":"provider-retry.d.ts","sourceRoot":"","sources":["../../src/utils/provider-retry.ts"],"names":[],"mappings":"AAEA,UAAU,oBAAoB;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,oFAAoF;IACpF,eAAe,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACpC;AA+FD;;;;;;;GAOG;AACH,wBAAsB,oBAAoB,CAAC,CAAC,EAC3C,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACzB,OAAO,GAAE,oBAAyB,GAChC,OAAO,CAAC,CAAC,CAAC,CAkBZ"}
|
|
@@ -19,10 +19,11 @@ function isRetryableProviderError(error) {
|
|
|
19
19
|
error.status === 429 ||
|
|
20
20
|
(typeof error.status === "number" && error.status >= 500));
|
|
21
21
|
}
|
|
22
|
-
function validateServerRetryDelayMs(delayMs, maxRetryDelayMs,
|
|
22
|
+
function validateServerRetryDelayMs(delayMs, maxRetryDelayMs, error) {
|
|
23
23
|
const maxDelayMs = maxRetryDelayMs ?? DEFAULT_MAX_RETRY_DELAY_MS;
|
|
24
24
|
if (maxDelayMs > 0 && delayMs > maxDelayMs) {
|
|
25
|
-
|
|
25
|
+
// Keep the server's hint and the original failure so callers can still schedule around the reset.
|
|
26
|
+
throw Object.assign(new Error(`Server requested ${Math.ceil(delayMs / 1000)}s retry delay (max: ${Math.ceil(maxDelayMs / 1000)}s). ${error.message}`, { cause: error }), { status: error.status, retryAfterMs: delayMs });
|
|
26
27
|
}
|
|
27
28
|
return delayMs;
|
|
28
29
|
}
|
|
@@ -31,14 +32,14 @@ function getRetryDelayMs(error, retryIndex, maxRetryDelayMs) {
|
|
|
31
32
|
if (retryAfterMs) {
|
|
32
33
|
const value = Number.parseFloat(retryAfterMs);
|
|
33
34
|
if (Number.isFinite(value))
|
|
34
|
-
return validateServerRetryDelayMs(value, maxRetryDelayMs, error
|
|
35
|
+
return validateServerRetryDelayMs(value, maxRetryDelayMs, error);
|
|
35
36
|
}
|
|
36
37
|
const retryAfter = error.headers?.get("retry-after");
|
|
37
38
|
if (retryAfter) {
|
|
38
39
|
const seconds = Number.parseFloat(retryAfter);
|
|
39
40
|
const delayMs = Number.isNaN(seconds) ? Date.parse(retryAfter) - Date.now() : seconds * 1000;
|
|
40
41
|
if (Number.isFinite(delayMs))
|
|
41
|
-
return validateServerRetryDelayMs(delayMs, maxRetryDelayMs, error
|
|
42
|
+
return validateServerRetryDelayMs(delayMs, maxRetryDelayMs, error);
|
|
42
43
|
}
|
|
43
44
|
const exponentialDelay = Math.min(0.5 * 2 ** retryIndex, 8) * 1000;
|
|
44
45
|
return exponentialDelay * (1 - Math.random() * 0.25);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider-retry.js","sourceRoot":"","sources":["../../src/utils/provider-retry.ts"],"names":[],"mappings":"AAAA,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAe1C,SAAS,eAAe,CAAC,KAAc;IACtC,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC7F,OAAO,CACN,CAAC,KAAK,CAAC,MAAM,KAAK,SAAS,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,CAAC;QAChE,CAAC,KAAK,CAAC,OAAO,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,YAAY,OAAO,CAAC,CACjE,CAAC;AACH,CAAC;AAED,gGAAgG;AAChG,SAAS,wBAAwB,CAAC,KAAoB;IACrD,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACzD,IAAI,WAAW,KAAK,MAAM;QAAE,OAAO,IAAI,CAAC;IACxC,IAAI,WAAW,KAAK,OAAO;QAAE,OAAO,KAAK,CAAC;IAE1C,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IAC5C,OAAO,CACN,KAAK,CAAC,MAAM,KAAK,GAAG;QACpB,KAAK,CA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E,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACzC,OAAyB,EACzB,OAAO,GAAyB,EAAE;IAElC,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,CAAC,CAAC;IAC3C,IAAI,gBAAgB,GAAG,UAAU,CAAC;IAElC,SAAS,CAAC;QACT,IAAI,CAAC;YACJ,8EAA8E;YAC9E,OAAO,MAAM,OAAO,EAAE,CAAC;QACxB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO;gBAAE,MAAM,gBAAgB,EAAE,CAAC;YACtD,IAAI,gBAAgB,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,wBAAwB,CAAC,KAAK,CAAC;gBAAE,MAAM,KAAK,CAAC;YACtG,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,IAAI,OAAO,CAAC,eAAe,EAAE,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC;gBAAE,MAAM,KAAK,CAAC;YAE/F,MAAM,UAAU,GAAG,UAAU,GAAG,gBAAgB,CAAC;YACjD,gBAAgB,EAAE,CAAC;YACnB,MAAM,cAAc,CAAC,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QACnG,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["const DEFAULT_MAX_RETRY_DELAY_MS = 60_000;\n\ninterface ProviderRetryOptions {\n\tmaxRetries?: number;\n\tmaxRetryDelayMs?: number;\n\tsignal?: AbortSignal;\n\t/** HTTP statuses that fail at once although the default policy would retry them. */\n\tnoRetryStatuses?: readonly number[];\n}\n\ninterface ProviderError extends Error {\n\tstatus: number | undefined;\n\theaders: Headers | undefined;\n}\n\nfunction isProviderError(error: unknown): error is ProviderError {\n\tif (!(error instanceof Error) || !(\"status\" in error) || !(\"headers\" in error)) return false;\n\treturn (\n\t\t(error.status === undefined || typeof error.status === \"number\") &&\n\t\t(error.headers === undefined || error.headers instanceof Headers)\n\t);\n}\n\n/** Mirrors the pinned OpenAI/Anthropic SDK retry policy; review when either SDK is upgraded. */\nfunction isRetryableProviderError(error: ProviderError): boolean {\n\tconst shouldRetry = error.headers?.get(\"x-should-retry\");\n\tif (shouldRetry === \"true\") return true;\n\tif (shouldRetry === \"false\") return false;\n\n\tif (error.status === undefined) return true;\n\treturn (\n\t\terror.status === 408 ||\n\t\terror.status === 409 ||\n\t\terror.status === 429 ||\n\t\t(typeof error.status === \"number\" && error.status >= 500)\n\t);\n}\n\nfunction validateServerRetryDelayMs(\n\tdelayMs: number,\n\tmaxRetryDelayMs: number | undefined,\n\terror: ProviderError,\n): number {\n\tconst maxDelayMs = maxRetryDelayMs ?? DEFAULT_MAX_RETRY_DELAY_MS;\n\tif (maxDelayMs > 0 && delayMs > maxDelayMs) {\n\t\t// Keep the server's hint and the original failure so callers can still schedule around the reset.\n\t\tthrow Object.assign(\n\t\t\tnew Error(\n\t\t\t\t`Server requested ${Math.ceil(delayMs / 1000)}s retry delay (max: ${Math.ceil(maxDelayMs / 1000)}s). ${error.message}`,\n\t\t\t\t{ cause: error },\n\t\t\t),\n\t\t\t{ status: error.status, retryAfterMs: delayMs },\n\t\t);\n\t}\n\treturn delayMs;\n}\n\nfunction getRetryDelayMs(error: ProviderError, retryIndex: number, maxRetryDelayMs: number | undefined): number {\n\tconst retryAfterMs = error.headers?.get(\"retry-after-ms\");\n\tif (retryAfterMs) {\n\t\tconst value = Number.parseFloat(retryAfterMs);\n\t\tif (Number.isFinite(value)) return validateServerRetryDelayMs(value, maxRetryDelayMs, error);\n\t}\n\n\tconst retryAfter = error.headers?.get(\"retry-after\");\n\tif (retryAfter) {\n\t\tconst seconds = Number.parseFloat(retryAfter);\n\t\tconst delayMs = Number.isNaN(seconds) ? Date.parse(retryAfter) - Date.now() : seconds * 1000;\n\t\tif (Number.isFinite(delayMs)) return validateServerRetryDelayMs(delayMs, maxRetryDelayMs, error);\n\t}\n\n\tconst exponentialDelay = Math.min(0.5 * 2 ** retryIndex, 8) * 1000;\n\treturn exponentialDelay * (1 - Math.random() * 0.25);\n}\n\nfunction createAbortError(): Error {\n\tconst error = new Error(\"Request aborted\");\n\terror.name = \"AbortError\";\n\treturn error;\n}\n\nfunction abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {\n\treturn new Promise((resolve, reject) => {\n\t\tif (signal?.aborted) {\n\t\t\treject(createAbortError());\n\t\t\treturn;\n\t\t}\n\n\t\tconst onAbort = () => {\n\t\t\tclearTimeout(timeout);\n\t\t\treject(createAbortError());\n\t\t};\n\t\tconst timeout = setTimeout(\n\t\t\t() => {\n\t\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t\tresolve();\n\t\t\t},\n\t\t\tMath.max(0, ms),\n\t\t);\n\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t});\n}\n\n/**\n * Reproduce the retry behavior used by the OpenAI and Anthropic SDKs while making\n * their backoff sleep interruptible. Their built-in retry timers ignore the\n * request AbortSignal, so callers must invoke the SDK with `maxRetries: 0` and\n * wrap the request with this helper. Provider-requested delays above\n * `maxRetryDelayMs` fail immediately (60 seconds by default); set it to zero to\n * disable the limit.\n */\nexport async function retryProviderRequest<T>(\n\trequest: () => Promise<T>,\n\toptions: ProviderRetryOptions = {},\n): Promise<T> {\n\tconst maxRetries = options.maxRetries ?? 0;\n\tlet retriesRemaining = maxRetries;\n\n\tfor (;;) {\n\t\ttry {\n\t\t\t// Each retry is a fresh SDK request, so X-Stainless-Retry-Count remains zero.\n\t\t\treturn await request();\n\t\t} catch (error) {\n\t\t\tif (options.signal?.aborted) throw createAbortError();\n\t\t\tif (retriesRemaining <= 0 || !isProviderError(error) || !isRetryableProviderError(error)) throw error;\n\t\t\tif (error.status !== undefined && options.noRetryStatuses?.includes(error.status)) throw error;\n\n\t\t\tconst retryIndex = maxRetries - retriesRemaining;\n\t\t\tretriesRemaining--;\n\t\t\tawait abortableSleep(getRetryDelayMs(error, retryIndex, options.maxRetryDelayMs), options.signal);\n\t\t}\n\t}\n}\n"]}
|
|
@@ -2,11 +2,14 @@ interface HeaderReader {
|
|
|
2
2
|
get(name: string): string | null;
|
|
3
3
|
}
|
|
4
4
|
/**
|
|
5
|
-
* Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or
|
|
6
|
-
*
|
|
5
|
+
* Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or HTTP date), a JSON body
|
|
6
|
+
* `retry_after` in seconds (Cloudflare 5xx pages) and a body `RetryInfo.retryDelay` (Google APIs).
|
|
7
7
|
*/
|
|
8
8
|
export declare function getRetryAfterMs(headers?: HeaderReader, body?: string, now?: number): number | undefined;
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Server retry hint carried by a provider error (`headers` or an AWS `$response`, plus a parsed body `error`,
|
|
11
|
+
* raw string `body` or a JSON body folded into the message), or an explicit `retryAfterMs`.
|
|
12
|
+
*/
|
|
10
13
|
export declare function getProviderRetryAfterMs(error: object): number | undefined;
|
|
11
14
|
export {};
|
|
12
15
|
//# sourceMappingURL=retry-hints.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry-hints.d.ts","sourceRoot":"","sources":["../../src/utils/retry-hints.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"retry-hints.d.ts","sourceRoot":"","sources":["../../src/utils/retry-hints.ts"],"names":[],"mappings":"AAEA,UAAU,YAAY;IACrB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CACjC;AAuED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,SAAa,GAAG,MAAM,GAAG,SAAS,CAkB3G;AAsBD;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGzE"}
|
|
@@ -4,9 +4,75 @@ function positiveDelay(value) {
|
|
|
4
4
|
function isHeaderReader(value) {
|
|
5
5
|
return value !== null && "get" in value && typeof value.get === "function";
|
|
6
6
|
}
|
|
7
|
+
/** Reads a plain header record (as the AWS SDK keeps on `$response.headers`) case-insensitively. */
|
|
8
|
+
function recordHeaderReader(record) {
|
|
9
|
+
const entries = Object.entries(record).filter((entry) => typeof entry[1] === "string");
|
|
10
|
+
return {
|
|
11
|
+
get(name) {
|
|
12
|
+
const lower = name.toLowerCase();
|
|
13
|
+
return entries.find(([key]) => key.toLowerCase() === lower)?.[1] ?? null;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function isJsonObject(value) {
|
|
18
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
/** A protobuf JSON `Duration` such as `"17s"` or `"0.250s"`, in ms. */
|
|
21
|
+
function durationMs(value) {
|
|
22
|
+
if (typeof value !== "string")
|
|
23
|
+
return undefined;
|
|
24
|
+
const match = /^\s*(\d+(?:\.\d+)?)s\s*$/.exec(value);
|
|
25
|
+
return match ? positiveDelay(Number(match[1]) * 1000) : undefined;
|
|
26
|
+
}
|
|
7
27
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
28
|
+
* Retry hints in a parsed JSON error body: a top-level or `error.retry_after` in seconds (Cloudflare 5xx
|
|
29
|
+
* pages), and a `RetryInfo` entry's `retryDelay` in `error.details` (Google APIs). Arrays of bodies, as
|
|
30
|
+
* some streaming endpoints send, are searched entry by entry.
|
|
31
|
+
*/
|
|
32
|
+
function bodyHints(parsed, hints) {
|
|
33
|
+
if (Array.isArray(parsed)) {
|
|
34
|
+
for (const entry of parsed)
|
|
35
|
+
bodyHints(entry, hints);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!isJsonObject(parsed))
|
|
39
|
+
return;
|
|
40
|
+
const error = isJsonObject(parsed.error) ? parsed.error : undefined;
|
|
41
|
+
const seconds = parsed.retry_after ?? error?.retry_after;
|
|
42
|
+
if (typeof seconds === "number") {
|
|
43
|
+
const delay = positiveDelay(seconds * 1000);
|
|
44
|
+
if (delay !== undefined)
|
|
45
|
+
hints.push(delay);
|
|
46
|
+
}
|
|
47
|
+
const details = error?.details;
|
|
48
|
+
if (!Array.isArray(details))
|
|
49
|
+
return;
|
|
50
|
+
for (const detail of details) {
|
|
51
|
+
if (!isJsonObject(detail))
|
|
52
|
+
continue;
|
|
53
|
+
const type = detail["@type"];
|
|
54
|
+
if (typeof type !== "string" || !type.endsWith("google.rpc.RetryInfo"))
|
|
55
|
+
continue;
|
|
56
|
+
const delay = durationMs(detail.retryDelay);
|
|
57
|
+
if (delay !== undefined)
|
|
58
|
+
hints.push(delay);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Parses `text` as JSON, or the JSON that follows a prefix such as `got status: 429. {...}`. */
|
|
62
|
+
function parseJsonBody(text) {
|
|
63
|
+
const start = text.search(/[[{]/);
|
|
64
|
+
if (start < 0)
|
|
65
|
+
return undefined;
|
|
66
|
+
try {
|
|
67
|
+
return JSON.parse(text.slice(start));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or HTTP date), a JSON body
|
|
75
|
+
* `retry_after` in seconds (Cloudflare 5xx pages) and a body `RetryInfo.retryDelay` (Google APIs).
|
|
10
76
|
*/
|
|
11
77
|
export function getRetryAfterMs(headers, body, now = Date.now()) {
|
|
12
78
|
const hints = [];
|
|
@@ -24,27 +90,41 @@ export function getRetryAfterMs(headers, body, now = Date.now()) {
|
|
|
24
90
|
hints.push(delay);
|
|
25
91
|
}
|
|
26
92
|
if (body) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (typeof seconds === "number") {
|
|
31
|
-
const delay = positiveDelay(seconds * 1000);
|
|
32
|
-
if (delay !== undefined)
|
|
33
|
-
hints.push(delay);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
catch { }
|
|
93
|
+
const parsed = parseJsonBody(body);
|
|
94
|
+
if (parsed !== undefined)
|
|
95
|
+
bodyHints(parsed, hints);
|
|
37
96
|
}
|
|
38
97
|
return hints.length ? Math.max(...hints) : undefined;
|
|
39
98
|
}
|
|
40
|
-
|
|
99
|
+
function errorHeaders(error) {
|
|
100
|
+
if ("headers" in error && typeof error.headers === "object" && isHeaderReader(error.headers))
|
|
101
|
+
return error.headers;
|
|
102
|
+
// AWS SDK service exceptions keep the raw HTTP response, with a plain header record, on `$response`.
|
|
103
|
+
if ("$response" in error && typeof error.$response === "object" && error.$response !== null) {
|
|
104
|
+
const response = error.$response;
|
|
105
|
+
if ("headers" in response && typeof response.headers === "object" && response.headers !== null) {
|
|
106
|
+
return isHeaderReader(response.headers) ? response.headers : recordHeaderReader(response.headers);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
function errorBody(error) {
|
|
112
|
+
if ("body" in error && typeof error.body === "string")
|
|
113
|
+
return error.body;
|
|
114
|
+
if ("error" in error && error.error !== undefined)
|
|
115
|
+
return JSON.stringify(error.error);
|
|
116
|
+
// SDKs that fold the JSON error body into the message (`@google/genai`'s ApiError).
|
|
117
|
+
if (error instanceof Error && /[[{]/.test(error.message))
|
|
118
|
+
return error.message;
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Server retry hint carried by a provider error (`headers` or an AWS `$response`, plus a parsed body `error`,
|
|
123
|
+
* raw string `body` or a JSON body folded into the message), or an explicit `retryAfterMs`.
|
|
124
|
+
*/
|
|
41
125
|
export function getProviderRetryAfterMs(error) {
|
|
42
126
|
if ("retryAfterMs" in error && typeof error.retryAfterMs === "number")
|
|
43
127
|
return positiveDelay(error.retryAfterMs);
|
|
44
|
-
|
|
45
|
-
? error.headers
|
|
46
|
-
: undefined;
|
|
47
|
-
const body = "error" in error ? JSON.stringify(error.error) : undefined;
|
|
48
|
-
return getRetryAfterMs(headers, body);
|
|
128
|
+
return getRetryAfterMs(errorHeaders(error), errorBody(error));
|
|
49
129
|
}
|
|
50
130
|
//# sourceMappingURL=retry-hints.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry-hints.js","sourceRoot":"","sources":["../../src/utils/retry-hints.ts"],"names":[],"mappings":"AAIA,SAAS,aAAa,CAAC,KAAa;IACnC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACjE,CAAC;AAED,SAAS,cAAc,CAAC,KAAoB;IAC3C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,GAAG,KAAK,UAAU,CAAC;AAC5E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,OAAsB,EAAE,IAAa,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE;IACtF,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,YAAY,GAAG,OAAO,EAAE,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACpD,IAAI,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC;QAC1B,MAAM,KAAK,GAAG,aAAa,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC;QAClD,IAAI,KAAK,KAAK,SAAS;YAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC5C,CAAC;IACD,MAAM,UAAU,GAAG,OAAO,EAA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AI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,IAAI,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC;QACvF,CAAC,CAAC,KAAK,CAAC,OAAO;QACf,CAAC,CAAC,SAAS,CAAC;IACd,MAAM,IAAI,GAAG,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACxE,OAAO,eAAe,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACvC,CAAC","sourcesContent":["interface HeaderReader {\n\tget(name: string): string | null;\n}\n\nfunction positiveDelay(value: number): number | undefined {\n\treturn Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\nfunction isHeaderReader(value: object | null): value is HeaderReader {\n\treturn value !== null && \"get\" in value && typeof value.get === \"function\";\n}\n\n/**\n * Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or\n * HTTP date) and a JSON body `retry_after` in seconds (Cloudflare 5xx pages).\n */\nexport function getRetryAfterMs(headers?: HeaderReader, body?: string, now = Date.now()): number | undefined {\n\tconst hints: number[] = [];\n\tconst milliseconds = headers?.get(\"retry-after-ms\");\n\tif (milliseconds?.trim()) {\n\t\tconst delay = positiveDelay(Number(milliseconds));\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n\tconst retryAfter = headers?.get(\"retry-after\");\n\tif (retryAfter?.trim()) {\n\t\tconst seconds = Number(retryAfter);\n\t\tconst delay = positiveDelay(Number.isFinite(seconds) ? seconds * 1000 : Date.parse(retryAfter) - now);\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n\tif (body) {\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(body) as { retry_after?: number; error?: { retry_after?: number } } | null;\n\t\t\tconst seconds = parsed?.retry_after ?? parsed?.error?.retry_after;\n\t\t\tif (typeof seconds === \"number\") {\n\t\t\t\tconst delay = positiveDelay(seconds * 1000);\n\t\t\t\tif (delay !== undefined) hints.push(delay);\n\t\t\t}\n\t\t} catch {}\n\t}\n\treturn hints.length ? Math.max(...hints) : undefined;\n}\n\n/** Server retry hint carried by a provider SDK error (`headers` plus parsed body `error`), or an explicit `retryAfterMs`. */\nexport function getProviderRetryAfterMs(error: object): number | undefined {\n\tif (\"retryAfterMs\" in error && typeof error.retryAfterMs === \"number\") return positiveDelay(error.retryAfterMs);\n\tconst headers =\n\t\t\"headers\" in error && typeof error.headers === \"object\" && isHeaderReader(error.headers)\n\t\t\t? error.headers\n\t\t\t: undefined;\n\tconst body = \"error\" in error ? JSON.stringify(error.error) : undefined;\n\treturn getRetryAfterMs(headers, body);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"retry-hints.js","sourceRoot":"","sources":["../../src/utils/retry-hints.ts"],"names":[],"mappings":"AAMA,SAAS,aAAa,CAAC,KAAa;IACnC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACjE,CAAC;AAED,SAAS,cAAc,CAAC,KAAoB;IAC3C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,GAAG,KAAK,UAAU,CAAC;AAC5E,CAAC;AAED,oGAAoG;AACpG,SAAS,kBAAkB,CAAC,MAAc;IACzC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAA6B,EAAE,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC;IAClH,OAAO;QACN,GAAG,CAAC,IAAY;YACf,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;YACjC,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC,E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AAQ,IAAI,OAAO,QAAQ,CAAC,OAAO,KAAK,QAAQ,IAAI,QAAQ,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;YAChG,OAAO,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,kBAAkB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACnG,CAAC;IACF,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,SAAS,SAAS,CAAC,KAAa;IAC/B,IAAI,MAAM,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC,IAAI,CAAC;IACzE,IAAI,OAAO,IAAI,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACtF,oFAAoF;IACpF,IAAI,KAAK,YAAY,KAAK,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;QAAE,OAAO,KAAK,CAAC,OAAO,CAAC;IAC/E,OAAO,SAAS,CAAC;AAClB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAa;IACpD,IAAI,cAAc,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,YAAY,KAAK,QAAQ;QAAE,OAAO,aAAa,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAChH,OAAO,eAAe,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;AAC/D,CAAC","sourcesContent":["import type { JsonObject, JsonValue } from \"../types.ts\";\n\ninterface HeaderReader {\n\tget(name: string): string | null;\n}\n\nfunction positiveDelay(value: number): number | undefined {\n\treturn Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\nfunction isHeaderReader(value: object | null): value is HeaderReader {\n\treturn value !== null && \"get\" in value && typeof value.get === \"function\";\n}\n\n/** Reads a plain header record (as the AWS SDK keeps on `$response.headers`) case-insensitively. */\nfunction recordHeaderReader(record: object): HeaderReader {\n\tconst entries = Object.entries(record).filter((entry): entry is [string, string] => typeof entry[1] === \"string\");\n\treturn {\n\t\tget(name: string): string | null {\n\t\t\tconst lower = name.toLowerCase();\n\t\t\treturn entries.find(([key]) => key.toLowerCase() === lower)?.[1] ?? null;\n\t\t},\n\t};\n}\n\nfunction isJsonObject(value: JsonValue | undefined): value is JsonObject {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** A protobuf JSON `Duration` such as `\"17s\"` or `\"0.250s\"`, in ms. */\nfunction durationMs(value: JsonValue | undefined): number | undefined {\n\tif (typeof value !== \"string\") return undefined;\n\tconst match = /^\\s*(\\d+(?:\\.\\d+)?)s\\s*$/.exec(value);\n\treturn match ? positiveDelay(Number(match[1]) * 1000) : undefined;\n}\n\n/**\n * Retry hints in a parsed JSON error body: a top-level or `error.retry_after` in seconds (Cloudflare 5xx\n * pages), and a `RetryInfo` entry's `retryDelay` in `error.details` (Google APIs). Arrays of bodies, as\n * some streaming endpoints send, are searched entry by entry.\n */\nfunction bodyHints(parsed: JsonValue, hints: number[]): void {\n\tif (Array.isArray(parsed)) {\n\t\tfor (const entry of parsed) bodyHints(entry, hints);\n\t\treturn;\n\t}\n\tif (!isJsonObject(parsed)) return;\n\tconst error = isJsonObject(parsed.error) ? parsed.error : undefined;\n\tconst seconds = parsed.retry_after ?? error?.retry_after;\n\tif (typeof seconds === \"number\") {\n\t\tconst delay = positiveDelay(seconds * 1000);\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n\tconst details = error?.details;\n\tif (!Array.isArray(details)) return;\n\tfor (const detail of details) {\n\t\tif (!isJsonObject(detail)) continue;\n\t\tconst type = detail[\"@type\"];\n\t\tif (typeof type !== \"string\" || !type.endsWith(\"google.rpc.RetryInfo\")) continue;\n\t\tconst delay = durationMs(detail.retryDelay);\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n}\n\n/** Parses `text` as JSON, or the JSON that follows a prefix such as `got status: 429. {...}`. */\nfunction parseJsonBody(text: string): JsonValue | undefined {\n\tconst start = text.search(/[[{]/);\n\tif (start < 0) return undefined;\n\ttry {\n\t\treturn JSON.parse(text.slice(start)) as JsonValue;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/**\n * Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or HTTP date), a JSON body\n * `retry_after` in seconds (Cloudflare 5xx pages) and a body `RetryInfo.retryDelay` (Google APIs).\n */\nexport function getRetryAfterMs(headers?: HeaderReader, body?: string, now = Date.now()): number | undefined {\n\tconst hints: number[] = [];\n\tconst milliseconds = headers?.get(\"retry-after-ms\");\n\tif (milliseconds?.trim()) {\n\t\tconst delay = positiveDelay(Number(milliseconds));\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n\tconst retryAfter = headers?.get(\"retry-after\");\n\tif (retryAfter?.trim()) {\n\t\tconst seconds = Number(retryAfter);\n\t\tconst delay = positiveDelay(Number.isFinite(seconds) ? seconds * 1000 : Date.parse(retryAfter) - now);\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n\tif (body) {\n\t\tconst parsed = parseJsonBody(body);\n\t\tif (parsed !== undefined) bodyHints(parsed, hints);\n\t}\n\treturn hints.length ? Math.max(...hints) : undefined;\n}\n\nfunction errorHeaders(error: object): HeaderReader | undefined {\n\tif (\"headers\" in error && typeof error.headers === \"object\" && isHeaderReader(error.headers)) return error.headers;\n\t// AWS SDK service exceptions keep the raw HTTP response, with a plain header record, on `$response`.\n\tif (\"$response\" in error && typeof error.$response === \"object\" && error.$response !== null) {\n\t\tconst response = error.$response;\n\t\tif (\"headers\" in response && typeof response.headers === \"object\" && response.headers !== null) {\n\t\t\treturn isHeaderReader(response.headers) ? response.headers : recordHeaderReader(response.headers);\n\t\t}\n\t}\n\treturn undefined;\n}\n\nfunction errorBody(error: object): string | undefined {\n\tif (\"body\" in error && typeof error.body === \"string\") return error.body;\n\tif (\"error\" in error && error.error !== undefined) return JSON.stringify(error.error);\n\t// SDKs that fold the JSON error body into the message (`@google/genai`'s ApiError).\n\tif (error instanceof Error && /[[{]/.test(error.message)) return error.message;\n\treturn undefined;\n}\n\n/**\n * Server retry hint carried by a provider error (`headers` or an AWS `$response`, plus a parsed body `error`,\n * raw string `body` or a JSON body folded into the message), or an explicit `retryAfterMs`.\n */\nexport function getProviderRetryAfterMs(error: object): number | undefined {\n\tif (\"retryAfterMs\" in error && typeof error.retryAfterMs === \"number\") return positiveDelay(error.retryAfterMs);\n\treturn getRetryAfterMs(errorHeaders(error), errorBody(error));\n}\n"]}
|
package/dist/utils/retry.js
CHANGED
|
@@ -31,8 +31,8 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
|
|
|
31
31
|
"rate.?limit",
|
|
32
32
|
"too many requests",
|
|
33
33
|
// Status codes must not be part of a longer word or number, e.g. a request id or byte count (#11057).
|
|
34
|
-
// 521-523 are Cloudflare origin errors (#3623).
|
|
35
|
-
"(?<![a-z0-9])(?:429|500|502|503|504|52[0-4])(?![a-z0-9])",
|
|
34
|
+
// 521-523 are Cloudflare origin errors (#3623); 529 is an overload response.
|
|
35
|
+
"(?<![a-z0-9])(?:429|500|502|503|504|52[0-4]|529)(?![a-z0-9])",
|
|
36
36
|
// Also matches "The service is temporarily unavailable" (#10965).
|
|
37
37
|
"service.?(?:is.?)?(?:temporarily.?)?unavailable",
|
|
38
38
|
"server.?error",
|
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,0CA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AAO,KAAK,EAAE,CAAC;YAChB,MAAM,SAAS,EAAE,eAAe,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC;YAC3E,IAAI,KAAK,YAAY,oBAAoB,EAAE,CAAC;gBAC3C,OAAO,EAAE,GAAG,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,SAAS,EAAE,CAAC;YACxE,CAAC;YACD,MAAM,KAAK,CAAC;QACb,CAAC;QACD,MAAM,SAAS,EAAE,mBAAmB,EAAE,EAAE,CAAC;IAC1C,CAAC;AACF,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAqB;IAC9D,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\t// Sign in with ChatGPT on a ChatGPT Business plan: a usage limit set by the\n\t// workspace admin. Only the admin can raise it.\n\t\"subscription_sharing_workplace_individual_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// Status codes must not be part of a longer word or number, e.g. a request id or byte count (#11057).\n\t// 521-523 are Cloudflare origin errors (#3623).\n\t\"(?<![a-z0-9])(?:429|500|502|503|504|52[0-4])(?![a-z0-9])\",\n\t// Also matches \"The service is temporarily unavailable\" (#10965).\n\t\"service.?(?:is.?)?(?:temporarily.?)?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\t\"unable to forward upstream\",\n\t\"upstream.?request.?failed\",\n\t\"upstream_stream_read_error\",\n\t\"stream was interrupted\",\n\t\"http/?2 stream failed\",\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\"ECONNRESET\",\n\t\"connection.?reset\",\n\t\"other side closed\",\n\t\"fetch failed\",\n\t\"request to \\\\S+ failed, reason:\",\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// Codex CLI mid-stream disconnect, relayed by proxies that wrap the Codex CLI,\n\t// e.g. \"stream disconnected before completion: stream closed before response.completed\" (#11057).\n\t\"stream disconnected before completion\",\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\t/**\n\t * Cap for a server retry hint (`Retry-After-Ms`, `Retry-After`, a body `retry_after`).\n\t * Defaults to 60 seconds; 0 caps the hint by `maxAgentDelayMs` instead.\n\t */\n\tmaxRetryDelayMs?: 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\ntype RetryFailure = Pick<AssistantMessage, \"stopReason\" | \"errorMessage\" | \"retryAfterMs\">;\n\n/**\n * Wait before retry `attempt` (1-indexed): the server retry hint when the failure\n * carries one, capped by `maxRetryDelayMs`, otherwise the exponential backoff.\n */\nexport function failureRetryDelayMs(\n\tpolicy: Pick<RetryPolicy, \"baseDelayMs\" | \"maxAgentDelayMs\" | \"maxRetryDelayMs\">,\n\tattempt: number,\n\tfailure: Pick<AssistantMessage, \"retryAfterMs\">,\n): number {\n\tconst hintMs = failure.retryAfterMs;\n\tif (hintMs === undefined || !Number.isFinite(hintMs) || hintMs <= 0) return retryDelayMs(policy, attempt);\n\tconst capMs = policy.maxRetryDelayMs || (policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS);\n\treturn Math.min(hintMs, capMs);\n}\n\n/**\n * Delay before retry `attempt` (1-indexed) of a failed assistant call, or `undefined`\n * when it is not retryable or `maxRetries` is spent.\n */\nexport function assistantRetryDelayMs(policy: RetryPolicy, attempt: number, message: RetryFailure): number | undefined {\n\tif (!policy.enabled || attempt > policy.maxRetries || !isRetryableAssistantError(message)) return undefined;\n\treturn failureRetryDelayMs(policy, attempt, message);\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, waiting for the server retry hint or\n * the exponential backoff (see {@link assistantRetryDelayMs}), 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\tconst delayMs = policy ? assistantRetryDelayMs(policy, attempt + 1, response) : undefined;\n\t\tif (delayMs === undefined) {\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\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: RetryFailure): 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;IAC3C,4E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CAAC;YACxE,CAAC;YACD,MAAM,KAAK,CAAC;QACb,CAAC;QACD,MAAM,SAAS,EAAE,mBAAmB,EAAE,EAAE,CAAC;IAC1C,CAAC;AACF,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAqB;IAC9D,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\t// Sign in with ChatGPT on a ChatGPT Business plan: a usage limit set by the\n\t// workspace admin. Only the admin can raise it.\n\t\"subscription_sharing_workplace_individual_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// Status codes must not be part of a longer word or number, e.g. a request id or byte count (#11057).\n\t// 521-523 are Cloudflare origin errors (#3623); 529 is an overload response.\n\t\"(?<![a-z0-9])(?:429|500|502|503|504|52[0-4]|529)(?![a-z0-9])\",\n\t// Also matches \"The service is temporarily unavailable\" (#10965).\n\t\"service.?(?:is.?)?(?:temporarily.?)?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\t\"unable to forward upstream\",\n\t\"upstream.?request.?failed\",\n\t\"upstream_stream_read_error\",\n\t\"stream was interrupted\",\n\t\"http/?2 stream failed\",\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\"ECONNRESET\",\n\t\"connection.?reset\",\n\t\"other side closed\",\n\t\"fetch failed\",\n\t\"request to \\\\S+ failed, reason:\",\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// Codex CLI mid-stream disconnect, relayed by proxies that wrap the Codex CLI,\n\t// e.g. \"stream disconnected before completion: stream closed before response.completed\" (#11057).\n\t\"stream disconnected before completion\",\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\t/**\n\t * Cap for a server retry hint (`Retry-After-Ms`, `Retry-After`, a body `retry_after`).\n\t * Defaults to 60 seconds; 0 caps the hint by `maxAgentDelayMs` instead.\n\t */\n\tmaxRetryDelayMs?: 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\ntype RetryFailure = Pick<AssistantMessage, \"stopReason\" | \"errorMessage\" | \"retryAfterMs\">;\n\n/**\n * Wait before retry `attempt` (1-indexed): the server retry hint when the failure\n * carries one, capped by `maxRetryDelayMs`, otherwise the exponential backoff.\n */\nexport function failureRetryDelayMs(\n\tpolicy: Pick<RetryPolicy, \"baseDelayMs\" | \"maxAgentDelayMs\" | \"maxRetryDelayMs\">,\n\tattempt: number,\n\tfailure: Pick<AssistantMessage, \"retryAfterMs\">,\n): number {\n\tconst hintMs = failure.retryAfterMs;\n\tif (hintMs === undefined || !Number.isFinite(hintMs) || hintMs <= 0) return retryDelayMs(policy, attempt);\n\tconst capMs = policy.maxRetryDelayMs || (policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS);\n\treturn Math.min(hintMs, capMs);\n}\n\n/**\n * Delay before retry `attempt` (1-indexed) of a failed assistant call, or `undefined`\n * when it is not retryable or `maxRetries` is spent.\n */\nexport function assistantRetryDelayMs(policy: RetryPolicy, attempt: number, message: RetryFailure): number | undefined {\n\tif (!policy.enabled || attempt > policy.maxRetries || !isRetryableAssistantError(message)) return undefined;\n\treturn failureRetryDelayMs(policy, attempt, message);\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, waiting for the server retry hint or\n * the exponential backoff (see {@link assistantRetryDelayMs}), 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\tconst delayMs = policy ? assistantRetryDelayMs(policy, attempt + 1, response) : undefined;\n\t\tif (delayMs === undefined) {\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\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: RetryFailure): 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/dist/utils/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const PI_AI_VERSION = "0.9.
|
|
1
|
+
export declare const PI_AI_VERSION = "0.9.46-alpha.1";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/utils/version.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,aAAa,
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/utils/version.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,aAAa,mBAAmB,CAAC"}
|
package/dist/utils/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/utils/version.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,MAAM,CAAC,MAAM,aAAa,GAAG,
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/utils/version.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,MAAM,CAAC,MAAM,aAAa,GAAG,gBAAgB,CAAC","sourcesContent":["// The @bastani/pi-ai package version. scripts/bump-version.ts keeps it in sync with package.json.\nexport const PI_AI_VERSION = \"0.9.46-alpha.1\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bastani/pi-ai",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.46-alpha.1",
|
|
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",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"@aws-sdk/client-bedrock-runtime": "3.1127.0",
|
|
80
80
|
"@earendil-works/pi-telemetry": "1.4.0",
|
|
81
81
|
"@google/genai": "2.21.0",
|
|
82
|
-
"@smithy/node-http-handler": "4.
|
|
82
|
+
"@smithy/node-http-handler": "4.13.0",
|
|
83
83
|
"http-proxy-agent": "9.1.0",
|
|
84
84
|
"https-proxy-agent": "9.1.0",
|
|
85
85
|
"openai": "7.19.0",
|