|
| 1 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 2 | +import { formatErrorMessage } from "../../infra/errors.js"; |
| 3 | +import type { FailoverReason } from "../embedded-agent-helpers/types.js"; |
| 4 | +import { buildProviderAuthRecoveryHint } from "../provider-auth-recovery-hint.js"; |
| 5 | + |
| 6 | +export type AuthProfileFailureCopyParams = { |
| 7 | + reason: FailoverReason; |
| 8 | + provider: string; |
| 9 | + /** |
| 10 | + * True when the failure was reached because every configured profile is in |
| 11 | + * cooldown / blocked. False when an attempt to use a specific profile threw |
| 12 | + * (e.g. credential lookup failed). The two paths produce different copy |
| 13 | + * because only the cooldown case implies "wait or rotate"; the other case |
| 14 | + * implies "the credential itself is broken". |
| 15 | + */ |
| 16 | + allInCooldown: boolean; |
| 17 | + /** |
| 18 | + * Underlying error that triggered the failover, if any. Used to append a |
| 19 | + * short diagnostic suffix and to fall back to the original message when no |
| 20 | + * structured recovery copy applies. |
| 21 | + */ |
| 22 | + cause?: unknown; |
| 23 | + config?: OpenClawConfig; |
| 24 | + workspaceDir?: string; |
| 25 | + env?: NodeJS.ProcessEnv; |
| 26 | +}; |
| 27 | + |
| 28 | +function describeReason( |
| 29 | + reason: FailoverReason, |
| 30 | + provider: string, |
| 31 | + allInCooldown: boolean, |
| 32 | +): string | null { |
| 33 | + if (allInCooldown) { |
| 34 | + switch (reason) { |
| 35 | + case "auth": |
| 36 | + case "session_expired": |
| 37 | + return `Every auth profile for ${provider} is currently failing authentication; sessions look expired or credentials were rejected.`; |
| 38 | + case "auth_permanent": |
| 39 | + return `Every auth profile for ${provider} has been permanently denied by the provider.`; |
| 40 | + case "billing": |
| 41 | + return `Every auth profile for ${provider} is blocked for billing on the provider account.`; |
| 42 | + case "rate_limit": |
| 43 | + return `Every auth profile for ${provider} is cooling down after recent rate-limit responses.`; |
| 44 | + case "overloaded": |
| 45 | + return `Every auth profile for ${provider} is cooling down while the provider is reporting overload.`; |
| 46 | + case "timeout": |
| 47 | + return `Every auth profile for ${provider} is cooling down after recent requests timed out.`; |
| 48 | + case "model_not_found": |
| 49 | + return `Every auth profile for ${provider} was rejected with a model-not-found error.`; |
| 50 | + case "server_error": |
| 51 | + return `Every auth profile for ${provider} is cooling down after recent provider server errors.`; |
| 52 | + default: |
| 53 | + return `No ${provider} auth profile is currently available; all are in cooldown or blocked.`; |
| 54 | + } |
| 55 | + } |
| 56 | + switch (reason) { |
| 57 | + case "auth": |
| 58 | + case "session_expired": |
| 59 | + return `Authentication with ${provider} did not succeed.`; |
| 60 | + case "auth_permanent": |
| 61 | + return `Authentication with ${provider} was permanently denied.`; |
| 62 | + case "billing": |
| 63 | + return `Provider ${provider} reported a billing problem on this account.`; |
| 64 | + default: |
| 65 | + return null; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function shouldIncludeRecoveryHint(reason: FailoverReason): boolean { |
| 70 | + switch (reason) { |
| 71 | + case "auth": |
| 72 | + case "auth_permanent": |
| 73 | + case "session_expired": |
| 74 | + case "billing": |
| 75 | + return true; |
| 76 | + case "rate_limit": |
| 77 | + case "overloaded": |
| 78 | + case "timeout": |
| 79 | + case "server_error": |
| 80 | + case "model_not_found": |
| 81 | + return false; |
| 82 | + default: |
| 83 | + return true; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function diagnosticSuffix(cause: unknown, primary: string): string | null { |
| 88 | + if (cause === undefined || cause === null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + const text = formatErrorMessage(cause).trim(); |
| 92 | + if (!text || primary.includes(text)) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + return ` (${text})`; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Single source of truth for user-facing copy when an auth-profile rotation |
| 100 | + * fails. Composes a reason-specific sentence with an actionable next-step |
| 101 | + * derived from the provider's plugin manifest (`buildProviderAuthRecoveryHint`). |
| 102 | + * |
| 103 | + * Falls back to the underlying error's text when the reason maps to nothing |
| 104 | + * actionable, so we never produce worse copy than the raw error. |
| 105 | + */ |
| 106 | +export function formatAuthProfileFailureMessage(params: AuthProfileFailureCopyParams): string { |
| 107 | + const description = describeReason(params.reason, params.provider, params.allInCooldown); |
| 108 | + if (!description) { |
| 109 | + const causeText = params.cause ? formatErrorMessage(params.cause).trim() : ""; |
| 110 | + if (causeText) { |
| 111 | + return causeText; |
| 112 | + } |
| 113 | + return `No ${params.provider} auth profile is currently available; all are in cooldown or blocked.`; |
| 114 | + } |
| 115 | + const hint = shouldIncludeRecoveryHint(params.reason) |
| 116 | + ? buildProviderAuthRecoveryHint({ |
| 117 | + provider: params.provider, |
| 118 | + config: params.config, |
| 119 | + workspaceDir: params.workspaceDir, |
| 120 | + env: params.env, |
| 121 | + }) |
| 122 | + : null; |
| 123 | + const suffix = diagnosticSuffix(params.cause, description); |
| 124 | + const parts = [description]; |
| 125 | + if (hint) { |
| 126 | + parts.push(hint); |
| 127 | + } |
| 128 | + const message = parts.join(" "); |
| 129 | + return suffix ? `${message}${suffix}` : message; |
| 130 | +} |
0 commit comments