Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';
import { mkdtemp } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { CreatorServicesConfigStore } from '../../src/creator-services/config-store.js';
import { createGatewayCreatorSource } from '../../src/gateway/creator-service-source.js';
import {
createKrillinTtsService,
KrillinTtsServiceError
} from '../../src/creator/krillin/tts-service.js';
describe('KrillinTtsService', () => {
const roots: string[] = [];
afterEach(async () => {
vi.unstubAllGlobals();
const { rm } = await import('node:fs/promises');
await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true })));
});
it.each([false, true])('validates official speech model, voices and optional style/speed (%s)', async supportsStyle => {
const root = await temporaryRoot();
const executeUtility = vi.fn();
const source = createGatewayCreatorSource({
account: { peekState: () => ({ source: 'gateway', authState: 'signed_in', activationState: 'ready', account: { id: 'a', email: '[email protected]', verified: true }, bindingVersion: 'a:1', models: [], activationError: null }), modelCredentials: async () => ({ accountId: 'a', modelKey: 'secret', keyVersion: '1', bindingVersion: 'a:1', baseUrl: 'https://gateway.example/v1', defaults: { speech: 'official-first' }, models: [{ id: 'official-first', modality: 'speech', capabilities: [], voices: ['first-voice'] }, { id: 'official-second', modality: 'speech', capabilities: supportsStyle ? ['speech_instructions', 'speech_speed'] : [], voices: ['second-voice'] }] }) },
manual: createConfigStore(createDefaultCreatorServicesConfig()), getLocalOrigin: () => 'http://127.0.0.1:39999'
});
const officialPreviewLanguages = vi.fn(async () => ({ 'second-voice': ['en'], 'first-voice': ['en', 'zh'] }));
const service = createKrillinTtsService({ resourceRoot: join(root, 'runtime'), workRoot: join(root, 'work'), configStore: source.store, executeUtility, officialPreviewLanguages });
await expect(service.listVoices('aliyun', 'personal-model')).rejects.toMatchObject({ code: 'VALIDATION_FAILED' });
const result = await service.listVoices('openai', 'official-second');
expect(result).toMatchObject({ provider: 'openai', model: 'official-second', voices: [{ id: 'second-voice', provider: 'openai', previewLanguages: ['en'] }] });
expect(officialPreviewLanguages).toHaveBeenCalledWith('official-second');
officialPreviewLanguages.mockRejectedValueOnce(new Error('sample catalog unavailable'));
expect(await service.listVoices('openai', 'official-second')).toMatchObject({ voices: [{ id: 'second-voice', previewLanguages: [] }] });
expect((await service.listVoices('openai')).voices.map(voice => voice.id)).toEqual(['first-voice']);
expect(executeUtility).not.toHaveBeenCalled();
const fetchMock = vi.fn<typeof fetch>(async () => new Response(new Uint8Array([1, 2, 3]), { headers: { 'Content-Type': 'audio/mpeg' } }));
vi.stubGlobal('fetch', fetchMock);
const audio = await service.synthesize({ text: 'hello', provider: 'openai', model: 'official-second', format: 'mp3' });
expect(audio).toMatchObject({ model: 'official-second', voiceId: 'second-voice' });
expect(JSON.parse(String(fetchMock.mock.lastCall?.[1]?.body))).toMatchObject({ model: 'official-second', voice: 'second-voice' });
await expect(service.synthesize({ text: 'hello', model: 'personal-model' })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' });
await expect(service.synthesize({ text: 'hello', model: 'official-second', voiceId: 'first-voice' })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' });
await expect(source.runForJob('job', 'stage', () => service.synthesize({ text: 'hello', model: 'official-second' }))).rejects.toMatchObject({ code: 'VALIDATION_FAILED' });
expect(fetchMock).toHaveBeenCalledOnce();
const styled = service.synthesize({ text: 'hello', model: 'official-second', instructions: 'Calm', speed: 1.1, format: 'mp3' });
if (supportsStyle) {
await styled;
expect(JSON.parse(String(fetchMock.mock.lastCall?.[1]?.body))).toMatchObject({ instructions: 'Calm', speed: 1.1 });
} else await expect(styled).rejects.toMatchObject({ code: 'unsupported_capability' });
});
it('projects catalog voice descriptions without changing IDs or inventing absent metadata', async () => {
const root = await temporaryRoot();
const profile = { gender: 'female' as const, traits: { zh: ['温暖'], en: ['Warm'] },
useCases: { zh: ['旁白'], en: ['Narration'] }, sourceUrls: ['https://docs.example/voices'] };
const source = createGatewayCreatorSource({
account: { peekState: () => ({ source: 'gateway', authState: 'signed_in', activationState: 'ready', account: { id: 'a', email: '[email protected]', verified: true }, bindingVersion: 'a:1', models: [], activationError: null }),
modelCredentials: async () => ({ accountId: 'a', modelKey: 'secret', keyVersion: '1', bindingVersion: 'a:1', baseUrl: 'https://gateway.example/v1', defaults: { speech: 'speech' },
models: [{ id: 'speech', modality: 'speech', capabilities: [], voices: ['known', 'unknown'], voiceProfiles: { known: profile, unadmitted: profile } }] }) },
manual: createConfigStore(createDefaultCreatorServicesConfig()), getLocalOrigin: () => 'http://127.0.0.1:39999'
});
const service = createKrillinTtsService({ resourceRoot: join(root, 'runtime'), workRoot: join(root, 'work'), configStore: source.store, executeUtility: vi.fn() });
const result = await service.listVoices('openai', 'speech');
expect(result.voices.map(voice => voice.id)).toEqual(['known', 'unknown']);
expect(result.voices[0]).toMatchObject({ ...profile, id: 'known', name: 'known' });
expect(result.voices[1]).not.toHaveProperty('gender');
expect(result.voices[1]).not.toHaveProperty('traits');
});
it('lists the bundled Qwen3 voice catalog without calling an outdated Runtime', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.aliyun.apiKey = 'dashscope-key';
const executeUtility = vi.fn();
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config),
executeUtility
});
const response = await service.listVoices('aliyun');
expect(response).toMatchObject({
provider: 'aliyun',
model: 'qwen3-tts-flash'
});
expect(response.voices).toEqual(expect.arrayContaining([
expect.objectContaining({
id: 'Cherry',
name: '芊悦',
provider: 'aliyun',
language: 'zh-CN',
kind: 'builtin',
recommended: true
}),
expect.objectContaining({
id: 'Kiki',
name: '粤语-阿清',
provider: 'aliyun',
language: 'zh-yue',
kind: 'builtin'
})
]));
expect(response.voices).toHaveLength(48);
expect(executeUtility).not.toHaveBeenCalled();
});
it('lists the static OpenAI voice catalog without scanning or launching the Runtime', async () => {
const root = await temporaryRoot();
const executeUtility = vi.fn();
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(createDefaultCreatorServicesConfig()),
executeUtility
});
const response = await service.listVoices('openai');
expect(response).toMatchObject({
provider: 'openai',
model: 'gpt-4o-mini-tts'
});
expect(response.voices).toHaveLength(13);
expect(response.voices).toEqual(expect.arrayContaining([
expect.objectContaining({
id: 'marin',
name: 'Marin',
provider: 'openai',
kind: 'builtin',
recommended: true
}),
expect.objectContaining({
id: 'cedar',
name: 'Cedar',
provider: 'openai',
kind: 'builtin',
recommended: true
})
]));
expect(executeUtility).not.toHaveBeenCalled();
});
it('limits the first Qwen3 snapshot to voices supported by that model version', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.aliyun.model = 'qwen3-tts-flash-2025-09-18';
const executeUtility = vi.fn();
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config),
executeUtility
});
const response = await service.listVoices('aliyun');
expect(response.voices.map(voice => voice.id)).toEqual([
'Cherry',
'Serena',
'Ethan',
'Chelsie',
'Momo',
'Vivian',
'Moon',
'Maia',
'Kai',
'Nofish',
'Bella'
]);
expect(executeUtility).not.toHaveBeenCalled();
});
it('uses the Runtime for other models and filters explicit model mismatches', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.aliyun.model = 'custom-tts-model';
const executeUtility = vi.fn(async input => {
expect(input.config.tts.aliyun.model).toBe('custom-tts-model');
expect(input.args).toEqual(['voices', '--provider', 'aliyun']);
return {
ok: true,
voices: [
{ code: 'legacy-voice', name: 'Legacy' },
{ code: 'matching-voice', supported_models: ['custom-tts-model'] },
{ code: 'other-voice', supported_models: ['another-model'] }
]
};
});
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config),
executeUtility
});
const response = await service.listVoices('aliyun');
expect(response.voices.map(voice => voice.id)).toEqual([
'legacy-voice',
'matching-voice'
]);
expect(executeUtility).toHaveBeenCalledOnce();
});
it('waits for Runtime verification before invoking the Runtime utility', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.aliyun.model = 'custom-tts-model';
let releaseVerification!: () => void;
const ensureRuntimeReady = vi.fn(() => new Promise<void>(resolve => {
releaseVerification = resolve;
}));
const executeUtility = vi.fn(async () => ({ ok: true, voices: [] }));
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config),
ensureRuntimeReady,
executeUtility
});
const listing = service.listVoices('aliyun');
await vi.waitFor(() => expect(ensureRuntimeReady).toHaveBeenCalledOnce());
expect(executeUtility).not.toHaveBeenCalled();
releaseVerification();
await listing;
expect(executeUtility).toHaveBeenCalledOnce();
});
it('synthesizes through the configured provider with voice controls', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.openai.apiKey = 'sk-test';
const executeSynthesis = vi.fn(async () => ({
content: Buffer.from('speech-audio'),
format: 'mp3' as const
}));
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config),
executeSynthesis
});
const result = await service.synthesize({
text: 'Speak this text.',
provider: 'openai',
voiceId: 'marin',
format: 'mp3',
speed: 1.1,
instructions: 'Speak warmly.'
});
expect(result).toMatchObject({
provider: 'openai',
model: 'gpt-4o-mini-tts',
voiceId: 'marin',
format: 'mp3',
mime: 'audio/mpeg'
});
expect(result.content.toString()).toBe('speech-audio');
expect(executeSynthesis).toHaveBeenCalledWith(expect.objectContaining({
provider: 'openai',
model: 'gpt-4o-mini-tts',
voiceId: 'marin',
format: 'mp3',
speed: 1.1,
instructions: 'Speak warmly.'
}));
});
it('uses the Qwen multimodal endpoint and downloads the returned Aliyun audio', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.provider = 'aliyun';
config.tts.aliyun.baseUrl = 'https://tts.example.com/api/v1';
config.tts.aliyun.apiKey = 'dashscope-key';
const wav = Buffer.from('RIFFfixture-wave');
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({
output: { audio: { url: 'http://audio.example.com/result.wav?signature=secret' } }
}), { status: 200, headers: { 'content-type': 'application/json' } }))
.mockResolvedValueOnce(new Response(wav, {
status: 200,
headers: { 'content-type': 'audio/wav' }
}));
vi.stubGlobal('fetch', fetchMock);
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config)
});
const result = await service.synthesize({
text: '测试文本',
provider: 'aliyun',
model: 'qwen3-tts-flash',
voiceId: 'Cherry',
format: 'wav'
});
expect(result).toMatchObject({
provider: 'aliyun',
model: 'qwen3-tts-flash',
voiceId: 'Cherry',
format: 'wav',
mime: 'audio/wav'
});
expect(result.content).toEqual(wav);
expect(fetchMock.mock.calls[0]![0]).toBe(
'https://tts.example.com/api/v1/services/aigc/multimodal-generation/generation'
);
const request = JSON.parse(String((fetchMock.mock.calls[0]![1] as RequestInit).body));
expect(request).toEqual({
model: 'qwen3-tts-flash',
input: { text: '测试文本', voice: 'Cherry', language_type: 'Chinese' }
});
expect(String(fetchMock.mock.calls[1]![0])).toBe(
'https://audio.example.com/result.wav?signature=secret'
);
});
it('lists bundled Volcengine voices and synthesizes through the V1 HTTP API', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.provider = 'volcengine';
config.tts.volcengine.appId = 'app-1';
config.tts.volcengine.accessToken = 'token-1';
// The default cluster is now the 2.0 one, which routes to V3; this case
// exercises the legacy V1 HTTP path, so pin it explicitly.
config.tts.volcengine.model = 'volcano_tts';
config.tts.volcengine.defaultVoiceId = 'BV001_streaming';
const audio = Buffer.from('mp3-bytes');
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
code: 3000,
message: 'Success',
data: audio.toString('base64')
}), { status: 200, headers: { 'content-type': 'application/json' } }));
vi.stubGlobal('fetch', fetchMock);
const executeUtility = vi.fn();
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config),
executeUtility
});
await expect(service.listVoices('volcengine')).resolves.toMatchObject({
provider: 'volcengine',
model: 'volcano_tts',
voices: expect.arrayContaining([
expect.objectContaining({ id: 'BV001_streaming', recommended: true })
])
});
expect(executeUtility).not.toHaveBeenCalled();
const result = await service.synthesize({
text: '你好,火山。',
provider: 'volcengine',
voiceId: 'BV001_streaming',
format: 'mp3'
});
expect(result.content).toEqual(audio);
expect(fetchMock.mock.calls[0]![0]).toBe('https://openspeech.bytedance.com/api/v1/tts');
expect((fetchMock.mock.calls[0]![1] as RequestInit).headers).toMatchObject({
authorization: 'Bearer;token-1'
});
const request = JSON.parse(String((fetchMock.mock.calls[0]![1] as RequestInit).body));
expect(request.app).toEqual({
appid: 'app-1',
token: 'token-1',
cluster: 'volcano_tts'
});
expect(request.audio.voice_type).toBe('BV001_streaming');
expect(request.request.operation).toBe('query');
});
it('synthesizes Doubao 2.0 and cloned voices through the V3 unidirectional API', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.provider = 'volcengine';
config.tts.volcengine.appId = 'app-1';
config.tts.volcengine.accessToken = 'token-1';
const audio = Buffer.from('v3-bytes');
const ndjson = [
JSON.stringify({ code: 0, data: audio.toString('base64') }),
JSON.stringify({ code: 20000000, message: 'OK' })
].join('\n');
const fetchMock = vi.fn(async (_url: string | URL, _init?: RequestInit) => (
new Response(ndjson, { status: 200 })
));
vi.stubGlobal('fetch', fetchMock);
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config),
executeUtility: vi.fn()
});
const twoOh = await service.listVoices('volcengine', 'seed-tts-2.0');
expect(twoOh.voices).toEqual(expect.arrayContaining([
expect.objectContaining({
id: 'zh_female_gaolengyujie_uranus_bigtts',
name: '高冷御姐 2.0',
gender: 'female'
})
]));
expect(twoOh.voices.some(voice => voice.id === 'BV019_streaming')).toBe(false);
const result = await service.synthesize({
text: '高冷御姐试听',
provider: 'volcengine',
voiceId: 'zh_female_gaolengyujie_uranus_bigtts',
format: 'mp3'
});
expect(result.content).toEqual(audio);
expect(fetchMock.mock.calls[0]![0]).toBe(
'https://openspeech.bytedance.com/api/v3/tts/unidirectional'
);
expect((fetchMock.mock.calls[0]![1] as RequestInit).headers).toMatchObject({
'X-Api-App-Id': 'app-1',
'X-Api-Access-Key': 'token-1',
'X-Api-Resource-Id': 'seed-tts-2.0'
});
const request = JSON.parse(String((fetchMock.mock.calls[0]![1] as RequestInit).body));
expect(request.req_params.speaker).toBe('zh_female_gaolengyujie_uranus_bigtts');
await service.synthesize({
text: '克隆音色',
provider: 'volcengine',
model: 'seed-icl-2.0',
voiceId: 'S_cloned_speaker',
format: 'mp3'
});
expect((fetchMock.mock.calls[1]![1] as RequestInit).headers).toMatchObject({
'X-Api-Resource-Id': 'seed-icl-2.0'
});
});
it('uses the speech API key without requiring an App ID for Doubao 2.0', async () => {
const root = await temporaryRoot();
const config = createDefaultCreatorServicesConfig();
config.tts.provider = 'volcengine';
config.tts.volcengine.accessToken = 'api-key-1';
const audio = Buffer.from('v3-api-key-bytes');
const fetchMock = vi.fn(async (_url: string | URL, _init?: RequestInit) => new Response([
JSON.stringify({ code: 0, data: audio.toString('base64') }),
JSON.stringify({ code: 20000000, message: 'OK' })
].join('\n'), { status: 200 }));
vi.stubGlobal('fetch', fetchMock);
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(config)
});
await expect(service.synthesize({
text: '豆包语音二点零',
provider: 'volcengine',
voiceId: 'zh_female_cancan_uranus_bigtts',
format: 'mp3'
})).resolves.toMatchObject({ model: 'seed-tts-2.0' });
const headers = (fetchMock.mock.calls[0]?.[1] as RequestInit | undefined)?.headers;
expect(headers).toMatchObject({
'X-Api-Key': 'api-key-1',
'X-Api-Resource-Id': 'seed-tts-2.0'
});
expect(headers).not.toHaveProperty('X-Api-App-Id');
});
it('requires the configured provider API key before synthesis', async () => {
const root = await temporaryRoot();
const service = createKrillinTtsService({
resourceRoot: join(root, 'runtime'),
workRoot: join(root, 'work'),
configStore: createConfigStore(createDefaultCreatorServicesConfig()),
executeUtility: vi.fn()
});
await expect(service.synthesize({
text: 'Missing configuration',
provider: 'minimax',
voiceId: 'English_Graceful_Lady'
})).rejects.toMatchObject({
code: 'creator_tts_config_missing',
statusCode: 409
} satisfies Partial<KrillinTtsServiceError>);
});
async function temporaryRoot() {
const root = await mkdtemp(join(tmpdir(), 'opencreator-tts-service-'));
roots.push(root);
return root;
}
});
function createConfigStore(
config: ReturnType<typeof createDefaultCreatorServicesConfig>
): CreatorServicesConfigStore {
return {
read: vi.fn(async () => structuredClone(config)),
write: vi.fn(async next => next),
reset: vi.fn(async () => createDefaultCreatorServicesConfig())
};
}