feat: add minimax tts support (#1043)

This commit is contained in:
Thiên Toán
2026-05-13 15:34:10 +07:00
committed by GitHub
parent 3c2503c4b4
commit 74c9879e8e
10 changed files with 578 additions and 32 deletions
+120
View File
@@ -0,0 +1,120 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { handleTtsCore } from "../../open-sse/handlers/ttsCore.js";
const originalFetch = global.fetch;
describe("MiniMax TTS", () => {
beforeEach(() => {
global.fetch = vi.fn();
});
afterEach(() => {
global.fetch = originalFetch;
});
it("sends MiniMax T2A payload and converts hex audio to base64 JSON", async () => {
global.fetch.mockResolvedValueOnce(
new Response(
JSON.stringify({
data: { audio: "00010203", status: 2 },
extra_info: { audio_format: "mp3" },
base_resp: { status_code: 0, status_msg: "success" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)
);
const result = await handleTtsCore({
provider: "minimax",
model: "speech-2.8-hd/English_expressive_narrator",
input: "Hello from MiniMax",
credentials: { apiKey: "test-key" },
responseFormat: "json",
});
expect(result.success).toBe(true);
expect(global.fetch).toHaveBeenCalledWith(
"https://api.minimax.io/v1/t2a_v2",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
"Content-Type": "application/json",
Authorization: "Bearer test-key",
}),
})
);
const sent = JSON.parse(global.fetch.mock.calls[0][1].body);
expect(sent).toMatchObject({
model: "speech-2.8-hd",
text: "Hello from MiniMax",
stream: false,
language_boost: "auto",
output_format: "hex",
voice_setting: {
voice_id: "English_expressive_narrator",
speed: 1,
vol: 1,
pitch: 0,
},
audio_setting: {
sample_rate: 32000,
bitrate: 128000,
format: "mp3",
channel: 1,
},
});
const body = await result.response.json();
expect(body).toEqual({ audio: "AAECAw==", format: "mp3" });
});
it("uses the default MiniMax voice when no voice is provided", async () => {
global.fetch.mockResolvedValueOnce(
new Response(
JSON.stringify({
data: { audio: "00010203", status: 2 },
base_resp: { status_code: 0, status_msg: "success" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)
);
const result = await handleTtsCore({
provider: "minimax-cn",
model: "speech-2.8-turbo",
input: "Hello",
credentials: { apiKey: "test-key" },
responseFormat: "json",
});
expect(result.success).toBe(true);
expect(global.fetch.mock.calls[0][0]).toBe("https://api.minimaxi.com/v1/t2a_v2");
const sent = JSON.parse(global.fetch.mock.calls[0][1].body);
expect(sent.model).toBe("speech-2.8-turbo");
expect(sent.voice_setting.voice_id).toBe("English_expressive_narrator");
});
it("surfaces MiniMax base_resp errors", async () => {
global.fetch.mockResolvedValueOnce(
new Response(
JSON.stringify({
base_resp: { status_code: 1008, status_msg: "insufficient quota" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)
);
const result = await handleTtsCore({
provider: "minimax",
model: "speech-2.8-hd/English_expressive_narrator",
input: "Hello",
credentials: { apiKey: "test-key" },
});
expect(result.success).toBe(false);
expect(result.status).toBe(502);
expect(result.error).toContain("insufficient quota");
});
});
+116
View File
@@ -0,0 +1,116 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("../../open-sse/utils/proxyFetch.js", () => ({
proxyAwareFetch: vi.fn(),
}));
import { proxyAwareFetch } from "../../open-sse/utils/proxyFetch.js";
import { getUsageForProvider } from "../../open-sse/services/usage.js";
function usageResponse(modelRemains) {
return new Response(
JSON.stringify({
base_resp: { status_code: 0, status_msg: "success" },
model_remains: modelRemains,
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
}
describe("MiniMax usage", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("parses token-plan TTS quota counts as used counts", async () => {
proxyAwareFetch.mockResolvedValueOnce(
usageResponse([
{
model_name: "text to speech hd",
current_interval_total_count: 4000,
current_interval_usage_count: 25,
current_weekly_total_count: 12000,
current_weekly_usage_count: 100,
end_time: "2026-05-12T10:00:00.000Z",
weekly_end_time: "2026-05-19T10:00:00.000Z",
},
])
);
const usage = await getUsageForProvider({
provider: "minimax",
apiKey: "test-key",
});
expect(usage.message).toBeUndefined();
expect(usage.quotas["Text to Speech HD (5h)"]).toMatchObject({
used: 25,
total: 4000,
remaining: 3975,
});
expect(usage.quotas["Text to Speech HD (7d)"]).toMatchObject({
used: 100,
total: 12000,
remaining: 11900,
});
});
it("parses coding-plan TTS quota counts as remaining counts", async () => {
proxyAwareFetch.mockResolvedValueOnce(
usageResponse([
{
modelName: "Text to Speech HD",
currentIntervalTotalCount: 4000,
currentIntervalUsageCount: 4000,
currentWeeklyTotalCount: 12000,
currentWeeklyUsageCount: 11800,
remainsTime: 1000,
weeklyRemainsTime: 2000,
},
])
);
const usage = await getUsageForProvider({
provider: "minimax-cn",
apiKey: "test-key",
});
expect(usage.message).toBeUndefined();
expect(usage.quotas["Text to Speech HD (5h)"]).toMatchObject({
used: 0,
total: 4000,
remaining: 4000,
});
expect(usage.quotas["Text to Speech HD (7d)"]).toMatchObject({
used: 200,
total: 12000,
remaining: 11800,
});
});
it("keeps non-TTS MiniMax quota rows instead of filtering to text only", async () => {
proxyAwareFetch.mockResolvedValueOnce(
usageResponse([
{
model_name: "music-2.6",
current_interval_total_count: 100,
current_interval_usage_count: 5,
},
{
model_name: "image-01",
current_interval_total_count: 50,
current_interval_usage_count: 2,
},
])
);
const usage = await getUsageForProvider({
provider: "minimax",
apiKey: "test-key",
});
expect(Object.keys(usage.quotas)).toEqual(["Music 2.6 (5h)", "Image 01 (5h)"]);
expect(usage.quotas["Music 2.6 (5h)"].used).toBe(5);
expect(usage.quotas["Image 01 (5h)"].used).toBe(2);
});
});
+83
View File
@@ -0,0 +1,83 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
vi.mock("../../src/lib/localDb.js", () => ({
getProviderConnections: vi.fn(),
}));
import { getProviderConnections } from "../../src/lib/localDb.js";
import { GET } from "../../src/app/api/media-providers/tts/minimax/voices/route.js";
const originalFetch = global.fetch;
describe("MiniMax voices API", () => {
beforeEach(() => {
global.fetch = vi.fn();
vi.clearAllMocks();
});
afterEach(() => {
global.fetch = originalFetch;
});
it("fetches global MiniMax voices with stored API key", async () => {
getProviderConnections.mockResolvedValueOnce([{ apiKey: "test-key" }]);
global.fetch.mockResolvedValueOnce(
new Response(
JSON.stringify({
system_voice: [
{ voice_id: "English_expressive_narrator", voice_name: "Expressive Narrator" },
{ voice_id: "Chinese (Mandarin)_female_beijing", voice_name: "Female Beijing" },
],
voice_cloning: [{ voice_id: "clone_123", voice_name: "My Voice" }],
base_resp: { status_code: 0, status_msg: "success" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)
);
const response = await GET(new Request("http://localhost/api/media-providers/tts/minimax/voices"));
const body = await response.json();
expect(response.status).toBe(200);
expect(getProviderConnections).toHaveBeenCalledWith({ provider: "minimax", isActive: true });
expect(global.fetch).toHaveBeenCalledWith(
"https://api.minimax.io/v1/get_voice",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
Authorization: "Bearer test-key",
"Content-Type": "application/json",
}),
body: JSON.stringify({ voice_type: "all" }),
})
);
expect(body.byLang.English.voices[0].id).toBe("English_expressive_narrator");
expect(body.byLang["Chinese (Mandarin)"].voices[0].id).toBe("Chinese (Mandarin)_female_beijing");
expect(body.byLang.Custom.voices[0]).toMatchObject({
id: "clone_123",
name: "My Voice · Cloned",
category: "voice_cloning",
});
});
it("fetches China MiniMax voices when provider=minimax-cn", async () => {
getProviderConnections.mockResolvedValueOnce([{ apiKey: "test-key" }]);
global.fetch.mockResolvedValueOnce(
new Response(
JSON.stringify({
system_voice: [{ voice_id: "Chinese (Mandarin)_female_beijing", voice_name: "Female Beijing" }],
base_resp: { status_code: 0, status_msg: "success" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)
);
const response = await GET(new Request("http://localhost/api/media-providers/tts/minimax/voices?provider=minimax-cn"));
const body = await response.json();
expect(response.status).toBe(200);
expect(getProviderConnections).toHaveBeenCalledWith({ provider: "minimax-cn", isActive: true });
expect(global.fetch.mock.calls[0][0]).toBe("https://api.minimaxi.com/v1/get_voice");
expect(body.byLang["Chinese (Mandarin)"].voices[0].id).toBe("Chinese (Mandarin)_female_beijing");
});
});