Skip to content

fix: Initialize environment variables for the active non-OpenAI provider #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions codex-cli/src/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
loadConfig,
PRETTY_PRINT,
INSTRUCTIONS_FILEPATH,
getApiKey,
} from "./utils/config";
import {
getApiKey as fetchApiKey,
Expand Down Expand Up @@ -307,20 +308,27 @@ let savedTokens:
}
| undefined;

// Try to load existing auth file if present
try {
const home = os.homedir();
const authDir = path.join(home, ".codex");
const authFile = path.join(authDir, "auth.json");
if (fs.existsSync(authFile)) {
const data = JSON.parse(fs.readFileSync(authFile, "utf-8"));
savedTokens = data.tokens;
const lastRefreshTime = data.last_refresh
? new Date(data.last_refresh).getTime()
: 0;
const expired = Date.now() - lastRefreshTime > 28 * 24 * 60 * 60 * 1000;
if (data.OPENAI_API_KEY && !expired) {
apiKey = data.OPENAI_API_KEY;
if (config.provider?.toLowerCase() === "openai") {
// Try to load existing auth file if present
const home = os.homedir();
const authDir = path.join(home, ".codex");
const authFile = path.join(authDir, "auth.json");
if (fs.existsSync(authFile)) {
const data = JSON.parse(fs.readFileSync(authFile, "utf-8"));
savedTokens = data.tokens;
const lastRefreshTime = data.last_refresh
? new Date(data.last_refresh).getTime()
: 0;
const expired = Date.now() - lastRefreshTime > 28 * 24 * 60 * 60 * 1000;
if (data.OPENAI_API_KEY && !expired) {
apiKey = data.OPENAI_API_KEY;
}
}
} else {
const providerApiKey = getApiKey(config.provider);
if (providerApiKey) {
apiKey = providerApiKey;
}
}
} catch {
Expand Down
38 changes: 33 additions & 5 deletions codex-cli/src/utils/get-api-key-components.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { loadConfig, saveConfig } from "./config.js";
import SelectInput from "../components/select-input/select-input.js";
import Spinner from "../components/vendor/ink-spinner.js";
import TextInput from "../components/vendor/ink-text-input.js";
Expand All @@ -11,7 +12,7 @@ export function ApiKeyPrompt({
}: {
onDone: (choice: Choice) => void;
}): JSX.Element {
const [step, setStep] = useState<"select" | "paste">("select");
const [step, setStep] = useState<"select" | "paste" | "exit">("select");
const [apiKey, setApiKey] = useState("");

if (step === "select") {
Expand All @@ -31,19 +32,46 @@ export function ApiKeyPrompt({
label: "Paste an API key (or set as OPENAI_API_KEY)",
value: "paste",
},
{
label: "Use a different provider (e.g. Azure, edit config file)",
value: "other",
},
]}
onSelect={(item: { value: string }) => {
if (item.value === "signin") {
onDone({ type: "signin" });
} else {
setStep("paste");
switch (item.value) {
case "signin":
onDone({ type: "signin" });
break;
case "paste":
setStep("paste");
break;
case "other":
saveConfig({
...loadConfig(),
provider: "openai",
});
setStep("exit");

setTimeout(() => process.exit(0), 60);
break;
default:
break;
}
}}
/>
</Box>
);
}

if (step === "exit") {
return (
<Text>
Please edit ~/.codex/config.json with your provider's API key, then
restart codex.
</Text>
);
}

return (
<Box flexDirection="column">
<Text>Paste your OpenAI API key and press &lt;Enter&gt;:</Text>
Expand Down