Update version to 0.4.9, enhance README with Trendshift badge, and add new embedding models to providerModels.js. Refactor TTS handling to support additional providers and improve API key validation for media providers.

This commit is contained in:
decolua
2026-04-29 11:34:39 +07:00
parent e8aa5e2222
commit 512e3de371
20 changed files with 586 additions and 83 deletions

View File

@@ -0,0 +1,65 @@
import { NextResponse } from "next/server";
import { getProviderConnections } from "@/lib/localDb";
const langNames = new Intl.DisplayNames(["en"], { type: "language" });
/**
* GET /api/media-providers/tts/deepgram/voices[?lang=en]
* Returns { languages, byLang } grouped by language code (same shape as edge-tts/elevenlabs/inworld)
* Each Deepgram voice = one model (canonical_name like "aura-2-thalia-en")
*/
export async function GET(request) {
try {
const { searchParams } = new URL(request.url);
const langFilter = searchParams.get("lang");
const connections = await getProviderConnections({ provider: "deepgram", isActive: true });
const apiKey = connections[0]?.apiKey;
if (!apiKey) return NextResponse.json({ error: "No Deepgram connection found" }, { status: 400 });
const res = await fetch("https://api.deepgram.com/v1/models", {
headers: { "Authorization": `Token ${apiKey}` },
});
if (!res.ok) {
const text = await res.text().catch(() => "");
return NextResponse.json({ error: `Deepgram API ${res.status}: ${text || "Failed"}` }, { status: 502 });
}
const data = await res.json();
const ttsModels = data.tts || [];
const byLang = {};
for (const m of ttsModels) {
// Deepgram returns `languages: ["en"]` or sometimes language inferred from canonical_name suffix
const langs = Array.isArray(m.languages) && m.languages.length
? m.languages
: [m.canonical_name?.split("-").pop() || "en"];
for (const code of langs) {
if (!byLang[code]) {
byLang[code] = {
code,
name: (() => { try { return langNames.of(code); } catch { return code; } })(),
voices: [],
};
}
const voiceId = m.canonical_name || m.name;
if (!byLang[code].voices.find((x) => x.id === voiceId)) {
byLang[code].voices.push({
id: voiceId,
name: m.name || voiceId,
gender: m.metadata?.tags?.find((t) => t === "masculine" || t === "feminine") || "",
lang: code,
});
}
}
}
const languages = Object.values(byLang).sort((a, b) => a.name.localeCompare(b.name));
if (langFilter) {
return NextResponse.json({ voices: byLang[langFilter]?.voices || [] });
}
return NextResponse.json({ languages, byLang });
} catch (err) {
return NextResponse.json({ error: err.message || "Failed to fetch voices" }, { status: 502 });
}
}

View File

@@ -0,0 +1,61 @@
import { NextResponse } from "next/server";
import { getProviderConnections } from "@/lib/localDb";
const langNames = new Intl.DisplayNames(["en"], { type: "language" });
/**
* GET /api/media-providers/tts/inworld/voices[?lang=en]
* Returns { languages, byLang } grouped by language code (same shape as edge-tts/elevenlabs)
*/
export async function GET(request) {
try {
const { searchParams } = new URL(request.url);
const langFilter = searchParams.get("lang");
const connections = await getProviderConnections({ provider: "inworld", isActive: true });
const apiKey = connections[0]?.apiKey;
if (!apiKey) return NextResponse.json({ error: "No Inworld connection found" }, { status: 400 });
const res = await fetch("https://api.inworld.ai/tts/v1/voices", {
headers: { "Authorization": `Basic ${apiKey}` },
});
if (!res.ok) {
const text = await res.text().catch(() => "");
return NextResponse.json({ error: `Inworld API ${res.status}: ${text || "Failed"}` }, { status: 502 });
}
const data = await res.json();
const voices = data.voices || [];
const byLang = {};
for (const v of voices) {
// Each voice has `languages: ["en", "es", ...]`
const langs = Array.isArray(v.languages) && v.languages.length ? v.languages : ["en"];
for (const code of langs) {
if (!byLang[code]) {
byLang[code] = {
code,
name: (() => { try { return langNames.of(code); } catch { return code; } })(),
voices: [],
};
}
if (!byLang[code].voices.find((x) => x.id === v.voiceId)) {
byLang[code].voices.push({
id: v.voiceId,
name: v.displayName || v.voiceId,
gender: v.gender || "",
lang: code,
});
}
}
}
const languages = Object.values(byLang).sort((a, b) => a.name.localeCompare(b.name));
if (langFilter) {
return NextResponse.json({ voices: byLang[langFilter]?.voices || [] });
}
return NextResponse.json({ languages, byLang });
} catch (err) {
return NextResponse.json({ error: err.message || "Failed to fetch voices" }, { status: 502 });
}
}

View File

@@ -40,6 +40,43 @@ async function probeWebProvider(provider, apiKey) {
return res.status !== 401 && res.status !== 403;
}
// Probe a tts/embedding provider using ttsConfig/embeddingConfig.
// Returns true if API key is accepted (status !== 401 && !== 403); null to skip.
async function probeMediaProvider(provider, apiKey) {
const p = AI_PROVIDERS[provider];
if (!p) return null;
// Only probe providers that are media-only (not LLM dual-purpose, let LLM validate handle those)
const kinds = p.serviceKinds || ["llm"];
const isMediaOnly = kinds.every((k) => k === "tts" || k === "embedding" || k === "stt");
if (!isMediaOnly) return null;
const cfg = p.ttsConfig || p.embeddingConfig;
if (!cfg) return null;
if (p.noAuth || cfg.authType === "none") return true;
// Skip auth schemes that need provider-specific data
if (cfg.authHeader === "playht" || cfg.authHeader === "aws-sigv4") return null;
const headers = { "Content-Type": "application/json" };
// Apply auth based on authHeader
switch (cfg.authHeader) {
case "bearer": headers["Authorization"] = `Bearer ${apiKey}`; break;
case "x-api-key": headers["x-api-key"] = apiKey; break;
case "xi-api-key": headers["xi-api-key"] = apiKey; break;
case "token": headers["Authorization"] = `Token ${apiKey}`; break;
case "basic": headers["Authorization"] = `Basic ${apiKey}`; break;
default: return null;
}
// Minimal POST body — server will reject auth before validating body
const res = await fetch(cfg.baseUrl, {
method: "POST",
headers,
body: JSON.stringify({ input: "ping", text: "ping", model: cfg.models?.[0]?.id || "test" }),
signal: AbortSignal.timeout(8000),
});
return res.status !== 401 && res.status !== 403;
}
// POST /api/providers/validate - Validate API key with provider
export async function POST(request) {
try {
@@ -192,6 +229,15 @@ export async function POST(request) {
});
}
// Generic probe for tts/embedding providers (config-driven)
const mediaResult = await probeMediaProvider(provider, apiKey);
if (mediaResult !== null) {
return NextResponse.json({
valid: mediaResult,
error: mediaResult ? null : "Invalid API key",
});
}
switch (provider) {
case "openai":
const openaiRes = await fetch("https://api.openai.com/v1/models", {