UNPKG

@devvai/devv-code-backend

Version:

Backend SDK for Devv Code - Provides authentication, data management, email and AI capabilities

116 lines (115 loc) 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DevvElevenLabs = void 0; const device_1 = require("./device"); const session_1 = require("./session"); const constants_1 = require("./constants"); /** * DevvElevenLabs - Complete ElevenLabs API integration * * Provides professional Text-to-Speech and Speech-to-Text capabilities * using ElevenLabs services. API key is handled automatically by the backend. * * Features: * - Text-to-Speech: Convert text to natural-sounding speech * - Speech-to-Text: Transcribe audio with word-level timestamps * * Usage: * ```typescript * const elevenlabs = new DevvElevenLabs(); * * // Text to Speech * const ttsResult = await elevenlabs.textToSpeech({ * text: "Hello, world!" * }); * * // Speech to Text (with URL) * const sttResult = await elevenlabs.speechToText({ * audio_url: "https://example.com/audio.mp3" * }); * * // Speech to Text (with file upload using Devv upload service) * import { upload } from '@devvai/devv-code-backend'; * * const audioFile = new File([audioBlob], "recording.mp3"); * const uploadResult = await upload.uploadFile(audioFile); * if (!upload.isErrorResponse(uploadResult)) { * const sttResult = await elevenlabs.speechToText({ * audio_url: uploadResult.link * }); * } * ``` * * Note: Authentication required. Make sure you are logged in via auth.verifyOTP() * For file uploads, use the Devv upload service (upload.uploadFile) to get audio URLs. * For detailed documentation, see: https://elevenlabs.io/docs */ class DevvElevenLabs { /** * Convert text to speech using ElevenLabs * @param options Text-to-speech configuration options * @returns Audio file URL and metadata */ async textToSpeech(options) { if (!options.text) { throw new Error('Text is required for text-to-speech conversion'); } const deviceId = (0, device_1.getEncryptedDeviceId)(); const sid = (0, session_1.getSid)(); const headers = { 'Content-Type': 'application/json', 'Device-Id': deviceId }; if (sid) { headers['sid'] = sid; } const response = await fetch(`${constants_1.BASE_URL}api/v1/text-to-speech`, { method: 'POST', headers, body: JSON.stringify(options) }); if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Text-to-speech conversion failed' })); // Handle authentication errors if (response.status === 401 || response.status === 403) { throw new Error('Authentication error. Please ensure you are logged in.'); } throw new Error(error.error || `Failed to convert text to speech (Status: ${response.status})`); } return await response.json(); } /** * Transcribe audio to text using ElevenLabs * @param options Speech-to-text configuration options * @returns Transcription with word-level timing information */ async speechToText(options) { if (!options.audio_url) { throw new Error('Audio URL is required for speech-to-text conversion'); } const deviceId = (0, device_1.getEncryptedDeviceId)(); const sid = (0, session_1.getSid)(); const headers = { 'Content-Type': 'application/json', 'Device-Id': deviceId }; if (sid) { headers['sid'] = sid; } const response = await fetch(`${constants_1.BASE_URL}api/v1/speech-to-text`, { method: 'POST', headers, body: JSON.stringify(options) }); if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Speech-to-text conversion failed' })); // Handle authentication errors if (response.status === 401 || response.status === 403) { throw new Error('Authentication error. Please ensure you are logged in.'); } throw new Error(error.error || `Failed to convert speech to text (Status: ${response.status})`); } return await response.json(); } } exports.DevvElevenLabs = DevvElevenLabs;