mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-05 06:32:56 +00:00
Refactor OpenAI provider identity so OpenAI remains the canonical provider for API-key and OAuth-backed flows while legacy openai-codex state is doctor/migration-only. Keeps OpenAI Codex Responses as an API/transport class rather than a provider identity, moves auth aliases through providerAuthAliases, updates doctor repair sequencing for old auth/profile state, and refreshes tests/docs around the canonical OpenAI behavior.
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { ensureGlobalUndiciEnvProxyDispatcher } from "openclaw/plugin-sdk/runtime-env";
|
|
import { refreshOpenAICodexToken as refreshOpenAICodexTokenFromFlow } from "./openai-codex-oauth-flow.runtime.js";
|
|
import type { OAuthCredentials } from "./openai-codex-oauth-types.runtime.js";
|
|
|
|
type OpenAICodexProviderRuntimeDeps = {
|
|
ensureGlobalUndiciEnvProxyDispatcher: typeof ensureGlobalUndiciEnvProxyDispatcher;
|
|
getOAuthApiKey: typeof getOpenAICodexOAuthApiKey;
|
|
refreshOpenAICodexToken: typeof refreshOpenAICodexTokenFromFlow;
|
|
};
|
|
|
|
export function createOpenAICodexProviderRuntime(deps: OpenAICodexProviderRuntimeDeps): {
|
|
getOAuthApiKey: typeof getOAuthApiKey;
|
|
refreshOpenAICodexToken: typeof refreshOpenAICodexToken;
|
|
} {
|
|
return {
|
|
async getOAuthApiKey(...args) {
|
|
deps.ensureGlobalUndiciEnvProxyDispatcher();
|
|
return await deps.getOAuthApiKey(...args);
|
|
},
|
|
async refreshOpenAICodexToken(...args) {
|
|
deps.ensureGlobalUndiciEnvProxyDispatcher();
|
|
return await deps.refreshOpenAICodexToken(...args);
|
|
},
|
|
};
|
|
}
|
|
|
|
const runtime = createOpenAICodexProviderRuntime({
|
|
ensureGlobalUndiciEnvProxyDispatcher,
|
|
getOAuthApiKey: getOpenAICodexOAuthApiKey,
|
|
refreshOpenAICodexToken: refreshOpenAICodexTokenFromFlow,
|
|
});
|
|
|
|
export async function getOAuthApiKey(
|
|
...args: Parameters<typeof getOpenAICodexOAuthApiKey>
|
|
): Promise<Awaited<ReturnType<typeof getOpenAICodexOAuthApiKey>>> {
|
|
return await runtime.getOAuthApiKey(...args);
|
|
}
|
|
|
|
export async function refreshOpenAICodexToken(
|
|
...args: Parameters<typeof refreshOpenAICodexTokenFromFlow>
|
|
): Promise<Awaited<ReturnType<typeof refreshOpenAICodexTokenFromFlow>>> {
|
|
return await runtime.refreshOpenAICodexToken(...args);
|
|
}
|
|
|
|
async function getOpenAICodexOAuthApiKey(
|
|
providerId: string,
|
|
credentials: Record<string, OAuthCredentials>,
|
|
): Promise<{ newCredentials: OAuthCredentials; apiKey: string } | null> {
|
|
if (providerId !== "openai" && providerId !== "openai-codex") {
|
|
throw new Error(`Unknown OAuth provider: ${providerId}`);
|
|
}
|
|
let creds = credentials[providerId] ?? credentials["openai-codex"];
|
|
if (!creds) {
|
|
return null;
|
|
}
|
|
if (Date.now() >= creds.expires) {
|
|
creds = await refreshOpenAICodexTokenFromFlow(creds.refresh);
|
|
}
|
|
return { newCredentials: creds, apiKey: creds.access };
|
|
}
|