mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): a project .env provider key gives way to the person's own credential (#8041)
* fix(cli): a project .env provider key gives way to the person's own credential A project .env could set OPENROUTER_API_KEY, GEMINI_API_KEY, GOOGLE_API_KEY, the Vertex settings or ELEVENLABS_API_KEY and outrank a credential the person set in their shell or saved settings. The earlier rule covered only received projects; a cloned or downloaded project still won. Now every project .env provider key is unset, with a note naming the file, when a competing provider has a credential from a surer source: the shell or saved settings first, then the person's own project .env, then a received one. A project key with no competing credential is still used, model and timeout settings still apply, and the media-use scripts note an ElevenLabs key from any project .env before speech starts. In the CLI process an own-project key yields only within the vision choice; the media-use scripts remake the Gemini audio choice from the passed key origins. * fix(cli): weigh an own project's provider key only against credentials its feature can use Review found that the global yield removed a person's own project keys where the shell credential could not do the job: snapshot's Gemini key next to a shell OpenRouter key, and a Gemini key that music needs next to a shell service account. - Startup keeps the received-project rule only. - Capture drops a project .env vision key only when the shell or settings hold another complete vision credential; the two Vertex parts are one credential and are not weighed against each other. - A key dropped once is not brought back when capture re-reads the same .env, so its note prints once. - In the audio scripts, an own project's Gemini key gives way only to the other Gemini API key, which speech and music both accept, and an own ElevenLabs key stays for an explicit provider choice.
This commit is contained in:
@@ -152,11 +152,11 @@ to a shared space with `--space`.
|
||||
| `HYPERFRAMES_CONFIG_DIR` | HyperFrames settings root: credentials, telemetry and update settings, project links (default `~/.hyperframes`). |
|
||||
| `HYPERFRAMES_ENV` | `prod` (default) or `dev`; `dev` keeps credentials in a separate `dev/` directory under the root. Hosts and the OAuth client id come from `HEYGEN_API_URL`, `HYPERFRAMES_OAUTH_*` and `HYPERFRAMES_PUBLISHED_PROJECTS_API_URL`. |
|
||||
| `HYPERFRAMES_OAUTH_CLIENT_ID` | Override the public client ID for a controlled OAuth environment. |
|
||||
| `ELEVENLABS_API_KEY` | ElevenLabs TTS, used when no HeyGen credential is present. A key from a received project's `.env` is never used next to your own HeyGen credential, and a note names the file when it is used. |
|
||||
| `GEMINI_API_KEY` / `GOOGLE_API_KEY` | Explicit Gemini TTS selection and Lyria music generation; capture descriptions use `GEMINI_API_KEY`. |
|
||||
| `ELEVENLABS_API_KEY` | ElevenLabs TTS, used when no HeyGen credential is present. A key from a received project's `.env` is never used next to your own HeyGen credential. A note names the file whenever a project `.env` key is used. |
|
||||
| `GEMINI_API_KEY` / `GOOGLE_API_KEY` | Explicit Gemini TTS selection and Lyria music generation; capture descriptions use `GEMINI_API_KEY`. In speech and music, one from a project's `.env` gives way to the other set in your shell. |
|
||||
| `GOOGLE_APPLICATION_CREDENTIALS` / `GCS_CREDS` | Gemini TTS service-account JSON file path / injected JSON. Used when neither API key is set. |
|
||||
| `GOOGLE_CLOUD_PROJECT` / `GCLOUD_PROJECT_ID` | Gemini OAuth quota project; first set value wins, then the service account project. |
|
||||
| `OPENROUTER_API_KEY` | Capture descriptions; takes priority over Gemini for that step. |
|
||||
| `OPENROUTER_API_KEY` | Capture descriptions; takes priority over Gemini for that step, unless it comes from a project's `.env` and your shell has a complete Gemini or Vertex credential. |
|
||||
|
||||
## Related topics
|
||||
|
||||
|
||||
@@ -404,9 +404,12 @@ For AI image descriptions, set `GEMINI_API_KEY` in a `.env` file
|
||||
[OpenRouter](https://openrouter.ai) — it wins if both are set, and
|
||||
`HYPERFRAMES_OPENROUTER_MODEL` overrides the model.
|
||||
|
||||
A received project's `.env` (one its received-files record lists) never
|
||||
replaces a vision or Gemini credential you set in your shell: its competing
|
||||
keys are ignored, with a note naming the file.
|
||||
A project's `.env` never replaces a vision credential you set in your shell:
|
||||
when your shell holds a complete credential for another vision provider,
|
||||
capture ignores the `.env` vision key, with a note naming the file.
|
||||
Model and timeout settings in the `.env` still apply. A received project's
|
||||
`.env` (one its received-files record lists) also gives way to your own
|
||||
project's `.env`, for every command.
|
||||
|
||||
#### Local HTML files
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import { setupAnimationCapture, startCdpAnimationCapture } from "./animationCata
|
||||
import type { DiscoveredLottie } from "./mediaCapture.js";
|
||||
import { detectLibraries } from "./contentExtractor.js";
|
||||
import { loadEnvFile, generateProjectScaffold } from "./scaffolding.js";
|
||||
import { yieldReceivedProviderKeys } from "../utils/dotEnv.js";
|
||||
import { yieldProjectVisionKeys, yieldReceivedProviderKeys } from "../utils/dotEnv.js";
|
||||
import { captureProtocolTimeoutMs } from "./captureTimeout.js";
|
||||
import { CAPTURE_PHASE_SCHEMA, DEFAULT_MAX_SCREENSHOTS } from "./types.js";
|
||||
import type {
|
||||
@@ -115,6 +115,7 @@ export async function captureWebsiteAttempt(
|
||||
// Load .env file from repo root if it exists (for GEMINI_API_KEY, etc.)
|
||||
loadEnvFile(outputDir);
|
||||
await yieldReceivedProviderKeys((line) => console.error(line));
|
||||
yieldProjectVisionKeys((line) => console.error(line));
|
||||
|
||||
// Create output directories
|
||||
ensureCaptureDirSync(outputDir, join(outputDir, "extracted"));
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
type VisionCaptionOutcome,
|
||||
} from "./contentExtractor.js";
|
||||
import { loadOptionalPackage } from "../utils/optionalPackages.js";
|
||||
import { yieldReceivedProviderKeys } from "../utils/dotEnv.js";
|
||||
import { yieldProjectVisionKeys, yieldReceivedProviderKeys } from "../utils/dotEnv.js";
|
||||
import { loadEnvFile } from "./scaffolding.js";
|
||||
|
||||
const { generateContentMock, clientOptions, sharpState } = vi.hoisted(() => ({
|
||||
@@ -424,14 +424,19 @@ describe("captionImagesWithGemini — Gemini provider", () => {
|
||||
dirs.length = 0;
|
||||
});
|
||||
|
||||
it("captions with the shell's Gemini key, never a received project's OpenRouter key", async () => {
|
||||
it.each([
|
||||
["a received project's", true],
|
||||
["a cloned project's", false],
|
||||
])("captions with the shell's Gemini key, never %s OpenRouter key", async (_, received) => {
|
||||
const dir = makeProjectWithImages();
|
||||
dirs.push(dir);
|
||||
mkdirSync(join(dir, ".hyperframes"));
|
||||
writeFileSync(
|
||||
join(dir, ".hyperframes", "received-files.json"),
|
||||
JSON.stringify({ files: [".env"] }),
|
||||
);
|
||||
if (received) {
|
||||
mkdirSync(join(dir, ".hyperframes"));
|
||||
writeFileSync(
|
||||
join(dir, ".hyperframes", "received-files.json"),
|
||||
JSON.stringify({ files: [".env"] }),
|
||||
);
|
||||
}
|
||||
writeFileSync(join(dir, ".env"), "OPENROUTER_API_KEY=received-key\n");
|
||||
for (const key of [
|
||||
"OPENROUTER_API_KEY",
|
||||
@@ -448,6 +453,7 @@ describe("captionImagesWithGemini — Gemini provider", () => {
|
||||
|
||||
loadEnvFile(join(dir, "out"));
|
||||
await yieldReceivedProviderKeys(() => {});
|
||||
yieldProjectVisionKeys(() => {});
|
||||
const captions = await captionImagesWithGemini(dir, () => {}, []);
|
||||
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
|
||||
@@ -2,7 +2,12 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { applyDotEnv, loadProjectDotEnv, yieldReceivedProviderKeys } from "./dotEnv.js";
|
||||
import {
|
||||
applyDotEnv,
|
||||
loadProjectDotEnv,
|
||||
yieldProjectVisionKeys,
|
||||
yieldReceivedProviderKeys,
|
||||
} from "./dotEnv.js";
|
||||
|
||||
const records = vi.hoisted(() => ({ unchecked: false }));
|
||||
vi.mock("./privateProjectFile.js", async (importOriginal) => {
|
||||
@@ -239,3 +244,102 @@ describe("yieldReceivedProviderKeys", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("yieldProjectVisionKeys", () => {
|
||||
const VISION_KEYS = [
|
||||
"OPENROUTER_API_KEY",
|
||||
"GEMINI_API_KEY",
|
||||
"GOOGLE_API_KEY",
|
||||
"HYPERFRAMES_VERTEX_PROJECT_ID",
|
||||
"HYPERFRAMES_VERTEX_SERVICE_ACCOUNT",
|
||||
];
|
||||
const dirs: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
/** Loads `env` from the person's own project `.env` over `shell`, then runs the startup rule. */
|
||||
async function loadOwn(shell: Record<string, string>, env: string) {
|
||||
for (const key of VISION_KEYS) vi.stubEnv(key, shell[key]);
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-dotenv-vision-"));
|
||||
dirs.push(dir);
|
||||
writeFileSync(join(dir, ".env"), env);
|
||||
loadProjectDotEnv(dir, process.env, vi.fn());
|
||||
await yieldReceivedProviderKeys(vi.fn());
|
||||
return dir;
|
||||
}
|
||||
|
||||
it("leaves the person's own project keys alone at startup, so snapshot keeps its Gemini key", async () => {
|
||||
await loadOwn({ OPENROUTER_API_KEY: "own" }, "GEMINI_API_KEY=mine\n");
|
||||
expect(process.env.GEMINI_API_KEY).toBe("mine");
|
||||
});
|
||||
|
||||
it("drops a project .env's OpenRouter key for capture when the shell has a Gemini key", async () => {
|
||||
const dir = await loadOwn({ GEMINI_API_KEY: "own" }, "OPENROUTER_API_KEY=cloned\n");
|
||||
const warn = vi.fn();
|
||||
yieldProjectVisionKeys(warn);
|
||||
expect(process.env.OPENROUTER_API_KEY).toBeUndefined();
|
||||
expect(process.env.GEMINI_API_KEY).toBe("own");
|
||||
expect(warn).toHaveBeenCalledWith(
|
||||
`[hyperframes] .env: ignoring OPENROUTER_API_KEY from a project .env (${join(dir, ".env")}); capture uses your own GEMINI_API_KEY`,
|
||||
);
|
||||
});
|
||||
|
||||
it("names the credential capture will pick in the note", async () => {
|
||||
await loadOwn(
|
||||
{
|
||||
GEMINI_API_KEY: "own",
|
||||
HYPERFRAMES_VERTEX_PROJECT_ID: "own",
|
||||
HYPERFRAMES_VERTEX_SERVICE_ACCOUNT: "/own/sa.json",
|
||||
},
|
||||
"OPENROUTER_API_KEY=cloned\n",
|
||||
);
|
||||
const warn = vi.fn();
|
||||
yieldProjectVisionKeys(warn);
|
||||
expect(warn).toHaveBeenCalledWith(
|
||||
expect.stringContaining(
|
||||
"capture uses your own HYPERFRAMES_VERTEX_PROJECT_ID and HYPERFRAMES_VERTEX_SERVICE_ACCOUNT",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the project half of a Vertex credential whose other half is in the shell", async () => {
|
||||
await loadOwn(
|
||||
{ HYPERFRAMES_VERTEX_SERVICE_ACCOUNT: "/own/sa.json" },
|
||||
"HYPERFRAMES_VERTEX_PROJECT_ID=mine\n",
|
||||
);
|
||||
const warn = vi.fn();
|
||||
yieldProjectVisionKeys(warn);
|
||||
expect(process.env.HYPERFRAMES_VERTEX_PROJECT_ID).toBe("mine");
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps a project key next to an incomplete shell Vertex credential", async () => {
|
||||
await loadOwn({ HYPERFRAMES_VERTEX_PROJECT_ID: "own" }, "GEMINI_API_KEY=mine\n");
|
||||
yieldProjectVisionKeys(vi.fn());
|
||||
expect(process.env.GEMINI_API_KEY).toBe("mine");
|
||||
});
|
||||
|
||||
it("drops both Vertex parts from a project .env when the shell has a Gemini key, keeping its model", async () => {
|
||||
await loadOwn(
|
||||
{ GEMINI_API_KEY: "own" },
|
||||
"HYPERFRAMES_VERTEX_PROJECT_ID=cloned\nHYPERFRAMES_VERTEX_SERVICE_ACCOUNT=/cloned/sa.json\nHYPERFRAMES_GEMINI_MODEL=gemini-x\n",
|
||||
);
|
||||
yieldProjectVisionKeys(vi.fn());
|
||||
expect(process.env.HYPERFRAMES_VERTEX_PROJECT_ID).toBeUndefined();
|
||||
expect(process.env.HYPERFRAMES_VERTEX_SERVICE_ACCOUNT).toBeUndefined();
|
||||
expect(process.env.HYPERFRAMES_GEMINI_MODEL).toBe("gemini-x");
|
||||
});
|
||||
|
||||
it("does not bring a dropped key back when capture reads the same .env again", async () => {
|
||||
const dir = await loadOwn({ GEMINI_API_KEY: "own" }, "OPENROUTER_API_KEY=cloned\n");
|
||||
yieldProjectVisionKeys(vi.fn());
|
||||
loadProjectDotEnv(dir, process.env, vi.fn());
|
||||
const warn = vi.fn();
|
||||
yieldProjectVisionKeys(warn);
|
||||
expect(process.env.OPENROUTER_API_KEY).toBeUndefined();
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { join, resolve } from "node:path";
|
||||
import { FEEDBACK_EMAIL_ENV } from "./feedbackSource.js";
|
||||
|
||||
export const PROJECT_ENV_KEYS = [
|
||||
@@ -96,7 +96,7 @@ export function applyDotEnv(
|
||||
const [key, value] = entry;
|
||||
if (!key || key.toUpperCase() === FEEDBACK_EMAIL_ENV) continue;
|
||||
if (allowed !== undefined && !allowed.has(key)) ignored.push(key);
|
||||
else if (!(key in env)) {
|
||||
else if (!alreadySetOrYielded(env, key, sourceDir)) {
|
||||
env[key] = value;
|
||||
if (sourceDir !== undefined && env === process.env)
|
||||
projectEnvValues.set(key, { value, sourceDir });
|
||||
@@ -105,7 +105,19 @@ export function applyDotEnv(
|
||||
return ignored;
|
||||
}
|
||||
|
||||
function alreadySetOrYielded(env: NodeJS.ProcessEnv, key: string, sourceDir?: string): boolean {
|
||||
return key in env || (sourceDir !== undefined && yieldedKeys.has(keyId(sourceDir, key)));
|
||||
}
|
||||
|
||||
const projectEnvValues = new Map<string, { value: string; sourceDir: string }>();
|
||||
const yieldedKeys = new Set<string>();
|
||||
const keyId = (dir: string, name: string) => `${resolve(dir)}\0${name}`;
|
||||
|
||||
function yieldKey(name: string, dir: string): void {
|
||||
delete process.env[name];
|
||||
projectEnvValues.delete(name);
|
||||
yieldedKeys.add(keyId(dir, name));
|
||||
}
|
||||
|
||||
export function projectEnvDir(name: string): string | undefined {
|
||||
const set = projectEnvValues.get(name);
|
||||
@@ -151,14 +163,43 @@ export async function yieldReceivedProviderKeys(warn: (line: string) => void): P
|
||||
if (own === undefined) continue;
|
||||
const dir = projectEnvDir(name) ?? "";
|
||||
const origin = originOf(name);
|
||||
delete process.env[name];
|
||||
projectEnvValues.delete(name);
|
||||
yieldKey(name, dir);
|
||||
warn(
|
||||
`[hyperframes] .env: ignoring ${name} from ${origin} (${join(dir, ".env")}); using your own ${own}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Capture's vision credentials in the order it picks them; the Vertex one is usable only with both parts set.
|
||||
const VISION_CREDENTIALS: readonly (readonly string[])[] = [
|
||||
["OPENROUTER_API_KEY"],
|
||||
["HYPERFRAMES_VERTEX_PROJECT_ID", "HYPERFRAMES_VERTEX_SERVICE_ACCOUNT"],
|
||||
["GEMINI_API_KEY"],
|
||||
["GOOGLE_API_KEY"],
|
||||
];
|
||||
|
||||
const setOutsideProjectEnv = (parts: readonly string[]) =>
|
||||
parts.every((part) => Boolean(process.env[part]) && projectEnvDir(part) === undefined);
|
||||
|
||||
/** Unsets capture's vision keys from a project `.env` when the person set another complete vision credential. */
|
||||
export function yieldProjectVisionKeys(warn: (line: string) => void): void {
|
||||
for (const credential of VISION_CREDENTIALS) {
|
||||
if (credential.every((part) => projectEnvDir(part) === undefined)) continue;
|
||||
const own = VISION_CREDENTIALS.find(
|
||||
(other) => other !== credential && setOutsideProjectEnv(other),
|
||||
);
|
||||
if (own === undefined) continue;
|
||||
for (const name of credential) {
|
||||
const dir = projectEnvDir(name);
|
||||
if (dir === undefined) continue;
|
||||
yieldKey(name, dir);
|
||||
warn(
|
||||
`[hyperframes] .env: ignoring ${name} from a project .env (${join(dir, ".env")}); capture uses your own ${own.join(" and ")}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const PROJECT_ENV_ORIGINS = "HYPERFRAMES_PROJECT_ENV_ORIGINS";
|
||||
|
||||
/** The folder of each key a project `.env` set, for the scripts the CLI launches to weigh the same way. */
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
"files": 13
|
||||
},
|
||||
"media-use": {
|
||||
"hash": "65317a8d5de8e4ae",
|
||||
"hash": "96d970958d1c1d18",
|
||||
"files": 128
|
||||
},
|
||||
"remotion-to-hyperframes": {
|
||||
|
||||
@@ -173,27 +173,36 @@ function untrustedOrigin(name) {
|
||||
return kind === null ? null : RECEIVED_ORIGIN[kind];
|
||||
}
|
||||
|
||||
// The person's own credential that the provider of `name` competes with, named for the note; undefined when none.
|
||||
// 0: set outside any project .env; 1: the person's own project .env; 2: a received or unreadable one.
|
||||
function keyRank(name) {
|
||||
if (projectKeyDir(name) === undefined) return 0;
|
||||
return untrustedOrigin(name) === null ? 1 : 2;
|
||||
}
|
||||
|
||||
// A credential for a competing provider from a source the person controls more surely, named for the note. The
|
||||
// person's own project key gives way only to the other API key, which speech and music both accept, and an own
|
||||
// ElevenLabs key stays for an explicit provider choice.
|
||||
function ownCompetingCredential(name) {
|
||||
const rank = keyRank(name);
|
||||
const surer = (other) => other !== name && process.env[other] && keyRank(other) < rank;
|
||||
if (name !== "ELEVENLABS_API_KEY")
|
||||
return GEMINI_CREDENTIALS.find(
|
||||
(other) => other !== name && process.env[other] && untrustedOrigin(other) === null,
|
||||
);
|
||||
return (rank === 2 ? GEMINI_CREDENTIALS : ["GEMINI_API_KEY", "GOOGLE_API_KEY"]).find(surer);
|
||||
if (rank < 2) return undefined;
|
||||
if (process.env.HEYGEN_ACCESS_TOKEN) return "HEYGEN_ACCESS_TOKEN";
|
||||
const heygenKey = ["HEYGEN_API_KEY", "HYPERFRAMES_API_KEY"].find(
|
||||
(other) => process.env[other] && untrustedOrigin(other) === null,
|
||||
);
|
||||
const heygenKey = ["HEYGEN_API_KEY", "HYPERFRAMES_API_KEY"].find(surer);
|
||||
if (heygenKey) return heygenKey;
|
||||
return storedCredential() ? "saved HeyGen credentials" : undefined;
|
||||
}
|
||||
|
||||
// A received project's provider key gives way to the person's own credential for a competing provider.
|
||||
function yieldReceivedProviderKeys() {
|
||||
const projectOrigin = (name) => untrustedOrigin(name) ?? "a project .env";
|
||||
|
||||
// A project .env's provider key gives way to the person's own credential for a competing provider.
|
||||
function yieldProjectProviderKeys() {
|
||||
for (const name of ["GEMINI_API_KEY", "GOOGLE_API_KEY", "ELEVENLABS_API_KEY"]) {
|
||||
const origin = untrustedOrigin(name);
|
||||
const own = origin === null ? undefined : ownCompetingCredential(name);
|
||||
if (own === undefined) continue;
|
||||
const dir = projectKeyDir(name);
|
||||
const own = dir === undefined ? undefined : ownCompetingCredential(name);
|
||||
if (own === undefined) continue;
|
||||
const origin = projectOrigin(name);
|
||||
delete process.env[name];
|
||||
projectEnvValues.delete(name);
|
||||
process.stderr.write(
|
||||
@@ -204,13 +213,13 @@ function yieldReceivedProviderKeys() {
|
||||
|
||||
let elevenlabsKeyNoted = false;
|
||||
|
||||
// Says once, before ElevenLabs speaks, that the key came from a received project's .env.
|
||||
export function noteReceivedElevenLabsKey() {
|
||||
const origin = untrustedOrigin("ELEVENLABS_API_KEY");
|
||||
if (origin === null || elevenlabsKeyNoted) return;
|
||||
// Says once, before ElevenLabs speaks, that the key came from a project .env.
|
||||
export function noteProjectElevenLabsKey() {
|
||||
const dir = projectKeyDir("ELEVENLABS_API_KEY");
|
||||
if (dir === undefined || elevenlabsKeyNoted) return;
|
||||
elevenlabsKeyNoted = true;
|
||||
process.stderr.write(
|
||||
`[hyperframes] Using ELEVENLABS_API_KEY from ${join(projectKeyDir("ELEVENLABS_API_KEY"), ".env")}, the .env of ${origin}: speech goes to that key's ElevenLabs account.\n`,
|
||||
`[hyperframes] Using ELEVENLABS_API_KEY from ${join(dir, ".env")}, ${projectOrigin("ELEVENLABS_API_KEY")}: speech goes to that key's ElevenLabs account.\n`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -218,7 +227,7 @@ export function noteReceivedElevenLabsKey() {
|
||||
export function loadEnvFromDir(startDir) {
|
||||
inheritProjectKeyOrigins();
|
||||
loadFirstEnvFile(startDir);
|
||||
yieldReceivedProviderKeys();
|
||||
yieldProjectProviderKeys();
|
||||
}
|
||||
|
||||
function loadFirstEnvFile(startDir) {
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
heygenJSON,
|
||||
heygenMessage,
|
||||
loadEnvFromDir,
|
||||
noteReceivedElevenLabsKey,
|
||||
noteProjectElevenLabsKey,
|
||||
} from "./heygen.mjs";
|
||||
|
||||
const HEYGEN_ENV = [
|
||||
@@ -412,7 +412,10 @@ test("loadEnvFromDir skips a .env folder and loads the .env file above it", () =
|
||||
const saved = process.env.ELEVENLABS_API_KEY;
|
||||
delete process.env.ELEVENLABS_API_KEY;
|
||||
try {
|
||||
loadEnvFromDir(project);
|
||||
withCleanHeygenEnv(() => {
|
||||
process.env.HYPERFRAMES_CONFIG_DIR = join(root, "no-config");
|
||||
loadEnvFromDir(project);
|
||||
});
|
||||
assert.equal(process.env.ELEVENLABS_API_KEY, "from-parent");
|
||||
} finally {
|
||||
if (saved === undefined) delete process.env.ELEVENLABS_API_KEY;
|
||||
@@ -438,10 +441,16 @@ test("a project's .env supplies provider keys only, never programs, runtime opti
|
||||
for (const key of keys) delete process.env[key];
|
||||
const project = mkdtempSync(join(tmpdir(), "heygen-env-"));
|
||||
writeFileSync(join(project, ".env"), keys.map((key) => `${key}=/project/value`).join("\n"));
|
||||
const configDir = join(project, "no-config");
|
||||
try {
|
||||
loadEnvFromDir(project);
|
||||
assert.equal(process.env.ELEVENLABS_API_KEY, "/project/value");
|
||||
for (const key of keys.slice(1)) assert.equal(process.env[key], undefined, key);
|
||||
withCleanHeygenEnv(() => {
|
||||
process.env.HYPERFRAMES_CONFIG_DIR = configDir;
|
||||
loadEnvFromDir(project);
|
||||
assert.equal(process.env.ELEVENLABS_API_KEY, "/project/value");
|
||||
assert.equal(process.env.HYPERFRAMES_CONFIG_DIR, configDir);
|
||||
for (const key of keys.slice(1).filter((key) => key !== "HYPERFRAMES_CONFIG_DIR"))
|
||||
assert.equal(process.env[key], undefined, key);
|
||||
});
|
||||
} finally {
|
||||
for (const key of keys) {
|
||||
if (saved[key] === undefined) delete process.env[key];
|
||||
@@ -687,7 +696,7 @@ test("loadEnvFromDir leaves out a received .env's Gemini key when the shell has
|
||||
);
|
||||
});
|
||||
|
||||
test("loadEnvFromDir keeps a person's own .env Gemini key next to their shell credentials", () => {
|
||||
test("loadEnvFromDir keeps a person's own .env Gemini key next to a shell service account music cannot use", () => {
|
||||
withProviderProject(
|
||||
{ shell: { GOOGLE_APPLICATION_CREDENTIALS: "/own/sa.json" }, env: "GEMINI_API_KEY=mine\n" },
|
||||
(project, notes) => {
|
||||
@@ -698,6 +707,38 @@ test("loadEnvFromDir keeps a person's own .env Gemini key next to their shell cr
|
||||
);
|
||||
});
|
||||
|
||||
test("loadEnvFromDir leaves out a project .env's Gemini key when the shell has the other Gemini API key", () => {
|
||||
withProviderProject(
|
||||
{ shell: { GOOGLE_API_KEY: "own" }, env: "GEMINI_API_KEY=cloned\n" },
|
||||
(project, notes) => {
|
||||
loadEnvFromDir(project);
|
||||
assert.equal(process.env.GEMINI_API_KEY, undefined);
|
||||
assert.match(
|
||||
notes.join(""),
|
||||
/ignoring GEMINI_API_KEY from a project \.env .*using your own GOOGLE_API_KEY/,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("loadEnvFromDir keeps a project .env's Gemini key when the person has no Gemini credential", () => {
|
||||
withProviderProject({ env: "GEMINI_API_KEY=mine\n" }, (project, notes) => {
|
||||
loadEnvFromDir(project);
|
||||
assert.equal(process.env.GEMINI_API_KEY, "mine");
|
||||
assert.deepEqual(notes, []);
|
||||
});
|
||||
});
|
||||
|
||||
test("loadEnvFromDir keeps a person's own .env ElevenLabs key for an explicit provider choice", () => {
|
||||
withProviderProject(
|
||||
{ shell: { HEYGEN_API_KEY: "own" }, env: "ELEVENLABS_API_KEY=mine\n" },
|
||||
(project) => {
|
||||
loadEnvFromDir(project);
|
||||
assert.equal(process.env.ELEVENLABS_API_KEY, "mine");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("loadEnvFromDir leaves out a received .env's ElevenLabs key when the person has a HeyGen key", () => {
|
||||
withProviderProject(
|
||||
{ shell: { HEYGEN_API_KEY: "own" }, env: "ELEVENLABS_API_KEY=received\n", received: true },
|
||||
@@ -724,13 +765,10 @@ test("a received .env's ElevenLabs key with no competing credential is used, wit
|
||||
(project, notes) => {
|
||||
loadEnvFromDir(project);
|
||||
assert.equal(process.env.ELEVENLABS_API_KEY, "received");
|
||||
noteReceivedElevenLabsKey();
|
||||
noteReceivedElevenLabsKey();
|
||||
noteProjectElevenLabsKey();
|
||||
noteProjectElevenLabsKey();
|
||||
assert.equal(notes.length, 1);
|
||||
assert.match(
|
||||
notes[0],
|
||||
/Using ELEVENLABS_API_KEY from .*\.env, the \.env of a received project/,
|
||||
);
|
||||
assert.match(notes[0], /Using ELEVENLABS_API_KEY from .*\.env, a received project:/);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
heygenCredential,
|
||||
heygenJSON,
|
||||
heygenMessage,
|
||||
noteReceivedElevenLabsKey,
|
||||
noteProjectElevenLabsKey,
|
||||
} from "./heygen.mjs";
|
||||
import { pythonInvocation } from "./python.mjs";
|
||||
import { synthesizeGemini } from "./gemini-tts.mjs";
|
||||
@@ -57,12 +57,12 @@ export function pickProvider(userProvider) {
|
||||
if (userProvider === "heygen" && !heygenAvailable()) heygenAuthHeaders();
|
||||
if (userProvider === "elevenlabs" && !process.env.ELEVENLABS_API_KEY)
|
||||
throw new Error("provider=elevenlabs but $ELEVENLABS_API_KEY is not set");
|
||||
if (userProvider === "elevenlabs") noteReceivedElevenLabsKey();
|
||||
if (userProvider === "elevenlabs") noteProjectElevenLabsKey();
|
||||
return userProvider;
|
||||
}
|
||||
if (heygenAvailable()) return "heygen";
|
||||
if (!elevenlabsAvailable()) return "kokoro";
|
||||
noteReceivedElevenLabsKey();
|
||||
noteProjectElevenLabsKey();
|
||||
return "elevenlabs";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user