@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
59 lines • 4.07 kB
JavaScript
import { AllAccountsBlockedError } from "./affinity.js";
import { errorAccountContext } from "./account-context.js";
import { classifySdkError } from "./errors.js";
import { ClassifiedSdkError } from "./failover.js";
import { allAccountsBlockedGuidance, claudeCodeVersionFloorGuidance, exhaustedPoolGuidance, sdkErrorGuidance, singleAccountUnavailableGuidance, switchAccountLine, } from "./guidance.js";
import { ClaudeSignInRequiredError } from "./sign-in-again.js";
export function withAuthGuidance(error, message, ran, now = Date.now()) {
// The sign-in-again prompt is the remedy; the generic auth_error hint ("token was rejected... refresh") contradicts it.
if (error instanceof ClaudeSignInRequiredError)
return message;
if (error instanceof AllAccountsBlockedError) {
const guidance = error.unavailableAccount === undefined
? allAccountsBlockedGuidance(error.soonestUnblockAt, error.blockReason, error.accounts)
: singleAccountUnavailableGuidance(error.unavailableAccount, error.soonestUnblockAt, error.blockReason);
// The model-limit sentence is how the fallback chain learns the limit binds this model only (senpi#5007).
// A pool that recovers on its own names its soonest retry, so the session's retry waits for it instead of
// spending its budget on local refusals that cannot change before then (senpi#6727).
return error.limitedModel === undefined
? `${guidance}${retryAfterMarker(error.rateLimitedUntil, now)}`
: `${modelLimitSentence(error, error.limitedModel, now)}\n${guidance}${retryAfterMarker(error.soonestUnblockAt, now)}`;
}
const kind = classifySdkError(error).kind;
const account = errorAccountContext(error instanceof ClassifiedSdkError ? error.original : error);
let guidance = sdkErrorGuidance(kind);
if (account && (kind === "billing" || /\bout of extra usage\b/i.test(message))) {
const selected = `Claude account '${account.accountLabel}' (source: ${account.accountSource}).`;
const action = account.accountSource === "ambient"
? "Switch the Claude Code login, or top up at claude.ai/settings/usage."
: `${switchAccountLine(account.pinSource).trim()} Or top up this account at claude.ai/settings/usage.`;
guidance = `${selected} ${action}`;
}
const versionGuidance = claudeCodeVersionFloorGuidance(message, ran);
const pool = error instanceof ClassifiedSdkError ? error.poolState : undefined;
const poolGuidance = pool && exhaustedPoolGuidance(pool.soonestUnblockAt, pool.accounts);
const hints = [guidance, versionGuidance, poolGuidance].filter((hint) => hint !== undefined);
const text = hints.length > 0 ? `${message}\n${hints.join("\n")}` : message;
// The last account's failure left no account to try: the next one is free at the pool's soonest retry (senpi#6727).
return `${text}${retryAfterMarker(pool?.rateLimitedUntil, now)}`;
}
/**
* Names the limit, the model and its reset as relative prose, the form `usageLimitResetMs` (the
* fallback hold) reads.
*/
function modelLimitSentence(error, model, now) {
const subject = error.unavailableAccount === undefined
? "All Anthropic Subscription accounts have"
: `Claude account '${error.unavailableAccount.name}' has`;
const until = error.soonestUnblockAt;
if (until === undefined || !Number.isFinite(until))
return `${subject} hit the usage limit for model ${model}.`;
const waitMs = Math.max(0, until - now);
const minutes = Math.ceil(waitMs / 60_000);
return `${subject} hit the usage limit for model ${model}; it resets in ${Math.floor(minutes / 60)} hours ${minutes % 60} minutes, at ${new Date(until).toISOString()}.`;
}
/** The session's retry-delay reader only takes the marker at the very end of the text. */
function retryAfterMarker(until, now) {
return until === undefined || !Number.isFinite(until) ? "" : ` (retry-after-ms: ${Math.max(0, until - now)})`;
}
//# sourceMappingURL=stream-guidance.js.map