mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-26 00:57:05 +00:00
* fix(cli): make the cache folder private before a pinned download writes into it (SECOPS-2745) The measuring tools' pinned-file downloader (measure/pinnedFiles.ts, for the MediaPipe hand landmarker) made its folder with a plain mkdir, downloaded into it, and only checked the folder was private in the seal afterwards. A folder another account owned, a group/world-writable one, or a link out of the cache received the bytes before the refusal. The model downloaders were worse: the TTS and background-removal models (utils/pinnedFile.ts) and the whisper models never checked at all, and trusted a cached copy from such a folder. Every pinned download into the HyperFrames cache now runs ensurePrivateCacheDir first: a new folder is made 0700, the person's own group/world-writable folder loses those bits, and a link, a foreign owner or a folder another account can change is refused before any partial or final file is written or any cached copy is used. Temp files stay inside the verified folder and are renamed into place. opencv.ts, which already did this, now shares ensurePinnedFiles instead of its own copy of the loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): keep the pinned downloaders' comment share at the base (SECOPS-2745) Cuts the comment lines the private-cache fix added to pinnedFiles.ts, pinnedFile.ts and whisper/manager.ts so each file's comment share is back at or under its merge-base share, and names ensurePinnedFile's root cacheRoot so the call reads without the comment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
150 lines
5.1 KiB
TypeScript
150 lines
5.1 KiB
TypeScript
import { join } from "node:path";
|
|
import { ensurePinnedFile } from "../utils/pinnedFile.js";
|
|
import type { ModelFile } from "../whisper/modelFile.js";
|
|
import { hyperframesCacheDir } from "@hyperframes/core/cache-dir";
|
|
|
|
const CACHE_ROOT = hyperframesCacheDir();
|
|
const CACHE_DIR = join(CACHE_ROOT, "tts");
|
|
const MODELS_DIR = join(CACHE_DIR, "models");
|
|
const VOICES_DIR = join(CACHE_DIR, "voices");
|
|
|
|
const DEFAULT_MODEL = "kokoro-v1.0";
|
|
|
|
const MODEL_URLS: Record<string, string> = {
|
|
"kokoro-v1.0":
|
|
"https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/kokoro-v1.0.onnx",
|
|
};
|
|
|
|
const MODEL_PINS: Record<string, ModelFile> = {
|
|
"kokoro-v1.0": {
|
|
name: "kokoro-v1.0.onnx",
|
|
bytes: 325_532_387,
|
|
sha256: "7d5df8ecf7d4b1878015a32686053fd0eebe2bc377234608764cc0ef3636a6c5",
|
|
},
|
|
};
|
|
|
|
const VOICES_URL =
|
|
"https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/voices-v1.0.bin";
|
|
|
|
const VOICES_PIN: ModelFile = {
|
|
name: "voices-v1.0.bin",
|
|
bytes: 28_214_398,
|
|
sha256: "bca610b8308e8d99f32e6fe4197e7ec01679264efed0cac9140fe9c29f1fbf7d",
|
|
};
|
|
|
|
// Locale codes accepted by Kokoro's phonemizer (misaki for English,
|
|
// espeak-ng for everything else). Kept as a readonly tuple so the union
|
|
// type below stays driven by this single source.
|
|
export const SUPPORTED_LANGS = [
|
|
"en-us",
|
|
"en-gb",
|
|
"es",
|
|
"fr-fr",
|
|
"hi",
|
|
"it",
|
|
"pt-br",
|
|
"ja",
|
|
"zh",
|
|
] as const;
|
|
|
|
export type SupportedLang = (typeof SUPPORTED_LANGS)[number];
|
|
|
|
// Kokoro voice IDs are `<lang><gender>_<name>` — the first letter is
|
|
// language, the second is gender. See https://github.com/hexgrad/kokoro.
|
|
const VOICE_PREFIX_LANG: Record<string, SupportedLang> = {
|
|
a: "en-us", // American English
|
|
b: "en-gb", // British English
|
|
e: "es", // Spanish
|
|
f: "fr-fr", // French
|
|
h: "hi", // Hindi
|
|
i: "it", // Italian
|
|
j: "ja", // Japanese
|
|
p: "pt-br", // Brazilian Portuguese
|
|
z: "zh", // Mandarin
|
|
};
|
|
|
|
/**
|
|
* Infer the phonemizer language from a Kokoro voice ID prefix.
|
|
* Unknown prefixes fall back to `en-us` — Kokoro's text frontend is
|
|
* English-trained, so that's the safe default.
|
|
*/
|
|
export function inferLangFromVoiceId(voiceId: string): SupportedLang {
|
|
const first = voiceId.charAt(0).toLowerCase();
|
|
return VOICE_PREFIX_LANG[first] ?? "en-us";
|
|
}
|
|
|
|
export function isSupportedLang(value: string): value is SupportedLang {
|
|
return (SUPPORTED_LANGS as readonly string[]).includes(value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Voices — Kokoro ships 54 voices across 8 languages. We expose a curated
|
|
// default set and allow users to specify any valid Kokoro voice ID.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export interface VoiceInfo {
|
|
id: string;
|
|
label: string;
|
|
language: string;
|
|
gender: "female" | "male";
|
|
}
|
|
|
|
export const BUNDLED_VOICES: VoiceInfo[] = [
|
|
{ id: "af_heart", label: "Heart", language: "en-US", gender: "female" },
|
|
{ id: "af_nova", label: "Nova", language: "en-US", gender: "female" },
|
|
{ id: "af_sky", label: "Sky", language: "en-US", gender: "female" },
|
|
{ id: "am_adam", label: "Adam", language: "en-US", gender: "male" },
|
|
{ id: "am_michael", label: "Michael", language: "en-US", gender: "male" },
|
|
{ id: "bf_emma", label: "Emma", language: "en-GB", gender: "female" },
|
|
{ id: "bf_isabella", label: "Isabella", language: "en-GB", gender: "female" },
|
|
{ id: "bm_george", label: "George", language: "en-GB", gender: "male" },
|
|
{ id: "ef_dora", label: "Dora", language: "es", gender: "female" },
|
|
{ id: "ff_siwis", label: "Siwis", language: "fr-FR", gender: "female" },
|
|
{ id: "jf_alpha", label: "Alpha", language: "ja", gender: "female" },
|
|
{ id: "zf_xiaobei", label: "Xiaobei", language: "zh", gender: "female" },
|
|
];
|
|
|
|
export const DEFAULT_VOICE = "af_heart";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Public API
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Ensure the Kokoro ONNX model is downloaded and cached.
|
|
* Returns the path to the .onnx model file.
|
|
*/
|
|
export async function ensureModel(
|
|
model: string = DEFAULT_MODEL,
|
|
options?: { onProgress?: (message: string) => void },
|
|
): Promise<string> {
|
|
const known = Object.hasOwn(MODEL_URLS, model) && Object.hasOwn(MODEL_PINS, model);
|
|
const url = known ? MODEL_URLS[model] : undefined;
|
|
const pin = known ? MODEL_PINS[model] : undefined;
|
|
if (!url || !pin) {
|
|
throw new Error(
|
|
`Unknown TTS model: ${model}. Available: ${Object.keys(MODEL_URLS).join(", ")}`,
|
|
);
|
|
}
|
|
|
|
return ensurePinnedFile(url, join(MODELS_DIR, `${model}.onnx`), CACHE_ROOT, pin, {
|
|
downloading: `Downloading TTS model ${model} (~311 MB)...`,
|
|
onProgress: options?.onProgress,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Ensure the Kokoro voices bundle is downloaded and cached.
|
|
* Returns the path to the voices .bin file.
|
|
*/
|
|
export async function ensureVoices(options?: {
|
|
onProgress?: (message: string) => void;
|
|
}): Promise<string> {
|
|
return ensurePinnedFile(VOICES_URL, join(VOICES_DIR, VOICES_PIN.name), CACHE_ROOT, VOICES_PIN, {
|
|
downloading: "Downloading voice data (~27 MB)...",
|
|
onProgress: options?.onProgress,
|
|
});
|
|
}
|
|
|
|
export { MODELS_DIR, DEFAULT_MODEL };
|