make profile startup state-driven

This commit is contained in:
2026-06-29 19:10:33 +08:00
parent 8c21691478
commit f1a755eebe
3 changed files with 40 additions and 3 deletions
+10 -2
View File
@@ -55,7 +55,15 @@ function parseEnvFile(content) {
function getProfileName(options) {
const positional = options._?.[0];
return `${options.profile || positional || process.env.CODEX_RETRY_GATEWAY_PROFILE || "default"}`;
return `${options.profile || positional || process.env.CODEX_RETRY_GATEWAY_PROFILE || ""}`.trim();
}
function resolveDefaultRequestedProfileName(existingState) {
const stateProfileName = `${existingState?.profile_name || ""}`.trim();
if (stateProfileName) {
return stateProfileName;
}
return process.env.CODEX_RETRY_GATEWAY_DEFAULT_PROFILE || "default";
}
function resolvePreferredProfileName({ requestedProfileName, existingState, preferStateProfile, profilesDir }) {
@@ -246,7 +254,6 @@ async function ensureCodexPointsToGateway({ paths, codexConfigPath, providerCont
async function main() {
const options = parseOptions(process.argv, { booleanFlags: ["no-codex-config-update", "prefer-state-profile"] });
const requestedProfileName = getProfileName(options);
const profilesDir = options.profilesDir || DEFAULT_PROFILES_DIR;
const stateRoot = options.stateRoot || process.env.CODEX_RETRY_GATEWAY_STATE_ROOT || DEFAULT_STATE_ROOT;
const codexConfigPath =
@@ -261,6 +268,7 @@ async function main() {
await ensureDirectory(paths.backupDir);
const existingState = await readJsonFile(paths.statePath);
const requestedProfileName = getProfileName(options) || resolveDefaultRequestedProfileName(existingState);
const profileName = resolvePreferredProfileName({
requestedProfileName,
existingState,