Fix env var handling for MacOs

This commit is contained in:
Will Chen
2025-04-11 10:33:10 -07:00
parent e0a0212934
commit 6ca060d207
5 changed files with 164 additions and 16 deletions

View File

@@ -24,6 +24,7 @@ import {
RunningAppInfo,
} from "../utils/process_manager";
import { ALLOWED_ENV_VARS } from "../../constants/models";
import { getEnvVar } from "../utils/read_env";
export function registerAppHandlers() {
ipcMain.handle("create-app", async (_, params: CreateAppParams) => {
@@ -164,7 +165,7 @@ export function registerAppHandlers() {
ipcMain.handle("get-env-vars", async () => {
const envVars: Record<string, string | undefined> = {};
for (const key of ALLOWED_ENV_VARS) {
envVars[key] = process.env[key];
envVars[key] = getEnvVar(key);
}
return envVars;
});

View File

@@ -4,7 +4,7 @@ import { createAnthropic } from "@ai-sdk/anthropic";
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
import type { LargeLanguageModel, UserSettings } from "../../lib/schemas";
import { PROVIDER_TO_ENV_VAR, AUTO_MODELS } from "../../constants/models";
import { getEnvVar } from "./read_env";
export function getModelClient(
model: LargeLanguageModel,
settings: UserSettings
@@ -15,7 +15,7 @@ export function getModelClient(
for (const autoModel of AUTO_MODELS) {
const apiKey =
settings.providerSettings?.[autoModel.provider]?.apiKey ||
process.env[PROVIDER_TO_ENV_VAR[autoModel.provider]];
getEnvVar(PROVIDER_TO_ENV_VAR[autoModel.provider]);
if (apiKey) {
console.log(
@@ -38,7 +38,7 @@ export function getModelClient(
const apiKey =
settings.providerSettings?.[model.provider]?.apiKey ||
process.env[PROVIDER_TO_ENV_VAR[model.provider]];
getEnvVar(PROVIDER_TO_ENV_VAR[model.provider]);
switch (model.provider) {
case "openai": {
const provider = createOpenAI({ apiKey });

15
src/ipc/utils/read_env.ts Normal file
View File

@@ -0,0 +1,15 @@
import { shellEnvSync } from "shell-env";
// Need to look up run-time env vars this way
// otherwise it doesn't work as expected in MacOs apps:
// https://github.com/sindresorhus/shell-env
let _env: Record<string, string> | null = null;
export function getEnvVar(key: string) {
// Cache it
if (!_env) {
_env = shellEnvSync();
}
return _env[key];
}