generated from fahricansecer/boilerplate-fe
90 lines
3.2 KiB
TypeScript
90 lines
3.2 KiB
TypeScript
import { clientMap } from '@/lib/api/client-map';
|
||
|
||
export const voiceboxApi = {
|
||
getProfiles: async () => {
|
||
try {
|
||
const response = await clientMap.core.get('/voicebox/profiles');
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error('Error fetching VoiceBox profiles:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
|
||
generateSpeech: async (text: string, profileId: string, options: { language?: string, engine?: string, modelSize?: string, instruct?: string, seed?: number } = {}): Promise<Blob> => {
|
||
try {
|
||
const response = await clientMap.core.post(
|
||
'/voicebox/generate',
|
||
{ text, profileId, language: options.language || 'tr', engine: options.engine, modelSize: options.modelSize, instruct: options.instruct, seed: options.seed },
|
||
{ responseType: 'blob' }
|
||
);
|
||
|
||
const data = response.data;
|
||
// Eğer backend JSON döndüyse (hata durumu) ama responseType blob olduğu için blob olarak geldiyse
|
||
if (data instanceof Blob) {
|
||
// Content-Type kontrolü her zaman güvenilir olmayabilir (özellikle hata durumlarında interceptor'lar değiştirebilir).
|
||
// Eğer boyut çok küçükse (örneğin < 1000 byte) ve type json içeriyorsa veya hiç type yoksa kontrol et:
|
||
if (data.type.includes('application/json') || data.size < 2000) {
|
||
const textData = await data.text();
|
||
try {
|
||
const json = JSON.parse(textData);
|
||
if (!json.success) {
|
||
throw new Error(json.message || 'Ses üretilirken bir hata oluştu.');
|
||
}
|
||
} catch (e) {
|
||
// Eğer JSON parse hatası verdiyse, demek ki gerçekten küçük bir ses dosyası (ya da geçersiz json).
|
||
// Eğer orijinal hata fırlatıldıysa (Error instance'ı), onu yukarı taşı:
|
||
if (e instanceof Error && e.message !== 'Unexpected token' && !e.message.includes('JSON')) {
|
||
throw e;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return data;
|
||
} catch (error) {
|
||
console.error('Error generating VoiceBox speech:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
|
||
getHistory: async () => {
|
||
try {
|
||
const response = await clientMap.core.get('/voicebox/history');
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error('Error fetching VoiceBox history:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
|
||
deleteHistory: async (id: string) => {
|
||
try {
|
||
const response = await clientMap.core.delete(`/voicebox/history/${id}`);
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error(`Error deleting VoiceBox history for ${id}:`, error);
|
||
throw error;
|
||
}
|
||
},
|
||
|
||
getAudioUrl: (generationId: string) => {
|
||
// API endpoint for `<audio src="...">` tag
|
||
return `${process.env.NEXT_PUBLIC_CORE_API_URL || 'http://localhost:3000/api'}/voicebox/audio/${generationId}`;
|
||
},
|
||
|
||
speak: async (text: string, profile: string, personality: boolean = false) => {
|
||
try {
|
||
const response = await clientMap.core.post('/voicebox/speak', {
|
||
text,
|
||
profile,
|
||
personality,
|
||
});
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error('Error triggering VoiceBox speak:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
};
|