From 4bcc58fc6dac2db70d2a44f61677ae29072bd302 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 6 Apr 2026 20:12:50 +0100 Subject: [PATCH] refactor: dedupe speech core vitest shim --- .../speech-core.ts | 69 +++---------------- 1 file changed, 9 insertions(+), 60 deletions(-) diff --git a/src/plugins/capability-runtime-vitest-shims/speech-core.ts b/src/plugins/capability-runtime-vitest-shims/speech-core.ts index 9a62a1f1c48..299f9d719d1 100644 --- a/src/plugins/capability-runtime-vitest-shims/speech-core.ts +++ b/src/plugins/capability-runtime-vitest-shims/speech-core.ts @@ -1,6 +1,3 @@ -import { rmSync } from "node:fs"; - -export type { SpeechProviderPlugin } from "../../plugins/types.js"; export type { SpeechDirectiveTokenParseContext, SpeechDirectiveTokenParseResult, @@ -8,6 +5,7 @@ export type { SpeechModelOverridePolicy, SpeechProviderConfig, SpeechProviderConfiguredContext, + SpeechProviderPlugin, SpeechProviderResolveConfigContext, SpeechProviderResolveTalkConfigContext, SpeechProviderResolveTalkOverridesContext, @@ -17,64 +15,15 @@ export type { SpeechVoiceOption, TtsDirectiveOverrides, TtsDirectiveParseResult, -} from "../../tts/provider-types.js"; +} from "../../plugin-sdk/speech-core.js"; -const TEMP_FILE_CLEANUP_DELAY_MS = 5 * 60 * 1000; - -export function requireInRange(value: number, min: number, max: number, label: string): void { - if (!Number.isFinite(value) || value < min || value > max) { - throw new Error(`${label} must be between ${min} and ${max}`); - } -} - -export function normalizeLanguageCode(code?: string): string | undefined { - const trimmed = code?.trim(); - if (!trimmed) { - return undefined; - } - const normalized = trimmed.toLowerCase(); - if (!/^[a-z]{2}$/.test(normalized)) { - throw new Error("languageCode must be a 2-letter ISO 639-1 code (e.g. en, de, fr)"); - } - return normalized; -} - -export function normalizeApplyTextNormalization(mode?: string): "auto" | "on" | "off" | undefined { - const trimmed = mode?.trim(); - if (!trimmed) { - return undefined; - } - const normalized = trimmed.toLowerCase(); - if (normalized === "auto" || normalized === "on" || normalized === "off") { - return normalized; - } - throw new Error("applyTextNormalization must be one of: auto, on, off"); -} - -export function normalizeSeed(seed?: number): number | undefined { - if (seed == null) { - return undefined; - } - const next = Math.floor(seed); - if (!Number.isFinite(next) || next < 0 || next > 4_294_967_295) { - throw new Error("seed must be between 0 and 4294967295"); - } - return next; -} - -export function scheduleCleanup( - tempDir: string, - delayMs: number = TEMP_FILE_CLEANUP_DELAY_MS, -): void { - const timer = setTimeout(() => { - try { - rmSync(tempDir, { recursive: true, force: true }); - } catch { - // ignore cleanup errors - } - }, delayMs); - timer.unref(); -} +export { + normalizeApplyTextNormalization, + normalizeLanguageCode, + normalizeSeed, + requireInRange, + scheduleCleanup, +} from "../../plugin-sdk/speech-core.js"; export async function summarizeText(): Promise { throw new Error("summarizeText is unavailable in the Vitest capability contract shim");