Skip to content

Commit cf8cfa8

Browse files
fix(gradium): keep isConfigured from throwing on invalid baseUrl (#108551)
* fix(gradium): keep isConfigured from throwing on invalid baseUrl * refactor(gradium): narrow configuration validation Co-authored-by: moguangyu5-design <[email protected]> --------- Co-authored-by: moguangyu5-design <[email protected]> Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent fbc5529 commit cf8cfa8

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎extensions/gradium/speech-provider.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ describe("gradium speech provider", () => {
4747
}
4848
});
4949

50+
it("reports not configured for an invalid baseUrl instead of throwing", () => {
51+
const original = process.env.GRADIUM_API_KEY;
52+
try {
53+
delete process.env.GRADIUM_API_KEY;
54+
expect(
55+
provider.isConfigured({
56+
providerConfig: { apiKey: String(true), baseUrl: "https://example.com" },
57+
timeoutMs: 5_000,
58+
}),
59+
).toBe(false);
60+
expect(
61+
provider.isConfigured({
62+
providerConfig: { apiKey: String(true), baseUrl: "not-a-url" },
63+
timeoutMs: 5_000,
64+
}),
65+
).toBe(false);
66+
} finally {
67+
if (original !== undefined) {
68+
process.env.GRADIUM_API_KEY = original;
69+
}
70+
}
71+
});
72+
5073
it("synthesizes audio via the Gradium TTS endpoint", async () => {
5174
const audioData = Buffer.from("wav-audio-data");
5275
const fetchMock = vi.fn().mockResolvedValue(new Response(audioData, { status: 200 }));

‎extensions/gradium/speech-provider.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ function readGradiumProviderConfig(config: SpeechProviderConfig): GradiumProvide
3939
};
4040
}
4141

42+
function isGradiumProviderConfigured(config: SpeechProviderConfig): boolean {
43+
const apiKey = trimToUndefined(config.apiKey) ?? trimToUndefined(process.env.GRADIUM_API_KEY);
44+
if (!apiKey) {
45+
return false;
46+
}
47+
try {
48+
normalizeGradiumBaseUrl(trimToUndefined(config.baseUrl));
49+
return true;
50+
} catch {
51+
// Provider selection is a predicate; synthesis reports the precise URL error.
52+
return false;
53+
}
54+
}
55+
4256
function resolveGeneratedAudioMaxBytes(req: {
4357
cfg: { agents?: { defaults?: { mediaMaxMb?: number } } };
4458
}): number {
@@ -81,8 +95,7 @@ export function buildGradiumSpeechProvider(): SpeechProviderPlugin {
8195
resolveConfig: ({ rawConfig }) => normalizeGradiumProviderConfig(rawConfig),
8296
parseDirectiveToken,
8397
listVoices: async () => GRADIUM_VOICES.map((v) => ({ id: v.id, name: v.name })),
84-
isConfigured: ({ providerConfig }) =>
85-
Boolean(readGradiumProviderConfig(providerConfig).apiKey || process.env.GRADIUM_API_KEY),
98+
isConfigured: ({ providerConfig }) => isGradiumProviderConfigured(providerConfig),
8699
synthesize: async (req) => {
87100
const config = readGradiumProviderConfig(req.providerConfig);
88101
const overrides = req.providerOverrides ?? {};

0 commit comments

Comments
 (0)