mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 02:30:24 +00:00
223 lines
6.6 KiB
TypeScript
223 lines
6.6 KiB
TypeScript
import {
|
|
buildAuthHealthSummary,
|
|
DEFAULT_OAUTH_WARN_MS,
|
|
formatRemainingShort,
|
|
} from "../agents/auth-health.js";
|
|
import {
|
|
type AuthCredentialReasonCode,
|
|
ensureAuthProfileStore,
|
|
repairOAuthProfileIdMismatch,
|
|
resolveApiKeyForProfile,
|
|
resolveProfileUnusableUntilForDisplay,
|
|
} from "../agents/auth-profiles.js";
|
|
import { formatAuthDoctorHint } from "../agents/auth-profiles/doctor.js";
|
|
import type { OpenClawConfig } from "../config/config.js";
|
|
import { resolvePluginProviders } from "../plugins/providers.runtime.js";
|
|
import { note } from "../terminal/note.js";
|
|
import type { DoctorPrompter } from "./doctor-prompter.js";
|
|
import { buildProviderAuthRecoveryHint } from "./provider-auth-guidance.js";
|
|
|
|
export async function maybeRepairLegacyOAuthProfileIds(
|
|
cfg: OpenClawConfig,
|
|
prompter: DoctorPrompter,
|
|
): Promise<OpenClawConfig> {
|
|
const store = ensureAuthProfileStore();
|
|
let nextCfg = cfg;
|
|
const providers = resolvePluginProviders({
|
|
config: cfg,
|
|
env: process.env,
|
|
mode: "setup",
|
|
});
|
|
for (const provider of providers) {
|
|
for (const repairSpec of provider.oauthProfileIdRepairs ?? []) {
|
|
const repair = repairOAuthProfileIdMismatch({
|
|
cfg: nextCfg,
|
|
store,
|
|
provider: provider.id,
|
|
legacyProfileId: repairSpec.legacyProfileId,
|
|
});
|
|
if (!repair.migrated || repair.changes.length === 0) {
|
|
continue;
|
|
}
|
|
|
|
note(repair.changes.map((c) => `- ${c}`).join("\n"), "Auth profiles");
|
|
const apply = await prompter.confirm({
|
|
message: `Update ${repairSpec.promptLabel ?? provider.label} OAuth profile id in config now?`,
|
|
initialValue: true,
|
|
});
|
|
if (!apply) {
|
|
continue;
|
|
}
|
|
nextCfg = repair.config;
|
|
}
|
|
}
|
|
return nextCfg;
|
|
}
|
|
|
|
type AuthIssue = {
|
|
profileId: string;
|
|
provider: string;
|
|
status: string;
|
|
reasonCode?: AuthCredentialReasonCode;
|
|
remainingMs?: number;
|
|
};
|
|
|
|
export function resolveUnusableProfileHint(params: {
|
|
kind: "cooldown" | "disabled";
|
|
reason?: string;
|
|
}): string {
|
|
if (params.kind === "disabled") {
|
|
if (params.reason === "billing") {
|
|
return "Top up credits (provider billing) or switch provider.";
|
|
}
|
|
if (params.reason === "auth_permanent" || params.reason === "auth") {
|
|
return "Refresh or replace credentials, then retry.";
|
|
}
|
|
}
|
|
return "Wait for cooldown or switch provider.";
|
|
}
|
|
|
|
export async function resolveAuthIssueHint(
|
|
issue: AuthIssue,
|
|
cfg: OpenClawConfig,
|
|
store: ReturnType<typeof ensureAuthProfileStore>,
|
|
): Promise<string | null> {
|
|
if (issue.reasonCode === "invalid_expires") {
|
|
return "Invalid token expires metadata. Set a future Unix ms timestamp or remove expires.";
|
|
}
|
|
const providerHint = await formatAuthDoctorHint({
|
|
cfg,
|
|
store,
|
|
provider: issue.provider,
|
|
profileId: issue.profileId,
|
|
});
|
|
if (providerHint.trim()) {
|
|
return providerHint;
|
|
}
|
|
return buildProviderAuthRecoveryHint({
|
|
provider: issue.provider,
|
|
}).replace(/^Run /, "Re-auth via ");
|
|
}
|
|
|
|
async function formatAuthIssueLine(
|
|
issue: AuthIssue,
|
|
cfg: OpenClawConfig,
|
|
store: ReturnType<typeof ensureAuthProfileStore>,
|
|
): Promise<string> {
|
|
const remaining =
|
|
issue.remainingMs !== undefined ? ` (${formatRemainingShort(issue.remainingMs)})` : "";
|
|
const hint = await resolveAuthIssueHint(issue, cfg, store);
|
|
const reason = issue.reasonCode ? ` [${issue.reasonCode}]` : "";
|
|
return `- ${issue.profileId}: ${issue.status}${reason}${remaining}${hint ? ` — ${hint}` : ""}`;
|
|
}
|
|
|
|
export async function noteAuthProfileHealth(params: {
|
|
cfg: OpenClawConfig;
|
|
prompter: DoctorPrompter;
|
|
allowKeychainPrompt: boolean;
|
|
}): Promise<void> {
|
|
const store = ensureAuthProfileStore(undefined, {
|
|
allowKeychainPrompt: params.allowKeychainPrompt,
|
|
});
|
|
const unusable = (() => {
|
|
const now = Date.now();
|
|
const out: string[] = [];
|
|
for (const profileId of Object.keys(store.usageStats ?? {})) {
|
|
const until = resolveProfileUnusableUntilForDisplay(store, profileId);
|
|
if (!until || now >= until) {
|
|
continue;
|
|
}
|
|
const stats = store.usageStats?.[profileId];
|
|
const remaining = formatRemainingShort(until - now);
|
|
const disabledActive = typeof stats?.disabledUntil === "number" && now < stats.disabledUntil;
|
|
const kind = disabledActive
|
|
? `disabled${stats.disabledReason ? `:${stats.disabledReason}` : ""}`
|
|
: "cooldown";
|
|
const hint = resolveUnusableProfileHint({
|
|
kind: disabledActive ? "disabled" : "cooldown",
|
|
reason: stats?.disabledReason,
|
|
});
|
|
out.push(`- ${profileId}: ${kind} (${remaining})${hint ? ` — ${hint}` : ""}`);
|
|
}
|
|
return out;
|
|
})();
|
|
|
|
if (unusable.length > 0) {
|
|
note(unusable.join("\n"), "Auth profile cooldowns");
|
|
}
|
|
|
|
let summary = buildAuthHealthSummary({
|
|
store,
|
|
cfg: params.cfg,
|
|
warnAfterMs: DEFAULT_OAUTH_WARN_MS,
|
|
});
|
|
|
|
const findIssues = () =>
|
|
summary.profiles.filter(
|
|
(profile) =>
|
|
(profile.type === "oauth" || profile.type === "token") &&
|
|
(profile.status === "expired" ||
|
|
profile.status === "expiring" ||
|
|
profile.status === "missing"),
|
|
);
|
|
|
|
let issues = findIssues();
|
|
if (issues.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const shouldRefresh = await params.prompter.confirmAutoFix({
|
|
message: "Refresh expiring OAuth tokens now? (static tokens need re-auth)",
|
|
initialValue: true,
|
|
});
|
|
|
|
if (shouldRefresh) {
|
|
const refreshTargets = issues.filter(
|
|
(issue) =>
|
|
issue.type === "oauth" && ["expired", "expiring", "missing"].includes(issue.status),
|
|
);
|
|
const errors: string[] = [];
|
|
for (const profile of refreshTargets) {
|
|
try {
|
|
await resolveApiKeyForProfile({
|
|
cfg: params.cfg,
|
|
store,
|
|
profileId: profile.profileId,
|
|
});
|
|
} catch (err) {
|
|
errors.push(`- ${profile.profileId}: ${err instanceof Error ? err.message : String(err)}`);
|
|
}
|
|
}
|
|
if (errors.length > 0) {
|
|
note(errors.join("\n"), "OAuth refresh errors");
|
|
}
|
|
summary = buildAuthHealthSummary({
|
|
store: ensureAuthProfileStore(undefined, {
|
|
allowKeychainPrompt: false,
|
|
}),
|
|
cfg: params.cfg,
|
|
warnAfterMs: DEFAULT_OAUTH_WARN_MS,
|
|
});
|
|
issues = findIssues();
|
|
}
|
|
|
|
if (issues.length > 0) {
|
|
const issueLines = await Promise.all(
|
|
issues.map((issue) =>
|
|
formatAuthIssueLine(
|
|
{
|
|
profileId: issue.profileId,
|
|
provider: issue.provider,
|
|
status: issue.status,
|
|
reasonCode: issue.reasonCode,
|
|
remainingMs: issue.remainingMs,
|
|
},
|
|
params.cfg,
|
|
store,
|
|
),
|
|
),
|
|
);
|
|
note(issueLines.join("\n"), "Model auth");
|
|
}
|
|
}
|