openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
65 lines (64 loc) • 3.21 kB
JavaScript
import { s as isGatewayTransportError } from "./transport-error-BL_kuysc.mjs";
import { f as isImplicitLocalGatewayTarget, g as GatewayLocalBackendSharedAuthUnavailableError, l as isGatewayClientRequestError, r as callGateway } from "./call-DVCK1deJ.mjs";
import { n as ProviderCredentialsSavedError, t as ProviderAuthConfigApplyError } from "./provider-auth-result-Cm5Twlfj.mjs";
//#region src/commands/models/auth-refresh.ts
/** Shared gateway refresh for CLI auth writes made outside the gateway process. */
async function refreshProviderAuthAfterLogin(e) {
if (!e.refreshAfterLogin) return refreshRunningGatewayAuthState(e.agentId, "login", e.runtime);
try {
await e.refreshAfterLogin(e.agentId);
return "refreshed";
} catch {
e.signal?.throwIfAborted();
e.assertCurrent?.();
return "gateway-rejected";
}
}
/** Publish committed credentials best-effort without replacing the original login failure. */
async function throwProviderAuthLoginError(e, t, n) {
if (e instanceof ProviderAuthConfigApplyError) try {
t.signal?.throwIfAborted();
t.assertCurrent?.();
await refreshProviderAuthAfterLogin(t);
} catch {}
if (n && !(e instanceof ProviderCredentialsSavedError)) throw new ProviderCredentialsSavedError(e instanceof Error ? `Provider credentials were saved, but sign-in did not finish: ${e.message}` : "Provider credentials were saved, but sign-in did not finish.", { cause: e });
throw e;
}
async function refreshRunningGatewayAuthState(e, t, n) {
let r = false;
let i;
try {
i = await isImplicitLocalGatewayTarget({});
if ((await callGateway({
method: "models.authRefresh",
params: {
operation: t,
...e ? { agentId: e } : {}
},
timeoutMs: 3e3,
requireLocalBackendSharedAuth: true,
onHelloOk: () => {
r = true;
}
})).refreshed) return "refreshed";
} catch (t) {
if (isGatewayClientRequestError(t) && t.gatewayCode === "INVALID_REQUEST" && t.message === "unknown method: models.authRefresh") await callGateway({
method: "models.authStatus",
params: {
refresh: true,
...e ? { agentId: e } : {}
},
timeoutMs: 3e3,
requireLocalBackendSharedAuth: true
}).catch(() => void 0);
if (t instanceof GatewayLocalBackendSharedAuthUnavailableError && i === false) {
n.error("Warning: Model auth changes were saved on this host, but the configured Gateway does not share this auth state. Run the auth command on the Gateway host (the far end of any SSH tunnel).");
return "gateway-rejected";
}
if (i === true && !r && isGatewayTransportError(t) && t.kind === "closed" && t.code === void 0 && t.reason?.includes("ECONNREFUSED")) return "gateway-unreachable";
}
n.error(i === true ? `Warning: Model auth changes were saved, but the ${r ? "running" : "local"} Gateway could not refresh them. Run \`openclaw gateway restart\` to apply the saved changes.` : "Warning: Model auth changes were saved, but the configured Gateway could not be identified or refreshed. Apply the auth change on the Gateway host, or restart it there.");
return r ? "gateway-rejected" : "gateway-unreachable";
}
//#endregion
export { refreshRunningGatewayAuthState as n, throwProviderAuthLoginError as r, refreshProviderAuthAfterLogin as t };