@oh-my-pi/pi-ai 17.2.11 → 17.2.12
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 +6 -0
- package/dist/types/auth-retry.d.ts +3 -3
- package/dist/types/auth-storage.d.ts +2 -0
- package/dist/types/error/auth-classify.d.ts +5 -5
- package/dist/types/error/flags.d.ts +4 -0
- package/dist/types/registry/api-key-login.d.ts +6 -4
- package/dist/types/registry/cloudflare-ai-gateway.d.ts +2 -2
- package/dist/types/registry/kagi.d.ts +2 -2
- package/dist/types/registry/litellm.d.ts +2 -2
- package/dist/types/registry/llama-cpp.d.ts +2 -2
- package/dist/types/registry/lm-studio.d.ts +2 -2
- package/dist/types/registry/parallel.d.ts +2 -2
- package/dist/types/registry/tavily.d.ts +1 -1
- package/dist/types/registry/vercel-ai-gateway.d.ts +2 -2
- package/dist/types/registry/vllm.d.ts +2 -2
- package/dist/types/usage/openai-codex-reset.d.ts +0 -20
- package/dist/types/usage/shared.d.ts +13 -1
- package/package.json +5 -5
- package/src/auth-retry.ts +5 -4
- package/src/auth-storage.ts +77 -33
- package/src/error/auth-classify.ts +7 -6
- package/src/error/flags.ts +14 -0
- package/src/registry/api-key-login.ts +26 -12
- package/src/registry/cloudflare-ai-gateway.ts +10 -29
- package/src/registry/kagi.ts +11 -29
- package/src/registry/litellm.ts +11 -29
- package/src/registry/llama-cpp.ts +11 -21
- package/src/registry/lm-studio.ts +9 -20
- package/src/registry/parallel.ts +10 -28
- package/src/registry/tavily.ts +9 -27
- package/src/registry/vercel-ai-gateway.ts +10 -28
- package/src/registry/vllm.ts +11 -21
- package/src/stream.ts +9 -7
- package/src/usage/alibaba-token-plan.ts +8 -15
- package/src/usage/claude.ts +12 -19
- package/src/usage/cursor.ts +6 -18
- package/src/usage/gemini.ts +3 -2
- package/src/usage/github-copilot.ts +3 -2
- package/src/usage/google-antigravity.ts +10 -17
- package/src/usage/kimi.ts +6 -12
- package/src/usage/minimax-code.ts +7 -27
- package/src/usage/openai-codex-reset.ts +1 -1
- package/src/usage/openai-codex.ts +3 -4
- package/src/usage/opencode-go.ts +1 -2
- package/src/usage/shared.ts +33 -10
- package/src/usage/synthetic.ts +4 -11
- package/src/usage/umans.ts +2 -2
- package/src/usage/xai-oauth.ts +13 -26
- package/src/usage/zai.ts +2 -5
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [17.2.12] - 2026-08-08
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Fixed account-scoped Codex cyber-policy denials bypassing sibling credential rotation; replay-safe requests now try every configured account before surfacing the error.
|
|
10
|
+
|
|
5
11
|
## [17.2.11] - 2026-08-07
|
|
6
12
|
|
|
7
13
|
### Breaking Changes
|
|
@@ -14,9 +14,9 @@ import { isAuthRetryableError } from "./error/auth-classify.js";
|
|
|
14
14
|
* (invalidate/usage-limit the current credential and rotate to a sibling).
|
|
15
15
|
*
|
|
16
16
|
* Current drivers preserve that bounded a/b/c sequence for ordinary 401/auth
|
|
17
|
-
* failures.
|
|
18
|
-
* until the resolver returns `undefined`,
|
|
19
|
-
* {@link AUTH_RETRY_MAX_ATTEMPTS}.
|
|
17
|
+
* failures. Account-scoped policy denials, 403s, and usage-limit failures skip
|
|
18
|
+
* refresh and may repeat step (c) until the resolver returns `undefined`,
|
|
19
|
+
* cycles, or hits {@link AUTH_RETRY_MAX_ATTEMPTS}.
|
|
20
20
|
*/
|
|
21
21
|
export interface ApiKeyResolveContext {
|
|
22
22
|
/** True when the resolver should rotate to a sibling credential. */
|
|
@@ -1143,6 +1143,8 @@ export declare class AuthStorage {
|
|
|
1143
1143
|
* - usage-limit / account-rate-limit error → {@link AuthStorage.markUsageLimitReached}
|
|
1144
1144
|
* (temporary block via its own backoff — default plus server usage-report
|
|
1145
1145
|
* reset; sticky left intact so the next resolve re-ranks around the block).
|
|
1146
|
+
* - account-scoped policy denial → temporarily block that account without
|
|
1147
|
+
* marking its credential suspect, then rotate through eligible siblings.
|
|
1146
1148
|
* - otherwise (hard 401 / auth failure) → mark the credential suspect (or
|
|
1147
1149
|
* reload when no broker hook is wired) and block it, then drop matching
|
|
1148
1150
|
* sticky state.
|
|
@@ -10,11 +10,11 @@ export declare function isInvalidatedOAuthTokenError(error: unknown): boolean;
|
|
|
10
10
|
/**
|
|
11
11
|
* Whether an upstream failure should rotate to a sibling credential: a hard
|
|
12
12
|
* `401`, a `403` (token valid but access denied — plan, model policy, or org
|
|
13
|
-
* restriction a sibling account may not share),
|
|
14
|
-
*
|
|
13
|
+
* restriction a sibling account may not share), an account-scoped policy
|
|
14
|
+
* denial such as Codex `cyber_policy`, a body-classified usage limit (Codex
|
|
15
|
+
* `usage_limit_reached`, Anthropic account rate-limit, Google
|
|
15
16
|
* `resource_exhausted`, OpenAI `insufficient_quota`, …), or a bare `429`
|
|
16
|
-
* whose payload did not preserve a richer quota code.
|
|
17
|
-
*
|
|
18
|
-
* upstream-backoff lane.
|
|
17
|
+
* whose payload did not preserve a richer quota code. Transient 429s
|
|
18
|
+
* (`Too many requests`, per-minute caps) stay in the upstream-backoff lane.
|
|
19
19
|
*/
|
|
20
20
|
export declare function isAuthRetryableError(error: unknown): boolean;
|
|
@@ -9,6 +9,8 @@ export declare const Flag: {
|
|
|
9
9
|
readonly MalformedFunctionCall: 2097152;
|
|
10
10
|
readonly ProviderFinishError: 4194304;
|
|
11
11
|
readonly ContentBlocked: 32768;
|
|
12
|
+
/** Account-scoped provider policy denial that may succeed with another credential. */
|
|
13
|
+
readonly AccountPolicy: 16384;
|
|
12
14
|
readonly ContextOverflow: 8388608;
|
|
13
15
|
readonly AuthFailed: 16777216;
|
|
14
16
|
readonly SilentAbort: 33554432;
|
|
@@ -53,6 +55,8 @@ export declare function classify(error: unknown, api?: Api): number;
|
|
|
53
55
|
* regexes at call sites.
|
|
54
56
|
*/
|
|
55
57
|
export declare function isUsageLimit(error: unknown, api?: Api): boolean;
|
|
58
|
+
/** Whether an upstream rejection is an account-scoped policy denial worth retrying with a sibling credential. */
|
|
59
|
+
export declare function isAccountPolicyError(error: unknown, api?: Api): boolean;
|
|
56
60
|
/**
|
|
57
61
|
* Strict-tool rejection: grammar too large, schema too complex, or structured
|
|
58
62
|
* outputs unsupported by the model/endpoint.
|
|
@@ -27,16 +27,18 @@ type ModelsEndpointValidation = {
|
|
|
27
27
|
export type ApiKeyLoginConfig = {
|
|
28
28
|
/** Display name used in error messages, e.g. "Cerebras", "NanoGPT". */
|
|
29
29
|
providerLabel: string;
|
|
30
|
-
/** URL opened in browser for the user to grab their key. */
|
|
31
|
-
authUrl
|
|
32
|
-
/** Instructions shown with the onAuth callback. */
|
|
33
|
-
instructions
|
|
30
|
+
/** URL opened in browser for the user to grab their key, or omitted to skip onAuth. */
|
|
31
|
+
authUrl?: string;
|
|
32
|
+
/** Instructions shown with the onAuth callback, or omitted to skip onAuth. */
|
|
33
|
+
instructions?: string;
|
|
34
34
|
/** Prompt message shown when asking for the key paste. */
|
|
35
35
|
promptMessage: string;
|
|
36
36
|
/** Placeholder string for the prompt (e.g. "sk-...", "csk-..."). */
|
|
37
37
|
placeholder: string;
|
|
38
38
|
/** Validation strategy, or `null` to skip validation. */
|
|
39
39
|
validation: ChatCompletionsValidation | AnthropicMessagesValidation | ModelsEndpointValidation | null;
|
|
40
|
+
/** Value returned for an empty key; also allows an empty prompt response. */
|
|
41
|
+
emptyKeyFallback?: string;
|
|
40
42
|
};
|
|
41
43
|
export declare function createApiKeyLogin(config: ApiKeyLoginConfig): (options: OAuthController) => Promise<string>;
|
|
42
44
|
export {};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Login to Cloudflare AI Gateway.
|
|
4
4
|
*
|
|
5
5
|
* Opens browser to Cloudflare AI Gateway authentication docs and prompts for a gateway token/API key.
|
|
6
6
|
* Returns the API key directly (not OAuthCredentials - this isn't OAuth).
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
8
|
+
export declare const loginCloudflareAiGateway: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
9
9
|
export declare const cloudflareAiGatewayProvider: {
|
|
10
10
|
readonly id: "cloudflare-ai-gateway";
|
|
11
11
|
readonly name: "Cloudflare AI Gateway";
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Login to Kagi.
|
|
4
4
|
*
|
|
5
5
|
* Opens browser to API settings and prompts user to paste their API key.
|
|
6
6
|
* Returns the API key directly (not OAuthCredentials - this isn't OAuth).
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
8
|
+
export declare const loginKagi: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
9
9
|
export declare const kagiProvider: {
|
|
10
10
|
readonly id: "kagi";
|
|
11
11
|
readonly name: "Kagi";
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Login to LiteLLM.
|
|
4
4
|
*
|
|
5
5
|
* Opens browser to LiteLLM setup docs, prompts user to paste their API key.
|
|
6
6
|
* Returns the API key directly (not OAuthCredentials - this isn't OAuth).
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
8
|
+
export declare const loginLiteLLM: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
9
9
|
export declare const litellmProvider: {
|
|
10
10
|
readonly id: "litellm";
|
|
11
11
|
readonly name: "LiteLLM";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
|
+
export declare const loginLlamaCpp: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
3
3
|
export declare const llamaCppProvider: {
|
|
4
4
|
readonly id: "llama.cpp";
|
|
5
5
|
readonly name: "llama.cpp (Local OpenAI-compatible)";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
2
|
export declare const DEFAULT_LOCAL_TOKEN = "lm-studio-local";
|
|
3
|
-
export declare
|
|
3
|
+
export declare const loginLmStudio: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
4
4
|
export declare const lmStudioProvider: {
|
|
5
5
|
readonly id: "lm-studio";
|
|
6
6
|
readonly name: "LM Studio (Local OpenAI-compatible)";
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Login to Parallel.
|
|
4
4
|
*
|
|
5
5
|
* Opens browser to the API keys page, prompts the user to paste their API key,
|
|
6
6
|
* and returns the API key directly.
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
8
|
+
export declare const loginParallel: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
9
9
|
export declare const parallelProvider: {
|
|
10
10
|
readonly id: "parallel";
|
|
11
11
|
readonly name: "Parallel";
|
|
@@ -5,7 +5,7 @@ import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
|
5
5
|
* Opens browser to API keys page and prompts user to paste their API key.
|
|
6
6
|
* Returns the API key directly (not OAuthCredentials - this isn't OAuth).
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
8
|
+
export declare const loginTavily: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
9
9
|
export declare const tavilyProvider: {
|
|
10
10
|
readonly id: "tavily";
|
|
11
11
|
readonly name: "Tavily";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
|
+
export declare const loginVercelAiGateway: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
3
3
|
export declare const vercelAiGatewayProvider: {
|
|
4
4
|
readonly id: "vercel-ai-gateway";
|
|
5
5
|
readonly name: "Vercel AI Gateway";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare
|
|
1
|
+
import type { OAuthLoginCallbacks } from "./oauth/types.js";
|
|
2
|
+
export declare const loginVllm: (options: import("./oauth/index.js").OAuthController) => Promise<string>;
|
|
3
3
|
export declare const vllmProvider: {
|
|
4
4
|
readonly id: "vllm";
|
|
5
5
|
readonly name: "vLLM (Local OpenAI-compatible)";
|
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenAI Codex "saved rate limit reset" redemption client.
|
|
3
|
-
*
|
|
4
|
-
* OpenAI lets paid Codex accounts bank a usage-window reset and spend it on
|
|
5
|
-
* demand (announced 2026-06-11). The count is surfaced on `/wham/usage` as
|
|
6
|
-
* `rate_limit_reset_credits.available_count` (see `./openai-codex.ts`), but the
|
|
7
|
-
* actual credit objects and the redeem action live on two dedicated routes:
|
|
8
|
-
*
|
|
9
|
-
* GET /wham/rate-limit-reset-credits → list redeemable credits
|
|
10
|
-
* POST /wham/rate-limit-reset-credits/consume → spend one credit
|
|
11
|
-
* body: { credit_id, redeem_request_id, account_id? }
|
|
12
|
-
*
|
|
13
|
-
* `redeem_request_id` is a client-generated idempotency key (UUID). The consume
|
|
14
|
-
* response carries a `code`: `"reset"` on success, otherwise a business reason
|
|
15
|
-
* (`already_redeemed`, `no_credit`, `nothing_to_reset`).
|
|
16
|
-
*
|
|
17
|
-
* These are thin, dependency-light functions so both the interactive session
|
|
18
|
-
* (the `/usage reset` command + auto-redeem) and any out-of-band tooling can
|
|
19
|
-
* share one wire contract.
|
|
20
|
-
*/
|
|
21
1
|
import type { FetchImpl } from "../types.js";
|
|
22
2
|
/** A single redeemable (or already-spent) saved reset. */
|
|
23
3
|
export interface CodexResetCredit {
|
|
@@ -1 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import type { UsageStatus } from "../usage.js";
|
|
2
|
+
/** Milliseconds in one hour. */
|
|
3
|
+
export declare const HOUR_MS: number;
|
|
4
|
+
/** Milliseconds in one day. */
|
|
5
|
+
export declare const DAY_MS: number;
|
|
6
|
+
/** Milliseconds in one seven-day week. */
|
|
7
|
+
export declare const WEEK_MS: number;
|
|
8
|
+
/** Parses a finite positive epoch timestamp, tolerating seconds or milliseconds. */
|
|
9
|
+
export declare function parsePositiveTimestamp(value: unknown): number | undefined;
|
|
10
|
+
/** Parses an ISO timestamp into epoch milliseconds. */
|
|
11
|
+
export declare function parseIsoTimestamp(value: unknown): number | undefined;
|
|
12
|
+
/** Maps a used fraction to the standard quota status thresholds. */
|
|
13
|
+
export declare function usageStatus(usedFraction: number | undefined): UsageStatus;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@oh-my-pi/pi-ai",
|
|
4
|
-
"version": "17.2.
|
|
4
|
+
"version": "17.2.12",
|
|
5
5
|
"description": "Unified LLM API with automatic model discovery and provider configuration",
|
|
6
6
|
"homepage": "https://omp.sh",
|
|
7
7
|
"author": "Can Boluk",
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@bufbuild/protobuf": "^2.12.1",
|
|
41
|
-
"@oh-my-pi/omptype": "17.2.
|
|
42
|
-
"@oh-my-pi/pi-catalog": "17.2.
|
|
43
|
-
"@oh-my-pi/pi-utils": "17.2.
|
|
44
|
-
"@oh-my-pi/pi-wire": "17.2.
|
|
41
|
+
"@oh-my-pi/omptype": "17.2.12",
|
|
42
|
+
"@oh-my-pi/pi-catalog": "17.2.12",
|
|
43
|
+
"@oh-my-pi/pi-utils": "17.2.12",
|
|
44
|
+
"@oh-my-pi/pi-wire": "17.2.12"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@bufbuild/protoc-gen-es": "^2.12.1",
|
package/src/auth-retry.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { extractHttpStatusFromError } from "@oh-my-pi/pi-utils";
|
|
|
2
2
|
import type { OAuthAccess } from "./auth-storage";
|
|
3
3
|
import * as AIError from "./error";
|
|
4
4
|
import { isAuthRetryableError, isInvalidatedOAuthTokenError } from "./error/auth-classify";
|
|
5
|
-
import { isUsageLimit } from "./error/flags";
|
|
5
|
+
import { isAccountPolicyError, isUsageLimit } from "./error/flags";
|
|
6
6
|
import { isConcurrencyCapExclusion, isUsageLimitOutcome } from "./error/rate-limit";
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -19,9 +19,9 @@ import { isConcurrencyCapExclusion, isUsageLimitOutcome } from "./error/rate-lim
|
|
|
19
19
|
* (invalidate/usage-limit the current credential and rotate to a sibling).
|
|
20
20
|
*
|
|
21
21
|
* Current drivers preserve that bounded a/b/c sequence for ordinary 401/auth
|
|
22
|
-
* failures.
|
|
23
|
-
* until the resolver returns `undefined`,
|
|
24
|
-
* {@link AUTH_RETRY_MAX_ATTEMPTS}.
|
|
22
|
+
* failures. Account-scoped policy denials, 403s, and usage-limit failures skip
|
|
23
|
+
* refresh and may repeat step (c) until the resolver returns `undefined`,
|
|
24
|
+
* cycles, or hits {@link AUTH_RETRY_MAX_ATTEMPTS}.
|
|
25
25
|
*/
|
|
26
26
|
export interface ApiKeyResolveContext {
|
|
27
27
|
/** True when the resolver should rotate to a sibling credential. */
|
|
@@ -91,6 +91,7 @@ export const AUTH_RETRY_STEPS: readonly boolean[] = [false, true];
|
|
|
91
91
|
export const AUTH_RETRY_MAX_ATTEMPTS = 64;
|
|
92
92
|
|
|
93
93
|
function isDirectCredentialRotationError(error: unknown): boolean {
|
|
94
|
+
if (isAccountPolicyError(error)) return true;
|
|
94
95
|
if (isUsageLimit(error) || isInvalidatedOAuthTokenError(error)) return true;
|
|
95
96
|
const status = AIError.status(error);
|
|
96
97
|
const message = error instanceof Error ? error.message : typeof error === "string" ? error : undefined;
|
package/src/auth-storage.ts
CHANGED
|
@@ -1211,6 +1211,14 @@ type OAuthCandidate = UsageCandidate<OAuthCredential>;
|
|
|
1211
1211
|
type ApiKeyCandidate = UsageCandidate<ApiKeyCredential>;
|
|
1212
1212
|
type UsageRankingResult<T extends AuthCredential> = UsageCandidate<T> & { blockedUntil: number | undefined };
|
|
1213
1213
|
|
|
1214
|
+
type CredentialBlockRouting = {
|
|
1215
|
+
providerKey: string;
|
|
1216
|
+
strategy: CredentialRankingStrategy | undefined;
|
|
1217
|
+
rankingContext: CredentialRankingContext;
|
|
1218
|
+
blockScope: string | undefined;
|
|
1219
|
+
siblingBlockScopes: readonly string[];
|
|
1220
|
+
};
|
|
1221
|
+
|
|
1214
1222
|
type UsageRankedCandidate<T extends AuthCredential> = UsageCandidate<T> & {
|
|
1215
1223
|
blocked: boolean;
|
|
1216
1224
|
blockedUntil?: number;
|
|
@@ -4271,6 +4279,58 @@ export class AuthStorage {
|
|
|
4271
4279
|
return sessionCredential ? { ...sessionCredential, explicit: false } : undefined;
|
|
4272
4280
|
}
|
|
4273
4281
|
|
|
4282
|
+
#credentialBlockRouting(
|
|
4283
|
+
provider: string,
|
|
4284
|
+
credentialType: AuthCredential["type"],
|
|
4285
|
+
modelId: string | undefined,
|
|
4286
|
+
): CredentialBlockRouting {
|
|
4287
|
+
const providerKey = this.#getProviderTypeKey(provider, credentialType);
|
|
4288
|
+
const strategy = this.#rankingStrategyResolver?.(provider);
|
|
4289
|
+
const rankingContext: CredentialRankingContext = { modelId };
|
|
4290
|
+
const blockScope = strategy?.blockScope?.(rankingContext);
|
|
4291
|
+
return {
|
|
4292
|
+
providerKey,
|
|
4293
|
+
strategy,
|
|
4294
|
+
rankingContext,
|
|
4295
|
+
blockScope,
|
|
4296
|
+
siblingBlockScopes: strategy?.blockScopes?.(rankingContext) ?? (blockScope ? [blockScope] : []),
|
|
4297
|
+
};
|
|
4298
|
+
}
|
|
4299
|
+
|
|
4300
|
+
#blockCredentialForRotation(
|
|
4301
|
+
provider: string,
|
|
4302
|
+
credentialType: AuthCredential["type"],
|
|
4303
|
+
targetIndex: number,
|
|
4304
|
+
blockedUntil: number,
|
|
4305
|
+
routing: CredentialBlockRouting,
|
|
4306
|
+
): UsageLimitMarkResult {
|
|
4307
|
+
if (targetIndex >= 0) {
|
|
4308
|
+
this.#markCredentialBlocked(provider, routing.providerKey, targetIndex, blockedUntil, routing.blockScope);
|
|
4309
|
+
}
|
|
4310
|
+
|
|
4311
|
+
const remainingCredentials = this.#getCredentialsForProvider(provider)
|
|
4312
|
+
.map((credential, index) => ({ credential, index }))
|
|
4313
|
+
.filter(
|
|
4314
|
+
(entry): entry is { credential: AuthCredential; index: number } =>
|
|
4315
|
+
entry.credential.type === credentialType && entry.index !== targetIndex,
|
|
4316
|
+
);
|
|
4317
|
+
|
|
4318
|
+
let retryAtMs: number | undefined;
|
|
4319
|
+
for (const candidate of remainingCredentials) {
|
|
4320
|
+
// Sibling availability must use the same scope set selection reads, or
|
|
4321
|
+
// this reports a sibling as free that selection will then refuse.
|
|
4322
|
+
const candidateBlockedUntil = this.#getCredentialBlockedUntil(
|
|
4323
|
+
provider,
|
|
4324
|
+
routing.providerKey,
|
|
4325
|
+
candidate.index,
|
|
4326
|
+
routing.siblingBlockScopes,
|
|
4327
|
+
);
|
|
4328
|
+
if (candidateBlockedUntil === undefined) return { switched: true };
|
|
4329
|
+
if (retryAtMs === undefined || candidateBlockedUntil < retryAtMs) retryAtMs = candidateBlockedUntil;
|
|
4330
|
+
}
|
|
4331
|
+
return { switched: false, retryAtMs };
|
|
4332
|
+
}
|
|
4333
|
+
|
|
4274
4334
|
/**
|
|
4275
4335
|
* Marks the current session's credential as temporarily blocked due to usage limits.
|
|
4276
4336
|
* Uses usage reports to determine accurate reset time when available.
|
|
@@ -4313,21 +4373,17 @@ export class AuthStorage {
|
|
|
4313
4373
|
const credentialType = sessionCredential.type;
|
|
4314
4374
|
const targetCredentialId = target.id;
|
|
4315
4375
|
|
|
4316
|
-
const
|
|
4317
|
-
const strategy = this.#rankingStrategyResolver?.(provider);
|
|
4318
|
-
const rankingContext: CredentialRankingContext = { modelId: options?.modelId };
|
|
4319
|
-
const blockScope = strategy?.blockScope?.(rankingContext);
|
|
4320
|
-
const siblingBlockScopes = strategy?.blockScopes?.(rankingContext) ?? (blockScope ? [blockScope] : []);
|
|
4376
|
+
const routing = this.#credentialBlockRouting(provider, credentialType, options?.modelId);
|
|
4321
4377
|
const now = Date.now();
|
|
4322
4378
|
let blockedUntil = now + (options?.retryAfterMs ?? AuthStorage.#defaultBackoffMs);
|
|
4323
4379
|
|
|
4324
|
-
if (credentialType === "oauth" && target.credential.type === "oauth" && strategy) {
|
|
4380
|
+
if (credentialType === "oauth" && target.credential.type === "oauth" && routing.strategy) {
|
|
4325
4381
|
const report = await raceUsageWithSignal(
|
|
4326
4382
|
this.#getUsageReport(provider, target.credential, options),
|
|
4327
4383
|
options?.signal,
|
|
4328
4384
|
);
|
|
4329
4385
|
if (report) {
|
|
4330
|
-
const scopedLimits = this.#getScopedUsageLimits(strategy, report, rankingContext);
|
|
4386
|
+
const scopedLimits = this.#getScopedUsageLimits(routing.strategy, report, routing.rankingContext);
|
|
4331
4387
|
if (this.#isUsageLimitReached(scopedLimits)) {
|
|
4332
4388
|
const resetAtMs = this.#getUsageResetAtMs(scopedLimits, Date.now());
|
|
4333
4389
|
if (resetAtMs && resetAtMs > blockedUntil) {
|
|
@@ -4343,32 +4399,7 @@ export class AuthStorage {
|
|
|
4343
4399
|
const targetIndex = this.#getStoredCredentials(provider).findIndex(
|
|
4344
4400
|
entry => entry.id === targetCredentialId && entry.credential.type === credentialType,
|
|
4345
4401
|
);
|
|
4346
|
-
|
|
4347
|
-
this.#markCredentialBlocked(provider, providerKey, targetIndex, blockedUntil, blockScope);
|
|
4348
|
-
}
|
|
4349
|
-
|
|
4350
|
-
const remainingCredentials = this.#getCredentialsForProvider(provider)
|
|
4351
|
-
.map((credential, index) => ({ credential, index }))
|
|
4352
|
-
.filter(
|
|
4353
|
-
(entry): entry is { credential: AuthCredential; index: number } =>
|
|
4354
|
-
entry.credential.type === credentialType && entry.index !== targetIndex,
|
|
4355
|
-
);
|
|
4356
|
-
|
|
4357
|
-
let retryAtMs: number | undefined;
|
|
4358
|
-
for (const candidate of remainingCredentials) {
|
|
4359
|
-
// Sibling availability must use the same scope set selection reads, or
|
|
4360
|
-
// this reports a sibling as free that selection will then refuse, most
|
|
4361
|
-
// visibly when the sibling still carries a legacy shared block.
|
|
4362
|
-
const candidateBlockedUntil = this.#getCredentialBlockedUntil(
|
|
4363
|
-
provider,
|
|
4364
|
-
providerKey,
|
|
4365
|
-
candidate.index,
|
|
4366
|
-
siblingBlockScopes,
|
|
4367
|
-
);
|
|
4368
|
-
if (candidateBlockedUntil === undefined) return { switched: true };
|
|
4369
|
-
if (retryAtMs === undefined || candidateBlockedUntil < retryAtMs) retryAtMs = candidateBlockedUntil;
|
|
4370
|
-
}
|
|
4371
|
-
return { switched: false, retryAtMs };
|
|
4402
|
+
return this.#blockCredentialForRotation(provider, credentialType, targetIndex, blockedUntil, routing);
|
|
4372
4403
|
}
|
|
4373
4404
|
|
|
4374
4405
|
#resolveWindowResetAt(window: UsageLimit["window"]): number | undefined {
|
|
@@ -6078,6 +6109,8 @@ export class AuthStorage {
|
|
|
6078
6109
|
* - usage-limit / account-rate-limit error → {@link AuthStorage.markUsageLimitReached}
|
|
6079
6110
|
* (temporary block via its own backoff — default plus server usage-report
|
|
6080
6111
|
* reset; sticky left intact so the next resolve re-ranks around the block).
|
|
6112
|
+
* - account-scoped policy denial → temporarily block that account without
|
|
6113
|
+
* marking its credential suspect, then rotate through eligible siblings.
|
|
6081
6114
|
* - otherwise (hard 401 / auth failure) → mark the credential suspect (or
|
|
6082
6115
|
* reload when no broker hook is wired) and block it, then drop matching
|
|
6083
6116
|
* sticky state.
|
|
@@ -6114,6 +6147,17 @@ export class AuthStorage {
|
|
|
6114
6147
|
});
|
|
6115
6148
|
if (!sessionCredential) return false;
|
|
6116
6149
|
|
|
6150
|
+
if (AIError.isAccountPolicyError(error)) {
|
|
6151
|
+
const routing = this.#credentialBlockRouting(provider, sessionCredential.type, options?.modelId);
|
|
6152
|
+
return this.#blockCredentialForRotation(
|
|
6153
|
+
provider,
|
|
6154
|
+
sessionCredential.type,
|
|
6155
|
+
sessionCredential.index,
|
|
6156
|
+
Date.now() + AuthStorage.#defaultBackoffMs,
|
|
6157
|
+
routing,
|
|
6158
|
+
).switched;
|
|
6159
|
+
}
|
|
6160
|
+
|
|
6117
6161
|
const providerKey = this.#getProviderTypeKey(provider, sessionCredential.type);
|
|
6118
6162
|
// Snapshot sibling availability before mutating so a soft-deleting
|
|
6119
6163
|
// suspect hook can't reindex the answer out from under us.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { extractHttpStatusFromError } from "@oh-my-pi/pi-utils";
|
|
2
|
-
import { isOAuthExpiry, isUsageLimit } from "./flags";
|
|
2
|
+
import { isAccountPolicyError, isOAuthExpiry, isUsageLimit } from "./flags";
|
|
3
3
|
import { isConcurrencyCapExclusion, isUsageLimitOutcome } from "./rate-limit";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -27,15 +27,16 @@ export function isInvalidatedOAuthTokenError(error: unknown): boolean {
|
|
|
27
27
|
/**
|
|
28
28
|
* Whether an upstream failure should rotate to a sibling credential: a hard
|
|
29
29
|
* `401`, a `403` (token valid but access denied — plan, model policy, or org
|
|
30
|
-
* restriction a sibling account may not share),
|
|
31
|
-
*
|
|
30
|
+
* restriction a sibling account may not share), an account-scoped policy
|
|
31
|
+
* denial such as Codex `cyber_policy`, a body-classified usage limit (Codex
|
|
32
|
+
* `usage_limit_reached`, Anthropic account rate-limit, Google
|
|
32
33
|
* `resource_exhausted`, OpenAI `insufficient_quota`, …), or a bare `429`
|
|
33
|
-
* whose payload did not preserve a richer quota code.
|
|
34
|
-
*
|
|
35
|
-
* upstream-backoff lane.
|
|
34
|
+
* whose payload did not preserve a richer quota code. Transient 429s
|
|
35
|
+
* (`Too many requests`, per-minute caps) stay in the upstream-backoff lane.
|
|
36
36
|
*/
|
|
37
37
|
export function isAuthRetryableError(error: unknown): boolean {
|
|
38
38
|
if (isUsageLimit(error)) return true;
|
|
39
|
+
if (isAccountPolicyError(error)) return true;
|
|
39
40
|
if (isInvalidatedOAuthTokenError(error)) return true;
|
|
40
41
|
const httpStatus = extractHttpStatusFromError(error);
|
|
41
42
|
const message = error instanceof Error ? error.message : typeof error === "string" ? error : undefined;
|
package/src/error/flags.ts
CHANGED
|
@@ -25,6 +25,8 @@ export const Flag = {
|
|
|
25
25
|
MalformedFunctionCall: 0x0020_0000,
|
|
26
26
|
ProviderFinishError: 0x0040_0000,
|
|
27
27
|
ContentBlocked: 0x0000_8000,
|
|
28
|
+
/** Account-scoped provider policy denial that may succeed with another credential. */
|
|
29
|
+
AccountPolicy: 0x0000_4000,
|
|
28
30
|
ContextOverflow: 0x0080_0000,
|
|
29
31
|
AuthFailed: 0x0100_0000,
|
|
30
32
|
SilentAbort: 0x0200_0000,
|
|
@@ -49,6 +51,7 @@ const KIND_MASK =
|
|
|
49
51
|
Flag.MalformedFunctionCall |
|
|
50
52
|
Flag.ProviderFinishError |
|
|
51
53
|
Flag.ContentBlocked |
|
|
54
|
+
Flag.AccountPolicy |
|
|
52
55
|
Flag.ContextOverflow |
|
|
53
56
|
Flag.AuthFailed |
|
|
54
57
|
Flag.SilentAbort |
|
|
@@ -102,6 +105,7 @@ const AUTH_FAILURE_PATTERN =
|
|
|
102
105
|
const MALFORMED_FUNCTION_CALL_PATTERN = /\bmalformed.?function.?call\b/i;
|
|
103
106
|
const PROVIDER_FINISH_ERROR_PATTERN = /\bProvider (?:returned error finish_reason|finish_reason:\s*error)\b/i;
|
|
104
107
|
const CONTENT_FILTER_PATTERN = /\b(?:incomplete:\s*)?content_filter\b/i;
|
|
108
|
+
const ACCOUNT_POLICY_PATTERN = /\bcyber_policy\b|trusted access for cyber/i;
|
|
105
109
|
const STALE_RESPONSE_ITEM_PATTERNS = [/\bItem with id ['"][^'"]+['"] not found\.?/i, /previous[ _]?response/i] as const;
|
|
106
110
|
const STALE_RESPONSE_ITEM_DETAIL_PATTERN = /not[ _]?found|invalid|expired|stale|zero[ _-]?data[ _-]?retention/i;
|
|
107
111
|
/**
|
|
@@ -194,6 +198,7 @@ const ERROR_KIND_LABELS: readonly [Flag, string][] = [
|
|
|
194
198
|
[Flag.MalformedFunctionCall, "malformed-function-call"],
|
|
195
199
|
[Flag.ProviderFinishError, "provider-finish-error"],
|
|
196
200
|
[Flag.ContentBlocked, "content-blocked"],
|
|
201
|
+
[Flag.AccountPolicy, "account-policy"],
|
|
197
202
|
[Flag.ContextOverflow, "context-overflow"],
|
|
198
203
|
[Flag.AuthFailed, "auth-failed"],
|
|
199
204
|
[Flag.SilentAbort, "silent-abort"],
|
|
@@ -336,6 +341,7 @@ function classifyText(errorMessage: string | undefined, errorStatus: number | un
|
|
|
336
341
|
if (isMalformedFunctionCallText(errorMessage)) kinds |= Flag.MalformedFunctionCall;
|
|
337
342
|
if (isProviderFinishErrorText(errorMessage)) kinds |= Flag.ProviderFinishError;
|
|
338
343
|
if (isContentBlockedText(errorMessage)) kinds |= Flag.ContentBlocked;
|
|
344
|
+
if (ACCOUNT_POLICY_PATTERN.test(errorMessage)) kinds |= Flag.AccountPolicy | Flag.ContentBlocked;
|
|
339
345
|
if (isAuthFailureText(errorMessage)) kinds |= Flag.AuthFailed;
|
|
340
346
|
|
|
341
347
|
const statusClean = errorStatus ? errorStatus : (status({ message: errorMessage }) ?? undefined);
|
|
@@ -398,6 +404,9 @@ export function classify(error: unknown, api?: Api): number {
|
|
|
398
404
|
if ("errorId" in link && typeof (link as { errorId: unknown }).errorId === "number") {
|
|
399
405
|
kinds |= (link as { errorId: number }).errorId & KIND_MASK;
|
|
400
406
|
}
|
|
407
|
+
if ("code" in link && typeof link.code === "string" && ACCOUNT_POLICY_PATTERN.test(link.code)) {
|
|
408
|
+
kinds |= Flag.AccountPolicy | Flag.ContentBlocked;
|
|
409
|
+
}
|
|
401
410
|
}
|
|
402
411
|
|
|
403
412
|
if (link instanceof AwsCredentialsError) {
|
|
@@ -475,6 +484,11 @@ export function isUsageLimit(error: unknown, api?: Api): boolean {
|
|
|
475
484
|
return is(classify(error, api), Flag.UsageLimit);
|
|
476
485
|
}
|
|
477
486
|
|
|
487
|
+
/** Whether an upstream rejection is an account-scoped policy denial worth retrying with a sibling credential. */
|
|
488
|
+
export function isAccountPolicyError(error: unknown, api?: Api): boolean {
|
|
489
|
+
return is(classify(error, api), Flag.AccountPolicy);
|
|
490
|
+
}
|
|
491
|
+
|
|
478
492
|
/**
|
|
479
493
|
* Strict-tool rejection: grammar too large, schema too complex, or structured
|
|
480
494
|
* outputs unsupported by the model/endpoint.
|
|
@@ -38,16 +38,18 @@ type ModelsEndpointValidation = {
|
|
|
38
38
|
export type ApiKeyLoginConfig = {
|
|
39
39
|
/** Display name used in error messages, e.g. "Cerebras", "NanoGPT". */
|
|
40
40
|
providerLabel: string;
|
|
41
|
-
/** URL opened in browser for the user to grab their key. */
|
|
42
|
-
authUrl
|
|
43
|
-
/** Instructions shown with the onAuth callback. */
|
|
44
|
-
instructions
|
|
41
|
+
/** URL opened in browser for the user to grab their key, or omitted to skip onAuth. */
|
|
42
|
+
authUrl?: string;
|
|
43
|
+
/** Instructions shown with the onAuth callback, or omitted to skip onAuth. */
|
|
44
|
+
instructions?: string;
|
|
45
45
|
/** Prompt message shown when asking for the key paste. */
|
|
46
46
|
promptMessage: string;
|
|
47
47
|
/** Placeholder string for the prompt (e.g. "sk-...", "csk-..."). */
|
|
48
48
|
placeholder: string;
|
|
49
49
|
/** Validation strategy, or `null` to skip validation. */
|
|
50
50
|
validation: ChatCompletionsValidation | AnthropicMessagesValidation | ModelsEndpointValidation | null;
|
|
51
|
+
/** Value returned for an empty key; also allows an empty prompt response. */
|
|
52
|
+
emptyKeyFallback?: string;
|
|
51
53
|
};
|
|
52
54
|
|
|
53
55
|
export function createApiKeyLogin(config: ApiKeyLoginConfig): (options: OAuthController) => Promise<string> {
|
|
@@ -56,15 +58,24 @@ export function createApiKeyLogin(config: ApiKeyLoginConfig): (options: OAuthCon
|
|
|
56
58
|
throw new AIError.OnPromptRequiredError(config.providerLabel);
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
if (config.authUrl && config.instructions) {
|
|
62
|
+
options.onAuth?.({
|
|
63
|
+
url: config.authUrl,
|
|
64
|
+
instructions: config.instructions,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
63
67
|
|
|
64
|
-
const apiKey =
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
const apiKey =
|
|
69
|
+
config.emptyKeyFallback === undefined
|
|
70
|
+
? await options.onPrompt({
|
|
71
|
+
message: config.promptMessage,
|
|
72
|
+
placeholder: config.placeholder,
|
|
73
|
+
})
|
|
74
|
+
: await options.onPrompt({
|
|
75
|
+
message: config.promptMessage,
|
|
76
|
+
placeholder: config.placeholder,
|
|
77
|
+
allowEmpty: true,
|
|
78
|
+
});
|
|
68
79
|
|
|
69
80
|
if (options.signal?.aborted) {
|
|
70
81
|
throw new AIError.LoginCancelledError();
|
|
@@ -72,6 +83,9 @@ export function createApiKeyLogin(config: ApiKeyLoginConfig): (options: OAuthCon
|
|
|
72
83
|
|
|
73
84
|
const trimmed = apiKey.trim();
|
|
74
85
|
if (!trimmed) {
|
|
86
|
+
if (config.emptyKeyFallback !== undefined) {
|
|
87
|
+
return config.emptyKeyFallback;
|
|
88
|
+
}
|
|
75
89
|
throw new AIError.ApiKeyRequiredError();
|
|
76
90
|
}
|
|
77
91
|
|