Skip to content

Commit 8af5e75

Browse files
ntdatt812decolua
authored andcommitted
feat(tts): add Fish Audio as a text-to-speech provider
Registry entry plus one config-driven FORMAT_HANDLERS handler. The model id travels in an HTTP `model` header rather than the JSON body, and the voice is a reference_id (preset or cloned voice model). Closes #2411
1 parent 8ed9da7 commit 8af5e75

4 files changed

Lines changed: 162 additions & 0 deletions

File tree

‎open-sse/handlers/ttsProviders/genericFormats.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ async function huggingface({ baseUrl, apiKey, text, modelId }) {
5151
return responseToBase64(res, "wav");
5252
}
5353

54+
// Fish Audio: model travels in an HTTP header, the voice is a reference_id, returns binary
55+
async function fishAudio({ baseUrl, apiKey, text, modelId, voiceId }) {
56+
const res = await fetch(baseUrl, {
57+
method: "POST",
58+
headers: {
59+
"Content-Type": "application/json",
60+
"Authorization": `Bearer ${apiKey}`,
61+
"model": modelId || "s2.1-pro-free",
62+
},
63+
body: JSON.stringify({
64+
text,
65+
format: "mp3",
66+
...(voiceId ? { reference_id: voiceId } : {}),
67+
}),
68+
});
69+
if (!res.ok) await throwUpstreamError(res);
70+
return responseToBase64(res, "mp3");
71+
}
72+
5473
// Inworld: Basic auth, JSON { audioContent }
5574
async function inworld({ baseUrl, apiKey, text, modelId, voiceId }) {
5675
const res = await fetch(baseUrl, {
@@ -166,4 +185,5 @@ export const FORMAT_HANDLERS = {
166185
tortoise,
167186
openai: openaiCompat,
168187
"minimax-tts": minimaxTts,
188+
"fish-audio": fishAudio,
169189
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Fish Audio TTS — the model id travels in an HTTP `model` header rather than the
2+
// JSON body, and the voice is a reference_id (a cloned or preset voice model).
3+
export default {
4+
id: "fish-audio",
5+
alias: "fish",
6+
display: {
7+
name: "Fish Audio",
8+
icon: "record_voice_over",
9+
color: "#1E9BF0",
10+
textIcon: "FA",
11+
website: "https://fish.audio",
12+
notice: {
13+
apiKeyUrl: "https://fish.audio/app/api-keys/",
14+
},
15+
},
16+
category: "apikey",
17+
authType: "apikey",
18+
serviceKinds: ["tts"],
19+
ttsConfig: {
20+
baseUrl: "https://api.fish.audio/v1/tts",
21+
authType: "apikey",
22+
authHeader: "bearer",
23+
format: "fish-audio",
24+
models: [
25+
{ id: "s2.1-pro-free", name: "S2.1 Pro Free" },
26+
{ id: "s2.1-pro", name: "S2.1 Pro" },
27+
{ id: "s2-pro", name: "S2 Pro" },
28+
{ id: "s1", name: "S1" },
29+
],
30+
},
31+
};

‎open-sse/providers/registry/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ import p116 from "./tokenrouter.js";
119119
import p117 from "./selfhosted-stt.js";
120120
import p118 from "./selfhosted-tts.js";
121121
import p119 from "./selfhosted-embedding.js";
122+
import p120 from "./fish-audio.js";
122123

123124
export default [
124125
p0,
@@ -239,4 +240,5 @@ export default [
239240
p117,
240241
p118,
241242
p119,
243+
p120,
242244
];

‎tests/unit/fish-audio-tts.test.js‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
import REGISTRY from "../../open-sse/providers/registry/index.js";
4+
import { PROVIDER_MEDIA } from "../../open-sse/providers/index.js";
5+
import { FORMAT_HANDLERS } from "../../open-sse/handlers/ttsProviders/genericFormats.js";
6+
import { AI_PROVIDERS } from "@/shared/constants/providers";
7+
8+
const AUDIO = new Uint8Array(256).fill(7);
9+
10+
function okResponse() {
11+
return {
12+
ok: true,
13+
status: 200,
14+
headers: new Headers({ "content-type": "audio/mpeg" }),
15+
arrayBuffer: async () => AUDIO.buffer,
16+
};
17+
}
18+
19+
describe("Fish Audio TTS provider", () => {
20+
const entry = REGISTRY.find((e) => e.id === "fish-audio");
21+
22+
it("is registered as a TTS-only apikey provider", () => {
23+
expect(entry).toBeDefined();
24+
expect(entry.category).toBe("apikey");
25+
expect(entry.serviceKinds).toEqual(["tts"]);
26+
expect(PROVIDER_MEDIA["fish-audio"]?.ttsConfig?.baseUrl).toBe("https://api.fish.audio/v1/tts");
27+
});
28+
29+
it("is visible to the generic dispatcher, which reads AI_PROVIDERS", () => {
30+
// synthesizeViaConfig() looks the provider up here, not in PROVIDER_MEDIA.
31+
expect(AI_PROVIDERS["fish-audio"]?.ttsConfig?.format).toBe("fish-audio");
32+
expect(typeof FORMAT_HANDLERS["fish-audio"]).toBe("function");
33+
});
34+
35+
it("exposes the four documented models", () => {
36+
const ids = (entry.ttsConfig.models || []).map((m) => m.id);
37+
expect(ids).toEqual(["s2.1-pro-free", "s2.1-pro", "s2-pro", "s1"]);
38+
});
39+
40+
it("keeps registry ids and aliases unique", () => {
41+
const ids = REGISTRY.map((e) => e.id);
42+
expect(new Set(ids).size).toBe(ids.length);
43+
const aliases = REGISTRY.map((e) => e.alias).filter(Boolean);
44+
expect(new Set(aliases).size).toBe(aliases.length);
45+
});
46+
});
47+
48+
describe("Fish Audio TTS request shape", () => {
49+
const handler = FORMAT_HANDLERS["fish-audio"];
50+
let fetchMock;
51+
52+
beforeEach(() => {
53+
fetchMock = vi.fn(async () => okResponse());
54+
global.fetch = fetchMock;
55+
});
56+
57+
const callArgs = () => {
58+
const [url, init] = fetchMock.mock.calls.at(-1);
59+
return { url, init, body: JSON.parse(init.body) };
60+
};
61+
62+
it("sends the model as an HTTP header, not in the body", async () => {
63+
await handler({
64+
baseUrl: "https://api.fish.audio/v1/tts",
65+
apiKey: "sk-test",
66+
text: "xin chào",
67+
modelId: "s1",
68+
voiceId: "",
69+
});
70+
71+
const { url, init, body } = callArgs();
72+
expect(url).toBe("https://api.fish.audio/v1/tts");
73+
expect(init.headers.model).toBe("s1");
74+
expect(init.headers.Authorization).toBe("Bearer sk-test");
75+
expect(body).toEqual({ text: "xin chào", format: "mp3" });
76+
});
77+
78+
it("maps the voice onto reference_id, and omits it when unset", async () => {
79+
await handler({ baseUrl: "u", apiKey: "k", text: "t", modelId: "s1", voiceId: "voice-abc" });
80+
expect(callArgs().body.reference_id).toBe("voice-abc");
81+
82+
await handler({ baseUrl: "u", apiKey: "k", text: "t", modelId: "s1", voiceId: "" });
83+
expect(callArgs().body).not.toHaveProperty("reference_id");
84+
});
85+
86+
it("defaults to the free model when none is given", async () => {
87+
await handler({ baseUrl: "u", apiKey: "k", text: "t", modelId: "", voiceId: "" });
88+
expect(callArgs().init.headers.model).toBe("s2.1-pro-free");
89+
});
90+
91+
it("returns base64 audio with its format", async () => {
92+
const out = await handler({ baseUrl: "u", apiKey: "k", text: "t", modelId: "s1", voiceId: "" });
93+
expect(out.format).toBe("mp3");
94+
expect(typeof out.base64).toBe("string");
95+
expect(out.base64.length).toBeGreaterThan(0);
96+
});
97+
98+
it("surfaces the upstream error message", async () => {
99+
global.fetch = vi.fn(async () => ({
100+
ok: false,
101+
status: 402,
102+
text: async () => JSON.stringify({ message: "Insufficient credit" }),
103+
}));
104+
105+
await expect(
106+
handler({ baseUrl: "u", apiKey: "k", text: "t", modelId: "s1", voiceId: "" }),
107+
).rejects.toThrow("Insufficient credit");
108+
});
109+
});

0 commit comments

Comments
 (0)