From 17c77f13079f05ac21bcbd99902a548b11718ccf Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 14:51:29 +0100 Subject: [PATCH] perf(test): skip tts provider lookup without directives --- src/tts/directives.test.ts | 22 ++++++++++++++++++++++ src/tts/directives.ts | 24 ++++++++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/tts/directives.test.ts b/src/tts/directives.test.ts index 0495d67ad27..cee0ef6902a 100644 --- a/src/tts/directives.test.ts +++ b/src/tts/directives.test.ts @@ -56,6 +56,28 @@ const fullPolicy: SpeechModelOverridePolicy = { }; describe("parseTtsDirectives provider-aware routing", () => { + it("does not resolve providers when text has no directives", () => { + const failProvider = { + get id() { + throw new Error("provider should not be read without directives"); + }, + get autoSelectOrder() { + throw new Error("provider order should not be read without directives"); + }, + } as unknown as SpeechProviderPlugin; + + const result = parseTtsDirectives("hello without TTS markup", fullPolicy, { + providers: [failProvider, failProvider], + }); + + expect(result).toEqual({ + cleanedText: "hello without TTS markup", + overrides: {}, + warnings: [], + hasDirective: false, + }); + }); + it("routes generic speed to the explicitly declared provider", () => { const result = parseTtsDirectives( "hello [[tts:provider=minimax speed=1.2]] world", diff --git a/src/tts/directives.ts b/src/tts/directives.ts index 80daf1e153d..c572cde07e9 100644 --- a/src/tts/directives.ts +++ b/src/tts/directives.ts @@ -62,7 +62,15 @@ export function parseTtsDirectives( return { cleanedText: text, overrides: {}, warnings: [], hasDirective: false }; } - const providers = resolveDirectiveProviders(options); + if (!/\[\[tts:/iu.test(text)) { + return { cleanedText: text, overrides: {}, warnings: [], hasDirective: false }; + } + + let providers: SpeechProviderPlugin[] | undefined; + const getProviders = () => { + providers ??= resolveDirectiveProviders(options); + return providers; + }; const overrides: TtsDirectiveOverrides = {}; const warnings: string[] = []; let cleanedText = text; @@ -107,10 +115,14 @@ export function parseTtsDirectives( } } - const orderedProviders = prioritizeProvider( - providers, - declaredProviderId ?? normalizeLowercaseStringOrEmpty(options?.preferredProviderId), - ); + let orderedProviders: SpeechProviderPlugin[] | undefined; + const getOrderedProviders = () => { + orderedProviders ??= prioritizeProvider( + getProviders(), + declaredProviderId ?? normalizeLowercaseStringOrEmpty(options?.preferredProviderId), + ); + return orderedProviders; + }; for (const token of tokens) { const eqIndex = token.indexOf("="); @@ -127,7 +139,7 @@ export function parseTtsDirectives( continue; } - for (const provider of orderedProviders) { + for (const provider of getOrderedProviders()) { const parsed = provider.parseDirectiveToken?.({ key, value: rawValue,