@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
69 lines • 3.63 kB
JavaScript
import { sanitizeProviderDiagnostic, } from "@earendil-works/pi-ai";
import { isThinkingBindingMismatchText } from "@earendil-works/pi-ai/api/anthropic-thinking-binding";
import { AllAccountsBlockedError } from "./affinity.js";
import { classifySdkError } from "./errors.js";
import { ClassifiedSdkError } from "./failover.js";
/** The allowlisted provider token each classified SDK failure proves; kinds that prove nothing map to none. */
const SDK_KIND_DIAGNOSTICS = {
auth_error: { category: "auth", code: "authentication_error" },
login_unreadable: { category: "auth", code: "authentication_error" },
rate_limit: { category: "rate_limit", code: "rate_limit_error" },
overloaded: { category: "provider_unavailable", code: "overloaded_error" },
billing: { category: "unknown", code: "billing_error" },
org_not_allowed: { category: "unknown", code: "permission_error" },
};
const CAUSE_DEPTH = 8;
/** The pool refusal behind `error`: the error itself or one it carries as `cause`, so wrapping never hides it. */
function poolRefusal(error) {
let current = error;
for (let depth = 0; depth < CAUSE_DEPTH && current instanceof Error; depth++) {
if (current instanceof AllAccountsBlockedError)
return current;
current = current.cause;
}
return undefined;
}
/**
* The providerDiagnostic for an Anthropic Subscription turn failure (senpi#4672), mirroring the
* chatgpt-subscription adapter (senpi#3771). A pool the extension refused locally is `auth` when every
* account waits on a new login, and `rate_limit` with the soonest reset only when every account that can
* come back on its own is rate-limited (senpi#3975); a pool held by billing, organization, or unknown blocks
* gets no diagnostic, so a goal never parks on it. A classified SDK failure maps to its provider token. The result is revalidated against the closed vocabulary, so nothing else escapes.
*/
export function anthropicSubscriptionDiagnostic(error) {
const pool = poolRefusal(error);
if (pool !== undefined) {
// A pin to an account that is no longer configured is a configuration error, not a limit: no diagnostic.
if (pool.unavailableAccount?.configured === false)
return undefined;
if (pool.blockReason === "auth_error") {
return sanitizeProviderDiagnostic({
category: "auth",
code: "account_pool_auth_blocked",
evidence: "structured_code",
});
}
if (pool.rateLimitedUntil === undefined)
return undefined;
return sanitizeProviderDiagnostic({
category: "rate_limit",
code: "account_pool_blocked",
evidence: "structured_code",
resetsAt: pool.rateLimitedUntil,
});
}
const original = error instanceof ClassifiedSdkError ? error.original : error;
if (original instanceof Error && isThinkingBindingMismatchText(original.message)) {
// senpi#5587: the API refused a replayed thinking block bound to a different conversation.
return sanitizeProviderDiagnostic({
category: "invalid_request",
httpStatus: 400,
code: "invalid_request_error",
evidence: "structured_code",
});
}
const kind = error instanceof ClassifiedSdkError ? error.classification.kind : classifySdkError(error).kind;
const mapped = SDK_KIND_DIAGNOSTICS[kind];
return mapped === undefined ? undefined : sanitizeProviderDiagnostic({ ...mapped, evidence: "structured_code" });
}
//# sourceMappingURL=provider-diagnostic.js.map